hid-sensor-prox.c 10 KB

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