hid-sensor-rotation.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * HID Sensors Driver
  3. * Copyright (c) 2014, 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. #include <linux/device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/module.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/slab.h>
  20. #include <linux/hid-sensor-hub.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/sysfs.h>
  23. #include <linux/iio/buffer.h>
  24. #include <linux/iio/trigger_consumer.h>
  25. #include <linux/iio/triggered_buffer.h>
  26. #include "../common/hid-sensors/hid-sensor-trigger.h"
  27. struct dev_rot_state {
  28. struct hid_sensor_hub_callbacks callbacks;
  29. struct hid_sensor_common common_attributes;
  30. struct hid_sensor_hub_attribute_info quaternion;
  31. u32 sampled_vals[4];
  32. int scale_pre_decml;
  33. int scale_post_decml;
  34. int scale_precision;
  35. int value_offset;
  36. };
  37. /* Channel definitions */
  38. static const struct iio_chan_spec dev_rot_channels[] = {
  39. {
  40. .type = IIO_ROT,
  41. .modified = 1,
  42. .channel2 = IIO_MOD_QUATERNION,
  43. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  44. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  45. BIT(IIO_CHAN_INFO_OFFSET) |
  46. BIT(IIO_CHAN_INFO_SCALE) |
  47. BIT(IIO_CHAN_INFO_HYSTERESIS)
  48. }
  49. };
  50. /* Adjust channel real bits based on report descriptor */
  51. static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec *chan,
  52. int size)
  53. {
  54. chan->scan_type.sign = 's';
  55. /* Real storage bits will change based on the report desc. */
  56. chan->scan_type.realbits = size * 8;
  57. /* Maximum size of a sample to capture is u32 */
  58. chan->scan_type.storagebits = sizeof(u32) * 8;
  59. chan->scan_type.repeat = 4;
  60. }
  61. /* Channel read_raw handler */
  62. static int dev_rot_read_raw(struct iio_dev *indio_dev,
  63. struct iio_chan_spec const *chan,
  64. int size, int *vals, int *val_len,
  65. long mask)
  66. {
  67. struct dev_rot_state *rot_state = iio_priv(indio_dev);
  68. int ret_type;
  69. int i;
  70. vals[0] = 0;
  71. vals[1] = 0;
  72. switch (mask) {
  73. case IIO_CHAN_INFO_RAW:
  74. if (size >= 4) {
  75. for (i = 0; i < 4; ++i)
  76. vals[i] = rot_state->sampled_vals[i];
  77. ret_type = IIO_VAL_INT_MULTIPLE;
  78. *val_len = 4;
  79. } else
  80. ret_type = -EINVAL;
  81. break;
  82. case IIO_CHAN_INFO_SCALE:
  83. vals[0] = rot_state->scale_pre_decml;
  84. vals[1] = rot_state->scale_post_decml;
  85. return rot_state->scale_precision;
  86. case IIO_CHAN_INFO_OFFSET:
  87. *vals = rot_state->value_offset;
  88. return IIO_VAL_INT;
  89. case IIO_CHAN_INFO_SAMP_FREQ:
  90. ret_type = hid_sensor_read_samp_freq_value(
  91. &rot_state->common_attributes, &vals[0], &vals[1]);
  92. break;
  93. case IIO_CHAN_INFO_HYSTERESIS:
  94. ret_type = hid_sensor_read_raw_hyst_value(
  95. &rot_state->common_attributes, &vals[0], &vals[1]);
  96. break;
  97. default:
  98. ret_type = -EINVAL;
  99. break;
  100. }
  101. return ret_type;
  102. }
  103. /* Channel write_raw handler */
  104. static int dev_rot_write_raw(struct iio_dev *indio_dev,
  105. struct iio_chan_spec const *chan,
  106. int val,
  107. int val2,
  108. long mask)
  109. {
  110. struct dev_rot_state *rot_state = iio_priv(indio_dev);
  111. int ret;
  112. switch (mask) {
  113. case IIO_CHAN_INFO_SAMP_FREQ:
  114. ret = hid_sensor_write_samp_freq_value(
  115. &rot_state->common_attributes, val, val2);
  116. break;
  117. case IIO_CHAN_INFO_HYSTERESIS:
  118. ret = hid_sensor_write_raw_hyst_value(
  119. &rot_state->common_attributes, val, val2);
  120. break;
  121. default:
  122. ret = -EINVAL;
  123. }
  124. return ret;
  125. }
  126. static const struct iio_info dev_rot_info = {
  127. .read_raw_multi = &dev_rot_read_raw,
  128. .write_raw = &dev_rot_write_raw,
  129. };
  130. /* Function to push data to buffer */
  131. static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
  132. {
  133. dev_dbg(&indio_dev->dev, "hid_sensor_push_data >>\n");
  134. iio_push_to_buffers(indio_dev, (u8 *)data);
  135. dev_dbg(&indio_dev->dev, "hid_sensor_push_data <<\n");
  136. }
  137. /* Callback handler to send event after all samples are received and captured */
  138. static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
  139. unsigned usage_id,
  140. void *priv)
  141. {
  142. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  143. struct dev_rot_state *rot_state = iio_priv(indio_dev);
  144. dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n");
  145. if (atomic_read(&rot_state->common_attributes.data_ready))
  146. hid_sensor_push_data(indio_dev,
  147. (u8 *)rot_state->sampled_vals,
  148. sizeof(rot_state->sampled_vals));
  149. return 0;
  150. }
  151. /* Capture samples in local storage */
  152. static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev,
  153. unsigned usage_id,
  154. size_t raw_len, char *raw_data,
  155. void *priv)
  156. {
  157. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  158. struct dev_rot_state *rot_state = iio_priv(indio_dev);
  159. if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) {
  160. memcpy(rot_state->sampled_vals, raw_data,
  161. sizeof(rot_state->sampled_vals));
  162. dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len,
  163. sizeof(rot_state->sampled_vals));
  164. }
  165. return 0;
  166. }
  167. /* Parse report which is specific to an usage id*/
  168. static int dev_rot_parse_report(struct platform_device *pdev,
  169. struct hid_sensor_hub_device *hsdev,
  170. struct iio_chan_spec *channels,
  171. unsigned usage_id,
  172. struct dev_rot_state *st)
  173. {
  174. int ret;
  175. ret = sensor_hub_input_get_attribute_info(hsdev,
  176. HID_INPUT_REPORT,
  177. usage_id,
  178. HID_USAGE_SENSOR_ORIENT_QUATERNION,
  179. &st->quaternion);
  180. if (ret)
  181. return ret;
  182. dev_rot_adjust_channel_bit_mask(&channels[0],
  183. st->quaternion.size / 4);
  184. dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index,
  185. st->quaternion.report_id);
  186. dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n",
  187. st->quaternion.size);
  188. st->scale_precision = hid_sensor_format_scale(
  189. hsdev->usage,
  190. &st->quaternion,
  191. &st->scale_pre_decml, &st->scale_post_decml);
  192. /* Set Sensitivity field ids, when there is no individual modifier */
  193. if (st->common_attributes.sensitivity.index < 0) {
  194. sensor_hub_input_get_attribute_info(hsdev,
  195. HID_FEATURE_REPORT, usage_id,
  196. HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
  197. HID_USAGE_SENSOR_DATA_ORIENTATION,
  198. &st->common_attributes.sensitivity);
  199. dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
  200. st->common_attributes.sensitivity.index,
  201. st->common_attributes.sensitivity.report_id);
  202. }
  203. return 0;
  204. }
  205. /* Function to initialize the processing for usage id */
  206. static int hid_dev_rot_probe(struct platform_device *pdev)
  207. {
  208. int ret;
  209. char *name;
  210. struct iio_dev *indio_dev;
  211. struct dev_rot_state *rot_state;
  212. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  213. indio_dev = devm_iio_device_alloc(&pdev->dev,
  214. sizeof(struct dev_rot_state));
  215. if (indio_dev == NULL)
  216. return -ENOMEM;
  217. platform_set_drvdata(pdev, indio_dev);
  218. rot_state = iio_priv(indio_dev);
  219. rot_state->common_attributes.hsdev = hsdev;
  220. rot_state->common_attributes.pdev = pdev;
  221. switch (hsdev->usage) {
  222. case HID_USAGE_SENSOR_DEVICE_ORIENTATION:
  223. name = "dev_rotation";
  224. break;
  225. case HID_USAGE_SENSOR_RELATIVE_ORIENTATION:
  226. name = "relative_orientation";
  227. break;
  228. case HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION:
  229. name = "geomagnetic_orientation";
  230. break;
  231. default:
  232. return -EINVAL;
  233. }
  234. ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage,
  235. &rot_state->common_attributes);
  236. if (ret) {
  237. dev_err(&pdev->dev, "failed to setup common attributes\n");
  238. return ret;
  239. }
  240. indio_dev->channels = devm_kmemdup(&pdev->dev, dev_rot_channels,
  241. sizeof(dev_rot_channels),
  242. GFP_KERNEL);
  243. if (!indio_dev->channels) {
  244. dev_err(&pdev->dev, "failed to duplicate channels\n");
  245. return -ENOMEM;
  246. }
  247. ret = dev_rot_parse_report(pdev, hsdev,
  248. (struct iio_chan_spec *)indio_dev->channels,
  249. hsdev->usage, rot_state);
  250. if (ret) {
  251. dev_err(&pdev->dev, "failed to setup attributes\n");
  252. return ret;
  253. }
  254. indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels);
  255. indio_dev->dev.parent = &pdev->dev;
  256. indio_dev->info = &dev_rot_info;
  257. indio_dev->name = name;
  258. indio_dev->modes = INDIO_DIRECT_MODE;
  259. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  260. NULL, NULL);
  261. if (ret) {
  262. dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  263. return ret;
  264. }
  265. atomic_set(&rot_state->common_attributes.data_ready, 0);
  266. ret = hid_sensor_setup_trigger(indio_dev, name,
  267. &rot_state->common_attributes);
  268. if (ret) {
  269. dev_err(&pdev->dev, "trigger setup failed\n");
  270. goto error_unreg_buffer_funcs;
  271. }
  272. ret = iio_device_register(indio_dev);
  273. if (ret) {
  274. dev_err(&pdev->dev, "device register failed\n");
  275. goto error_remove_trigger;
  276. }
  277. rot_state->callbacks.send_event = dev_rot_proc_event;
  278. rot_state->callbacks.capture_sample = dev_rot_capture_sample;
  279. rot_state->callbacks.pdev = pdev;
  280. ret = sensor_hub_register_callback(hsdev, hsdev->usage,
  281. &rot_state->callbacks);
  282. if (ret) {
  283. dev_err(&pdev->dev, "callback reg failed\n");
  284. goto error_iio_unreg;
  285. }
  286. return 0;
  287. error_iio_unreg:
  288. iio_device_unregister(indio_dev);
  289. error_remove_trigger:
  290. hid_sensor_remove_trigger(&rot_state->common_attributes);
  291. error_unreg_buffer_funcs:
  292. iio_triggered_buffer_cleanup(indio_dev);
  293. return ret;
  294. }
  295. /* Function to deinitialize the processing for usage id */
  296. static int hid_dev_rot_remove(struct platform_device *pdev)
  297. {
  298. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  299. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  300. struct dev_rot_state *rot_state = iio_priv(indio_dev);
  301. sensor_hub_remove_callback(hsdev, hsdev->usage);
  302. iio_device_unregister(indio_dev);
  303. hid_sensor_remove_trigger(&rot_state->common_attributes);
  304. iio_triggered_buffer_cleanup(indio_dev);
  305. return 0;
  306. }
  307. static const struct platform_device_id hid_dev_rot_ids[] = {
  308. {
  309. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  310. .name = "HID-SENSOR-20008a",
  311. },
  312. {
  313. /* Relative orientation(AG) sensor */
  314. .name = "HID-SENSOR-20008e",
  315. },
  316. {
  317. /* Geomagnetic orientation(AM) sensor */
  318. .name = "HID-SENSOR-2000c1",
  319. },
  320. { /* sentinel */ }
  321. };
  322. MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids);
  323. static struct platform_driver hid_dev_rot_platform_driver = {
  324. .id_table = hid_dev_rot_ids,
  325. .driver = {
  326. .name = KBUILD_MODNAME,
  327. .pm = &hid_sensor_pm_ops,
  328. },
  329. .probe = hid_dev_rot_probe,
  330. .remove = hid_dev_rot_remove,
  331. };
  332. module_platform_driver(hid_dev_rot_platform_driver);
  333. MODULE_DESCRIPTION("HID Sensor Device Rotation");
  334. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  335. MODULE_LICENSE("GPL");