2 * NXP 74HC153 - Dual 4-input multiplexer GPIO driver
4 * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
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.
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>
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
22 struct nxp_74hc153_chip {
23 struct device *parent;
24 struct gpio_chip gpio_chip;
28 static struct nxp_74hc153_chip *gpio_to_nxp(struct gpio_chip *gc)
30 return container_of(gc, struct nxp_74hc153_chip, gpio_chip);
33 static int nxp_74hc153_direction_input(struct gpio_chip *gc, unsigned offset)
38 static int nxp_74hc153_direction_output(struct gpio_chip *gc,
39 unsigned offset, int val)
44 static int nxp_74hc153_get_value(struct gpio_chip *gc, unsigned offset)
46 struct nxp_74hc153_chip *nxp;
47 struct nxp_74hc153_platform_data *pdata;
53 nxp = gpio_to_nxp(gc);
54 pdata = nxp->parent->platform_data;
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
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);
70 static void nxp_74hc153_set_value(struct gpio_chip *gc,
71 unsigned offset, int val)
76 static int __devinit nxp_74hc153_probe(struct platform_device *pdev)
78 struct nxp_74hc153_platform_data *pdata;
79 struct nxp_74hc153_chip *nxp;
83 pdata = pdev->dev.platform_data;
85 dev_dbg(&pdev->dev, "no platform data specified\n");
89 nxp = kzalloc(sizeof(struct nxp_74hc153_chip), GFP_KERNEL);
91 dev_err(&pdev->dev, "no memory for private data\n");
95 err = gpio_request(pdata->gpio_pin_s0, dev_name(&pdev->dev));
97 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
98 pdata->gpio_pin_s0, err);
102 err = gpio_request(pdata->gpio_pin_s1, dev_name(&pdev->dev));
104 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
105 pdata->gpio_pin_s1, err);
109 err = gpio_request(pdata->gpio_pin_1y, dev_name(&pdev->dev));
111 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
112 pdata->gpio_pin_1y, err);
116 err = gpio_request(pdata->gpio_pin_2y, dev_name(&pdev->dev));
118 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
119 pdata->gpio_pin_2y, err);
123 err = gpio_direction_output(pdata->gpio_pin_s0, 0);
126 "unable to set direction of gpio %u, err=%d\n",
127 pdata->gpio_pin_s0, err);
131 err = gpio_direction_output(pdata->gpio_pin_s1, 0);
134 "unable to set direction of gpio %u, err=%d\n",
135 pdata->gpio_pin_s1, err);
139 err = gpio_direction_input(pdata->gpio_pin_1y);
142 "unable to set direction of gpio %u, err=%d\n",
143 pdata->gpio_pin_1y, err);
147 err = gpio_direction_input(pdata->gpio_pin_2y);
150 "unable to set direction of gpio %u, err=%d\n",
151 pdata->gpio_pin_2y, err);
155 nxp->parent = &pdev->dev;
156 mutex_init(&nxp->lock);
158 gc = &nxp->gpio_chip;
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;
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;
172 err = gpiochip_add(&nxp->gpio_chip);
174 dev_err(&pdev->dev, "unable to add gpio chip, err=%d\n", err);
178 platform_set_drvdata(pdev, nxp);
182 gpio_free(pdata->gpio_pin_2y);
184 gpio_free(pdata->gpio_pin_1y);
186 gpio_free(pdata->gpio_pin_s1);
188 gpio_free(pdata->gpio_pin_s0);
194 static int nxp_74hc153_remove(struct platform_device *pdev)
196 struct nxp_74hc153_chip *nxp = platform_get_drvdata(pdev);
197 struct nxp_74hc153_platform_data *pdata = pdev->dev.platform_data;
202 err = gpiochip_remove(&nxp->gpio_chip);
205 "unable to remove gpio chip, err=%d\n",
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);
216 platform_set_drvdata(pdev, NULL);
222 static struct platform_driver nxp_74hc153_driver = {
223 .probe = nxp_74hc153_probe,
224 .remove = __devexit_p(nxp_74hc153_remove),
226 .name = NXP_74HC153_DRIVER_NAME,
227 .owner = THIS_MODULE,
231 static int __init nxp_74hc153_init(void)
233 return platform_driver_register(&nxp_74hc153_driver);
235 subsys_initcall(nxp_74hc153_init);
237 static void __exit nxp_74hc153_exit(void)
239 platform_driver_unregister(&nxp_74hc153_driver);
241 module_exit(nxp_74hc153_exit);
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);