1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (C) 2018 ROHM Semiconductors
4 #include <linux/gpio/driver.h>
5 #include <linux/mfd/rohm-bd71828.h>
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/regmap.h>
10 #define GPIO_OUT_REG(off) (BD71828_REG_GPIO_CTRL1 + (off))
11 #define HALL_GPIO_OFFSET 3
14 * These defines can be removed when
15 * "gpio: Add definition for GPIO direction"
16 * (9208b1e77d6e8e9776f34f46ef4079ecac9c3c25 in GPIO tree) gets merged,
18 #ifndef GPIO_LINE_DIRECTION_IN
19 #define GPIO_LINE_DIRECTION_IN 1
20 #define GPIO_LINE_DIRECTION_OUT 0
24 struct rohm_regmap_dev chip;
25 struct gpio_chip gpio;
28 static void bd71828_gpio_set(struct gpio_chip *chip, unsigned int offset,
32 struct bd71828_gpio *bdgpio = gpiochip_get_data(chip);
33 u8 val = (value) ? BD71828_GPIO_OUT_HI : BD71828_GPIO_OUT_LO;
36 * The HALL input pin can only be used as input. If this is the pin
37 * we are dealing with - then we are done
39 if (offset == HALL_GPIO_OFFSET)
42 ret = regmap_update_bits(bdgpio->chip.regmap, GPIO_OUT_REG(offset),
43 BD71828_GPIO_OUT_MASK, val);
45 dev_err(bdgpio->chip.dev, "Could not set gpio to %d\n", value);
48 static int bd71828_gpio_get(struct gpio_chip *chip, unsigned int offset)
52 struct bd71828_gpio *bdgpio = gpiochip_get_data(chip);
54 if (offset == HALL_GPIO_OFFSET)
55 ret = regmap_read(bdgpio->chip.regmap, BD71828_REG_IO_STAT,
58 ret = regmap_read(bdgpio->chip.regmap, GPIO_OUT_REG(offset),
61 ret = (val & BD71828_GPIO_OUT_MASK);
66 static int bd71828_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
69 struct bd71828_gpio *bdgpio = gpiochip_get_data(chip);
71 if (offset == HALL_GPIO_OFFSET)
74 switch (pinconf_to_config_param(config)) {
75 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
76 return regmap_update_bits(bdgpio->chip.regmap,
78 BD71828_GPIO_DRIVE_MASK,
79 BD71828_GPIO_OPEN_DRAIN);
80 case PIN_CONFIG_DRIVE_PUSH_PULL:
81 return regmap_update_bits(bdgpio->chip.regmap,
83 BD71828_GPIO_DRIVE_MASK,
84 BD71828_GPIO_PUSH_PULL);
91 static int bd71828_get_direction(struct gpio_chip *chip, unsigned int offset)
94 * Pin usage is selected by OTP data. We can't read it runtime. Hence
95 * we trust that if the pin is not excluded by "gpio-reserved-ranges"
96 * the OTP configuration is set to OUT. (Other pins but HALL input pin
97 * on BD71828 can't really be used for general purpose input - input
98 * states are used for specific cases like regulator control or
101 if (offset == HALL_GPIO_OFFSET)
102 return GPIO_LINE_DIRECTION_IN;
104 return GPIO_LINE_DIRECTION_OUT;
107 static int bd71828_probe(struct platform_device *pdev)
109 struct bd71828_gpio *bdgpio;
110 struct rohm_regmap_dev *bd71828;
112 bd71828 = dev_get_drvdata(pdev->dev.parent);
114 dev_err(&pdev->dev, "No MFD driver data\n");
118 bdgpio = devm_kzalloc(&pdev->dev, sizeof(*bdgpio),
123 bdgpio->chip.dev = &pdev->dev;
124 bdgpio->gpio.parent = pdev->dev.parent;
125 bdgpio->gpio.label = "bd71828-gpio";
126 bdgpio->gpio.owner = THIS_MODULE;
127 bdgpio->gpio.get_direction = bd71828_get_direction;
128 bdgpio->gpio.set_config = bd71828_gpio_set_config;
129 bdgpio->gpio.can_sleep = true;
130 bdgpio->gpio.get = bd71828_gpio_get;
131 bdgpio->gpio.set = bd71828_gpio_set;
132 bdgpio->gpio.base = -1;
135 * See if we need some implementation to mark some PINs as
136 * not controllable based on DT info or if core can handle
137 * "gpio-reserved-ranges" and exclude them from control
139 bdgpio->gpio.ngpio = 4;
140 bdgpio->gpio.of_node = pdev->dev.parent->of_node;
141 bdgpio->chip.regmap = bd71828->regmap;
143 return devm_gpiochip_add_data(&pdev->dev, &bdgpio->gpio,
147 static struct platform_driver bd71828_gpio = {
149 .name = "bd71828-gpio"
151 .probe = bd71828_probe,
154 module_platform_driver(bd71828_gpio);
156 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
157 MODULE_DESCRIPTION("BD71828 voltage regulator driver");
158 MODULE_LICENSE("GPL");
159 MODULE_ALIAS("platform:bd71828-gpio");