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

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