hid-sensor-prox.c 10 KB

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