update to 2.6.32.7
[openwrt.git] / target / linux / ar71xx / files / drivers / gpio / nxp_74hc153.c
1 /*
2 * NXP 74HC153 - Dual 4-input multiplexer GPIO driver
3 *
4 * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/gpio.h>
14 #include <linux/platform_device.h>
15 #include <linux/nxp_74hc153.h>
16
17 #define NXP_74HC153_NUM_GPIOS 8
18 #define NXP_74HC153_S0_MASK 0x1
19 #define NXP_74HC153_S1_MASK 0x2
20 #define NXP_74HC153_BANK_MASK 0x4
21
22 struct nxp_74hc153_chip {
23 struct device *parent;
24 struct gpio_chip gpio_chip;
25 struct mutex lock;
26 };
27
28 static struct nxp_74hc153_chip *gpio_to_nxp(struct gpio_chip *gc)
29 {
30 return container_of(gc, struct nxp_74hc153_chip, gpio_chip);
31 }
32
33 static int nxp_74hc153_direction_input(struct gpio_chip *gc, unsigned offset)
34 {
35 return 0;
36 }
37
38 static int nxp_74hc153_direction_output(struct gpio_chip *gc,
39 unsigned offset, int val)
40 {
41 return -EINVAL;
42 }
43
44 static int nxp_74hc153_get_value(struct gpio_chip *gc, unsigned offset)
45 {
46 struct nxp_74hc153_chip *nxp;
47 struct nxp_74hc153_platform_data *pdata;
48 unsigned s0;
49 unsigned s1;
50 unsigned pin;
51 int ret;
52
53 nxp = gpio_to_nxp(gc);
54 pdata = nxp->parent->platform_data;
55
56 s0 = !!(offset & NXP_74HC153_S0_MASK);
57 s1 = !!(offset & NXP_74HC153_S1_MASK);
58 pin = (offset & NXP_74HC153_BANK_MASK) ? pdata->gpio_pin_2y
59 : pdata->gpio_pin_1y;
60
61 mutex_lock(&nxp->lock);
62 gpio_set_value(pdata->gpio_pin_s0, s0);
63 gpio_set_value(pdata->gpio_pin_s1, s1);
64 ret = gpio_get_value(pin);
65 mutex_unlock(&nxp->lock);
66
67 return ret;
68 }
69
70 static void nxp_74hc153_set_value(struct gpio_chip *gc,
71 unsigned offset, int val)
72 {
73 /* not supported */
74 }
75
76 static int __devinit nxp_74hc153_probe(struct platform_device *pdev)
77 {
78 struct nxp_74hc153_platform_data *pdata;
79 struct nxp_74hc153_chip *nxp;
80 struct gpio_chip *gc;
81 int err;
82
83 pdata = pdev->dev.platform_data;
84 if (pdata == NULL) {
85 dev_dbg(&pdev->dev, "no platform data specified\n");
86 return -EINVAL;
87 }
88
89 nxp = kzalloc(sizeof(struct nxp_74hc153_chip), GFP_KERNEL);
90 if (nxp == NULL) {
91 dev_err(&pdev->dev, "no memory for private data\n");
92 return -ENOMEM;
93 }
94
95 err = gpio_request(pdata->gpio_pin_s0, dev_name(&pdev->dev));
96 if (err) {
97 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
98 pdata->gpio_pin_s0, err);
99 goto err_free_nxp;
100 }
101
102 err = gpio_request(pdata->gpio_pin_s1, dev_name(&pdev->dev));
103 if (err) {
104 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
105 pdata->gpio_pin_s1, err);
106 goto err_free_s0;
107 }
108
109 err = gpio_request(pdata->gpio_pin_1y, dev_name(&pdev->dev));
110 if (err) {
111 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
112 pdata->gpio_pin_1y, err);
113 goto err_free_s1;
114 }
115
116 err = gpio_request(pdata->gpio_pin_2y, dev_name(&pdev->dev));
117 if (err) {
118 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
119 pdata->gpio_pin_2y, err);
120 goto err_free_1y;
121 }
122
123 err = gpio_direction_output(pdata->gpio_pin_s0, 0);
124 if (err) {
125 dev_err(&pdev->dev,
126 "unable to set direction of gpio %u, err=%d\n",
127 pdata->gpio_pin_s0, err);
128 goto err_free_2y;
129 }
130
131 err = gpio_direction_output(pdata->gpio_pin_s1, 0);
132 if (err) {
133 dev_err(&pdev->dev,
134 "unable to set direction of gpio %u, err=%d\n",
135 pdata->gpio_pin_s1, err);
136 goto err_free_2y;
137 }
138
139 err = gpio_direction_input(pdata->gpio_pin_1y);
140 if (err) {
141 dev_err(&pdev->dev,
142 "unable to set direction of gpio %u, err=%d\n",
143 pdata->gpio_pin_1y, err);
144 goto err_free_2y;
145 }
146
147 err = gpio_direction_input(pdata->gpio_pin_2y);
148 if (err) {
149 dev_err(&pdev->dev,
150 "unable to set direction of gpio %u, err=%d\n",
151 pdata->gpio_pin_2y, err);
152 goto err_free_2y;
153 }
154
155 nxp->parent = &pdev->dev;
156 mutex_init(&nxp->lock);
157
158 gc = &nxp->gpio_chip;
159
160 gc->direction_input = nxp_74hc153_direction_input;
161 gc->direction_output = nxp_74hc153_direction_output;
162 gc->get = nxp_74hc153_get_value;
163 gc->set = nxp_74hc153_set_value;
164 gc->can_sleep = 1;
165
166 gc->base = pdata->gpio_base;
167 gc->ngpio = NXP_74HC153_NUM_GPIOS;
168 gc->label = dev_name(nxp->parent);
169 gc->dev = nxp->parent;
170 gc->owner = THIS_MODULE;
171
172 err = gpiochip_add(&nxp->gpio_chip);
173 if (err) {
174 dev_err(&pdev->dev, "unable to add gpio chip, err=%d\n", err);
175 goto err_free_2y;
176 }
177
178 platform_set_drvdata(pdev, nxp);
179 return 0;
180
181 err_free_2y:
182 gpio_free(pdata->gpio_pin_2y);
183 err_free_1y:
184 gpio_free(pdata->gpio_pin_1y);
185 err_free_s1:
186 gpio_free(pdata->gpio_pin_s1);
187 err_free_s0:
188 gpio_free(pdata->gpio_pin_s0);
189 err_free_nxp:
190 kfree(nxp);
191 return err;
192 }
193
194 static int nxp_74hc153_remove(struct platform_device *pdev)
195 {
196 struct nxp_74hc153_chip *nxp = platform_get_drvdata(pdev);
197 struct nxp_74hc153_platform_data *pdata = pdev->dev.platform_data;
198
199 if (nxp) {
200 int err;
201
202 err = gpiochip_remove(&nxp->gpio_chip);
203 if (err) {
204 dev_err(&pdev->dev,
205 "unable to remove gpio chip, err=%d\n",
206 err);
207 return err;
208 }
209
210 gpio_free(pdata->gpio_pin_2y);
211 gpio_free(pdata->gpio_pin_1y);
212 gpio_free(pdata->gpio_pin_s1);
213 gpio_free(pdata->gpio_pin_s0);
214
215 kfree(nxp);
216 platform_set_drvdata(pdev, NULL);
217 }
218
219 return 0;
220 }
221
222 static struct platform_driver nxp_74hc153_driver = {
223 .probe = nxp_74hc153_probe,
224 .remove = __devexit_p(nxp_74hc153_remove),
225 .driver = {
226 .name = NXP_74HC153_DRIVER_NAME,
227 .owner = THIS_MODULE,
228 },
229 };
230
231 static int __init nxp_74hc153_init(void)
232 {
233 return platform_driver_register(&nxp_74hc153_driver);
234 }
235 subsys_initcall(nxp_74hc153_init);
236
237 static void __exit nxp_74hc153_exit(void)
238 {
239 platform_driver_unregister(&nxp_74hc153_driver);
240 }
241 module_exit(nxp_74hc153_exit);
242
243 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
244 MODULE_DESCRIPTION("GPIO expander driver for NXP 74HC153");
245 MODULE_LICENSE("GPL v2");
246 MODULE_ALIAS("platform:" NXP_74HC153_DRIVER_NAME);
This page took 0.06531 seconds and 5 git commands to generate.