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/version.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/gpio.h>
15 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/nxp_74hc153.h>
19 #define NXP_74HC153_NUM_GPIOS 8
20 #define NXP_74HC153_S0_MASK 0x1
21 #define NXP_74HC153_S1_MASK 0x2
22 #define NXP_74HC153_BANK_MASK 0x4
24 struct nxp_74hc153_chip {
25 struct device *parent;
26 struct gpio_chip gpio_chip;
30 static struct nxp_74hc153_chip *gpio_to_nxp(struct gpio_chip *gc)
32 return container_of(gc, struct nxp_74hc153_chip, gpio_chip);
35 static int nxp_74hc153_direction_input(struct gpio_chip *gc, unsigned offset)
40 static int nxp_74hc153_direction_output(struct gpio_chip *gc,
41 unsigned offset, int val)
46 static int nxp_74hc153_get_value(struct gpio_chip *gc, unsigned offset)
48 struct nxp_74hc153_chip *nxp;
49 struct nxp_74hc153_platform_data *pdata;
55 nxp = gpio_to_nxp(gc);
56 pdata = nxp->parent->platform_data;
58 s0 = !!(offset & NXP_74HC153_S0_MASK);
59 s1 = !!(offset & NXP_74HC153_S1_MASK);
60 pin = (offset & NXP_74HC153_BANK_MASK) ? pdata->gpio_pin_2y
63 mutex_lock(&nxp->lock);
64 gpio_set_value(pdata->gpio_pin_s0, s0);
65 gpio_set_value(pdata->gpio_pin_s1, s1);
66 ret = gpio_get_value(pin);
67 mutex_unlock(&nxp->lock);
72 static void nxp_74hc153_set_value(struct gpio_chip *gc,
73 unsigned offset, int val)
78 static int nxp_74hc153_probe(struct platform_device *pdev)
80 struct nxp_74hc153_platform_data *pdata;
81 struct nxp_74hc153_chip *nxp;
85 pdata = pdev->dev.platform_data;
87 dev_dbg(&pdev->dev, "no platform data specified\n");
91 nxp = kzalloc(sizeof(struct nxp_74hc153_chip), GFP_KERNEL);
93 dev_err(&pdev->dev, "no memory for private data\n");
97 err = gpio_request(pdata->gpio_pin_s0, dev_name(&pdev->dev));
99 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
100 pdata->gpio_pin_s0, err);
104 err = gpio_request(pdata->gpio_pin_s1, dev_name(&pdev->dev));
106 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
107 pdata->gpio_pin_s1, err);
111 err = gpio_request(pdata->gpio_pin_1y, dev_name(&pdev->dev));
113 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
114 pdata->gpio_pin_1y, err);
118 err = gpio_request(pdata->gpio_pin_2y, dev_name(&pdev->dev));
120 dev_err(&pdev->dev, "unable to claim gpio %u, err=%d\n",
121 pdata->gpio_pin_2y, err);
125 err = gpio_direction_output(pdata->gpio_pin_s0, 0);
128 "unable to set direction of gpio %u, err=%d\n",
129 pdata->gpio_pin_s0, err);
133 err = gpio_direction_output(pdata->gpio_pin_s1, 0);
136 "unable to set direction of gpio %u, err=%d\n",
137 pdata->gpio_pin_s1, err);
141 err = gpio_direction_input(pdata->gpio_pin_1y);
144 "unable to set direction of gpio %u, err=%d\n",
145 pdata->gpio_pin_1y, err);
149 err = gpio_direction_input(pdata->gpio_pin_2y);
152 "unable to set direction of gpio %u, err=%d\n",
153 pdata->gpio_pin_2y, err);
157 nxp->parent = &pdev->dev;
158 mutex_init(&nxp->lock);
160 gc = &nxp->gpio_chip;
162 gc->direction_input = nxp_74hc153_direction_input;
163 gc->direction_output = nxp_74hc153_direction_output;
164 gc->get = nxp_74hc153_get_value;
165 gc->set = nxp_74hc153_set_value;
168 gc->base = pdata->gpio_base;
169 gc->ngpio = NXP_74HC153_NUM_GPIOS;
170 gc->label = dev_name(nxp->parent);
171 gc->dev = nxp->parent;
172 gc->owner = THIS_MODULE;
174 err = gpiochip_add(&nxp->gpio_chip);
176 dev_err(&pdev->dev, "unable to add gpio chip, err=%d\n", err);
180 platform_set_drvdata(pdev, nxp);
184 gpio_free(pdata->gpio_pin_2y);
186 gpio_free(pdata->gpio_pin_1y);
188 gpio_free(pdata->gpio_pin_s1);
190 gpio_free(pdata->gpio_pin_s0);
196 static int nxp_74hc153_remove(struct platform_device *pdev)
198 struct nxp_74hc153_chip *nxp = platform_get_drvdata(pdev);
199 struct nxp_74hc153_platform_data *pdata = pdev->dev.platform_data;
202 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0)
205 err = gpiochip_remove(&nxp->gpio_chip);
208 "unable to remove gpio chip, err=%d\n",
213 gpiochip_remove(&nxp->gpio_chip);
215 gpio_free(pdata->gpio_pin_2y);
216 gpio_free(pdata->gpio_pin_1y);
217 gpio_free(pdata->gpio_pin_s1);
218 gpio_free(pdata->gpio_pin_s0);
221 platform_set_drvdata(pdev, NULL);
227 static struct platform_driver nxp_74hc153_driver = {
228 .probe = nxp_74hc153_probe,
229 .remove = nxp_74hc153_remove,
231 .name = NXP_74HC153_DRIVER_NAME,
232 .owner = THIS_MODULE,
236 static int __init nxp_74hc153_init(void)
238 return platform_driver_register(&nxp_74hc153_driver);
240 subsys_initcall(nxp_74hc153_init);
242 static void __exit nxp_74hc153_exit(void)
244 platform_driver_unregister(&nxp_74hc153_driver);
246 module_exit(nxp_74hc153_exit);
248 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
249 MODULE_DESCRIPTION("GPIO expander driver for NXP 74HC153");
250 MODULE_LICENSE("GPL v2");
251 MODULE_ALIAS("platform:" NXP_74HC153_DRIVER_NAME);