1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * ChromeOS EC sensor hub
4  *
5  * Copyright (C) 2016 Google, Inc
6  */
7 
8 #ifndef __CROS_EC_SENSORS_CORE_H
9 #define __CROS_EC_SENSORS_CORE_H
10 
11 #include <linux/iio/iio.h>
12 #include <linux/irqreturn.h>
13 #include <linux/platform_data/cros_ec_commands.h>
14 #include <linux/platform_data/cros_ec_proto.h>
15 
16 enum {
17 	CROS_EC_SENSOR_X,
18 	CROS_EC_SENSOR_Y,
19 	CROS_EC_SENSOR_Z,
20 	CROS_EC_SENSOR_MAX_AXIS,
21 };
22 
23 /* EC returns sensor values using signed 16 bit registers */
24 #define CROS_EC_SENSOR_BITS 16
25 
26 /*
27  * 4 16 bit channels are allowed.
28  * Good enough for current sensors, they use up to 3 16 bit vectors.
29  */
30 #define CROS_EC_SAMPLE_SIZE  (sizeof(s64) * 2)
31 
32 /* Minimum sampling period to use when device is suspending */
33 #define CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY 1000  /* 1 second */
34 
35 /**
36  * struct cros_ec_sensors_core_state - state data for EC sensors IIO driver
37  * @ec:				cros EC device structure
38  * @cmd_lock:			lock used to prevent simultaneous access to the
39  *				commands.
40  * @msg:			cros EC command structure
41  * @param:			motion sensor parameters structure
42  * @resp:			motion sensor response structure
43  * @type:			type of motion sensor
44  * @loc:			location where the motion sensor is placed
45  * @calib:			calibration parameters. Note that trigger
46  *				captured data will always provide the calibrated
47  *				data
48  * @samples:			static array to hold data from a single capture.
49  *				For each channel we need 2 bytes, except for
50  *				the timestamp. The timestamp is always last and
51  *				is always 8-byte aligned.
52  * @read_ec_sensors_data:	function used for accessing sensors values
53  * @cuur_sampl_freq:		current sampling period
54  */
55 struct cros_ec_sensors_core_state {
56 	struct cros_ec_device *ec;
57 	struct mutex cmd_lock;
58 
59 	struct cros_ec_command *msg;
60 	struct ec_params_motion_sense param;
61 	struct ec_response_motion_sense *resp;
62 
63 	enum motionsensor_type type;
64 	enum motionsensor_location loc;
65 
66 	struct calib_data {
67 		s16 offset;
68 		u16 scale;
69 	} calib[CROS_EC_SENSOR_MAX_AXIS];
70 	s8 sign[CROS_EC_SENSOR_MAX_AXIS];
71 	u8 samples[CROS_EC_SAMPLE_SIZE];
72 
73 	int (*read_ec_sensors_data)(struct iio_dev *indio_dev,
74 				    unsigned long scan_mask, s16 *data);
75 
76 	int curr_sampl_freq;
77 
78 	/* Table of known available frequencies : 0, Min and Max in mHz */
79 	int frequencies[3];
80 };
81 
82 /**
83  * cros_ec_sensors_read_lpc() - retrieve data from EC shared memory
84  * @indio_dev:	pointer to IIO device
85  * @scan_mask:	bitmap of the sensor indices to scan
86  * @data:	location to store data
87  *
88  * This is the safe function for reading the EC data. It guarantees that the
89  * data sampled was not modified by the EC while being read.
90  *
91  * Return: 0 on success, -errno on failure.
92  */
93 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev, unsigned long scan_mask,
94 			     s16 *data);
95 
96 /**
97  * cros_ec_sensors_read_cmd() - retrieve data using the EC command protocol
98  * @indio_dev:	pointer to IIO device
99  * @scan_mask:	bitmap of the sensor indices to scan
100  * @data:	location to store data
101  *
102  * Return: 0 on success, -errno on failure.
103  */
104 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev, unsigned long scan_mask,
105 			     s16 *data);
106 
107 struct platform_device;
108 /**
109  * cros_ec_sensors_core_init() - basic initialization of the core structure
110  * @pdev:		platform device created for the sensors
111  * @indio_dev:		iio device structure of the device
112  * @physical_device:	true if the device refers to a physical device
113  *
114  * Return: 0 on success, -errno on failure.
115  */
116 int cros_ec_sensors_core_init(struct platform_device *pdev,
117 			      struct iio_dev *indio_dev, bool physical_device);
118 
119 /**
120  * cros_ec_sensors_capture() - the trigger handler function
121  * @irq:	the interrupt number.
122  * @p:		a pointer to the poll function.
123  *
124  * On a trigger event occurring, if the pollfunc is attached then this
125  * handler is called as a threaded interrupt (and hence may sleep). It
126  * is responsible for grabbing data from the device and pushing it into
127  * the associated buffer.
128  *
129  * Return: IRQ_HANDLED
130  */
131 irqreturn_t cros_ec_sensors_capture(int irq, void *p);
132 
133 /**
134  * cros_ec_motion_send_host_cmd() - send motion sense host command
135  * @st:		pointer to state information for device
136  * @opt_length:	optional length to reduce the response size, useful on the data
137  *		path. Otherwise, the maximal allowed response size is used
138  *
139  * When called, the sub-command is assumed to be set in param->cmd.
140  *
141  * Return: 0 on success, -errno on failure.
142  */
143 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *st,
144 				 u16 opt_length);
145 
146 /**
147  * cros_ec_sensors_core_read() - function to request a value from the sensor
148  * @st:		pointer to state information for device
149  * @chan:	channel specification structure table
150  * @val:	will contain one element making up the returned value
151  * @val2:	will contain another element making up the returned value
152  * @mask:	specifies which values to be requested
153  *
154  * Return:	the type of value returned by the device
155  */
156 int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
157 			      struct iio_chan_spec const *chan,
158 			      int *val, int *val2, long mask);
159 
160 /**
161  * cros_ec_sensors_core_read_avail() - get available values
162  * @indio_dev:		pointer to state information for device
163  * @chan:	channel specification structure table
164  * @vals:	list of available values
165  * @type:	type of data returned
166  * @length:	number of data returned in the array
167  * @mask:	specifies which values to be requested
168  *
169  * Return:	an error code, IIO_AVAIL_RANGE or IIO_AVAIL_LIST
170  */
171 int cros_ec_sensors_core_read_avail(struct iio_dev *indio_dev,
172 				    struct iio_chan_spec const *chan,
173 				    const int **vals,
174 				    int *type,
175 				    int *length,
176 				    long mask);
177 
178 /**
179  * cros_ec_sensors_core_write() - function to write a value to the sensor
180  * @st:		pointer to state information for device
181  * @chan:	channel specification structure table
182  * @val:	first part of value to write
183  * @val2:	second part of value to write
184  * @mask:	specifies which values to write
185  *
186  * Return:	the type of value returned by the device
187  */
188 int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
189 			       struct iio_chan_spec const *chan,
190 			       int val, int val2, long mask);
191 
192 extern const struct dev_pm_ops cros_ec_sensors_pm_ops;
193 
194 /* List of extended channel specification for all sensors */
195 extern const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[];
196 
197 #endif  /* __CROS_EC_SENSORS_CORE_H */
198