gpio-mpc8xxx.c 11.0 KB
Newer Older
1
/*
2
 * GPIOs on MPC512x/8349/8572/8610/QorIQ and compatible
3 4
 *
 * Copyright (C) 2008 Peter Korsgaard <jacmet@sunsite.dk>
5
 * Copyright (C) 2016 Freescale Semiconductor Inc.
6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This file is licensed under the terms of the GNU General Public License
 * version 2.  This program is licensed "as is" without any warranty of any
 * kind, whether express or implied.
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
18
#include <linux/of_address.h>
19
#include <linux/of_irq.h>
20
#include <linux/of_platform.h>
21
#include <linux/slab.h>
22
#include <linux/irq.h>
23
#include <linux/gpio/driver.h>
24
#include <linux/bitops.h>
25 26 27 28 29 30 31 32 33

#define MPC8XXX_GPIO_PINS	32

#define GPIO_DIR		0x00
#define GPIO_ODR		0x04
#define GPIO_DAT		0x08
#define GPIO_IER		0x0c
#define GPIO_IMR		0x10
#define GPIO_ICR		0x14
34
#define GPIO_ICR2		0x18
35 36

struct mpc8xxx_gpio_chip {
37 38
	struct gpio_chip	gc;
	void __iomem *regs;
39
	raw_spinlock_t lock;
40

41 42 43
	int (*direction_output)(struct gpio_chip *chip,
				unsigned offset, int value);

44
	struct irq_domain *irq;
45
	unsigned int irqn;
46 47
};

48 49 50 51 52 53 54 55 56 57
/*
 * This hardware has a big endian bit assignment such that GPIO line 0 is
 * connected to bit 31, line 1 to bit 30 ... line 31 to bit 0.
 * This inline helper give the right bitmask for a certain line.
 */
static inline u32 mpc_pin2mask(unsigned int offset)
{
	return BIT(31 - offset);
}

58 59 60 61 62 63 64 65
/* Workaround GPIO 1 errata on MPC8572/MPC8536. The status of GPIOs
 * defined as output cannot be determined by reading GPDAT register,
 * so we use shadow data register instead. The status of input pins
 * is determined by reading GPDAT register.
 */
static int mpc8572_gpio_get(struct gpio_chip *gc, unsigned int gpio)
{
	u32 val;
66
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
67
	u32 out_mask, out_shadow;
68

69 70
	out_mask = gc->read_reg(mpc8xxx_gc->regs + GPIO_DIR);
	val = gc->read_reg(mpc8xxx_gc->regs + GPIO_DAT) & ~out_mask;
71
	out_shadow = gc->bgpio_data & out_mask;
72

73
	return !!((val | out_shadow) & mpc_pin2mask(gpio));
74 75
}

76 77
static int mpc5121_gpio_dir_out(struct gpio_chip *gc,
				unsigned int gpio, int val)
78
{
79
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
80 81 82 83
	/* GPIO 28..31 are input only on MPC5121 */
	if (gpio >= 28)
		return -EINVAL;

84
	return mpc8xxx_gc->direction_output(gc, gpio, val);
85 86
}

87 88
static int mpc5125_gpio_dir_out(struct gpio_chip *gc,
				unsigned int gpio, int val)
89
{
90
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
91 92 93 94
	/* GPIO 0..3 are input only on MPC5125 */
	if (gpio <= 3)
		return -EINVAL;

95
	return mpc8xxx_gc->direction_output(gc, gpio, val);
96 97
}

98 99
static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
{
100
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
101 102 103 104 105 106 107

	if (mpc8xxx_gc->irq && offset < MPC8XXX_GPIO_PINS)
		return irq_create_mapping(mpc8xxx_gc->irq, offset);
	else
		return -ENXIO;
}

108
static void mpc8xxx_gpio_irq_cascade(struct irq_desc *desc)
109
{
110
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_desc_get_handler_data(desc);
111
	struct irq_chip *chip = irq_desc_get_chip(desc);
112
	struct gpio_chip *gc = &mpc8xxx_gc->gc;
113 114
	unsigned int mask;

115 116
	mask = gc->read_reg(mpc8xxx_gc->regs + GPIO_IER)
		& gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR);
117 118 119
	if (mask)
		generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq,
						     32 - ffs(mask)));
120 121
	if (chip->irq_eoi)
		chip->irq_eoi(&desc->irq_data);
122 123
}

124
static void mpc8xxx_irq_unmask(struct irq_data *d)
125
{
126
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
127
	struct gpio_chip *gc = &mpc8xxx_gc->gc;
128 129
	unsigned long flags;

130
	raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
131

132 133
	gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
		gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
134
		| mpc_pin2mask(irqd_to_hwirq(d)));
135

136
	raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
137 138
}

139
static void mpc8xxx_irq_mask(struct irq_data *d)
140
{
141
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
142
	struct gpio_chip *gc = &mpc8xxx_gc->gc;
143 144
	unsigned long flags;

145
	raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
146

147 148
	gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
		gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
149
		& ~mpc_pin2mask(irqd_to_hwirq(d)));
150

151
	raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
