2 * TI Palma series PMIC's GPIO driver.
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6 * Author: Laxman Dewangan <ldewangan@nvidia.com>
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.
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
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/>.
21 #include <linux/gpio.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/mfd/palmas.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
30 struct gpio_chip gpio_chip
;
31 struct palmas
*palmas
;
34 static inline struct palmas_gpio
*to_palmas_gpio(struct gpio_chip
*chip
)
36 return container_of(chip
, struct palmas_gpio
, gpio_chip
);
39 static int palmas_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
41 struct palmas_gpio
*pg
= to_palmas_gpio(gc
);
42 struct palmas
*palmas
= pg
->palmas
;
46 ret
= palmas_read(palmas
, PALMAS_GPIO_BASE
, PALMAS_GPIO_DATA_DIR
, &val
);
48 dev_err(gc
->dev
, "GPIO_DATA_DIR read failed, err = %d\n", ret
);
52 if (val
& (1 << offset
)) {
53 ret
= palmas_read(palmas
, PALMAS_GPIO_BASE
,
54 PALMAS_GPIO_DATA_OUT
, &val
);
56 ret
= palmas_read(palmas
, PALMAS_GPIO_BASE
,
57 PALMAS_GPIO_DATA_IN
, &val
);
60 dev_err(gc
->dev
, "GPIO_DATA_IN/OUT read failed, err = %d\n",
64 return !!(val
& BIT(offset
));
67 static void palmas_gpio_set(struct gpio_chip
*gc
, unsigned offset
,
70 struct palmas_gpio
*pg
= to_palmas_gpio(gc
);
71 struct palmas
*palmas
= pg
->palmas
;
75 ret
= palmas_write(palmas
, PALMAS_GPIO_BASE
,
76 PALMAS_GPIO_SET_DATA_OUT
, BIT(offset
));
78 ret
= palmas_write(palmas
, PALMAS_GPIO_BASE
,
79 PALMAS_GPIO_CLEAR_DATA_OUT
, BIT(offset
));
81 dev_err(gc
->dev
, "%s write failed, err = %d\n",
82 (value
) ? "GPIO_SET_DATA_OUT" : "GPIO_CLEAR_DATA_OUT",
86 static int palmas_gpio_output(struct gpio_chip
*gc
, unsigned offset
,
89 struct palmas_gpio
*pg
= to_palmas_gpio(gc
);
90 struct palmas
*palmas
= pg
->palmas
;
93 /* Set the initial value */
94 palmas_gpio_set(gc
, offset
, value
);
96 ret
= palmas_update_bits(palmas
, PALMAS_GPIO_BASE
,
97 PALMAS_GPIO_DATA_DIR
, BIT(offset
), BIT(offset
));
99 dev_err(gc
->dev
, "GPIO_DATA_DIR write failed, err = %d\n", ret
);
103 static int palmas_gpio_input(struct gpio_chip
*gc
, unsigned offset
)
105 struct palmas_gpio
*pg
= to_palmas_gpio(gc
);
106 struct palmas
*palmas
= pg
->palmas
;
109 ret
= palmas_update_bits(palmas
, PALMAS_GPIO_BASE
,
110 PALMAS_GPIO_DATA_DIR
, BIT(offset
), 0);
112 dev_err(gc
->dev
, "GPIO_DATA_DIR write failed, err = %d\n", ret
);
116 static int palmas_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
118 struct palmas_gpio
*pg
= to_palmas_gpio(gc
);
119 struct palmas
*palmas
= pg
->palmas
;
121 return palmas_irq_get_virq(palmas
, PALMAS_GPIO_0_IRQ
+ offset
);
124 static int palmas_gpio_probe(struct platform_device
*pdev
)
126 struct palmas
*palmas
= dev_get_drvdata(pdev
->dev
.parent
);
127 struct palmas_platform_data
*palmas_pdata
;
128 struct palmas_gpio
*palmas_gpio
;
131 palmas_gpio
= devm_kzalloc(&pdev
->dev
,
132 sizeof(*palmas_gpio
), GFP_KERNEL
);
134 dev_err(&pdev
->dev
, "Could not allocate palmas_gpio\n");
138 palmas_gpio
->palmas
= palmas
;
139 palmas_gpio
->gpio_chip
.owner
= THIS_MODULE
;
140 palmas_gpio
->gpio_chip
.label
= dev_name(&pdev
->dev
);
141 palmas_gpio
->gpio_chip
.ngpio
= 8;
142 palmas_gpio
->gpio_chip
.can_sleep
= 1;
143 palmas_gpio
->gpio_chip
.direction_input
= palmas_gpio_input
;
144 palmas_gpio
->gpio_chip
.direction_output
= palmas_gpio_output
;
145 palmas_gpio
->gpio_chip
.to_irq
= palmas_gpio_to_irq
;
146 palmas_gpio
->gpio_chip
.set
= palmas_gpio_set
;
147 palmas_gpio
->gpio_chip
.get
= palmas_gpio_get
;
148 palmas_gpio
->gpio_chip
.dev
= &pdev
->dev
;
149 #ifdef CONFIG_OF_GPIO
150 palmas_gpio
->gpio_chip
.of_node
= pdev
->dev
.of_node
;
152 palmas_pdata
= dev_get_platdata(palmas
->dev
);
153 if (palmas_pdata
&& palmas_pdata
->gpio_base
)
154 palmas_gpio
->gpio_chip
.base
= palmas_pdata
->gpio_base
;
156 palmas_gpio
->gpio_chip
.base
= -1;
158 ret
= gpiochip_add(&palmas_gpio
->gpio_chip
);
160 dev_err(&pdev
->dev
, "Could not register gpiochip, %d\n", ret
);
164 platform_set_drvdata(pdev
, palmas_gpio
);
168 static int palmas_gpio_remove(struct platform_device
*pdev
)
170 struct palmas_gpio
*palmas_gpio
= platform_get_drvdata(pdev
);
172 return gpiochip_remove(&palmas_gpio
->gpio_chip
);
175 static struct of_device_id of_palmas_gpio_match
[] = {
176 { .compatible
= "ti,palmas-gpio"},
177 { .compatible
= "ti,tps65913-gpio"},
178 { .compatible
= "ti,tps65914-gpio"},
179 { .compatible
= "ti,tps80036-gpio"},
182 MODULE_DEVICE_TABLE(of
, of_palmas_gpio_match
);
184 static struct platform_driver palmas_gpio_driver
= {
185 .driver
.name
= "palmas-gpio",
186 .driver
.owner
= THIS_MODULE
,
187 .driver
.of_match_table
= of_palmas_gpio_match
,
188 .probe
= palmas_gpio_probe
,
189 .remove
= palmas_gpio_remove
,
192 static int __init
palmas_gpio_init(void)
194 return platform_driver_register(&palmas_gpio_driver
);
196 subsys_initcall(palmas_gpio_init
);
198 static void __exit
palmas_gpio_exit(void)
200 platform_driver_unregister(&palmas_gpio_driver
);
202 module_exit(palmas_gpio_exit
);
204 MODULE_ALIAS("platform:palmas-gpio");
205 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
206 MODULE_DESCRIPTION("GPIO driver for TI Palmas series PMICs");
207 MODULE_LICENSE("GPL v2");