1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2020 Daniel Palmer<daniel@thingy.jp> */
4 #include <linux/bitops.h>
5 #include <linux/kernel.h>
6 #include <linux/types.h>
9 #include <linux/of_irq.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
14 #include <dt-bindings/gpio/msc313-gpio.h>
15 #include <dt-bindings/interrupt-controller/arm-gic.h>
17 #define DRIVER_NAME "gpio-msc313"
19 #define MSC313_GPIO_IN BIT(0)
20 #define MSC313_GPIO_OUT BIT(4)
21 #define MSC313_GPIO_OEN BIT(5)
24 * These bits need to be saved to correctly restore the
25 * gpio state when resuming from suspend to memory.
27 #define MSC313_GPIO_BITSTOSAVE (MSC313_GPIO_OUT | MSC313_GPIO_OEN)
29 /* pad names for fuart, same for all SoCs so far */
30 #define MSC313_PINNAME_FUART_RX "fuart_rx"
31 #define MSC313_PINNAME_FUART_TX "fuart_tx"
32 #define MSC313_PINNAME_FUART_CTS "fuart_cts"
33 #define MSC313_PINNAME_FUART_RTS "fuart_rts"
35 /* pad names for sr, mercury5 is different */
36 #define MSC313_PINNAME_SR_IO2 "sr_io2"
37 #define MSC313_PINNAME_SR_IO3 "sr_io3"
38 #define MSC313_PINNAME_SR_IO4 "sr_io4"
39 #define MSC313_PINNAME_SR_IO5 "sr_io5"
40 #define MSC313_PINNAME_SR_IO6 "sr_io6"
41 #define MSC313_PINNAME_SR_IO7 "sr_io7"
42 #define MSC313_PINNAME_SR_IO8 "sr_io8"
43 #define MSC313_PINNAME_SR_IO9 "sr_io9"
44 #define MSC313_PINNAME_SR_IO10 "sr_io10"
45 #define MSC313_PINNAME_SR_IO11 "sr_io11"
46 #define MSC313_PINNAME_SR_IO12 "sr_io12"
47 #define MSC313_PINNAME_SR_IO13 "sr_io13"
48 #define MSC313_PINNAME_SR_IO14 "sr_io14"
49 #define MSC313_PINNAME_SR_IO15 "sr_io15"
50 #define MSC313_PINNAME_SR_IO16 "sr_io16"
51 #define MSC313_PINNAME_SR_IO17 "sr_io17"
53 /* pad names for sd, same for all SoCs so far */
54 #define MSC313_PINNAME_SD_CLK "sd_clk"
55 #define MSC313_PINNAME_SD_CMD "sd_cmd"
56 #define MSC313_PINNAME_SD_D0 "sd_d0"
57 #define MSC313_PINNAME_SD_D1 "sd_d1"
58 #define MSC313_PINNAME_SD_D2 "sd_d2"
59 #define MSC313_PINNAME_SD_D3 "sd_d3"
61 /* pad names for i2c1, same for all SoCs so for */
62 #define MSC313_PINNAME_I2C1_SCL "i2c1_scl"
63 #define MSC313_PINNAME_I2C1_SCA "i2c1_sda"
65 /* pad names for spi0, same for all SoCs so far */
66 #define MSC313_PINNAME_SPI0_CZ "spi0_cz"
67 #define MSC313_PINNAME_SPI0_CK "spi0_ck"
68 #define MSC313_PINNAME_SPI0_DI "spi0_di"
69 #define MSC313_PINNAME_SPI0_DO "spi0_do"
72 MSC313_PINNAME_FUART_RX, \
73 MSC313_PINNAME_FUART_TX, \
74 MSC313_PINNAME_FUART_CTS, \
75 MSC313_PINNAME_FUART_RTS
77 #define OFF_FUART_RX 0x50
78 #define OFF_FUART_TX 0x54
79 #define OFF_FUART_CTS 0x58
80 #define OFF_FUART_RTS 0x5c
82 #define FUART_OFFSETS \
89 MSC313_PINNAME_SR_IO2, \
90 MSC313_PINNAME_SR_IO3, \
91 MSC313_PINNAME_SR_IO4, \
92 MSC313_PINNAME_SR_IO5, \
93 MSC313_PINNAME_SR_IO6, \
94 MSC313_PINNAME_SR_IO7, \
95 MSC313_PINNAME_SR_IO8, \
96 MSC313_PINNAME_SR_IO9, \
97 MSC313_PINNAME_SR_IO10, \
98 MSC313_PINNAME_SR_IO11, \
99 MSC313_PINNAME_SR_IO12, \
100 MSC313_PINNAME_SR_IO13, \
101 MSC313_PINNAME_SR_IO14, \
102 MSC313_PINNAME_SR_IO15, \
103 MSC313_PINNAME_SR_IO16, \
104 MSC313_PINNAME_SR_IO17
106 #define OFF_SR_IO2 0x88
107 #define OFF_SR_IO3 0x8c
108 #define OFF_SR_IO4 0x90
109 #define OFF_SR_IO5 0x94
110 #define OFF_SR_IO6 0x98
111 #define OFF_SR_IO7 0x9c
112 #define OFF_SR_IO8 0xa0
113 #define OFF_SR_IO9 0xa4
114 #define OFF_SR_IO10 0xa8
115 #define OFF_SR_IO11 0xac
116 #define OFF_SR_IO12 0xb0
117 #define OFF_SR_IO13 0xb4
118 #define OFF_SR_IO14 0xb8
119 #define OFF_SR_IO15 0xbc
120 #define OFF_SR_IO16 0xc0
121 #define OFF_SR_IO17 0xc4
142 MSC313_PINNAME_SD_CLK, \
143 MSC313_PINNAME_SD_CMD, \
144 MSC313_PINNAME_SD_D0, \
145 MSC313_PINNAME_SD_D1, \
146 MSC313_PINNAME_SD_D2, \
149 #define OFF_SD_CLK 0x140
150 #define OFF_SD_CMD 0x144
151 #define OFF_SD_D0 0x148
152 #define OFF_SD_D1 0x14c
153 #define OFF_SD_D2 0x150
154 #define OFF_SD_D3 0x154
165 MSC313_PINNAME_I2C1_SCL, \
166 MSC313_PINNAME_I2C1_SCA
168 #define OFF_I2C1_SCL 0x188
169 #define OFF_I2C1_SCA 0x18c
171 #define I2C1_OFFSETS \
176 MSC313_PINNAME_SPI0_CZ, \
177 MSC313_PINNAME_SPI0_CK, \
178 MSC313_PINNAME_SPI0_DI, \
179 MSC313_PINNAME_SPI0_DO
181 #define OFF_SPI0_CZ 0x1c0
182 #define OFF_SPI0_CK 0x1c4
183 #define OFF_SPI0_DI 0x1c8
184 #define OFF_SPI0_DO 0x1cc
186 #define SPI0_OFFSETS \
192 struct msc313_gpio_data {
193 const char * const *names;
194 const unsigned int *offsets;
195 const unsigned int num;
198 #define MSC313_GPIO_CHIPDATA(_chip) \
199 static const struct msc313_gpio_data _chip##_data = { \
200 .names = _chip##_names, \
201 .offsets = _chip##_offsets, \
202 .num = ARRAY_SIZE(_chip##_offsets), \
205 #ifdef CONFIG_MACH_INFINITY
206 static const char * const msc313_names[] = {
214 static const unsigned int msc313_offsets[] = {
222 MSC313_GPIO_CHIPDATA(msc313);
225 * Unlike the msc313(e) the ssd20xd have a bunch of pins
226 * that are actually called gpio probably because they
227 * have no dedicated function.
229 #define SSD20XD_PINNAME_GPIO0 "gpio0"
230 #define SSD20XD_PINNAME_GPIO1 "gpio1"
231 #define SSD20XD_PINNAME_GPIO2 "gpio2"
232 #define SSD20XD_PINNAME_GPIO3 "gpio3"
233 #define SSD20XD_PINNAME_GPIO4 "gpio4"
234 #define SSD20XD_PINNAME_GPIO5 "gpio5"
235 #define SSD20XD_PINNAME_GPIO6 "gpio6"
236 #define SSD20XD_PINNAME_GPIO7 "gpio7"
237 #define SSD20XD_PINNAME_GPIO10 "gpio10"
238 #define SSD20XD_PINNAME_GPIO11 "gpio11"
239 #define SSD20XD_PINNAME_GPIO12 "gpio12"
240 #define SSD20XD_PINNAME_GPIO13 "gpio13"
241 #define SSD20XD_PINNAME_GPIO14 "gpio14"
242 #define SSD20XD_PINNAME_GPIO85 "gpio85"
243 #define SSD20XD_PINNAME_GPIO86 "gpio86"
244 #define SSD20XD_PINNAME_GPIO90 "gpio90"
246 #define SSD20XD_GPIO_NAMES SSD20XD_PINNAME_GPIO0, \
247 SSD20XD_PINNAME_GPIO1, \
248 SSD20XD_PINNAME_GPIO2, \
249 SSD20XD_PINNAME_GPIO3, \
250 SSD20XD_PINNAME_GPIO4, \
251 SSD20XD_PINNAME_GPIO5, \
252 SSD20XD_PINNAME_GPIO6, \
253 SSD20XD_PINNAME_GPIO7, \
254 SSD20XD_PINNAME_GPIO10, \
255 SSD20XD_PINNAME_GPIO11, \
256 SSD20XD_PINNAME_GPIO12, \
257 SSD20XD_PINNAME_GPIO13, \
258 SSD20XD_PINNAME_GPIO14, \
259 SSD20XD_PINNAME_GPIO85, \
260 SSD20XD_PINNAME_GPIO86, \
261 SSD20XD_PINNAME_GPIO90
263 #define SSD20XD_GPIO_OFF_GPIO0 0x0
264 #define SSD20XD_GPIO_OFF_GPIO1 0x4
265 #define SSD20XD_GPIO_OFF_GPIO2 0x8
266 #define SSD20XD_GPIO_OFF_GPIO3 0xc
267 #define SSD20XD_GPIO_OFF_GPIO4 0x10
268 #define SSD20XD_GPIO_OFF_GPIO5 0x14
269 #define SSD20XD_GPIO_OFF_GPIO6 0x18
270 #define SSD20XD_GPIO_OFF_GPIO7 0x1c
271 #define SSD20XD_GPIO_OFF_GPIO10 0x28
272 #define SSD20XD_GPIO_OFF_GPIO11 0x2c
273 #define SSD20XD_GPIO_OFF_GPIO12 0x30
274 #define SSD20XD_GPIO_OFF_GPIO13 0x34
275 #define SSD20XD_GPIO_OFF_GPIO14 0x38
276 #define SSD20XD_GPIO_OFF_GPIO85 0x100
277 #define SSD20XD_GPIO_OFF_GPIO86 0x104
278 #define SSD20XD_GPIO_OFF_GPIO90 0x114
280 #define SSD20XD_GPIO_OFFSETS SSD20XD_GPIO_OFF_GPIO0, \
281 SSD20XD_GPIO_OFF_GPIO1, \
282 SSD20XD_GPIO_OFF_GPIO2, \
283 SSD20XD_GPIO_OFF_GPIO3, \
284 SSD20XD_GPIO_OFF_GPIO4, \
285 SSD20XD_GPIO_OFF_GPIO5, \
286 SSD20XD_GPIO_OFF_GPIO6, \
287 SSD20XD_GPIO_OFF_GPIO7, \
288 SSD20XD_GPIO_OFF_GPIO10, \
289 SSD20XD_GPIO_OFF_GPIO11, \
290 SSD20XD_GPIO_OFF_GPIO12, \
291 SSD20XD_GPIO_OFF_GPIO13, \
292 SSD20XD_GPIO_OFF_GPIO14, \
293 SSD20XD_GPIO_OFF_GPIO85, \
294 SSD20XD_GPIO_OFF_GPIO86, \
295 SSD20XD_GPIO_OFF_GPIO90
297 /* "ttl" pins lcd interface pins */
298 #define SSD20XD_PINNAME_TTL0 "ttl0"
299 #define SSD20XD_PINNAME_TTL1 "ttl1"
300 #define SSD20XD_PINNAME_TTL2 "ttl2"
301 #define SSD20XD_PINNAME_TTL3 "ttl3"
302 #define SSD20XD_PINNAME_TTL4 "ttl4"
303 #define SSD20XD_PINNAME_TTL5 "ttl5"
304 #define SSD20XD_PINNAME_TTL6 "ttl6"
305 #define SSD20XD_PINNAME_TTL7 "ttl7"
306 #define SSD20XD_PINNAME_TTL8 "ttl8"
307 #define SSD20XD_PINNAME_TTL9 "ttl9"
308 #define SSD20XD_PINNAME_TTL10 "ttl10"
309 #define SSD20XD_PINNAME_TTL11 "ttl11"
310 #define SSD20XD_PINNAME_TTL12 "ttl12"
311 #define SSD20XD_PINNAME_TTL13 "ttl13"
312 #define SSD20XD_PINNAME_TTL14 "ttl14"
313 #define SSD20XD_PINNAME_TTL15 "ttl15"
314 #define SSD20XD_PINNAME_TTL16 "ttl16"
315 #define SSD20XD_PINNAME_TTL17 "ttl17"
316 #define SSD20XD_PINNAME_TTL18 "ttl18"
317 #define SSD20XD_PINNAME_TTL19 "ttl19"
318 #define SSD20XD_PINNAME_TTL20 "ttl20"
319 #define SSD20XD_PINNAME_TTL21 "ttl21"
320 #define SSD20XD_PINNAME_TTL22 "ttl22"
321 #define SSD20XD_PINNAME_TTL23 "ttl23"
322 #define SSD20XD_PINNAME_TTL24 "ttl24"
323 #define SSD20XD_PINNAME_TTL25 "ttl25"
324 #define SSD20XD_PINNAME_TTL26 "ttl26"
325 #define SSD20XD_PINNAME_TTL27 "ttl27"
327 #define SSD20XD_TTL_PINNAMES SSD20XD_PINNAME_TTL0, \
328 SSD20XD_PINNAME_TTL1, \
329 SSD20XD_PINNAME_TTL2, \
330 SSD20XD_PINNAME_TTL3, \
331 SSD20XD_PINNAME_TTL4, \
332 SSD20XD_PINNAME_TTL5, \
333 SSD20XD_PINNAME_TTL6, \
334 SSD20XD_PINNAME_TTL7, \
335 SSD20XD_PINNAME_TTL8, \
336 SSD20XD_PINNAME_TTL9, \
337 SSD20XD_PINNAME_TTL10, \
338 SSD20XD_PINNAME_TTL11, \
339 SSD20XD_PINNAME_TTL12, \
340 SSD20XD_PINNAME_TTL13, \
341 SSD20XD_PINNAME_TTL14, \
342 SSD20XD_PINNAME_TTL15, \
343 SSD20XD_PINNAME_TTL16, \
344 SSD20XD_PINNAME_TTL17, \
345 SSD20XD_PINNAME_TTL18, \
346 SSD20XD_PINNAME_TTL19, \
347 SSD20XD_PINNAME_TTL20, \
348 SSD20XD_PINNAME_TTL21, \
349 SSD20XD_PINNAME_TTL22, \
350 SSD20XD_PINNAME_TTL23, \
351 SSD20XD_PINNAME_TTL24, \
352 SSD20XD_PINNAME_TTL25, \
353 SSD20XD_PINNAME_TTL26, \
354 SSD20XD_PINNAME_TTL27
356 #define SSD20XD_TTL_OFFSET_TTL0 0x80
357 #define SSD20XD_TTL_OFFSET_TTL1 0x84
358 #define SSD20XD_TTL_OFFSET_TTL2 0x88
359 #define SSD20XD_TTL_OFFSET_TTL3 0x8c
360 #define SSD20XD_TTL_OFFSET_TTL4 0x90
361 #define SSD20XD_TTL_OFFSET_TTL5 0x94
362 #define SSD20XD_TTL_OFFSET_TTL6 0x98
363 #define SSD20XD_TTL_OFFSET_TTL7 0x9c
364 #define SSD20XD_TTL_OFFSET_TTL8 0xa0
365 #define SSD20XD_TTL_OFFSET_TTL9 0xa4
366 #define SSD20XD_TTL_OFFSET_TTL10 0xa8
367 #define SSD20XD_TTL_OFFSET_TTL11 0xac
368 #define SSD20XD_TTL_OFFSET_TTL12 0xb0
369 #define SSD20XD_TTL_OFFSET_TTL13 0xb4
370 #define SSD20XD_TTL_OFFSET_TTL14 0xb8
371 #define SSD20XD_TTL_OFFSET_TTL15 0xbc
372 #define SSD20XD_TTL_OFFSET_TTL16 0xc0
373 #define SSD20XD_TTL_OFFSET_TTL17 0xc4
374 #define SSD20XD_TTL_OFFSET_TTL18 0xc8
375 #define SSD20XD_TTL_OFFSET_TTL19 0xcc
376 #define SSD20XD_TTL_OFFSET_TTL20 0xd0
377 #define SSD20XD_TTL_OFFSET_TTL21 0xd4
378 #define SSD20XD_TTL_OFFSET_TTL22 0xd8
379 #define SSD20XD_TTL_OFFSET_TTL23 0xdc
380 #define SSD20XD_TTL_OFFSET_TTL24 0xe0
381 #define SSD20XD_TTL_OFFSET_TTL25 0xe4
382 #define SSD20XD_TTL_OFFSET_TTL26 0xe8
383 #define SSD20XD_TTL_OFFSET_TTL27 0xec
385 #define SSD20XD_TTL_OFFSETS SSD20XD_TTL_OFFSET_TTL0, \
386 SSD20XD_TTL_OFFSET_TTL1, \
387 SSD20XD_TTL_OFFSET_TTL2, \
388 SSD20XD_TTL_OFFSET_TTL3, \
389 SSD20XD_TTL_OFFSET_TTL4, \
390 SSD20XD_TTL_OFFSET_TTL5, \
391 SSD20XD_TTL_OFFSET_TTL6, \
392 SSD20XD_TTL_OFFSET_TTL7, \
393 SSD20XD_TTL_OFFSET_TTL8, \
394 SSD20XD_TTL_OFFSET_TTL9, \
395 SSD20XD_TTL_OFFSET_TTL10, \
396 SSD20XD_TTL_OFFSET_TTL11, \
397 SSD20XD_TTL_OFFSET_TTL12, \
398 SSD20XD_TTL_OFFSET_TTL13, \
399 SSD20XD_TTL_OFFSET_TTL14, \
400 SSD20XD_TTL_OFFSET_TTL15, \
401 SSD20XD_TTL_OFFSET_TTL16, \
402 SSD20XD_TTL_OFFSET_TTL17, \
403 SSD20XD_TTL_OFFSET_TTL18, \
404 SSD20XD_TTL_OFFSET_TTL19, \
405 SSD20XD_TTL_OFFSET_TTL20, \
406 SSD20XD_TTL_OFFSET_TTL21, \
407 SSD20XD_TTL_OFFSET_TTL22, \
408 SSD20XD_TTL_OFFSET_TTL23, \
409 SSD20XD_TTL_OFFSET_TTL24, \
410 SSD20XD_TTL_OFFSET_TTL25, \
411 SSD20XD_TTL_OFFSET_TTL26, \
412 SSD20XD_TTL_OFFSET_TTL27
414 /* On the ssd20xd the two normal uarts have dedicated pins */
415 #define SSD20XD_PINNAME_UART0_RX "uart0_rx"
416 #define SSD20XD_PINNAME_UART0_TX "uart0_tx"
418 #define SSD20XD_UART0_NAMES \
419 SSD20XD_PINNAME_UART0_RX, \
420 SSD20XD_PINNAME_UART0_TX
422 #define SSD20XD_PINNAME_UART1_RX "uart1_rx"
423 #define SSD20XD_PINNAME_UART1_TX "uart1_tx"
425 #define SSD20XD_UART1_NAMES \
426 SSD20XD_PINNAME_UART1_RX, \
427 SSD20XD_PINNAME_UART1_TX
429 #define SSD20XD_OFF_UART0_RX 0x60
430 #define SSD20XD_OFF_UART0_TX 0x64
432 #define SSD20XD_UART0_OFFSETS \
433 SSD20XD_OFF_UART0_RX, \
436 #define SSD20XD_OFF_UART1_RX 0x68
437 #define SSD20XD_OFF_UART1_TX 0x6c
439 #define SSD20XD_UART1_OFFSETS \
440 SSD20XD_OFF_UART1_RX, \
444 * ssd20x has the same pin names but different ordering
445 * of the registers that control the gpio.
447 #define SSD20XD_OFF_SD_D0 0x140
448 #define SSD20XD_OFF_SD_D1 0x144
449 #define SSD20XD_OFF_SD_D2 0x148
450 #define SSD20XD_OFF_SD_D3 0x14c
451 #define SSD20XD_OFF_SD_CMD 0x150
452 #define SSD20XD_OFF_SD_CLK 0x154
454 #define SSD20XD_SD_OFFSETS SSD20XD_OFF_SD_CLK, \
455 SSD20XD_OFF_SD_CMD, \
461 static const char * const ssd20xd_names[] = {
466 SSD20XD_TTL_PINNAMES,
470 static const unsigned int ssd20xd_offsets[] = {
473 SSD20XD_UART0_OFFSETS,
474 SSD20XD_UART1_OFFSETS,
476 SSD20XD_GPIO_OFFSETS,
479 MSC313_GPIO_CHIPDATA(ssd20xd);
484 const struct msc313_gpio_data *gpio_data;
488 static void msc313_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
490 struct msc313_gpio *gpio = gpiochip_get_data(chip);
491 u8 gpioreg = readb_relaxed(gpio->base + gpio->gpio_data->offsets[offset]);
494 gpioreg |= MSC313_GPIO_OUT;
496 gpioreg &= ~MSC313_GPIO_OUT;
498 writeb_relaxed(gpioreg, gpio->base + gpio->gpio_data->offsets[offset]);
501 static int msc313_gpio_get(struct gpio_chip *chip, unsigned int offset)
503 struct msc313_gpio *gpio = gpiochip_get_data(chip);
505 return readb_relaxed(gpio->base + gpio->gpio_data->offsets[offset]) & MSC313_GPIO_IN;
508 static int msc313_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
510 struct msc313_gpio *gpio = gpiochip_get_data(chip);
511 u8 gpioreg = readb_relaxed(gpio->base + gpio->gpio_data->offsets[offset]);
513 gpioreg |= MSC313_GPIO_OEN;
514 writeb_relaxed(gpioreg, gpio->base + gpio->gpio_data->offsets[offset]);
519 static int msc313_gpio_direction_output(struct gpio_chip *chip, unsigned int offset, int value)
521 struct msc313_gpio *gpio = gpiochip_get_data(chip);
522 u8 gpioreg = readb_relaxed(gpio->base + gpio->gpio_data->offsets[offset]);
524 gpioreg &= ~MSC313_GPIO_OEN;
526 gpioreg |= MSC313_GPIO_OUT;
528 gpioreg &= ~MSC313_GPIO_OUT;
529 writeb_relaxed(gpioreg, gpio->base + gpio->gpio_data->offsets[offset]);
534 static void msc313_gpio_irq_mask(struct irq_data *d)
536 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
538 irq_chip_mask_parent(d);
539 gpiochip_disable_irq(gc, d->hwirq);
542 static void msc313_gpio_irq_unmask(struct irq_data *d)
544 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
546 gpiochip_enable_irq(gc, d->hwirq);
547 irq_chip_unmask_parent(d);
551 * The interrupt handling happens in the parent interrupt controller,
552 * we don't do anything here.
554 static const struct irq_chip msc313_gpio_irqchip = {
556 .irq_eoi = irq_chip_eoi_parent,
557 .irq_mask = msc313_gpio_irq_mask,
558 .irq_unmask = msc313_gpio_irq_unmask,
559 .irq_set_type = irq_chip_set_type_parent,
560 .irq_set_affinity = irq_chip_set_affinity_parent,
561 .flags = IRQCHIP_IMMUTABLE,
562 GPIOCHIP_IRQ_RESOURCE_HELPERS,
566 * The parent interrupt controller needs the GIC interrupt type set to GIC_SPI
567 * so we need to provide the fwspec. Essentially gpiochip_populate_parent_fwspec_twocell
568 * that puts GIC_SPI into the first cell.
570 static int msc313_gpio_populate_parent_fwspec(struct gpio_chip *gc,
571 union gpio_irq_fwspec *gfwspec,
572 unsigned int parent_hwirq,
573 unsigned int parent_type)
575 struct irq_fwspec *fwspec = &gfwspec->fwspec;
577 fwspec->fwnode = gc->irq.parent_domain->fwnode;
578 fwspec->param_count = 3;
579 fwspec->param[0] = GIC_SPI;
580 fwspec->param[1] = parent_hwirq;
581 fwspec->param[2] = parent_type;
586 static int msc313e_gpio_child_to_parent_hwirq(struct gpio_chip *chip,
588 unsigned int child_type,
589 unsigned int *parent,
590 unsigned int *parent_type)
592 struct msc313_gpio *priv = gpiochip_get_data(chip);
593 unsigned int offset = priv->gpio_data->offsets[child];
596 * only the spi0 pins have interrupts on the parent
597 * on all of the known chips and so far they are all
598 * mapped to the same place
600 if (offset >= OFF_SPI0_CZ && offset <= OFF_SPI0_DO) {
601 *parent_type = child_type;
602 *parent = ((offset - OFF_SPI0_CZ) >> 2) + 28;
609 static int msc313_gpio_probe(struct platform_device *pdev)
611 const struct msc313_gpio_data *match_data;
612 struct msc313_gpio *gpio;
613 struct gpio_chip *gpiochip;
614 struct gpio_irq_chip *gpioirqchip;
615 struct irq_domain *parent_domain;
616 struct device_node *parent_node;
617 struct device *dev = &pdev->dev;
619 match_data = of_device_get_match_data(dev);
623 parent_node = of_irq_find_parent(dev->of_node);
627 parent_domain = irq_find_host(parent_node);
631 gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL);
635 gpio->gpio_data = match_data;
637 gpio->saved = devm_kcalloc(dev, gpio->gpio_data->num, sizeof(*gpio->saved), GFP_KERNEL);
641 gpio->base = devm_platform_ioremap_resource(pdev, 0);
642 if (IS_ERR(gpio->base))
643 return PTR_ERR(gpio->base);
645 platform_set_drvdata(pdev, gpio);
647 gpiochip = devm_kzalloc(dev, sizeof(*gpiochip), GFP_KERNEL);
651 gpiochip->label = DRIVER_NAME;
652 gpiochip->parent = dev;
653 gpiochip->request = gpiochip_generic_request;
654 gpiochip->free = gpiochip_generic_free;
655 gpiochip->direction_input = msc313_gpio_direction_input;
656 gpiochip->direction_output = msc313_gpio_direction_output;
657 gpiochip->get = msc313_gpio_get;
658 gpiochip->set = msc313_gpio_set;
660 gpiochip->ngpio = gpio->gpio_data->num;
661 gpiochip->names = gpio->gpio_data->names;
663 gpioirqchip = &gpiochip->irq;
664 gpio_irq_chip_set_chip(gpioirqchip, &msc313_gpio_irqchip);
665 gpioirqchip->fwnode = of_node_to_fwnode(dev->of_node);
666 gpioirqchip->parent_domain = parent_domain;
667 gpioirqchip->child_to_parent_hwirq = msc313e_gpio_child_to_parent_hwirq;
668 gpioirqchip->populate_parent_alloc_arg = msc313_gpio_populate_parent_fwspec;
669 gpioirqchip->handler = handle_bad_irq;
670 gpioirqchip->default_type = IRQ_TYPE_NONE;
672 return devm_gpiochip_add_data(dev, gpiochip, gpio);
675 static const struct of_device_id msc313_gpio_of_match[] = {
676 #ifdef CONFIG_MACH_INFINITY
678 .compatible = "mstar,msc313-gpio",
679 .data = &msc313_data,
682 .compatible = "sstar,ssd20xd-gpio",
683 .data = &ssd20xd_data,
690 * The GPIO controller loses the state of the registers when the
691 * SoC goes into suspend to memory mode so we need to save some
692 * of the register bits before suspending and put it back when resuming
694 static int __maybe_unused msc313_gpio_suspend(struct device *dev)
696 struct msc313_gpio *gpio = dev_get_drvdata(dev);
699 for (i = 0; i < gpio->gpio_data->num; i++)
700 gpio->saved[i] = readb_relaxed(gpio->base + gpio->gpio_data->offsets[i]) & MSC313_GPIO_BITSTOSAVE;
705 static int __maybe_unused msc313_gpio_resume(struct device *dev)
707 struct msc313_gpio *gpio = dev_get_drvdata(dev);
710 for (i = 0; i < gpio->gpio_data->num; i++)
711 writeb_relaxed(gpio->saved[i], gpio->base + gpio->gpio_data->offsets[i]);
716 static SIMPLE_DEV_PM_OPS(msc313_gpio_ops, msc313_gpio_suspend, msc313_gpio_resume);
718 static struct platform_driver msc313_gpio_driver = {
721 .of_match_table = msc313_gpio_of_match,
722 .pm = &msc313_gpio_ops,
724 .probe = msc313_gpio_probe,
726 builtin_platform_driver(msc313_gpio_driver);