hid-sensor-prox.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. static const u32 prox_usage_ids[] = {
  16. HID_USAGE_SENSOR_HUMAN_PRESENCE,
  17. HID_USAGE_SENSOR_HUMAN_PROXIMITY,
  18. HID_USAGE_SENSOR_HUMAN_ATTENTION,
  19. };
  20. #define MAX_CHANNELS ARRAY_SIZE(prox_usage_ids)
  21. enum {
  22. HID_HUMAN_PRESENCE,
  23. HID_HUMAN_PROXIMITY,
  24. HID_HUMAN_ATTENTION,
  25. };
  26. struct prox_state {
  27. struct hid_sensor_hub_callbacks callbacks;
  28. struct hid_sensor_common common_attributes;
  29. struct hid_sensor_hub_attribute_info prox_attr[MAX_CHANNELS];
  30. struct iio_chan_spec channels[MAX_CHANNELS];
  31. u32 channel2usage[MAX_CHANNELS];
  32. u32 human_presence[MAX_CHANNELS];
  33. int scale_pre_decml[MAX_CHANNELS];
  34. int scale_post_decml[MAX_CHANNELS];
  35. int scale_precision[MAX_CHANNELS];
  36. unsigned long scan_mask[2]; /* One entry plus one terminator. */
  37. int num_channels;
  38. };
  39. static const u32 prox_sensitivity_addresses[] = {
  40. HID_USAGE_SENSOR_HUMAN_PRESENCE,
  41. HID_USAGE_SENSOR_DATA_PRESENCE,
  42. };
  43. #define PROX_CHANNEL(_is_proximity, _channel) \
  44. {\
  45. .type = _is_proximity ? IIO_PROXIMITY : IIO_ATTENTION,\
  46. .info_mask_separate = \
  47. (_is_proximity ? BIT(IIO_CHAN_INFO_RAW) :\
  48. BIT(IIO_CHAN_INFO_PROCESSED)) |\
  49. BIT(IIO_CHAN_INFO_OFFSET) |\
  50. BIT(IIO_CHAN_INFO_SCALE) |\
  51. BIT(IIO_CHAN_INFO_SAMP_FREQ) |\
  52. BIT(IIO_CHAN_INFO_HYSTERESIS),\
  53. .indexed = _is_proximity,\
  54. .channel = _channel,\
  55. }
  56. /* Channel definitions (same order as prox_usage_ids) */
  57. static const struct iio_chan_spec prox_channels[] = {
  58. PROX_CHANNEL(true, HID_HUMAN_PRESENCE),
  59. PROX_CHANNEL(true, HID_HUMAN_PROXIMITY),
  60. PROX_CHANNEL(false, 0),
  61. };
  62. /* Adjust channel real bits based on report descriptor */
  63. static void prox_adjust_channel_bit_mask(struct iio_chan_spec *channels,
  64. int channel, int size)
  65. {
  66. channels[channel].scan_type.sign = 's';
  67. /* Real storage bits will change based on the report desc. */
  68. channels[channel].scan_type.realbits = size * 8;
  69. /* Maximum size of a sample to capture is u32 */
  70. channels[channel].scan_type.storagebits = sizeof(u32) * 8;
  71. }
  72. /* Channel read_raw handler */
  73. static int prox_read_raw(struct iio_dev *indio_dev,
  74. struct iio_chan_spec const *chan,
  75. int *val, int *val2,
  76. long mask)
  77. {
  78. struct prox_state *prox_state = iio_priv(indio_dev);
  79. struct hid_sensor_hub_device *hsdev;
  80. int report_id;
  81. u32 address;
  82. int ret_type;
  83. s32 min;
  84. *val = 0;
  85. *val2 = 0;
  86. switch (mask) {
  87. case IIO_CHAN_INFO_RAW:
  88. case IIO_CHAN_INFO_PROCESSED:
  89. if (chan->scan_index >= prox_state->num_channels)
  90. return -EINVAL;
  91. address = prox_state->channel2usage[chan->scan_index];
  92. report_id = prox_state->prox_attr[chan->scan_index].report_id;
  93. hsdev = prox_state->common_attributes.hsdev;
  94. min = prox_state->prox_attr[chan->scan_index].logical_minimum;
  95. hid_sensor_power_state(&prox_state->common_attributes, true);
  96. *val = sensor_hub_input_attr_get_raw_value(hsdev,
  97. hsdev->usage,
  98. address,
  99. report_id,
  100. SENSOR_HUB_SYNC,
  101. min < 0);
  102. if (prox_state->channel2usage[chan->scan_index] ==
  103. HID_USAGE_SENSOR_HUMAN_ATTENTION)
  104. *val *= 100;
  105. hid_sensor_power_state(&prox_state->common_attributes, false);
  106. ret_type = IIO_VAL_INT;
  107. break;
  108. case IIO_CHAN_INFO_SCALE:
  109. if (chan->scan_index >= prox_state->num_channels)
  110. return -EINVAL;
  111. *val = prox_state->scale_pre_decml[chan->scan_index];
  112. *val2 = prox_state->scale_post_decml[chan->scan_index];
  113. ret_type = prox_state->scale_precision[chan->scan_index];
  114. break;
  115. case IIO_CHAN_INFO_OFFSET:
  116. *val = 0;
  117. ret_type = IIO_VAL_INT;
  118. break;
  119. case IIO_CHAN_INFO_SAMP_FREQ:
  120. ret_type = hid_sensor_read_samp_freq_value(
  121. &prox_state->common_attributes, val, val2);
  122. break;
  123. case IIO_CHAN_INFO_HYSTERESIS:
  124. ret_type = hid_sensor_read_raw_hyst_value(
  125. &prox_state->common_attributes, val, val2);
  126. break;
  127. default:
  128. ret_type = -EINVAL;
  129. break;
  130. }
  131. return ret_type;
  132. }
  133. /* Channel write_raw handler */
  134. static int prox_write_raw(struct iio_dev *indio_dev,
  135. struct iio_chan_spec const *chan,
  136. int val,
  137. int val2,
  138. long mask)
  139. {
  140. struct prox_state *prox_state = iio_priv(indio_dev);
  141. int ret = 0;
  142. switch (mask) {
  143. case IIO_CHAN_INFO_SAMP_FREQ:
  144. ret = hid_sensor_write_samp_freq_value(
  145. &prox_state->common_attributes, val, val2);
  146. break;
  147. case IIO_CHAN_INFO_HYSTERESIS:
  148. ret = hid_sensor_write_raw_hyst_value(
  149. &prox_state->common_attributes, val, val2);
  150. break;
  151. default:
  152. ret = -EINVAL;
  153. }
  154. return ret;
  155. }
  156. static const struct iio_info prox_info = {
  157. .read_raw = &prox_read_raw,
  158. .write_raw = &prox_write_raw,
  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. dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
  170. iio_push_to_buffers(indio_dev, &prox_state->human_presence);
  171. }
  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 multiplier = 1;
  183. int chan;
  184. for (chan = 0; chan < prox_state->num_channels; chan++)
  185. if (prox_state->channel2usage[chan] == usage_id)
  186. break;
  187. if (chan == prox_state->num_channels)
  188. return -EINVAL;
  189. if (usage_id == HID_USAGE_SENSOR_HUMAN_ATTENTION)
  190. multiplier = 100;
  191. switch (raw_len) {
  192. case 1:
  193. prox_state->human_presence[chan] = *(u8 *)raw_data * multiplier;
  194. return 0;
  195. case 2:
  196. prox_state->human_presence[chan] = *(u16 *)raw_data * multiplier;
  197. return 0;
  198. case 4:
  199. prox_state->human_presence[chan] = *(u32 *)raw_data * multiplier;
  200. return 0;
  201. }
  202. return -EINVAL;
  203. }
  204. /* Parse report which is specific to an usage id*/
  205. static int prox_parse_report(struct platform_device *pdev,
  206. struct hid_sensor_hub_device *hsdev,
  207. struct prox_state *st)
  208. {
  209. struct iio_chan_spec *channels = st->channels;
  210. int index = 0;
  211. int ret;
  212. int i;
  213. for (i = 0; i < MAX_CHANNELS; i++) {
  214. u32 usage_id = prox_usage_ids[i];
  215. ret = sensor_hub_input_get_attribute_info(hsdev,
  216. HID_INPUT_REPORT,
  217. hsdev->usage,
  218. usage_id,
  219. &st->prox_attr[index]);
  220. if (ret < 0)
  221. continue;
  222. st->channel2usage[index] = usage_id;
  223. st->scan_mask[0] |= BIT(index);
  224. channels[index] = prox_channels[i];
  225. channels[index].scan_index = index;
  226. prox_adjust_channel_bit_mask(channels, index,
  227. st->prox_attr[index].size);
  228. dev_dbg(&pdev->dev, "prox %x:%x\n", st->prox_attr[index].index,
  229. st->prox_attr[index].report_id);
  230. st->scale_precision[index] =
  231. hid_sensor_format_scale(usage_id, &st->prox_attr[index],
  232. &st->scale_pre_decml[index],
  233. &st->scale_post_decml[index]);
  234. index++;
  235. }
  236. if (!index)
  237. return ret;
  238. st->num_channels = index;
  239. return 0;
  240. }
  241. /* Function to initialize the processing for usage id */
  242. static int hid_prox_probe(struct platform_device *pdev)
  243. {
  244. struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
  245. int ret = 0;
  246. static const char *name = "prox";
  247. struct iio_dev *indio_dev;
  248. struct prox_state *prox_state;
  249. indio_dev = devm_iio_device_alloc(&pdev->dev,
  250. sizeof(struct prox_state));
  251. if (!indio_dev)
  252. return -ENOMEM;
  253. platform_set_drvdata(pdev, indio_dev);
  254. prox_state = iio_priv(indio_dev);
  255. prox_state->common_attributes.hsdev = hsdev;
  256. prox_state->common_attributes.pdev = pdev;
  257. ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage,
  258. &prox_state->common_attributes,
  259. prox_sensitivity_addresses,
  260. ARRAY_SIZE(prox_sensitivity_addresses));
  261. if (ret) {
  262. dev_err(&pdev->dev, "failed to setup common attributes\n");
  263. return ret;
  264. }
  265. ret = prox_parse_report(pdev, hsdev, prox_state);
  266. if (ret) {
  267. dev_err(&pdev->dev, "failed to setup attributes\n");
  268. return ret;
  269. }
  270. indio_dev->num_channels = prox_state->num_channels;
  271. indio_dev->channels = prox_state->channels;
  272. indio_dev->available_scan_masks = prox_state->scan_mask;
  273. indio_dev->info = &prox_info;
  274. indio_dev->name = name;
  275. indio_dev->modes = INDIO_DIRECT_MODE;
  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. return ret;
  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, hsdev->usage,
  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(indio_dev, &prox_state->common_attributes);
  302. return ret;
  303. }
  304. /* Function to deinitialize the processing for usage id */
  305. static void hid_prox_remove(struct platform_device *pdev)
  306. {
  307. struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
  308. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  309. struct prox_state *prox_state = iio_priv(indio_dev);
  310. sensor_hub_remove_callback(hsdev, hsdev->usage);
  311. iio_device_unregister(indio_dev);
  312. hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes);
  313. }
  314. static const struct platform_device_id hid_prox_ids[] = {
  315. {
  316. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  317. .name = "HID-SENSOR-200011",
  318. },
  319. {
  320. /* Format: HID-SENSOR-tag-usage_id_in_hex_lowercase */
  321. .name = "HID-SENSOR-LISS-0226",
  322. },
  323. { }
  324. };
  325. MODULE_DEVICE_TABLE(platform, hid_prox_ids);
  326. static struct platform_driver hid_prox_platform_driver = {
  327. .id_table = hid_prox_ids,
  328. .driver = {
  329. .name = KBUILD_MODNAME,
  330. .pm = &hid_sensor_pm_ops,
  331. },
  332. .probe = hid_prox_probe,
  333. .remove = hid_prox_remove,
  334. };
  335. module_platform_driver(hid_prox_platform_driver);
  336. MODULE_DESCRIPTION("HID Sensor Proximity");
  337. MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
  338. MODULE_LICENSE("GPL");
  339. MODULE_IMPORT_NS("IIO_HID");