button-adc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. * Author: Marek Szyprowski <m.szyprowski@samsung.com>
  6. */
  7. #include <common.h>
  8. #include <adc.h>
  9. #include <button.h>
  10. #include <log.h>
  11. #include <dm.h>
  12. #include <dm/lists.h>
  13. #include <dm/of_access.h>
  14. #include <dm/uclass-internal.h>
  15. /**
  16. * struct button_adc_priv - private data for button-adc driver.
  17. *
  18. * @adc: Analog to Digital Converter device to which button is connected.
  19. * @channel: channel of the ADC device to probe the button state.
  20. * @min: minimal uV value to consider button as pressed.
  21. * @max: maximal uV value to consider button as pressed.
  22. */
  23. struct button_adc_priv {
  24. struct udevice *adc;
  25. int channel;
  26. int min;
  27. int max;
  28. };
  29. static enum button_state_t button_adc_get_state(struct udevice *dev)
  30. {
  31. struct button_adc_priv *priv = dev_get_priv(dev);
  32. unsigned int val;
  33. int ret, uV;
  34. ret = adc_start_channel(priv->adc, priv->channel);
  35. if (ret)
  36. return ret;
  37. ret = adc_channel_data(priv->adc, priv->channel, &val);
  38. if (ret)
  39. return ret;
  40. ret = adc_raw_to_uV(priv->adc, val, &uV);
  41. if (ret)
  42. return ret;
  43. return (uV >= priv->min && uV < priv->max) ? BUTTON_ON : BUTTON_OFF;
  44. }
  45. static int button_adc_of_to_plat(struct udevice *dev)
  46. {
  47. struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev);
  48. struct button_adc_priv *priv = dev_get_priv(dev);
  49. struct ofnode_phandle_args args;
  50. u32 threshold, up_threshold, t;
  51. ofnode node;
  52. int ret;
  53. /* Ignore the top-level button node */
  54. if (!uc_plat->label)
  55. return 0;
  56. ret = dev_read_phandle_with_args(dev->parent, "io-channels",
  57. "#io-channel-cells", 0, 0, &args);
  58. if (ret)
  59. return ret;
  60. ret = uclass_get_device_by_ofnode(UCLASS_ADC, args.node, &priv->adc);
  61. if (ret)
  62. return ret;
  63. ret = ofnode_read_u32(dev_ofnode(dev->parent),
  64. "keyup-threshold-microvolt", &up_threshold);
  65. if (ret)
  66. return ret;
  67. ret = ofnode_read_u32(dev_ofnode(dev), "press-threshold-microvolt",
  68. &threshold);
  69. if (ret)
  70. return ret;
  71. dev_for_each_subnode(node, dev->parent) {
  72. ret = ofnode_read_u32(node, "press-threshold-microvolt", &t);
  73. if (ret)
  74. return ret;
  75. if (t > threshold)
  76. up_threshold = t;
  77. }
  78. priv->channel = args.args[0];
  79. priv->min = threshold;
  80. priv->max = up_threshold;
  81. return ret;
  82. }
  83. static int button_adc_bind(struct udevice *parent)
  84. {
  85. struct udevice *dev;
  86. ofnode node;
  87. int ret;
  88. dev_for_each_subnode(node, parent) {
  89. struct button_uc_plat *uc_plat;
  90. const char *label;
  91. label = ofnode_read_string(node, "label");
  92. if (!label) {
  93. debug("%s: node %s has no label\n", __func__,
  94. ofnode_get_name(node));
  95. return -EINVAL;
  96. }
  97. ret = device_bind_driver_to_node(parent, "button_adc",
  98. ofnode_get_name(node),
  99. node, &dev);
  100. if (ret)
  101. return ret;
  102. uc_plat = dev_get_uclass_plat(dev);
  103. uc_plat->label = label;
  104. }
  105. return 0;
  106. }
  107. static const struct button_ops button_adc_ops = {
  108. .get_state = button_adc_get_state,
  109. };
  110. static const struct udevice_id button_adc_ids[] = {
  111. { .compatible = "adc-keys" },
  112. { }
  113. };
  114. U_BOOT_DRIVER(button_adc) = {
  115. .name = "button_adc",
  116. .id = UCLASS_BUTTON,
  117. .of_match = button_adc_ids,
  118. .ops = &button_adc_ops,
  119. .priv_auto = sizeof(struct button_adc_priv),
  120. .bind = button_adc_bind,
  121. .of_to_plat = button_adc_of_to_plat,
  122. };