1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 Spreadtrum Communications Inc.
9 #include <linux/math64.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/pwm.h>
15 #define SPRD_PWM_PRESCALE 0x0
16 #define SPRD_PWM_MOD 0x4
17 #define SPRD_PWM_DUTY 0x8
18 #define SPRD_PWM_ENABLE 0x18
20 #define SPRD_PWM_MOD_MAX GENMASK(7, 0)
21 #define SPRD_PWM_DUTY_MSK GENMASK(15, 0)
22 #define SPRD_PWM_PRESCALE_MSK GENMASK(7, 0)
23 #define SPRD_PWM_ENABLE_BIT BIT(0)
25 #define SPRD_PWM_CHN_NUM 4
26 #define SPRD_PWM_REGS_SHIFT 5
27 #define SPRD_PWM_CHN_CLKS_NUM 2
28 #define SPRD_PWM_CHN_OUTPUT_CLK 1
31 struct clk_bulk_data clks
[SPRD_PWM_CHN_CLKS_NUM
];
35 struct sprd_pwm_chip
{
37 struct sprd_pwm_chn chn
[SPRD_PWM_CHN_NUM
];
40 static inline struct sprd_pwm_chip
* sprd_pwm_from_chip(struct pwm_chip
*chip
)
42 return pwmchip_get_drvdata(chip
);
46 * The list of clocks required by PWM channels, and each channel has 2 clocks:
47 * enable clock and pwm clock.
49 static const char * const sprd_pwm_clks
[] = {
56 static u32
sprd_pwm_read(struct sprd_pwm_chip
*spc
, u32 hwid
, u32 reg
)
58 u32 offset
= reg
+ (hwid
<< SPRD_PWM_REGS_SHIFT
);
60 return readl_relaxed(spc
->base
+ offset
);
63 static void sprd_pwm_write(struct sprd_pwm_chip
*spc
, u32 hwid
,
66 u32 offset
= reg
+ (hwid
<< SPRD_PWM_REGS_SHIFT
);
68 writel_relaxed(val
, spc
->base
+ offset
);
71 static int sprd_pwm_get_state(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
72 struct pwm_state
*state
)
74 struct sprd_pwm_chip
*spc
= sprd_pwm_from_chip(chip
);
75 struct sprd_pwm_chn
*chn
= &spc
->chn
[pwm
->hwpwm
];
76 u32 val
, duty
, prescale
;
81 * The clocks to PWM channel has to be enabled first before
82 * reading to the registers.
84 ret
= clk_bulk_prepare_enable(SPRD_PWM_CHN_CLKS_NUM
, chn
->clks
);
86 dev_err(pwmchip_parent(chip
), "failed to enable pwm%u clocks\n",
91 val
= sprd_pwm_read(spc
, pwm
->hwpwm
, SPRD_PWM_ENABLE
);
92 if (val
& SPRD_PWM_ENABLE_BIT
)
93 state
->enabled
= true;
95 state
->enabled
= false;
98 * The hardware provides a counter that is feed by the source clock.
99 * The period length is (PRESCALE + 1) * MOD counter steps.
100 * The duty cycle length is (PRESCALE + 1) * DUTY counter steps.
101 * Thus the period_ns and duty_ns calculation formula should be:
102 * period_ns = NSEC_PER_SEC * (prescale + 1) * mod / clk_rate
103 * duty_ns = NSEC_PER_SEC * (prescale + 1) * duty / clk_rate
105 val
= sprd_pwm_read(spc
, pwm
->hwpwm
, SPRD_PWM_PRESCALE
);
106 prescale
= val
& SPRD_PWM_PRESCALE_MSK
;
107 tmp
= (prescale
+ 1) * NSEC_PER_SEC
* SPRD_PWM_MOD_MAX
;
108 state
->period
= DIV_ROUND_CLOSEST_ULL(tmp
, chn
->clk_rate
);
110 val
= sprd_pwm_read(spc
, pwm
->hwpwm
, SPRD_PWM_DUTY
);
111 duty
= val
& SPRD_PWM_DUTY_MSK
;
112 tmp
= (prescale
+ 1) * NSEC_PER_SEC
* duty
;
113 state
->duty_cycle
= DIV_ROUND_CLOSEST_ULL(tmp
, chn
->clk_rate
);
114 state
->polarity
= PWM_POLARITY_NORMAL
;
116 /* Disable PWM clocks if the PWM channel is not in enable state. */
118 clk_bulk_disable_unprepare(SPRD_PWM_CHN_CLKS_NUM
, chn
->clks
);
123 static int sprd_pwm_config(struct sprd_pwm_chip
*spc
, struct pwm_device
*pwm
,
124 int duty_ns
, int period_ns
)
126 struct sprd_pwm_chn
*chn
= &spc
->chn
[pwm
->hwpwm
];
131 * The hardware provides a counter that is feed by the source clock.
132 * The period length is (PRESCALE + 1) * MOD counter steps.
133 * The duty cycle length is (PRESCALE + 1) * DUTY counter steps.
135 * To keep the maths simple we're always using MOD = SPRD_PWM_MOD_MAX.
136 * The value for PRESCALE is selected such that the resulting period
137 * gets the maximal length not bigger than the requested one with the
138 * given settings (MOD = SPRD_PWM_MOD_MAX and input clock).
140 duty
= duty_ns
* SPRD_PWM_MOD_MAX
/ period_ns
;
142 tmp
= (u64
)chn
->clk_rate
* period_ns
;
143 do_div(tmp
, NSEC_PER_SEC
);
144 prescale
= DIV_ROUND_CLOSEST_ULL(tmp
, SPRD_PWM_MOD_MAX
) - 1;
145 if (prescale
> SPRD_PWM_PRESCALE_MSK
)
146 prescale
= SPRD_PWM_PRESCALE_MSK
;
149 * Note: Writing DUTY triggers the hardware to actually apply the
150 * values written to MOD and DUTY to the output, so must keep writing
153 * The hardware can ensures that current running period is completed
154 * before changing a new configuration to avoid mixed settings.
156 sprd_pwm_write(spc
, pwm
->hwpwm
, SPRD_PWM_PRESCALE
, prescale
);
157 sprd_pwm_write(spc
, pwm
->hwpwm
, SPRD_PWM_MOD
, SPRD_PWM_MOD_MAX
);
158 sprd_pwm_write(spc
, pwm
->hwpwm
, SPRD_PWM_DUTY
, duty
);
163 static int sprd_pwm_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
164 const struct pwm_state
*state
)
166 struct sprd_pwm_chip
*spc
= sprd_pwm_from_chip(chip
);
167 struct sprd_pwm_chn
*chn
= &spc
->chn
[pwm
->hwpwm
];
168 struct pwm_state
*cstate
= &pwm
->state
;
171 if (state
->polarity
!= PWM_POLARITY_NORMAL
)
174 if (state
->enabled
) {
175 if (!cstate
->enabled
) {
177 * The clocks to PWM channel has to be enabled first
178 * before writing to the registers.
180 ret
= clk_bulk_prepare_enable(SPRD_PWM_CHN_CLKS_NUM
,
183 dev_err(pwmchip_parent(chip
),
184 "failed to enable pwm%u clocks\n",
190 ret
= sprd_pwm_config(spc
, pwm
, state
->duty_cycle
,
195 sprd_pwm_write(spc
, pwm
->hwpwm
, SPRD_PWM_ENABLE
, 1);
196 } else if (cstate
->enabled
) {
198 * Note: After setting SPRD_PWM_ENABLE to zero, the controller
199 * will not wait for current period to be completed, instead it
200 * will stop the PWM channel immediately.
202 sprd_pwm_write(spc
, pwm
->hwpwm
, SPRD_PWM_ENABLE
, 0);
204 clk_bulk_disable_unprepare(SPRD_PWM_CHN_CLKS_NUM
, chn
->clks
);
210 static const struct pwm_ops sprd_pwm_ops
= {
211 .apply
= sprd_pwm_apply
,
212 .get_state
= sprd_pwm_get_state
,
215 static int sprd_pwm_clk_init(struct device
*dev
,
216 struct sprd_pwm_chn chn
[SPRD_PWM_CHN_NUM
])
221 for (i
= 0; i
< SPRD_PWM_CHN_NUM
; i
++) {
224 for (j
= 0; j
< SPRD_PWM_CHN_CLKS_NUM
; ++j
)
226 sprd_pwm_clks
[i
* SPRD_PWM_CHN_CLKS_NUM
+ j
];
228 ret
= devm_clk_bulk_get(dev
, SPRD_PWM_CHN_CLKS_NUM
,
234 return dev_err_probe(dev
, ret
,
235 "failed to get channel clocks\n");
238 clk_pwm
= chn
[i
].clks
[SPRD_PWM_CHN_OUTPUT_CLK
].clk
;
239 chn
[i
].clk_rate
= clk_get_rate(clk_pwm
);
243 return dev_err_probe(dev
, -ENODEV
, "no available PWM channels\n");
248 static int sprd_pwm_probe(struct platform_device
*pdev
)
250 struct pwm_chip
*chip
;
251 struct sprd_pwm_chip
*spc
;
252 struct sprd_pwm_chn chn
[SPRD_PWM_CHN_NUM
];
255 npwm
= sprd_pwm_clk_init(&pdev
->dev
, chn
);
259 chip
= devm_pwmchip_alloc(&pdev
->dev
, npwm
, sizeof(*spc
));
261 return PTR_ERR(chip
);
262 spc
= sprd_pwm_from_chip(chip
);
264 spc
->base
= devm_platform_ioremap_resource(pdev
, 0);
265 if (IS_ERR(spc
->base
))
266 return PTR_ERR(spc
->base
);
268 memcpy(spc
->chn
, chn
, sizeof(chn
));
270 chip
->ops
= &sprd_pwm_ops
;
272 ret
= devm_pwmchip_add(&pdev
->dev
, chip
);
274 dev_err(&pdev
->dev
, "failed to add PWM chip\n");
279 static const struct of_device_id sprd_pwm_of_match
[] = {
280 { .compatible
= "sprd,ums512-pwm", },
283 MODULE_DEVICE_TABLE(of
, sprd_pwm_of_match
);
285 static struct platform_driver sprd_pwm_driver
= {
288 .of_match_table
= sprd_pwm_of_match
,
290 .probe
= sprd_pwm_probe
,
293 module_platform_driver(sprd_pwm_driver
);
295 MODULE_DESCRIPTION("Spreadtrum PWM Driver");
296 MODULE_LICENSE("GPL v2");