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/slab.h>
15 #include <linux/platform_device.h>
16 #include <linux/nxp_74hc153.h>
18 #define NXP_74HC153_NUM_GPIOS 8
19 #define NXP_74HC153_S0_MASK 0x1
20 #define NXP_74HC153_S1_MASK 0x2
21 #define NXP_74HC153_BANK_MASK 0x4
23 struct nxp_74hc153_chip {
24 struct device *parent;
25 struct gpio_chip gpio_chip;
29 static struct nxp_74hc153_chip *gpio_to_nxp(struct gpio_chip *gc)
31 return container_of(gc, struct nxp_74hc153_chip, gpio_chip);
34 static int nxp_74hc153_direction_input(struct gpio_chip *gc, unsigned offset)
39 static int nxp_74hc153_direction_output(struct gpio_chip *gc,
40 unsigned offset, int val)
45 static int nxp_74hc153_get_value(struct gpio_chip *gc, unsigned offset)
47 struct nxp_74hc153_chip *nxp;
48 struct nxp_74hc153_platform_data *pdata;
54 nxp = gpio_to_nxp(gc);
55 pdata = nxp->parent->platform_data;
57 s0 = !!(offset & NXP_74HC153_S0_MASK);
58 s1 = !!(offset & NXP_74HC153_S1_MASK);
59 pin = (offset & NXP_74HC153_BANK_MASK) ? pdata->gpio_pin_2y
62 mutex_lock(&nxp->lock);
63 gpio_set_value(pdata->gpio_pin_s0, s0);
64 gpio_set_value(pdata->gpio_pin_s1, s1);
65 ret = gpio_get_value(pin);
66 mutex_unlock(&nxp->lock);
71 static void nxp_74hc153_set_value(struct gpio_chip *gc,
72 unsigned offset, int val)
77 static int __devinit nxp_74hc153_probe(struct platform_device *pdev)
79 struct nxp_74hc153_platform_data *pdata;
80 struct nxp_74hc153_chip *nxp;
84 pdata = pdev->dev.platform_data;
86 dev_dbg(&pdev->dev, "no platform data specified\n");
90 nxp = kzalloc(sizeof(struct nxp_74hc153_chip), GFP_KERNEL);
92 dev_err(&pdev->dev, "no memory for private data\n");
96 err = gpio_request(pdata->gpio_pin_s0, dev_name(&pdev->dev));
98 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
99 pdata->gpio_pin_s0, err);
103 err = gpio_request(pdata->gpio_pin_s1, dev_name(&pdev->dev));
105 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
106 pdata->gpio_pin_s1, err);
110 err = gpio_request(pdata->gpio_pin_1y, dev_name(&pdev->dev));
112 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
113 pdata->gpio_pin_1y, err);
117 err = gpio_request(pdata->gpio_pin_2y, dev_name(&pdev->dev));
119 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
120 pdata->gpio_pin_2y, err);
124 err = gpio_direction_output(pdata->gpio_pin_s0, 0);
127 "unable to set direction of gpio %u, err=%d\n",
128 pdata->gpio_pin_s0, err);
132 err = gpio_direction_output(pdata->gpio_pin_s1, 0);
135 "unable to set direction of gpio %u, err=%d\n",
136 pdata->gpio_pin_s1, err);
140 err = gpio_direction_input(pdata->gpio_pin_1y);
143 "unable to set direction of gpio %u, err=%d\n",
144 pdata->gpio_pin_1y, err);
148 err = gpio_direction_input(pdata->gpio_pin_2y);
151 "unable to set direction of gpio %u, err=%d\n",
152 pdata->gpio_pin_2y, err);
156 nxp->parent = &pdev->dev;
157 mutex_init(&nxp->lock);
159 gc = &nxp->gpio_chip;
161 gc->direction_input = nxp_74hc153_direction_input;
162 gc->direction_output = nxp_74hc153_direction_output;
163 gc->get = nxp_74hc153_get_value;
164 gc->set = nxp_74hc153_set_value;
167 gc->base = pdata->gpio_base;
168 gc->ngpio = NXP_74HC153_NUM_GPIOS;
169 gc->label = dev_name(nxp->parent);
170 gc->dev = nxp->parent;
171 gc->owner = THIS_MODULE;
173 err = gpiochip_add(&nxp->gpio_chip);
175 dev_err(&pdev->dev, "unable to add gpio chip, err=%d\n", err);
179 platform_set_drvdata(pdev, nxp);
183 gpio_free(pdata->gpio_pin_2y);
185 gpio_free(pdata->gpio_pin_1y);
187 gpio_free(pdata->gpio_pin_s1);
189 gpio_free(pdata->gpio_pin_s0);
195 static int nxp_74hc153_remove(struct platform_device *pdev)
197 struct nxp_74hc153_chip *nxp = platform_get_drvdata(pdev);
198 struct nxp_74hc153_platform_data *pdata = pdev->dev.platform_data;
203 err = gpiochip_remove(&nxp->gpio_chip);
206 "unable to remove gpio chip, err=%d\n",
211 gpio_free(pdata->gpio_pin_2y);
212 gpio_free(pdata->gpio_pin_1y);
213 gpio_free(pdata->gpio_pin_s1);
214 gpio_free(pdata->gpio_pin_s0);
217 platform_set_drvdata(pdev, NULL);
223 static struct platform_driver nxp_74hc153_driver = {
224 .probe = nxp_74hc153_probe,
225 .remove = __devexit_p(nxp_74hc153_remove),
227 .name = NXP_74HC153_DRIVER_NAME,
228 .owner = THIS_MODULE,
232 static int __init nxp_74hc153_init(void)
234 return platform_driver_register(&nxp_74hc153_driver);
236 subsys_initcall(nxp_74hc153_init);
238 static void __exit nxp_74hc153_exit(void)
240 platform_driver_unregister(&nxp_74hc153_driver);
242 module_exit(nxp_74hc153_exit);
244 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
245 MODULE_DESCRIPTION("GPIO expander driver for NXP 74HC153");
246 MODULE_LICENSE("GPL v2");
247 MODULE_ALIAS("platform:" NXP_74HC153_DRIVER_NAME);