Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 *  GPIO interface for Intel Sodaville SoCs.
  4 *
  5 *  Copyright (c) 2010, 2011 Intel Corporation
  6 *
  7 *  Author: Hans J. Koch <hjk@linutronix.de>
 
 
 
  8 */
  9
 10#include <linux/errno.h>
 11#include <linux/gpio/driver.h>
 12#include <linux/init.h>
 13#include <linux/interrupt.h>
 14#include <linux/io.h>
 15#include <linux/irq.h>
 
 16#include <linux/kernel.h>
 17#include <linux/of_irq.h>
 18#include <linux/pci.h>
 19#include <linux/platform_device.h>
 
 
 20
 21#define DRV_NAME		"sdv_gpio"
 22#define SDV_NUM_PUB_GPIOS	12
 23#define PCI_DEVICE_ID_SDV_GPIO	0x2e67
 24#define GPIO_BAR		0
 25
 26#define GPOUTR		0x00
 27#define GPOER		0x04
 28#define GPINR		0x08
 29
 30#define GPSTR		0x0c
 31#define GPIT1R0		0x10
 32#define GPIO_INT	0x14
 33#define GPIT1R1		0x18
 34
 35#define GPMUXCTL	0x1c
 36
 37struct sdv_gpio_chip_data {
 38	int irq_base;
 39	void __iomem *gpio_pub_base;
 40	struct irq_domain *id;
 41	struct irq_chip_generic *gc;
 42	struct gpio_chip chip;
 43};
 44
 45static int sdv_gpio_pub_set_type(struct irq_data *d, unsigned int type)
 46{
 47	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
 48	struct sdv_gpio_chip_data *sd = gc->private;
 49	void __iomem *type_reg;
 50	u32 reg;
 51
 52	if (d->hwirq < 8)
 53		type_reg = sd->gpio_pub_base + GPIT1R0;
 54	else
 55		type_reg = sd->gpio_pub_base + GPIT1R1;
 56
 57	reg = readl(type_reg);
 58
 59	switch (type) {
 60	case IRQ_TYPE_LEVEL_HIGH:
 61		reg &= ~BIT(4 * (d->hwirq % 8));
 62		break;
 63
 64	case IRQ_TYPE_LEVEL_LOW:
 65		reg |= BIT(4 * (d->hwirq % 8));
 66		break;
 67
 68	default:
 69		return -EINVAL;
 70	}
 71
 72	writel(reg, type_reg);
 73	return 0;
 74}
 75
 76static irqreturn_t sdv_gpio_pub_irq_handler(int irq, void *data)
 77{
 78	struct sdv_gpio_chip_data *sd = data;
 79	unsigned long irq_stat = readl(sd->gpio_pub_base + GPSTR);
 80	int irq_bit;
 81
 82	irq_stat &= readl(sd->gpio_pub_base + GPIO_INT);
 83	if (!irq_stat)
 84		return IRQ_NONE;
 85
 86	for_each_set_bit(irq_bit, &irq_stat, 32)
 87		generic_handle_domain_irq(sd->id, irq_bit);
 
 
 
 
 88
 89	return IRQ_HANDLED;
 90}
 91
 92static int sdv_xlate(struct irq_domain *h, struct device_node *node,
 93		const u32 *intspec, u32 intsize, irq_hw_number_t *out_hwirq,
 94		u32 *out_type)
 95{
 96	u32 line, type;
 97
 98	if (node != irq_domain_get_of_node(h))
 99		return -EINVAL;
100
101	if (intsize < 2)
102		return -EINVAL;
103
104	line = *intspec;
105	*out_hwirq = line;
106
107	intspec++;
108	type = *intspec;
109
110	switch (type) {
111	case IRQ_TYPE_LEVEL_LOW:
112	case IRQ_TYPE_LEVEL_HIGH:
113		*out_type = type;
114		break;
115	default:
116		return -EINVAL;
117	}
118	return 0;
119}
120
121static const struct irq_domain_ops irq_domain_sdv_ops = {
122	.xlate = sdv_xlate,
123};
124
125static int sdv_register_irqsupport(struct sdv_gpio_chip_data *sd,
126		struct pci_dev *pdev)
127{
128	struct irq_chip_type *ct;
129	int ret;
130
131	sd->irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0,
132					    SDV_NUM_PUB_GPIOS, -1);
133	if (sd->irq_base < 0)
134		return sd->irq_base;
135
136	/* mask + ACK all interrupt sources */
137	writel(0, sd->gpio_pub_base + GPIO_INT);
138	writel((1 << 11) - 1, sd->gpio_pub_base + GPSTR);
139
140	ret = devm_request_irq(&pdev->dev, pdev->irq,
141			       sdv_gpio_pub_irq_handler, IRQF_SHARED,
142			       "sdv_gpio", sd);
143	if (ret)
144		return ret;
145
146	/*
147	 * This gpio irq controller latches level irqs. Testing shows that if
148	 * we unmask & ACK the IRQ before the source of the interrupt is gone
149	 * then the interrupt is active again.
150	 */
151	sd->gc = devm_irq_alloc_generic_chip(&pdev->dev, "sdv-gpio", 1,
152					     sd->irq_base,
153					     sd->gpio_pub_base,
154					     handle_fasteoi_irq);
155	if (!sd->gc)
156		return -ENOMEM;
157
158	sd->gc->private = sd;
159	ct = sd->gc->chip_types;
160	ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
161	ct->regs.eoi = GPSTR;
162	ct->regs.mask = GPIO_INT;
163	ct->chip.irq_mask = irq_gc_mask_clr_bit;
164	ct->chip.irq_unmask = irq_gc_mask_set_bit;
165	ct->chip.irq_eoi = irq_gc_eoi;
166	ct->chip.irq_set_type = sdv_gpio_pub_set_type;
167
168	irq_setup_generic_chip(sd->gc, IRQ_MSK(SDV_NUM_PUB_GPIOS),
169			IRQ_GC_INIT_MASK_CACHE, IRQ_NOREQUEST,
170			IRQ_LEVEL | IRQ_NOPROBE);
171
172	sd->id = irq_domain_add_legacy(pdev->dev.of_node, SDV_NUM_PUB_GPIOS,
173				sd->irq_base, 0, &irq_domain_sdv_ops, sd);
174	if (!sd->id)
175		return -ENODEV;
176
 
