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