blob: 6ab597e54005cf1a31b6658317e1f198f74c1cc4 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Boris Brezillon2b4984b2014-10-07 15:38:14 +02002/*
3 * Copyright (C) 2014 Free Electrons
4 * Copyright (C) 2014 Atmel
5 *
6 * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
Boris Brezillon2b4984b2014-10-07 15:38:14 +02007 */
8
9#include <linux/clk.h>
10#include <linux/delay.h>
11#include <linux/mfd/atmel-hlcdc.h>
12#include <linux/module.h>
13#include <linux/platform_device.h>
14#include <linux/pwm.h>
15#include <linux/regmap.h>
16
17#define ATMEL_HLCDC_PWMCVAL_MASK GENMASK(15, 8)
18#define ATMEL_HLCDC_PWMCVAL(x) (((x) << 8) & ATMEL_HLCDC_PWMCVAL_MASK)
19#define ATMEL_HLCDC_PWMPOL BIT(4)
20#define ATMEL_HLCDC_PWMPS_MASK GENMASK(2, 0)
21#define ATMEL_HLCDC_PWMPS_MAX 0x6
22#define ATMEL_HLCDC_PWMPS(x) ((x) & ATMEL_HLCDC_PWMPS_MASK)
23
Boris BREZILLON39e046f2014-11-19 15:33:09 +010024struct atmel_hlcdc_pwm_errata {
25 bool slow_clk_erratum;
26 bool div1_clk_erratum;
27};
28
Boris Brezillon2b4984b2014-10-07 15:38:14 +020029struct atmel_hlcdc_pwm {
30 struct pwm_chip chip;
31 struct atmel_hlcdc *hlcdc;
32 struct clk *cur_clk;
Boris BREZILLON39e046f2014-11-19 15:33:09 +010033 const struct atmel_hlcdc_pwm_errata *errata;
Boris Brezillon2b4984b2014-10-07 15:38:14 +020034};
35
36static inline struct atmel_hlcdc_pwm *to_atmel_hlcdc_pwm(struct pwm_chip *chip)
37{
38 return container_of(chip, struct atmel_hlcdc_pwm, chip);
39}
40
Boris Brezillon22675172017-03-01 15:48:51 +010041static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
Uwe Kleine-König71523d12019-08-24 17:37:07 +020042 const struct pwm_state *state)
Boris Brezillon2b4984b2014-10-07 15:38:14 +020043{
44 struct atmel_hlcdc_pwm *chip = to_atmel_hlcdc_pwm(c);
45 struct atmel_hlcdc *hlcdc = chip->hlcdc;
Boris Brezillon22675172017-03-01 15:48:51 +010046 unsigned int status;
47 int ret;
Boris Brezillon2b4984b2014-10-07 15:38:14 +020048
Boris Brezillon22675172017-03-01 15:48:51 +010049 if (state->enabled) {
50 struct clk *new_clk = hlcdc->slow_clk;
51 u64 pwmcval = state->duty_cycle * 256;
52 unsigned long clk_freq;
53 u64 clk_period_ns;
54 u32 pwmcfg;
55 int pres;
Boris BREZILLONdf6922a2014-12-18 21:05:30 +010056
Boris Brezillon22675172017-03-01 15:48:51 +010057 if (!chip->errata || !chip->errata->slow_clk_erratum) {
58 clk_freq = clk_get_rate(new_clk);
59 if (!clk_freq)
60 return -EINVAL;
Boris Brezillon2b4984b2014-10-07 15:38:14 +020061
Boris Brezillon22675172017-03-01 15:48:51 +010062 clk_period_ns = (u64)NSEC_PER_SEC * 256;
63 do_div(clk_period_ns, clk_freq);
64 }
Boris BREZILLONdf6922a2014-12-18 21:05:30 +010065
Boris Brezillon22675172017-03-01 15:48:51 +010066 /* Errata: cannot use slow clk on some IP revisions */
67 if ((chip->errata && chip->errata->slow_clk_erratum) ||
68 clk_period_ns > state->period) {
69 new_clk = hlcdc->sys_clk;
70 clk_freq = clk_get_rate(new_clk);
71 if (!clk_freq)
72 return -EINVAL;
Boris Brezillon2b4984b2014-10-07 15:38:14 +020073
Boris Brezillon22675172017-03-01 15:48:51 +010074 clk_period_ns = (u64)NSEC_PER_SEC * 256;
75 do_div(clk_period_ns, clk_freq);
76 }
77
78 for (pres = 0; pres <= ATMEL_HLCDC_PWMPS_MAX; pres++) {
Boris BREZILLON39e046f2014-11-19 15:33:09 +010079 /* Errata: cannot divide by 1 on some IP revisions */
Boris Brezillon22675172017-03-01 15:48:51 +010080 if (!pres && chip->errata &&
81 chip->errata->div1_clk_erratum)
82 continue;
Boris BREZILLON39e046f2014-11-19 15:33:09 +010083
Boris Brezillon22675172017-03-01 15:48:51 +010084 if ((clk_period_ns << pres) >= state->period)
85 break;
86 }
Boris Brezillon2b4984b2014-10-07 15:38:14 +020087
Boris Brezillon22675172017-03-01 15:48:51 +010088 if (pres > ATMEL_HLCDC_PWMPS_MAX)
89 return -EINVAL;
Boris Brezillon2b4984b2014-10-07 15:38:14 +020090
Boris Brezillon22675172017-03-01 15:48:51 +010091 pwmcfg = ATMEL_HLCDC_PWMPS(pres);
Boris Brezillon2b4984b2014-10-07 15:38:14 +020092
Boris Brezillon22675172017-03-01 15:48:51 +010093 if (new_clk != chip->cur_clk) {
94 u32 gencfg = 0;
95 int ret;
Boris Brezillon2b4984b2014-10-07 15:38:14 +020096
Boris Brezillon22675172017-03-01 15:48:51 +010097 ret = clk_prepare_enable(new_clk);
98 if (ret)
99 return ret;
100
101 clk_disable_unprepare(chip->cur_clk);
102 chip->cur_clk = new_clk;
103
104 if (new_clk == hlcdc->sys_clk)
105 gencfg = ATMEL_HLCDC_CLKPWMSEL;
106
107 ret = regmap_update_bits(hlcdc->regmap,
108 ATMEL_HLCDC_CFG(0),
109 ATMEL_HLCDC_CLKPWMSEL,
110 gencfg);
111 if (ret)
112 return ret;
113 }
114
115 do_div(pwmcval, state->period);
116
117 /*
118 * The PWM duty cycle is configurable from 0/256 to 255/256 of
119 * the period cycle. Hence we can't set a duty cycle occupying
120 * the whole period cycle if we're asked to.
121 * Set it to 255 if pwmcval is greater than 256.
122 */
123 if (pwmcval > 255)
124 pwmcval = 255;
125
126 pwmcfg |= ATMEL_HLCDC_PWMCVAL(pwmcval);
127
128 if (state->polarity == PWM_POLARITY_NORMAL)
129 pwmcfg |= ATMEL_HLCDC_PWMPOL;
130
131 ret = regmap_update_bits(hlcdc->regmap, ATMEL_HLCDC_CFG(6),
132 ATMEL_HLCDC_PWMCVAL_MASK |
133 ATMEL_HLCDC_PWMPS_MASK |
134 ATMEL_HLCDC_PWMPOL,
135 pwmcfg);
136 if (ret)
137 return ret;
138
139 ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_EN,
140 ATMEL_HLCDC_PWM);
141 if (ret)
142 return ret;
143
144 ret = regmap_read_poll_timeout(hlcdc->regmap, ATMEL_HLCDC_SR,
145 status,
146 status & ATMEL_HLCDC_PWM,
147 10, 0);
148 if (ret)
149 return ret;
150 } else {
151 ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_DIS,
152 ATMEL_HLCDC_PWM);
153 if (ret)
154 return ret;
155
156 ret = regmap_read_poll_timeout(hlcdc->regmap, ATMEL_HLCDC_SR,
157 status,
158 !(status & ATMEL_HLCDC_PWM),
159 10, 0);
Boris Brezillon2b4984b2014-10-07 15:38:14 +0200160 if (ret)
161 return ret;
162
163 clk_disable_unprepare(chip->cur_clk);
Boris Brezillon22675172017-03-01 15:48:51 +0100164 chip->cur_clk = NULL;
Boris Brezillon2b4984b2014-10-07 15:38:14 +0200165 }
166
167 return 0;
168}
169
Boris Brezillon2b4984b2014-10-07 15:38:14 +0200170static const struct pwm_ops atmel_hlcdc_pwm_ops = {
Boris Brezillon22675172017-03-01 15:48:51 +0100171 .apply = atmel_hlcdc_pwm_apply,
Boris Brezillon2b4984b2014-10-07 15:38:14 +0200172 .owner = THIS_MODULE,
173};
174
Boris BREZILLON39e046f2014-11-19 15:33:09 +0100175static const struct atmel_hlcdc_pwm_errata atmel_hlcdc_pwm_at91sam9x5_errata = {
176 .slow_clk_erratum = true,
177};
178
179static const struct atmel_hlcdc_pwm_errata atmel_hlcdc_pwm_sama5d3_errata = {
180 .div1_clk_erratum = true,
181};
182
Boris Brezillonf9bb9da2017-03-01 15:52:27 +0100183#ifdef CONFIG_PM_SLEEP
184static int atmel_hlcdc_pwm_suspend(struct device *dev)
185{
186 struct atmel_hlcdc_pwm *chip = dev_get_drvdata(dev);
187
188 /* Keep the periph clock enabled if the PWM is still running. */
189 if (pwm_is_enabled(&chip->chip.pwms[0]))
190 clk_disable_unprepare(chip->hlcdc->periph_clk);
191
192 return 0;
193}
194
195static int atmel_hlcdc_pwm_resume(struct device *dev)
196{
197 struct atmel_hlcdc_pwm *chip = dev_get_drvdata(dev);
198 struct pwm_state state;
199 int ret;
200
201 pwm_get_state(&chip->chip.pwms[0], &state);
202
203 /* Re-enable the periph clock it was stopped during suspend. */
204 if (!state.enabled) {
205 ret = clk_prepare_enable(chip->hlcdc->periph_clk);
206 if (ret)
207 return ret;
208 }
209
210 return atmel_hlcdc_pwm_apply(&chip->chip, &chip->chip.pwms[0], &state);
211}
212#endif
213
214static SIMPLE_DEV_PM_OPS(atmel_hlcdc_pwm_pm_ops,
215 atmel_hlcdc_pwm_suspend, atmel_hlcdc_pwm_resume);
216
Boris BREZILLON39e046f2014-11-19 15:33:09 +0100217static const struct of_device_id atmel_hlcdc_dt_ids[] = {
218 {
Josh Wu7a593822015-07-31 18:51:20 +0200219 .compatible = "atmel,at91sam9n12-hlcdc",
220 /* 9n12 has same errata as 9x5 HLCDC PWM */
221 .data = &atmel_hlcdc_pwm_at91sam9x5_errata,
222 },
223 {
Boris BREZILLON39e046f2014-11-19 15:33:09 +0100224 .compatible = "atmel,at91sam9x5-hlcdc",
225 .data = &atmel_hlcdc_pwm_at91sam9x5_errata,
226 },
227 {
Nicolas Ferre2b8b0ef2015-09-09 15:32:30 +0200228 .compatible = "atmel,sama5d2-hlcdc",
229 },
230 {
Boris BREZILLON39e046f2014-11-19 15:33:09 +0100231 .compatible = "atmel,sama5d3-hlcdc",
232 .data = &atmel_hlcdc_pwm_sama5d3_errata,
233 },
Nicolas Ferre054d3e12015-02-20 16:58:18 +0100234 {
235 .compatible = "atmel,sama5d4-hlcdc",
236 .data = &atmel_hlcdc_pwm_sama5d3_errata,
237 },
Claudiu Bezneada9b3862019-06-05 10:25:44 +0000238 { .compatible = "microchip,sam9x60-hlcdc", },
Boris BREZILLON39e046f2014-11-19 15:33:09 +0100239 { /* sentinel */ },
240};
Luis de Bethencourta83a6a82015-09-18 18:58:21 +0200241MODULE_DEVICE_TABLE(of, atmel_hlcdc_dt_ids);
Boris BREZILLON39e046f2014-11-19 15:33:09 +0100242
Boris Brezillon2b4984b2014-10-07 15:38:14 +0200243static int atmel_hlcdc_pwm_probe(struct platform_device *pdev)
244{
Boris BREZILLON39e046f2014-11-19 15:33:09 +0100245 const struct of_device_id *match;
Boris Brezillon2b4984b2014-10-07 15:38:14 +0200246 struct device *dev = &pdev->dev;
247 struct atmel_hlcdc_pwm *chip;
248 struct atmel_hlcdc *hlcdc;
249 int ret;
250
251 hlcdc = dev_get_drvdata(dev->parent);
252
253 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
254 if (!chip)
255 return -ENOMEM;
256
257 ret = clk_prepare_enable(hlcdc->periph_clk);
258 if (ret)
259 return ret;
260
Boris BREZILLON39e046f2014-11-19 15:33:09 +0100261 match = of_match_node(atmel_hlcdc_dt_ids, dev->parent->of_node);
262 if (match)
263 chip->errata = match->data;
264
Boris Brezillon2b4984b2014-10-07 15:38:14 +0200265 chip->hlcdc = hlcdc;
266 chip->chip.ops = &atmel_hlcdc_pwm_ops;
267 chip->chip.dev = dev;
Boris Brezillon2b4984b2014-10-07 15:38:14 +0200268 chip->chip.npwm = 1;
269 chip->chip.of_xlate = of_pwm_xlate_with_flags;
270 chip->chip.of_pwm_n_cells = 3;
Boris Brezillon2b4984b2014-10-07 15:38:14 +0200271
Uwe Kleine-König965ebe392020-12-07 14:45:55 +0100272 ret = pwmchip_add(&chip->chip);
Boris Brezillon2b4984b2014-10-07 15:38:14 +0200273 if (ret) {
274 clk_disable_unprepare(hlcdc->periph_clk);
275 return ret;
276 }
277
278 platform_set_drvdata(pdev, chip);
279
280 return 0;
281}
282
283static int atmel_hlcdc_pwm_remove(struct platform_device *pdev)
284{
285 struct atmel_hlcdc_pwm *chip = platform_get_drvdata(pdev);
286 int ret;
287
288 ret = pwmchip_remove(&chip->chip);
289 if (ret)
290 return ret;
291
292 clk_disable_unprepare(chip->hlcdc->periph_clk);
293
294 return 0;
295}
296
297static const struct of_device_id atmel_hlcdc_pwm_dt_ids[] = {
298 { .compatible = "atmel,hlcdc-pwm" },
299 { /* sentinel */ },
300};
301
302static struct platform_driver atmel_hlcdc_pwm_driver = {
303 .driver = {
304 .name = "atmel-hlcdc-pwm",
305 .of_match_table = atmel_hlcdc_pwm_dt_ids,
Boris Brezillonf9bb9da2017-03-01 15:52:27 +0100306 .pm = &atmel_hlcdc_pwm_pm_ops,
Boris Brezillon2b4984b2014-10-07 15:38:14 +0200307 },
308 .probe = atmel_hlcdc_pwm_probe,
309 .remove = atmel_hlcdc_pwm_remove,
310};
311module_platform_driver(atmel_hlcdc_pwm_driver);
312
313MODULE_ALIAS("platform:atmel-hlcdc-pwm");
314MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
315MODULE_DESCRIPTION("Atmel HLCDC PWM driver");
316MODULE_LICENSE("GPL v2");