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/platform_device.h>
18 struct gpio_chip gpio_chip;
19 struct palmas *palmas;
22 struct palmas_device_data {
26 static int palmas_gpio_get(struct gpio_chip *gc, unsigned offset)
28 struct palmas_gpio *pg = gpiochip_get_data(gc);
29 struct palmas *palmas = pg->palmas;
33 int gpio16 = (offset/8);
36 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
38 ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
40 dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
44 if (val & BIT(offset))
45 reg = (gpio16) ? PALMAS_GPIO_DATA_OUT2 : PALMAS_GPIO_DATA_OUT;
47 reg = (gpio16) ? PALMAS_GPIO_DATA_IN2 : PALMAS_GPIO_DATA_IN;
49 ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val);
51 dev_err(gc->parent, "Reg 0x%02x read failed, %d\n", reg, ret);
54 return !!(val & BIT(offset));
57 static void palmas_gpio_set(struct gpio_chip *gc, unsigned offset,
60 struct palmas_gpio *pg = gpiochip_get_data(gc);
61 struct palmas *palmas = pg->palmas;
64 int gpio16 = (offset/8);
69 PALMAS_GPIO_SET_DATA_OUT2 : PALMAS_GPIO_CLEAR_DATA_OUT2;
72 PALMAS_GPIO_SET_DATA_OUT : PALMAS_GPIO_CLEAR_DATA_OUT;
74 ret = palmas_write(palmas, PALMAS_GPIO_BASE, reg, BIT(offset));
76 dev_err(gc->parent, "Reg 0x%02x write failed, %d\n", reg, ret);
79 static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset,
82 struct palmas_gpio *pg = gpiochip_get_data(gc);
83 struct palmas *palmas = pg->palmas;
86 int gpio16 = (offset/8);
89 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
91 /* Set the initial value */
92 palmas_gpio_set(gc, offset, value);
94 ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg,
95 BIT(offset), BIT(offset));
97 dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
102 static int palmas_gpio_input(struct gpio_chip *gc, unsigned offset)
104 struct palmas_gpio *pg = gpiochip_get_data(gc);
105 struct palmas *palmas = pg->palmas;
108 int gpio16 = (offset/8);
111 reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR;
113 ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg, BIT(offset), 0);
115 dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg,
120 static int palmas_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
122 struct palmas_gpio *pg = gpiochip_get_data(gc);
123 struct palmas *palmas = pg->palmas;
125 return palmas_irq_get_virq(palmas, PALMAS_GPIO_0_IRQ + offset);
128 static const struct palmas_device_data palmas_dev_data = {
132 static const struct palmas_device_data tps80036_dev_data = {
136 static const struct of_device_id of_palmas_gpio_match[] = {
137 { .compatible = "ti,palmas-gpio", .data = &palmas_dev_data,},
138 { .compatible = "ti,tps65913-gpio", .data = &palmas_dev_data,},
139 { .compatible = "ti,tps65914-gpio", .data = &palmas_dev_data,},
140 { .compatible = "ti,tps80036-gpio", .data = &tps80036_dev_data,},
144 static int palmas_gpio_probe(struct platform_device *pdev)
146 struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
147 struct palmas_platform_data *palmas_pdata;
148 struct palmas_gpio *palmas_gpio;
150 const struct palmas_device_data *dev_data;
152 dev_data = of_device_get_match_data(&pdev->dev);
154 dev_data = &palmas_dev_data;
156 palmas_gpio = devm_kzalloc(&pdev->dev,
157 sizeof(*palmas_gpio), GFP_KERNEL);
161 palmas_gpio->palmas = palmas;
162 palmas_gpio->gpio_chip.owner = THIS_MODULE;
163 palmas_gpio->gpio_chip.label = dev_name(&pdev->dev);
164 palmas_gpio->gpio_chip.ngpio = dev_data->ngpio;
165 palmas_gpio->gpio_chip.can_sleep = true;
166 palmas_gpio->gpio_chip.direction_input = palmas_gpio_input;
167 palmas_gpio->gpio_chip.direction_output = palmas_gpio_output;
168 palmas_gpio->gpio_chip.to_irq = palmas_gpio_to_irq;
169 palmas_gpio->gpio_chip.set = palmas_gpio_set;
170 palmas_gpio->gpio_chip.get = palmas_gpio_get;
171 palmas_gpio->gpio_chip.parent = &pdev->dev;
173 palmas_pdata = dev_get_platdata(palmas->dev);
174 if (palmas_pdata && palmas_pdata->gpio_base)
175 palmas_gpio->gpio_chip.base = palmas_pdata->gpio_base;
177 palmas_gpio->gpio_chip.base = -1;
179 ret = devm_gpiochip_add_data(&pdev->dev, &palmas_gpio->gpio_chip,
182 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
189 static struct platform_driver palmas_gpio_driver = {
190 .driver.name = "palmas-gpio",
191 .driver.of_match_table = of_palmas_gpio_match,
192 .probe = palmas_gpio_probe,
195 static int __init palmas_gpio_init(void)
197 return platform_driver_register(&palmas_gpio_driver);
199 subsys_initcall(palmas_gpio_init);