blob: c1ad2884f2edb0b79f3894a9a16662e5776bda51 [file] [log] [blame]
Graeme Gregory2537df72011-05-02 16:19:52 -05001/*
Grant Likelyc103de22011-06-04 18:38:28 -06002 * TI TPS6591x GPIO driver
Graeme Gregory2537df72011-05-02 16:19:52 -05003 *
4 * Copyright 2010 Texas Instruments Inc.
5 *
6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
7 * Author: Jorge Eduardo Candelaria jedu@slimlogic.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/errno.h>
19#include <linux/gpio.h>
20#include <linux/i2c.h>
Laxman Dewangan10bbc482012-05-11 21:48:27 +053021#include <linux/platform_device.h>
Graeme Gregory2537df72011-05-02 16:19:52 -050022#include <linux/mfd/tps65910.h>
Laxman Dewangan6fe02e92012-05-19 02:01:43 +053023#include <linux/of_device.h>
Graeme Gregory2537df72011-05-02 16:19:52 -050024
Laxman Dewangan10bbc482012-05-11 21:48:27 +053025struct tps65910_gpio {
26 struct gpio_chip gpio_chip;
27 struct tps65910 *tps65910;
28};
29
30static inline struct tps65910_gpio *to_tps65910_gpio(struct gpio_chip *chip)
31{
32 return container_of(chip, struct tps65910_gpio, gpio_chip);
33}
34
Graeme Gregory2537df72011-05-02 16:19:52 -050035static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
36{
Laxman Dewangan10bbc482012-05-11 21:48:27 +053037 struct tps65910_gpio *tps65910_gpio = to_tps65910_gpio(gc);
38 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
Rhyland Klein3f7e8272012-05-08 11:42:38 -070039 unsigned int val;
Graeme Gregory2537df72011-05-02 16:19:52 -050040
Rhyland Klein3f7e8272012-05-08 11:42:38 -070041 tps65910_reg_read(tps65910, TPS65910_GPIO0 + offset, &val);
Graeme Gregory2537df72011-05-02 16:19:52 -050042
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -050043 if (val & GPIO_STS_MASK)
Graeme Gregory2537df72011-05-02 16:19:52 -050044 return 1;
45
46 return 0;
47}
48
49static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
50 int value)
51{
Laxman Dewangan10bbc482012-05-11 21:48:27 +053052 struct tps65910_gpio *tps65910_gpio = to_tps65910_gpio(gc);
53 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
Graeme Gregory2537df72011-05-02 16:19:52 -050054
55 if (value)
Rhyland Klein3f7e8272012-05-08 11:42:38 -070056 tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -050057 GPIO_SET_MASK);
Graeme Gregory2537df72011-05-02 16:19:52 -050058 else
Rhyland Klein3f7e8272012-05-08 11:42:38 -070059 tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -050060 GPIO_SET_MASK);
Graeme Gregory2537df72011-05-02 16:19:52 -050061}
62
63static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
64 int value)
65{
Laxman Dewangan10bbc482012-05-11 21:48:27 +053066 struct tps65910_gpio *tps65910_gpio = to_tps65910_gpio(gc);
67 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
Graeme Gregory2537df72011-05-02 16:19:52 -050068
69 /* Set the initial value */
Laxman Dewangan94bd2442012-01-18 20:07:35 +053070 tps65910_gpio_set(gc, offset, value);
Graeme Gregory2537df72011-05-02 16:19:52 -050071
Rhyland Klein3f7e8272012-05-08 11:42:38 -070072 return tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -050073 GPIO_CFG_MASK);
Graeme Gregory2537df72011-05-02 16:19:52 -050074}
75
76static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
77{
Laxman Dewangan10bbc482012-05-11 21:48:27 +053078 struct tps65910_gpio *tps65910_gpio = to_tps65910_gpio(gc);
79 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
Graeme Gregory2537df72011-05-02 16:19:52 -050080
Rhyland Klein3f7e8272012-05-08 11:42:38 -070081 return tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -050082 GPIO_CFG_MASK);
Graeme Gregory2537df72011-05-02 16:19:52 -050083}
84
Laxman Dewangan6fe02e92012-05-19 02:01:43 +053085#ifdef CONFIG_OF
86static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
87 struct tps65910 *tps65910, int chip_ngpio)
88{
89 struct tps65910_board *tps65910_board = tps65910->of_plat_data;
90 unsigned int prop_array[TPS6591X_MAX_NUM_GPIO];
91 int ngpio = min(chip_ngpio, TPS6591X_MAX_NUM_GPIO);
92 int ret;
93 int idx;
94
95 tps65910_board->gpio_base = -1;
96 ret = of_property_read_u32_array(tps65910->dev->of_node,
97 "ti,en-gpio-sleep", prop_array, ngpio);
98 if (ret < 0) {
99 dev_dbg(dev, "ti,en-gpio-sleep not specified\n");
100 return tps65910_board;
101 }
102
103 for (idx = 0; idx < ngpio; idx++)
104 tps65910_board->en_gpio_sleep[idx] = (prop_array[idx] != 0);
105
106 return tps65910_board;
107}
108#else
109static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
110 struct tps65910 *tps65910, int chip_ngpio)
111{
112 return NULL;
113}
114#endif
115
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530116static int __devinit tps65910_gpio_probe(struct platform_device *pdev)
Graeme Gregory2537df72011-05-02 16:19:52 -0500117{
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530118 struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
119 struct tps65910_board *pdata = dev_get_platdata(tps65910->dev);
120 struct tps65910_gpio *tps65910_gpio;
Graeme Gregory2537df72011-05-02 16:19:52 -0500121 int ret;
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530122 int i;
Graeme Gregory2537df72011-05-02 16:19:52 -0500123
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530124 tps65910_gpio = devm_kzalloc(&pdev->dev,
125 sizeof(*tps65910_gpio), GFP_KERNEL);
126 if (!tps65910_gpio) {
127 dev_err(&pdev->dev, "Could not allocate tps65910_gpio\n");
128 return -ENOMEM;
129 }
Graeme Gregory2537df72011-05-02 16:19:52 -0500130
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530131 tps65910_gpio->tps65910 = tps65910;
132
133 tps65910_gpio->gpio_chip.owner = THIS_MODULE;
134 tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name;
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -0500135
136 switch(tps65910_chip_id(tps65910)) {
137 case TPS65910:
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530138 tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
Axel Lin58956ba2011-07-06 10:08:27 +0800139 break;
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -0500140 case TPS65911:
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530141 tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
Axel Lin58956ba2011-07-06 10:08:27 +0800142 break;
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -0500143 default:
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530144 return -EINVAL;
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -0500145 }
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530146 tps65910_gpio->gpio_chip.can_sleep = 1;
147 tps65910_gpio->gpio_chip.direction_input = tps65910_gpio_input;
148 tps65910_gpio->gpio_chip.direction_output = tps65910_gpio_output;
149 tps65910_gpio->gpio_chip.set = tps65910_gpio_set;
150 tps65910_gpio->gpio_chip.get = tps65910_gpio_get;
151 tps65910_gpio->gpio_chip.dev = &pdev->dev;
152 if (pdata && pdata->gpio_base)
153 tps65910_gpio->gpio_chip.base = pdata->gpio_base;
154 else
155 tps65910_gpio->gpio_chip.base = -1;
Graeme Gregory2537df72011-05-02 16:19:52 -0500156
Laxman Dewangan6fe02e92012-05-19 02:01:43 +0530157 if (!pdata && tps65910->dev->of_node)
158 pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
159 tps65910_gpio->gpio_chip.ngpio);
160
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530161 if (!pdata)
162 goto skip_init;
Graeme Gregory2537df72011-05-02 16:19:52 -0500163
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530164 /* Configure sleep control for gpios if provided */
165 for (i = 0; i < tps65910_gpio->gpio_chip.ngpio; ++i) {
166 if (!pdata->en_gpio_sleep[i])
167 continue;
168
169 ret = tps65910_reg_set_bits(tps65910,
170 TPS65910_GPIO0 + i, GPIO_SLEEP_MASK);
171 if (ret < 0)
172 dev_warn(tps65910->dev,
173 "GPIO Sleep setting failed with err %d\n", ret);
Laxman Dewangan9467d292012-02-01 12:09:04 +0530174 }
175
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530176skip_init:
177 ret = gpiochip_add(&tps65910_gpio->gpio_chip);
178 if (ret < 0) {
179 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
180 return ret;
181 }
Graeme Gregory2537df72011-05-02 16:19:52 -0500182
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530183 platform_set_drvdata(pdev, tps65910_gpio);
184
185 return ret;
Graeme Gregory2537df72011-05-02 16:19:52 -0500186}
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530187
188static int __devexit tps65910_gpio_remove(struct platform_device *pdev)
189{
190 struct tps65910_gpio *tps65910_gpio = platform_get_drvdata(pdev);
191
192 return gpiochip_remove(&tps65910_gpio->gpio_chip);
193}
194
195static struct platform_driver tps65910_gpio_driver = {
196 .driver.name = "tps65910-gpio",
197 .driver.owner = THIS_MODULE,
198 .probe = tps65910_gpio_probe,
199 .remove = __devexit_p(tps65910_gpio_remove),
200};
201
202static int __init tps65910_gpio_init(void)
203{
204 return platform_driver_register(&tps65910_gpio_driver);
205}
206subsys_initcall(tps65910_gpio_init);
207
208static void __exit tps65910_gpio_exit(void)
209{
210 platform_driver_unregister(&tps65910_gpio_driver);
211}
212module_exit(tps65910_gpio_exit);
213
214MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
215MODULE_AUTHOR("Jorge Eduardo Candelaria jedu@slimlogic.co.uk>");
216MODULE_DESCRIPTION("GPIO interface for TPS65910/TPS6511 PMICs");
217MODULE_LICENSE("GPL v2");
218MODULE_ALIAS("platform:tps65910-gpio");