3 * Copyright (c) 2012, Intel Corporation.
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.
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
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.
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"
34 /*Format: HID-SENSOR-usage_id_in_hex*/
35 /*Usage ID from spec for Accelerometer-3D: 0x200041*/
36 #define DRIVER_NAME "HID-SENSOR-200041"
38 #define CHANNEL_SCAN_INDEX_ILLUM 0
41 struct hid_sensor_hub_callbacks callbacks;
42 struct hid_sensor_iio_common common_attributes;
43 struct hid_sensor_hub_attribute_info als_illum;
47 /* Channel definitions */
48 static const struct iio_chan_spec als_channels[] = {
50 .type = IIO_INTENSITY,
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,
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)
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;
72 /* Channel read_raw handler */
73 static int als_read_raw(struct iio_dev *indio_dev,
74 struct iio_chan_spec const *chan,
78 struct als_state *als_state = iio_priv(indio_dev);
88 switch (chan->scan_index) {
89 case CHANNEL_SCAN_INDEX_ILLUM:
90 report_id = als_state->als_illum.report_id;
92 HID_USAGE_SENSOR_LIGHT_ILLUM;
99 *val = sensor_hub_input_attr_get_raw_value(
100 als_state->common_attributes.hsdev,
101 HID_USAGE_SENSOR_ALS, address,
107 ret_type = IIO_VAL_INT;
109 case IIO_CHAN_INFO_SCALE:
110 *val = als_state->als_illum.units;
111 ret_type = IIO_VAL_INT;
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;
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;
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;
136 /* Channel write_raw handler */
137 static int als_write_raw(struct iio_dev *indio_dev,
138 struct iio_chan_spec const *chan,
143 struct als_state *als_state = iio_priv(indio_dev);
147 case IIO_CHAN_INFO_SAMP_FREQ:
148 ret = hid_sensor_write_samp_freq_value(
149 &als_state->common_attributes, val, val2);
151 case IIO_CHAN_INFO_HYSTERESIS:
152 ret = hid_sensor_write_raw_hyst_value(
153 &als_state->common_attributes, val, val2);
162 static int als_write_raw_get_fmt(struct iio_dev *indio_dev,
163 struct iio_chan_spec const *chan,
166 return IIO_VAL_INT_PLUS_MICRO;
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,
176 /* Function to push data to buffer */
177 static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
179 struct iio_buffer *buffer = indio_dev->buffer;
182 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
184 dev_err(&indio_dev->dev, "Buffer == NULL\n");
187 datum_sz = buffer->access->get_bytes_per_datum(buffer);
188 if (len > datum_sz) {
189 dev_err(&indio_dev->dev, "Datum size mismatch %d:%d\n", len,
193 iio_push_to_buffer(buffer, (u8 *)data);
196 /* Callback handler to send event after all samples are received and captured */
197 static int als_proc_event(struct hid_sensor_hub_device *hsdev,
201 struct iio_dev *indio_dev = platform_get_drvdata(priv);
202 struct als_state *als_state = iio_priv(indio_dev);
204 dev_dbg(&indio_dev->dev, "als_proc_event [%d]\n",
205 als_state->common_attributes.data_ready);
206 if (als_state->common_attributes.data_ready)
207 hid_sensor_push_data(indio_dev,
208 (u8 *)&als_state->illum,
209 sizeof(als_state->illum));
214 /* Capture samples in local storage */
215 static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
217 size_t raw_len, char *raw_data,
220 struct iio_dev *indio_dev = platform_get_drvdata(priv);
221 struct als_state *als_state = iio_priv(indio_dev);
225 case HID_USAGE_SENSOR_LIGHT_ILLUM:
226 als_state->illum = *(u32 *)raw_data;
236 /* Parse report which is specific to an usage id*/
237 static int als_parse_report(struct platform_device *pdev,
238 struct hid_sensor_hub_device *hsdev,
239 struct iio_chan_spec *channels,
241 struct als_state *st)
245 ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
247 HID_USAGE_SENSOR_LIGHT_ILLUM,
251 als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_ILLUM,
254 dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
255 st->als_illum.report_id);
260 /* Function to initialize the processing for usage id */
261 static int __devinit hid_als_probe(struct platform_device *pdev)
264 static const char *name = "als";
265 struct iio_dev *indio_dev;
266 struct als_state *als_state;
267 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
268 struct iio_chan_spec *channels;
270 indio_dev = iio_device_alloc(sizeof(struct als_state));
271 if (indio_dev == NULL) {
275 platform_set_drvdata(pdev, indio_dev);
277 als_state = iio_priv(indio_dev);
278 als_state->common_attributes.hsdev = hsdev;
279 als_state->common_attributes.pdev = pdev;
281 ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_ALS,
282 &als_state->common_attributes);
284 dev_err(&pdev->dev, "failed to setup common attributes\n");
288 channels = kmemdup(als_channels,
289 sizeof(als_channels),
292 dev_err(&pdev->dev, "failed to duplicate channels\n");
296 ret = als_parse_report(pdev, hsdev, channels,
297 HID_USAGE_SENSOR_ALS, als_state);
299 dev_err(&pdev->dev, "failed to setup attributes\n");
300 goto error_free_dev_mem;
303 indio_dev->channels = channels;
304 indio_dev->num_channels =
305 ARRAY_SIZE(als_channels);
306 indio_dev->dev.parent = &pdev->dev;
307 indio_dev->info = &als_info;
308 indio_dev->name = name;
309 indio_dev->modes = INDIO_DIRECT_MODE;
311 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
314 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
315 goto error_free_dev_mem;
317 als_state->common_attributes.data_ready = false;
318 ret = hid_sensor_setup_trigger(indio_dev, name,
319 &als_state->common_attributes);
321 dev_err(&pdev->dev, "trigger setup failed\n");
322 goto error_unreg_buffer_funcs;
325 ret = iio_device_register(indio_dev);
327 dev_err(&pdev->dev, "device register failed\n");
328 goto error_remove_trigger;
331 als_state->callbacks.send_event = als_proc_event;
332 als_state->callbacks.capture_sample = als_capture_sample;
333 als_state->callbacks.pdev = pdev;
334 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ALS,
335 &als_state->callbacks);
337 dev_err(&pdev->dev, "callback reg failed\n");
338 goto error_iio_unreg;
344 iio_device_unregister(indio_dev);
345 error_remove_trigger:
346 hid_sensor_remove_trigger(indio_dev);
347 error_unreg_buffer_funcs:
348 iio_triggered_buffer_cleanup(indio_dev);
350 kfree(indio_dev->channels);
352 iio_device_free(indio_dev);
357 /* Function to deinitialize the processing for usage id */
358 static int __devinit hid_als_remove(struct platform_device *pdev)
360 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
361 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
363 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ALS);
364 iio_device_unregister(indio_dev);
365 hid_sensor_remove_trigger(indio_dev);
366 iio_triggered_buffer_cleanup(indio_dev);
367 kfree(indio_dev->channels);
368 iio_device_free(indio_dev);
373 static struct platform_driver hid_als_platform_driver = {
376 .owner = THIS_MODULE,
378 .probe = hid_als_probe,
379 .remove = hid_als_remove,
381 module_platform_driver(hid_als_platform_driver);
383 MODULE_DESCRIPTION("HID Sensor ALS");
384 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
385 MODULE_LICENSE("GPL");