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