GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / iio / common / cros_ec_sensors / cros_ec_sensors_core.h
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/irqreturn.h>
20
21 enum {
22         CROS_EC_SENSOR_X,
23         CROS_EC_SENSOR_Y,
24         CROS_EC_SENSOR_Z,
25         CROS_EC_SENSOR_MAX_AXIS,
26 };
27
28 /* EC returns sensor values using signed 16 bit registers */
29 #define CROS_EC_SENSOR_BITS 16
30
31 /*
32  * 4 16 bit channels are allowed.
33  * Good enough for current sensors, they use up to 3 16 bit vectors.
34  */
35 #define CROS_EC_SAMPLE_SIZE  (sizeof(s64) * 2)
36
37 /* Minimum sampling period to use when device is suspending */
38 #define CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY 1000  /* 1 second */
39
40 /**
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
44  *                              commands.
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
52  *                              data
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
59  */
60 struct cros_ec_sensors_core_state {
61         struct cros_ec_device *ec;
62         struct mutex cmd_lock;
63
64         struct cros_ec_command *msg;
65         struct ec_params_motion_sense param;
66         struct ec_response_motion_sense *resp;
67
68         enum motionsensor_type type;
69         enum motionsensor_location loc;
70
71         s16 calib[CROS_EC_SENSOR_MAX_AXIS];
72
73         u8 samples[CROS_EC_SAMPLE_SIZE];
74
75         int (*read_ec_sensors_data)(struct iio_dev *indio_dev,
76                                     unsigned long scan_mask, s16 *data);
77
78         int curr_sampl_freq;
79 };
80
81 /**
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
86  *
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.
89  *
90  * Return: 0 on success, -errno on failure.
91  */
92 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev, unsigned long scan_mask,
93                              s16 *data);
94
95 /**
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
100  *
101  * Return: 0 on success, -errno on failure.
102  */
103 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev, unsigned long scan_mask,
104                              s16 *data);
105
106 /**
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
111  *
112  * Return: 0 on success, -errno on failure.
113  */
114 int cros_ec_sensors_core_init(struct platform_device *pdev,
115                               struct iio_dev *indio_dev, bool physical_device);
116
117 /**
118  * cros_ec_sensors_capture() - the trigger handler function
119  * @irq:        the interrupt number.
120  * @p:          a pointer to the poll function.
121  *
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.
126  *
127  * Return: IRQ_HANDLED
128  */
129 irqreturn_t cros_ec_sensors_capture(int irq, void *p);
130
131 /**
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
136  *
137  * When called, the sub-command is assumed to be set in param->cmd.
138  *
139  * Return: 0 on success, -errno on failure.
140  */
141 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *st,
142                                  u16 opt_length);
143
144 /**
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
151  *
152  * Return:      the type of value returned by the device
153  */
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);
157
158 /**
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
165  *
166  * Return:      the type of value returned by the device
167  */
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);
171
172 /* List of extended channel specification for all sensors */
173 extern const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[];
174
175 #endif  /* __CROS_EC_SENSORS_CORE_H */