hid-sensor-rotation.c 9.6 KB

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