blob: 6b99489e0ebb12032c07c6c6be158a5c308c6565 [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;
73
74 /* Number of bits (GPIOs): <register width> * 8. */
75 int bits;
76
77 /*
78 * Some GPIO controllers work with the big-endian bits notation,
79 * e.g. in a 8-bits register, GPIO7 is the least significant bit.
80 */
Jamie Iles8467afe2011-05-20 00:40:14 -060081 unsigned long (*pin2mask)(struct bgpio_chip *bgc, unsigned int pin);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -070082
83 /*
84 * Used to lock bgpio_chip->data. Also, this is needed to keep
85 * shadowed and real data registers writes together.
86 */
87 spinlock_t lock;
88
89 /* Shadowed data register to clear/set bits safely. */
90 unsigned long data;
91};
92
93static struct bgpio_chip *to_bgpio_chip(struct gpio_chip *gc)
94{
95 return container_of(gc, struct bgpio_chip, gc);
96}
97
Jamie Iles8467afe2011-05-20 00:40:14 -060098static void bgpio_write8(void __iomem *reg, unsigned long data)
Anton Vorontsovaeec56e2010-10-27 15:33:15 -070099{
Jamie Iles8467afe2011-05-20 00:40:14 -0600100 __raw_writeb(data, reg);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700101}
102
Jamie Iles8467afe2011-05-20 00:40:14 -0600103static unsigned long bgpio_read8(void __iomem *reg)
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700104{
Jamie Iles8467afe2011-05-20 00:40:14 -0600105 return __raw_readb(reg);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700106}
107
Jamie Iles8467afe2011-05-20 00:40:14 -0600108static void bgpio_write16(void __iomem *reg, unsigned long data)
109{
110 __raw_writew(data, reg);
111}
112
113static unsigned long bgpio_read16(void __iomem *reg)
114{
115 return __raw_readw(reg);
116}
117
118static void bgpio_write32(void __iomem *reg, unsigned long data)
119{
120 __raw_writel(data, reg);
121}
122
123static unsigned long bgpio_read32(void __iomem *reg)
124{
125 return __raw_readl(reg);
126}
127
128#if BITS_PER_LONG >= 64
129static void bgpio_write64(void __iomem *reg, unsigned long data)
130{
131 __raw_writeq(data, reg);
132}
133
134static unsigned long bgpio_read64(void __iomem *reg)
135{
136 return __raw_readq(reg);
137}
138#endif /* BITS_PER_LONG >= 64 */
139
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700140static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin)
141{
Jamie Iles8467afe2011-05-20 00:40:14 -0600142 return 1 << pin;
143}
144
145static unsigned long bgpio_pin2mask_be(struct bgpio_chip *bgc,
146 unsigned int pin)
147{
148 return 1 << (bgc->bits - 1 - pin);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700149}
150
151static int bgpio_get(struct gpio_chip *gc, unsigned int gpio)
152{
153 struct bgpio_chip *bgc = to_bgpio_chip(gc);
154
Jamie Iles8467afe2011-05-20 00:40:14 -0600155 return bgc->read_reg(bgc->reg_dat) & bgc->pin2mask(bgc, gpio);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700156}
157
158static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
159{
160 struct bgpio_chip *bgc = to_bgpio_chip(gc);
Jamie Iles8467afe2011-05-20 00:40:14 -0600161 unsigned long mask = bgc->pin2mask(bgc, gpio);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700162 unsigned long flags;
163
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700164 spin_lock_irqsave(&bgc->lock, flags);
165
166 if (val)
167 bgc->data |= mask;
168 else
169 bgc->data &= ~mask;
170
Jamie Iles8467afe2011-05-20 00:40:14 -0600171 bgc->write_reg(bgc->reg_dat, bgc->data);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700172
173 spin_unlock_irqrestore(&bgc->lock, flags);
174}
175
Jamie Ilese027d6f2011-05-20 00:40:16 -0600176static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
177 int val)
178{
179 struct bgpio_chip *bgc = to_bgpio_chip(gc);
180 unsigned long mask = bgc->pin2mask(bgc, gpio);
181
182 if (val)
183 bgc->write_reg(bgc->reg_set, mask);
184 else
185 bgc->write_reg(bgc->reg_clr, mask);
186}
187
Jamie Ilesdd86a0c2011-05-20 00:40:16 -0600188static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val)
189{
190 struct bgpio_chip *bgc = to_bgpio_chip(gc);
191 unsigned long mask = bgc->pin2mask(bgc, gpio);
192 unsigned long flags;
193
194 spin_lock_irqsave(&bgc->lock, flags);
195
196 if (val)
197 bgc->data |= mask;
198 else
199 bgc->data &= ~mask;
200
201 bgc->write_reg(bgc->reg_set, bgc->data);
202
203 spin_unlock_irqrestore(&bgc->lock, flags);
204}
205
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700206static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
207{
208 return 0;
209}
210
211static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
212{
Jamie Ilese027d6f2011-05-20 00:40:16 -0600213 gc->set(gc, gpio, val);
214
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700215 return 0;
216}
217
Jamie Iles364b5e82011-05-20 00:40:16 -0600218static void __iomem *bgpio_request_and_map(struct device *dev,
219 struct resource *res)
220{
221 if (!devm_request_mem_region(dev, res->start, resource_size(res),
222 res->name ?: "mmio_gpio"))
223 return NULL;
224
225 return devm_ioremap(dev, res->start, resource_size(res));
226}
227
Jamie Iles8467afe2011-05-20 00:40:14 -0600228static int bgpio_setup_accessors(struct platform_device *pdev,
229 struct bgpio_chip *bgc)
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700230{
231 const struct platform_device_id *platid = platform_get_device_id(pdev);
Jamie Iles8467afe2011-05-20 00:40:14 -0600232
233 switch (bgc->bits) {
234 case 8:
235 bgc->read_reg = bgpio_read8;
236 bgc->write_reg = bgpio_write8;
237 break;
238 case 16:
239 bgc->read_reg = bgpio_read16;
240 bgc->write_reg = bgpio_write16;
241 break;
242 case 32:
243 bgc->read_reg = bgpio_read32;
244 bgc->write_reg = bgpio_write32;
245 break;
246#if BITS_PER_LONG >= 64
247 case 64:
248 bgc->read_reg = bgpio_read64;
249 bgc->write_reg = bgpio_write64;
250 break;
251#endif /* BITS_PER_LONG >= 64 */
252 default:
253 dev_err(&pdev->dev, "unsupported data width %u bits\n",
254 bgc->bits);
255 return -EINVAL;
256 }
257
258 bgc->pin2mask = strcmp(platid->name, "basic-mmio-gpio-be") ?
259 bgpio_pin2mask : bgpio_pin2mask_be;
260
261 return 0;
262}
263
Jamie Ilese027d6f2011-05-20 00:40:16 -0600264/*
265 * Create the device and allocate the resources. For setting GPIO's there are
Jamie Ilesdd86a0c2011-05-20 00:40:16 -0600266 * three supported configurations:
Jamie Ilese027d6f2011-05-20 00:40:16 -0600267 *
Jamie Ilesdd86a0c2011-05-20 00:40:16 -0600268 * - single input/output register resource (named "dat").
Jamie Ilese027d6f2011-05-20 00:40:16 -0600269 * - set/clear pair (named "set" and "clr").
Jamie Ilesdd86a0c2011-05-20 00:40:16 -0600270 * - single output register resource and single input resource ("set" and
271 * dat").
Jamie Ilese027d6f2011-05-20 00:40:16 -0600272 *
273 * For the single output register, this drives a 1 by setting a bit and a zero
274 * by clearing a bit. For the set clr pair, this drives a 1 by setting a bit
275 * in the set register and clears it by setting a bit in the clear register.
276 * The configuration is detected by which resources are present.
277 */
278static int bgpio_setup_io(struct platform_device *pdev,
279 struct bgpio_chip *bgc)
Jamie Iles8467afe2011-05-20 00:40:14 -0600280{
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700281 struct resource *res_set;
282 struct resource *res_clr;
Jamie Ilese027d6f2011-05-20 00:40:16 -0600283 struct resource *res_dat;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700284 resource_size_t dat_sz;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700285
286 res_dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
287 if (!res_dat)
288 return -EINVAL;
289
290 dat_sz = resource_size(res_dat);
291 if (!is_power_of_2(dat_sz))
292 return -EINVAL;
293
Jamie Ilese027d6f2011-05-20 00:40:16 -0600294 bgc->bits = dat_sz * 8;
295 if (bgc->bits > BITS_PER_LONG)
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700296 return -EINVAL;
297
Jamie Ilese027d6f2011-05-20 00:40:16 -0600298 bgc->reg_dat = bgpio_request_and_map(&pdev->dev, res_dat);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700299 if (!bgc->reg_dat)
300 return -ENOMEM;
301
302 res_set = platform_get_resource_byname(pdev, IORESOURCE_MEM, "set");
303 res_clr = platform_get_resource_byname(pdev, IORESOURCE_MEM, "clr");
304 if (res_set && res_clr) {
305 if (resource_size(res_set) != resource_size(res_clr) ||
Jamie Ilese027d6f2011-05-20 00:40:16 -0600306 resource_size(res_set) != resource_size(res_dat))
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700307 return -EINVAL;
308
Jamie Ilese027d6f2011-05-20 00:40:16 -0600309 bgc->reg_set = bgpio_request_and_map(&pdev->dev, res_set);
310 bgc->reg_clr = bgpio_request_and_map(&pdev->dev, res_clr);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700311 if (!bgc->reg_set || !bgc->reg_clr)
312 return -ENOMEM;
Jamie Ilese027d6f2011-05-20 00:40:16 -0600313
314 bgc->gc.set = bgpio_set_with_clear;
Jamie Ilesdd86a0c2011-05-20 00:40:16 -0600315 } else if (res_set && !res_clr) {
316 if (resource_size(res_set) != resource_size(res_dat))
317 return -EINVAL;
318
319 bgc->reg_set = bgpio_request_and_map(&pdev->dev, res_set);
320 if (!bgc->reg_set)
321 return -ENOMEM;
322
323 bgc->gc.set = bgpio_set_set;
Jamie Ilese027d6f2011-05-20 00:40:16 -0600324 } else {
325 bgc->gc.set = bgpio_set;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700326 }
327
Jamie Ilesdd86a0c2011-05-20 00:40:16 -0600328 bgc->gc.get = bgpio_get;
329
Jamie Ilese027d6f2011-05-20 00:40:16 -0600330 return 0;
331}
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700332
Jamie Ilese027d6f2011-05-20 00:40:16 -0600333static int __devinit bgpio_probe(struct platform_device *pdev)
334{
335 struct device *dev = &pdev->dev;
336 struct bgpio_pdata *pdata = dev_get_platdata(dev);
337 struct bgpio_chip *bgc;
338 int ret;
339 int ngpio;
340
341 bgc = devm_kzalloc(dev, sizeof(*bgc), GFP_KERNEL);
342 if (!bgc)
343 return -ENOMEM;
344
345 ret = bgpio_setup_io(pdev, bgc);
346 if (ret)
347 return ret;
348
349 ngpio = bgc->bits;
Jamie Iles924e7a92011-05-20 00:40:15 -0600350 if (pdata) {
351 bgc->gc.base = pdata->base;
352 if (pdata->ngpio > 0)
353 ngpio = pdata->ngpio;
354 } else {
355 bgc->gc.base = -1;
356 }
357
Jamie Iles8467afe2011-05-20 00:40:14 -0600358 ret = bgpio_setup_accessors(pdev, bgc);
359 if (ret)
360 return ret;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700361
Jamie Ilese027d6f2011-05-20 00:40:16 -0600362 spin_lock_init(&bgc->lock);
Jamie Iles8467afe2011-05-20 00:40:14 -0600363 bgc->data = bgc->read_reg(bgc->reg_dat);
Jamie Iles924e7a92011-05-20 00:40:15 -0600364
365 bgc->gc.ngpio = ngpio;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700366 bgc->gc.direction_input = bgpio_dir_in;
367 bgc->gc.direction_output = bgpio_dir_out;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700368 bgc->gc.dev = dev;
369 bgc->gc.label = dev_name(dev);
370
Jamie Iles4ddb8ae2011-05-20 00:40:14 -0600371 platform_set_drvdata(pdev, bgc);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700372
373 ret = gpiochip_add(&bgc->gc);
374 if (ret)
375 dev_err(dev, "gpiochip_add() failed: %d\n", ret);
376
377 return ret;
378}
379
380static int __devexit bgpio_remove(struct platform_device *pdev)
381{
Jamie Iles4ddb8ae2011-05-20 00:40:14 -0600382 struct bgpio_chip *bgc = platform_get_drvdata(pdev);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700383
384 return gpiochip_remove(&bgc->gc);
385}
386
387static const struct platform_device_id bgpio_id_table[] = {
388 { "basic-mmio-gpio", },
389 { "basic-mmio-gpio-be", },
390 {},
391};
392MODULE_DEVICE_TABLE(platform, bgpio_id_table);
393
394static struct platform_driver bgpio_driver = {
395 .driver = {
396 .name = "basic-mmio-gpio",
397 },
398 .id_table = bgpio_id_table,
399 .probe = bgpio_probe,
400 .remove = __devexit_p(bgpio_remove),
401};
402
403static int __init bgpio_init(void)
404{
405 return platform_driver_register(&bgpio_driver);
406}
407module_init(bgpio_init);
408
409static void __exit bgpio_exit(void)
410{
411 platform_driver_unregister(&bgpio_driver);
412}
413module_exit(bgpio_exit);
414
415MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers");
416MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
417MODULE_LICENSE("GPL");