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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 min;
  109. *val = 0;
  110. *val2 = 0;
  111. switch (mask) {
  112. case IIO_CHAN_INFO_RAW:
  113. hid_sensor_power_state(&gyro_state->common_attributes, true);
  114. report_id = gyro_state->gyro[chan->scan_index].report_id;
  115. min = gyro_state->gyro[chan->scan_index].logical_minimum;
  116. address = gyro_3d_addresses[chan->scan_index];
  117. if (report_id >= 0)
  118. *val = sensor_hub_input_attr_get_raw_value(
  119. gyro_state->common_attributes.hsdev,
  120. HID_USAGE_SENSOR_GYRO_3D, address,
  121. report_id,
  122. SENSOR_HUB_SYNC,
  123. min < 0);
  124. else {
  125. *val = 0;
  126. hid_sensor_power_state(&gyro_state->common_attributes,
  127. false);
  128. return -EINVAL;
  129. }
  130. hid_sensor_power_state(&gyro_state->common_attributes, false);
  131. ret_type = IIO_VAL_INT;
  132. break;
  133. case IIO_CHAN_INFO_SCALE:
  134. *val = gyro_state->scale_pre_decml;
  135. *val2 = gyro_state->scale_post_decml;
  136. ret_type = gyro_state->scale_precision;
  137. break;
  138. case IIO_CHAN_INFO_OFFSET:
  139. *val = gyro_state->value_offset;
  140. ret_type = IIO_VAL_INT;
  141. break;
  142. case IIO_CHAN_INFO_SAMP_FREQ:
  143. ret_type = hid_sensor_read_samp_freq_value(
  144. &gyro_state->common_attributes, val, val2);
  145. break;
  146. case IIO_CHAN_INFO_HYSTERESIS:
  147. ret_type = hid_sensor_read_raw_hyst_value(
  148. &gyro_state->common_attributes, val, val2);
  149. break;
  150. default:
  151. ret_type = -EINVAL;
  152. break;
  153. }
  154. return ret_type;
  155. }
  156. /* Channel write_raw handler */
  157. static int gyro_3d_write_raw(struct iio_dev *indio_dev,
  158. struct iio_chan_spec const *chan,
  159. int val,
  160. int val2,
  161. long mask)
  162. {
  163. struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
  164. int ret = 0;
  165. switch (mask) {
  166. case IIO_CHAN_INFO_SAMP_FREQ:
  167. ret = hid_sensor_write_samp_freq_value(
  168. &gyro_state->common_attributes, val, val2);
  169. break;
  170. case IIO_CHAN_INFO_HYSTERESIS:
  171. ret = hid_sensor_write_raw_hyst_value(
  172. &gyro_state->common_attributes, val, val2);
  173. break;
  174. default:
  175. ret = -EINVAL;
  176. }
  177. return ret;
  178. }
  179. static const struct iio_info gyro_3d_info = {
  180. .read_raw = &gyro_3d_read_raw,
  181. .write_raw = &gyro_3d_write_raw,
  182. };
  183. /* Function to push data to buffer */
  184. static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
  185. int len)
  186. {
  187. dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
  188. iio_push_to_buffers(indio_dev, data);
  189. }
  190. /* Callback handler to send event after all samples are received and captured */
  191. static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
  192. unsigned usage_id,
  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. dev_dbg(&indio_dev->dev, "gyro_3d_proc_event\n");
  198. if (atomic_read(&gyro_state->common_attributes.data_ready))
  199. hid_sensor_push_data(indio_dev,
  200. gyro_state->gyro_val,
  201. sizeof(gyro_state->gyro_val));
  202. return 0;
  203. }
  204. /* Capture samples in local storage */
  205. static int gyro_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
  206. unsigned usage_id,
  207. size_t raw_len, char *raw_data,
  208. void *priv)
  209. {
  210. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  211. struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
  212. int offset;
  213. int ret = -EINVAL;
  214. switch (usage_id) {
  215. case HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS:
  216. case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS:
  217. case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS:
  218. offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS;
  219. gyro_state->gyro_val[CHANNEL_SCAN_INDEX_X + offset] =
  220. *(u32 *)raw_data;
  221. ret = 0;
  222. break;
  223. default:
  224. break;
  225. }
  226. return ret;
  227. }
  228. /* Parse report which is specific to an usage id*/
  229. static int gyro_3d_parse_report(struct platform_device *pdev,
  230. struct hid_sensor_hub_device *hsdev,
  231. struct iio_chan_spec *channels,
  232. unsigned usage_id,
  233. struct gyro_3d_state *st)
  234. {
  235. int ret;
  236. int i;
  237. for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
  238. ret = sensor_hub_input_get_attribute_info(hsdev,
  239. HID_INPUT_REPORT,
  240. usage_id,
  241. HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + i,
  242. &st->gyro[CHANNEL_SCAN_INDEX_X + i]);
  243. if (ret < 0)
  244. break;
  245. gyro_3d_adjust_channel_bit_mask(channels,
  246. CHANNEL_SCAN_INDEX_X + i,
  247. st->gyro[CHANNEL_SCAN_INDEX_X + i].size);
  248. }
  249. dev_dbg(&pdev->dev, "gyro_3d %x:%x, %x:%x, %x:%x\n",
  250. st->gyro[0].index,
  251. st->gyro[0].report_id,
  252. st->gyro[1].index, st->gyro[1].report_id,
  253. st->gyro[2].index, st->gyro[2].report_id);
  254. st->scale_precision = hid_sensor_format_scale(
  255. HID_USAGE_SENSOR_GYRO_3D,
  256. &st->gyro[CHANNEL_SCAN_INDEX_X],
  257. &st->scale_pre_decml, &st->scale_post_decml);
  258. /* Set Sensitivity field ids, when there is no individual modifier */
  259. if (st->common_attributes.sensitivity.index < 0) {
  260. sensor_hub_input_get_attribute_info(hsdev,
  261. HID_FEATURE_REPORT, usage_id,
  262. HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
  263. HID_USAGE_SENSOR_DATA_ANGL_VELOCITY,
  264. &st->common_attributes.sensitivity);
  265. dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
  266. st->common_attributes.sensitivity.index,
  267. st->common_attributes.sensitivity.report_id);
  268. }
  269. return ret;
  270. }
  271. /* Function to initialize the processing for usage id */
  272. static int hid_gyro_3d_probe(struct platform_device *pdev)
  273. {
  274. int ret = 0;
  275. static const char *name = "gyro_3d";
  276. struct iio_dev *indio_dev;
  277. struct gyro_3d_state *gyro_state;
  278. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  279. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state));
  280. if (!indio_dev)
  281. return -ENOMEM;
  282. platform_set_drvdata(pdev, indio_dev);
  283. gyro_state = iio_priv(indio_dev);
  284. gyro_state->common_attributes.hsdev = hsdev;
  285. gyro_state->common_attributes.pdev = pdev;
  286. ret = hid_sensor_parse_common_attributes(hsdev,
  287. HID_USAGE_SENSOR_GYRO_3D,
  288. &gyro_state->common_attributes);
  289. if (ret) {
  290. dev_err(&pdev->dev, "failed to setup common attributes\n");
  291. return ret;
  292. }
  293. indio_dev->channels = kmemdup(gyro_3d_channels,
  294. sizeof(gyro_3d_channels), GFP_KERNEL);
  295. if (!indio_dev->channels) {
  296. dev_err(&pdev->dev, "failed to duplicate channels\n");
  297. return -ENOMEM;
  298. }
  299. ret = gyro_3d_parse_report(pdev, hsdev,
  300. (struct iio_chan_spec *)indio_dev->channels,
  301. HID_USAGE_SENSOR_GYRO_3D, gyro_state);
  302. if (ret) {
  303. dev_err(&pdev->dev, "failed to setup attributes\n");
  304. goto error_free_dev_mem;
  305. }
  306. indio_dev->num_channels = ARRAY_SIZE(gyro_3d_channels);
  307. indio_dev->dev.parent = &pdev->dev;
  308. indio_dev->info = &gyro_3d_info;
  309. indio_dev->name = name;
  310. indio_dev->modes = INDIO_DIRECT_MODE;
  311. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  312. NULL, NULL);
  313. if (ret) {
  314. dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  315. goto error_free_dev_mem;
  316. }
  317. atomic_set(&gyro_state->common_attributes.data_ready, 0);
  318. ret = hid_sensor_setup_trigger(indio_dev, name,
  319. &gyro_state->common_attributes);
  320. if (ret < 0) {
  321. dev_err(&pdev->dev, "trigger setup failed\n");
  322. goto error_unreg_buffer_funcs;
  323. }
  324. ret = iio_device_register(indio_dev);
  325. if (ret) {
  326. dev_err(&pdev->dev, "device register failed\n");
  327. goto error_remove_trigger;
  328. }
  329. gyro_state->callbacks.send_event = gyro_3d_proc_event;
  330. gyro_state->callbacks.capture_sample = gyro_3d_capture_sample;
  331. gyro_state->callbacks.pdev = pdev;
  332. ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D,
  333. &gyro_state->callbacks);
  334. if (ret < 0) {
  335. dev_err(&pdev->dev, "callback reg failed\n");
  336. goto error_iio_unreg;
  337. }
  338. return ret;
  339. error_iio_unreg:
  340. iio_device_unregister(indio_dev);
  341. error_remove_trigger:
  342. hid_sensor_remove_trigger(&gyro_state->common_attributes);
  343. error_unreg_buffer_funcs:
  344. iio_triggered_buffer_cleanup(indio_dev);
  345. error_free_dev_mem:
  346. kfree(indio_dev->channels);
  347. return ret;
  348. }
  349. /* Function to deinitialize the processing for usage id */
  350. static int hid_gyro_3d_remove(struct platform_device *pdev)
  351. {
  352. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  353. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  354. struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
  355. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D);
  356. iio_device_unregister(indio_dev);
  357. hid_sensor_remove_trigger(&gyro_state->common_attributes);
  358. iio_triggered_buffer_cleanup(indio_dev);
  359. kfree(indio_dev->channels);
  360. return 0;
  361. }
  362. static const struct platform_device_id hid_gyro_3d_ids[] = {
  363. {
  364. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  365. .name = "HID-SENSOR-200076",
  366. },
  367. { /* sentinel */ }
  368. };
  369. MODULE_DEVICE_TABLE(platform, hid_gyro_3d_ids);
  370. static struct platform_driver hid_gyro_3d_platform_driver = {
  371. .id_table = hid_gyro_3d_ids,
  372. .driver = {
  373. .name = KBUILD_MODNAME,
  374. .pm = &hid_sensor_pm_ops,
  375. },
  376. .probe = hid_gyro_3d_probe,
  377. .remove = hid_gyro_3d_remove,
  378. };
  379. module_platform_driver(hid_gyro_3d_platform_driver);
  380. MODULE_DESCRIPTION("HID Sensor Gyroscope 3D");
  381. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  382. MODULE_LICENSE("GPL");