| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Driver for older Chrome OS EC accelerometer |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright 2017 Google, Inc |
|---|
| 5 | 6 | * |
|---|
| 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 | 7 | * This driver uses the memory mapper cros-ec interface to communicate |
|---|
| 16 | | - * with the Chrome OS EC about accelerometer data. |
|---|
| 8 | + * with the Chrome OS EC about accelerometer data or older commands. |
|---|
| 17 | 9 | * Accelerometer access is presented through iio sysfs. |
|---|
| 18 | 10 | */ |
|---|
| 19 | 11 | |
|---|
| 20 | 12 | #include <linux/delay.h> |
|---|
| 21 | 13 | #include <linux/device.h> |
|---|
| 22 | 14 | #include <linux/iio/buffer.h> |
|---|
| 15 | +#include <linux/iio/common/cros_ec_sensors_core.h> |
|---|
| 23 | 16 | #include <linux/iio/iio.h> |
|---|
| 24 | 17 | #include <linux/iio/kfifo_buf.h> |
|---|
| 25 | 18 | #include <linux/iio/trigger_consumer.h> |
|---|
| 26 | 19 | #include <linux/iio/triggered_buffer.h> |
|---|
| 27 | 20 | #include <linux/kernel.h> |
|---|
| 28 | | -#include <linux/mfd/cros_ec.h> |
|---|
| 29 | | -#include <linux/mfd/cros_ec_commands.h> |
|---|
| 30 | 21 | #include <linux/module.h> |
|---|
| 31 | 22 | #include <linux/slab.h> |
|---|
| 32 | | -#include <linux/sysfs.h> |
|---|
| 23 | +#include <linux/platform_data/cros_ec_commands.h> |
|---|
| 24 | +#include <linux/platform_data/cros_ec_proto.h> |
|---|
| 33 | 25 | #include <linux/platform_device.h> |
|---|
| 34 | 26 | |
|---|
| 35 | 27 | #define DRV_NAME "cros-ec-accel-legacy" |
|---|
| 36 | 28 | |
|---|
| 29 | +#define CROS_EC_SENSOR_LEGACY_NUM 2 |
|---|
| 37 | 30 | /* |
|---|
| 38 | 31 | * Sensor scale hard coded at 10 bits per g, computed as: |
|---|
| 39 | 32 | * g / (2^10 - 1) = 0.009586168; with g = 9.80665 m.s^-2 |
|---|
| 40 | 33 | */ |
|---|
| 41 | 34 | #define ACCEL_LEGACY_NSCALE 9586168 |
|---|
| 42 | 35 | |
|---|
| 43 | | -/* Indices for EC sensor values. */ |
|---|
| 44 | | -enum { |
|---|
| 45 | | - X, |
|---|
| 46 | | - Y, |
|---|
| 47 | | - Z, |
|---|
| 48 | | - MAX_AXIS, |
|---|
| 49 | | -}; |
|---|
| 50 | | - |
|---|
| 51 | | -/* State data for cros_ec_accel_legacy iio driver. */ |
|---|
| 52 | | -struct cros_ec_accel_legacy_state { |
|---|
| 53 | | - struct cros_ec_device *ec; |
|---|
| 54 | | - |
|---|
| 55 | | - /* |
|---|
| 56 | | - * Array holding data from a single capture. 2 bytes per channel |
|---|
| 57 | | - * for the 3 channels plus the timestamp which is always last and |
|---|
| 58 | | - * 8-bytes aligned. |
|---|
| 59 | | - */ |
|---|
| 60 | | - s16 capture_data[8]; |
|---|
| 61 | | - s8 sign[MAX_AXIS]; |
|---|
| 62 | | - u8 sensor_num; |
|---|
| 63 | | -}; |
|---|
| 64 | | - |
|---|
| 65 | | -static int ec_cmd_read_u8(struct cros_ec_device *ec, unsigned int offset, |
|---|
| 66 | | - u8 *dest) |
|---|
| 67 | | -{ |
|---|
| 68 | | - return ec->cmd_readmem(ec, offset, 1, dest); |
|---|
| 69 | | -} |
|---|
| 70 | | - |
|---|
| 71 | | -static int ec_cmd_read_u16(struct cros_ec_device *ec, unsigned int offset, |
|---|
| 72 | | - u16 *dest) |
|---|
| 73 | | -{ |
|---|
| 74 | | - __le16 tmp; |
|---|
| 75 | | - int ret = ec->cmd_readmem(ec, offset, 2, &tmp); |
|---|
| 76 | | - |
|---|
| 77 | | - *dest = le16_to_cpu(tmp); |
|---|
| 78 | | - |
|---|
| 79 | | - return ret; |
|---|
| 80 | | -} |
|---|
| 81 | | - |
|---|
| 82 | | -/** |
|---|
| 83 | | - * read_ec_until_not_busy() - Read from EC status byte until it reads not busy. |
|---|
| 84 | | - * @st: Pointer to state information for device. |
|---|
| 85 | | - * |
|---|
| 86 | | - * This function reads EC status until its busy bit gets cleared. It does not |
|---|
| 87 | | - * wait indefinitely and returns -EIO if the EC status is still busy after a |
|---|
| 88 | | - * few hundreds milliseconds. |
|---|
| 89 | | - * |
|---|
| 90 | | - * Return: 8-bit status if ok, -EIO on error |
|---|
| 36 | +/* |
|---|
| 37 | + * Sensor frequency is hard-coded to 10Hz. |
|---|
| 91 | 38 | */ |
|---|
| 92 | | -static int read_ec_until_not_busy(struct cros_ec_accel_legacy_state *st) |
|---|
| 39 | +static const int cros_ec_legacy_sample_freq[] = { 10, 0 }; |
|---|
| 40 | + |
|---|
| 41 | +static int cros_ec_accel_legacy_read_cmd(struct iio_dev *indio_dev, |
|---|
| 42 | + unsigned long scan_mask, s16 *data) |
|---|
| 93 | 43 | { |
|---|
| 94 | | - struct cros_ec_device *ec = st->ec; |
|---|
| 95 | | - u8 status; |
|---|
| 96 | | - int attempts = 0; |
|---|
| 97 | | - |
|---|
| 98 | | - ec_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, &status); |
|---|
| 99 | | - while (status & EC_MEMMAP_ACC_STATUS_BUSY_BIT) { |
|---|
| 100 | | - /* Give up after enough attempts, return error. */ |
|---|
| 101 | | - if (attempts++ >= 50) |
|---|
| 102 | | - return -EIO; |
|---|
| 103 | | - |
|---|
| 104 | | - /* Small delay every so often. */ |
|---|
| 105 | | - if (attempts % 5 == 0) |
|---|
| 106 | | - msleep(25); |
|---|
| 107 | | - |
|---|
| 108 | | - ec_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, &status); |
|---|
| 109 | | - } |
|---|
| 110 | | - |
|---|
| 111 | | - return status; |
|---|
| 112 | | -} |
|---|
| 113 | | - |
|---|
| 114 | | -/** |
|---|
| 115 | | - * read_ec_accel_data_unsafe() - Read acceleration data from EC shared memory. |
|---|
| 116 | | - * @st: Pointer to state information for device. |
|---|
| 117 | | - * @scan_mask: Bitmap of the sensor indices to scan. |
|---|
| 118 | | - * @data: Location to store data. |
|---|
| 119 | | - * |
|---|
| 120 | | - * This is the unsafe function for reading the EC data. It does not guarantee |
|---|
| 121 | | - * that the EC will not modify the data as it is being read in. |
|---|
| 122 | | - */ |
|---|
| 123 | | -static void read_ec_accel_data_unsafe(struct cros_ec_accel_legacy_state *st, |
|---|
| 124 | | - unsigned long scan_mask, s16 *data) |
|---|
| 125 | | -{ |
|---|
| 126 | | - int i = 0; |
|---|
| 127 | | - int num_enabled = bitmap_weight(&scan_mask, MAX_AXIS); |
|---|
| 128 | | - |
|---|
| 129 | | - /* Read all sensors enabled in scan_mask. Each value is 2 bytes. */ |
|---|
| 130 | | - while (num_enabled--) { |
|---|
| 131 | | - i = find_next_bit(&scan_mask, MAX_AXIS, i); |
|---|
| 132 | | - ec_cmd_read_u16(st->ec, |
|---|
| 133 | | - EC_MEMMAP_ACC_DATA + |
|---|
| 134 | | - sizeof(s16) * |
|---|
| 135 | | - (1 + i + st->sensor_num * MAX_AXIS), |
|---|
| 136 | | - data); |
|---|
| 137 | | - *data *= st->sign[i]; |
|---|
| 138 | | - i++; |
|---|
| 139 | | - data++; |
|---|
| 140 | | - } |
|---|
| 141 | | -} |
|---|
| 142 | | - |
|---|
| 143 | | -/** |
|---|
| 144 | | - * read_ec_accel_data() - Read acceleration data from EC shared memory. |
|---|
| 145 | | - * @st: Pointer to state information for device. |
|---|
| 146 | | - * @scan_mask: Bitmap of the sensor indices to scan. |
|---|
| 147 | | - * @data: Location to store data. |
|---|
| 148 | | - * |
|---|
| 149 | | - * This is the safe function for reading the EC data. It guarantees that |
|---|
| 150 | | - * the data sampled was not modified by the EC while being read. |
|---|
| 151 | | - * |
|---|
| 152 | | - * Return: 0 if ok, -ve on error |
|---|
| 153 | | - */ |
|---|
| 154 | | -static int read_ec_accel_data(struct cros_ec_accel_legacy_state *st, |
|---|
| 155 | | - unsigned long scan_mask, s16 *data) |
|---|
| 156 | | -{ |
|---|
| 157 | | - u8 samp_id = 0xff; |
|---|
| 158 | | - u8 status = 0; |
|---|
| 44 | + struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); |
|---|
| 159 | 45 | int ret; |
|---|
| 160 | | - int attempts = 0; |
|---|
| 46 | + unsigned int i; |
|---|
| 47 | + u8 sensor_num; |
|---|
| 161 | 48 | |
|---|
| 162 | 49 | /* |
|---|
| 163 | | - * Continually read all data from EC until the status byte after |
|---|
| 164 | | - * all reads reflects that the EC is not busy and the sample id |
|---|
| 165 | | - * matches the sample id from before all reads. This guarantees |
|---|
| 166 | | - * that data read in was not modified by the EC while reading. |
|---|
| 50 | + * Read all sensor data through a command. |
|---|
| 51 | + * Save sensor_num, it is assumed to stay. |
|---|
| 167 | 52 | */ |
|---|
| 168 | | - while ((status & (EC_MEMMAP_ACC_STATUS_BUSY_BIT | |
|---|
| 169 | | - EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK)) != samp_id) { |
|---|
| 170 | | - /* If we have tried to read too many times, return error. */ |
|---|
| 171 | | - if (attempts++ >= 5) |
|---|
| 172 | | - return -EIO; |
|---|
| 53 | + sensor_num = st->param.info.sensor_num; |
|---|
| 54 | + st->param.cmd = MOTIONSENSE_CMD_DUMP; |
|---|
| 55 | + st->param.dump.max_sensor_count = CROS_EC_SENSOR_LEGACY_NUM; |
|---|
| 56 | + ret = cros_ec_motion_send_host_cmd(st, |
|---|
| 57 | + sizeof(st->resp->dump) + CROS_EC_SENSOR_LEGACY_NUM * |
|---|
| 58 | + sizeof(struct ec_response_motion_sensor_data)); |
|---|
| 59 | + st->param.info.sensor_num = sensor_num; |
|---|
| 60 | + if (ret != 0) { |
|---|
| 61 | + dev_warn(&indio_dev->dev, "Unable to read sensor data\n"); |
|---|
| 62 | + return ret; |
|---|
| 63 | + } |
|---|
| 173 | 64 | |
|---|
| 174 | | - /* Read status byte until EC is not busy. */ |
|---|
| 175 | | - ret = read_ec_until_not_busy(st); |
|---|
| 176 | | - if (ret < 0) |
|---|
| 177 | | - return ret; |
|---|
| 178 | | - status = ret; |
|---|
| 179 | | - |
|---|
| 180 | | - /* |
|---|
| 181 | | - * Store the current sample id so that we can compare to the |
|---|
| 182 | | - * sample id after reading the data. |
|---|
| 183 | | - */ |
|---|
| 184 | | - samp_id = status & EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK; |
|---|
| 185 | | - |
|---|
| 186 | | - /* Read all EC data, format it, and store it into data. */ |
|---|
| 187 | | - read_ec_accel_data_unsafe(st, scan_mask, data); |
|---|
| 188 | | - |
|---|
| 189 | | - /* Read status byte. */ |
|---|
| 190 | | - ec_cmd_read_u8(st->ec, EC_MEMMAP_ACC_STATUS, &status); |
|---|
| 65 | + for_each_set_bit(i, &scan_mask, indio_dev->masklength) { |
|---|
| 66 | + *data = st->resp->dump.sensor[sensor_num].data[i] * |
|---|
| 67 | + st->sign[i]; |
|---|
| 68 | + data++; |
|---|
| 191 | 69 | } |
|---|
| 192 | 70 | |
|---|
| 193 | 71 | return 0; |
|---|
| .. | .. |
|---|
| 197 | 75 | struct iio_chan_spec const *chan, |
|---|
| 198 | 76 | int *val, int *val2, long mask) |
|---|
| 199 | 77 | { |
|---|
| 200 | | - struct cros_ec_accel_legacy_state *st = iio_priv(indio_dev); |
|---|
| 78 | + struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); |
|---|
| 201 | 79 | s16 data = 0; |
|---|
| 202 | | - int ret = IIO_VAL_INT; |
|---|
| 80 | + int ret; |
|---|
| 81 | + int idx = chan->scan_index; |
|---|
| 82 | + |
|---|
| 83 | + mutex_lock(&st->cmd_lock); |
|---|
| 203 | 84 | |
|---|
| 204 | 85 | switch (mask) { |
|---|
| 205 | 86 | case IIO_CHAN_INFO_RAW: |
|---|
| 206 | | - ret = read_ec_accel_data(st, (1 << chan->scan_index), &data); |
|---|
| 207 | | - if (ret) |
|---|
| 208 | | - return ret; |
|---|
| 87 | + ret = st->read_ec_sensors_data(indio_dev, 1 << idx, &data); |
|---|
| 88 | + if (ret < 0) |
|---|
| 89 | + break; |
|---|
| 90 | + ret = IIO_VAL_INT; |
|---|
| 209 | 91 | *val = data; |
|---|
| 210 | | - return IIO_VAL_INT; |
|---|
| 92 | + break; |
|---|
| 211 | 93 | case IIO_CHAN_INFO_SCALE: |
|---|
| 94 | + WARN_ON(st->type != MOTIONSENSE_TYPE_ACCEL); |
|---|
| 212 | 95 | *val = 0; |
|---|
| 213 | 96 | *val2 = ACCEL_LEGACY_NSCALE; |
|---|
| 214 | | - return IIO_VAL_INT_PLUS_NANO; |
|---|
| 97 | + ret = IIO_VAL_INT_PLUS_NANO; |
|---|
| 98 | + break; |
|---|
| 215 | 99 | case IIO_CHAN_INFO_CALIBBIAS: |
|---|
| 216 | 100 | /* Calibration not supported. */ |
|---|
| 217 | 101 | *val = 0; |
|---|
| 218 | | - return IIO_VAL_INT; |
|---|
| 102 | + ret = IIO_VAL_INT; |
|---|
| 103 | + break; |
|---|
| 104 | + case IIO_CHAN_INFO_SAMP_FREQ: |
|---|
| 105 | + *val = cros_ec_legacy_sample_freq[0]; |
|---|
| 106 | + *val2 = cros_ec_legacy_sample_freq[1]; |
|---|
| 107 | + ret = IIO_VAL_INT_PLUS_MICRO; |
|---|
| 108 | + break; |
|---|
| 219 | 109 | default: |
|---|
| 220 | | - return -EINVAL; |
|---|
| 110 | + ret = cros_ec_sensors_core_read(st, chan, val, val2, |
|---|
| 111 | + mask); |
|---|
| 112 | + break; |
|---|
| 221 | 113 | } |
|---|
| 114 | + mutex_unlock(&st->cmd_lock); |
|---|
| 115 | + |
|---|
| 116 | + return ret; |
|---|
| 222 | 117 | } |
|---|
| 223 | 118 | |
|---|
| 224 | 119 | static int cros_ec_accel_legacy_write(struct iio_dev *indio_dev, |
|---|
| .. | .. |
|---|
| 235 | 130 | return -EINVAL; |
|---|
| 236 | 131 | } |
|---|
| 237 | 132 | |
|---|
| 133 | +/** |
|---|
| 134 | + * cros_ec_accel_legacy_read_avail() - get available values |
|---|
| 135 | + * @indio_dev: pointer to state information for device |
|---|
| 136 | + * @chan: channel specification structure table |
|---|
| 137 | + * @vals: list of available values |
|---|
| 138 | + * @type: type of data returned |
|---|
| 139 | + * @length: number of data returned in the array |
|---|
| 140 | + * @mask: specifies which values to be requested |
|---|
| 141 | + * |
|---|
| 142 | + * Return: an error code or IIO_AVAIL_LIST |
|---|
| 143 | + */ |
|---|
| 144 | +static int cros_ec_accel_legacy_read_avail(struct iio_dev *indio_dev, |
|---|
| 145 | + struct iio_chan_spec const *chan, |
|---|
| 146 | + const int **vals, |
|---|
| 147 | + int *type, |
|---|
| 148 | + int *length, |
|---|
| 149 | + long mask) |
|---|
| 150 | +{ |
|---|
| 151 | + switch (mask) { |
|---|
| 152 | + case IIO_CHAN_INFO_SAMP_FREQ: |
|---|
| 153 | + *length = ARRAY_SIZE(cros_ec_legacy_sample_freq); |
|---|
| 154 | + *vals = cros_ec_legacy_sample_freq; |
|---|
| 155 | + *type = IIO_VAL_INT_PLUS_MICRO; |
|---|
| 156 | + return IIO_AVAIL_LIST; |
|---|
| 157 | + } |
|---|
| 158 | + |
|---|
| 159 | + return -EINVAL; |
|---|
| 160 | +} |
|---|
| 161 | + |
|---|
| 238 | 162 | static const struct iio_info cros_ec_accel_legacy_info = { |
|---|
| 239 | 163 | .read_raw = &cros_ec_accel_legacy_read, |
|---|
| 240 | 164 | .write_raw = &cros_ec_accel_legacy_write, |
|---|
| 165 | + .read_avail = &cros_ec_accel_legacy_read_avail, |
|---|
| 241 | 166 | }; |
|---|
| 242 | 167 | |
|---|
| 243 | | -/** |
|---|
| 244 | | - * cros_ec_accel_legacy_capture() - The trigger handler function |
|---|
| 245 | | - * @irq: The interrupt number. |
|---|
| 246 | | - * @p: Private data - always a pointer to the poll func. |
|---|
| 247 | | - * |
|---|
| 248 | | - * On a trigger event occurring, if the pollfunc is attached then this |
|---|
| 249 | | - * handler is called as a threaded interrupt (and hence may sleep). It |
|---|
| 250 | | - * is responsible for grabbing data from the device and pushing it into |
|---|
| 251 | | - * the associated buffer. |
|---|
| 252 | | - * |
|---|
| 253 | | - * Return: IRQ_HANDLED |
|---|
| 168 | +/* |
|---|
| 169 | + * Present the channel using HTML5 standard: |
|---|
| 170 | + * need to invert X and Y and invert some lid axis. |
|---|
| 254 | 171 | */ |
|---|
| 255 | | -static irqreturn_t cros_ec_accel_legacy_capture(int irq, void *p) |
|---|
| 256 | | -{ |
|---|
| 257 | | - struct iio_poll_func *pf = p; |
|---|
| 258 | | - struct iio_dev *indio_dev = pf->indio_dev; |
|---|
| 259 | | - struct cros_ec_accel_legacy_state *st = iio_priv(indio_dev); |
|---|
| 260 | | - |
|---|
| 261 | | - /* Clear capture data. */ |
|---|
| 262 | | - memset(st->capture_data, 0, sizeof(st->capture_data)); |
|---|
| 263 | | - |
|---|
| 264 | | - /* |
|---|
| 265 | | - * Read data based on which channels are enabled in scan mask. Note |
|---|
| 266 | | - * that on a capture we are always reading the calibrated data. |
|---|
| 267 | | - */ |
|---|
| 268 | | - read_ec_accel_data(st, *indio_dev->active_scan_mask, st->capture_data); |
|---|
| 269 | | - |
|---|
| 270 | | - iio_push_to_buffers_with_timestamp(indio_dev, (void *)st->capture_data, |
|---|
| 271 | | - iio_get_time_ns(indio_dev)); |
|---|
| 272 | | - |
|---|
| 273 | | - /* |
|---|
| 274 | | - * Tell the core we are done with this trigger and ready for the |
|---|
| 275 | | - * next one. |
|---|
| 276 | | - */ |
|---|
| 277 | | - iio_trigger_notify_done(indio_dev->trig); |
|---|
| 278 | | - |
|---|
| 279 | | - return IRQ_HANDLED; |
|---|
| 280 | | -} |
|---|
| 281 | | - |
|---|
| 282 | | -static char *cros_ec_accel_legacy_loc_strings[] = { |
|---|
| 283 | | - [MOTIONSENSE_LOC_BASE] = "base", |
|---|
| 284 | | - [MOTIONSENSE_LOC_LID] = "lid", |
|---|
| 285 | | - [MOTIONSENSE_LOC_MAX] = "unknown", |
|---|
| 286 | | -}; |
|---|
| 287 | | - |
|---|
| 288 | | -static ssize_t cros_ec_accel_legacy_loc(struct iio_dev *indio_dev, |
|---|
| 289 | | - uintptr_t private, |
|---|
| 290 | | - const struct iio_chan_spec *chan, |
|---|
| 291 | | - char *buf) |
|---|
| 292 | | -{ |
|---|
| 293 | | - struct cros_ec_accel_legacy_state *st = iio_priv(indio_dev); |
|---|
| 294 | | - |
|---|
| 295 | | - return sprintf(buf, "%s\n", |
|---|
| 296 | | - cros_ec_accel_legacy_loc_strings[st->sensor_num + |
|---|
| 297 | | - MOTIONSENSE_LOC_BASE]); |
|---|
| 298 | | -} |
|---|
| 299 | | - |
|---|
| 300 | | -static ssize_t cros_ec_accel_legacy_id(struct iio_dev *indio_dev, |
|---|
| 301 | | - uintptr_t private, |
|---|
| 302 | | - const struct iio_chan_spec *chan, |
|---|
| 303 | | - char *buf) |
|---|
| 304 | | -{ |
|---|
| 305 | | - struct cros_ec_accel_legacy_state *st = iio_priv(indio_dev); |
|---|
| 306 | | - |
|---|
| 307 | | - return sprintf(buf, "%d\n", st->sensor_num); |
|---|
| 308 | | -} |
|---|
| 309 | | - |
|---|
| 310 | | -static const struct iio_chan_spec_ext_info cros_ec_accel_legacy_ext_info[] = { |
|---|
| 311 | | - { |
|---|
| 312 | | - .name = "id", |
|---|
| 313 | | - .shared = IIO_SHARED_BY_ALL, |
|---|
| 314 | | - .read = cros_ec_accel_legacy_id, |
|---|
| 315 | | - }, |
|---|
| 316 | | - { |
|---|
| 317 | | - .name = "location", |
|---|
| 318 | | - .shared = IIO_SHARED_BY_ALL, |
|---|
| 319 | | - .read = cros_ec_accel_legacy_loc, |
|---|
| 320 | | - }, |
|---|
| 321 | | - { } |
|---|
| 322 | | -}; |
|---|
| 172 | +#define CROS_EC_ACCEL_ROTATE_AXIS(_axis) \ |
|---|
| 173 | + ((_axis) == CROS_EC_SENSOR_Z ? CROS_EC_SENSOR_Z : \ |
|---|
| 174 | + ((_axis) == CROS_EC_SENSOR_X ? CROS_EC_SENSOR_Y : \ |
|---|
| 175 | + CROS_EC_SENSOR_X)) |
|---|
| 323 | 176 | |
|---|
| 324 | 177 | #define CROS_EC_ACCEL_LEGACY_CHAN(_axis) \ |
|---|
| 325 | 178 | { \ |
|---|
| .. | .. |
|---|
| 329 | 182 | .info_mask_separate = \ |
|---|
| 330 | 183 | BIT(IIO_CHAN_INFO_RAW) | \ |
|---|
| 331 | 184 | BIT(IIO_CHAN_INFO_CALIBBIAS), \ |
|---|
| 332 | | - .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE), \ |
|---|
| 333 | | - .ext_info = cros_ec_accel_legacy_ext_info, \ |
|---|
| 185 | + .info_mask_shared_by_all = \ |
|---|
| 186 | + BIT(IIO_CHAN_INFO_SCALE) | \ |
|---|
| 187 | + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
|---|
| 188 | + .info_mask_shared_by_all_available = \ |
|---|
| 189 | + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
|---|
| 190 | + .ext_info = cros_ec_sensors_ext_info, \ |
|---|
| 334 | 191 | .scan_type = { \ |
|---|
| 335 | 192 | .sign = 's', \ |
|---|
| 336 | | - .realbits = 16, \ |
|---|
| 337 | | - .storagebits = 16, \ |
|---|
| 193 | + .realbits = CROS_EC_SENSOR_BITS, \ |
|---|
| 194 | + .storagebits = CROS_EC_SENSOR_BITS, \ |
|---|
| 338 | 195 | }, \ |
|---|
| 196 | + .scan_index = CROS_EC_ACCEL_ROTATE_AXIS(_axis), \ |
|---|
| 339 | 197 | } \ |
|---|
| 340 | 198 | |
|---|
| 341 | | -static struct iio_chan_spec ec_accel_channels[] = { |
|---|
| 342 | | - CROS_EC_ACCEL_LEGACY_CHAN(X), |
|---|
| 343 | | - CROS_EC_ACCEL_LEGACY_CHAN(Y), |
|---|
| 344 | | - CROS_EC_ACCEL_LEGACY_CHAN(Z), |
|---|
| 345 | | - IIO_CHAN_SOFT_TIMESTAMP(MAX_AXIS) |
|---|
| 199 | +static const struct iio_chan_spec cros_ec_accel_legacy_channels[] = { |
|---|
| 200 | + CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_X), |
|---|
| 201 | + CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_Y), |
|---|
| 202 | + CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_Z), |
|---|
| 203 | + IIO_CHAN_SOFT_TIMESTAMP(CROS_EC_SENSOR_MAX_AXIS) |
|---|
| 346 | 204 | }; |
|---|
| 347 | 205 | |
|---|
| 348 | 206 | static int cros_ec_accel_legacy_probe(struct platform_device *pdev) |
|---|
| 349 | 207 | { |
|---|
| 350 | 208 | struct device *dev = &pdev->dev; |
|---|
| 351 | | - struct cros_ec_dev *ec = dev_get_drvdata(dev->parent); |
|---|
| 352 | | - struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev); |
|---|
| 353 | 209 | struct iio_dev *indio_dev; |
|---|
| 354 | | - struct cros_ec_accel_legacy_state *state; |
|---|
| 355 | | - int ret, i; |
|---|
| 356 | | - |
|---|
| 357 | | - if (!ec || !ec->ec_dev) { |
|---|
| 358 | | - dev_warn(&pdev->dev, "No EC device found.\n"); |
|---|
| 359 | | - return -EINVAL; |
|---|
| 360 | | - } |
|---|
| 361 | | - |
|---|
| 362 | | - if (!ec->ec_dev->cmd_readmem) { |
|---|
| 363 | | - dev_warn(&pdev->dev, "EC does not support direct reads.\n"); |
|---|
| 364 | | - return -EINVAL; |
|---|
| 365 | | - } |
|---|
| 210 | + struct cros_ec_sensors_core_state *state; |
|---|
| 211 | + int ret; |
|---|
| 366 | 212 | |
|---|
| 367 | 213 | indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state)); |
|---|
| 368 | 214 | if (!indio_dev) |
|---|
| 369 | 215 | return -ENOMEM; |
|---|
| 370 | 216 | |
|---|
| 371 | | - platform_set_drvdata(pdev, indio_dev); |
|---|
| 372 | | - state = iio_priv(indio_dev); |
|---|
| 373 | | - state->ec = ec->ec_dev; |
|---|
| 374 | | - state->sensor_num = sensor_platform->sensor_num; |
|---|
| 375 | | - |
|---|
| 376 | | - indio_dev->dev.parent = dev; |
|---|
| 377 | | - indio_dev->name = pdev->name; |
|---|
| 378 | | - indio_dev->channels = ec_accel_channels; |
|---|
| 379 | | - /* |
|---|
| 380 | | - * Present the channel using HTML5 standard: |
|---|
| 381 | | - * need to invert X and Y and invert some lid axis. |
|---|
| 382 | | - */ |
|---|
| 383 | | - for (i = X ; i < MAX_AXIS; i++) { |
|---|
| 384 | | - switch (i) { |
|---|
| 385 | | - case X: |
|---|
| 386 | | - ec_accel_channels[X].scan_index = Y; |
|---|
| 387 | | - case Y: |
|---|
| 388 | | - ec_accel_channels[Y].scan_index = X; |
|---|
| 389 | | - case Z: |
|---|
| 390 | | - ec_accel_channels[Z].scan_index = Z; |
|---|
| 391 | | - } |
|---|
| 392 | | - if (state->sensor_num == MOTIONSENSE_LOC_LID && i != Y) |
|---|
| 393 | | - state->sign[i] = -1; |
|---|
| 394 | | - else |
|---|
| 395 | | - state->sign[i] = 1; |
|---|
| 396 | | - } |
|---|
| 397 | | - indio_dev->num_channels = ARRAY_SIZE(ec_accel_channels); |
|---|
| 398 | | - indio_dev->dev.parent = &pdev->dev; |
|---|
| 399 | | - indio_dev->info = &cros_ec_accel_legacy_info; |
|---|
| 400 | | - indio_dev->modes = INDIO_DIRECT_MODE; |
|---|
| 401 | | - |
|---|
| 402 | | - ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, |
|---|
| 403 | | - cros_ec_accel_legacy_capture, |
|---|
| 404 | | - NULL); |
|---|
| 217 | + ret = cros_ec_sensors_core_init(pdev, indio_dev, true, |
|---|
| 218 | + cros_ec_sensors_capture, NULL, false); |
|---|
| 405 | 219 | if (ret) |
|---|
| 406 | 220 | return ret; |
|---|
| 221 | + |
|---|
| 222 | + indio_dev->info = &cros_ec_accel_legacy_info; |
|---|
| 223 | + state = iio_priv(indio_dev); |
|---|
| 224 | + |
|---|
| 225 | + if (state->ec->cmd_readmem != NULL) |
|---|
| 226 | + state->read_ec_sensors_data = cros_ec_sensors_read_lpc; |
|---|
| 227 | + else |
|---|
| 228 | + state->read_ec_sensors_data = cros_ec_accel_legacy_read_cmd; |
|---|
| 229 | + |
|---|
| 230 | + indio_dev->channels = cros_ec_accel_legacy_channels; |
|---|
| 231 | + indio_dev->num_channels = ARRAY_SIZE(cros_ec_accel_legacy_channels); |
|---|
| 232 | + /* The lid sensor needs to be presented inverted. */ |
|---|
| 233 | + if (state->loc == MOTIONSENSE_LOC_LID) { |
|---|
| 234 | + state->sign[CROS_EC_SENSOR_X] = -1; |
|---|
| 235 | + state->sign[CROS_EC_SENSOR_Z] = -1; |
|---|
| 236 | + } |
|---|
| 407 | 237 | |
|---|
| 408 | 238 | return devm_iio_device_register(dev, indio_dev); |
|---|
| 409 | 239 | } |
|---|
| .. | .. |
|---|
| 418 | 248 | |
|---|
| 419 | 249 | MODULE_DESCRIPTION("ChromeOS EC legacy accelerometer driver"); |
|---|
| 420 | 250 | MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>"); |
|---|
| 421 | | -MODULE_LICENSE("GPL"); |
|---|
| 251 | +MODULE_LICENSE("GPL v2"); |
|---|
| 422 | 252 | MODULE_ALIAS("platform:" DRV_NAME); |
|---|