xref: /linux/drivers/gpio/gpio-wm8350.c (revision 5b4254386bf9a96dfcef9ec8e099bd06fe3e6896)
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 inline struct wm8350_gpio_data *to_wm8350_gpio(struct gpio_chip *chip)
32c103de24SGrant Likely {
33c103de24SGrant Likely 	return container_of(chip, struct wm8350_gpio_data, gpio_chip);
34c103de24SGrant Likely }
35c103de24SGrant Likely 
36c103de24SGrant Likely static int wm8350_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
37c103de24SGrant Likely {
38c103de24SGrant Likely 	struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
39c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
40c103de24SGrant Likely 
41c103de24SGrant Likely 	return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
42c103de24SGrant Likely 			       1 << offset);
43c103de24SGrant Likely }
44c103de24SGrant Likely 
45c103de24SGrant Likely static int wm8350_gpio_get(struct gpio_chip *chip, unsigned offset)
46c103de24SGrant Likely {
47c103de24SGrant Likely 	struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
48c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
49c103de24SGrant Likely 	int ret;
50c103de24SGrant Likely 
51c103de24SGrant Likely 	ret = wm8350_reg_read(wm8350, WM8350_GPIO_LEVEL);
52c103de24SGrant Likely 	if (ret < 0)
53c103de24SGrant Likely 		return ret;
54c103de24SGrant Likely 
55c103de24SGrant Likely 	if (ret & (1 << offset))
56c103de24SGrant Likely 		return 1;
57c103de24SGrant Likely 	else
58c103de24SGrant Likely 		return 0;
59c103de24SGrant Likely }
60c103de24SGrant Likely 
61c103de24SGrant Likely static void wm8350_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
62c103de24SGrant Likely {
63c103de24SGrant Likely 	struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
64c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
65c103de24SGrant Likely 
66c103de24SGrant Likely 	if (value)
67c103de24SGrant Likely 		wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
68c103de24SGrant Likely 	else
69c103de24SGrant Likely 		wm8350_clear_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
70c103de24SGrant Likely }
71c103de24SGrant Likely 
72c103de24SGrant Likely static int wm8350_gpio_direction_out(struct gpio_chip *chip,
73c103de24SGrant Likely 				     unsigned offset, int value)
74c103de24SGrant Likely {
75c103de24SGrant Likely 	struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
76c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
77c103de24SGrant Likely 	int ret;
78c103de24SGrant Likely 
79c103de24SGrant Likely 	ret = wm8350_clear_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
80c103de24SGrant Likely 				1 << offset);
81c103de24SGrant Likely 	if (ret < 0)
82c103de24SGrant Likely 		return ret;
83c103de24SGrant Likely 
84c103de24SGrant Likely 	/* Don't have an atomic direction/value setup */
85c103de24SGrant Likely 	wm8350_gpio_set(chip, offset, value);
86c103de24SGrant Likely 
87c103de24SGrant Likely 	return 0;
88c103de24SGrant Likely }
89c103de24SGrant Likely 
90c103de24SGrant Likely static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
91c103de24SGrant Likely {
92c103de24SGrant Likely 	struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
93c103de24SGrant Likely 	struct wm8350 *wm8350 = wm8350_gpio->wm8350;
94c103de24SGrant Likely 
95c103de24SGrant Likely 	if (!wm8350->irq_base)
96c103de24SGrant Likely 		return -EINVAL;
97c103de24SGrant Likely 
98c103de24SGrant Likely 	return wm8350->irq_base + WM8350_IRQ_GPIO(offset);
99c103de24SGrant Likely }
100c103de24SGrant Likely 
101c103de24SGrant Likely static struct gpio_chip template_chip = {
102c103de24SGrant Likely 	.label			= "wm8350",
103c103de24SGrant Likely 	.owner			= THIS_MODULE,
104c103de24SGrant Likely 	.direction_input	= wm8350_gpio_direction_in,
105c103de24SGrant Likely 	.get			= wm8350_gpio_get,
106c103de24SGrant Likely 	.direction_output	= wm8350_gpio_direction_out,
107c103de24SGrant Likely 	.set			= wm8350_gpio_set,
108c103de24SGrant Likely 	.to_irq			= wm8350_gpio_to_irq,
109c103de24SGrant Likely 	.can_sleep		= 1,
110c103de24SGrant Likely };
111c103de24SGrant Likely 
112c103de24SGrant Likely static int __devinit wm8350_gpio_probe(struct platform_device *pdev)
113c103de24SGrant Likely {
114c103de24SGrant Likely 	struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent);
115c103de24SGrant Likely 	struct wm8350_platform_data *pdata = wm8350->dev->platform_data;
116c103de24SGrant Likely 	struct wm8350_gpio_data *wm8350_gpio;
117c103de24SGrant Likely 	int ret;
118c103de24SGrant Likely 
119*5b425438SAxel Lin 	wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio),
120*5b425438SAxel Lin 				   GFP_KERNEL);
121c103de24SGrant Likely 	if (wm8350_gpio == NULL)
122c103de24SGrant Likely 		return -ENOMEM;
123c103de24SGrant Likely 
124c103de24SGrant Likely 	wm8350_gpio->wm8350 = wm8350;
125c103de24SGrant Likely 	wm8350_gpio->gpio_chip = template_chip;
126c103de24SGrant Likely 	wm8350_gpio->gpio_chip.ngpio = 13;
127c103de24SGrant Likely 	wm8350_gpio->gpio_chip.dev = &pdev->dev;
128c103de24SGrant Likely 	if (pdata && pdata->gpio_base)
129c103de24SGrant Likely 		wm8350_gpio->gpio_chip.base = pdata->gpio_base;
130c103de24SGrant Likely 	else
131c103de24SGrant Likely 		wm8350_gpio->gpio_chip.base = -1;
132c103de24SGrant Likely 
133c103de24SGrant Likely 	ret = gpiochip_add(&wm8350_gpio->gpio_chip);
134c103de24SGrant Likely 	if (ret < 0) {
135*5b425438SAxel Lin 		dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
136*5b425438SAxel Lin 		return ret;
137c103de24SGrant Likely 	}
138c103de24SGrant Likely 
139c103de24SGrant Likely 	platform_set_drvdata(pdev, wm8350_gpio);
140c103de24SGrant Likely 
141c103de24SGrant Likely 	return ret;
142c103de24SGrant Likely }
143c103de24SGrant Likely 
144c103de24SGrant Likely static int __devexit wm8350_gpio_remove(struct platform_device *pdev)
145c103de24SGrant Likely {
146c103de24SGrant Likely 	struct wm8350_gpio_data *wm8350_gpio = platform_get_drvdata(pdev);
147c103de24SGrant Likely 
148*5b425438SAxel Lin 	return gpiochip_remove(&wm8350_gpio->gpio_chip);
149c103de24SGrant Likely }
150c103de24SGrant Likely 
151c103de24SGrant Likely static struct platform_driver wm8350_gpio_driver = {
152c103de24SGrant Likely 	.driver.name	= "wm8350-gpio",
153c103de24SGrant Likely 	.driver.owner	= THIS_MODULE,
154c103de24SGrant Likely 	.probe		= wm8350_gpio_probe,
155c103de24SGrant Likely 	.remove		= __devexit_p(wm8350_gpio_remove),
156c103de24SGrant Likely };
157c103de24SGrant Likely 
158c103de24SGrant Likely static int __init wm8350_gpio_init(void)
159c103de24SGrant Likely {
160c103de24SGrant Likely 	return platform_driver_register(&wm8350_gpio_driver);
161c103de24SGrant Likely }
162c103de24SGrant Likely subsys_initcall(wm8350_gpio_init);
163c103de24SGrant Likely 
164c103de24SGrant Likely static void __exit wm8350_gpio_exit(void)
165c103de24SGrant Likely {
166c103de24SGrant Likely 	platform_driver_unregister(&wm8350_gpio_driver);
167c103de24SGrant Likely }
168c103de24SGrant Likely module_exit(wm8350_gpio_exit);
169c103de24SGrant Likely 
170c103de24SGrant Likely MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
171c103de24SGrant Likely MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs");
172c103de24SGrant Likely MODULE_LICENSE("GPL");
173c103de24SGrant Likely MODULE_ALIAS("platform:wm8350-gpio");
174