177	return 0;
 
 
 
 
 
178}
179
180static int sdv_gpio_probe(struct pci_dev *pdev,
181					const struct pci_device_id *pci_id)
182{
183	struct sdv_gpio_chip_data *sd;
 
 
 
184	int ret;
185	u32 mux_val;
186
187	sd = devm_kzalloc(&pdev->dev, sizeof(*sd), GFP_KERNEL);
188	if (!sd)
189		return -ENOMEM;
190
191	ret = pcim_enable_device(pdev);
192	if (ret) {
193		dev_err(&pdev->dev, "can't enable device.\n");
194		return ret;
195	}
196
197	ret = pcim_iomap_regions(pdev, 1 << GPIO_BAR, DRV_NAME);
198	if (ret) {
199		dev_err(&pdev->dev, "can't alloc PCI BAR #%d\n", GPIO_BAR);
200		return ret;
201	}
202
203	sd->gpio_pub_base = pcim_iomap_table(pdev)[GPIO_BAR];
 
 
 
 
 
204
205	ret = of_property_read_u32(pdev->dev.of_node, "intel,muxctl", &mux_val);
206	if (!ret)
 
207		writel(mux_val, sd->gpio_pub_base + GPMUXCTL);
 
208
209	ret = bgpio_init(&sd->chip, &pdev->dev, 4,
210			sd->gpio_pub_base + GPINR, sd->gpio_pub_base + GPOUTR,
211			NULL, sd->gpio_pub_base + GPOER, NULL, 0);
212	if (ret)
213		return ret;
214
215	sd->chip.ngpio = SDV_NUM_PUB_GPIOS;
216
217	ret = devm_gpiochip_add_data(&pdev->dev, &sd->chip, sd);
218	if (ret < 0) {
219		dev_err(&pdev->dev, "gpiochip_add() failed.\n");
220		return ret;
221	}
222
223	ret = sdv_register_irqsupport(sd, pdev);
224	if (ret)
225		return ret;
226
227	pci_set_drvdata(pdev, sd);
228	dev_info(&pdev->dev, "Sodaville GPIO driver registered.\n");
229	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230}
231
232static const struct pci_device_id sdv_gpio_pci_ids[] = {
233	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_SDV_GPIO) },
234	{ 0, },
235};
236
237static struct pci_driver sdv_gpio_driver = {
238	.driver = {
239		.suppress_bind_attrs = true,
240	},
241	.name = DRV_NAME,
242	.id_table = sdv_gpio_pci_ids,
243	.probe = sdv_gpio_probe,
 
244};
245builtin_pci_driver(sdv_gpio_driver);
 
 
 
 
 
