xref: /linux/drivers/gpio/gpio-madera.c (revision e42615ec)
1690499d9SCharles Keepax // SPDX-License-Identifier: GPL-2.0-only
2aca429ffSRichard Fitzgerald /*
3aca429ffSRichard Fitzgerald  * GPIO support for Cirrus Logic Madera codecs
4aca429ffSRichard Fitzgerald  *
5aca429ffSRichard Fitzgerald  * Copyright (C) 2015-2018 Cirrus Logic
6aca429ffSRichard Fitzgerald  */
7aca429ffSRichard Fitzgerald 
8aca429ffSRichard Fitzgerald #include <linux/gpio/driver.h>
9aca429ffSRichard Fitzgerald #include <linux/kernel.h>
10aca429ffSRichard Fitzgerald #include <linux/module.h>
11aca429ffSRichard Fitzgerald #include <linux/platform_device.h>
12aca429ffSRichard Fitzgerald 
13aca429ffSRichard Fitzgerald #include <linux/mfd/madera/core.h>
14aca429ffSRichard Fitzgerald #include <linux/mfd/madera/pdata.h>
15aca429ffSRichard Fitzgerald #include <linux/mfd/madera/registers.h>
16aca429ffSRichard Fitzgerald 
17aca429ffSRichard Fitzgerald struct madera_gpio {
18aca429ffSRichard Fitzgerald 	struct madera *madera;
19aca429ffSRichard Fitzgerald 	/* storage space for the gpio_chip we're using */
20aca429ffSRichard Fitzgerald 	struct gpio_chip gpio_chip;
21aca429ffSRichard Fitzgerald };
22aca429ffSRichard Fitzgerald 
madera_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)23aca429ffSRichard Fitzgerald static int madera_gpio_get_direction(struct gpio_chip *chip,
24aca429ffSRichard Fitzgerald 				     unsigned int offset)
25aca429ffSRichard Fitzgerald {
26aca429ffSRichard Fitzgerald 	struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
27aca429ffSRichard Fitzgerald 	struct madera *madera = madera_gpio->madera;
28aca429ffSRichard Fitzgerald 	unsigned int reg_offset = 2 * offset;
29aca429ffSRichard Fitzgerald 	unsigned int val;
30aca429ffSRichard Fitzgerald 	int ret;
31aca429ffSRichard Fitzgerald 
32aca429ffSRichard Fitzgerald 	ret = regmap_read(madera->regmap, MADERA_GPIO1_CTRL_2 + reg_offset,
33aca429ffSRichard Fitzgerald 			  &val);
34aca429ffSRichard Fitzgerald 	if (ret < 0)
35aca429ffSRichard Fitzgerald 		return ret;
36aca429ffSRichard Fitzgerald 
37*e42615ecSMatti Vaittinen 	if (val & MADERA_GP1_DIR_MASK)
38*e42615ecSMatti Vaittinen 		return GPIO_LINE_DIRECTION_IN;
39*e42615ecSMatti Vaittinen 
40*e42615ecSMatti Vaittinen 	return GPIO_LINE_DIRECTION_OUT;
41aca429ffSRichard Fitzgerald }
42aca429ffSRichard Fitzgerald 
madera_gpio_direction_in(struct gpio_chip * chip,unsigned int offset)43aca429ffSRichard Fitzgerald static int madera_gpio_direction_in(struct gpio_chip *chip, unsigned int offset)
44aca429ffSRichard Fitzgerald {
45aca429ffSRichard Fitzgerald 	struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
46aca429ffSRichard Fitzgerald 	struct madera *madera = madera_gpio->madera;
47aca429ffSRichard Fitzgerald 	unsigned int reg_offset = 2 * offset;
48aca429ffSRichard Fitzgerald 
49aca429ffSRichard Fitzgerald 	return regmap_update_bits(madera->regmap,
50aca429ffSRichard Fitzgerald 				  MADERA_GPIO1_CTRL_2 + reg_offset,
51aca429ffSRichard Fitzgerald 				  MADERA_GP1_DIR_MASK, MADERA_GP1_DIR);
52aca429ffSRichard Fitzgerald }
53aca429ffSRichard Fitzgerald 
madera_gpio_get(struct gpio_chip * chip,unsigned int offset)54aca429ffSRichard Fitzgerald static int madera_gpio_get(struct gpio_chip *chip, unsigned int offset)
55aca429ffSRichard Fitzgerald {
56aca429ffSRichard Fitzgerald 	struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
57aca429ffSRichard Fitzgerald 	struct madera *madera = madera_gpio->madera;
58aca429ffSRichard Fitzgerald 	unsigned int reg_offset = 2 * offset;
59aca429ffSRichard Fitzgerald 	unsigned int val;
60aca429ffSRichard Fitzgerald 	int ret;
61aca429ffSRichard Fitzgerald 
62aca429ffSRichard Fitzgerald 	ret = regmap_read(madera->regmap, MADERA_GPIO1_CTRL_1 + reg_offset,
63aca429ffSRichard Fitzgerald 			  &val);
64aca429ffSRichard Fitzgerald 	if (ret < 0)
65aca429ffSRichard Fitzgerald 		return ret;
66aca429ffSRichard Fitzgerald 
67aca429ffSRichard Fitzgerald 	return !!(val & MADERA_GP1_LVL_MASK);
68aca429ffSRichard Fitzgerald }
69aca429ffSRichard Fitzgerald 
madera_gpio_direction_out(struct gpio_chip * chip,unsigned int offset,int value)70aca429ffSRichard Fitzgerald static int madera_gpio_direction_out(struct gpio_chip *chip,
71aca429ffSRichard Fitzgerald 				     unsigned int offset, int value)
72aca429ffSRichard Fitzgerald {
73aca429ffSRichard Fitzgerald 	struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
74aca429ffSRichard Fitzgerald 	struct madera *madera = madera_gpio->madera;
75aca429ffSRichard Fitzgerald 	unsigned int reg_offset = 2 * offset;
76aca429ffSRichard Fitzgerald 	unsigned int reg_val = value ? MADERA_GP1_LVL : 0;
77aca429ffSRichard Fitzgerald 	int ret;
78aca429ffSRichard Fitzgerald 
79aca429ffSRichard Fitzgerald 	ret = regmap_update_bits(madera->regmap,
80aca429ffSRichard Fitzgerald 				 MADERA_GPIO1_CTRL_2 + reg_offset,
81aca429ffSRichard Fitzgerald 				 MADERA_GP1_DIR_MASK, 0);
82aca429ffSRichard Fitzgerald 	if (ret < 0)
83aca429ffSRichard Fitzgerald 		return ret;
84aca429ffSRichard Fitzgerald 
85aca429ffSRichard Fitzgerald 	return regmap_update_bits(madera->regmap,
86aca429ffSRichard Fitzgerald 				  MADERA_GPIO1_CTRL_1 + reg_offset,
87aca429ffSRichard Fitzgerald 				  MADERA_GP1_LVL_MASK, reg_val);
88aca429ffSRichard Fitzgerald }
89aca429ffSRichard Fitzgerald 
madera_gpio_set(struct gpio_chip * chip,unsigned int offset,int value)90aca429ffSRichard Fitzgerald static void madera_gpio_set(struct gpio_chip *chip, unsigned int offset,
91aca429ffSRichard Fitzgerald 			    int value)
92aca429ffSRichard Fitzgerald {
93aca429ffSRichard Fitzgerald 	struct madera_gpio *madera_gpio = gpiochip_get_data(chip);
94aca429ffSRichard Fitzgerald 	struct madera *madera = madera_gpio->madera;
95aca429ffSRichard Fitzgerald 	unsigned int reg_offset = 2 * offset;
96aca429ffSRichard Fitzgerald 	unsigned int reg_val = value ? MADERA_GP1_LVL : 0;
97aca429ffSRichard Fitzgerald 	int ret;
98aca429ffSRichard Fitzgerald 
99aca429ffSRichard Fitzgerald 	ret = regmap_update_bits(madera->regmap,
100aca429ffSRichard Fitzgerald 				 MADERA_GPIO1_CTRL_1 + reg_offset,
101aca429ffSRichard Fitzgerald 				 MADERA_GP1_LVL_MASK, reg_val);
102aca429ffSRichard Fitzgerald 
103aca429ffSRichard Fitzgerald 	/* set() doesn't return an error so log a warning */
104aca429ffSRichard Fitzgerald 	if (ret)
105aca429ffSRichard Fitzgerald 		dev_warn(madera->dev, "Failed to write to 0x%x (%d)\n",
106aca429ffSRichard Fitzgerald 			 MADERA_GPIO1_CTRL_1 + reg_offset, ret);
107aca429ffSRichard Fitzgerald }
108aca429ffSRichard Fitzgerald 
1090faf40e3SCharles Keepax static const struct gpio_chip madera_gpio_chip = {
110aca429ffSRichard Fitzgerald 	.label			= "madera",
111aca429ffSRichard Fitzgerald 	.owner			= THIS_MODULE,
112aca429ffSRichard Fitzgerald 	.request		= gpiochip_generic_request,
113aca429ffSRichard Fitzgerald 	.free			= gpiochip_generic_free,
114aca429ffSRichard Fitzgerald 	.get_direction		= madera_gpio_get_direction,
115aca429ffSRichard Fitzgerald 	.direction_input	= madera_gpio_direction_in,
116aca429ffSRichard Fitzgerald 	.get			= madera_gpio_get,
117aca429ffSRichard Fitzgerald 	.direction_output	= madera_gpio_direction_out,
118aca429ffSRichard Fitzgerald 	.set			= madera_gpio_set,
119aca429ffSRichard Fitzgerald 	.set_config		= gpiochip_generic_config,
120aca429ffSRichard Fitzgerald 	.can_sleep		= true,
121aca429ffSRichard Fitzgerald };
122aca429ffSRichard Fitzgerald 
madera_gpio_probe(struct platform_device * pdev)123aca429ffSRichard Fitzgerald static int madera_gpio_probe(struct platform_device *pdev)
124aca429ffSRichard Fitzgerald {
125aca429ffSRichard Fitzgerald 	struct madera *madera = dev_get_drvdata(pdev->dev.parent);
126fe2afa0dSCharles Keepax 	struct madera_pdata *pdata = &madera->pdata;
127aca429ffSRichard Fitzgerald 	struct madera_gpio *madera_gpio;
128aca429ffSRichard Fitzgerald 	int ret;
129aca429ffSRichard Fitzgerald 
130aca429ffSRichard Fitzgerald 	madera_gpio = devm_kzalloc(&pdev->dev, sizeof(*madera_gpio),
131aca429ffSRichard Fitzgerald 				   GFP_KERNEL);
132aca429ffSRichard Fitzgerald 	if (!madera_gpio)
133aca429ffSRichard Fitzgerald 		return -ENOMEM;
134aca429ffSRichard Fitzgerald 
135aca429ffSRichard Fitzgerald 	madera_gpio->madera = madera;
136aca429ffSRichard Fitzgerald 
137aca429ffSRichard Fitzgerald 	/* Construct suitable gpio_chip from the template in madera_gpio_chip */
138aca429ffSRichard Fitzgerald 	madera_gpio->gpio_chip = madera_gpio_chip;
139aca429ffSRichard Fitzgerald 	madera_gpio->gpio_chip.parent = pdev->dev.parent;
140aca429ffSRichard Fitzgerald 
141aca429ffSRichard Fitzgerald 	switch (madera->type) {
142d06be8bcSRichard Fitzgerald 	case CS47L15:
143d06be8bcSRichard Fitzgerald 		madera_gpio->gpio_chip.ngpio = CS47L15_NUM_GPIOS;
144d06be8bcSRichard Fitzgerald 		break;
145aca429ffSRichard Fitzgerald 	case CS47L35:
146aca429ffSRichard Fitzgerald 		madera_gpio->gpio_chip.ngpio = CS47L35_NUM_GPIOS;
147aca429ffSRichard Fitzgerald 		break;
148aca429ffSRichard Fitzgerald 	case CS47L85:
149aca429ffSRichard Fitzgerald 	case WM1840:
150aca429ffSRichard Fitzgerald 		madera_gpio->gpio_chip.ngpio = CS47L85_NUM_GPIOS;
151aca429ffSRichard Fitzgerald 		break;
152aca429ffSRichard Fitzgerald 	case CS47L90:
153aca429ffSRichard Fitzgerald 	case CS47L91:
154aca429ffSRichard Fitzgerald 		madera_gpio->gpio_chip.ngpio = CS47L90_NUM_GPIOS;
155aca429ffSRichard Fitzgerald 		break;
15674d2d0e6SCharles Keepax 	case CS42L92:
15774d2d0e6SCharles Keepax 	case CS47L92:
15874d2d0e6SCharles Keepax 	case CS47L93:
15974d2d0e6SCharles Keepax 		madera_gpio->gpio_chip.ngpio = CS47L92_NUM_GPIOS;
16074d2d0e6SCharles Keepax 		break;
161aca429ffSRichard Fitzgerald 	default:
162aca429ffSRichard Fitzgerald 		dev_err(&pdev->dev, "Unknown chip variant %d\n", madera->type);
163aca429ffSRichard Fitzgerald 		return -EINVAL;
164aca429ffSRichard Fitzgerald 	}
165aca429ffSRichard Fitzgerald 
166aca429ffSRichard Fitzgerald 	/* We want to be usable on systems that don't use devicetree or acpi */
167fe2afa0dSCharles Keepax 	if (pdata->gpio_base)
168aca429ffSRichard Fitzgerald 		madera_gpio->gpio_chip.base = pdata->gpio_base;
169aca429ffSRichard Fitzgerald 	else
170aca429ffSRichard Fitzgerald 		madera_gpio->gpio_chip.base = -1;
171aca429ffSRichard Fitzgerald 
172aca429ffSRichard Fitzgerald 	ret = devm_gpiochip_add_data(&pdev->dev,
173aca429ffSRichard Fitzgerald 				     &madera_gpio->gpio_chip,
174aca429ffSRichard Fitzgerald 				     madera_gpio);
175aca429ffSRichard Fitzgerald 	if (ret < 0) {
176aca429ffSRichard Fitzgerald 		dev_dbg(&pdev->dev, "Could not register gpiochip, %d\n", ret);
177aca429ffSRichard Fitzgerald 		return ret;
178aca429ffSRichard Fitzgerald 	}
179aca429ffSRichard Fitzgerald 
180aca429ffSRichard Fitzgerald 	/*
181aca429ffSRichard Fitzgerald 	 * This is part of a composite MFD device which can only be used with
182aca429ffSRichard Fitzgerald 	 * the corresponding pinctrl driver. On all supported silicon the GPIO
183aca429ffSRichard Fitzgerald 	 * to pinctrl mapping is fixed in the silicon, so we register it
184aca429ffSRichard Fitzgerald 	 * explicitly instead of requiring a redundant gpio-ranges in the
185aca429ffSRichard Fitzgerald 	 * devicetree.
186aca429ffSRichard Fitzgerald 	 * In any case we also want to work on systems that don't use devicetree
187aca429ffSRichard Fitzgerald 	 * or acpi.
188aca429ffSRichard Fitzgerald 	 */
189aca429ffSRichard Fitzgerald 	ret = gpiochip_add_pin_range(&madera_gpio->gpio_chip, "madera-pinctrl",
190aca429ffSRichard Fitzgerald 				     0, 0, madera_gpio->gpio_chip.ngpio);
191aca429ffSRichard Fitzgerald 	if (ret) {
192aca429ffSRichard Fitzgerald 		dev_dbg(&pdev->dev, "Failed to add pin range (%d)\n", ret);
193aca429ffSRichard Fitzgerald 		return ret;
194aca429ffSRichard Fitzgerald 	}
195aca429ffSRichard Fitzgerald 
196aca429ffSRichard Fitzgerald 	return 0;
197aca429ffSRichard Fitzgerald }
198aca429ffSRichard Fitzgerald 
199aca429ffSRichard Fitzgerald static struct platform_driver madera_gpio_driver = {
200aca429ffSRichard Fitzgerald 	.driver = {
201aca429ffSRichard Fitzgerald 		.name	= "madera-gpio",
202aca429ffSRichard Fitzgerald 	},
203aca429ffSRichard Fitzgerald 	.probe		= madera_gpio_probe,
204aca429ffSRichard Fitzgerald };
205aca429ffSRichard Fitzgerald 
206aca429ffSRichard Fitzgerald module_platform_driver(madera_gpio_driver);
207aca429ffSRichard Fitzgerald 
208aca429ffSRichard Fitzgerald MODULE_SOFTDEP("pre: pinctrl-madera");
209aca429ffSRichard Fitzgerald MODULE_DESCRIPTION("GPIO interface for Madera codecs");
210aca429ffSRichard Fitzgerald MODULE_AUTHOR("Nariman Poushin <nariman@opensource.cirrus.com>");
211aca429ffSRichard Fitzgerald MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
212aca429ffSRichard Fitzgerald MODULE_LICENSE("GPL v2");
213aca429ffSRichard Fitzgerald MODULE_ALIAS("platform:madera-gpio");
214