blob: 0c785b0fd1617aac8a6e9793e0311eadb551e101 [file] [log] [blame]
Linus Walleijd5a4da12018-08-29 16:49:14 +02001// SPDX-License-Identifier: GPL-2.0+
Graeme Gregory2537df72011-05-02 16:19:52 -05002/*
Grant Likelyc103de22011-06-04 18:38:28 -06003 * TI TPS6591x GPIO driver
Graeme Gregory2537df72011-05-02 16:19:52 -05004 *
5 * Copyright 2010 Texas Instruments Inc.
6 *
7 * Author: Graeme Gregory <gg@slimlogic.co.uk>
Paul Gortmaker02c7a132016-04-01 14:49:37 -04008 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
Graeme Gregory2537df72011-05-02 16:19:52 -05009 */
10
11#include <linux/kernel.h>
Paul Gortmaker02c7a132016-04-01 14:49:37 -040012#include <linux/init.h>
Graeme Gregory2537df72011-05-02 16:19:52 -050013#include <linux/errno.h>
Linus Walleij5d756832018-08-29 16:45:30 +020014#include <linux/gpio/driver.h>
Graeme Gregory2537df72011-05-02 16:19:52 -050015#include <linux/i2c.h>
Laxman Dewangan10bbc482012-05-11 21:48:27 +053016#include <linux/platform_device.h>
Graeme Gregory2537df72011-05-02 16:19:52 -050017#include <linux/mfd/tps65910.h>
Laxman Dewangan6fe02e92012-05-19 02:01:43 +053018#include <linux/of_device.h>
Graeme Gregory2537df72011-05-02 16:19:52 -050019
Laxman Dewangan10bbc482012-05-11 21:48:27 +053020struct tps65910_gpio {
21 struct gpio_chip gpio_chip;
22 struct tps65910 *tps65910;
23};
24
Graeme Gregory2537df72011-05-02 16:19:52 -050025static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
26{
Linus Walleijb7c17b12015-12-07 14:48:49 +010027 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
Laxman Dewangan10bbc482012-05-11 21:48:27 +053028 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
Rhyland Klein3f7e8272012-05-08 11:42:38 -070029 unsigned int val;
Graeme Gregory2537df72011-05-02 16:19:52 -050030
Rhyland Klein3f7e8272012-05-08 11:42:38 -070031 tps65910_reg_read(tps65910, TPS65910_GPIO0 + offset, &val);
Graeme Gregory2537df72011-05-02 16:19:52 -050032
Jorge Eduardo Candelaria11ad14f82011-05-16 18:35:42 -050033 if (val & GPIO_STS_MASK)
Graeme Gregory2537df72011-05-02 16:19:52 -050034 return 1;
35
36 return 0;
37}
38
39static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
40 int value)
41{
Linus Walleijb7c17b12015-12-07 14:48:49 +010042 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
Laxman Dewangan10bbc482012-05-11 21:48:27 +053043 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
Graeme Gregory2537df72011-05-02 16:19:52 -050044
45 if (value)
Rhyland Klein3f7e8272012-05-08 11:42:38 -070046 tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
Jorge Eduardo Candelaria11ad14f82011-05-16 18:35:42 -050047 GPIO_SET_MASK);
Graeme Gregory2537df72011-05-02 16:19:52 -050048 else
Rhyland Klein3f7e8272012-05-08 11:42:38 -070049 tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
Jorge Eduardo Candelaria11ad14f82011-05-16 18:35:42 -050050 GPIO_SET_MASK);
Graeme Gregory2537df72011-05-02 16:19:52 -050051}
52
53static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
54 int value)
55{
Linus Walleijb7c17b12015-12-07 14:48:49 +010056 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
Laxman Dewangan10bbc482012-05-11 21:48:27 +053057 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
Graeme Gregory2537df72011-05-02 16:19:52 -050058
59 /* Set the initial value */
Laxman Dewangan94bd2442012-01-18 20:07:35 +053060 tps65910_gpio_set(gc, offset, value);
Graeme Gregory2537df72011-05-02 16:19:52 -050061
Rhyland Klein3f7e8272012-05-08 11:42:38 -070062 return tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
Jorge Eduardo Candelaria11ad14f82011-05-16 18:35:42 -050063 GPIO_CFG_MASK);
Graeme Gregory2537df72011-05-02 16:19:52 -050064}
65
66static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
67{
Linus Walleijb7c17b12015-12-07 14:48:49 +010068 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
Laxman Dewangan10bbc482012-05-11 21:48:27 +053069 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
Graeme Gregory2537df72011-05-02 16:19:52 -050070
Rhyland Klein3f7e8272012-05-08 11:42:38 -070071 return tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
Jorge Eduardo Candelaria11ad14f82011-05-16 18:35:42 -050072 GPIO_CFG_MASK);
Graeme Gregory2537df72011-05-02 16:19:52 -050073}
74
Laxman Dewangan6fe02e92012-05-19 02:01:43 +053075#ifdef CONFIG_OF
76static 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
99static 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 Pemberton38363092012-11-19 13:22:34 -0500106static int tps65910_gpio_probe(struct platform_device *pdev)
Graeme Gregory2537df72011-05-02 16:19:52 -0500107{
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530108 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 Gregory2537df72011-05-02 16:19:52 -0500111 int ret;
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530112 int i;
Graeme Gregory2537df72011-05-02 16:19:52 -0500113
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530114 tps65910_gpio = devm_kzalloc(&pdev->dev,
115 sizeof(*tps65910_gpio), GFP_KERNEL);
Jingoo Han06611752014-04-29 17:45:46 +0900116 if (!tps65910_gpio)
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530117 return -ENOMEM;
Graeme Gregory2537df72011-05-02 16:19:52 -0500118
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530119 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 Candelaria11ad14f82011-05-16 18:35:42 -0500123
Laurent Navet195c65e2013-03-20 13:16:04 +0100124 switch (tps65910_chip_id(tps65910)) {
Jorge Eduardo Candelaria11ad14f82011-05-16 18:35:42 -0500125 case TPS65910:
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530126 tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
Axel Lin58956ba2011-07-06 10:08:27 +0800127 break;
Jorge Eduardo Candelaria11ad14f82011-05-16 18:35:42 -0500128 case TPS65911:
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530129 tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
Axel Lin58956ba2011-07-06 10:08:27 +0800130 break;
Jorge Eduardo Candelaria11ad14f82011-05-16 18:35:42 -0500131 default:
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530132 return -EINVAL;
Jorge Eduardo Candelaria11ad14f82011-05-16 18:35:42 -0500133 }
Linus Walleij9fb1f392013-12-04 14:42:46 +0100134 tps65910_gpio->gpio_chip.can_sleep = true;
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530135 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 Walleij58383c782015-11-04 09:56:26 +0100139 tps65910_gpio->gpio_chip.parent = &pdev->dev;
Jerry Snitselaarbb39b652012-07-09 22:16:34 -0700140#ifdef CONFIG_OF_GPIO
Laxman Dewangan626f9912012-07-04 20:36:45 +0530141 tps65910_gpio->gpio_chip.of_node = tps65910->dev->of_node;
Jerry Snitselaarbb39b652012-07-09 22:16:34 -0700142#endif
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530143 if (pdata && pdata->gpio_base)
144 tps65910_gpio->gpio_chip.base = pdata->gpio_base;
145 else
146 tps65910_gpio->gpio_chip.base = -1;
Graeme Gregory2537df72011-05-02 16:19:52 -0500147
Laxman Dewangan6fe02e92012-05-19 02:01:43 +0530148 if (!pdata && tps65910->dev->of_node)
149 pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
150 tps65910_gpio->gpio_chip.ngpio);
151
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530152 if (!pdata)
153 goto skip_init;
Graeme Gregory2537df72011-05-02 16:19:52 -0500154
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530155 /* 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 Dewangan9467d292012-02-01 12:09:04 +0530165 }
166
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530167skip_init:
Laxman Dewanganbf6e8552016-02-22 17:43:28 +0530168 ret = devm_gpiochip_add_data(&pdev->dev, &tps65910_gpio->gpio_chip,
169 tps65910_gpio);
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530170 if (ret < 0) {
171 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
172 return ret;
173 }
Graeme Gregory2537df72011-05-02 16:19:52 -0500174
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530175 platform_set_drvdata(pdev, tps65910_gpio);
176
177 return ret;
Graeme Gregory2537df72011-05-02 16:19:52 -0500178}
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530179
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530180static struct platform_driver tps65910_gpio_driver = {
181 .driver.name = "tps65910-gpio",
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530182 .probe = tps65910_gpio_probe,
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530183};
184
185static int __init tps65910_gpio_init(void)
186{
187 return platform_driver_register(&tps65910_gpio_driver);
188}
189subsys_initcall(tps65910_gpio_init);