blob: 31f332074d7d5b96a12da5b9a825f73de5a55f86 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +04002/*
3 * SYSCON GPIO driver
4 *
5 * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +04006 */
7
8#include <linux/err.h>
Linus Walleij122d00f2018-06-28 08:35:46 +02009#include <linux/gpio/driver.h>
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +040010#include <linux/module.h>
11#include <linux/of.h>
12#include <linux/of_device.h>
13#include <linux/platform_device.h>
14#include <linux/regmap.h>
15#include <linux/mfd/syscon.h>
16
17#define GPIO_SYSCON_FEAT_IN BIT(0)
18#define GPIO_SYSCON_FEAT_OUT BIT(1)
19#define GPIO_SYSCON_FEAT_DIR BIT(2)
20
21/* SYSCON driver is designed to use 32-bit wide registers */
22#define SYSCON_REG_SIZE (4)
23#define SYSCON_REG_BITS (SYSCON_REG_SIZE * 8)
24
25/**
26 * struct syscon_gpio_data - Configuration for the device.
27 * compatible: SYSCON driver compatible string.
28 * flags: Set of GPIO_SYSCON_FEAT_ flags:
29 * GPIO_SYSCON_FEAT_IN: GPIOs supports input,
30 * GPIO_SYSCON_FEAT_OUT: GPIOs supports output,
31 * GPIO_SYSCON_FEAT_DIR: GPIOs supports switch direction.
32 * bit_count: Number of bits used as GPIOs.
33 * dat_bit_offset: Offset (in bits) to the first GPIO bit.
34 * dir_bit_offset: Optional offset (in bits) to the first bit to switch
35 * GPIO direction (Used with GPIO_SYSCON_FEAT_DIR flag).
Grygorii Strashko2c341d62014-09-03 20:05:32 +030036 * set: HW specific callback to assigns output value
37 * for signal "offset"
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +040038 */
39
40struct syscon_gpio_data {
41 const char *compatible;
42 unsigned int flags;
43 unsigned int bit_count;
44 unsigned int dat_bit_offset;
45 unsigned int dir_bit_offset;
Grygorii Strashko2c341d62014-09-03 20:05:32 +030046 void (*set)(struct gpio_chip *chip,
47 unsigned offset, int value);
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +040048};
49
50struct syscon_gpio_priv {
51 struct gpio_chip chip;
52 struct regmap *syscon;
53 const struct syscon_gpio_data *data;
Grygorii Strashko5a3e3f82014-09-03 20:05:33 +030054 u32 dreg_offset;
55 u32 dir_reg_offset;
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +040056};
57
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +040058static int syscon_gpio_get(struct gpio_chip *chip, unsigned offset)
59{
Linus Walleijd27ad7a2015-12-07 14:38:36 +010060 struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
Grygorii Strashko5a3e3f82014-09-03 20:05:33 +030061 unsigned int val, offs;
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +040062 int ret;
63
Grygorii Strashko5a3e3f82014-09-03 20:05:33 +030064 offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
65
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +040066 ret = regmap_read(priv->syscon,
67 (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE, &val);
68 if (ret)
69 return ret;
70
71 return !!(val & BIT(offs % SYSCON_REG_BITS));
72}
73
74static void syscon_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
75{
Linus Walleijd27ad7a2015-12-07 14:38:36 +010076 struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
Grygorii Strashko5a3e3f82014-09-03 20:05:33 +030077 unsigned int offs;
78
79 offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +040080
81 regmap_update_bits(priv->syscon,
82 (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
83 BIT(offs % SYSCON_REG_BITS),
84 val ? BIT(offs % SYSCON_REG_BITS) : 0);
85}
86
87static int syscon_gpio_dir_in(struct gpio_chip *chip, unsigned offset)
88{
Linus Walleijd27ad7a2015-12-07 14:38:36 +010089 struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +040090
91 if (priv->data->flags & GPIO_SYSCON_FEAT_DIR) {
Grygorii Strashko5a3e3f82014-09-03 20:05:33 +030092 unsigned int offs;
93
94 offs = priv->dir_reg_offset +
95 priv->data->dir_bit_offset + offset;
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +040096
97 regmap_update_bits(priv->syscon,
98 (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
99 BIT(offs % SYSCON_REG_BITS), 0);
100 }
101
102 return 0;
103}
104
105static int syscon_gpio_dir_out(struct gpio_chip *chip, unsigned offset, int val)
106{
Linus Walleijd27ad7a2015-12-07 14:38:36 +0100107 struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400108
109 if (priv->data->flags & GPIO_SYSCON_FEAT_DIR) {
Grygorii Strashko5a3e3f82014-09-03 20:05:33 +0300110 unsigned int offs;
111
112 offs = priv->dir_reg_offset +
113 priv->data->dir_bit_offset + offset;
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400114
115 regmap_update_bits(priv->syscon,
116 (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
117 BIT(offs % SYSCON_REG_BITS),
118 BIT(offs % SYSCON_REG_BITS));
119 }
120
Marek Vasut70728c22018-10-04 00:52:52 +0200121 chip->set(chip, offset, val);
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400122
123 return 0;
124}
125
126static const struct syscon_gpio_data clps711x_mctrl_gpio = {
127 /* ARM CLPS711X SYSFLG1 Bits 8-10 */
Alexander Shiyan2e607fc2016-06-04 10:10:00 +0300128 .compatible = "cirrus,ep7209-syscon1",
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400129 .flags = GPIO_SYSCON_FEAT_IN,
130 .bit_count = 3,
131 .dat_bit_offset = 0x40 * 8 + 8,
132};
133
Levin Ducf2ff872018-07-31 13:59:19 +0800134static void rockchip_gpio_set(struct gpio_chip *chip, unsigned int offset,
135 int val)
136{
137 struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
138 unsigned int offs;
139 u8 bit;
140 u32 data;
141 int ret;
142
143 offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
144 bit = offs % SYSCON_REG_BITS;
145 data = (val ? BIT(bit) : 0) | BIT(bit + 16);
146 ret = regmap_write(priv->syscon,
147 (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
148 data);
149 if (ret < 0)
150 dev_err(chip->parent, "gpio write failed ret(%d)\n", ret);
151}
152
153static const struct syscon_gpio_data rockchip_rk3328_gpio_mute = {
154 /* RK3328 GPIO_MUTE is an output only pin at GRF_SOC_CON10[1] */
155 .flags = GPIO_SYSCON_FEAT_OUT,
156 .bit_count = 1,
157 .dat_bit_offset = 0x0428 * 8 + 1,
158 .set = rockchip_gpio_set,
159};
160
Grygorii Strashko2134cb92014-09-03 20:05:34 +0300161#define KEYSTONE_LOCK_BIT BIT(0)
162
163static void keystone_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
164{
Linus Walleijd27ad7a2015-12-07 14:38:36 +0100165 struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
Grygorii Strashko2134cb92014-09-03 20:05:34 +0300166 unsigned int offs;
167 int ret;
168
169 offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
170
171 if (!val)
172 return;
173
174 ret = regmap_update_bits(
175 priv->syscon,
176 (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
177 BIT(offs % SYSCON_REG_BITS) | KEYSTONE_LOCK_BIT,
178 BIT(offs % SYSCON_REG_BITS) | KEYSTONE_LOCK_BIT);
179 if (ret < 0)
Linus Walleij58383c782015-11-04 09:56:26 +0100180 dev_err(chip->parent, "gpio write failed ret(%d)\n", ret);
Grygorii Strashko2134cb92014-09-03 20:05:34 +0300181}
182
183static const struct syscon_gpio_data keystone_dsp_gpio = {
184 /* ARM Keystone 2 */
185 .compatible = NULL,
186 .flags = GPIO_SYSCON_FEAT_OUT,
187 .bit_count = 28,
188 .dat_bit_offset = 4,
189 .set = keystone_gpio_set,
190};
191
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400192static const struct of_device_id syscon_gpio_ids[] = {
193 {
Alexander Shiyan2e607fc2016-06-04 10:10:00 +0300194 .compatible = "cirrus,ep7209-mctrl-gpio",
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400195 .data = &clps711x_mctrl_gpio,
196 },
Grygorii Strashko2134cb92014-09-03 20:05:34 +0300197 {
198 .compatible = "ti,keystone-dsp-gpio",
199 .data = &keystone_dsp_gpio,
200 },
Levin Ducf2ff872018-07-31 13:59:19 +0800201 {
202 .compatible = "rockchip,rk3328-grf-gpio",
203 .data = &rockchip_rk3328_gpio_mute,
204 },
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400205 { }
206};
207MODULE_DEVICE_TABLE(of, syscon_gpio_ids);
208
209static int syscon_gpio_probe(struct platform_device *pdev)
210{
211 struct device *dev = &pdev->dev;
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400212 struct syscon_gpio_priv *priv;
Grygorii Strashko5a3e3f82014-09-03 20:05:33 +0300213 struct device_node *np = dev->of_node;
214 int ret;
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400215
216 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
217 if (!priv)
218 return -ENOMEM;
219
Thierry Redingedf874e2018-04-30 09:38:16 +0200220 priv->data = of_device_get_match_data(dev);
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400221
Grygorii Strashko5a3e3f82014-09-03 20:05:33 +0300222 if (priv->data->compatible) {
223 priv->syscon = syscon_regmap_lookup_by_compatible(
224 priv->data->compatible);
225 if (IS_ERR(priv->syscon))
226 return PTR_ERR(priv->syscon);
227 } else {
228 priv->syscon =
229 syscon_regmap_lookup_by_phandle(np, "gpio,syscon-dev");
Heiko Stuebneraa1fdda2018-05-18 11:52:04 +0800230 if (IS_ERR(priv->syscon) && np->parent)
231 priv->syscon = syscon_node_to_regmap(np->parent);
Grygorii Strashko5a3e3f82014-09-03 20:05:33 +0300232 if (IS_ERR(priv->syscon))
233 return PTR_ERR(priv->syscon);
234
235 ret = of_property_read_u32_index(np, "gpio,syscon-dev", 1,
236 &priv->dreg_offset);
237 if (ret)
238 dev_err(dev, "can't read the data register offset!\n");
239
240 priv->dreg_offset <<= 3;
241
242 ret = of_property_read_u32_index(np, "gpio,syscon-dev", 2,
243 &priv->dir_reg_offset);
244 if (ret)
Grygorii Strashkoc6ac19d2015-03-24 20:42:42 +0200245 dev_dbg(dev, "can't read the dir register offset!\n");
Grygorii Strashko5a3e3f82014-09-03 20:05:33 +0300246
247 priv->dir_reg_offset <<= 3;
248 }
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400249
Linus Walleij58383c782015-11-04 09:56:26 +0100250 priv->chip.parent = dev;
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400251 priv->chip.owner = THIS_MODULE;
252 priv->chip.label = dev_name(dev);
253 priv->chip.base = -1;
254 priv->chip.ngpio = priv->data->bit_count;
255 priv->chip.get = syscon_gpio_get;
256 if (priv->data->flags & GPIO_SYSCON_FEAT_IN)
257 priv->chip.direction_input = syscon_gpio_dir_in;
258 if (priv->data->flags & GPIO_SYSCON_FEAT_OUT) {
Grygorii Strashko2c341d62014-09-03 20:05:32 +0300259 priv->chip.set = priv->data->set ? : syscon_gpio_set;
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400260 priv->chip.direction_output = syscon_gpio_dir_out;
261 }
262
263 platform_set_drvdata(pdev, priv);
264
Laxman Dewangan94c683a2016-02-22 17:43:28 +0530265 return devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv);
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400266}
267
268static struct platform_driver syscon_gpio_driver = {
269 .driver = {
270 .name = "gpio-syscon",
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400271 .of_match_table = syscon_gpio_ids,
272 },
273 .probe = syscon_gpio_probe,
Alexander Shiyan6a8a0c12014-03-11 21:55:14 +0400274};
275module_platform_driver(syscon_gpio_driver);
276
277MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
278MODULE_DESCRIPTION("SYSCON GPIO driver");
279MODULE_LICENSE("GPL");