1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2012, Intel Corporation.
6 #include <linux/device.h>
7 #include <linux/platform_device.h>
8 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/slab.h>
12 #include <linux/hid-sensor-hub.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/sysfs.h>
18 int unit; /* 0 for default others from HID sensor spec */
19 int scale_val0; /* scale, whole number */
20 int scale_val1; /* scale, fraction in nanos */
21 } unit_conversion[] = {
22 {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650000},
23 {HID_USAGE_SENSOR_ACCEL_3D,
24 HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
25 {HID_USAGE_SENSOR_ACCEL_3D,
26 HID_USAGE_SENSOR_UNITS_G, 9, 806650000},
28 {HID_USAGE_SENSOR_GRAVITY_VECTOR, 0, 9, 806650000},
29 {HID_USAGE_SENSOR_GRAVITY_VECTOR,
30 HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
31 {HID_USAGE_SENSOR_GRAVITY_VECTOR,
32 HID_USAGE_SENSOR_UNITS_G, 9, 806650000},
34 {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453293},
35 {HID_USAGE_SENSOR_GYRO_3D,
36 HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
37 {HID_USAGE_SENSOR_GYRO_3D,
38 HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453293},
40 {HID_USAGE_SENSOR_COMPASS_3D, 0, 0, 1000000},
41 {HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
43 {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 0, 17453293},
44 {HID_USAGE_SENSOR_INCLINOMETER_3D,
45 HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453293},
46 {HID_USAGE_SENSOR_INCLINOMETER_3D,
47 HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
49 {HID_USAGE_SENSOR_ALS, 0, 1, 0},
50 {HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
52 {HID_USAGE_SENSOR_PRESSURE, 0, 100, 0},
53 {HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 0, 1000000},
55 {HID_USAGE_SENSOR_TIME_TIMESTAMP, 0, 1000000000, 0},
56 {HID_USAGE_SENSOR_TIME_TIMESTAMP, HID_USAGE_SENSOR_UNITS_MILLISECOND,
59 {HID_USAGE_SENSOR_DEVICE_ORIENTATION, 0, 1, 0},
61 {HID_USAGE_SENSOR_RELATIVE_ORIENTATION, 0, 1, 0},
63 {HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION, 0, 1, 0},
65 {HID_USAGE_SENSOR_TEMPERATURE, 0, 1000, 0},
66 {HID_USAGE_SENSOR_TEMPERATURE, HID_USAGE_SENSOR_UNITS_DEGREES, 1000, 0},
68 {HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0},
71 static int pow_10(unsigned power)
75 for (i = 0; i < power; ++i)
81 static void simple_div(int dividend, int divisor, int *whole,
92 *whole = dividend/divisor;
93 rem = dividend % divisor;
95 while (rem <= divisor) {
99 *micro_frac = (rem / divisor) * pow_10(6-exp);
103 static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
105 *val1 = no/pow_10(exp);
106 *val2 = no%pow_10(exp) * pow_10(6-exp);
110 VTF format uses exponent and variable size format.
111 For example if the size is 2 bytes
112 0x0067 with VTF16E14 format -> +1.03
113 To convert just change to 0x67 to decimal and use two decimal as E14 stands
115 Negative numbers are 2's complement
117 static void convert_from_vtf_format(u32 value, int size, int exp,
118 int *val1, int *val2)
122 if (value & BIT(size*8 - 1)) {
123 value = ((1LL << (size * 8)) - value);
126 exp = hid_sensor_convert_exponent(exp);
128 *val1 = sign * value * pow_10(exp);
131 split_micro_fraction(value, -exp, val1, val2);
133 *val1 = sign * (*val1);
135 *val2 = sign * (*val2);
139 static u32 convert_to_vtf_format(int size, int exp, int val1, int val2)
144 if (val1 < 0 || val2 < 0)
146 exp = hid_sensor_convert_exponent(exp);
148 value = abs(val1) * pow_10(-exp);
149 value += abs(val2) / pow_10(6+exp);
151 value = abs(val1) / pow_10(exp);
153 value = ((1LL << (size * 8)) - value);
158 s32 hid_sensor_read_poll_value(struct hid_sensor_common *st)
163 ret = sensor_hub_get_feature(st->hsdev,
165 st->poll.index, sizeof(value), &value);
167 if (ret < 0 || value < 0) {
170 if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
171 value = value * 1000;
176 EXPORT_SYMBOL(hid_sensor_read_poll_value);
178 int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
179 int *val1, int *val2)
184 ret = sensor_hub_get_feature(st->hsdev,
186 st->poll.index, sizeof(value), &value);
187 if (ret < 0 || value < 0) {
191 if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
192 simple_div(1000, value, val1, val2);
193 else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
194 simple_div(1, value, val1, val2);
201 return IIO_VAL_INT_PLUS_MICRO;
203 EXPORT_SYMBOL(hid_sensor_read_samp_freq_value);
205 int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
211 if (val1 < 0 || val2 < 0)
214 value = val1 * pow_10(6) + val2;
216 if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
217 value = pow_10(9)/value;
218 else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
219 value = pow_10(6)/value;
223 ret = sensor_hub_set_feature(st->hsdev, st->poll.report_id,
224 st->poll.index, sizeof(value), &value);
225 if (ret < 0 || value < 0)
228 ret = sensor_hub_get_feature(st->hsdev,
230 st->poll.index, sizeof(value), &value);
231 if (ret < 0 || value < 0)
234 st->poll_interval = value;
238 EXPORT_SYMBOL(hid_sensor_write_samp_freq_value);
240 int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st,
241 int *val1, int *val2)
246 ret = sensor_hub_get_feature(st->hsdev,
247 st->sensitivity.report_id,
248 st->sensitivity.index, sizeof(value),
250 if (ret < 0 || value < 0) {
254 convert_from_vtf_format(value, st->sensitivity.size,
255 st->sensitivity.unit_expo,
259 return IIO_VAL_INT_PLUS_MICRO;
261 EXPORT_SYMBOL(hid_sensor_read_raw_hyst_value);
263 int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
269 if (val1 < 0 || val2 < 0)
272 value = convert_to_vtf_format(st->sensitivity.size,
273 st->sensitivity.unit_expo,
275 ret = sensor_hub_set_feature(st->hsdev, st->sensitivity.report_id,
276 st->sensitivity.index, sizeof(value),
278 if (ret < 0 || value < 0)
281 ret = sensor_hub_get_feature(st->hsdev,
282 st->sensitivity.report_id,
283 st->sensitivity.index, sizeof(value),
285 if (ret < 0 || value < 0)
288 st->raw_hystersis = value;
292 EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
295 * This fuction applies the unit exponent to the scale.
297 * 9.806650000 ->exp:2-> val0[980]val1[665000000]
298 * 9.000806000 ->exp:2-> val0[900]val1[80600000]
299 * 0.174535293 ->exp:2-> val0[17]val1[453529300]
300 * 1.001745329 ->exp:0-> val0[1]val1[1745329]
301 * 1.001745329 ->exp:2-> val0[100]val1[174532900]
302 * 1.001745329 ->exp:4-> val0[10017]val1[453290000]
303 * 9.806650000 ->exp:-2-> val0[0]val1[98066500]
305 static void adjust_exponent_nano(int *val0, int *val1, int scale0,
314 *val0 = scale0 * pow_10(exp);
320 for (i = 0; i < exp; ++i) {
321 x = scale1 / pow_10(8 - i);
322 res += (pow_10(exp - 1 - i) * x);
323 scale1 = scale1 % pow_10(8 - i);
326 *val1 = scale1 * pow_10(exp);
327 } else if (exp < 0) {
333 *val0 = scale0 / pow_10(exp);
334 rem = scale0 % pow_10(exp);
336 for (i = 0; i < (9 - exp); ++i) {
337 x = scale1 / pow_10(8 - i);
338 res += (pow_10(8 - exp - i) * x);
339 scale1 = scale1 % pow_10(8 - i);
341 *val1 = rem * pow_10(9 - exp) + res;
348 int hid_sensor_format_scale(u32 usage_id,
349 struct hid_sensor_hub_attribute_info *attr_info,
350 int *val0, int *val1)
358 for (i = 0; i < ARRAY_SIZE(unit_conversion); ++i) {
359 if (unit_conversion[i].usage_id == usage_id &&
360 unit_conversion[i].unit == attr_info->units) {
361 exp = hid_sensor_convert_exponent(
362 attr_info->unit_expo);
363 adjust_exponent_nano(val0, val1,
364 unit_conversion[i].scale_val0,
365 unit_conversion[i].scale_val1, exp);
370 return IIO_VAL_INT_PLUS_NANO;
372 EXPORT_SYMBOL(hid_sensor_format_scale);
374 int64_t hid_sensor_convert_timestamp(struct hid_sensor_common *st,
377 return st->timestamp_ns_scale * raw_value;
379 EXPORT_SYMBOL(hid_sensor_convert_timestamp);
382 int hid_sensor_get_reporting_interval(struct hid_sensor_hub_device *hsdev,
384 struct hid_sensor_common *st)
386 sensor_hub_input_get_attribute_info(hsdev,
387 HID_FEATURE_REPORT, usage_id,
388 HID_USAGE_SENSOR_PROP_REPORT_INTERVAL,
390 /* Default unit of measure is milliseconds */
391 if (st->poll.units == 0)
392 st->poll.units = HID_USAGE_SENSOR_UNITS_MILLISECOND;
394 st->poll_interval = -1;
400 static void hid_sensor_get_report_latency_info(struct hid_sensor_hub_device *hsdev,
402 struct hid_sensor_common *st)
404 sensor_hub_input_get_attribute_info(hsdev, HID_FEATURE_REPORT,
406 HID_USAGE_SENSOR_PROP_REPORT_LATENCY,
407 &st->report_latency);
409 hid_dbg(hsdev->hdev, "Report latency attributes: %x:%x\n",
410 st->report_latency.index, st->report_latency.report_id);
413 int hid_sensor_get_report_latency(struct hid_sensor_common *st)
418 ret = sensor_hub_get_feature(st->hsdev, st->report_latency.report_id,
419 st->report_latency.index, sizeof(value),
426 EXPORT_SYMBOL(hid_sensor_get_report_latency);
428 int hid_sensor_set_report_latency(struct hid_sensor_common *st, int latency_ms)
430 return sensor_hub_set_feature(st->hsdev, st->report_latency.report_id,
431 st->report_latency.index,
432 sizeof(latency_ms), &latency_ms);
434 EXPORT_SYMBOL(hid_sensor_set_report_latency);
436 bool hid_sensor_batch_mode_supported(struct hid_sensor_common *st)
438 return st->report_latency.index > 0 && st->report_latency.report_id > 0;
440 EXPORT_SYMBOL(hid_sensor_batch_mode_supported);
442 int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
444 struct hid_sensor_common *st)
447 struct hid_sensor_hub_attribute_info timestamp;
451 hid_sensor_get_reporting_interval(hsdev, usage_id, st);
453 sensor_hub_input_get_attribute_info(hsdev,
454 HID_FEATURE_REPORT, usage_id,
455 HID_USAGE_SENSOR_PROP_REPORT_STATE,
458 sensor_hub_input_get_attribute_info(hsdev,
459 HID_FEATURE_REPORT, usage_id,
460 HID_USAGE_SENSOR_PROY_POWER_STATE,
463 st->power_state.logical_minimum = 1;
464 st->report_state.logical_minimum = 1;
466 sensor_hub_input_get_attribute_info(hsdev,
467 HID_FEATURE_REPORT, usage_id,
468 HID_USAGE_SENSOR_PROP_SENSITIVITY_ABS,
471 st->raw_hystersis = -1;
473 sensor_hub_input_get_attribute_info(hsdev,
474 HID_INPUT_REPORT, usage_id,
475 HID_USAGE_SENSOR_TIME_TIMESTAMP,
477 if (timestamp.index >= 0 && timestamp.report_id) {
480 hid_sensor_format_scale(HID_USAGE_SENSOR_TIME_TIMESTAMP,
481 ×tamp, &val0, &val1);
482 st->timestamp_ns_scale = val0;
484 st->timestamp_ns_scale = 1000000000;
486 hid_sensor_get_report_latency_info(hsdev, usage_id, st);
488 hid_dbg(hsdev->hdev, "common attributes: %x:%x, %x:%x, %x:%x %x:%x %x:%x\n",
489 st->poll.index, st->poll.report_id,
490 st->report_state.index, st->report_state.report_id,
491 st->power_state.index, st->power_state.report_id,
492 st->sensitivity.index, st->sensitivity.report_id,
493 timestamp.index, timestamp.report_id);
495 ret = sensor_hub_get_feature(hsdev,
496 st->power_state.report_id,
497 st->power_state.index, sizeof(value), &value);
505 EXPORT_SYMBOL(hid_sensor_parse_common_attributes);
507 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
508 MODULE_DESCRIPTION("HID Sensor common attribute processing");
509 MODULE_LICENSE("GPL");