Blame view

drivers/gpio/gpio-bt8xx.c 7.05 KB
74ba9207e   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
2
3
4
  /*
  
      bt8xx GPIO abuser
eb032b983   Michael Buesch   Update my e-mail ...
5
      Copyright (C) 2008 Michael Buesch <m@bues.ch>
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  
      Please do _only_ contact the people listed _above_ with issues related to this driver.
      All the other people listed below are not related to this driver. Their names
      are only here, because this driver is derived from the bt848 driver.
  
  
      Derived from the bt848 driver:
  
      Copyright (C) 1996,97,98 Ralph  Metzler
  			   & Marcus Metzler
      (c) 1999-2002 Gerd Knorr
  
      some v4l2 code lines are taken from Justin's bttv2 driver which is
      (c) 2000 Justin Schoeman
  
      V4L1 removal from:
      (c) 2005-2006 Nickolay V. Shmyrev
  
      Fixes to be fully V4L2 compliant by
      (c) 2006 Mauro Carvalho Chehab
  
      Cropping and overscan support
      Copyright (C) 2005, 2006 Michael H. Schimek
      Sponsored by OPQ Systems AB
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
30
31
32
33
34
  */
  
  #include <linux/module.h>
  #include <linux/pci.h>
  #include <linux/spinlock.h>
e88a2ae2b   Linus Walleij   gpio: bt8xx: Incl...
35
  #include <linux/gpio/driver.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
36
  #include <linux/slab.h>
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
37
38
  
  /* Steal the hardware definitions from the bttv driver. */
