8e50ee740dc702a723f8efb7b67f18428ccb889d
[openwrt/staging/yousong.git] / target / linux / brcm63xx / patches-2.6.32 / 211-nxp-74hc164-gpio-chip-driver.patch
1 --- a/drivers/gpio/Kconfig
2 +++ b/drivers/gpio/Kconfig
3 @@ -230,4 +230,12 @@ config GPIO_UCB1400
4 To compile this driver as a module, choose M here: the
5 module will be called ucb1400_gpio.
6
7 +comment "Other GPIO expanders"
8 +
9 +config GPIO_NXP_74HC164
10 + tristate "NXP 74HC164 Output expanders"
11 + help
12 + Platform driver for NXP 74HC164 8-output Expanders. This
13 + provides a GPIO interface supporting outputs.
14 +
15 endif
16 --- a/drivers/gpio/Makefile
17 +++ b/drivers/gpio/Makefile
18 @@ -10,6 +10,7 @@ obj-$(CONFIG_GPIO_MAX7301) += max7301.o
19 obj-$(CONFIG_GPIO_MAX732X) += max732x.o
20 obj-$(CONFIG_GPIO_MC33880) += mc33880.o
21 obj-$(CONFIG_GPIO_MCP23S08) += mcp23s08.o
22 +obj-$(CONFIG_GPIO_NXP_74HC164) += nxp_74hc164.o
23 obj-$(CONFIG_GPIO_PCA953X) += pca953x.o
24 obj-$(CONFIG_GPIO_PCF857X) += pcf857x.o
25 obj-$(CONFIG_GPIO_PL061) += pl061.o
26 --- /dev/null
27 +++ b/drivers/gpio/nxp_74hc164.c
28 @@ -0,0 +1,226 @@
29 +/*
30 + * NXP 74HC164 - output expander GPIO driver
31 + *
32 + * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
33 + * Copyright (C) 2010 Miguel Gaio <miguel.gaio@efixo.com>
34 + *
35 + * This program is free software; you can redistribute it and/or modify
36 + * it under the terms of the GNU General Public License version 2 as
37 + * published by the Free Software Foundation.
38 + *
39 + * Copy from nxp_74hc153.c code
40 + */
41 +
42 +#include <linux/module.h>
43 +#include <linux/init.h>
44 +#include <linux/gpio.h>
45 +#include <linux/bitops.h>
46 +#include <linux/platform_device.h>
47 +#include <linux/nxp_74hc164.h>
48 +
49 +#define NXP_74HC164_NUM_GPIOS 8
50 +
51 +struct nxp_74hc164_chip {
52 + struct device *parent;
53 + struct gpio_chip gpio_chip;
54 + struct mutex lock;
55 + long mask;
56 +};
57 +
58 +static void nxp_74hc164_set_value(struct gpio_chip *, unsigned, int);
59 +
60 +static struct nxp_74hc164_chip *gpio_to_nxp(struct gpio_chip *gc)
61 +{
62 + return container_of(gc, struct nxp_74hc164_chip, gpio_chip);
63 +}
64 +
65 +static int nxp_74hc164_direction_input(struct gpio_chip *gc, unsigned offset)
66 +{
67 + WARN_ON(1);
68 + return -EINVAL;
69 +}
70 +
71 +static int nxp_74hc164_direction_output(struct gpio_chip *gc,
72 + unsigned offset, int val)
73 +{
74 + nxp_74hc164_set_value(gc, offset, val);
75 + return 0;
76 +}
77 +
78 +static int nxp_74hc164_get_value(struct gpio_chip *gc, unsigned offset)
79 +{
80 + struct nxp_74hc164_chip *nxp = gpio_to_nxp(gc);
81 + int ret;
82 +
83 + mutex_lock(&nxp->lock);
84 + ret = test_bit(offset, &nxp->mask);
85 + mutex_unlock(&nxp->lock);
86 +
87 + return ret;
88 +}
89 +
90 +static void nxp_74hc164_set_value(struct gpio_chip *gc,
91 + unsigned offset, int val)
92 +{
93 + struct nxp_74hc164_chip *nxp;
94 + struct nxp_74hc164_platform_data *pdata;
95 + long mask;
96 + int refresh;
97 + int i;
98 +
99 + nxp = gpio_to_nxp(gc);
100 + pdata = nxp->parent->platform_data;
101 +
102 + mutex_lock(&nxp->lock);
103 + if (val)
104 + refresh = (test_and_set_bit(offset, &nxp->mask) != val);
105 + else
106 + refresh = (test_and_clear_bit(offset, &nxp->mask) != val);
107 +
108 + if (refresh) {
109 + mask = nxp->mask;
110 + for (i = 8; i > 0; --i, mask <<= 1) {
111 + gpio_set_value(pdata->gpio_pin_data, mask & 0x80);
112 + gpio_set_value(pdata->gpio_pin_clk, 1);
113 + gpio_set_value(pdata->gpio_pin_clk, 0);
114 + }
115 + }
116 + mutex_unlock(&nxp->lock);
117 +}
118 +
119 +static int __devinit nxp_74hc164_probe(struct platform_device *pdev)
120 +{
121 + struct nxp_74hc164_platform_data *pdata;
122 + struct nxp_74hc164_chip *nxp;
123 + struct gpio_chip *gc;
124 + int err;
125 +
126 + pdata = pdev->dev.platform_data;
127 + if (pdata == NULL) {
128 + dev_dbg(&pdev->dev, "no platform data specified\n");
129 + return -EINVAL;
130 + }
131 +
132 + nxp = kzalloc(sizeof(struct nxp_74hc164_chip), GFP_KERNEL);
133 + if (nxp == NULL) {
134 + dev_err(&pdev->dev, "no memory for private data\n");
135 + return -ENOMEM;
136 + }
137 +
138 + err = gpio_request(pdata->gpio_pin_clk, dev_name(&pdev->dev));
139 + if (err) {
140 + dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
141 + pdata->gpio_pin_clk, err);
142 + goto err_free_nxp;
143 + }
144 +
145 + err = gpio_request(pdata->gpio_pin_data, dev_name(&pdev->dev));
146 + if (err) {
147 + dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
148 + pdata->gpio_pin_data, err);
149 + goto err_free_clk;
150 + }
151 +
152 + err = gpio_direction_output(pdata->gpio_pin_clk, 0);
153 + if (err) {
154 + dev_err(&pdev->dev,
155 + "unable to set direction of gpio %u, err=%d\n",
156 + pdata->gpio_pin_clk, err);
157 + goto err_free_data;
158 + }
159 +
160 + err = gpio_direction_output(pdata->gpio_pin_data, 0);
161 + if (err) {
162 + dev_err(&pdev->dev,
163 + "unable to set direction of gpio %u, err=%d\n",
164 + pdata->gpio_pin_data, err);
165 + goto err_free_data;
166 + }
167 +
168 + nxp->parent = &pdev->dev;
169 + mutex_init(&nxp->lock);
170 +
171 + gc = &nxp->gpio_chip;
172 +
173 + gc->direction_input = nxp_74hc164_direction_input;
174 + gc->direction_output = nxp_74hc164_direction_output;
175 + gc->get = nxp_74hc164_get_value;
176 + gc->set = nxp_74hc164_set_value;
177 + gc->can_sleep = 1;
178 +
179 + gc->base = pdata->gpio_base;
180 + gc->ngpio = NXP_74HC164_NUM_GPIOS;
181 + gc->label = dev_name(nxp->parent);
182 + gc->dev = nxp->parent;
183 + gc->owner = THIS_MODULE;
184 +
185 + err = gpiochip_add(&nxp->gpio_chip);
186 + if (err) {
187 + dev_err(&pdev->dev, "unable to add gpio chip, err=%d\n", err);
188 + goto err_free_data;
189 + }
190 +
191 + platform_set_drvdata(pdev, nxp);
192 + return 0;
193 +
194 + err_free_data:
195 + gpio_free(pdata->gpio_pin_data);
196 + err_free_clk:
197 + gpio_free(pdata->gpio_pin_clk);
198 + err_free_nxp:
199 + kfree(nxp);
200 + return err;
201 +}
202 +
203 +static int nxp_74hc164_remove(struct platform_device *pdev)
204 +{
205 + struct nxp_74hc164_chip *nxp = platform_get_drvdata(pdev);
206 + struct nxp_74hc164_platform_data *pdata = pdev->dev.platform_data;
207 +
208 + if (nxp) {
209 + int err;
210 +
211 + err = gpiochip_remove(&nxp->gpio_chip);
212 + if (err) {
213 + dev_err(&pdev->dev,
214 + "unable to remove gpio chip, err=%d\n",
215 + err);
216 + return err;
217 + }
218 +
219 + gpio_free(pdata->gpio_pin_clk);
220 + gpio_free(pdata->gpio_pin_data);
221 +
222 + kfree(nxp);
223 + platform_set_drvdata(pdev, NULL);
224 + }
225 +
226 + return 0;
227 +}
228 +
229 +static struct platform_driver nxp_74hc164_driver = {
230 + .probe = nxp_74hc164_probe,
231 + .remove = __devexit_p(nxp_74hc164_remove),
232 + .driver = {
233 + .name = NXP_74HC164_DRIVER_NAME,
234 + .owner = THIS_MODULE,
235 + },
236 +};
237 +
238 +static int __init nxp_74hc164_init(void)
239 +{
240 + return platform_driver_register(&nxp_74hc164_driver);
241 +}
242 +subsys_initcall(nxp_74hc164_init);
243 +
244 +static void __exit nxp_74hc164_exit(void)
245 +{
246 + platform_driver_unregister(&nxp_74hc164_driver);
247 +}
248 +module_exit(nxp_74hc164_exit);
249 +
250 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
251 +MODULE_AUTHOR("Miguel Gaio <miguel.gaio@efixo.com>");
252 +MODULE_DESCRIPTION("GPIO expander driver for NXP 74HC164");
253 +MODULE_LICENSE("GPL v2");
254 +MODULE_ALIAS("platform:" NXP_74HC164_DRIVER_NAME);
255 --- /dev/null
256 +++ b/include/linux/nxp_74hc164.h
257 @@ -0,0 +1,22 @@
258 +/*
259 + * NXP 74HC164 - Dual 4-input multiplexer defines
260 + *
261 + * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
262 + *
263 + * This program is free software; you can redistribute it and/or modify
264 + * it under the terms of the GNU General Public License version 2 as
265 + * published by the Free Software Foundation.
266 + */
267 +
268 +#ifndef _NXP_74HC164_H
269 +#define _NXP_74HC164_H
270 +
271 +#define NXP_74HC164_DRIVER_NAME "nxp-74hc164"
272 +
273 +struct nxp_74hc164_platform_data {
274 + unsigned gpio_base;
275 + unsigned gpio_pin_data;
276 + unsigned gpio_pin_clk;
277 +};
278 +
279 +#endif /* _NXP_74HC164_H */