blob: d64accc17b0ef7854438845f6b4cb49096988e89 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
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)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080010 & Marcus Metzler (mocm@thp.uni-koeln.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 (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
40static 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
50struct bus_type bttv_sub_bus_type = {
51 .name = "bttv-sub",
52 .match = &bttv_sub_bus_match,
53};
54EXPORT_SYMBOL(bttv_sub_bus_type);
55
56static void release_sub_device(struct device *dev)
57{
58 struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
59 kfree(sub);
60}
61
62int bttv_sub_add_device(struct bttv_core *core, char *name)
63{
64 struct bttv_sub_device *sub;
65 int err;
66
Panagiotis Issaris74081872006-01-11 19:40:56 -020067 sub = kzalloc(sizeof(*sub),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (NULL == sub)
69 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 sub->core = core;
72 sub->dev.parent = &core->pci->dev;
73 sub->dev.bus = &bttv_sub_bus_type;
74 sub->dev.release = release_sub_device;
75 snprintf(sub->dev.bus_id,sizeof(sub->dev.bus_id),"%s%d",
76 name, core->nr);
77
78 err = device_register(&sub->dev);
79 if (0 != err) {
80 kfree(sub);
81 return err;
82 }
83 printk("bttv%d: add subdevice \"%s\"\n", core->nr, sub->dev.bus_id);
84 list_add_tail(&sub->list,&core->subs);
85 return 0;
86}
87
88int bttv_sub_del_devices(struct bttv_core *core)
89{
90 struct bttv_sub_device *sub;
91 struct list_head *item,*save;
92
93 list_for_each_safe(item,save,&core->subs) {
94 sub = list_entry(item,struct bttv_sub_device,list);
95 list_del(&sub->list);
96 device_unregister(&sub->dev);
97 }
98 return 0;
99}
100
101void bttv_gpio_irq(struct bttv_core *core)
102{
103 struct bttv_sub_driver *drv;
104 struct bttv_sub_device *dev;
105 struct list_head *item;
106
107 list_for_each(item,&core->subs) {
108 dev = list_entry(item,struct bttv_sub_device,list);
109 drv = to_bttv_sub_drv(dev->dev.driver);
110 if (drv && drv->gpio_irq)
111 drv->gpio_irq(dev);
112 }
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* ----------------------------------------------------------------------- */
116/* external: sub-driver register/unregister */
117
118int bttv_sub_register(struct bttv_sub_driver *sub, char *wanted)
119{
120 sub->drv.bus = &bttv_sub_bus_type;
121 snprintf(sub->wanted,sizeof(sub->wanted),"%s",wanted);
122 return driver_register(&sub->drv);
123}
124EXPORT_SYMBOL(bttv_sub_register);
125
126int bttv_sub_unregister(struct bttv_sub_driver *sub)
127{
128 driver_unregister(&sub->drv);
129 return 0;
130}
131EXPORT_SYMBOL(bttv_sub_unregister);
132
133/* ----------------------------------------------------------------------- */
134/* external: gpio access functions */
135
136void bttv_gpio_inout(struct bttv_core *core, u32 mask, u32 outbits)
137{
138 struct bttv *btv = container_of(core, struct bttv, c);
139 unsigned long flags;
140 u32 data;
141
142 spin_lock_irqsave(&btv->gpio_lock,flags);
143 data = btread(BT848_GPIO_OUT_EN);
144 data = data & ~mask;
145 data = data | (mask & outbits);
146 btwrite(data,BT848_GPIO_OUT_EN);
147 spin_unlock_irqrestore(&btv->gpio_lock,flags);
148}
149EXPORT_SYMBOL(bttv_gpio_inout);
150
151u32 bttv_gpio_read(struct bttv_core *core)
152{
153 struct bttv *btv = container_of(core, struct bttv, c);
154 u32 value;
155
156 value = btread(BT848_GPIO_DATA);
157 return value;
158}
159EXPORT_SYMBOL(bttv_gpio_read);
160
161void bttv_gpio_write(struct bttv_core *core, u32 value)
162{
163 struct bttv *btv = container_of(core, struct bttv, c);
164
165 btwrite(value,BT848_GPIO_DATA);
166}
167EXPORT_SYMBOL(bttv_gpio_write);
168
169void bttv_gpio_bits(struct bttv_core *core, u32 mask, u32 bits)
170{
171 struct bttv *btv = container_of(core, struct bttv, c);
172 unsigned long flags;
173 u32 data;
174
175 spin_lock_irqsave(&btv->gpio_lock,flags);
176 data = btread(BT848_GPIO_DATA);
177 data = data & ~mask;
178 data = data | (mask & bits);
179 btwrite(data,BT848_GPIO_DATA);
180 spin_unlock_irqrestore(&btv->gpio_lock,flags);
181}
182EXPORT_SYMBOL(bttv_gpio_bits);
183
184/*
185 * Local variables:
186 * c-basic-offset: 8
187 * End:
188 */