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/slab.h>
49 +#include <linux/platform_device.h>
50 +#include <linux/nxp_74hc164.h>
52 +#define NXP_74HC164_NUM_GPIOS 8
54 +struct nxp_74hc164_chip {
55 + struct device *parent;
56 + struct gpio_chip gpio_chip;
61 +static void nxp_74hc164_set_value(struct gpio_chip *, unsigned, int);
63 +static struct nxp_74hc164_chip *gpio_to_nxp(struct gpio_chip *gc)
65 + return container_of(gc, struct nxp_74hc164_chip, gpio_chip);
68 +static int nxp_74hc164_direction_input(struct gpio_chip *gc, unsigned offset)
74 +static int nxp_74hc164_direction_output(struct gpio_chip *gc,
75 + unsigned offset, int val)
77 + nxp_74hc164_set_value(gc, offset, val);
81 +static int nxp_74hc164_get_value(struct gpio_chip *gc, unsigned offset)
83 + struct nxp_74hc164_chip *nxp = gpio_to_nxp(gc);
86 + mutex_lock(&nxp->lock);
87 + ret = test_bit(offset, &nxp->mask);
88 + mutex_unlock(&nxp->lock);
93 +static void nxp_74hc164_set_value(struct gpio_chip *gc,
94 + unsigned offset, int val)
96 + struct nxp_74hc164_chip *nxp;
97 + struct nxp_74hc164_platform_data *pdata;
102 + nxp = gpio_to_nxp(gc);
103 + pdata = nxp->parent->platform_data;
105 + mutex_lock(&nxp->lock);
107 + refresh = (test_and_set_bit(offset, &nxp->mask) != val);
109 + refresh = (test_and_clear_bit(offset, &nxp->mask) != val);
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);
119 + mutex_unlock(&nxp->lock);
122 +static int __devinit nxp_74hc164_probe(struct platform_device *pdev)
124 + struct nxp_74hc164_platform_data *pdata;
125 + struct nxp_74hc164_chip *nxp;
126 + struct gpio_chip *gc;
129 + pdata = pdev->dev.platform_data;
130 + if (pdata == NULL) {
131 + dev_dbg(&pdev->dev, "no platform data specified\n");
135 + nxp = kzalloc(sizeof(struct nxp_74hc164_chip), GFP_KERNEL);
137 + dev_err(&pdev->dev, "no memory for private data\n");
141 + err = gpio_request(pdata->gpio_pin_clk, dev_name(&pdev->dev));
143 + dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
144 + pdata->gpio_pin_clk, err);
148 + err = gpio_request(pdata->gpio_pin_data, dev_name(&pdev->dev));
150 + dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
151 + pdata->gpio_pin_data, err);
155 + err = gpio_direction_output(pdata->gpio_pin_clk, 0);
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;
163 + err = gpio_direction_output(pdata->gpio_pin_data, 0);
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;
171 + nxp->parent = &pdev->dev;
172 + mutex_init(&nxp->lock);
174 + gc = &nxp->gpio_chip;
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;
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;
188 + err = gpiochip_add(&nxp->gpio_chip);
190 + dev_err(&pdev->dev, "unable to add gpio chip, err=%d\n", err);
191 + goto err_free_data;
194 + platform_set_drvdata(pdev, nxp);
198 + gpio_free(pdata->gpio_pin_data);
200 + gpio_free(pdata->gpio_pin_clk);
206 +static int nxp_74hc164_remove(struct platform_device *pdev)
208 + struct nxp_74hc164_chip *nxp = platform_get_drvdata(pdev);
209 + struct nxp_74hc164_platform_data *pdata = pdev->dev.platform_data;
214 + err = gpiochip_remove(&nxp->gpio_chip);
216 + dev_err(&pdev->dev,
217 + "unable to remove gpio chip, err=%d\n",
222 + gpio_free(pdata->gpio_pin_clk);
223 + gpio_free(pdata->gpio_pin_data);
226 + platform_set_drvdata(pdev, NULL);
232 +static struct platform_driver nxp_74hc164_driver = {
233 + .probe = nxp_74hc164_probe,
234 + .remove = __devexit_p(nxp_74hc164_remove),
236 + .name = NXP_74HC164_DRIVER_NAME,
237 + .owner = THIS_MODULE,
241 +static int __init nxp_74hc164_init(void)
243 + return platform_driver_register(&nxp_74hc164_driver);
245 +subsys_initcall(nxp_74hc164_init);
247 +static void __exit nxp_74hc164_exit(void)
249 + platform_driver_unregister(&nxp_74hc164_driver);
251 +module_exit(nxp_74hc164_exit);
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);
259 +++ b/drivers/gpio/74x164.c
262 + * 74Hx164 - Generic serial-in/parallel-out 8-bits shift register GPIO driver
264 + * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
265 + * Copyright (C) 2010 Miguel Gaio <miguel.gaio@efixo.com>
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.
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>
279 +#define GEN_74X164_GPIO_COUNT 8
282 +struct gen_74x164_chip {
283 + struct spi_device *spi;
284 + struct gpio_chip gpio_chip;
289 +static void gen_74x164_set_value(struct gpio_chip *, unsigned, int);
291 +static struct gen_74x164_chip *gpio_to_chip(struct gpio_chip *gc)
293 + return container_of(gc, struct gen_74x164_chip, gpio_chip);
296 +static int __gen_74x164_write_config(struct gen_74x164_chip *chip)
298 + return spi_write(chip->spi,
299 + &chip->port_config, sizeof(chip->port_config));
302 +static int gen_74x164_direction_output(struct gpio_chip *gc,
303 + unsigned offset, int val)
305 + gen_74x164_set_value(gc, offset, val);
309 +static int gen_74x164_get_value(struct gpio_chip *gc, unsigned offset)
311 + struct gen_74x164_chip *chip = gpio_to_chip(gc);
314 + mutex_lock(&chip->lock);
315 + ret = (chip->port_config >> offset) & 0x1;
316 + mutex_unlock(&chip->lock);
321 +static void gen_74x164_set_value(struct gpio_chip *gc,
322 + unsigned offset, int val)
324 + struct gen_74x164_chip *chip = gpio_to_chip(gc);
327 + mutex_lock(&chip->lock);
329 + chip->port_config |= (1 << offset);
331 + chip->port_config &= ~(1 << offset);
333 + __gen_74x164_write_config(chip);
334 + mutex_unlock(&chip->lock);
337 +static int __devinit gen_74x164_probe(struct spi_device *spi)
339 + struct gen_74x164_chip *chip;
340 + struct gen_74x164_chip_platform_data *pdata;
343 + pdata = spi->dev.platform_data;
344 + if (!pdata || !pdata->base) {
345 + dev_dbg(&spi->dev, "incorrect or missing platform data\n");
350 + * bits_per_word cannot be configured in platform data
352 + spi->bits_per_word = 8;
354 + ret = spi_setup(spi);
358 + chip = kzalloc(sizeof(*chip), GFP_KERNEL);
362 + mutex_init(&chip->lock);
364 + dev_set_drvdata(&spi->dev, chip);
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;
378 + ret = __gen_74x164_write_config(chip);
380 + dev_err(&spi->dev, "Failed writing: %d\n", ret);
384 + ret = gpiochip_add(&chip->gpio_chip);
391 + dev_set_drvdata(&spi->dev, NULL);
392 + mutex_destroy(&chip->lock);
397 +static int gen_74x164_remove(struct spi_device *spi)
399 + struct gen_74x164_chip *chip;
402 + chip = dev_get_drvdata(&spi->dev);
406 + dev_set_drvdata(&spi->dev, NULL);
408 + ret = gpiochip_remove(&chip->gpio_chip);
410 + mutex_destroy(&chip->lock);
413 + dev_err(&spi->dev, "Failed to remove the GPIO controller: %d\n",
419 +static struct spi_driver gen_74x164_driver = {
421 + .name = GEN_74X164_DRIVER_NAME,
422 + .owner = THIS_MODULE,
424 + .probe = gen_74x164_probe,
425 + .remove = __devexit_p(gen_74x164_remove),
428 +static int __init gen_74x164_init(void)
430 + return spi_register_driver(&gen_74x164_driver);
432 +subsys_initcall(gen_74x164_init);
434 +static void __exit gen_74x164_exit(void)
436 + spi_unregister_driver(&gen_74x164_driver);
438 +module_exit(gen_74x164_exit);
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");
446 +++ b/include/linux/spi/74x164.h
448 +#ifndef LINUX_SPI_74X164_H
449 +#define LINUX_SPI_74X164_H
451 +#define GEN_74X164_DRIVER_NAME "74x164"
453 +struct gen_74x164_chip_platform_data {
454 + /* number assigned to the first GPIO */