blob: f4afd96303c158f78d9cc1bd34210f10d83f4e2d [file] [log] [blame]
Anton Vorontsovaeec56e2010-10-27 15:33:15 -07001/*
2 * Driver for basic memory-mapped GPIO controllers.
3 *
4 * Copyright 2008 MontaVista Software, Inc.
5 * Copyright 2008,2010 Anton Vorontsov <cbouatmailru@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 * ....``.```~~~~````.`.`.`.`.```````'',,,.........`````......`.......
13 * ...`` ```````..
14 * ..The simplest form of a GPIO controller that the driver supports is``
15 * `.just a single "data" register, where GPIO state can be read and/or `
16 * `,..written. ,,..``~~~~ .....``.`.`.~~.```.`.........``````.```````
17 * `````````
18 ___
19_/~~|___/~| . ```~~~~~~ ___/___\___ ,~.`.`.`.`````.~~...,,,,...
20__________|~$@~~~ %~ /o*o*o*o*o*o\ .. Implementing such a GPIO .
21o ` ~~~~\___/~~~~ ` controller in FPGA is ,.`
22 `....trivial..'~`.```.```
23 * ```````
24 * .```````~~~~`..`.``.``.
25 * . The driver supports `... ,..```.`~~~```````````````....````.``,,
26 * . big-endian notation, just`. .. A bit more sophisticated controllers ,
27 * . register the device with -be`. .with a pair of set/clear-bit registers ,
28 * `.. suffix. ```~~`````....`.` . affecting the data register and the .`
29 * ``.`.``...``` ```.. output pins are also supported.`
30 * ^^ `````.`````````.,``~``~``~~``````
31 * . ^^
32 * ,..`.`.`...````````````......`.`.`.`.`.`..`.`.`..
33 * .. The expectation is that in at least some cases . ,-~~~-,
34 * .this will be used with roll-your-own ASIC/FPGA .` \ /
35 * .logic in Verilog or VHDL. ~~~`````````..`````~~` \ /
36 * ..````````......``````````` \o_
37 * |
38 * ^^ / \
39 *
40 * ...`````~~`.....``.`..........``````.`.``.```........``.
41 * ` 8, 16, 32 and 64 bits registers are supported, and``.
42 * . the number of GPIOs is determined by the width of ~
43 * .. the registers. ,............```.`.`..`.`.~~~.`.`.`~
44 * `.......````.```
45 */
46
47#include <linux/init.h>
48#include <linux/bug.h>
49#include <linux/kernel.h>
50#include <linux/module.h>
51#include <linux/spinlock.h>
52#include <linux/compiler.h>
53#include <linux/types.h>
54#include <linux/errno.h>
55#include <linux/log2.h>
56#include <linux/ioport.h>
57#include <linux/io.h>
58#include <linux/gpio.h>
59#include <linux/slab.h>
60#include <linux/platform_device.h>
61#include <linux/mod_devicetable.h>
62#include <linux/basic_mmio_gpio.h>
63
64struct bgpio_chip {
65 struct gpio_chip gc;
Jamie Iles8467afe2011-05-20 00:40:14 -060066
67 unsigned long (*read_reg)(void __iomem *reg);
68 void (*write_reg)(void __iomem *reg, unsigned long data);
69
Anton Vorontsovaeec56e2010-10-27 15:33:15 -070070 void __iomem *reg_dat;
71 void __iomem *reg_set;
72 void __iomem *reg_clr;
Jamie Iles31029112011-05-20 00:40:17 -060073 void __iomem *reg_dir;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -070074
75 /* Number of bits (GPIOs): <register width> * 8. */
76 int bits;
77
78 /*
79 * Some GPIO controllers work with the big-endian bits notation,
80 * e.g. in a 8-bits register, GPIO7 is the least significant bit.
81 */
Jamie Iles8467afe2011-05-20 00:40:14 -060082 unsigned long (*pin2mask)(struct bgpio_chip *bgc, unsigned int pin);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -070083
84 /*
85 * Used to lock bgpio_chip->data. Also, this is needed to keep
86 * shadowed and real data registers writes together.
87 */
88 spinlock_t lock;
89
90 /* Shadowed data register to clear/set bits safely. */
91 unsigned long data;
Jamie Iles31029112011-05-20 00:40:17 -060092
93 /* Shadowed direction registers to clear/set direction safely. */
94 unsigned long dir;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -070095};
96
97static struct bgpio_chip *to_bgpio_chip(struct gpio_chip *gc)
98{
99 return container_of(gc, struct bgpio_chip, gc);
100}
101
Jamie Iles8467afe2011-05-20 00:40:14 -0600102static void bgpio_write8(void __iomem *reg, unsigned long data)
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700103{
Jamie Iles8467afe2011-05-20 00:40:14 -0600104 __raw_writeb(data, reg);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700105}
106
Jamie Iles8467afe2011-05-20 00:40:14 -0600107static unsigned long bgpio_read8(void __iomem *reg)
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700108{
Jamie Iles8467afe2011-05-20 00:40:14 -0600109 return __raw_readb(reg);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700110}
111
Jamie Iles8467afe2011-05-20 00:40:14 -0600112static void bgpio_write16(void __iomem *reg, unsigned long data)
113{
114 __raw_writew(data, reg);
115}
116
117static unsigned long bgpio_read16(void __iomem *reg)
118{
119 return __raw_readw(reg);
120}
121
122static void bgpio_write32(void __iomem *reg, unsigned long data)
123{
124 __raw_writel(data, reg);
125}
126
127static unsigned long bgpio_read32(void __iomem *reg)
128{
129 return __raw_readl(reg);
130}
131
132#if BITS_PER_LONG >= 64
133static void bgpio_write64(void __iomem *reg, unsigned long data)
134{
135 __raw_writeq(data, reg);
136}
137
138static unsigned long bgpio_read64(void __iomem *reg)
139{
140 return __raw_readq(reg);
141}
142#endif /* BITS_PER_LONG >= 64 */
143
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700144static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin)
145{
Jamie Iles8467afe2011-05-20 00:40:14 -0600146 return 1 << pin;
147}
148
149static unsigned long bgpio_pin2mask_be(struct bgpio_chip *bgc,
150 unsigned int pin)
151{
152 return 1 << (bgc->bits - 1 - pin);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700153}
154
155static int bgpio_get(struct gpio_chip *gc, unsigned int gpio)
156{
157 struct bgpio_chip *bgc = to_bgpio_chip(gc);
158
Jamie Iles8467afe2011-05-20 00:40:14 -0600159 return bgc->read_reg(bgc->reg_dat) & bgc->pin2mask(bgc, gpio);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700160}
161
162static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
163{
164 struct bgpio_chip *bgc = to_bgpio_chip(gc);
Jamie Iles8467afe2011-05-20 00:40:14 -0600165 unsigned long mask = bgc->pin2mask(bgc, gpio);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700166 unsigned long flags;
167
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700168 spin_lock_irqsave(&bgc->lock, flags);
169
170 if (val)
171 bgc->data |= mask;
172 else
173 bgc->data &= ~mask;
174
Jamie Iles8467afe2011-05-20 00:40:14 -0600175 bgc->write_reg(bgc->reg_dat, bgc->data);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700176
177 spin_unlock_irqrestore(&bgc->lock, flags);
178}
179
Jamie Ilese027d6f2011-05-20 00:40:16 -0600180static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
181 int val)
182{
183 struct bgpio_chip *bgc = to_bgpio_chip(gc);
184 unsigned long mask = bgc->pin2mask(bgc, gpio);
185
186 if (val)
187 bgc->write_reg(bgc->reg_set, mask);
188 else
189 bgc->write_reg(bgc->reg_clr, mask);
190}
191
Jamie Ilesdd86a0c2011-05-20 00:40:16 -0600192static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val)
193{
194 struct bgpio_chip *bgc = to_bgpio_chip(gc);
195 unsigned long mask = bgc->pin2mask(bgc, gpio);
196 unsigned long flags;
197
198 spin_lock_irqsave(&bgc->lock, flags);
199
200 if (val)
201 bgc->data |= mask;
202 else
203 bgc->data &= ~mask;
204
205 bgc->write_reg(bgc->reg_set, bgc->data);
206
207 spin_unlock_irqrestore(&bgc->lock, flags);
208}
209
Jamie Iles31029112011-05-20 00:40:17 -0600210static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio)
211{
212 return 0;
213}
214
215static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio,
216 int val)
217{
218 gc->set(gc, gpio, val);
219
220 return 0;
221}
222
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700223static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
224{
Jamie Iles31029112011-05-20 00:40:17 -0600225 struct bgpio_chip *bgc = to_bgpio_chip(gc);
226 unsigned long flags;
227
228 spin_lock_irqsave(&bgc->lock, flags);
229
230 bgc->dir &= ~bgc->pin2mask(bgc, gpio);
231 bgc->write_reg(bgc->reg_dir, bgc->dir);
232
233 spin_unlock_irqrestore(&bgc->lock, flags);
234
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700235 return 0;
236}
237
238static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
239{
Jamie Iles31029112011-05-20 00:40:17 -0600240 struct bgpio_chip *bgc = to_bgpio_chip(gc);
241 unsigned long flags;
242
Jamie Ilese027d6f2011-05-20 00:40:16 -0600243 gc->set(gc, gpio, val);
244
Jamie Iles31029112011-05-20 00:40:17 -0600245 spin_lock_irqsave(&bgc->lock, flags);
246
247 bgc->dir |= bgc->pin2mask(bgc, gpio);
248 bgc->write_reg(bgc->reg_dir, bgc->dir);
249
250 spin_unlock_irqrestore(&bgc->lock, flags);
251
252 return 0;
253}
254
255static int bgpio_dir_in_inv(struct gpio_chip *gc, unsigned int gpio)
256{
257 struct bgpio_chip *bgc = to_bgpio_chip(gc);
258 unsigned long flags;
259
260 spin_lock_irqsave(&bgc->lock, flags);
261
262 bgc->dir |= bgc->pin2mask(bgc, gpio);
263 bgc->write_reg(bgc->reg_dir, bgc->dir);
264
265 spin_unlock_irqrestore(&bgc->lock, flags);
266
267 return 0;
268}
269
270static int bgpio_dir_out_inv(struct gpio_chip *gc, unsigned int gpio, int val)
271{
272 struct bgpio_chip *bgc = to_bgpio_chip(gc);
273 unsigned long flags;
274
275 gc->set(gc, gpio, val);
276
277 spin_lock_irqsave(&bgc->lock, flags);
278
279 bgc->dir &= ~bgc->pin2mask(bgc, gpio);
280 bgc->write_reg(bgc->reg_dir, bgc->dir);
281
282 spin_unlock_irqrestore(&bgc->lock, flags);
283
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700284 return 0;
285}
286
Jamie Iles364b5e82011-05-20 00:40:16 -0600287static void __iomem *bgpio_request_and_map(struct device *dev,
288 struct resource *res)
289{
290 if (!devm_request_mem_region(dev, res->start, resource_size(res),
291 res->name ?: "mmio_gpio"))
292 return NULL;
293
294 return devm_ioremap(dev, res->start, resource_size(res));
295}
296
Jamie Iles8467afe2011-05-20 00:40:14 -0600297static int bgpio_setup_accessors(struct platform_device *pdev,
298 struct bgpio_chip *bgc)
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700299{
300 const struct platform_device_id *platid = platform_get_device_id(pdev);
Jamie Iles8467afe2011-05-20 00:40:14 -0600301
302 switch (bgc->bits) {
303 case 8:
304 bgc->read_reg = bgpio_read8;
305 bgc->write_reg = bgpio_write8;
306 break;
307 case 16:
308 bgc->read_reg = bgpio_read16;
309 bgc->write_reg = bgpio_write16;
310 break;
311 case 32:
312 bgc->read_reg = bgpio_read32;
313 bgc->write_reg = bgpio_write32;
314 break;
315#if BITS_PER_LONG >= 64
316 case 64:
317 bgc->read_reg = bgpio_read64;
318 bgc->write_reg = bgpio_write64;
319 break;
320#endif /* BITS_PER_LONG >= 64 */
321 default:
322 dev_err(&pdev->dev, "unsupported data width %u bits\n",
323 bgc->bits);
324 return -EINVAL;
325 }
326
327 bgc->pin2mask = strcmp(platid->name, "basic-mmio-gpio-be") ?
328 bgpio_pin2mask : bgpio_pin2mask_be;
329
330 return 0;
331}
332
Jamie Ilese027d6f2011-05-20 00:40:16 -0600333/*
334 * Create the device and allocate the resources. For setting GPIO's there are
Jamie Ilesdd86a0c2011-05-20 00:40:16 -0600335 * three supported configurations:
Jamie Ilese027d6f2011-05-20 00:40:16 -0600336 *
Jamie Ilesdd86a0c2011-05-20 00:40:16 -0600337 * - single input/output register resource (named "dat").
Jamie Ilese027d6f2011-05-20 00:40:16 -0600338 * - set/clear pair (named "set" and "clr").
Jamie Ilesdd86a0c2011-05-20 00:40:16 -0600339 * - single output register resource and single input resource ("set" and
340 * dat").
Jamie Ilese027d6f2011-05-20 00:40:16 -0600341 *
342 * For the single output register, this drives a 1 by setting a bit and a zero
343 * by clearing a bit. For the set clr pair, this drives a 1 by setting a bit
344 * in the set register and clears it by setting a bit in the clear register.
345 * The configuration is detected by which resources are present.
Jamie Iles31029112011-05-20 00:40:17 -0600346 *
347 * For setting the GPIO direction, there are three supported configurations:
348 *
349 * - simple bidirection GPIO that requires no configuration.
350 * - an output direction register (named "dirout") where a 1 bit
351 * indicates the GPIO is an output.
352 * - an input direction register (named "dirin") where a 1 bit indicates
353 * the GPIO is an input.
Jamie Ilese027d6f2011-05-20 00:40:16 -0600354 */
355static int bgpio_setup_io(struct platform_device *pdev,
356 struct bgpio_chip *bgc)
Jamie Iles8467afe2011-05-20 00:40:14 -0600357{
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700358 struct resource *res_set;
359 struct resource *res_clr;
Jamie Ilese027d6f2011-05-20 00:40:16 -0600360 struct resource *res_dat;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700361 resource_size_t dat_sz;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700362
363 res_dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
364 if (!res_dat)
365 return -EINVAL;
366
367 dat_sz = resource_size(res_dat);
368 if (!is_power_of_2(dat_sz))
369 return -EINVAL;
370
Jamie Ilese027d6f2011-05-20 00:40:16 -0600371 bgc->bits = dat_sz * 8;
372 if (bgc->bits > BITS_PER_LONG)
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700373 return -EINVAL;
374
Jamie Ilese027d6f2011-05-20 00:40:16 -0600375 bgc->reg_dat = bgpio_request_and_map(&pdev->dev, res_dat);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700376 if (!bgc->reg_dat)
377 return -ENOMEM;
378
379 res_set = platform_get_resource_byname(pdev, IORESOURCE_MEM, "set");
380 res_clr = platform_get_resource_byname(pdev, IORESOURCE_MEM, "clr");
381 if (res_set && res_clr) {
382 if (resource_size(res_set) != resource_size(res_clr) ||
Jamie Ilese027d6f2011-05-20 00:40:16 -0600383 resource_size(res_set) != resource_size(res_dat))
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700384 return -EINVAL;
385
Jamie Ilese027d6f2011-05-20 00:40:16 -0600386 bgc->reg_set = bgpio_request_and_map(&pdev->dev, res_set);
387 bgc->reg_clr = bgpio_request_and_map(&pdev->dev, res_clr);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700388 if (!bgc->reg_set || !bgc->reg_clr)
389 return -ENOMEM;
Jamie Ilese027d6f2011-05-20 00:40:16 -0600390
391 bgc->gc.set = bgpio_set_with_clear;
Jamie Ilesdd86a0c2011-05-20 00:40:16 -0600392 } else if (res_set && !res_clr) {
393 if (resource_size(res_set) != resource_size(res_dat))
394 return -EINVAL;
395
396 bgc->reg_set = bgpio_request_and_map(&pdev->dev, res_set);
397 if (!bgc->reg_set)
398 return -ENOMEM;
399
400 bgc->gc.set = bgpio_set_set;
Jamie Ilese027d6f2011-05-20 00:40:16 -0600401 } else {
402 bgc->gc.set = bgpio_set;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700403 }
404
Jamie Ilesdd86a0c2011-05-20 00:40:16 -0600405 bgc->gc.get = bgpio_get;
406
Jamie Ilese027d6f2011-05-20 00:40:16 -0600407 return 0;
408}
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700409
Jamie Iles31029112011-05-20 00:40:17 -0600410static int bgpio_setup_direction(struct platform_device *pdev,
411 struct bgpio_chip *bgc)
412{
413 struct resource *res_dirout;
414 struct resource *res_dirin;
415
416 res_dirout = platform_get_resource_byname(pdev, IORESOURCE_MEM,
417 "dirout");
418 res_dirin = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dirin");
419 if (res_dirout && res_dirin) {
420 return -EINVAL;
421 } else if (res_dirout) {
422 bgc->reg_dir = bgpio_request_and_map(&pdev->dev, res_dirout);
423 if (!bgc->reg_dir)
424 return -ENOMEM;
425 bgc->gc.direction_output = bgpio_dir_out;
426 bgc->gc.direction_input = bgpio_dir_in;
427 } else if (res_dirin) {
428 bgc->reg_dir = bgpio_request_and_map(&pdev->dev, res_dirin);
429 if (!bgc->reg_dir)
430 return -ENOMEM;
431 bgc->gc.direction_output = bgpio_dir_out_inv;
432 bgc->gc.direction_input = bgpio_dir_in_inv;
433 } else {
434 bgc->gc.direction_output = bgpio_simple_dir_out;
435 bgc->gc.direction_input = bgpio_simple_dir_in;
436 }
437
438 return 0;
439}
440
Jamie Ilese027d6f2011-05-20 00:40:16 -0600441static int __devinit bgpio_probe(struct platform_device *pdev)
442{
443 struct device *dev = &pdev->dev;
444 struct bgpio_pdata *pdata = dev_get_platdata(dev);
445 struct bgpio_chip *bgc;
446 int ret;
447 int ngpio;
448
449 bgc = devm_kzalloc(dev, sizeof(*bgc), GFP_KERNEL);
450 if (!bgc)
451 return -ENOMEM;
452
453 ret = bgpio_setup_io(pdev, bgc);
454 if (ret)
455 return ret;
456
457 ngpio = bgc->bits;
Jamie Iles924e7a92011-05-20 00:40:15 -0600458 if (pdata) {
459 bgc->gc.base = pdata->base;
460 if (pdata->ngpio > 0)
461 ngpio = pdata->ngpio;
462 } else {
463 bgc->gc.base = -1;
464 }
465
Jamie Iles8467afe2011-05-20 00:40:14 -0600466 ret = bgpio_setup_accessors(pdev, bgc);
467 if (ret)
468 return ret;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700469
Jamie Ilese027d6f2011-05-20 00:40:16 -0600470 spin_lock_init(&bgc->lock);
Jamie Iles31029112011-05-20 00:40:17 -0600471 ret = bgpio_setup_direction(pdev, bgc);
472 if (ret)
473 return ret;
474
Jamie Iles8467afe2011-05-20 00:40:14 -0600475 bgc->data = bgc->read_reg(bgc->reg_dat);
Jamie Iles924e7a92011-05-20 00:40:15 -0600476
477 bgc->gc.ngpio = ngpio;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700478 bgc->gc.dev = dev;
479 bgc->gc.label = dev_name(dev);
480
Jamie Iles4ddb8ae2011-05-20 00:40:14 -0600481 platform_set_drvdata(pdev, bgc);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700482
483 ret = gpiochip_add(&bgc->gc);
484 if (ret)
485 dev_err(dev, "gpiochip_add() failed: %d\n", ret);
486
487 return ret;
488}
489
490static int __devexit bgpio_remove(struct platform_device *pdev)
491{
Jamie Iles4ddb8ae2011-05-20 00:40:14 -0600492 struct bgpio_chip *bgc = platform_get_drvdata(pdev);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700493
494 return gpiochip_remove(&bgc->gc);
495}
496
497static const struct platform_device_id bgpio_id_table[] = {
498 { "basic-mmio-gpio", },
499 { "basic-mmio-gpio-be", },
500 {},
501};
502MODULE_DEVICE_TABLE(platform, bgpio_id_table);
503
504static struct platform_driver bgpio_driver = {
505 .driver = {
506 .name = "basic-mmio-gpio",
507 },
508 .id_table = bgpio_id_table,
509 .probe = bgpio_probe,
510 .remove = __devexit_p(bgpio_remove),
511};
512
513static int __init bgpio_init(void)
514{
515 return platform_driver_register(&bgpio_driver);
516}
517module_init(bgpio_init);
518
519static void __exit bgpio_exit(void)
520{
521 platform_driver_unregister(&bgpio_driver);
522}
523module_exit(bgpio_exit);
524
525MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers");
526MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
527MODULE_LICENSE("GPL");