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_device.h>
10 #include <linux/of_irq.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
15 #include <dt-bindings/gpio/msc313-gpio.h>
16 #include <dt-bindings/interrupt-controller/arm-gic.h>
18 #define DRIVER_NAME "gpio-msc313"
20 #define MSC313_GPIO_IN BIT(0)
21 #define MSC313_GPIO_OUT BIT(4)
22 #define MSC313_GPIO_OEN BIT(5)
25 * These bits need to be saved to correctly restore the
26 * gpio state when resuming from suspend to memory.
28 #define MSC313_GPIO_BITSTOSAVE (MSC313_GPIO_OUT | MSC313_GPIO_OEN)
30 /* pad names for fuart, same for all SoCs so far */
31 #define MSC313_PINNAME_FUART_RX "fuart_rx"
32 #define MSC313_PINNAME_FUART_TX "fuart_tx"
33 #define MSC313_PINNAME_FUART_CTS "fuart_cts"
34 #define MSC313_PINNAME_FUART_RTS "fuart_rts"
36 /* pad names for sr, mercury5 is different */
37 #define MSC313_PINNAME_SR_IO2 "sr_io2"
38 #define MSC313_PINNAME_SR_IO3 "sr_io3"
39 #define MSC313_PINNAME_SR_IO4 "sr_io4"
40 #define MSC313_PINNAME_SR_IO5 "sr_io5"
41 #define MSC313_PINNAME_SR_IO6 "sr_io6"
42 #define MSC313_PINNAME_SR_IO7 "sr_io7"
43 #define MSC313_PINNAME_SR_IO8 "sr_io8"
44 #define MSC313_PINNAME_SR_IO9 "sr_io9"
45 #define MSC313_PINNAME_SR_IO10 "sr_io10"
46 #define MSC313_PINNAME_SR_IO11 "sr_io11"
47 #define MSC313_PINNAME_SR_IO12 "sr_io12"
48 #define MSC313_PINNAME_SR_IO13 "sr_io13"
49 #define MSC313_PINNAME_SR_IO14 "sr_io14"
50 #define MSC313_PINNAME_SR_IO15 "sr_io15"
51 #define MSC313_PINNAME_SR_IO16 "sr_io16"
52 #define MSC313_PINNAME_SR_IO17 "sr_io17"
54 /* pad names for sd, same for all SoCs so far */
55 #define MSC313_PINNAME_SD_CLK "sd_clk"
56 #define MSC313_PINNAME_SD_CMD "sd_cmd"
57 #define MSC313_PINNAME_SD_D0 "sd_d0"
58 #define MSC313_PINNAME_SD_D1 "sd_d1"
59 #define MSC313_PINNAME_SD_D2 "sd_d2"
60 #define MSC313_PINNAME_SD_D3 "sd_d3"
62 /* pad names for i2c1, same for all SoCs so for */
63 #define MSC313_PINNAME_I2C1_SCL "i2c1_scl"
64 #define MSC313_PINNAME_I2C1_SCA "i2c1_sda"
66 /* pad names for spi0, same for all SoCs so far */
67 #define MSC313_PINNAME_SPI0_CZ "spi0_cz"
68 #define MSC313_PINNAME_SPI0_CK "spi0_ck"
69 #define MSC313_PINNAME_SPI0_DI "spi0_di"
70 #define MSC313_PINNAME_SPI0_DO "spi0_do"
73 MSC313_PINNAME_FUART_RX, \
74 MSC313_PINNAME_FUART_TX, \
75 MSC313_PINNAME_FUART_CTS, \
76 MSC313_PINNAME_FUART_RTS
78 #define OFF_FUART_RX 0x50
79 #define OFF_FUART_TX 0x54
80 #define OFF_FUART_CTS 0x58
81 #define OFF_FUART_RTS 0x5c
83 #define FUART_OFFSETS \
90 MSC313_PINNAME_SR_IO2, \
91 MSC313_PINNAME_SR_IO3, \
92 MSC313_PINNAME_SR_IO4, \
93 MSC313_PINNAME_SR_IO5, \
94 MSC313_PINNAME_SR_IO6, \
95 MSC313_PINNAME_SR_IO7, \
96 MSC313_PINNAME_SR_IO8, \
97 MSC313_PINNAME_SR_IO9, \
98 MSC313_PINNAME_SR_IO10, \
99 MSC313_PINNAME_SR_IO11, \
100 MSC313_PINNAME_SR_IO12, \
101 MSC313_PINNAME_SR_IO13, \
102 MSC313_PINNAME_SR_IO14, \
103 MSC313_PINNAME_SR_IO15, \
104 MSC313_PINNAME_SR_IO16, \
105 MSC313_PINNAME_SR_IO17
107 #define OFF_SR_IO2 0x88
108 #define OFF_SR_IO3 0x8c
109 #define OFF_SR_IO4 0x90
110 #define OFF_SR_IO5 0x94
111 #define OFF_SR_IO6 0x98
112 #define OFF_SR_IO7 0x9c
113 #define OFF_SR_IO8 0xa0
114 #define OFF_SR_IO9 0xa4
115 #define OFF_SR_IO10 0xa8
116 #define OFF_SR_IO11 0xac
117 #define OFF_SR_IO12 0xb0
118 #define OFF_SR_IO13 0xb4
119 #define OFF_SR_IO14 0xb8
120 #define OFF_SR_IO15 0xbc
121 #define OFF_SR_IO16 0xc0
122 #define OFF_SR_IO17 0xc4
143 MSC313_PINNAME_SD_CLK, \
144 MSC313_PINNAME_SD_CMD, \
145 MSC313_PINNAME_SD_D0, \
146 MSC313_PINNAME_SD_D1, \
147 MSC313_PINNAME_SD_D2, \
150 #define OFF_SD_CLK 0x140
151 #define OFF_SD_CMD 0x144
152 #define OFF_SD_D0 0x148
153 #define OFF_SD_D1 0x14c
154 #define OFF_SD_D2 0x150
155 #define OFF_SD_D3 0x154
166 MSC313_PINNAME_I2C1_SCL, \
167 MSC313_PINNAME_I2C1_SCA
169 #define OFF_I2C1_SCL 0x188
170 #define OFF_I2C1_SCA 0x18c
172 #define I2C1_OFFSETS \
177 MSC313_PINNAME_SPI0_CZ, \
178 MSC313_PINNAME_SPI0_CK, \
179 MSC313_PINNAME_SPI0_DI, \
180 MSC313_PINNAME_SPI0_DO
182 #define OFF_SPI0_CZ 0x1c0
183 #define OFF_SPI0_CK 0x1c4
184 #define OFF_SPI0_DI 0x1c8
185 #define OFF_SPI0_DO 0x1cc
187 #define SPI0_OFFSETS \
193 struct msc313_gpio_data {
194 const char * const *names;
195 const unsigned int *offsets;
196 const unsigned int num;
199 #define MSC313_GPIO_CHIPDATA(_chip) \
200 static const struct msc313_gpio_data _chip##_data = { \
201 .names = _chip##_names, \
202 .offsets = _chip##_offsets, \
203 .num = ARRAY_SIZE(_chip##_offsets), \
206 #ifdef CONFIG_MACH_INFINITY
207 static const char * const msc313_names[] = {
215 static const unsigned int msc313_offsets[] = {
223 MSC313_GPIO_CHIPDATA(msc313);
228 const struct msc313_gpio_data *gpio_data;
232 static void msc313_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
234 struct msc313_gpio *gpio = gpiochip_get_data(chip);
235 u8 gpioreg = readb_relaxed(gpio->base + gpio->gpio_data->offsets[offset]);
238 gpioreg |= MSC313_GPIO_OUT;
240 gpioreg &= ~MSC313_GPIO_OUT;
242 writeb_relaxed(gpioreg, gpio->base + gpio->gpio_data->offsets[offset]);
245 static int msc313_gpio_get(struct gpio_chip *chip, unsigned int offset)
247 struct msc313_gpio *gpio = gpiochip_get_data(chip);
249 return readb_relaxed(gpio->base + gpio->gpio_data->offsets[offset]) & MSC313_GPIO_IN;
252 static int msc313_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
254 struct msc313_gpio *gpio = gpiochip_get_data(chip);
255 u8 gpioreg = readb_relaxed(gpio->base + gpio->gpio_data->offsets[offset]);
257 gpioreg |= MSC313_GPIO_OEN;
258 writeb_relaxed(gpioreg, gpio->base + gpio->gpio_data->offsets[offset]);
263 static int msc313_gpio_direction_output(struct gpio_chip *chip, unsigned int offset, int value)
265 struct msc313_gpio *gpio = gpiochip_get_data(chip);
266 u8 gpioreg = readb_relaxed(gpio->base + gpio->gpio_data->offsets[offset]);
268 gpioreg &= ~MSC313_GPIO_OEN;
270 gpioreg |= MSC313_GPIO_OUT;
272 gpioreg &= ~MSC313_GPIO_OUT;
273 writeb_relaxed(gpioreg, gpio->base + gpio->gpio_data->offsets[offset]);
279 * The interrupt handling happens in the parent interrupt controller,
280 * we don't do anything here.
282 static struct irq_chip msc313_gpio_irqchip = {
284 .irq_eoi = irq_chip_eoi_parent,
285 .irq_mask = irq_chip_mask_parent,
286 .irq_unmask = irq_chip_unmask_parent,
287 .irq_set_type = irq_chip_set_type_parent,
288 .irq_set_affinity = irq_chip_set_affinity_parent,
292 * The parent interrupt controller needs the GIC interrupt type set to GIC_SPI
293 * so we need to provide the fwspec. Essentially gpiochip_populate_parent_fwspec_twocell
294 * that puts GIC_SPI into the first cell.
296 static void *msc313_gpio_populate_parent_fwspec(struct gpio_chip *gc,
297 unsigned int parent_hwirq,
298 unsigned int parent_type)
300 struct irq_fwspec *fwspec;
302 fwspec = kmalloc(sizeof(*fwspec), GFP_KERNEL);
306 fwspec->fwnode = gc->irq.parent_domain->fwnode;
307 fwspec->param_count = 3;
308 fwspec->param[0] = GIC_SPI;
309 fwspec->param[1] = parent_hwirq;
310 fwspec->param[2] = parent_type;
315 static int msc313e_gpio_child_to_parent_hwirq(struct gpio_chip *chip,
317 unsigned int child_type,
318 unsigned int *parent,
319 unsigned int *parent_type)
321 struct msc313_gpio *priv = gpiochip_get_data(chip);
322 unsigned int offset = priv->gpio_data->offsets[child];
325 * only the spi0 pins have interrupts on the parent
326 * on all of the known chips and so far they are all
327 * mapped to the same place
329 if (offset >= OFF_SPI0_CZ && offset <= OFF_SPI0_DO) {
330 *parent_type = child_type;
331 *parent = ((offset - OFF_SPI0_CZ) >> 2) + 28;
338 static int msc313_gpio_probe(struct platform_device *pdev)
340 const struct msc313_gpio_data *match_data;
341 struct msc313_gpio *gpio;
342 struct gpio_chip *gpiochip;
343 struct gpio_irq_chip *gpioirqchip;
344 struct irq_domain *parent_domain;
345 struct device_node *parent_node;
346 struct device *dev = &pdev->dev;
349 match_data = of_device_get_match_data(dev);
353 parent_node = of_irq_find_parent(dev->of_node);
357 parent_domain = irq_find_host(parent_node);
361 gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL);
365 gpio->gpio_data = match_data;
367 gpio->saved = devm_kcalloc(dev, gpio->gpio_data->num, sizeof(*gpio->saved), GFP_KERNEL);
371 gpio->base = devm_platform_ioremap_resource(pdev, 0);
372 if (IS_ERR(gpio->base))
373 return PTR_ERR(gpio->base);
375 platform_set_drvdata(pdev, gpio);
377 gpiochip = devm_kzalloc(dev, sizeof(*gpiochip), GFP_KERNEL);
381 gpiochip->label = DRIVER_NAME;
382 gpiochip->parent = dev;
383 gpiochip->request = gpiochip_generic_request;
384 gpiochip->free = gpiochip_generic_free;
385 gpiochip->direction_input = msc313_gpio_direction_input;
386 gpiochip->direction_output = msc313_gpio_direction_output;
387 gpiochip->get = msc313_gpio_get;
388 gpiochip->set = msc313_gpio_set;
390 gpiochip->ngpio = gpio->gpio_data->num;
391 gpiochip->names = gpio->gpio_data->names;
393 gpioirqchip = &gpiochip->irq;
394 gpioirqchip->chip = &msc313_gpio_irqchip;
395 gpioirqchip->fwnode = of_node_to_fwnode(dev->of_node);
396 gpioirqchip->parent_domain = parent_domain;
397 gpioirqchip->child_to_parent_hwirq = msc313e_gpio_child_to_parent_hwirq;
398 gpioirqchip->populate_parent_alloc_arg = msc313_gpio_populate_parent_fwspec;
399 gpioirqchip->handler = handle_bad_irq;
400 gpioirqchip->default_type = IRQ_TYPE_NONE;
402 ret = devm_gpiochip_add_data(dev, gpiochip, gpio);
406 static int msc313_gpio_remove(struct platform_device *pdev)
411 static const struct of_device_id msc313_gpio_of_match[] = {
412 #ifdef CONFIG_MACH_INFINITY
414 .compatible = "mstar,msc313-gpio",
415 .data = &msc313_data,
422 * The GPIO controller loses the state of the registers when the
423 * SoC goes into suspend to memory mode so we need to save some
424 * of the register bits before suspending and put it back when resuming
426 static int __maybe_unused msc313_gpio_suspend(struct device *dev)
428 struct msc313_gpio *gpio = dev_get_drvdata(dev);
431 for (i = 0; i < gpio->gpio_data->num; i++)
432 gpio->saved[i] = readb_relaxed(gpio->base + gpio->gpio_data->offsets[i]) & MSC313_GPIO_BITSTOSAVE;
437 static int __maybe_unused msc313_gpio_resume(struct device *dev)
439 struct msc313_gpio *gpio = dev_get_drvdata(dev);
442 for (i = 0; i < gpio->gpio_data->num; i++)
443 writeb_relaxed(gpio->saved[i], gpio->base + gpio->gpio_data->offsets[i]);
448 static SIMPLE_DEV_PM_OPS(msc313_gpio_ops, msc313_gpio_suspend, msc313_gpio_resume);
450 static struct platform_driver msc313_gpio_driver = {
453 .of_match_table = msc313_gpio_of_match,
454 .pm = &msc313_gpio_ops,
456 .probe = msc313_gpio_probe,
457 .remove = msc313_gpio_remove,
460 builtin_platform_driver(msc313_gpio_driver);