xref: /linux/drivers/gpio/gpio-wm8350.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
166dbe750SLinus 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 
1110833c4bSLinus Walleij #include <linux/gpio/driver.h>
12*275d1356SAndy Shevchenko #include <linux/kernel.h>
13c103de24SGrant Likely #include <linux/mfd/core.h>
14*275d1356SAndy Shevchenko #include <linux/module.h>
15c103de24SGrant Likely #include <linux/platform_device.h>
16*275d1356SAndy Shevchenko #include <linux/slab.h>
17c103de24SGrant Likely 
18c103de24SGrant Likely #include <linux/mfd/wm8350/core.h>
19c103de24SGrant Likely #include <linux/mfd/wm8350/gpio.h>
20c103de24SGrant Likely 
21c103de24SGrant Likely struct wm8350_gpio_data {
22c103de24SGrant Likely 	struct wm8350 *wm8350;
23c103de24SGrant Likely 	struct gpio_chip gpio_chip;
24c103de24SGrant Likely };
25c103de24SGrant Likely 
wm8350_gpio_direction_in(struct gpio_chip * chip,unsigned offset)26c103de24SGrant Likely static int wm8350_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
27c103de24SGrant Likely {
28dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
29c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
30c103de24SGrant Likely 
31c103de24SGrant Likely 	return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
32c103de24SGrant Likely 			       1 << offset);
33c103de24SGrant Likely }
34c103de24SGrant Likely 
wm8350_gpio_get(struct gpio_chip * chip,unsigned offset)35c103de24SGrant Likely static int wm8350_gpio_get(struct gpio_chip *chip, unsigned offset)
36c103de24SGrant Likely {
37dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
38c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
39c103de24SGrant Likely 	int ret;
40c103de24SGrant Likely 
41c103de24SGrant Likely 	ret = wm8350_reg_read(wm8350, WM8350_GPIO_LEVEL);
42c103de24SGrant Likely 	if (ret < 0)
43c103de24SGrant Likely 		return ret;
44c103de24SGrant Likely 
45c103de24SGrant Likely 	if (ret & (1 << offset))
46c103de24SGrant Likely 		return 1;
47c103de24SGrant Likely 	else
48c103de24SGrant Likely 		return 0;
49c103de24SGrant Likely }
50c103de24SGrant Likely 
wm8350_gpio_set(struct gpio_chip * chip,unsigned offset,int value)51c103de24SGrant Likely static void wm8350_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
52c103de24SGrant Likely {
53dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
54c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
55c103de24SGrant Likely 
56c103de24SGrant Likely 	if (value)
57c103de24SGrant Likely 		wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
58c103de24SGrant Likely 	else
59c103de24SGrant Likely 		wm8350_clear_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
60c103de24SGrant Likely }
61c103de24SGrant Likely 
wm8350_gpio_direction_out(struct gpio_chip * chip,unsigned offset,int value)62c103de24SGrant Likely static int wm8350_gpio_direction_out(struct gpio_chip *chip,
63c103de24SGrant Likely 				     unsigned offset, int value)
64c103de24SGrant Likely {
65dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
66c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
67c103de24SGrant Likely 	int ret;
68c103de24SGrant Likely 
69c103de24SGrant Likely 	ret = wm8350_clear_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
70c103de24SGrant Likely 				1 << offset);
71c103de24SGrant Likely 	if (ret < 0)
72c103de24SGrant Likely 		return ret;
73c103de24SGrant Likely 
74c103de24SGrant Likely 	/* Don't have an atomic direction/value setup */
75c103de24SGrant Likely 	wm8350_gpio_set(chip, offset, value);
76c103de24SGrant Likely 
77c103de24SGrant Likely 	return 0;
78c103de24SGrant Likely }
79c103de24SGrant Likely 
wm8350_gpio_to_irq(struct gpio_chip * chip,unsigned offset)80c103de24SGrant Likely static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
81c103de24SGrant Likely {
82dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
83c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
84c103de24SGrant Likely 
85c103de24SGrant Likely 	if (!wm8350->irq_base)
86c103de24SGrant Likely 		return -EINVAL;
87c103de24SGrant Likely 
88c103de24SGrant Likely 	return wm8350->irq_base + WM8350_IRQ_GPIO(offset);
89c103de24SGrant Likely }
90c103de24SGrant Likely 
91e35b5ab0SJulia Lawall static const struct gpio_chip template_chip = {
92c103de24SGrant Likely 	.label			= "wm8350",
93c103de24SGrant Likely 	.owner			= THIS_MODULE,
94c103de24SGrant Likely 	.direction_input	= wm8350_gpio_direction_in,
95c103de24SGrant Likely 	.get			= wm8350_gpio_get,
96c103de24SGrant Likely 	.direction_output	= wm8350_gpio_direction_out,
97c103de24SGrant Likely 	.set			= wm8350_gpio_set,
98c103de24SGrant Likely 	.to_irq			= wm8350_gpio_to_irq,
999fb1f39eSLinus Walleij 	.can_sleep		= true,
100c103de24SGrant Likely };
101c103de24SGrant Likely 
wm8350_gpio_probe(struct platform_device * pdev)1023836309dSBill Pemberton static int wm8350_gpio_probe(struct platform_device *pdev)
103c103de24SGrant Likely {
104c103de24SGrant Likely 	struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent);
105e56aee18SJingoo Han 	struct wm8350_platform_data *pdata = dev_get_platdata(wm8350->dev);
106c103de24SGrant Likely 	struct wm8350_gpio_data *wm8350_gpio;
107c103de24SGrant Likely 
1085b425438SAxel Lin 	wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio),
1095b425438SAxel Lin 				   GFP_KERNEL);
110c103de24SGrant Likely 	if (wm8350_gpio == NULL)
111c103de24SGrant Likely 		return -ENOMEM;
112c103de24SGrant Likely 
113c103de24SGrant Likely 	wm8350_gpio->wm8350 = wm8350;
114c103de24SGrant Likely 	wm8350_gpio->gpio_chip = template_chip;
115c103de24SGrant Likely 	wm8350_gpio->gpio_chip.ngpio = 13;
11658383c78SLinus Walleij 	wm8350_gpio->gpio_chip.parent = &pdev->dev;
117c103de24SGrant Likely 	if (pdata && pdata->gpio_base)
118c103de24SGrant Likely 		wm8350_gpio->gpio_chip.base = pdata->gpio_base;
119c103de24SGrant Likely 	else
120c103de24SGrant Likely 		wm8350_gpio->gpio_chip.base = -1;
121c103de24SGrant Likely 
12205332606SAlexandru Ardelean 	return devm_gpiochip_add_data(&pdev->dev, &wm8350_gpio->gpio_chip, wm8350_gpio);
123c103de24SGrant Likely }
124c103de24SGrant Likely 
125c103de24SGrant Likely static struct platform_driver wm8350_gpio_driver = {
126c103de24SGrant Likely 	.driver.name	= "wm8350-gpio",
127c103de24SGrant Likely 	.probe		= wm8350_gpio_probe,
128c103de24SGrant Likely };
129c103de24SGrant Likely 
wm8350_gpio_init(void)130c103de24SGrant Likely static int __init wm8350_gpio_init(void)
131c103de24SGrant Likely {
132c103de24SGrant Likely 	return platform_driver_register(&wm8350_gpio_driver);
133c103de24SGrant Likely }
134c103de24SGrant Likely subsys_initcall(wm8350_gpio_init);
135c103de24SGrant Likely 
wm8350_gpio_exit(void)136c103de24SGrant Likely static void __exit wm8350_gpio_exit(void)
137c103de24SGrant Likely {
138c103de24SGrant Likely 	platform_driver_unregister(&wm8350_gpio_driver);
139c103de24SGrant Likely }
140c103de24SGrant Likely module_exit(wm8350_gpio_exit);
141c103de24SGrant Likely 
142c103de24SGrant Likely MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
143c103de24SGrant Likely MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs");
144c103de24SGrant Likely MODULE_LICENSE("GPL");
145c103de24SGrant Likely MODULE_ALIAS("platform:wm8350-gpio");
146