Gwendal Grignou | 48458b0 | 2017-01-30 18:07:53 +0100 | [diff] [blame] | 1 | /* |
| 2 | * cros_ec_light_prox - Driver for light and prox sensors behing CrosEC. |
| 3 | * |
| 4 | * Copyright (C) 2017 Google, Inc |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/iio/buffer.h> |
| 19 | #include <linux/iio/iio.h> |
| 20 | #include <linux/iio/kfifo_buf.h> |
| 21 | #include <linux/iio/trigger.h> |
| 22 | #include <linux/iio/triggered_buffer.h> |
| 23 | #include <linux/iio/trigger_consumer.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/mfd/cros_ec.h> |
| 26 | #include <linux/mfd/cros_ec_commands.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/sysfs.h> |
| 31 | |
| 32 | #include "../common/cros_ec_sensors/cros_ec_sensors_core.h" |
| 33 | |
| 34 | /* |
| 35 | * We only represent one entry for light or proximity. EC is merging different |
| 36 | * light sensors to return the what the eye would see. For proximity, we |
| 37 | * currently support only one light source. |
| 38 | */ |
| 39 | #define CROS_EC_LIGHT_PROX_MAX_CHANNELS (1 + 1) |
| 40 | |
| 41 | /* State data for ec_sensors iio driver. */ |
| 42 | struct cros_ec_light_prox_state { |
| 43 | /* Shared by all sensors */ |
| 44 | struct cros_ec_sensors_core_state core; |
| 45 | |
| 46 | struct iio_chan_spec channels[CROS_EC_LIGHT_PROX_MAX_CHANNELS]; |
| 47 | }; |
| 48 | |
| 49 | static int cros_ec_light_prox_read(struct iio_dev *indio_dev, |
| 50 | struct iio_chan_spec const *chan, |
| 51 | int *val, int *val2, long mask) |
| 52 | { |
| 53 | struct cros_ec_light_prox_state *st = iio_priv(indio_dev); |
| 54 | u16 data = 0; |
| 55 | s64 val64; |
| 56 | int ret = IIO_VAL_INT; |
| 57 | int idx = chan->scan_index; |
| 58 | |
| 59 | mutex_lock(&st->core.cmd_lock); |
| 60 | |
| 61 | switch (mask) { |
| 62 | case IIO_CHAN_INFO_RAW: |
| 63 | if (chan->type == IIO_PROXIMITY) { |
| 64 | if (cros_ec_sensors_read_cmd(indio_dev, 1 << idx, |
| 65 | (s16 *)&data) < 0) { |
| 66 | ret = -EIO; |
| 67 | break; |
| 68 | } |
| 69 | *val = data; |
| 70 | } else { |
| 71 | ret = -EINVAL; |
| 72 | } |
| 73 | break; |
| 74 | case IIO_CHAN_INFO_PROCESSED: |
| 75 | if (chan->type == IIO_LIGHT) { |
| 76 | if (cros_ec_sensors_read_cmd(indio_dev, 1 << idx, |
| 77 | (s16 *)&data) < 0) { |
| 78 | ret = -EIO; |
| 79 | break; |
| 80 | } |
| 81 | /* |
| 82 | * The data coming from the light sensor is |
| 83 | * pre-processed and represents the ambient light |
| 84 | * illuminance reading expressed in lux. |
| 85 | */ |
| 86 | *val = data; |
| 87 | ret = IIO_VAL_INT; |
| 88 | } else { |
| 89 | ret = -EINVAL; |
| 90 | } |
| 91 | break; |
| 92 | case IIO_CHAN_INFO_CALIBBIAS: |
| 93 | st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET; |
| 94 | st->core.param.sensor_offset.flags = 0; |
| 95 | |
| 96 | if (cros_ec_motion_send_host_cmd(&st->core, 0)) { |
| 97 | ret = -EIO; |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | /* Save values */ |
| 102 | st->core.calib[0] = st->core.resp->sensor_offset.offset[0]; |
| 103 | |
| 104 | *val = st->core.calib[idx]; |
| 105 | break; |
| 106 | case IIO_CHAN_INFO_CALIBSCALE: |
| 107 | /* |
| 108 | * RANGE is used for calibration |
| 109 | * scale is a number x.y, where x is coded on 16 bits, |
| 110 | * y coded on 16 bits, between 0 and 9999. |
| 111 | */ |
| 112 | st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; |
| 113 | st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE; |
| 114 | |
| 115 | if (cros_ec_motion_send_host_cmd(&st->core, 0)) { |
| 116 | ret = -EIO; |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | val64 = st->core.resp->sensor_range.ret; |
| 121 | *val = val64 >> 16; |
| 122 | *val2 = (val64 & 0xffff) * 100; |
| 123 | ret = IIO_VAL_INT_PLUS_MICRO; |
| 124 | break; |
| 125 | default: |
| 126 | ret = cros_ec_sensors_core_read(&st->core, chan, val, val2, |
| 127 | mask); |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | mutex_unlock(&st->core.cmd_lock); |
| 132 | |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | static int cros_ec_light_prox_write(struct iio_dev *indio_dev, |
| 137 | struct iio_chan_spec const *chan, |
| 138 | int val, int val2, long mask) |
| 139 | { |
| 140 | struct cros_ec_light_prox_state *st = iio_priv(indio_dev); |
| 141 | int ret = 0; |
| 142 | int idx = chan->scan_index; |
| 143 | |
| 144 | mutex_lock(&st->core.cmd_lock); |
| 145 | |
| 146 | switch (mask) { |
| 147 | case IIO_CHAN_INFO_CALIBBIAS: |
| 148 | st->core.calib[idx] = val; |
| 149 | /* Send to EC for each axis, even if not complete */ |
| 150 | st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET; |
| 151 | st->core.param.sensor_offset.flags = MOTION_SENSE_SET_OFFSET; |
| 152 | st->core.param.sensor_offset.offset[0] = st->core.calib[0]; |
| 153 | st->core.param.sensor_offset.temp = |
| 154 | EC_MOTION_SENSE_INVALID_CALIB_TEMP; |
| 155 | if (cros_ec_motion_send_host_cmd(&st->core, 0)) |
| 156 | ret = -EIO; |
| 157 | break; |
| 158 | case IIO_CHAN_INFO_CALIBSCALE: |
| 159 | st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; |
| 160 | st->core.param.sensor_range.data = (val << 16) | (val2 / 100); |
| 161 | if (cros_ec_motion_send_host_cmd(&st->core, 0)) |
| 162 | ret = -EIO; |
| 163 | break; |
| 164 | default: |
| 165 | ret = cros_ec_sensors_core_write(&st->core, chan, val, val2, |
| 166 | mask); |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | mutex_unlock(&st->core.cmd_lock); |
| 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | static const struct iio_info cros_ec_light_prox_info = { |
| 176 | .read_raw = &cros_ec_light_prox_read, |
| 177 | .write_raw = &cros_ec_light_prox_write, |
Gwendal Grignou | 48458b0 | 2017-01-30 18:07:53 +0100 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | static int cros_ec_light_prox_probe(struct platform_device *pdev) |
| 181 | { |
| 182 | struct device *dev = &pdev->dev; |
| 183 | struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent); |
| 184 | struct cros_ec_device *ec_device; |
| 185 | struct iio_dev *indio_dev; |
| 186 | struct cros_ec_light_prox_state *state; |
| 187 | struct iio_chan_spec *channel; |
| 188 | int ret; |
| 189 | |
| 190 | if (!ec_dev || !ec_dev->ec_dev) { |
| 191 | dev_warn(dev, "No CROS EC device found.\n"); |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | ec_device = ec_dev->ec_dev; |
| 195 | |
| 196 | indio_dev = devm_iio_device_alloc(dev, sizeof(*state)); |
| 197 | if (!indio_dev) |
| 198 | return -ENOMEM; |
| 199 | |
| 200 | ret = cros_ec_sensors_core_init(pdev, indio_dev, true); |
| 201 | if (ret) |
| 202 | return ret; |
| 203 | |
| 204 | indio_dev->info = &cros_ec_light_prox_info; |
| 205 | state = iio_priv(indio_dev); |
| 206 | state->core.type = state->core.resp->info.type; |
| 207 | state->core.loc = state->core.resp->info.location; |
| 208 | channel = state->channels; |
| 209 | |
| 210 | /* Common part */ |
| 211 | channel->info_mask_shared_by_all = |
| 212 | BIT(IIO_CHAN_INFO_SAMP_FREQ) | |
| 213 | BIT(IIO_CHAN_INFO_FREQUENCY); |
| 214 | channel->scan_type.realbits = CROS_EC_SENSOR_BITS; |
| 215 | channel->scan_type.storagebits = CROS_EC_SENSOR_BITS; |
| 216 | channel->scan_type.shift = 0; |
| 217 | channel->scan_index = 0; |
| 218 | channel->ext_info = cros_ec_sensors_ext_info; |
| 219 | channel->scan_type.sign = 'u'; |
| 220 | |
| 221 | state->core.calib[0] = 0; |
| 222 | |
| 223 | /* Sensor specific */ |
| 224 | switch (state->core.type) { |
| 225 | case MOTIONSENSE_TYPE_LIGHT: |
| 226 | channel->type = IIO_LIGHT; |
| 227 | channel->info_mask_separate = |
| 228 | BIT(IIO_CHAN_INFO_PROCESSED) | |
| 229 | BIT(IIO_CHAN_INFO_CALIBBIAS) | |
| 230 | BIT(IIO_CHAN_INFO_CALIBSCALE); |
| 231 | break; |
| 232 | case MOTIONSENSE_TYPE_PROX: |
| 233 | channel->type = IIO_PROXIMITY; |
| 234 | channel->info_mask_separate = |
| 235 | BIT(IIO_CHAN_INFO_RAW) | |
| 236 | BIT(IIO_CHAN_INFO_CALIBBIAS) | |
| 237 | BIT(IIO_CHAN_INFO_CALIBSCALE); |
| 238 | break; |
| 239 | default: |
| 240 | dev_warn(dev, "Unknown motion sensor\n"); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | |
| 244 | /* Timestamp */ |
| 245 | channel++; |
| 246 | channel->type = IIO_TIMESTAMP; |
| 247 | channel->channel = -1; |
| 248 | channel->scan_index = 1; |
| 249 | channel->scan_type.sign = 's'; |
| 250 | channel->scan_type.realbits = 64; |
| 251 | channel->scan_type.storagebits = 64; |
| 252 | |
| 253 | indio_dev->channels = state->channels; |
| 254 | |
| 255 | indio_dev->num_channels = CROS_EC_LIGHT_PROX_MAX_CHANNELS; |
| 256 | |
| 257 | state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd; |
| 258 | |
| 259 | ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, |
| 260 | cros_ec_sensors_capture, NULL); |
| 261 | if (ret) |
| 262 | return ret; |
| 263 | |
| 264 | return devm_iio_device_register(dev, indio_dev); |
| 265 | } |
| 266 | |
| 267 | static const struct platform_device_id cros_ec_light_prox_ids[] = { |
| 268 | { |
| 269 | .name = "cros-ec-prox", |
| 270 | }, |
| 271 | { |
| 272 | .name = "cros-ec-light", |
| 273 | }, |
| 274 | { /* sentinel */ } |
| 275 | }; |
| 276 | MODULE_DEVICE_TABLE(platform, cros_ec_light_prox_ids); |
| 277 | |
| 278 | static struct platform_driver cros_ec_light_prox_platform_driver = { |
| 279 | .driver = { |
| 280 | .name = "cros-ec-light-prox", |
| 281 | }, |
| 282 | .probe = cros_ec_light_prox_probe, |
| 283 | .id_table = cros_ec_light_prox_ids, |
| 284 | }; |
| 285 | module_platform_driver(cros_ec_light_prox_platform_driver); |
| 286 | |
| 287 | MODULE_DESCRIPTION("ChromeOS EC light/proximity sensors driver"); |
| 288 | MODULE_LICENSE("GPL v2"); |