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
,
180 static int cros_ec_light_prox_probe(struct platform_device
*pdev
)
182 struct device
*dev
= &pdev
->dev
;
183 struct cros_ec_dev
*ec_dev
= dev_get_drvdata(dev
->parent
);
184 struct iio_dev
*indio_dev
;
185 struct cros_ec_light_prox_state
*state
;
186 struct iio_chan_spec
*channel
;
189 if (!ec_dev
|| !ec_dev
->ec_dev
) {
190 dev_warn(dev
, "No CROS EC device found.\n");
194 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*state
));
198 ret
= cros_ec_sensors_core_init(pdev
, indio_dev
, true);
202 indio_dev
->info
= &cros_ec_light_prox_info
;
203 state
= iio_priv(indio_dev
);
204 state
->core
.type
= state
->core
.resp
->info
.type
;
205 state
->core
.loc
= state
->core
.resp
->info
.location
;
206 channel
= state
->channels
;
209 channel
->info_mask_shared_by_all
=
210 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
211 BIT(IIO_CHAN_INFO_FREQUENCY
);
212 channel
->scan_type
.realbits
= CROS_EC_SENSOR_BITS
;
213 channel
->scan_type
.storagebits
= CROS_EC_SENSOR_BITS
;
214 channel
->scan_type
.shift
= 0;
215 channel
->scan_index
= 0;
216 channel
->ext_info
= cros_ec_sensors_ext_info
;
217 channel
->scan_type
.sign
= 'u';
219 state
->core
.calib
[0] = 0;
221 /* Sensor specific */
222 switch (state
->core
.type
) {
223 case MOTIONSENSE_TYPE_LIGHT
:
224 channel
->type
= IIO_LIGHT
;
225 channel
->info_mask_separate
=
226 BIT(IIO_CHAN_INFO_PROCESSED
) |
227 BIT(IIO_CHAN_INFO_CALIBBIAS
) |
228 BIT(IIO_CHAN_INFO_CALIBSCALE
);
230 case MOTIONSENSE_TYPE_PROX
:
231 channel
->type
= IIO_PROXIMITY
;
232 channel
->info_mask_separate
=
233 BIT(IIO_CHAN_INFO_RAW
) |
234 BIT(IIO_CHAN_INFO_CALIBBIAS
) |
235 BIT(IIO_CHAN_INFO_CALIBSCALE
);
238 dev_warn(dev
, "Unknown motion sensor\n");
244 channel
->type
= IIO_TIMESTAMP
;
245 channel
->channel
= -1;
246 channel
->scan_index
= 1;
247 channel
->scan_type
.sign
= 's';
248 channel
->scan_type
.realbits
= 64;
249 channel
->scan_type
.storagebits
= 64;
251 indio_dev
->channels
= state
->channels
;
253 indio_dev
->num_channels
= CROS_EC_LIGHT_PROX_MAX_CHANNELS
;
255 state
->core
.read_ec_sensors_data
= cros_ec_sensors_read_cmd
;
257 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
, NULL
,
258 cros_ec_sensors_capture
, NULL
);
262 return devm_iio_device_register(dev
, indio_dev
);
265 static const struct platform_device_id cros_ec_light_prox_ids
[] = {
267 .name
= "cros-ec-prox",
270 .name
= "cros-ec-light",
274 MODULE_DEVICE_TABLE(platform
, cros_ec_light_prox_ids
);
276 static struct platform_driver cros_ec_light_prox_platform_driver
= {
278 .name
= "cros-ec-light-prox",
280 .probe
= cros_ec_light_prox_probe
,
281 .id_table
= cros_ec_light_prox_ids
,
283 module_platform_driver(cros_ec_light_prox_platform_driver
);
285 MODULE_DESCRIPTION("ChromeOS EC light/proximity sensors driver");
286 MODULE_LICENSE("GPL v2");