Commit | Line | Data |
---|---|---|
66dbe750 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
38f6ce45 | 2 | /* |
c103de24 | 3 | * gpiolib support for Wolfson WM835x PMICs |
38f6ce45 MB |
4 | * |
5 | * Copyright 2009 Wolfson Microelectronics PLC. | |
6 | * | |
7 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> | |
8 | * | |
38f6ce45 MB |
9 | */ |
10 | ||
10833c4b | 11 | #include <linux/gpio/driver.h> |
275d1356 | 12 | #include <linux/kernel.h> |
38f6ce45 | 13 | #include <linux/mfd/core.h> |
275d1356 | 14 | #include <linux/module.h> |
38f6ce45 | 15 | #include <linux/platform_device.h> |
275d1356 | 16 | #include <linux/slab.h> |
38f6ce45 MB |
17 | |
18 | #include <linux/mfd/wm8350/core.h> | |
19 | #include <linux/mfd/wm8350/gpio.h> | |
20 | ||
21 | struct wm8350_gpio_data { | |
22 | struct wm8350 *wm8350; | |
23 | struct gpio_chip gpio_chip; | |
24 | }; | |
25 | ||
38f6ce45 MB |
26 | static int wm8350_gpio_direction_in(struct gpio_chip *chip, unsigned offset) |
27 | { | |
dfcdf721 | 28 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
38f6ce45 MB |
29 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
30 | ||
31 | return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O, | |
32 | 1 << offset); | |
33 | } | |
34 | ||
35 | static int wm8350_gpio_get(struct gpio_chip *chip, unsigned offset) | |
36 | { | |
dfcdf721 | 37 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
38f6ce45 MB |
38 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
39 | int ret; | |
40 | ||
41 | ret = wm8350_reg_read(wm8350, WM8350_GPIO_LEVEL); | |
42 | if (ret < 0) | |
43 | return ret; | |
44 | ||
45 | if (ret & (1 << offset)) | |
46 | return 1; | |
47 | else | |
48 | return 0; | |
49 | } | |
50 | ||
51 | static void wm8350_gpio_set(struct gpio_chip *chip, unsigned offset, int value) | |
52 | { | |
dfcdf721 | 53 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
38f6ce45 MB |
54 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
55 | ||
56 | if (value) | |
57 | wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset); | |
58 | else | |
59 | wm8350_clear_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset); | |
60 | } | |
61 | ||
62 | static int wm8350_gpio_direction_out(struct gpio_chip *chip, | |
63 | unsigned offset, int value) | |
64 | { | |
dfcdf721 | 65 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
38f6ce45 MB |
66 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
67 | int ret; | |
68 | ||
69 | ret = wm8350_clear_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O, | |
70 | 1 << offset); | |
71 | if (ret < 0) | |
72 | return ret; | |
73 | ||
74 | /* Don't have an atomic direction/value setup */ | |
75 | wm8350_gpio_set(chip, offset, value); | |
76 | ||
77 | return 0; | |
78 | } | |
79 | ||
80 | static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset) | |
81 | { | |
dfcdf721 | 82 | struct wm8350_gpio_data *wm8350_gpio = gpiochip_get_data(chip); |
38f6ce45 MB |
83 | struct wm8350 *wm8350 = wm8350_gpio->wm8350; |
84 | ||
85 | if (!wm8350->irq_base) | |
86 | return -EINVAL; | |
87 | ||
88 | return wm8350->irq_base + WM8350_IRQ_GPIO(offset); | |
89 | } | |
90 | ||
e35b5ab0 | 91 | static const struct gpio_chip template_chip = { |
38f6ce45 MB |
92 | .label = "wm8350", |
93 | .owner = THIS_MODULE, | |
94 | .direction_input = wm8350_gpio_direction_in, | |
95 | .get = wm8350_gpio_get, | |
96 | .direction_output = wm8350_gpio_direction_out, | |
97 | .set = wm8350_gpio_set, | |
98 | .to_irq = wm8350_gpio_to_irq, | |
9fb1f39e | 99 | .can_sleep = true, |
38f6ce45 MB |
100 | }; |
101 | ||
3836309d | 102 | static int wm8350_gpio_probe(struct platform_device *pdev) |
38f6ce45 MB |
103 | { |
104 | struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent); | |
e56aee18 | 105 | struct wm8350_platform_data *pdata = dev_get_platdata(wm8350->dev); |
38f6ce45 | 106 | struct wm8350_gpio_data *wm8350_gpio; |
38f6ce45 | 107 | |
5b425438 AL |
108 | wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio), |
109 | GFP_KERNEL); | |
38f6ce45 MB |
110 | if (wm8350_gpio == NULL) |
111 | return -ENOMEM; | |
112 | ||
113 | wm8350_gpio->wm8350 = wm8350; | |
114 | wm8350_gpio->gpio_chip = template_chip; | |
115 | wm8350_gpio->gpio_chip.ngpio = 13; | |
58383c78 | 116 | wm8350_gpio->gpio_chip.parent = &pdev->dev; |
38f6ce45 MB |
117 | if (pdata && pdata->gpio_base) |
118 | wm8350_gpio->gpio_chip.base = pdata->gpio_base; | |
119 | else | |
120 | wm8350_gpio->gpio_chip.base = -1; | |
121 | ||
05332606 | 122 | return devm_gpiochip_add_data(&pdev->dev, &wm8350_gpio->gpio_chip, wm8350_gpio); |
38f6ce45 MB |
123 | } |
124 | ||
38f6ce45 MB |
125 | static struct platform_driver wm8350_gpio_driver = { |
126 | .driver.name = "wm8350-gpio", | |
38f6ce45 | 127 | .probe = wm8350_gpio_probe, |
38f6ce45 MB |
128 | }; |
129 | ||
130 | static int __init wm8350_gpio_init(void) | |
131 | { | |
132 | return platform_driver_register(&wm8350_gpio_driver); | |
133 | } | |
134 | subsys_initcall(wm8350_gpio_init); | |
135 | ||
136 | static void __exit wm8350_gpio_exit(void) | |
137 | { | |
138 | platform_driver_unregister(&wm8350_gpio_driver); | |
139 | } | |
140 | module_exit(wm8350_gpio_exit); | |
141 | ||
142 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); | |
143 | MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs"); | |
144 | MODULE_LICENSE("GPL"); | |
145 | MODULE_ALIAS("platform:wm8350-gpio"); |