hid-sensor-attributes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * HID Sensors Driver
  3. * Copyright (c) 2012, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  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/hid-sensor-hub.h>
  26. #include <linux/iio/iio.h>
  27. #include <linux/iio/sysfs.h>
  28. static struct {
  29. u32 usage_id;
  30. int unit; /* 0 for default others from HID sensor spec */
  31. int scale_val0; /* scale, whole number */
  32. int scale_val1; /* scale, fraction in nanos */
  33. } unit_conversion[] = {
  34. {HID_USAGE_SENSOR_ACCEL_3D, 0, 9, 806650000},
  35. {HID_USAGE_SENSOR_ACCEL_3D,
  36. HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
  37. {HID_USAGE_SENSOR_ACCEL_3D,
  38. HID_USAGE_SENSOR_UNITS_G, 9, 806650000},
  39. {HID_USAGE_SENSOR_GRAVITY_VECTOR, 0, 9, 806650000},
  40. {HID_USAGE_SENSOR_GRAVITY_VECTOR,
  41. HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0},
  42. {HID_USAGE_SENSOR_GRAVITY_VECTOR,
  43. HID_USAGE_SENSOR_UNITS_G, 9, 806650000},
  44. {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453293},
  45. {HID_USAGE_SENSOR_GYRO_3D,
  46. HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0},
  47. {HID_USAGE_SENSOR_GYRO_3D,
  48. HID_USAGE_SENSOR_UNITS_DEGREES_PER_SECOND, 0, 17453293},
  49. {HID_USAGE_SENSOR_COMPASS_3D, 0, 0, 1000000},
  50. {HID_USAGE_SENSOR_COMPASS_3D, HID_USAGE_SENSOR_UNITS_GAUSS, 1, 0},
  51. {HID_USAGE_SENSOR_INCLINOMETER_3D, 0, 0, 17453293},
  52. {HID_USAGE_SENSOR_INCLINOMETER_3D,
  53. HID_USAGE_SENSOR_UNITS_DEGREES, 0, 17453293},
  54. {HID_USAGE_SENSOR_INCLINOMETER_3D,
  55. HID_USAGE_SENSOR_UNITS_RADIANS, 1, 0},
  56. {HID_USAGE_SENSOR_ALS, 0, 1, 0},
  57. {HID_USAGE_SENSOR_ALS, HID_USAGE_SENSOR_UNITS_LUX, 1, 0},
  58. {HID_USAGE_SENSOR_PRESSURE, 0, 100, 0},
  59. {HID_USAGE_SENSOR_PRESSURE, HID_USAGE_SENSOR_UNITS_PASCAL, 0, 1000000},
  60. {HID_USAGE_SENSOR_TIME_TIMESTAMP, 0, 1000000000, 0},
  61. {HID_USAGE_SENSOR_TIME_TIMESTAMP, HID_USAGE_SENSOR_UNITS_MILLISECOND,
  62. 1000000, 0},
  63. {HID_USAGE_SENSOR_DEVICE_ORIENTATION, 0, 1, 0},
  64. {HID_USAGE_SENSOR_RELATIVE_ORIENTATION, 0, 1, 0},
  65. {HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION, 0, 1, 0},
  66. {HID_USAGE_SENSOR_TEMPERATURE, 0, 1000, 0},
  67. {HID_USAGE_SENSOR_TEMPERATURE, HID_USAGE_SENSOR_UNITS_DEGREES, 1000, 0},
  68. {HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0},
  69. };
  70. static int pow_10(unsigned power)
  71. {
  72. int i;
  73. int ret = 1;
  74. for (i = 0; i < power; ++i)
  75. ret = ret * 10;
  76. return ret;
  77. }
  78. static void simple_div(int dividend, int divisor, int *whole,
  79. int *micro_frac)
  80. {
  81. int rem;
  82. int exp = 0;
  83. *micro_frac = 0;
  84. if (divisor == 0) {
  85. *whole = 0;
  86. return;
  87. }
  88. *whole = dividend/divisor;
  89. rem = dividend % divisor;
  90. if (rem) {
  91. while (rem <= divisor) {
  92. rem *= 10;
  93. exp++;
  94. }
  95. *micro_frac = (rem / divisor) * pow_10(6-exp);
  96. }
  97. }
  98. static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
  99. {
  100. *val1 = no/pow_10(exp);
  101. *val2 = no%pow_10(exp) * pow_10(6-exp);
  102. }
  103. /*
  104. VTF format uses exponent and variable size format.
  105. For example if the size is 2 bytes
  106. 0x0067 with VTF16E14 format -> +1.03
  107. To convert just change to 0x67 to decimal and use two decimal as E14 stands
  108. for 10^-2.
  109. Negative numbers are 2's complement
  110. */
  111. static void convert_from_vtf_format(u32 value, int size, int exp,
  112. int *val1, int *val2)
  113. {
  114. int sign = 1;
  115. if (value & BIT(size*8 - 1)) {
  116. value = ((1LL << (size * 8)) - value);
  117. sign = -1;
  118. }
  119. exp = hid_sensor_convert_exponent(exp);
  120. if (exp >= 0) {
  121. *val1 = sign * value * pow_10(exp);
  122. *val2 = 0;
  123. } else {
  124. split_micro_fraction(value, -exp, val1, val2);
  125. if (*val1)
  126. *val1 = sign * (*val1);
  127. else
  128. *val2 = sign * (*val2);
  129. }
  130. }
  131. static u32 convert_to_vtf_format(int size, int exp, int val1, int val2)
  132. {
  133. u32 value;
  134. int sign = 1;
  135. if (val1 < 0 || val2 < 0)
  136. sign = -1;
  137. exp = hid_sensor_convert_exponent(exp);
  138. if (exp < 0) {
  139. value = abs(val1) * pow_10(-exp);
  140. value += abs(val2) / pow_10(6+exp);
  141. } else
  142. value = abs(val1) / pow_10(exp);
  143. if (sign < 0)
  144. value = ((1LL << (size * 8)) - value);
  145. return value;
  146. }
  147. s32 hid_sensor_read_poll_value(struct hid_sensor_common *st)
  148. {
  149. s32 value = 0;
  150. int ret;
  151. ret = sensor_hub_get_feature(st->hsdev,
  152. st->poll.report_id,
  153. st->poll.index, sizeof(value), &value);
  154. if (ret < 0 || value < 0) {
  155. return -EINVAL;
  156. } else {
  157. if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
  158. value = value * 1000;
  159. }
  160. return value;
  161. }
  162. EXPORT_SYMBOL(hid_sensor_read_poll_value);
  163. int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
  164. int *val1, int *val2)
  165. {
  166. s32 value;
  167. int ret;
  168. ret = sensor_hub_get_feature(st->hsdev,
  169. st->poll.report_id,
  170. st->poll.index, sizeof(value), &value);
  171. if (ret < 0 || value < 0) {
  172. *val1 = *val2 = 0;
  173. return -EINVAL;
  174. } else {
  175. if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
  176. simple_div(1000, value, val1, val2);
  177. else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
  178. simple_div(1, value, val1, val2);
  179. else {
  180. *val1 = *val2 = 0;
  181. return -EINVAL;
  182. }
  183. }
  184. return IIO_VAL_INT_PLUS_MICRO;
  185. }
  186. EXPORT_SYMBOL(hid_sensor_read_samp_freq_value);
  187. int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
  188. int val1, int val2)
  189. {
  190. s32 value;
  191. int ret;
  192. if (val1 < 0 || val2 < 0)
  193. return -EINVAL;
  194. value = val1 * pow_10(6) + val2;
  195. if (value) {
  196. if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
  197. value = pow_10(9)/value;
  198. else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
  199. value = pow_10(6)/value;
  200. else
  201. value = 0;
  202. }
  203. ret = sensor_hub_set_feature(st->hsdev, st->poll.report_id,
  204. st->poll.index, sizeof(value), &value);
  205. if (ret < 0 || value < 0)
  206. return -EINVAL;
  207. ret = sensor_hub_get_feature(st->hsdev,
  208. st->poll.report_id,
  209. st->poll.index, sizeof(value), &value);
  210. if (ret < 0 || value < 0)
  211. return -EINVAL;
  212. st->poll_interval = value;
  213. return 0;
  214. }
  215. EXPORT_SYMBOL(hid_sensor_write_samp_freq_value);
  216. int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st,
  217. int *val1, int *val2)
  218. {
  219. s32 value;
  220. int ret;
  221. ret = sensor_hub_get_feature(st->hsdev,
  222. st->sensitivity.report_id,
  223. st->sensitivity.index, sizeof(value),
  224. &value);
  225. if (ret < 0 || value < 0) {
  226. *val1 = *val2 = 0;
  227. return -EINVAL;
  228. } else {
  229. convert_from_vtf_format(value, st->sensitivity.size,
  230. st->sensitivity.unit_expo,
  231. val1, val2);
  232. }
  233. return IIO_VAL_INT_PLUS_MICRO;
  234. }
  235. EXPORT_SYMBOL(hid_sensor_read_raw_hyst_value);
  236. int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
  237. int val1, int val2)
  238. {
  239. s32 value;
  240. int ret;
  241. if (val1 < 0 || val2 < 0)
  242. return -EINVAL;
  243. value = convert_to_vtf_format(st->sensitivity.size,
  244. st->sensitivity.unit_expo,
  245. val1, val2);
  246. ret = sensor_hub_set_feature(st->hsdev, st->sensitivity.report_id,
  247. st->sensitivity.index, sizeof(value),
  248. &value);
  249. if (ret < 0 || value < 0)
  250. return -EINVAL;
  251. ret = sensor_hub_get_feature(st->hsdev,
  252. st->sensitivity.report_id,
  253. st->sensitivity.index, sizeof(value),
  254. &value);
  255. if (ret < 0 || value < 0)
  256. return -EINVAL;
  257. st->raw_hystersis = value;
  258. return 0;
  259. }
  260. EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
  261. /*
  262. * This fuction applies the unit exponent to the scale.
  263. * For example:
  264. * 9.806650000 ->exp:2-> val0[980]val1[665000000]
  265. * 9.000806000 ->exp:2-> val0[900]val1[80600000]
  266. * 0.174535293 ->exp:2-> val0[17]val1[453529300]
  267. * 1.001745329 ->exp:0-> val0[1]val1[1745329]
  268. * 1.001745329 ->exp:2-> val0[100]val1[174532900]
  269. * 1.001745329 ->exp:4-> val0[10017]val1[453290000]
  270. * 9.806650000 ->exp:-2-> val0[0]val1[98066500]
  271. */
  272. static void adjust_exponent_nano(int *val0, int *val1, int scale0,
  273. int scale1, int exp)
  274. {
  275. int i;
  276. int x;
  277. int res;
  278. int rem;
  279. if (exp > 0) {
  280. *val0 = scale0 * pow_10(exp);
  281. res = 0;
  282. if (exp > 9) {
  283. *val1 = 0;
  284. return;
  285. }
  286. for (i = 0; i < exp; ++i) {
  287. x = scale1 / pow_10(8 - i);
  288. res += (pow_10(exp - 1 - i) * x);
  289. scale1 = scale1 % pow_10(8 - i);
  290. }
  291. *val0 += res;
  292. *val1 = scale1 * pow_10(exp);
  293. } else if (exp < 0) {
  294. exp = abs(exp);
  295. if (exp > 9) {
  296. *val0 = *val1 = 0;
  297. return;
  298. }
  299. *val0 = scale0 / pow_10(exp);
  300. rem = scale0 % pow_10(exp);
  301. res = 0;
  302. for (i = 0; i < (9 - exp); ++i) {
  303. x = scale1 / pow_10(8 - i);
  304. res += (pow_10(8 - exp - i) * x);
  305. scale1 = scale1 % pow_10(8 - i);
  306. }
  307. *val1 = rem * pow_10(9 - exp) + res;
  308. } else {
  309. *val0 = scale0;
  310. *val1 = scale1;
  311. }
  312. }
  313. int hid_sensor_format_scale(u32 usage_id,
  314. struct hid_sensor_hub_attribute_info *attr_info,
  315. int *val0, int *val1)
  316. {
  317. int i;
  318. int exp;
  319. *val0 = 1;
  320. *val1 = 0;
  321. for (i = 0; i < ARRAY_SIZE(unit_conversion); ++i) {
  322. if (unit_conversion[i].usage_id == usage_id &&
  323. unit_conversion[i].unit == attr_info->units) {
  324. exp = hid_sensor_convert_exponent(
  325. attr_info->unit_expo);
  326. adjust_exponent_nano(val0, val1,
  327. unit_conversion[i].scale_val0,
  328. unit_conversion[i].scale_val1, exp);
  329. break;
  330. }
  331. }
  332. return IIO_VAL_INT_PLUS_NANO;
  333. }
  334. EXPORT_SYMBOL(hid_sensor_format_scale);
  335. int64_t hid_sensor_convert_timestamp(struct hid_sensor_common *st,
  336. int64_t raw_value)
  337. {
  338. return st->timestamp_ns_scale * raw_value;
  339. }
  340. EXPORT_SYMBOL(hid_sensor_convert_timestamp);
  341. static
  342. int hid_sensor_get_reporting_interval(struct hid_sensor_hub_device *hsdev,
  343. u32 usage_id,
  344. struct hid_sensor_common *st)
  345. {
  346. sensor_hub_input_get_attribute_info(hsdev,
  347. HID_FEATURE_REPORT, usage_id,
  348. HID_USAGE_SENSOR_PROP_REPORT_INTERVAL,
  349. &st->poll);
  350. /* Default unit of measure is milliseconds */
  351. if (st->poll.units == 0)
  352. st->poll.units = HID_USAGE_SENSOR_UNITS_MILLISECOND;
  353. st->poll_interval = -1;
  354. return 0;
  355. }
  356. static void hid_sensor_get_report_latency_info(struct hid_sensor_hub_device *hsdev,
  357. u32 usage_id,
  358. struct hid_sensor_common *st)
  359. {
  360. sensor_hub_input_get_attribute_info(hsdev, HID_FEATURE_REPORT,
  361. usage_id,
  362. HID_USAGE_SENSOR_PROP_REPORT_LATENCY,
  363. &st->report_latency);
  364. hid_dbg(hsdev->hdev, "Report latency attributes: %x:%x\n",
  365. st->report_latency.index, st->report_latency.report_id);
  366. }
  367. int hid_sensor_get_report_latency(struct hid_sensor_common *st)
  368. {
  369. int ret;
  370. int value;
  371. ret = sensor_hub_get_feature(st->hsdev, st->report_latency.report_id,
  372. st->report_latency.index, sizeof(value),
  373. &value);
  374. if (ret < 0)
  375. return ret;
  376. return value;
  377. }
  378. EXPORT_SYMBOL(hid_sensor_get_report_latency);
  379. int hid_sensor_set_report_latency(struct hid_sensor_common *st, int latency_ms)
  380. {
  381. return sensor_hub_set_feature(st->hsdev, st->report_latency.report_id,
  382. st->report_latency.index,
  383. sizeof(latency_ms), &latency_ms);
  384. }
  385. EXPORT_SYMBOL(hid_sensor_set_report_latency);
  386. bool hid_sensor_batch_mode_supported(struct hid_sensor_common *st)
  387. {
  388. return st->report_latency.index > 0 && st->report_latency.report_id > 0;
  389. }
  390. EXPORT_SYMBOL(hid_sensor_batch_mode_supported);
  391. int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
  392. u32 usage_id,
  393. struct hid_sensor_common *st)
  394. {
  395. struct hid_sensor_hub_attribute_info timestamp;
  396. s32 value;
  397. int ret;
  398. hid_sensor_get_reporting_interval(hsdev, usage_id, st);
  399. sensor_hub_input_get_attribute_info(hsdev,
  400. HID_FEATURE_REPORT, usage_id,
  401. HID_USAGE_SENSOR_PROP_REPORT_STATE,
  402. &st->report_state);
  403. sensor_hub_input_get_attribute_info(hsdev,
  404. HID_FEATURE_REPORT, usage_id,
  405. HID_USAGE_SENSOR_PROY_POWER_STATE,
  406. &st->power_state);
  407. st->power_state.logical_minimum = 1;
  408. st->report_state.logical_minimum = 1;
  409. sensor_hub_input_get_attribute_info(hsdev,
  410. HID_FEATURE_REPORT, usage_id,
  411. HID_USAGE_SENSOR_PROP_SENSITIVITY_ABS,
  412. &st->sensitivity);
  413. st->raw_hystersis = -1;
  414. sensor_hub_input_get_attribute_info(hsdev,
  415. HID_INPUT_REPORT, usage_id,
  416. HID_USAGE_SENSOR_TIME_TIMESTAMP,
  417. &timestamp);
  418. if (timestamp.index >= 0 && timestamp.report_id) {
  419. int val0, val1;
  420. hid_sensor_format_scale(HID_USAGE_SENSOR_TIME_TIMESTAMP,
  421. &timestamp, &val0, &val1);
  422. st->timestamp_ns_scale = val0;
  423. } else
  424. st->timestamp_ns_scale = 1000000000;
  425. hid_sensor_get_report_latency_info(hsdev, usage_id, st);
  426. hid_dbg(hsdev->hdev, "common attributes: %x:%x, %x:%x, %x:%x %x:%x %x:%x\n",
  427. st->poll.index, st->poll.report_id,
  428. st->report_state.index, st->report_state.report_id,
  429. st->power_state.index, st->power_state.report_id,
  430. st->sensitivity.index, st->sensitivity.report_id,
  431. timestamp.index, timestamp.report_id);
  432. ret = sensor_hub_get_feature(hsdev,
  433. st->power_state.report_id,
  434. st->power_state.index, sizeof(value), &value);
  435. if (ret < 0)
  436. return ret;
  437. if (value < 0)
  438. return -EINVAL;
  439. return 0;
  440. }
  441. EXPORT_SYMBOL(hid_sensor_parse_common_attributes);
  442. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  443. MODULE_DESCRIPTION("HID Sensor common attribute processing");
  444. MODULE_LICENSE("GPL");