hid-sensor-prox.c 11 KB

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