OSDN Git Service

Merge tag '5.6-rc-smb3-plugfest-patches' of git://git.samba.org/sfrench/cifs-2.6
[tomoyo/tomoyo-test1.git] / drivers / iio / common / cros_ec_sensors / cros_ec_sensors_core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * cros_ec_sensors_core - Common function for Chrome OS EC sensor driver.
4  *
5  * Copyright (C) 2016 Google, Inc
6  */
7
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/iio/buffer.h>
11 #include <linux/iio/common/cros_ec_sensors_core.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/kfifo_buf.h>
14 #include <linux/iio/trigger_consumer.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/platform_data/cros_ec_commands.h>
19 #include <linux/platform_data/cros_ec_proto.h>
20 #include <linux/platform_data/cros_ec_sensorhub.h>
21 #include <linux/platform_device.h>
22
23 static char *cros_ec_loc[] = {
24         [MOTIONSENSE_LOC_BASE] = "base",
25         [MOTIONSENSE_LOC_LID] = "lid",
26         [MOTIONSENSE_LOC_MAX] = "unknown",
27 };
28
29 static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev,
30                                              u16 cmd_offset, u16 cmd, u32 *mask)
31 {
32         int ret;
33         struct {
34                 struct cros_ec_command msg;
35                 union {
36                         struct ec_params_get_cmd_versions params;
37                         struct ec_response_get_cmd_versions resp;
38                 };
39         } __packed buf = {
40                 .msg = {
41                         .command = EC_CMD_GET_CMD_VERSIONS + cmd_offset,
42                         .insize = sizeof(struct ec_response_get_cmd_versions),
43                         .outsize = sizeof(struct ec_params_get_cmd_versions)
44                         },
45                 .params = {.cmd = cmd}
46         };
47
48         ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
49         if (ret >= 0)
50                 *mask = buf.resp.version_mask;
51         return ret;
52 }
53
54 static void get_default_min_max_freq(enum motionsensor_type type,
55                                      u32 *min_freq,
56                                      u32 *max_freq)
57 {
58         switch (type) {
59         case MOTIONSENSE_TYPE_ACCEL:
60         case MOTIONSENSE_TYPE_GYRO:
61                 *min_freq = 12500;
62                 *max_freq = 100000;
63                 break;
64         case MOTIONSENSE_TYPE_MAG:
65                 *min_freq = 5000;
66                 *max_freq = 25000;
67                 break;
68         case MOTIONSENSE_TYPE_PROX:
69         case MOTIONSENSE_TYPE_LIGHT:
70                 *min_freq = 100;
71                 *max_freq = 50000;
72                 break;
73         case MOTIONSENSE_TYPE_BARO:
74                 *min_freq = 250;
75                 *max_freq = 20000;
76                 break;
77         case MOTIONSENSE_TYPE_ACTIVITY:
78         default:
79                 *min_freq = 0;
80                 *max_freq = 0;
81                 break;
82         }
83 }
84
85 int cros_ec_sensors_core_init(struct platform_device *pdev,
86                               struct iio_dev *indio_dev,
87                               bool physical_device)
88 {
89         struct device *dev = &pdev->dev;
90         struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
91         struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
92         struct cros_ec_dev *ec = sensor_hub->ec;
93         struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
94         u32 ver_mask;
95         int ret, i;
96
97         platform_set_drvdata(pdev, indio_dev);
98
99         state->ec = ec->ec_dev;
100         state->msg = devm_kzalloc(&pdev->dev,
101                                 max((u16)sizeof(struct ec_params_motion_sense),
102                                 state->ec->max_response), GFP_KERNEL);
103         if (!state->msg)
104                 return -ENOMEM;
105
106         state->resp = (struct ec_response_motion_sense *)state->msg->data;
107
108         mutex_init(&state->cmd_lock);
109
110         ret = cros_ec_get_host_cmd_version_mask(state->ec,
111                                                 ec->cmd_offset,
112                                                 EC_CMD_MOTION_SENSE_CMD,
113                                                 &ver_mask);
114         if (ret < 0)
115                 return ret;
116
117         /* Set up the host command structure. */
118         state->msg->version = fls(ver_mask) - 1;
119         state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
120         state->msg->outsize = sizeof(struct ec_params_motion_sense);
121
122         indio_dev->dev.parent = &pdev->dev;
123         indio_dev->name = pdev->name;
124
125         if (physical_device) {
126                 indio_dev->modes = INDIO_DIRECT_MODE;
127
128                 state->param.cmd = MOTIONSENSE_CMD_INFO;
129                 state->param.info.sensor_num = sensor_platform->sensor_num;
130                 ret = cros_ec_motion_send_host_cmd(state, 0);
131                 if (ret) {
132                         dev_warn(dev, "Can not access sensor info\n");
133                         return ret;
134                 }
135                 state->type = state->resp->info.type;
136                 state->loc = state->resp->info.location;
137
138                 /* Set sign vector, only used for backward compatibility. */
139                 memset(state->sign, 1, CROS_EC_SENSOR_MAX_AXIS);
140
141                 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
142                         state->calib[i].scale = MOTION_SENSE_DEFAULT_SCALE;
143
144                 /* 0 is a correct value used to stop the device */
145                 state->frequencies[0] = 0;
146                 if (state->msg->version < 3) {
147                         get_default_min_max_freq(state->resp->info.type,
148                                                  &state->frequencies[1],
149                                                  &state->frequencies[2]);
150                 } else {
151                         state->frequencies[1] =
152                             state->resp->info_3.min_frequency;
153                         state->frequencies[2] =
154                             state->resp->info_3.max_frequency;
155                 }
156         }
157
158         return 0;
159 }
160 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_init);
161
162 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *state,
163                                  u16 opt_length)
164 {
165         int ret;
166
167         if (opt_length)
168                 state->msg->insize = min(opt_length, state->ec->max_response);
169         else
170                 state->msg->insize = state->ec->max_response;
171
172         memcpy(state->msg->data, &state->param, sizeof(state->param));
173
174         ret = cros_ec_cmd_xfer_status(state->ec, state->msg);
175         if (ret < 0)
176                 return ret;
177
178         if (ret &&
179             state->resp != (struct ec_response_motion_sense *)state->msg->data)
180                 memcpy(state->resp, state->msg->data, ret);
181
182         return 0;
183 }
184 EXPORT_SYMBOL_GPL(cros_ec_motion_send_host_cmd);
185
186 static ssize_t cros_ec_sensors_calibrate(struct iio_dev *indio_dev,
187                 uintptr_t private, const struct iio_chan_spec *chan,
188                 const char *buf, size_t len)
189 {
190         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
191         int ret, i;
192         bool calibrate;
193
194         ret = strtobool(buf, &calibrate);
195         if (ret < 0)
196                 return ret;
197         if (!calibrate)
198                 return -EINVAL;
199
200         mutex_lock(&st->cmd_lock);
201         st->param.cmd = MOTIONSENSE_CMD_PERFORM_CALIB;
202         ret = cros_ec_motion_send_host_cmd(st, 0);
203         if (ret != 0) {
204                 dev_warn(&indio_dev->dev, "Unable to calibrate sensor\n");
205         } else {
206                 /* Save values */
207                 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
208                         st->calib[i].offset = st->resp->perform_calib.offset[i];
209         }
210         mutex_unlock(&st->cmd_lock);
211
212         return ret ? ret : len;
213 }
214
215 static ssize_t cros_ec_sensors_id(struct iio_dev *indio_dev,
216                                   uintptr_t private,
217                                   const struct iio_chan_spec *chan, char *buf)
218 {
219         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
220
221         return snprintf(buf, PAGE_SIZE, "%d\n", st->param.info.sensor_num);
222 }
223
224 static ssize_t cros_ec_sensors_loc(struct iio_dev *indio_dev,
225                 uintptr_t private, const struct iio_chan_spec *chan,
226                 char *buf)
227 {
228         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
229
230         return snprintf(buf, PAGE_SIZE, "%s\n", cros_ec_loc[st->loc]);
231 }
232
233 const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = {
234         {
235                 .name = "calibrate",
236                 .shared = IIO_SHARED_BY_ALL,
237                 .write = cros_ec_sensors_calibrate
238         },
239         {
240                 .name = "id",
241                 .shared = IIO_SHARED_BY_ALL,
242                 .read = cros_ec_sensors_id
243         },
244         {
245                 .name = "location",
246                 .shared = IIO_SHARED_BY_ALL,
247                 .read = cros_ec_sensors_loc
248         },
249         { },
250 };
251 EXPORT_SYMBOL_GPL(cros_ec_sensors_ext_info);
252
253 /**
254  * cros_ec_sensors_idx_to_reg - convert index into offset in shared memory
255  * @st:         pointer to state information for device
256  * @idx:        sensor index (should be element of enum sensor_index)
257  *
258  * Return:      address to read at
259  */
260 static unsigned int cros_ec_sensors_idx_to_reg(
261                                         struct cros_ec_sensors_core_state *st,
262                                         unsigned int idx)
263 {
264         /*
265          * When using LPC interface, only space for 2 Accel and one Gyro.
266          * First halfword of MOTIONSENSE_TYPE_ACCEL is used by angle.
267          */
268         if (st->type == MOTIONSENSE_TYPE_ACCEL)
269                 return EC_MEMMAP_ACC_DATA + sizeof(u16) *
270                         (1 + idx + st->param.info.sensor_num *
271                          CROS_EC_SENSOR_MAX_AXIS);
272
273         return EC_MEMMAP_GYRO_DATA + sizeof(u16) * idx;
274 }
275
276 static int cros_ec_sensors_cmd_read_u8(struct cros_ec_device *ec,
277                                        unsigned int offset, u8 *dest)
278 {
279         return ec->cmd_readmem(ec, offset, 1, dest);
280 }
281
282 static int cros_ec_sensors_cmd_read_u16(struct cros_ec_device *ec,
283                                          unsigned int offset, u16 *dest)
284 {
285         __le16 tmp;
286         int ret = ec->cmd_readmem(ec, offset, 2, &tmp);
287
288         if (ret >= 0)
289                 *dest = le16_to_cpu(tmp);
290
291         return ret;
292 }
293
294 /**
295  * cros_ec_sensors_read_until_not_busy() - read until is not busy
296  *
297  * @st: pointer to state information for device
298  *
299  * Read from EC status byte until it reads not busy.
300  * Return: 8-bit status if ok, -errno on failure.
301  */
302 static int cros_ec_sensors_read_until_not_busy(
303                                         struct cros_ec_sensors_core_state *st)
304 {
305         struct cros_ec_device *ec = st->ec;
306         u8 status;
307         int ret, attempts = 0;
308
309         ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, &status);
310         if (ret < 0)
311                 return ret;
312
313         while (status & EC_MEMMAP_ACC_STATUS_BUSY_BIT) {
314                 /* Give up after enough attempts, return error. */
315                 if (attempts++ >= 50)
316                         return -EIO;
317
318                 /* Small delay every so often. */
319                 if (attempts % 5 == 0)
320                         msleep(25);
321
322                 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
323                                                   &status);
324                 if (ret < 0)
325                         return ret;
326         }
327
328         return status;
329 }
330
331 /**
332  * read_ec_sensors_data_unsafe() - read acceleration data from EC shared memory
333  * @indio_dev:  pointer to IIO device
334  * @scan_mask:  bitmap of the sensor indices to scan
335  * @data:       location to store data
336  *
337  * This is the unsafe function for reading the EC data. It does not guarantee
338  * that the EC will not modify the data as it is being read in.
339  *
340  * Return: 0 on success, -errno on failure.
341  */
342 static int cros_ec_sensors_read_data_unsafe(struct iio_dev *indio_dev,
343                          unsigned long scan_mask, s16 *data)
344 {
345         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
346         struct cros_ec_device *ec = st->ec;
347         unsigned int i;
348         int ret;
349
350         /* Read all sensors enabled in scan_mask. Each value is 2 bytes. */
351         for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
352                 ret = cros_ec_sensors_cmd_read_u16(ec,
353                                              cros_ec_sensors_idx_to_reg(st, i),
354                                              data);
355                 if (ret < 0)
356                         return ret;
357
358                 *data *= st->sign[i];
359                 data++;
360         }
361
362         return 0;
363 }
364
365 /**
366  * cros_ec_sensors_read_lpc() - read acceleration data from EC shared memory.
367  * @indio_dev: pointer to IIO device.
368  * @scan_mask: bitmap of the sensor indices to scan.
369  * @data: location to store data.
370  *
371  * Note: this is the safe function for reading the EC data. It guarantees
372  * that the data sampled was not modified by the EC while being read.
373  *
374  * Return: 0 on success, -errno on failure.
375  */
376 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev,
377                              unsigned long scan_mask, s16 *data)
378 {
379         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
380         struct cros_ec_device *ec = st->ec;
381         u8 samp_id = 0xff, status = 0;
382         int ret, attempts = 0;
383
384         /*
385          * Continually read all data from EC until the status byte after
386          * all reads reflects that the EC is not busy and the sample id
387          * matches the sample id from before all reads. This guarantees
388          * that data read in was not modified by the EC while reading.
389          */
390         while ((status & (EC_MEMMAP_ACC_STATUS_BUSY_BIT |
391                           EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK)) != samp_id) {
392                 /* If we have tried to read too many times, return error. */
393                 if (attempts++ >= 5)
394                         return -EIO;
395
396                 /* Read status byte until EC is not busy. */
397                 ret = cros_ec_sensors_read_until_not_busy(st);
398                 if (ret < 0)
399                         return ret;
400
401                 /*
402                  * Store the current sample id so that we can compare to the
403                  * sample id after reading the data.
404                  */
405                 samp_id = ret & EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK;
406
407                 /* Read all EC data, format it, and store it into data. */
408                 ret = cros_ec_sensors_read_data_unsafe(indio_dev, scan_mask,
409                                                        data);
410                 if (ret < 0)
411                         return ret;
412
413                 /* Read status byte. */
414                 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
415                                                   &status);
416                 if (ret < 0)
417                         return ret;
418         }
419
420         return 0;
421 }
422 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_lpc);
423
424 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev,
425                              unsigned long scan_mask, s16 *data)
426 {
427         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
428         int ret;
429         unsigned int i;
430
431         /* Read all sensor data through a command. */
432         st->param.cmd = MOTIONSENSE_CMD_DATA;
433         ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->data));
434         if (ret != 0) {
435                 dev_warn(&indio_dev->dev, "Unable to read sensor data\n");
436                 return ret;
437         }
438
439         for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
440                 *data = st->resp->data.data[i];
441                 data++;
442         }
443
444         return 0;
445 }
446 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_cmd);
447
448 irqreturn_t cros_ec_sensors_capture(int irq, void *p)
449 {
450         struct iio_poll_func *pf = p;
451         struct iio_dev *indio_dev = pf->indio_dev;
452         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
453         int ret;
454
455         mutex_lock(&st->cmd_lock);
456
457         /* Clear capture data. */
458         memset(st->samples, 0, indio_dev->scan_bytes);
459
460         /* Read data based on which channels are enabled in scan mask. */
461         ret = st->read_ec_sensors_data(indio_dev,
462                                        *(indio_dev->active_scan_mask),
463                                        (s16 *)st->samples);
464         if (ret < 0)
465                 goto done;
466
467         iio_push_to_buffers_with_timestamp(indio_dev, st->samples,
468                                            iio_get_time_ns(indio_dev));
469
470 done:
471         /*
472          * Tell the core we are done with this trigger and ready for the
473          * next one.
474          */
475         iio_trigger_notify_done(indio_dev->trig);
476
477         mutex_unlock(&st->cmd_lock);
478
479         return IRQ_HANDLED;
480 }
481 EXPORT_SYMBOL_GPL(cros_ec_sensors_capture);
482
483 int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
484                           struct iio_chan_spec const *chan,
485                           int *val, int *val2, long mask)
486 {
487         int ret;
488
489         switch (mask) {
490         case IIO_CHAN_INFO_SAMP_FREQ:
491                 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
492                 st->param.ec_rate.data =
493                         EC_MOTION_SENSE_NO_VALUE;
494
495                 ret = cros_ec_motion_send_host_cmd(st, 0);
496                 if (ret)
497                         break;
498
499                 *val = st->resp->ec_rate.ret;
500                 ret = IIO_VAL_INT;
501                 break;
502         case IIO_CHAN_INFO_FREQUENCY:
503                 st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
504                 st->param.sensor_odr.data =
505                         EC_MOTION_SENSE_NO_VALUE;
506
507                 ret = cros_ec_motion_send_host_cmd(st, 0);
508                 if (ret)
509                         break;
510
511                 *val = st->resp->sensor_odr.ret;
512                 ret = IIO_VAL_INT;
513                 break;
514         default:
515                 ret = -EINVAL;
516                 break;
517         }
518
519         return ret;
520 }
521 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read);
522
523 int cros_ec_sensors_core_read_avail(struct iio_dev *indio_dev,
524                                     struct iio_chan_spec const *chan,
525                                     const int **vals,
526                                     int *type,
527                                     int *length,
528                                     long mask)
529 {
530         struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
531
532         switch (mask) {
533         case IIO_CHAN_INFO_SAMP_FREQ:
534                 *length = ARRAY_SIZE(state->frequencies);
535                 *vals = (const int *)&state->frequencies;
536                 *type = IIO_VAL_INT;
537                 return IIO_AVAIL_LIST;
538         }
539
540         return -EINVAL;
541 }
542 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read_avail);
543
544 int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
545                                struct iio_chan_spec const *chan,
546                                int val, int val2, long mask)
547 {
548         int ret;
549
550         switch (mask) {
551         case IIO_CHAN_INFO_FREQUENCY:
552                 st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
553                 st->param.sensor_odr.data = val;
554
555                 /* Always roundup, so caller gets at least what it asks for. */
556                 st->param.sensor_odr.roundup = 1;
557
558                 ret = cros_ec_motion_send_host_cmd(st, 0);
559                 break;
560         case IIO_CHAN_INFO_SAMP_FREQ:
561                 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
562                 st->param.ec_rate.data = val;
563
564                 ret = cros_ec_motion_send_host_cmd(st, 0);
565                 if (ret)
566                         break;
567                 st->curr_sampl_freq = val;
568                 break;
569         default:
570                 ret = -EINVAL;
571                 break;
572         }
573         return ret;
574 }
575 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_write);
576
577 static int __maybe_unused cros_ec_sensors_prepare(struct device *dev)
578 {
579         struct iio_dev *indio_dev = dev_get_drvdata(dev);
580         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
581
582         if (st->curr_sampl_freq == 0)
583                 return 0;
584
585         /*
586          * If the sensors are sampled at high frequency, we will not be able to
587          * sleep. Set sampling to a long period if necessary.
588          */
589         if (st->curr_sampl_freq < CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY) {
590                 mutex_lock(&st->cmd_lock);
591                 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
592                 st->param.ec_rate.data = CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY;
593                 cros_ec_motion_send_host_cmd(st, 0);
594                 mutex_unlock(&st->cmd_lock);
595         }
596         return 0;
597 }
598
599 static void __maybe_unused cros_ec_sensors_complete(struct device *dev)
600 {
601         struct iio_dev *indio_dev = dev_get_drvdata(dev);
602         struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
603
604         if (st->curr_sampl_freq == 0)
605                 return;
606
607         if (st->curr_sampl_freq < CROS_EC_MIN_SUSPEND_SAMPLING_FREQUENCY) {
608                 mutex_lock(&st->cmd_lock);
609                 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
610                 st->param.ec_rate.data = st->curr_sampl_freq;
611                 cros_ec_motion_send_host_cmd(st, 0);
612                 mutex_unlock(&st->cmd_lock);
613         }
614 }
615
616 const struct dev_pm_ops cros_ec_sensors_pm_ops = {
617 #ifdef CONFIG_PM_SLEEP
618         .prepare = cros_ec_sensors_prepare,
619         .complete = cros_ec_sensors_complete
620 #endif
621 };
622 EXPORT_SYMBOL_GPL(cros_ec_sensors_pm_ops);
623
624 MODULE_DESCRIPTION("ChromeOS EC sensor hub core functions");
625 MODULE_LICENSE("GPL v2");