hid-sensor-rotation.c 9.7 KB

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