1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2014, 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"
19 #define CHANNEL_SCAN_INDEX_PRESENCE 0
22 struct hid_sensor_hub_callbacks callbacks;
23 struct hid_sensor_common common_attributes;
24 struct hid_sensor_hub_attribute_info prox_attr;
31 static const u32 prox_sensitivity_addresses[] = {
32 HID_USAGE_SENSOR_HUMAN_PRESENCE,
33 HID_USAGE_SENSOR_DATA_PRESENCE,
36 /* Channel definitions */
37 static const struct iio_chan_spec prox_channels[] = {
39 .type = IIO_PROXIMITY,
40 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
41 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
42 BIT(IIO_CHAN_INFO_SCALE) |
43 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
44 BIT(IIO_CHAN_INFO_HYSTERESIS),
45 .scan_index = CHANNEL_SCAN_INDEX_PRESENCE,
49 /* Adjust channel real bits based on report descriptor */
50 static void prox_adjust_channel_bit_mask(struct iio_chan_spec *channels,
51 int channel, int size)
53 channels[channel].scan_type.sign = 's';
54 /* Real storage bits will change based on the report desc. */
55 channels[channel].scan_type.realbits = size * 8;
56 /* Maximum size of a sample to capture is u32 */
57 channels[channel].scan_type.storagebits = sizeof(u32) * 8;
60 /* Channel read_raw handler */
61 static int prox_read_raw(struct iio_dev *indio_dev,
62 struct iio_chan_spec const *chan,
66 struct prox_state *prox_state = iio_priv(indio_dev);
75 case IIO_CHAN_INFO_RAW:
76 switch (chan->scan_index) {
77 case CHANNEL_SCAN_INDEX_PRESENCE:
78 report_id = prox_state->prox_attr.report_id;
79 min = prox_state->prox_attr.logical_minimum;
80 address = HID_USAGE_SENSOR_HUMAN_PRESENCE;
87 hid_sensor_power_state(&prox_state->common_attributes,
89 *val = sensor_hub_input_attr_get_raw_value(
90 prox_state->common_attributes.hsdev,
91 HID_USAGE_SENSOR_PROX, address,
95 hid_sensor_power_state(&prox_state->common_attributes,
101 ret_type = IIO_VAL_INT;
103 case IIO_CHAN_INFO_SCALE:
104 *val = prox_state->scale_pre_decml;
105 *val2 = prox_state->scale_post_decml;
106 ret_type = prox_state->scale_precision;
108 case IIO_CHAN_INFO_OFFSET:
109 *val = hid_sensor_convert_exponent(
110 prox_state->prox_attr.unit_expo);
111 ret_type = IIO_VAL_INT;
113 case IIO_CHAN_INFO_SAMP_FREQ:
114 ret_type = hid_sensor_read_samp_freq_value(
115 &prox_state->common_attributes, val, val2);
117 case IIO_CHAN_INFO_HYSTERESIS:
118 ret_type = hid_sensor_read_raw_hyst_value(
119 &prox_state->common_attributes, val, val2);
129 /* Channel write_raw handler */
130 static int prox_write_raw(struct iio_dev *indio_dev,
131 struct iio_chan_spec const *chan,
136 struct prox_state *prox_state = iio_priv(indio_dev);
140 case IIO_CHAN_INFO_SAMP_FREQ:
141 ret = hid_sensor_write_samp_freq_value(
142 &prox_state->common_attributes, val, val2);
144 case IIO_CHAN_INFO_HYSTERESIS:
145 ret = hid_sensor_write_raw_hyst_value(
146 &prox_state->common_attributes, val, val2);
155 static const struct iio_info prox_info = {
156 .read_raw = &prox_read_raw,
157 .write_raw = &prox_write_raw,
160 /* Function to push data to buffer */
161 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
164 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
165 iio_push_to_buffers(indio_dev, data);
168 /* Callback handler to send event after all samples are received and captured */
169 static int prox_proc_event(struct hid_sensor_hub_device *hsdev,
173 struct iio_dev *indio_dev = platform_get_drvdata(priv);
174 struct prox_state *prox_state = iio_priv(indio_dev);
176 dev_dbg(&indio_dev->dev, "prox_proc_event\n");
177 if (atomic_read(&prox_state->common_attributes.data_ready))
178 hid_sensor_push_data(indio_dev,
179 &prox_state->human_presence,
180 sizeof(prox_state->human_presence));
185 /* Capture samples in local storage */
186 static int prox_capture_sample(struct hid_sensor_hub_device *hsdev,
188 size_t raw_len, char *raw_data,
191 struct iio_dev *indio_dev = platform_get_drvdata(priv);
192 struct prox_state *prox_state = iio_priv(indio_dev);
196 case HID_USAGE_SENSOR_HUMAN_PRESENCE:
197 prox_state->human_presence = *(u32 *)raw_data;
207 /* Parse report which is specific to an usage id*/
208 static int prox_parse_report(struct platform_device *pdev,
209 struct hid_sensor_hub_device *hsdev,
210 struct iio_chan_spec *channels,
212 struct prox_state *st)
216 ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
218 HID_USAGE_SENSOR_HUMAN_PRESENCE,
222 prox_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESENCE,
225 dev_dbg(&pdev->dev, "prox %x:%x\n", st->prox_attr.index,
226 st->prox_attr.report_id);
231 /* Function to initialize the processing for usage id */
232 static int hid_prox_probe(struct platform_device *pdev)
235 static const char *name = "prox";
236 struct iio_dev *indio_dev;
237 struct prox_state *prox_state;
238 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
240 indio_dev = devm_iio_device_alloc(&pdev->dev,
241 sizeof(struct prox_state));
244 platform_set_drvdata(pdev, indio_dev);
246 prox_state = iio_priv(indio_dev);
247 prox_state->common_attributes.hsdev = hsdev;
248 prox_state->common_attributes.pdev = pdev;
250 ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_PROX,
251 &prox_state->common_attributes,
252 prox_sensitivity_addresses,
253 ARRAY_SIZE(prox_sensitivity_addresses));
255 dev_err(&pdev->dev, "failed to setup common attributes\n");
259 indio_dev->channels = kmemdup(prox_channels, sizeof(prox_channels),
261 if (!indio_dev->channels) {
262 dev_err(&pdev->dev, "failed to duplicate channels\n");
266 ret = prox_parse_report(pdev, hsdev,
267 (struct iio_chan_spec *)indio_dev->channels,
268 HID_USAGE_SENSOR_PROX, prox_state);
270 dev_err(&pdev->dev, "failed to setup attributes\n");
271 goto error_free_dev_mem;
274 indio_dev->num_channels = ARRAY_SIZE(prox_channels);
275 indio_dev->info = &prox_info;
276 indio_dev->name = name;
277 indio_dev->modes = INDIO_DIRECT_MODE;
279 atomic_set(&prox_state->common_attributes.data_ready, 0);
281 ret = hid_sensor_setup_trigger(indio_dev, name,
282 &prox_state->common_attributes);
284 dev_err(&pdev->dev, "trigger setup failed\n");
285 goto error_free_dev_mem;
288 ret = iio_device_register(indio_dev);
290 dev_err(&pdev->dev, "device register failed\n");
291 goto error_remove_trigger;
294 prox_state->callbacks.send_event = prox_proc_event;
295 prox_state->callbacks.capture_sample = prox_capture_sample;
296 prox_state->callbacks.pdev = pdev;
297 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_PROX,
298 &prox_state->callbacks);
300 dev_err(&pdev->dev, "callback reg failed\n");
301 goto error_iio_unreg;
307 iio_device_unregister(indio_dev);
308 error_remove_trigger:
309 hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
311 kfree(indio_dev->channels);
315 /* Function to deinitialize the processing for usage id */
316 static int hid_prox_remove(struct platform_device *pdev)
318 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
319 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
320 struct prox_state *prox_state = iio_priv(indio_dev);
322 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PROX);
323 iio_device_unregister(indio_dev);
324 hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
325 kfree(indio_dev->channels);
330 static const struct platform_device_id hid_prox_ids[] = {
332 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
333 .name = "HID-SENSOR-200011",
337 MODULE_DEVICE_TABLE(platform, hid_prox_ids);
339 static struct platform_driver hid_prox_platform_driver = {
340 .id_table = hid_prox_ids,
342 .name = KBUILD_MODNAME,
343 .pm = &hid_sensor_pm_ops,
345 .probe = hid_prox_probe,
346 .remove = hid_prox_remove,
348 module_platform_driver(hid_prox_platform_driver);
350 MODULE_DESCRIPTION("HID Sensor Proximity");
351 MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
352 MODULE_LICENSE("GPL");