bttv-gpio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. bttv-gpio.c -- gpio sub drivers
  3. sysfs-based sub driver interface for bttv
  4. mainly intented for gpio access
  5. Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
  6. & Marcus Metzler (mocm@thp.uni-koeln.de)
  7. (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/device.h>
  24. #include <asm/io.h>
  25. #include "bttvp.h"
  26. /* ----------------------------------------------------------------------- */
  27. /* internal: the bttv "bus" */
  28. static int bttv_sub_bus_match(struct device *dev, struct device_driver *drv)
  29. {
  30. struct bttv_sub_driver *sub = to_bttv_sub_drv(drv);
  31. int len = strlen(sub->wanted);
  32. if (0 == strncmp(dev->bus_id, sub->wanted, len))
  33. return 1;
  34. return 0;
  35. }
  36. struct bus_type bttv_sub_bus_type = {
  37. .name = "bttv-sub",
  38. .match = &bttv_sub_bus_match,
  39. };
  40. EXPORT_SYMBOL(bttv_sub_bus_type);
  41. static void release_sub_device(struct device *dev)
  42. {
  43. struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
  44. kfree(sub);
  45. }
  46. int bttv_sub_add_device(struct bttv_core *core, char *name)
  47. {
  48. struct bttv_sub_device *sub;
  49. int err;
  50. sub = kzalloc(sizeof(*sub),GFP_KERNEL);
  51. if (NULL == sub)
  52. return -ENOMEM;
  53. sub->core = core;
  54. sub->dev.parent = &core->pci->dev;
  55. sub->dev.bus = &bttv_sub_bus_type;
  56. sub->dev.release = release_sub_device;
  57. snprintf(sub->dev.bus_id,sizeof(sub->dev.bus_id),"%s%d",
  58. name, core->nr);
  59. err = device_register(&sub->dev);
  60. if (0 != err) {
  61. kfree(sub);
  62. return err;
  63. }
  64. printk("bttv%d: add subdevice \"%s\"\n", core->nr, sub->dev.bus_id);
  65. list_add_tail(&sub->list,&core->subs);
  66. return 0;
  67. }
  68. int bttv_sub_del_devices(struct bttv_core *core)
  69. {
  70. struct bttv_sub_device *sub;
  71. struct list_head *item,*save;
  72. list_for_each_safe(item,save,&core->subs) {
  73. sub = list_entry(item,struct bttv_sub_device,list);
  74. list_del(&sub->list);
  75. device_unregister(&sub->dev);
  76. }
  77. return 0;
  78. }
  79. void bttv_gpio_irq(struct bttv_core *core)
  80. {
  81. struct bttv_sub_driver *drv;
  82. struct bttv_sub_device *dev;
  83. struct list_head *item;
  84. list_for_each(item,&core->subs) {
  85. dev = list_entry(item,struct bttv_sub_device,list);
  86. drv = to_bttv_sub_drv(dev->dev.driver);
  87. if (drv && drv->gpio_irq)
  88. drv->gpio_irq(dev);
  89. }
  90. }
  91. /* ----------------------------------------------------------------------- */
  92. /* external: sub-driver register/unregister */
  93. int bttv_sub_register(struct bttv_sub_driver *sub, char *wanted)
  94. {
  95. sub->drv.bus = &bttv_sub_bus_type;
  96. snprintf(sub->wanted,sizeof(sub->wanted),"%s",wanted);
  97. return driver_register(&sub->drv);
  98. }
  99. EXPORT_SYMBOL(bttv_sub_register);
  100. int bttv_sub_unregister(struct bttv_sub_driver *sub)
  101. {
  102. driver_unregister(&sub->drv);
  103. return 0;
  104. }
  105. EXPORT_SYMBOL(bttv_sub_unregister);
  106. /* ----------------------------------------------------------------------- */
  107. /* external: gpio access functions */
  108. void bttv_gpio_inout(struct bttv_core *core, u32 mask, u32 outbits)
  109. {
  110. struct bttv *btv = container_of(core, struct bttv, c);
  111. unsigned long flags;
  112. u32 data;
  113. spin_lock_irqsave(&btv->gpio_lock,flags);
  114. data = btread(BT848_GPIO_OUT_EN);
  115. data = data & ~mask;
  116. data = data | (mask & outbits);
  117. btwrite(data,BT848_GPIO_OUT_EN);
  118. spin_unlock_irqrestore(&btv->gpio_lock,flags);
  119. }
  120. EXPORT_SYMBOL(bttv_gpio_inout);
  121. u32 bttv_gpio_read(struct bttv_core *core)
  122. {
  123. struct bttv *btv = container_of(core, struct bttv, c);
  124. u32 value;
  125. value = btread(BT848_GPIO_DATA);
  126. return value;
  127. }
  128. EXPORT_SYMBOL(bttv_gpio_read);
  129. void bttv_gpio_write(struct bttv_core *core, u32 value)
  130. {
  131. struct bttv *btv = container_of(core, struct bttv, c);
  132. btwrite(value,BT848_GPIO_DATA);
  133. }
  134. EXPORT_SYMBOL(bttv_gpio_write);
  135. void bttv_gpio_bits(struct bttv_core *core, u32 mask, u32 bits)
  136. {
  137. struct bttv *btv = container_of(core, struct bttv, c);
  138. unsigned long flags;
  139. u32 data;
  140. spin_lock_irqsave(&btv->gpio_lock,flags);
  141. data = btread(BT848_GPIO_DATA);
  142. data = data & ~mask;
  143. data = data | (mask & bits);
  144. btwrite(data,BT848_GPIO_DATA);
  145. spin_unlock_irqrestore(&btv->gpio_lock,flags);
  146. }
  147. EXPORT_SYMBOL(bttv_gpio_bits);
  148. /*
  149. * Local variables:
  150. * c-basic-offset: 8
  151. * End:
  152. */