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