Thomas Gleixner | 9952f69 | 2019-05-28 10:10:04 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 2 | /* |
| 3 | * TI Palma series PMIC's GPIO driver. |
| 4 | * |
| 5 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
| 6 | * |
| 7 | * Author: Laxman Dewangan <ldewangan@nvidia.com> |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 8 | */ |
| 9 | |
Linus Walleij | b11a0b3 | 2018-05-24 14:25:13 +0200 | [diff] [blame] | 10 | #include <linux/gpio/driver.h> |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Paul Gortmaker | f9f2b5c | 2016-04-01 14:49:36 -0400 | [diff] [blame] | 12 | #include <linux/init.h> |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 13 | #include <linux/mfd/palmas.h> |
| 14 | #include <linux/of.h> |
| 15 | #include <linux/of_device.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | |
| 18 | struct palmas_gpio { |
| 19 | struct gpio_chip gpio_chip; |
| 20 | struct palmas *palmas; |
| 21 | }; |
| 22 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 23 | struct palmas_device_data { |
| 24 | int ngpio; |
| 25 | }; |
| 26 | |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 27 | static int palmas_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 28 | { |
Linus Walleij | 5b68cc2 | 2015-12-07 11:18:17 +0100 | [diff] [blame] | 29 | struct palmas_gpio *pg = gpiochip_get_data(gc); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 30 | struct palmas *palmas = pg->palmas; |
| 31 | unsigned int val; |
| 32 | int ret; |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 33 | unsigned int reg; |
| 34 | int gpio16 = (offset/8); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 35 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 36 | offset %= 8; |
| 37 | reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR; |
| 38 | |
| 39 | ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 40 | if (ret < 0) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 41 | 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] | 42 | return ret; |
| 43 | } |
| 44 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 45 | if (val & BIT(offset)) |
| 46 | reg = (gpio16) ? PALMAS_GPIO_DATA_OUT2 : PALMAS_GPIO_DATA_OUT; |
| 47 | else |
| 48 | reg = (gpio16) ? PALMAS_GPIO_DATA_IN2 : PALMAS_GPIO_DATA_IN; |
| 49 | |
| 50 | ret = palmas_read(palmas, PALMAS_GPIO_BASE, reg, &val); |
Andrew Chew | 8b628c6 | 2013-07-26 13:39:53 -0700 | [diff] [blame] | 51 | if (ret < 0) { |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 52 | 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] | 53 | return ret; |
| 54 | } |
| 55 | return !!(val & BIT(offset)); |
| 56 | } |
| 57 | |
| 58 | static void palmas_gpio_set(struct gpio_chip *gc, unsigned offset, |
| 59 | int value) |
| 60 | { |
Linus Walleij | 5b68cc2 | 2015-12-07 11:18:17 +0100 | [diff] [blame] | 61 | struct palmas_gpio *pg = gpiochip_get_data(gc); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 62 | struct palmas *palmas = pg->palmas; |
| 63 | int ret; |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 64 | unsigned int reg; |
| 65 | int gpio16 = (offset/8); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 66 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 67 | offset %= 8; |
| 68 | if (gpio16) |
| 69 | reg = (value) ? |
| 70 | PALMAS_GPIO_SET_DATA_OUT2 : PALMAS_GPIO_CLEAR_DATA_OUT2; |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 71 | else |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 72 | reg = (value) ? |
| 73 | PALMAS_GPIO_SET_DATA_OUT : PALMAS_GPIO_CLEAR_DATA_OUT; |
| 74 | |
| 75 | ret = palmas_write(palmas, PALMAS_GPIO_BASE, reg, BIT(offset)); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 76 | if (ret < 0) |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 77 | 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] | 78 | } |
| 79 | |
| 80 | static int palmas_gpio_output(struct gpio_chip *gc, unsigned offset, |
| 81 | int value) |
| 82 | { |
Linus Walleij | 5b68cc2 | 2015-12-07 11:18:17 +0100 | [diff] [blame] | 83 | struct palmas_gpio *pg = gpiochip_get_data(gc); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 84 | struct palmas *palmas = pg->palmas; |
| 85 | int ret; |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 86 | unsigned int reg; |
| 87 | int gpio16 = (offset/8); |
| 88 | |
| 89 | offset %= 8; |
| 90 | reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR; |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 91 | |
| 92 | /* Set the initial value */ |
| 93 | palmas_gpio_set(gc, offset, value); |
| 94 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 95 | ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg, |
| 96 | BIT(offset), BIT(offset)); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 97 | if (ret < 0) |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 98 | dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg, |
| 99 | ret); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | static int palmas_gpio_input(struct gpio_chip *gc, unsigned offset) |
| 104 | { |
Linus Walleij | 5b68cc2 | 2015-12-07 11:18:17 +0100 | [diff] [blame] | 105 | struct palmas_gpio *pg = gpiochip_get_data(gc); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 106 | struct palmas *palmas = pg->palmas; |
| 107 | int ret; |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 108 | unsigned int reg; |
| 109 | int gpio16 = (offset/8); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 110 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 111 | offset %= 8; |
| 112 | reg = (gpio16) ? PALMAS_GPIO_DATA_DIR2 : PALMAS_GPIO_DATA_DIR; |
| 113 | |
| 114 | ret = palmas_update_bits(palmas, PALMAS_GPIO_BASE, reg, BIT(offset), 0); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 115 | if (ret < 0) |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 116 | dev_err(gc->parent, "Reg 0x%02x update failed, %d\n", reg, |
| 117 | ret); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 118 | return ret; |
| 119 | } |
| 120 | |
| 121 | static int palmas_gpio_to_irq(struct gpio_chip *gc, unsigned offset) |
| 122 | { |
Linus Walleij | 5b68cc2 | 2015-12-07 11:18:17 +0100 | [diff] [blame] | 123 | struct palmas_gpio *pg = gpiochip_get_data(gc); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 124 | struct palmas *palmas = pg->palmas; |
| 125 | |
| 126 | return palmas_irq_get_virq(palmas, PALMAS_GPIO_0_IRQ + offset); |
| 127 | } |
| 128 | |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 129 | static const struct palmas_device_data palmas_dev_data = { |
| 130 | .ngpio = 8, |
| 131 | }; |
| 132 | |
| 133 | static const struct palmas_device_data tps80036_dev_data = { |
| 134 | .ngpio = 16, |
| 135 | }; |
| 136 | |
Jingoo Han | 722782f | 2014-05-07 18:06:50 +0900 | [diff] [blame] | 137 | static const struct of_device_id of_palmas_gpio_match[] = { |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 138 | { .compatible = "ti,palmas-gpio", .data = &palmas_dev_data,}, |
| 139 | { .compatible = "ti,tps65913-gpio", .data = &palmas_dev_data,}, |
| 140 | { .compatible = "ti,tps65914-gpio", .data = &palmas_dev_data,}, |
| 141 | { .compatible = "ti,tps80036-gpio", .data = &tps80036_dev_data,}, |
| 142 | { }, |
| 143 | }; |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 144 | |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 145 | static int palmas_gpio_probe(struct platform_device *pdev) |
| 146 | { |
| 147 | struct palmas *palmas = dev_get_drvdata(pdev->dev.parent); |
| 148 | struct palmas_platform_data *palmas_pdata; |
| 149 | struct palmas_gpio *palmas_gpio; |
| 150 | int ret; |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 151 | const struct palmas_device_data *dev_data; |
| 152 | |
Thierry Reding | fd46d5c | 2018-04-30 09:38:13 +0200 | [diff] [blame] | 153 | dev_data = of_device_get_match_data(&pdev->dev); |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 154 | if (!dev_data) |
| 155 | dev_data = &palmas_dev_data; |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 156 | |
| 157 | palmas_gpio = devm_kzalloc(&pdev->dev, |
| 158 | sizeof(*palmas_gpio), GFP_KERNEL); |
Jingoo Han | 5605beb | 2014-04-29 17:39:07 +0900 | [diff] [blame] | 159 | if (!palmas_gpio) |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 160 | return -ENOMEM; |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 161 | |
| 162 | palmas_gpio->palmas = palmas; |
| 163 | palmas_gpio->gpio_chip.owner = THIS_MODULE; |
| 164 | palmas_gpio->gpio_chip.label = dev_name(&pdev->dev); |
Laxman Dewangan | ca6af7b | 2013-09-03 19:58:03 +0530 | [diff] [blame] | 165 | palmas_gpio->gpio_chip.ngpio = dev_data->ngpio; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 166 | palmas_gpio->gpio_chip.can_sleep = true; |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 167 | palmas_gpio->gpio_chip.direction_input = palmas_gpio_input; |
| 168 | palmas_gpio->gpio_chip.direction_output = palmas_gpio_output; |
| 169 | palmas_gpio->gpio_chip.to_irq = palmas_gpio_to_irq; |
| 170 | palmas_gpio->gpio_chip.set = palmas_gpio_set; |
| 171 | palmas_gpio->gpio_chip.get = palmas_gpio_get; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 172 | palmas_gpio->gpio_chip.parent = &pdev->dev; |
Andy Shevchenko | 448cf90 | 2021-12-17 17:39:35 +0100 | [diff] [blame] | 173 | |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 174 | palmas_pdata = dev_get_platdata(palmas->dev); |
| 175 | if (palmas_pdata && palmas_pdata->gpio_base) |
| 176 | palmas_gpio->gpio_chip.base = palmas_pdata->gpio_base; |
| 177 | else |
| 178 | palmas_gpio->gpio_chip.base = -1; |
| 179 | |
Laxman Dewangan | 297bf06 | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 180 | ret = devm_gpiochip_add_data(&pdev->dev, &palmas_gpio->gpio_chip, |
| 181 | palmas_gpio); |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 182 | if (ret < 0) { |
| 183 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret); |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | platform_set_drvdata(pdev, palmas_gpio); |
| 188 | return ret; |
| 189 | } |
| 190 | |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 191 | static struct platform_driver palmas_gpio_driver = { |
| 192 | .driver.name = "palmas-gpio", |
Laxman Dewangan | 5763318 | 2013-07-23 13:06:04 +0530 | [diff] [blame] | 193 | .driver.of_match_table = of_palmas_gpio_match, |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 194 | .probe = palmas_gpio_probe, |
Laxman Dewangan | 3d50a27 | 2013-01-03 16:16:59 +0530 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | static int __init palmas_gpio_init(void) |
| 198 | { |
| 199 | return platform_driver_register(&palmas_gpio_driver); |
| 200 | } |
| 201 | subsys_initcall(palmas_gpio_init); |