1 // SPDX-License-Identifier: GPL-2.0
3 * Dialog DA9062 pinctrl and GPIO driver.
4 * Based on DA9055 GPIO driver.
7 * - add pinmux and pinctrl support (gpio alternate mode)
10 * [1] https://www.dialog-semiconductor.com/sites/default/files/da9062_datasheet_3v6.pdf
12 * Copyright (C) 2019 Pengutronix, Marco Felsch <kernel@pengutronix.de>
14 #include <linux/bits.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/regmap.h>
20 #include <linux/gpio/driver.h>
22 #include <linux/mfd/da9062/core.h>
23 #include <linux/mfd/da9062/registers.h>
26 * We need this get the gpio_desc from a <gpio_chip,offset> tuple to decide if
27 * the gpio is active low without a vendor specific dt-binding.
29 #include "../gpio/gpiolib.h"
31 #define DA9062_TYPE(offset) (4 * (offset % 2))
32 #define DA9062_PIN_SHIFT(offset) (4 * (offset % 2))
33 #define DA9062_PIN_ALTERNATE 0x00 /* gpio alternate mode */
34 #define DA9062_PIN_GPI 0x01 /* gpio in */
35 #define DA9062_PIN_GPO_OD 0x02 /* gpio out open-drain */
36 #define DA9062_PIN_GPO_PP 0x03 /* gpio out push-pull */
37 #define DA9062_GPIO_NUM 5
40 struct da9062 *da9062;
42 unsigned int pin_config[DA9062_GPIO_NUM];
45 static int da9062_pctl_get_pin_mode(struct da9062_pctl *pctl,
48 struct regmap *regmap = pctl->da9062->regmap;
51 ret = regmap_read(regmap, DA9062AA_GPIO_0_1 + (offset >> 1), &val);
55 val >>= DA9062_PIN_SHIFT(offset);
56 val &= DA9062AA_GPIO0_PIN_MASK;
61 static int da9062_pctl_set_pin_mode(struct da9062_pctl *pctl,
62 unsigned int offset, unsigned int mode_req)
64 struct regmap *regmap = pctl->da9062->regmap;
65 unsigned int mode = mode_req;
69 mode &= DA9062AA_GPIO0_PIN_MASK;
70 mode <<= DA9062_PIN_SHIFT(offset);
71 mask = DA9062AA_GPIO0_PIN_MASK << DA9062_PIN_SHIFT(offset);
73 ret = regmap_update_bits(regmap, DA9062AA_GPIO_0_1 + (offset >> 1),
76 pctl->pin_config[offset] = mode_req;
81 static int da9062_gpio_get(struct gpio_chip *gc, unsigned int offset)
83 struct da9062_pctl *pctl = gpiochip_get_data(gc);
84 struct regmap *regmap = pctl->da9062->regmap;
88 gpio_mode = da9062_pctl_get_pin_mode(pctl, offset);
93 case DA9062_PIN_ALTERNATE:
96 ret = regmap_read(regmap, DA9062AA_STATUS_B, &val);
100 case DA9062_PIN_GPO_OD:
101 case DA9062_PIN_GPO_PP:
102 ret = regmap_read(regmap, DA9062AA_GPIO_MODE0_4, &val);
107 return !!(val & BIT(offset));
110 static void da9062_gpio_set(struct gpio_chip *gc, unsigned int offset,
113 struct da9062_pctl *pctl = gpiochip_get_data(gc);
114 struct regmap *regmap = pctl->da9062->regmap;
116 regmap_update_bits(regmap, DA9062AA_GPIO_MODE0_4, BIT(offset),
120 static int da9062_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
122 struct da9062_pctl *pctl = gpiochip_get_data(gc);
125 gpio_mode = da9062_pctl_get_pin_mode(pctl, offset);
130 case DA9062_PIN_ALTERNATE:
133 return GPIO_LINE_DIRECTION_IN;
134 case DA9062_PIN_GPO_OD:
135 case DA9062_PIN_GPO_PP:
136 return GPIO_LINE_DIRECTION_OUT;
142 static int da9062_gpio_direction_input(struct gpio_chip *gc,
145 struct da9062_pctl *pctl = gpiochip_get_data(gc);
146 struct regmap *regmap = pctl->da9062->regmap;
147 struct gpio_desc *desc = gpiochip_get_desc(gc, offset);
148 unsigned int gpi_type;
151 ret = da9062_pctl_set_pin_mode(pctl, offset, DA9062_PIN_GPI);
156 * If the gpio is active low we should set it in hw too. No worries
157 * about gpio_get() because we read and return the gpio-level. So the
158 * gpiolib active_low handling is still correct.
160 * 0 - active low, 1 - active high
162 gpi_type = !gpiod_is_active_low(desc);
164 return regmap_update_bits(regmap, DA9062AA_GPIO_0_1 + (offset >> 1),
165 DA9062AA_GPIO0_TYPE_MASK << DA9062_TYPE(offset),
166 gpi_type << DA9062_TYPE(offset));
169 static int da9062_gpio_direction_output(struct gpio_chip *gc,
170 unsigned int offset, int value)
172 struct da9062_pctl *pctl = gpiochip_get_data(gc);
173 unsigned int pin_config = pctl->pin_config[offset];
176 ret = da9062_pctl_set_pin_mode(pctl, offset, pin_config);
180 da9062_gpio_set(gc, offset, value);
185 static int da9062_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
186 unsigned long config)
188 struct da9062_pctl *pctl = gpiochip_get_data(gc);
189 struct regmap *regmap = pctl->da9062->regmap;
193 * We need to meet the following restrictions [1, Figure 18]:
194 * - PIN_CONFIG_BIAS_PULL_DOWN -> only allowed if the pin is used as
196 * - PIN_CONFIG_BIAS_PULL_UP -> only allowed if the pin is used as
197 * gpio output open-drain.
200 switch (pinconf_to_config_param(config)) {
201 case PIN_CONFIG_BIAS_DISABLE:
202 return regmap_update_bits(regmap, DA9062AA_CONFIG_K,
204 case PIN_CONFIG_BIAS_PULL_DOWN:
205 gpio_mode = da9062_pctl_get_pin_mode(pctl, offset);
208 else if (gpio_mode != DA9062_PIN_GPI)
210 return regmap_update_bits(regmap, DA9062AA_CONFIG_K,
211 BIT(offset), BIT(offset));
212 case PIN_CONFIG_BIAS_PULL_UP:
213 gpio_mode = da9062_pctl_get_pin_mode(pctl, offset);
216 else if (gpio_mode != DA9062_PIN_GPO_OD)
218 return regmap_update_bits(regmap, DA9062AA_CONFIG_K,
219 BIT(offset), BIT(offset));
220 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
221 return da9062_pctl_set_pin_mode(pctl, offset,
223 case PIN_CONFIG_DRIVE_PUSH_PULL:
224 return da9062_pctl_set_pin_mode(pctl, offset,
231 static int da9062_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
233 struct da9062_pctl *pctl = gpiochip_get_data(gc);
234 struct da9062 *da9062 = pctl->da9062;
236 return regmap_irq_get_virq(da9062->regmap_irq,
237 DA9062_IRQ_GPI0 + offset);
240 static const struct gpio_chip reference_gc = {
241 .owner = THIS_MODULE,
242 .get = da9062_gpio_get,
243 .set = da9062_gpio_set,
244 .get_direction = da9062_gpio_get_direction,
245 .direction_input = da9062_gpio_direction_input,
246 .direction_output = da9062_gpio_direction_output,
247 .set_config = da9062_gpio_set_config,
248 .to_irq = da9062_gpio_to_irq,
250 .ngpio = DA9062_GPIO_NUM,
254 static int da9062_pctl_probe(struct platform_device *pdev)
256 struct device *parent = pdev->dev.parent;
257 struct da9062_pctl *pctl;
260 device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
262 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
266 pctl->da9062 = dev_get_drvdata(parent);
270 if (!device_property_present(parent, "gpio-controller"))
273 for (i = 0; i < ARRAY_SIZE(pctl->pin_config); i++)
274 pctl->pin_config[i] = DA9062_PIN_GPO_PP;
277 * Currently the driver handles only the GPIO support. The
278 * pinctrl/pinmux support can be added later if needed.
280 pctl->gc = reference_gc;
281 pctl->gc.label = dev_name(&pdev->dev);
282 pctl->gc.parent = &pdev->dev;
284 platform_set_drvdata(pdev, pctl);
286 return devm_gpiochip_add_data(&pdev->dev, &pctl->gc, pctl);
289 static struct platform_driver da9062_pctl_driver = {
290 .probe = da9062_pctl_probe,
292 .name = "da9062-gpio",
295 module_platform_driver(da9062_pctl_driver);
297 MODULE_AUTHOR("Marco Felsch <kernel@pengutronix.de>");
298 MODULE_DESCRIPTION("DA9062 PMIC pinctrl and GPIO Driver");
299 MODULE_LICENSE("GPL v2");
300 MODULE_ALIAS("platform:da9062-gpio");