as3722_gpio.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2014 NVIDIA Corporation
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <asm/gpio.h>
  9. #include <power/as3722.h>
  10. #include <power/pmic.h>
  11. #define NUM_GPIOS 8
  12. int as3722_gpio_configure(struct udevice *pmic, unsigned int gpio,
  13. unsigned long flags)
  14. {
  15. u8 value = 0;
  16. int err;
  17. if (flags & AS3722_GPIO_OUTPUT_VDDH)
  18. value |= AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDH;
  19. if (flags & AS3722_GPIO_INVERT)
  20. value |= AS3722_GPIO_CONTROL_INVERT;
  21. err = pmic_reg_write(pmic, AS3722_GPIO_CONTROL(gpio), value);
  22. if (err) {
  23. pr_err("failed to configure GPIO#%u: %d", gpio, err);
  24. return err;
  25. }
  26. return 0;
  27. }
  28. static int as3722_gpio_set_value(struct udevice *dev, unsigned int gpio,
  29. int level)
  30. {
  31. struct udevice *pmic = dev_get_parent(dev);
  32. const char *l;
  33. u8 value;
  34. int err;
  35. if (gpio >= NUM_GPIOS)
  36. return -EINVAL;
  37. err = pmic_reg_read(pmic, AS3722_GPIO_SIGNAL_OUT);
  38. if (err < 0) {
  39. pr_err("failed to read GPIO signal out register: %d", err);
  40. return err;
  41. }
  42. value = err;
  43. if (level == 0) {
  44. value &= ~(1 << gpio);
  45. l = "low";
  46. } else {
  47. value |= 1 << gpio;
  48. l = "high";
  49. }
  50. err = pmic_reg_write(pmic, AS3722_GPIO_SIGNAL_OUT, value);
  51. if (err) {
  52. pr_err("failed to set GPIO#%u %s: %d", gpio, l, err);
  53. return err;
  54. }
  55. return 0;
  56. }
  57. int as3722_gpio_direction_output(struct udevice *dev, unsigned int gpio,
  58. int value)
  59. {
  60. struct udevice *pmic = dev_get_parent(dev);
  61. int err;
  62. if (gpio > 7)
  63. return -EINVAL;
  64. if (value == 0)
  65. value = AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDL;
  66. else
  67. value = AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDH;
  68. err = pmic_reg_write(pmic, AS3722_GPIO_CONTROL(gpio), value);
  69. if (err) {
  70. pr_err("failed to configure GPIO#%u as output: %d", gpio, err);
  71. return err;
  72. }
  73. err = as3722_gpio_set_value(pmic, gpio, value);
  74. if (err < 0) {
  75. pr_err("failed to set GPIO#%u high: %d", gpio, err);
  76. return err;
  77. }
  78. return 0;
  79. }
  80. static int as3722_gpio_probe(struct udevice *dev)
  81. {
  82. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  83. uc_priv->gpio_count = NUM_GPIOS;
  84. uc_priv->bank_name = "as3722_";
  85. return 0;
  86. }
  87. static const struct dm_gpio_ops gpio_as3722_ops = {
  88. .direction_output = as3722_gpio_direction_output,
  89. .set_value = as3722_gpio_set_value,
  90. };
  91. U_BOOT_DRIVER(gpio_as3722) = {
  92. .name = "gpio_as3722",
  93. .id = UCLASS_GPIO,
  94. .ops = &gpio_as3722_ops,
  95. .probe = as3722_gpio_probe,
  96. };