1 // SPDX-License-Identifier: GPL-2.0
3 * Support to GPOs on ROHM BD71815
4 * Copyright 2021 ROHM Semiconductors.
5 * Author: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
7 * Copyright 2014 Embest Technology Co. Ltd. Inc.
8 * Author: yanglsh@embest-tech.com
11 #include <linux/gpio/driver.h>
12 #include <linux/init.h>
13 #include <linux/irq.h>
14 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 /* For the BD71815 register definitions */
18 #include <linux/mfd/rohm-bd71815.h>
21 /* chip.parent points the MFD which provides DT node and regmap */
22 struct gpio_chip chip;
23 /* dev points to the platform device for devm and prints */
25 struct regmap *regmap;
28 static int bd71815gpo_get(struct gpio_chip *chip, unsigned int offset)
30 struct bd71815_gpio *bd71815 = gpiochip_get_data(chip);
33 ret = regmap_read(bd71815->regmap, BD71815_REG_GPO, &val);
37 return (val >> offset) & 1;
40 static void bd71815gpo_set(struct gpio_chip *chip, unsigned int offset,
43 struct bd71815_gpio *bd71815 = gpiochip_get_data(chip);
49 ret = regmap_set_bits(bd71815->regmap, BD71815_REG_GPO, bit);
51 ret = regmap_clear_bits(bd71815->regmap, BD71815_REG_GPO, bit);
54 dev_warn(bd71815->dev, "failed to toggle GPO\n");
57 static int bd71815_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
60 struct bd71815_gpio *bdgpio = gpiochip_get_data(chip);
62 switch (pinconf_to_config_param(config)) {
63 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
64 return regmap_update_bits(bdgpio->regmap,
66 BD71815_GPIO_DRIVE_MASK << offset,
67 BD71815_GPIO_OPEN_DRAIN << offset);
68 case PIN_CONFIG_DRIVE_PUSH_PULL:
69 return regmap_update_bits(bdgpio->regmap,
71 BD71815_GPIO_DRIVE_MASK << offset,
72 BD71815_GPIO_CMOS << offset);
79 /* BD71815 GPIO is actually GPO */
80 static int bd71815gpo_direction_get(struct gpio_chip *gc, unsigned int offset)
82 return GPIO_LINE_DIRECTION_OUT;
85 /* Template for GPIO chip */
86 static const struct gpio_chip bd71815gpo_chip = {
89 .get = bd71815gpo_get,
90 .get_direction = bd71815gpo_direction_get,
91 .set = bd71815gpo_set,
92 .set_config = bd71815_gpio_set_config,
96 #define BD71815_TWO_GPIOS GENMASK(1, 0)
97 #define BD71815_ONE_GPIO BIT(0)
100 * Sigh. The BD71815 and BD71817 were originally designed to support two GPO
101 * pins. At some point it was noticed the second GPO pin which is the E5 pin
102 * located at the center of IC is hard to use on PCB (due to the location). It
103 * was decided to not promote this second GPO and the pin is marked as GND in
104 * the datasheet. The functionality is still there though! I guess driving a GPO
105 * connected to the ground is a bad idea. Thus we do not support it by default.
106 * OTOH - the original driver written by colleagues at Embest did support
107 * controlling this second GPO. It is thus possible this is used in some of the
110 * This driver does not by default support configuring this second GPO
111 * but allows using it by providing the DT property
112 * "rohm,enable-hidden-gpo".
114 static int bd71815_init_valid_mask(struct gpio_chip *gc,
115 unsigned long *valid_mask,
121 if (gc->parent && device_property_present(gc->parent,
122 "rohm,enable-hidden-gpo"))
123 *valid_mask = BD71815_TWO_GPIOS;
125 *valid_mask = BD71815_ONE_GPIO;
130 static int gpo_bd71815_probe(struct platform_device *pdev)
132 struct bd71815_gpio *g;
133 struct device *parent, *dev;
136 * Bind devm lifetime to this platform device => use dev for devm.
137 * also the prints should originate from this device.
140 /* The device-tree and regmap come from MFD => use parent for that */
141 parent = dev->parent;
143 g = devm_kzalloc(dev, sizeof(*g), GFP_KERNEL);
147 g->chip = bd71815gpo_chip;
150 * FIXME: As writing of this the sysfs interface for GPIO control does
151 * not respect the valid_mask. Do not trust it but rather set the ngpios
152 * to 1 if "rohm,enable-hidden-gpo" is not given.
154 * This check can be removed later if the sysfs export is fixed and
155 * if the fix is backported.
157 * For now it is safest to just set the ngpios though.
159 if (device_property_present(parent, "rohm,enable-hidden-gpo"))
164 g->chip.init_valid_mask = bd71815_init_valid_mask;
166 g->chip.parent = parent;
167 g->regmap = dev_get_regmap(parent, NULL);
170 return devm_gpiochip_add_data(dev, &g->chip, g);
173 static struct platform_driver gpo_bd71815_driver = {
175 .name = "bd71815-gpo",
177 .probe = gpo_bd71815_probe,
179 module_platform_driver(gpo_bd71815_driver);
181 MODULE_ALIAS("platform:bd71815-gpo");
182 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
183 MODULE_AUTHOR("Peter Yang <yanglsh@embest-tech.com>");
184 MODULE_DESCRIPTION("GPO interface for BD71815");
185 MODULE_LICENSE("GPL");