e530c94dcce8d0b2c219c4ed3186891d8b5ec64d
[linux-2.6-block.git] / drivers / gpio / gpio-arizona.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * gpiolib support for Wolfson Arizona class devices
4  *
5  * Copyright 2012 Wolfson Microelectronics PLC.
6  *
7  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8  */
9
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>
16
17 #include <linux/mfd/arizona/core.h>
18 #include <linux/mfd/arizona/pdata.h>
19 #include <linux/mfd/arizona/registers.h>
20
21 struct arizona_gpio {
22         struct arizona *arizona;
23         struct gpio_chip gpio_chip;
24 };
25
26 static int arizona_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
27 {
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);
31         bool change;
32         int ret;
33
34         ret = regmap_update_bits_check(arizona->regmap,
35                                        ARIZONA_GPIO1_CTRL + offset,
36                                        ARIZONA_GPN_DIR, ARIZONA_GPN_DIR,
37                                        &change);
38         if (ret < 0)
39                 return ret;
40
41         if (change && persistent) {
42                 pm_runtime_mark_last_busy(chip->parent);
43                 pm_runtime_put_autosuspend(chip->parent);
44         }
45
46         return 0;
47 }
48
49 static int arizona_gpio_get(struct gpio_chip *chip, unsigned offset)
50 {
51         struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
52         struct arizona *arizona = arizona_gpio->arizona;
53         unsigned int reg, val;
54         int ret;
55
56         reg = ARIZONA_GPIO1_CTRL + offset;
57         ret = regmap_read(arizona->regmap, reg, &val);
58         if (ret < 0)
59                 return ret;
60
61         /* Resume to read actual registers for input pins */
62         if (val & ARIZONA_GPN_DIR) {
63                 ret = pm_runtime_get_sync(chip->parent);
64                 if (ret < 0) {
65                         dev_err(chip->parent, "Failed to resume: %d\n", ret);
66                         pm_runtime_put_autosuspend(chip->parent);
67                         return ret;
68                 }
69
70                 /* Register is cached, drop it to ensure a physical read */
71                 ret = regcache_drop_region(arizona->regmap, reg, reg);
72                 if (ret < 0) {
73                         dev_err(chip->parent, "Failed to drop cache: %d\n",
74                                 ret);
75                         pm_runtime_put_autosuspend(chip->parent);
76                         return ret;
77                 }
78
79                 ret = regmap_read(arizona->regmap, reg, &val);
80                 if (ret < 0) {
81                         pm_runtime_put_autosuspend(chip->parent);
82                         return ret;
83                 }
84
85                 pm_runtime_mark_last_busy(chip->parent);
86                 pm_runtime_put_autosuspend(chip->parent);
87         }
88
89         if (val & ARIZONA_GPN_LVL)
90                 return 1;
91         else
92                 return 0;
93 }
94
95 static int arizona_gpio_direction_out(struct gpio_chip *chip,
96                                      unsigned offset, int value)
97 {
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);
101         unsigned int val;
102         int ret;
103
104         ret = regmap_read(arizona->regmap, ARIZONA_GPIO1_CTRL + offset, &val);
105         if (ret < 0)
106                 return ret;
107
108         if ((val & ARIZONA_GPN_DIR) && persistent) {
109                 ret = pm_runtime_get_sync(chip->parent);
110                 if (ret < 0) {
111                         dev_err(chip->parent, "Failed to resume: %d\n", ret);
112                         pm_runtime_put(chip->parent);
113                         return ret;
114                 }
115         }
116
117         if (value)
118                 value = ARIZONA_GPN_LVL;
119
120         return regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
121                                   ARIZONA_GPN_DIR | ARIZONA_GPN_LVL, value);
122 }
123
124 static int arizona_gpio_set(struct gpio_chip *chip, unsigned int offset,
125                             int value)
126 {
127         struct arizona_gpio *arizona_gpio = gpiochip_get_data(chip);
128         struct arizona *arizona = arizona_gpio->arizona;
129
130         if (value)
131                 value = ARIZONA_GPN_LVL;
132
133         return regmap_update_bits(arizona->regmap, ARIZONA_GPIO1_CTRL + offset,
134                                   ARIZONA_GPN_LVL, value);
135 }
136
137 static const struct gpio_chip template_chip = {
138         .label                  = "arizona",
139         .owner                  = THIS_MODULE,
140         .direction_input        = arizona_gpio_direction_in,
141         .get                    = arizona_gpio_get,
142         .direction_output       = arizona_gpio_direction_out,
143         .set_rv                 = arizona_gpio_set,
144         .can_sleep              = true,
145 };
146
147 static int arizona_gpio_probe(struct platform_device *pdev)
148 {
149         struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
150         struct arizona_pdata *pdata = &arizona->pdata;
151         struct arizona_gpio *arizona_gpio;
152         int ret;
153
154         device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
155
156         arizona_gpio = devm_kzalloc(&pdev->dev, sizeof(*arizona_gpio),
157                                     GFP_KERNEL);
158         if (!arizona_gpio)
159                 return -ENOMEM;
160
161         arizona_gpio->arizona = arizona;
162         arizona_gpio->gpio_chip = template_chip;
163         arizona_gpio->gpio_chip.parent = &pdev->dev;
164
165         switch (arizona->type) {
166         case WM5102:
167         case WM5110:
168         case WM8280:
169         case WM8997:
170         case WM8998:
171         case WM1814:
172                 arizona_gpio->gpio_chip.ngpio = 5;
173                 break;
174         case WM1831:
175         case CS47L24:
176                 arizona_gpio->gpio_chip.ngpio = 2;
177                 break;
178         default:
179                 dev_err(&pdev->dev, "Unknown chip variant %d\n",
180                         arizona->type);
181                 return -EINVAL;
182         }
183
184         if (pdata->gpio_base)
185                 arizona_gpio->gpio_chip.base = pdata->gpio_base;
186         else
187                 arizona_gpio->gpio_chip.base = -1;
188
189         pm_runtime_enable(&pdev->dev);
190
191         ret = devm_gpiochip_add_data(&pdev->dev, &arizona_gpio->gpio_chip,
192                                      arizona_gpio);
193         if (ret < 0) {
194                 pm_runtime_disable(&pdev->dev);
195                 dev_err(&pdev->dev, "Could not register gpiochip, %d\n",
196                         ret);
197                 return ret;
198         }
199
200         return 0;
201 }
202
203 static struct platform_driver arizona_gpio_driver = {
204         .driver.name    = "arizona-gpio",
205         .probe          = arizona_gpio_probe,
206 };
207
208 module_platform_driver(arizona_gpio_driver);
209
210 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
211 MODULE_DESCRIPTION("GPIO interface for Arizona devices");
212 MODULE_LICENSE("GPL");
213 MODULE_ALIAS("platform:arizona-gpio");