hid-sensor-prox.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * HID Sensors Driver
  3. * Copyright (c) 2014, 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.
  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. #define CHANNEL_SCAN_INDEX_PRESENCE 0
  33. struct prox_state {
  34. struct hid_sensor_hub_callbacks callbacks;
  35. struct hid_sensor_common common_attributes;
  36. struct hid_sensor_hub_attribute_info prox_attr;
  37. u32 human_presence;
  38. };
  39. /* Channel definitions */
  40. static const struct iio_chan_spec prox_channels[] = {
  41. {
  42. .type = IIO_PROXIMITY,
  43. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  44. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  45. BIT(IIO_CHAN_INFO_SCALE) |
  46. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  47. BIT(IIO_CHAN_INFO_HYSTERESIS),
  48. .scan_index = CHANNEL_SCAN_INDEX_PRESENCE,
  49. }
  50. };
  51. /* Adjust channel real bits based on report descriptor */
  52. static void prox_adjust_channel_bit_mask(struct iio_chan_spec *channels,
  53. int channel, int size)
  54. {
  55. channels[channel].scan_type.sign = 's';
  56. /* Real storage bits will change based on the report desc. */
  57. channels[channel].scan_type.realbits = size * 8;
  58. /* Maximum size of a sample to capture is u32 */
  59. channels[channel].scan_type.storagebits = sizeof(u32) * 8;
  60. }
  61. /* Channel read_raw handler */
  62. static int prox_read_raw(struct iio_dev *indio_dev,
  63. struct iio_chan_spec const *chan,
  64. int *val, int *val2,
  65. long mask)
  66. {
  67. struct prox_state *prox_state = iio_priv(indio_dev);
  68. int report_id = -1;
  69. u32 address;
  70. int ret_type;
  71. *val = 0;
  72. *val2 = 0;
  73. switch (mask) {
  74. case IIO_CHAN_INFO_RAW:
  75. switch (chan->scan_index) {
  76. case CHANNEL_SCAN_INDEX_PRESENCE:
  77. report_id = prox_state->prox_attr.report_id;
  78. address =
  79. HID_USAGE_SENSOR_HUMAN_PRESENCE;
  80. break;
  81. default:
  82. report_id = -1;
  83. break;
  84. }
  85. if (report_id >= 0) {
  86. hid_sensor_power_state(&prox_state->common_attributes,
  87. true);
  88. *val = sensor_hub_input_attr_get_raw_value(
  89. prox_state->common_attributes.hsdev,
  90. HID_USAGE_SENSOR_PROX, address,
  91. report_id,
  92. SENSOR_HUB_SYNC);
  93. hid_sensor_power_state(&prox_state->common_attributes,
  94. false);
  95. } else {
  96. *val = 0;
  97. return -EINVAL;
  98. }
  99. ret_type = IIO_VAL_INT;
  100. break;
  101. case IIO_CHAN_INFO_SCALE:
  102. *val = prox_state->prox_attr.units;
  103. ret_type = IIO_VAL_INT;
  104. break;
  105. case IIO_CHAN_INFO_OFFSET:
  106. *val = hid_sensor_convert_exponent(
  107. prox_state->prox_attr.unit_expo);
  108. ret_type = IIO_VAL_INT;
  109. break;
  110. case IIO_CHAN_INFO_SAMP_FREQ:
  111. ret_type = hid_sensor_read_samp_freq_value(
  112. &prox_state->common_attributes, val, val2);
  113. break;
  114. case IIO_CHAN_INFO_HYSTERESIS:
  115. ret_type = hid_sensor_read_raw_hyst_value(
  116. &prox_state->common_attributes, val, val2);
  117. break;
  118. default:
  119. ret_type = -EINVAL;
  120. break;
  121. }
  122. return ret_type;
  123. }
  124. /* Channel write_raw handler */
  125. static int prox_write_raw(struct iio_dev *indio_dev,
  126. struct iio_chan_spec const *chan,
  127. int val,
  128. int val2,
  129. long mask)
  130. {
  131. struct prox_state *prox_state = iio_priv(indio_dev);
  132. int ret = 0;
  133. switch (mask) {
  134. case IIO_CHAN_INFO_SAMP_FREQ:
  135. ret = hid_sensor_write_samp_freq_value(
  136. &prox_state->common_attributes, val, val2);
  137. break;
  138. case IIO_CHAN_INFO_HYSTERESIS:
  139. ret = hid_sensor_write_raw_hyst_value(
  140. &prox_state->common_attributes, val, val2);
  141. break;
  142. default:
  143. ret = -EINVAL;
  144. }
  145. return ret;
  146. }
  147. static const struct iio_info prox_info = {
  148. .driver_module = THIS_MODULE,
  149. .read_raw = &prox_read_raw,
  150. .write_raw = &prox_write_raw,
  151. };
  152. /* Function to push data to buffer */
  153. static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
  154. int len)
  155. {
  156. dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
  157. iio_push_to_buffers(indio_dev, data);
  158. }
  159. /* Callback handler to send event after all samples are received and captured */
  160. static int prox_proc_event(struct hid_sensor_hub_device *hsdev,
  161. unsigned usage_id,
  162. void *priv)
  163. {
  164. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  165. struct prox_state *prox_state = iio_priv(indio_dev);
  166. dev_dbg(&indio_dev->dev, "prox_proc_event\n");
  167. if (atomic_read(&prox_state->common_attributes.data_ready))
  168. hid_sensor_push_data(indio_dev,
  169. &prox_state->human_presence,
  170. sizeof(prox_state->human_presence));
  171. return 0;
  172. }
  173. /* Capture samples in local storage */
  174. static int prox_capture_sample(struct hid_sensor_hub_device *hsdev,
  175. unsigned usage_id,
  176. size_t raw_len, char *raw_data,
  177. void *priv)
  178. {
  179. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  180. struct prox_state *prox_state = iio_priv(indio_dev);
  181. int ret = -EINVAL;
  182. switch (usage_id) {
  183. case HID_USAGE_SENSOR_HUMAN_PRESENCE:
  184. prox_state->human_presence = *(u32 *)raw_data;
  185. ret = 0;
  186. break;
  187. default:
  188. break;
  189. }
  190. return ret;
  191. }
  192. /* Parse report which is specific to an usage id*/
  193. static int prox_parse_report(struct platform_device *pdev,
  194. struct hid_sensor_hub_device *hsdev,
  195. struct iio_chan_spec *channels,
  196. unsigned usage_id,
  197. struct prox_state *st)
  198. {
  199. int ret;
  200. ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
  201. usage_id,
  202. HID_USAGE_SENSOR_HUMAN_PRESENCE,
  203. &st->prox_attr);
  204. if (ret < 0)
  205. return ret;
  206. prox_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESENCE,
  207. st->prox_attr.size);
  208. dev_dbg(&pdev->dev, "prox %x:%x\n", st->prox_attr.index,
  209. st->prox_attr.report_id);
  210. /* Set Sensitivity field ids, when there is no individual modifier */
  211. if (st->common_attributes.sensitivity.index < 0) {
  212. sensor_hub_input_get_attribute_info(hsdev,
  213. HID_FEATURE_REPORT, usage_id,
  214. HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
  215. HID_USAGE_SENSOR_DATA_PRESENCE,
  216. &st->common_attributes.sensitivity);
  217. dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
  218. st->common_attributes.sensitivity.index,
  219. st->common_attributes.sensitivity.report_id);
  220. }
  221. return ret;
  222. }
  223. /* Function to initialize the processing for usage id */
  224. static int hid_prox_probe(struct platform_device *pdev)
  225. {
  226. int ret = 0;
  227. static const char *name = "prox";
  228. struct iio_dev *indio_dev;
  229. struct prox_state *prox_state;
  230. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  231. indio_dev = devm_iio_device_alloc(&pdev->dev,
  232. sizeof(struct prox_state));
  233. if (!indio_dev)
  234. return -ENOMEM;
  235. platform_set_drvdata(pdev, indio_dev);
  236. prox_state = iio_priv(indio_dev);
  237. prox_state->common_attributes.hsdev = hsdev;
  238. prox_state->common_attributes.pdev = pdev;
  239. ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_PROX,
  240. &prox_state->common_attributes);
  241. if (ret) {
  242. dev_err(&pdev->dev, "failed to setup common attributes\n");
  243. return ret;
  244. }
  245. indio_dev->channels = kmemdup(prox_channels, sizeof(prox_channels),
  246. GFP_KERNEL);
  247. if (!indio_dev->channels) {
  248. dev_err(&pdev->dev, "failed to duplicate channels\n");
  249. return -ENOMEM;
  250. }
  251. ret = prox_parse_report(pdev, hsdev,
  252. (struct iio_chan_spec *)indio_dev->channels,
  253. HID_USAGE_SENSOR_PROX, prox_state);
  254. if (ret) {
  255. dev_err(&pdev->dev, "failed to setup attributes\n");
  256. goto error_free_dev_mem;
  257. }
  258. indio_dev->num_channels = ARRAY_SIZE(prox_channels);
  259. indio_dev->dev.parent = &pdev->dev;
  260. indio_dev->info = &prox_info;
  261. indio_dev->name = name;
  262. indio_dev->modes = INDIO_DIRECT_MODE;
  263. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  264. NULL, NULL);
  265. if (ret) {
  266. dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  267. goto error_free_dev_mem;
  268. }
  269. atomic_set(&prox_state->common_attributes.data_ready, 0);
  270. ret = hid_sensor_setup_trigger(indio_dev, name,
  271. &prox_state->common_attributes);
  272. if (ret) {
  273. dev_err(&pdev->dev, "trigger setup failed\n");
  274. goto error_unreg_buffer_funcs;
  275. }
  276. ret = iio_device_register(indio_dev);
  277. if (ret) {
  278. dev_err(&pdev->dev, "device register failed\n");
  279. goto error_remove_trigger;
  280. }
  281. prox_state->callbacks.send_event = prox_proc_event;
  282. prox_state->callbacks.capture_sample = prox_capture_sample;
  283. prox_state->callbacks.pdev = pdev;
  284. ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_PROX,
  285. &prox_state->callbacks);
  286. if (ret < 0) {
  287. dev_err(&pdev->dev, "callback reg failed\n");
  288. goto error_iio_unreg;
  289. }
  290. return ret;
  291. error_iio_unreg:
  292. iio_device_unregister(indio_dev);
  293. error_remove_trigger:
  294. hid_sensor_remove_trigger(&prox_state->common_attributes);
  295. error_unreg_buffer_funcs:
  296. iio_triggered_buffer_cleanup(indio_dev);
  297. error_free_dev_mem:
  298. kfree(indio_dev->channels);
  299. return ret;
  300. }
  301. /* Function to deinitialize the processing for usage id */
  302. static int hid_prox_remove(struct platform_device *pdev)
  303. {
  304. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  305. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  306. struct prox_state *prox_state = iio_priv(indio_dev);
  307. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PROX);
  308. iio_device_unregister(indio_dev);
  309. hid_sensor_remove_trigger(&prox_state->common_attributes);
  310. iio_triggered_buffer_cleanup(indio_dev);
  311. kfree(indio_dev->channels);
  312. return 0;
  313. }
  314. static const struct platform_device_id hid_prox_ids[] = {
  315. {
  316. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  317. .name = "HID-SENSOR-200011",
  318. },
  319. { /* sentinel */ }
  320. };
  321. MODULE_DEVICE_TABLE(platform, hid_prox_ids);
  322. static struct platform_driver hid_prox_platform_driver = {
  323. .id_table = hid_prox_ids,
  324. .driver = {
  325. .name = KBUILD_MODNAME,
  326. .pm = &hid_sensor_pm_ops,
  327. },
  328. .probe = hid_prox_probe,
  329. .remove = hid_prox_remove,
  330. };
  331. module_platform_driver(hid_prox_platform_driver);
  332. MODULE_DESCRIPTION("HID Sensor Proximity");
  333. MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
  334. MODULE_LICENSE("GPL");