152 153
}

154
static void mpc8xxx_irq_ack(struct irq_data *d)
155
{
156
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
157
	struct gpio_chip *gc = &mpc8xxx_gc->gc;
158

159
	gc->write_reg(mpc8xxx_gc->regs + GPIO_IER,
160
		      mpc_pin2mask(irqd_to_hwirq(d)));
161 162
}

163
static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
164
{
165
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
166
	struct gpio_chip *gc = &mpc8xxx_gc->gc;
167 168 169 170
	unsigned long flags;

	switch (flow_type) {
	case IRQ_TYPE_EDGE_FALLING:
171
		raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
172 173
		gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
			gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
174
			| mpc_pin2mask(irqd_to_hwirq(d)));
175
		raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
176 177 178
		break;

	case IRQ_TYPE_EDGE_BOTH:
179
		raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
180 181
		gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
			gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
182
			& ~mpc_pin2mask(irqd_to_hwirq(d)));
183
		raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
184 185 186 187 188 189 190 191 192
		break;

	default:
		return -EINVAL;
	}

	return 0;
}

193
static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type)
194
{
195
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
196
	struct gpio_chip *gc = &mpc8xxx_gc->gc;
197
	unsigned long gpio = irqd_to_hwirq(d);
198 199 200 201 202
	void __iomem *reg;
	unsigned int shift;
	unsigned long flags;

	if (gpio < 16) {
203
		reg = mpc8xxx_gc->regs + GPIO_ICR;
204 205
		shift = (15 - gpio) * 2;
	} else {
206
		reg = mpc8xxx_gc->regs + GPIO_ICR2;
207 208 209 210 211 212
		shift = (15 - (gpio % 16)) * 2;
	}

	switch (flow_type) {
	case IRQ_TYPE_EDGE_FALLING:
	case IRQ_TYPE_LEVEL_LOW:
213
		raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
214
		gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift))
215
			| (2 << shift));
216
		raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
217 218 219 220
		break;

	case IRQ_TYPE_EDGE_RISING:
	case IRQ_TYPE_LEVEL_HIGH:
221
		raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
222
		gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift))
223
			| (1 << shift));
224
		raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
225 226 227
		break;

	case IRQ_TYPE_EDGE_BOTH:
228
		raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
229
		gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift)));
230
		raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
231 232 233 234 235 236 237 238 239
		break;

	default:
		return -EINVAL;
	}

	return 0;
}

240 241
static struct irq_chip mpc8xxx_irq_chip = {
	.name		= "mpc8xxx-gpio",
242 243 244
	.irq_unmask	= mpc8xxx_irq_unmask,
	.irq_mask	= mpc8xxx_irq_mask,
	.irq_ack	= mpc8xxx_irq_ack,
245
	/* this might get overwritten in mpc8xxx_probe() */
246
	.irq_set_type	= mpc8xxx_irq_set_type,
247 248
};

249 250
static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int irq,
				irq_hw_number_t hwirq)
251
{
252
	irq_set_chip_data(irq, h->host_data);
253
	irq_set_chip_and_handler(irq, &mpc8xxx_irq_chip, handle_edge_irq);
254 255 256 257

	return 0;
}

258
static const struct irq_domain_ops mpc8xxx_gpio_irq_ops = {
259
	.map	= mpc8xxx_gpio_irq_map,
260
	.xlate	= irq_domain_xlate_twocell,
261 262
};

263 264 265 266 267 268 269 270 271 272 273
struct mpc8xxx_gpio_devtype {
	int (*gpio_dir_out)(struct gpio_chip *, unsigned int, int);
	int (*gpio_get)(struct gpio_chip *, unsigned int);
	int (*irq_set_type)(struct irq_data *, unsigned int);
};

static const struct mpc8xxx_gpio_devtype mpc512x_gpio_devtype = {
	.gpio_dir_out = mpc5121_gpio_dir_out,
	.irq_set_type = mpc512x_irq_set_type,
};

274 275 276 277 278
static const struct mpc8xxx_gpio_devtype mpc5125_gpio_devtype = {
	.gpio_dir_out = mpc5125_gpio_dir_out,
	.irq_set_type = mpc512x_irq_set_type,
};

279 280 281 282 283 284 285 286
static const struct mpc8xxx_gpio_devtype mpc8572_gpio_devtype = {
	.gpio_get = mpc8572_gpio_get,
};

static const struct mpc8xxx_gpio_devtype mpc8xxx_gpio_devtype_default = {
	.irq_set_type = mpc8xxx_irq_set_type,
};

287
static const struct of_device_id mpc8xxx_gpio_ids[] = {
288
	{ .compatible = "fsl,mpc8349-gpio", },
289
	{ .compatible = "fsl,mpc8572-gpio", .data = &mpc8572_gpio_devtype, },
290
	{ .compatible = "fsl,mpc8610-gpio", },
291
	{ .compatible = "fsl,mpc5121-gpio", .data = &mpc512x_gpio_devtype, },
292
	{ .compatible = "fsl,mpc5125-gpio", .data = &mpc5125_gpio_devtype, },
293
	{ .compatible = "fsl,pq3-gpio",     },
294
	{ .compatible = "fsl,qoriq-gpio",   },
295 296 297
	{}
};

