1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * HID Sensors Driver
4 * Copyright (c) 2014, Intel Corporation.
5 */
6 #include <linux/device.h>
7 #include <linux/platform_device.h>
8 #include <linux/module.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/slab.h>
11 #include <linux/hid-sensor-hub.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/buffer.h>
14 #include "../common/hid-sensors/hid-sensor-trigger.h"
15
16 static const u32 prox_usage_ids[] = {
17 HID_USAGE_SENSOR_HUMAN_PRESENCE,
18 HID_USAGE_SENSOR_HUMAN_PROXIMITY,
19 HID_USAGE_SENSOR_HUMAN_ATTENTION,
20 };
21
22 #define MAX_CHANNELS ARRAY_SIZE(prox_usage_ids)
23
24 enum {
25 HID_HUMAN_PRESENCE,
26 HID_HUMAN_PROXIMITY,
27 HID_HUMAN_ATTENTION,
28 };
29
30 struct prox_state {
31 struct hid_sensor_hub_callbacks callbacks;
32 struct hid_sensor_common common_attributes;
33 struct hid_sensor_hub_attribute_info prox_attr[MAX_CHANNELS];
34 struct iio_chan_spec channels[MAX_CHANNELS];
35 u32 channel2usage[MAX_CHANNELS];
36 u32 human_presence[MAX_CHANNELS];
37 int scale_pre_decml[MAX_CHANNELS];
38 int scale_post_decml[MAX_CHANNELS];
39 int scale_precision[MAX_CHANNELS];
40 unsigned long scan_mask[2]; /* One entry plus one terminator. */
41 int num_channels;
42 };
43
44 static const u32 prox_sensitivity_addresses[] = {
45 HID_USAGE_SENSOR_HUMAN_PRESENCE,
46 HID_USAGE_SENSOR_DATA_PRESENCE,
47 };
48
49 #define PROX_CHANNEL(_is_proximity, _channel) \
50 {\
51 .type = _is_proximity ? IIO_PROXIMITY : IIO_ATTENTION,\
52 .info_mask_separate = \
53 (_is_proximity ? BIT(IIO_CHAN_INFO_RAW) :\
54 BIT(IIO_CHAN_INFO_PROCESSED)) |\
55 BIT(IIO_CHAN_INFO_OFFSET) |\
56 BIT(IIO_CHAN_INFO_SCALE) |\
57 BIT(IIO_CHAN_INFO_SAMP_FREQ) |\
58 BIT(IIO_CHAN_INFO_HYSTERESIS),\
59 .indexed = _is_proximity,\
60 .channel = _channel,\
61 }
62
63 /* Channel definitions (same order as prox_usage_ids) */
64 static const struct iio_chan_spec prox_channels[] = {
65 PROX_CHANNEL(true, HID_HUMAN_PRESENCE),
66 PROX_CHANNEL(true, HID_HUMAN_PROXIMITY),
67 PROX_CHANNEL(false, 0),
68 };
69
70 /* Adjust channel real bits based on report descriptor */
prox_adjust_channel_bit_mask(struct iio_chan_spec * channels,int channel,int size)71 static void prox_adjust_channel_bit_mask(struct iio_chan_spec *channels,
72 int channel, int size)
73 {
74 channels[channel].scan_type.sign = 's';
75 /* Real storage bits will change based on the report desc. */
76 channels[channel].scan_type.realbits = size * 8;
77 /* Maximum size of a sample to capture is u32 */
78 channels[channel].scan_type.storagebits = sizeof(u32) * 8;
79 }
80
81 /* Channel read_raw handler */
prox_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)82 static int prox_read_raw(struct iio_dev *indio_dev,
83 struct iio_chan_spec const *chan,
84 int *val, int *val2,
85 long mask)
86 {
87 struct prox_state *prox_state = iio_priv(indio_dev);
88 struct hid_sensor_hub_device *hsdev;
89 int report_id;
90 u32 address;
91 int ret_type;
92 s32 min;
93
94 *val = 0;
95 *val2 = 0;
96 switch (mask) {
97 case IIO_CHAN_INFO_RAW:
98 case IIO_CHAN_INFO_PROCESSED:
99 if (chan->scan_index >= prox_state->num_channels)
100 return -EINVAL;
101 address = prox_state->channel2usage[chan->scan_index];
102 report_id = prox_state->prox_attr[chan->scan_index].report_id;
103 hsdev = prox_state->common_attributes.hsdev;
104 min = prox_state->prox_attr[chan->scan_index].logical_minimum;
105 hid_sensor_power_state(&prox_state->common_attributes, true);
106 *val = sensor_hub_input_attr_get_raw_value(hsdev,
107 hsdev->usage,
108 address,
109 report_id,
110 SENSOR_HUB_SYNC,
111 min < 0);
112 if (prox_state->channel2usage[chan->scan_index] ==
113 HID_USAGE_SENSOR_HUMAN_ATTENTION)
114 *val *= 100;
115 hid_sensor_power_state(&prox_state->common_attributes, false);
116 ret_type = IIO_VAL_INT;
117 break;
118 case IIO_CHAN_INFO_SCALE:
119 if (chan->scan_index >= prox_state->num_channels)
120 return -EINVAL;
121
122 *val = prox_state->scale_pre_decml[chan->scan_index];
123 *val2 = prox_state->scale_post_decml[chan->scan_index];
124 ret_type = prox_state->scale_precision[chan->scan_index];
125 break;
126 case IIO_CHAN_INFO_OFFSET:
127 *val = 0;
128 ret_type = IIO_VAL_INT;
129 break;
130 case IIO_CHAN_INFO_SAMP_FREQ:
131 ret_type = hid_sensor_read_samp_freq_value(
132 &prox_state->common_attributes, val, val2);
133 break;
134 case IIO_CHAN_INFO_HYSTERESIS:
135 ret_type = hid_sensor_read_raw_hyst_value(
136 &prox_state->common_attributes, val, val2);
137 break;
138 default:
139 ret_type = -EINVAL;
140 break;
141 }
142
143 return ret_type;
144 }
145
146 /* Channel write_raw handler */
prox_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)147 static int prox_write_raw(struct iio_dev *indio_dev,
148 struct iio_chan_spec const *chan,
149 int val,
150 int val2,
151 long mask)
152 {
153 struct prox_state *prox_state = iio_priv(indio_dev);
154 int ret = 0;
155
156 switch (mask) {
157 case IIO_CHAN_INFO_SAMP_FREQ:
158 ret = hid_sensor_write_samp_freq_value(
159 &prox_state->common_attributes, val, val2);
160 break;
161 case IIO_CHAN_INFO_HYSTERESIS:
162 ret = hid_sensor_write_raw_hyst_value(
163 &prox_state->common_attributes, val, val2);
164 break;
165 default:
166 ret = -EINVAL;
167 }
168
169 return ret;
170 }
171
172 static const struct iio_info prox_info = {
173 .read_raw = &prox_read_raw,
174 .write_raw = &prox_write_raw,
175 };
176
177 /* Callback handler to send event after all samples are received and captured */
prox_proc_event(struct hid_sensor_hub_device * hsdev,unsigned usage_id,void * priv)178 static int prox_proc_event(struct hid_sensor_hub_device *hsdev,
179 unsigned usage_id,
180 void *priv)
181 {
182 struct iio_dev *indio_dev = platform_get_drvdata(priv);
183 struct prox_state *prox_state = iio_priv(indio_dev);
184
185 dev_dbg(&indio_dev->dev, "prox_proc_event\n");
186 if (atomic_read(&prox_state->common_attributes.data_ready)) {
187 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
188 iio_push_to_buffers(indio_dev, &prox_state->human_presence);
189 }
190
191 return 0;
192 }
193
194 /* Capture samples in local storage */
prox_capture_sample(struct hid_sensor_hub_device * hsdev,unsigned usage_id,size_t raw_len,char * raw_data,void * priv)195 static int prox_capture_sample(struct hid_sensor_hub_device *hsdev,
196 unsigned usage_id,
197 size_t raw_len, char *raw_data,
198 void *priv)
199 {
200 struct iio_dev *indio_dev = platform_get_drvdata(priv);
201 struct prox_state *prox_state = iio_priv(indio_dev);
202 int multiplier = 1;
203 int chan;
204
205 for (chan = 0; chan < prox_state->num_channels; chan++)
206 if (prox_state->channel2usage[chan] == usage_id)
207 break;
208 if (chan == prox_state->num_channels)
209 return -EINVAL;
210
211 if (usage_id == HID_USAGE_SENSOR_HUMAN_ATTENTION)
212 multiplier = 100;
213
214 switch (raw_len) {
215 case 1:
216 prox_state->human_presence[chan] = *(u8 *)raw_data * multiplier;
217 return 0;
218 case 4:
219 prox_state->human_presence[chan] = *(u32 *)raw_data * multiplier;
220 return 0;
221 }
222
223 return -EINVAL;
224 }
225
226 /* Parse report which is specific to an usage id*/
prox_parse_report(struct platform_device * pdev,struct hid_sensor_hub_device * hsdev,struct prox_state * st)227 static int prox_parse_report(struct platform_device *pdev,
228 struct hid_sensor_hub_device *hsdev,
229 struct prox_state *st)
230 {
231 struct iio_chan_spec *channels = st->channels;
232 int index = 0;
233 int ret;
234 int i;
235
236 for (i = 0; i < MAX_CHANNELS; i++) {
237 u32 usage_id = prox_usage_ids[i];
238
239 ret = sensor_hub_input_get_attribute_info(hsdev,
240 HID_INPUT_REPORT,
241 hsdev->usage,
242 usage_id,
243 &st->prox_attr[index]);
244 if (ret < 0)
245 continue;
246 st->channel2usage[index] = usage_id;
247 st->scan_mask[0] |= BIT(index);
248 channels[index] = prox_channels[i];
249 channels[index].scan_index = index;
250 prox_adjust_channel_bit_mask(channels, index,
251 st->prox_attr[index].size);
252 dev_dbg(&pdev->dev, "prox %x:%x\n", st->prox_attr[index].index,
253 st->prox_attr[index].report_id);
254 st->scale_precision[index] =
255 hid_sensor_format_scale(usage_id, &st->prox_attr[index],
256 &st->scale_pre_decml[index],
257 &st->scale_post_decml[index]);
258 index++;
259 }
260
261 if (!index)
262 return ret;
263
264 st->num_channels = index;
265
266 return 0;
267 }
268
269 /* Function to initialize the processing for usage id */
hid_prox_probe(struct platform_device * pdev)270 static int hid_prox_probe(struct platform_device *pdev)
271 {
272 struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
273 int ret = 0;
274 static const char *name = "prox";
275 struct iio_dev *indio_dev;
276 struct prox_state *prox_state;
277
278 indio_dev = devm_iio_device_alloc(&pdev->dev,
279 sizeof(struct prox_state));
280 if (!indio_dev)
281 return -ENOMEM;
282 platform_set_drvdata(pdev, indio_dev);
283
284 prox_state = iio_priv(indio_dev);
285 prox_state->common_attributes.hsdev = hsdev;
286 prox_state->common_attributes.pdev = pdev;
287
288 ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage,
289 &prox_state->common_attributes,
290 prox_sensitivity_addresses,
291 ARRAY_SIZE(prox_sensitivity_addresses));
292 if (ret) {
293 dev_err(&pdev->dev, "failed to setup common attributes\n");
294 return ret;
295 }
296
297 ret = prox_parse_report(pdev, hsdev, prox_state);
298 if (ret) {
299 dev_err(&pdev->dev, "failed to setup attributes\n");
300 return ret;
301 }
302
303 indio_dev->num_channels = prox_state->num_channels;
304 indio_dev->channels = prox_state->channels;
305 indio_dev->available_scan_masks = prox_state->scan_mask;
306 indio_dev->info = &prox_info;
307 indio_dev->name = name;
308 indio_dev->modes = INDIO_DIRECT_MODE;
309
310 atomic_set(&prox_state->common_attributes.data_ready, 0);
311
312 ret = hid_sensor_setup_trigger(indio_dev, name,
313 &prox_state->common_attributes);
314 if (ret) {
315 dev_err(&pdev->dev, "trigger setup failed\n");
316 return ret;
317 }
318
319 ret = iio_device_register(indio_dev);
320 if (ret) {
321 dev_err(&pdev->dev, "device register failed\n");
322 goto error_remove_trigger;
323 }
324
325 prox_state->callbacks.send_event = prox_proc_event;
326 prox_state->callbacks.capture_sample = prox_capture_sample;
327 prox_state->callbacks.pdev = pdev;
328 ret = sensor_hub_register_callback(hsdev, hsdev->usage,
329 &prox_state->callbacks);
330 if (ret < 0) {
331 dev_err(&pdev->dev, "callback reg failed\n");
332 goto error_iio_unreg;
333 }
334
335 return ret;
336
337 error_iio_unreg:
338 iio_device_unregister(indio_dev);
339 error_remove_trigger:
340 hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
341 return ret;
342 }
343
344 /* Function to deinitialize the processing for usage id */
hid_prox_remove(struct platform_device * pdev)345 static void hid_prox_remove(struct platform_device *pdev)
346 {
347 struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
348 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
349 struct prox_state *prox_state = iio_priv(indio_dev);
350
351 sensor_hub_remove_callback(hsdev, hsdev->usage);
352 iio_device_unregister(indio_dev);
353 hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
354 }
355
356 static const struct platform_device_id hid_prox_ids[] = {
357 {
358 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
359 .name = "HID-SENSOR-200011",
360 },
361 {
362 /* Format: HID-SENSOR-tag-usage_id_in_hex_lowercase */
363 .name = "HID-SENSOR-LISS-0226",
364 },
365 { /* sentinel */ }
366 };
367 MODULE_DEVICE_TABLE(platform, hid_prox_ids);
368
369 static struct platform_driver hid_prox_platform_driver = {
370 .id_table = hid_prox_ids,
371 .driver = {
372 .name = KBUILD_MODNAME,
373 .pm = &hid_sensor_pm_ops,
374 },
375 .probe = hid_prox_probe,
376 .remove = hid_prox_remove,
377 };
378 module_platform_driver(hid_prox_platform_driver);
379
380 MODULE_DESCRIPTION("HID Sensor Proximity");
381 MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
382 MODULE_LICENSE("GPL");
383 MODULE_IMPORT_NS("IIO_HID");
384