2 * ChromeOS EC sensor hub
4 * Copyright (C) 2016 Google, Inc
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.
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.
16 #ifndef __CROS_EC_SENSORS_CORE_H
17 #define __CROS_EC_SENSORS_CORE_H
19 #include <linux/irqreturn.h>
25 CROS_EC_SENSOR_MAX_AXIS,
28 /* EC returns sensor values using signed 16 bit registers */
29 #define CROS_EC_SENSOR_BITS 16
32 * 4 16 bit channels are allowed.
33 * Good enough for current sensors, they use up to 3 16 bit vectors.
35 #define CROS_EC_SAMPLE_SIZE (sizeof(s64) * 2)
37 /* Minimum sampling period to use when device is suspending */
38 #define CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY 1000 /* 1 second */
41 * struct cros_ec_sensors_core_state - state data for EC sensors IIO driver
42 * @ec: cros EC device structure
43 * @cmd_lock: lock used to prevent simultaneous access to the
45 * @msg: cros EC command structure
46 * @param: motion sensor parameters structure
47 * @resp: motion sensor response structure
48 * @type: type of motion sensor
49 * @loc: location where the motion sensor is placed
50 * @calib: calibration parameters. Note that trigger
51 * captured data will always provide the calibrated
53 * @samples: static array to hold data from a single capture.
54 * For each channel we need 2 bytes, except for
55 * the timestamp. The timestamp is always last and
56 * is always 8-byte aligned.
57 * @read_ec_sensors_data: function used for accessing sensors values
58 * @cuur_sampl_freq: current sampling period
60 struct cros_ec_sensors_core_state {
61 struct cros_ec_device *ec;
62 struct mutex cmd_lock;
64 struct cros_ec_command *msg;
65 struct ec_params_motion_sense param;
66 struct ec_response_motion_sense *resp;
68 enum motionsensor_type type;
69 enum motionsensor_location loc;
71 s16 calib[CROS_EC_SENSOR_MAX_AXIS];
73 u8 samples[CROS_EC_SAMPLE_SIZE];
75 int (*read_ec_sensors_data)(struct iio_dev *indio_dev,
76 unsigned long scan_mask, s16 *data);
82 * cros_ec_sensors_read_lpc() - retrieve data from EC shared memory
83 * @indio_dev: pointer to IIO device
84 * @scan_mask: bitmap of the sensor indices to scan
85 * @data: location to store data
87 * This is the safe function for reading the EC data. It guarantees that the
88 * data sampled was not modified by the EC while being read.
90 * Return: 0 on success, -errno on failure.
92 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev, unsigned long scan_mask,
96 * cros_ec_sensors_read_cmd() - retrieve data using the EC command protocol
97 * @indio_dev: pointer to IIO device
98 * @scan_mask: bitmap of the sensor indices to scan
99 * @data: location to store data
101 * Return: 0 on success, -errno on failure.
103 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev, unsigned long scan_mask,
107 * cros_ec_sensors_core_init() - basic initialization of the core structure
108 * @pdev: platform device created for the sensors
109 * @indio_dev: iio device structure of the device
110 * @physical_device: true if the device refers to a physical device
112 * Return: 0 on success, -errno on failure.
114 int cros_ec_sensors_core_init(struct platform_device *pdev,
115 struct iio_dev *indio_dev, bool physical_device);
118 * cros_ec_sensors_capture() - the trigger handler function
119 * @irq: the interrupt number.
120 * @p: a pointer to the poll function.
122 * On a trigger event occurring, if the pollfunc is attached then this
123 * handler is called as a threaded interrupt (and hence may sleep). It
124 * is responsible for grabbing data from the device and pushing it into
125 * the associated buffer.
127 * Return: IRQ_HANDLED
129 irqreturn_t cros_ec_sensors_capture(int irq, void *p);
132 * cros_ec_motion_send_host_cmd() - send motion sense host command
133 * @st: pointer to state information for device
134 * @opt_length: optional length to reduce the response size, useful on the data
135 * path. Otherwise, the maximal allowed response size is used
137 * When called, the sub-command is assumed to be set in param->cmd.
139 * Return: 0 on success, -errno on failure.
141 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *st,
145 * cros_ec_sensors_core_read() - function to request a value from the sensor
146 * @st: pointer to state information for device
147 * @chan: channel specification structure table
148 * @val: will contain one element making up the returned value
149 * @val2: will contain another element making up the returned value
150 * @mask: specifies which values to be requested
152 * Return: the type of value returned by the device
154 int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
155 struct iio_chan_spec const *chan,
156 int *val, int *val2, long mask);
159 * cros_ec_sensors_core_write() - function to write a value to the sensor
160 * @st: pointer to state information for device
161 * @chan: channel specification structure table
162 * @val: first part of value to write
163 * @val2: second part of value to write
164 * @mask: specifies which values to write
166 * Return: the type of value returned by the device
168 int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
169 struct iio_chan_spec const *chan,
170 int val, int val2, long mask);
172 /* List of extended channel specification for all sensors */
173 extern const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[];
175 #endif /* __CROS_EC_SENSORS_CORE_H */