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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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, u8 *data, int len)
  173. {
  174. dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
  175. iio_push_to_buffers(indio_dev, (u8 *)data);
  176. }
  177. /* Callback handler to send event after all samples are received and captured */
  178. static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
  179. unsigned usage_id,
  180. void *priv)
  181. {
  182. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  183. struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
  184. dev_dbg(&indio_dev->dev, "gyro_3d_proc_event [%d]\n",
  185. gyro_state->common_attributes.data_ready);
  186. if (gyro_state->common_attributes.data_ready)
  187. hid_sensor_push_data(indio_dev,
  188. (u8 *)gyro_state->gyro_val,
  189. sizeof(gyro_state->gyro_val));
  190. return 0;
  191. }
  192. /* Capture samples in local storage */
  193. static int gyro_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
  194. unsigned usage_id,
  195. size_t raw_len, char *raw_data,
  196. void *priv)
  197. {
  198. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  199. struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
  200. int offset;
  201. int ret = -EINVAL;
  202. switch (usage_id) {
  203. case HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS:
  204. case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS:
  205. case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS:
  206. offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS;
  207. gyro_state->gyro_val[CHANNEL_SCAN_INDEX_X + offset] =
  208. *(u32 *)raw_data;
  209. ret = 0;
  210. break;
  211. default:
  212. break;
  213. }
  214. return ret;
  215. }
  216. /* Parse report which is specific to an usage id*/
  217. static int gyro_3d_parse_report(struct platform_device *pdev,
  218. struct hid_sensor_hub_device *hsdev,
  219. struct iio_chan_spec *channels,
  220. unsigned usage_id,
  221. struct gyro_3d_state *st)
  222. {
  223. int ret;
  224. int i;
  225. for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
  226. ret = sensor_hub_input_get_attribute_info(hsdev,
  227. HID_INPUT_REPORT,
  228. usage_id,
  229. HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + i,
  230. &st->gyro[CHANNEL_SCAN_INDEX_X + i]);
  231. if (ret < 0)
  232. break;
  233. gyro_3d_adjust_channel_bit_mask(channels,
  234. CHANNEL_SCAN_INDEX_X + i,
  235. st->gyro[CHANNEL_SCAN_INDEX_X + i].size);
  236. }
  237. dev_dbg(&pdev->dev, "gyro_3d %x:%x, %x:%x, %x:%x\n",
  238. st->gyro[0].index,
  239. st->gyro[0].report_id,
  240. st->gyro[1].index, st->gyro[1].report_id,
  241. st->gyro[2].index, st->gyro[2].report_id);
  242. return ret;
  243. }
  244. /* Function to initialize the processing for usage id */
  245. static int hid_gyro_3d_probe(struct platform_device *pdev)
  246. {
  247. int ret = 0;
  248. static const char *name = "gyro_3d";
  249. struct iio_dev *indio_dev;
  250. struct gyro_3d_state *gyro_state;
  251. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  252. struct iio_chan_spec *channels;
  253. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state));
  254. if (!indio_dev)
  255. return -ENOMEM;
  256. platform_set_drvdata(pdev, indio_dev);
  257. gyro_state = iio_priv(indio_dev);
  258. gyro_state->common_attributes.hsdev = hsdev;
  259. gyro_state->common_attributes.pdev = pdev;
  260. ret = hid_sensor_parse_common_attributes(hsdev,
  261. HID_USAGE_SENSOR_GYRO_3D,
  262. &gyro_state->common_attributes);
  263. if (ret) {
  264. dev_err(&pdev->dev, "failed to setup common attributes\n");
  265. return ret;
  266. }
  267. channels = kmemdup(gyro_3d_channels, sizeof(gyro_3d_channels),
  268. GFP_KERNEL);
  269. if (!channels) {
  270. dev_err(&pdev->dev, "failed to duplicate channels\n");
  271. return -ENOMEM;
  272. }
  273. ret = gyro_3d_parse_report(pdev, hsdev, channels,
  274. HID_USAGE_SENSOR_GYRO_3D, gyro_state);
  275. if (ret) {
  276. dev_err(&pdev->dev, "failed to setup attributes\n");
  277. goto error_free_dev_mem;
  278. }
  279. indio_dev->channels = channels;
  280. indio_dev->num_channels = ARRAY_SIZE(gyro_3d_channels);
  281. indio_dev->dev.parent = &pdev->dev;
  282. indio_dev->info = &gyro_3d_info;
  283. indio_dev->name = name;
  284. indio_dev->modes = INDIO_DIRECT_MODE;
  285. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  286. NULL, NULL);
  287. if (ret) {
  288. dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  289. goto error_free_dev_mem;
  290. }
  291. gyro_state->common_attributes.data_ready = false;
  292. ret = hid_sensor_setup_trigger(indio_dev, name,
  293. &gyro_state->common_attributes);
  294. if (ret < 0) {
  295. dev_err(&pdev->dev, "trigger setup failed\n");
  296. goto error_unreg_buffer_funcs;
  297. }
  298. ret = iio_device_register(indio_dev);
  299. if (ret) {
  300. dev_err(&pdev->dev, "device register failed\n");
  301. goto error_remove_trigger;
  302. }
  303. gyro_state->callbacks.send_event = gyro_3d_proc_event;
  304. gyro_state->callbacks.capture_sample = gyro_3d_capture_sample;
  305. gyro_state->callbacks.pdev = pdev;
  306. ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D,
  307. &gyro_state->callbacks);
  308. if (ret < 0) {
  309. dev_err(&pdev->dev, "callback reg failed\n");
  310. goto error_iio_unreg;
  311. }
  312. return ret;
  313. error_iio_unreg:
  314. iio_device_unregister(indio_dev);
  315. error_remove_trigger:
  316. hid_sensor_remove_trigger(indio_dev);
  317. error_unreg_buffer_funcs:
  318. iio_triggered_buffer_cleanup(indio_dev);
  319. error_free_dev_mem:
  320. kfree(indio_dev->channels);
  321. return ret;
  322. }
  323. /* Function to deinitialize the processing for usage id */
  324. static int hid_gyro_3d_remove(struct platform_device *pdev)
  325. {
  326. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  327. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  328. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D);
  329. iio_device_unregister(indio_dev);
  330. hid_sensor_remove_trigger(indio_dev);
  331. iio_triggered_buffer_cleanup(indio_dev);
  332. kfree(indio_dev->channels);
  333. return 0;
  334. }
  335. static struct platform_device_id hid_gyro_3d_ids[] = {
  336. {
  337. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  338. .name = "HID-SENSOR-200076",
  339. },
  340. { /* sentinel */ }
  341. };
  342. MODULE_DEVICE_TABLE(platform, hid_gyro_3d_ids);
  343. static struct platform_driver hid_gyro_3d_platform_driver = {
  344. .id_table = hid_gyro_3d_ids,
  345. .driver = {
  346. .name = KBUILD_MODNAME,
  347. .owner = THIS_MODULE,
  348. },
  349. .probe = hid_gyro_3d_probe,
  350. .remove = hid_gyro_3d_remove,
  351. };
  352. module_platform_driver(hid_gyro_3d_platform_driver);
  353. MODULE_DESCRIPTION("HID Sensor Gyroscope 3D");
  354. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  355. MODULE_LICENSE("GPL");