blob: f33f78dcadaad817ed331c40daa0447d126282a6 [file] [log] [blame]
Michael Hennerichef72af42009-09-22 16:46:39 -07001/*
2 * GPIO driver for Analog Devices ADP5520 MFD PMICs
3 *
4 * Copyright 2009 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Michael Hennerichef72af42009-09-22 16:46:39 -070011#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/platform_device.h>
14#include <linux/mfd/adp5520.h>
15
16#include <linux/gpio.h>
17
18struct adp5520_gpio {
19 struct device *master;
20 struct gpio_chip gpio_chip;
21 unsigned char lut[ADP5520_MAXGPIOS];
22 unsigned long output;
23};
24
25static int adp5520_gpio_get_value(struct gpio_chip *chip, unsigned off)
26{
27 struct adp5520_gpio *dev;
28 uint8_t reg_val;
29
30 dev = container_of(chip, struct adp5520_gpio, gpio_chip);
31
32 /*
33 * There are dedicated registers for GPIO IN/OUT.
34 * Make sure we return the right value, even when configured as output
35 */
36
37 if (test_bit(off, &dev->output))
Michael Hennerich7c29a472009-10-10 13:54:03 -040038 adp5520_read(dev->master, ADP5520_GPIO_OUT, &reg_val);
Michael Hennerichef72af42009-09-22 16:46:39 -070039 else
Michael Hennerich7c29a472009-10-10 13:54:03 -040040 adp5520_read(dev->master, ADP5520_GPIO_IN, &reg_val);
Michael Hennerichef72af42009-09-22 16:46:39 -070041
42 return !!(reg_val & dev->lut[off]);
43}
44
45static void adp5520_gpio_set_value(struct gpio_chip *chip,
46 unsigned off, int val)
47{
48 struct adp5520_gpio *dev;
49 dev = container_of(chip, struct adp5520_gpio, gpio_chip);
50
51 if (val)
Michael Hennerich7c29a472009-10-10 13:54:03 -040052 adp5520_set_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070053 else
Michael Hennerich7c29a472009-10-10 13:54:03 -040054 adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070055}
56
57static int adp5520_gpio_direction_input(struct gpio_chip *chip, unsigned off)
58{
59 struct adp5520_gpio *dev;
60 dev = container_of(chip, struct adp5520_gpio, gpio_chip);
61
62 clear_bit(off, &dev->output);
63
Michael Hennerich7c29a472009-10-10 13:54:03 -040064 return adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_2,
65 dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070066}
67
68static int adp5520_gpio_direction_output(struct gpio_chip *chip,
69 unsigned off, int val)
70{
71 struct adp5520_gpio *dev;
72 int ret = 0;
73 dev = container_of(chip, struct adp5520_gpio, gpio_chip);
74
75 set_bit(off, &dev->output);
76
77 if (val)
Michael Hennerich7c29a472009-10-10 13:54:03 -040078 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_OUT,
79 dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070080 else
Michael Hennerich7c29a472009-10-10 13:54:03 -040081 ret |= adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT,
82 dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070083
Michael Hennerich7c29a472009-10-10 13:54:03 -040084 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_2,
85 dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070086
87 return ret;
88}
89
Bill Pemberton38363092012-11-19 13:22:34 -050090static int adp5520_gpio_probe(struct platform_device *pdev)
Michael Hennerichef72af42009-09-22 16:46:39 -070091{
Michael Hennerich7c29a472009-10-10 13:54:03 -040092 struct adp5520_gpio_platform_data *pdata = pdev->dev.platform_data;
Michael Hennerichef72af42009-09-22 16:46:39 -070093 struct adp5520_gpio *dev;
94 struct gpio_chip *gc;
95 int ret, i, gpios;
96 unsigned char ctl_mask = 0;
97
98 if (pdata == NULL) {
99 dev_err(&pdev->dev, "missing platform data\n");
100 return -ENODEV;
101 }
102
103 if (pdev->id != ID_ADP5520) {
104 dev_err(&pdev->dev, "only ADP5520 supports GPIO\n");
105 return -ENODEV;
106 }
107
Jingoo Han24bb3812013-03-15 18:14:01 +0900108 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Michael Hennerichef72af42009-09-22 16:46:39 -0700109 if (dev == NULL) {
110 dev_err(&pdev->dev, "failed to alloc memory\n");
111 return -ENOMEM;
112 }
113
114 dev->master = pdev->dev.parent;
115
116 for (gpios = 0, i = 0; i < ADP5520_MAXGPIOS; i++)
117 if (pdata->gpio_en_mask & (1 << i))
118 dev->lut[gpios++] = 1 << i;
119
120 if (gpios < 1) {
121 ret = -EINVAL;
122 goto err;
123 }
124
125 gc = &dev->gpio_chip;
126 gc->direction_input = adp5520_gpio_direction_input;
127 gc->direction_output = adp5520_gpio_direction_output;
128 gc->get = adp5520_gpio_get_value;
129 gc->set = adp5520_gpio_set_value;
130 gc->can_sleep = 1;
131
132 gc->base = pdata->gpio_start;
133 gc->ngpio = gpios;
134 gc->label = pdev->name;
135 gc->owner = THIS_MODULE;
136
Michael Hennerich7c29a472009-10-10 13:54:03 -0400137 ret = adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_1,
Michael Hennerichef72af42009-09-22 16:46:39 -0700138 pdata->gpio_en_mask);
139
Michael Hennerich7c29a472009-10-10 13:54:03 -0400140 if (pdata->gpio_en_mask & ADP5520_GPIO_C3)
141 ctl_mask |= ADP5520_C3_MODE;
Michael Hennerichef72af42009-09-22 16:46:39 -0700142
Michael Hennerich7c29a472009-10-10 13:54:03 -0400143 if (pdata->gpio_en_mask & ADP5520_GPIO_R3)
144 ctl_mask |= ADP5520_R3_MODE;
Michael Hennerichef72af42009-09-22 16:46:39 -0700145
146 if (ctl_mask)
Michael Hennerich7c29a472009-10-10 13:54:03 -0400147 ret = adp5520_set_bits(dev->master, ADP5520_LED_CONTROL,
Michael Hennerichef72af42009-09-22 16:46:39 -0700148 ctl_mask);
149
Michael Hennerich7c29a472009-10-10 13:54:03 -0400150 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP,
Michael Hennerichef72af42009-09-22 16:46:39 -0700151 pdata->gpio_pullup_mask);
152
153 if (ret) {
154 dev_err(&pdev->dev, "failed to write\n");
155 goto err;
156 }
157
158 ret = gpiochip_add(&dev->gpio_chip);
159 if (ret)
160 goto err;
161
162 platform_set_drvdata(pdev, dev);
163 return 0;
164
165err:
Michael Hennerichef72af42009-09-22 16:46:39 -0700166 return ret;
167}
168
Bill Pemberton206210c2012-11-19 13:25:50 -0500169static int adp5520_gpio_remove(struct platform_device *pdev)
Michael Hennerichef72af42009-09-22 16:46:39 -0700170{
171 struct adp5520_gpio *dev;
172 int ret;
173
174 dev = platform_get_drvdata(pdev);
175 ret = gpiochip_remove(&dev->gpio_chip);
176 if (ret) {
177 dev_err(&pdev->dev, "%s failed, %d\n",
178 "gpiochip_remove()", ret);
179 return ret;
180 }
181
Michael Hennerichef72af42009-09-22 16:46:39 -0700182 return 0;
183}
184
185static struct platform_driver adp5520_gpio_driver = {
186 .driver = {
187 .name = "adp5520-gpio",
188 .owner = THIS_MODULE,
189 },
190 .probe = adp5520_gpio_probe,
Bill Pemberton8283c4f2012-11-19 13:20:08 -0500191 .remove = adp5520_gpio_remove,
Michael Hennerichef72af42009-09-22 16:46:39 -0700192};
193
Mark Brown6f614152011-12-08 00:24:00 +0800194module_platform_driver(adp5520_gpio_driver);
Michael Hennerichef72af42009-09-22 16:46:39 -0700195
196MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
197MODULE_DESCRIPTION("GPIO ADP5520 Driver");
198MODULE_LICENSE("GPL");
199MODULE_ALIAS("platform:adp5520-gpio");