1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * cros_ec_baro - Driver for barometer sensor behind CrosEC. 4 * 5 * Copyright (C) 2017 Google, Inc 6 */ 7 8 #include <linux/device.h> 9 #include <linux/iio/buffer.h> 10 #include <linux/iio/common/cros_ec_sensors_core.h> 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> 18 #include <linux/mfd/cros_ec_commands.h> 19 #include <linux/module.h> 20 #include <linux/slab.h> 21 #include <linux/platform_device.h> 22 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; 42 int ret; 43 int idx = chan->scan_index; 44 45 mutex_lock(&st->core.cmd_lock); 46 47 switch (mask) { 48 case IIO_CHAN_INFO_RAW: 49 ret = cros_ec_sensors_read_cmd(indio_dev, 1 << idx, 50 (s16 *)&data); 51 if (ret) 52 break; 53 54 *val = data; 55 ret = IIO_VAL_INT; 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 61 ret = cros_ec_motion_send_host_cmd(&st->core, 0); 62 if (ret) 63 break; 64 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 99 if (cros_ec_motion_send_host_cmd(&st->core, 0)) 100 ret = -EIO; 101 break; 102 default: 103 ret = cros_ec_sensors_core_write(&st->core, chan, val, val2, 104 mask); 105 break; 106 } 107 108 mutex_unlock(&st->core.cmd_lock); 109 110 return ret; 111 } 112 113 static const struct iio_info cros_ec_baro_info = { 114 .read_raw = &cros_ec_baro_read, 115 .write_raw = &cros_ec_baro_write, 116 }; 117 118 static int cros_ec_baro_probe(struct platform_device *pdev) 119 { 120 struct device *dev = &pdev->dev; 121 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent); 122 struct iio_dev *indio_dev; 123 struct cros_ec_baro_state *state; 124 struct iio_chan_spec *channel; 125 int ret; 126 127 if (!ec_dev || !ec_dev->ec_dev) { 128 dev_warn(dev, "No CROS EC device found.\n"); 129 return -EINVAL; 130 } 131 132 indio_dev = devm_iio_device_alloc(dev, sizeof(*state)); 133 if (!indio_dev) 134 return -ENOMEM; 135 136 ret = cros_ec_sensors_core_init(pdev, indio_dev, true); 137 if (ret) 138 return ret; 139 140 indio_dev->info = &cros_ec_baro_info; 141 state = iio_priv(indio_dev); 142 state->core.type = state->core.resp->info.type; 143 state->core.loc = state->core.resp->info.location; 144 channel = state->channels; 145 /* Common part */ 146 channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); 147 channel->info_mask_shared_by_all = 148 BIT(IIO_CHAN_INFO_SCALE) | 149 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 150 BIT(IIO_CHAN_INFO_FREQUENCY); 151 channel->scan_type.realbits = CROS_EC_SENSOR_BITS; 152 channel->scan_type.storagebits = CROS_EC_SENSOR_BITS; 153 channel->scan_type.shift = 0; 154 channel->scan_index = 0; 155 channel->ext_info = cros_ec_sensors_ext_info; 156 channel->scan_type.sign = 'u'; 157 158 /* Sensor specific */ 159 switch (state->core.type) { 160 case MOTIONSENSE_TYPE_BARO: 161 channel->type = IIO_PRESSURE; 162 break; 163 default: 164 dev_warn(dev, "Unknown motion sensor\n"); 165 return -EINVAL; 166 } 167 168 /* Timestamp */ 169 channel++; 170 channel->type = IIO_TIMESTAMP; 171 channel->channel = -1; 172 channel->scan_index = 1; 173 channel->scan_type.sign = 's'; 174 channel->scan_type.realbits = 64; 175 channel->scan_type.storagebits = 64; 176 177 indio_dev->channels = state->channels; 178 indio_dev->num_channels = CROS_EC_BARO_MAX_CHANNELS; 179 180 state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd; 181 182 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, 183 cros_ec_sensors_capture, NULL); 184 if (ret) 185 return ret; 186 187 return devm_iio_device_register(dev, indio_dev); 188 } 189 190 static const struct platform_device_id cros_ec_baro_ids[] = { 191 { 192 .name = "cros-ec-baro", 193 }, 194 { /* sentinel */ } 195 }; 196 MODULE_DEVICE_TABLE(platform, cros_ec_baro_ids); 197 198 static struct platform_driver cros_ec_baro_platform_driver = { 199 .driver = { 200 .name = "cros-ec-baro", 201 }, 202 .probe = cros_ec_baro_probe, 203 .id_table = cros_ec_baro_ids, 204 }; 205 module_platform_driver(cros_ec_baro_platform_driver); 206 207 MODULE_DESCRIPTION("ChromeOS EC barometer sensor driver"); 208 MODULE_LICENSE("GPL v2"); 209