Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 1 | /* |
| 2 | * TI Palma series PMIC's GPIO driver. |
| 3 | * |
| 4 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
| 5 | * |
| 6 | * Author: Laxman Dewangan <ldewangan@nvidia.com> |
| 7 | * |
| 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. |
| 11 | * |
| 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 |
| 15 | * more details. |
| 16 | * |
| 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/>. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/gpio.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/mfd/palmas.h> |
| 25 | #include <linux/of.h> |
| 26 | #include <linux/of_device.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | |
| 29 | struct palmas_gpio { |
| 30 | struct gpio_chip gpio_chip; |
| 31 | struct palmas *palmas; |
| 32 | }; |
| 33 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 34 | struct palmas_device_data { |
| 35 | int ngpio; |
| 36 | }; |
| 37 | |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 38 | static inline struct palmas_gpio *to_palmas_gpio(struct gpio_chip *chip) |
| 39 | { |
| 40 | return container_of(chip, struct palmas_gpio, gpio_chip); |
| 41 | } |
| 42 | |
| 43 | static int palmas_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 44 | { |
| 45 | struct palmas_gpio *pg = to_palmas_gpio(gc); |
| 46 | struct palmas *palmas = pg->palmas; |
| 47 | unsigned int val; |
| 48 | int ret; |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 49 | unsigned int reg; |
| 50 | int gpio16 = (offset/8); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 51 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 52 | offset %= 8; |
| 53 | reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR; |
| 54 | |
| 55 | ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 56 | if (ret < 0) { |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 57 | dev_err(gc->dev, "Reg 0x%02x read failed, %d\n", reg, ret); |
Andrew Chew | 8b628c6 | 2013-07-26 13:39:53 -0700 | [diff] [blame] | 58 | return ret; |
| 59 | } |
| 60 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 61 | if (val & BIT(offset)) |
| 62 | reg = (gpio16) ? PALMAS_GPIO_DATA_OUT2 : PALMAS_GPIO_DATA_OUT; |
| 63 | else |
| 64 | reg = (gpio16) ? PALMAS_GPIO_DATA_IN2 : PALMAS_GPIO_DATA_IN; |
| 65 | |
| 66 | ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val); |
Andrew Chew | 8b628c6 | 2013-07-26 13:39:53 -0700 | [diff] [blame] | 67 | if (ret < 0) { |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 68 | dev_err(gc->dev, "Reg 0x%02x read failed, %d\n", reg, ret); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 69 | return ret; |
| 70 | } |
| 71 | return !!(val & BIT(offset)); |
| 72 | } |
| 73 | |
| 74 | static void palmas_gpio_set(struct gpio_chip *gc, unsigned offset, |
| 75 | int value) |
| 76 | { |
| 77 | struct palmas_gpio *pg = to_palmas_gpio(gc); |
| 78 | struct palmas *palmas = pg->palmas; |
| 79 | int ret; |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 80 | unsigned int reg; |
| 81 | int gpio16 = (offset/8); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 82 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 83 | offset %= 8; |
| 84 | if (gpio16) |
| 85 | reg = (value) ? |
| 86 | PALMAS_GPIO_SET_DATA_OUT2 : PALMAS_GPIO_CLEAR_DATA_OUT2; |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 87 | else |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 88 | reg = (value) ? |
| 89 | PALMAS_GPIO_SET_DATA_OUT : PALMAS_GPIO_CLEAR_DATA_OUT; |
| 90 | |
| 91 | ret = palmas_write(palmas, PALMAS_GPIO_BASE, reg, BIT(offset)); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 92 | if (ret < 0) |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 93 | dev_err(gc->dev, "Reg 0x%02x write failed, %d\n", reg, ret); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset, |
| 97 | int value) |
| 98 | { |
| 99 | struct palmas_gpio *pg = to_palmas_gpio(gc); |
| 100 | struct palmas *palmas = pg->palmas; |
| 101 | int ret; |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 102 | unsigned int reg; |
| 103 | int gpio16 = (offset/8); |
| 104 | |
| 105 | offset %= 8; |
| 106 | reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR; |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 107 | |
| 108 | /* Set the initial value */ |
| 109 | palmas_gpio_set(gc, offset, value); |
| 110 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 111 | ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg, |
| 112 | BIT(offset), BIT(offset)); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 113 | if (ret < 0) |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 114 | dev_err(gc->dev, "Reg 0x%02x update failed, %d\n", reg, ret); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | static int palmas_gpio_input(struct gpio_chip *gc, unsigned offset) |
| 119 | { |
| 120 | struct palmas_gpio *pg = to_palmas_gpio(gc); |
| 121 | struct palmas *palmas = pg->palmas; |
| 122 | int ret; |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 123 | unsigned int reg; |
| 124 | int gpio16 = (offset/8); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 125 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 126 | offset %= 8; |
| 127 | reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR; |
| 128 | |
| 129 | ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg, BIT(offset), 0); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 130 | if (ret < 0) |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 131 | dev_err(gc->dev, "Reg 0x%02x update failed, %d\n", reg, ret); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | static int palmas_gpio_to_irq(struct gpio_chip *gc, unsigned offset) |
| 136 | { |
| 137 | struct palmas_gpio *pg = to_palmas_gpio(gc); |
| 138 | struct palmas *palmas = pg->palmas; |
| 139 | |
| 140 | return palmas_irq_get_virq(palmas, PALMAS_GPIO_0_IRQ + offset); |
| 141 | } |
| 142 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 143 | static const struct palmas_device_data palmas_dev_data = { |
| 144 | .ngpio = 8, |
| 145 | }; |
| 146 | |
| 147 | static const struct palmas_device_data tps80036_dev_data = { |
| 148 | .ngpio = 16, |
| 149 | }; |
| 150 | |
Jingoo Han | 722782f | 2014-05-07 18:06:50 +0900 | [diff] [blame] | 151 | static const struct of_device_id of_palmas_gpio_match[] = { |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 152 | { .compatible = "ti,palmas-gpio", .data = &palmas_dev_data,}, |
| 153 | { .compatible = "ti,tps65913-gpio", .data = &palmas_dev_data,}, |
| 154 | { .compatible = "ti,tps65914-gpio", .data = &palmas_dev_data,}, |
| 155 | { .compatible = "ti,tps80036-gpio", .data = &tps80036_dev_data,}, |
| 156 | { }, |
| 157 | }; |
| 158 | MODULE_DEVICE_TABLE(of, of_palmas_gpio_match); |
| 159 | |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 160 | static int palmas_gpio_probe(struct platform_device *pdev) |
| 161 | { |
| 162 | struct palmas *palmas = dev_get_drvdata(pdev->dev.parent); |
| 163 | struct palmas_platform_data *palmas_pdata; |
| 164 | struct palmas_gpio *palmas_gpio; |
| 165 | int ret; |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 166 | const struct of_device_id *match; |
| 167 | const struct palmas_device_data *dev_data; |
| 168 | |
| 169 | match = of_match_device(of_palmas_gpio_match, &pdev->dev); |
| 170 | dev_data = match->data; |
| 171 | if (!dev_data) |
| 172 | dev_data = &palmas_dev_data; |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 173 | |
| 174 | palmas_gpio = devm_kzalloc(&pdev->dev, |
| 175 | sizeof(*palmas_gpio), GFP_KERNEL); |
Jingoo Han | 5605beb | 2014-04-29 17:39:07 +0900 | [diff] [blame] | 176 | if (!palmas_gpio) |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 177 | return -ENOMEM; |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 178 | |
| 179 | palmas_gpio->palmas = palmas; |
| 180 | palmas_gpio->gpio_chip.owner = THIS_MODULE; |
| 181 | palmas_gpio->gpio_chip.label = dev_name(&pdev->dev); |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 182 | palmas_gpio->gpio_chip.ngpio = dev_data->ngpio; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 183 | palmas_gpio->gpio_chip.can_sleep = true; |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 184 | palmas_gpio->gpio_chip.direction_input = palmas_gpio_input; |
| 185 | palmas_gpio->gpio_chip.direction_output = palmas_gpio_output; |
| 186 | palmas_gpio->gpio_chip.to_irq = palmas_gpio_to_irq; |
| 187 | palmas_gpio->gpio_chip.set = palmas_gpio_set; |
| 188 | palmas_gpio->gpio_chip.get = palmas_gpio_get; |
| 189 | palmas_gpio->gpio_chip.dev = &pdev->dev; |
| 190 | #ifdef CONFIG_OF_GPIO |
Laxman Dewangan | 5763318 | 2013-07-23 13:06:04 +0530 | [diff] [blame] | 191 | palmas_gpio->gpio_chip.of_node = pdev->dev.of_node; |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 192 | #endif |
| 193 | palmas_pdata = dev_get_platdata(palmas->dev); |
| 194 | if (palmas_pdata && palmas_pdata->gpio_base) |
| 195 | palmas_gpio->gpio_chip.base = palmas_pdata->gpio_base; |
| 196 | else |
| 197 | palmas_gpio->gpio_chip.base = -1; |
| 198 | |
| 199 | ret = gpiochip_add(&palmas_gpio->gpio_chip); |
| 200 | if (ret < 0) { |
| 201 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret); |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | platform_set_drvdata(pdev, palmas_gpio); |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | static int palmas_gpio_remove(struct platform_device *pdev) |
| 210 | { |
| 211 | struct palmas_gpio *palmas_gpio = platform_get_drvdata(pdev); |
| 212 | |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 213 | gpiochip_remove(&palmas_gpio->gpio_chip); |
| 214 | return 0; |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static struct platform_driver palmas_gpio_driver = { |
| 218 | .driver.name = "palmas-gpio", |
| 219 | .driver.owner = THIS_MODULE, |
Laxman Dewangan | 5763318 | 2013-07-23 13:06:04 +0530 | [diff] [blame] | 220 | .driver.of_match_table = of_palmas_gpio_match, |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 221 | .probe = palmas_gpio_probe, |
| 222 | .remove = palmas_gpio_remove, |
| 223 | }; |
| 224 | |
| 225 | static int __init palmas_gpio_init(void) |
| 226 | { |
| 227 | return platform_driver_register(&palmas_gpio_driver); |
| 228 | } |
| 229 | subsys_initcall(palmas_gpio_init); |
| 230 | |
| 231 | static void __exit palmas_gpio_exit(void) |
| 232 | { |
| 233 | platform_driver_unregister(&palmas_gpio_driver); |
| 234 | } |
| 235 | module_exit(palmas_gpio_exit); |
| 236 | |
| 237 | MODULE_ALIAS("platform:palmas-gpio"); |
| 238 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); |
| 239 | MODULE_DESCRIPTION("GPIO driver for TI Palmas series PMICs"); |
| 240 | MODULE_LICENSE("GPL v2"); |