gpio-mpc8xxx.c 10.7 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 25 26 27 28 29 30 31 32

#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
33
#define GPIO_ICR2		0x18
34 35

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

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

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

47 48 49 50 51 52 53 54
/* 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;
55
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
56
	u32 out_mask, out_shadow;
57

58 59
	out_mask = gc->read_reg(mpc8xxx_gc->regs + GPIO_DIR);
	val = gc->read_reg(mpc8xxx_gc->regs + GPIO_DAT) & ~out_mask;
60
	out_shadow = gc->bgpio_data & out_mask;
61

62
	return !!((val | out_shadow) & gc->pin2mask(gc, gpio));
63 64
}

65 66
static int mpc5121_gpio_dir_out(struct gpio_chip *gc,
				unsigned int gpio, int val)
67
{
68
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
69 70 71 72
	/* GPIO 28..31 are input only on MPC5121 */
	if (gpio >= 28)
		return -EINVAL;

73
	return mpc8xxx_gc->direction_output(gc, gpio, val);
74 75
}

76 77
static int mpc5125_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 0..3 are input only on MPC5125 */
	if (gpio <= 3)
		return -EINVAL;

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

87 88
static int mpc8xxx_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
{
89
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = gpiochip_get_data(gc);
90 91 92 93 94 95 96

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

97
static void mpc8xxx_gpio_irq_cascade(struct irq_desc *desc)
98
{
99
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_desc_get_handler_data(desc);
100
	struct irq_chip *chip = irq_desc_get_chip(desc);
101
	struct gpio_chip *gc = &mpc8xxx_gc->gc;
102 103
	unsigned int mask;

104 105
	mask = gc->read_reg(mpc8xxx_gc->regs + GPIO_IER)
		& gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR);
106 107 108
	if (mask)
		generic_handle_irq(irq_linear_revmap(mpc8xxx_gc->irq,
						     32 - ffs(mask)));
109 110
	if (chip->irq_eoi)
		chip->irq_eoi(&desc->irq_data);
111 112
}

113
static void mpc8xxx_irq_unmask(struct irq_data *d)
114
{
115
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
116
	struct gpio_chip *gc = &mpc8xxx_gc->gc;
117 118
	unsigned long flags;

119
	raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
120

121 122
	gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
		gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
123
		| gc->pin2mask(gc, irqd_to_hwirq(d)));
124

125
	raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
126 127
}

128
static void mpc8xxx_irq_mask(struct irq_data *d)
129
{
130
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
131
	struct gpio_chip *gc = &mpc8xxx_gc->gc;
132 133
	unsigned long flags;

134
	raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
135

136 137
	gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR,
		gc->read_reg(mpc8xxx_gc->regs + GPIO_IMR)
138
		& ~(gc->pin2mask(gc, irqd_to_hwirq(d))));
139

140
	raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
141 142
}

143
static void mpc8xxx_irq_ack(struct irq_data *d)
144
{
145
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
146
	struct gpio_chip *gc = &mpc8xxx_gc->gc;
147

148 149
	gc->write_reg(mpc8xxx_gc->regs + GPIO_IER,
		      gc->pin2mask(gc, irqd_to_hwirq(d)));
150 151
}

152
static int mpc8xxx_irq_set_type(struct irq_data *d, unsigned int flow_type)
153
{
154
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
155
	struct gpio_chip *gc = &mpc8xxx_gc->gc;
156 157 158 159
	unsigned long flags;

	switch (flow_type) {
	case IRQ_TYPE_EDGE_FALLING:
160
		raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
161 162
		gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
			gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
163
			| gc->pin2mask(gc, irqd_to_hwirq(d)));
164
		raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
165 166 167
		break;

	case IRQ_TYPE_EDGE_BOTH:
168
		raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
169 170
		gc->write_reg(mpc8xxx_gc->regs + GPIO_ICR,
			gc->read_reg(mpc8xxx_gc->regs + GPIO_ICR)
171
			& ~(gc->pin2mask(gc, irqd_to_hwirq(d))));
172
		raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
173 174 175 176 177 178 179 180 181
		break;

	default:
		return -EINVAL;
	}

	return 0;
}

182
static int mpc512x_irq_set_type(struct irq_data *d, unsigned int flow_type)
183
{
184
	struct mpc8xxx_gpio_chip *mpc8xxx_gc = irq_data_get_irq_chip_data(d);
185
	struct gpio_chip *gc = &mpc8xxx_gc->gc;
186
	unsigned long gpio = irqd_to_hwirq(d);
187 188 189 190 191
	void __iomem *reg;
	unsigned int shift;
	unsigned long flags;

	if (gpio < 16) {
192
		reg = mpc8xxx_gc->regs + GPIO_ICR;
193 194
		shift = (15 - gpio) * 2;
	} else {
195
		reg = mpc8xxx_gc->regs + GPIO_ICR2;
196 197 198 199 200 201
		shift = (15 - (gpio % 16)) * 2;
	}

	switch (flow_type) {
	case IRQ_TYPE_EDGE_FALLING:
	case IRQ_TYPE_LEVEL_LOW:
202
		raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
203
		gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift))
204
			| (2 << shift));
205
		raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
206 207 208 209
		break;

	case IRQ_TYPE_EDGE_RISING:
	case IRQ_TYPE_LEVEL_HIGH:
210
		raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
211
		gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift))
212
			| (1 << shift));
213
		raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
214 215 216
		break;

	case IRQ_TYPE_EDGE_BOTH:
217
		raw_spin_lock_irqsave(&mpc8xxx_gc->lock, flags);
218
		gc->write_reg(reg, (gc->read_reg(reg) & ~(3 << shift)));
