1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * gpiolib support for Wolfson Arizona class devices
5 * Copyright 2012 Wolfson Microelectronics PLC.
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
10 #include <linux/gpio/driver.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/slab.h>
17 #include <linux/mfd/arizona/core.h>
18 #include <linux/mfd/arizona/pdata.h>
19 #include <linux/mfd/arizona/registers.h>
22 struct arizona *arizona;
23 struct gpio_chip gpio_chip;
26 static int arizona_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
28 struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
29 struct arizona *arizona = arizona_gpio->arizona;
30 bool persistent = gpiochip_line_is_persistent(chip, offset);
34 ret = regmap_update_bits_check(arizona->regmap,
35 ARIZONA_GPIO1_CTRL + offset,
36 ARIZONA_GPN_DIR, ARIZONA_GPN_DIR,
41 if (change && persistent) {
42 pm_runtime_mark_last_busy(chip->parent);
43 pm_runtime_put_autosuspend(chip->parent);
49 static int arizona_gpio_get(struct gpio_chip *chip, unsigned offset)
51 struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
52 struct arizona *arizona = arizona_gpio->arizona;
53 unsigned int reg, val;
56 reg = ARIZONA_GPIO1_CTRL + offset;
57 ret = regmap_read(arizona->regmap, reg, &val);
61 /* Resume to read actual registers for input pins */
62 if (val & ARIZONA_GPN_DIR) {
63 ret = pm_runtime_get_sync(chip->parent);
65 dev_err(chip->parent, "Failed to resume: %d\n", ret);
66 pm_runtime_put_autosuspend(chip->parent);
70 /* Register is cached, drop it to ensure a physical read */
71 ret = regcache_drop_region(arizona->regmap, reg, reg);
73 dev_err(chip->parent, "Failed to drop cache: %d\n",
75 pm_runtime_put_autosuspend(chip->parent);
79 ret = regmap_read(arizona->regmap, reg, &val);
81 pm_runtime_put_autosuspend(chip->parent);
85 pm_runtime_mark_last_busy(chip->parent);
86 pm_runtime_put_autosuspend(chip->parent);
89 if (val & ARIZONA_GPN_LVL)
95 static int arizona_gpio_direction_out(struct gpio_chip *chip,
96 unsigned offset, int value)
98 struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
99 struct arizona *arizona = arizona_gpio->arizona;
100 bool persistent = gpiochip_line_is_persistent(chip, offset);
104 ret = regmap_read(arizona->regmap, ARIZONA_GPIO1_CTRL + offset, &val);
108 if ((val & ARIZONA_GPN_DIR) && persistent) {
109 ret = pm_runtime_get_sync(chip->parent);
111 dev_err(chip->parent, "Failed to resume: %d\n", ret);
112 pm_runtime_put(chip->parent);
118 value = ARIZONA_GPN_LVL;
120 return regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
121 ARIZONA_GPN_DIR | ARIZONA_GPN_LVL, value);
124 static void arizona_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
126 struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
127 struct arizona *arizona = arizona_gpio->arizona;
130 value = ARIZONA_GPN_LVL;
132 regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
133 ARIZONA_GPN_LVL, value);
136 static const struct gpio_chip template_chip = {
138 .owner = THIS_MODULE,
139 .direction_input = arizona_gpio_direction_in,
140 .get = arizona_gpio_get,
141 .direction_output = arizona_gpio_direction_out,
142 .set = arizona_gpio_set,
146 static int arizona_gpio_probe(struct platform_device *pdev)
148 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
149 struct arizona_pdata *pdata = &arizona->pdata;
150 struct arizona_gpio *arizona_gpio;
153 device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
155 arizona_gpio = devm_kzalloc(&pdev->dev, sizeof(*arizona_gpio),
160 arizona_gpio->arizona = arizona;
161 arizona_gpio->gpio_chip = template_chip;
162 arizona_gpio->gpio_chip.parent = &pdev->dev;
164 switch (arizona->type) {
171 arizona_gpio->gpio_chip.ngpio = 5;
175 arizona_gpio->gpio_chip.ngpio = 2;
178 dev_err(&pdev->dev, "Unknown chip variant %d\n",
183 if (pdata->gpio_base)
184 arizona_gpio->gpio_chip.base = pdata->gpio_base;
186 arizona_gpio->gpio_chip.base = -1;
188 pm_runtime_enable(&pdev->dev);
190 ret = devm_gpiochip_add_data(&pdev->dev, &arizona_gpio->gpio_chip,
193 pm_runtime_disable(&pdev->dev);
194 dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
202 static struct platform_driver arizona_gpio_driver = {
203 .driver.name = "arizona-gpio",
204 .probe = arizona_gpio_probe,
207 module_platform_driver(arizona_gpio_driver);
209 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
210 MODULE_DESCRIPTION("GPIO interface for Arizona devices");
211 MODULE_LICENSE("GPL");
212 MODULE_ALIAS("platform:arizona-gpio");