52d322582   Axel Lin   gpio: bt8xx: Fix ...
39
  #include "../media/pci/bt8xx/bt848.h"
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  
  
  #define BT8XXGPIO_NR_GPIOS		24 /* We have 24 GPIO pins */
  
  
  struct bt8xxgpio {
  	spinlock_t lock;
  
  	void __iomem *mmio;
  	struct pci_dev *pdev;
  	struct gpio_chip gpio;
  
  #ifdef CONFIG_PM
  	u32 saved_outen;
  	u32 saved_data;
  #endif
  };
  
  #define bgwrite(dat, adr)	writel((dat), bg->mmio+(adr))
  #define bgread(adr)		readl(bg->mmio+(adr))
  
  
  static int modparam_gpiobase = -1/* dynamic */;
  module_param_named(gpiobase, modparam_gpiobase, int, 0444);
  MODULE_PARM_DESC(gpiobase, "The GPIO number base. -1 means dynamic, which is the default.");
  
  
  static int bt8xxgpio_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
  {
e51b55236   Linus Walleij   gpio: bt8xx: use ...
69
  	struct bt8xxgpio *bg = gpiochip_get_data(gpio);
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  	unsigned long flags;
  	u32 outen, data;
  
  	spin_lock_irqsave(&bg->lock, flags);
  
  	data = bgread(BT848_GPIO_DATA);
  	data &= ~(1 << nr);
  	bgwrite(data, BT848_GPIO_DATA);
  
  	outen = bgread(BT848_GPIO_OUT_EN);
  	outen &= ~(1 << nr);
  	bgwrite(outen, BT848_GPIO_OUT_EN);
  
  	spin_unlock_irqrestore(&bg->lock, flags);
  
  	return 0;
  }
  
  static int bt8xxgpio_gpio_get(struct gpio_chip *gpio, unsigned nr)
  {
e51b55236   Linus Walleij   gpio: bt8xx: use ...
90
  	struct bt8xxgpio *bg = gpiochip_get_data(gpio);
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
91
92
93
94
95
96
97
98
99
100
101
102
103
  	unsigned long flags;
  	u32 val;
  
  	spin_lock_irqsave(&bg->lock, flags);
  	val = bgread(BT848_GPIO_DATA);
  	spin_unlock_irqrestore(&bg->lock, flags);
  
  	return !!(val & (1 << nr));
  }
  
  static int bt8xxgpio_gpio_direction_output(struct gpio_chip *gpio,
  					unsigned nr, int val)
  {
e51b55236   Linus Walleij   gpio: bt8xx: use ...
104
  	struct bt8xxgpio *bg = gpiochip_get_data(gpio);
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  	unsigned long flags;
  	u32 outen, data;
  
  	spin_lock_irqsave(&bg->lock, flags);
  
  	outen = bgread(BT848_GPIO_OUT_EN);
  	outen |= (1 << nr);
  	bgwrite(outen, BT848_GPIO_OUT_EN);
  
  	data = bgread(BT848_GPIO_DATA);
  	if (val)
  		data |= (1 << nr);
  	else
  		data &= ~(1 << nr);
  	bgwrite(data, BT848_GPIO_DATA);
  
  	spin_unlock_irqrestore(&bg->lock, flags);
  
  	return 0;
  }
  
  static void bt8xxgpio_gpio_set(struct gpio_chip *gpio,
  			    unsigned nr, int val)
  {
e51b55236   Linus Walleij   gpio: bt8xx: use ...
129
  	struct bt8xxgpio *bg = gpiochip_get_data(gpio);
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  	unsigned long flags;
  	u32 data;
  
  	spin_lock_irqsave(&bg->lock, flags);
  
  	data = bgread(BT848_GPIO_DATA);
  	if (val)
  		data |= (1 << nr);
  	else
  		data &= ~(1 << nr);
  	bgwrite(data, BT848_GPIO_DATA);
  
  	spin_unlock_irqrestore(&bg->lock, flags);
  }
  
  static void bt8xxgpio_gpio_setup(struct bt8xxgpio *bg)
  {
  	struct gpio_chip *c = &bg->gpio;
3e274bd02   Kay Sievers   gpio: struct devi...
148
  	c->label = dev_name(&bg->pdev->dev);
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
149
150
151
152
153
154
155
156
  	c->owner = THIS_MODULE;
  	c->direction_input = bt8xxgpio_gpio_direction_input;
  	c->get = bt8xxgpio_gpio_get;
  	c->direction_output = bt8xxgpio_gpio_direction_output;
  	c->set = bt8xxgpio_gpio_set;
  	c->dbg_show = NULL;
  	c->base = modparam_gpiobase;
  	c->ngpio = BT8XXGPIO_NR_GPIOS;
9fb1f39eb   Linus Walleij   gpio/pinctrl: mak...
157
  	c->can_sleep = false;
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
158
159
160
161
162
163
164
  }
  
  static int bt8xxgpio_probe(struct pci_dev *dev,
  			const struct pci_device_id *pci_id)
  {
  	struct bt8xxgpio *bg;
  	int err;
a435e1802   abdoulaye berthe   gpio: bt8xx: use ...
165
  	bg = devm_kzalloc(&dev->dev, sizeof(struct bt8xxgpio), GFP_KERNEL);
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
166
167
168
169
170
171
172
173
174
175
  	if (!bg)
  		return -ENOMEM;
  
  	bg->pdev = dev;
  	spin_lock_init(&bg->lock);
  
  	err = pci_enable_device(dev);
  	if (err) {
  		printk(KERN_ERR "bt8xxgpio: Can't enable device.
  ");
a435e1802   abdoulaye berthe   gpio: bt8xx: use ...
176
  		return err;
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
177
  	}
a435e1802   abdoulaye berthe   gpio: bt8xx: use ...
178
  	if (!devm_request_mem_region(&dev->dev, pci_resource_start(dev, 0),
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
179
180
181
182
183
184
185
186
187
188
  				pci_resource_len(dev, 0),
  				"bt8xxgpio")) {
  		printk(KERN_WARNING "bt8xxgpio: Can't request iomem (0x%llx).
  ",
  		       (unsigned long long)pci_resource_start(dev, 0));
  		err = -EBUSY;
  		goto err_disable;
  	}
  	pci_set_master(dev);
  	pci_set_drvdata(dev, bg);
a435e1802   abdoulaye berthe   gpio: bt8xx: use ...
189
  	bg->mmio = devm_ioremap(&dev->dev, pci_resource_start(dev, 0), 0x1000);
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
190
191
192
193
  	if (!bg->mmio) {
  		printk(KERN_ERR "bt8xxgpio: ioremap() failed
  ");
  		err = -EIO;
a435e1802   abdoulaye berthe   gpio: bt8xx: use ...
194
  		goto err_disable;
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
195
196
197
198
199
200
201
202
203
204
205
  	}
  
  	/* Disable interrupts */
  	bgwrite(0, BT848_INT_MASK);
  
  	/* gpio init */
  	bgwrite(0, BT848_GPIO_DMA_CTL);
  	bgwrite(0, BT848_GPIO_REG_INP);
  	bgwrite(0, BT848_GPIO_OUT_EN);
  
  	bt8xxgpio_gpio_setup(bg);
e51b55236   Linus Walleij   gpio: bt8xx: use ...
206
  	err = gpiochip_add_data(&bg->gpio, bg);
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
207
208
209
  	if (err) {
  		printk(KERN_ERR "bt8xxgpio: Failed to register GPIOs
  ");
a435e1802   abdoulaye berthe   gpio: bt8xx: use ...
210
  		goto err_disable;
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
211
  	}
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
212
  	return 0;
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
213
214
  err_disable:
  	pci_disable_device(dev);
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
215
216
217
218
219
220
221
222
223
224
225
226
227
  
  	return err;
  }
  
  static void bt8xxgpio_remove(struct pci_dev *pdev)
  {
  	struct bt8xxgpio *bg = pci_get_drvdata(pdev);
  
  	gpiochip_remove(&bg->gpio);
  
  	bgwrite(0, BT848_INT_MASK);
  	bgwrite(~0x0, BT848_INT_STAT);
  	bgwrite(0x0, BT848_GPIO_OUT_EN);
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
228
  	pci_disable_device(pdev);
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
  }
  
  #ifdef CONFIG_PM
  static int bt8xxgpio_suspend(struct pci_dev *pdev, pm_message_t state)
  {
  	struct bt8xxgpio *bg = pci_get_drvdata(pdev);
  	unsigned long flags;
  
  	spin_lock_irqsave(&bg->lock, flags);
  
  	bg->saved_outen = bgread(BT848_GPIO_OUT_EN);
  	bg->saved_data = bgread(BT848_GPIO_DATA);
  
  	bgwrite(0, BT848_INT_MASK);
  	bgwrite(~0x0, BT848_INT_STAT);
  	bgwrite(0x0, BT848_GPIO_OUT_EN);
  
  	spin_unlock_irqrestore(&bg->lock, flags);
  
  	pci_save_state(pdev);
  	pci_disable_device(pdev);
  	pci_set_power_state(pdev, pci_choose_state(pdev, state));
  
  	return 0;
  }
  
  static int bt8xxgpio_resume(struct pci_dev *pdev)
  {
  	struct bt8xxgpio *bg = pci_get_drvdata(pdev);
  	unsigned long flags;
  	int err;
038f0babc   Yijing Wang   Gpio/trivial: rep...
260
  	pci_set_power_state(pdev, PCI_D0);
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  	err = pci_enable_device(pdev);
  	if (err)
  		return err;
  	pci_restore_state(pdev);
  
  	spin_lock_irqsave(&bg->lock, flags);
  
  	bgwrite(0, BT848_INT_MASK);
  	bgwrite(0, BT848_GPIO_DMA_CTL);
  	bgwrite(0, BT848_GPIO_REG_INP);
  	bgwrite(bg->saved_outen, BT848_GPIO_OUT_EN);
  	bgwrite(bg->saved_data & bg->saved_outen,
  		BT848_GPIO_DATA);
  
  	spin_unlock_irqrestore(&bg->lock, flags);
  
  	return 0;
  }
  #else
  #define bt8xxgpio_suspend NULL
  #define bt8xxgpio_resume NULL
  #endif /* CONFIG_PM */
14f4a8838   Jingoo Han   gpio: remove DEFI...
283
  static const struct pci_device_id bt8xxgpio_pci_tbl[] = {
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  	{ PCI_DEVICE(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT848) },
  	{ PCI_DEVICE(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT849) },
  	{ PCI_DEVICE(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT878) },
  	{ PCI_DEVICE(PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BT879) },
  	{ 0, },
  };
  MODULE_DEVICE_TABLE(pci, bt8xxgpio_pci_tbl);
  
  static struct pci_driver bt8xxgpio_pci_driver = {
  	.name		= "bt8xxgpio",
  	.id_table	= bt8xxgpio_pci_tbl,
  	.probe		= bt8xxgpio_probe,
  	.remove		= bt8xxgpio_remove,
  	.suspend	= bt8xxgpio_suspend,
  	.resume		= bt8xxgpio_resume,
  };
93baa65fe   Axel Lin   gpio: Convert dri...
300
  module_pci_driver(bt8xxgpio_pci_driver);
ff1d5c2f0   Michael Buesch   gpio: add bt8xxgp...
301
302
303
304
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Michael Buesch");
  MODULE_DESCRIPTION("Abuse a BT8xx framegrabber card as generic GPIO card");