2 * wm835x-gpiolib.c -- gpiolib support for Wolfson WM835x PMICs
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/gpio.h>
18 #include <linux/mfd/core.h>
19 #include <linux/platform_device.h>
20 #include <linux/seq_file.h>
22 #include <linux/mfd/wm8350/core.h>
23 #include <linux/mfd/wm8350/gpio.h>
25 struct wm8350_gpio_data
{
26 struct wm8350
*wm8350
;
27 struct gpio_chip gpio_chip
;
30 static inline struct wm8350_gpio_data
*to_wm8350_gpio(struct gpio_chip
*chip
)
32 return container_of(chip
, struct wm8350_gpio_data
, gpio_chip
);
35 static int wm8350_gpio_direction_in(struct gpio_chip
*chip
, unsigned offset
)
37 struct wm8350_gpio_data
*wm8350_gpio
= to_wm8350_gpio(chip
);
38 struct wm8350
*wm8350
= wm8350_gpio
->wm8350
;
40 return wm8350_set_bits(wm8350
, WM8350_GPIO_CONFIGURATION_I_O
,
44 static int wm8350_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
46 struct wm8350_gpio_data
*wm8350_gpio
= to_wm8350_gpio(chip
);
47 struct wm8350
*wm8350
= wm8350_gpio
->wm8350
;
50 ret
= wm8350_reg_read(wm8350
, WM8350_GPIO_LEVEL
);
54 if (ret
& (1 << offset
))
60 static void wm8350_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
62 struct wm8350_gpio_data
*wm8350_gpio
= to_wm8350_gpio(chip
);
63 struct wm8350
*wm8350
= wm8350_gpio
->wm8350
;
66 wm8350_set_bits(wm8350
, WM8350_GPIO_LEVEL
, 1 << offset
);
68 wm8350_clear_bits(wm8350
, WM8350_GPIO_LEVEL
, 1 << offset
);
71 static int wm8350_gpio_direction_out(struct gpio_chip
*chip
,
72 unsigned offset
, int value
)
74 struct wm8350_gpio_data
*wm8350_gpio
= to_wm8350_gpio(chip
);
75 struct wm8350
*wm8350
= wm8350_gpio
->wm8350
;
78 ret
= wm8350_clear_bits(wm8350
, WM8350_GPIO_CONFIGURATION_I_O
,
83 /* Don't have an atomic direction/value setup */
84 wm8350_gpio_set(chip
, offset
, value
);
89 static int wm8350_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
91 struct wm8350_gpio_data
*wm8350_gpio
= to_wm8350_gpio(chip
);
92 struct wm8350
*wm8350
= wm8350_gpio
->wm8350
;
94 if (!wm8350
->irq_base
)
97 return wm8350
->irq_base
+ WM8350_IRQ_GPIO(offset
);
100 static struct gpio_chip template_chip
= {
102 .owner
= THIS_MODULE
,
103 .direction_input
= wm8350_gpio_direction_in
,
104 .get
= wm8350_gpio_get
,
105 .direction_output
= wm8350_gpio_direction_out
,
106 .set
= wm8350_gpio_set
,
107 .to_irq
= wm8350_gpio_to_irq
,
111 static int __devinit
wm8350_gpio_probe(struct platform_device
*pdev
)
113 struct wm8350
*wm8350
= dev_get_drvdata(pdev
->dev
.parent
);
114 struct wm8350_platform_data
*pdata
= wm8350
->dev
->platform_data
;
115 struct wm8350_gpio_data
*wm8350_gpio
;
118 wm8350_gpio
= kzalloc(sizeof(*wm8350_gpio
), GFP_KERNEL
);
119 if (wm8350_gpio
== NULL
)
122 wm8350_gpio
->wm8350
= wm8350
;
123 wm8350_gpio
->gpio_chip
= template_chip
;
124 wm8350_gpio
->gpio_chip
.ngpio
= 13;
125 wm8350_gpio
->gpio_chip
.dev
= &pdev
->dev
;
126 if (pdata
&& pdata
->gpio_base
)
127 wm8350_gpio
->gpio_chip
.base
= pdata
->gpio_base
;
129 wm8350_gpio
->gpio_chip
.base
= -1;
131 ret
= gpiochip_add(&wm8350_gpio
->gpio_chip
);
133 dev_err(&pdev
->dev
, "Could not register gpiochip, %d\n",
138 platform_set_drvdata(pdev
, wm8350_gpio
);
147 static int __devexit
wm8350_gpio_remove(struct platform_device
*pdev
)
149 struct wm8350_gpio_data
*wm8350_gpio
= platform_get_drvdata(pdev
);
152 ret
= gpiochip_remove(&wm8350_gpio
->gpio_chip
);
159 static struct platform_driver wm8350_gpio_driver
= {
160 .driver
.name
= "wm8350-gpio",
161 .driver
.owner
= THIS_MODULE
,
162 .probe
= wm8350_gpio_probe
,
163 .remove
= __devexit_p(wm8350_gpio_remove
),
166 static int __init
wm8350_gpio_init(void)
168 return platform_driver_register(&wm8350_gpio_driver
);
170 subsys_initcall(wm8350_gpio_init
);
172 static void __exit
wm8350_gpio_exit(void)
174 platform_driver_unregister(&wm8350_gpio_driver
);
176 module_exit(wm8350_gpio_exit
);
178 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
179 MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs");
180 MODULE_LICENSE("GPL");
181 MODULE_ALIAS("platform:wm8350-gpio");