blob: 39d431da2dbcb403498b8c78bdc676bc19e22ceb [file] [log] [blame]
Thomas Gleixner75a6faf2019-06-01 10:08:37 +02001// SPDX-License-Identifier: GPL-2.0-only
Laxman Dewangan02c5ba12016-05-13 10:49:14 +05302/*
3 * MAXIM MAX77620 GPIO driver
4 *
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
Laxman Dewangan02c5ba12016-05-13 10:49:14 +05306 */
7
8#include <linux/gpio/driver.h>
9#include <linux/interrupt.h>
10#include <linux/mfd/max77620.h>
11#include <linux/module.h>
12#include <linux/platform_device.h>
13#include <linux/regmap.h>
14
15#define GPIO_REG_ADDR(offset) (MAX77620_REG_GPIO0 + offset)
16
17struct max77620_gpio {
18 struct gpio_chip gpio_chip;
19 struct regmap *rmap;
20 struct device *dev;
Timo Alhoab3dd9c2019-10-02 14:28:25 +020021 struct mutex buslock; /* irq_bus_lock */
Dmitry Osipenko4ee82252020-07-09 20:11:58 +030022 unsigned int irq_type[MAX77620_GPIO_NR];
23 bool irq_enabled[MAX77620_GPIO_NR];
Laxman Dewangan02c5ba12016-05-13 10:49:14 +053024};
25
Timo Alhoab3dd9c2019-10-02 14:28:25 +020026static irqreturn_t max77620_gpio_irqhandler(int irq, void *data)
27{
28 struct max77620_gpio *gpio = data;
29 unsigned int value, offset;
30 unsigned long pending;
31 int err;
Laxman Dewangan02c5ba12016-05-13 10:49:14 +053032
Timo Alhoab3dd9c2019-10-02 14:28:25 +020033 err = regmap_read(gpio->rmap, MAX77620_REG_IRQ_LVL2_GPIO, &value);
34 if (err < 0) {
35 dev_err(gpio->dev, "REG_IRQ_LVL2_GPIO read failed: %d\n", err);
36 return IRQ_NONE;
37 }
38
39 pending = value;
40
Dmitry Osipenko4ee82252020-07-09 20:11:58 +030041 for_each_set_bit(offset, &pending, MAX77620_GPIO_NR) {
Timo Alhoab3dd9c2019-10-02 14:28:25 +020042 unsigned int virq;
43
44 virq = irq_find_mapping(gpio->gpio_chip.irq.domain, offset);
45 handle_nested_irq(virq);
46 }
47
48 return IRQ_HANDLED;
49}
50
51static void max77620_gpio_irq_mask(struct irq_data *data)
52{
53 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
54 struct max77620_gpio *gpio = gpiochip_get_data(chip);
55
56 gpio->irq_enabled[data->hwirq] = false;
57}
58
59static void max77620_gpio_irq_unmask(struct irq_data *data)
60{
61 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
62 struct max77620_gpio *gpio = gpiochip_get_data(chip);
63
64 gpio->irq_enabled[data->hwirq] = true;
65}
66
67static int max77620_gpio_set_irq_type(struct irq_data *data, unsigned int type)
68{
69 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
70 struct max77620_gpio *gpio = gpiochip_get_data(chip);
71 unsigned int irq_type;
72
73 switch (type) {
74 case IRQ_TYPE_EDGE_RISING:
75 irq_type = MAX77620_CNFG_GPIO_INT_RISING;
76 break;
77
78 case IRQ_TYPE_EDGE_FALLING:
79 irq_type = MAX77620_CNFG_GPIO_INT_FALLING;
80 break;
81
82 case IRQ_TYPE_EDGE_BOTH:
83 irq_type = MAX77620_CNFG_GPIO_INT_RISING |
84 MAX77620_CNFG_GPIO_INT_FALLING;
85 break;
86
87 default:
88 return -EINVAL;
89 }
90
91 gpio->irq_type[data->hwirq] = irq_type;
92
93 return 0;
94}
95
96static void max77620_gpio_bus_lock(struct irq_data *data)
97{
98 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
99 struct max77620_gpio *gpio = gpiochip_get_data(chip);
100
101 mutex_lock(&gpio->buslock);
102}
103
104static void max77620_gpio_bus_sync_unlock(struct irq_data *data)
105{
106 struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
107 struct max77620_gpio *gpio = gpiochip_get_data(chip);
108 unsigned int value, offset = data->hwirq;
109 int err;
110
111 value = gpio->irq_enabled[offset] ? gpio->irq_type[offset] : 0;
112
113 err = regmap_update_bits(gpio->rmap, GPIO_REG_ADDR(offset),
114 MAX77620_CNFG_GPIO_INT_MASK, value);
115 if (err < 0)
116 dev_err(chip->parent, "failed to update interrupt mask: %d\n",
117 err);
118
119 mutex_unlock(&gpio->buslock);
120}
121
122static struct irq_chip max77620_gpio_irqchip = {
123 .name = "max77620-gpio",
124 .irq_mask = max77620_gpio_irq_mask,
125 .irq_unmask = max77620_gpio_irq_unmask,
126 .irq_set_type = max77620_gpio_set_irq_type,
127 .irq_bus_lock = max77620_gpio_bus_lock,
128 .irq_bus_sync_unlock = max77620_gpio_bus_sync_unlock,
129 .flags = IRQCHIP_MASK_ON_SUSPEND,
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530130};
131
132static int max77620_gpio_dir_input(struct gpio_chip *gc, unsigned int offset)
133{
134 struct max77620_gpio *mgpio = gpiochip_get_data(gc);
135 int ret;
136
137 ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
138 MAX77620_CNFG_GPIO_DIR_MASK,
139 MAX77620_CNFG_GPIO_DIR_INPUT);
140 if (ret < 0)
141 dev_err(mgpio->dev, "CNFG_GPIOx dir update failed: %d\n", ret);
142
143 return ret;
144}
145
146static int max77620_gpio_get(struct gpio_chip *gc, unsigned int offset)
147{
148 struct max77620_gpio *mgpio = gpiochip_get_data(gc);
149 unsigned int val;
150 int ret;
151
152 ret = regmap_read(mgpio->rmap, GPIO_REG_ADDR(offset), &val);
153 if (ret < 0) {
154 dev_err(mgpio->dev, "CNFG_GPIOx read failed: %d\n", ret);
155 return ret;
156 }
157
Venkat Reddy Talla1941b442016-06-27 16:26:24 +0530158 if (val & MAX77620_CNFG_GPIO_DIR_MASK)
159 return !!(val & MAX77620_CNFG_GPIO_INPUT_VAL_MASK);
160 else
161 return !!(val & MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK);
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530162}
163
164static int max77620_gpio_dir_output(struct gpio_chip *gc, unsigned int offset,
165 int value)
166{
167 struct max77620_gpio *mgpio = gpiochip_get_data(gc);
168 u8 val;
169 int ret;
170
171 val = (value) ? MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH :
172 MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW;
173
174 ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
175 MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK, val);
176 if (ret < 0) {
177 dev_err(mgpio->dev, "CNFG_GPIOx val update failed: %d\n", ret);
178 return ret;
179 }
180
181 ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
182 MAX77620_CNFG_GPIO_DIR_MASK,
183 MAX77620_CNFG_GPIO_DIR_OUTPUT);
184 if (ret < 0)
185 dev_err(mgpio->dev, "CNFG_GPIOx dir update failed: %d\n", ret);
186
187 return ret;
188}
189
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300190static int max77620_gpio_set_debounce(struct max77620_gpio *mgpio,
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530191 unsigned int offset,
192 unsigned int debounce)
193{
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530194 u8 val;
195 int ret;
196
197 switch (debounce) {
198 case 0:
199 val = MAX77620_CNFG_GPIO_DBNC_None;
200 break;
Thierry Redingb0391472019-11-08 17:07:46 +0100201 case 1 ... 8000:
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530202 val = MAX77620_CNFG_GPIO_DBNC_8ms;
203 break;
Thierry Redingb0391472019-11-08 17:07:46 +0100204 case 8001 ... 16000:
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530205 val = MAX77620_CNFG_GPIO_DBNC_16ms;
206 break;
Thierry Redingb0391472019-11-08 17:07:46 +0100207 case 16001 ... 32000:
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530208 val = MAX77620_CNFG_GPIO_DBNC_32ms;
209 break;
210 default:
211 dev_err(mgpio->dev, "Illegal value %u\n", debounce);
212 return -EINVAL;
213 }
214
215 ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
216 MAX77620_CNFG_GPIO_DBNC_MASK, val);
217 if (ret < 0)
218 dev_err(mgpio->dev, "CNFG_GPIOx_DBNC update failed: %d\n", ret);
219
220 return ret;
221}
222
223static void max77620_gpio_set(struct gpio_chip *gc, unsigned int offset,
224 int value)
225{
226 struct max77620_gpio *mgpio = gpiochip_get_data(gc);
227 u8 val;
228 int ret;
229
230 val = (value) ? MAX77620_CNFG_GPIO_OUTPUT_VAL_HIGH :
231 MAX77620_CNFG_GPIO_OUTPUT_VAL_LOW;
232
233 ret = regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
234 MAX77620_CNFG_GPIO_OUTPUT_VAL_MASK, val);
235 if (ret < 0)
236 dev_err(mgpio->dev, "CNFG_GPIO_OUT update failed: %d\n", ret);
237}
238
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300239static int max77620_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
240 unsigned long config)
Laxman Dewangan23087a02016-05-24 18:43:46 +0530241{
242 struct max77620_gpio *mgpio = gpiochip_get_data(gc);
243
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300244 switch (pinconf_to_config_param(config)) {
245 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
Laxman Dewangan23087a02016-05-24 18:43:46 +0530246 return regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
247 MAX77620_CNFG_GPIO_DRV_MASK,
248 MAX77620_CNFG_GPIO_DRV_OPENDRAIN);
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300249 case PIN_CONFIG_DRIVE_PUSH_PULL:
Laxman Dewangan23087a02016-05-24 18:43:46 +0530250 return regmap_update_bits(mgpio->rmap, GPIO_REG_ADDR(offset),
251 MAX77620_CNFG_GPIO_DRV_MASK,
252 MAX77620_CNFG_GPIO_DRV_PUSHPULL);
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300253 case PIN_CONFIG_INPUT_DEBOUNCE:
254 return max77620_gpio_set_debounce(mgpio, offset,
255 pinconf_to_config_argument(config));
Laxman Dewangan23087a02016-05-24 18:43:46 +0530256 default:
257 break;
258 }
259
260 return -ENOTSUPP;
261}
262
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530263static int max77620_gpio_probe(struct platform_device *pdev)
264{
265 struct max77620_chip *chip = dev_get_drvdata(pdev->dev.parent);
266 struct max77620_gpio *mgpio;
267 int gpio_irq;
268 int ret;
269
270 gpio_irq = platform_get_irq(pdev, 0);
Stephen Boyd15bddb72019-07-30 11:15:15 -0700271 if (gpio_irq <= 0)
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530272 return -ENODEV;
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530273
274 mgpio = devm_kzalloc(&pdev->dev, sizeof(*mgpio), GFP_KERNEL);
275 if (!mgpio)
276 return -ENOMEM;
277
278 mgpio->rmap = chip->rmap;
279 mgpio->dev = &pdev->dev;
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530280
281 mgpio->gpio_chip.label = pdev->name;
Dmitry Osipenko78934d82020-07-09 20:12:00 +0300282 mgpio->gpio_chip.parent = pdev->dev.parent;
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530283 mgpio->gpio_chip.direction_input = max77620_gpio_dir_input;
284 mgpio->gpio_chip.get = max77620_gpio_get;
285 mgpio->gpio_chip.direction_output = max77620_gpio_dir_output;
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530286 mgpio->gpio_chip.set = max77620_gpio_set;
Mika Westerberg2956b5d2017-01-23 15:34:34 +0300287 mgpio->gpio_chip.set_config = max77620_gpio_set_config;
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530288 mgpio->gpio_chip.ngpio = MAX77620_GPIO_NR;
289 mgpio->gpio_chip.can_sleep = 1;
290 mgpio->gpio_chip.base = -1;
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530291
292 platform_set_drvdata(pdev, mgpio);
293
294 ret = devm_gpiochip_add_data(&pdev->dev, &mgpio->gpio_chip, mgpio);
295 if (ret < 0) {
296 dev_err(&pdev->dev, "gpio_init: Failed to add max77620_gpio\n");
297 return ret;
298 }
299
Timo Alhoab3dd9c2019-10-02 14:28:25 +0200300 mutex_init(&mgpio->buslock);
301
302 gpiochip_irqchip_add_nested(&mgpio->gpio_chip, &max77620_gpio_irqchip,
303 0, handle_edge_irq, IRQ_TYPE_NONE);
304
Dmitry Osipenko2a5e6f72020-07-09 20:11:59 +0300305 ret = devm_request_threaded_irq(&pdev->dev, gpio_irq, NULL,
306 max77620_gpio_irqhandler, IRQF_ONESHOT,
307 "max77620-gpio", mgpio);
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530308 if (ret < 0) {
Timo Alhoab3dd9c2019-10-02 14:28:25 +0200309 dev_err(&pdev->dev, "failed to request IRQ: %d\n", ret);
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530310 return ret;
311 }
312
Timo Alhoab3dd9c2019-10-02 14:28:25 +0200313 gpiochip_set_nested_irqchip(&mgpio->gpio_chip, &max77620_gpio_irqchip,
314 gpio_irq);
315
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530316 return 0;
317}
318
319static const struct platform_device_id max77620_gpio_devtype[] = {
320 { .name = "max77620-gpio", },
Venkat Reddy Talla3107d572016-11-16 14:47:23 +0530321 { .name = "max20024-gpio", },
Laxman Dewangan02c5ba12016-05-13 10:49:14 +0530322 {},
323};
324MODULE_DEVICE_TABLE(platform, max77620_gpio_devtype);
325
326static struct platform_driver max77620_gpio_driver = {
327 .driver.name = "max77620-gpio",
328 .probe = max77620_gpio_probe,
329 .id_table = max77620_gpio_devtype,
330};
331
332module_platform_driver(max77620_gpio_driver);
333
334MODULE_DESCRIPTION("GPIO interface for MAX77620 and MAX20024 PMIC");
335MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
336MODULE_AUTHOR("Chaitanya Bandi <bandik@nvidia.com>");
337MODULE_ALIAS("platform:max77620-gpio");
338MODULE_LICENSE("GPL v2");