1 // SPDX-License-Identifier: GPL-2.0+
3 * Cirrus Logic CLPS711X PWM driver
4 * Author: Alexander Shiyan <shc_work@mail.ru>
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pwm.h>
14 struct clps711x_chip
{
20 static inline struct clps711x_chip
*to_clps711x_chip(struct pwm_chip
*chip
)
22 return pwmchip_get_drvdata(chip
);
25 static int clps711x_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
27 struct clps711x_chip
*priv
= to_clps711x_chip(chip
);
28 unsigned int freq
= clk_get_rate(priv
->clk
);
33 /* Store constant period value */
34 pwm
->args
.period
= DIV_ROUND_CLOSEST(NSEC_PER_SEC
, freq
);
39 static int clps711x_pwm_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
40 const struct pwm_state
*state
)
42 struct clps711x_chip
*priv
= to_clps711x_chip(chip
);
43 /* PWM0 - bits 4..7, PWM1 - bits 8..11 */
44 u32 shift
= (pwm
->hwpwm
+ 1) * 4;
48 if (state
->polarity
!= PWM_POLARITY_NORMAL
)
51 if (state
->period
!= pwm
->args
.period
)
55 val
= mul_u64_u64_div_u64(state
->duty_cycle
, 0xf, state
->period
);
59 spin_lock_irqsave(&priv
->lock
, flags
);
61 pmpcon
= readl(priv
->pmpcon
);
62 pmpcon
&= ~(0xf << shift
);
63 pmpcon
|= val
<< shift
;
64 writel(pmpcon
, priv
->pmpcon
);
66 spin_unlock_irqrestore(&priv
->lock
, flags
);
71 static const struct pwm_ops clps711x_pwm_ops
= {
72 .request
= clps711x_pwm_request
,
73 .apply
= clps711x_pwm_apply
,
76 static int clps711x_pwm_probe(struct platform_device
*pdev
)
78 struct pwm_chip
*chip
;
79 struct clps711x_chip
*priv
;
81 chip
= devm_pwmchip_alloc(&pdev
->dev
, 2, sizeof(*priv
));
84 priv
= to_clps711x_chip(chip
);
86 priv
->pmpcon
= devm_platform_ioremap_resource(pdev
, 0);
87 if (IS_ERR(priv
->pmpcon
))
88 return PTR_ERR(priv
->pmpcon
);
90 priv
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
91 if (IS_ERR(priv
->clk
))
92 return PTR_ERR(priv
->clk
);
94 chip
->ops
= &clps711x_pwm_ops
;
96 spin_lock_init(&priv
->lock
);
98 return devm_pwmchip_add(&pdev
->dev
, chip
);
101 static const struct of_device_id __maybe_unused clps711x_pwm_dt_ids
[] = {
102 { .compatible
= "cirrus,ep7209-pwm", },
105 MODULE_DEVICE_TABLE(of
, clps711x_pwm_dt_ids
);
107 static struct platform_driver clps711x_pwm_driver
= {
109 .name
= "clps711x-pwm",
110 .of_match_table
= of_match_ptr(clps711x_pwm_dt_ids
),
112 .probe
= clps711x_pwm_probe
,
114 module_platform_driver(clps711x_pwm_driver
);
116 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
117 MODULE_DESCRIPTION("Cirrus Logic CLPS711X PWM driver");
118 MODULE_LICENSE("GPL");