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/delay.h>
26 #include <linux/hid-sensor-hub.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/trigger_consumer.h>
31 #include <linux/iio/triggered_buffer.h>
32 #include "../common/hid-sensors/hid-sensor-trigger.h"
35 CHANNEL_SCAN_INDEX_INTENSITY = 0,
36 CHANNEL_SCAN_INDEX_ILLUM = 1,
37 CHANNEL_SCAN_INDEX_MAX
41 struct hid_sensor_hub_callbacks callbacks;
42 struct hid_sensor_common common_attributes;
43 struct hid_sensor_hub_attribute_info als_illum;
44 u32 illum[CHANNEL_SCAN_INDEX_MAX];
51 /* Channel definitions */
52 static const struct iio_chan_spec als_channels[] = {
54 .type = IIO_INTENSITY,
56 .channel2 = IIO_MOD_LIGHT_BOTH,
57 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
58 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
59 BIT(IIO_CHAN_INFO_SCALE) |
60 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
61 BIT(IIO_CHAN_INFO_HYSTERESIS),
62 .scan_index = CHANNEL_SCAN_INDEX_INTENSITY,
66 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
67 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
68 BIT(IIO_CHAN_INFO_SCALE) |
69 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
70 BIT(IIO_CHAN_INFO_HYSTERESIS),
71 .scan_index = CHANNEL_SCAN_INDEX_ILLUM,
75 /* Adjust channel real bits based on report descriptor */
76 static void als_adjust_channel_bit_mask(struct iio_chan_spec *channels,
77 int channel, int size)
79 channels[channel].scan_type.sign = 's';
80 /* Real storage bits will change based on the report desc. */
81 channels[channel].scan_type.realbits = size * 8;
82 /* Maximum size of a sample to capture is u32 */
83 channels[channel].scan_type.storagebits = sizeof(u32) * 8;
86 /* Channel read_raw handler */
87 static int als_read_raw(struct iio_dev *indio_dev,
88 struct iio_chan_spec const *chan,
92 struct als_state *als_state = iio_priv(indio_dev);
102 switch (chan->scan_index) {
103 case CHANNEL_SCAN_INDEX_INTENSITY:
104 case CHANNEL_SCAN_INDEX_ILLUM:
105 report_id = als_state->als_illum.report_id;
106 min = als_state->als_illum.logical_minimum;
107 address = HID_USAGE_SENSOR_LIGHT_ILLUM;
113 if (report_id >= 0) {
114 hid_sensor_power_state(&als_state->common_attributes,
116 *val = sensor_hub_input_attr_get_raw_value(
117 als_state->common_attributes.hsdev,
118 HID_USAGE_SENSOR_ALS, address,
122 hid_sensor_power_state(&als_state->common_attributes,
128 ret_type = IIO_VAL_INT;
130 case IIO_CHAN_INFO_SCALE:
131 *val = als_state->scale_pre_decml;
132 *val2 = als_state->scale_post_decml;
133 ret_type = als_state->scale_precision;
135 case IIO_CHAN_INFO_OFFSET:
136 *val = als_state->value_offset;
137 ret_type = IIO_VAL_INT;
139 case IIO_CHAN_INFO_SAMP_FREQ:
140 ret_type = hid_sensor_read_samp_freq_value(
141 &als_state->common_attributes, val, val2);
143 case IIO_CHAN_INFO_HYSTERESIS:
144 ret_type = hid_sensor_read_raw_hyst_value(
145 &als_state->common_attributes, val, val2);
155 /* Channel write_raw handler */
156 static int als_write_raw(struct iio_dev *indio_dev,
157 struct iio_chan_spec const *chan,
162 struct als_state *als_state = iio_priv(indio_dev);
166 case IIO_CHAN_INFO_SAMP_FREQ:
167 ret = hid_sensor_write_samp_freq_value(
168 &als_state->common_attributes, val, val2);
170 case IIO_CHAN_INFO_HYSTERESIS:
171 ret = hid_sensor_write_raw_hyst_value(
172 &als_state->common_attributes, val, val2);
181 static const struct iio_info als_info = {
182 .driver_module = THIS_MODULE,
183 .read_raw = &als_read_raw,
184 .write_raw = &als_write_raw,
187 /* Function to push data to buffer */
188 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
191 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
192 iio_push_to_buffers(indio_dev, data);
195 /* Callback handler to send event after all samples are received and captured */
196 static int als_proc_event(struct hid_sensor_hub_device *hsdev,
200 struct iio_dev *indio_dev = platform_get_drvdata(priv);
201 struct als_state *als_state = iio_priv(indio_dev);
203 dev_dbg(&indio_dev->dev, "als_proc_event\n");
204 if (atomic_read(&als_state->common_attributes.data_ready))
205 hid_sensor_push_data(indio_dev,
207 sizeof(als_state->illum));
212 /* Capture samples in local storage */
213 static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
215 size_t raw_len, char *raw_data,
218 struct iio_dev *indio_dev = platform_get_drvdata(priv);
219 struct als_state *als_state = iio_priv(indio_dev);
221 u32 sample_data = *(u32 *)raw_data;
224 case HID_USAGE_SENSOR_LIGHT_ILLUM:
225 als_state->illum[CHANNEL_SCAN_INDEX_INTENSITY] = sample_data;
226 als_state->illum[CHANNEL_SCAN_INDEX_ILLUM] = sample_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_INTENSITY,
253 als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_ILLUM,
256 dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
257 st->als_illum.report_id);
259 st->scale_precision = hid_sensor_format_scale(
260 HID_USAGE_SENSOR_ALS,
262 &st->scale_pre_decml, &st->scale_post_decml);
264 /* Set Sensitivity field ids, when there is no individual modifier */
265 if (st->common_attributes.sensitivity.index < 0) {
266 sensor_hub_input_get_attribute_info(hsdev,
267 HID_FEATURE_REPORT, usage_id,
268 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
269 HID_USAGE_SENSOR_DATA_LIGHT,
270 &st->common_attributes.sensitivity);
271 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
272 st->common_attributes.sensitivity.index,
273 st->common_attributes.sensitivity.report_id);
278 /* Function to initialize the processing for usage id */
279 static int hid_als_probe(struct platform_device *pdev)
282 static const char *name = "als";
283 struct iio_dev *indio_dev;
284 struct als_state *als_state;
285 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
287 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct als_state));
290 platform_set_drvdata(pdev, indio_dev);
292 als_state = iio_priv(indio_dev);
293 als_state->common_attributes.hsdev = hsdev;
294 als_state->common_attributes.pdev = pdev;
296 ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_ALS,
297 &als_state->common_attributes);
299 dev_err(&pdev->dev, "failed to setup common attributes\n");
303 indio_dev->channels = kmemdup(als_channels,
304 sizeof(als_channels), GFP_KERNEL);
305 if (!indio_dev->channels) {
306 dev_err(&pdev->dev, "failed to duplicate channels\n");
310 ret = als_parse_report(pdev, hsdev,
311 (struct iio_chan_spec *)indio_dev->channels,
312 HID_USAGE_SENSOR_ALS, als_state);
314 dev_err(&pdev->dev, "failed to setup attributes\n");
315 goto error_free_dev_mem;
318 indio_dev->num_channels =
319 ARRAY_SIZE(als_channels);
320 indio_dev->dev.parent = &pdev->dev;
321 indio_dev->info = &als_info;
322 indio_dev->name = name;
323 indio_dev->modes = INDIO_DIRECT_MODE;
325 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
328 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
329 goto error_free_dev_mem;
331 atomic_set(&als_state->common_attributes.data_ready, 0);
332 ret = hid_sensor_setup_trigger(indio_dev, name,
333 &als_state->common_attributes);
335 dev_err(&pdev->dev, "trigger setup failed\n");
336 goto error_unreg_buffer_funcs;
339 ret = iio_device_register(indio_dev);
341 dev_err(&pdev->dev, "device register failed\n");
342 goto error_remove_trigger;
345 als_state->callbacks.send_event = als_proc_event;
346 als_state->callbacks.capture_sample = als_capture_sample;
347 als_state->callbacks.pdev = pdev;
348 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ALS,
349 &als_state->callbacks);
351 dev_err(&pdev->dev, "callback reg failed\n");
352 goto error_iio_unreg;
358 iio_device_unregister(indio_dev);
359 error_remove_trigger:
360 hid_sensor_remove_trigger(&als_state->common_attributes);
361 error_unreg_buffer_funcs:
362 iio_triggered_buffer_cleanup(indio_dev);
364 kfree(indio_dev->channels);
368 /* Function to deinitialize the processing for usage id */
369 static int hid_als_remove(struct platform_device *pdev)
371 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
372 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
373 struct als_state *als_state = iio_priv(indio_dev);
375 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ALS);
376 iio_device_unregister(indio_dev);
377 hid_sensor_remove_trigger(&als_state->common_attributes);
378 iio_triggered_buffer_cleanup(indio_dev);
379 kfree(indio_dev->channels);
384 static const struct platform_device_id hid_als_ids[] = {
386 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
387 .name = "HID-SENSOR-200041",
391 MODULE_DEVICE_TABLE(platform, hid_als_ids);
393 static struct platform_driver hid_als_platform_driver = {
394 .id_table = hid_als_ids,
396 .name = KBUILD_MODNAME,
397 .pm = &hid_sensor_pm_ops,
399 .probe = hid_als_probe,
400 .remove = hid_als_remove,
402 module_platform_driver(hid_als_platform_driver);
404 MODULE_DESCRIPTION("HID Sensor ALS");
405 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
406 MODULE_LICENSE("GPL");