Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Driver for older Chrome OS EC accelerometer
0004  *
0005  * Copyright 2017 Google, Inc
0006  *
0007  * This driver uses the memory mapper cros-ec interface to communicate
0008  * with the Chrome OS EC about accelerometer data or older commands.
0009  * Accelerometer access is presented through iio sysfs.
0010  */
0011 
0012 #include <linux/delay.h>
0013 #include <linux/device.h>
0014 #include <linux/iio/buffer.h>
0015 #include <linux/iio/common/cros_ec_sensors_core.h>
0016 #include <linux/iio/iio.h>
0017 #include <linux/iio/kfifo_buf.h>
0018 #include <linux/iio/trigger_consumer.h>
0019 #include <linux/iio/triggered_buffer.h>
0020 #include <linux/kernel.h>
0021 #include <linux/module.h>
0022 #include <linux/slab.h>
0023 #include <linux/platform_data/cros_ec_commands.h>
0024 #include <linux/platform_data/cros_ec_proto.h>
0025 #include <linux/platform_device.h>
0026 
0027 #define DRV_NAME    "cros-ec-accel-legacy"
0028 
0029 #define CROS_EC_SENSOR_LEGACY_NUM 2
0030 /*
0031  * Sensor scale hard coded at 10 bits per g, computed as:
0032  * g / (2^10 - 1) = 0.009586168; with g = 9.80665 m.s^-2
0033  */
0034 #define ACCEL_LEGACY_NSCALE 9586168
0035 
0036 /*
0037  * Sensor frequency is hard-coded to 10Hz.
0038  */
0039 static const int cros_ec_legacy_sample_freq[] = { 10, 0 };
0040 
0041 static int cros_ec_accel_legacy_read_cmd(struct iio_dev *indio_dev,
0042                   unsigned long scan_mask, s16 *data)
0043 {
0044     struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
0045     int ret;
0046     unsigned int i;
0047     u8 sensor_num;
0048 
0049     /*
0050      * Read all sensor data through a command.
0051      * Save sensor_num, it is assumed to stay.
0052      */
0053     sensor_num = st->param.info.sensor_num;
0054     st->param.cmd = MOTIONSENSE_CMD_DUMP;
0055     st->param.dump.max_sensor_count = CROS_EC_SENSOR_LEGACY_NUM;
0056     ret = cros_ec_motion_send_host_cmd(st,
0057             sizeof(st->resp->dump) + CROS_EC_SENSOR_LEGACY_NUM *
0058             sizeof(struct ec_response_motion_sensor_data));
0059     st->param.info.sensor_num = sensor_num;
0060     if (ret != 0) {
0061         dev_warn(&indio_dev->dev, "Unable to read sensor data\n");
0062         return ret;
0063     }
0064 
0065     for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
0066         *data = st->resp->dump.sensor[sensor_num].data[i] *
0067             st->sign[i];
0068         data++;
0069     }
0070 
0071     return 0;
0072 }
0073 
0074 static int cros_ec_accel_legacy_read(struct iio_dev *indio_dev,
0075                      struct iio_chan_spec const *chan,
0076                      int *val, int *val2, long mask)
0077 {
0078     struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
0079     s16 data = 0;
0080     int ret;
0081     int idx = chan->scan_index;
0082 
0083     mutex_lock(&st->cmd_lock);
0084 
0085     switch (mask) {
0086     case IIO_CHAN_INFO_RAW:
0087         ret = st->read_ec_sensors_data(indio_dev, 1 << idx, &data);
0088         if (ret < 0)
0089             break;
0090         ret = IIO_VAL_INT;
0091         *val = data;
0092         break;
0093     case IIO_CHAN_INFO_SCALE:
0094         WARN_ON(st->type != MOTIONSENSE_TYPE_ACCEL);
0095         *val = 0;
0096         *val2 = ACCEL_LEGACY_NSCALE;
0097         ret = IIO_VAL_INT_PLUS_NANO;
0098         break;
0099     case IIO_CHAN_INFO_CALIBBIAS:
0100         /* Calibration not supported. */
0101         *val = 0;
0102         ret = IIO_VAL_INT;
0103         break;
0104     case IIO_CHAN_INFO_SAMP_FREQ:
0105         *val = cros_ec_legacy_sample_freq[0];
0106         *val2 = cros_ec_legacy_sample_freq[1];
0107         ret = IIO_VAL_INT_PLUS_MICRO;
0108         break;
0109     default:
0110         ret = cros_ec_sensors_core_read(st, chan, val, val2,
0111                 mask);
0112         break;
0113     }
0114     mutex_unlock(&st->cmd_lock);
0115 
0116     return ret;
0117 }
0118 
0119 static int cros_ec_accel_legacy_write(struct iio_dev *indio_dev,
0120                       struct iio_chan_spec const *chan,
0121                       int val, int val2, long mask)
0122 {
0123     /*
0124      * Do nothing but don't return an error code to allow calibration
0125      * script to work.
0126      */
0127     if (mask == IIO_CHAN_INFO_CALIBBIAS)
0128         return 0;
0129 
0130     return -EINVAL;
0131 }
0132 
0133 /**
0134  * cros_ec_accel_legacy_read_avail() - get available values
0135  * @indio_dev:      pointer to state information for device
0136  * @chan:   channel specification structure table
0137  * @vals:   list of available values
0138  * @type:   type of data returned
0139  * @length: number of data returned in the array
0140  * @mask:   specifies which values to be requested
0141  *
0142  * Return:  an error code or IIO_AVAIL_LIST
0143  */
0144 static int cros_ec_accel_legacy_read_avail(struct iio_dev *indio_dev,
0145                        struct iio_chan_spec const *chan,
0146                        const int **vals,
0147                        int *type,
0148                        int *length,
0149                        long mask)
0150 {
0151     switch (mask) {
0152     case IIO_CHAN_INFO_SAMP_FREQ:
0153         *length = ARRAY_SIZE(cros_ec_legacy_sample_freq);
0154         *vals = cros_ec_legacy_sample_freq;
0155         *type = IIO_VAL_INT_PLUS_MICRO;
0156         return IIO_AVAIL_LIST;
0157     }
0158 
0159     return -EINVAL;
0160 }
0161 
0162 static const struct iio_info cros_ec_accel_legacy_info = {
0163     .read_raw = &cros_ec_accel_legacy_read,
0164     .write_raw = &cros_ec_accel_legacy_write,
0165     .read_avail = &cros_ec_accel_legacy_read_avail,
0166 };
0167 
0168 /*
0169  * Present the channel using HTML5 standard:
0170  * need to invert X and Y and invert some lid axis.
0171  */
0172 #define CROS_EC_ACCEL_ROTATE_AXIS(_axis)                \
0173     ((_axis) == CROS_EC_SENSOR_Z ? CROS_EC_SENSOR_Z :       \
0174      ((_axis) == CROS_EC_SENSOR_X ? CROS_EC_SENSOR_Y :      \
0175       CROS_EC_SENSOR_X))
0176 
0177 #define CROS_EC_ACCEL_LEGACY_CHAN(_axis)                \
0178     {                               \
0179         .type = IIO_ACCEL,                  \
0180         .channel2 = IIO_MOD_X + (_axis),            \
0181         .modified = 1,                          \
0182         .info_mask_separate =                   \
0183             BIT(IIO_CHAN_INFO_RAW) |            \
0184             BIT(IIO_CHAN_INFO_CALIBBIAS),           \
0185         .info_mask_shared_by_all =              \
0186             BIT(IIO_CHAN_INFO_SCALE) |          \
0187             BIT(IIO_CHAN_INFO_SAMP_FREQ),           \
0188         .info_mask_shared_by_all_available =            \
0189             BIT(IIO_CHAN_INFO_SAMP_FREQ),           \
0190         .ext_info = cros_ec_sensors_ext_info,           \
0191         .scan_type = {                      \
0192             .sign = 's',                    \
0193             .realbits = CROS_EC_SENSOR_BITS,        \
0194             .storagebits = CROS_EC_SENSOR_BITS,     \
0195         },                          \
0196         .scan_index = CROS_EC_ACCEL_ROTATE_AXIS(_axis),     \
0197     }                               \
0198 
0199 static const struct iio_chan_spec cros_ec_accel_legacy_channels[] = {
0200         CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_X),
0201         CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_Y),
0202         CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_Z),
0203         IIO_CHAN_SOFT_TIMESTAMP(CROS_EC_SENSOR_MAX_AXIS)
0204 };
0205 
0206 static int cros_ec_accel_legacy_probe(struct platform_device *pdev)
0207 {
0208     struct device *dev = &pdev->dev;
0209     struct iio_dev *indio_dev;
0210     struct cros_ec_sensors_core_state *state;
0211     int ret;
0212 
0213     indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state));
0214     if (!indio_dev)
0215         return -ENOMEM;
0216 
0217     ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
0218                     cros_ec_sensors_capture);
0219     if (ret)
0220         return ret;
0221 
0222     indio_dev->info = &cros_ec_accel_legacy_info;
0223     state = iio_priv(indio_dev);
0224 
0225     if (state->ec->cmd_readmem != NULL)
0226         state->read_ec_sensors_data = cros_ec_sensors_read_lpc;
0227     else
0228         state->read_ec_sensors_data = cros_ec_accel_legacy_read_cmd;
0229 
0230     indio_dev->channels = cros_ec_accel_legacy_channels;
0231     indio_dev->num_channels = ARRAY_SIZE(cros_ec_accel_legacy_channels);
0232     /* The lid sensor needs to be presented inverted. */
0233     if (!strcmp(indio_dev->label, "accel-display")) {
0234         state->sign[CROS_EC_SENSOR_X] = -1;
0235         state->sign[CROS_EC_SENSOR_Z] = -1;
0236     }
0237 
0238     return cros_ec_sensors_core_register(dev, indio_dev, NULL);
0239 }
0240 
0241 static struct platform_driver cros_ec_accel_platform_driver = {
0242     .driver = {
0243         .name   = DRV_NAME,
0244     },
0245     .probe      = cros_ec_accel_legacy_probe,
0246 };
0247 module_platform_driver(cros_ec_accel_platform_driver);
0248 
0249 MODULE_DESCRIPTION("ChromeOS EC legacy accelerometer driver");
0250 MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
0251 MODULE_LICENSE("GPL v2");
0252 MODULE_ALIAS("platform:" DRV_NAME);