1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
9 #include <linux/gpio/driver.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
14 #include <linux/mfd/syscon.h>
16 #define GPIO_SYSCON_FEAT_IN BIT(0)
17 #define GPIO_SYSCON_FEAT_OUT BIT(1)
18 #define GPIO_SYSCON_FEAT_DIR BIT(2)
20 /* SYSCON driver is designed to use 32-bit wide registers */
21 #define SYSCON_REG_SIZE (4)
22 #define SYSCON_REG_BITS (SYSCON_REG_SIZE * 8)
25 * struct syscon_gpio_data - Configuration for the device.
26 * @compatible: SYSCON driver compatible string.
27 * @flags: Set of GPIO_SYSCON_FEAT_ flags:
28 * GPIO_SYSCON_FEAT_IN: GPIOs supports input,
29 * GPIO_SYSCON_FEAT_OUT: GPIOs supports output,
30 * GPIO_SYSCON_FEAT_DIR: GPIOs supports switch direction.
31 * @bit_count: Number of bits used as GPIOs.
32 * @dat_bit_offset: Offset (in bits) to the first GPIO bit.
33 * @dir_bit_offset: Optional offset (in bits) to the first bit to switch
34 * GPIO direction (Used with GPIO_SYSCON_FEAT_DIR flag).
35 * @set: HW specific callback to assigns output value
39 struct syscon_gpio_data {
41 unsigned int bit_count;
42 unsigned int dat_bit_offset;
43 unsigned int dir_bit_offset;
44 void (*set)(struct gpio_chip *chip,
45 unsigned offset, int value);
48 struct syscon_gpio_priv {
49 struct gpio_chip chip;
50 struct regmap *syscon;
51 const struct syscon_gpio_data *data;
56 static int syscon_gpio_get(struct gpio_chip *chip, unsigned offset)
58 struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
59 unsigned int val, offs;
62 offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
64 ret = regmap_read(priv->syscon,
65 (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE, &val);
69 return !!(val & BIT(offs % SYSCON_REG_BITS));
72 static void syscon_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
74 struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
77 offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
79 regmap_update_bits(priv->syscon,
80 (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
81 BIT(offs % SYSCON_REG_BITS),
82 val ? BIT(offs % SYSCON_REG_BITS) : 0);
85 static int syscon_gpio_dir_in(struct gpio_chip *chip, unsigned offset)
87 struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
89 if (priv->data->flags & GPIO_SYSCON_FEAT_DIR) {
92 offs = priv->dir_reg_offset +
93 priv->data->dir_bit_offset + offset;
95 regmap_update_bits(priv->syscon,
96 (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
97 BIT(offs % SYSCON_REG_BITS), 0);
103 static int syscon_gpio_dir_out(struct gpio_chip *chip, unsigned offset, int val)
105 struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
107 if (priv->data->flags & GPIO_SYSCON_FEAT_DIR) {
110 offs = priv->dir_reg_offset +
111 priv->data->dir_bit_offset + offset;
113 regmap_update_bits(priv->syscon,
114 (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
115 BIT(offs % SYSCON_REG_BITS),
116 BIT(offs % SYSCON_REG_BITS));
119 chip->set(chip, offset, val);
124 static const struct syscon_gpio_data clps711x_mctrl_gpio = {
125 /* ARM CLPS711X SYSFLG1 Bits 8-10 */
126 .flags = GPIO_SYSCON_FEAT_IN,
128 .dat_bit_offset = 0x40 * 8 + 8,
131 static void rockchip_gpio_set(struct gpio_chip *chip, unsigned int offset,
134 struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
140 offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
141 bit = offs % SYSCON_REG_BITS;
142 data = (val ? BIT(bit) : 0) | BIT(bit + 16);
143 ret = regmap_write(priv->syscon,
144 (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
147 dev_err(chip->parent, "gpio write failed ret(%d)\n", ret);
150 static const struct syscon_gpio_data rockchip_rk3328_gpio_mute = {
151 /* RK3328 GPIO_MUTE is an output only pin at GRF_SOC_CON10[1] */
152 .flags = GPIO_SYSCON_FEAT_OUT,
154 .dat_bit_offset = 0x0428 * 8 + 1,
155 .set = rockchip_gpio_set,
158 #define KEYSTONE_LOCK_BIT BIT(0)
160 static void keystone_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
162 struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
166 offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
171 ret = regmap_update_bits(
173 (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
174 BIT(offs % SYSCON_REG_BITS) | KEYSTONE_LOCK_BIT,
175 BIT(offs % SYSCON_REG_BITS) | KEYSTONE_LOCK_BIT);
177 dev_err(chip->parent, "gpio write failed ret(%d)\n", ret);
180 static const struct syscon_gpio_data keystone_dsp_gpio = {
182 .flags = GPIO_SYSCON_FEAT_OUT,
185 .set = keystone_gpio_set,
188 static const struct of_device_id syscon_gpio_ids[] = {
190 .compatible = "cirrus,ep7209-mctrl-gpio",
191 .data = &clps711x_mctrl_gpio,
194 .compatible = "ti,keystone-dsp-gpio",
195 .data = &keystone_dsp_gpio,
198 .compatible = "rockchip,rk3328-grf-gpio",
199 .data = &rockchip_rk3328_gpio_mute,
203 MODULE_DEVICE_TABLE(of, syscon_gpio_ids);
205 static int syscon_gpio_probe(struct platform_device *pdev)
207 struct device *dev = &pdev->dev;
208 struct syscon_gpio_priv *priv;
209 struct device_node *np = dev->of_node;
212 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
216 priv->data = of_device_get_match_data(dev);
218 priv->syscon = syscon_regmap_lookup_by_phandle(np, "gpio,syscon-dev");
219 if (IS_ERR(priv->syscon) && np->parent)
220 priv->syscon = syscon_node_to_regmap(np->parent);
221 if (IS_ERR(priv->syscon))
222 return PTR_ERR(priv->syscon);
224 ret = of_property_read_u32_index(np, "gpio,syscon-dev", 1,
227 dev_err(dev, "can't read the data register offset!\n");
229 priv->dreg_offset <<= 3;
231 ret = of_property_read_u32_index(np, "gpio,syscon-dev", 2,
232 &priv->dir_reg_offset);
234 dev_dbg(dev, "can't read the dir register offset!\n");
236 priv->dir_reg_offset <<= 3;
238 priv->chip.parent = dev;
239 priv->chip.owner = THIS_MODULE;
240 priv->chip.label = dev_name(dev);
241 priv->chip.base = -1;
242 priv->chip.ngpio = priv->data->bit_count;
243 priv->chip.get = syscon_gpio_get;
244 if (priv->data->flags & GPIO_SYSCON_FEAT_IN)
245 priv->chip.direction_input = syscon_gpio_dir_in;
246 if (priv->data->flags & GPIO_SYSCON_FEAT_OUT) {
247 priv->chip.set = priv->data->set ? : syscon_gpio_set;
248 priv->chip.direction_output = syscon_gpio_dir_out;
251 return devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv);
254 static struct platform_driver syscon_gpio_driver = {
256 .name = "gpio-syscon",
257 .of_match_table = syscon_gpio_ids,
259 .probe = syscon_gpio_probe,
261 module_platform_driver(syscon_gpio_driver);
263 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
264 MODULE_DESCRIPTION("SYSCON GPIO driver");
265 MODULE_LICENSE("GPL");