as3722_gpio.c 2.3 KB

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