hid-sensor-attributes.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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_TEMPERATURE, 0, 1000, 0},
  64. {HID_USAGE_SENSOR_TEMPERATURE, HID_USAGE_SENSOR_UNITS_DEGREES, 1000, 0},
  65. {HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0},
  66. };
  67. static int pow_10(unsigned power)
  68. {
  69. int i;
  70. int ret = 1;
  71. for (i = 0; i < power; ++i)
  72. ret = ret * 10;
  73. return ret;
  74. }
  75. static void simple_div(int dividend, int divisor, int *whole,
  76. int *micro_frac)
  77. {
  78. int rem;
  79. int exp = 0;
  80. *micro_frac = 0;
  81. if (divisor == 0) {
  82. *whole = 0;
  83. return;
  84. }
  85. *whole = dividend/divisor;
  86. rem = dividend % divisor;
  87. if (rem) {
  88. while (rem <= divisor) {
  89. rem *= 10;
  90. exp++;
  91. }
  92. *micro_frac = (rem / divisor) * pow_10(6-exp);
  93. }
  94. }
  95. static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
  96. {
  97. *val1 = no/pow_10(exp);
  98. *val2 = no%pow_10(exp) * pow_10(6-exp);
  99. }
  100. /*
  101. VTF format uses exponent and variable size format.
  102. For example if the size is 2 bytes
  103. 0x0067 with VTF16E14 format -> +1.03
  104. To convert just change to 0x67 to decimal and use two decimal as E14 stands
  105. for 10^-2.
  106. Negative numbers are 2's complement
  107. */
  108. static void convert_from_vtf_format(u32 value, int size, int exp,
  109. int *val1, int *val2)
  110. {
  111. int sign = 1;
  112. if (value & BIT(size*8 - 1)) {
  113. value = ((1LL << (size * 8)) - value);
  114. sign = -1;
  115. }
  116. exp = hid_sensor_convert_exponent(exp);
  117. if (exp >= 0) {
  118. *val1 = sign * value * pow_10(exp);
  119. *val2 = 0;
  120. } else {
  121. split_micro_fraction(value, -exp, val1, val2);
  122. if (*val1)
  123. *val1 = sign * (*val1);
  124. else
  125. *val2 = sign * (*val2);
  126. }
  127. }
  128. static u32 convert_to_vtf_format(int size, int exp, int val1, int val2)
  129. {
  130. u32 value;
  131. int sign = 1;
  132. if (val1 < 0 || val2 < 0)
  133. sign = -1;
  134. exp = hid_sensor_convert_exponent(exp);
  135. if (exp < 0) {
  136. value = abs(val1) * pow_10(-exp);
  137. value += abs(val2) / pow_10(6+exp);
  138. } else
  139. value = abs(val1) / pow_10(exp);
  140. if (sign < 0)
  141. value = ((1LL << (size * 8)) - value);
  142. return value;
  143. }
  144. s32 hid_sensor_read_poll_value(struct hid_sensor_common *st)
  145. {
  146. s32 value = 0;
  147. int ret;
  148. ret = sensor_hub_get_feature(st->hsdev,
  149. st->poll.report_id,
  150. st->poll.index, sizeof(value), &value);
  151. if (ret < 0 || value < 0) {
  152. return -EINVAL;
  153. } else {
  154. if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
  155. value = value * 1000;
  156. }
  157. return value;
  158. }
  159. EXPORT_SYMBOL(hid_sensor_read_poll_value);
  160. int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
  161. int *val1, int *val2)
  162. {
  163. s32 value;
  164. int ret;
  165. ret = sensor_hub_get_feature(st->hsdev,
  166. st->poll.report_id,
  167. st->poll.index, sizeof(value), &value);
  168. if (ret < 0 || value < 0) {
  169. *val1 = *val2 = 0;
  170. return -EINVAL;
  171. } else {
  172. if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
  173. simple_div(1000, value, val1, val2);
  174. else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
  175. simple_div(1, value, val1, val2);
  176. else {
  177. *val1 = *val2 = 0;
  178. return -EINVAL;
  179. }
  180. }
  181. return IIO_VAL_INT_PLUS_MICRO;
  182. }
  183. EXPORT_SYMBOL(hid_sensor_read_samp_freq_value);
  184. int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
  185. int val1, int val2)
  186. {
  187. s32 value;
  188. int ret;
  189. if (val1 < 0 || val2 < 0)
  190. return -EINVAL;
  191. value = val1 * pow_10(6) + val2;
  192. if (value) {
  193. if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
  194. value = pow_10(9)/value;
  195. else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
  196. value = pow_10(6)/value;
  197. else
  198. value = 0;
  199. }
  200. ret = sensor_hub_set_feature(st->hsdev, st->poll.report_id,
  201. st->poll.index, sizeof(value), &value);
  202. if (ret < 0 || value < 0)
  203. ret = -EINVAL;
  204. ret = sensor_hub_get_feature(st->hsdev,
  205. st->poll.report_id,
  206. st->poll.index, sizeof(value), &value);
  207. if (ret < 0 || value < 0)
  208. return -EINVAL;
  209. st->poll_interval = value;
  210. return 0;
  211. }
  212. EXPORT_SYMBOL(hid_sensor_write_samp_freq_value);
  213. int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st,
  214. int *val1, int *val2)
  215. {
  216. s32 value;
  217. int ret;
  218. ret = sensor_hub_get_feature(st->hsdev,
  219. st->sensitivity.report_id,
  220. st->sensitivity.index, sizeof(value),
  221. &value);
  222. if (ret < 0 || value < 0) {
  223. *val1 = *val2 = 0;
  224. return -EINVAL;
  225. } else {
  226. convert_from_vtf_format(value, st->sensitivity.size,
  227. st->sensitivity.unit_expo,
  228. val1, val2);
  229. }
  230. return IIO_VAL_INT_PLUS_MICRO;
  231. }
  232. EXPORT_SYMBOL(hid_sensor_read_raw_hyst_value);
  233. int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
  234. int val1, int val2)
  235. {
  236. s32 value;
  237. int ret;
  238. if (val1 < 0 || val2 < 0)
  239. return -EINVAL;
  240. value = convert_to_vtf_format(st->sensitivity.size,
  241. st->sensitivity.unit_expo,
  242. val1, val2);
  243. ret = sensor_hub_set_feature(st->hsdev, st->sensitivity.report_id,
  244. st->sensitivity.index, sizeof(value),
  245. &value);
  246. if (ret < 0 || value < 0)
  247. ret = -EINVAL;
  248. ret = sensor_hub_get_feature(st->hsdev,
  249. st->sensitivity.report_id,
  250. st->sensitivity.index, sizeof(value),
  251. &value);
  252. if (ret < 0 || value < 0)
  253. return -EINVAL;
  254. st->raw_hystersis = value;
  255. return 0;
  256. }
  257. EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
  258. /*
  259. * This fuction applies the unit exponent to the scale.
  260. * For example:
  261. * 9.806650000 ->exp:2-> val0[980]val1[665000000]
  262. * 9.000806000 ->exp:2-> val0[900]val1[80600000]
  263. * 0.174535293 ->exp:2-> val0[17]val1[453529300]
  264. * 1.001745329 ->exp:0-> val0[1]val1[1745329]
  265. * 1.001745329 ->exp:2-> val0[100]val1[174532900]
  266. * 1.001745329 ->exp:4-> val0[10017]val1[453290000]
  267. * 9.806650000 ->exp:-2-> val0[0]val1[98066500]
  268. */
  269. static void adjust_exponent_nano(int *val0, int *val1, int scale0,
  270. int scale1, int exp)
  271. {
  272. int i;
  273. int x;
  274. int res;
  275. int rem;
  276. if (exp > 0) {
  277. *val0 = scale0 * pow_10(exp);
  278. res = 0;
  279. if (exp > 9) {
  280. *val1 = 0;
  281. return;
  282. }
  283. for (i = 0; i < exp; ++i) {
  284. x = scale1 / pow_10(8 - i);
  285. res += (pow_10(exp - 1 - i) * x);
  286. scale1 = scale1 % pow_10(8 - i);
  287. }
  288. *val0 += res;
  289. *val1 = scale1 * pow_10(exp);
  290. } else if (exp < 0) {
  291. exp = abs(exp);
  292. if (exp > 9) {
  293. *val0 = *val1 = 0;
  294. return;
  295. }
  296. *val0 = scale0 / pow_10(exp);
  297. rem = scale0 % pow_10(exp);
  298. res = 0;
  299. for (i = 0; i < (9 - exp); ++i) {
  300. x = scale1 / pow_10(8 - i);
  301. res += (pow_10(8 - exp - i) * x);
  302. scale1 = scale1 % pow_10(8 - i);
  303. }
  304. *val1 = rem * pow_10(9 - exp) + res;
  305. } else {
  306. *val0 = scale0;
  307. *val1 = scale1;
  308. }
  309. }
  310. int hid_sensor_format_scale(u32 usage_id,
  311. struct hid_sensor_hub_attribute_info *attr_info,
  312. int *val0, int *val1)
  313. {
  314. int i;
  315. int exp;
  316. *val0 = 1;
  317. *val1 = 0;
  318. for (i = 0; i < ARRAY_SIZE(unit_conversion); ++i) {
  319. if (unit_conversion[i].usage_id == usage_id &&
  320. unit_conversion[i].unit == attr_info->units) {
  321. exp = hid_sensor_convert_exponent(
  322. attr_info->unit_expo);
  323. adjust_exponent_nano(val0, val1,
  324. unit_conversion[i].scale_val0,
  325. unit_conversion[i].scale_val1, exp);
  326. break;
  327. }
  328. }
  329. return IIO_VAL_INT_PLUS_NANO;
  330. }
  331. EXPORT_SYMBOL(hid_sensor_format_scale);
  332. int64_t hid_sensor_convert_timestamp(struct hid_sensor_common *st,
  333. int64_t raw_value)
  334. {
  335. return st->timestamp_ns_scale * raw_value;
  336. }
  337. EXPORT_SYMBOL(hid_sensor_convert_timestamp);
  338. static
  339. int hid_sensor_get_reporting_interval(struct hid_sensor_hub_device *hsdev,
  340. u32 usage_id,
  341. struct hid_sensor_common *st)
  342. {
  343. sensor_hub_input_get_attribute_info(hsdev,
  344. HID_FEATURE_REPORT, usage_id,
  345. HID_USAGE_SENSOR_PROP_REPORT_INTERVAL,
  346. &st->poll);
  347. /* Default unit of measure is milliseconds */
  348. if (st->poll.units == 0)
  349. st->poll.units = HID_USAGE_SENSOR_UNITS_MILLISECOND;
  350. st->poll_interval = -1;
  351. return 0;
  352. }
  353. int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
  354. u32 usage_id,
  355. struct hid_sensor_common *st)
  356. {
  357. struct hid_sensor_hub_attribute_info timestamp;
  358. s32 value;
  359. int ret;
  360. hid_sensor_get_reporting_interval(hsdev, usage_id, st);
  361. sensor_hub_input_get_attribute_info(hsdev,
  362. HID_FEATURE_REPORT, usage_id,
  363. HID_USAGE_SENSOR_PROP_REPORT_STATE,
  364. &st->report_state);
  365. sensor_hub_input_get_attribute_info(hsdev,
  366. HID_FEATURE_REPORT, usage_id,
  367. HID_USAGE_SENSOR_PROY_POWER_STATE,
  368. &st->power_state);
  369. sensor_hub_input_get_attribute_info(hsdev,
  370. HID_FEATURE_REPORT, usage_id,
  371. HID_USAGE_SENSOR_PROP_SENSITIVITY_ABS,
  372. &st->sensitivity);
  373. st->raw_hystersis = -1;
  374. sensor_hub_input_get_attribute_info(hsdev,
  375. HID_INPUT_REPORT, usage_id,
  376. HID_USAGE_SENSOR_TIME_TIMESTAMP,
  377. &timestamp);
  378. if (timestamp.index >= 0 && timestamp.report_id) {
  379. int val0, val1;
  380. hid_sensor_format_scale(HID_USAGE_SENSOR_TIME_TIMESTAMP,
  381. &timestamp, &val0, &val1);
  382. st->timestamp_ns_scale = val0;
  383. } else
  384. st->timestamp_ns_scale = 1000000000;
  385. hid_dbg(hsdev->hdev, "common attributes: %x:%x, %x:%x, %x:%x %x:%x %x:%x\n",
  386. st->poll.index, st->poll.report_id,
  387. st->report_state.index, st->report_state.report_id,
  388. st->power_state.index, st->power_state.report_id,
  389. st->sensitivity.index, st->sensitivity.report_id,
  390. timestamp.index, timestamp.report_id);
  391. ret = sensor_hub_get_feature(hsdev,
  392. st->power_state.report_id,
  393. st->power_state.index, sizeof(value), &value);
  394. if (ret < 0)
  395. return ret;
  396. if (value < 0)
  397. return -EINVAL;
  398. return 0;
  399. }
  400. EXPORT_SYMBOL(hid_sensor_parse_common_attributes);
  401. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  402. MODULE_DESCRIPTION("HID Sensor common attribute processing");
  403. MODULE_LICENSE("GPL");