1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * HID Sensors Driver
4 * Copyright (c) 2013, Intel Corporation.
5 */
6
7 #include <linux/device.h>
8 #include <linux/platform_device.h>
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/slab.h>
13 #include <linux/delay.h>
14 #include <linux/hid-sensor-hub.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
17 #include <linux/iio/buffer.h>
18 #include "../common/hid-sensors/hid-sensor-trigger.h"
19
20 enum incl_3d_channel {
21 CHANNEL_SCAN_INDEX_X,
22 CHANNEL_SCAN_INDEX_Y,
23 CHANNEL_SCAN_INDEX_Z,
24 INCLI_3D_CHANNEL_MAX,
25 };
26
27 struct incl_3d_state {
28 struct hid_sensor_hub_callbacks callbacks;
29 struct hid_sensor_common common_attributes;
30 struct hid_sensor_hub_attribute_info incl[INCLI_3D_CHANNEL_MAX];
31 u32 incl_val[INCLI_3D_CHANNEL_MAX];
32 int scale_pre_decml;
33 int scale_post_decml;
34 int scale_precision;
35 int value_offset;
36 };
37
38 static const u32 incl_3d_addresses[INCLI_3D_CHANNEL_MAX] = {
39 HID_USAGE_SENSOR_ORIENT_TILT_X,
40 HID_USAGE_SENSOR_ORIENT_TILT_Y,
41 HID_USAGE_SENSOR_ORIENT_TILT_Z
42 };
43
44 /* Channel definitions */
45 static const struct iio_chan_spec incl_3d_channels[] = {
46 {
47 .type = IIO_INCLI,
48 .modified = 1,
49 .channel2 = IIO_MOD_X,
50 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
51 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
52 BIT(IIO_CHAN_INFO_SCALE) |
53 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
54 BIT(IIO_CHAN_INFO_HYSTERESIS),
55 .scan_index = CHANNEL_SCAN_INDEX_X,
56 }, {
57 .type = IIO_INCLI,
58 .modified = 1,
59 .channel2 = IIO_MOD_Y,
60 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
61 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
62 BIT(IIO_CHAN_INFO_SCALE) |
63 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
64 BIT(IIO_CHAN_INFO_HYSTERESIS),
65 .scan_index = CHANNEL_SCAN_INDEX_Y,
66 }, {
67 .type = IIO_INCLI,
68 .modified = 1,
69 .channel2 = IIO_MOD_Z,
70 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
71 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
72 BIT(IIO_CHAN_INFO_SCALE) |
73 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
74 BIT(IIO_CHAN_INFO_HYSTERESIS),
75 .scan_index = CHANNEL_SCAN_INDEX_Z,
76 }
77 };
78
79 /* Adjust channel real bits based on report descriptor */
incl_3d_adjust_channel_bit_mask(struct iio_chan_spec * chan,int size)80 static void incl_3d_adjust_channel_bit_mask(struct iio_chan_spec *chan,
81 int size)
82 {
83 chan->scan_type.sign = 's';
84 /* Real storage bits will change based on the report desc. */
85 chan->scan_type.realbits = size * 8;
86 /* Maximum size of a sample to capture is u32 */
87 chan->scan_type.storagebits = sizeof(u32) * 8;
88 }
89
90 /* Channel read_raw handler */
incl_3d_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)91 static int incl_3d_read_raw(struct iio_dev *indio_dev,
92 struct iio_chan_spec const *chan,
93 int *val, int *val2,
94 long mask)
95 {
96 struct incl_3d_state *incl_state = iio_priv(indio_dev);
97 int report_id = -1;
98 u32 address;
99 int ret_type;
100 s32 min;
101
102 *val = 0;
103 *val2 = 0;
104 switch (mask) {
105 case IIO_CHAN_INFO_RAW:
106 hid_sensor_power_state(&incl_state->common_attributes, true);
107 report_id = incl_state->incl[chan->scan_index].report_id;
108 min = incl_state->incl[chan->scan_index].logical_minimum;
109 address = incl_3d_addresses[chan->scan_index];
110 if (report_id >= 0)
111 *val = sensor_hub_input_attr_get_raw_value(
112 incl_state->common_attributes.hsdev,
113 HID_USAGE_SENSOR_INCLINOMETER_3D, address,
114 report_id,
115 SENSOR_HUB_SYNC,
116 min < 0);
117 else {
118 hid_sensor_power_state(&incl_state->common_attributes,
119 false);
120 return -EINVAL;
121 }
122 hid_sensor_power_state(&incl_state->common_attributes, false);
123 ret_type = IIO_VAL_INT;
124 break;
125 case IIO_CHAN_INFO_SCALE:
126 *val = incl_state->scale_pre_decml;
127 *val2 = incl_state->scale_post_decml;
128 ret_type = incl_state->scale_precision;
129 break;
130 case IIO_CHAN_INFO_OFFSET:
131 *val = incl_state->value_offset;
132 ret_type = IIO_VAL_INT;
133 break;
134 case IIO_CHAN_INFO_SAMP_FREQ:
135 ret_type = hid_sensor_read_samp_freq_value(
136 &incl_state->common_attributes, val, val2);
137 break;
138 case IIO_CHAN_INFO_HYSTERESIS:
139 ret_type = hid_sensor_read_raw_hyst_value(
140 &incl_state->common_attributes, val, val2);
141 break;
142 default:
143 ret_type = -EINVAL;
144 break;
145 }
146
147 return ret_type;
148 }
149
150 /* Channel write_raw handler */
incl_3d_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)151 static int incl_3d_write_raw(struct iio_dev *indio_dev,
152 struct iio_chan_spec const *chan,
153 int val,
154 int val2,
155 long mask)
156 {
157 struct incl_3d_state *incl_state = iio_priv(indio_dev);
158 int ret;
159
160 switch (mask) {
161 case IIO_CHAN_INFO_SAMP_FREQ:
162 ret = hid_sensor_write_samp_freq_value(
163 &incl_state->common_attributes, val, val2);
164 break;
165 case IIO_CHAN_INFO_HYSTERESIS:
166 ret = hid_sensor_write_raw_hyst_value(
167 &incl_state->common_attributes, val, val2);
168 break;
169 default:
170 ret = -EINVAL;
171 }
172
173 return ret;
174 }
175
176 static const struct iio_info incl_3d_info = {
177 .read_raw = &incl_3d_read_raw,
178 .write_raw = &incl_3d_write_raw,
179 };
180
181 /* Function to push data to buffer */
hid_sensor_push_data(struct iio_dev * indio_dev,u8 * data,int len)182 static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
183 {
184 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
185 iio_push_to_buffers(indio_dev, (u8 *)data);
186 }
187
188 /* Callback handler to send event after all samples are received and captured */
incl_3d_proc_event(struct hid_sensor_hub_device * hsdev,unsigned usage_id,void * priv)189 static int incl_3d_proc_event(struct hid_sensor_hub_device *hsdev,
190 unsigned usage_id,
191 void *priv)
192 {
193 struct iio_dev *indio_dev = platform_get_drvdata(priv);
194 struct incl_3d_state *incl_state = iio_priv(indio_dev);
195
196 dev_dbg(&indio_dev->dev, "incl_3d_proc_event\n");
197 if (atomic_read(&incl_state->common_attributes.data_ready))
198 hid_sensor_push_data(indio_dev,
199 (u8 *)incl_state->incl_val,
200 sizeof(incl_state->incl_val));
201
202 return 0;
203 }
204
205 /* Capture samples in local storage */
incl_3d_capture_sample(struct hid_sensor_hub_device * hsdev,unsigned usage_id,size_t raw_len,char * raw_data,void * priv)206 static int incl_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
207 unsigned usage_id,
208 size_t raw_len, char *raw_data,
209 void *priv)
210 {
211 struct iio_dev *indio_dev = platform_get_drvdata(priv);
212 struct incl_3d_state *incl_state = iio_priv(indio_dev);
213 int ret = 0;
214
215 switch (usage_id) {
216 case HID_USAGE_SENSOR_ORIENT_TILT_X:
217 incl_state->incl_val[CHANNEL_SCAN_INDEX_X] = *(u32 *)raw_data;
218 break;
219 case HID_USAGE_SENSOR_ORIENT_TILT_Y:
220 incl_state->incl_val[CHANNEL_SCAN_INDEX_Y] = *(u32 *)raw_data;
221 break;
222 case HID_USAGE_SENSOR_ORIENT_TILT_Z:
223 incl_state->incl_val[CHANNEL_SCAN_INDEX_Z] = *(u32 *)raw_data;
224 break;
225 default:
226 ret = -EINVAL;
227 break;
228 }
229
230 return ret;
231 }
232
233 /* Parse report which is specific to an usage id*/
incl_3d_parse_report(struct platform_device * pdev,struct hid_sensor_hub_device * hsdev,struct iio_chan_spec * channels,unsigned usage_id,struct incl_3d_state * st)234 static int incl_3d_parse_report(struct platform_device *pdev,
235 struct hid_sensor_hub_device *hsdev,
236 struct iio_chan_spec *channels,
237 unsigned usage_id,
238 struct incl_3d_state *st)
239 {
240 int ret;
241
242 ret = sensor_hub_input_get_attribute_info(hsdev,
243 HID_INPUT_REPORT,
244 usage_id,
245 HID_USAGE_SENSOR_ORIENT_TILT_X,
246 &st->incl[CHANNEL_SCAN_INDEX_X]);
247 if (ret)
248 return ret;
249 incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_X],
250 st->incl[CHANNEL_SCAN_INDEX_X].size);
251
252 ret = sensor_hub_input_get_attribute_info(hsdev,
253 HID_INPUT_REPORT,
254 usage_id,
255 HID_USAGE_SENSOR_ORIENT_TILT_Y,
256 &st->incl[CHANNEL_SCAN_INDEX_Y]);
257 if (ret)
258 return ret;
259 incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_Y],
260 st->incl[CHANNEL_SCAN_INDEX_Y].size);
261
262 ret = sensor_hub_input_get_attribute_info(hsdev,
263 HID_INPUT_REPORT,
264 usage_id,
265 HID_USAGE_SENSOR_ORIENT_TILT_Z,
266 &st->incl[CHANNEL_SCAN_INDEX_Z]);
267 if (ret)
268 return ret;
269 incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_Z],
270 st->incl[CHANNEL_SCAN_INDEX_Z].size);
271
272 dev_dbg(&pdev->dev, "incl_3d %x:%x, %x:%x, %x:%x\n",
273 st->incl[0].index,
274 st->incl[0].report_id,
275 st->incl[1].index, st->incl[1].report_id,
276 st->incl[2].index, st->incl[2].report_id);
277
278 st->scale_precision = hid_sensor_format_scale(
279 HID_USAGE_SENSOR_INCLINOMETER_3D,
280 &st->incl[CHANNEL_SCAN_INDEX_X],
281 &st->scale_pre_decml, &st->scale_post_decml);
282
283 /* Set Sensitivity field ids, when there is no individual modifier */
284 if (st->common_attributes.sensitivity.index < 0) {
285 sensor_hub_input_get_attribute_info(hsdev,
286 HID_FEATURE_REPORT, usage_id,
287 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
288 HID_USAGE_SENSOR_DATA_ORIENTATION,
289 &st->common_attributes.sensitivity);
290 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
291 st->common_attributes.sensitivity.index,
292 st->common_attributes.sensitivity.report_id);
293 }
294 return ret;
295 }
296
297 /* Function to initialize the processing for usage id */
hid_incl_3d_probe(struct platform_device * pdev)298 static int hid_incl_3d_probe(struct platform_device *pdev)
299 {
300 int ret;
301 static char *name = "incli_3d";
302 struct iio_dev *indio_dev;
303 struct incl_3d_state *incl_state;
304 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
305
306 indio_dev = devm_iio_device_alloc(&pdev->dev,
307 sizeof(struct incl_3d_state));
308 if (indio_dev == NULL)
309 return -ENOMEM;
310
311 platform_set_drvdata(pdev, indio_dev);
312
313 incl_state = iio_priv(indio_dev);
314 incl_state->common_attributes.hsdev = hsdev;
315 incl_state->common_attributes.pdev = pdev;
316
317 ret = hid_sensor_parse_common_attributes(hsdev,
318 HID_USAGE_SENSOR_INCLINOMETER_3D,
319 &incl_state->common_attributes);
320 if (ret) {
321 dev_err(&pdev->dev, "failed to setup common attributes\n");
322 return ret;
323 }
324
325 indio_dev->channels = kmemdup(incl_3d_channels,
326 sizeof(incl_3d_channels), GFP_KERNEL);
327 if (!indio_dev->channels) {
328 dev_err(&pdev->dev, "failed to duplicate channels\n");
329 return -ENOMEM;
330 }
331
332 ret = incl_3d_parse_report(pdev, hsdev,
333 (struct iio_chan_spec *)indio_dev->channels,
334 HID_USAGE_SENSOR_INCLINOMETER_3D,
335 incl_state);
336 if (ret) {
337 dev_err(&pdev->dev, "failed to setup attributes\n");
338 goto error_free_dev_mem;
339 }
340
341 indio_dev->num_channels = ARRAY_SIZE(incl_3d_channels);
342 indio_dev->info = &incl_3d_info;
343 indio_dev->name = name;
344 indio_dev->modes = INDIO_DIRECT_MODE;
345
346 atomic_set(&incl_state->common_attributes.data_ready, 0);
347
348 ret = hid_sensor_setup_trigger(indio_dev, name,
349 &incl_state->common_attributes);
350 if (ret) {
351 dev_err(&pdev->dev, "trigger setup failed\n");
352 goto error_free_dev_mem;
353 }
354
355 ret = iio_device_register(indio_dev);
356 if (ret) {
357 dev_err(&pdev->dev, "device register failed\n");
358 goto error_remove_trigger;
359 }
360
361 incl_state->callbacks.send_event = incl_3d_proc_event;
362 incl_state->callbacks.capture_sample = incl_3d_capture_sample;
363 incl_state->callbacks.pdev = pdev;
364 ret = sensor_hub_register_callback(hsdev,
365 HID_USAGE_SENSOR_INCLINOMETER_3D,
366 &incl_state->callbacks);
367 if (ret) {
368 dev_err(&pdev->dev, "callback reg failed\n");
369 goto error_iio_unreg;
370 }
371
372 return 0;
373
374 error_iio_unreg:
375 iio_device_unregister(indio_dev);
376 error_remove_trigger:
377 hid_sensor_remove_trigger(indio_dev, &incl_state->common_attributes);
378 error_free_dev_mem:
379 kfree(indio_dev->channels);
380 return ret;
381 }
382
383 /* Function to deinitialize the processing for usage id */
hid_incl_3d_remove(struct platform_device * pdev)384 static int hid_incl_3d_remove(struct platform_device *pdev)
385 {
386 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
387 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
388 struct incl_3d_state *incl_state = iio_priv(indio_dev);
389
390 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_INCLINOMETER_3D);
391 iio_device_unregister(indio_dev);
392 hid_sensor_remove_trigger(indio_dev, &incl_state->common_attributes);
393 kfree(indio_dev->channels);
394
395 return 0;
396 }
397
398 static const struct platform_device_id hid_incl_3d_ids[] = {
399 {
400 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
401 .name = "HID-SENSOR-200086",
402 },
403 { /* sentinel */ }
404 };
405 MODULE_DEVICE_TABLE(platform, hid_incl_3d_ids);
406
407 static struct platform_driver hid_incl_3d_platform_driver = {
408 .id_table = hid_incl_3d_ids,
409 .driver = {
410 .name = KBUILD_MODNAME,
411 .pm = &hid_sensor_pm_ops,
412 },
413 .probe = hid_incl_3d_probe,
414 .remove = hid_incl_3d_remove,
415 };
416 module_platform_driver(hid_incl_3d_platform_driver);
417
418 MODULE_DESCRIPTION("HID Sensor Inclinometer 3D");
419 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
420 MODULE_LICENSE("GPL");
421