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 * @flags: Set of GPIO_SYSCON_FEAT_ flags:
27 * GPIO_SYSCON_FEAT_IN: GPIOs supports input,
28 * GPIO_SYSCON_FEAT_OUT: GPIOs supports output,
29 * GPIO_SYSCON_FEAT_DIR: GPIOs supports switch direction.
30 * @bit_count: Number of bits used as GPIOs.
31 * @dat_bit_offset: Offset (in bits) to the first GPIO bit.
32 * @dir_bit_offset: Optional offset (in bits) to the first bit to switch
33 * GPIO direction (Used with GPIO_SYSCON_FEAT_DIR flag).
34 * @set: HW specific callback to assigns output value
38 struct syscon_gpio_data
{
40 unsigned int bit_count
;
41 unsigned int dat_bit_offset
;
42 unsigned int dir_bit_offset
;
43 void (*set
)(struct gpio_chip
*chip
,
44 unsigned offset
, int value
);
47 struct syscon_gpio_priv
{
48 struct gpio_chip chip
;
49 struct regmap
*syscon
;
50 const struct syscon_gpio_data
*data
;
55 static int syscon_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
57 struct syscon_gpio_priv
*priv
= gpiochip_get_data(chip
);
58 unsigned int val
, offs
;
61 offs
= priv
->dreg_offset
+ priv
->data
->dat_bit_offset
+ offset
;
63 ret
= regmap_read(priv
->syscon
,
64 (offs
/ SYSCON_REG_BITS
) * SYSCON_REG_SIZE
, &val
);
68 return !!(val
& BIT(offs
% SYSCON_REG_BITS
));
71 static void syscon_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int val
)
73 struct syscon_gpio_priv
*priv
= gpiochip_get_data(chip
);
76 offs
= priv
->dreg_offset
+ priv
->data
->dat_bit_offset
+ offset
;
78 regmap_update_bits(priv
->syscon
,
79 (offs
/ SYSCON_REG_BITS
) * SYSCON_REG_SIZE
,
80 BIT(offs
% SYSCON_REG_BITS
),
81 val
? BIT(offs
% SYSCON_REG_BITS
) : 0);
84 static int syscon_gpio_dir_in(struct gpio_chip
*chip
, unsigned offset
)
86 struct syscon_gpio_priv
*priv
= gpiochip_get_data(chip
);
88 if (priv
->data
->flags
& GPIO_SYSCON_FEAT_DIR
) {
91 offs
= priv
->dir_reg_offset
+
92 priv
->data
->dir_bit_offset
+ offset
;
94 regmap_update_bits(priv
->syscon
,
95 (offs
/ SYSCON_REG_BITS
) * SYSCON_REG_SIZE
,
96 BIT(offs
% SYSCON_REG_BITS
), 0);
102 static int syscon_gpio_dir_out(struct gpio_chip
*chip
, unsigned offset
, int val
)
104 struct syscon_gpio_priv
*priv
= gpiochip_get_data(chip
);
106 if (priv
->data
->flags
& GPIO_SYSCON_FEAT_DIR
) {
109 offs
= priv
->dir_reg_offset
+
110 priv
->data
->dir_bit_offset
+ offset
;
112 regmap_update_bits(priv
->syscon
,
113 (offs
/ SYSCON_REG_BITS
) * SYSCON_REG_SIZE
,
114 BIT(offs
% SYSCON_REG_BITS
),
115 BIT(offs
% SYSCON_REG_BITS
));
118 chip
->set(chip
, offset
, val
);
123 static const struct syscon_gpio_data clps711x_mctrl_gpio
= {
124 /* ARM CLPS711X SYSFLG1 Bits 8-10 */
125 .flags
= GPIO_SYSCON_FEAT_IN
,
127 .dat_bit_offset
= 0x40 * 8 + 8,
130 static void rockchip_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
133 struct syscon_gpio_priv
*priv
= gpiochip_get_data(chip
);
139 offs
= priv
->dreg_offset
+ priv
->data
->dat_bit_offset
+ offset
;
140 bit
= offs
% SYSCON_REG_BITS
;
141 data
= (val
? BIT(bit
) : 0) | BIT(bit
+ 16);
142 ret
= regmap_write(priv
->syscon
,
143 (offs
/ SYSCON_REG_BITS
) * SYSCON_REG_SIZE
,
146 dev_err(chip
->parent
, "gpio write failed ret(%d)\n", ret
);
149 static const struct syscon_gpio_data rockchip_rk3328_gpio_mute
= {
150 /* RK3328 GPIO_MUTE is an output only pin at GRF_SOC_CON10[1] */
151 .flags
= GPIO_SYSCON_FEAT_OUT
,
153 .dat_bit_offset
= 0x0428 * 8 + 1,
154 .set
= rockchip_gpio_set
,
157 #define KEYSTONE_LOCK_BIT BIT(0)
159 static void keystone_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int val
)
161 struct syscon_gpio_priv
*priv
= gpiochip_get_data(chip
);
165 offs
= priv
->dreg_offset
+ priv
->data
->dat_bit_offset
+ offset
;
170 ret
= regmap_update_bits(
172 (offs
/ SYSCON_REG_BITS
) * SYSCON_REG_SIZE
,
173 BIT(offs
% SYSCON_REG_BITS
) | KEYSTONE_LOCK_BIT
,
174 BIT(offs
% SYSCON_REG_BITS
) | KEYSTONE_LOCK_BIT
);
176 dev_err(chip
->parent
, "gpio write failed ret(%d)\n", ret
);
179 static const struct syscon_gpio_data keystone_dsp_gpio
= {
181 .flags
= GPIO_SYSCON_FEAT_OUT
,
184 .set
= keystone_gpio_set
,
187 static const struct of_device_id syscon_gpio_ids
[] = {
189 .compatible
= "cirrus,ep7209-mctrl-gpio",
190 .data
= &clps711x_mctrl_gpio
,
193 .compatible
= "ti,keystone-dsp-gpio",
194 .data
= &keystone_dsp_gpio
,
197 .compatible
= "rockchip,rk3328-grf-gpio",
198 .data
= &rockchip_rk3328_gpio_mute
,
202 MODULE_DEVICE_TABLE(of
, syscon_gpio_ids
);
204 static int syscon_gpio_probe(struct platform_device
*pdev
)
206 struct device
*dev
= &pdev
->dev
;
207 struct syscon_gpio_priv
*priv
;
208 struct device_node
*np
= dev
->of_node
;
210 bool use_parent_regmap
= false;
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 use_parent_regmap
= true;
223 if (IS_ERR(priv
->syscon
))
224 return PTR_ERR(priv
->syscon
);
226 if (!use_parent_regmap
) {
227 ret
= of_property_read_u32_index(np
, "gpio,syscon-dev", 1,
230 dev_err(dev
, "can't read the data register offset!\n");
232 priv
->dreg_offset
<<= 3;
234 ret
= of_property_read_u32_index(np
, "gpio,syscon-dev", 2,
235 &priv
->dir_reg_offset
);
237 dev_dbg(dev
, "can't read the dir register offset!\n");
239 priv
->dir_reg_offset
<<= 3;
242 priv
->chip
.parent
= dev
;
243 priv
->chip
.owner
= THIS_MODULE
;
244 priv
->chip
.label
= dev_name(dev
);
245 priv
->chip
.base
= -1;
246 priv
->chip
.ngpio
= priv
->data
->bit_count
;
247 priv
->chip
.get
= syscon_gpio_get
;
248 if (priv
->data
->flags
& GPIO_SYSCON_FEAT_IN
)
249 priv
->chip
.direction_input
= syscon_gpio_dir_in
;
250 if (priv
->data
->flags
& GPIO_SYSCON_FEAT_OUT
) {
251 priv
->chip
.set
= priv
->data
->set
? : syscon_gpio_set
;
252 priv
->chip
.direction_output
= syscon_gpio_dir_out
;
255 return devm_gpiochip_add_data(&pdev
->dev
, &priv
->chip
, priv
);
258 static struct platform_driver syscon_gpio_driver
= {
260 .name
= "gpio-syscon",
261 .of_match_table
= syscon_gpio_ids
,
263 .probe
= syscon_gpio_probe
,
265 module_platform_driver(syscon_gpio_driver
);
267 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
268 MODULE_DESCRIPTION("SYSCON GPIO driver");
269 MODULE_LICENSE("GPL");