1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * ChromeOS EC sensor hub
5 * Copyright (C) 2016 Google, Inc
8 #ifndef __CROS_EC_SENSORS_CORE_H
9 #define __CROS_EC_SENSORS_CORE_H
11 #include <linux/iio/iio.h>
12 #include <linux/irqreturn.h>
13 #include <linux/mfd/cros_ec.h>
19 CROS_EC_SENSOR_MAX_AXIS,
22 /* EC returns sensor values using signed 16 bit registers */
23 #define CROS_EC_SENSOR_BITS 16
26 * 4 16 bit channels are allowed.
27 * Good enough for current sensors, they use up to 3 16 bit vectors.
29 #define CROS_EC_SAMPLE_SIZE (sizeof(s64) * 2)
31 /* Minimum sampling period to use when device is suspending */
32 #define CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY 1000 /* 1 second */
35 * struct cros_ec_sensors_core_state - state data for EC sensors IIO driver
36 * @ec: cros EC device structure
37 * @cmd_lock: lock used to prevent simultaneous access to the
39 * @msg: cros EC command structure
40 * @param: motion sensor parameters structure
41 * @resp: motion sensor response structure
42 * @type: type of motion sensor
43 * @loc: location where the motion sensor is placed
44 * @calib: calibration parameters. Note that trigger
45 * captured data will always provide the calibrated
47 * @samples: static array to hold data from a single capture.
48 * For each channel we need 2 bytes, except for
49 * the timestamp. The timestamp is always last and
50 * is always 8-byte aligned.
51 * @read_ec_sensors_data: function used for accessing sensors values
52 * @cuur_sampl_freq: current sampling period
54 struct cros_ec_sensors_core_state {
55 struct cros_ec_device *ec;
56 struct mutex cmd_lock;
58 struct cros_ec_command *msg;
59 struct ec_params_motion_sense param;
60 struct ec_response_motion_sense *resp;
62 enum motionsensor_type type;
63 enum motionsensor_location loc;
65 s16 calib[CROS_EC_SENSOR_MAX_AXIS];
67 u8 samples[CROS_EC_SAMPLE_SIZE];
69 int (*read_ec_sensors_data)(struct iio_dev *indio_dev,
70 unsigned long scan_mask, s16 *data);
76 * cros_ec_sensors_read_lpc() - retrieve data from EC shared memory
77 * @indio_dev: pointer to IIO device
78 * @scan_mask: bitmap of the sensor indices to scan
79 * @data: location to store data
81 * This is the safe function for reading the EC data. It guarantees that the
82 * data sampled was not modified by the EC while being read.
84 * Return: 0 on success, -errno on failure.
86 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev, unsigned long scan_mask,
90 * cros_ec_sensors_read_cmd() - retrieve data using the EC command protocol
91 * @indio_dev: pointer to IIO device
92 * @scan_mask: bitmap of the sensor indices to scan
93 * @data: location to store data
95 * Return: 0 on success, -errno on failure.
97 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev, unsigned long scan_mask,
100 struct platform_device;
102 * cros_ec_sensors_core_init() - basic initialization of the core structure
103 * @pdev: platform device created for the sensors
104 * @indio_dev: iio device structure of the device
105 * @physical_device: true if the device refers to a physical device
107 * Return: 0 on success, -errno on failure.
109 int cros_ec_sensors_core_init(struct platform_device *pdev,
110 struct iio_dev *indio_dev, bool physical_device);
113 * cros_ec_sensors_capture() - the trigger handler function
114 * @irq: the interrupt number.
115 * @p: a pointer to the poll function.
117 * On a trigger event occurring, if the pollfunc is attached then this
118 * handler is called as a threaded interrupt (and hence may sleep). It
119 * is responsible for grabbing data from the device and pushing it into
120 * the associated buffer.
122 * Return: IRQ_HANDLED
124 irqreturn_t cros_ec_sensors_capture(int irq, void *p);
127 * cros_ec_motion_send_host_cmd() - send motion sense host command
128 * @st: pointer to state information for device
129 * @opt_length: optional length to reduce the response size, useful on the data
130 * path. Otherwise, the maximal allowed response size is used
132 * When called, the sub-command is assumed to be set in param->cmd.
134 * Return: 0 on success, -errno on failure.
136 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *st,
140 * cros_ec_sensors_core_read() - function to request a value from the sensor
141 * @st: pointer to state information for device
142 * @chan: channel specification structure table
143 * @val: will contain one element making up the returned value
144 * @val2: will contain another element making up the returned value
145 * @mask: specifies which values to be requested
147 * Return: the type of value returned by the device
149 int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
150 struct iio_chan_spec const *chan,
151 int *val, int *val2, long mask);
154 * cros_ec_sensors_core_write() - function to write a value to the sensor
155 * @st: pointer to state information for device
156 * @chan: channel specification structure table
157 * @val: first part of value to write
158 * @val2: second part of value to write
159 * @mask: specifies which values to write
161 * Return: the type of value returned by the device
163 int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
164 struct iio_chan_spec const *chan,
165 int val, int val2, long mask);
167 extern const struct dev_pm_ops cros_ec_sensors_pm_ops;
169 /* List of extended channel specification for all sensors */
170 extern const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[];
172 #endif /* __CROS_EC_SENSORS_CORE_H */