blob: e63d7dabf78b1e61482cc5716b69640efa5d9c9c [file] [log] [blame]
Graeme Gregory2537df72011-05-02 16:19:52 -05001/*
Grant Likelyc103de22011-06-04 18:38:28 -06002 * TI TPS6591x GPIO driver
Graeme Gregory2537df72011-05-02 16:19:52 -05003 *
4 * Copyright 2010 Texas Instruments Inc.
5 *
6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
Paul Gortmaker02c7a132016-04-01 14:49:37 -04007 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
Graeme Gregory2537df72011-05-02 16:19:52 -05008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
Paul Gortmaker02c7a132016-04-01 14:49:37 -040017#include <linux/init.h>
Graeme Gregory2537df72011-05-02 16:19:52 -050018#include <linux/errno.h>
19#include <linux/gpio.h>
20#include <linux/i2c.h>
Laxman Dewangan10bbc482012-05-11 21:48:27 +053021#include <linux/platform_device.h>
Graeme Gregory2537df72011-05-02 16:19:52 -050022#include <linux/mfd/tps65910.h>
Laxman Dewangan6fe02e92012-05-19 02:01:43 +053023#include <linux/of_device.h>
Graeme Gregory2537df72011-05-02 16:19:52 -050024
Laxman Dewangan10bbc482012-05-11 21:48:27 +053025struct tps65910_gpio {
26 struct gpio_chip gpio_chip;
27 struct tps65910 *tps65910;
28};
29
Graeme Gregory2537df72011-05-02 16:19:52 -050030static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
31{
Linus Walleijb7c17b12015-12-07 14:48:49 +010032 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
Laxman Dewangan10bbc482012-05-11 21:48:27 +053033 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
Rhyland Klein3f7e8272012-05-08 11:42:38 -070034 unsigned int val;
Graeme Gregory2537df72011-05-02 16:19:52 -050035
Rhyland Klein3f7e8272012-05-08 11:42:38 -070036 tps65910_reg_read(tps65910, TPS65910_GPIO0 + offset, &val);
Graeme Gregory2537df72011-05-02 16:19:52 -050037
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -050038 if (val & GPIO_STS_MASK)
Graeme Gregory2537df72011-05-02 16:19:52 -050039 return 1;
40
41 return 0;
42}
43
44static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
45 int value)
46{
Linus Walleijb7c17b12015-12-07 14:48:49 +010047 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
Laxman Dewangan10bbc482012-05-11 21:48:27 +053048 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
Graeme Gregory2537df72011-05-02 16:19:52 -050049
50 if (value)
Rhyland Klein3f7e8272012-05-08 11:42:38 -070051 tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -050052 GPIO_SET_MASK);
Graeme Gregory2537df72011-05-02 16:19:52 -050053 else
Rhyland Klein3f7e8272012-05-08 11:42:38 -070054 tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -050055 GPIO_SET_MASK);
Graeme Gregory2537df72011-05-02 16:19:52 -050056}
57
58static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
59 int value)
60{
Linus Walleijb7c17b12015-12-07 14:48:49 +010061 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
Laxman Dewangan10bbc482012-05-11 21:48:27 +053062 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
Graeme Gregory2537df72011-05-02 16:19:52 -050063
64 /* Set the initial value */
Laxman Dewangan94bd2442012-01-18 20:07:35 +053065 tps65910_gpio_set(gc, offset, value);
Graeme Gregory2537df72011-05-02 16:19:52 -050066
Rhyland Klein3f7e8272012-05-08 11:42:38 -070067 return tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -050068 GPIO_CFG_MASK);
Graeme Gregory2537df72011-05-02 16:19:52 -050069}
70
71static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
72{
Linus Walleijb7c17b12015-12-07 14:48:49 +010073 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
Laxman Dewangan10bbc482012-05-11 21:48:27 +053074 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
Graeme Gregory2537df72011-05-02 16:19:52 -050075
Rhyland Klein3f7e8272012-05-08 11:42:38 -070076 return tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -050077 GPIO_CFG_MASK);
Graeme Gregory2537df72011-05-02 16:19:52 -050078}
79
Laxman Dewangan6fe02e92012-05-19 02:01:43 +053080#ifdef CONFIG_OF
81static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
82 struct tps65910 *tps65910, int chip_ngpio)
83{
84 struct tps65910_board *tps65910_board = tps65910->of_plat_data;
85 unsigned int prop_array[TPS6591X_MAX_NUM_GPIO];
86 int ngpio = min(chip_ngpio, TPS6591X_MAX_NUM_GPIO);
87 int ret;
88 int idx;
89
90 tps65910_board->gpio_base = -1;
91 ret = of_property_read_u32_array(tps65910->dev->of_node,
92 "ti,en-gpio-sleep", prop_array, ngpio);
93 if (ret < 0) {
94 dev_dbg(dev, "ti,en-gpio-sleep not specified\n");
95 return tps65910_board;
96 }
97
98 for (idx = 0; idx < ngpio; idx++)
99 tps65910_board->en_gpio_sleep[idx] = (prop_array[idx] != 0);
100
101 return tps65910_board;
102}
103#else
104static struct tps65910_board *tps65910_parse_dt_for_gpio(struct device *dev,
105 struct tps65910 *tps65910, int chip_ngpio)
106{
107 return NULL;
108}
109#endif
110
Bill Pemberton38363092012-11-19 13:22:34 -0500111static int tps65910_gpio_probe(struct platform_device *pdev)
Graeme Gregory2537df72011-05-02 16:19:52 -0500112{
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530113 struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
114 struct tps65910_board *pdata = dev_get_platdata(tps65910->dev);
115 struct tps65910_gpio *tps65910_gpio;
Graeme Gregory2537df72011-05-02 16:19:52 -0500116 int ret;
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530117 int i;
Graeme Gregory2537df72011-05-02 16:19:52 -0500118
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530119 tps65910_gpio = devm_kzalloc(&pdev->dev,
120 sizeof(*tps65910_gpio), GFP_KERNEL);
Jingoo Han06611752014-04-29 17:45:46 +0900121 if (!tps65910_gpio)
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530122 return -ENOMEM;
Graeme Gregory2537df72011-05-02 16:19:52 -0500123
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530124 tps65910_gpio->tps65910 = tps65910;
125
126 tps65910_gpio->gpio_chip.owner = THIS_MODULE;
127 tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name;
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -0500128
Laurent Navet195c65e2013-03-20 13:16:04 +0100129 switch (tps65910_chip_id(tps65910)) {
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -0500130 case TPS65910:
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530131 tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
Axel Lin58956ba2011-07-06 10:08:27 +0800132 break;
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -0500133 case TPS65911:
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530134 tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
Axel Lin58956ba2011-07-06 10:08:27 +0800135 break;
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -0500136 default:
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530137 return -EINVAL;
Jorge Eduardo Candelaria11ad14f2011-05-16 18:35:42 -0500138 }
Linus Walleij9fb1f392013-12-04 14:42:46 +0100139 tps65910_gpio->gpio_chip.can_sleep = true;
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530140 tps65910_gpio->gpio_chip.direction_input = tps65910_gpio_input;
141 tps65910_gpio->gpio_chip.direction_output = tps65910_gpio_output;
142 tps65910_gpio->gpio_chip.set = tps65910_gpio_set;
143 tps65910_gpio->gpio_chip.get = tps65910_gpio_get;
Linus Walleij58383c782015-11-04 09:56:26 +0100144 tps65910_gpio->gpio_chip.parent = &pdev->dev;
Jerry Snitselaarbb39b652012-07-09 22:16:34 -0700145#ifdef CONFIG_OF_GPIO
Laxman Dewangan626f9912012-07-04 20:36:45 +0530146 tps65910_gpio->gpio_chip.of_node = tps65910->dev->of_node;
Jerry Snitselaarbb39b652012-07-09 22:16:34 -0700147#endif
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530148 if (pdata && pdata->gpio_base)
149 tps65910_gpio->gpio_chip.base = pdata->gpio_base;
150 else
151 tps65910_gpio->gpio_chip.base = -1;
Graeme Gregory2537df72011-05-02 16:19:52 -0500152
Laxman Dewangan6fe02e92012-05-19 02:01:43 +0530153 if (!pdata && tps65910->dev->of_node)
154 pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
155 tps65910_gpio->gpio_chip.ngpio);
156
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530157 if (!pdata)
158 goto skip_init;
Graeme Gregory2537df72011-05-02 16:19:52 -0500159
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530160 /* Configure sleep control for gpios if provided */
161 for (i = 0; i < tps65910_gpio->gpio_chip.ngpio; ++i) {
162 if (!pdata->en_gpio_sleep[i])
163 continue;
164
165 ret = tps65910_reg_set_bits(tps65910,
166 TPS65910_GPIO0 + i, GPIO_SLEEP_MASK);
167 if (ret < 0)
168 dev_warn(tps65910->dev,
169 "GPIO Sleep setting failed with err %d\n", ret);
Laxman Dewangan9467d292012-02-01 12:09:04 +0530170 }
171
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530172skip_init:
Laxman Dewanganbf6e8552016-02-22 17:43:28 +0530173 ret = devm_gpiochip_add_data(&pdev->dev, &tps65910_gpio->gpio_chip,
174 tps65910_gpio);
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530175 if (ret < 0) {
176 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
177 return ret;
178 }
Graeme Gregory2537df72011-05-02 16:19:52 -0500179
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530180 platform_set_drvdata(pdev, tps65910_gpio);
181
182 return ret;
Graeme Gregory2537df72011-05-02 16:19:52 -0500183}
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530184
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530185static struct platform_driver tps65910_gpio_driver = {
186 .driver.name = "tps65910-gpio",
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530187 .probe = tps65910_gpio_probe,
Laxman Dewangan10bbc482012-05-11 21:48:27 +0530188};
189
190static int __init tps65910_gpio_init(void)
191{
192 return platform_driver_register(&tps65910_gpio_driver);
193}
194subsys_initcall(tps65910_gpio_init);