Linux 4.1.16
[linux/fpc-iii.git] / drivers / gpio / gpio-wm8350.c
blob060b89303bb6125e28919a4c31f032adf6d7ed0a
1 /*
2 * 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/slab.h>
17 #include <linux/module.h>
18 #include <linux/gpio.h>
19 #include <linux/mfd/core.h>
20 #include <linux/platform_device.h>
21 #include <linux/seq_file.h>
23 #include <linux/mfd/wm8350/core.h>
24 #include <linux/mfd/wm8350/gpio.h>
26 struct wm8350_gpio_data {
27 struct wm8350 *wm8350;
28 struct gpio_chip gpio_chip;
31 static inline struct wm8350_gpio_data *to_wm8350_gpio(struct gpio_chip *chip)
33 return container_of(chip, struct wm8350_gpio_data, gpio_chip);
36 static int wm8350_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
38 struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
39 struct wm8350 *wm8350 = wm8350_gpio->wm8350;
41 return wm8350_set_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
42 1 << offset);
45 static int wm8350_gpio_get(struct gpio_chip *chip, unsigned offset)
47 struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
48 struct wm8350 *wm8350 = wm8350_gpio->wm8350;
49 int ret;
51 ret = wm8350_reg_read(wm8350, WM8350_GPIO_LEVEL);
52 if (ret < 0)
53 return ret;
55 if (ret & (1 << offset))
56 return 1;
57 else
58 return 0;
61 static void wm8350_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
63 struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
64 struct wm8350 *wm8350 = wm8350_gpio->wm8350;
66 if (value)
67 wm8350_set_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
68 else
69 wm8350_clear_bits(wm8350, WM8350_GPIO_LEVEL, 1 << offset);
72 static int wm8350_gpio_direction_out(struct gpio_chip *chip,
73 unsigned offset, int value)
75 struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
76 struct wm8350 *wm8350 = wm8350_gpio->wm8350;
77 int ret;
79 ret = wm8350_clear_bits(wm8350, WM8350_GPIO_CONFIGURATION_I_O,
80 1 << offset);
81 if (ret < 0)
82 return ret;
84 /* Don't have an atomic direction/value setup */
85 wm8350_gpio_set(chip, offset, value);
87 return 0;
90 static int wm8350_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
92 struct wm8350_gpio_data *wm8350_gpio = to_wm8350_gpio(chip);
93 struct wm8350 *wm8350 = wm8350_gpio->wm8350;
95 if (!wm8350->irq_base)
96 return -EINVAL;
98 return wm8350->irq_base + WM8350_IRQ_GPIO(offset);
101 static struct gpio_chip template_chip = {
102 .label = "wm8350",
103 .owner = THIS_MODULE,
104 .direction_input = wm8350_gpio_direction_in,
105 .get = wm8350_gpio_get,
106 .direction_output = wm8350_gpio_direction_out,
107 .set = wm8350_gpio_set,
108 .to_irq = wm8350_gpio_to_irq,
109 .can_sleep = true,
112 static int wm8350_gpio_probe(struct platform_device *pdev)
114 struct wm8350 *wm8350 = dev_get_drvdata(pdev->dev.parent);
115 struct wm8350_platform_data *pdata = dev_get_platdata(wm8350->dev);
116 struct wm8350_gpio_data *wm8350_gpio;
117 int ret;
119 wm8350_gpio = devm_kzalloc(&pdev->dev, sizeof(*wm8350_gpio),
120 GFP_KERNEL);
121 if (wm8350_gpio == NULL)
122 return -ENOMEM;
124 wm8350_gpio->wm8350 = wm8350;
125 wm8350_gpio->gpio_chip = template_chip;
126 wm8350_gpio->gpio_chip.ngpio = 13;
127 wm8350_gpio->gpio_chip.dev = &pdev->dev;
128 if (pdata && pdata->gpio_base)
129 wm8350_gpio->gpio_chip.base = pdata->gpio_base;
130 else
131 wm8350_gpio->gpio_chip.base = -1;
133 ret = gpiochip_add(&wm8350_gpio->gpio_chip);
134 if (ret < 0) {
135 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
136 return ret;
139 platform_set_drvdata(pdev, wm8350_gpio);
141 return ret;
144 static int wm8350_gpio_remove(struct platform_device *pdev)
146 struct wm8350_gpio_data *wm8350_gpio = platform_get_drvdata(pdev);
148 gpiochip_remove(&wm8350_gpio->gpio_chip);
149 return 0;
152 static struct platform_driver wm8350_gpio_driver = {
153 .driver.name = "wm8350-gpio",
154 .driver.owner = THIS_MODULE,
155 .probe = wm8350_gpio_probe,
156 .remove = wm8350_gpio_remove,
159 static int __init wm8350_gpio_init(void)
161 return platform_driver_register(&wm8350_gpio_driver);
163 subsys_initcall(wm8350_gpio_init);
165 static void __exit wm8350_gpio_exit(void)
167 platform_driver_unregister(&wm8350_gpio_driver);
169 module_exit(wm8350_gpio_exit);
171 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
172 MODULE_DESCRIPTION("GPIO interface for WM8350 PMICs");
173 MODULE_LICENSE("GPL");
174 MODULE_ALIAS("platform:wm8350-gpio");