1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2021~2022 NXP
5 * The driver exports a standard gpiochip interface
6 * to control the PIN resources on SCU domain.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/platform_device.h>
13 #include <linux/firmware/imx/svc/rm.h>
14 #include <dt-bindings/firmware/imx/rsrc.h>
16 struct scu_gpio_priv {
17 struct gpio_chip chip;
20 struct imx_sc_ipc *handle;
23 static unsigned int scu_rsrc_arr[] = {
34 static int imx_scu_gpio_get(struct gpio_chip *chip, unsigned int offset)
36 struct scu_gpio_priv *priv = gpiochip_get_data(chip);
40 if (offset >= chip->ngpio)
43 mutex_lock(&priv->lock);
45 /* to read PIN state via scu api */
46 err = imx_sc_misc_get_control(priv->handle,
47 scu_rsrc_arr[offset], 0, &level);
48 mutex_unlock(&priv->lock);
51 dev_err(priv->dev, "SCU get failed: %d\n", err);
58 static void imx_scu_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
60 struct scu_gpio_priv *priv = gpiochip_get_data(chip);
63 if (offset >= chip->ngpio)
66 mutex_lock(&priv->lock);
68 /* to set PIN output level via scu api */
69 err = imx_sc_misc_set_control(priv->handle,
70 scu_rsrc_arr[offset], 0, value);
71 mutex_unlock(&priv->lock);
74 dev_err(priv->dev, "SCU set (%d) failed: %d\n",
75 scu_rsrc_arr[offset], err);
78 static int imx_scu_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
80 if (offset >= chip->ngpio)
83 return GPIO_LINE_DIRECTION_OUT;
86 static int imx_scu_gpio_probe(struct platform_device *pdev)
88 struct device *dev = &pdev->dev;
89 struct scu_gpio_priv *priv;
93 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
97 ret = imx_scu_get_handle(&priv->handle);
102 mutex_init(&priv->lock);
107 gc->ngpio = ARRAY_SIZE(scu_rsrc_arr);
108 gc->label = dev_name(dev);
109 gc->get = imx_scu_gpio_get;
110 gc->set = imx_scu_gpio_set;
111 gc->get_direction = imx_scu_gpio_get_direction;
113 platform_set_drvdata(pdev, priv);
115 return devm_gpiochip_add_data(dev, gc, priv);
118 static const struct of_device_id imx_scu_gpio_dt_ids[] = {
119 { .compatible = "fsl,imx8qxp-sc-gpio" },
123 static struct platform_driver imx_scu_gpio_driver = {
125 .name = "gpio-imx-scu",
126 .of_match_table = imx_scu_gpio_dt_ids,
128 .probe = imx_scu_gpio_probe,
131 static int __init _imx_scu_gpio_init(void)
133 return platform_driver_register(&imx_scu_gpio_driver);
136 subsys_initcall_sync(_imx_scu_gpio_init);
138 MODULE_AUTHOR("Shenwei Wang <shenwei.wang@nxp.com>");
139 MODULE_DESCRIPTION("NXP GPIO over IMX SCU API");