gpio-sl28cpld.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sl28cpld GPIO driver
  4. *
  5. * Copyright 2020 Michael Walle <michael@walle.cc>
  6. */
  7. #include <linux/device.h>
  8. #include <linux/gpio/driver.h>
  9. #include <linux/gpio/regmap.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regmap.h>
  16. /* GPIO flavor */
  17. #define GPIO_REG_DIR 0x00
  18. #define GPIO_REG_OUT 0x01
  19. #define GPIO_REG_IN 0x02
  20. #define GPIO_REG_IE 0x03
  21. #define GPIO_REG_IP 0x04
  22. /* input-only flavor */
  23. #define GPI_REG_IN 0x00
  24. /* output-only flavor */
  25. #define GPO_REG_OUT 0x00
  26. enum sl28cpld_gpio_type {
  27. SL28CPLD_GPIO = 1,
  28. SL28CPLD_GPI,
  29. SL28CPLD_GPO,
  30. };
  31. static const struct regmap_irq sl28cpld_gpio_irqs[] = {
  32. REGMAP_IRQ_REG_LINE(0, 8),
  33. REGMAP_IRQ_REG_LINE(1, 8),
  34. REGMAP_IRQ_REG_LINE(2, 8),
  35. REGMAP_IRQ_REG_LINE(3, 8),
  36. REGMAP_IRQ_REG_LINE(4, 8),
  37. REGMAP_IRQ_REG_LINE(5, 8),
  38. REGMAP_IRQ_REG_LINE(6, 8),
  39. REGMAP_IRQ_REG_LINE(7, 8),
  40. };
  41. static int sl28cpld_gpio_irq_init(struct platform_device *pdev,
  42. unsigned int base,
  43. struct gpio_regmap_config *config)
  44. {
  45. struct regmap_irq_chip_data *irq_data;
  46. struct regmap_irq_chip *irq_chip;
  47. struct device *dev = &pdev->dev;
  48. int irq, ret;
  49. if (!device_property_read_bool(dev, "interrupt-controller"))
  50. return 0;
  51. irq = platform_get_irq(pdev, 0);
  52. if (irq < 0)
  53. return irq;
  54. irq_chip = devm_kzalloc(dev, sizeof(*irq_chip), GFP_KERNEL);
  55. if (!irq_chip)
  56. return -ENOMEM;
  57. irq_chip->name = "sl28cpld-gpio-irq",
  58. irq_chip->irqs = sl28cpld_gpio_irqs;
  59. irq_chip->num_irqs = ARRAY_SIZE(sl28cpld_gpio_irqs);
  60. irq_chip->num_regs = 1;
  61. irq_chip->status_base = base + GPIO_REG_IP;
  62. irq_chip->mask_base = base + GPIO_REG_IE;
  63. irq_chip->mask_invert = true,
  64. irq_chip->ack_base = base + GPIO_REG_IP;
  65. ret = devm_regmap_add_irq_chip_fwnode(dev, dev_fwnode(dev),
  66. config->regmap, irq,
  67. IRQF_SHARED | IRQF_ONESHOT,
  68. 0, irq_chip, &irq_data);
  69. if (ret)
  70. return ret;
  71. config->irq_domain = regmap_irq_get_domain(irq_data);
  72. return 0;
  73. }
  74. static int sl28cpld_gpio_probe(struct platform_device *pdev)
  75. {
  76. struct gpio_regmap_config config = {0};
  77. enum sl28cpld_gpio_type type;
  78. struct regmap *regmap;
  79. u32 base;
  80. int ret;
  81. if (!pdev->dev.parent)
  82. return -ENODEV;
  83. type = (uintptr_t)device_get_match_data(&pdev->dev);
  84. if (!type)
  85. return -ENODEV;
  86. ret = device_property_read_u32(&pdev->dev, "reg", &base);
  87. if (ret)
  88. return -EINVAL;
  89. regmap = dev_get_regmap(pdev->dev.parent, NULL);
  90. if (!regmap)
  91. return -ENODEV;
  92. config.regmap = regmap;
  93. config.parent = &pdev->dev;
  94. config.ngpio = 8;
  95. switch (type) {
  96. case SL28CPLD_GPIO:
  97. config.reg_dat_base = base + GPIO_REG_IN;
  98. config.reg_set_base = base + GPIO_REG_OUT;
  99. /* reg_dir_out_base might be zero */
  100. config.reg_dir_out_base = GPIO_REGMAP_ADDR(base + GPIO_REG_DIR);
  101. /* This type supports interrupts */
  102. ret = sl28cpld_gpio_irq_init(pdev, base, &config);
  103. if (ret)
  104. return ret;
  105. break;
  106. case SL28CPLD_GPO:
  107. config.reg_set_base = base + GPO_REG_OUT;
  108. break;
  109. case SL28CPLD_GPI:
  110. config.reg_dat_base = base + GPI_REG_IN;
  111. break;
  112. default:
  113. dev_err(&pdev->dev, "unknown type %d\n", type);
  114. return -ENODEV;
  115. }
  116. return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(&pdev->dev, &config));
  117. }
  118. static const struct of_device_id sl28cpld_gpio_of_match[] = {
  119. { .compatible = "kontron,sl28cpld-gpio", .data = (void *)SL28CPLD_GPIO },
  120. { .compatible = "kontron,sl28cpld-gpi", .data = (void *)SL28CPLD_GPI },
  121. { .compatible = "kontron,sl28cpld-gpo", .data = (void *)SL28CPLD_GPO },
  122. {}
  123. };
  124. MODULE_DEVICE_TABLE(of, sl28cpld_gpio_of_match);
  125. static struct platform_driver sl28cpld_gpio_driver = {
  126. .probe = sl28cpld_gpio_probe,
  127. .driver = {
  128. .name = "sl28cpld-gpio",
  129. .of_match_table = sl28cpld_gpio_of_match,
  130. },
  131. };
  132. module_platform_driver(sl28cpld_gpio_driver);
  133. MODULE_DESCRIPTION("sl28cpld GPIO Driver");
  134. MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
  135. MODULE_LICENSE("GPL");