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