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 };
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 iio_dev *indio_dev;
185 	struct cros_ec_light_prox_state *state;
186 	struct iio_chan_spec *channel;
187 	int ret;
188 
189 	if (!ec_dev || !ec_dev->ec_dev) {
190 		dev_warn(dev, "No CROS EC device found.\n");
191 		return -EINVAL;
192 	}
193 
194 	indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
195 	if (!indio_dev)
196 		return -ENOMEM;
197 
198 	ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
199 	if (ret)
200 		return ret;
201 
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;
207 
208 	/* Common part */
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';
218 
219 	state->core.calib[0] = 0;
220 
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);
229 		break;
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);
236 		break;
237 	default:
238 		dev_warn(dev, "Unknown motion sensor\n");
239 		return -EINVAL;
240 	}
241 
242 	/* Timestamp */
243 	channel++;
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;
250 
251 	indio_dev->channels = state->channels;
252 
253 	indio_dev->num_channels = CROS_EC_LIGHT_PROX_MAX_CHANNELS;
254 
255 	state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
256 
257 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
258 					      cros_ec_sensors_capture, NULL);
259 	if (ret)
260 		return ret;
261 
262 	return devm_iio_device_register(dev, indio_dev);
263 }
264 
265 static const struct platform_device_id cros_ec_light_prox_ids[] = {
266 	{
267 		.name = "cros-ec-prox",
268 	},
269 	{
270 		.name = "cros-ec-light",
271 	},
272 	{ /* sentinel */ }
273 };
274 MODULE_DEVICE_TABLE(platform, cros_ec_light_prox_ids);
275 
276 static struct platform_driver cros_ec_light_prox_platform_driver = {
277 	.driver = {
278 		.name	= "cros-ec-light-prox",
279 	},
280 	.probe		= cros_ec_light_prox_probe,
281 	.id_table	= cros_ec_light_prox_ids,
282 };
283 module_platform_driver(cros_ec_light_prox_platform_driver);
284 
285 MODULE_DESCRIPTION("ChromeOS EC light/proximity sensors driver");
286 MODULE_LICENSE("GPL v2");
287