bt8xxgpio.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. bt8xx GPIO abuser
  3. Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
  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/video/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 = 0;
  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 = kzalloc(sizeof(*bg), 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. goto err_freebg;
  142. }
  143. if (!request_mem_region(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 = ioremap(pci_resource_start(dev, 0), 0x1000);
  154. if (!bg->mmio) {
  155. printk(KERN_ERR "bt8xxgpio: ioremap() failed\n");
  156. err = -EIO;
  157. goto err_release_mem;
  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_release_mem;
  170. }
  171. printk(KERN_INFO "bt8xxgpio: Abusing BT8xx card for GPIOs %d to %d\n",
  172. bg->gpio.base, bg->gpio.base + BT8XXGPIO_NR_GPIOS - 1);
  173. return 0;
  174. err_release_mem:
  175. release_mem_region(pci_resource_start(dev, 0),
  176. pci_resource_len(dev, 0));
  177. pci_set_drvdata(dev, NULL);
  178. err_disable:
  179. pci_disable_device(dev);
  180. err_freebg:
  181. kfree(bg);
  182. return err;
  183. }
  184. static void bt8xxgpio_remove(struct pci_dev *pdev)
  185. {
  186. struct bt8xxgpio *bg = pci_get_drvdata(pdev);
  187. gpiochip_remove(&bg->gpio);
  188. bgwrite(0, BT848_INT_MASK);
  189. bgwrite(~0x0, BT848_INT_STAT);
  190. bgwrite(0x0, BT848_GPIO_OUT_EN);
  191. iounmap(bg->mmio);
  192. release_mem_region(pci_resource_start(pdev, 0),
  193. pci_resource_len(pdev, 0));
  194. pci_disable_device(pdev);
  195. pci_set_drvdata(pdev, NULL);
  196. kfree(bg);
  197. }
  198. #ifdef CONFIG_PM
  199. static int bt8xxgpio_suspend(struct pci_dev *pdev, pm_message_t state)
  200. {
  201. struct bt8xxgpio *bg = pci_get_drvdata(pdev);
  202. unsigned long flags;
  203. spin_lock_irqsave(&bg->lock, flags);
  204. bg->saved_outen = bgread(BT848_GPIO_OUT_EN);
  205. bg->saved_data = bgread(BT848_GPIO_DATA);
  206. bgwrite(0, BT848_INT_MASK);
  207. bgwrite(~0x0, BT848_INT_STAT);
  208. bgwrite(0x0, BT848_GPIO_OUT_EN);
  209. spin_unlock_irqrestore(&bg->lock, flags);
  210. pci_save_state(pdev);
  211. pci_disable_device(pdev);
  212. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  213. return 0;
  214. }
  215. static int bt8xxgpio_resume(struct pci_dev *pdev)
  216. {
  217. struct bt8xxgpio *bg = pci_get_drvdata(pdev);
  218. unsigned long flags;
  219. int err;
  220. pci_set_power_state(pdev, 0);
  221. err = pci_enable_device(pdev);
  222. if (err)
  223. return err;
  224. pci_restore_state(pdev);
  225. spin_lock_irqsave(&bg->lock, flags);
  226. bgwrite(0, BT848_INT_MASK);
  227. bgwrite(0, BT848_GPIO_DMA_CTL);
  228. bgwrite(0, BT848_GPIO_REG_INP);
  229. bgwrite(bg->saved_outen, BT848_GPIO_OUT_EN);
  230. bgwrite(bg->saved_data & bg->saved_outen,
  231. BT848_GPIO_DATA);
  232. spin_unlock_irqrestore(&bg->lock, flags);
  233. return 0;
  234. }
  235. #else
  236. #define bt8xxgpio_suspend NULL
  237. #define bt8xxgpio_resume NULL
  238. #endif /* CONFIG_PM */
  239. static struct pci_device_id bt8xxgpio_pci_tbl[] = {
  240. { PCI_DEVICE(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848) },
  241. { PCI_DEVICE(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849) },
  242. { PCI_DEVICE(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878) },
  243. { PCI_DEVICE(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879) },
  244. { 0, },
  245. };
  246. MODULE_DEVICE_TABLE(pci, bt8xxgpio_pci_tbl);
  247. static struct pci_driver bt8xxgpio_pci_driver = {
  248. .name = "bt8xxgpio",
  249. .id_table = bt8xxgpio_pci_tbl,
  250. .probe = bt8xxgpio_probe,
  251. .remove = bt8xxgpio_remove,
  252. .suspend = bt8xxgpio_suspend,
  253. .resume = bt8xxgpio_resume,
  254. };
  255. static int __init bt8xxgpio_init(void)
  256. {
  257. return pci_register_driver(&bt8xxgpio_pci_driver);
  258. }
  259. module_init(bt8xxgpio_init)
  260. static void __exit bt8xxgpio_exit(void)
  261. {
  262. pci_unregister_driver(&bt8xxgpio_pci_driver);
  263. }
  264. module_exit(bt8xxgpio_exit)
  265. MODULE_LICENSE("GPL");
  266. MODULE_AUTHOR("Michael Buesch");
  267. MODULE_DESCRIPTION("Abuse a BT8xx framegrabber card as generic GPIO card");