hsdk-creg-gpio.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. DECLARE_GLOBAL_DATA_PTR;
  18. #define HSDK_CREG_MAX_GPIO 8
  19. #define GPIO_ACTIVATE 0x2
  20. #define GPIO_DEACTIVATE 0x3
  21. #define GPIO_PIN_MASK 0x3
  22. #define BIT_PER_GPIO 2
  23. struct hsdk_creg_gpio {
  24. uint32_t *regs;
  25. };
  26. static int hsdk_creg_gpio_set_value(struct udevice *dev, unsigned oft, int val)
  27. {
  28. struct hsdk_creg_gpio *hcg = dev_get_priv(dev);
  29. uint32_t reg = readl(hcg->regs);
  30. uint32_t cmd = val ? GPIO_DEACTIVATE : GPIO_ACTIVATE;
  31. reg &= ~(GPIO_PIN_MASK << (oft * BIT_PER_GPIO));
  32. reg |= (cmd << (oft * BIT_PER_GPIO));
  33. writel(reg, hcg->regs);
  34. return 0;
  35. }
  36. static int hsdk_creg_gpio_direction_output(struct udevice *dev, unsigned oft,
  37. int val)
  38. {
  39. hsdk_creg_gpio_set_value(dev, oft, val);
  40. return 0;
  41. }
  42. static int hsdk_creg_gpio_direction_input(struct udevice *dev, unsigned oft)
  43. {
  44. pr_err("hsdk-creg-gpio can't be used as input!\n");
  45. return -ENOTSUPP;
  46. }
  47. static int hsdk_creg_gpio_get_value(struct udevice *dev, unsigned int oft)
  48. {
  49. struct hsdk_creg_gpio *hcg = dev_get_priv(dev);
  50. uint32_t val = readl(hcg->regs);
  51. val = (val >> (oft * BIT_PER_GPIO)) & GPIO_PIN_MASK;
  52. return (val == GPIO_DEACTIVATE) ? 1 : 0;
  53. }
  54. static const struct dm_gpio_ops hsdk_creg_gpio_ops = {
  55. .direction_output = hsdk_creg_gpio_direction_output,
  56. .direction_input = hsdk_creg_gpio_direction_input,
  57. .set_value = hsdk_creg_gpio_set_value,
  58. .get_value = hsdk_creg_gpio_get_value,
  59. };
  60. static int hsdk_creg_gpio_probe(struct udevice *dev)
  61. {
  62. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  63. struct hsdk_creg_gpio *hcg = dev_get_priv(dev);
  64. hcg->regs = (uint32_t *)devfdt_get_addr_ptr(dev);
  65. uc_priv->gpio_count = dev_read_u32_default(dev, "gpio-count", 1);
  66. if (uc_priv->gpio_count > HSDK_CREG_MAX_GPIO)
  67. uc_priv->gpio_count = HSDK_CREG_MAX_GPIO;
  68. uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
  69. if (!uc_priv->bank_name)
  70. uc_priv->bank_name = dev_read_name(dev);
  71. pr_debug("%s GPIO [0x%p] controller with %d gpios probed\n",
  72. uc_priv->bank_name, hcg->regs, uc_priv->gpio_count);
  73. return 0;
  74. }
  75. static const struct udevice_id hsdk_creg_gpio_ids[] = {
  76. { .compatible = "snps,hsdk-creg-gpio" },
  77. { }
  78. };
  79. U_BOOT_DRIVER(gpio_hsdk_creg) = {
  80. .name = "gpio_hsdk_creg",
  81. .id = UCLASS_GPIO,
  82. .ops = &hsdk_creg_gpio_ops,
  83. .probe = hsdk_creg_gpio_probe,
  84. .of_match = hsdk_creg_gpio_ids,
  85. .platdata_auto_alloc_size = sizeof(struct hsdk_creg_gpio),
  86. };