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/module.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/platform_device.h>
12 #include <linux/firmware/imx/svc/rm.h>
13 #include <dt-bindings/firmware/imx/rsrc.h>
15 struct scu_gpio_priv {
16 struct gpio_chip chip;
19 struct imx_sc_ipc *handle;
22 static unsigned int scu_rsrc_arr[] = {
33 static int imx_scu_gpio_get(struct gpio_chip *chip, unsigned int offset)
35 struct scu_gpio_priv *priv = gpiochip_get_data(chip);
39 if (offset >= chip->ngpio)
42 mutex_lock(&priv->lock);
44 /* to read PIN state via scu api */
45 err = imx_sc_misc_get_control(priv->handle,
46 scu_rsrc_arr[offset], 0, &level);
47 mutex_unlock(&priv->lock);
50 dev_err(priv->dev, "SCU get failed: %d\n", err);
57 static void imx_scu_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
59 struct scu_gpio_priv *priv = gpiochip_get_data(chip);
62 if (offset >= chip->ngpio)
65 mutex_lock(&priv->lock);
67 /* to set PIN output level via scu api */
68 err = imx_sc_misc_set_control(priv->handle,
69 scu_rsrc_arr[offset], 0, value);
70 mutex_unlock(&priv->lock);
73 dev_err(priv->dev, "SCU set (%d) failed: %d\n",
74 scu_rsrc_arr[offset], err);
77 static int imx_scu_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
79 if (offset >= chip->ngpio)
82 return GPIO_LINE_DIRECTION_OUT;
85 static int imx_scu_gpio_probe(struct platform_device *pdev)
87 struct device *dev = &pdev->dev;
88 struct scu_gpio_priv *priv;
92 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
96 ret = imx_scu_get_handle(&priv->handle);
101 mutex_init(&priv->lock);
106 gc->ngpio = sizeof(scu_rsrc_arr)/sizeof(unsigned int);
107 gc->label = dev_name(dev);
108 gc->get = imx_scu_gpio_get;
109 gc->set = imx_scu_gpio_set;
110 gc->get_direction = imx_scu_gpio_get_direction;
112 platform_set_drvdata(pdev, priv);
114 return devm_gpiochip_add_data(dev, gc, priv);
117 static const struct of_device_id imx_scu_gpio_dt_ids[] = {
118 { .compatible = "fsl,imx8qxp-sc-gpio" },
122 static struct platform_driver imx_scu_gpio_driver = {
124 .name = "gpio-imx-scu",
125 .of_match_table = imx_scu_gpio_dt_ids,
127 .probe = imx_scu_gpio_probe,
130 static int __init _imx_scu_gpio_init(void)
132 return platform_driver_register(&imx_scu_gpio_driver);
135 subsys_initcall_sync(_imx_scu_gpio_init);
137 MODULE_AUTHOR("Shenwei Wang <shenwei.wang@nxp.com>");
138 MODULE_DESCRIPTION("NXP GPIO over IMX SCU API");
139 MODULE_LICENSE("GPL");