1 // SPDX-License-Identifier: GPL-2.0
3 * cros_ec_light_prox - Driver for light and prox sensors behing CrosEC.
5 * Copyright (C) 2017 Google, Inc
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/module.h>
18 #include <linux/platform_data/cros_ec_commands.h>
19 #include <linux/platform_data/cros_ec_proto.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
24 * We only represent one entry for light or proximity. EC is merging different
25 * light sensors to return the what the eye would see. For proximity, we
26 * currently support only one light source.
28 #define CROS_EC_LIGHT_PROX_MAX_CHANNELS (1 + 1)
30 /* State data for ec_sensors iio driver. */
31 struct cros_ec_light_prox_state
{
32 /* Shared by all sensors */
33 struct cros_ec_sensors_core_state core
;
35 struct iio_chan_spec channels
[CROS_EC_LIGHT_PROX_MAX_CHANNELS
];
38 static int cros_ec_light_prox_read(struct iio_dev
*indio_dev
,
39 struct iio_chan_spec
const *chan
,
40 int *val
, int *val2
, long mask
)
42 struct cros_ec_light_prox_state
*st
= iio_priv(indio_dev
);
46 int idx
= chan
->scan_index
;
48 mutex_lock(&st
->core
.cmd_lock
);
51 case IIO_CHAN_INFO_RAW
:
52 if (chan
->type
== IIO_PROXIMITY
) {
53 ret
= cros_ec_sensors_read_cmd(indio_dev
, 1 << idx
,
63 case IIO_CHAN_INFO_PROCESSED
:
64 if (chan
->type
== IIO_LIGHT
) {
65 ret
= cros_ec_sensors_read_cmd(indio_dev
, 1 << idx
,
70 * The data coming from the light sensor is
71 * pre-processed and represents the ambient light
72 * illuminance reading expressed in lux.
80 case IIO_CHAN_INFO_CALIBBIAS
:
81 st
->core
.param
.cmd
= MOTIONSENSE_CMD_SENSOR_OFFSET
;
82 st
->core
.param
.sensor_offset
.flags
= 0;
84 ret
= cros_ec_motion_send_host_cmd(&st
->core
, 0);
89 st
->core
.calib
[0].offset
=
90 st
->core
.resp
->sensor_offset
.offset
[0];
92 *val
= st
->core
.calib
[idx
].offset
;
95 case IIO_CHAN_INFO_CALIBSCALE
:
97 * RANGE is used for calibration
98 * scale is a number x.y, where x is coded on 16 bits,
99 * y coded on 16 bits, between 0 and 9999.
101 st
->core
.param
.cmd
= MOTIONSENSE_CMD_SENSOR_RANGE
;
102 st
->core
.param
.sensor_range
.data
= EC_MOTION_SENSE_NO_VALUE
;
104 ret
= cros_ec_motion_send_host_cmd(&st
->core
, 0);
108 val64
= st
->core
.resp
->sensor_range
.ret
;
110 *val2
= (val64
& 0xffff) * 100;
111 ret
= IIO_VAL_INT_PLUS_MICRO
;
114 ret
= cros_ec_sensors_core_read(&st
->core
, chan
, val
, val2
,
119 mutex_unlock(&st
->core
.cmd_lock
);
124 static int cros_ec_light_prox_write(struct iio_dev
*indio_dev
,
125 struct iio_chan_spec
const *chan
,
126 int val
, int val2
, long mask
)
128 struct cros_ec_light_prox_state
*st
= iio_priv(indio_dev
);
130 int idx
= chan
->scan_index
;
132 mutex_lock(&st
->core
.cmd_lock
);
135 case IIO_CHAN_INFO_CALIBBIAS
:
136 st
->core
.calib
[idx
].offset
= val
;
137 /* Send to EC for each axis, even if not complete */
138 st
->core
.param
.cmd
= MOTIONSENSE_CMD_SENSOR_OFFSET
;
139 st
->core
.param
.sensor_offset
.flags
= MOTION_SENSE_SET_OFFSET
;
140 st
->core
.param
.sensor_offset
.offset
[0] =
141 st
->core
.calib
[0].offset
;
142 st
->core
.param
.sensor_offset
.temp
=
143 EC_MOTION_SENSE_INVALID_CALIB_TEMP
;
144 ret
= cros_ec_motion_send_host_cmd(&st
->core
, 0);
146 case IIO_CHAN_INFO_CALIBSCALE
:
147 st
->core
.param
.cmd
= MOTIONSENSE_CMD_SENSOR_RANGE
;
148 st
->core
.param
.sensor_range
.data
= (val
<< 16) | (val2
/ 100);
149 ret
= cros_ec_motion_send_host_cmd(&st
->core
, 0);
152 ret
= cros_ec_sensors_core_write(&st
->core
, chan
, val
, val2
,
157 mutex_unlock(&st
->core
.cmd_lock
);
162 static const struct iio_info cros_ec_light_prox_info
= {
163 .read_raw
= &cros_ec_light_prox_read
,
164 .write_raw
= &cros_ec_light_prox_write
,
165 .read_avail
= &cros_ec_sensors_core_read_avail
,
168 static int cros_ec_light_prox_probe(struct platform_device
*pdev
)
170 struct device
*dev
= &pdev
->dev
;
171 struct iio_dev
*indio_dev
;
172 struct cros_ec_light_prox_state
*state
;
173 struct iio_chan_spec
*channel
;
176 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*state
));
180 ret
= cros_ec_sensors_core_init(pdev
, indio_dev
, true,
181 cros_ec_sensors_capture
,
182 cros_ec_sensors_push_data
);
186 iio_buffer_set_attrs(indio_dev
->buffer
, cros_ec_sensor_fifo_attributes
);
188 indio_dev
->info
= &cros_ec_light_prox_info
;
189 state
= iio_priv(indio_dev
);
190 state
->core
.type
= state
->core
.resp
->info
.type
;
191 state
->core
.loc
= state
->core
.resp
->info
.location
;
192 channel
= state
->channels
;
195 channel
->info_mask_shared_by_all
=
196 BIT(IIO_CHAN_INFO_SAMP_FREQ
);
197 channel
->info_mask_shared_by_all_available
=
198 BIT(IIO_CHAN_INFO_SAMP_FREQ
);
199 channel
->scan_type
.realbits
= CROS_EC_SENSOR_BITS
;
200 channel
->scan_type
.storagebits
= CROS_EC_SENSOR_BITS
;
201 channel
->scan_type
.shift
= 0;
202 channel
->scan_index
= 0;
203 channel
->ext_info
= cros_ec_sensors_ext_info
;
204 channel
->scan_type
.sign
= 'u';
206 /* Sensor specific */
207 switch (state
->core
.type
) {
208 case MOTIONSENSE_TYPE_LIGHT
:
209 channel
->type
= IIO_LIGHT
;
210 channel
->info_mask_separate
=
211 BIT(IIO_CHAN_INFO_PROCESSED
) |
212 BIT(IIO_CHAN_INFO_CALIBBIAS
) |
213 BIT(IIO_CHAN_INFO_CALIBSCALE
);
215 case MOTIONSENSE_TYPE_PROX
:
216 channel
->type
= IIO_PROXIMITY
;
217 channel
->info_mask_separate
=
218 BIT(IIO_CHAN_INFO_RAW
) |
219 BIT(IIO_CHAN_INFO_CALIBBIAS
) |
220 BIT(IIO_CHAN_INFO_CALIBSCALE
);
223 dev_warn(dev
, "Unknown motion sensor\n");
229 channel
->type
= IIO_TIMESTAMP
;
230 channel
->channel
= -1;
231 channel
->scan_index
= 1;
232 channel
->scan_type
.sign
= 's';
233 channel
->scan_type
.realbits
= 64;
234 channel
->scan_type
.storagebits
= 64;
236 indio_dev
->channels
= state
->channels
;
238 indio_dev
->num_channels
= CROS_EC_LIGHT_PROX_MAX_CHANNELS
;
240 state
->core
.read_ec_sensors_data
= cros_ec_sensors_read_cmd
;
242 return devm_iio_device_register(dev
, indio_dev
);
245 static const struct platform_device_id cros_ec_light_prox_ids
[] = {
247 .name
= "cros-ec-prox",
250 .name
= "cros-ec-light",
254 MODULE_DEVICE_TABLE(platform
, cros_ec_light_prox_ids
);
256 static struct platform_driver cros_ec_light_prox_platform_driver
= {
258 .name
= "cros-ec-light-prox",
260 .probe
= cros_ec_light_prox_probe
,
261 .id_table
= cros_ec_light_prox_ids
,
263 module_platform_driver(cros_ec_light_prox_platform_driver
);
265 MODULE_DESCRIPTION("ChromeOS EC light/proximity sensors driver");
266 MODULE_LICENSE("GPL v2");