gpio-hisi.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2020 HiSilicon Limited. */
  3. #include <linux/gpio/driver.h>
  4. #include <linux/module.h>
  5. #include <linux/mod_devicetable.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/property.h>
  8. #define HISI_GPIO_SWPORT_DR_SET_WX 0x000
  9. #define HISI_GPIO_SWPORT_DR_CLR_WX 0x004
  10. #define HISI_GPIO_SWPORT_DDR_SET_WX 0x010
  11. #define HISI_GPIO_SWPORT_DDR_CLR_WX 0x014
  12. #define HISI_GPIO_SWPORT_DDR_ST_WX 0x018
  13. #define HISI_GPIO_INTEN_SET_WX 0x020
  14. #define HISI_GPIO_INTEN_CLR_WX 0x024
  15. #define HISI_GPIO_INTMASK_SET_WX 0x030
  16. #define HISI_GPIO_INTMASK_CLR_WX 0x034
  17. #define HISI_GPIO_INTTYPE_EDGE_SET_WX 0x040
  18. #define HISI_GPIO_INTTYPE_EDGE_CLR_WX 0x044
  19. #define HISI_GPIO_INT_POLARITY_SET_WX 0x050
  20. #define HISI_GPIO_INT_POLARITY_CLR_WX 0x054
  21. #define HISI_GPIO_DEBOUNCE_SET_WX 0x060
  22. #define HISI_GPIO_DEBOUNCE_CLR_WX 0x064
  23. #define HISI_GPIO_INTSTATUS_WX 0x070
  24. #define HISI_GPIO_PORTA_EOI_WX 0x078
  25. #define HISI_GPIO_EXT_PORT_WX 0x080
  26. #define HISI_GPIO_INTCOMB_MASK_WX 0x0a0
  27. #define HISI_GPIO_INT_DEDGE_SET 0x0b0
  28. #define HISI_GPIO_INT_DEDGE_CLR 0x0b4
  29. #define HISI_GPIO_INT_DEDGE_ST 0x0b8
  30. #define HISI_GPIO_LINE_NUM_MAX 32
  31. #define HISI_GPIO_DRIVER_NAME "gpio-hisi"
  32. struct hisi_gpio {
  33. struct gpio_chip chip;
  34. struct device *dev;
  35. void __iomem *reg_base;
  36. unsigned int line_num;
  37. int irq;
  38. };
  39. static inline u32 hisi_gpio_read_reg(struct gpio_chip *chip,
  40. unsigned int off)
  41. {
  42. struct hisi_gpio *hisi_gpio =
  43. container_of(chip, struct hisi_gpio, chip);
  44. void __iomem *reg = hisi_gpio->reg_base + off;
  45. return readl(reg);
  46. }
  47. static inline void hisi_gpio_write_reg(struct gpio_chip *chip,
  48. unsigned int off, u32 val)
  49. {
  50. struct hisi_gpio *hisi_gpio =
  51. container_of(chip, struct hisi_gpio, chip);
  52. void __iomem *reg = hisi_gpio->reg_base + off;
  53. writel(val, reg);
  54. }
  55. static void hisi_gpio_set_debounce(struct gpio_chip *chip, unsigned int off,
  56. u32 debounce)
  57. {
  58. if (debounce)
  59. hisi_gpio_write_reg(chip, HISI_GPIO_DEBOUNCE_SET_WX, BIT(off));
  60. else
  61. hisi_gpio_write_reg(chip, HISI_GPIO_DEBOUNCE_CLR_WX, BIT(off));
  62. }
  63. static int hisi_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
  64. unsigned long config)
  65. {
  66. u32 config_para = pinconf_to_config_param(config);
  67. u32 config_arg;
  68. switch (config_para) {
  69. case PIN_CONFIG_INPUT_DEBOUNCE:
  70. config_arg = pinconf_to_config_argument(config);
  71. hisi_gpio_set_debounce(chip, offset, config_arg);
  72. break;
  73. default:
  74. return -ENOTSUPP;
  75. }
  76. return 0;
  77. }
  78. static void hisi_gpio_set_ack(struct irq_data *d)
  79. {
  80. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  81. hisi_gpio_write_reg(chip, HISI_GPIO_PORTA_EOI_WX, BIT(irqd_to_hwirq(d)));
  82. }
  83. static void hisi_gpio_irq_set_mask(struct irq_data *d)
  84. {
  85. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  86. hisi_gpio_write_reg(chip, HISI_GPIO_INTMASK_SET_WX, BIT(irqd_to_hwirq(d)));
  87. gpiochip_disable_irq(chip, irqd_to_hwirq(d));
  88. }
  89. static void hisi_gpio_irq_clr_mask(struct irq_data *d)
  90. {
  91. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  92. gpiochip_enable_irq(chip, irqd_to_hwirq(d));
  93. hisi_gpio_write_reg(chip, HISI_GPIO_INTMASK_CLR_WX, BIT(irqd_to_hwirq(d)));
  94. }
  95. static int hisi_gpio_irq_set_type(struct irq_data *d, u32 type)
  96. {
  97. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  98. unsigned int mask = BIT(irqd_to_hwirq(d));
  99. switch (type) {
  100. case IRQ_TYPE_EDGE_BOTH:
  101. hisi_gpio_write_reg(chip, HISI_GPIO_INT_DEDGE_SET, mask);
  102. break;
  103. case IRQ_TYPE_EDGE_RISING:
  104. hisi_gpio_write_reg(chip, HISI_GPIO_INTTYPE_EDGE_SET_WX, mask);
  105. hisi_gpio_write_reg(chip, HISI_GPIO_INT_POLARITY_SET_WX, mask);
  106. break;
  107. case IRQ_TYPE_EDGE_FALLING:
  108. hisi_gpio_write_reg(chip, HISI_GPIO_INTTYPE_EDGE_SET_WX, mask);
  109. hisi_gpio_write_reg(chip, HISI_GPIO_INT_POLARITY_CLR_WX, mask);
  110. break;
  111. case IRQ_TYPE_LEVEL_HIGH:
  112. hisi_gpio_write_reg(chip, HISI_GPIO_INTTYPE_EDGE_CLR_WX, mask);
  113. hisi_gpio_write_reg(chip, HISI_GPIO_INT_POLARITY_SET_WX, mask);
  114. break;
  115. case IRQ_TYPE_LEVEL_LOW:
  116. hisi_gpio_write_reg(chip, HISI_GPIO_INTTYPE_EDGE_CLR_WX, mask);
  117. hisi_gpio_write_reg(chip, HISI_GPIO_INT_POLARITY_CLR_WX, mask);
  118. break;
  119. default:
  120. return -EINVAL;
  121. }
  122. /*
  123. * The dual-edge interrupt and other interrupt's registers do not
  124. * take effect at the same time. The registers of the two-edge
  125. * interrupts have higher priorities, the configuration of
  126. * the dual-edge interrupts must be disabled before the configuration
  127. * of other kind of interrupts.
  128. */
  129. if (type != IRQ_TYPE_EDGE_BOTH) {
  130. unsigned int both = hisi_gpio_read_reg(chip, HISI_GPIO_INT_DEDGE_ST);
  131. if (both & mask)
  132. hisi_gpio_write_reg(chip, HISI_GPIO_INT_DEDGE_CLR, mask);
  133. }
  134. if (type & IRQ_TYPE_LEVEL_MASK)
  135. irq_set_handler_locked(d, handle_level_irq);
  136. else if (type & IRQ_TYPE_EDGE_BOTH)
  137. irq_set_handler_locked(d, handle_edge_irq);
  138. return 0;
  139. }
  140. static void hisi_gpio_irq_enable(struct irq_data *d)
  141. {
  142. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  143. hisi_gpio_irq_clr_mask(d);
  144. hisi_gpio_write_reg(chip, HISI_GPIO_INTEN_SET_WX, BIT(irqd_to_hwirq(d)));
  145. }
  146. static void hisi_gpio_irq_disable(struct irq_data *d)
  147. {
  148. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  149. hisi_gpio_irq_set_mask(d);
  150. hisi_gpio_write_reg(chip, HISI_GPIO_INTEN_CLR_WX, BIT(irqd_to_hwirq(d)));
  151. }
  152. static void hisi_gpio_irq_handler(struct irq_desc *desc)
  153. {
  154. struct hisi_gpio *hisi_gpio = irq_desc_get_handler_data(desc);
  155. unsigned long irq_msk = hisi_gpio_read_reg(&hisi_gpio->chip,
  156. HISI_GPIO_INTSTATUS_WX);
  157. struct irq_chip *irq_c = irq_desc_get_chip(desc);
  158. int hwirq;
  159. chained_irq_enter(irq_c, desc);
  160. for_each_set_bit(hwirq, &irq_msk, HISI_GPIO_LINE_NUM_MAX)
  161. generic_handle_domain_irq(hisi_gpio->chip.irq.domain,
  162. hwirq);
  163. chained_irq_exit(irq_c, desc);
  164. }
  165. static const struct irq_chip hisi_gpio_irq_chip = {
  166. .name = "HISI-GPIO",
  167. .irq_ack = hisi_gpio_set_ack,
  168. .irq_mask = hisi_gpio_irq_set_mask,
  169. .irq_unmask = hisi_gpio_irq_clr_mask,
  170. .irq_set_type = hisi_gpio_irq_set_type,
  171. .irq_enable = hisi_gpio_irq_enable,
  172. .irq_disable = hisi_gpio_irq_disable,
  173. .flags = IRQCHIP_IMMUTABLE,
  174. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  175. };
  176. static void hisi_gpio_init_irq(struct hisi_gpio *hisi_gpio)
  177. {
  178. struct gpio_chip *chip = &hisi_gpio->chip;
  179. struct gpio_irq_chip *girq_chip = &chip->irq;
  180. gpio_irq_chip_set_chip(girq_chip, &hisi_gpio_irq_chip);
  181. girq_chip->default_type = IRQ_TYPE_NONE;
  182. girq_chip->num_parents = 1;
  183. girq_chip->parents = &hisi_gpio->irq;
  184. girq_chip->parent_handler = hisi_gpio_irq_handler;
  185. girq_chip->parent_handler_data = hisi_gpio;
  186. /* Clear Mask of GPIO controller combine IRQ */
  187. hisi_gpio_write_reg(chip, HISI_GPIO_INTCOMB_MASK_WX, 1);
  188. }
  189. static const struct acpi_device_id hisi_gpio_acpi_match[] = {
  190. {"HISI0184", 0},
  191. {}
  192. };
  193. MODULE_DEVICE_TABLE(acpi, hisi_gpio_acpi_match);
  194. static const struct of_device_id hisi_gpio_dts_match[] = {
  195. { .compatible = "hisilicon,ascend910-gpio", },
  196. { }
  197. };
  198. MODULE_DEVICE_TABLE(of, hisi_gpio_dts_match);
  199. static void hisi_gpio_get_pdata(struct device *dev,
  200. struct hisi_gpio *hisi_gpio)
  201. {
  202. struct platform_device *pdev = to_platform_device(dev);
  203. struct fwnode_handle *fwnode;
  204. int idx = 0;
  205. device_for_each_child_node(dev, fwnode) {
  206. /* Cycle for once, no need for an array to save line_num */
  207. if (fwnode_property_read_u32(fwnode, "ngpios",
  208. &hisi_gpio->line_num)) {
  209. dev_err(dev,
  210. "failed to get number of lines for port%d and use default value instead\n",
  211. idx);
  212. hisi_gpio->line_num = HISI_GPIO_LINE_NUM_MAX;
  213. }
  214. if (WARN_ON(hisi_gpio->line_num > HISI_GPIO_LINE_NUM_MAX))
  215. hisi_gpio->line_num = HISI_GPIO_LINE_NUM_MAX;
  216. hisi_gpio->irq = platform_get_irq(pdev, idx);
  217. dev_info(dev,
  218. "get hisi_gpio[%d] with %u lines\n", idx,
  219. hisi_gpio->line_num);
  220. idx++;
  221. }
  222. }
  223. static int hisi_gpio_probe(struct platform_device *pdev)
  224. {
  225. struct device *dev = &pdev->dev;
  226. struct hisi_gpio *hisi_gpio;
  227. int port_num;
  228. int ret;
  229. /*
  230. * One GPIO controller own one port currently,
  231. * if we get more from ACPI table, return error.
  232. */
  233. port_num = device_get_child_node_count(dev);
  234. if (WARN_ON(port_num != 1))
  235. return -ENODEV;
  236. hisi_gpio = devm_kzalloc(dev, sizeof(*hisi_gpio), GFP_KERNEL);
  237. if (!hisi_gpio)
  238. return -ENOMEM;
  239. hisi_gpio->reg_base = devm_platform_ioremap_resource(pdev, 0);
  240. if (IS_ERR(hisi_gpio->reg_base))
  241. return PTR_ERR(hisi_gpio->reg_base);
  242. hisi_gpio_get_pdata(dev, hisi_gpio);
  243. hisi_gpio->dev = dev;
  244. ret = bgpio_init(&hisi_gpio->chip, hisi_gpio->dev, 0x4,
  245. hisi_gpio->reg_base + HISI_GPIO_EXT_PORT_WX,
  246. hisi_gpio->reg_base + HISI_GPIO_SWPORT_DR_SET_WX,
  247. hisi_gpio->reg_base + HISI_GPIO_SWPORT_DR_CLR_WX,
  248. hisi_gpio->reg_base + HISI_GPIO_SWPORT_DDR_SET_WX,
  249. hisi_gpio->reg_base + HISI_GPIO_SWPORT_DDR_CLR_WX,
  250. BGPIOF_NO_SET_ON_INPUT);
  251. if (ret) {
  252. dev_err(dev, "failed to init, ret = %d\n", ret);
  253. return ret;
  254. }
  255. hisi_gpio->chip.set_config = hisi_gpio_set_config;
  256. hisi_gpio->chip.ngpio = hisi_gpio->line_num;
  257. hisi_gpio->chip.bgpio_dir_unreadable = 1;
  258. hisi_gpio->chip.base = -1;
  259. if (hisi_gpio->irq > 0)
  260. hisi_gpio_init_irq(hisi_gpio);
  261. ret = devm_gpiochip_add_data(dev, &hisi_gpio->chip, hisi_gpio);
  262. if (ret) {
  263. dev_err(dev, "failed to register gpiochip, ret = %d\n", ret);
  264. return ret;
  265. }
  266. return 0;
  267. }
  268. static struct platform_driver hisi_gpio_driver = {
  269. .driver = {
  270. .name = HISI_GPIO_DRIVER_NAME,
  271. .acpi_match_table = hisi_gpio_acpi_match,
  272. .of_match_table = hisi_gpio_dts_match,
  273. },
  274. .probe = hisi_gpio_probe,
  275. };
  276. module_platform_driver(hisi_gpio_driver);
  277. MODULE_LICENSE("GPL");
  278. MODULE_AUTHOR("Luo Jiaxing <luojiaxing@huawei.com>");
  279. MODULE_DESCRIPTION("HiSilicon GPIO controller driver");
  280. MODULE_ALIAS("platform:" HISI_GPIO_DRIVER_NAME);