blob: e8e9029ba5bd80a88809e9e8f509461d2da0339b [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Laxman Dewangan3d50a272013-01-03 16:16:59 +05302/*
3 * TI Palma series PMIC's GPIO driver.
4 *
5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6 *
7 * Author: Laxman Dewangan <ldewangan@nvidia.com>
Laxman Dewangan3d50a272013-01-03 16:16:59 +05308 */
9
Linus Walleijb11a0b32018-05-24 14:25:13 +020010#include <linux/gpio/driver.h>
Laxman Dewangan3d50a272013-01-03 16:16:59 +053011#include <linux/kernel.h>
Paul Gortmakerf9f2b5c2016-04-01 14:49:36 -040012#include <linux/init.h>
Laxman Dewangan3d50a272013-01-03 16:16:59 +053013#include <linux/mfd/palmas.h>
14#include <linux/of.h>
15#include <linux/of_device.h>
16#include <linux/platform_device.h>
17
18struct palmas_gpio {
19 struct gpio_chip gpio_chip;
20 struct palmas *palmas;
21};
22
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053023struct palmas_device_data {
24 int ngpio;
25};
26
Laxman Dewangan3d50a272013-01-03 16:16:59 +053027static int palmas_gpio_get(struct gpio_chip *gc, unsigned offset)
28{
Linus Walleij5b68cc22015-12-07 11:18:17 +010029 struct palmas_gpio *pg = gpiochip_get_data(gc);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053030 struct palmas *palmas = pg->palmas;
31 unsigned int val;
32 int ret;
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053033 unsigned int reg;
34 int gpio16 = (offset/8);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053035
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053036 offset %= 8;
37 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
38
39 ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053040 if (ret < 0) {
Linus Walleij58383c782015-11-04 09:56:26 +010041 dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
Andrew Chew8b628c62013-07-26 13:39:53 -070042 return ret;
43 }
44
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053045 if (val & BIT(offset))
46 reg = (gpio16) ? PALMAS_GPIO_DATA_OUT2 : PALMAS_GPIO_DATA_OUT;
47 else
48 reg = (gpio16) ? PALMAS_GPIO_DATA_IN2 : PALMAS_GPIO_DATA_IN;
49
50 ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
Andrew Chew8b628c62013-07-26 13:39:53 -070051 if (ret < 0) {
Linus Walleij58383c782015-11-04 09:56:26 +010052 dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053053 return ret;
54 }
55 return !!(val & BIT(offset));
56}
57
58static void palmas_gpio_set(struct gpio_chip *gc, unsigned offset,
59 int value)
60{
Linus Walleij5b68cc22015-12-07 11:18:17 +010061 struct palmas_gpio *pg = gpiochip_get_data(gc);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053062 struct palmas *palmas = pg->palmas;
63 int ret;
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053064 unsigned int reg;
65 int gpio16 = (offset/8);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053066
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053067 offset %= 8;
68 if (gpio16)
69 reg = (value) ?
70 PALMAS_GPIO_SET_DATA_OUT2 : PALMAS_GPIO_CLEAR_DATA_OUT2;
Laxman Dewangan3d50a272013-01-03 16:16:59 +053071 else
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053072 reg = (value) ?
73 PALMAS_GPIO_SET_DATA_OUT : PALMAS_GPIO_CLEAR_DATA_OUT;
74
75 ret = palmas_write(palmas, PALMAS_GPIO_BASE, reg, BIT(offset));
Laxman Dewangan3d50a272013-01-03 16:16:59 +053076 if (ret < 0)
Linus Walleij58383c782015-11-04 09:56:26 +010077 dev_err(gc->parent, "Reg 0x%02x write failed, %d\n", reg, ret);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053078}
79
80static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset,
81 int value)
82{
Linus Walleij5b68cc22015-12-07 11:18:17 +010083 struct palmas_gpio *pg = gpiochip_get_data(gc);
Laxman Dewangan3d50a272013-01-03 16:16:59 +053084 struct palmas *palmas = pg->palmas;
85 int ret;
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053086 unsigned int reg;
87 int gpio16 = (offset/8);
88
89 offset %= 8;
90 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
Laxman Dewangan3d50a272013-01-03 16:16:59 +053091
92 /* Set the initial value */
93 palmas_gpio_set(gc, offset, value);
94
Laxman Dewanganca6af7b2013-09-03 19:58:03 +053095 ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg,
96 BIT(offset), BIT(offset));
Laxman Dewangan3d50a272013-01-03 16:16:59 +053097 if (ret < 0)
Linus Walleij58383c782015-11-04 09:56:26 +010098 dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
99 ret);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530100 return ret;
101}
102
103static int palmas_gpio_input(struct gpio_chip *gc, unsigned offset)
104{
Linus Walleij5b68cc22015-12-07 11:18:17 +0100105 struct palmas_gpio *pg = gpiochip_get_data(gc);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530106 struct palmas *palmas = pg->palmas;
107 int ret;
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530108 unsigned int reg;
109 int gpio16 = (offset/8);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530110
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530111 offset %= 8;
112 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
113
114 ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg, BIT(offset), 0);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530115 if (ret < 0)
Linus Walleij58383c782015-11-04 09:56:26 +0100116 dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
117 ret);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530118 return ret;
119}
120
121static int palmas_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
122{
Linus Walleij5b68cc22015-12-07 11:18:17 +0100123 struct palmas_gpio *pg = gpiochip_get_data(gc);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530124 struct palmas *palmas = pg->palmas;
125
126 return palmas_irq_get_virq(palmas, PALMAS_GPIO_0_IRQ + offset);
127}
128
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530129static const struct palmas_device_data palmas_dev_data = {
130 .ngpio = 8,
131};
132
133static const struct palmas_device_data tps80036_dev_data = {
134 .ngpio = 16,
135};
136
Jingoo Han722782f2014-05-07 18:06:50 +0900137static const struct of_device_id of_palmas_gpio_match[] = {
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530138 { .compatible = "ti,palmas-gpio", .data = &palmas_dev_data,},
139 { .compatible = "ti,tps65913-gpio", .data = &palmas_dev_data,},
140 { .compatible = "ti,tps65914-gpio", .data = &palmas_dev_data,},
141 { .compatible = "ti,tps80036-gpio", .data = &tps80036_dev_data,},
142 { },
143};
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530144
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530145static int palmas_gpio_probe(struct platform_device *pdev)
146{
147 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
148 struct palmas_platform_data *palmas_pdata;
149 struct palmas_gpio *palmas_gpio;
150 int ret;
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530151 const struct palmas_device_data *dev_data;
152
Thierry Redingfd46d5c2018-04-30 09:38:13 +0200153 dev_data = of_device_get_match_data(&pdev->dev);
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530154 if (!dev_data)
155 dev_data = &palmas_dev_data;
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530156
157 palmas_gpio = devm_kzalloc(&pdev->dev,
158 sizeof(*palmas_gpio), GFP_KERNEL);
Jingoo Han5605beb2014-04-29 17:39:07 +0900159 if (!palmas_gpio)
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530160 return -ENOMEM;
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530161
162 palmas_gpio->palmas = palmas;
163 palmas_gpio->gpio_chip.owner = THIS_MODULE;
164 palmas_gpio->gpio_chip.label = dev_name(&pdev->dev);
Laxman Dewanganca6af7b2013-09-03 19:58:03 +0530165 palmas_gpio->gpio_chip.ngpio = dev_data->ngpio;
Linus Walleij9fb1f392013-12-04 14:42:46 +0100166 palmas_gpio->gpio_chip.can_sleep = true;
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530167 palmas_gpio->gpio_chip.direction_input = palmas_gpio_input;
168 palmas_gpio->gpio_chip.direction_output = palmas_gpio_output;
169 palmas_gpio->gpio_chip.to_irq = palmas_gpio_to_irq;
170 palmas_gpio->gpio_chip.set = palmas_gpio_set;
171 palmas_gpio->gpio_chip.get = palmas_gpio_get;
Linus Walleij58383c782015-11-04 09:56:26 +0100172 palmas_gpio->gpio_chip.parent = &pdev->dev;
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530173#ifdef CONFIG_OF_GPIO
Laxman Dewangan57633182013-07-23 13:06:04 +0530174 palmas_gpio->gpio_chip.of_node = pdev->dev.of_node;
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530175#endif
176 palmas_pdata = dev_get_platdata(palmas->dev);
177 if (palmas_pdata && palmas_pdata->gpio_base)
178 palmas_gpio->gpio_chip.base = palmas_pdata->gpio_base;
179 else
180 palmas_gpio->gpio_chip.base = -1;
181
Laxman Dewangan297bf062016-02-22 17:43:28 +0530182 ret = devm_gpiochip_add_data(&pdev->dev, &palmas_gpio->gpio_chip,
183 palmas_gpio);
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530184 if (ret < 0) {
185 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
186 return ret;
187 }
188
189 platform_set_drvdata(pdev, palmas_gpio);
190 return ret;
191}
192
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530193static struct platform_driver palmas_gpio_driver = {
194 .driver.name = "palmas-gpio",
Laxman Dewangan57633182013-07-23 13:06:04 +0530195 .driver.of_match_table = of_palmas_gpio_match,
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530196 .probe = palmas_gpio_probe,
Laxman Dewangan3d50a272013-01-03 16:16:59 +0530197};
198
199static int __init palmas_gpio_init(void)
200{
201 return platform_driver_register(&palmas_gpio_driver);
202}
203subsys_initcall(palmas_gpio_init);