gpio-mcs814x.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Moschip MCS814x GPIO support
  3. *
  4. * Copyright (C) 2012, Florian Fainelli <florian@openwrt.org>
  5. *
  6. * Licensed under the GPLv2
  7. */
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/gpio.h>
  14. #include <linux/io.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. struct mcs814x_gpio_chip {
  18. void __iomem *regs;
  19. struct gpio_chip chip;
  20. };
  21. #define GPIO_PIN 0x00
  22. #define GPIO_DIR 0x04
  23. #define to_mcs814x_gpio_chip(x) container_of(x, struct mcs814x_gpio_chip, chip)
  24. static int mcs814x_gpio_get(struct gpio_chip *chip, unsigned offset)
  25. {
  26. struct mcs814x_gpio_chip *mcs814x = to_mcs814x_gpio_chip(chip);
  27. return readl_relaxed(mcs814x->regs + GPIO_PIN) & (1 << offset);
  28. }
  29. static void mcs814x_gpio_set(struct gpio_chip *chip,
  30. unsigned offset, int value)
  31. {
  32. struct mcs814x_gpio_chip *mcs814x = to_mcs814x_gpio_chip(chip);
  33. u32 mask;
  34. mask = readl_relaxed(mcs814x->regs + GPIO_PIN);
  35. if (value)
  36. mask |= (1 << offset);
  37. else
  38. mask &= ~(1 << offset);
  39. writel_relaxed(mask, mcs814x->regs + GPIO_PIN);
  40. }
  41. static int mcs814x_gpio_direction_output(struct gpio_chip *chip,
  42. unsigned offset, int value)
  43. {
  44. struct mcs814x_gpio_chip *mcs814x = to_mcs814x_gpio_chip(chip);
  45. u32 mask;
  46. mask = readl_relaxed(mcs814x->regs + GPIO_DIR);
  47. mask &= ~(1 << offset);
  48. writel_relaxed(mask, mcs814x->regs + GPIO_DIR);
  49. return 0;
  50. }
  51. static int mcs814x_gpio_direction_input(struct gpio_chip *chip,
  52. unsigned offset)
  53. {
  54. struct mcs814x_gpio_chip *mcs814x = to_mcs814x_gpio_chip(chip);
  55. u32 mask;
  56. mask = readl_relaxed(mcs814x->regs + GPIO_DIR);
  57. mask |= (1 << offset);
  58. writel_relaxed(mask, mcs814x->regs + GPIO_DIR);
  59. return 0;
  60. }
  61. static int mcs814x_gpio_probe(struct platform_device *pdev)
  62. {
  63. struct resource *res;
  64. struct mcs814x_gpio_chip *mcs814x_chip;
  65. int ret;
  66. const unsigned int *num_gpios;
  67. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  68. if (!res)
  69. return -ENODEV;
  70. num_gpios = of_get_property(pdev->dev.of_node, "num-gpios", NULL);
  71. if (!num_gpios)
  72. dev_err(&pdev->dev, "FIXME: no num-gpios property\n");
  73. mcs814x_chip = kzalloc(sizeof(*mcs814x_chip), GFP_KERNEL);
  74. if (!mcs814x_chip)
  75. return -ENOMEM;
  76. mcs814x_chip->regs = devm_ioremap_resource(&pdev->dev, res);
  77. if (!mcs814x_chip->regs) {
  78. ret = -ENOMEM;
  79. goto out;
  80. }
  81. platform_set_drvdata(pdev, mcs814x_chip);
  82. #ifdef CONFIG_OF_GPIO
  83. mcs814x_chip->chip.of_node = pdev->dev.of_node;
  84. #endif
  85. mcs814x_chip->chip.label = pdev->name;
  86. mcs814x_chip->chip.get = mcs814x_gpio_get;
  87. mcs814x_chip->chip.set = mcs814x_gpio_set;
  88. mcs814x_chip->chip.direction_input = mcs814x_gpio_direction_input;
  89. mcs814x_chip->chip.direction_output = mcs814x_gpio_direction_output;
  90. mcs814x_chip->chip.ngpio = be32_to_cpup(num_gpios);
  91. /* we want dynamic base allocation */
  92. mcs814x_chip->chip.base = -1;
  93. ret = gpiochip_add(&mcs814x_chip->chip);
  94. if (ret) {
  95. dev_err(&pdev->dev, "failed to register gpiochip\n");
  96. goto out;
  97. }
  98. return 0;
  99. out:
  100. platform_set_drvdata(pdev, NULL);
  101. kfree(mcs814x_chip);
  102. return ret;
  103. }
  104. static struct of_device_id mcs814x_gpio_ids[] = {
  105. { .compatible = "moschip,mcs814x-gpio" },
  106. { /* sentinel */ },
  107. };
  108. static struct platform_driver mcs814x_gpio_driver = {
  109. .driver = {
  110. .name = "mcs814x-gpio",
  111. .owner = THIS_MODULE,
  112. .of_match_table = mcs814x_gpio_ids,
  113. },
  114. .probe = mcs814x_gpio_probe,
  115. };
  116. int __init mcs814x_gpio_init(void)
  117. {
  118. return platform_driver_register(&mcs814x_gpio_driver);
  119. }
  120. postcore_initcall(mcs814x_gpio_init);