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

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