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