2 * cros_ec_light_prox - Driver for light and prox sensors behing CrosEC.
4 * Copyright (C) 2017 Google, Inc
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.
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.
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>
32 #include "../common/cros_ec_sensors/cros_ec_sensors_core.h"
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.
39 #define CROS_EC_LIGHT_PROX_MAX_CHANNELS (1 + 1)
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;
46 struct iio_chan_spec channels[CROS_EC_LIGHT_PROX_MAX_CHANNELS];
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)
53 struct cros_ec_light_prox_state *st = iio_priv(indio_dev);
56 int ret = IIO_VAL_INT;
57 int idx = chan->scan_index;
59 mutex_lock(&st->core.cmd_lock);
62 case IIO_CHAN_INFO_RAW:
63 if (chan->type == IIO_PROXIMITY) {
64 if (cros_ec_sensors_read_cmd(indio_dev, 1 << idx,
74 case IIO_CHAN_INFO_PROCESSED:
75 if (chan->type == IIO_LIGHT) {
76 if (cros_ec_sensors_read_cmd(indio_dev, 1 << idx,
82 * The data coming from the light sensor is
83 * pre-processed and represents the ambient light
84 * illuminance reading expressed in lux.
92 case IIO_CHAN_INFO_CALIBBIAS:
93 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET;
94 st->core.param.sensor_offset.flags = 0;
96 if (cros_ec_motion_send_host_cmd(&st->core, 0)) {
102 st->core.calib[0] = st->core.resp->sensor_offset.offset[0];
104 *val = st->core.calib[idx];
106 case IIO_CHAN_INFO_CALIBSCALE:
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.
112 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
113 st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE;
115 if (cros_ec_motion_send_host_cmd(&st->core, 0)) {
120 val64 = st->core.resp->sensor_range.ret;
122 *val2 = (val64 & 0xffff) * 100;
123 ret = IIO_VAL_INT_PLUS_MICRO;
126 ret = cros_ec_sensors_core_read(&st->core, chan, val, val2,
131 mutex_unlock(&st->core.cmd_lock);
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)
140 struct cros_ec_light_prox_state *st = iio_priv(indio_dev);
142 int idx = chan->scan_index;
144 mutex_lock(&st->core.cmd_lock);
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))
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))
165 ret = cros_ec_sensors_core_write(&st->core, chan, val, val2,
170 mutex_unlock(&st->core.cmd_lock);
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,
181 static int cros_ec_light_prox_probe(struct platform_device *pdev)
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;
191 if (!ec_dev || !ec_dev->ec_dev) {
192 dev_warn(dev, "No CROS EC device found.\n");
195 ec_device = ec_dev->ec_dev;
197 indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
201 ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
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;
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';
222 state->core.calib[0] = 0;
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);
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);
241 dev_warn(dev, "Unknown motion sensor\n");
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;
254 indio_dev->channels = state->channels;
256 indio_dev->num_channels = CROS_EC_LIGHT_PROX_MAX_CHANNELS;
258 state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
260 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
261 cros_ec_sensors_capture, NULL);
265 return devm_iio_device_register(dev, indio_dev);
268 static const struct platform_device_id cros_ec_light_prox_ids[] = {
270 .name = "cros-ec-prox",
273 .name = "cros-ec-light",
277 MODULE_DEVICE_TABLE(platform, cros_ec_light_prox_ids);
279 static struct platform_driver cros_ec_light_prox_platform_driver = {
281 .name = "cros-ec-light-prox",
283 .probe = cros_ec_light_prox_probe,
284 .id_table = cros_ec_light_prox_ids,
286 module_platform_driver(cros_ec_light_prox_platform_driver);
288 MODULE_DESCRIPTION("ChromeOS EC light/proximity sensors driver");
289 MODULE_LICENSE("GPL v2");