1 // SPDX-License-Identifier: GPL-2.0-only
3 * GPIO driver for EXAR XRA1403 16-bit GPIO expander
5 * Copyright (c) 2017, General Electric Company
8 #include <linux/bitops.h>
9 #include <linux/gpio/driver.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/of_device.h>
14 #include <linux/of_gpio.h>
15 #include <linux/seq_file.h>
16 #include <linux/spi/spi.h>
17 #include <linux/regmap.h>
19 /* XRA1403 registers */
20 #define XRA_GSR 0x00 /* GPIO State */
21 #define XRA_OCR 0x02 /* Output Control */
22 #define XRA_PIR 0x04 /* Input Polarity Inversion */
23 #define XRA_GCR 0x06 /* GPIO Configuration */
24 #define XRA_PUR 0x08 /* Input Internal Pull-up Resistor Enable/Disable */
25 #define XRA_IER 0x0A /* Input Interrupt Enable */
26 #define XRA_TSCR 0x0C /* Output Three-State Control */
27 #define XRA_ISR 0x0E /* Input Interrupt Status */
28 #define XRA_REIR 0x10 /* Input Rising Edge Interrupt Enable */
29 #define XRA_FEIR 0x12 /* Input Falling Edge Interrupt Enable */
30 #define XRA_IFR 0x14 /* Input Filter Enable/Disable */
31 #define XRA_LAST 0x15 /* Bounds */
34 struct gpio_chip chip
;
35 struct regmap
*regmap
;
38 static const struct regmap_config xra1403_regmap_cfg
= {
43 .max_register
= XRA_LAST
,
46 static unsigned int to_reg(unsigned int reg
, unsigned int offset
)
48 return reg
+ (offset
> 7);
51 static int xra1403_direction_input(struct gpio_chip
*chip
, unsigned int offset
)
53 struct xra1403
*xra
= gpiochip_get_data(chip
);
55 return regmap_update_bits(xra
->regmap
, to_reg(XRA_GCR
, offset
),
56 BIT(offset
% 8), BIT(offset
% 8));
59 static int xra1403_direction_output(struct gpio_chip
*chip
, unsigned int offset
,
63 struct xra1403
*xra
= gpiochip_get_data(chip
);
65 ret
= regmap_update_bits(xra
->regmap
, to_reg(XRA_GCR
, offset
),
70 ret
= regmap_update_bits(xra
->regmap
, to_reg(XRA_OCR
, offset
),
71 BIT(offset
% 8), value
? BIT(offset
% 8) : 0);
76 static int xra1403_get_direction(struct gpio_chip
*chip
, unsigned int offset
)
80 struct xra1403
*xra
= gpiochip_get_data(chip
);
82 ret
= regmap_read(xra
->regmap
, to_reg(XRA_GCR
, offset
), &val
);
86 if (val
& BIT(offset
% 8))
87 return GPIO_LINE_DIRECTION_IN
;
89 return GPIO_LINE_DIRECTION_OUT
;
92 static int xra1403_get(struct gpio_chip
*chip
, unsigned int offset
)
96 struct xra1403
*xra
= gpiochip_get_data(chip
);
98 ret
= regmap_read(xra
->regmap
, to_reg(XRA_GSR
, offset
), &val
);
102 return !!(val
& BIT(offset
% 8));
105 static void xra1403_set(struct gpio_chip
*chip
, unsigned int offset
, int value
)
108 struct xra1403
*xra
= gpiochip_get_data(chip
);
110 ret
= regmap_update_bits(xra
->regmap
, to_reg(XRA_OCR
, offset
),
111 BIT(offset
% 8), value
? BIT(offset
% 8) : 0);
113 dev_err(chip
->parent
, "Failed to set pin: %d, ret: %d\n",
117 #ifdef CONFIG_DEBUG_FS
118 static void xra1403_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
121 struct xra1403
*xra
= gpiochip_get_data(chip
);
127 seq_puts(s
, "xra reg:");
128 for (reg
= 0; reg
<= XRA_LAST
; reg
++)
129 seq_printf(s
, " %2.2x", reg
);
130 seq_puts(s
, "\n value:");
131 for (reg
= 0; reg
< XRA_LAST
; reg
++) {
132 regmap_read(xra
->regmap
, reg
, &value
[reg
]);
133 seq_printf(s
, " %2.2x", value
[reg
]);
137 gcr
= value
[XRA_GCR
+ 1] << 8 | value
[XRA_GCR
];
138 gsr
= value
[XRA_GSR
+ 1] << 8 | value
[XRA_GSR
];
139 for (i
= 0; i
< chip
->ngpio
; i
++) {
140 const char *label
= gpiochip_is_requested(chip
, i
);
145 seq_printf(s
, " gpio-%-3d (%-12s) %s %s\n",
146 chip
->base
+ i
, label
,
147 (gcr
& BIT(i
)) ? "in" : "out",
148 (gsr
& BIT(i
)) ? "hi" : "lo");
152 #define xra1403_dbg_show NULL
155 static int xra1403_probe(struct spi_device
*spi
)
158 struct gpio_desc
*reset_gpio
;
161 xra
= devm_kzalloc(&spi
->dev
, sizeof(*xra
), GFP_KERNEL
);
165 /* bring the chip out of reset if reset pin is provided*/
166 reset_gpio
= devm_gpiod_get_optional(&spi
->dev
, "reset", GPIOD_OUT_LOW
);
167 if (IS_ERR(reset_gpio
))
168 dev_warn(&spi
->dev
, "Could not get reset-gpios\n");
170 xra
->chip
.direction_input
= xra1403_direction_input
;
171 xra
->chip
.direction_output
= xra1403_direction_output
;
172 xra
->chip
.get_direction
= xra1403_get_direction
;
173 xra
->chip
.get
= xra1403_get
;
174 xra
->chip
.set
= xra1403_set
;
176 xra
->chip
.dbg_show
= xra1403_dbg_show
;
178 xra
->chip
.ngpio
= 16;
179 xra
->chip
.label
= "xra1403";
182 xra
->chip
.can_sleep
= true;
183 xra
->chip
.parent
= &spi
->dev
;
184 xra
->chip
.owner
= THIS_MODULE
;
186 xra
->regmap
= devm_regmap_init_spi(spi
, &xra1403_regmap_cfg
);
187 if (IS_ERR(xra
->regmap
)) {
188 ret
= PTR_ERR(xra
->regmap
);
189 dev_err(&spi
->dev
, "Failed to allocate regmap: %d\n", ret
);
193 ret
= devm_gpiochip_add_data(&spi
->dev
, &xra
->chip
, xra
);
195 dev_err(&spi
->dev
, "Unable to register gpiochip\n");
199 spi_set_drvdata(spi
, xra
);
204 static const struct spi_device_id xra1403_ids
[] = {
208 MODULE_DEVICE_TABLE(spi
, xra1403_ids
);
210 static const struct of_device_id xra1403_spi_of_match
[] = {
211 { .compatible
= "exar,xra1403" },
214 MODULE_DEVICE_TABLE(of
, xra1403_spi_of_match
);
216 static struct spi_driver xra1403_driver
= {
217 .probe
= xra1403_probe
,
218 .id_table
= xra1403_ids
,
221 .of_match_table
= of_match_ptr(xra1403_spi_of_match
),
225 module_spi_driver(xra1403_driver
);
227 MODULE_AUTHOR("Nandor Han <nandor.han@ge.com>");
228 MODULE_AUTHOR("Semi Malinen <semi.malinen@ge.com>");
229 MODULE_DESCRIPTION("GPIO expander driver for EXAR XRA1403");
230 MODULE_LICENSE("GPL v2");