Enric Balletbo i Serra | 37aa055 | 2019-03-13 12:41:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 2 | /* |
| 3 | * cros_ec_baro - Driver for barometer sensor behind CrosEC. |
| 4 | * |
| 5 | * Copyright (C) 2017 Google, Inc |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 8 | #include <linux/device.h> |
| 9 | #include <linux/iio/buffer.h> |
Gwendal Grignou | 5a0b8cb | 2018-03-13 14:23:28 -0700 | [diff] [blame] | 10 | #include <linux/iio/common/cros_ec_sensors_core.h> |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 11 | #include <linux/iio/iio.h> |
| 12 | #include <linux/iio/kfifo_buf.h> |
| 13 | #include <linux/iio/trigger.h> |
| 14 | #include <linux/iio/triggered_buffer.h> |
| 15 | #include <linux/iio/trigger_consumer.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/mfd/cros_ec.h> |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/slab.h> |
Enric Balletbo i Serra | 840d9f1 | 2019-09-02 11:53:05 +0200 | [diff] [blame] | 20 | #include <linux/platform_data/cros_ec_commands.h> |
| 21 | #include <linux/platform_data/cros_ec_proto.h> |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
| 23 | |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 24 | /* |
| 25 | * One channel for pressure, the other for timestamp. |
| 26 | */ |
| 27 | #define CROS_EC_BARO_MAX_CHANNELS (1 + 1) |
| 28 | |
| 29 | /* State data for ec_sensors iio driver. */ |
| 30 | struct cros_ec_baro_state { |
| 31 | /* Shared by all sensors */ |
| 32 | struct cros_ec_sensors_core_state core; |
| 33 | |
| 34 | struct iio_chan_spec channels[CROS_EC_BARO_MAX_CHANNELS]; |
| 35 | }; |
| 36 | |
| 37 | static int cros_ec_baro_read(struct iio_dev *indio_dev, |
| 38 | struct iio_chan_spec const *chan, |
| 39 | int *val, int *val2, long mask) |
| 40 | { |
| 41 | struct cros_ec_baro_state *st = iio_priv(indio_dev); |
| 42 | u16 data = 0; |
Gwendal Grignou | f53199c | 2019-07-18 15:22:37 -0700 | [diff] [blame] | 43 | int ret; |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 44 | int idx = chan->scan_index; |
| 45 | |
| 46 | mutex_lock(&st->core.cmd_lock); |
| 47 | |
| 48 | switch (mask) { |
| 49 | case IIO_CHAN_INFO_RAW: |
Gwendal Grignou | f53199c | 2019-07-18 15:22:37 -0700 | [diff] [blame] | 50 | ret = cros_ec_sensors_read_cmd(indio_dev, 1 << idx, |
| 51 | (s16 *)&data); |
| 52 | if (ret) |
| 53 | break; |
| 54 | |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 55 | *val = data; |
Gwendal Grignou | f53199c | 2019-07-18 15:22:37 -0700 | [diff] [blame] | 56 | ret = IIO_VAL_INT; |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 57 | break; |
| 58 | case IIO_CHAN_INFO_SCALE: |
| 59 | st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; |
| 60 | st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE; |
| 61 | |
Gwendal Grignou | f53199c | 2019-07-18 15:22:37 -0700 | [diff] [blame] | 62 | ret = cros_ec_motion_send_host_cmd(&st->core, 0); |
| 63 | if (ret) |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 64 | break; |
Gwendal Grignou | f53199c | 2019-07-18 15:22:37 -0700 | [diff] [blame] | 65 | |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 66 | *val = st->core.resp->sensor_range.ret; |
| 67 | |
| 68 | /* scale * in_pressure_raw --> kPa */ |
| 69 | *val2 = 10 << CROS_EC_SENSOR_BITS; |
| 70 | ret = IIO_VAL_FRACTIONAL; |
| 71 | break; |
| 72 | default: |
| 73 | ret = cros_ec_sensors_core_read(&st->core, chan, val, val2, |
| 74 | mask); |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | mutex_unlock(&st->core.cmd_lock); |
| 79 | |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | static int cros_ec_baro_write(struct iio_dev *indio_dev, |
| 84 | struct iio_chan_spec const *chan, |
| 85 | int val, int val2, long mask) |
| 86 | { |
| 87 | struct cros_ec_baro_state *st = iio_priv(indio_dev); |
| 88 | int ret = 0; |
| 89 | |
| 90 | mutex_lock(&st->core.cmd_lock); |
| 91 | |
| 92 | switch (mask) { |
| 93 | case IIO_CHAN_INFO_SCALE: |
| 94 | st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; |
| 95 | st->core.param.sensor_range.data = val; |
| 96 | |
| 97 | /* Always roundup, so caller gets at least what it asks for. */ |
| 98 | st->core.param.sensor_range.roundup = 1; |
| 99 | |
| 100 | if (cros_ec_motion_send_host_cmd(&st->core, 0)) |
| 101 | ret = -EIO; |
| 102 | break; |
| 103 | default: |
| 104 | ret = cros_ec_sensors_core_write(&st->core, chan, val, val2, |
| 105 | mask); |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | mutex_unlock(&st->core.cmd_lock); |
| 110 | |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | static const struct iio_info cros_ec_baro_info = { |
| 115 | .read_raw = &cros_ec_baro_read, |
| 116 | .write_raw = &cros_ec_baro_write, |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | static int cros_ec_baro_probe(struct platform_device *pdev) |
| 120 | { |
| 121 | struct device *dev = &pdev->dev; |
| 122 | struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent); |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 123 | struct iio_dev *indio_dev; |
| 124 | struct cros_ec_baro_state *state; |
| 125 | struct iio_chan_spec *channel; |
| 126 | int ret; |
| 127 | |
| 128 | if (!ec_dev || !ec_dev->ec_dev) { |
| 129 | dev_warn(dev, "No CROS EC device found.\n"); |
| 130 | return -EINVAL; |
| 131 | } |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 132 | |
| 133 | indio_dev = devm_iio_device_alloc(dev, sizeof(*state)); |
| 134 | if (!indio_dev) |
| 135 | return -ENOMEM; |
| 136 | |
| 137 | ret = cros_ec_sensors_core_init(pdev, indio_dev, true); |
| 138 | if (ret) |
| 139 | return ret; |
| 140 | |
| 141 | indio_dev->info = &cros_ec_baro_info; |
| 142 | state = iio_priv(indio_dev); |
| 143 | state->core.type = state->core.resp->info.type; |
| 144 | state->core.loc = state->core.resp->info.location; |
| 145 | channel = state->channels; |
| 146 | /* Common part */ |
| 147 | channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); |
| 148 | channel->info_mask_shared_by_all = |
| 149 | BIT(IIO_CHAN_INFO_SCALE) | |
| 150 | BIT(IIO_CHAN_INFO_SAMP_FREQ) | |
| 151 | BIT(IIO_CHAN_INFO_FREQUENCY); |
| 152 | channel->scan_type.realbits = CROS_EC_SENSOR_BITS; |
| 153 | channel->scan_type.storagebits = CROS_EC_SENSOR_BITS; |
| 154 | channel->scan_type.shift = 0; |
| 155 | channel->scan_index = 0; |
| 156 | channel->ext_info = cros_ec_sensors_ext_info; |
| 157 | channel->scan_type.sign = 'u'; |
| 158 | |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 159 | /* Sensor specific */ |
| 160 | switch (state->core.type) { |
| 161 | case MOTIONSENSE_TYPE_BARO: |
| 162 | channel->type = IIO_PRESSURE; |
| 163 | break; |
| 164 | default: |
| 165 | dev_warn(dev, "Unknown motion sensor\n"); |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | /* Timestamp */ |
| 170 | channel++; |
| 171 | channel->type = IIO_TIMESTAMP; |
| 172 | channel->channel = -1; |
| 173 | channel->scan_index = 1; |
| 174 | channel->scan_type.sign = 's'; |
| 175 | channel->scan_type.realbits = 64; |
| 176 | channel->scan_type.storagebits = 64; |
| 177 | |
| 178 | indio_dev->channels = state->channels; |
| 179 | indio_dev->num_channels = CROS_EC_BARO_MAX_CHANNELS; |
| 180 | |
| 181 | state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd; |
| 182 | |
| 183 | ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, |
| 184 | cros_ec_sensors_capture, NULL); |
| 185 | if (ret) |
| 186 | return ret; |
| 187 | |
| 188 | return devm_iio_device_register(dev, indio_dev); |
| 189 | } |
| 190 | |
| 191 | static const struct platform_device_id cros_ec_baro_ids[] = { |
| 192 | { |
| 193 | .name = "cros-ec-baro", |
| 194 | }, |
| 195 | { /* sentinel */ } |
| 196 | }; |
| 197 | MODULE_DEVICE_TABLE(platform, cros_ec_baro_ids); |
| 198 | |
| 199 | static struct platform_driver cros_ec_baro_platform_driver = { |
| 200 | .driver = { |
| 201 | .name = "cros-ec-baro", |
| 202 | }, |
| 203 | .probe = cros_ec_baro_probe, |
| 204 | .id_table = cros_ec_baro_ids, |
| 205 | }; |
| 206 | module_platform_driver(cros_ec_baro_platform_driver); |
| 207 | |
| 208 | MODULE_DESCRIPTION("ChromeOS EC barometer sensor driver"); |
| 209 | MODULE_LICENSE("GPL v2"); |