WIP FPC-III support
[linux/fpc-iii.git] / drivers / media / pci / bt8xx / bttv-gpio.c
blobb730225ca8871be37d67974f5dd75021cd12eb5a
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
4 bttv-gpio.c -- gpio sub drivers
6 sysfs-based sub driver interface for bttv
7 mainly intended for gpio access
10 Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
11 & Marcus Metzler (mocm@thp.uni-koeln.de)
12 (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/slab.h>
24 #include <asm/io.h>
26 #include "bttvp.h"
28 /* ----------------------------------------------------------------------- */
29 /* internal: the bttv "bus" */
31 static int bttv_sub_bus_match(struct device *dev, struct device_driver *drv)
33 struct bttv_sub_driver *sub = to_bttv_sub_drv(drv);
34 int len = strlen(sub->wanted);
36 if (0 == strncmp(dev_name(dev), sub->wanted, len))
37 return 1;
38 return 0;
41 static int bttv_sub_probe(struct device *dev)
43 struct bttv_sub_device *sdev = to_bttv_sub_dev(dev);
44 struct bttv_sub_driver *sub = to_bttv_sub_drv(dev->driver);
46 return sub->probe ? sub->probe(sdev) : -ENODEV;
49 static int bttv_sub_remove(struct device *dev)
51 struct bttv_sub_device *sdev = to_bttv_sub_dev(dev);
52 struct bttv_sub_driver *sub = to_bttv_sub_drv(dev->driver);
54 if (sub->remove)
55 sub->remove(sdev);
56 return 0;
59 struct bus_type bttv_sub_bus_type = {
60 .name = "bttv-sub",
61 .match = &bttv_sub_bus_match,
62 .probe = bttv_sub_probe,
63 .remove = bttv_sub_remove,
66 static void release_sub_device(struct device *dev)
68 struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
69 kfree(sub);
72 int bttv_sub_add_device(struct bttv_core *core, char *name)
74 struct bttv_sub_device *sub;
75 int err;
77 sub = kzalloc(sizeof(*sub),GFP_KERNEL);
78 if (NULL == sub)
79 return -ENOMEM;
81 sub->core = core;
82 sub->dev.parent = &core->pci->dev;
83 sub->dev.bus = &bttv_sub_bus_type;
84 sub->dev.release = release_sub_device;
85 dev_set_name(&sub->dev, "%s%d", name, core->nr);
87 err = device_register(&sub->dev);
88 if (0 != err) {
89 put_device(&sub->dev);
90 return err;
92 pr_info("%d: add subdevice \"%s\"\n", core->nr, dev_name(&sub->dev));
93 list_add_tail(&sub->list,&core->subs);
94 return 0;
97 int bttv_sub_del_devices(struct bttv_core *core)
99 struct bttv_sub_device *sub, *save;
101 list_for_each_entry_safe(sub, save, &core->subs, list) {
102 list_del(&sub->list);
103 device_unregister(&sub->dev);
105 return 0;
108 /* ----------------------------------------------------------------------- */
109 /* external: sub-driver register/unregister */
111 int bttv_sub_register(struct bttv_sub_driver *sub, char *wanted)
113 sub->drv.bus = &bttv_sub_bus_type;
114 snprintf(sub->wanted,sizeof(sub->wanted),"%s",wanted);
115 return driver_register(&sub->drv);
117 EXPORT_SYMBOL(bttv_sub_register);
119 int bttv_sub_unregister(struct bttv_sub_driver *sub)
121 driver_unregister(&sub->drv);
122 return 0;
124 EXPORT_SYMBOL(bttv_sub_unregister);
126 /* ----------------------------------------------------------------------- */
127 /* external: gpio access functions */
129 void bttv_gpio_inout(struct bttv_core *core, u32 mask, u32 outbits)
131 struct bttv *btv = container_of(core, struct bttv, c);
132 unsigned long flags;
133 u32 data;
135 spin_lock_irqsave(&btv->gpio_lock,flags);
136 data = btread(BT848_GPIO_OUT_EN);
137 data = data & ~mask;
138 data = data | (mask & outbits);
139 btwrite(data,BT848_GPIO_OUT_EN);
140 spin_unlock_irqrestore(&btv->gpio_lock,flags);
143 u32 bttv_gpio_read(struct bttv_core *core)
145 struct bttv *btv = container_of(core, struct bttv, c);
146 u32 value;
148 value = btread(BT848_GPIO_DATA);
149 return value;
152 void bttv_gpio_write(struct bttv_core *core, u32 value)
154 struct bttv *btv = container_of(core, struct bttv, c);
156 btwrite(value,BT848_GPIO_DATA);
159 void bttv_gpio_bits(struct bttv_core *core, u32 mask, u32 bits)
161 struct bttv *btv = container_of(core, struct bttv, c);
162 unsigned long flags;
163 u32 data;
165 spin_lock_irqsave(&btv->gpio_lock,flags);
166 data = btread(BT848_GPIO_DATA);
167 data = data & ~mask;
168 data = data | (mask & bits);
169 btwrite(data,BT848_GPIO_DATA);
170 spin_unlock_irqrestore(&btv->gpio_lock,flags);