bttv-gpio.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. bttv-gpio.c -- gpio sub drivers
  3. sysfs-based sub driver interface for bttv
  4. mainly intended 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/device.h>
  25. #include <linux/slab.h>
  26. #include <asm/io.h>
  27. #include "bttvp.h"
  28. /* ----------------------------------------------------------------------- */
  29. /* internal: the bttv "bus" */
  30. static int bttv_sub_bus_match(struct device *dev, struct device_driver *drv)
  31. {
  32. struct bttv_sub_driver *sub = to_bttv_sub_drv(drv);
  33. int len = strlen(sub->wanted);
  34. if (0 == strncmp(dev_name(dev), sub->wanted, len))
  35. return 1;
  36. return 0;
  37. }
  38. static int bttv_sub_probe(struct device *dev)
  39. {
  40. struct bttv_sub_device *sdev = to_bttv_sub_dev(dev);
  41. struct bttv_sub_driver *sub = to_bttv_sub_drv(dev->driver);
  42. return sub->probe ? sub->probe(sdev) : -ENODEV;
  43. }
  44. static int bttv_sub_remove(struct device *dev)
  45. {
  46. struct bttv_sub_device *sdev = to_bttv_sub_dev(dev);
  47. struct bttv_sub_driver *sub = to_bttv_sub_drv(dev->driver);
  48. if (sub->remove)
  49. sub->remove(sdev);
  50. return 0;
  51. }
  52. struct bus_type bttv_sub_bus_type = {
  53. .name = "bttv-sub",
  54. .match = &bttv_sub_bus_match,
  55. .probe = bttv_sub_probe,
  56. .remove = bttv_sub_remove,
  57. };
  58. static void release_sub_device(struct device *dev)
  59. {
  60. struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
  61. kfree(sub);
  62. }
  63. int bttv_sub_add_device(struct bttv_core *core, char *name)
  64. {
  65. struct bttv_sub_device *sub;
  66. int err;
  67. sub = kzalloc(sizeof(*sub),GFP_KERNEL);
  68. if (NULL == sub)
  69. return -ENOMEM;
  70. sub->core = core;
  71. sub->dev.parent = &core->pci->dev;
  72. sub->dev.bus = &bttv_sub_bus_type;
  73. sub->dev.release = release_sub_device;
  74. dev_set_name(&sub->dev, "%s%d", name, core->nr);
  75. err = device_register(&sub->dev);
  76. if (0 != err) {
  77. put_device(&sub->dev);
  78. return err;
  79. }
  80. pr_info("%d: add subdevice \"%s\"\n", core->nr, dev_name(&sub->dev));
  81. list_add_tail(&sub->list,&core->subs);
  82. return 0;
  83. }
  84. int bttv_sub_del_devices(struct bttv_core *core)
  85. {
  86. struct bttv_sub_device *sub, *save;
  87. list_for_each_entry_safe(sub, save, &core->subs, list) {
  88. list_del(&sub->list);
  89. device_unregister(&sub->dev);
  90. }
  91. return 0;
  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. u32 bttv_gpio_read(struct bttv_core *core)
  123. {
  124. struct bttv *btv = container_of(core, struct bttv, c);
  125. u32 value;
  126. value = btread(BT848_GPIO_DATA);
  127. return value;
  128. }
  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. void bttv_gpio_bits(struct bttv_core *core, u32 mask, u32 bits)
  135. {
  136. struct bttv *btv = container_of(core, struct bttv, c);
  137. unsigned long flags;
  138. u32 data;
  139. spin_lock_irqsave(&btv->gpio_lock,flags);
  140. data = btread(BT848_GPIO_DATA);
  141. data = data & ~mask;
  142. data = data | (mask & bits);
  143. btwrite(data,BT848_GPIO_DATA);
  144. spin_unlock_irqrestore(&btv->gpio_lock,flags);
  145. }