hid-sensor-prox.c 10 KB

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