]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpio/gpio-tps65910.c
Merge branch 'stable/for-jens-4.13' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-artful-kernel.git] / drivers / gpio / gpio-tps65910.c
CommitLineData
2537df72 1/*
c103de24 2 * TI TPS6591x GPIO driver
2537df72
GG
3 *
4 * Copyright 2010 Texas Instruments Inc.
5 *
6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
02c7a13e 7 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
2537df72
GG
8 *
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>
02c7a13e 17#include <linux/init.h>
2537df72
GG
18#include <linux/errno.h>
19#include <linux/gpio.h>
20#include <linux/i2c.h>
10bbc48d 21#include <linux/platform_device.h>
2537df72 22#include <linux/mfd/tps65910.h>
6fe02e9f 23#include <linux/of_device.h>
2537df72 24
10bbc48d
LD
25struct tps65910_gpio {
26 struct gpio_chip gpio_chip;
27 struct tps65910 *tps65910;
28};
29
2537df72
GG
30static int tps65910_gpio_get(struct gpio_chip *gc, unsigned offset)
31{
b7c17b1b 32 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
10bbc48d 33 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
3f7e8275 34 unsigned int val;
2537df72 35
3f7e8275 36 tps65910_reg_read(tps65910, TPS65910_GPIO0 + offset, &val);
2537df72 37
11ad14f8 38 if (val & GPIO_STS_MASK)
2537df72
GG
39 return 1;
40
41 return 0;
42}
43
44static void tps65910_gpio_set(struct gpio_chip *gc, unsigned offset,
45 int value)
46{
b7c17b1b 47 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
10bbc48d 48 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
2537df72
GG
49
50 if (value)
3f7e8275 51 tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
11ad14f8 52 GPIO_SET_MASK);
2537df72 53 else
3f7e8275 54 tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
11ad14f8 55 GPIO_SET_MASK);
2537df72
GG
56}
57
58static int tps65910_gpio_output(struct gpio_chip *gc, unsigned offset,
59 int value)
60{
b7c17b1b 61 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
10bbc48d 62 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
2537df72
GG
63
64 /* Set the initial value */
94bd2442 65 tps65910_gpio_set(gc, offset, value);
2537df72 66
3f7e8275 67 return tps65910_reg_set_bits(tps65910, TPS65910_GPIO0 + offset,
11ad14f8 68 GPIO_CFG_MASK);
2537df72
GG
69}
70
71static int tps65910_gpio_input(struct gpio_chip *gc, unsigned offset)
72{
b7c17b1b 73 struct tps65910_gpio *tps65910_gpio = gpiochip_get_data(gc);
10bbc48d 74 struct tps65910 *tps65910 = tps65910_gpio->tps65910;
2537df72 75
3f7e8275 76 return tps65910_reg_clear_bits(tps65910, TPS65910_GPIO0 + offset,
11ad14f8 77 GPIO_CFG_MASK);
2537df72
GG
78}
79
6fe02e9f
LD
80#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
3836309d 111static int tps65910_gpio_probe(struct platform_device *pdev)
2537df72 112{
10bbc48d
LD
113 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;
2537df72 116 int ret;
10bbc48d
LD
117 int i;
118
119 tps65910_gpio = devm_kzalloc(&pdev->dev,
120 sizeof(*tps65910_gpio), GFP_KERNEL);
0661175a 121 if (!tps65910_gpio)
10bbc48d 122 return -ENOMEM;
2537df72 123
10bbc48d 124 tps65910_gpio->tps65910 = tps65910;
2537df72 125
10bbc48d
LD
126 tps65910_gpio->gpio_chip.owner = THIS_MODULE;
127 tps65910_gpio->gpio_chip.label = tps65910->i2c_client->name;
11ad14f8 128
195c65e8 129 switch (tps65910_chip_id(tps65910)) {
11ad14f8 130 case TPS65910:
10bbc48d 131 tps65910_gpio->gpio_chip.ngpio = TPS65910_NUM_GPIO;
58956ba2 132 break;
11ad14f8 133 case TPS65911:
10bbc48d 134 tps65910_gpio->gpio_chip.ngpio = TPS65911_NUM_GPIO;
58956ba2 135 break;
11ad14f8 136 default:
10bbc48d 137 return -EINVAL;
11ad14f8 138 }
9fb1f39e 139 tps65910_gpio->gpio_chip.can_sleep = true;
10bbc48d
LD
140 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;
58383c78 144 tps65910_gpio->gpio_chip.parent = &pdev->dev;
bb39b655 145#ifdef CONFIG_OF_GPIO
626f9914 146 tps65910_gpio->gpio_chip.of_node = tps65910->dev->of_node;
bb39b655 147#endif
10bbc48d
LD
148 if (pdata && pdata->gpio_base)
149 tps65910_gpio->gpio_chip.base = pdata->gpio_base;
150 else
151 tps65910_gpio->gpio_chip.base = -1;
152
6fe02e9f
LD
153 if (!pdata && tps65910->dev->of_node)
154 pdata = tps65910_parse_dt_for_gpio(&pdev->dev, tps65910,
155 tps65910_gpio->gpio_chip.ngpio);
156
10bbc48d
LD
157 if (!pdata)
158 goto skip_init;
159
160 /* 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);
170 }
171
172skip_init:
bf6e855a
LD
173 ret = devm_gpiochip_add_data(&pdev->dev, &tps65910_gpio->gpio_chip,
174 tps65910_gpio);
10bbc48d
LD
175 if (ret < 0) {
176 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
177 return ret;
9467d298
LD
178 }
179
10bbc48d 180 platform_set_drvdata(pdev, tps65910_gpio);
2537df72 181
10bbc48d 182 return ret;
2537df72 183}
10bbc48d 184
10bbc48d
LD
185static struct platform_driver tps65910_gpio_driver = {
186 .driver.name = "tps65910-gpio",
10bbc48d 187 .probe = tps65910_gpio_probe,
10bbc48d
LD
188};
189
190static int __init tps65910_gpio_init(void)
191{
192 return platform_driver_register(&tps65910_gpio_driver);
193}
194subsys_initcall(tps65910_gpio_init);