hid-sensor-prox.c 9.2 KB

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