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

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