blob: 74c325e594a2eaf3509b139ef6b46a7a2f4d242a [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
Kay Sieversaf128a12008-10-30 00:51:46 -030045 if (0 == strncmp(dev_name(dev), sub->wanted, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return 1;
47 return 0;
48}
49
Russell King348290a2006-01-06 11:42:03 +000050static int bttv_sub_probe(struct device *dev)
51{
52 struct bttv_sub_device *sdev = to_bttv_sub_dev(dev);
53 struct bttv_sub_driver *sub = to_bttv_sub_drv(dev->driver);
54
55 return sub->probe ? sub->probe(sdev) : -ENODEV;
56}
57
58static int bttv_sub_remove(struct device *dev)
59{
60 struct bttv_sub_device *sdev = to_bttv_sub_dev(dev);
61 struct bttv_sub_driver *sub = to_bttv_sub_drv(dev->driver);
62
63 if (sub->remove)
64 sub->remove(sdev);
65 return 0;
66}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068struct bus_type bttv_sub_bus_type = {
Russell King348290a2006-01-06 11:42:03 +000069 .name = "bttv-sub",
70 .match = &bttv_sub_bus_match,
71 .probe = bttv_sub_probe,
72 .remove = bttv_sub_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75static void release_sub_device(struct device *dev)
76{
77 struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
78 kfree(sub);
79}
80
81int bttv_sub_add_device(struct bttv_core *core, char *name)
82{
83 struct bttv_sub_device *sub;
84 int err;
85
Panagiotis Issaris74081872006-01-11 19:40:56 -020086 sub = kzalloc(sizeof(*sub),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (NULL == sub)
88 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 sub->core = core;
91 sub->dev.parent = &core->pci->dev;
92 sub->dev.bus = &bttv_sub_bus_type;
93 sub->dev.release = release_sub_device;
Kay Sieversaf128a12008-10-30 00:51:46 -030094 dev_set_name(&sub->dev, "%s%d", name, core->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 err = device_register(&sub->dev);
97 if (0 != err) {
98 kfree(sub);
99 return err;
100 }
Kay Sieversaf128a12008-10-30 00:51:46 -0300101 printk("bttv%d: add subdevice \"%s\"\n", core->nr, dev_name(&sub->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 list_add_tail(&sub->list,&core->subs);
103 return 0;
104}
105
106int bttv_sub_del_devices(struct bttv_core *core)
107{
Trent Piephoa991f442007-10-10 05:37:43 -0300108 struct bttv_sub_device *sub, *save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Trent Piephoa991f442007-10-10 05:37:43 -0300110 list_for_each_entry_safe(sub, save, &core->subs, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 list_del(&sub->list);
112 device_unregister(&sub->dev);
113 }
114 return 0;
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* ----------------------------------------------------------------------- */
118/* external: sub-driver register/unregister */
119
120int bttv_sub_register(struct bttv_sub_driver *sub, char *wanted)
121{
122 sub->drv.bus = &bttv_sub_bus_type;
123 snprintf(sub->wanted,sizeof(sub->wanted),"%s",wanted);
124 return driver_register(&sub->drv);
125}
126EXPORT_SYMBOL(bttv_sub_register);
127
128int bttv_sub_unregister(struct bttv_sub_driver *sub)
129{
130 driver_unregister(&sub->drv);
131 return 0;
132}
133EXPORT_SYMBOL(bttv_sub_unregister);
134
135/* ----------------------------------------------------------------------- */
136/* external: gpio access functions */
137
138void bttv_gpio_inout(struct bttv_core *core, u32 mask, u32 outbits)
139{
140 struct bttv *btv = container_of(core, struct bttv, c);
141 unsigned long flags;
142 u32 data;
143
144 spin_lock_irqsave(&btv->gpio_lock,flags);
145 data = btread(BT848_GPIO_OUT_EN);
146 data = data & ~mask;
147 data = data | (mask & outbits);
148 btwrite(data,BT848_GPIO_OUT_EN);
149 spin_unlock_irqrestore(&btv->gpio_lock,flags);
150}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152u32 bttv_gpio_read(struct bttv_core *core)
153{
154 struct bttv *btv = container_of(core, struct bttv, c);
155 u32 value;
156
157 value = btread(BT848_GPIO_DATA);
158 return value;
159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168void bttv_gpio_bits(struct bttv_core *core, u32 mask, u32 bits)
169{
170 struct bttv *btv = container_of(core, struct bttv, c);
171 unsigned long flags;
172 u32 data;
173
174 spin_lock_irqsave(&btv->gpio_lock,flags);
175 data = btread(BT848_GPIO_DATA);
176 data = data & ~mask;
177 data = data | (mask & bits);
178 btwrite(data,BT848_GPIO_DATA);
179 spin_unlock_irqrestore(&btv->gpio_lock,flags);
180}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/*
183 * Local variables:
184 * c-basic-offset: 8
185 * End:
186 */