1 // SPDX-License-Identifier: GPL-2.0-or-later
4 bttv-gpio.c -- gpio sub drivers
6 sysfs-based sub driver interface for bttv
7 mainly intended for gpio access
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>
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))
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 void 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);
58 struct bus_type bttv_sub_bus_type = {
60 .match = &bttv_sub_bus_match,
61 .probe = bttv_sub_probe,
62 .remove = bttv_sub_remove,
65 static void release_sub_device(struct device *dev)
67 struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
71 int bttv_sub_add_device(struct bttv_core *core, char *name)
73 struct bttv_sub_device *sub;
76 sub = kzalloc(sizeof(*sub),GFP_KERNEL);
81 sub->dev.parent = &core->pci->dev;
82 sub->dev.bus = &bttv_sub_bus_type;
83 sub->dev.release = release_sub_device;
84 dev_set_name(&sub->dev, "%s%d", name, core->nr);
86 err = device_register(&sub->dev);
88 put_device(&sub->dev);
91 pr_info("%d: add subdevice \"%s\"\n", core->nr, dev_name(&sub->dev));
92 list_add_tail(&sub->list,&core->subs);
96 int bttv_sub_del_devices(struct bttv_core *core)
98 struct bttv_sub_device *sub, *save;
100 list_for_each_entry_safe(sub, save, &core->subs, list) {
101 list_del(&sub->list);
102 device_unregister(&sub->dev);
107 /* ----------------------------------------------------------------------- */
108 /* external: sub-driver register/unregister */
110 int bttv_sub_register(struct bttv_sub_driver *sub, char *wanted)
112 sub->drv.bus = &bttv_sub_bus_type;
113 snprintf(sub->wanted,sizeof(sub->wanted),"%s",wanted);
114 return driver_register(&sub->drv);
116 EXPORT_SYMBOL(bttv_sub_register);
118 int bttv_sub_unregister(struct bttv_sub_driver *sub)
120 driver_unregister(&sub->drv);
123 EXPORT_SYMBOL(bttv_sub_unregister);
125 /* ----------------------------------------------------------------------- */
126 /* external: gpio access functions */
128 void bttv_gpio_inout(struct bttv_core *core, u32 mask, u32 outbits)
130 struct bttv *btv = container_of(core, struct bttv, c);
134 spin_lock_irqsave(&btv->gpio_lock,flags);
135 data = btread(BT848_GPIO_OUT_EN);
137 data = data | (mask & outbits);
138 btwrite(data,BT848_GPIO_OUT_EN);
139 spin_unlock_irqrestore(&btv->gpio_lock,flags);
142 u32 bttv_gpio_read(struct bttv_core *core)
144 struct bttv *btv = container_of(core, struct bttv, c);
147 value = btread(BT848_GPIO_DATA);
151 void bttv_gpio_write(struct bttv_core *core, u32 value)
153 struct bttv *btv = container_of(core, struct bttv, c);
155 btwrite(value,BT848_GPIO_DATA);
158 void bttv_gpio_bits(struct bttv_core *core, u32 mask, u32 bits)
160 struct bttv *btv = container_of(core, struct bttv, c);
164 spin_lock_irqsave(&btv->gpio_lock,flags);
165 data = btread(BT848_GPIO_DATA);
167 data = data | (mask & bits);
168 btwrite(data,BT848_GPIO_DATA);
169 spin_unlock_irqrestore(&btv->gpio_lock,flags);