xref: /openbmc/linux/drivers/gpio/gpio-wm8350.c (revision dfcdf7214ec921d792d93a4f5602da2073043073)
1c103de24SGrant Likely /*
2c103de24SGrant Likely  * gpiolib support for Wolfson WM835x PMICs
3c103de24SGrant Likely  *
4c103de24SGrant Likely  * Copyright 2009 Wolfson Microelectronics PLC.
5c103de24SGrant Likely  *
6c103de24SGrant Likely  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7c103de24SGrant Likely  *
8c103de24SGrant Likely  *  This program is free software; you can redistribute  it and/or modify it
9c103de24SGrant Likely  *  under  the terms of  the GNU General  Public License as published by the
10c103de24SGrant Likely  *  Free Software Foundation;  either version 2 of the  License, or (at your
11c103de24SGrant Likely  *  option) any later version.
12c103de24SGrant Likely  *
13c103de24SGrant Likely  */
14c103de24SGrant Likely 
15c103de24SGrant Likely #include <linux/kernel.h>
16c103de24SGrant Likely #include <linux/slab.h>
17c103de24SGrant Likely #include <linux/module.h>
18c103de24SGrant Likely #include <linux/gpio.h>
19c103de24SGrant Likely #include <linux/mfd/core.h>
20c103de24SGrant Likely #include <linux/platform_device.h>
21c103de24SGrant Likely #include <linux/seq_file.h>
22c103de24SGrant Likely 
23c103de24SGrant Likely #include <linux/mfd/wm8350/core.h>
24c103de24SGrant Likely #include <linux/mfd/wm8350/gpio.h>
25c103de24SGrant Likely 
26c103de24SGrant Likely struct wm8350_gpio_data {
27c103de24SGrant Likely 	struct wm8350 *wm8350;
28c103de24SGrant Likely 	struct gpio_chip gpio_chip;
29c103de24SGrant Likely };
30c103de24SGrant Likely 
31c103de24SGrant Likely static int wm8350_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
32c103de24SGrant Likely {
33*dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
34c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
35c103de24SGrant Likely 
36c103de24SGrant Likely 	return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
37c103de24SGrant Likely 			       1 << offset);
38c103de24SGrant Likely }
39c103de24SGrant Likely 
40c103de24SGrant Likely static int wm8350_gpio_get(struct gpio_chip *chip, unsigned offset)
41c103de24SGrant Likely {
42*dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
43c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
44c103de24SGrant Likely 	int ret;
45c103de24SGrant Likely 
46c103de24SGrant Likely 	ret = wm8350_reg_read(wm8350, WM8350_GPIO_LEVEL);
47c103de24SGrant Likely 	if (ret < 0)
48c103de24SGrant Likely 		return ret;
49c103de24SGrant Likely 
50c103de24SGrant Likely 	if (ret & (1 << offset))
51c103de24SGrant Likely 		return 1;
52c103de24SGrant Likely 	else
53c103de24SGrant Likely 		return 0;
54c103de24SGrant Likely }
55c103de24SGrant Likely 
56c103de24SGrant Likely static void wm8350_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
57c103de24SGrant Likely {
58*dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
59c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
60c103de24SGrant Likely 
61c103de24SGrant Likely 	if (value)
62c103de24SGrant Likely 		wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
63c103de24SGrant Likely 	else
64c103de24SGrant Likely 		wm8350_clear_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
65c103de24SGrant Likely }
66c103de24SGrant Likely 
67c103de24SGrant Likely static int wm8350_gpio_direction_out(struct gpio_chip *chip,
68c103de24SGrant Likely 				     unsigned offset, int value)
69c103de24SGrant Likely {
70*dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
71c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
72c103de24SGrant Likely 	int ret;
73c103de24SGrant Likely 
74c103de24SGrant Likely 	ret = wm8350_clear_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
75c103de24SGrant Likely 				1 << offset);
76c103de24SGrant Likely 	if (ret < 0)
77c103de24SGrant Likely 		return ret;
78c103de24SGrant Likely 
79c103de24SGrant Likely 	/* Don't have an atomic direction/value setup */
80c103de24SGrant Likely 	wm8350_gpio_set(chip, offset, value);
81c103de24SGrant Likely 
82c103de24SGrant Likely 	return 0;
83c103de24SGrant Likely }
84c103de24SGrant Likely 
85c103de24SGrant Likely static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
86c103de24SGrant Likely {
87*dfcdf721SLinus Walleij 	struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip);
88c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
89c103de24SGrant Likely 
90c103de24SGrant Likely 	if (!wm8350->irq_base)
91c103de24SGrant Likely 		return -EINVAL;
92c103de24SGrant Likely 
93c103de24SGrant Likely 	return wm8350->irq_base + WM8350_IRQ_GPIO(offset);
94c103de24SGrant Likely }
95c103de24SGrant Likely 
96c103de24SGrant Likely static struct gpio_chip template_chip = {
97c103de24SGrant Likely 	.label			= "wm8350",
98c103de24SGrant Likely 	.owner			= THIS_MODULE,
99c103de24SGrant Likely 	.direction_input	= wm8350_gpio_direction_in,
100c103de24SGrant Likely 	.get			= wm8350_gpio_get,
101c103de24SGrant Likely 	.direction_output	= wm8350_gpio_direction_out,
102c103de24SGrant Likely 	.set			= wm8350_gpio_set,
103c103de24SGrant Likely 	.to_irq			= wm8350_gpio_to_irq,
1049fb1f39eSLinus Walleij 	.can_sleep		= true,
105c103de24SGrant Likely };
106c103de24SGrant Likely 
1073836309dSBill Pemberton static int wm8350_gpio_probe(struct platform_device *pdev)
108c103de24SGrant Likely {
109c103de24SGrant Likely 	struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent);
110e56aee18SJingoo Han 	struct wm8350_platform_data *pdata = dev_get_platdata(wm8350->dev);
111c103de24SGrant Likely 	struct wm8350_gpio_data *wm8350_gpio;
112c103de24SGrant Likely 	int ret;
113c103de24SGrant Likely 
1145b425438SAxel Lin 	wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio),
1155b425438SAxel Lin 				   GFP_KERNEL);
116c103de24SGrant Likely 	if (wm8350_gpio == NULL)
117c103de24SGrant Likely 		return -ENOMEM;
118c103de24SGrant Likely 
119c103de24SGrant Likely 	wm8350_gpio->wm8350 = wm8350;
120c103de24SGrant Likely 	wm8350_gpio->gpio_chip = template_chip;
121c103de24SGrant Likely 	wm8350_gpio->gpio_chip.ngpio = 13;
12258383c78SLinus Walleij 	wm8350_gpio->gpio_chip.parent = &pdev->dev;
123c103de24SGrant Likely 	if (pdata && pdata->gpio_base)
124c103de24SGrant Likely 		wm8350_gpio->gpio_chip.base = pdata->gpio_base;
125c103de24SGrant Likely 	else
126c103de24SGrant Likely 		wm8350_gpio->gpio_chip.base = -1;
127c103de24SGrant Likely 
128*dfcdf721SLinus Walleij 	ret = gpiochip_add_data(&wm8350_gpio->gpio_chip, wm8350_gpio);
129c103de24SGrant Likely 	if (ret < 0) {
1305b425438SAxel Lin 		dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
1315b425438SAxel Lin 		return ret;
132c103de24SGrant Likely 	}
133c103de24SGrant Likely 
134c103de24SGrant Likely 	platform_set_drvdata(pdev, wm8350_gpio);
135c103de24SGrant Likely 
136c103de24SGrant Likely 	return ret;
137c103de24SGrant Likely }
138c103de24SGrant Likely 
139206210ceSBill Pemberton static int wm8350_gpio_remove(struct platform_device *pdev)
140c103de24SGrant Likely {
141c103de24SGrant Likely 	struct wm8350_gpio_data *wm8350_gpio = platform_get_drvdata(pdev);
142c103de24SGrant Likely 
1439f5132aeSabdoulaye berthe 	gpiochip_remove(&wm8350_gpio->gpio_chip);
1449f5132aeSabdoulaye berthe 	return 0;
145c103de24SGrant Likely }
146c103de24SGrant Likely 
147c103de24SGrant Likely static struct platform_driver wm8350_gpio_driver = {
148c103de24SGrant Likely 	.driver.name	= "wm8350-gpio",
149c103de24SGrant Likely 	.driver.owner	= THIS_MODULE,
150c103de24SGrant Likely 	.probe		= wm8350_gpio_probe,
1518283c4ffSBill Pemberton 	.remove		= wm8350_gpio_remove,
152c103de24SGrant Likely };
153c103de24SGrant Likely 
154c103de24SGrant Likely static int __init wm8350_gpio_init(void)
155c103de24SGrant Likely {
156c103de24SGrant Likely 	return platform_driver_register(&wm8350_gpio_driver);
157c103de24SGrant Likely }
158c103de24SGrant Likely subsys_initcall(wm8350_gpio_init);
159c103de24SGrant Likely 
160c103de24SGrant Likely static void __exit wm8350_gpio_exit(void)
161c103de24SGrant Likely {
162c103de24SGrant Likely 	platform_driver_unregister(&wm8350_gpio_driver);
163c103de24SGrant Likely }
164c103de24SGrant Likely module_exit(wm8350_gpio_exit);
165c103de24SGrant Likely 
166c103de24SGrant Likely MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
167c103de24SGrant Likely MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs");
168c103de24SGrant Likely MODULE_LICENSE("GPL");
169c103de24SGrant Likely MODULE_ALIAS("platform:wm8350-gpio");
170