blob: 36f7ea381838d2abdbb9ead17a103ff8b681b85b [file] [log] [blame]
Thomas Gleixneraf873fc2019-05-28 09:57:21 -07001// SPDX-License-Identifier: GPL-2.0-only
Boris BREZILLON9421bad2013-01-08 16:36:42 +01002/*
3 * Copyright (C) Overkiz SAS 2012
4 *
5 * Author: Boris BREZILLON <b.brezillon@overkiz.com>
Boris BREZILLON9421bad2013-01-08 16:36:42 +01006 */
7
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/clocksource.h>
11#include <linux/clockchips.h>
12#include <linux/interrupt.h>
13#include <linux/irq.h>
14
15#include <linux/clk.h>
16#include <linux/err.h>
17#include <linux/ioport.h>
18#include <linux/io.h>
Alexandre Belloni061f8572020-10-30 19:36:56 +010019#include <linux/mfd/syscon.h>
Boris BREZILLON9421bad2013-01-08 16:36:42 +010020#include <linux/platform_device.h>
Boris BREZILLON9421bad2013-01-08 16:36:42 +010021#include <linux/pwm.h>
22#include <linux/of_device.h>
Alexandre Belloni061f8572020-10-30 19:36:56 +010023#include <linux/of_irq.h>
24#include <linux/regmap.h>
Boris BREZILLON9421bad2013-01-08 16:36:42 +010025#include <linux/slab.h>
Alexandre Bellonic2c91362019-04-26 23:47:10 +020026#include <soc/at91/atmel_tcb.h>
Boris BREZILLON9421bad2013-01-08 16:36:42 +010027
Alexandre Belloni061f8572020-10-30 19:36:56 +010028#define NPWM 2
Boris BREZILLON9421bad2013-01-08 16:36:42 +010029
30#define ATMEL_TC_ACMR_MASK (ATMEL_TC_ACPA | ATMEL_TC_ACPC | \
31 ATMEL_TC_AEEVT | ATMEL_TC_ASWTRG)
32
33#define ATMEL_TC_BCMR_MASK (ATMEL_TC_BCPB | ATMEL_TC_BCPC | \
34 ATMEL_TC_BEEVT | ATMEL_TC_BSWTRG)
35
36struct atmel_tcb_pwm_device {
37 enum pwm_polarity polarity; /* PWM polarity */
38 unsigned div; /* PWM clock divider */
39 unsigned duty; /* PWM duty expressed in clk cycles */
40 unsigned period; /* PWM period expressed in clk cycles */
41};
42
Romain Izard1b3d9a92017-10-19 18:44:10 +020043struct atmel_tcb_channel {
44 u32 enabled;
45 u32 cmr;
46 u32 ra;
47 u32 rb;
48 u32 rc;
49};
50
Boris BREZILLON9421bad2013-01-08 16:36:42 +010051struct atmel_tcb_pwm_chip {
52 struct pwm_chip chip;
53 spinlock_t lock;
Alexandre Belloni061f8572020-10-30 19:36:56 +010054 u8 channel;
55 u8 width;
56 struct regmap *regmap;
57 struct clk *clk;
Alexandre Belloni34cbcd72020-10-30 19:36:57 +010058 struct clk *gclk;
Alexandre Belloni061f8572020-10-30 19:36:56 +010059 struct clk *slow_clk;
Boris BREZILLON9421bad2013-01-08 16:36:42 +010060 struct atmel_tcb_pwm_device *pwms[NPWM];
Alexandre Belloni061f8572020-10-30 19:36:56 +010061 struct atmel_tcb_channel bkup;
Boris BREZILLON9421bad2013-01-08 16:36:42 +010062};
63
Alexandre Belloni061f8572020-10-30 19:36:56 +010064const u8 atmel_tcb_divisors[] = { 2, 8, 32, 128, 0, };
65
Boris BREZILLON9421bad2013-01-08 16:36:42 +010066static inline struct atmel_tcb_pwm_chip *to_tcb_chip(struct pwm_chip *chip)
67{
68 return container_of(chip, struct atmel_tcb_pwm_chip, chip);
69}
70
71static int atmel_tcb_pwm_set_polarity(struct pwm_chip *chip,
72 struct pwm_device *pwm,
73 enum pwm_polarity polarity)
74{
75 struct atmel_tcb_pwm_device *tcbpwm = pwm_get_chip_data(pwm);
76
77 tcbpwm->polarity = polarity;
78
79 return 0;
80}
81
82static int atmel_tcb_pwm_request(struct pwm_chip *chip,
83 struct pwm_device *pwm)
84{
85 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
86 struct atmel_tcb_pwm_device *tcbpwm;
Boris BREZILLON9421bad2013-01-08 16:36:42 +010087 unsigned cmr;
88 int ret;
89
90 tcbpwm = devm_kzalloc(chip->dev, sizeof(*tcbpwm), GFP_KERNEL);
91 if (!tcbpwm)
92 return -ENOMEM;
93
Alexandre Belloni061f8572020-10-30 19:36:56 +010094 ret = clk_prepare_enable(tcbpwmc->clk);
Boris BREZILLON9421bad2013-01-08 16:36:42 +010095 if (ret) {
96 devm_kfree(chip->dev, tcbpwm);
97 return ret;
98 }
99
100 pwm_set_chip_data(pwm, tcbpwm);
101 tcbpwm->polarity = PWM_POLARITY_NORMAL;
102 tcbpwm->duty = 0;
103 tcbpwm->period = 0;
104 tcbpwm->div = 0;
105
106 spin_lock(&tcbpwmc->lock);
Alexandre Belloni061f8572020-10-30 19:36:56 +0100107 regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), &cmr);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100108 /*
109 * Get init config from Timer Counter registers if
110 * Timer Counter is already configured as a PWM generator.
111 */
112 if (cmr & ATMEL_TC_WAVE) {
Alexandre Belloni061f8572020-10-30 19:36:56 +0100113 if (pwm->hwpwm == 0)
114 regmap_read(tcbpwmc->regmap,
115 ATMEL_TC_REG(tcbpwmc->channel, RA),
116 &tcbpwm->duty);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100117 else
Alexandre Belloni061f8572020-10-30 19:36:56 +0100118 regmap_read(tcbpwmc->regmap,
119 ATMEL_TC_REG(tcbpwmc->channel, RB),
120 &tcbpwm->duty);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100121
122 tcbpwm->div = cmr & ATMEL_TC_TCCLKS;
Alexandre Belloni061f8572020-10-30 19:36:56 +0100123 regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, RC),
124 &tcbpwm->period);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100125 cmr &= (ATMEL_TC_TCCLKS | ATMEL_TC_ACMR_MASK |
126 ATMEL_TC_BCMR_MASK);
127 } else
128 cmr = 0;
129
130 cmr |= ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO | ATMEL_TC_EEVT_XC0;
Alexandre Belloni061f8572020-10-30 19:36:56 +0100131 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), cmr);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100132 spin_unlock(&tcbpwmc->lock);
133
134 tcbpwmc->pwms[pwm->hwpwm] = tcbpwm;
135
136 return 0;
137}
138
139static void atmel_tcb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
140{
141 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
142 struct atmel_tcb_pwm_device *tcbpwm = pwm_get_chip_data(pwm);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100143
Alexandre Belloni061f8572020-10-30 19:36:56 +0100144 clk_disable_unprepare(tcbpwmc->clk);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100145 tcbpwmc->pwms[pwm->hwpwm] = NULL;
146 devm_kfree(chip->dev, tcbpwm);
147}
148
149static void atmel_tcb_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
150{
151 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
152 struct atmel_tcb_pwm_device *tcbpwm = pwm_get_chip_data(pwm);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100153 unsigned cmr;
154 enum pwm_polarity polarity = tcbpwm->polarity;
155
156 /*
157 * If duty is 0 the timer will be stopped and we have to
158 * configure the output correctly on software trigger:
159 * - set output to high if PWM_POLARITY_INVERSED
160 * - set output to low if PWM_POLARITY_NORMAL
161 *
162 * This is why we're reverting polarity in this case.
163 */
164 if (tcbpwm->duty == 0)
165 polarity = !polarity;
166
167 spin_lock(&tcbpwmc->lock);
Alexandre Belloni061f8572020-10-30 19:36:56 +0100168 regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), &cmr);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100169
170 /* flush old setting and set the new one */
Alexandre Belloni061f8572020-10-30 19:36:56 +0100171 if (pwm->hwpwm == 0) {
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100172 cmr &= ~ATMEL_TC_ACMR_MASK;
173 if (polarity == PWM_POLARITY_INVERSED)
174 cmr |= ATMEL_TC_ASWTRG_CLEAR;
175 else
176 cmr |= ATMEL_TC_ASWTRG_SET;
177 } else {
178 cmr &= ~ATMEL_TC_BCMR_MASK;
179 if (polarity == PWM_POLARITY_INVERSED)
180 cmr |= ATMEL_TC_BSWTRG_CLEAR;
181 else
182 cmr |= ATMEL_TC_BSWTRG_SET;
183 }
184
Alexandre Belloni061f8572020-10-30 19:36:56 +0100185 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), cmr);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100186
187 /*
188 * Use software trigger to apply the new setting.
189 * If both PWM devices in this group are disabled we stop the clock.
190 */
Romain Izard1b3d9a92017-10-19 18:44:10 +0200191 if (!(cmr & (ATMEL_TC_ACPC | ATMEL_TC_BCPC))) {
Alexandre Belloni061f8572020-10-30 19:36:56 +0100192 regmap_write(tcbpwmc->regmap,
193 ATMEL_TC_REG(tcbpwmc->channel, CCR),
194 ATMEL_TC_SWTRG | ATMEL_TC_CLKDIS);
195 tcbpwmc->bkup.enabled = 1;
Romain Izard1b3d9a92017-10-19 18:44:10 +0200196 } else {
Alexandre Belloni061f8572020-10-30 19:36:56 +0100197 regmap_write(tcbpwmc->regmap,
198 ATMEL_TC_REG(tcbpwmc->channel, CCR),
199 ATMEL_TC_SWTRG);
200 tcbpwmc->bkup.enabled = 0;
Romain Izard1b3d9a92017-10-19 18:44:10 +0200201 }
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100202
203 spin_unlock(&tcbpwmc->lock);
204}
205
206static int atmel_tcb_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
207{
208 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
209 struct atmel_tcb_pwm_device *tcbpwm = pwm_get_chip_data(pwm);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100210 u32 cmr;
211 enum pwm_polarity polarity = tcbpwm->polarity;
212
213 /*
214 * If duty is 0 the timer will be stopped and we have to
215 * configure the output correctly on software trigger:
216 * - set output to high if PWM_POLARITY_INVERSED
217 * - set output to low if PWM_POLARITY_NORMAL
218 *
219 * This is why we're reverting polarity in this case.
220 */
221 if (tcbpwm->duty == 0)
222 polarity = !polarity;
223
224 spin_lock(&tcbpwmc->lock);
Alexandre Belloni061f8572020-10-30 19:36:56 +0100225 regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), &cmr);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100226
227 /* flush old setting and set the new one */
228 cmr &= ~ATMEL_TC_TCCLKS;
229
Alexandre Belloni061f8572020-10-30 19:36:56 +0100230 if (pwm->hwpwm == 0) {
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100231 cmr &= ~ATMEL_TC_ACMR_MASK;
232
233 /* Set CMR flags according to given polarity */
234 if (polarity == PWM_POLARITY_INVERSED)
235 cmr |= ATMEL_TC_ASWTRG_CLEAR;
236 else
237 cmr |= ATMEL_TC_ASWTRG_SET;
238 } else {
239 cmr &= ~ATMEL_TC_BCMR_MASK;
240 if (polarity == PWM_POLARITY_INVERSED)
241 cmr |= ATMEL_TC_BSWTRG_CLEAR;
242 else
243 cmr |= ATMEL_TC_BSWTRG_SET;
244 }
245
246 /*
247 * If duty is 0 or equal to period there's no need to register
248 * a specific action on RA/RB and RC compare.
249 * The output will be configured on software trigger and keep
250 * this config till next config call.
251 */
252 if (tcbpwm->duty != tcbpwm->period && tcbpwm->duty > 0) {
Alexandre Belloni061f8572020-10-30 19:36:56 +0100253 if (pwm->hwpwm == 0) {
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100254 if (polarity == PWM_POLARITY_INVERSED)
255 cmr |= ATMEL_TC_ACPA_SET | ATMEL_TC_ACPC_CLEAR;
256 else
257 cmr |= ATMEL_TC_ACPA_CLEAR | ATMEL_TC_ACPC_SET;
258 } else {
259 if (polarity == PWM_POLARITY_INVERSED)
260 cmr |= ATMEL_TC_BCPB_SET | ATMEL_TC_BCPC_CLEAR;
261 else
262 cmr |= ATMEL_TC_BCPB_CLEAR | ATMEL_TC_BCPC_SET;
263 }
264 }
265
Boris BREZILLONf3a82172013-09-18 17:06:05 +0200266 cmr |= (tcbpwm->div & ATMEL_TC_TCCLKS);
267
Alexandre Belloni061f8572020-10-30 19:36:56 +0100268 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), cmr);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100269
Alexandre Belloni061f8572020-10-30 19:36:56 +0100270 if (pwm->hwpwm == 0)
271 regmap_write(tcbpwmc->regmap,
272 ATMEL_TC_REG(tcbpwmc->channel, RA),
273 tcbpwm->duty);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100274 else
Alexandre Belloni061f8572020-10-30 19:36:56 +0100275 regmap_write(tcbpwmc->regmap,
276 ATMEL_TC_REG(tcbpwmc->channel, RB),
277 tcbpwm->duty);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100278
Alexandre Belloni061f8572020-10-30 19:36:56 +0100279 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, RC),
280 tcbpwm->period);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100281
282 /* Use software trigger to apply the new setting */
Alexandre Belloni061f8572020-10-30 19:36:56 +0100283 regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CCR),
284 ATMEL_TC_SWTRG | ATMEL_TC_CLKEN);
285 tcbpwmc->bkup.enabled = 1;
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100286 spin_unlock(&tcbpwmc->lock);
287 return 0;
288}
289
290static int atmel_tcb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
291 int duty_ns, int period_ns)
292{
293 struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip);
294 struct atmel_tcb_pwm_device *tcbpwm = pwm_get_chip_data(pwm);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100295 struct atmel_tcb_pwm_device *atcbpwm = NULL;
Alexandre Belloni34cbcd72020-10-30 19:36:57 +0100296 int i = 0;
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100297 int slowclk = 0;
298 unsigned period;
299 unsigned duty;
Alexandre Belloni061f8572020-10-30 19:36:56 +0100300 unsigned rate = clk_get_rate(tcbpwmc->clk);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100301 unsigned long long min;
302 unsigned long long max;
303
304 /*
305 * Find best clk divisor:
306 * the smallest divisor which can fulfill the period_ns requirements.
Alexandre Belloni34cbcd72020-10-30 19:36:57 +0100307 * If there is a gclk, the first divisor is actuallly the gclk selector
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100308 */
Alexandre Belloni34cbcd72020-10-30 19:36:57 +0100309 if (tcbpwmc->gclk)
310 i = 1;
311 for (; i < ARRAY_SIZE(atmel_tcb_divisors); ++i) {
Alexandre Belloni061f8572020-10-30 19:36:56 +0100312 if (atmel_tcb_divisors[i] == 0) {
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100313 slowclk = i;
314 continue;
315 }
Alexandre Belloni061f8572020-10-30 19:36:56 +0100316 min = div_u64((u64)NSEC_PER_SEC * atmel_tcb_divisors[i], rate);
317 max = min << tcbpwmc->width;
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100318 if (max >= period_ns)
319 break;
320 }
321
322 /*
323 * If none of the divisor are small enough to represent period_ns
324 * take slow clock (32KHz).
325 */
Alexandre Belloni061f8572020-10-30 19:36:56 +0100326 if (i == ARRAY_SIZE(atmel_tcb_divisors)) {
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100327 i = slowclk;
Alexandre Belloni061f8572020-10-30 19:36:56 +0100328 rate = clk_get_rate(tcbpwmc->slow_clk);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100329 min = div_u64(NSEC_PER_SEC, rate);
Alexandre Belloni061f8572020-10-30 19:36:56 +0100330 max = min << tcbpwmc->width;
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100331
332 /* If period is too big return ERANGE error */
333 if (max < period_ns)
334 return -ERANGE;
335 }
336
337 duty = div_u64(duty_ns, min);
338 period = div_u64(period_ns, min);
339
Alexandre Belloni061f8572020-10-30 19:36:56 +0100340 if (pwm->hwpwm == 0)
341 atcbpwm = tcbpwmc->pwms[1];
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100342 else
Alexandre Belloni061f8572020-10-30 19:36:56 +0100343 atcbpwm = tcbpwmc->pwms[0];
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100344
345 /*
Alexandre Belloni061f8572020-10-30 19:36:56 +0100346 * PWM devices provided by the TCB driver are grouped by 2.
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100347 * PWM devices in a given group must be configured with the
348 * same period_ns.
349 *
350 * We're checking the period value of the second PWM device
351 * in this group before applying the new config.
352 */
353 if ((atcbpwm && atcbpwm->duty > 0 &&
354 atcbpwm->duty != atcbpwm->period) &&
355 (atcbpwm->div != i || atcbpwm->period != period)) {
356 dev_err(chip->dev,
357 "failed to configure period_ns: PWM group already configured with a different value\n");
358 return -EINVAL;
359 }
360
361 tcbpwm->period = period;
362 tcbpwm->div = i;
363 tcbpwm->duty = duty;
364
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100365 return 0;
366}
367
Uwe Kleine-König30882cf2021-03-08 10:50:12 +0100368static int atmel_tcb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
369 const struct pwm_state *state)
370{
371 int duty_cycle, period;
372 int ret;
373
374 /* This function only sets a flag in driver data */
375 atmel_tcb_pwm_set_polarity(chip, pwm, state->polarity);
376
377 if (!state->enabled) {
378 atmel_tcb_pwm_disable(chip, pwm);
379 return 0;
380 }
381
382 period = state->period < INT_MAX ? state->period : INT_MAX;
383 duty_cycle = state->duty_cycle < INT_MAX ? state->duty_cycle : INT_MAX;
384
385 ret = atmel_tcb_pwm_config(chip, pwm, duty_cycle, period);
386 if (ret)
387 return ret;
388
389 return atmel_tcb_pwm_enable(chip, pwm);
390}
391
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100392static const struct pwm_ops atmel_tcb_pwm_ops = {
393 .request = atmel_tcb_pwm_request,
394 .free = atmel_tcb_pwm_free,
Uwe Kleine-König30882cf2021-03-08 10:50:12 +0100395 .apply = atmel_tcb_pwm_apply,
Axel Lin83c80dc2013-03-31 11:15:15 +0800396 .owner = THIS_MODULE,
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100397};
398
Alexandre Belloni061f8572020-10-30 19:36:56 +0100399static struct atmel_tcb_config tcb_rm9200_config = {
400 .counter_width = 16,
401};
402
403static struct atmel_tcb_config tcb_sam9x5_config = {
404 .counter_width = 32,
405};
406
Alexandre Belloni34cbcd72020-10-30 19:36:57 +0100407static struct atmel_tcb_config tcb_sama5d2_config = {
408 .counter_width = 32,
409 .has_gclk = 1,
410};
411
Alexandre Belloni061f8572020-10-30 19:36:56 +0100412static const struct of_device_id atmel_tcb_of_match[] = {
413 { .compatible = "atmel,at91rm9200-tcb", .data = &tcb_rm9200_config, },
414 { .compatible = "atmel,at91sam9x5-tcb", .data = &tcb_sam9x5_config, },
Alexandre Belloni34cbcd72020-10-30 19:36:57 +0100415 { .compatible = "atmel,sama5d2-tcb", .data = &tcb_sama5d2_config, },
Alexandre Belloni061f8572020-10-30 19:36:56 +0100416 { /* sentinel */ }
417};
418
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100419static int atmel_tcb_pwm_probe(struct platform_device *pdev)
420{
Alexandre Belloni061f8572020-10-30 19:36:56 +0100421 const struct of_device_id *match;
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100422 struct atmel_tcb_pwm_chip *tcbpwm;
Alexandre Belloni061f8572020-10-30 19:36:56 +0100423 const struct atmel_tcb_config *config;
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100424 struct device_node *np = pdev->dev.of_node;
Alexandre Belloni061f8572020-10-30 19:36:56 +0100425 struct regmap *regmap;
Alexandre Belloni34cbcd72020-10-30 19:36:57 +0100426 struct clk *clk, *gclk = NULL;
Alexandre Belloni061f8572020-10-30 19:36:56 +0100427 struct clk *slow_clk;
428 char clk_name[] = "t0_clk";
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100429 int err;
Alexandre Belloni061f8572020-10-30 19:36:56 +0100430 int channel;
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100431
Alexandre Belloni061f8572020-10-30 19:36:56 +0100432 err = of_property_read_u32(np, "reg", &channel);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100433 if (err < 0) {
434 dev_err(&pdev->dev,
Alexandre Belloni061f8572020-10-30 19:36:56 +0100435 "failed to get Timer Counter Block channel from device tree (error: %d)\n",
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100436 err);
437 return err;
438 }
439
Alexandre Belloni061f8572020-10-30 19:36:56 +0100440 regmap = syscon_node_to_regmap(np->parent);
441 if (IS_ERR(regmap))
442 return PTR_ERR(regmap);
443
444 slow_clk = of_clk_get_by_name(np->parent, "slow_clk");
445 if (IS_ERR(slow_clk))
446 return PTR_ERR(slow_clk);
447
448 clk_name[1] += channel;
449 clk = of_clk_get_by_name(np->parent, clk_name);
450 if (IS_ERR(clk))
451 clk = of_clk_get_by_name(np->parent, "t0_clk");
452 if (IS_ERR(clk))
453 return PTR_ERR(clk);
454
455 match = of_match_node(atmel_tcb_of_match, np->parent);
456 config = match->data;
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100457
Alexandre Belloni34cbcd72020-10-30 19:36:57 +0100458 if (config->has_gclk) {
459 gclk = of_clk_get_by_name(np->parent, "gclk");
460 if (IS_ERR(gclk))
461 return PTR_ERR(gclk);
462 }
463
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100464 tcbpwm = devm_kzalloc(&pdev->dev, sizeof(*tcbpwm), GFP_KERNEL);
465 if (tcbpwm == NULL) {
Boris Brezillon7d8d05d2015-08-16 11:23:46 +0200466 err = -ENOMEM;
Alexandre Belloni061f8572020-10-30 19:36:56 +0100467 goto err_slow_clk;
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100468 }
469
470 tcbpwm->chip.dev = &pdev->dev;
471 tcbpwm->chip.ops = &atmel_tcb_pwm_ops;
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100472 tcbpwm->chip.npwm = NPWM;
Alexandre Belloni061f8572020-10-30 19:36:56 +0100473 tcbpwm->channel = channel;
474 tcbpwm->regmap = regmap;
475 tcbpwm->clk = clk;
Alexandre Belloni34cbcd72020-10-30 19:36:57 +0100476 tcbpwm->gclk = gclk;
Alexandre Belloni061f8572020-10-30 19:36:56 +0100477 tcbpwm->slow_clk = slow_clk;
478 tcbpwm->width = config->counter_width;
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100479
Alexandre Belloni061f8572020-10-30 19:36:56 +0100480 err = clk_prepare_enable(slow_clk);
Boris Brezillon7d8d05d2015-08-16 11:23:46 +0200481 if (err)
Alexandre Belloni061f8572020-10-30 19:36:56 +0100482 goto err_slow_clk;
Boris Brezillon7d8d05d2015-08-16 11:23:46 +0200483
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100484 spin_lock_init(&tcbpwm->lock);
485
486 err = pwmchip_add(&tcbpwm->chip);
Boris Brezillon7d8d05d2015-08-16 11:23:46 +0200487 if (err < 0)
488 goto err_disable_clk;
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100489
490 platform_set_drvdata(pdev, tcbpwm);
491
492 return 0;
Boris Brezillon7d8d05d2015-08-16 11:23:46 +0200493
494err_disable_clk:
Alexandre Belloni061f8572020-10-30 19:36:56 +0100495 clk_disable_unprepare(tcbpwm->slow_clk);
Boris Brezillon7d8d05d2015-08-16 11:23:46 +0200496
Alexandre Belloni061f8572020-10-30 19:36:56 +0100497err_slow_clk:
498 clk_put(slow_clk);
Boris Brezillon7d8d05d2015-08-16 11:23:46 +0200499
500 return err;
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100501}
502
503static int atmel_tcb_pwm_remove(struct platform_device *pdev)
504{
505 struct atmel_tcb_pwm_chip *tcbpwm = platform_get_drvdata(pdev);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100506
Uwe Kleine-König319333b2021-07-07 18:28:22 +0200507 pwmchip_remove(&tcbpwm->chip);
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100508
Uwe Kleine-Königc77e99f2021-03-08 10:51:50 +0100509 clk_disable_unprepare(tcbpwm->slow_clk);
510 clk_put(tcbpwm->slow_clk);
511 clk_put(tcbpwm->clk);
512
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100513 return 0;
514}
515
516static const struct of_device_id atmel_tcb_pwm_dt_ids[] = {
517 { .compatible = "atmel,tcb-pwm", },
518 { /* sentinel */ }
519};
520MODULE_DEVICE_TABLE(of, atmel_tcb_pwm_dt_ids);
521
Romain Izard1b3d9a92017-10-19 18:44:10 +0200522#ifdef CONFIG_PM_SLEEP
523static int atmel_tcb_pwm_suspend(struct device *dev)
524{
Wolfram Sang692099c2018-04-19 16:06:13 +0200525 struct atmel_tcb_pwm_chip *tcbpwm = dev_get_drvdata(dev);
Alexandre Belloni061f8572020-10-30 19:36:56 +0100526 struct atmel_tcb_channel *chan = &tcbpwm->bkup;
527 unsigned int channel = tcbpwm->channel;
Romain Izard1b3d9a92017-10-19 18:44:10 +0200528
Alexandre Belloni061f8572020-10-30 19:36:56 +0100529 regmap_read(tcbpwm->regmap, ATMEL_TC_REG(channel, CMR), &chan->cmr);
530 regmap_read(tcbpwm->regmap, ATMEL_TC_REG(channel, RA), &chan->ra);
531 regmap_read(tcbpwm->regmap, ATMEL_TC_REG(channel, RB), &chan->rb);
532 regmap_read(tcbpwm->regmap, ATMEL_TC_REG(channel, RC), &chan->rc);
Romain Izard1b3d9a92017-10-19 18:44:10 +0200533
Romain Izard1b3d9a92017-10-19 18:44:10 +0200534 return 0;
535}
536
537static int atmel_tcb_pwm_resume(struct device *dev)
538{
Wolfram Sang692099c2018-04-19 16:06:13 +0200539 struct atmel_tcb_pwm_chip *tcbpwm = dev_get_drvdata(dev);
Alexandre Belloni061f8572020-10-30 19:36:56 +0100540 struct atmel_tcb_channel *chan = &tcbpwm->bkup;
541 unsigned int channel = tcbpwm->channel;
Romain Izard1b3d9a92017-10-19 18:44:10 +0200542
Alexandre Belloni061f8572020-10-30 19:36:56 +0100543 regmap_write(tcbpwm->regmap, ATMEL_TC_REG(channel, CMR), chan->cmr);
544 regmap_write(tcbpwm->regmap, ATMEL_TC_REG(channel, RA), chan->ra);
545 regmap_write(tcbpwm->regmap, ATMEL_TC_REG(channel, RB), chan->rb);
546 regmap_write(tcbpwm->regmap, ATMEL_TC_REG(channel, RC), chan->rc);
Romain Izard1b3d9a92017-10-19 18:44:10 +0200547
Alexandre Belloni061f8572020-10-30 19:36:56 +0100548 if (chan->enabled)
549 regmap_write(tcbpwm->regmap,
550 ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
551 ATMEL_TC_REG(channel, CCR));
552
Romain Izard1b3d9a92017-10-19 18:44:10 +0200553 return 0;
554}
555#endif
556
557static SIMPLE_DEV_PM_OPS(atmel_tcb_pwm_pm_ops, atmel_tcb_pwm_suspend,
558 atmel_tcb_pwm_resume);
559
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100560static struct platform_driver atmel_tcb_pwm_driver = {
561 .driver = {
562 .name = "atmel-tcb-pwm",
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100563 .of_match_table = atmel_tcb_pwm_dt_ids,
Romain Izard1b3d9a92017-10-19 18:44:10 +0200564 .pm = &atmel_tcb_pwm_pm_ops,
Boris BREZILLON9421bad2013-01-08 16:36:42 +0100565 },
566 .probe = atmel_tcb_pwm_probe,
567 .remove = atmel_tcb_pwm_remove,
568};
569module_platform_driver(atmel_tcb_pwm_driver);
570
571MODULE_AUTHOR("Boris BREZILLON <b.brezillon@overkiz.com>");
572MODULE_DESCRIPTION("Atmel Timer Counter Pulse Width Modulation Driver");
573MODULE_LICENSE("GPL v2");