1 // SPDX-License-Identifier: GPL-2.0+
3 * gpiolib support for Wolfson WM835x PMICs
5 * Copyright 2009 Wolfson Microelectronics PLC.
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/mfd/core.h>
16 #include <linux/platform_device.h>
17 #include <linux/seq_file.h>
19 #include <linux/mfd/wm8350/core.h>
20 #include <linux/mfd/wm8350/gpio.h>
22 struct wm8350_gpio_data
{
23 struct wm8350
*wm8350
;
24 struct gpio_chip gpio_chip
;
27 static int wm8350_gpio_direction_in(struct gpio_chip
*chip
, unsigned offset
)
29 struct wm8350_gpio_data
*wm8350_gpio
= gpiochip_get_data(chip
);
30 struct wm8350
*wm8350
= wm8350_gpio
->wm8350
;
32 return wm8350_set_bits(wm8350
, WM8350_GPIO_CONFIGURATION_I_O
,
36 static int wm8350_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
38 struct wm8350_gpio_data
*wm8350_gpio
= gpiochip_get_data(chip
);
39 struct wm8350
*wm8350
= wm8350_gpio
->wm8350
;
42 ret
= wm8350_reg_read(wm8350
, WM8350_GPIO_LEVEL
);
46 if (ret
& (1 << offset
))
52 static void wm8350_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
54 struct wm8350_gpio_data
*wm8350_gpio
= gpiochip_get_data(chip
);
55 struct wm8350
*wm8350
= wm8350_gpio
->wm8350
;
58 wm8350_set_bits(wm8350
, WM8350_GPIO_LEVEL
, 1 << offset
);
60 wm8350_clear_bits(wm8350
, WM8350_GPIO_LEVEL
, 1 << offset
);
63 static int wm8350_gpio_direction_out(struct gpio_chip
*chip
,
64 unsigned offset
, int value
)
66 struct wm8350_gpio_data
*wm8350_gpio
= gpiochip_get_data(chip
);
67 struct wm8350
*wm8350
= wm8350_gpio
->wm8350
;
70 ret
= wm8350_clear_bits(wm8350
, WM8350_GPIO_CONFIGURATION_I_O
,
75 /* Don't have an atomic direction/value setup */
76 wm8350_gpio_set(chip
, offset
, value
);
81 static int wm8350_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
83 struct wm8350_gpio_data
*wm8350_gpio
= gpiochip_get_data(chip
);
84 struct wm8350
*wm8350
= wm8350_gpio
->wm8350
;
86 if (!wm8350
->irq_base
)
89 return wm8350
->irq_base
+ WM8350_IRQ_GPIO(offset
);
92 static const struct gpio_chip template_chip
= {
95 .direction_input
= wm8350_gpio_direction_in
,
96 .get
= wm8350_gpio_get
,
97 .direction_output
= wm8350_gpio_direction_out
,
98 .set
= wm8350_gpio_set
,
99 .to_irq
= wm8350_gpio_to_irq
,
103 static int wm8350_gpio_probe(struct platform_device
*pdev
)
105 struct wm8350
*wm8350
= dev_get_drvdata(pdev
->dev
.parent
);
106 struct wm8350_platform_data
*pdata
= dev_get_platdata(wm8350
->dev
);
107 struct wm8350_gpio_data
*wm8350_gpio
;
110 wm8350_gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*wm8350_gpio
),
112 if (wm8350_gpio
== NULL
)
115 wm8350_gpio
->wm8350
= wm8350
;
116 wm8350_gpio
->gpio_chip
= template_chip
;
117 wm8350_gpio
->gpio_chip
.ngpio
= 13;
118 wm8350_gpio
->gpio_chip
.parent
= &pdev
->dev
;
119 if (pdata
&& pdata
->gpio_base
)
120 wm8350_gpio
->gpio_chip
.base
= pdata
->gpio_base
;
122 wm8350_gpio
->gpio_chip
.base
= -1;
124 ret
= devm_gpiochip_add_data(&pdev
->dev
, &wm8350_gpio
->gpio_chip
,
127 dev_err(&pdev
->dev
, "Could not register gpiochip, %d\n", ret
);
131 platform_set_drvdata(pdev
, wm8350_gpio
);
136 static struct platform_driver wm8350_gpio_driver
= {
137 .driver
.name
= "wm8350-gpio",
138 .probe
= wm8350_gpio_probe
,
141 static int __init
wm8350_gpio_init(void)
143 return platform_driver_register(&wm8350_gpio_driver
);
145 subsys_initcall(wm8350_gpio_init
);
147 static void __exit
wm8350_gpio_exit(void)
149 platform_driver_unregister(&wm8350_gpio_driver
);
151 module_exit(wm8350_gpio_exit
);
153 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
154 MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs");
155 MODULE_LICENSE("GPL");
156 MODULE_ALIAS("platform:wm8350-gpio");