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, 178 .driver_module = THIS_MODULE, 179 }; 180 181 static int cros_ec_light_prox_probe(struct platform_device *pdev) 182 { 183 struct device *dev = &pdev->dev; 184 struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent); 185 struct cros_ec_device *ec_device; 186 struct iio_dev *indio_dev; 187 struct cros_ec_light_prox_state *state; 188 struct iio_chan_spec *channel; 189 int ret; 190 191 if (!ec_dev || !ec_dev->ec_dev) { 192 dev_warn(dev, "No CROS EC device found.\n"); 193 return -EINVAL; 194 } 195 ec_device = ec_dev->ec_dev; 196 197 indio_dev = devm_iio_device_alloc(dev, sizeof(*state)); 198 if (!indio_dev) 199 return -ENOMEM; 200 201 ret = cros_ec_sensors_core_init(pdev, indio_dev, true); 202 if (ret) 203 return ret; 204 205 indio_dev->info = &cros_ec_light_prox_info; 206 state = iio_priv(indio_dev); 207 state->core.type = state->core.resp->info.type; 208 state->core.loc = state->core.resp->info.location; 209 channel = state->channels; 210 211 /* Common part */ 212 channel->info_mask_shared_by_all = 213 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 214 BIT(IIO_CHAN_INFO_FREQUENCY); 215 channel->scan_type.realbits = CROS_EC_SENSOR_BITS; 216 channel->scan_type.storagebits = CROS_EC_SENSOR_BITS; 217 channel->scan_type.shift = 0; 218 channel->scan_index = 0; 219 channel->ext_info = cros_ec_sensors_ext_info; 220 channel->scan_type.sign = 'u'; 221 222 state->core.calib[0] = 0; 223 224 /* Sensor specific */ 225 switch (state->core.type) { 226 case MOTIONSENSE_TYPE_LIGHT: 227 channel->type = IIO_LIGHT; 228 channel->info_mask_separate = 229 BIT(IIO_CHAN_INFO_PROCESSED) | 230 BIT(IIO_CHAN_INFO_CALIBBIAS) | 231 BIT(IIO_CHAN_INFO_CALIBSCALE); 232 break; 233 case MOTIONSENSE_TYPE_PROX: 234 channel->type = IIO_PROXIMITY; 235 channel->info_mask_separate = 236 BIT(IIO_CHAN_INFO_RAW) | 237 BIT(IIO_CHAN_INFO_CALIBBIAS) | 238 BIT(IIO_CHAN_INFO_CALIBSCALE); 239 break; 240 default: 241 dev_warn(dev, "Unknown motion sensor\n"); 242 return -EINVAL; 243 } 244 245 /* Timestamp */ 246 channel++; 247 channel->type = IIO_TIMESTAMP; 248 channel->channel = -1; 249 channel->scan_index = 1; 250 channel->scan_type.sign = 's'; 251 channel->scan_type.realbits = 64; 252 channel->scan_type.storagebits = 64; 253 254 indio_dev->channels = state->channels; 255 256 indio_dev->num_channels = CROS_EC_LIGHT_PROX_MAX_CHANNELS; 257 258 state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd; 259 260 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, 261 cros_ec_sensors_capture, NULL); 262 if (ret) 263 return ret; 264 265 return devm_iio_device_register(dev, indio_dev); 266 } 267 268 static const struct platform_device_id cros_ec_light_prox_ids[] = { 269 { 270 .name = "cros-ec-prox", 271 }, 272 { 273 .name = "cros-ec-light", 274 }, 275 { /* sentinel */ } 276 }; 277 MODULE_DEVICE_TABLE(platform, cros_ec_light_prox_ids); 278 279 static struct platform_driver cros_ec_light_prox_platform_driver = { 280 .driver = { 281 .name = "cros-ec-light-prox", 282 }, 283 .probe = cros_ec_light_prox_probe, 284 .id_table = cros_ec_light_prox_ids, 285 }; 286 module_platform_driver(cros_ec_light_prox_platform_driver); 287 288 MODULE_DESCRIPTION("ChromeOS EC light/proximity sensors driver"); 289 MODULE_LICENSE("GPL v2"); 290