Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / include / linux / iio / common / cros_ec_sensors_core.h
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/mfd/cros_ec.h>
14
15 enum {
16         CROS_EC_SENSOR_X,
17         CROS_EC_SENSOR_Y,
18         CROS_EC_SENSOR_Z,
19         CROS_EC_SENSOR_MAX_AXIS,
20 };
21
22 /* EC returns sensor values using signed 16 bit registers */
23 #define CROS_EC_SENSOR_BITS 16
24
25 /*
26  * 4 16 bit channels are allowed.
27  * Good enough for current sensors, they use up to 3 16 bit vectors.
28  */
29 #define CROS_EC_SAMPLE_SIZE  (sizeof(s64) * 2)
30
31 /* Minimum sampling period to use when device is suspending */
32 #define CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY 1000  /* 1 second */
33
34 /**
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
38  *                              commands.
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
46  *                              data
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
53  */
54 struct cros_ec_sensors_core_state {
55         struct cros_ec_device *ec;
56         struct mutex cmd_lock;
57
58         struct cros_ec_command *msg;
59         struct ec_params_motion_sense param;
60         struct ec_response_motion_sense *resp;
61
62         enum motionsensor_type type;
63         enum motionsensor_location loc;
64
65         s16 calib[CROS_EC_SENSOR_MAX_AXIS];
66
67         u8 samples[CROS_EC_SAMPLE_SIZE];
68
69         int (*read_ec_sensors_data)(struct iio_dev *indio_dev,
70                                     unsigned long scan_mask, s16 *data);
71
72         int curr_sampl_freq;
73 };
74
75 /**
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
80  *
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.
83  *
84  * Return: 0 on success, -errno on failure.
85  */
86 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev, unsigned long scan_mask,
87                              s16 *data);
88
89 /**
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
94  *
95  * Return: 0 on success, -errno on failure.
96  */
97 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev, unsigned long scan_mask,
98                              s16 *data);
99
100 struct platform_device;
101 /**
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
106  *
107  * Return: 0 on success, -errno on failure.
108  */
109 int cros_ec_sensors_core_init(struct platform_device *pdev,
110                               struct iio_dev *indio_dev, bool physical_device);
111
112 /**
113  * cros_ec_sensors_capture() - the trigger handler function
114  * @irq:        the interrupt number.
115  * @p:          a pointer to the poll function.
116  *
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.
121  *
122  * Return: IRQ_HANDLED
123  */
124 irqreturn_t cros_ec_sensors_capture(int irq, void *p);
125
126 /**
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
131  *
132  * When called, the sub-command is assumed to be set in param->cmd.
133  *
134  * Return: 0 on success, -errno on failure.
135  */
136 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *st,
137                                  u16 opt_length);
138
139 /**
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
146  *
147  * Return:      the type of value returned by the device
148  */
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);
152
153 /**
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
160  *
161  * Return:      the type of value returned by the device
162  */
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);
166
167 extern const struct dev_pm_ops cros_ec_sensors_pm_ops;
168
169 /* List of extended channel specification for all sensors */
170 extern const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[];
171
172 #endif  /* __CROS_EC_SENSORS_CORE_H */