1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2012, Intel Corporation.
6 #include <linux/device.h>
7 #include <linux/platform_device.h>
8 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/slab.h>
12 #include <linux/delay.h>
13 #include <linux/hid-sensor-hub.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/buffer.h>
17 #include "../common/hid-sensors/hid-sensor-trigger.h"
20 CHANNEL_SCAN_INDEX_INTENSITY
= 0,
21 CHANNEL_SCAN_INDEX_ILLUM
= 1,
22 CHANNEL_SCAN_INDEX_MAX
26 struct hid_sensor_hub_callbacks callbacks
;
27 struct hid_sensor_common common_attributes
;
28 struct hid_sensor_hub_attribute_info als_illum
;
29 u32 illum
[CHANNEL_SCAN_INDEX_MAX
];
36 /* Channel definitions */
37 static const struct iio_chan_spec als_channels
[] = {
39 .type
= IIO_INTENSITY
,
41 .channel2
= IIO_MOD_LIGHT_BOTH
,
42 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
43 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
44 BIT(IIO_CHAN_INFO_SCALE
) |
45 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
46 BIT(IIO_CHAN_INFO_HYSTERESIS
),
47 .scan_index
= CHANNEL_SCAN_INDEX_INTENSITY
,
51 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
52 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
53 BIT(IIO_CHAN_INFO_SCALE
) |
54 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
55 BIT(IIO_CHAN_INFO_HYSTERESIS
),
56 .scan_index
= CHANNEL_SCAN_INDEX_ILLUM
,
60 /* Adjust channel real bits based on report descriptor */
61 static void als_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
62 int channel
, int size
)
64 channels
[channel
].scan_type
.sign
= 's';
65 /* Real storage bits will change based on the report desc. */
66 channels
[channel
].scan_type
.realbits
= size
* 8;
67 /* Maximum size of a sample to capture is u32 */
68 channels
[channel
].scan_type
.storagebits
= sizeof(u32
) * 8;
71 /* Channel read_raw handler */
72 static int als_read_raw(struct iio_dev
*indio_dev
,
73 struct iio_chan_spec
const *chan
,
77 struct als_state
*als_state
= iio_priv(indio_dev
);
86 case IIO_CHAN_INFO_RAW
:
87 switch (chan
->scan_index
) {
88 case CHANNEL_SCAN_INDEX_INTENSITY
:
89 case CHANNEL_SCAN_INDEX_ILLUM
:
90 report_id
= als_state
->als_illum
.report_id
;
91 min
= als_state
->als_illum
.logical_minimum
;
92 address
= HID_USAGE_SENSOR_LIGHT_ILLUM
;
99 hid_sensor_power_state(&als_state
->common_attributes
,
101 *val
= sensor_hub_input_attr_get_raw_value(
102 als_state
->common_attributes
.hsdev
,
103 HID_USAGE_SENSOR_ALS
, address
,
107 hid_sensor_power_state(&als_state
->common_attributes
,
113 ret_type
= IIO_VAL_INT
;
115 case IIO_CHAN_INFO_SCALE
:
116 *val
= als_state
->scale_pre_decml
;
117 *val2
= als_state
->scale_post_decml
;
118 ret_type
= als_state
->scale_precision
;
120 case IIO_CHAN_INFO_OFFSET
:
121 *val
= als_state
->value_offset
;
122 ret_type
= IIO_VAL_INT
;
124 case IIO_CHAN_INFO_SAMP_FREQ
:
125 ret_type
= hid_sensor_read_samp_freq_value(
126 &als_state
->common_attributes
, val
, val2
);
128 case IIO_CHAN_INFO_HYSTERESIS
:
129 ret_type
= hid_sensor_read_raw_hyst_value(
130 &als_state
->common_attributes
, val
, val2
);
140 /* Channel write_raw handler */
141 static int als_write_raw(struct iio_dev
*indio_dev
,
142 struct iio_chan_spec
const *chan
,
147 struct als_state
*als_state
= iio_priv(indio_dev
);
151 case IIO_CHAN_INFO_SAMP_FREQ
:
152 ret
= hid_sensor_write_samp_freq_value(
153 &als_state
->common_attributes
, val
, val2
);
155 case IIO_CHAN_INFO_HYSTERESIS
:
156 ret
= hid_sensor_write_raw_hyst_value(
157 &als_state
->common_attributes
, val
, val2
);
166 static const struct iio_info als_info
= {
167 .read_raw
= &als_read_raw
,
168 .write_raw
= &als_write_raw
,
171 /* Function to push data to buffer */
172 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, const void *data
,
175 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data\n");
176 iio_push_to_buffers(indio_dev
, data
);
179 /* Callback handler to send event after all samples are received and captured */
180 static int als_proc_event(struct hid_sensor_hub_device
*hsdev
,
184 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
185 struct als_state
*als_state
= iio_priv(indio_dev
);
187 dev_dbg(&indio_dev
->dev
, "als_proc_event\n");
188 if (atomic_read(&als_state
->common_attributes
.data_ready
))
189 hid_sensor_push_data(indio_dev
,
191 sizeof(als_state
->illum
));
196 /* Capture samples in local storage */
197 static int als_capture_sample(struct hid_sensor_hub_device
*hsdev
,
199 size_t raw_len
, char *raw_data
,
202 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
203 struct als_state
*als_state
= iio_priv(indio_dev
);
205 u32 sample_data
= *(u32
*)raw_data
;
208 case HID_USAGE_SENSOR_LIGHT_ILLUM
:
209 als_state
->illum
[CHANNEL_SCAN_INDEX_INTENSITY
] = sample_data
;
210 als_state
->illum
[CHANNEL_SCAN_INDEX_ILLUM
] = sample_data
;
220 /* Parse report which is specific to an usage id*/
221 static int als_parse_report(struct platform_device
*pdev
,
222 struct hid_sensor_hub_device
*hsdev
,
223 struct iio_chan_spec
*channels
,
225 struct als_state
*st
)
229 ret
= sensor_hub_input_get_attribute_info(hsdev
, HID_INPUT_REPORT
,
231 HID_USAGE_SENSOR_LIGHT_ILLUM
,
235 als_adjust_channel_bit_mask(channels
, CHANNEL_SCAN_INDEX_INTENSITY
,
237 als_adjust_channel_bit_mask(channels
, CHANNEL_SCAN_INDEX_ILLUM
,
240 dev_dbg(&pdev
->dev
, "als %x:%x\n", st
->als_illum
.index
,
241 st
->als_illum
.report_id
);
243 st
->scale_precision
= hid_sensor_format_scale(
244 HID_USAGE_SENSOR_ALS
,
246 &st
->scale_pre_decml
, &st
->scale_post_decml
);
248 /* Set Sensitivity field ids, when there is no individual modifier */
249 if (st
->common_attributes
.sensitivity
.index
< 0) {
250 sensor_hub_input_get_attribute_info(hsdev
,
251 HID_FEATURE_REPORT
, usage_id
,
252 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
253 HID_USAGE_SENSOR_DATA_LIGHT
,
254 &st
->common_attributes
.sensitivity
);
255 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
256 st
->common_attributes
.sensitivity
.index
,
257 st
->common_attributes
.sensitivity
.report_id
);
262 /* Function to initialize the processing for usage id */
263 static int hid_als_probe(struct platform_device
*pdev
)
266 static const char *name
= "als";
267 struct iio_dev
*indio_dev
;
268 struct als_state
*als_state
;
269 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
271 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(struct als_state
));
274 platform_set_drvdata(pdev
, indio_dev
);
276 als_state
= iio_priv(indio_dev
);
277 als_state
->common_attributes
.hsdev
= hsdev
;
278 als_state
->common_attributes
.pdev
= pdev
;
280 ret
= hid_sensor_parse_common_attributes(hsdev
, HID_USAGE_SENSOR_ALS
,
281 &als_state
->common_attributes
);
283 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
287 indio_dev
->channels
= kmemdup(als_channels
,
288 sizeof(als_channels
), GFP_KERNEL
);
289 if (!indio_dev
->channels
) {
290 dev_err(&pdev
->dev
, "failed to duplicate channels\n");
294 ret
= als_parse_report(pdev
, hsdev
,
295 (struct iio_chan_spec
*)indio_dev
->channels
,
296 HID_USAGE_SENSOR_ALS
, als_state
);
298 dev_err(&pdev
->dev
, "failed to setup attributes\n");
299 goto error_free_dev_mem
;
302 indio_dev
->num_channels
=
303 ARRAY_SIZE(als_channels
);
304 indio_dev
->info
= &als_info
;
305 indio_dev
->name
= name
;
306 indio_dev
->modes
= INDIO_DIRECT_MODE
;
308 atomic_set(&als_state
->common_attributes
.data_ready
, 0);
310 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
311 &als_state
->common_attributes
);
313 dev_err(&pdev
->dev
, "trigger setup failed\n");
314 goto error_free_dev_mem
;
317 ret
= iio_device_register(indio_dev
);
319 dev_err(&pdev
->dev
, "device register failed\n");
320 goto error_remove_trigger
;
323 als_state
->callbacks
.send_event
= als_proc_event
;
324 als_state
->callbacks
.capture_sample
= als_capture_sample
;
325 als_state
->callbacks
.pdev
= pdev
;
326 ret
= sensor_hub_register_callback(hsdev
, HID_USAGE_SENSOR_ALS
,
327 &als_state
->callbacks
);
329 dev_err(&pdev
->dev
, "callback reg failed\n");
330 goto error_iio_unreg
;
336 iio_device_unregister(indio_dev
);
337 error_remove_trigger
:
338 hid_sensor_remove_trigger(indio_dev
, &als_state
->common_attributes
);
340 kfree(indio_dev
->channels
);
344 /* Function to deinitialize the processing for usage id */
345 static int hid_als_remove(struct platform_device
*pdev
)
347 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
348 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
349 struct als_state
*als_state
= iio_priv(indio_dev
);
351 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_ALS
);
352 iio_device_unregister(indio_dev
);
353 hid_sensor_remove_trigger(indio_dev
, &als_state
->common_attributes
);
354 kfree(indio_dev
->channels
);
359 static const struct platform_device_id hid_als_ids
[] = {
361 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
362 .name
= "HID-SENSOR-200041",
366 MODULE_DEVICE_TABLE(platform
, hid_als_ids
);
368 static struct platform_driver hid_als_platform_driver
= {
369 .id_table
= hid_als_ids
,
371 .name
= KBUILD_MODNAME
,
372 .pm
= &hid_sensor_pm_ops
,
374 .probe
= hid_als_probe
,
375 .remove
= hid_als_remove
,
377 module_platform_driver(hid_als_platform_driver
);
379 MODULE_DESCRIPTION("HID Sensor ALS");
380 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
381 MODULE_LICENSE("GPL");