gpio-tn48m.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Delta TN48M CPLD GPIO driver
  4. *
  5. * Copyright (C) 2021 Sartura Ltd.
  6. *
  7. * Author: Robert Marko <[email protected]>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/gpio/driver.h>
  11. #include <linux/gpio/regmap.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regmap.h>
  16. enum tn48m_gpio_type {
  17. TN48M_GP0 = 1,
  18. TN48M_GPI,
  19. };
  20. struct tn48m_gpio_config {
  21. int ngpio;
  22. int ngpio_per_reg;
  23. enum tn48m_gpio_type type;
  24. };
  25. static const struct tn48m_gpio_config tn48m_gpo_config = {
  26. .ngpio = 4,
  27. .ngpio_per_reg = 4,
  28. .type = TN48M_GP0,
  29. };
  30. static const struct tn48m_gpio_config tn48m_gpi_config = {
  31. .ngpio = 4,
  32. .ngpio_per_reg = 4,
  33. .type = TN48M_GPI,
  34. };
  35. static int tn48m_gpio_probe(struct platform_device *pdev)
  36. {
  37. const struct tn48m_gpio_config *gpio_config;
  38. struct gpio_regmap_config config = {};
  39. struct regmap *regmap;
  40. u32 base;
  41. int ret;
  42. if (!pdev->dev.parent)
  43. return -ENODEV;
  44. gpio_config = device_get_match_data(&pdev->dev);
  45. if (!gpio_config)
  46. return -ENODEV;
  47. ret = device_property_read_u32(&pdev->dev, "reg", &base);
  48. if (ret)
  49. return ret;
  50. regmap = dev_get_regmap(pdev->dev.parent, NULL);
  51. if (!regmap)
  52. return -ENODEV;
  53. config.regmap = regmap;
  54. config.parent = &pdev->dev;
  55. config.ngpio = gpio_config->ngpio;
  56. config.ngpio_per_reg = gpio_config->ngpio_per_reg;
  57. switch (gpio_config->type) {
  58. case TN48M_GP0:
  59. config.reg_set_base = base;
  60. break;
  61. case TN48M_GPI:
  62. config.reg_dat_base = base;
  63. break;
  64. default:
  65. return -EINVAL;
  66. }
  67. return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(&pdev->dev, &config));
  68. }
  69. static const struct of_device_id tn48m_gpio_of_match[] = {
  70. { .compatible = "delta,tn48m-gpo", .data = &tn48m_gpo_config },
  71. { .compatible = "delta,tn48m-gpi", .data = &tn48m_gpi_config },
  72. { }
  73. };
  74. MODULE_DEVICE_TABLE(of, tn48m_gpio_of_match);
  75. static struct platform_driver tn48m_gpio_driver = {
  76. .driver = {
  77. .name = "delta-tn48m-gpio",
  78. .of_match_table = tn48m_gpio_of_match,
  79. },
  80. .probe = tn48m_gpio_probe,
  81. };
  82. module_platform_driver(tn48m_gpio_driver);
  83. MODULE_AUTHOR("Robert Marko <[email protected]>");
  84. MODULE_DESCRIPTION("Delta TN48M CPLD GPIO driver");
  85. MODULE_LICENSE("GPL");