1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3
4 bttv-gpio.c -- gpio sub drivers
5
6 sysfs-based sub driver interface for bttv
7 mainly intended for gpio access
8
9
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>
13
14
15 */
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
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>
25
26 #include "bttvp.h"
27
28 /* ----------------------------------------------------------------------- */
29 /* internal: the bttv "bus" */
30
bttv_sub_bus_match(struct device * dev,struct device_driver * drv)31 static int bttv_sub_bus_match(struct device *dev, struct device_driver *drv)
32 {
33 struct bttv_sub_driver *sub = to_bttv_sub_drv(drv);
34 int len = strlen(sub->wanted);
35
36 if (0 == strncmp(dev_name(dev), sub->wanted, len))
37 return 1;
38 return 0;
39 }
40
bttv_sub_probe(struct device * dev)41 static int bttv_sub_probe(struct device *dev)
42 {
43 struct bttv_sub_device *sdev = to_bttv_sub_dev(dev);
44 struct bttv_sub_driver *sub = to_bttv_sub_drv(dev->driver);
45
46 return sub->probe ? sub->probe(sdev) : -ENODEV;
47 }
48
bttv_sub_remove(struct device * dev)49 static int bttv_sub_remove(struct device *dev)
50 {
51 struct bttv_sub_device *sdev = to_bttv_sub_dev(dev);
52 struct bttv_sub_driver *sub = to_bttv_sub_drv(dev->driver);
53
54 if (sub->remove)
55 sub->remove(sdev);
56 return 0;
57 }
58
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,
64 };
65
release_sub_device(struct device * dev)66 static void release_sub_device(struct device *dev)
67 {
68 struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
69 kfree(sub);
70 }
71
bttv_sub_add_device(struct bttv_core * core,char * name)72 int bttv_sub_add_device(struct bttv_core *core, char *name)
73 {
74 struct bttv_sub_device *sub;
75 int err;
76
77 sub = kzalloc(sizeof(*sub),GFP_KERNEL);
78 if (NULL == sub)
79 return -ENOMEM;
80
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);
86
87 err = device_register(&sub->dev);
88 if (0 != err) {
89 put_device(&sub->dev);
90 return err;
91 }
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;
95 }
96
bttv_sub_del_devices(struct bttv_core * core)97 int bttv_sub_del_devices(struct bttv_core *core)
98 {
99 struct bttv_sub_device *sub, *save;
100
101 list_for_each_entry_safe(sub, save, &core->subs, list) {
102 list_del(&sub->list);
103 device_unregister(&sub->dev);
104 }
105 return 0;
106 }
107
108 /* ----------------------------------------------------------------------- */
109 /* external: sub-driver register/unregister */
110
bttv_sub_register(struct bttv_sub_driver * sub,char * wanted)111 int bttv_sub_register(struct bttv_sub_driver *sub, char *wanted)
112 {
113 sub->drv.bus = &bttv_sub_bus_type;
114 snprintf(sub->wanted,sizeof(sub->wanted),"%s",wanted);
115 return driver_register(&sub->drv);
116 }
117 EXPORT_SYMBOL(bttv_sub_register);
118
bttv_sub_unregister(struct bttv_sub_driver * sub)119 int bttv_sub_unregister(struct bttv_sub_driver *sub)
120 {
121 driver_unregister(&sub->drv);
122 return 0;
123 }
124 EXPORT_SYMBOL(bttv_sub_unregister);
125
126 /* ----------------------------------------------------------------------- */
127 /* external: gpio access functions */
128
bttv_gpio_inout(struct bttv_core * core,u32 mask,u32 outbits)129 void bttv_gpio_inout(struct bttv_core *core, u32 mask, u32 outbits)
130 {
131 struct bttv *btv = container_of(core, struct bttv, c);
132 unsigned long flags;
133 u32 data;
134
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);
141 }
142
bttv_gpio_read(struct bttv_core * core)143 u32 bttv_gpio_read(struct bttv_core *core)
144 {
145 struct bttv *btv = container_of(core, struct bttv, c);
146 u32 value;
147
148 value = btread(BT848_GPIO_DATA);
149 return value;
150 }
151
bttv_gpio_write(struct bttv_core * core,u32 value)152 void bttv_gpio_write(struct bttv_core *core, u32 value)
153 {
154 struct bttv *btv = container_of(core, struct bttv, c);
155
156 btwrite(value,BT848_GPIO_DATA);
157 }
158
bttv_gpio_bits(struct bttv_core * core,u32 mask,u32 bits)159 void bttv_gpio_bits(struct bttv_core *core, u32 mask, u32 bits)
160 {
161 struct bttv *btv = container_of(core, struct bttv, c);
162 unsigned long flags;
163 u32 data;
164
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);
171 }
172