hid-sensor-prox.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HID Sensors Driver
  4. * Copyright (c) 2014, Intel Corporation.
  5. */
  6. #include <linux/device.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/module.h>
  9. #include <linux/mod_devicetable.h>
  10. #include <linux/slab.h>
  11. #include <linux/hid-sensor-hub.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/iio/buffer.h>
  14. #include "../common/hid-sensors/hid-sensor-trigger.h"
  15. #define CHANNEL_SCAN_INDEX_PRESENCE 0
  16. struct prox_state {
  17. struct hid_sensor_hub_callbacks callbacks;
  18. struct hid_sensor_common common_attributes;
  19. struct hid_sensor_hub_attribute_info prox_attr;
  20. u32 human_presence;
  21. int scale_pre_decml;
  22. int scale_post_decml;
  23. int scale_precision;
  24. };
  25. static const u32 prox_sensitivity_addresses[] = {
  26. HID_USAGE_SENSOR_HUMAN_PRESENCE,
  27. HID_USAGE_SENSOR_DATA_PRESENCE,
  28. };
  29. /* Channel definitions */
  30. static const struct iio_chan_spec prox_channels[] = {
  31. {
  32. .type = IIO_PROXIMITY,
  33. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  34. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
  35. BIT(IIO_CHAN_INFO_SCALE) |
  36. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  37. BIT(IIO_CHAN_INFO_HYSTERESIS),
  38. .scan_index = CHANNEL_SCAN_INDEX_PRESENCE,
  39. }
  40. };
  41. /* Adjust channel real bits based on report descriptor */
  42. static void prox_adjust_channel_bit_mask(struct iio_chan_spec *channels,
  43. int channel, int size)
  44. {
  45. channels[channel].scan_type.sign = 's';
  46. /* Real storage bits will change based on the report desc. */
  47. channels[channel].scan_type.realbits = size * 8;
  48. /* Maximum size of a sample to capture is u32 */
  49. channels[channel].scan_type.storagebits = sizeof(u32) * 8;
  50. }
  51. /* Channel read_raw handler */
  52. static int prox_read_raw(struct iio_dev *indio_dev,
  53. struct iio_chan_spec const *chan,
  54. int *val, int *val2,
  55. long mask)
  56. {
  57. struct prox_state *prox_state = iio_priv(indio_dev);
  58. int report_id = -1;
  59. u32 address;
  60. int ret_type;
  61. s32 min;
  62. *val = 0;
  63. *val2 = 0;
  64. switch (mask) {
  65. case IIO_CHAN_INFO_RAW:
  66. switch (chan->scan_index) {
  67. case CHANNEL_SCAN_INDEX_PRESENCE:
  68. report_id = prox_state->prox_attr.report_id;
  69. min = prox_state->prox_attr.logical_minimum;
  70. address = HID_USAGE_SENSOR_HUMAN_PRESENCE;
  71. break;
  72. default:
  73. report_id = -1;
  74. break;
  75. }
  76. if (report_id >= 0) {
  77. hid_sensor_power_state(&prox_state->common_attributes,
  78. true);
  79. *val = sensor_hub_input_attr_get_raw_value(
  80. prox_state->common_attributes.hsdev,
  81. HID_USAGE_SENSOR_PROX, address,
  82. report_id,
  83. SENSOR_HUB_SYNC,
  84. min < 0);
  85. hid_sensor_power_state(&prox_state->common_attributes,
  86. false);
  87. } else {
  88. *val = 0;
  89. return -EINVAL;
  90. }
  91. ret_type = IIO_VAL_INT;
  92. break;
  93. case IIO_CHAN_INFO_SCALE:
  94. *val = prox_state->scale_pre_decml;
  95. *val2 = prox_state->scale_post_decml;
  96. ret_type = prox_state->scale_precision;
  97. break;
  98. case IIO_CHAN_INFO_OFFSET:
  99. *val = hid_sensor_convert_exponent(
  100. prox_state->prox_attr.unit_expo);
  101. ret_type = IIO_VAL_INT;
  102. break;
  103. case IIO_CHAN_INFO_SAMP_FREQ:
  104. ret_type = hid_sensor_read_samp_freq_value(
  105. &prox_state->common_attributes, val, val2);
  106. break;
  107. case IIO_CHAN_INFO_HYSTERESIS:
  108. ret_type = hid_sensor_read_raw_hyst_value(
  109. &prox_state->common_attributes, val, val2);
  110. break;
  111. default:
  112. ret_type = -EINVAL;
  113. break;
  114. }
  115. return ret_type;
  116. }
  117. /* Channel write_raw handler */
  118. static int prox_write_raw(struct iio_dev *indio_dev,
  119. struct iio_chan_spec const *chan,
  120. int val,
  121. int val2,
  122. long mask)
  123. {
  124. struct prox_state *prox_state = iio_priv(indio_dev);
  125. int ret = 0;
  126. switch (mask) {
  127. case IIO_CHAN_INFO_SAMP_FREQ:
  128. ret = hid_sensor_write_samp_freq_value(
  129. &prox_state->common_attributes, val, val2);
  130. break;
  131. case IIO_CHAN_INFO_HYSTERESIS:
  132. ret = hid_sensor_write_raw_hyst_value(
  133. &prox_state->common_attributes, val, val2);
  134. break;
  135. default:
  136. ret = -EINVAL;
  137. }
  138. return ret;
  139. }
  140. static const struct iio_info prox_info = {
  141. .read_raw = &prox_read_raw,
  142. .write_raw = &prox_write_raw,
  143. };
  144. /* Function to push data to buffer */
  145. static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
  146. int len)
  147. {
  148. dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
  149. iio_push_to_buffers(indio_dev, data);
  150. }
  151. /* Callback handler to send event after all samples are received and captured */
  152. static int prox_proc_event(struct hid_sensor_hub_device *hsdev,
  153. unsigned usage_id,
  154. void *priv)
  155. {
  156. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  157. struct prox_state *prox_state = iio_priv(indio_dev);
  158. dev_dbg(&indio_dev->dev, "prox_proc_event\n");
  159. if (atomic_read(&prox_state->common_attributes.data_ready))
  160. hid_sensor_push_data(indio_dev,
  161. &prox_state->human_presence,
  162. sizeof(prox_state->human_presence));
  163. return 0;
  164. }
  165. /* Capture samples in local storage */
  166. static int prox_capture_sample(struct hid_sensor_hub_device *hsdev,
  167. unsigned usage_id,
  168. size_t raw_len, char *raw_data,
  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. int ret = -EINVAL;
  174. switch (usage_id) {
  175. case HID_USAGE_SENSOR_HUMAN_PRESENCE:
  176. prox_state->human_presence = *(u32 *)raw_data;
  177. ret = 0;
  178. break;
  179. default:
  180. break;
  181. }
  182. return ret;
  183. }
  184. /* Parse report which is specific to an usage id*/
  185. static int prox_parse_report(struct platform_device *pdev,
  186. struct hid_sensor_hub_device *hsdev,
  187. struct iio_chan_spec *channels,
  188. unsigned usage_id,
  189. struct prox_state *st)
  190. {
  191. int ret;
  192. ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
  193. usage_id,
  194. HID_USAGE_SENSOR_HUMAN_PRESENCE,
  195. &st->prox_attr);
  196. if (ret < 0)
  197. return ret;
  198. prox_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESENCE,
  199. st->prox_attr.size);
  200. dev_dbg(&pdev->dev, "prox %x:%x\n", st->prox_attr.index,
  201. st->prox_attr.report_id);
  202. return ret;
  203. }
  204. /* Function to initialize the processing for usage id */
  205. static int hid_prox_probe(struct platform_device *pdev)
  206. {
  207. int ret = 0;
  208. static const char *name = "prox";
  209. struct iio_dev *indio_dev;
  210. struct prox_state *prox_state;
  211. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  212. indio_dev = devm_iio_device_alloc(&pdev->dev,
  213. sizeof(struct prox_state));
  214. if (!indio_dev)
  215. return -ENOMEM;
  216. platform_set_drvdata(pdev, indio_dev);
  217. prox_state = iio_priv(indio_dev);
  218. prox_state->common_attributes.hsdev = hsdev;
  219. prox_state->common_attributes.pdev = pdev;
  220. ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_PROX,
  221. &prox_state->common_attributes,
  222. prox_sensitivity_addresses,
  223. ARRAY_SIZE(prox_sensitivity_addresses));
  224. if (ret) {
  225. dev_err(&pdev->dev, "failed to setup common attributes\n");
  226. return ret;
  227. }
  228. indio_dev->channels = devm_kmemdup(&pdev->dev, prox_channels,
  229. sizeof(prox_channels), GFP_KERNEL);
  230. if (!indio_dev->channels) {
  231. dev_err(&pdev->dev, "failed to duplicate channels\n");
  232. return -ENOMEM;
  233. }
  234. ret = prox_parse_report(pdev, hsdev,
  235. (struct iio_chan_spec *)indio_dev->channels,
  236. HID_USAGE_SENSOR_PROX, prox_state);
  237. if (ret) {
  238. dev_err(&pdev->dev, "failed to setup attributes\n");
  239. return ret;
  240. }
  241. indio_dev->num_channels = ARRAY_SIZE(prox_channels);
  242. indio_dev->info = &prox_info;
  243. indio_dev->name = name;
  244. indio_dev->modes = INDIO_DIRECT_MODE;
  245. atomic_set(&prox_state->common_attributes.data_ready, 0);
  246. ret = hid_sensor_setup_trigger(indio_dev, name,
  247. &prox_state->common_attributes);
  248. if (ret) {
  249. dev_err(&pdev->dev, "trigger setup failed\n");
  250. return ret;
  251. }
  252. ret = iio_device_register(indio_dev);
  253. if (ret) {
  254. dev_err(&pdev->dev, "device register failed\n");
  255. goto error_remove_trigger;
  256. }
  257. prox_state->callbacks.send_event = prox_proc_event;
  258. prox_state->callbacks.capture_sample = prox_capture_sample;
  259. prox_state->callbacks.pdev = pdev;
  260. ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_PROX,
  261. &prox_state->callbacks);
  262. if (ret < 0) {
  263. dev_err(&pdev->dev, "callback reg failed\n");
  264. goto error_iio_unreg;
  265. }
  266. return ret;
  267. error_iio_unreg:
  268. iio_device_unregister(indio_dev);
  269. error_remove_trigger:
  270. hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
  271. return ret;
  272. }
  273. /* Function to deinitialize the processing for usage id */
  274. static int hid_prox_remove(struct platform_device *pdev)
  275. {
  276. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  277. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  278. struct prox_state *prox_state = iio_priv(indio_dev);
  279. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PROX);
  280. iio_device_unregister(indio_dev);
  281. hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
  282. return 0;
  283. }
  284. static const struct platform_device_id hid_prox_ids[] = {
  285. {
  286. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  287. .name = "HID-SENSOR-200011",
  288. },
  289. { /* sentinel */ }
  290. };
  291. MODULE_DEVICE_TABLE(platform, hid_prox_ids);
  292. static struct platform_driver hid_prox_platform_driver = {
  293. .id_table = hid_prox_ids,
  294. .driver = {
  295. .name = KBUILD_MODNAME,
  296. .pm = &hid_sensor_pm_ops,
  297. },
  298. .probe = hid_prox_probe,
  299. .remove = hid_prox_remove,
  300. };
  301. module_platform_driver(hid_prox_platform_driver);
  302. MODULE_DESCRIPTION("HID Sensor Proximity");
  303. MODULE_AUTHOR("Archana Patni <[email protected]>");
  304. MODULE_LICENSE("GPL");
  305. MODULE_IMPORT_NS(IIO_HID);