blob: 3d818195e351a7e6d16138bd38b1d81abd2ac0ba [file] [log] [blame]
Laxman Dewangan3d50a272013-01-03 16:16:59 +05301/*
2 * TI Palma series PMIC's GPIO driver.
3 *
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
5 *
6 * Author: Laxman Dewangan <ldewangan@nvidia.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <linux/gpio.h>
22#include <linux/kernel.h>
Paul Gortmakerf9f2b5c2016-04-01 14:49:36 -040023#include <linux/init.h>
Laxman Dewangan3d50a272013-01-03 16:16:59 +053024#include <linux/mfd/palmas.h>
25#include <linux/of.h>
26#include <linux/of_device.h>
27#include <linux/platform_device.h>
28
29struct palmas_gpio {
30 struct gpio_chip gpio_chip;
31 struct palmas *palmas;
32};
33
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053034struct palmas_device_data {
35 int ngpio;
36};
37
Laxman Dewangan3d50a272013-01-03 16:16:59 +053038static int palmas_gpio_get(struct gpio_chip *gc, unsigned offset)
39{
Linus Walleij5b68cc22015-12-07 11:18:17 +010040 struct palmas_gpio *pg = gpiochip_get_data(gc);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053041 struct palmas *palmas = pg->palmas;
42 unsigned int val;
43 int ret;
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053044 unsigned int reg;
45 int gpio16 = (offset/8);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053046
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053047 offset %= 8;
48 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
49
50 ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053051 if (ret < 0) {
Linus Walleij58383c782015-11-04 09:56:26 +010052 dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
Andrew Chew8b628c62013-07-26 13:39:53 -070053 return ret;
54 }
55
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053056 if (val & BIT(offset))
57 reg = (gpio16) ? PALMAS_GPIO_DATA_OUT2 : PALMAS_GPIO_DATA_OUT;
58 else
59 reg = (gpio16) ? PALMAS_GPIO_DATA_IN2 : PALMAS_GPIO_DATA_IN;
60
61 ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
Andrew Chew8b628c62013-07-26 13:39:53 -070062 if (ret < 0) {
Linus Walleij58383c782015-11-04 09:56:26 +010063 dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053064 return ret;
65 }
66 return !!(val & BIT(offset));
67}
68
69static void palmas_gpio_set(struct gpio_chip *gc, unsigned offset,
70 int value)
71{
Linus Walleij5b68cc22015-12-07 11:18:17 +010072 struct palmas_gpio *pg = gpiochip_get_data(gc);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053073 struct palmas *palmas = pg->palmas;
74 int ret;
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053075 unsigned int reg;
76 int gpio16 = (offset/8);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053077
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053078 offset %= 8;
79 if (gpio16)
80 reg = (value) ?
81 PALMAS_GPIO_SET_DATA_OUT2 : PALMAS_GPIO_CLEAR_DATA_OUT2;
Laxman Dewangan3d50a272013-01-03 16:16:59 +053082 else
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053083 reg = (value) ?
84 PALMAS_GPIO_SET_DATA_OUT : PALMAS_GPIO_CLEAR_DATA_OUT;
85
86 ret = palmas_write(palmas, PALMAS_GPIO_BASE, reg, BIT(offset));
Laxman Dewangan3d50a272013-01-03 16:16:59 +053087 if (ret < 0)
Linus Walleij58383c782015-11-04 09:56:26 +010088 dev_err(gc->parent, "Reg 0x%02x write failed, %d\n", reg, ret);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053089}
90
91static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset,
92 int value)
93{
Linus Walleij5b68cc22015-12-07 11:18:17 +010094 struct palmas_gpio *pg = gpiochip_get_data(gc);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053095 struct palmas *palmas = pg->palmas;
96 int ret;
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053097 unsigned int reg;
98 int gpio16 = (offset/8);
99
100 offset %= 8;
101 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530102
103 /* Set the initial value */
104 palmas_gpio_set(gc, offset, value);
105
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530106 ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg,
107 BIT(offset), BIT(offset));
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530108 if (ret < 0)
Linus Walleij58383c782015-11-04 09:56:26 +0100109 dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
110 ret);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530111 return ret;
112}
113
114static int palmas_gpio_input(struct gpio_chip *gc, unsigned offset)
115{
Linus Walleij5b68cc22015-12-07 11:18:17 +0100116 struct palmas_gpio *pg = gpiochip_get_data(gc);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530117 struct palmas *palmas = pg->palmas;
118 int ret;
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530119 unsigned int reg;
120 int gpio16 = (offset/8);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530121
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530122 offset %= 8;
123 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
124
125 ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg, BIT(offset), 0);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530126 if (ret < 0)
Linus Walleij58383c782015-11-04 09:56:26 +0100127 dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
128 ret);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530129 return ret;
130}
131
132static int palmas_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
133{
Linus Walleij5b68cc22015-12-07 11:18:17 +0100134 struct palmas_gpio *pg = gpiochip_get_data(gc);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530135 struct palmas *palmas = pg->palmas;
136
137 return palmas_irq_get_virq(palmas, PALMAS_GPIO_0_IRQ + offset);
138}
139
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530140static const struct palmas_device_data palmas_dev_data = {
141 .ngpio = 8,
142};
143
144static const struct palmas_device_data tps80036_dev_data = {
145 .ngpio = 16,
146};
147
Jingoo Han722782f2014-05-07 18:06:50 +0900148static const struct of_device_id of_palmas_gpio_match[] = {
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530149 { .compatible = "ti,palmas-gpio", .data = &palmas_dev_data,},
150 { .compatible = "ti,tps65913-gpio", .data = &palmas_dev_data,},
151 { .compatible = "ti,tps65914-gpio", .data = &palmas_dev_data,},
152 { .compatible = "ti,tps80036-gpio", .data = &tps80036_dev_data,},
153 { },
154};
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530155
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530156static int palmas_gpio_probe(struct platform_device *pdev)
157{
158 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
159 struct palmas_platform_data *palmas_pdata;
160 struct palmas_gpio *palmas_gpio;
161 int ret;
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530162 const struct of_device_id *match;
163 const struct palmas_device_data *dev_data;
164
165 match = of_match_device(of_palmas_gpio_match, &pdev->dev);
LABBE Corentin5664de22015-11-11 21:27:36 +0100166 if (!match)
167 return -ENODEV;
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530168 dev_data = match->data;
169 if (!dev_data)
170 dev_data = &palmas_dev_data;
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530171
172 palmas_gpio = devm_kzalloc(&pdev->dev,
173 sizeof(*palmas_gpio), GFP_KERNEL);
Jingoo Han5605beb2014-04-29 17:39:07 +0900174 if (!palmas_gpio)
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530175 return -ENOMEM;
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530176
177 palmas_gpio->palmas = palmas;
178 palmas_gpio->gpio_chip.owner = THIS_MODULE;
179 palmas_gpio->gpio_chip.label = dev_name(&pdev->dev);
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530180 palmas_gpio->gpio_chip.ngpio = dev_data->ngpio;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100181 palmas_gpio->gpio_chip.can_sleep = true;
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530182 palmas_gpio->gpio_chip.direction_input = palmas_gpio_input;
183 palmas_gpio->gpio_chip.direction_output = palmas_gpio_output;
184 palmas_gpio->gpio_chip.to_irq = palmas_gpio_to_irq;
185 palmas_gpio->gpio_chip.set = palmas_gpio_set;
186 palmas_gpio->gpio_chip.get = palmas_gpio_get;
Linus Walleij58383c782015-11-04 09:56:26 +0100187 palmas_gpio->gpio_chip.parent = &pdev->dev;
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530188#ifdef CONFIG_OF_GPIO
Laxman Dewangan57633182013-07-23 13:06:04 +0530189 palmas_gpio->gpio_chip.of_node = pdev->dev.of_node;
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530190#endif
191 palmas_pdata = dev_get_platdata(palmas->dev);
192 if (palmas_pdata && palmas_pdata->gpio_base)
193 palmas_gpio->gpio_chip.base = palmas_pdata->gpio_base;
194 else
195 palmas_gpio->gpio_chip.base = -1;
196
Laxman Dewangan297bf062016-02-22 17:43:28 +0530197 ret = devm_gpiochip_add_data(&pdev->dev, &palmas_gpio->gpio_chip,
198 palmas_gpio);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530199 if (ret < 0) {
200 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
201 return ret;
202 }
203
204 platform_set_drvdata(pdev, palmas_gpio);
205 return ret;
206}
207
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530208static struct platform_driver palmas_gpio_driver = {
209 .driver.name = "palmas-gpio",
Laxman Dewangan57633182013-07-23 13:06:04 +0530210 .driver.of_match_table = of_palmas_gpio_match,
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530211 .probe = palmas_gpio_probe,
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530212};
213
214static int __init palmas_gpio_init(void)
215{
216 return platform_driver_register(&palmas_gpio_driver);
217}
218subsys_initcall(palmas_gpio_init);