max7320_gpio.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * max7320 I2C GPIO EXPANDER DRIVER
  4. *
  5. * Copyright (C) 2021 Hannes Schmelzer <oe5hpm@oevsv.at>
  6. * B&R Industrial Automation GmbH - http://www.br-automation.com
  7. *
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <i2c.h>
  12. #include <asm-generic/gpio.h>
  13. #include <linux/bitops.h>
  14. struct max7320_chip {
  15. u32 outreg;
  16. };
  17. static int max7320_direction_output(struct udevice *dev,
  18. unsigned int offset, int value)
  19. {
  20. struct max7320_chip *plat = dev_get_plat(dev);
  21. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  22. struct dm_i2c_chip *chip = dev_get_parent_plat(dev);
  23. int ret;
  24. if (value)
  25. plat->outreg |= BIT(offset);
  26. else
  27. plat->outreg &= ~BIT(offset);
  28. ret = dm_i2c_write(dev,
  29. plat->outreg & 0xff,
  30. (uint8_t *)&plat->outreg + 1,
  31. uc_priv->gpio_count > 8 ? 1 : 0);
  32. if (ret)
  33. printf("%s i2c write failed to addr %x\n", __func__,
  34. chip->chip_addr);
  35. return ret;
  36. }
  37. static int max7320_get_value(struct udevice *dev, unsigned int offset)
  38. {
  39. struct max7320_chip *plat = dev_get_plat(dev);
  40. return (plat->outreg >> offset) & 0x1;
  41. }
  42. static int max7320_set_value(struct udevice *dev, unsigned int offset,
  43. int value)
  44. {
  45. return max7320_direction_output(dev, offset, value);
  46. }
  47. static int max7320_get_function(struct udevice *dev, unsigned int offset)
  48. {
  49. return GPIOF_OUTPUT;
  50. }
  51. static int max7320_ofdata_plat(struct udevice *dev)
  52. {
  53. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  54. uc_priv->gpio_count = dev_read_u32_default(dev, "ngpios", 8);
  55. if (uc_priv->gpio_count > 16) {
  56. printf("%s: max7320 doesn't support more than 16 gpios!",
  57. __func__);
  58. return -EINVAL;
  59. }
  60. uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
  61. "gpio-bank-name", NULL);
  62. if (!uc_priv->bank_name)
  63. uc_priv->bank_name = fdt_get_name(gd->fdt_blob,
  64. dev_of_offset(dev), NULL);
  65. return 0;
  66. }
  67. static int max7320_gpio_probe(struct udevice *dev)
  68. {
  69. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  70. debug("%s GPIO controller with %d gpios probed\n",
  71. uc_priv->bank_name, uc_priv->gpio_count);
  72. return 0;
  73. }
  74. static const struct dm_gpio_ops max7320_gpio_ops = {
  75. .direction_output = max7320_direction_output,
  76. .set_value = max7320_set_value,
  77. .get_value = max7320_get_value,
  78. .get_function = max7320_get_function,
  79. };
  80. static const struct udevice_id max7320_gpio_ids[] = {
  81. { .compatible = "maxim,max7320" },
  82. { }
  83. };
  84. U_BOOT_DRIVER(gpio_max7320) = {
  85. .name = "gpio_max7320",
  86. .id = UCLASS_GPIO,
  87. .ops = &max7320_gpio_ops,
  88. .of_match = max7320_gpio_ids,
  89. .of_to_plat = max7320_ofdata_plat,
  90. .probe = max7320_gpio_probe,
  91. .plat_auto = sizeof(struct max7320_chip),
  92. };