1commit 7f2268e6c9bf0a679afb08925facc27c8ecbcf57 2Author: zhaoxc0502 <zhaoxc0502@thundersoft.com> 3Date: Thu Jun 16 17:19:42 2022 +0800 4 5 linux_drivers_gpio 6 7 Change-Id: I85874db4979e1d5dcfde13a3ecaeca8507d0ea43 8 9diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig 10index d1300fc00..e4ecf9d02 100644 11--- a/drivers/gpio/Kconfig 12+++ b/drivers/gpio/Kconfig 13@@ -427,6 +427,12 @@ config GPIO_MXC 14 select GPIO_GENERIC 15 select GENERIC_IRQ_CHIP 16 17+config GPIO_SCU 18+ def_bool y 19+ depends on IMX_SCU 20+ help 21+ Say Y here to enable the imx8 gpio over SCFW MISC API 22+ 23 config GPIO_MXS 24 bool "Freescale MXS GPIO support" if COMPILE_TEST 25 depends on ARCH_MXS || COMPILE_TEST 26@@ -434,6 +440,13 @@ config GPIO_MXS 27 select GPIO_GENERIC 28 select GENERIC_IRQ_CHIP 29 30+config GPIO_MXC_PAD_WAKEUP 31+ def_bool y 32+ depends on IMX_SCU 33+ select GPIO_MXC 34+ help 35+ Say Y here to enable the imx8 gpio pad wakeup 36+ 37 config GPIO_OCTEON 38 tristate "Cavium OCTEON GPIO" 39 depends on CAVIUM_OCTEON_SOC 40@@ -628,7 +641,7 @@ config GPIO_UNIPHIER 41 42 config GPIO_VF610 43 def_bool y 44- depends on ARCH_MXC && SOC_VF610 45+ depends on ARCH_MXC 46 select GPIOLIB_IRQCHIP 47 help 48 Say yes here to support Vybrid vf610 GPIOs. 49diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile 50index 09dada80a..a8dd32998 100644 51--- a/drivers/gpio/Makefile 52+++ b/drivers/gpio/Makefile 53@@ -129,6 +129,7 @@ obj-$(CONFIG_ARCH_SA1100) += gpio-sa1100.o 54 obj-$(CONFIG_GPIO_SAMA5D2_PIOBU) += gpio-sama5d2-piobu.o 55 obj-$(CONFIG_GPIO_SCH311X) += gpio-sch311x.o 56 obj-$(CONFIG_GPIO_SCH) += gpio-sch.o 57+obj-$(CONFIG_GPIO_SCU) += gpio-scu.o 58 obj-$(CONFIG_GPIO_SIFIVE) += gpio-sifive.o 59 obj-$(CONFIG_GPIO_SIOX) += gpio-siox.o 60 obj-$(CONFIG_GPIO_SL28CPLD) += gpio-sl28cpld.o 61diff --git a/drivers/gpio/gpio-max732x.c b/drivers/gpio/gpio-max732x.c 62index 238cbe926..4af7ea075 100644 63--- a/drivers/gpio/gpio-max732x.c 64+++ b/drivers/gpio/gpio-max732x.c 65@@ -19,6 +19,7 @@ 66 #include <linux/i2c.h> 67 #include <linux/platform_data/max732x.h> 68 #include <linux/of.h> 69+#include <linux/reset.h> 70 71 72 /* 73@@ -76,6 +77,12 @@ 74 75 #define INT_CAPS(x) (((uint64_t)(x)) << 32) 76 77+enum { 78+ OUTPUT_MASK, 79+ OUTPUT_VAL, 80+ OUTPUT_NUM, 81+}; 82+ 83 enum { 84 MAX7319, 85 MAX7320, 86@@ -622,6 +629,8 @@ static int max732x_probe(struct i2c_client *client, 87 struct i2c_client *c; 88 uint16_t addr_a, addr_b; 89 int ret, nr_port; 90+ u16 out_set[OUTPUT_NUM]; 91+ unsigned long mask, val; 92 93 pdata = dev_get_platdata(&client->dev); 94 node = client->dev.of_node; 95@@ -639,6 +648,10 @@ static int max732x_probe(struct i2c_client *client, 96 return -ENOMEM; 97 chip->client = client; 98 99+ ret = device_reset(&client->dev); 100+ if (ret == -EPROBE_DEFER) 101+ return ret; 102+ 103 nr_port = max732x_setup_gpio(chip, id, pdata->gpio_base); 104 chip->gpio_chip.parent = &client->dev; 105 106@@ -711,6 +724,15 @@ static int max732x_probe(struct i2c_client *client, 107 } 108 109 i2c_set_clientdata(client, chip); 110+ 111+ /* set the output IO default voltage */ 112+ if (!of_property_read_u16_array(node, "out-default", out_set, 113+ ARRAY_SIZE(out_set))) { 114+ mask = out_set[OUTPUT_MASK] & chip->dir_output; 115+ val = out_set[OUTPUT_VAL]; 116+ max732x_gpio_set_multiple(&chip->gpio_chip, &mask, &val); 117+ } 118+ 119 return 0; 120 } 121 122diff --git a/drivers/gpio/gpio-mpc8xxx.c b/drivers/gpio/gpio-mpc8xxx.c 123index d60d55207..4982e90d8 100644 124--- a/drivers/gpio/gpio-mpc8xxx.c 125+++ b/drivers/gpio/gpio-mpc8xxx.c 126@@ -426,9 +426,20 @@ static int mpc8xxx_remove(struct platform_device *pdev) 127 return 0; 128 } 129 130+static void mpc8xxx_shutdown(struct platform_device *pdev) 131+{ 132+ struct mpc8xxx_gpio_chip *mpc8xxx_gc = platform_get_drvdata(pdev); 133+ 134+ if (mpc8xxx_gc->irq) { 135+ irq_set_chained_handler_and_data(mpc8xxx_gc->irqn, NULL, NULL); 136+ irq_domain_remove(mpc8xxx_gc->irq); 137+ } 138+} 139+ 140 static struct platform_driver mpc8xxx_plat_driver = { 141 .probe = mpc8xxx_probe, 142 .remove = mpc8xxx_remove, 143+ .shutdown = mpc8xxx_shutdown, 144 .driver = { 145 .name = "gpio-mpc8xxx", 146 .of_match_table = mpc8xxx_gpio_ids, 147diff --git a/drivers/gpio/gpio-mxc.c b/drivers/gpio/gpio-mxc.c 148index ba6ed2a41..cd167ce3b 100644 149--- a/drivers/gpio/gpio-mxc.c 150+++ b/drivers/gpio/gpio-mxc.c 151@@ -17,12 +17,21 @@ 152 #include <linux/irqchip/chained_irq.h> 153 #include <linux/module.h> 154 #include <linux/platform_device.h> 155+#include <linux/pm_runtime.h> 156 #include <linux/slab.h> 157 #include <linux/syscore_ops.h> 158 #include <linux/gpio/driver.h> 159 #include <linux/of.h> 160 #include <linux/of_device.h> 161 #include <linux/bug.h> 162+#ifdef CONFIG_GPIO_MXC_PAD_WAKEUP 163+#include <linux/firmware/imx/sci.h> 164+ 165+#define IMX_SC_PAD_FUNC_GET_WAKEUP 9 166+#define IMX_SC_PAD_FUNC_SET_WAKEUP 4 167+#define IMX_SC_PAD_WAKEUP_OFF 0 168+#define IMX_SC_IRQ_PAD (1 << 1) 169+#endif 170 171 enum mxc_gpio_hwtype { 172 IMX1_GPIO, /* runs on i.mx1 */ 173@@ -31,6 +40,33 @@ enum mxc_gpio_hwtype { 174 IMX35_GPIO, /* runs on all other i.mx */ 175 }; 176 177+#ifdef CONFIG_GPIO_MXC_PAD_WAKEUP 178+struct mxc_gpio_pad_wakeup { 179+ u32 pin_id; 180+ u32 type; 181+ u32 line; 182+}; 183+ 184+struct imx_sc_msg_gpio_get_pad_wakeup { 185+ struct imx_sc_rpc_msg hdr; 186+ union { 187+ struct req_pad { 188+ u16 pad; 189+ } __packed req; 190+ struct resp_wakeup { 191+ u8 wakeup; 192+ } resp; 193+ } data; 194+} __packed __aligned(4); 195+ 196+struct imx_sc_msg_gpio_set_pad_wakeup { 197+ struct imx_sc_rpc_msg hdr; 198+ u16 pad; 199+ u8 wakeup; 200+} __packed __aligned(4); 201+ 202+#endif 203+ 204 /* device type dependent stuff */ 205 struct mxc_gpio_hwdata { 206 unsigned dr_reg; 207@@ -68,8 +104,17 @@ struct mxc_gpio_port { 208 u32 both_edges; 209 struct mxc_gpio_reg_saved gpio_saved_reg; 210 bool power_off; 211+ bool gpio_ranges; 212+#ifdef CONFIG_GPIO_MXC_PAD_WAKEUP 213+ u32 pad_wakeup_num; 214+ struct mxc_gpio_pad_wakeup pad_wakeup[32]; 215+#endif 216 }; 217 218+#ifdef CONFIG_GPIO_MXC_PAD_WAKEUP 219+static struct imx_sc_ipc *gpio_ipc_handle; 220+#endif 221+ 222 static struct mxc_gpio_hwdata imx1_imx21_gpio_hwdata = { 223 .dr_reg = 0x1c, 224 .gdir_reg = 0x00, 225@@ -312,6 +357,85 @@ static void mx2_gpio_irq_handler(struct irq_desc *desc) 226 chained_irq_exit(chip, desc); 227 } 228 229+#ifdef CONFIG_GPIO_MXC_PAD_WAKEUP 230+static int mxc_gpio_get_pad_wakeup(struct mxc_gpio_port *port) 231+{ 232+ struct imx_sc_msg_gpio_get_pad_wakeup msg; 233+ struct imx_sc_rpc_msg *hdr = &msg.hdr; 234+ u8 wakeup_type; 235+ int ret; 236+ int i; 237+ 238+ hdr->ver = IMX_SC_RPC_VERSION; 239+ hdr->svc = IMX_SC_RPC_SVC_PAD; 240+ hdr->func = IMX_SC_PAD_FUNC_GET_WAKEUP; 241+ hdr->size = 2; 242+ 243+ for (i = 0; i < port->pad_wakeup_num; i++) { 244+ /* get original pad type */ 245+ wakeup_type = port->pad_wakeup[i].type; 246+ msg.data.req.pad = port->pad_wakeup[i].pin_id; 247+ ret = imx_scu_call_rpc(gpio_ipc_handle, &msg, true); 248+ if (ret) { 249+ dev_err(port->gc.parent, "get pad wakeup failed, ret %d\n", ret); 250+ return ret; 251+ } 252+ wakeup_type = msg.data.resp.wakeup; 253+ /* return wakeup gpio pin's line */ 254+ if (wakeup_type != port->pad_wakeup[i].type) 255+ return port->pad_wakeup[i].line; 256+ } 257+ 258+ return -EINVAL; 259+} 260+ 261+static void mxc_gpio_set_pad_wakeup(struct mxc_gpio_port *port, bool enable) 262+{ 263+ struct imx_sc_msg_gpio_set_pad_wakeup msg; 264+ struct imx_sc_rpc_msg *hdr = &msg.hdr; 265+ int ret; 266+ int i; 267+ 268+ hdr->ver = IMX_SC_RPC_VERSION; 269+ hdr->svc = IMX_SC_RPC_SVC_PAD; 270+ hdr->func = IMX_SC_PAD_FUNC_SET_WAKEUP; 271+ hdr->size = 2; 272+ 273+ for (i = 0; i < port->pad_wakeup_num; i++) { 274+ msg.pad = port->pad_wakeup[i].pin_id; 275+ msg.wakeup = enable ? port->pad_wakeup[i].type : IMX_SC_PAD_WAKEUP_OFF; 276+ ret = imx_scu_call_rpc(gpio_ipc_handle, &msg, true); 277+ if (ret) { 278+ dev_err(port->gc.parent, "set pad wakeup failed, ret %d\n", ret); 279+ return; 280+ } 281+ } 282+} 283+ 284+static void mxc_gpio_handle_pad_wakeup(struct mxc_gpio_port *port, int line) 285+{ 286+ struct irq_desc *desc = irq_to_desc(port->irq); 287+ struct irq_chip *chip = irq_desc_get_chip(desc); 288+ u32 irq_stat; 289+ 290+ /* skip invalid line */ 291+ if (line > 31) { 292+ dev_err(port->gc.parent, "invalid wakeup line %d\n", line); 293+ return; 294+ } 295+ 296+ dev_info(port->gc.parent, "wakeup by pad, line %d\n", line); 297+ 298+ chained_irq_enter(chip, desc); 299+ 300+ irq_stat = (1 << line); 301+ 302+ mxc_gpio_irq_handler(port, irq_stat); 303+ 304+ chained_irq_exit(chip, desc); 305+} 306+#endif 307+ 308 /* 309 * Set interrupt number "irq" in the GPIO as a wake-up source. 310 * While system is running, all registered GPIO interrupts need to have 311@@ -343,7 +467,32 @@ static int gpio_set_wake_irq(struct irq_data *d, u32 enable) 312 return ret; 313 } 314 315-static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base) 316+static int mxc_gpio_irq_reqres(struct irq_data *d) 317+{ 318+ struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 319+ struct mxc_gpio_port *port = gc->private; 320+ 321+ if (gpiochip_lock_as_irq(&port->gc, d->hwirq)) { 322+ dev_err(port->gc.parent, 323+ "unable to lock HW IRQ %lu for IRQ\n", 324+ d->hwirq); 325+ return -EINVAL; 326+ } 327+ 328+ return irq_chip_pm_get(d); 329+} 330+ 331+static void mxc_gpio_irq_relres(struct irq_data *d) 332+{ 333+ struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d); 334+ struct mxc_gpio_port *port = gc->private; 335+ 336+ gpiochip_unlock_as_irq(&port->gc, d->hwirq); 337+ irq_chip_pm_put(d); 338+} 339+ 340+static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base, 341+ struct device *dev) 342 { 343 struct irq_chip_generic *gc; 344 struct irq_chip_type *ct; 345@@ -356,11 +505,14 @@ static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base) 346 gc->private = port; 347 348 ct = gc->chip_types; 349+ ct->chip.parent_device = dev; 350 ct->chip.irq_ack = irq_gc_ack_set_bit; 351 ct->chip.irq_mask = irq_gc_mask_clr_bit; 352 ct->chip.irq_unmask = irq_gc_mask_set_bit; 353 ct->chip.irq_set_type = gpio_set_irq_type; 354 ct->chip.irq_set_wake = gpio_set_wake_irq; 355+ ct->chip.irq_request_resources = mxc_gpio_irq_reqres; 356+ ct->chip.irq_release_resources = mxc_gpio_irq_relres, 357 ct->chip.flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND; 358 ct->regs.ack = GPIO_ISR; 359 ct->regs.mask = GPIO_IMR; 360@@ -409,6 +561,30 @@ static int mxc_gpio_to_irq(struct gpio_chip *gc, unsigned offset) 361 return irq_find_mapping(port->domain, offset); 362 } 363 364+static int mxc_gpio_request(struct gpio_chip *chip, unsigned offset) 365+{ 366+ struct mxc_gpio_port *port = gpiochip_get_data(chip); 367+ int ret; 368+ 369+ if (port->gpio_ranges) { 370+ ret = gpiochip_generic_request(chip, offset); 371+ if (ret) 372+ return ret; 373+ } 374+ 375+ ret = pm_runtime_get_sync(chip->parent); 376+ return ret < 0 ? ret : 0; 377+} 378+ 379+static void mxc_gpio_free(struct gpio_chip *chip, unsigned offset) 380+{ 381+ struct mxc_gpio_port *port = gpiochip_get_data(chip); 382+ 383+ if (port->gpio_ranges) 384+ gpiochip_generic_free(chip, offset); 385+ pm_runtime_put(chip->parent); 386+} 387+ 388 static int mxc_gpio_probe(struct platform_device *pdev) 389 { 390 struct device_node *np = pdev->dev.of_node; 391@@ -416,6 +592,9 @@ static int mxc_gpio_probe(struct platform_device *pdev) 392 int irq_count; 393 int irq_base; 394 int err; 395+#ifdef CONFIG_GPIO_MXC_PAD_WAKEUP 396+ int i; 397+#endif 398 399 mxc_gpio_get_hw(pdev); 400 401@@ -434,7 +613,7 @@ static int mxc_gpio_probe(struct platform_device *pdev) 402 return irq_count; 403 404 if (irq_count > 1) { 405- port->irq_high = platform_get_irq(pdev, 1); 406+ port->irq_high = platform_get_irq_optional(pdev, 1); 407 if (port->irq_high < 0) 408 port->irq_high = 0; 409 } 410@@ -454,9 +633,44 @@ static int mxc_gpio_probe(struct platform_device *pdev) 411 return err; 412 } 413 414+#ifdef CONFIG_GPIO_MXC_PAD_WAKEUP 415+ /* 416+ * parse pad wakeup info from dtb, each pad has to provide 417+ * <pin_id, type, line>, these info should be put in each 418+ * gpio node and with a "pad-wakeup-num" to indicate the 419+ * total lines are with pad wakeup enabled. 420+ */ 421+ if (!of_property_read_u32(np, "pad-wakeup-num", &port->pad_wakeup_num)) { 422+ if (port->pad_wakeup_num != 0) { 423+ if (!gpio_ipc_handle) { 424+ err = imx_scu_get_handle(&gpio_ipc_handle); 425+ if (err) 426+ return err; 427+ } 428+ for (i = 0; i < port->pad_wakeup_num; i++) { 429+ of_property_read_u32_index(np, "pad-wakeup", 430+ i * 3 + 0, &port->pad_wakeup[i].pin_id); 431+ of_property_read_u32_index(np, "pad-wakeup", 432+ i * 3 + 1, &port->pad_wakeup[i].type); 433+ of_property_read_u32_index(np, "pad-wakeup", 434+ i * 3 + 2, &port->pad_wakeup[i].line); 435+ } 436+ err = imx_scu_irq_group_enable(IMX_SC_IRQ_GROUP_WAKE, IMX_SC_IRQ_PAD, true); 437+ if (err) 438+ dev_warn(&pdev->dev, "Enable irq failed, GPIO pad wakeup NOT supported\n"); 439+ } 440+ } 441+#endif 442+ 443 if (of_device_is_compatible(np, "fsl,imx7d-gpio")) 444 port->power_off = true; 445 446+ pm_runtime_set_active(&pdev->dev); 447+ pm_runtime_enable(&pdev->dev); 448+ err = pm_runtime_get_sync(&pdev->dev); 449+ if (err < 0) 450+ goto out_pm_dis; 451+ 452 /* disable the interrupt and clear the status */ 453 writel(0, port->base + GPIO_IMR); 454 writel(~0, port->base + GPIO_ISR); 455@@ -487,8 +701,14 @@ static int mxc_gpio_probe(struct platform_device *pdev) 456 if (err) 457 goto out_bgio; 458 459- port->gc.request = gpiochip_generic_request; 460- port->gc.free = gpiochip_generic_free; 461+ if (of_property_read_bool(np, "gpio_ranges")) 462+ port->gpio_ranges = true; 463+ else 464+ port->gpio_ranges = false; 465+ 466+ port->gc.request = mxc_gpio_request; 467+ port->gc.free = mxc_gpio_free; 468+ port->gc.parent = &pdev->dev; 469 port->gc.to_irq = mxc_gpio_to_irq; 470 port->gc.base = (pdev->id < 0) ? of_alias_get_id(np, "gpio") * 32 : 471 pdev->id * 32; 472@@ -511,16 +731,20 @@ static int mxc_gpio_probe(struct platform_device *pdev) 473 } 474 475 /* gpio-mxc can be a generic irq chip */ 476- err = mxc_gpio_init_gc(port, irq_base); 477+ err = mxc_gpio_init_gc(port, irq_base, &pdev->dev); 478 if (err < 0) 479 goto out_irqdomain_remove; 480 481 list_add_tail(&port->node, &mxc_gpio_ports); 482 483 platform_set_drvdata(pdev, port); 484+ pm_runtime_put(&pdev->dev); 485 486 return 0; 487 488+out_pm_dis: 489+ pm_runtime_disable(&pdev->dev); 490+ clk_disable_unprepare(port->clk); 491 out_irqdomain_remove: 492 irq_domain_remove(port->domain); 493 out_bgio: 494@@ -555,12 +779,73 @@ static void mxc_gpio_restore_regs(struct mxc_gpio_port *port) 495 writel(port->gpio_saved_reg.dr, port->base + GPIO_DR); 496 } 497 498+static int __maybe_unused mxc_gpio_runtime_suspend(struct device *dev) 499+{ 500+ struct platform_device *pdev = to_platform_device(dev); 501+ struct mxc_gpio_port *port = platform_get_drvdata(pdev); 502+ 503+ mxc_gpio_save_regs(port); 504+ clk_disable_unprepare(port->clk); 505+ 506+ return 0; 507+} 508+ 509+static int __maybe_unused mxc_gpio_runtime_resume(struct device *dev) 510+{ 511+ struct platform_device *pdev = to_platform_device(dev); 512+ struct mxc_gpio_port *port = platform_get_drvdata(pdev); 513+ int ret; 514+ 515+ ret = clk_prepare_enable(port->clk); 516+ if (ret) 517+ return ret; 518+ 519+ mxc_gpio_restore_regs(port); 520+ 521+ return 0; 522+} 523+ 524+static int __maybe_unused mxc_gpio_noirq_suspend(struct device *dev) 525+{ 526+#ifdef CONFIG_GPIO_MXC_PAD_WAKEUP 527+ struct platform_device *pdev = to_platform_device(dev); 528+ struct mxc_gpio_port *port = platform_get_drvdata(pdev); 529+ 530+ mxc_gpio_set_pad_wakeup(port, true); 531+#endif 532+ return 0; 533+} 534+ 535+static int __maybe_unused mxc_gpio_noirq_resume(struct device *dev) 536+{ 537+#ifdef CONFIG_GPIO_MXC_PAD_WAKEUP 538+ struct platform_device *pdev = to_platform_device(dev); 539+ struct mxc_gpio_port *port = platform_get_drvdata(pdev); 540+ int wakeup_line = mxc_gpio_get_pad_wakeup(port); 541+ 542+ mxc_gpio_set_pad_wakeup(port, false); 543+ 544+ if (wakeup_line >= 0) 545+ mxc_gpio_handle_pad_wakeup(port, wakeup_line); 546+#endif 547+ return 0; 548+} 549+ 550+static const struct dev_pm_ops mxc_gpio_dev_pm_ops = { 551+ SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mxc_gpio_noirq_suspend, mxc_gpio_noirq_resume) 552+ SET_RUNTIME_PM_OPS(mxc_gpio_runtime_suspend, mxc_gpio_runtime_resume, NULL) 553+}; 554+ 555 static int mxc_gpio_syscore_suspend(void) 556 { 557 struct mxc_gpio_port *port; 558+ int ret; 559 560 /* walk through all ports */ 561 list_for_each_entry(port, &mxc_gpio_ports, node) { 562+ ret = clk_prepare_enable(port->clk); 563+ if (ret) 564+ return ret; 565 mxc_gpio_save_regs(port); 566 clk_disable_unprepare(port->clk); 567 } 568@@ -581,6 +866,7 @@ static void mxc_gpio_syscore_resume(void) 569 return; 570 } 571 mxc_gpio_restore_regs(port); 572+ clk_disable_unprepare(port->clk); 573 } 574 } 575 576@@ -594,6 +880,7 @@ static struct platform_driver mxc_gpio_driver = { 577 .name = "gpio-mxc", 578 .of_match_table = mxc_gpio_dt_ids, 579 .suppress_bind_attrs = true, 580+ .pm = &mxc_gpio_dev_pm_ops, 581 }, 582 .probe = mxc_gpio_probe, 583 .id_table = mxc_gpio_devtype, 584diff --git a/drivers/gpio/gpio-pca953x.c b/drivers/gpio/gpio-pca953x.c 585index a78167b2c..33d3a0bfe 100644 586--- a/drivers/gpio/gpio-pca953x.c 587+++ b/drivers/gpio/gpio-pca953x.c 588@@ -20,6 +20,7 @@ 589 #include <linux/platform_data/pca953x.h> 590 #include <linux/regmap.h> 591 #include <linux/regulator/consumer.h> 592+#include <linux/reset.h> 593 #include <linux/slab.h> 594 595 #include <asm/unaligned.h> 596@@ -981,14 +982,19 @@ static int pca953x_probe(struct i2c_client *client, 597 598 chip->client = client; 599 600- reg = devm_regulator_get(&client->dev, "vcc"); 601- if (IS_ERR(reg)) 602- return dev_err_probe(&client->dev, PTR_ERR(reg), "reg get err\n"); 603+ reg = devm_regulator_get_optional(&client->dev, "vcc"); 604+ if (IS_ERR(reg)) { 605+ if (PTR_ERR(reg) != -ENODEV) 606+ return dev_err_probe(&client->dev, PTR_ERR(reg), "reg get err\n"); 607+ reg = NULL; 608+ } 609 610- ret = regulator_enable(reg); 611- if (ret) { 612- dev_err(&client->dev, "reg en err: %d\n", ret); 613- return ret; 614+ if (reg) { 615+ ret = regulator_enable(reg); 616+ if (ret) { 617+ dev_err(&client->dev, "reg en err: %d\n", ret); 618+ return ret; 619+ } 620 } 621 chip->regulator = reg; 622 623@@ -1046,6 +1052,10 @@ static int pca953x_probe(struct i2c_client *client, 624 lockdep_set_subclass(&chip->i2c_lock, 625 i2c_adapter_depth(client->adapter)); 626 627+ ret = device_reset(&client->dev); 628+ if (ret == -EPROBE_DEFER) 629+ return -EPROBE_DEFER; 630+ 631 /* initialize cached registers from their original values. 632 * we can't share this chip with another i2c master. 633 */ 634@@ -1078,7 +1088,8 @@ static int pca953x_probe(struct i2c_client *client, 635 return 0; 636 637 err_exit: 638- regulator_disable(chip->regulator); 639+ if (chip->regulator) 640+ regulator_disable(chip->regulator); 641 return ret; 642 } 643 644@@ -1097,7 +1108,8 @@ static int pca953x_remove(struct i2c_client *client) 645 ret = 0; 646 } 647 648- regulator_disable(chip->regulator); 649+ if (chip->regulator) 650+ regulator_disable(chip->regulator); 651 652 return ret; 653 } 654@@ -1158,7 +1170,8 @@ static int pca953x_suspend(struct device *dev) 655 if (atomic_read(&chip->wakeup_path)) 656 device_set_wakeup_path(dev); 657 else 658- regulator_disable(chip->regulator); 659+ if (chip->regulator) 660+ regulator_disable(chip->regulator); 661 662 return 0; 663 } 664@@ -1168,15 +1181,20 @@ static int pca953x_resume(struct device *dev) 665 struct pca953x_chip *chip = dev_get_drvdata(dev); 666 int ret; 667 668+ regcache_cache_only(chip->regmap, false); 669+ 670 if (!atomic_read(&chip->wakeup_path)) { 671- ret = regulator_enable(chip->regulator); 672- if (ret) { 673- dev_err(dev, "Failed to enable regulator: %d\n", ret); 674+ if (chip->regulator) { 675+ ret = regulator_enable(chip->regulator); 676+ if (ret) { 677+ dev_err(dev, "Failed to enable regulator: %d\n", ret); 678+ return 0; 679+ } 680+ } else { 681 return 0; 682 } 683 } 684 685- regcache_cache_only(chip->regmap, false); 686 regcache_mark_dirty(chip->regmap); 687 ret = pca953x_regcache_sync(dev); 688 if (ret) 689diff --git a/drivers/gpio/gpio-scu.c b/drivers/gpio/gpio-scu.c 690new file mode 100644 691index 000000000..5fb2d8b72 692--- /dev/null 693+++ b/drivers/gpio/gpio-scu.c 694@@ -0,0 +1,137 @@ 695+// SPDX-License-Identifier: GPL-2.0-only 696+/* 697+ * Copyright 2021 NXP 698+ * 699+ * The driver exports a standard gpiochip interface 700+ */ 701+ 702+#include <linux/init.h> 703+#include <linux/slab.h> 704+#include <linux/mutex.h> 705+#include <linux/module.h> 706+#include <linux/gpio/driver.h> 707+#include <linux/platform_device.h> 708+#include <linux/firmware/imx/svc/rm.h> 709+#include <dt-bindings/firmware/imx/rsrc.h> 710+ 711+#define PIN_NUMBER 8 712+ 713+struct imxscfw { 714+ struct mutex lock; 715+ struct imx_sc_ipc *handle; 716+ struct gpio_chip chip; 717+ struct device *dev; 718+}; 719+ 720+static unsigned int sc_arr[] = { 721+ IMX_SC_R_BOARD_R0, 722+ IMX_SC_R_BOARD_R1, 723+ IMX_SC_R_BOARD_R2, 724+ IMX_SC_R_BOARD_R3, 725+ IMX_SC_R_BOARD_R4, 726+ IMX_SC_R_BOARD_R5, 727+ IMX_SC_R_BOARD_R6, //R6 is MII select 728+ IMX_SC_R_BOARD_R7, 729+}; 730+ 731+static int imxscfw_get(struct gpio_chip *chip, unsigned int offset) 732+{ 733+ struct imxscfw *scu = gpiochip_get_data(chip); 734+ int err = -EINVAL, level = 0; 735+ 736+ if (offset >= sizeof(sc_arr)/sizeof(unsigned int)) 737+ return err; 738+ 739+ mutex_lock(&scu->lock); 740+ 741+ /* to read PIN state via scu api */ 742+ err = imx_sc_misc_get_control(scu->handle, sc_arr[offset], 743+ 0, &level); 744+ mutex_unlock(&scu->lock); 745+ 746+ if (err) { 747+ pr_err("%s: failed %d\n", __func__, err); 748+ return -EINVAL; 749+ } 750+ 751+ return level; 752+} 753+ 754+static void imxscfw_set(struct gpio_chip *chip, unsigned int offset, int value) 755+{ 756+ struct imxscfw *scu = gpiochip_get_data(chip); 757+ int err; 758+ 759+ if (offset >= sizeof(sc_arr)/sizeof(unsigned int)) 760+ return; 761+ 762+ mutex_lock(&scu->lock); 763+ 764+ /* to set PIN output level via scu api */ 765+ err = imx_sc_misc_set_control(scu->handle, sc_arr[offset], 0, value); 766+ 767+ mutex_unlock(&scu->lock); 768+ 769+ if (err) 770+ pr_err("%s: failed %d\n", __func__, err); 771+ 772+ 773+} 774+ 775+static int imx_scu_gpio_probe(struct platform_device *pdev) 776+{ 777+ struct device *dev = &pdev->dev; 778+ struct device_node *np = dev->of_node; 779+ struct imxscfw *port; 780+ struct gpio_chip *gc; 781+ int ret; 782+ 783+ port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL); 784+ if (!port) 785+ return -ENOMEM; 786+ 787+ ret = imx_scu_get_handle(&port->handle); 788+ if (ret) 789+ return ret; 790+ 791+ mutex_init(&port->lock); 792+ gc = &port->chip; 793+ gc->of_node = np; 794+ gc->parent = dev; 795+ gc->label = "imx-scu-gpio"; 796+ gc->ngpio = PIN_NUMBER; 797+ gc->base = of_alias_get_id(np, "gpio") * 32; 798+ 799+ gc->get = imxscfw_get; 800+ gc->set = imxscfw_set; 801+ 802+ platform_set_drvdata(pdev, port); 803+ 804+ ret = devm_gpiochip_add_data(dev, gc, port); 805+ 806+ return ret; 807+} 808+ 809+static const struct of_device_id imx_scu_gpio_dt_ids[] = { 810+ { .compatible = "fsl,imx-scu-gpio" }, 811+ { /* sentinel */ } 812+}; 813+ 814+static struct platform_driver imx_scu_gpio_driver = { 815+ .driver = { 816+ .name = "gpio-imx-scu", 817+ .of_match_table = imx_scu_gpio_dt_ids, 818+ }, 819+ .probe = imx_scu_gpio_probe, 820+}; 821+ 822+static int __init _imx_scu_gpio_init(void) 823+{ 824+ return platform_driver_register(&imx_scu_gpio_driver); 825+} 826+ 827+subsys_initcall_sync(_imx_scu_gpio_init); 828+ 829+MODULE_AUTHOR("Shenwei Wang"); 830+MODULE_LICENSE("GPL v2"); 831+MODULE_DESCRIPTION("NXP GPIO over SCU-MISC API, i.MX8"); 832diff --git a/drivers/gpio/gpio-vf610.c b/drivers/gpio/gpio-vf610.c 833index 58776f2d6..b99f4968b 100644 834--- a/drivers/gpio/gpio-vf610.c 835+++ b/drivers/gpio/gpio-vf610.c 836@@ -300,7 +300,7 @@ static int vf610_gpio_probe(struct platform_device *pdev) 837 gc = &port->gc; 838 gc->of_node = np; 839 gc->parent = dev; 840- gc->label = "vf610-gpio"; 841+ gc->label = dev_name(dev); 842 gc->ngpio = VF610_GPIO_PER_PORT; 843 gc->base = of_alias_get_id(np, "gpio") * VF610_GPIO_PER_PORT; 844 845