PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/iio/orientation/hid-sensor-rotation.c

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