rdc321x-gpio.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * RDC321x GPIO driver
  3. *
  4. * Copyright (C) 2008, Volker Weiss <dev@tintuc.de>
  5. * Copyright (C) 2007-2010 Florian Fainelli <florian@openwrt.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pci.h>
  28. #include <linux/gpio.h>
  29. #include <linux/mfd/rdc321x.h>
  30. #include <linux/mfd/core.h>
  31. #include <linux/slab.h>
  32. struct rdc321x_gpio {
  33. spinlock_t lock;
  34. struct pci_dev *sb_pdev;
  35. u32 data_reg[2];
  36. int reg1_ctrl_base;
  37. int reg1_data_base;
  38. int reg2_ctrl_base;
  39. int reg2_data_base;
  40. struct gpio_chip chip;
  41. };
  42. /* read GPIO pin */
  43. static int rdc_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  44. {
  45. struct rdc321x_gpio *gpch;
  46. u32 value = 0;
  47. int reg;
  48. gpch = container_of(chip, struct rdc321x_gpio, chip);
  49. reg = gpio < 32 ? gpch->reg1_data_base : gpch->reg2_data_base;
  50. spin_lock(&gpch->lock);
  51. pci_write_config_dword(gpch->sb_pdev, reg,
  52. gpch->data_reg[gpio < 32 ? 0 : 1]);
  53. pci_read_config_dword(gpch->sb_pdev, reg, &value);
  54. spin_unlock(&gpch->lock);
  55. return (1 << (gpio & 0x1f)) & value ? 1 : 0;
  56. }
  57. static void rdc_gpio_set_value_impl(struct gpio_chip *chip,
  58. unsigned gpio, int value)
  59. {
  60. struct rdc321x_gpio *gpch;
  61. int reg = (gpio < 32) ? 0 : 1;
  62. gpch = container_of(chip, struct rdc321x_gpio, chip);
  63. if (value)
  64. gpch->data_reg[reg] |= 1 << (gpio & 0x1f);
  65. else
  66. gpch->data_reg[reg] &= ~(1 << (gpio & 0x1f));
  67. pci_write_config_dword(gpch->sb_pdev,
  68. reg ? gpch->reg2_data_base : gpch->reg1_data_base,
  69. gpch->data_reg[reg]);
  70. }
  71. /* set GPIO pin to value */
  72. static void rdc_gpio_set_value(struct gpio_chip *chip,
  73. unsigned gpio, int value)
  74. {
  75. struct rdc321x_gpio *gpch;
  76. gpch = container_of(chip, struct rdc321x_gpio, chip);
  77. spin_lock(&gpch->lock);
  78. rdc_gpio_set_value_impl(chip, gpio, value);
  79. spin_unlock(&gpch->lock);
  80. }
  81. static int rdc_gpio_config(struct gpio_chip *chip,
  82. unsigned gpio, int value)
  83. {
  84. struct rdc321x_gpio *gpch;
  85. int err;
  86. u32 reg;
  87. gpch = container_of(chip, struct rdc321x_gpio, chip);
  88. spin_lock(&gpch->lock);
  89. err = pci_read_config_dword(gpch->sb_pdev, gpio < 32 ?
  90. gpch->reg1_ctrl_base : gpch->reg2_ctrl_base, &reg);
  91. if (err)
  92. goto unlock;
  93. reg |= 1 << (gpio & 0x1f);
  94. err = pci_write_config_dword(gpch->sb_pdev, gpio < 32 ?
  95. gpch->reg1_ctrl_base : gpch->reg2_ctrl_base, reg);
  96. if (err)
  97. goto unlock;
  98. rdc_gpio_set_value_impl(chip, gpio, value);
  99. unlock:
  100. spin_unlock(&gpch->lock);
  101. return err;
  102. }
  103. /* configure GPIO pin as input */
  104. static int rdc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  105. {
  106. return rdc_gpio_config(chip, gpio, 1);
  107. }
  108. /*
  109. * Cache the initial value of both GPIO data registers
  110. */
  111. static int __devinit rdc321x_gpio_probe(struct platform_device *pdev)
  112. {
  113. int err;
  114. struct resource *r;
  115. struct rdc321x_gpio *rdc321x_gpio_dev;
  116. struct rdc321x_gpio_pdata *pdata;
  117. pdata = mfd_get_data(pdev);
  118. if (!pdata) {
  119. dev_err(&pdev->dev, "no platform data supplied\n");
  120. return -ENODEV;
  121. }
  122. rdc321x_gpio_dev = kzalloc(sizeof(struct rdc321x_gpio), GFP_KERNEL);
  123. if (!rdc321x_gpio_dev) {
  124. dev_err(&pdev->dev, "failed to allocate private data\n");
  125. return -ENOMEM;
  126. }
  127. r = platform_get_resource_byname(pdev, IORESOURCE_IO, "gpio-reg1");
  128. if (!r) {
  129. dev_err(&pdev->dev, "failed to get gpio-reg1 resource\n");
  130. err = -ENODEV;
  131. goto out_free;
  132. }
  133. spin_lock_init(&rdc321x_gpio_dev->lock);
  134. rdc321x_gpio_dev->sb_pdev = pdata->sb_pdev;
  135. rdc321x_gpio_dev->reg1_ctrl_base = r->start;
  136. rdc321x_gpio_dev->reg1_data_base = r->start + 0x4;
  137. r = platform_get_resource_byname(pdev, IORESOURCE_IO, "gpio-reg2");
  138. if (!r) {
  139. dev_err(&pdev->dev, "failed to get gpio-reg2 resource\n");
  140. err = -ENODEV;
  141. goto out_free;
  142. }
  143. rdc321x_gpio_dev->reg2_ctrl_base = r->start;
  144. rdc321x_gpio_dev->reg2_data_base = r->start + 0x4;
  145. rdc321x_gpio_dev->chip.label = "rdc321x-gpio";
  146. rdc321x_gpio_dev->chip.direction_input = rdc_gpio_direction_input;
  147. rdc321x_gpio_dev->chip.direction_output = rdc_gpio_config;
  148. rdc321x_gpio_dev->chip.get = rdc_gpio_get_value;
  149. rdc321x_gpio_dev->chip.set = rdc_gpio_set_value;
  150. rdc321x_gpio_dev->chip.base = 0;
  151. rdc321x_gpio_dev->chip.ngpio = pdata->max_gpios;
  152. platform_set_drvdata(pdev, rdc321x_gpio_dev);
  153. /* This might not be, what others (BIOS, bootloader, etc.)
  154. wrote to these registers before, but it's a good guess. Still
  155. better than just using 0xffffffff. */
  156. err = pci_read_config_dword(rdc321x_gpio_dev->sb_pdev,
  157. rdc321x_gpio_dev->reg1_data_base,
  158. &rdc321x_gpio_dev->data_reg[0]);
  159. if (err)
  160. goto out_drvdata;
  161. err = pci_read_config_dword(rdc321x_gpio_dev->sb_pdev,
  162. rdc321x_gpio_dev->reg2_data_base,
  163. &rdc321x_gpio_dev->data_reg[1]);
  164. if (err)
  165. goto out_drvdata;
  166. dev_info(&pdev->dev, "registering %d GPIOs\n",
  167. rdc321x_gpio_dev->chip.ngpio);
  168. return gpiochip_add(&rdc321x_gpio_dev->chip);
  169. out_drvdata:
  170. platform_set_drvdata(pdev, NULL);
  171. out_free:
  172. kfree(rdc321x_gpio_dev);
  173. return err;
  174. }
  175. static int __devexit rdc321x_gpio_remove(struct platform_device *pdev)
  176. {
  177. int ret;
  178. struct rdc321x_gpio *rdc321x_gpio_dev = platform_get_drvdata(pdev);
  179. ret = gpiochip_remove(&rdc321x_gpio_dev->chip);
  180. if (ret)
  181. dev_err(&pdev->dev, "failed to unregister chip\n");
  182. kfree(rdc321x_gpio_dev);
  183. platform_set_drvdata(pdev, NULL);
  184. return ret;
  185. }
  186. static struct platform_driver rdc321x_gpio_driver = {
  187. .driver.name = "rdc321x-gpio",
  188. .driver.owner = THIS_MODULE,
  189. .probe = rdc321x_gpio_probe,
  190. .remove = __devexit_p(rdc321x_gpio_remove),
  191. };
  192. static int __init rdc321x_gpio_init(void)
  193. {
  194. return platform_driver_register(&rdc321x_gpio_driver);
  195. }
  196. static void __exit rdc321x_gpio_exit(void)
  197. {
  198. platform_driver_unregister(&rdc321x_gpio_driver);
  199. }
  200. module_init(rdc321x_gpio_init);
  201. module_exit(rdc321x_gpio_exit);
  202. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  203. MODULE_DESCRIPTION("RDC321x GPIO driver");
  204. MODULE_LICENSE("GPL");
  205. MODULE_ALIAS("platform:rdc321x-gpio");