v3.15
 
  1/*
  2 *  GPIO interface for Intel Sodaville SoCs.
  3 *
  4 *  Copyright (c) 2010, 2011 Intel Corporation
  5 *
  6 *  This program is free software; you can redistribute it and/or modify
  7 *  it under the terms of the GNU General Public License 2 as published
  8 *  by the Free Software Foundation.
  9 *
 10 */
 11
 12#include <linux/errno.h>
 13#include <linux/gpio.h>
 14#include <linux/init.h>
 
 15#include <linux/io.h>
 16#include <linux/irq.h>
 17#include <linux/interrupt.h>
 18#include <linux/kernel.h>
 19#include <linux/module.h>
 20#include <linux/pci.h>
 21#include <linux/platform_device.h>
 22#include <linux/of_irq.h>
 23#include <linux/basic_mmio_gpio.h>
 24
 25#define DRV_NAME		"sdv_gpio"
 26#define SDV_NUM_PUB_GPIOS	12
 27#define PCI_DEVICE_ID_SDV_GPIO	0x2e67
 28#define GPIO_BAR		0
 29
 30#define GPOUTR		0x00
 31#define GPOER		0x04
 32#define GPINR		0x08
 33
 34#define GPSTR		0x0c
 35#define GPIT1R0		0x10
 36#define GPIO_INT	0x14
 37#define GPIT1R1		0x18
 38
 39#define GPMUXCTL	0x1c
 40
 41struct sdv_gpio_chip_data {
 42	int irq_base;
 43	void __iomem *gpio_pub_base;
 44	struct irq_domain *id;
 45	struct irq_chip_generic *gc;
 46	struct bgpio_chip bgpio;
 47};
 48
 49static int sdv_gpio_pub_set_type(struct irq_data *d, unsigned int type)
 50{
 51	struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
 52	struct sdv_gpio_chip_data *sd = gc->private;
 53	void __iomem *type_reg;
 54	u32 reg;
 55
 56	if (d->hwirq < 8)
 57		type_reg = sd->gpio_pub_base + GPIT1R0;
 58	else
 59		type_reg = sd->gpio_pub_base + GPIT1R1;
 60
 61	reg = readl(type_reg);
 62
 63	switch (type) {
 64	case IRQ_TYPE_LEVEL_HIGH:
 65		reg &= ~BIT(4 * (d->hwirq % 8));
 66		break;
 67
 68	case IRQ_TYPE_LEVEL_LOW:
 69		reg |= BIT(4 * (d->hwirq % 8));
 70		break;
 71
 72	default:
 73		return -EINVAL;
 74	}
 75
 76	writel(reg, type_reg);
 77	return 0;
 78}
 79
 80static irqreturn_t sdv_gpio_pub_irq_handler(int irq, void *data)
 81{
 82	struct sdv_gpio_chip_data *sd = data;
 83	u32 irq_stat = readl(sd->gpio_pub_base + GPSTR);
 
 84
 85	irq_stat &= readl(sd->gpio_pub_base + GPIO_INT);
 86	if (!irq_stat)
 87		return IRQ_NONE;
 88
 89	while (irq_stat) {
 90		u32 irq_bit = __fls(irq_stat);
 91
 92		irq_stat &= ~BIT(irq_bit);
 93		generic_handle_irq(irq_find_mapping(sd->id, irq_bit));
 94	}
 95
 96	return IRQ_HANDLED;
 97}
 98
 99static int sdv_xlate(struct irq_domain *h, struct device_node *node,
100		const u32 *intspec, u32 intsize, irq_hw_number_t *out_hwirq,
101		u32 *out_type)
102{
103	u32 line, type;
104
105	if (node != h->of_node)
106		return -EINVAL;
107
108	if (intsize < 2)
109		return -EINVAL;
110
111	line = *intspec;
112	*out_hwirq = line;
113
114	intspec++;
115	type = *intspec;
116
117	switch (type) {
118	case IRQ_TYPE_LEVEL_LOW:
119	case IRQ_TYPE_LEVEL_HIGH:
120		*out_type = type;
121		break;
122	default:
123		return -EINVAL;
124	}
125	return 0;
126}
127
128static struct irq_domain_ops irq_domain_sdv_ops = {
129	.xlate = sdv_xlate,
130};
131
132static int sdv_register_irqsupport(struct sdv_gpio_chip_data *sd,
133		struct pci_dev *pdev)
134{
135	struct irq_chip_type *ct;
136	int ret;
137
138	sd->irq_base = irq_alloc_descs(-1, 0, SDV_NUM_PUB_GPIOS, -1);
 
139	if (sd->irq_base < 0)
140		return sd->irq_base;
141
142	/* mask + ACK all interrupt sources */
143	writel(0, sd->gpio_pub_base + GPIO_INT);
144	writel((1 << 11) - 1, sd->gpio_pub_base + GPSTR);
145
146	ret = request_irq(pdev->irq, sdv_gpio_pub_irq_handler, IRQF_SHARED,
147			"sdv_gpio", sd);
 
148	if (ret)
149		goto out_free_desc;
150
151	/*
152	 * This gpio irq controller latches level irqs. Testing shows that if
153	 * we unmask & ACK the IRQ before the source of the interrupt is gone
154	 * then the interrupt is active again.
155	 */
156	sd->gc = irq_alloc_generic_chip("sdv-gpio", 1, sd->irq_base,
157			sd->gpio_pub_base, handle_fasteoi_irq);
158	if (!sd->gc) {
159		ret = -ENOMEM;
160		goto out_free_irq;
161	}
162
163	sd->gc->private = sd;
164	ct = sd->gc->chip_types;
165	ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
166	ct->regs.eoi = GPSTR;
167	ct->regs.mask = GPIO_INT;
168	ct->chip.irq_mask = irq_gc_mask_clr_bit;
169	ct->chip.irq_unmask = irq_gc_mask_set_bit;
170	ct->chip.irq_eoi = irq_gc_eoi;
171	ct->chip.irq_set_type = sdv_gpio_pub_set_type;
172
173	irq_setup_generic_chip(sd->gc, IRQ_MSK(SDV_NUM_PUB_GPIOS),
174			IRQ_GC_INIT_MASK_CACHE, IRQ_NOREQUEST,
175			IRQ_LEVEL | IRQ_NOPROBE);
176
177	sd->id = irq_domain_add_legacy(pdev->dev.of_node, SDV_NUM_PUB_GPIOS,
178				sd->irq_base, 0, &irq_domain_sdv_ops, sd);
179	if (!sd->id) {
180		ret = -ENODEV;
181		goto out_free_irq;
182	}
183	return 0;
184out_free_irq:
185	free_irq(pdev->irq, sd);
186out_free_desc:
187	irq_free_descs(sd->irq_base, SDV_NUM_PUB_GPIOS);
188	return ret;
189}
190
191static int sdv_gpio_probe(struct pci_dev *pdev,
192					const struct pci_device_id *pci_id)
193{
194	struct sdv_gpio_chip_data *sd;
195	unsigned long addr;
196	const void *prop;
197	int len;
198	int ret;
199	u32 mux_val;
200
201	sd = kzalloc(sizeof(struct sdv_gpio_chip_data), GFP_KERNEL);
202	if (!sd)
203		return -ENOMEM;
204	ret = pci_enable_device(pdev);
 
205	if (ret) {
206		dev_err(&pdev->dev, "can't enable device.\n");
207		goto done;
208	}
209
210	ret = pci_request_region(pdev, GPIO_BAR, DRV_NAME);
211	if (ret) {
212		dev_err(&pdev->dev, "can't alloc PCI BAR #%d\n", GPIO_BAR);
213		goto disable_pci;
214	}
215
216	addr = pci_resource_start(pdev, GPIO_BAR);
217	if (!addr) {
218		ret = -ENODEV;
219		goto release_reg;
220	}
221	sd->gpio_pub_base = ioremap(addr, pci_resource_len(pdev, GPIO_BAR));
222
223	prop = of_get_property(pdev->dev.of_node, "intel,muxctl", &len);
224	if (prop && len == 4) {
225		mux_val = of_read_number(prop, 1);
226		writel(mux_val, sd->gpio_pub_base + GPMUXCTL);
227	}
228
229	ret = bgpio_init(&sd->bgpio, &pdev->dev, 4,
230			sd->gpio_pub_base + GPINR, sd->gpio_pub_base + GPOUTR,
231			NULL, sd->gpio_pub_base + GPOER, NULL, 0);
232	if (ret)
233		goto unmap;
234	sd->bgpio.gc.ngpio = SDV_NUM_PUB_GPIOS;
 
235
236	ret = gpiochip_add(&sd->bgpio.gc);
237	if (ret < 0) {
238		dev_err(&pdev->dev, "gpiochip_add() failed.\n");
239		goto unmap;
240	}
241
242	ret = sdv_register_irqsupport(sd, pdev);
243	if (ret)
244		goto unmap;
245
246	pci_set_drvdata(pdev, sd);
247	dev_info(&pdev->dev, "Sodaville GPIO driver registered.\n");
248	return 0;
249
250unmap:
251	iounmap(sd->gpio_pub_base);
252release_reg:
253	pci_release_region(pdev, GPIO_BAR);
254disable_pci:
255	pci_disable_device(pdev);
256done:
257	kfree(sd);
258	return ret;
259}
260
261static void sdv_gpio_remove(struct pci_dev *pdev)
262{
263	struct sdv_gpio_chip_data *sd = pci_get_drvdata(pdev);
264
265	free_irq(pdev->irq, sd);
266	irq_free_descs(sd->irq_base, SDV_NUM_PUB_GPIOS);
267
268	if (gpiochip_remove(&sd->bgpio.gc))
269		dev_err(&pdev->dev, "gpiochip_remove() failed.\n");
270
271	pci_release_region(pdev, GPIO_BAR);
272	iounmap(sd->gpio_pub_base);
273	pci_disable_device(pdev);
274	kfree(sd);
275}
276
277static const struct pci_device_id sdv_gpio_pci_ids[] = {
278	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_SDV_GPIO) },
279	{ 0, },
280};
281
282static struct pci_driver sdv_gpio_driver = {
 
 
 
283	.name = DRV_NAME,
284	.id_table = sdv_gpio_pci_ids,
285	.probe = sdv_gpio_probe,
286	.remove = sdv_gpio_remove,
287};
288
289module_pci_driver(sdv_gpio_driver);
290
291MODULE_AUTHOR("Hans J. Koch <hjk@linutronix.de>");
292MODULE_DESCRIPTION("GPIO interface for Intel Sodaville SoCs");
293MODULE_LICENSE("GPL v2");