blob: c55e821c63b6663a7884e0549a2d2e63c2bfbd6b [file] [log] [blame]
Thomas Gleixner80503b22019-05-24 12:04:09 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Michael Hennerichef72af42009-09-22 16:46:39 -07002/*
3 * GPIO driver for Analog Devices ADP5520 MFD PMICs
4 *
5 * Copyright 2009 Analog Devices Inc.
Michael Hennerichef72af42009-09-22 16:46:39 -07006 */
7
8#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Michael Hennerichef72af42009-09-22 16:46:39 -070010#include <linux/kernel.h>
11#include <linux/init.h>
12#include <linux/platform_device.h>
13#include <linux/mfd/adp5520.h>
Linus Walleij8a3b4f22018-01-13 22:12:07 +010014#include <linux/gpio/driver.h>
Michael Hennerichef72af42009-09-22 16:46:39 -070015
16struct adp5520_gpio {
17 struct device *master;
18 struct gpio_chip gpio_chip;
19 unsigned char lut[ADP5520_MAXGPIOS];
20 unsigned long output;
21};
22
23static int adp5520_gpio_get_value(struct gpio_chip *chip, unsigned off)
24{
25 struct adp5520_gpio *dev;
26 uint8_t reg_val;
27
Linus Walleij5060e0e2015-12-04 15:01:50 +010028 dev = gpiochip_get_data(chip);
Michael Hennerichef72af42009-09-22 16:46:39 -070029
30 /*
31 * There are dedicated registers for GPIO IN/OUT.
32 * Make sure we return the right value, even when configured as output
33 */
34
35 if (test_bit(off, &dev->output))
Michael Hennerich7c29a472009-10-10 13:54:03 -040036 adp5520_read(dev->master, ADP5520_GPIO_OUT, &reg_val);
Michael Hennerichef72af42009-09-22 16:46:39 -070037 else
Michael Hennerich7c29a472009-10-10 13:54:03 -040038 adp5520_read(dev->master, ADP5520_GPIO_IN, &reg_val);
Michael Hennerichef72af42009-09-22 16:46:39 -070039
40 return !!(reg_val & dev->lut[off]);
41}
42
43static void adp5520_gpio_set_value(struct gpio_chip *chip,
44 unsigned off, int val)
45{
46 struct adp5520_gpio *dev;
Linus Walleij5060e0e2015-12-04 15:01:50 +010047 dev = gpiochip_get_data(chip);
Michael Hennerichef72af42009-09-22 16:46:39 -070048
49 if (val)
Michael Hennerich7c29a472009-10-10 13:54:03 -040050 adp5520_set_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070051 else
Michael Hennerich7c29a472009-10-10 13:54:03 -040052 adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070053}
54
55static int adp5520_gpio_direction_input(struct gpio_chip *chip, unsigned off)
56{
57 struct adp5520_gpio *dev;
Linus Walleij5060e0e2015-12-04 15:01:50 +010058 dev = gpiochip_get_data(chip);
Michael Hennerichef72af42009-09-22 16:46:39 -070059
60 clear_bit(off, &dev->output);
61
Michael Hennerich7c29a472009-10-10 13:54:03 -040062 return adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_2,
63 dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070064}
65
66static int adp5520_gpio_direction_output(struct gpio_chip *chip,
67 unsigned off, int val)
68{
69 struct adp5520_gpio *dev;
70 int ret = 0;
Linus Walleij5060e0e2015-12-04 15:01:50 +010071 dev = gpiochip_get_data(chip);
Michael Hennerichef72af42009-09-22 16:46:39 -070072
73 set_bit(off, &dev->output);
74
75 if (val)
Michael Hennerich7c29a472009-10-10 13:54:03 -040076 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_OUT,
77 dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070078 else
Michael Hennerich7c29a472009-10-10 13:54:03 -040079 ret |= adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT,
80 dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070081
Michael Hennerich7c29a472009-10-10 13:54:03 -040082 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_2,
83 dev->lut[off]);
Michael Hennerichef72af42009-09-22 16:46:39 -070084
85 return ret;
86}
87
Bill Pemberton38363092012-11-19 13:22:34 -050088static int adp5520_gpio_probe(struct platform_device *pdev)
Michael Hennerichef72af42009-09-22 16:46:39 -070089{
Jingoo Hane56aee12013-07-30 17:08:05 +090090 struct adp5520_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
Michael Hennerichef72af42009-09-22 16:46:39 -070091 struct adp5520_gpio *dev;
92 struct gpio_chip *gc;
93 int ret, i, gpios;
94 unsigned char ctl_mask = 0;
95
96 if (pdata == NULL) {
97 dev_err(&pdev->dev, "missing platform data\n");
98 return -ENODEV;
99 }
100
101 if (pdev->id != ID_ADP5520) {
102 dev_err(&pdev->dev, "only ADP5520 supports GPIO\n");
103 return -ENODEV;
104 }
105
Jingoo Han24bb3812013-03-15 18:14:01 +0900106 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Jingoo Hane8a71aa2014-04-29 17:31:31 +0900107 if (dev == NULL)
Michael Hennerichef72af42009-09-22 16:46:39 -0700108 return -ENOMEM;
Michael Hennerichef72af42009-09-22 16:46:39 -0700109
110 dev->master = pdev->dev.parent;
111
112 for (gpios = 0, i = 0; i < ADP5520_MAXGPIOS; i++)
113 if (pdata->gpio_en_mask & (1 << i))
114 dev->lut[gpios++] = 1 << i;
115
Alexandru Ardelean6681db52021-05-14 11:56:27 +0300116 if (gpios < 1)
117 return -EINVAL;
Michael Hennerichef72af42009-09-22 16:46:39 -0700118
119 gc = &dev->gpio_chip;
120 gc->direction_input = adp5520_gpio_direction_input;
121 gc->direction_output = adp5520_gpio_direction_output;
122 gc->get = adp5520_gpio_get_value;
123 gc->set = adp5520_gpio_set_value;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100124 gc->can_sleep = true;
Michael Hennerichef72af42009-09-22 16:46:39 -0700125
126 gc->base = pdata->gpio_start;
127 gc->ngpio = gpios;
128 gc->label = pdev->name;
129 gc->owner = THIS_MODULE;
130
Michael Hennerich7c29a472009-10-10 13:54:03 -0400131 ret = adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_1,
Michael Hennerichef72af42009-09-22 16:46:39 -0700132 pdata->gpio_en_mask);
133
Michael Hennerich7c29a472009-10-10 13:54:03 -0400134 if (pdata->gpio_en_mask & ADP5520_GPIO_C3)
135 ctl_mask |= ADP5520_C3_MODE;
Michael Hennerichef72af42009-09-22 16:46:39 -0700136
Michael Hennerich7c29a472009-10-10 13:54:03 -0400137 if (pdata->gpio_en_mask & ADP5520_GPIO_R3)
138 ctl_mask |= ADP5520_R3_MODE;
Michael Hennerichef72af42009-09-22 16:46:39 -0700139
140 if (ctl_mask)
Michael Hennerich7c29a472009-10-10 13:54:03 -0400141 ret = adp5520_set_bits(dev->master, ADP5520_LED_CONTROL,
Michael Hennerichef72af42009-09-22 16:46:39 -0700142 ctl_mask);
143
Michael Hennerich7c29a472009-10-10 13:54:03 -0400144 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP,
Michael Hennerichef72af42009-09-22 16:46:39 -0700145 pdata->gpio_pullup_mask);
146
147 if (ret) {
148 dev_err(&pdev->dev, "failed to write\n");
Alexandru Ardelean6681db52021-05-14 11:56:27 +0300149 return ret;
Michael Hennerichef72af42009-09-22 16:46:39 -0700150 }
151
Alexandru Ardelean6681db52021-05-14 11:56:27 +0300152 return devm_gpiochip_add_data(&pdev->dev, &dev->gpio_chip, dev);
Michael Hennerichef72af42009-09-22 16:46:39 -0700153}
154
Michael Hennerichef72af42009-09-22 16:46:39 -0700155static struct platform_driver adp5520_gpio_driver = {
156 .driver = {
157 .name = "adp5520-gpio",
Michael Hennerichef72af42009-09-22 16:46:39 -0700158 },
159 .probe = adp5520_gpio_probe,
Michael Hennerichef72af42009-09-22 16:46:39 -0700160};
161
Mark Brown6f614152011-12-08 00:24:00 +0800162module_platform_driver(adp5520_gpio_driver);
Michael Hennerichef72af42009-09-22 16:46:39 -0700163
Michael Hennerichbe887842018-08-14 13:29:18 +0200164MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
Michael Hennerichef72af42009-09-22 16:46:39 -0700165MODULE_DESCRIPTION("GPIO ADP5520 Driver");
166MODULE_LICENSE("GPL");
167MODULE_ALIAS("platform:adp5520-gpio");