xref: /linux/drivers/gpio/gpio-wm8350.c (revision 66dbe750a96fe10c7270593cbffeb4701cfcd180)
1*66dbe750SLinus Walleij // SPDX-License-Identifier: GPL-2.0+
2c103de24SGrant Likely /*
3c103de24SGrant Likely  * gpiolib support for Wolfson WM835x PMICs
4c103de24SGrant Likely  *
5c103de24SGrant Likely  * Copyright 2009 Wolfson Microelectronics PLC.
6c103de24SGrant Likely  *
7c103de24SGrant Likely  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8c103de24SGrant Likely  *
9c103de24SGrant Likely  */
10c103de24SGrant Likely 
11c103de24SGrant Likely #include <linux/kernel.h>
12c103de24SGrant Likely #include <linux/slab.h>
13c103de24SGrant Likely #include <linux/module.h>
1410833c4bSLinus Walleij #include <linux/gpio/driver.h>
15c103de24SGrant Likely #include <linux/mfd/core.h>
16c103de24SGrant Likely #include <linux/platform_device.h>
17c103de24SGrant Likely #include <linux/seq_file.h>
18c103de24SGrant Likely 
19c103de24SGrant Likely #include <linux/mfd/wm8350/core.h>
20c103de24SGrant Likely #include <linux/mfd/wm8350/gpio.h>
21c103de24SGrant Likely 
22c103de24SGrant Likely struct wm8350_gpio_data {
23c103de24SGrant Likely 	struct wm8350 *wm8350;
24c103de24SGrant Likely 	struct gpio_chip gpio_chip;
25c103de24SGrant Likely };
26c103de24SGrant Likely 
27c103de24SGrant Likely static int wm8350_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
28c103de24SGrant Likely {
29dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
30c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
31c103de24SGrant Likely 
32c103de24SGrant Likely 	return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
33c103de24SGrant Likely 			       1 << offset);
34c103de24SGrant Likely }
35c103de24SGrant Likely 
36c103de24SGrant Likely static int wm8350_gpio_get(struct gpio_chip *chip, unsigned offset)
37c103de24SGrant Likely {
38dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
39c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
40c103de24SGrant Likely 	int ret;
41c103de24SGrant Likely 
42c103de24SGrant Likely 	ret = wm8350_reg_read(wm8350, WM8350_GPIO_LEVEL);
43c103de24SGrant Likely 	if (ret < 0)
44c103de24SGrant Likely 		return ret;
45c103de24SGrant Likely 
46c103de24SGrant Likely 	if (ret & (1 << offset))
47c103de24SGrant Likely 		return 1;
48c103de24SGrant Likely 	else
49c103de24SGrant Likely 		return 0;
50c103de24SGrant Likely }
51c103de24SGrant Likely 
52c103de24SGrant Likely static void wm8350_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
53c103de24SGrant Likely {
54dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
55c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
56c103de24SGrant Likely 
57c103de24SGrant Likely 	if (value)
58c103de24SGrant Likely 		wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
59c103de24SGrant Likely 	else
60c103de24SGrant Likely 		wm8350_clear_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
61c103de24SGrant Likely }
62c103de24SGrant Likely 
63c103de24SGrant Likely static int wm8350_gpio_direction_out(struct gpio_chip *chip,
64c103de24SGrant Likely 				     unsigned offset, int value)
65c103de24SGrant Likely {
66dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
67c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
68c103de24SGrant Likely 	int ret;
69c103de24SGrant Likely 
70c103de24SGrant Likely 	ret = wm8350_clear_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
71c103de24SGrant Likely 				1 << offset);
72c103de24SGrant Likely 	if (ret < 0)
73c103de24SGrant Likely 		return ret;
74c103de24SGrant Likely 
75c103de24SGrant Likely 	/* Don't have an atomic direction/value setup */
76c103de24SGrant Likely 	wm8350_gpio_set(chip, offset, value);
77c103de24SGrant Likely 
78c103de24SGrant Likely 	return 0;
79c103de24SGrant Likely }
80c103de24SGrant Likely 
81c103de24SGrant Likely static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
82c103de24SGrant Likely {
83dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
84c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
85c103de24SGrant Likely 
86c103de24SGrant Likely 	if (!wm8350->irq_base)
87c103de24SGrant Likely 		return -EINVAL;
88c103de24SGrant Likely 
89c103de24SGrant Likely 	return wm8350->irq_base + WM8350_IRQ_GPIO(offset);
90c103de24SGrant Likely }
91c103de24SGrant Likely 
92e35b5ab0SJulia Lawall static const struct gpio_chip template_chip = {
93c103de24SGrant Likely 	.label			= "wm8350",
94c103de24SGrant Likely 	.owner			= THIS_MODULE,
95c103de24SGrant Likely 	.direction_input	= wm8350_gpio_direction_in,
96c103de24SGrant Likely 	.get			= wm8350_gpio_get,
97c103de24SGrant Likely 	.direction_output	= wm8350_gpio_direction_out,
98c103de24SGrant Likely 	.set			= wm8350_gpio_set,
99c103de24SGrant Likely 	.to_irq			= wm8350_gpio_to_irq,
1009fb1f39eSLinus Walleij 	.can_sleep		= true,
101c103de24SGrant Likely };
102c103de24SGrant Likely 
1033836309dSBill Pemberton static int wm8350_gpio_probe(struct platform_device *pdev)
104c103de24SGrant Likely {
105c103de24SGrant Likely 	struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent);
106e56aee18SJingoo Han 	struct wm8350_platform_data *pdata = dev_get_platdata(wm8350->dev);
107c103de24SGrant Likely 	struct wm8350_gpio_data *wm8350_gpio;
108c103de24SGrant Likely 	int ret;
109c103de24SGrant Likely 
1105b425438SAxel Lin 	wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio),
1115b425438SAxel Lin 				   GFP_KERNEL);
112c103de24SGrant Likely 	if (wm8350_gpio == NULL)
113c103de24SGrant Likely 		return -ENOMEM;
114c103de24SGrant Likely 
115c103de24SGrant Likely 	wm8350_gpio->wm8350 = wm8350;
116c103de24SGrant Likely 	wm8350_gpio->gpio_chip = template_chip;
117c103de24SGrant Likely 	wm8350_gpio->gpio_chip.ngpio = 13;
11858383c78SLinus Walleij 	wm8350_gpio->gpio_chip.parent = &pdev->dev;
119c103de24SGrant Likely 	if (pdata && pdata->gpio_base)
120c103de24SGrant Likely 		wm8350_gpio->gpio_chip.base = pdata->gpio_base;
121c103de24SGrant Likely 	else
122c103de24SGrant Likely 		wm8350_gpio->gpio_chip.base = -1;
123c103de24SGrant Likely 
1244a4925cfSLaxman Dewangan 	ret = devm_gpiochip_add_data(&pdev->dev, &wm8350_gpio->gpio_chip,
1254a4925cfSLaxman Dewangan 				     wm8350_gpio);
126c103de24SGrant Likely 	if (ret < 0) {
1275b425438SAxel Lin 		dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
1285b425438SAxel Lin 		return ret;
129c103de24SGrant Likely 	}
130c103de24SGrant Likely 
131c103de24SGrant Likely 	platform_set_drvdata(pdev, wm8350_gpio);
132c103de24SGrant Likely 
133c103de24SGrant Likely 	return ret;
134c103de24SGrant Likely }
135c103de24SGrant Likely 
136c103de24SGrant Likely static struct platform_driver wm8350_gpio_driver = {
137c103de24SGrant Likely 	.driver.name	= "wm8350-gpio",
138c103de24SGrant Likely 	.probe		= wm8350_gpio_probe,
139c103de24SGrant Likely };
140c103de24SGrant Likely 
141c103de24SGrant Likely static int __init wm8350_gpio_init(void)
142c103de24SGrant Likely {
143c103de24SGrant Likely 	return platform_driver_register(&wm8350_gpio_driver);
144c103de24SGrant Likely }
145c103de24SGrant Likely subsys_initcall(wm8350_gpio_init);
146c103de24SGrant Likely 
147c103de24SGrant Likely static void __exit wm8350_gpio_exit(void)
148c103de24SGrant Likely {
149c103de24SGrant Likely 	platform_driver_unregister(&wm8350_gpio_driver);
150c103de24SGrant Likely }
151c103de24SGrant Likely module_exit(wm8350_gpio_exit);
152c103de24SGrant Likely 
153c103de24SGrant Likely MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
154c103de24SGrant Likely MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs");
155c103de24SGrant Likely MODULE_LICENSE("GPL");
156c103de24SGrant Likely MODULE_ALIAS("platform:wm8350-gpio");
157