1 // SPDX-License-Identifier: GPL-2.0
3 * cros_ec_sensors_core - Common function for Chrome OS EC sensor driver.
5 * Copyright (C) 2016 Google, Inc
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>
23 static char *cros_ec_loc[] = {
24 [MOTIONSENSE_LOC_BASE] = "base",
25 [MOTIONSENSE_LOC_LID] = "lid",
26 [MOTIONSENSE_LOC_MAX] = "unknown",
29 static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev,
30 u16 cmd_offset, u16 cmd, u32 *mask)
34 struct cros_ec_command msg;
36 struct ec_params_get_cmd_versions params;
37 struct ec_response_get_cmd_versions resp;
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)
45 .params = {.cmd = cmd}
48 ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
50 *mask = buf.resp.version_mask;
54 static void get_default_min_max_freq(enum motionsensor_type type,
59 case MOTIONSENSE_TYPE_ACCEL:
60 case MOTIONSENSE_TYPE_GYRO:
64 case MOTIONSENSE_TYPE_MAG:
68 case MOTIONSENSE_TYPE_PROX:
69 case MOTIONSENSE_TYPE_LIGHT:
73 case MOTIONSENSE_TYPE_BARO:
77 case MOTIONSENSE_TYPE_ACTIVITY:
85 int cros_ec_sensors_core_init(struct platform_device *pdev,
86 struct iio_dev *indio_dev,
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);
97 platform_set_drvdata(pdev, indio_dev);
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);
106 state->resp = (struct ec_response_motion_sense *)state->msg->data;
108 mutex_init(&state->cmd_lock);
110 ret = cros_ec_get_host_cmd_version_mask(state->ec,
112 EC_CMD_MOTION_SENSE_CMD,
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);
122 indio_dev->dev.parent = &pdev->dev;
123 indio_dev->name = pdev->name;
125 if (physical_device) {
126 indio_dev->modes = INDIO_DIRECT_MODE;
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);
132 dev_warn(dev, "Can not access sensor info\n");
135 state->type = state->resp->info.type;
136 state->loc = state->resp->info.location;
138 /* Set sign vector, only used for backward compatibility. */
139 memset(state->sign, 1, CROS_EC_SENSOR_MAX_AXIS);
141 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
142 state->calib[i].scale = MOTION_SENSE_DEFAULT_SCALE;
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]);
151 state->frequencies[1] =
152 state->resp->info_3.min_frequency;
153 state->frequencies[2] =
154 state->resp->info_3.max_frequency;
160 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_init);
162 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *state,
168 state->msg->insize = min(opt_length, state->ec->max_response);
170 state->msg->insize = state->ec->max_response;
172 memcpy(state->msg->data, &state->param, sizeof(state->param));
174 ret = cros_ec_cmd_xfer_status(state->ec, state->msg);
179 state->resp != (struct ec_response_motion_sense *)state->msg->data)
180 memcpy(state->resp, state->msg->data, ret);
184 EXPORT_SYMBOL_GPL(cros_ec_motion_send_host_cmd);
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)
190 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
194 ret = strtobool(buf, &calibrate);
200 mutex_lock(&st->cmd_lock);
201 st->param.cmd = MOTIONSENSE_CMD_PERFORM_CALIB;
202 ret = cros_ec_motion_send_host_cmd(st, 0);
204 dev_warn(&indio_dev->dev, "Unable to calibrate sensor\n");
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];
210 mutex_unlock(&st->cmd_lock);
212 return ret ? ret : len;
215 static ssize_t cros_ec_sensors_id(struct iio_dev *indio_dev,
217 const struct iio_chan_spec *chan, char *buf)
219 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
221 return snprintf(buf, PAGE_SIZE, "%d\n", st->param.info.sensor_num);
224 static ssize_t cros_ec_sensors_loc(struct iio_dev *indio_dev,
225 uintptr_t private, const struct iio_chan_spec *chan,
228 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
230 return snprintf(buf, PAGE_SIZE, "%s\n", cros_ec_loc[st->loc]);
233 const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = {
236 .shared = IIO_SHARED_BY_ALL,
237 .write = cros_ec_sensors_calibrate
241 .shared = IIO_SHARED_BY_ALL,
242 .read = cros_ec_sensors_id
246 .shared = IIO_SHARED_BY_ALL,
247 .read = cros_ec_sensors_loc
251 EXPORT_SYMBOL_GPL(cros_ec_sensors_ext_info);
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)
258 * Return: address to read at
260 static unsigned int cros_ec_sensors_idx_to_reg(
261 struct cros_ec_sensors_core_state *st,
265 * When using LPC interface, only space for 2 Accel and one Gyro.
266 * First halfword of MOTIONSENSE_TYPE_ACCEL is used by angle.
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);
273 return EC_MEMMAP_GYRO_DATA + sizeof(u16) * idx;
276 static int cros_ec_sensors_cmd_read_u8(struct cros_ec_device *ec,
277 unsigned int offset, u8 *dest)
279 return ec->cmd_readmem(ec, offset, 1, dest);
282 static int cros_ec_sensors_cmd_read_u16(struct cros_ec_device *ec,
283 unsigned int offset, u16 *dest)
286 int ret = ec->cmd_readmem(ec, offset, 2, &tmp);
289 *dest = le16_to_cpu(tmp);
295 * cros_ec_sensors_read_until_not_busy() - read until is not busy
297 * @st: pointer to state information for device
299 * Read from EC status byte until it reads not busy.
300 * Return: 8-bit status if ok, -errno on failure.
302 static int cros_ec_sensors_read_until_not_busy(
303 struct cros_ec_sensors_core_state *st)
305 struct cros_ec_device *ec = st->ec;
307 int ret, attempts = 0;
309 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, &status);
313 while (status & EC_MEMMAP_ACC_STATUS_BUSY_BIT) {
314 /* Give up after enough attempts, return error. */
315 if (attempts++ >= 50)
318 /* Small delay every so often. */
319 if (attempts % 5 == 0)
322 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
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
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.
340 * Return: 0 on success, -errno on failure.
342 static int cros_ec_sensors_read_data_unsafe(struct iio_dev *indio_dev,
343 unsigned long scan_mask, s16 *data)
345 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
346 struct cros_ec_device *ec = st->ec;
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),
358 *data *= st->sign[i];
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.
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.
374 * Return: 0 on success, -errno on failure.
376 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev,
377 unsigned long scan_mask, s16 *data)
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;
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.
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. */
396 /* Read status byte until EC is not busy. */
397 ret = cros_ec_sensors_read_until_not_busy(st);
402 * Store the current sample id so that we can compare to the
403 * sample id after reading the data.
405 samp_id = ret & EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK;
407 /* Read all EC data, format it, and store it into data. */
408 ret = cros_ec_sensors_read_data_unsafe(indio_dev, scan_mask,
413 /* Read status byte. */
414 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
422 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_lpc);
424 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev,
425 unsigned long scan_mask, s16 *data)
427 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
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));
435 dev_warn(&indio_dev->dev, "Unable to read sensor data\n");
439 for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
440 *data = st->resp->data.data[i];
446 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_cmd);
448 irqreturn_t cros_ec_sensors_capture(int irq, void *p)
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);
455 mutex_lock(&st->cmd_lock);
457 /* Clear capture data. */
458 memset(st->samples, 0, indio_dev->scan_bytes);
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),
467 iio_push_to_buffers_with_timestamp(indio_dev, st->samples,
468 iio_get_time_ns(indio_dev));
472 * Tell the core we are done with this trigger and ready for the
475 iio_trigger_notify_done(indio_dev->trig);
477 mutex_unlock(&st->cmd_lock);
481 EXPORT_SYMBOL_GPL(cros_ec_sensors_capture);
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)
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;
495 ret = cros_ec_motion_send_host_cmd(st, 0);
499 *val = st->resp->ec_rate.ret;
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;
507 ret = cros_ec_motion_send_host_cmd(st, 0);
511 *val = st->resp->sensor_odr.ret;
521 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read);
523 int cros_ec_sensors_core_read_avail(struct iio_dev *indio_dev,
524 struct iio_chan_spec const *chan,
530 struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
533 case IIO_CHAN_INFO_SAMP_FREQ:
534 *length = ARRAY_SIZE(state->frequencies);
535 *vals = (const int *)&state->frequencies;
537 return IIO_AVAIL_LIST;
542 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read_avail);
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)
551 case IIO_CHAN_INFO_FREQUENCY:
552 st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
553 st->param.sensor_odr.data = val;
555 /* Always roundup, so caller gets at least what it asks for. */
556 st->param.sensor_odr.roundup = 1;
558 ret = cros_ec_motion_send_host_cmd(st, 0);
560 case IIO_CHAN_INFO_SAMP_FREQ:
561 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
562 st->param.ec_rate.data = val;
564 ret = cros_ec_motion_send_host_cmd(st, 0);
567 st->curr_sampl_freq = val;
575 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_write);
577 static int __maybe_unused cros_ec_sensors_prepare(struct device *dev)
579 struct iio_dev *indio_dev = dev_get_drvdata(dev);
580 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
582 if (st->curr_sampl_freq == 0)
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.
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);
599 static void __maybe_unused cros_ec_sensors_complete(struct device *dev)
601 struct iio_dev *indio_dev = dev_get_drvdata(dev);
602 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
604 if (st->curr_sampl_freq == 0)
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);
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
622 EXPORT_SYMBOL_GPL(cros_ec_sensors_pm_ops);
624 MODULE_DESCRIPTION("ChromeOS EC sensor hub core functions");
625 MODULE_LICENSE("GPL v2");