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 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
180 iio_push_to_buffers(indio_dev, (u8 *)data);
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,
188 struct iio_dev *indio_dev = platform_get_drvdata(priv);
189 struct als_state *als_state = iio_priv(indio_dev);
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));
201 /* Capture samples in local storage */
202 static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
204 size_t raw_len, char *raw_data,
207 struct iio_dev *indio_dev = platform_get_drvdata(priv);
208 struct als_state *als_state = iio_priv(indio_dev);
212 case HID_USAGE_SENSOR_LIGHT_ILLUM:
213 als_state->illum = *(u32 *)raw_data;
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,
228 struct als_state *st)
232 ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
234 HID_USAGE_SENSOR_LIGHT_ILLUM,
238 als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_ILLUM,
241 dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
242 st->als_illum.report_id);
247 /* Function to initialize the processing for usage id */
248 static int __devinit hid_als_probe(struct platform_device *pdev)
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;
257 indio_dev = iio_device_alloc(sizeof(struct als_state));
258 if (indio_dev == NULL) {
262 platform_set_drvdata(pdev, indio_dev);
264 als_state = iio_priv(indio_dev);
265 als_state->common_attributes.hsdev = hsdev;
266 als_state->common_attributes.pdev = pdev;
268 ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_ALS,
269 &als_state->common_attributes);
271 dev_err(&pdev->dev, "failed to setup common attributes\n");
275 channels = kmemdup(als_channels, sizeof(als_channels), GFP_KERNEL);
278 dev_err(&pdev->dev, "failed to duplicate channels\n");
282 ret = als_parse_report(pdev, hsdev, channels,
283 HID_USAGE_SENSOR_ALS, als_state);
285 dev_err(&pdev->dev, "failed to setup attributes\n");
286 goto error_free_dev_mem;
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;
297 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
300 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
301 goto error_free_dev_mem;
303 als_state->common_attributes.data_ready = false;
304 ret = hid_sensor_setup_trigger(indio_dev, name,
305 &als_state->common_attributes);
307 dev_err(&pdev->dev, "trigger setup failed\n");
308 goto error_unreg_buffer_funcs;
311 ret = iio_device_register(indio_dev);
313 dev_err(&pdev->dev, "device register failed\n");
314 goto error_remove_trigger;
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);
323 dev_err(&pdev->dev, "callback reg failed\n");
324 goto 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);
336 kfree(indio_dev->channels);
338 iio_device_free(indio_dev);
343 /* Function to deinitialize the processing for usage id */
344 static int __devinit hid_als_remove(struct platform_device *pdev)
346 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
347 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
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);
359 static struct platform_driver hid_als_platform_driver = {
362 .owner = THIS_MODULE,
364 .probe = hid_als_probe,
365 .remove = hid_als_remove,
367 module_platform_driver(hid_als_platform_driver);
369 MODULE_DESCRIPTION("HID Sensor ALS");
370 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
371 MODULE_LICENSE("GPL");