hid-sensor-prox.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. .read_raw = &prox_read_raw,
  149. .write_raw = &prox_write_raw,
  150. };
  151. /* Function to push data to buffer */
  152. static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
  153. int len)
  154. {
  155. dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
  156. iio_push_to_buffers(indio_dev, data);
  157. }
  158. /* Callback handler to send event after all samples are received and captured */
  159. static int prox_proc_event(struct hid_sensor_hub_device *hsdev,
  160. unsigned usage_id,
  161. void *priv)
  162. {
  163. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  164. struct prox_state *prox_state = iio_priv(indio_dev);
  165. dev_dbg(&indio_dev->dev, "prox_proc_event\n");
  166. if (atomic_read(&prox_state->common_attributes.data_ready))
  167. hid_sensor_push_data(indio_dev,
  168. &prox_state->human_presence,
  169. sizeof(prox_state->human_presence));
  170. return 0;
  171. }
  172. /* Capture samples in local storage */
  173. static int prox_capture_sample(struct hid_sensor_hub_device *hsdev,
  174. unsigned usage_id,
  175. size_t raw_len, char *raw_data,
  176. void *priv)
  177. {
  178. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  179. struct prox_state *prox_state = iio_priv(indio_dev);
  180. int ret = -EINVAL;
  181. switch (usage_id) {
  182. case HID_USAGE_SENSOR_HUMAN_PRESENCE:
  183. prox_state->human_presence = *(u32 *)raw_data;
  184. ret = 0;
  185. break;
  186. default:
  187. break;
  188. }
  189. return ret;
  190. }
  191. /* Parse report which is specific to an usage id*/
  192. static int prox_parse_report(struct platform_device *pdev,
  193. struct hid_sensor_hub_device *hsdev,
  194. struct iio_chan_spec *channels,
  195. unsigned usage_id,
  196. struct prox_state *st)
  197. {
  198. int ret;
  199. ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
  200. usage_id,
  201. HID_USAGE_SENSOR_HUMAN_PRESENCE,
  202. &st->prox_attr);
  203. if (ret < 0)
  204. return ret;
  205. prox_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESENCE,
  206. st->prox_attr.size);
  207. dev_dbg(&pdev->dev, "prox %x:%x\n", st->prox_attr.index,
  208. st->prox_attr.report_id);
  209. /* Set Sensitivity field ids, when there is no individual modifier */
  210. if (st->common_attributes.sensitivity.index < 0) {
  211. sensor_hub_input_get_attribute_info(hsdev,
  212. HID_FEATURE_REPORT, usage_id,
  213. HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
  214. HID_USAGE_SENSOR_DATA_PRESENCE,
  215. &st->common_attributes.sensitivity);
  216. dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
  217. st->common_attributes.sensitivity.index,
  218. st->common_attributes.sensitivity.report_id);
  219. }
  220. if (st->common_attributes.sensitivity.index < 0)
  221. sensor_hub_input_get_attribute_info(hsdev,
  222. HID_FEATURE_REPORT, usage_id,
  223. HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
  224. HID_USAGE_SENSOR_HUMAN_PRESENCE,
  225. &st->common_attributes.sensitivity);
  226. return ret;
  227. }
  228. /* Function to initialize the processing for usage id */
  229. static int hid_prox_probe(struct platform_device *pdev)
  230. {
  231. int ret = 0;
  232. static const char *name = "prox";
  233. struct iio_dev *indio_dev;
  234. struct prox_state *prox_state;
  235. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  236. indio_dev = devm_iio_device_alloc(&pdev->dev,
  237. sizeof(struct prox_state));
  238. if (!indio_dev)
  239. return -ENOMEM;
  240. platform_set_drvdata(pdev, indio_dev);
  241. prox_state = iio_priv(indio_dev);
  242. prox_state->common_attributes.hsdev = hsdev;
  243. prox_state->common_attributes.pdev = pdev;
  244. ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_PROX,
  245. &prox_state->common_attributes);
  246. if (ret) {
  247. dev_err(&pdev->dev, "failed to setup common attributes\n");
  248. return ret;
  249. }
  250. indio_dev->channels = kmemdup(prox_channels, sizeof(prox_channels),
  251. GFP_KERNEL);
  252. if (!indio_dev->channels) {
  253. dev_err(&pdev->dev, "failed to duplicate channels\n");
  254. return -ENOMEM;
  255. }
  256. ret = prox_parse_report(pdev, hsdev,
  257. (struct iio_chan_spec *)indio_dev->channels,
  258. HID_USAGE_SENSOR_PROX, prox_state);
  259. if (ret) {
  260. dev_err(&pdev->dev, "failed to setup attributes\n");
  261. goto error_free_dev_mem;
  262. }
  263. indio_dev->num_channels = ARRAY_SIZE(prox_channels);
  264. indio_dev->dev.parent = &pdev->dev;
  265. indio_dev->info = &prox_info;
  266. indio_dev->name = name;
  267. indio_dev->modes = INDIO_DIRECT_MODE;
  268. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  269. NULL, NULL);
  270. if (ret) {
  271. dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  272. goto error_free_dev_mem;
  273. }
  274. atomic_set(&prox_state->common_attributes.data_ready, 0);
  275. ret = hid_sensor_setup_trigger(indio_dev, name,
  276. &prox_state->common_attributes);
  277. if (ret) {
  278. dev_err(&pdev->dev, "trigger setup failed\n");
  279. goto error_unreg_buffer_funcs;
  280. }
  281. ret = iio_device_register(indio_dev);
  282. if (ret) {
  283. dev_err(&pdev->dev, "device register failed\n");
  284. goto error_remove_trigger;
  285. }
  286. prox_state->callbacks.send_event = prox_proc_event;
  287. prox_state->callbacks.capture_sample = prox_capture_sample;
  288. prox_state->callbacks.pdev = pdev;
  289. ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_PROX,
  290. &prox_state->callbacks);
  291. if (ret < 0) {
  292. dev_err(&pdev->dev, "callback reg failed\n");
  293. goto error_iio_unreg;
  294. }
  295. return ret;
  296. error_iio_unreg:
  297. iio_device_unregister(indio_dev);
  298. error_remove_trigger:
  299. hid_sensor_remove_trigger(&prox_state->common_attributes);
  300. error_unreg_buffer_funcs:
  301. iio_triggered_buffer_cleanup(indio_dev);
  302. error_free_dev_mem:
  303. kfree(indio_dev->channels);
  304. return ret;
  305. }
  306. /* Function to deinitialize the processing for usage id */
  307. static int hid_prox_remove(struct platform_device *pdev)
  308. {
  309. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  310. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  311. struct prox_state *prox_state = iio_priv(indio_dev);
  312. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PROX);
  313. iio_device_unregister(indio_dev);
  314. hid_sensor_remove_trigger(&prox_state->common_attributes);
  315. iio_triggered_buffer_cleanup(indio_dev);
  316. kfree(indio_dev->channels);
  317. return 0;
  318. }
  319. static const struct platform_device_id hid_prox_ids[] = {
  320. {
  321. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  322. .name = "HID-SENSOR-200011",
  323. },
  324. { /* sentinel */ }
  325. };
  326. MODULE_DEVICE_TABLE(platform, hid_prox_ids);
  327. static struct platform_driver hid_prox_platform_driver = {
  328. .id_table = hid_prox_ids,
  329. .driver = {
  330. .name = KBUILD_MODNAME,
  331. .pm = &hid_sensor_pm_ops,
  332. },
  333. .probe = hid_prox_probe,
  334. .remove = hid_prox_remove,
  335. };
  336. module_platform_driver(hid_prox_platform_driver);
  337. MODULE_DESCRIPTION("HID Sensor Proximity");
  338. MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
  339. MODULE_LICENSE("GPL");