blob: ae132a93bcae391954547132ba9d0f717e17e1ac [file] [log] [blame]
Thomas Gleixnerf6cc69f2019-05-29 16:57:24 -07001// SPDX-License-Identifier: GPL-2.0-only
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +00002/*
3 * HID Sensors Driver
4 * Copyright (c) 2013, Intel Corporation.
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +00005 */
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>
Srinivas Pandruvada6fe588e2014-04-19 00:22:00 +010013#include <linux/delay.h>
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +000014#include <linux/hid-sensor-hub.h>
15#include <linux/iio/iio.h>
16#include <linux/iio/sysfs.h>
17#include <linux/iio/buffer.h>
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +000018#include "../common/hid-sensors/hid-sensor-trigger.h"
19
20enum 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
27struct 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];
Srinivas Pandruvadabe8e48d2014-04-19 00:22:00 +010032 int scale_pre_decml;
33 int scale_post_decml;
34 int scale_precision;
35 int value_offset;
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +000036};
37
38static 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 */
45static 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 */
80static 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 */
91static 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;
Hans de Goede0145b502018-10-31 15:20:05 +0100100 s32 min;
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000101
102 *val = 0;
103 *val2 = 0;
104 switch (mask) {
105 case IIO_CHAN_INFO_RAW:
Srinivas Pandruvada6fe588e2014-04-19 00:22:00 +0100106 hid_sensor_power_state(&incl_state->common_attributes, true);
Hans de Goede0145b502018-10-31 15:20:05 +0100107 report_id = incl_state->incl[chan->scan_index].report_id;
108 min = incl_state->incl[chan->scan_index].logical_minimum;
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000109 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,
Srinivas Pandruvadab3f47372015-02-19 15:33:56 -0800114 report_id,
Hans de Goede0145b502018-10-31 15:20:05 +0100115 SENSOR_HUB_SYNC,
116 min < 0);
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000117 else {
Srinivas Pandruvada6fe588e2014-04-19 00:22:00 +0100118 hid_sensor_power_state(&incl_state->common_attributes,
119 false);
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000120 return -EINVAL;
121 }
Srinivas Pandruvada6fe588e2014-04-19 00:22:00 +0100122 hid_sensor_power_state(&incl_state->common_attributes, false);
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000123 ret_type = IIO_VAL_INT;
124 break;
125 case IIO_CHAN_INFO_SCALE:
Srinivas Pandruvadabe8e48d2014-04-19 00:22:00 +0100126 *val = incl_state->scale_pre_decml;
127 *val2 = incl_state->scale_post_decml;
128 ret_type = incl_state->scale_precision;
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000129 break;
130 case IIO_CHAN_INFO_OFFSET:
Srinivas Pandruvadabe8e48d2014-04-19 00:22:00 +0100131 *val = incl_state->value_offset;
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000132 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 */
151static 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
176static const struct iio_info incl_3d_info = {
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000177 .read_raw = &incl_3d_read_raw,
178 .write_raw = &incl_3d_write_raw,
179};
180
181/* Function to push data to buffer */
182static 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 */
189static 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
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +0100196 dev_dbg(&indio_dev->dev, "incl_3d_proc_event\n");
197 if (atomic_read(&incl_state->common_attributes.data_ready))
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000198 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 */
206static 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*/
234static 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
Srinivas Pandruvadabe8e48d2014-04-19 00:22:00 +0100278 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
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000283 /* 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 */
298static 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;
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000305
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
Fabio Estevam32ee56e2015-05-13 15:06:12 -0300325 indio_dev->channels = kmemdup(incl_3d_channels,
326 sizeof(incl_3d_channels), GFP_KERNEL);
327 if (!indio_dev->channels) {
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000328 dev_err(&pdev->dev, "failed to duplicate channels\n");
329 return -ENOMEM;
330 }
331
Fabio Estevam32ee56e2015-05-13 15:06:12 -0300332 ret = incl_3d_parse_report(pdev, hsdev,
333 (struct iio_chan_spec *)indio_dev->channels,
334 HID_USAGE_SENSOR_INCLINOMETER_3D,
335 incl_state);
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000336 if (ret) {
337 dev_err(&pdev->dev, "failed to setup attributes\n");
338 goto error_free_dev_mem;
339 }
340
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000341 indio_dev->num_channels = ARRAY_SIZE(incl_3d_channels);
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000342 indio_dev->info = &incl_3d_info;
343 indio_dev->name = name;
344 indio_dev->modes = INDIO_DIRECT_MODE;
345
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +0100346 atomic_set(&incl_state->common_attributes.data_ready, 0);
Alexandru Ardelean067fda12020-04-24 07:34:18 +0300347
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000348 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");
Alexandru Ardelean067fda12020-04-24 07:34:18 +0300352 goto error_free_dev_mem;
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000353 }
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
374error_iio_unreg:
375 iio_device_unregister(indio_dev);
376error_remove_trigger:
Alexandru Ardelean067fda12020-04-24 07:34:18 +0300377 hid_sensor_remove_trigger(indio_dev, &incl_state->common_attributes);
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000378error_free_dev_mem:
379 kfree(indio_dev->channels);
380 return ret;
381}
382
383/* Function to deinitialize the processing for usage id */
384static 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);
Alexandru Ardelean067fda12020-04-24 07:34:18 +0300392 hid_sensor_remove_trigger(indio_dev, &incl_state->common_attributes);
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000393 kfree(indio_dev->channels);
394
395 return 0;
396}
397
Krzysztof Kozlowski5dd86df2015-05-02 00:53:36 +0900398static const struct platform_device_id hid_incl_3d_ids[] = {
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000399 {
400 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
401 .name = "HID-SENSOR-200086",
402 },
403 { /* sentinel */ }
404};
405MODULE_DEVICE_TABLE(platform, hid_incl_3d_ids);
406
407static struct platform_driver hid_incl_3d_platform_driver = {
408 .id_table = hid_incl_3d_ids,
409 .driver = {
410 .name = KBUILD_MODNAME,
Srinivas Pandruvadafeb2b062015-01-07 10:55:59 -0800411 .pm = &hid_sensor_pm_ops,
Srinivas Pandruvada098d3be2013-11-06 00:11:00 +0000412 },
413 .probe = hid_incl_3d_probe,
414 .remove = hid_incl_3d_remove,
415};
416module_platform_driver(hid_incl_3d_platform_driver);
417
418MODULE_DESCRIPTION("HID Sensor Inclinometer 3D");
419MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
420MODULE_LICENSE("GPL");