1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * GPIO driver for Analog Devices ADP5520 MFD PMICs
4  *
5  * Copyright 2009 Analog Devices Inc.
6  */
7 
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/platform_device.h>
13 #include <linux/mfd/adp5520.h>
14 #include <linux/gpio/driver.h>
15 
16 struct adp5520_gpio {
17 	struct device *master;
18 	struct gpio_chip gpio_chip;
19 	unsigned char lut[ADP5520_MAXGPIOS];
20 	unsigned long output;
21 };
22 
adp5520_gpio_get_value(struct gpio_chip * chip,unsigned off)23 static int adp5520_gpio_get_value(struct gpio_chip *chip, unsigned off)
24 {
25 	struct adp5520_gpio *dev;
26 	uint8_t reg_val;
27 
28 	dev = gpiochip_get_data(chip);
29 
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))
36 		adp5520_read(dev->master, ADP5520_GPIO_OUT, &reg_val);
37 	else
38 		adp5520_read(dev->master, ADP5520_GPIO_IN, &reg_val);
39 
40 	return !!(reg_val & dev->lut[off]);
41 }
42 
adp5520_gpio_set_value(struct gpio_chip * chip,unsigned int off,int val)43 static int adp5520_gpio_set_value(struct gpio_chip *chip,
44 				  unsigned int off, int val)
45 {
46 	struct adp5520_gpio *dev;
47 	dev = gpiochip_get_data(chip);
48 
49 	if (val)
50 		return adp5520_set_bits(dev->master, ADP5520_GPIO_OUT,
51 					dev->lut[off]);
52 	else
53 		return adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT,
54 					dev->lut[off]);
55 }
56 
adp5520_gpio_direction_input(struct gpio_chip * chip,unsigned off)57 static int adp5520_gpio_direction_input(struct gpio_chip *chip, unsigned off)
58 {
59 	struct adp5520_gpio *dev;
60 	dev = gpiochip_get_data(chip);
61 
62 	clear_bit(off, &dev->output);
63 
64 	return adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_2,
65 				dev->lut[off]);
66 }
67 
adp5520_gpio_direction_output(struct gpio_chip * chip,unsigned off,int val)68 static 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 = gpiochip_get_data(chip);
74 
75 	set_bit(off, &dev->output);
76 
77 	if (val)
78 		ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_OUT,
79 					dev->lut[off]);
80 	else
81 		ret |= adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT,
82 					dev->lut[off]);
83 
84 	ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_2,
85 					dev->lut[off]);
86 
87 	return ret;
88 }
89 
adp5520_gpio_probe(struct platform_device * pdev)90 static int adp5520_gpio_probe(struct platform_device *pdev)
91 {
92 	struct adp5520_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
93 	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 
108 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
109 	if (dev == NULL)
110 		return -ENOMEM;
111 
112 	dev->master = pdev->dev.parent;
113 
114 	for (gpios = 0, i = 0; i < ADP5520_MAXGPIOS; i++)
115 		if (pdata->gpio_en_mask & (1 << i))
116 			dev->lut[gpios++] = 1 << i;
117 
118 	if (gpios < 1)
119 		return -EINVAL;
120 
121 	gc = &dev->gpio_chip;
122 	gc->direction_input  = adp5520_gpio_direction_input;
123 	gc->direction_output = adp5520_gpio_direction_output;
124 	gc->get = adp5520_gpio_get_value;
125 	gc->set_rv = adp5520_gpio_set_value;
126 	gc->can_sleep = true;
127 
128 	gc->base = pdata->gpio_start;
129 	gc->ngpio = gpios;
130 	gc->label = pdev->name;
131 	gc->owner = THIS_MODULE;
132 
133 	ret = adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_1,
134 		pdata->gpio_en_mask);
135 
136 	if (pdata->gpio_en_mask & ADP5520_GPIO_C3)
137 		ctl_mask |= ADP5520_C3_MODE;
138 
139 	if (pdata->gpio_en_mask & ADP5520_GPIO_R3)
140 		ctl_mask |= ADP5520_R3_MODE;
141 
142 	if (ctl_mask)
143 		ret = adp5520_set_bits(dev->master, ADP5520_LED_CONTROL,
144 			ctl_mask);
145 
146 	ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP,
147 		pdata->gpio_pullup_mask);
148 
149 	if (ret) {
150 		dev_err(&pdev->dev, "failed to write\n");
151 		return ret;
152 	}
153 
154 	return devm_gpiochip_add_data(&pdev->dev, &dev->gpio_chip, dev);
155 }
156 
157 static struct platform_driver adp5520_gpio_driver = {
158 	.driver	= {
159 		.name	= "adp5520-gpio",
160 	},
161 	.probe		= adp5520_gpio_probe,
162 };
163 
164 module_platform_driver(adp5520_gpio_driver);
165 
166 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
167 MODULE_DESCRIPTION("GPIO ADP5520 Driver");
168 MODULE_LICENSE("GPL");
169 MODULE_ALIAS("platform:adp5520-gpio");
170