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

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