xref: /linux/drivers/iio/light/hid-sensor-als.c (revision b889fcf63cb62e7fdb7816565e28f44dbe4a76a5)
1 /*
2  * HID Sensors Driver
3  * Copyright (c) 2012, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  */
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/slab.h>
25 #include <linux/hid-sensor-hub.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/trigger_consumer.h>
30 #include <linux/iio/triggered_buffer.h>
31 #include "../common/hid-sensors/hid-sensor-attributes.h"
32 #include "../common/hid-sensors/hid-sensor-trigger.h"
33 
34 /*Format: HID-SENSOR-usage_id_in_hex*/
35 /*Usage ID from spec for Accelerometer-3D: 0x200041*/
36 #define DRIVER_NAME "HID-SENSOR-200041"
37 
38 #define CHANNEL_SCAN_INDEX_ILLUM 0
39 
40 struct als_state {
41 	struct hid_sensor_hub_callbacks callbacks;
42 	struct hid_sensor_iio_common common_attributes;
43 	struct hid_sensor_hub_attribute_info als_illum;
44 	u32 illum;
45 };
46 
47 /* Channel definitions */
48 static const struct iio_chan_spec als_channels[] = {
49 	{
50 		.type = IIO_INTENSITY,
51 		.modified = 1,
52 		.channel2 = IIO_MOD_LIGHT_BOTH,
53 		.info_mask = IIO_CHAN_INFO_OFFSET_SHARED_BIT |
54 		IIO_CHAN_INFO_SCALE_SHARED_BIT |
55 		IIO_CHAN_INFO_SAMP_FREQ_SHARED_BIT |
56 		IIO_CHAN_INFO_HYSTERESIS_SHARED_BIT,
57 		.scan_index = CHANNEL_SCAN_INDEX_ILLUM,
58 	}
59 };
60 
61 /* Adjust channel real bits based on report descriptor */
62 static void als_adjust_channel_bit_mask(struct iio_chan_spec *channels,
63 					int channel, int size)
64 {
65 	channels[channel].scan_type.sign = 's';
66 	/* Real storage bits will change based on the report desc. */
67 	channels[channel].scan_type.realbits = size * 8;
68 	/* Maximum size of a sample to capture is u32 */
69 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
70 }
71 
72 /* Channel read_raw handler */
73 static int als_read_raw(struct iio_dev *indio_dev,
74 			      struct iio_chan_spec const *chan,
75 			      int *val, int *val2,
76 			      long mask)
77 {
78 	struct als_state *als_state = iio_priv(indio_dev);
79 	int report_id = -1;
80 	u32 address;
81 	int ret;
82 	int ret_type;
83 
84 	*val = 0;
85 	*val2 = 0;
86 	switch (mask) {
87 	case 0:
88 		switch (chan->scan_index) {
89 		case  CHANNEL_SCAN_INDEX_ILLUM:
90 			report_id = als_state->als_illum.report_id;
91 			address =
92 			HID_USAGE_SENSOR_LIGHT_ILLUM;
93 			break;
94 		default:
95 			report_id = -1;
96 			break;
97 		}
98 		if (report_id >= 0)
99 			*val = sensor_hub_input_attr_get_raw_value(
100 				als_state->common_attributes.hsdev,
101 				HID_USAGE_SENSOR_ALS, address,
102 				report_id);
103 		else {
104 			*val = 0;
105 			return -EINVAL;
106 		}
107 		ret_type = IIO_VAL_INT;
108 		break;
109 	case IIO_CHAN_INFO_SCALE:
110 		*val = als_state->als_illum.units;
111 		ret_type = IIO_VAL_INT;
112 		break;
113 	case IIO_CHAN_INFO_OFFSET:
114 		*val = hid_sensor_convert_exponent(
115 				als_state->als_illum.unit_expo);
116 		ret_type = IIO_VAL_INT;
117 		break;
118 	case IIO_CHAN_INFO_SAMP_FREQ:
119 		ret = hid_sensor_read_samp_freq_value(
120 				&als_state->common_attributes, val, val2);
121 		ret_type = IIO_VAL_INT_PLUS_MICRO;
122 		break;
123 	case IIO_CHAN_INFO_HYSTERESIS:
124 		ret = hid_sensor_read_raw_hyst_value(
125 				&als_state->common_attributes, val, val2);
126 		ret_type = IIO_VAL_INT_PLUS_MICRO;
127 		break;
128 	default:
129 		ret_type = -EINVAL;
130 		break;
131 	}
132 
133 	return ret_type;
134 }
135 
136 /* Channel write_raw handler */
137 static int als_write_raw(struct iio_dev *indio_dev,
138 			       struct iio_chan_spec const *chan,
139 			       int val,
140 			       int val2,
141 			       long mask)
142 {
143 	struct als_state *als_state = iio_priv(indio_dev);
144 	int ret = 0;
145 
146 	switch (mask) {
147 	case IIO_CHAN_INFO_SAMP_FREQ:
148 		ret = hid_sensor_write_samp_freq_value(
149 				&als_state->common_attributes, val, val2);
150 		break;
151 	case IIO_CHAN_INFO_HYSTERESIS:
152 		ret = hid_sensor_write_raw_hyst_value(
153 				&als_state->common_attributes, val, val2);
154 		break;
155 	default:
156 		ret = -EINVAL;
157 	}
158 
159 	return ret;
160 }
161 
162 static int als_write_raw_get_fmt(struct iio_dev *indio_dev,
163 			       struct iio_chan_spec const *chan,
164 			       long mask)
165 {
166 	return IIO_VAL_INT_PLUS_MICRO;
167 }
168 
169 static const struct iio_info als_info = {
170 	.driver_module = THIS_MODULE,
171 	.read_raw = &als_read_raw,
172 	.write_raw = &als_write_raw,
173 	.write_raw_get_fmt = &als_write_raw_get_fmt,
174 };
175 
176 /* Function to push data to buffer */
177 static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
178 {
179 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
180 	iio_push_to_buffers(indio_dev, (u8 *)data);
181 }
182 
183 /* Callback handler to send event after all samples are received and captured */
184 static int als_proc_event(struct hid_sensor_hub_device *hsdev,
185 				unsigned usage_id,
186 				void *priv)
187 {
188 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
189 	struct als_state *als_state = iio_priv(indio_dev);
190 
191 	dev_dbg(&indio_dev->dev, "als_proc_event [%d]\n",
192 				als_state->common_attributes.data_ready);
193 	if (als_state->common_attributes.data_ready)
194 		hid_sensor_push_data(indio_dev,
195 				(u8 *)&als_state->illum,
196 				sizeof(als_state->illum));
197 
198 	return 0;
199 }
200 
201 /* Capture samples in local storage */
202 static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
203 				unsigned usage_id,
204 				size_t raw_len, char *raw_data,
205 				void *priv)
206 {
207 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
208 	struct als_state *als_state = iio_priv(indio_dev);
209 	int ret = -EINVAL;
210 
211 	switch (usage_id) {
212 	case HID_USAGE_SENSOR_LIGHT_ILLUM:
213 		als_state->illum = *(u32 *)raw_data;
214 		ret = 0;
215 		break;
216 	default:
217 		break;
218 	}
219 
220 	return ret;
221 }
222 
223 /* Parse report which is specific to an usage id*/
224 static int als_parse_report(struct platform_device *pdev,
225 				struct hid_sensor_hub_device *hsdev,
226 				struct iio_chan_spec *channels,
227 				unsigned usage_id,
228 				struct als_state *st)
229 {
230 	int ret;
231 
232 	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
233 			usage_id,
234 			HID_USAGE_SENSOR_LIGHT_ILLUM,
235 			&st->als_illum);
236 	if (ret < 0)
237 		return ret;
238 	als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_ILLUM,
239 					st->als_illum.size);
240 
241 	dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
242 			st->als_illum.report_id);
243 
244 	return ret;
245 }
246 
247 /* Function to initialize the processing for usage id */
248 static int __devinit hid_als_probe(struct platform_device *pdev)
249 {
250 	int ret = 0;
251 	static const char *name = "als";
252 	struct iio_dev *indio_dev;
253 	struct als_state *als_state;
254 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
255 	struct iio_chan_spec *channels;
256 
257 	indio_dev = iio_device_alloc(sizeof(struct als_state));
258 	if (indio_dev == NULL) {
259 		ret = -ENOMEM;
260 		goto error_ret;
261 	}
262 	platform_set_drvdata(pdev, indio_dev);
263 
264 	als_state = iio_priv(indio_dev);
265 	als_state->common_attributes.hsdev = hsdev;
266 	als_state->common_attributes.pdev = pdev;
267 
268 	ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_ALS,
269 					&als_state->common_attributes);
270 	if (ret) {
271 		dev_err(&pdev->dev, "failed to setup common attributes\n");
272 		goto error_free_dev;
273 	}
274 
275 	channels = kmemdup(als_channels, sizeof(als_channels), GFP_KERNEL);
276 	if (!channels) {
277 		ret = -ENOMEM;
278 		dev_err(&pdev->dev, "failed to duplicate channels\n");
279 		goto error_free_dev;
280 	}
281 
282 	ret = als_parse_report(pdev, hsdev, channels,
283 				HID_USAGE_SENSOR_ALS, als_state);
284 	if (ret) {
285 		dev_err(&pdev->dev, "failed to setup attributes\n");
286 		goto error_free_dev_mem;
287 	}
288 
289 	indio_dev->channels = channels;
290 	indio_dev->num_channels =
291 				ARRAY_SIZE(als_channels);
292 	indio_dev->dev.parent = &pdev->dev;
293 	indio_dev->info = &als_info;
294 	indio_dev->name = name;
295 	indio_dev->modes = INDIO_DIRECT_MODE;
296 
297 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
298 		NULL, NULL);
299 	if (ret) {
300 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
301 		goto error_free_dev_mem;
302 	}
303 	als_state->common_attributes.data_ready = false;
304 	ret = hid_sensor_setup_trigger(indio_dev, name,
305 				&als_state->common_attributes);
306 	if (ret < 0) {
307 		dev_err(&pdev->dev, "trigger setup failed\n");
308 		goto error_unreg_buffer_funcs;
309 	}
310 
311 	ret = iio_device_register(indio_dev);
312 	if (ret) {
313 		dev_err(&pdev->dev, "device register failed\n");
314 		goto error_remove_trigger;
315 	}
316 
317 	als_state->callbacks.send_event = als_proc_event;
318 	als_state->callbacks.capture_sample = als_capture_sample;
319 	als_state->callbacks.pdev = pdev;
320 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ALS,
321 					&als_state->callbacks);
322 	if (ret < 0) {
323 		dev_err(&pdev->dev, "callback reg failed\n");
324 		goto error_iio_unreg;
325 	}
326 
327 	return ret;
328 
329 error_iio_unreg:
330 	iio_device_unregister(indio_dev);
331 error_remove_trigger:
332 	hid_sensor_remove_trigger(indio_dev);
333 error_unreg_buffer_funcs:
334 	iio_triggered_buffer_cleanup(indio_dev);
335 error_free_dev_mem:
336 	kfree(indio_dev->channels);
337 error_free_dev:
338 	iio_device_free(indio_dev);
339 error_ret:
340 	return ret;
341 }
342 
343 /* Function to deinitialize the processing for usage id */
344 static int __devinit hid_als_remove(struct platform_device *pdev)
345 {
346 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
347 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
348 
349 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ALS);
350 	iio_device_unregister(indio_dev);
351 	hid_sensor_remove_trigger(indio_dev);
352 	iio_triggered_buffer_cleanup(indio_dev);
353 	kfree(indio_dev->channels);
354 	iio_device_free(indio_dev);
355 
356 	return 0;
357 }
358 
359 static struct platform_driver hid_als_platform_driver = {
360 	.driver = {
361 		.name	= DRIVER_NAME,
362 		.owner	= THIS_MODULE,
363 	},
364 	.probe		= hid_als_probe,
365 	.remove		= hid_als_remove,
366 };
367 module_platform_driver(hid_als_platform_driver);
368 
369 MODULE_DESCRIPTION("HID Sensor ALS");
370 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
371 MODULE_LICENSE("GPL");
372