Merge x86-64 update from Andi
[cascardo/linux.git] / drivers / media / video / bttv-gpio.c
1 /*
2
3     bttv-gpio.c  --  gpio sub drivers
4
5     sysfs-based sub driver interface for bttv
6     mainly intented for gpio access
7
8
9     Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
10                            & Marcus Metzler (mocm@thp.uni-koeln.de)
11     (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
12
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 */
28
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/delay.h>
32 #include <linux/device.h>
33 #include <asm/io.h>
34
35 #include "bttvp.h"
36
37 /* ----------------------------------------------------------------------- */
38 /* internal: the bttv "bus"                                                */
39
40 static int bttv_sub_bus_match(struct device *dev, struct device_driver *drv)
41 {
42         struct bttv_sub_driver *sub = to_bttv_sub_drv(drv);
43         int len = strlen(sub->wanted);
44
45         if (0 == strncmp(dev->bus_id, sub->wanted, len))
46                 return 1;
47         return 0;
48 }
49
50 struct bus_type bttv_sub_bus_type = {
51         .name  = "bttv-sub",
52         .match = &bttv_sub_bus_match,
53 };
54 EXPORT_SYMBOL(bttv_sub_bus_type);
55
56 static void release_sub_device(struct device *dev)
57 {
58         struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
59         kfree(sub);
60 }
61
62 int bttv_sub_add_device(struct bttv_core *core, char *name)
63 {
64         struct bttv_sub_device *sub;
65         int err;
66
67         sub = kmalloc(sizeof(*sub),GFP_KERNEL);
68         if (NULL == sub)
69                 return -ENOMEM;
70         memset(sub,0,sizeof(*sub));
71
72         sub->core        = core;
73         sub->dev.parent  = &core->pci->dev;
74         sub->dev.bus     = &bttv_sub_bus_type;
75         sub->dev.release = release_sub_device;
76         snprintf(sub->dev.bus_id,sizeof(sub->dev.bus_id),"%s%d",
77                  name, core->nr);
78
79         err = device_register(&sub->dev);
80         if (0 != err) {
81                 kfree(sub);
82                 return err;
83         }
84         printk("bttv%d: add subdevice \"%s\"\n", core->nr, sub->dev.bus_id);
85         list_add_tail(&sub->list,&core->subs);
86         return 0;
87 }
88
89 int bttv_sub_del_devices(struct bttv_core *core)
90 {
91         struct bttv_sub_device *sub;
92         struct list_head *item,*save;
93
94         list_for_each_safe(item,save,&core->subs) {
95                 sub = list_entry(item,struct bttv_sub_device,list);
96                 list_del(&sub->list);
97                 device_unregister(&sub->dev);
98         }
99         return 0;
100 }
101
102 void bttv_gpio_irq(struct bttv_core *core)
103 {
104         struct bttv_sub_driver *drv;
105         struct bttv_sub_device *dev;
106         struct list_head *item;
107
108         list_for_each(item,&core->subs) {
109                 dev = list_entry(item,struct bttv_sub_device,list);
110                 drv = to_bttv_sub_drv(dev->dev.driver);
111                 if (drv && drv->gpio_irq)
112                         drv->gpio_irq(dev);
113         }
114 }
115
116 int bttv_any_irq(struct bttv_core *core)
117 {
118         struct bttv_sub_driver *drv;
119         struct bttv_sub_device *dev;
120         struct list_head *item;
121         int handled = 0;
122
123         list_for_each(item,&core->subs) {
124                 dev = list_entry(item,struct bttv_sub_device,list);
125                 drv = to_bttv_sub_drv(dev->dev.driver);
126                 if (drv && drv->any_irq) {
127                         if (drv->any_irq(dev))
128                                 handled = 1;
129                 }
130         }
131         return handled;
132 }
133
134 /* ----------------------------------------------------------------------- */
135 /* external: sub-driver register/unregister                                */
136
137 int bttv_sub_register(struct bttv_sub_driver *sub, char *wanted)
138 {
139         sub->drv.bus = &bttv_sub_bus_type;
140         snprintf(sub->wanted,sizeof(sub->wanted),"%s",wanted);
141         return driver_register(&sub->drv);
142 }
143 EXPORT_SYMBOL(bttv_sub_register);
144
145 int bttv_sub_unregister(struct bttv_sub_driver *sub)
146 {
147         driver_unregister(&sub->drv);
148         return 0;
149 }
150 EXPORT_SYMBOL(bttv_sub_unregister);
151
152 /* ----------------------------------------------------------------------- */
153 /* external: gpio access functions                                         */
154
155 void bttv_gpio_inout(struct bttv_core *core, u32 mask, u32 outbits)
156 {
157         struct bttv *btv = container_of(core, struct bttv, c);
158         unsigned long flags;
159         u32 data;
160
161         spin_lock_irqsave(&btv->gpio_lock,flags);
162         data = btread(BT848_GPIO_OUT_EN);
163         data = data & ~mask;
164         data = data | (mask & outbits);
165         btwrite(data,BT848_GPIO_OUT_EN);
166         spin_unlock_irqrestore(&btv->gpio_lock,flags);
167 }
168 EXPORT_SYMBOL(bttv_gpio_inout);
169
170 u32 bttv_gpio_read(struct bttv_core *core)
171 {
172         struct bttv *btv = container_of(core, struct bttv, c);
173         u32 value;
174
175         value = btread(BT848_GPIO_DATA);
176         return value;
177 }
178 EXPORT_SYMBOL(bttv_gpio_read);
179
180 void bttv_gpio_write(struct bttv_core *core, u32 value)
181 {
182         struct bttv *btv = container_of(core, struct bttv, c);
183
184         btwrite(value,BT848_GPIO_DATA);
185 }
186 EXPORT_SYMBOL(bttv_gpio_write);
187
188 void bttv_gpio_bits(struct bttv_core *core, u32 mask, u32 bits)
189 {
190         struct bttv *btv = container_of(core, struct bttv, c);
191         unsigned long flags;
192         u32 data;
193
194         spin_lock_irqsave(&btv->gpio_lock,flags);
195         data = btread(BT848_GPIO_DATA);
196         data = data & ~mask;
197         data = data | (mask & bits);
198         btwrite(data,BT848_GPIO_DATA);
199         spin_unlock_irqrestore(&btv->gpio_lock,flags);
200 }
201 EXPORT_SYMBOL(bttv_gpio_bits);
202
203 /*
204  * Local variables:
205  * c-basic-offset: 8
206  * End:
207  */