hid-sensor-attributes.c 11 KB

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