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 = IIO_VAL_INT; 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 if (cros_ec_sensors_read_cmd(indio_dev, 1 << idx, 50 (s16 *)&data) < 0) 51 ret = -EIO; 52 *val = data; 53 break; 54 case IIO_CHAN_INFO_SCALE: 55 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; 56 st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE; 57 58 if (cros_ec_motion_send_host_cmd(&st->core, 0)) { 59 ret = -EIO; 60 break; 61 } 62 *val = st->core.resp->sensor_range.ret; 63 64 /* scale * in_pressure_raw --> kPa */ 65 *val2 = 10 << CROS_EC_SENSOR_BITS; 66 ret = IIO_VAL_FRACTIONAL; 67 break; 68 default: 69 ret = cros_ec_sensors_core_read(&st->core, chan, val, val2, 70 mask); 71 break; 72 } 73 74 mutex_unlock(&st->core.cmd_lock); 75 76 return ret; 77 } 78 79 static int cros_ec_baro_write(struct iio_dev *indio_dev, 80 struct iio_chan_spec const *chan, 81 int val, int val2, long mask) 82 { 83 struct cros_ec_baro_state *st = iio_priv(indio_dev); 84 int ret = 0; 85 86 mutex_lock(&st->core.cmd_lock); 87 88 switch (mask) { 89 case IIO_CHAN_INFO_SCALE: 90 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; 91 st->core.param.sensor_range.data = val; 92 93 /* Always roundup, so caller gets at least what it asks for. */ 94 st->core.param.sensor_range.roundup = 1; 95 96 if (cros_ec_motion_send_host_cmd(&st->core, 0)) 97 ret = -EIO; 98 break; 99 default: 100 ret = cros_ec_sensors_core_write(&st->core, chan, val, val2, 101 mask); 102 break; 103 } 104 105 mutex_unlock(&st->core.cmd_lock); 106 107 return ret; 108 } 109 110 static const struct iio_info cros_ec_baro_info = { 111 .read_raw = &cros_ec_baro_read, 112 .write_raw = &cros_ec_baro_write, 113 }; 114 115 static int cros_ec_baro_probe(struct platform_device *pdev) 116 { 117 struct device *dev = &pdev->dev; 118 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent); 119 struct iio_dev *indio_dev; 120 struct cros_ec_baro_state *state; 121 struct iio_chan_spec *channel; 122 int ret; 123 124 if (!ec_dev || !ec_dev->ec_dev) { 125 dev_warn(dev, "No CROS EC device found.\n"); 126 return -EINVAL; 127 } 128 129 indio_dev = devm_iio_device_alloc(dev, sizeof(*state)); 130 if (!indio_dev) 131 return -ENOMEM; 132 133 ret = cros_ec_sensors_core_init(pdev, indio_dev, true); 134 if (ret) 135 return ret; 136 137 indio_dev->info = &cros_ec_baro_info; 138 state = iio_priv(indio_dev); 139 state->core.type = state->core.resp->info.type; 140 state->core.loc = state->core.resp->info.location; 141 channel = state->channels; 142 /* Common part */ 143 channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); 144 channel->info_mask_shared_by_all = 145 BIT(IIO_CHAN_INFO_SCALE) | 146 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 147 BIT(IIO_CHAN_INFO_FREQUENCY); 148 channel->scan_type.realbits = CROS_EC_SENSOR_BITS; 149 channel->scan_type.storagebits = CROS_EC_SENSOR_BITS; 150 channel->scan_type.shift = 0; 151 channel->scan_index = 0; 152 channel->ext_info = cros_ec_sensors_ext_info; 153 channel->scan_type.sign = 'u'; 154 155 state->core.calib[0] = 0; 156 157 /* Sensor specific */ 158 switch (state->core.type) { 159 case MOTIONSENSE_TYPE_BARO: 160 channel->type = IIO_PRESSURE; 161 break; 162 default: 163 dev_warn(dev, "Unknown motion sensor\n"); 164 return -EINVAL; 165 } 166 167 /* Timestamp */ 168 channel++; 169 channel->type = IIO_TIMESTAMP; 170 channel->channel = -1; 171 channel->scan_index = 1; 172 channel->scan_type.sign = 's'; 173 channel->scan_type.realbits = 64; 174 channel->scan_type.storagebits = 64; 175 176 indio_dev->channels = state->channels; 177 indio_dev->num_channels = CROS_EC_BARO_MAX_CHANNELS; 178 179 state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd; 180 181 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, 182 cros_ec_sensors_capture, NULL); 183 if (ret) 184 return ret; 185 186 return devm_iio_device_register(dev, indio_dev); 187 } 188 189 static const struct platform_device_id cros_ec_baro_ids[] = { 190 { 191 .name = "cros-ec-baro", 192 }, 193 { /* sentinel */ } 194 }; 195 MODULE_DEVICE_TABLE(platform, cros_ec_baro_ids); 196 197 static struct platform_driver cros_ec_baro_platform_driver = { 198 .driver = { 199 .name = "cros-ec-baro", 200 }, 201 .probe = cros_ec_baro_probe, 202 .id_table = cros_ec_baro_ids, 203 }; 204 module_platform_driver(cros_ec_baro_platform_driver); 205 206 MODULE_DESCRIPTION("ChromeOS EC barometer sensor driver"); 207 MODULE_LICENSE("GPL v2"); 208