1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2012, Intel Corporation.
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/time.h>
9 #include <linux/units.h>
11 #include <linux/hid-sensor-hub.h>
12 #include <linux/iio/iio.h>
16 int unit; /* 0 for default others from HID sensor spec */
17 int scale_val0; /* scale, whole number */
18 int scale_val1; /* scale, fraction in nanos */
19 } unit_conversion[] = {
20 {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650000},
21 {HID_USAGE_SENSOR_ACCEL_3D,
22 HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
23 {HID_USAGE_SENSOR_ACCEL_3D,
24 HID_USAGE_SENSOR_UNITS_G, 9, 806650000},
26 {HID_USAGE_SENSOR_GRAVITY_VECTOR, 0, 9, 806650000},
27 {HID_USAGE_SENSOR_GRAVITY_VECTOR,
28 HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
29 {HID_USAGE_SENSOR_GRAVITY_VECTOR,
30 HID_USAGE_SENSOR_UNITS_G, 9, 806650000},
32 {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453293},
33 {HID_USAGE_SENSOR_GYRO_3D,
34 HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
35 {HID_USAGE_SENSOR_GYRO_3D,
36 HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453293},
38 {HID_USAGE_SENSOR_COMPASS_3D, 0, 0, 1000000},
39 {HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
41 {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 0, 17453293},
42 {HID_USAGE_SENSOR_INCLINOMETER_3D,
43 HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453293},
44 {HID_USAGE_SENSOR_INCLINOMETER_3D,
45 HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
47 {HID_USAGE_SENSOR_ALS, 0, 1, 0},
48 {HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
50 {HID_USAGE_SENSOR_PRESSURE, 0, 100, 0},
51 {HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 0, 1000000},
53 {HID_USAGE_SENSOR_TIME_TIMESTAMP, 0, 1000000000, 0},
54 {HID_USAGE_SENSOR_TIME_TIMESTAMP, HID_USAGE_SENSOR_UNITS_MILLISECOND,
57 {HID_USAGE_SENSOR_DEVICE_ORIENTATION, 0, 1, 0},
59 {HID_USAGE_SENSOR_RELATIVE_ORIENTATION, 0, 1, 0},
61 {HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION, 0, 1, 0},
63 {HID_USAGE_SENSOR_TEMPERATURE, 0, 1000, 0},
64 {HID_USAGE_SENSOR_TEMPERATURE, HID_USAGE_SENSOR_UNITS_DEGREES, 1000, 0},
66 {HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0},
67 {HID_USAGE_SENSOR_HINGE, 0, 0, 17453293},
68 {HID_USAGE_SENSOR_HINGE, HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453293},
71 static void simple_div(int dividend, int divisor, int *whole,
82 *whole = dividend/divisor;
83 rem = dividend % divisor;
85 while (rem <= divisor) {
89 *micro_frac = (rem / divisor) * int_pow(10, 6 - exp);
93 static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
95 int divisor = int_pow(10, exp);
98 *val2 = no % divisor * int_pow(10, 6 - exp);
102 VTF format uses exponent and variable size format.
103 For example if the size is 2 bytes
104 0x0067 with VTF16E14 format -> +1.03
105 To convert just change to 0x67 to decimal and use two decimal as E14 stands
107 Negative numbers are 2's complement
109 static void convert_from_vtf_format(u32 value, int size, int exp,
110 int *val1, int *val2)
114 if (value & BIT(size*8 - 1)) {
115 value = ((1LL << (size * 8)) - value);
118 exp = hid_sensor_convert_exponent(exp);
120 *val1 = sign * value * int_pow(10, exp);
123 split_micro_fraction(value, -exp, val1, val2);
125 *val1 = sign * (*val1);
127 *val2 = sign * (*val2);
131 static u32 convert_to_vtf_format(int size, int exp, int val1, int val2)
137 if (val1 < 0 || val2 < 0)
139 exp = hid_sensor_convert_exponent(exp);
141 divisor = int_pow(10, 6 + exp);
142 value = abs(val1) * int_pow(10, -exp);
143 value += abs(val2) / divisor;
145 divisor = int_pow(10, exp);
146 value = abs(val1) / divisor;
149 value = ((1LL << (size * 8)) - value);
154 s32 hid_sensor_read_poll_value(struct hid_sensor_common *st)
159 ret = sensor_hub_get_feature(st->hsdev,
161 st->poll.index, sizeof(value), &value);
163 if (ret < 0 || value < 0) {
166 if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
167 value = value * 1000;
172 EXPORT_SYMBOL_NS(hid_sensor_read_poll_value, IIO_HID_ATTRIBUTES);
174 int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
175 int *val1, int *val2)
180 ret = sensor_hub_get_feature(st->hsdev,
182 st->poll.index, sizeof(value), &value);
183 if (ret < 0 || value < 0) {
187 if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
188 simple_div(1000, value, val1, val2);
189 else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
190 simple_div(1, value, val1, val2);
197 return IIO_VAL_INT_PLUS_MICRO;
199 EXPORT_SYMBOL_NS(hid_sensor_read_samp_freq_value, IIO_HID);
201 int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
207 if (val1 < 0 || val2 < 0)
210 value = val1 * HZ_PER_MHZ + val2;
212 if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
213 value = NSEC_PER_SEC / value;
214 else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
215 value = USEC_PER_SEC / value;
219 ret = sensor_hub_set_feature(st->hsdev, st->poll.report_id,
220 st->poll.index, sizeof(value), &value);
221 if (ret < 0 || value < 0)
224 ret = sensor_hub_get_feature(st->hsdev,
226 st->poll.index, sizeof(value), &value);
227 if (ret < 0 || value < 0)
230 st->poll_interval = value;
234 EXPORT_SYMBOL_NS(hid_sensor_write_samp_freq_value, IIO_HID);
236 int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st,
237 int *val1, int *val2)
242 ret = sensor_hub_get_feature(st->hsdev,
243 st->sensitivity.report_id,
244 st->sensitivity.index, sizeof(value),
246 if (ret < 0 || value < 0) {
250 convert_from_vtf_format(value, st->sensitivity.size,
251 st->sensitivity.unit_expo,
255 return IIO_VAL_INT_PLUS_MICRO;
257 EXPORT_SYMBOL_NS(hid_sensor_read_raw_hyst_value, IIO_HID);
259 int hid_sensor_read_raw_hyst_rel_value(struct hid_sensor_common *st, int *val1,
265 ret = sensor_hub_get_feature(st->hsdev,
266 st->sensitivity_rel.report_id,
267 st->sensitivity_rel.index, sizeof(value),
269 if (ret < 0 || value < 0) {
274 convert_from_vtf_format(value, st->sensitivity_rel.size,
275 st->sensitivity_rel.unit_expo, val1, val2);
277 return IIO_VAL_INT_PLUS_MICRO;
279 EXPORT_SYMBOL_NS(hid_sensor_read_raw_hyst_rel_value, IIO_HID);
282 int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
288 if (val1 < 0 || val2 < 0)
291 value = convert_to_vtf_format(st->sensitivity.size,
292 st->sensitivity.unit_expo,
294 ret = sensor_hub_set_feature(st->hsdev, st->sensitivity.report_id,
295 st->sensitivity.index, sizeof(value),
297 if (ret < 0 || value < 0)
300 ret = sensor_hub_get_feature(st->hsdev,
301 st->sensitivity.report_id,
302 st->sensitivity.index, sizeof(value),
304 if (ret < 0 || value < 0)
307 st->raw_hystersis = value;
311 EXPORT_SYMBOL_NS(hid_sensor_write_raw_hyst_value, IIO_HID);
313 int hid_sensor_write_raw_hyst_rel_value(struct hid_sensor_common *st,
319 if (val1 < 0 || val2 < 0)
322 value = convert_to_vtf_format(st->sensitivity_rel.size,
323 st->sensitivity_rel.unit_expo,
325 ret = sensor_hub_set_feature(st->hsdev, st->sensitivity_rel.report_id,
326 st->sensitivity_rel.index, sizeof(value),
328 if (ret < 0 || value < 0)
331 ret = sensor_hub_get_feature(st->hsdev,
332 st->sensitivity_rel.report_id,
333 st->sensitivity_rel.index, sizeof(value),
335 if (ret < 0 || value < 0)
338 st->raw_hystersis = value;
342 EXPORT_SYMBOL_NS(hid_sensor_write_raw_hyst_rel_value, IIO_HID);
345 * This fuction applies the unit exponent to the scale.
347 * 9.806650000 ->exp:2-> val0[980]val1[665000000]
348 * 9.000806000 ->exp:2-> val0[900]val1[80600000]
349 * 0.174535293 ->exp:2-> val0[17]val1[453529300]
350 * 1.001745329 ->exp:0-> val0[1]val1[1745329]
351 * 1.001745329 ->exp:2-> val0[100]val1[174532900]
352 * 1.001745329 ->exp:4-> val0[10017]val1[453290000]
353 * 9.806650000 ->exp:-2-> val0[0]val1[98066500]
355 static void adjust_exponent_nano(int *val0, int *val1, int scale0,
365 *val0 = scale0 * int_pow(10, exp);
371 for (i = 0; i < exp; ++i) {
372 divisor = int_pow(10, 8 - i);
373 x = scale1 / divisor;
374 res += int_pow(10, exp - 1 - i) * x;
375 scale1 = scale1 % divisor;
378 *val1 = scale1 * int_pow(10, exp);
379 } else if (exp < 0) {
385 divisor = int_pow(10, exp);
386 *val0 = scale0 / divisor;
387 rem = scale0 % divisor;
389 for (i = 0; i < (9 - exp); ++i) {
390 divisor = int_pow(10, 8 - i);
391 x = scale1 / divisor;
392 res += int_pow(10, 8 - exp - i) * x;
393 scale1 = scale1 % divisor;
395 *val1 = rem * int_pow(10, 9 - exp) + res;
402 int hid_sensor_format_scale(u32 usage_id,
403 struct hid_sensor_hub_attribute_info *attr_info,
404 int *val0, int *val1)
412 for (i = 0; i < ARRAY_SIZE(unit_conversion); ++i) {
413 if (unit_conversion[i].usage_id == usage_id &&
414 unit_conversion[i].unit == attr_info->units) {
415 exp = hid_sensor_convert_exponent(
416 attr_info->unit_expo);
417 adjust_exponent_nano(val0, val1,
418 unit_conversion[i].scale_val0,
419 unit_conversion[i].scale_val1, exp);
424 return IIO_VAL_INT_PLUS_NANO;
426 EXPORT_SYMBOL_NS(hid_sensor_format_scale, IIO_HID);
428 int64_t hid_sensor_convert_timestamp(struct hid_sensor_common *st,
431 return st->timestamp_ns_scale * raw_value;
433 EXPORT_SYMBOL_NS(hid_sensor_convert_timestamp, IIO_HID);
436 int hid_sensor_get_reporting_interval(struct hid_sensor_hub_device *hsdev,
438 struct hid_sensor_common *st)
440 sensor_hub_input_get_attribute_info(hsdev,
441 HID_FEATURE_REPORT, usage_id,
442 HID_USAGE_SENSOR_PROP_REPORT_INTERVAL,
444 /* Default unit of measure is milliseconds */
445 if (st->poll.units == 0)
446 st->poll.units = HID_USAGE_SENSOR_UNITS_MILLISECOND;
448 st->poll_interval = -1;
454 static void hid_sensor_get_report_latency_info(struct hid_sensor_hub_device *hsdev,
456 struct hid_sensor_common *st)
458 sensor_hub_input_get_attribute_info(hsdev, HID_FEATURE_REPORT,
460 HID_USAGE_SENSOR_PROP_REPORT_LATENCY,
461 &st->report_latency);
463 hid_dbg(hsdev->hdev, "Report latency attributes: %x:%x\n",
464 st->report_latency.index, st->report_latency.report_id);
467 int hid_sensor_get_report_latency(struct hid_sensor_common *st)
472 ret = sensor_hub_get_feature(st->hsdev, st->report_latency.report_id,
473 st->report_latency.index, sizeof(value),
480 EXPORT_SYMBOL_NS(hid_sensor_get_report_latency, IIO_HID_ATTRIBUTES);
482 int hid_sensor_set_report_latency(struct hid_sensor_common *st, int latency_ms)
484 return sensor_hub_set_feature(st->hsdev, st->report_latency.report_id,
485 st->report_latency.index,
486 sizeof(latency_ms), &latency_ms);
488 EXPORT_SYMBOL_NS(hid_sensor_set_report_latency, IIO_HID_ATTRIBUTES);
490 bool hid_sensor_batch_mode_supported(struct hid_sensor_common *st)
492 return st->report_latency.index > 0 && st->report_latency.report_id > 0;
494 EXPORT_SYMBOL_NS(hid_sensor_batch_mode_supported, IIO_HID_ATTRIBUTES);
496 int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
498 struct hid_sensor_common *st,
499 const u32 *sensitivity_addresses,
500 u32 sensitivity_addresses_len)
503 struct hid_sensor_hub_attribute_info timestamp;
508 hid_sensor_get_reporting_interval(hsdev, usage_id, st);
510 sensor_hub_input_get_attribute_info(hsdev,
511 HID_FEATURE_REPORT, usage_id,
512 HID_USAGE_SENSOR_PROP_REPORT_STATE,
515 sensor_hub_input_get_attribute_info(hsdev,
516 HID_FEATURE_REPORT, usage_id,
517 HID_USAGE_SENSOR_PROY_POWER_STATE,
520 st->power_state.logical_minimum = 1;
521 st->report_state.logical_minimum = 1;
523 sensor_hub_input_get_attribute_info(hsdev,
524 HID_FEATURE_REPORT, usage_id,
525 HID_USAGE_SENSOR_PROP_SENSITIVITY_ABS,
528 sensor_hub_input_get_attribute_info(hsdev,
529 HID_FEATURE_REPORT, usage_id,
530 HID_USAGE_SENSOR_PROP_SENSITIVITY_REL_PCT,
531 &st->sensitivity_rel);
533 * Set Sensitivity field ids, when there is no individual modifier, will
534 * check absolute sensitivity and relative sensitivity of data field
536 for (i = 0; i < sensitivity_addresses_len; i++) {
537 if (st->sensitivity.index < 0)
538 sensor_hub_input_get_attribute_info(
539 hsdev, HID_FEATURE_REPORT, usage_id,
540 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
541 sensitivity_addresses[i],
544 if (st->sensitivity_rel.index < 0)
545 sensor_hub_input_get_attribute_info(
546 hsdev, HID_FEATURE_REPORT, usage_id,
547 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_REL_PCT |
548 sensitivity_addresses[i],
549 &st->sensitivity_rel);
552 st->raw_hystersis = -1;
554 sensor_hub_input_get_attribute_info(hsdev,
555 HID_INPUT_REPORT, usage_id,
556 HID_USAGE_SENSOR_TIME_TIMESTAMP,
558 if (timestamp.index >= 0 && timestamp.report_id) {
561 hid_sensor_format_scale(HID_USAGE_SENSOR_TIME_TIMESTAMP,
562 ×tamp, &val0, &val1);
563 st->timestamp_ns_scale = val0;
565 st->timestamp_ns_scale = 1000000000;
567 hid_sensor_get_report_latency_info(hsdev, usage_id, st);
569 hid_dbg(hsdev->hdev, "common attributes: %x:%x, %x:%x, %x:%x %x:%x %x:%x\n",
570 st->poll.index, st->poll.report_id,
571 st->report_state.index, st->report_state.report_id,
572 st->power_state.index, st->power_state.report_id,
573 st->sensitivity.index, st->sensitivity.report_id,
574 timestamp.index, timestamp.report_id);
576 ret = sensor_hub_get_feature(hsdev,
577 st->power_state.report_id,
578 st->power_state.index, sizeof(value), &value);
586 EXPORT_SYMBOL_NS(hid_sensor_parse_common_attributes, IIO_HID);
588 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
589 MODULE_DESCRIPTION("HID Sensor common attribute processing");
590 MODULE_LICENSE("GPL");