iio: cros_ec: Switch to SPDX identifier.
[platform/kernel/linux-rpi.git] / drivers / iio / common / cros_ec_sensors / cros_ec_sensors.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * cros_ec_sensors - Driver for Chrome OS Embedded Controller sensors.
4  *
5  * Copyright (C) 2016 Google, Inc
6  *
7  * This driver uses the cros-ec interface to communicate with the Chrome OS
8  * EC about sensors data. Data access is presented through iio sysfs.
9  */
10
11 #include <linux/device.h>
12 #include <linux/iio/buffer.h>
13 #include <linux/iio/common/cros_ec_sensors_core.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/kfifo_buf.h>
16 #include <linux/iio/trigger_consumer.h>
17 #include <linux/iio/triggered_buffer.h>
18 #include <linux/kernel.h>
19 #include <linux/mfd/cros_ec.h>
20 #include <linux/mfd/cros_ec_commands.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24
25 #define CROS_EC_SENSORS_MAX_CHANNELS 4
26
27 /* State data for ec_sensors iio driver. */
28 struct cros_ec_sensors_state {
29         /* Shared by all sensors */
30         struct cros_ec_sensors_core_state core;
31
32         struct iio_chan_spec channels[CROS_EC_SENSORS_MAX_CHANNELS];
33 };
34
35 static int cros_ec_sensors_read(struct iio_dev *indio_dev,
36                           struct iio_chan_spec const *chan,
37                           int *val, int *val2, long mask)
38 {
39         struct cros_ec_sensors_state *st = iio_priv(indio_dev);
40         s16 data = 0;
41         s64 val64;
42         int i;
43         int ret;
44         int idx = chan->scan_index;
45
46         mutex_lock(&st->core.cmd_lock);
47
48         switch (mask) {
49         case IIO_CHAN_INFO_RAW:
50                 ret = st->core.read_ec_sensors_data(indio_dev, 1 << idx, &data);
51                 if (ret < 0)
52                         break;
53                 ret = IIO_VAL_INT;
54                 *val = data;
55                 break;
56         case IIO_CHAN_INFO_CALIBBIAS:
57                 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET;
58                 st->core.param.sensor_offset.flags = 0;
59
60                 ret = cros_ec_motion_send_host_cmd(&st->core, 0);
61                 if (ret < 0)
62                         break;
63
64                 /* Save values */
65                 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
66                         st->core.calib[i] =
67                                 st->core.resp->sensor_offset.offset[i];
68                 ret = IIO_VAL_INT;
69                 *val = st->core.calib[idx];
70                 break;
71         case IIO_CHAN_INFO_SCALE:
72                 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
73                 st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE;
74
75                 ret = cros_ec_motion_send_host_cmd(&st->core, 0);
76                 if (ret < 0)
77                         break;
78
79                 val64 = st->core.resp->sensor_range.ret;
80                 switch (st->core.type) {
81                 case MOTIONSENSE_TYPE_ACCEL:
82                         /*
83                          * EC returns data in g, iio exepects m/s^2.
84                          * Do not use IIO_G_TO_M_S_2 to avoid precision loss.
85                          */
86                         *val = div_s64(val64 * 980665, 10);
87                         *val2 = 10000 << (CROS_EC_SENSOR_BITS - 1);
88                         ret = IIO_VAL_FRACTIONAL;
89                         break;
90                 case MOTIONSENSE_TYPE_GYRO:
91                         /*
92                          * EC returns data in dps, iio expects rad/s.
93                          * Do not use IIO_DEGREE_TO_RAD to avoid precision
94                          * loss. Round to the nearest integer.
95                          */
96                         *val = div_s64(val64 * 314159 + 9000000ULL, 1000);
97                         *val2 = 18000 << (CROS_EC_SENSOR_BITS - 1);
98                         ret = IIO_VAL_FRACTIONAL;
99                         break;
100                 case MOTIONSENSE_TYPE_MAG:
101                         /*
102                          * EC returns data in 16LSB / uT,
103                          * iio expects Gauss
104                          */
105                         *val = val64;
106                         *val2 = 100 << (CROS_EC_SENSOR_BITS - 1);
107                         ret = IIO_VAL_FRACTIONAL;
108                         break;
109                 default:
110                         ret = -EINVAL;
111                 }
112                 break;
113         default:
114                 ret = cros_ec_sensors_core_read(&st->core, chan, val, val2,
115                                                 mask);
116                 break;
117         }
118         mutex_unlock(&st->core.cmd_lock);
119
120         return ret;
121 }
122
123 static int cros_ec_sensors_write(struct iio_dev *indio_dev,
124                                struct iio_chan_spec const *chan,
125                                int val, int val2, long mask)
126 {
127         struct cros_ec_sensors_state *st = iio_priv(indio_dev);
128         int i;
129         int ret;
130         int idx = chan->scan_index;
131
132         mutex_lock(&st->core.cmd_lock);
133
134         switch (mask) {
135         case IIO_CHAN_INFO_CALIBBIAS:
136                 st->core.calib[idx] = val;
137
138                 /* Send to EC for each axis, even if not complete */
139                 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET;
140                 st->core.param.sensor_offset.flags =
141                         MOTION_SENSE_SET_OFFSET;
142                 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
143                         st->core.param.sensor_offset.offset[i] =
144                                 st->core.calib[i];
145                 st->core.param.sensor_offset.temp =
146                         EC_MOTION_SENSE_INVALID_CALIB_TEMP;
147
148                 ret = cros_ec_motion_send_host_cmd(&st->core, 0);
149                 break;
150         case IIO_CHAN_INFO_SCALE:
151                 if (st->core.type == MOTIONSENSE_TYPE_MAG) {
152                         ret = -EINVAL;
153                         break;
154                 }
155                 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
156                 st->core.param.sensor_range.data = val;
157
158                 /* Always roundup, so caller gets at least what it asks for. */
159                 st->core.param.sensor_range.roundup = 1;
160
161                 ret = cros_ec_motion_send_host_cmd(&st->core, 0);
162                 break;
163         default:
164                 ret = cros_ec_sensors_core_write(
165                                 &st->core, chan, val, val2, mask);
166                 break;
167         }
168
169         mutex_unlock(&st->core.cmd_lock);
170
171         return ret;
172 }
173
174 static const struct iio_info ec_sensors_info = {
175         .read_raw = &cros_ec_sensors_read,
176         .write_raw = &cros_ec_sensors_write,
177 };
178
179 static int cros_ec_sensors_probe(struct platform_device *pdev)
180 {
181         struct device *dev = &pdev->dev;
182         struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
183         struct iio_dev *indio_dev;
184         struct cros_ec_sensors_state *state;
185         struct iio_chan_spec *channel;
186         int ret, i;
187
188         if (!ec_dev || !ec_dev->ec_dev) {
189                 dev_warn(&pdev->dev, "No CROS EC device found.\n");
190                 return -EINVAL;
191         }
192
193         indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state));
194         if (!indio_dev)
195                 return -ENOMEM;
196
197         ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
198         if (ret)
199                 return ret;
200
201         indio_dev->info = &ec_sensors_info;
202         state = iio_priv(indio_dev);
203         for (channel = state->channels, i = CROS_EC_SENSOR_X;
204              i < CROS_EC_SENSOR_MAX_AXIS; i++, channel++) {
205                 /* Common part */
206                 channel->info_mask_separate =
207                         BIT(IIO_CHAN_INFO_RAW) |
208                         BIT(IIO_CHAN_INFO_CALIBBIAS);
209                 channel->info_mask_shared_by_all =
210                         BIT(IIO_CHAN_INFO_SCALE) |
211                         BIT(IIO_CHAN_INFO_FREQUENCY) |
212                         BIT(IIO_CHAN_INFO_SAMP_FREQ);
213                 channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
214                 channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
215                 channel->scan_index = i;
216                 channel->ext_info = cros_ec_sensors_ext_info;
217                 channel->modified = 1;
218                 channel->channel2 = IIO_MOD_X + i;
219                 channel->scan_type.sign = 's';
220
221                 /* Sensor specific */
222                 switch (state->core.type) {
223                 case MOTIONSENSE_TYPE_ACCEL:
224                         channel->type = IIO_ACCEL;
225                         break;
226                 case MOTIONSENSE_TYPE_GYRO:
227                         channel->type = IIO_ANGL_VEL;
228                         break;
229                 case MOTIONSENSE_TYPE_MAG:
230                         channel->type = IIO_MAGN;
231                         break;
232                 default:
233                         dev_err(&pdev->dev, "Unknown motion sensor\n");
234                         return -EINVAL;
235                 }
236         }
237
238         /* Timestamp */
239         channel->type = IIO_TIMESTAMP;
240         channel->channel = -1;
241         channel->scan_index = CROS_EC_SENSOR_MAX_AXIS;
242         channel->scan_type.sign = 's';
243         channel->scan_type.realbits = 64;
244         channel->scan_type.storagebits = 64;
245
246         indio_dev->channels = state->channels;
247         indio_dev->num_channels = CROS_EC_SENSORS_MAX_CHANNELS;
248
249         /* There is only enough room for accel and gyro in the io space */
250         if ((state->core.ec->cmd_readmem != NULL) &&
251             (state->core.type != MOTIONSENSE_TYPE_MAG))
252                 state->core.read_ec_sensors_data = cros_ec_sensors_read_lpc;
253         else
254                 state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
255
256         ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
257                         cros_ec_sensors_capture, NULL);
258         if (ret)
259                 return ret;
260
261         return devm_iio_device_register(dev, indio_dev);
262 }
263
264 static const struct platform_device_id cros_ec_sensors_ids[] = {
265         {
266                 .name = "cros-ec-accel",
267         },
268         {
269                 .name = "cros-ec-gyro",
270         },
271         {
272                 .name = "cros-ec-mag",
273         },
274         { /* sentinel */ }
275 };
276 MODULE_DEVICE_TABLE(platform, cros_ec_sensors_ids);
277
278 static struct platform_driver cros_ec_sensors_platform_driver = {
279         .driver = {
280                 .name   = "cros-ec-sensors",
281                 .pm     = &cros_ec_sensors_pm_ops,
282         },
283         .probe          = cros_ec_sensors_probe,
284         .id_table       = cros_ec_sensors_ids,
285 };
286 module_platform_driver(cros_ec_sensors_platform_driver);
287
288 MODULE_DESCRIPTION("ChromeOS EC 3-axis sensors driver");
289 MODULE_LICENSE("GPL v2");