hid-sensor-attributes.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 int pow_10(unsigned power)
  29. {
  30. int i;
  31. int ret = 1;
  32. for (i = 0; i < power; ++i)
  33. ret = ret * 10;
  34. return ret;
  35. }
  36. static void simple_div(int dividend, int divisor, int *whole,
  37. int *micro_frac)
  38. {
  39. int rem;
  40. int exp = 0;
  41. *micro_frac = 0;
  42. if (divisor == 0) {
  43. *whole = 0;
  44. return;
  45. }
  46. *whole = dividend/divisor;
  47. rem = dividend % divisor;
  48. if (rem) {
  49. while (rem <= divisor) {
  50. rem *= 10;
  51. exp++;
  52. }
  53. *micro_frac = (rem / divisor) * pow_10(6-exp);
  54. }
  55. }
  56. static void split_micro_fraction(unsigned int no, int exp, int *val1, int *val2)
  57. {
  58. *val1 = no/pow_10(exp);
  59. *val2 = no%pow_10(exp) * pow_10(6-exp);
  60. }
  61. /*
  62. VTF format uses exponent and variable size format.
  63. For example if the size is 2 bytes
  64. 0x0067 with VTF16E14 format -> +1.03
  65. To convert just change to 0x67 to decimal and use two decimal as E14 stands
  66. for 10^-2.
  67. Negative numbers are 2's complement
  68. */
  69. static void convert_from_vtf_format(u32 value, int size, int exp,
  70. int *val1, int *val2)
  71. {
  72. int sign = 1;
  73. if (value & BIT(size*8 - 1)) {
  74. value = ((1LL << (size * 8)) - value);
  75. sign = -1;
  76. }
  77. exp = hid_sensor_convert_exponent(exp);
  78. if (exp >= 0) {
  79. *val1 = sign * value * pow_10(exp);
  80. *val2 = 0;
  81. } else {
  82. split_micro_fraction(value, -exp, val1, val2);
  83. if (*val1)
  84. *val1 = sign * (*val1);
  85. else
  86. *val2 = sign * (*val2);
  87. }
  88. }
  89. static u32 convert_to_vtf_format(int size, int exp, int val1, int val2)
  90. {
  91. u32 value;
  92. int sign = 1;
  93. if (val1 < 0 || val2 < 0)
  94. sign = -1;
  95. exp = hid_sensor_convert_exponent(exp);
  96. if (exp < 0) {
  97. value = abs(val1) * pow_10(-exp);
  98. value += abs(val2) / pow_10(6+exp);
  99. } else
  100. value = abs(val1) / pow_10(exp);
  101. if (sign < 0)
  102. value = ((1LL << (size * 8)) - value);
  103. return value;
  104. }
  105. int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st,
  106. int *val1, int *val2)
  107. {
  108. s32 value;
  109. int ret;
  110. ret = sensor_hub_get_feature(st->hsdev,
  111. st->poll.report_id,
  112. st->poll.index, &value);
  113. if (ret < 0 || value < 0) {
  114. *val1 = *val2 = 0;
  115. return -EINVAL;
  116. } else {
  117. if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
  118. simple_div(1000, value, val1, val2);
  119. else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
  120. simple_div(1, value, val1, val2);
  121. else {
  122. *val1 = *val2 = 0;
  123. return -EINVAL;
  124. }
  125. }
  126. return IIO_VAL_INT_PLUS_MICRO;
  127. }
  128. EXPORT_SYMBOL(hid_sensor_read_samp_freq_value);
  129. int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st,
  130. int val1, int val2)
  131. {
  132. s32 value;
  133. int ret;
  134. if (val1 < 0 || val2 < 0)
  135. ret = -EINVAL;
  136. value = val1 * pow_10(6) + val2;
  137. if (value) {
  138. if (st->poll.units == HID_USAGE_SENSOR_UNITS_MILLISECOND)
  139. value = pow_10(9)/value;
  140. else if (st->poll.units == HID_USAGE_SENSOR_UNITS_SECOND)
  141. value = pow_10(6)/value;
  142. else
  143. value = 0;
  144. }
  145. ret = sensor_hub_set_feature(st->hsdev,
  146. st->poll.report_id,
  147. st->poll.index, value);
  148. if (ret < 0 || value < 0)
  149. ret = -EINVAL;
  150. return ret;
  151. }
  152. EXPORT_SYMBOL(hid_sensor_write_samp_freq_value);
  153. int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st,
  154. int *val1, int *val2)
  155. {
  156. s32 value;
  157. int ret;
  158. ret = sensor_hub_get_feature(st->hsdev,
  159. st->sensitivity.report_id,
  160. st->sensitivity.index, &value);
  161. if (ret < 0 || value < 0) {
  162. *val1 = *val2 = 0;
  163. return -EINVAL;
  164. } else {
  165. convert_from_vtf_format(value, st->sensitivity.size,
  166. st->sensitivity.unit_expo,
  167. val1, val2);
  168. }
  169. return IIO_VAL_INT_PLUS_MICRO;
  170. }
  171. EXPORT_SYMBOL(hid_sensor_read_raw_hyst_value);
  172. int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st,
  173. int val1, int val2)
  174. {
  175. s32 value;
  176. int ret;
  177. value = convert_to_vtf_format(st->sensitivity.size,
  178. st->sensitivity.unit_expo,
  179. val1, val2);
  180. ret = sensor_hub_set_feature(st->hsdev,
  181. st->sensitivity.report_id,
  182. st->sensitivity.index, value);
  183. if (ret < 0 || value < 0)
  184. ret = -EINVAL;
  185. return ret;
  186. }
  187. EXPORT_SYMBOL(hid_sensor_write_raw_hyst_value);
  188. int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev,
  189. u32 usage_id,
  190. struct hid_sensor_common *st)
  191. {
  192. sensor_hub_input_get_attribute_info(hsdev,
  193. HID_FEATURE_REPORT, usage_id,
  194. HID_USAGE_SENSOR_PROP_REPORT_INTERVAL,
  195. &st->poll);
  196. sensor_hub_input_get_attribute_info(hsdev,
  197. HID_FEATURE_REPORT, usage_id,
  198. HID_USAGE_SENSOR_PROP_REPORT_STATE,
  199. &st->report_state);
  200. sensor_hub_input_get_attribute_info(hsdev,
  201. HID_FEATURE_REPORT, usage_id,
  202. HID_USAGE_SENSOR_PROY_POWER_STATE,
  203. &st->power_state);
  204. sensor_hub_input_get_attribute_info(hsdev,
  205. HID_FEATURE_REPORT, usage_id,
  206. HID_USAGE_SENSOR_PROP_SENSITIVITY_ABS,
  207. &st->sensitivity);
  208. hid_dbg(hsdev->hdev, "common attributes: %x:%x, %x:%x, %x:%x %x:%x\n",
  209. st->poll.index, st->poll.report_id,
  210. st->report_state.index, st->report_state.report_id,
  211. st->power_state.index, st->power_state.report_id,
  212. st->sensitivity.index, st->sensitivity.report_id);
  213. return 0;
  214. }
  215. EXPORT_SYMBOL(hid_sensor_parse_common_attributes);
  216. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  217. MODULE_DESCRIPTION("HID Sensor common attribute processing");
  218. MODULE_LICENSE("GPL");