GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / iio / common / cros_ec_sensors / cros_ec_sensors_core.c
1 /*
2  * cros_ec_sensors_core - Common function for Chrome OS EC sensor driver.
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 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/kfifo_buf.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/kernel.h>
23 #include <linux/mfd/cros_ec.h>
24 #include <linux/mfd/cros_ec_commands.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/sysfs.h>
28 #include <linux/platform_device.h>
29
30 #include "cros_ec_sensors_core.h"
31
32 static char *cros_ec_loc[] = {
33         [MOTIONSENSE_LOC_BASE] = "base",
34         [MOTIONSENSE_LOC_LID] = "lid",
35         [MOTIONSENSE_LOC_MAX] = "unknown",
36 };
37
38 int cros_ec_sensors_core_init(struct platform_device *pdev,
39                               struct iio_dev *indio_dev,
40                               bool physical_device)
41 {
42         struct device *dev = &pdev->dev;
43         struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
44         struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
45         struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
46
47         platform_set_drvdata(pdev, indio_dev);
48
49         state->ec = ec->ec_dev;
50         state->msg = devm_kzalloc(&pdev->dev,
51                                 max((u16)sizeof(struct ec_params_motion_sense),
52                                 state->ec->max_response), GFP_KERNEL);
53         if (!state->msg)
54                 return -ENOMEM;
55
56         state->resp = (struct ec_response_motion_sense *)state->msg->data;
57
58         mutex_init(&state->cmd_lock);
59
60         /* Set up the host command structure. */
61         state->msg->version = 2;
62         state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
63         state->msg->outsize = sizeof(struct ec_params_motion_sense);
64
65         indio_dev->dev.parent = &pdev->dev;
66         indio_dev->name = pdev->name;
67
68         if (physical_device) {
69                 indio_dev->modes = INDIO_DIRECT_MODE;
70
71                 state->param.cmd = MOTIONSENSE_CMD_INFO;
72                 state->param.info.sensor_num = sensor_platform->sensor_num;
73                 if (cros_ec_motion_send_host_cmd(state, 0)) {
74                         dev_warn(dev, "Can not access sensor info\n");
75                         return -EIO;
76                 }
77                 state->type = state->resp->info.type;
78                 state->loc = state->resp->info.location;
79         }
80
81         return 0;
82 }
83 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_init);
84
85 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *state,
86                                  u16 opt_length)
87 {
88         int ret;
89
90         if (opt_length)
91                 state->msg->insize = min(opt_length, state->ec->max_response);
92         else
93                 state->msg->insize = state->ec->max_response;
94
95         memcpy(state->msg->data, &state->param, sizeof(state->param));
96
97         ret = cros_ec_cmd_xfer_status(state->ec, state->msg);
98         if (ret < 0)
99                 return -EIO;
100
101         if (ret &&
102             state->resp != (struct ec_response_motion_sense *)state->msg->data)
103                 memcpy(state->resp, state->msg->data, ret);
104
105         return 0;
106 }
107 EXPORT_SYMBOL_GPL(cros_ec_motion_send_host_cmd);
108
109 static ssize_t cros_ec_sensors_calibrate(struct iio_dev *indio_dev,
110                 uintptr_t private, const struct iio_chan_spec *chan,
111                 const char *buf, size_t len)
112 {
113         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
114         int ret, i;
115         bool calibrate;
116
117         ret = strtobool(buf, &calibrate);
118         if (ret < 0)
119                 return ret;
120         if (!calibrate)
121                 return -EINVAL;
122
123         mutex_lock(&st->cmd_lock);
124         st->param.cmd = MOTIONSENSE_CMD_PERFORM_CALIB;
125         ret = cros_ec_motion_send_host_cmd(st, 0);
126         if (ret != 0) {
127                 dev_warn(&indio_dev->dev, "Unable to calibrate sensor\n");
128         } else {
129                 /* Save values */
130                 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
131                         st->calib[i] = st->resp->perform_calib.offset[i];
132         }
133         mutex_unlock(&st->cmd_lock);
134
135         return ret ? ret : len;
136 }
137
138 static ssize_t cros_ec_sensors_loc(struct iio_dev *indio_dev,
139                 uintptr_t private, const struct iio_chan_spec *chan,
140                 char *buf)
141 {
142         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
143
144         return snprintf(buf, PAGE_SIZE, "%s\n", cros_ec_loc[st->loc]);
145 }
146
147 const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = {
148         {
149                 .name = "calibrate",
150                 .shared = IIO_SHARED_BY_ALL,
151                 .write = cros_ec_sensors_calibrate
152         },
153         {
154                 .name = "location",
155                 .shared = IIO_SHARED_BY_ALL,
156                 .read = cros_ec_sensors_loc
157         },
158         { },
159 };
160 EXPORT_SYMBOL_GPL(cros_ec_sensors_ext_info);
161
162 /**
163  * cros_ec_sensors_idx_to_reg - convert index into offset in shared memory
164  * @st:         pointer to state information for device
165  * @idx:        sensor index (should be element of enum sensor_index)
166  *
167  * Return:      address to read at
168  */
169 static unsigned int cros_ec_sensors_idx_to_reg(
170                                         struct cros_ec_sensors_core_state *st,
171                                         unsigned int idx)
172 {
173         /*
174          * When using LPC interface, only space for 2 Accel and one Gyro.
175          * First halfword of MOTIONSENSE_TYPE_ACCEL is used by angle.
176          */
177         if (st->type == MOTIONSENSE_TYPE_ACCEL)
178                 return EC_MEMMAP_ACC_DATA + sizeof(u16) *
179                         (1 + idx + st->param.info.sensor_num *
180                          CROS_EC_SENSOR_MAX_AXIS);
181
182         return EC_MEMMAP_GYRO_DATA + sizeof(u16) * idx;
183 }
184
185 static int cros_ec_sensors_cmd_read_u8(struct cros_ec_device *ec,
186                                        unsigned int offset, u8 *dest)
187 {
188         return ec->cmd_readmem(ec, offset, 1, dest);
189 }
190
191 static int cros_ec_sensors_cmd_read_u16(struct cros_ec_device *ec,
192                                          unsigned int offset, u16 *dest)
193 {
194         __le16 tmp;
195         int ret = ec->cmd_readmem(ec, offset, 2, &tmp);
196
197         if (ret >= 0)
198                 *dest = le16_to_cpu(tmp);
199
200         return ret;
201 }
202
203 /**
204  * cros_ec_sensors_read_until_not_busy() - read until is not busy
205  *
206  * @st: pointer to state information for device
207  *
208  * Read from EC status byte until it reads not busy.
209  * Return: 8-bit status if ok, -errno on failure.
210  */
211 static int cros_ec_sensors_read_until_not_busy(
212                                         struct cros_ec_sensors_core_state *st)
213 {
214         struct cros_ec_device *ec = st->ec;
215         u8 status;
216         int ret, attempts = 0;
217
218         ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, &status);
219         if (ret < 0)
220                 return ret;
221
222         while (status & EC_MEMMAP_ACC_STATUS_BUSY_BIT) {
223                 /* Give up after enough attempts, return error. */
224                 if (attempts++ >= 50)
225                         return -EIO;
226
227                 /* Small delay every so often. */
228                 if (attempts % 5 == 0)
229                         msleep(25);
230
231                 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
232                                                   &status);
233                 if (ret < 0)
234                         return ret;
235         }
236
237         return status;
238 }
239
240 /**
241  * read_ec_sensors_data_unsafe() - read acceleration data from EC shared memory
242  * @indio_dev:  pointer to IIO device
243  * @scan_mask:  bitmap of the sensor indices to scan
244  * @data:       location to store data
245  *
246  * This is the unsafe function for reading the EC data. It does not guarantee
247  * that the EC will not modify the data as it is being read in.
248  *
249  * Return: 0 on success, -errno on failure.
250  */
251 static int cros_ec_sensors_read_data_unsafe(struct iio_dev *indio_dev,
252                          unsigned long scan_mask, s16 *data)
253 {
254         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
255         struct cros_ec_device *ec = st->ec;
256         unsigned int i;
257         int ret;
258
259         /* Read all sensors enabled in scan_mask. Each value is 2 bytes. */
260         for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
261                 ret = cros_ec_sensors_cmd_read_u16(ec,
262                                              cros_ec_sensors_idx_to_reg(st, i),
263                                              data);
264                 if (ret < 0)
265                         return ret;
266
267                 data++;
268         }
269
270         return 0;
271 }
272
273 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev,
274                              unsigned long scan_mask, s16 *data)
275 {
276         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
277         struct cros_ec_device *ec = st->ec;
278         u8 samp_id = 0xff, status = 0;
279         int ret, attempts = 0;
280
281         /*
282          * Continually read all data from EC until the status byte after
283          * all reads reflects that the EC is not busy and the sample id
284          * matches the sample id from before all reads. This guarantees
285          * that data read in was not modified by the EC while reading.
286          */
287         while ((status & (EC_MEMMAP_ACC_STATUS_BUSY_BIT |
288                           EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK)) != samp_id) {
289                 /* If we have tried to read too many times, return error. */
290                 if (attempts++ >= 5)
291                         return -EIO;
292
293                 /* Read status byte until EC is not busy. */
294                 ret = cros_ec_sensors_read_until_not_busy(st);
295                 if (ret < 0)
296                         return ret;
297
298                 /*
299                  * Store the current sample id so that we can compare to the
300                  * sample id after reading the data.
301                  */
302                 samp_id = ret & EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK;
303
304                 /* Read all EC data, format it, and store it into data. */
305                 ret = cros_ec_sensors_read_data_unsafe(indio_dev, scan_mask,
306                                                        data);
307                 if (ret < 0)
308                         return ret;
309
310                 /* Read status byte. */
311                 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
312                                                   &status);
313                 if (ret < 0)
314                         return ret;
315         }
316
317         return 0;
318 }
319 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_lpc);
320
321 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev,
322                              unsigned long scan_mask, s16 *data)
323 {
324         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
325         int ret;
326         unsigned int i;
327
328         /* Read all sensor data through a command. */
329         st->param.cmd = MOTIONSENSE_CMD_DATA;
330         ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->data));
331         if (ret != 0) {
332                 dev_warn(&indio_dev->dev, "Unable to read sensor data\n");
333                 return ret;
334         }
335
336         for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
337                 *data = st->resp->data.data[i];
338                 data++;
339         }
340
341         return 0;
342 }
343 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_cmd);
344
345 irqreturn_t cros_ec_sensors_capture(int irq, void *p)
346 {
347         struct iio_poll_func *pf = p;
348         struct iio_dev *indio_dev = pf->indio_dev;
349         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
350         int ret;
351
352         mutex_lock(&st->cmd_lock);
353
354         /* Clear capture data. */
355         memset(st->samples, 0, indio_dev->scan_bytes);
356
357         /* Read data based on which channels are enabled in scan mask. */
358         ret = st->read_ec_sensors_data(indio_dev,
359                                        *(indio_dev->active_scan_mask),
360                                        (s16 *)st->samples);
361         if (ret < 0)
362                 goto done;
363
364         iio_push_to_buffers_with_timestamp(indio_dev, st->samples,
365                                            iio_get_time_ns(indio_dev));
366
367 done:
368         /*
369          * Tell the core we are done with this trigger and ready for the
370          * next one.
371          */
372         iio_trigger_notify_done(indio_dev->trig);
373
374         mutex_unlock(&st->cmd_lock);
375
376         return IRQ_HANDLED;
377 }
378 EXPORT_SYMBOL_GPL(cros_ec_sensors_capture);
379
380 int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
381                           struct iio_chan_spec const *chan,
382                           int *val, int *val2, long mask)
383 {
384         int ret = IIO_VAL_INT;
385
386         switch (mask) {
387         case IIO_CHAN_INFO_SAMP_FREQ:
388                 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
389                 st->param.ec_rate.data =
390                         EC_MOTION_SENSE_NO_VALUE;
391
392                 if (cros_ec_motion_send_host_cmd(st, 0))
393                         ret = -EIO;
394                 else
395                         *val = st->resp->ec_rate.ret;
396                 break;
397         case IIO_CHAN_INFO_FREQUENCY:
398                 st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
399                 st->param.sensor_odr.data =
400                         EC_MOTION_SENSE_NO_VALUE;
401
402                 if (cros_ec_motion_send_host_cmd(st, 0))
403                         ret = -EIO;
404                 else
405                         *val = st->resp->sensor_odr.ret;
406                 break;
407         default:
408                 break;
409         }
410
411         return ret;
412 }
413 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read);
414
415 int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
416                                struct iio_chan_spec const *chan,
417                                int val, int val2, long mask)
418 {
419         int ret = 0;
420
421         switch (mask) {
422         case IIO_CHAN_INFO_FREQUENCY:
423                 st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
424                 st->param.sensor_odr.data = val;
425
426                 /* Always roundup, so caller gets at least what it asks for. */
427                 st->param.sensor_odr.roundup = 1;
428
429                 if (cros_ec_motion_send_host_cmd(st, 0))
430                         ret = -EIO;
431                 break;
432         case IIO_CHAN_INFO_SAMP_FREQ:
433                 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
434                 st->param.ec_rate.data = val;
435
436                 if (cros_ec_motion_send_host_cmd(st, 0))
437                         ret = -EIO;
438                 else
439                         st->curr_sampl_freq = val;
440                 break;
441         default:
442                 ret = -EINVAL;
443                 break;
444         }
445         return ret;
446 }
447 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_write);
448
449 MODULE_DESCRIPTION("ChromeOS EC sensor hub core functions");
450 MODULE_LICENSE("GPL v2");