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