xref: /linux/drivers/gpio/gpio-bd71815.c (revision 172cdcaefea5c297fdb3d20b7d5aff60ae4fbce6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support to GPOs on ROHM BD71815
4  * Copyright 2021 ROHM Semiconductors.
5  * Author: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
6  *
7  * Copyright 2014 Embest Technology Co. Ltd. Inc.
8  * Author: yanglsh@embest-tech.com
9  */
10 
11 #include <linux/gpio/driver.h>
12 #include <linux/init.h>
13 #include <linux/irq.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/platform_device.h>
17 /* For the BD71815 register definitions */
18 #include <linux/mfd/rohm-bd71815.h>
19 
20 struct bd71815_gpio {
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 */
24 	struct device *dev;
25 	struct regmap *regmap;
26 };
27 
28 static int bd71815gpo_get(struct gpio_chip *chip, unsigned int offset)
29 {
30 	struct bd71815_gpio *bd71815 = gpiochip_get_data(chip);
31 	int ret, val;
32 
33 	ret = regmap_read(bd71815->regmap, BD71815_REG_GPO, &val);
34 	if (ret)
35 		return ret;
36 
37 	return (val >> offset) & 1;
38 }
39 
40 static void bd71815gpo_set(struct gpio_chip *chip, unsigned int offset,
41 			   int value)
42 {
43 	struct bd71815_gpio *bd71815 = gpiochip_get_data(chip);
44 	int ret, bit;
45 
46 	bit = BIT(offset);
47 
48 	if (value)
49 		ret = regmap_set_bits(bd71815->regmap, BD71815_REG_GPO, bit);
50 	else
51 		ret = regmap_clear_bits(bd71815->regmap, BD71815_REG_GPO, bit);
52 
53 	if (ret)
54 		dev_warn(bd71815->dev, "failed to toggle GPO\n");
55 }
56 
57 static int bd71815_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
58 				   unsigned long config)
59 {
60 	struct bd71815_gpio *bdgpio = gpiochip_get_data(chip);
61 
62 	switch (pinconf_to_config_param(config)) {
63 	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
64 		return regmap_update_bits(bdgpio->regmap,
65 					  BD71815_REG_GPO,
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,
70 					  BD71815_REG_GPO,
71 					  BD71815_GPIO_DRIVE_MASK << offset,
72 					  BD71815_GPIO_CMOS << offset);
73 	default:
74 		break;
75 	}
76 	return -ENOTSUPP;
77 }
78 
79 /* BD71815 GPIO is actually GPO */
80 static int bd71815gpo_direction_get(struct gpio_chip *gc, unsigned int offset)
81 {
82 	return GPIO_LINE_DIRECTION_OUT;
83 }
84 
85 /* Template for GPIO chip */
86 static const struct gpio_chip bd71815gpo_chip = {
87 	.label			= "bd71815",
88 	.owner			= THIS_MODULE,
89 	.get			= bd71815gpo_get,
90 	.get_direction		= bd71815gpo_direction_get,
91 	.set			= bd71815gpo_set,
92 	.set_config		= bd71815_gpio_set_config,
93 	.can_sleep		= true,
94 };
95 
96 #define BD71815_TWO_GPIOS	GENMASK(1, 0)
97 #define BD71815_ONE_GPIO	BIT(0)
98 
99 /*
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
108  * products.
109  *
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".
113  */
114 static int bd71815_init_valid_mask(struct gpio_chip *gc,
115 				   unsigned long *valid_mask,
116 				   unsigned int ngpios)
117 {
118 	if (ngpios != 2)
119 		return 0;
120 
121 	if (gc->parent && device_property_present(gc->parent,
122 						  "rohm,enable-hidden-gpo"))
123 		*valid_mask = BD71815_TWO_GPIOS;
124 	else
125 		*valid_mask = BD71815_ONE_GPIO;
126 
127 	return 0;
128 }
129 
130 static int gpo_bd71815_probe(struct platform_device *pdev)
131 {
132 	struct bd71815_gpio *g;
133 	struct device *parent, *dev;
134 
135 	/*
136 	 * Bind devm lifetime to this platform device => use dev for devm.
137 	 * also the prints should originate from this device.
138 	 */
139 	dev = &pdev->dev;
140 	/* The device-tree and regmap come from MFD => use parent for that */
141 	parent = dev->parent;
142 
143 	g = devm_kzalloc(dev, sizeof(*g), GFP_KERNEL);
144 	if (!g)
145 		return -ENOMEM;
146 
147 	g->chip = bd71815gpo_chip;
148 
149 	/*
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.
153 	 *
154 	 * This check can be removed later if the sysfs export is fixed and
155 	 * if the fix is backported.
156 	 *
157 	 * For now it is safest to just set the ngpios though.
158 	 */
159 	if (device_property_present(parent, "rohm,enable-hidden-gpo"))
160 		g->chip.ngpio = 2;
161 	else
162 		g->chip.ngpio = 1;
163 
164 	g->chip.init_valid_mask = bd71815_init_valid_mask;
165 	g->chip.base = -1;
166 	g->chip.parent = parent;
167 	g->regmap = dev_get_regmap(parent, NULL);
168 	g->dev = dev;
169 
170 	return devm_gpiochip_add_data(dev, &g->chip, g);
171 }
172 
173 static struct platform_driver gpo_bd71815_driver = {
174 	.driver = {
175 		.name	= "bd71815-gpo",
176 	},
177 	.probe		= gpo_bd71815_probe,
178 };
179 module_platform_driver(gpo_bd71815_driver);
180 
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");
186