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;
28 /* Channel definitions */
29 static const struct iio_chan_spec prox_channels[] = {
31 .type = IIO_PROXIMITY,
32 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
33 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
34 BIT(IIO_CHAN_INFO_SCALE) |
35 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
36 BIT(IIO_CHAN_INFO_HYSTERESIS),
37 .scan_index = CHANNEL_SCAN_INDEX_PRESENCE,
41 /* Adjust channel real bits based on report descriptor */
42 static void prox_adjust_channel_bit_mask(struct iio_chan_spec *channels,
43 int channel, int size)
45 channels[channel].scan_type.sign = 's';
46 /* Real storage bits will change based on the report desc. */
47 channels[channel].scan_type.realbits = size * 8;
48 /* Maximum size of a sample to capture is u32 */
49 channels[channel].scan_type.storagebits = sizeof(u32) * 8;
52 /* Channel read_raw handler */
53 static int prox_read_raw(struct iio_dev *indio_dev,
54 struct iio_chan_spec const *chan,
58 struct prox_state *prox_state = iio_priv(indio_dev);
67 case IIO_CHAN_INFO_RAW:
68 switch (chan->scan_index) {
69 case CHANNEL_SCAN_INDEX_PRESENCE:
70 report_id = prox_state->prox_attr.report_id;
71 min = prox_state->prox_attr.logical_minimum;
72 address = HID_USAGE_SENSOR_HUMAN_PRESENCE;
79 hid_sensor_power_state(&prox_state->common_attributes,
81 *val = sensor_hub_input_attr_get_raw_value(
82 prox_state->common_attributes.hsdev,
83 HID_USAGE_SENSOR_PROX, address,
87 hid_sensor_power_state(&prox_state->common_attributes,
93 ret_type = IIO_VAL_INT;
95 case IIO_CHAN_INFO_SCALE:
96 *val = prox_state->prox_attr.units;
97 ret_type = IIO_VAL_INT;
99 case IIO_CHAN_INFO_OFFSET:
100 *val = hid_sensor_convert_exponent(
101 prox_state->prox_attr.unit_expo);
102 ret_type = IIO_VAL_INT;
104 case IIO_CHAN_INFO_SAMP_FREQ:
105 ret_type = hid_sensor_read_samp_freq_value(
106 &prox_state->common_attributes, val, val2);
108 case IIO_CHAN_INFO_HYSTERESIS:
109 ret_type = hid_sensor_read_raw_hyst_value(
110 &prox_state->common_attributes, val, val2);
120 /* Channel write_raw handler */
121 static int prox_write_raw(struct iio_dev *indio_dev,
122 struct iio_chan_spec const *chan,
127 struct prox_state *prox_state = iio_priv(indio_dev);
131 case IIO_CHAN_INFO_SAMP_FREQ:
132 ret = hid_sensor_write_samp_freq_value(
133 &prox_state->common_attributes, val, val2);
135 case IIO_CHAN_INFO_HYSTERESIS:
136 ret = hid_sensor_write_raw_hyst_value(
137 &prox_state->common_attributes, val, val2);
146 static const struct iio_info prox_info = {
147 .read_raw = &prox_read_raw,
148 .write_raw = &prox_write_raw,
151 /* Function to push data to buffer */
152 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
155 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
156 iio_push_to_buffers(indio_dev, data);
159 /* Callback handler to send event after all samples are received and captured */
160 static int prox_proc_event(struct hid_sensor_hub_device *hsdev,
164 struct iio_dev *indio_dev = platform_get_drvdata(priv);
165 struct prox_state *prox_state = iio_priv(indio_dev);
167 dev_dbg(&indio_dev->dev, "prox_proc_event\n");
168 if (atomic_read(&prox_state->common_attributes.data_ready))
169 hid_sensor_push_data(indio_dev,
170 &prox_state->human_presence,
171 sizeof(prox_state->human_presence));
176 /* Capture samples in local storage */
177 static int prox_capture_sample(struct hid_sensor_hub_device *hsdev,
179 size_t raw_len, char *raw_data,
182 struct iio_dev *indio_dev = platform_get_drvdata(priv);
183 struct prox_state *prox_state = iio_priv(indio_dev);
187 case HID_USAGE_SENSOR_HUMAN_PRESENCE:
188 prox_state->human_presence = *(u32 *)raw_data;
198 /* Parse report which is specific to an usage id*/
199 static int prox_parse_report(struct platform_device *pdev,
200 struct hid_sensor_hub_device *hsdev,
201 struct iio_chan_spec *channels,
203 struct prox_state *st)
207 ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
209 HID_USAGE_SENSOR_HUMAN_PRESENCE,
213 prox_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESENCE,
216 dev_dbg(&pdev->dev, "prox %x:%x\n", st->prox_attr.index,
217 st->prox_attr.report_id);
219 /* Set Sensitivity field ids, when there is no individual modifier */
220 if (st->common_attributes.sensitivity.index < 0) {
221 sensor_hub_input_get_attribute_info(hsdev,
222 HID_FEATURE_REPORT, usage_id,
223 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
224 HID_USAGE_SENSOR_DATA_PRESENCE,
225 &st->common_attributes.sensitivity);
226 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
227 st->common_attributes.sensitivity.index,
228 st->common_attributes.sensitivity.report_id);
230 if (st->common_attributes.sensitivity.index < 0)
231 sensor_hub_input_get_attribute_info(hsdev,
232 HID_FEATURE_REPORT, usage_id,
233 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
234 HID_USAGE_SENSOR_HUMAN_PRESENCE,
235 &st->common_attributes.sensitivity);
240 /* Function to initialize the processing for usage id */
241 static int hid_prox_probe(struct platform_device *pdev)
244 static const char *name = "prox";
245 struct iio_dev *indio_dev;
246 struct prox_state *prox_state;
247 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
249 indio_dev = devm_iio_device_alloc(&pdev->dev,
250 sizeof(struct prox_state));
253 platform_set_drvdata(pdev, indio_dev);
255 prox_state = iio_priv(indio_dev);
256 prox_state->common_attributes.hsdev = hsdev;
257 prox_state->common_attributes.pdev = pdev;
259 ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_PROX,
260 &prox_state->common_attributes);
262 dev_err(&pdev->dev, "failed to setup common attributes\n");
266 indio_dev->channels = kmemdup(prox_channels, sizeof(prox_channels),
268 if (!indio_dev->channels) {
269 dev_err(&pdev->dev, "failed to duplicate channels\n");
273 ret = prox_parse_report(pdev, hsdev,
274 (struct iio_chan_spec *)indio_dev->channels,
275 HID_USAGE_SENSOR_PROX, prox_state);
277 dev_err(&pdev->dev, "failed to setup attributes\n");
278 goto error_free_dev_mem;
281 indio_dev->num_channels = ARRAY_SIZE(prox_channels);
282 indio_dev->info = &prox_info;
283 indio_dev->name = name;
284 indio_dev->modes = INDIO_DIRECT_MODE;
286 atomic_set(&prox_state->common_attributes.data_ready, 0);
288 ret = hid_sensor_setup_trigger(indio_dev, name,
289 &prox_state->common_attributes);
291 dev_err(&pdev->dev, "trigger setup failed\n");
292 goto error_free_dev_mem;
295 ret = iio_device_register(indio_dev);
297 dev_err(&pdev->dev, "device register failed\n");
298 goto error_remove_trigger;
301 prox_state->callbacks.send_event = prox_proc_event;
302 prox_state->callbacks.capture_sample = prox_capture_sample;
303 prox_state->callbacks.pdev = pdev;
304 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_PROX,
305 &prox_state->callbacks);
307 dev_err(&pdev->dev, "callback reg failed\n");
308 goto error_iio_unreg;
314 iio_device_unregister(indio_dev);
315 error_remove_trigger:
316 hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
318 kfree(indio_dev->channels);
322 /* Function to deinitialize the processing for usage id */
323 static int hid_prox_remove(struct platform_device *pdev)
325 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
326 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
327 struct prox_state *prox_state = iio_priv(indio_dev);
329 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PROX);
330 iio_device_unregister(indio_dev);
331 hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
332 kfree(indio_dev->channels);
337 static const struct platform_device_id hid_prox_ids[] = {
339 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
340 .name = "HID-SENSOR-200011",
344 MODULE_DEVICE_TABLE(platform, hid_prox_ids);
346 static struct platform_driver hid_prox_platform_driver = {
347 .id_table = hid_prox_ids,
349 .name = KBUILD_MODNAME,
350 .pm = &hid_sensor_pm_ops,
352 .probe = hid_prox_probe,
353 .remove = hid_prox_remove,
355 module_platform_driver(hid_prox_platform_driver);
357 MODULE_DESCRIPTION("HID Sensor Proximity");
359 MODULE_LICENSE("GPL");