Linus Walleij | d5a4da1 | 2018-08-29 16:49:14 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 2 | /* |
Grant Likely | c103de2 | 2011-06-04 18:38:28 -0600 | [diff] [blame] | 3 | * TI TPS6591x GPIO driver |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 4 | * |
| 5 | * Copyright 2010 Texas Instruments Inc. |
| 6 | * |
| 7 | * Author: Graeme Gregory <gg@slimlogic.co.uk> |
Paul Gortmaker | 02c7a13 | 2016-04-01 14:49:37 -0400 | [diff] [blame] | 8 | * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk> |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
Paul Gortmaker | 02c7a13 | 2016-04-01 14:49:37 -0400 | [diff] [blame] | 12 | #include <linux/init.h> |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 13 | #include <linux/errno.h> |
Linus Walleij | 5d75683 | 2018-08-29 16:45:30 +0200 | [diff] [blame] | 14 | #include <linux/gpio/driver.h> |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 15 | #include <linux/i2c.h> |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 17 | #include <linux/mfd/tps65910.h> |
Laxman Dewangan | 6fe02e9 | 2012-05-19 02:01:43 +0530 | [diff] [blame] | 18 | #include <linux/of_device.h> |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 19 | |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 20 | struct tps65910_gpio { |
| 21 | struct gpio_chip gpio_chip; |
| 22 | struct tps65910 *tps65910; |
| 23 | }; |
| 24 | |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 25 | static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 26 | { |
Linus Walleij | b7c17b1 | 2015-12-07 14:48:49 +0100 | [diff] [blame] | 27 | struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc); |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 28 | struct tps65910 *tps65910 = tps65910_gpio->tps65910; |
Rhyland Klein | 3f7e827 | 2012-05-08 11:42:38 -0700 | [diff] [blame] | 29 | unsigned int val; |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 30 | |
Rhyland Klein | 3f7e827 | 2012-05-08 11:42:38 -0700 | [diff] [blame] | 31 | tps65910_reg_read(tps65910, TPS65910_GPIO0 + offset, &val); |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 32 | |
Jorge Eduardo Candelaria | 11ad14f8 | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 33 | if (val & GPIO_STS_MASK) |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 34 | return 1; |
| 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset, |
| 40 | int value) |
| 41 | { |
Linus Walleij | b7c17b1 | 2015-12-07 14:48:49 +0100 | [diff] [blame] | 42 | struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc); |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 43 | struct tps65910 *tps65910 = tps65910_gpio->tps65910; |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 44 | |
| 45 | if (value) |
Rhyland Klein | 3f7e827 | 2012-05-08 11:42:38 -0700 | [diff] [blame] | 46 | tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset, |
Jorge Eduardo Candelaria | 11ad14f8 | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 47 | GPIO_SET_MASK); |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 48 | else |
Rhyland Klein | 3f7e827 | 2012-05-08 11:42:38 -0700 | [diff] [blame] | 49 | tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset, |
Jorge Eduardo Candelaria | 11ad14f8 | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 50 | GPIO_SET_MASK); |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset, |
| 54 | int value) |
| 55 | { |
Linus Walleij | b7c17b1 | 2015-12-07 14:48:49 +0100 | [diff] [blame] | 56 | struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc); |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 57 | struct tps65910 *tps65910 = tps65910_gpio->tps65910; |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 58 | |
| 59 | /* Set the initial value */ |
Laxman Dewangan | 94bd244 | 2012-01-18 20:07:35 +0530 | [diff] [blame] | 60 | tps65910_gpio_set(gc, offset, value); |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 61 | |
Rhyland Klein | 3f7e827 | 2012-05-08 11:42:38 -0700 | [diff] [blame] | 62 | return tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset, |
Jorge Eduardo Candelaria | 11ad14f8 | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 63 | GPIO_CFG_MASK); |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset) |
| 67 | { |
Linus Walleij | b7c17b1 | 2015-12-07 14:48:49 +0100 | [diff] [blame] | 68 | struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc); |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 69 | struct tps65910 *tps65910 = tps65910_gpio->tps65910; |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 70 | |
Rhyland Klein | 3f7e827 | 2012-05-08 11:42:38 -0700 | [diff] [blame] | 71 | return tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset, |
Jorge Eduardo Candelaria | 11ad14f8 | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 72 | GPIO_CFG_MASK); |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Laxman Dewangan | 6fe02e9 | 2012-05-19 02:01:43 +0530 | [diff] [blame] | 75 | #ifdef CONFIG_OF |
| 76 | static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev, |
| 77 | struct tps65910 *tps65910, int chip_ngpio) |
| 78 | { |
| 79 | struct tps65910_board *tps65910_board = tps65910->of_plat_data; |
| 80 | unsigned int prop_array[TPS6591X_MAX_NUM_GPIO]; |
| 81 | int ngpio = min(chip_ngpio, TPS6591X_MAX_NUM_GPIO); |
| 82 | int ret; |
| 83 | int idx; |
| 84 | |
| 85 | tps65910_board->gpio_base = -1; |
| 86 | ret = of_property_read_u32_array(tps65910->dev->of_node, |
| 87 | "ti,en-gpio-sleep", prop_array, ngpio); |
| 88 | if (ret < 0) { |
| 89 | dev_dbg(dev, "ti,en-gpio-sleep not specified\n"); |
| 90 | return tps65910_board; |
| 91 | } |
| 92 | |
| 93 | for (idx = 0; idx < ngpio; idx++) |
| 94 | tps65910_board->en_gpio_sleep[idx] = (prop_array[idx] != 0); |
| 95 | |
| 96 | return tps65910_board; |
| 97 | } |
| 98 | #else |
| 99 | static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev, |
| 100 | struct tps65910 *tps65910, int chip_ngpio) |
| 101 | { |
| 102 | return NULL; |
| 103 | } |
| 104 | #endif |
| 105 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 106 | static int tps65910_gpio_probe(struct platform_device *pdev) |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 107 | { |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 108 | struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent); |
| 109 | struct tps65910_board *pdata = dev_get_platdata(tps65910->dev); |
| 110 | struct tps65910_gpio *tps65910_gpio; |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 111 | int ret; |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 112 | int i; |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 113 | |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 114 | tps65910_gpio = devm_kzalloc(&pdev->dev, |
| 115 | sizeof(*tps65910_gpio), GFP_KERNEL); |
Jingoo Han | 0661175 | 2014-04-29 17:45:46 +0900 | [diff] [blame] | 116 | if (!tps65910_gpio) |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 117 | return -ENOMEM; |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 118 | |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 119 | tps65910_gpio->tps65910 = tps65910; |
| 120 | |
| 121 | tps65910_gpio->gpio_chip.owner = THIS_MODULE; |
| 122 | tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name; |
Jorge Eduardo Candelaria | 11ad14f8 | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 123 | |
Laurent Navet | 195c65e | 2013-03-20 13:16:04 +0100 | [diff] [blame] | 124 | switch (tps65910_chip_id(tps65910)) { |
Jorge Eduardo Candelaria | 11ad14f8 | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 125 | case TPS65910: |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 126 | tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO; |
Axel Lin | 58956ba | 2011-07-06 10:08:27 +0800 | [diff] [blame] | 127 | break; |
Jorge Eduardo Candelaria | 11ad14f8 | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 128 | case TPS65911: |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 129 | tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO; |
Axel Lin | 58956ba | 2011-07-06 10:08:27 +0800 | [diff] [blame] | 130 | break; |
Jorge Eduardo Candelaria | 11ad14f8 | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 131 | default: |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 132 | return -EINVAL; |
Jorge Eduardo Candelaria | 11ad14f8 | 2011-05-16 18:35:42 -0500 | [diff] [blame] | 133 | } |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 134 | tps65910_gpio->gpio_chip.can_sleep = true; |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 135 | tps65910_gpio->gpio_chip.direction_input = tps65910_gpio_input; |
| 136 | tps65910_gpio->gpio_chip.direction_output = tps65910_gpio_output; |
| 137 | tps65910_gpio->gpio_chip.set = tps65910_gpio_set; |
| 138 | tps65910_gpio->gpio_chip.get = tps65910_gpio_get; |
Linus Walleij | 58383c78 | 2015-11-04 09:56:26 +0100 | [diff] [blame] | 139 | tps65910_gpio->gpio_chip.parent = &pdev->dev; |
Jerry Snitselaar | bb39b65 | 2012-07-09 22:16:34 -0700 | [diff] [blame] | 140 | #ifdef CONFIG_OF_GPIO |
Laxman Dewangan | 626f991 | 2012-07-04 20:36:45 +0530 | [diff] [blame] | 141 | tps65910_gpio->gpio_chip.of_node = tps65910->dev->of_node; |
Jerry Snitselaar | bb39b65 | 2012-07-09 22:16:34 -0700 | [diff] [blame] | 142 | #endif |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 143 | if (pdata && pdata->gpio_base) |
| 144 | tps65910_gpio->gpio_chip.base = pdata->gpio_base; |
| 145 | else |
| 146 | tps65910_gpio->gpio_chip.base = -1; |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 147 | |
Laxman Dewangan | 6fe02e9 | 2012-05-19 02:01:43 +0530 | [diff] [blame] | 148 | if (!pdata && tps65910->dev->of_node) |
| 149 | pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910, |
| 150 | tps65910_gpio->gpio_chip.ngpio); |
| 151 | |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 152 | if (!pdata) |
| 153 | goto skip_init; |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 154 | |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 155 | /* Configure sleep control for gpios if provided */ |
| 156 | for (i = 0; i < tps65910_gpio->gpio_chip.ngpio; ++i) { |
| 157 | if (!pdata->en_gpio_sleep[i]) |
| 158 | continue; |
| 159 | |
| 160 | ret = tps65910_reg_set_bits(tps65910, |
| 161 | TPS65910_GPIO0 + i, GPIO_SLEEP_MASK); |
| 162 | if (ret < 0) |
| 163 | dev_warn(tps65910->dev, |
| 164 | "GPIO Sleep setting failed with err %d\n", ret); |
Laxman Dewangan | 9467d29 | 2012-02-01 12:09:04 +0530 | [diff] [blame] | 165 | } |
| 166 | |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 167 | skip_init: |
Laxman Dewangan | bf6e855 | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 168 | ret = devm_gpiochip_add_data(&pdev->dev, &tps65910_gpio->gpio_chip, |
| 169 | tps65910_gpio); |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 170 | if (ret < 0) { |
| 171 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret); |
| 172 | return ret; |
| 173 | } |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 174 | |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 175 | platform_set_drvdata(pdev, tps65910_gpio); |
| 176 | |
| 177 | return ret; |
Graeme Gregory | 2537df7 | 2011-05-02 16:19:52 -0500 | [diff] [blame] | 178 | } |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 179 | |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 180 | static struct platform_driver tps65910_gpio_driver = { |
| 181 | .driver.name = "tps65910-gpio", |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 182 | .probe = tps65910_gpio_probe, |
Laxman Dewangan | 10bbc48 | 2012-05-11 21:48:27 +0530 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | static int __init tps65910_gpio_init(void) |
| 186 | { |
| 187 | return platform_driver_register(&tps65910_gpio_driver); |
| 188 | } |
| 189 | subsys_initcall(tps65910_gpio_init); |