hid-sensor-attributes.c 9.7 KB

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