hid-sensor-attributes.c 6.0 KB

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