219
		raw_spin_unlock_irqrestore(&mpc8xxx_gc->lock, flags);
220 221 222 223 224 225 226 227 228
		break;

	default:
		return -EINVAL;
	}

	return 0;
}

229 230
static struct irq_chip mpc8xxx_irq_chip = {
	.name		= "mpc8xxx-gpio",
231 232 233
	.irq_unmask	= mpc8xxx_irq_unmask,
	.irq_mask	= mpc8xxx_irq_mask,
	.irq_ack	= mpc8xxx_irq_ack,
234
	/* this might get overwritten in mpc8xxx_probe() */
235
	.irq_set_type	= mpc8xxx_irq_set_type,
236 237
};

238 239
static int mpc8xxx_gpio_irq_map(struct irq_domain *h, unsigned int irq,
				irq_hw_number_t hwirq)
240
{
241 242
	irq_set_chip_data(irq, h->host_data);
	irq_set_chip_and_handler(irq, &mpc8xxx_irq_chip, handle_level_irq);
243 244 245 246

	return 0;
}

247
static const struct irq_domain_ops mpc8xxx_gpio_irq_ops = {
248
	.map	= mpc8xxx_gpio_irq_map,
249
	.xlate	= irq_domain_xlate_twocell,
250 251
};

252 253 254 255 256 257 258 259 260 261 262
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,
};

263 264 265 266 267
static const struct mpc8xxx_gpio_devtype mpc5125_gpio_devtype = {
	.gpio_dir_out = mpc5125_gpio_dir_out,
	.irq_set_type = mpc512x_irq_set_type,
};

268 269 270 271 272 273 274 275
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,
};

276
static const struct of_device_id mpc8xxx_gpio_ids[] = {
277
	{ .compatible = "fsl,mpc8349-gpio", },
278
	{ .compatible = "fsl,mpc8572-gpio", .data = &mpc8572_gpio_devtype, },
279
	{ .compatible = "fsl,mpc8610-gpio", },
280
	{ .compatible = "fsl,mpc5121-gpio", .data = &mpc512x_gpio_devtype, },
281
	{ .compatible = "fsl,mpc5125-gpio", .data = &mpc5125_gpio_devtype, },
282
	{ .compatible = "fsl,pq3-gpio",     },
283
	{ .compatible = "fsl,qoriq-gpio",   },
284 285 286
	{}
};

287
static int mpc8xxx_probe(struct platform_device *pdev)
288
{
289
	struct device_node *np = pdev->dev.of_node;
290
	struct mpc8xxx_gpio_chip *mpc8xxx_gc;
291
	struct gpio_chip	*gc;
292 293
	const struct mpc8xxx_gpio_devtype *devtype =
		of_device_get_match_data(&pdev->dev);
294 295
	int ret;

296 297 298
	mpc8xxx_gc = devm_kzalloc(&pdev->dev, sizeof(*mpc8xxx_gc), GFP_KERNEL);
	if (!mpc8xxx_gc)
		return -ENOMEM;
299

300 301
	platform_set_drvdata(pdev, mpc8xxx_gc);

302
	raw_spin_lock_init(&mpc8xxx_gc->lock);
303

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	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");
	}
330

331
	mpc8xxx_gc->direction_output = gc->direction_output;
332 333 334 335 336 337 338 339 340 341

	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;

342 343 344 345 346
	if (devtype->gpio_dir_out)
		gc->direction_output = devtype->gpio_dir_out;
	if (devtype->gpio_get)
		gc->get = devtype->gpio_get;

347
	gc->to_irq = mpc8xxx_gpio_to_irq;
348

349 350 351 352 353 354
	ret = gpiochip_add_data(gc, mpc8xxx_gc);
	if (ret) {
		pr_err("%s: GPIO chip registration failed with status %d\n",
		       np->full_name, ret);
		goto err;
	}
355

356
	mpc8xxx_gc->irqn = irq_of_parse_and_map(np, 0);
357
	if (!mpc8xxx_gc->irqn)
358
		return 0;
359

360 361
	mpc8xxx_gc->irq = irq_domain_add_linear(np, MPC8XXX_GPIO_PINS,
					&mpc8xxx_gpio_irq_ops, mpc8xxx_gc);
362
	if (!mpc8xxx_gc->irq)
363
		return 0;
364 365

	/* ack and mask all irqs */
366 367
	gc->write_reg(mpc8xxx_gc->regs + GPIO_IER, 0xffffffff);
	gc->write_reg(mpc8xxx_gc->regs + GPIO_IMR, 0);
368

369 370
	irq_set_chained_handler_and_data(mpc8xxx_gc->irqn,
					 mpc8xxx_gpio_irq_cascade, mpc8xxx_gc);
371
	return 0;
372 373 374
err:
	iounmap(mpc8xxx_gc->regs);
	return ret;
375 376 377 378 379 380 381
}

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

	if (mpc8xxx_gc->irq) {
382
		irq_set_chained_handler_and_data(mpc8xxx_gc->irqn, NULL, NULL);
383 384 385
		irq_domain_remove(mpc8xxx_gc->irq);
	}

386 387
	gpiochip_remove(&mpc8xxx_gc->gc);
	iounmap(mpc8xxx_gc->regs);
388

389
	return 0;
390 391
}

392 393
static struct platform_driver mpc8xxx_plat_driver = {
	.probe		= mpc8xxx_probe,
394
	.remove		= mpc8xxx_remove,
395 396 397 398 399
	.driver		= {
		.name = "gpio-mpc8xxx",
		.of_match_table	= mpc8xxx_gpio_ids,
	},
};
400

401 402 403
static int __init mpc8xxx_init(void)
{
	return platform_driver_register(&mpc8xxx_plat_driver);
404
}
405 406

arch_initcall(mpc8xxx_init);