2 * cros_ec_sensors_core - Common function for Chrome OS EC sensor driver.
4 * Copyright (C) 2016 Google, Inc
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.
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.
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>
30 #include "cros_ec_sensors_core.h"
32 static char *cros_ec_loc[] = {
33 [MOTIONSENSE_LOC_BASE] = "base",
34 [MOTIONSENSE_LOC_LID] = "lid",
35 [MOTIONSENSE_LOC_MAX] = "unknown",
38 int cros_ec_sensors_core_init(struct platform_device *pdev,
39 struct iio_dev *indio_dev,
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);
47 platform_set_drvdata(pdev, indio_dev);
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);
56 state->resp = (struct ec_response_motion_sense *)state->msg->data;
58 mutex_init(&state->cmd_lock);
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);
65 indio_dev->dev.parent = &pdev->dev;
66 indio_dev->name = pdev->name;
68 if (physical_device) {
69 indio_dev->modes = INDIO_DIRECT_MODE;
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");
77 state->type = state->resp->info.type;
78 state->loc = state->resp->info.location;
83 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_init);
85 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *state,
91 state->msg->insize = min(opt_length, state->ec->max_response);
93 state->msg->insize = state->ec->max_response;
95 memcpy(state->msg->data, &state->param, sizeof(state->param));
97 ret = cros_ec_cmd_xfer_status(state->ec, state->msg);
102 state->resp != (struct ec_response_motion_sense *)state->msg->data)
103 memcpy(state->resp, state->msg->data, ret);
107 EXPORT_SYMBOL_GPL(cros_ec_motion_send_host_cmd);
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)
113 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
117 ret = strtobool(buf, &calibrate);
123 mutex_lock(&st->cmd_lock);
124 st->param.cmd = MOTIONSENSE_CMD_PERFORM_CALIB;
125 ret = cros_ec_motion_send_host_cmd(st, 0);
127 dev_warn(&indio_dev->dev, "Unable to calibrate sensor\n");
130 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
131 st->calib[i] = st->resp->perform_calib.offset[i];
133 mutex_unlock(&st->cmd_lock);
135 return ret ? ret : len;
138 static ssize_t cros_ec_sensors_loc(struct iio_dev *indio_dev,
139 uintptr_t private, const struct iio_chan_spec *chan,
142 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
144 return snprintf(buf, PAGE_SIZE, "%s\n", cros_ec_loc[st->loc]);
147 const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = {
150 .shared = IIO_SHARED_BY_ALL,
151 .write = cros_ec_sensors_calibrate
155 .shared = IIO_SHARED_BY_ALL,
156 .read = cros_ec_sensors_loc
160 EXPORT_SYMBOL_GPL(cros_ec_sensors_ext_info);
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)
167 * Return: address to read at
169 static unsigned int cros_ec_sensors_idx_to_reg(
170 struct cros_ec_sensors_core_state *st,
174 * When using LPC interface, only space for 2 Accel and one Gyro.
175 * First halfword of MOTIONSENSE_TYPE_ACCEL is used by angle.
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);
182 return EC_MEMMAP_GYRO_DATA + sizeof(u16) * idx;
185 static int cros_ec_sensors_cmd_read_u8(struct cros_ec_device *ec,
186 unsigned int offset, u8 *dest)
188 return ec->cmd_readmem(ec, offset, 1, dest);
191 static int cros_ec_sensors_cmd_read_u16(struct cros_ec_device *ec,
192 unsigned int offset, u16 *dest)
195 int ret = ec->cmd_readmem(ec, offset, 2, &tmp);
198 *dest = le16_to_cpu(tmp);
204 * cros_ec_sensors_read_until_not_busy() - read until is not busy
206 * @st: pointer to state information for device
208 * Read from EC status byte until it reads not busy.
209 * Return: 8-bit status if ok, -errno on failure.
211 static int cros_ec_sensors_read_until_not_busy(
212 struct cros_ec_sensors_core_state *st)
214 struct cros_ec_device *ec = st->ec;
216 int ret, attempts = 0;
218 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, &status);
222 while (status & EC_MEMMAP_ACC_STATUS_BUSY_BIT) {
223 /* Give up after enough attempts, return error. */
224 if (attempts++ >= 50)
227 /* Small delay every so often. */
228 if (attempts % 5 == 0)
231 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
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
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.
249 * Return: 0 on success, -errno on failure.
251 static int cros_ec_sensors_read_data_unsafe(struct iio_dev *indio_dev,
252 unsigned long scan_mask, s16 *data)
254 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
255 struct cros_ec_device *ec = st->ec;
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),
273 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev,
274 unsigned long scan_mask, s16 *data)
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;
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.
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. */
293 /* Read status byte until EC is not busy. */
294 ret = cros_ec_sensors_read_until_not_busy(st);
299 * Store the current sample id so that we can compare to the
300 * sample id after reading the data.
302 samp_id = ret & EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK;
304 /* Read all EC data, format it, and store it into data. */
305 ret = cros_ec_sensors_read_data_unsafe(indio_dev, scan_mask,
310 /* Read status byte. */
311 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
319 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_lpc);
321 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev,
322 unsigned long scan_mask, s16 *data)
324 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
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));
332 dev_warn(&indio_dev->dev, "Unable to read sensor data\n");
336 for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
337 *data = st->resp->data.data[i];
343 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_cmd);
345 irqreturn_t cros_ec_sensors_capture(int irq, void *p)
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);
352 mutex_lock(&st->cmd_lock);
354 /* Clear capture data. */
355 memset(st->samples, 0, indio_dev->scan_bytes);
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),
364 iio_push_to_buffers_with_timestamp(indio_dev, st->samples,
365 iio_get_time_ns(indio_dev));
369 * Tell the core we are done with this trigger and ready for the
372 iio_trigger_notify_done(indio_dev->trig);
374 mutex_unlock(&st->cmd_lock);
378 EXPORT_SYMBOL_GPL(cros_ec_sensors_capture);
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)
384 int ret = IIO_VAL_INT;
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;
392 if (cros_ec_motion_send_host_cmd(st, 0))
395 *val = st->resp->ec_rate.ret;
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;
402 if (cros_ec_motion_send_host_cmd(st, 0))
405 *val = st->resp->sensor_odr.ret;
413 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read);
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)
422 case IIO_CHAN_INFO_FREQUENCY:
423 st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
424 st->param.sensor_odr.data = val;
426 /* Always roundup, so caller gets at least what it asks for. */
427 st->param.sensor_odr.roundup = 1;
429 if (cros_ec_motion_send_host_cmd(st, 0))
432 case IIO_CHAN_INFO_SAMP_FREQ:
433 st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
434 st->param.ec_rate.data = val;
436 if (cros_ec_motion_send_host_cmd(st, 0))
439 st->curr_sampl_freq = val;
447 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_write);
449 MODULE_DESCRIPTION("ChromeOS EC sensor hub core functions");
450 MODULE_LICENSE("GPL v2");