hid-sensor-gyro-3d.c 11 KB

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