1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI Palma series PMIC's GPIO driver.
5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
7 * Author: Laxman Dewangan <ldewangan@nvidia.com>
10 #include <linux/gpio/driver.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/mfd/palmas.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
19 struct gpio_chip gpio_chip;
20 struct palmas *palmas;
23 struct palmas_device_data {
27 static int palmas_gpio_get(struct gpio_chip *gc, unsigned offset)
29 struct palmas_gpio *pg = gpiochip_get_data(gc);
30 struct palmas *palmas = pg->palmas;
34 int gpio16 = (offset/8);
37 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
39 ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
41 dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
45 if (val & BIT(offset))
46 reg = (gpio16) ? PALMAS_GPIO_DATA_OUT2 : PALMAS_GPIO_DATA_OUT;
48 reg = (gpio16) ? PALMAS_GPIO_DATA_IN2 : PALMAS_GPIO_DATA_IN;
50 ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
52 dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
55 return !!(val & BIT(offset));
58 static void palmas_gpio_set(struct gpio_chip *gc, unsigned offset,
61 struct palmas_gpio *pg = gpiochip_get_data(gc);
62 struct palmas *palmas = pg->palmas;
65 int gpio16 = (offset/8);
70 PALMAS_GPIO_SET_DATA_OUT2 : PALMAS_GPIO_CLEAR_DATA_OUT2;
73 PALMAS_GPIO_SET_DATA_OUT : PALMAS_GPIO_CLEAR_DATA_OUT;
75 ret = palmas_write(palmas, PALMAS_GPIO_BASE, reg, BIT(offset));
77 dev_err(gc->parent, "Reg 0x%02x write failed, %d\n", reg, ret);
80 static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset,
83 struct palmas_gpio *pg = gpiochip_get_data(gc);
84 struct palmas *palmas = pg->palmas;
87 int gpio16 = (offset/8);
90 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
92 /* Set the initial value */
93 palmas_gpio_set(gc, offset, value);
95 ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg,
96 BIT(offset), BIT(offset));
98 dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
103 static int palmas_gpio_input(struct gpio_chip *gc, unsigned offset)
105 struct palmas_gpio *pg = gpiochip_get_data(gc);
106 struct palmas *palmas = pg->palmas;
109 int gpio16 = (offset/8);
112 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
114 ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg, BIT(offset), 0);
116 dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
121 static int palmas_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
123 struct palmas_gpio *pg = gpiochip_get_data(gc);
124 struct palmas *palmas = pg->palmas;
126 return palmas_irq_get_virq(palmas, PALMAS_GPIO_0_IRQ + offset);
129 static const struct palmas_device_data palmas_dev_data = {
133 static const struct palmas_device_data tps80036_dev_data = {
137 static const struct of_device_id of_palmas_gpio_match[] = {
138 { .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,},
145 static int palmas_gpio_probe(struct platform_device *pdev)
147 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
148 struct palmas_platform_data *palmas_pdata;
149 struct palmas_gpio *palmas_gpio;
151 const struct palmas_device_data *dev_data;
153 dev_data = of_device_get_match_data(&pdev->dev);
155 dev_data = &palmas_dev_data;
157 palmas_gpio = devm_kzalloc(&pdev->dev,
158 sizeof(*palmas_gpio), GFP_KERNEL);
162 palmas_gpio->palmas = palmas;
163 palmas_gpio->gpio_chip.owner = THIS_MODULE;
164 palmas_gpio->gpio_chip.label = dev_name(&pdev->dev);
165 palmas_gpio->gpio_chip.ngpio = dev_data->ngpio;
166 palmas_gpio->gpio_chip.can_sleep = true;
167 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;
172 palmas_gpio->gpio_chip.parent = &pdev->dev;
174 palmas_pdata = dev_get_platdata(palmas->dev);
175 if (palmas_pdata && palmas_pdata->gpio_base)
176 palmas_gpio->gpio_chip.base = palmas_pdata->gpio_base;
178 palmas_gpio->gpio_chip.base = -1;
180 ret = devm_gpiochip_add_data(&pdev->dev, &palmas_gpio->gpio_chip,
183 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
187 platform_set_drvdata(pdev, palmas_gpio);
191 static struct platform_driver palmas_gpio_driver = {
192 .driver.name = "palmas-gpio",
193 .driver.of_match_table = of_palmas_gpio_match,
194 .probe = palmas_gpio_probe,
197 static int __init palmas_gpio_init(void)
199 return platform_driver_register(&palmas_gpio_driver);
201 subsys_initcall(palmas_gpio_init);