blob: 90f7f8930d5bdd824ad190e902db65ab1c97aea1 [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
164 if (bgc->reg_set) {
165 if (val)
Jamie Iles8467afe2011-05-20 00:40:14 -0600166 bgc->write_reg(bgc->reg_set, mask);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700167 else
Jamie Iles8467afe2011-05-20 00:40:14 -0600168 bgc->write_reg(bgc->reg_clr, mask);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700169 return;
170 }
171
172 spin_lock_irqsave(&bgc->lock, flags);
173
174 if (val)
175 bgc->data |= mask;
176 else
177 bgc->data &= ~mask;
178
Jamie Iles8467afe2011-05-20 00:40:14 -0600179 bgc->write_reg(bgc->reg_dat, bgc->data);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700180
181 spin_unlock_irqrestore(&bgc->lock, flags);
182}
183
184static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
185{
186 return 0;
187}
188
189static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
190{
191 bgpio_set(gc, gpio, val);
192 return 0;
193}
194
Jamie Iles364b5e82011-05-20 00:40:16 -0600195static void __iomem *bgpio_request_and_map(struct device *dev,
196 struct resource *res)
197{
198 if (!devm_request_mem_region(dev, res->start, resource_size(res),
199 res->name ?: "mmio_gpio"))
200 return NULL;
201
202 return devm_ioremap(dev, res->start, resource_size(res));
203}
204
Jamie Iles8467afe2011-05-20 00:40:14 -0600205static int bgpio_setup_accessors(struct platform_device *pdev,
206 struct bgpio_chip *bgc)
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700207{
208 const struct platform_device_id *platid = platform_get_device_id(pdev);
Jamie Iles8467afe2011-05-20 00:40:14 -0600209
210 switch (bgc->bits) {
211 case 8:
212 bgc->read_reg = bgpio_read8;
213 bgc->write_reg = bgpio_write8;
214 break;
215 case 16:
216 bgc->read_reg = bgpio_read16;
217 bgc->write_reg = bgpio_write16;
218 break;
219 case 32:
220 bgc->read_reg = bgpio_read32;
221 bgc->write_reg = bgpio_write32;
222 break;
223#if BITS_PER_LONG >= 64
224 case 64:
225 bgc->read_reg = bgpio_read64;
226 bgc->write_reg = bgpio_write64;
227 break;
228#endif /* BITS_PER_LONG >= 64 */
229 default:
230 dev_err(&pdev->dev, "unsupported data width %u bits\n",
231 bgc->bits);
232 return -EINVAL;
233 }
234
235 bgc->pin2mask = strcmp(platid->name, "basic-mmio-gpio-be") ?
236 bgpio_pin2mask : bgpio_pin2mask_be;
237
238 return 0;
239}
240
241static int __devinit bgpio_probe(struct platform_device *pdev)
242{
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700243 struct device *dev = &pdev->dev;
244 struct bgpio_pdata *pdata = dev_get_platdata(dev);
245 struct bgpio_chip *bgc;
246 struct resource *res_dat;
247 struct resource *res_set;
248 struct resource *res_clr;
249 resource_size_t dat_sz;
250 int bits;
251 int ret;
Jamie Iles924e7a92011-05-20 00:40:15 -0600252 int ngpio;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700253
254 res_dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
255 if (!res_dat)
256 return -EINVAL;
257
258 dat_sz = resource_size(res_dat);
259 if (!is_power_of_2(dat_sz))
260 return -EINVAL;
261
262 bits = dat_sz * 8;
Jamie Iles924e7a92011-05-20 00:40:15 -0600263 ngpio = bits;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700264 if (bits > BITS_PER_LONG)
265 return -EINVAL;
266
267 bgc = devm_kzalloc(dev, sizeof(*bgc), GFP_KERNEL);
268 if (!bgc)
269 return -ENOMEM;
270
Jamie Iles364b5e82011-05-20 00:40:16 -0600271 bgc->reg_dat = bgpio_request_and_map(dev, res_dat);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700272 if (!bgc->reg_dat)
273 return -ENOMEM;
274
275 res_set = platform_get_resource_byname(pdev, IORESOURCE_MEM, "set");
276 res_clr = platform_get_resource_byname(pdev, IORESOURCE_MEM, "clr");
277 if (res_set && res_clr) {
278 if (resource_size(res_set) != resource_size(res_clr) ||
279 resource_size(res_set) != dat_sz)
280 return -EINVAL;
281
Jamie Iles364b5e82011-05-20 00:40:16 -0600282 bgc->reg_set = bgpio_request_and_map(dev, res_set);
283 bgc->reg_clr = bgpio_request_and_map(dev, res_clr);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700284 if (!bgc->reg_set || !bgc->reg_clr)
285 return -ENOMEM;
286 } else if (res_set || res_clr) {
287 return -EINVAL;
288 }
289
290 spin_lock_init(&bgc->lock);
291
Jamie Iles924e7a92011-05-20 00:40:15 -0600292 if (pdata) {
293 bgc->gc.base = pdata->base;
294 if (pdata->ngpio > 0)
295 ngpio = pdata->ngpio;
296 } else {
297 bgc->gc.base = -1;
298 }
299
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700300 bgc->bits = bits;
Jamie Iles8467afe2011-05-20 00:40:14 -0600301 ret = bgpio_setup_accessors(pdev, bgc);
302 if (ret)
303 return ret;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700304
Jamie Iles8467afe2011-05-20 00:40:14 -0600305 bgc->data = bgc->read_reg(bgc->reg_dat);
Jamie Iles924e7a92011-05-20 00:40:15 -0600306
307 bgc->gc.ngpio = ngpio;
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700308 bgc->gc.direction_input = bgpio_dir_in;
309 bgc->gc.direction_output = bgpio_dir_out;
310 bgc->gc.get = bgpio_get;
311 bgc->gc.set = bgpio_set;
312 bgc->gc.dev = dev;
313 bgc->gc.label = dev_name(dev);
314
Jamie Iles4ddb8ae2011-05-20 00:40:14 -0600315 platform_set_drvdata(pdev, bgc);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700316
317 ret = gpiochip_add(&bgc->gc);
318 if (ret)
319 dev_err(dev, "gpiochip_add() failed: %d\n", ret);
320
321 return ret;
322}
323
324static int __devexit bgpio_remove(struct platform_device *pdev)
325{
Jamie Iles4ddb8ae2011-05-20 00:40:14 -0600326 struct bgpio_chip *bgc = platform_get_drvdata(pdev);
Anton Vorontsovaeec56e2010-10-27 15:33:15 -0700327
328 return gpiochip_remove(&bgc->gc);
329}
330
331static const struct platform_device_id bgpio_id_table[] = {
332 { "basic-mmio-gpio", },
333 { "basic-mmio-gpio-be", },
334 {},
335};
336MODULE_DEVICE_TABLE(platform, bgpio_id_table);
337
338static struct platform_driver bgpio_driver = {
339 .driver = {
340 .name = "basic-mmio-gpio",
341 },
342 .id_table = bgpio_id_table,
343 .probe = bgpio_probe,
344 .remove = __devexit_p(bgpio_remove),
345};
346
347static int __init bgpio_init(void)
348{
349 return platform_driver_register(&bgpio_driver);
350}
351module_init(bgpio_init);
352
353static void __exit bgpio_exit(void)
354{
355 platform_driver_unregister(&bgpio_driver);
356}
357module_exit(bgpio_exit);
358
359MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers");
360MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
361MODULE_LICENSE("GPL");