298
static int mpc8xxx_probe(struct platform_device *pdev)
299
{
300
	struct device_node *np = pdev->dev.of_node;
301
	struct mpc8xxx_gpio_chip *mpc8xxx_gc;
302
	struct gpio_chip	*gc;
303 304
	const struct mpc8xxx_gpio_devtype *devtype =
		of_device_get_match_data(&pdev->dev);
305 306
	int ret;

307 308 309
	mpc8xxx_gc = devm_kzalloc(&pdev->dev, sizeof(*mpc8xxx_gc), GFP_KERNEL);
	if (!mpc8xxx_gc)
		return -ENOMEM;
310

311 312
	platform_set_drvdata(pdev, mpc8xxx_gc);

313
	raw_spin_lock_init(&mpc8xxx_gc->lock);
314

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	mpc8xxx_gc->regs = of_iomap(np, 0);
	if (!mpc8xxx_gc->regs)
		return -ENOMEM;

	gc = &mpc8xxx_gc->gc;

	if (of_property_read_bool(np, "little-endian")) {
		ret = bgpio_init(gc, &pdev->dev, 4,
				 mpc8xxx_gc->regs + GPIO_DAT,
				 NULL, NULL,
				 mpc8xxx_gc->regs + GPIO_DIR, NULL,
				 BGPIOF_BIG_ENDIAN);
		if (ret)
			goto err;
		dev_dbg(&pdev->dev, "GPIO registers are LITTLE endian\n");
	} else {
		ret = bgpio_init(gc, &pdev->dev, 4,
				 mpc8xxx_gc->regs + GPIO_DAT,
				 NULL, NULL,
				 mpc8xxx_gc->regs + GPIO_DIR, NULL,
				 BGPIOF_BIG_ENDIAN
				 | BGPIOF_BIG_ENDIAN_BYTE_ORDER);
		if (ret)
			goto err;
		dev_dbg(&pdev->dev, "GPIO registers are BIG endian\n");
	}
341

342
	mpc8xxx_gc->direction_output = gc->direction_output;
343 344 345 346 347 348 349 350 351 352

	if (!devtype)
		devtype = &mpc8xxx_gpio_devtype_default;

	/*
	 * It's assumed that only a single type of gpio controller is available
	 * on the current machine, so overwriting global data is fine.
	 */
	mpc8xxx_irq_chip.irq_set_type = devtype->irq_set_type;

353 354 355 356 357
	if (devtype->gpio_dir_out)
		gc->direction_output = devtype->gpio_dir_out;
	if (devtype->gpio_get)
		gc->get = devtype->gpio_get;

358
	gc->to_irq = mpc8xxx_gpio_to_irq;
359

360 361
	ret = gpiochip_add_data(gc, mpc8xxx_gc);
	if (ret) {
362 363
		pr_err("%pOF: GPIO chip registration failed with status %d\n",
		       np, ret);
364 365
		goto err;
	}
366

367
	mpc8xxx_gc->irqn = irq_of_parse_and_map(np, 0);
368
	if (!mpc8xxx_gc->irqn)
369
		return 0;
370

371 372
	mpc8xxx_gc->irq = irq_domain_add_linear(np, MPC8XXX_GPIO_PINS,
					&mpc8xxx_gpio_irq_ops, mpc8xxx_gc);
373
	if (!mpc8xxx_gc->irq)
374
		return 0;
375 376

	/* ack and mask all irqs */
377 378
	gc->write_reg(mpc8xxx_gc->regs + GPIO_IER, 0xffffffff);
	gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR, 0);
379

380 381
	irq_set_chained_handler_and_data(mpc8xxx_gc->irqn,
					 mpc8xxx_gpio_irq_cascade, mpc8xxx_gc);
382
	return 0;
383 384 385
err:
	iounmap(mpc8xxx_gc->regs);
	return ret;
386 387 388 389 390 391 392
}

static int mpc8xxx_remove(struct platform_device *pdev)
{
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = platform_get_drvdata(pdev);

	if (mpc8xxx_gc->irq) {
393
		irq_set_chained_handler_and_data(mpc8xxx_gc->irqn, NULL, NULL);
394 395 396
		irq_domain_remove(mpc8xxx_gc->irq);
	}

397 398
	gpiochip_remove(&mpc8xxx_gc->gc);
	iounmap(mpc8xxx_gc->regs);
399

400
	return 0;
401 402
}

403 404
static struct platform_driver mpc8xxx_plat_driver = {
	.probe		= mpc8xxx_probe,
405
	.remove		= mpc8xxx_remove,
406 407 408 409 410
	.driver		= {
		.name = "gpio-mpc8xxx",
		.of_match_table	= mpc8xxx_gpio_ids,
	},
};
411

412 413 414
static int __init mpc8xxx_init(void)
{
	return platform_driver_register(&mpc8xxx_plat_driver);
415
}
416 417

arch_initcall(mpc8xxx_init);