hid-sensor-prox.c 9.3 KB

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