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_IN
, &val
);
48 dev_err(gc
->dev
, "GPIO_DATA_IN read failed, err = %d\n", ret
);
51 return !!(val
& BIT(offset
));
54 static void palmas_gpio_set(struct gpio_chip
*gc
, unsigned offset
,
57 struct palmas_gpio
*pg
= to_palmas_gpio(gc
);
58 struct palmas
*palmas
= pg
->palmas
;
62 ret
= palmas_write(palmas
, PALMAS_GPIO_BASE
,
63 PALMAS_GPIO_SET_DATA_OUT
, BIT(offset
));
65 ret
= palmas_write(palmas
, PALMAS_GPIO_BASE
,
66 PALMAS_GPIO_CLEAR_DATA_OUT
, BIT(offset
));
68 dev_err(gc
->dev
, "%s write failed, err = %d\n",
69 (value
) ? "GPIO_SET_DATA_OUT" : "GPIO_CLEAR_DATA_OUT",
73 static int palmas_gpio_output(struct gpio_chip
*gc
, unsigned offset
,
76 struct palmas_gpio
*pg
= to_palmas_gpio(gc
);
77 struct palmas
*palmas
= pg
->palmas
;
80 /* Set the initial value */
81 palmas_gpio_set(gc
, offset
, value
);
83 ret
= palmas_update_bits(palmas
, PALMAS_GPIO_BASE
,
84 PALMAS_GPIO_DATA_DIR
, BIT(offset
), BIT(offset
));
86 dev_err(gc
->dev
, "GPIO_DATA_DIR write failed, err = %d\n", ret
);
90 static int palmas_gpio_input(struct gpio_chip
*gc
, unsigned offset
)
92 struct palmas_gpio
*pg
= to_palmas_gpio(gc
);
93 struct palmas
*palmas
= pg
->palmas
;
96 ret
= palmas_update_bits(palmas
, PALMAS_GPIO_BASE
,
97 PALMAS_GPIO_DATA_DIR
, BIT(offset
), 0);
99 dev_err(gc
->dev
, "GPIO_DATA_DIR write failed, err = %d\n", ret
);
103 static int palmas_gpio_to_irq(struct gpio_chip
*gc
, unsigned offset
)
105 struct palmas_gpio
*pg
= to_palmas_gpio(gc
);
106 struct palmas
*palmas
= pg
->palmas
;
108 return palmas_irq_get_virq(palmas
, PALMAS_GPIO_0_IRQ
+ offset
);
111 static int palmas_gpio_probe(struct platform_device
*pdev
)
113 struct palmas
*palmas
= dev_get_drvdata(pdev
->dev
.parent
);
114 struct palmas_platform_data
*palmas_pdata
;
115 struct palmas_gpio
*palmas_gpio
;
118 palmas_gpio
= devm_kzalloc(&pdev
->dev
,
119 sizeof(*palmas_gpio
), GFP_KERNEL
);
121 dev_err(&pdev
->dev
, "Could not allocate palmas_gpio\n");
125 palmas_gpio
->palmas
= palmas
;
126 palmas_gpio
->gpio_chip
.owner
= THIS_MODULE
;
127 palmas_gpio
->gpio_chip
.label
= dev_name(&pdev
->dev
);
128 palmas_gpio
->gpio_chip
.ngpio
= 8;
129 palmas_gpio
->gpio_chip
.can_sleep
= 1;
130 palmas_gpio
->gpio_chip
.direction_input
= palmas_gpio_input
;
131 palmas_gpio
->gpio_chip
.direction_output
= palmas_gpio_output
;
132 palmas_gpio
->gpio_chip
.to_irq
= palmas_gpio_to_irq
;
133 palmas_gpio
->gpio_chip
.set
= palmas_gpio_set
;
134 palmas_gpio
->gpio_chip
.get
= palmas_gpio_get
;
135 palmas_gpio
->gpio_chip
.dev
= &pdev
->dev
;
136 #ifdef CONFIG_OF_GPIO
137 palmas_gpio
->gpio_chip
.of_node
= palmas
->dev
->of_node
;
139 palmas_pdata
= dev_get_platdata(palmas
->dev
);
140 if (palmas_pdata
&& palmas_pdata
->gpio_base
)
141 palmas_gpio
->gpio_chip
.base
= palmas_pdata
->gpio_base
;
143 palmas_gpio
->gpio_chip
.base
= -1;
145 ret
= gpiochip_add(&palmas_gpio
->gpio_chip
);
147 dev_err(&pdev
->dev
, "Could not register gpiochip, %d\n", ret
);
151 platform_set_drvdata(pdev
, palmas_gpio
);
155 static int palmas_gpio_remove(struct platform_device
*pdev
)
157 struct palmas_gpio
*palmas_gpio
= platform_get_drvdata(pdev
);
159 return gpiochip_remove(&palmas_gpio
->gpio_chip
);
162 static struct platform_driver palmas_gpio_driver
= {
163 .driver
.name
= "palmas-gpio",
164 .driver
.owner
= THIS_MODULE
,
165 .probe
= palmas_gpio_probe
,
166 .remove
= palmas_gpio_remove
,
169 static int __init
palmas_gpio_init(void)
171 return platform_driver_register(&palmas_gpio_driver
);
173 subsys_initcall(palmas_gpio_init
);
175 static void __exit
palmas_gpio_exit(void)
177 platform_driver_unregister(&palmas_gpio_driver
);
179 module_exit(palmas_gpio_exit
);
181 MODULE_ALIAS("platform:palmas-gpio");
182 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
183 MODULE_DESCRIPTION("GPIO driver for TI Palmas series PMICs");
184 MODULE_LICENSE("GPL v2");