pwm: lpc32xx: Make use of devm_pwmchip_alloc() function
authorUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Wed, 14 Feb 2024 09:31:51 +0000 (10:31 +0100)
committerUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Mon, 19 Feb 2024 10:04:11 +0000 (11:04 +0100)
This prepares the pwm-lpc32xx driver to further changes of the pwm core
outlined in the commit introducing devm_pwmchip_alloc(). There is no
intended semantical change and the driver should behave as before.

Also convert the to_lpc32xx_pwm_chip() helper macro to a static inline
to get some type safety.

Link: https://lore.kernel.org/r/ef26238772ee9ca455a49e9b976a4f66654b26f7.1707900770.git.u.kleine-koenig@pengutronix.de
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
drivers/pwm/pwm-lpc32xx.c

index 1d9f3e7a2434ae55e515d64acacadb212b3bb776..c748537e57d1a798eb74de1d79c5008ec22f9358 100644 (file)
@@ -15,7 +15,6 @@
 #include <linux/slab.h>
 
 struct lpc32xx_pwm_chip {
-       struct pwm_chip chip;
        struct clk *clk;
        void __iomem *base;
 };
@@ -23,8 +22,10 @@ struct lpc32xx_pwm_chip {
 #define PWM_ENABLE     BIT(31)
 #define PWM_PIN_LEVEL  BIT(30)
 
-#define to_lpc32xx_pwm_chip(_chip) \
-       container_of(_chip, struct lpc32xx_pwm_chip, chip)
+static inline struct lpc32xx_pwm_chip *to_lpc32xx_pwm_chip(struct pwm_chip *chip)
+{
+       return pwmchip_get_drvdata(chip);
+}
 
 static int lpc32xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
                              int duty_ns, int period_ns)
@@ -119,13 +120,15 @@ static const struct pwm_ops lpc32xx_pwm_ops = {
 
 static int lpc32xx_pwm_probe(struct platform_device *pdev)
 {
+       struct pwm_chip *chip;
        struct lpc32xx_pwm_chip *lpc32xx;
        int ret;
        u32 val;
 
-       lpc32xx = devm_kzalloc(&pdev->dev, sizeof(*lpc32xx), GFP_KERNEL);
-       if (!lpc32xx)
-               return -ENOMEM;
+       chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*lpc32xx));
+       if (IS_ERR(chip))
+               return PTR_ERR(chip);
+       lpc32xx = to_lpc32xx_pwm_chip(chip);
 
        lpc32xx->base = devm_platform_ioremap_resource(pdev, 0);
        if (IS_ERR(lpc32xx->base))
@@ -135,16 +138,14 @@ static int lpc32xx_pwm_probe(struct platform_device *pdev)
        if (IS_ERR(lpc32xx->clk))
                return PTR_ERR(lpc32xx->clk);
 
-       lpc32xx->chip.dev = &pdev->dev;
-       lpc32xx->chip.ops = &lpc32xx_pwm_ops;
-       lpc32xx->chip.npwm = 1;
+       chip->ops = &lpc32xx_pwm_ops;
 
        /* If PWM is disabled, configure the output to the default value */
        val = readl(lpc32xx->base);
        val &= ~PWM_PIN_LEVEL;
        writel(val, lpc32xx->base);
 
-       ret = devm_pwmchip_add(&pdev->dev, &lpc32xx->chip);
+       ret = devm_pwmchip_add(&pdev->dev, chip);
        if (ret < 0) {
                dev_err(&pdev->dev, "failed to add PWM chip, error %d\n", ret);
                return ret;