PageRenderTime 69ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/gby/linux
C | 386 lines | 307 code | 50 blank | 29 comment | 19 complexity | a29907961bbfccb7a7f32d42befb3326 MD5 | raw file
  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. .driver_module = THIS_MODULE,
  128. .read_raw_multi = &dev_rot_read_raw,
  129. .write_raw = &dev_rot_write_raw,
  130. };
  131. /* Function to push data to buffer */
  132. static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
  133. {
  134. dev_dbg(&indio_dev->dev, "hid_sensor_push_data >>\n");
  135. iio_push_to_buffers(indio_dev, (u8 *)data);
  136. dev_dbg(&indio_dev->dev, "hid_sensor_push_data <<\n");
  137. }
  138. /* Callback handler to send event after all samples are received and captured */
  139. static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
  140. unsigned usage_id,
  141. void *priv)
  142. {
  143. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  144. struct dev_rot_state *rot_state = iio_priv(indio_dev);
  145. dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n");
  146. if (atomic_read(&rot_state->common_attributes.data_ready))
  147. hid_sensor_push_data(indio_dev,
  148. (u8 *)rot_state->sampled_vals,
  149. sizeof(rot_state->sampled_vals));
  150. return 0;
  151. }
  152. /* Capture samples in local storage */
  153. static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev,
  154. unsigned usage_id,
  155. size_t raw_len, char *raw_data,
  156. void *priv)
  157. {
  158. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  159. struct dev_rot_state *rot_state = iio_priv(indio_dev);
  160. if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) {
  161. memcpy(rot_state->sampled_vals, raw_data,
  162. sizeof(rot_state->sampled_vals));
  163. dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len,
  164. sizeof(rot_state->sampled_vals));
  165. }
  166. return 0;
  167. }
  168. /* Parse report which is specific to an usage id*/
  169. static int dev_rot_parse_report(struct platform_device *pdev,
  170. struct hid_sensor_hub_device *hsdev,
  171. struct iio_chan_spec *channels,
  172. unsigned usage_id,
  173. struct dev_rot_state *st)
  174. {
  175. int ret;
  176. ret = sensor_hub_input_get_attribute_info(hsdev,
  177. HID_INPUT_REPORT,
  178. usage_id,
  179. HID_USAGE_SENSOR_ORIENT_QUATERNION,
  180. &st->quaternion);
  181. if (ret)
  182. return ret;
  183. dev_rot_adjust_channel_bit_mask(&channels[0],
  184. st->quaternion.size / 4);
  185. dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index,
  186. st->quaternion.report_id);
  187. dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n",
  188. st->quaternion.size);
  189. st->scale_precision = hid_sensor_format_scale(
  190. hsdev->usage,
  191. &st->quaternion,
  192. &st->scale_pre_decml, &st->scale_post_decml);
  193. /* Set Sensitivity field ids, when there is no individual modifier */
  194. if (st->common_attributes.sensitivity.index < 0) {
  195. sensor_hub_input_get_attribute_info(hsdev,
  196. HID_FEATURE_REPORT, usage_id,
  197. HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
  198. HID_USAGE_SENSOR_DATA_ORIENTATION,
  199. &st->common_attributes.sensitivity);
  200. dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
  201. st->common_attributes.sensitivity.index,
  202. st->common_attributes.sensitivity.report_id);
  203. }
  204. return 0;
  205. }
  206. /* Function to initialize the processing for usage id */
  207. static int hid_dev_rot_probe(struct platform_device *pdev)
  208. {
  209. int ret;
  210. static char *name;
  211. struct iio_dev *indio_dev;
  212. struct dev_rot_state *rot_state;
  213. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  214. indio_dev = devm_iio_device_alloc(&pdev->dev,
  215. sizeof(struct dev_rot_state));
  216. if (indio_dev == NULL)
  217. return -ENOMEM;
  218. platform_set_drvdata(pdev, indio_dev);
  219. rot_state = iio_priv(indio_dev);
  220. rot_state->common_attributes.hsdev = hsdev;
  221. rot_state->common_attributes.pdev = pdev;
  222. switch (hsdev->usage) {
  223. case HID_USAGE_SENSOR_DEVICE_ORIENTATION:
  224. name = "dev_rotation";
  225. break;
  226. case HID_USAGE_SENSOR_RELATIVE_ORIENTATION:
  227. name = "relative_orientation";
  228. break;
  229. case HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION:
  230. name = "geomagnetic_orientation";
  231. break;
  232. default:
  233. return -EINVAL;
  234. }
  235. ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage,
  236. &rot_state->common_attributes);
  237. if (ret) {
  238. dev_err(&pdev->dev, "failed to setup common attributes\n");
  239. return ret;
  240. }
  241. indio_dev->channels = devm_kmemdup(&pdev->dev, dev_rot_channels,
  242. sizeof(dev_rot_channels),
  243. GFP_KERNEL);
  244. if (!indio_dev->channels) {
  245. dev_err(&pdev->dev, "failed to duplicate channels\n");
  246. return -ENOMEM;
  247. }
  248. ret = dev_rot_parse_report(pdev, hsdev,
  249. (struct iio_chan_spec *)indio_dev->channels,
  250. hsdev->usage, rot_state);
  251. if (ret) {
  252. dev_err(&pdev->dev, "failed to setup attributes\n");
  253. return ret;
  254. }
  255. indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels);
  256. indio_dev->dev.parent = &pdev->dev;
  257. indio_dev->info = &dev_rot_info;
  258. indio_dev->name = name;
  259. indio_dev->modes = INDIO_DIRECT_MODE;
  260. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  261. NULL, NULL);
  262. if (ret) {
  263. dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  264. return ret;
  265. }
  266. atomic_set(&rot_state->common_attributes.data_ready, 0);
  267. ret = hid_sensor_setup_trigger(indio_dev, name,
  268. &rot_state->common_attributes);
  269. if (ret) {
  270. dev_err(&pdev->dev, "trigger setup failed\n");
  271. goto error_unreg_buffer_funcs;
  272. }
  273. ret = iio_device_register(indio_dev);
  274. if (ret) {
  275. dev_err(&pdev->dev, "device register failed\n");
  276. goto error_remove_trigger;
  277. }
  278. rot_state->callbacks.send_event = dev_rot_proc_event;
  279. rot_state->callbacks.capture_sample = dev_rot_capture_sample;
  280. rot_state->callbacks.pdev = pdev;
  281. ret = sensor_hub_register_callback(hsdev, hsdev->usage,
  282. &rot_state->callbacks);
  283. if (ret) {
  284. dev_err(&pdev->dev, "callback reg failed\n");
  285. goto error_iio_unreg;
  286. }
  287. return 0;
  288. error_iio_unreg:
  289. iio_device_unregister(indio_dev);
  290. error_remove_trigger:
  291. hid_sensor_remove_trigger(&rot_state->common_attributes);
  292. error_unreg_buffer_funcs:
  293. iio_triggered_buffer_cleanup(indio_dev);
  294. return ret;
  295. }
  296. /* Function to deinitialize the processing for usage id */
  297. static int hid_dev_rot_remove(struct platform_device *pdev)
  298. {
  299. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  300. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  301. struct dev_rot_state *rot_state = iio_priv(indio_dev);
  302. sensor_hub_remove_callback(hsdev, hsdev->usage);
  303. iio_device_unregister(indio_dev);
  304. hid_sensor_remove_trigger(&rot_state->common_attributes);
  305. iio_triggered_buffer_cleanup(indio_dev);
  306. return 0;
  307. }
  308. static const struct platform_device_id hid_dev_rot_ids[] = {
  309. {
  310. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  311. .name = "HID-SENSOR-20008a",
  312. },
  313. {
  314. /* Relative orientation(AG) sensor */
  315. .name = "HID-SENSOR-20008e",
  316. },
  317. {
  318. /* Geomagnetic orientation(AM) sensor */
  319. .name = "HID-SENSOR-2000c1",
  320. },
  321. { /* sentinel */ }
  322. };
  323. MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids);
  324. static struct platform_driver hid_dev_rot_platform_driver = {
  325. .id_table = hid_dev_rot_ids,
  326. .driver = {
  327. .name = KBUILD_MODNAME,
  328. .pm = &hid_sensor_pm_ops,
  329. },
  330. .probe = hid_dev_rot_probe,
  331. .remove = hid_dev_rot_remove,
  332. };
  333. module_platform_driver(hid_dev_rot_platform_driver);
  334. MODULE_DESCRIPTION("HID Sensor Device Rotation");
  335. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  336. MODULE_LICENSE("GPL");