[brcm63xx] add 74x164 SPI chip support
[openwrt.git] / target / linux / brcm63xx / patches-2.6.35 / 211-gen-74x164-gpio-chip-driver.patch
1 --- a/drivers/gpio/Kconfig
2 +++ b/drivers/gpio/Kconfig
3 @@ -321,6 +321,14 @@ config GPIO_MC33880
4           SPI driver for Freescale MC33880 high-side/low-side switch.
5           This provides GPIO interface supporting inputs and outputs.
6  
7 +config GPIO_74X164
8 +       tristate "74x164 serial-in/parallel-out 8-bits shift register"
9 +       depends on SPI_MASTER
10 +       help
11 +         Platform driver for 74x164 compatible serial-in/parallel-out
12 +         8-outputs shift registers. This driver can be used to provide access
13 +         to more gpio outputs.
14 +
15  comment "AC97 GPIO expanders:"
16  
17  config GPIO_UCB1400
18 --- a/drivers/gpio/Makefile
19 +++ b/drivers/gpio/Makefile
20 @@ -17,6 +17,7 @@ obj-$(CONFIG_GPIO_MAX7301)    += max7301.o
21  obj-$(CONFIG_GPIO_MAX732X)     += max732x.o
22  obj-$(CONFIG_GPIO_MC33880)     += mc33880.o
23  obj-$(CONFIG_GPIO_MCP23S08)    += mcp23s08.o
24 +obj-$(CONFIG_GPIO_74X164)      += 74x164.o
25  obj-$(CONFIG_GPIO_PCA953X)     += pca953x.o
26  obj-$(CONFIG_GPIO_PCF857X)     += pcf857x.o
27  obj-$(CONFIG_GPIO_PL061)       += pl061.o
28 --- /dev/null
29 +++ b/drivers/gpio/nxp_74hc164.c
30 @@ -0,0 +1,226 @@
31 +/*
32 + *  NXP 74HC164 - output expander  GPIO driver
33 + *
34 + *  Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
35 + *  Copyright (C) 2010 Miguel Gaio <miguel.gaio@efixo.com>
36 + *
37 + *  This program is free software; you can redistribute it and/or modify
38 + *  it under the terms of the GNU General Public License version 2 as
39 + *  published by the Free Software Foundation.
40 + *
41 + *  Copy from nxp_74hc153.c code
42 + */
43 +
44 +#include <linux/module.h>
45 +#include <linux/init.h>
46 +#include <linux/gpio.h>
47 +#include <linux/bitops.h>
48 +#include <linux/platform_device.h>
49 +#include <linux/nxp_74hc164.h>
50 +
51 +#define NXP_74HC164_NUM_GPIOS  8
52 +
53 +struct nxp_74hc164_chip {
54 +       struct device           *parent;
55 +       struct gpio_chip        gpio_chip;
56 +       struct mutex            lock;
57 +       long                    mask;
58 +};
59 +
60 +static void nxp_74hc164_set_value(struct gpio_chip *, unsigned, int);
61 +
62 +static struct nxp_74hc164_chip *gpio_to_nxp(struct gpio_chip *gc)
63 +{
64 +       return container_of(gc, struct nxp_74hc164_chip, gpio_chip);
65 +}
66 +
67 +static int nxp_74hc164_direction_input(struct gpio_chip *gc, unsigned offset)
68 +{
69 +       WARN_ON(1);
70 +       return -EINVAL;
71 +}
72 +
73 +static int nxp_74hc164_direction_output(struct gpio_chip *gc,
74 +                                       unsigned offset, int val)
75 +{
76 +       nxp_74hc164_set_value(gc, offset, val);
77 +       return 0;
78 +}
79 +
80 +static int nxp_74hc164_get_value(struct gpio_chip *gc, unsigned offset)
81 +{
82 +       struct nxp_74hc164_chip *nxp = gpio_to_nxp(gc);
83 +       int ret;
84 +
85 +       mutex_lock(&nxp->lock);
86 +       ret = test_bit(offset, &nxp->mask);
87 +       mutex_unlock(&nxp->lock);
88 +
89 +       return ret;
90 +}
91 +
92 +static void nxp_74hc164_set_value(struct gpio_chip *gc,
93 +                                 unsigned offset, int val)
94 +{
95 +       struct nxp_74hc164_chip *nxp;
96 +       struct nxp_74hc164_platform_data *pdata;
97 +       long mask;
98 +       int refresh;
99 +       int i;
100 +
101 +       nxp = gpio_to_nxp(gc);
102 +       pdata = nxp->parent->platform_data;
103 +
104 +       mutex_lock(&nxp->lock);
105 +       if (val)
106 +               refresh = (test_and_set_bit(offset, &nxp->mask) != val);
107 +       else
108 +               refresh = (test_and_clear_bit(offset, &nxp->mask) != val);
109 +
110 +       if (refresh) {
111 +               mask = nxp->mask;
112 +               for (i = 8; i > 0; --i, mask <<= 1) {
113 +                       gpio_set_value(pdata->gpio_pin_data, mask & 0x80);
114 +                       gpio_set_value(pdata->gpio_pin_clk, 1);
115 +                       gpio_set_value(pdata->gpio_pin_clk, 0);
116 +               }
117 +       }
118 +       mutex_unlock(&nxp->lock);
119 +}
120 +
121 +static int __devinit nxp_74hc164_probe(struct platform_device *pdev)
122 +{
123 +       struct nxp_74hc164_platform_data *pdata;
124 +       struct nxp_74hc164_chip *nxp;
125 +       struct gpio_chip *gc;
126 +       int err;
127 +
128 +       pdata = pdev->dev.platform_data;
129 +       if (pdata == NULL) {
130 +               dev_dbg(&pdev->dev, "no platform data specified\n");
131 +               return -EINVAL;
132 +       }
133 +
134 +       nxp = kzalloc(sizeof(struct nxp_74hc164_chip), GFP_KERNEL);
135 +       if (nxp == NULL) {
136 +               dev_err(&pdev->dev, "no memory for private data\n");
137 +               return -ENOMEM;
138 +       }
139 +
140 +       err = gpio_request(pdata->gpio_pin_clk, dev_name(&pdev->dev));
141 +       if (err) {
142 +               dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
143 +                       pdata->gpio_pin_clk, err);
144 +               goto err_free_nxp;
145 +       }
146 +
147 +       err = gpio_request(pdata->gpio_pin_data, dev_name(&pdev->dev));
148 +       if (err) {
149 +               dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
150 +                       pdata->gpio_pin_data, err);
151 +               goto err_free_clk;
152 +       }
153 +
154 +       err = gpio_direction_output(pdata->gpio_pin_clk, 0);
155 +       if (err) {
156 +               dev_err(&pdev->dev,
157 +                       "unable to set direction of gpio %u, err=%d\n",
158 +                       pdata->gpio_pin_clk, err);
159 +               goto err_free_data;
160 +       }
161 +
162 +       err = gpio_direction_output(pdata->gpio_pin_data, 0);
163 +       if (err) {
164 +               dev_err(&pdev->dev,
165 +                       "unable to set direction of gpio %u, err=%d\n",
166 +                       pdata->gpio_pin_data, err);
167 +               goto err_free_data;
168 +       }
169 +
170 +       nxp->parent = &pdev->dev;
171 +       mutex_init(&nxp->lock);
172 +
173 +       gc = &nxp->gpio_chip;
174 +
175 +       gc->direction_input  = nxp_74hc164_direction_input;
176 +       gc->direction_output = nxp_74hc164_direction_output;
177 +       gc->get = nxp_74hc164_get_value;
178 +       gc->set = nxp_74hc164_set_value;
179 +       gc->can_sleep = 1;
180 +
181 +       gc->base = pdata->gpio_base;
182 +       gc->ngpio = NXP_74HC164_NUM_GPIOS;
183 +       gc->label = dev_name(nxp->parent);
184 +       gc->dev = nxp->parent;
185 +       gc->owner = THIS_MODULE;
186 +
187 +       err = gpiochip_add(&nxp->gpio_chip);
188 +       if (err) {
189 +               dev_err(&pdev->dev, "unable to add gpio chip, err=%d\n", err);
190 +               goto err_free_data;
191 +       }
192 +
193 +       platform_set_drvdata(pdev, nxp);
194 +       return 0;
195 +
196 + err_free_data:
197 +       gpio_free(pdata->gpio_pin_data);
198 + err_free_clk:
199 +       gpio_free(pdata->gpio_pin_clk);
200 + err_free_nxp:
201 +       kfree(nxp);
202 +       return err;
203 +}
204 +
205 +static int nxp_74hc164_remove(struct platform_device *pdev)
206 +{
207 +       struct nxp_74hc164_chip *nxp = platform_get_drvdata(pdev);
208 +       struct nxp_74hc164_platform_data *pdata = pdev->dev.platform_data;
209 +
210 +       if (nxp) {
211 +               int err;
212 +
213 +               err = gpiochip_remove(&nxp->gpio_chip);
214 +               if (err) {
215 +                       dev_err(&pdev->dev,
216 +                               "unable to remove gpio chip, err=%d\n",
217 +                               err);
218 +                       return err;
219 +               }
220 +
221 +               gpio_free(pdata->gpio_pin_clk);
222 +               gpio_free(pdata->gpio_pin_data);
223 +
224 +               kfree(nxp);
225 +               platform_set_drvdata(pdev, NULL);
226 +       }
227 +
228 +       return 0;
229 +}
230 +
231 +static struct platform_driver nxp_74hc164_driver = {
232 +       .probe          = nxp_74hc164_probe,
233 +       .remove         = __devexit_p(nxp_74hc164_remove),
234 +       .driver = {
235 +               .name   = NXP_74HC164_DRIVER_NAME,
236 +               .owner  = THIS_MODULE,
237 +       },
238 +};
239 +
240 +static int __init nxp_74hc164_init(void)
241 +{
242 +       return platform_driver_register(&nxp_74hc164_driver);
243 +}
244 +subsys_initcall(nxp_74hc164_init);
245 +
246 +static void __exit nxp_74hc164_exit(void)
247 +{
248 +       platform_driver_unregister(&nxp_74hc164_driver);
249 +}
250 +module_exit(nxp_74hc164_exit);
251 +
252 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
253 +MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
254 +MODULE_DESCRIPTION("GPIO expander driver for NXP 74HC164");
255 +MODULE_LICENSE("GPL v2");
256 +MODULE_ALIAS("platform:" NXP_74HC164_DRIVER_NAME);
257 --- /dev/null
258 +++ b/drivers/gpio/74x164.c
259 @@ -0,0 +1,184 @@
260 +/*
261 + *  74Hx164 - Generic serial-in/parallel-out 8-bits shift register GPIO driver
262 + *
263 + *  Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
264 + *  Copyright (C) 2010 Miguel Gaio <miguel.gaio@efixo.com>
265 + *
266 + *  This program is free software; you can redistribute it and/or modify
267 + *  it under the terms of the GNU General Public License version 2 as
268 + *  published by the Free Software Foundation.
269 + */
270 +
271 +#include <linux/init.h>
272 +#include <linux/mutex.h>
273 +#include <linux/spi/spi.h>
274 +#include <linux/spi/74x164.h>
275 +#include <linux/gpio.h>
276 +#include <linux/slab.h>
277 +
278 +#define GEN_74X164_GPIO_COUNT  8
279 +
280 +
281 +struct gen_74x164_chip {
282 +       struct spi_device       *spi;
283 +       struct gpio_chip        gpio_chip;
284 +       struct mutex            lock;
285 +       u8                      port_config;
286 +};
287 +
288 +static void gen_74x164_set_value(struct gpio_chip *, unsigned, int);
289 +
290 +static struct gen_74x164_chip *gpio_to_chip(struct gpio_chip *gc)
291 +{
292 +       return container_of(gc, struct gen_74x164_chip, gpio_chip);
293 +}
294 +
295 +static int __gen_74x164_write_config(struct gen_74x164_chip *chip)
296 +{
297 +       return spi_write(chip->spi,
298 +                        &chip->port_config, sizeof(chip->port_config));
299 +}
300 +
301 +static int gen_74x164_direction_output(struct gpio_chip *gc,
302 +               unsigned offset, int val)
303 +{
304 +       gen_74x164_set_value(gc, offset, val);
305 +       return 0;
306 +}
307 +
308 +static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset)
309 +{
310 +       struct gen_74x164_chip *chip = gpio_to_chip(gc);
311 +       int ret;
312 +
313 +       mutex_lock(&chip->lock);
314 +       ret = (chip->port_config >> offset) & 0x1;
315 +       mutex_unlock(&chip->lock);
316 +
317 +       return ret;
318 +}
319 +
320 +static void gen_74x164_set_value(struct gpio_chip *gc,
321 +               unsigned offset, int val)
322 +{
323 +       struct gen_74x164_chip *chip = gpio_to_chip(gc);
324 +       bool refresh;
325 +
326 +       mutex_lock(&chip->lock);
327 +       if (val)
328 +               chip->port_config |= (1 << offset);
329 +       else
330 +               chip->port_config &= ~(1 << offset);
331 +
332 +       __gen_74x164_write_config(chip);
333 +       mutex_unlock(&chip->lock);
334 +}
335 +
336 +static int __devinit gen_74x164_probe(struct spi_device *spi)
337 +{
338 +       struct gen_74x164_chip *chip;
339 +       struct gen_74x164_chip_platform_data *pdata;
340 +       int ret;
341 +
342 +       pdata = spi->dev.platform_data;
343 +       if (!pdata || !pdata->base) {
344 +               dev_dbg(&spi->dev, "incorrect or missing platform data\n");
345 +               return -EINVAL;
346 +       }
347 +
348 +       /*
349 +        * bits_per_word cannot be configured in platform data
350 +        */
351 +       spi->bits_per_word = 8;
352 +
353 +       ret = spi_setup(spi);
354 +       if (ret < 0)
355 +               return ret;
356 +
357 +       chip = kzalloc(sizeof(*chip), GFP_KERNEL);
358 +       if (!chip)
359 +               return -ENOMEM;
360 +
361 +       mutex_init(&chip->lock);
362 +
363 +       dev_set_drvdata(&spi->dev, chip);
364 +
365 +       chip->spi = spi;
366 +
367 +       chip->gpio_chip.label = GEN_74X164_DRIVER_NAME,
368 +               chip->gpio_chip.direction_output = gen_74x164_direction_output;
369 +       chip->gpio_chip.get = gen_74x164_get_value;
370 +       chip->gpio_chip.set = gen_74x164_set_value;
371 +       chip->gpio_chip.base = pdata->base;
372 +       chip->gpio_chip.ngpio = GEN_74X164_GPIO_COUNT;
373 +       chip->gpio_chip.can_sleep = 1;
374 +       chip->gpio_chip.dev = &spi->dev;
375 +       chip->gpio_chip.owner = THIS_MODULE;
376 +
377 +       ret = __gen_74x164_write_config(chip);
378 +       if (ret) {
379 +               dev_err(&spi->dev, "Failed writing: %d\n", ret);
380 +               goto exit_destroy;
381 +       }
382 +
383 +       ret = gpiochip_add(&chip->gpio_chip);
384 +       if (ret)
385 +               goto exit_destroy;
386 +
387 +       return ret;
388 +
389 +exit_destroy:
390 +       dev_set_drvdata(&spi->dev, NULL);
391 +       mutex_destroy(&chip->lock);
392 +       kfree(chip);
393 +       return ret;
394 +}
395 +
396 +static int gen_74x164_remove(struct spi_device *spi)
397 +{
398 +       struct gen_74x164_chip *chip;
399 +       int ret;
400 +
401 +       chip = dev_get_drvdata(&spi->dev);
402 +       if (chip == NULL)
403 +               return -ENODEV;
404 +
405 +       dev_set_drvdata(&spi->dev, NULL);
406 +
407 +       ret = gpiochip_remove(&chip->gpio_chip);
408 +       if (!ret) {
409 +               mutex_destroy(&chip->lock);
410 +               kfree(chip);
411 +       } else
412 +               dev_err(&spi->dev, "Failed to remove the GPIO controller: %d\n",
413 +                               ret);
414 +
415 +       return ret;
416 +}
417 +
418 +static struct spi_driver gen_74x164_driver = {
419 +       .driver = {
420 +               .name           = GEN_74X164_DRIVER_NAME,
421 +               .owner          = THIS_MODULE,
422 +       },
423 +       .probe          = gen_74x164_probe,
424 +       .remove         = __devexit_p(gen_74x164_remove),
425 +};
426 +
427 +static int __init gen_74x164_init(void)
428 +{
429 +       return spi_register_driver(&gen_74x164_driver);
430 +}
431 +subsys_initcall(gen_74x164_init);
432 +
433 +static void __exit gen_74x164_exit(void)
434 +{
435 +       spi_unregister_driver(&gen_74x164_driver);
436 +}
437 +module_exit(gen_74x164_exit);
438 +
439 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
440 +MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
441 +MODULE_DESCRIPTION("GPIO expander driver for 74X164 8-bits shift register");
442 +MODULE_LICENSE("GPL v2");
443 +
444 --- /dev/null
445 +++ b/include/linux/spi/74x164.h
446 @@ -0,0 +1,11 @@
447 +#ifndef LINUX_SPI_74X164_H
448 +#define LINUX_SPI_74X164_H
449 +
450 +#define GEN_74X164_DRIVER_NAME "74x164"
451 +
452 +struct gen_74x164_chip_platform_data {
453 +       /* number assigned to the first GPIO */
454 +       unsigned        base;
455 +};
456 +
457 +#endif