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.
8 + tristate "74x164 serial-in/parallel-out 8-bits shift register"
9 + depends on SPI_MASTER
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.
15 comment "AC97 GPIO expanders:"
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
29 +++ b/drivers/gpio/nxp_74hc164.c
32 + * NXP 74HC164 - output expander GPIO driver
34 + * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
35 + * Copyright (C) 2010 Miguel Gaio <miguel.gaio@efixo.com>
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.
41 + * Copy from nxp_74hc153.c code
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>
51 +#define NXP_74HC164_NUM_GPIOS 8
53 +struct nxp_74hc164_chip {
54 + struct device *parent;
55 + struct gpio_chip gpio_chip;
60 +static void nxp_74hc164_set_value(struct gpio_chip *, unsigned, int);
62 +static struct nxp_74hc164_chip *gpio_to_nxp(struct gpio_chip *gc)
64 + return container_of(gc, struct nxp_74hc164_chip, gpio_chip);
67 +static int nxp_74hc164_direction_input(struct gpio_chip *gc, unsigned offset)
73 +static int nxp_74hc164_direction_output(struct gpio_chip *gc,
74 + unsigned offset, int val)
76 + nxp_74hc164_set_value(gc, offset, val);
80 +static int nxp_74hc164_get_value(struct gpio_chip *gc, unsigned offset)
82 + struct nxp_74hc164_chip *nxp = gpio_to_nxp(gc);
85 + mutex_lock(&nxp->lock);
86 + ret = test_bit(offset, &nxp->mask);
87 + mutex_unlock(&nxp->lock);
92 +static void nxp_74hc164_set_value(struct gpio_chip *gc,
93 + unsigned offset, int val)
95 + struct nxp_74hc164_chip *nxp;
96 + struct nxp_74hc164_platform_data *pdata;
101 + nxp = gpio_to_nxp(gc);
102 + pdata = nxp->parent->platform_data;
104 + mutex_lock(&nxp->lock);
106 + refresh = (test_and_set_bit(offset, &nxp->mask) != val);
108 + refresh = (test_and_clear_bit(offset, &nxp->mask) != val);
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);
118 + mutex_unlock(&nxp->lock);
121 +static int __devinit nxp_74hc164_probe(struct platform_device *pdev)
123 + struct nxp_74hc164_platform_data *pdata;
124 + struct nxp_74hc164_chip *nxp;
125 + struct gpio_chip *gc;
128 + pdata = pdev->dev.platform_data;
129 + if (pdata == NULL) {
130 + dev_dbg(&pdev->dev, "no platform data specified\n");
134 + nxp = kzalloc(sizeof(struct nxp_74hc164_chip), GFP_KERNEL);
136 + dev_err(&pdev->dev, "no memory for private data\n");
140 + err = gpio_request(pdata->gpio_pin_clk, dev_name(&pdev->dev));
142 + dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
143 + pdata->gpio_pin_clk, err);
147 + err = gpio_request(pdata->gpio_pin_data, dev_name(&pdev->dev));
149 + dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
150 + pdata->gpio_pin_data, err);
154 + err = gpio_direction_output(pdata->gpio_pin_clk, 0);
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;
162 + err = gpio_direction_output(pdata->gpio_pin_data, 0);
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;
170 + nxp->parent = &pdev->dev;
171 + mutex_init(&nxp->lock);
173 + gc = &nxp->gpio_chip;
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;
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;
187 + err = gpiochip_add(&nxp->gpio_chip);
189 + dev_err(&pdev->dev, "unable to add gpio chip, err=%d\n", err);
190 + goto err_free_data;
193 + platform_set_drvdata(pdev, nxp);
197 + gpio_free(pdata->gpio_pin_data);
199 + gpio_free(pdata->gpio_pin_clk);
205 +static int nxp_74hc164_remove(struct platform_device *pdev)
207 + struct nxp_74hc164_chip *nxp = platform_get_drvdata(pdev);
208 + struct nxp_74hc164_platform_data *pdata = pdev->dev.platform_data;
213 + err = gpiochip_remove(&nxp->gpio_chip);
215 + dev_err(&pdev->dev,
216 + "unable to remove gpio chip, err=%d\n",
221 + gpio_free(pdata->gpio_pin_clk);
222 + gpio_free(pdata->gpio_pin_data);
225 + platform_set_drvdata(pdev, NULL);
231 +static struct platform_driver nxp_74hc164_driver = {
232 + .probe = nxp_74hc164_probe,
233 + .remove = __devexit_p(nxp_74hc164_remove),
235 + .name = NXP_74HC164_DRIVER_NAME,
236 + .owner = THIS_MODULE,
240 +static int __init nxp_74hc164_init(void)
242 + return platform_driver_register(&nxp_74hc164_driver);
244 +subsys_initcall(nxp_74hc164_init);
246 +static void __exit nxp_74hc164_exit(void)
248 + platform_driver_unregister(&nxp_74hc164_driver);
250 +module_exit(nxp_74hc164_exit);
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);
258 +++ b/drivers/gpio/74x164.c
261 + * 74Hx164 - Generic serial-in/parallel-out 8-bits shift register GPIO driver
263 + * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
264 + * Copyright (C) 2010 Miguel Gaio <miguel.gaio@efixo.com>
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.
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>
278 +#define GEN_74X164_GPIO_COUNT 8
281 +struct gen_74x164_chip {
282 + struct spi_device *spi;
283 + struct gpio_chip gpio_chip;
288 +static void gen_74x164_set_value(struct gpio_chip *, unsigned, int);
290 +static struct gen_74x164_chip *gpio_to_chip(struct gpio_chip *gc)
292 + return container_of(gc, struct gen_74x164_chip, gpio_chip);
295 +static int __gen_74x164_write_config(struct gen_74x164_chip *chip)
297 + return spi_write(chip->spi,
298 + &chip->port_config, sizeof(chip->port_config));
301 +static int gen_74x164_direction_output(struct gpio_chip *gc,
302 + unsigned offset, int val)
304 + gen_74x164_set_value(gc, offset, val);
308 +static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset)
310 + struct gen_74x164_chip *chip = gpio_to_chip(gc);
313 + mutex_lock(&chip->lock);
314 + ret = (chip->port_config >> offset) & 0x1;
315 + mutex_unlock(&chip->lock);
320 +static void gen_74x164_set_value(struct gpio_chip *gc,
321 + unsigned offset, int val)
323 + struct gen_74x164_chip *chip = gpio_to_chip(gc);
326 + mutex_lock(&chip->lock);
328 + chip->port_config |= (1 << offset);
330 + chip->port_config &= ~(1 << offset);
332 + __gen_74x164_write_config(chip);
333 + mutex_unlock(&chip->lock);
336 +static int __devinit gen_74x164_probe(struct spi_device *spi)
338 + struct gen_74x164_chip *chip;
339 + struct gen_74x164_chip_platform_data *pdata;
342 + pdata = spi->dev.platform_data;
343 + if (!pdata || !pdata->base) {
344 + dev_dbg(&spi->dev, "incorrect or missing platform data\n");
349 + * bits_per_word cannot be configured in platform data
351 + spi->bits_per_word = 8;
353 + ret = spi_setup(spi);
357 + chip = kzalloc(sizeof(*chip), GFP_KERNEL);
361 + mutex_init(&chip->lock);
363 + dev_set_drvdata(&spi->dev, chip);
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;
377 + ret = __gen_74x164_write_config(chip);
379 + dev_err(&spi->dev, "Failed writing: %d\n", ret);
383 + ret = gpiochip_add(&chip->gpio_chip);
390 + dev_set_drvdata(&spi->dev, NULL);
391 + mutex_destroy(&chip->lock);
396 +static int gen_74x164_remove(struct spi_device *spi)
398 + struct gen_74x164_chip *chip;
401 + chip = dev_get_drvdata(&spi->dev);
405 + dev_set_drvdata(&spi->dev, NULL);
407 + ret = gpiochip_remove(&chip->gpio_chip);
409 + mutex_destroy(&chip->lock);
412 + dev_err(&spi->dev, "Failed to remove the GPIO controller: %d\n",
418 +static struct spi_driver gen_74x164_driver = {
420 + .name = GEN_74X164_DRIVER_NAME,
421 + .owner = THIS_MODULE,
423 + .probe = gen_74x164_probe,
424 + .remove = __devexit_p(gen_74x164_remove),
427 +static int __init gen_74x164_init(void)
429 + return spi_register_driver(&gen_74x164_driver);
431 +subsys_initcall(gen_74x164_init);
433 +static void __exit gen_74x164_exit(void)
435 + spi_unregister_driver(&gen_74x164_driver);
437 +module_exit(gen_74x164_exit);
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");
445 +++ b/include/linux/spi/74x164.h
447 +#ifndef LINUX_SPI_74X164_H
448 +#define LINUX_SPI_74X164_H
450 +#define GEN_74X164_DRIVER_NAME "74x164"
452 +struct gen_74x164_chip_platform_data {
453 + /* number assigned to the first GPIO */