blob: 21452622d954249e9b46423234bb14acbe535329 [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>
Linus Walleij8a3b4f22018-01-13 22:12:07 +010015#include <linux/gpio/driver.h>
Michael Hennerichef72af42009-09-22 16:46:39 -070016
17struct adp5520_gpio {
18 struct device *master;
19 struct gpio_chip gpio_chip;
20 unsigned char lut[ADP5520_MAXGPIOS];
21 unsigned long output;
22};
23
24static int adp5520_gpio_get_value(struct gpio_chip *chip, unsigned off)
25{
26 struct adp5520_gpio *dev;
27 uint8_t reg_val;
28
Linus Walleij5060e0e2015-12-04 15:01:50 +010029 dev = gpiochip_get_data(chip);
Michael Hennerichef72af42009-09-22 16:46:39 -070030
31 /*
32 * There are dedicated registers for GPIO IN/OUT.
33 * Make sure we return the right value, even when configured as output
34 */
35
36 if (test_bit(off, &dev->output))
Michael Hennerich7c29a472009-10-10 13:54:03 -040037 adp5520_read(dev->master, ADP5520_GPIO_OUT, &reg_val);
Michael Hennerichef72af42009-09-22 16:46:39 -070038 else
Michael Hennerich7c29a472009-10-10 13:54:03 -040039 adp5520_read(dev->master, ADP5520_GPIO_IN, &reg_val);
Michael Hennerichef72af42009-09-22 16:46:39 -070040
41 return !!(reg_val & dev->lut[off]);
42}
43
44static void adp5520_gpio_set_value(struct gpio_chip *chip,
45 unsigned off, int val)
46{
47 struct adp5520_gpio *dev;
Linus Walleij5060e0e2015-12-04 15:01:50 +010048 dev = gpiochip_get_data(chip);
Michael Hennerichef72af42009-09-22 16:46:39 -070049
50 if (val)
Michael Hennerich7c29a472009-10-10 13:54:03 -040051 adp5520_set_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070052 else
Michael Hennerich7c29a472009-10-10 13:54:03 -040053 adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070054}
55
56static int adp5520_gpio_direction_input(struct gpio_chip *chip, unsigned off)
57{
58 struct adp5520_gpio *dev;
Linus Walleij5060e0e2015-12-04 15:01:50 +010059 dev = gpiochip_get_data(chip);
Michael Hennerichef72af42009-09-22 16:46:39 -070060
61 clear_bit(off, &dev->output);
62
Michael Hennerich7c29a472009-10-10 13:54:03 -040063 return adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_2,
64 dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070065}
66
67static int adp5520_gpio_direction_output(struct gpio_chip *chip,
68 unsigned off, int val)
69{
70 struct adp5520_gpio *dev;
71 int ret = 0;
Linus Walleij5060e0e2015-12-04 15:01:50 +010072 dev = gpiochip_get_data(chip);
Michael Hennerichef72af42009-09-22 16:46:39 -070073
74 set_bit(off, &dev->output);
75
76 if (val)
Michael Hennerich7c29a472009-10-10 13:54:03 -040077 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_OUT,
78 dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070079 else
Michael Hennerich7c29a472009-10-10 13:54:03 -040080 ret |= adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT,
81 dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070082
Michael Hennerich7c29a472009-10-10 13:54:03 -040083 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_2,
84 dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070085
86 return ret;
87}
88
Bill Pemberton38363092012-11-19 13:22:34 -050089static int adp5520_gpio_probe(struct platform_device *pdev)
Michael Hennerichef72af42009-09-22 16:46:39 -070090{
Jingoo Hane56aee12013-07-30 17:08:05 +090091 struct adp5520_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
Michael Hennerichef72af42009-09-22 16:46:39 -070092 struct adp5520_gpio *dev;
93 struct gpio_chip *gc;
94 int ret, i, gpios;
95 unsigned char ctl_mask = 0;
96
97 if (pdata == NULL) {
98 dev_err(&pdev->dev, "missing platform data\n");
99 return -ENODEV;
100 }
101
102 if (pdev->id != ID_ADP5520) {
103 dev_err(&pdev->dev, "only ADP5520 supports GPIO\n");
104 return -ENODEV;
105 }
106
Jingoo Han24bb3812013-03-15 18:14:01 +0900107 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Jingoo Hane8a71aa2014-04-29 17:31:31 +0900108 if (dev == NULL)
Michael Hennerichef72af42009-09-22 16:46:39 -0700109 return -ENOMEM;
Michael Hennerichef72af42009-09-22 16:46:39 -0700110
111 dev->master = pdev->dev.parent;
112
113 for (gpios = 0, i = 0; i < ADP5520_MAXGPIOS; i++)
114 if (pdata->gpio_en_mask & (1 << i))
115 dev->lut[gpios++] = 1 << i;
116
117 if (gpios < 1) {
118 ret = -EINVAL;
119 goto err;
120 }
121
122 gc = &dev->gpio_chip;
123 gc->direction_input = adp5520_gpio_direction_input;
124 gc->direction_output = adp5520_gpio_direction_output;
125 gc->get = adp5520_gpio_get_value;
126 gc->set = adp5520_gpio_set_value;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100127 gc->can_sleep = true;
Michael Hennerichef72af42009-09-22 16:46:39 -0700128
129 gc->base = pdata->gpio_start;
130 gc->ngpio = gpios;
131 gc->label = pdev->name;
132 gc->owner = THIS_MODULE;
133
Michael Hennerich7c29a472009-10-10 13:54:03 -0400134 ret = adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_1,
Michael Hennerichef72af42009-09-22 16:46:39 -0700135 pdata->gpio_en_mask);
136
Michael Hennerich7c29a472009-10-10 13:54:03 -0400137 if (pdata->gpio_en_mask & ADP5520_GPIO_C3)
138 ctl_mask |= ADP5520_C3_MODE;
Michael Hennerichef72af42009-09-22 16:46:39 -0700139
Michael Hennerich7c29a472009-10-10 13:54:03 -0400140 if (pdata->gpio_en_mask & ADP5520_GPIO_R3)
141 ctl_mask |= ADP5520_R3_MODE;
Michael Hennerichef72af42009-09-22 16:46:39 -0700142
143 if (ctl_mask)
Michael Hennerich7c29a472009-10-10 13:54:03 -0400144 ret = adp5520_set_bits(dev->master, ADP5520_LED_CONTROL,
Michael Hennerichef72af42009-09-22 16:46:39 -0700145 ctl_mask);
146
Michael Hennerich7c29a472009-10-10 13:54:03 -0400147 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP,
Michael Hennerichef72af42009-09-22 16:46:39 -0700148 pdata->gpio_pullup_mask);
149
150 if (ret) {
151 dev_err(&pdev->dev, "failed to write\n");
152 goto err;
153 }
154
Laxman Dewangan8ae6ace2016-02-22 17:43:28 +0530155 ret = devm_gpiochip_add_data(&pdev->dev, &dev->gpio_chip, dev);
Michael Hennerichef72af42009-09-22 16:46:39 -0700156 if (ret)
157 goto err;
158
159 platform_set_drvdata(pdev, dev);
160 return 0;
161
162err:
Michael Hennerichef72af42009-09-22 16:46:39 -0700163 return ret;
164}
165
Michael Hennerichef72af42009-09-22 16:46:39 -0700166static struct platform_driver adp5520_gpio_driver = {
167 .driver = {
168 .name = "adp5520-gpio",
Michael Hennerichef72af42009-09-22 16:46:39 -0700169 },
170 .probe = adp5520_gpio_probe,
Michael Hennerichef72af42009-09-22 16:46:39 -0700171};
172
Mark Brown6f614152011-12-08 00:24:00 +0800173module_platform_driver(adp5520_gpio_driver);
Michael Hennerichef72af42009-09-22 16:46:39 -0700174
175MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
176MODULE_DESCRIPTION("GPIO ADP5520 Driver");
177MODULE_LICENSE("GPL");
178MODULE_ALIAS("platform:adp5520-gpio");