1 From cc09cd9804f681ae3efec1832f2be890d9be467d Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Thu, 14 Oct 2021 11:09:18 +0100
4 Subject: [PATCH] drivers/gpio: Add a driver that wraps the PWM API as
7 For cases where spare PWM outputs are available, but are desired
8 to be addressed a standard outputs instead.
9 Wraps a PWM channel as a new GPIO chip with the one output.
11 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
13 drivers/gpio/Kconfig | 8 +++
14 drivers/gpio/Makefile | 1 +
15 drivers/gpio/gpio-pwm.c | 144 ++++++++++++++++++++++++++++++++++++++++
16 3 files changed, 153 insertions(+)
17 create mode 100644 drivers/gpio/gpio-pwm.c
19 --- a/drivers/gpio/Kconfig
20 +++ b/drivers/gpio/Kconfig
21 @@ -485,6 +485,14 @@ config GPIO_PMIC_EIC_SPRD
23 Say yes here to support Spreadtrum PMIC EIC device.
26 + tristate "PWM chip GPIO"
30 + Turn on support for exposing a PWM chip as a GPIO
34 bool "PXA GPIO support"
35 depends on ARCH_PXA || ARCH_MMP || COMPILE_TEST
36 --- a/drivers/gpio/Makefile
37 +++ b/drivers/gpio/Makefile
38 @@ -122,6 +122,7 @@ obj-$(CONFIG_GPIO_PCI_IDIO_16) += gpio-
39 obj-$(CONFIG_GPIO_PISOSR) += gpio-pisosr.o
40 obj-$(CONFIG_GPIO_PL061) += gpio-pl061.o
41 obj-$(CONFIG_GPIO_PMIC_EIC_SPRD) += gpio-pmic-eic-sprd.o
42 +obj-$(CONFIG_GPIO_PWM) += gpio-pwm.o
43 obj-$(CONFIG_GPIO_PXA) += gpio-pxa.o
44 obj-$(CONFIG_GPIO_RASPBERRYPI_EXP) += gpio-raspberrypi-exp.o
45 obj-$(CONFIG_GPIO_RC5T583) += gpio-rc5t583.o
47 +++ b/drivers/gpio/gpio-pwm.c
49 +// SPDX-License-Identifier: GPL-2.0+
51 + * GPIO driver wrapping PWM API
53 + * PWM 0% and PWM 100% are equivalent to digital GPIO
54 + * outputs, and there are times where it is useful to use
55 + * PWM outputs as straight GPIOs (eg outputs of NXP PCA9685
56 + * I2C PWM chip). This driver wraps the PWM API as a GPIO
59 + * Copyright (C) 2021 Raspberry Pi (Trading) Ltd.
62 +#include <linux/err.h>
63 +#include <linux/gpio/driver.h>
64 +#include <linux/module.h>
65 +#include <linux/platform_device.h>
66 +#include <linux/pwm.h>
69 + struct gpio_chip gc;
70 + struct pwm_device **pwm;
73 +static int pwm_gpio_get_direction(struct gpio_chip *gc, unsigned int off)
75 + return GPIO_LINE_DIRECTION_OUT;
78 +static void pwm_gpio_set(struct gpio_chip *gc, unsigned int off, int val)
80 + struct pwm_gpio *pwm_gpio = gpiochip_get_data(gc);
81 + struct pwm_state state;
83 + pwm_get_state(pwm_gpio->pwm[off], &state);
84 + state.duty_cycle = val ? state.period : 0;
85 + pwm_apply_state(pwm_gpio->pwm[off], &state);
88 +static int pwm_gpio_parse_dt(struct pwm_gpio *pwm_gpio,
91 + struct device_node *node = dev->of_node;
92 + struct pwm_state state;
93 + int ret = 0, i, num_gpios;
94 + const char *pwm_name;
99 + num_gpios = of_property_count_strings(node, "pwm-names");
100 + if (num_gpios <= 0)
103 + pwm_gpio->pwm = devm_kzalloc(dev,
104 + sizeof(*pwm_gpio->pwm) * num_gpios,
106 + if (!pwm_gpio->pwm)
109 + for (i = 0; i < num_gpios; i++) {
110 + ret = of_property_read_string_index(node, "pwm-names", i,
113 + dev_err(dev, "unable to get pwm device index %d, name %s",
118 + pwm_gpio->pwm[i] = devm_pwm_get(dev, pwm_name);
119 + if (IS_ERR(pwm_gpio->pwm[i])) {
120 + ret = PTR_ERR(pwm_gpio->pwm[i]);
121 + if (ret != -EPROBE_DEFER)
122 + dev_err(dev, "unable to request PWM\n");
126 + /* Sync up PWM state. */
127 + pwm_init_state(pwm_gpio->pwm[i], &state);
129 + state.duty_cycle = 0;
130 + pwm_apply_state(pwm_gpio->pwm[i], &state);
133 + pwm_gpio->gc.ngpio = num_gpios;
139 +static int pwm_gpio_probe(struct platform_device *pdev)
141 + struct device *dev = &pdev->dev;
142 + struct pwm_gpio *pwm_gpio;
145 + pwm_gpio = devm_kzalloc(dev, sizeof(*pwm_gpio), GFP_KERNEL);
149 + pwm_gpio->gc.parent = dev;
150 + pwm_gpio->gc.label = "pwm-gpio";
151 + pwm_gpio->gc.owner = THIS_MODULE;
152 + pwm_gpio->gc.of_node = dev->of_node;
153 + pwm_gpio->gc.base = -1;
155 + pwm_gpio->gc.get_direction = pwm_gpio_get_direction;
156 + pwm_gpio->gc.set = pwm_gpio_set;
157 + pwm_gpio->gc.can_sleep = true;
159 + ret = pwm_gpio_parse_dt(pwm_gpio, dev);
163 + if (!pwm_gpio->gc.ngpio)
166 + return devm_gpiochip_add_data(dev, &pwm_gpio->gc, pwm_gpio);
169 +static int pwm_gpio_remove(struct platform_device *pdev)
174 +static const struct of_device_id pwm_gpio_of_match[] = {
175 + { .compatible = "pwm-gpio" },
178 +MODULE_DEVICE_TABLE(of, pwm_gpio_of_match);
180 +static struct platform_driver pwm_gpio_driver = {
182 + .name = "pwm-gpio",
183 + .of_match_table = of_match_ptr(pwm_gpio_of_match),
185 + .probe = pwm_gpio_probe,
186 + .remove = pwm_gpio_remove,
188 +module_platform_driver(pwm_gpio_driver);
190 +MODULE_LICENSE("GPL");
191 +MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com>");
192 +MODULE_DESCRIPTION("PWM GPIO driver");