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

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