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

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