gpio-rdc321x.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/driver.h>
  29. #include <linux/mfd/rdc321x.h>
  30. #include <linux/slab.h>
  31. struct rdc321x_gpio {
  32. spinlock_t lock;
  33. struct pci_dev *sb_pdev;
  34. u32 data_reg[2];
  35. int reg1_ctrl_base;
  36. int reg1_data_base;
  37. int reg2_ctrl_base;
  38. int reg2_data_base;
  39. struct gpio_chip chip;
  40. };
  41. /* read GPIO pin */
  42. static int rdc_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
  43. {
  44. struct rdc321x_gpio *gpch;
  45. u32 value = 0;
  46. int reg;
  47. gpch = gpiochip_get_data(chip);
  48. reg = gpio < 32 ? gpch->reg1_data_base : gpch->reg2_data_base;
  49. spin_lock(&gpch->lock);
  50. pci_write_config_dword(gpch->sb_pdev, reg,
  51. gpch->data_reg[gpio < 32 ? 0 : 1]);
  52. pci_read_config_dword(gpch->sb_pdev, reg, &value);
  53. spin_unlock(&gpch->lock);
  54. return (1 << (gpio & 0x1f)) & value ? 1 : 0;
  55. }
  56. static void rdc_gpio_set_value_impl(struct gpio_chip *chip,
  57. unsigned gpio, int value)
  58. {
  59. struct rdc321x_gpio *gpch;
  60. int reg = (gpio < 32) ? 0 : 1;
  61. gpch = gpiochip_get_data(chip);
  62. if (value)
  63. gpch->data_reg[reg] |= 1 << (gpio & 0x1f);
  64. else
  65. gpch->data_reg[reg] &= ~(1 << (gpio & 0x1f));
  66. pci_write_config_dword(gpch->sb_pdev,
  67. reg ? gpch->reg2_data_base : gpch->reg1_data_base,
  68. gpch->data_reg[reg]);
  69. }
  70. /* set GPIO pin to value */
  71. static void rdc_gpio_set_value(struct gpio_chip *chip,
  72. unsigned gpio, int value)
  73. {
  74. struct rdc321x_gpio *gpch;
  75. gpch = gpiochip_get_data(chip);
  76. spin_lock(&gpch->lock);
  77. rdc_gpio_set_value_impl(chip, gpio, value);
  78. spin_unlock(&gpch->lock);
  79. }
  80. static int rdc_gpio_config(struct gpio_chip *chip,
  81. unsigned gpio, int value)
  82. {
  83. struct rdc321x_gpio *gpch;
  84. int err;
  85. u32 reg;
  86. gpch = gpiochip_get_data(chip);
  87. spin_lock(&gpch->lock);
  88. err = pci_read_config_dword(gpch->sb_pdev, gpio < 32 ?
  89. gpch->reg1_ctrl_base : gpch->reg2_ctrl_base, &reg);
  90. if (err)
  91. goto unlock;
  92. reg |= 1 << (gpio & 0x1f);
  93. err = pci_write_config_dword(gpch->sb_pdev, gpio < 32 ?
  94. gpch->reg1_ctrl_base : gpch->reg2_ctrl_base, reg);
  95. if (err)
  96. goto unlock;
  97. rdc_gpio_set_value_impl(chip, gpio, value);
  98. unlock:
  99. spin_unlock(&gpch->lock);
  100. return err;
  101. }
  102. /* configure GPIO pin as input */
  103. static int rdc_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  104. {
  105. return rdc_gpio_config(chip, gpio, 1);
  106. }
  107. /*
  108. * Cache the initial value of both GPIO data registers
  109. */
  110. static int rdc321x_gpio_probe(struct platform_device *pdev)
  111. {
  112. int err;
  113. struct resource *r;
  114. struct rdc321x_gpio *rdc321x_gpio_dev;
  115. struct rdc321x_gpio_pdata *pdata;
  116. pdata = dev_get_platdata(&pdev->dev);
  117. if (!pdata) {
  118. dev_err(&pdev->dev, "no platform data supplied\n");
  119. return -ENODEV;
  120. }
  121. rdc321x_gpio_dev = devm_kzalloc(&pdev->dev, sizeof(struct rdc321x_gpio),
  122. GFP_KERNEL);
  123. if (!rdc321x_gpio_dev)
  124. return -ENOMEM;
  125. r = platform_get_resource_byname(pdev, IORESOURCE_IO, "gpio-reg1");
  126. if (!r) {
  127. dev_err(&pdev->dev, "failed to get gpio-reg1 resource\n");
  128. return -ENODEV;
  129. }
  130. spin_lock_init(&rdc321x_gpio_dev->lock);
  131. rdc321x_gpio_dev->sb_pdev = pdata->sb_pdev;
  132. rdc321x_gpio_dev->reg1_ctrl_base = r->start;
  133. rdc321x_gpio_dev->reg1_data_base = r->start + 0x4;
  134. r = platform_get_resource_byname(pdev, IORESOURCE_IO, "gpio-reg2");
  135. if (!r) {
  136. dev_err(&pdev->dev, "failed to get gpio-reg2 resource\n");
  137. return -ENODEV;
  138. }
  139. rdc321x_gpio_dev->reg2_ctrl_base = r->start;
  140. rdc321x_gpio_dev->reg2_data_base = r->start + 0x4;
  141. rdc321x_gpio_dev->chip.label = "rdc321x-gpio";
  142. rdc321x_gpio_dev->chip.owner = THIS_MODULE;
  143. rdc321x_gpio_dev->chip.direction_input = rdc_gpio_direction_input;
  144. rdc321x_gpio_dev->chip.direction_output = rdc_gpio_config;
  145. rdc321x_gpio_dev->chip.get = rdc_gpio_get_value;
  146. rdc321x_gpio_dev->chip.set = rdc_gpio_set_value;
  147. rdc321x_gpio_dev->chip.base = 0;
  148. rdc321x_gpio_dev->chip.ngpio = pdata->max_gpios;
  149. platform_set_drvdata(pdev, rdc321x_gpio_dev);
  150. /* This might not be, what others (BIOS, bootloader, etc.)
  151. wrote to these registers before, but it's a good guess. Still
  152. better than just using 0xffffffff. */
  153. err = pci_read_config_dword(rdc321x_gpio_dev->sb_pdev,
  154. rdc321x_gpio_dev->reg1_data_base,
  155. &rdc321x_gpio_dev->data_reg[0]);
  156. if (err)
  157. return err;
  158. err = pci_read_config_dword(rdc321x_gpio_dev->sb_pdev,
  159. rdc321x_gpio_dev->reg2_data_base,
  160. &rdc321x_gpio_dev->data_reg[1]);
  161. if (err)
  162. return err;
  163. dev_info(&pdev->dev, "registering %d GPIOs\n",
  164. rdc321x_gpio_dev->chip.ngpio);
  165. return devm_gpiochip_add_data(&pdev->dev, &rdc321x_gpio_dev->chip,
  166. rdc321x_gpio_dev);
  167. }
  168. static struct platform_driver rdc321x_gpio_driver = {
  169. .driver.name = "rdc321x-gpio",
  170. .probe = rdc321x_gpio_probe,
  171. };
  172. module_platform_driver(rdc321x_gpio_driver);
  173. MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
  174. MODULE_DESCRIPTION("RDC321x GPIO driver");
  175. MODULE_LICENSE("GPL");
  176. MODULE_ALIAS("platform:rdc321x-gpio");