1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 Free Electrons
4 * Copyright (C) 2014 Atmel
10 #include <linux/delay.h>
11 #include <linux/mfd/atmel-hlcdc.h>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/pwm.h>
16 #include <linux/regmap.h>
18 #define ATMEL_HLCDC_PWMCVAL_MASK GENMASK(15, 8)
19 #define ATMEL_HLCDC_PWMCVAL(x) (((x) << 8) & ATMEL_HLCDC_PWMCVAL_MASK)
20 #define ATMEL_HLCDC_PWMPOL BIT(4)
21 #define ATMEL_HLCDC_PWMPS_MASK GENMASK(2, 0)
22 #define ATMEL_HLCDC_PWMPS_MAX 0x6
23 #define ATMEL_HLCDC_PWMPS(x) ((x) & ATMEL_HLCDC_PWMPS_MASK)
25 struct atmel_hlcdc_pwm_errata {
26 bool slow_clk_erratum;
27 bool div1_clk_erratum;
30 struct atmel_hlcdc_pwm {
32 struct atmel_hlcdc *hlcdc;
34 const struct atmel_hlcdc_pwm_errata *errata;
37 static inline struct atmel_hlcdc_pwm *to_atmel_hlcdc_pwm(struct pwm_chip *chip)
39 return container_of(chip, struct atmel_hlcdc_pwm, chip);
42 static int atmel_hlcdc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
43 const struct pwm_state *state)
45 struct atmel_hlcdc_pwm *atmel = to_atmel_hlcdc_pwm(chip);
46 struct atmel_hlcdc *hlcdc = atmel->hlcdc;
51 struct clk *new_clk = hlcdc->slow_clk;
52 u64 pwmcval = state->duty_cycle * 256;
53 unsigned long clk_freq;
58 if (!atmel->errata || !atmel->errata->slow_clk_erratum) {
59 clk_freq = clk_get_rate(new_clk);
63 clk_period_ns = (u64)NSEC_PER_SEC * 256;
64 do_div(clk_period_ns, clk_freq);
67 /* Errata: cannot use slow clk on some IP revisions */
68 if ((atmel->errata && atmel->errata->slow_clk_erratum) ||
69 clk_period_ns > state->period) {
70 new_clk = hlcdc->sys_clk;
71 clk_freq = clk_get_rate(new_clk);
75 clk_period_ns = (u64)NSEC_PER_SEC * 256;
76 do_div(clk_period_ns, clk_freq);
79 for (pres = 0; pres <= ATMEL_HLCDC_PWMPS_MAX; pres++) {
80 /* Errata: cannot divide by 1 on some IP revisions */
81 if (!pres && atmel->errata &&
82 atmel->errata->div1_clk_erratum)
85 if ((clk_period_ns << pres) >= state->period)
89 if (pres > ATMEL_HLCDC_PWMPS_MAX)
92 pwmcfg = ATMEL_HLCDC_PWMPS(pres);
94 if (new_clk != atmel->cur_clk) {
98 ret = clk_prepare_enable(new_clk);
102 clk_disable_unprepare(atmel->cur_clk);
103 atmel->cur_clk = new_clk;
105 if (new_clk == hlcdc->sys_clk)
106 gencfg = ATMEL_HLCDC_CLKPWMSEL;
108 ret = regmap_update_bits(hlcdc->regmap,
110 ATMEL_HLCDC_CLKPWMSEL,
116 do_div(pwmcval, state->period);
119 * The PWM duty cycle is configurable from 0/256 to 255/256 of
120 * the period cycle. Hence we can't set a duty cycle occupying
121 * the whole period cycle if we're asked to.
122 * Set it to 255 if pwmcval is greater than 256.
127 pwmcfg |= ATMEL_HLCDC_PWMCVAL(pwmcval);
129 if (state->polarity == PWM_POLARITY_NORMAL)
130 pwmcfg |= ATMEL_HLCDC_PWMPOL;
132 ret = regmap_update_bits(hlcdc->regmap, ATMEL_HLCDC_CFG(6),
133 ATMEL_HLCDC_PWMCVAL_MASK |
134 ATMEL_HLCDC_PWMPS_MASK |
140 ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_EN,
145 ret = regmap_read_poll_timeout(hlcdc->regmap, ATMEL_HLCDC_SR,
147 status & ATMEL_HLCDC_PWM,
152 ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_DIS,
157 ret = regmap_read_poll_timeout(hlcdc->regmap, ATMEL_HLCDC_SR,
159 !(status & ATMEL_HLCDC_PWM),
164 clk_disable_unprepare(atmel->cur_clk);
165 atmel->cur_clk = NULL;
171 static const struct pwm_ops atmel_hlcdc_pwm_ops = {
172 .apply = atmel_hlcdc_pwm_apply,
175 static const struct atmel_hlcdc_pwm_errata atmel_hlcdc_pwm_at91sam9x5_errata = {
176 .slow_clk_erratum = true,
179 static const struct atmel_hlcdc_pwm_errata atmel_hlcdc_pwm_sama5d3_errata = {
180 .div1_clk_erratum = true,
183 static int atmel_hlcdc_pwm_suspend(struct device *dev)
185 struct atmel_hlcdc_pwm *atmel = dev_get_drvdata(dev);
187 /* Keep the periph clock enabled if the PWM is still running. */
188 if (pwm_is_enabled(&atmel->chip.pwms[0]))
189 clk_disable_unprepare(atmel->hlcdc->periph_clk);
194 static int atmel_hlcdc_pwm_resume(struct device *dev)
196 struct atmel_hlcdc_pwm *atmel = dev_get_drvdata(dev);
197 struct pwm_state state;
200 pwm_get_state(&atmel->chip.pwms[0], &state);
202 /* Re-enable the periph clock it was stopped during suspend. */
203 if (!state.enabled) {
204 ret = clk_prepare_enable(atmel->hlcdc->periph_clk);
209 return atmel_hlcdc_pwm_apply(&atmel->chip, &atmel->chip.pwms[0],
213 static DEFINE_SIMPLE_DEV_PM_OPS(atmel_hlcdc_pwm_pm_ops,
214 atmel_hlcdc_pwm_suspend, atmel_hlcdc_pwm_resume);
216 static const struct of_device_id atmel_hlcdc_dt_ids[] = {
218 .compatible = "atmel,at91sam9n12-hlcdc",
219 /* 9n12 has same errata as 9x5 HLCDC PWM */
220 .data = &atmel_hlcdc_pwm_at91sam9x5_errata,
223 .compatible = "atmel,at91sam9x5-hlcdc",
224 .data = &atmel_hlcdc_pwm_at91sam9x5_errata,
227 .compatible = "atmel,sama5d2-hlcdc",
230 .compatible = "atmel,sama5d3-hlcdc",
231 .data = &atmel_hlcdc_pwm_sama5d3_errata,
234 .compatible = "atmel,sama5d4-hlcdc",
235 .data = &atmel_hlcdc_pwm_sama5d3_errata,
237 { .compatible = "microchip,sam9x60-hlcdc", },
240 MODULE_DEVICE_TABLE(of, atmel_hlcdc_dt_ids);
242 static int atmel_hlcdc_pwm_probe(struct platform_device *pdev)
244 const struct of_device_id *match;
245 struct device *dev = &pdev->dev;
246 struct atmel_hlcdc_pwm *atmel;
247 struct atmel_hlcdc *hlcdc;
250 hlcdc = dev_get_drvdata(dev->parent);
252 atmel = devm_kzalloc(dev, sizeof(*atmel), GFP_KERNEL);
256 ret = clk_prepare_enable(hlcdc->periph_clk);
260 match = of_match_node(atmel_hlcdc_dt_ids, dev->parent->of_node);
262 atmel->errata = match->data;
264 atmel->hlcdc = hlcdc;
265 atmel->chip.ops = &atmel_hlcdc_pwm_ops;
266 atmel->chip.dev = dev;
267 atmel->chip.npwm = 1;
269 ret = pwmchip_add(&atmel->chip);
271 clk_disable_unprepare(hlcdc->periph_clk);
275 platform_set_drvdata(pdev, atmel);
280 static void atmel_hlcdc_pwm_remove(struct platform_device *pdev)
282 struct atmel_hlcdc_pwm *atmel = platform_get_drvdata(pdev);
284 pwmchip_remove(&atmel->chip);
286 clk_disable_unprepare(atmel->hlcdc->periph_clk);
289 static const struct of_device_id atmel_hlcdc_pwm_dt_ids[] = {
290 { .compatible = "atmel,hlcdc-pwm" },
294 static struct platform_driver atmel_hlcdc_pwm_driver = {
296 .name = "atmel-hlcdc-pwm",
297 .of_match_table = atmel_hlcdc_pwm_dt_ids,
298 .pm = pm_ptr(&atmel_hlcdc_pwm_pm_ops),
300 .probe = atmel_hlcdc_pwm_probe,
301 .remove_new = atmel_hlcdc_pwm_remove,
303 module_platform_driver(atmel_hlcdc_pwm_driver);
305 MODULE_ALIAS("platform:atmel-hlcdc-pwm");
307 MODULE_DESCRIPTION("Atmel HLCDC PWM driver");
308 MODULE_LICENSE("GPL v2");