bttv-gpio.c 4.9 KB

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