gpio-bt8xx.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. bt8xx GPIO abuser
  3. Copyright (C) 2008 Michael Buesch <m@bues.ch>
  4. Please do _only_ contact the people listed _above_ with issues related to this driver.
  5. All the other people listed below are not related to this driver. Their names
  6. are only here, because this driver is derived from the bt848 driver.
  7. Derived from the bt848 driver:
  8. Copyright (C) 1996,97,98 Ralph Metzler
  9. & Marcus Metzler
  10. (c) 1999-2002 Gerd Knorr
  11. some v4l2 code lines are taken from Justin's bttv2 driver which is
  12. (c) 2000 Justin Schoeman
  13. V4L1 removal from:
  14. (c) 2005-2006 Nickolay V. Shmyrev
  15. Fixes to be fully V4L2 compliant by
  16. (c) 2006 Mauro Carvalho Chehab
  17. Cropping and overscan support
  18. Copyright (C) 2005, 2006 Michael H. Schimek
  19. Sponsored by OPQ Systems AB
  20. This program is free software; you can redistribute it and/or modify
  21. it under the terms of the GNU General Public License as published by
  22. the Free Software Foundation; either version 2 of the License, or
  23. (at your option) any later version.
  24. This program is distributed in the hope that it will be useful,
  25. but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. GNU General Public License for more details.
  28. You should have received a copy of the GNU General Public License
  29. along with this program; if not, write to the Free Software
  30. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/pci.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/gpio.h>
  36. #include <linux/slab.h>
  37. /* Steal the hardware definitions from the bttv driver. */
  38. #include "../media/pci/bt8xx/bt848.h"
  39. #define BT8XXGPIO_NR_GPIOS 24 /* We have 24 GPIO pins */
  40. struct bt8xxgpio {
  41. spinlock_t lock;
  42. void __iomem *mmio;
  43. struct pci_dev *pdev;
  44. struct gpio_chip gpio;
  45. #ifdef CONFIG_PM
  46. u32 saved_outen;
  47. u32 saved_data;
  48. #endif
  49. };
  50. #define bgwrite(dat, adr) writel((dat), bg->mmio+(adr))
  51. #define bgread(adr) readl(bg->mmio+(adr))
  52. static int modparam_gpiobase = -1/* dynamic */;
  53. module_param_named(gpiobase, modparam_gpiobase, int, 0444);
  54. MODULE_PARM_DESC(gpiobase, "The GPIO number base. -1 means dynamic, which is the default.");
  55. static int bt8xxgpio_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
  56. {
  57. struct bt8xxgpio *bg = container_of(gpio, struct bt8xxgpio, gpio);
  58. unsigned long flags;
  59. u32 outen, data;
  60. spin_lock_irqsave(&bg->lock, flags);
  61. data = bgread(BT848_GPIO_DATA);
  62. data &= ~(1 << nr);
  63. bgwrite(data, BT848_GPIO_DATA);
  64. outen = bgread(BT848_GPIO_OUT_EN);
  65. outen &= ~(1 << nr);
  66. bgwrite(outen, BT848_GPIO_OUT_EN);
  67. spin_unlock_irqrestore(&bg->lock, flags);
  68. return 0;
  69. }
  70. static int bt8xxgpio_gpio_get(struct gpio_chip *gpio, unsigned nr)
  71. {
  72. struct bt8xxgpio *bg = container_of(gpio, struct bt8xxgpio, gpio);
  73. unsigned long flags;
  74. u32 val;
  75. spin_lock_irqsave(&bg->lock, flags);
  76. val = bgread(BT848_GPIO_DATA);
  77. spin_unlock_irqrestore(&bg->lock, flags);
  78. return !!(val & (1 << nr));
  79. }
  80. static int bt8xxgpio_gpio_direction_output(struct gpio_chip *gpio,
  81. unsigned nr, int val)
  82. {
  83. struct bt8xxgpio *bg = container_of(gpio, struct bt8xxgpio, gpio);
  84. unsigned long flags;
  85. u32 outen, data;
  86. spin_lock_irqsave(&bg->lock, flags);
  87. outen = bgread(BT848_GPIO_OUT_EN);
  88. outen |= (1 << nr);
  89. bgwrite(outen, BT848_GPIO_OUT_EN);
  90. data = bgread(BT848_GPIO_DATA);
  91. if (val)
  92. data |= (1 << nr);
  93. else
  94. data &= ~(1 << nr);
  95. bgwrite(data, BT848_GPIO_DATA);
  96. spin_unlock_irqrestore(&bg->lock, flags);
  97. return 0;
  98. }
  99. static void bt8xxgpio_gpio_set(struct gpio_chip *gpio,
  100. unsigned nr, int val)
  101. {
  102. struct bt8xxgpio *bg = container_of(gpio, struct bt8xxgpio, gpio);
  103. unsigned long flags;
  104. u32 data;
  105. spin_lock_irqsave(&bg->lock, flags);
  106. data = bgread(BT848_GPIO_DATA);
  107. if (val)
  108. data |= (1 << nr);
  109. else
  110. data &= ~(1 << nr);
  111. bgwrite(data, BT848_GPIO_DATA);
  112. spin_unlock_irqrestore(&bg->lock, flags);
  113. }
  114. static void bt8xxgpio_gpio_setup(struct bt8xxgpio *bg)
  115. {
  116. struct gpio_chip *c = &bg->gpio;
  117. c->label = dev_name(&bg->pdev->dev);
  118. c->owner = THIS_MODULE;
  119. c->direction_input = bt8xxgpio_gpio_direction_input;
  120. c->get = bt8xxgpio_gpio_get;
  121. c->direction_output = bt8xxgpio_gpio_direction_output;
  122. c->set = bt8xxgpio_gpio_set;
  123. c->dbg_show = NULL;
  124. c->base = modparam_gpiobase;
  125. c->ngpio = BT8XXGPIO_NR_GPIOS;
  126. c->can_sleep = false;
  127. }
  128. static int bt8xxgpio_probe(struct pci_dev *dev,
  129. const struct pci_device_id *pci_id)
  130. {
  131. struct bt8xxgpio *bg;
  132. int err;
  133. bg = devm_kzalloc(&dev->dev, sizeof(struct bt8xxgpio), GFP_KERNEL);
  134. if (!bg)
  135. return -ENOMEM;
  136. bg->pdev = dev;
  137. spin_lock_init(&bg->lock);
  138. err = pci_enable_device(dev);
  139. if (err) {
  140. printk(KERN_ERR "bt8xxgpio: Can't enable device.\n");
  141. return err;
  142. }
  143. if (!devm_request_mem_region(&dev->dev, pci_resource_start(dev, 0),
  144. pci_resource_len(dev, 0),
  145. "bt8xxgpio")) {
  146. printk(KERN_WARNING "bt8xxgpio: Can't request iomem (0x%llx).\n",
  147. (unsigned long long)pci_resource_start(dev, 0));
  148. err = -EBUSY;
  149. goto err_disable;
  150. }
  151. pci_set_master(dev);
  152. pci_set_drvdata(dev, bg);
  153. bg->mmio = devm_ioremap(&dev->dev, pci_resource_start(dev, 0), 0x1000);
  154. if (!bg->mmio) {
  155. printk(KERN_ERR "bt8xxgpio: ioremap() failed\n");
  156. err = -EIO;
  157. goto err_disable;
  158. }
  159. /* Disable interrupts */
  160. bgwrite(0, BT848_INT_MASK);
  161. /* gpio init */
  162. bgwrite(0, BT848_GPIO_DMA_CTL);
  163. bgwrite(0, BT848_GPIO_REG_INP);
  164. bgwrite(0, BT848_GPIO_OUT_EN);
  165. bt8xxgpio_gpio_setup(bg);
  166. err = gpiochip_add(&bg->gpio);
  167. if (err) {
  168. printk(KERN_ERR "bt8xxgpio: Failed to register GPIOs\n");
  169. goto err_disable;
  170. }
  171. return 0;
  172. err_disable:
  173. pci_disable_device(dev);
  174. return err;
  175. }
  176. static void bt8xxgpio_remove(struct pci_dev *pdev)
  177. {
  178. struct bt8xxgpio *bg = pci_get_drvdata(pdev);
  179. gpiochip_remove(&bg->gpio);
  180. bgwrite(0, BT848_INT_MASK);
  181. bgwrite(~0x0, BT848_INT_STAT);
  182. bgwrite(0x0, BT848_GPIO_OUT_EN);
  183. pci_disable_device(pdev);
  184. }
  185. #ifdef CONFIG_PM
  186. static int bt8xxgpio_suspend(struct pci_dev *pdev, pm_message_t state)
  187. {
  188. struct bt8xxgpio *bg = pci_get_drvdata(pdev);
  189. unsigned long flags;
  190. spin_lock_irqsave(&bg->lock, flags);
  191. bg->saved_outen = bgread(BT848_GPIO_OUT_EN);
  192. bg->saved_data = bgread(BT848_GPIO_DATA);
  193. bgwrite(0, BT848_INT_MASK);
  194. bgwrite(~0x0, BT848_INT_STAT);
  195. bgwrite(0x0, BT848_GPIO_OUT_EN);
  196. spin_unlock_irqrestore(&bg->lock, flags);
  197. pci_save_state(pdev);
  198. pci_disable_device(pdev);
  199. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  200. return 0;
  201. }
  202. static int bt8xxgpio_resume(struct pci_dev *pdev)
  203. {
  204. struct bt8xxgpio *bg = pci_get_drvdata(pdev);
  205. unsigned long flags;
  206. int err;
  207. pci_set_power_state(pdev, PCI_D0);
  208. err = pci_enable_device(pdev);
  209. if (err)
  210. return err;
  211. pci_restore_state(pdev);
  212. spin_lock_irqsave(&bg->lock, flags);
  213. bgwrite(0, BT848_INT_MASK);
  214. bgwrite(0, BT848_GPIO_DMA_CTL);
  215. bgwrite(0, BT848_GPIO_REG_INP);
  216. bgwrite(bg->saved_outen, BT848_GPIO_OUT_EN);
  217. bgwrite(bg->saved_data & bg->saved_outen,
  218. BT848_GPIO_DATA);
  219. spin_unlock_irqrestore(&bg->lock, flags);
  220. return 0;
  221. }
  222. #else
  223. #define bt8xxgpio_suspend NULL
  224. #define bt8xxgpio_resume NULL
  225. #endif /* CONFIG_PM */
  226. static const struct pci_device_id bt8xxgpio_pci_tbl[] = {
  227. { PCI_DEVICE(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848) },
  228. { PCI_DEVICE(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849) },
  229. { PCI_DEVICE(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878) },
  230. { PCI_DEVICE(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879) },
  231. { 0, },
  232. };
  233. MODULE_DEVICE_TABLE(pci, bt8xxgpio_pci_tbl);
  234. static struct pci_driver bt8xxgpio_pci_driver = {
  235. .name = "bt8xxgpio",
  236. .id_table = bt8xxgpio_pci_tbl,
  237. .probe = bt8xxgpio_probe,
  238. .remove = bt8xxgpio_remove,
  239. .suspend = bt8xxgpio_suspend,
  240. .resume = bt8xxgpio_resume,
  241. };
  242. module_pci_driver(bt8xxgpio_pci_driver);
  243. MODULE_LICENSE("GPL");
  244. MODULE_AUTHOR("Michael Buesch");
  245. MODULE_DESCRIPTION("Abuse a BT8xx framegrabber card as generic GPIO card");