hsdk-creg-gpio.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Synopsys HSDK SDP Generic PLL clock driver
  3. *
  4. * Copyright (C) 2017 Synopsys
  5. * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <asm-generic/gpio.h>
  12. #include <asm/io.h>
  13. #include <common.h>
  14. #include <dm.h>
  15. #include <errno.h>
  16. #include <linux/printk.h>
  17. #define HSDK_CREG_MAX_GPIO 8
  18. #define GPIO_ACTIVATE 0x2
  19. #define GPIO_DEACTIVATE 0x3
  20. #define GPIO_PIN_MASK 0x3
  21. #define BIT_PER_GPIO 2
  22. struct hsdk_creg_gpio {
  23. uint32_t *regs;
  24. };
  25. static int hsdk_creg_gpio_set_value(struct udevice *dev, unsigned oft, int val)
  26. {
  27. struct hsdk_creg_gpio *hcg = dev_get_priv(dev);
  28. uint32_t reg = readl(hcg->regs);
  29. uint32_t cmd = val ? GPIO_DEACTIVATE : GPIO_ACTIVATE;
  30. reg &= ~(GPIO_PIN_MASK << (oft * BIT_PER_GPIO));
  31. reg |= (cmd << (oft * BIT_PER_GPIO));
  32. writel(reg, hcg->regs);
  33. return 0;
  34. }
  35. static int hsdk_creg_gpio_direction_output(struct udevice *dev, unsigned oft,
  36. int val)
  37. {
  38. hsdk_creg_gpio_set_value(dev, oft, val);
  39. return 0;
  40. }
  41. static int hsdk_creg_gpio_direction_input(struct udevice *dev, unsigned oft)
  42. {
  43. pr_err("hsdk-creg-gpio can't be used as input!\n");
  44. return -ENOTSUPP;
  45. }
  46. static int hsdk_creg_gpio_get_value(struct udevice *dev, unsigned int oft)
  47. {
  48. struct hsdk_creg_gpio *hcg = dev_get_priv(dev);
  49. uint32_t val = readl(hcg->regs);
  50. val = (val >> (oft * BIT_PER_GPIO)) & GPIO_PIN_MASK;
  51. return (val == GPIO_DEACTIVATE) ? 1 : 0;
  52. }
  53. static const struct dm_gpio_ops hsdk_creg_gpio_ops = {
  54. .direction_output = hsdk_creg_gpio_direction_output,
  55. .direction_input = hsdk_creg_gpio_direction_input,
  56. .set_value = hsdk_creg_gpio_set_value,
  57. .get_value = hsdk_creg_gpio_get_value,
  58. };
  59. static int hsdk_creg_gpio_probe(struct udevice *dev)
  60. {
  61. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  62. struct hsdk_creg_gpio *hcg = dev_get_priv(dev);
  63. hcg->regs = (uint32_t *)devfdt_get_addr_ptr(dev);
  64. uc_priv->gpio_count = dev_read_u32_default(dev, "gpio-count", 1);
  65. if (uc_priv->gpio_count > HSDK_CREG_MAX_GPIO)
  66. uc_priv->gpio_count = HSDK_CREG_MAX_GPIO;
  67. uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
  68. if (!uc_priv->bank_name)
  69. uc_priv->bank_name = dev_read_name(dev);
  70. pr_debug("%s GPIO [0x%p] controller with %d gpios probed\n",
  71. uc_priv->bank_name, hcg->regs, uc_priv->gpio_count);
  72. return 0;
  73. }
  74. static const struct udevice_id hsdk_creg_gpio_ids[] = {
  75. { .compatible = "snps,hsdk-creg-gpio" },
  76. { }
  77. };
  78. U_BOOT_DRIVER(gpio_hsdk_creg) = {
  79. .name = "gpio_hsdk_creg",
  80. .id = UCLASS_GPIO,
  81. .ops = &hsdk_creg_gpio_ops,
  82. .probe = hsdk_creg_gpio_probe,
  83. .of_match = hsdk_creg_gpio_ids,
  84. .platdata_auto_alloc_size = sizeof(struct hsdk_creg_gpio),
  85. };