| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (C) Overkiz SAS 2012 |
| 4 |
* |
| 5 |
* Author: Boris BREZILLON <[email protected]> |
| 6 |
*/ |
| 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> |
| 19 |
#include <linux/mfd/syscon.h> |
| 20 |
#include <linux/platform_device.h> |
| 21 |
#include <linux/pwm.h> |
| 22 |
#include <linux/of.h> |
| 23 |
#include <linux/regmap.h> |
| 24 |
#include <linux/slab.h> |
| 25 |
#include <soc/at91/atmel_tcb.h> |
| 26 |
|
| 27 |
#define NPWM 2 |
| 28 |
|
| 29 |
#define ATMEL_TC_ACMR_MASK (ATMEL_TC_ACPA | ATMEL_TC_ACPC | \ |
| 30 |
ATMEL_TC_AEEVT | ATMEL_TC_ASWTRG) |
| 31 |
|
| 32 |
#define ATMEL_TC_BCMR_MASK (ATMEL_TC_BCPB | ATMEL_TC_BCPC | \ |
| 33 |
ATMEL_TC_BEEVT | ATMEL_TC_BSWTRG) |
| 34 |
|
| 35 |
struct atmel_tcb_pwm_device { |
| 36 |
unsigned div; /* PWM clock divider */ |
| 37 |
unsigned duty; /* PWM duty expressed in clk cycles */ |
| 38 |
unsigned period; /* PWM period expressed in clk cycles */ |
| 39 |
}; |
| 40 |
|
| 41 |
struct atmel_tcb_channel { |
| 42 |
u32 enabled; |
| 43 |
u32 cmr; |
| 44 |
u32 ra; |
| 45 |
u32 rb; |
| 46 |
u32 rc; |
| 47 |
}; |
| 48 |
|
| 49 |
struct atmel_tcb_pwm_chip { |
| 50 |
spinlock_t lock; |
| 51 |
u8 channel; |
| 52 |
u8 width; |
| 53 |
unsigned long rate; |
| 54 |
unsigned long slow_rate; |
| 55 |
struct regmap *regmap; |
| 56 |
struct clk *clk; |
| 57 |
struct clk *gclk; |
| 58 |
struct clk *slow_clk; |
| 59 |
struct atmel_tcb_pwm_device pwms[NPWM]; |
| 60 |
struct atmel_tcb_channel bkup; |
| 61 |
}; |
| 62 |
|
| 63 |
static const u8 atmel_tcb_divisors[] = { 2, 8, 32, 128, 0, }; |
| 64 |
|
| 65 |
static inline struct atmel_tcb_pwm_chip *to_tcb_chip(struct pwm_chip *chip) |
| 66 |
{ |
| 67 |
return pwmchip_get_drvdata(chip); |
| 68 |
} |
| 69 |
|
| 70 |
static int atmel_tcb_pwm_request(struct pwm_chip *chip, |
| 71 |
struct pwm_device *pwm) |
| 72 |
{ |
| 73 |
struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip); |
| 74 |
struct atmel_tcb_pwm_device *tcbpwm = &tcbpwmc->pwms[pwm->hwpwm]; |
| 75 |
unsigned cmr; |
| 76 |
int ret; |
| 77 |
|
| 78 |
ret = clk_prepare_enable(tcbpwmc->clk); |
| 79 |
if (ret) |
| 80 |
return ret; |
| 81 |
|
| 82 |
tcbpwm->duty = 0; |
| 83 |
tcbpwm->period = 0; |
| 84 |
tcbpwm->div = 0; |
| 85 |
|
| 86 |
guard(spinlock)(&tcbpwmc->lock); |
| 87 |
|
| 88 |
regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), &cmr); |
| 89 |
/* |
| 90 |
* Get init config from Timer Counter registers if |
| 91 |
* Timer Counter is already configured as a PWM generator. |
| 92 |
*/ |
| 93 |
if (cmr & ATMEL_TC_WAVE) { |
| 94 |
if (pwm->hwpwm == 0) |
| 95 |
regmap_read(tcbpwmc->regmap, |
| 96 |
ATMEL_TC_REG(tcbpwmc->channel, RA), |
| 97 |
&tcbpwm->duty); |
| 98 |
else |
| 99 |
regmap_read(tcbpwmc->regmap, |
| 100 |
ATMEL_TC_REG(tcbpwmc->channel, RB), |
| 101 |
&tcbpwm->duty); |
| 102 |
|
| 103 |
tcbpwm->div = cmr & ATMEL_TC_TCCLKS; |
| 104 |
regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, RC), |
| 105 |
&tcbpwm->period); |
| 106 |
cmr &= (ATMEL_TC_TCCLKS | ATMEL_TC_ACMR_MASK | |
| 107 |
ATMEL_TC_BCMR_MASK); |
| 108 |
} else |
| 109 |
cmr = 0; |
| 110 |
|
| 111 |
cmr |= ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO | ATMEL_TC_EEVT_XC0; |
| 112 |
regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), cmr); |
| 113 |
|
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
static void atmel_tcb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) |
| 118 |
{ |
| 119 |
struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip); |
| 120 |
|
| 121 |
clk_disable_unprepare(tcbpwmc->clk); |
| 122 |
} |
| 123 |
|
| 124 |
static void atmel_tcb_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm, |
| 125 |
enum pwm_polarity polarity) |
| 126 |
{ |
| 127 |
struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip); |
| 128 |
struct atmel_tcb_pwm_device *tcbpwm = &tcbpwmc->pwms[pwm->hwpwm]; |
| 129 |
unsigned cmr; |
| 130 |
|
| 131 |
/* |
| 132 |
* If duty is 0 the timer will be stopped and we have to |
| 133 |
* configure the output correctly on software trigger: |
| 134 |
* - set output to high if PWM_POLARITY_INVERSED |
| 135 |
* - set output to low if PWM_POLARITY_NORMAL |
| 136 |
* |
| 137 |
* This is why we're reverting polarity in this case. |
| 138 |
*/ |
| 139 |
if (tcbpwm->duty == 0) |
| 140 |
polarity = !polarity; |
| 141 |
|
| 142 |
regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), &cmr); |
| 143 |
|
| 144 |
/* flush old setting and set the new one */ |
| 145 |
if (pwm->hwpwm == 0) { |
| 146 |
cmr &= ~ATMEL_TC_ACMR_MASK; |
| 147 |
if (polarity == PWM_POLARITY_INVERSED) |
| 148 |
cmr |= ATMEL_TC_ASWTRG_CLEAR; |
| 149 |
else |
| 150 |
cmr |= ATMEL_TC_ASWTRG_SET; |
| 151 |
} else { |
| 152 |
cmr &= ~ATMEL_TC_BCMR_MASK; |
| 153 |
if (polarity == PWM_POLARITY_INVERSED) |
| 154 |
cmr |= ATMEL_TC_BSWTRG_CLEAR; |
| 155 |
else |
| 156 |
cmr |= ATMEL_TC_BSWTRG_SET; |
| 157 |
} |
| 158 |
|
| 159 |
regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), cmr); |
| 160 |
|
| 161 |
/* |
| 162 |
* Use software trigger to apply the new setting. |
| 163 |
* If both PWM devices in this group are disabled we stop the clock. |
| 164 |
*/ |
| 165 |
if (!(cmr & (ATMEL_TC_ACPC | ATMEL_TC_BCPC))) { |
| 166 |
regmap_write(tcbpwmc->regmap, |
| 167 |
ATMEL_TC_REG(tcbpwmc->channel, CCR), |
| 168 |
ATMEL_TC_SWTRG | ATMEL_TC_CLKDIS); |
| 169 |
tcbpwmc->bkup.enabled = 1; |
| 170 |
} else { |
| 171 |
regmap_write(tcbpwmc->regmap, |
| 172 |
ATMEL_TC_REG(tcbpwmc->channel, CCR), |
| 173 |
ATMEL_TC_SWTRG); |
| 174 |
tcbpwmc->bkup.enabled = 0; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
static int atmel_tcb_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm, |
| 179 |
enum pwm_polarity polarity) |
| 180 |
{ |
| 181 |
struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip); |
| 182 |
struct atmel_tcb_pwm_device *tcbpwm = &tcbpwmc->pwms[pwm->hwpwm]; |
| 183 |
u32 cmr; |
| 184 |
|
| 185 |
/* |
| 186 |
* If duty is 0 the timer will be stopped and we have to |
| 187 |
* configure the output correctly on software trigger: |
| 188 |
* - set output to high if PWM_POLARITY_INVERSED |
| 189 |
* - set output to low if PWM_POLARITY_NORMAL |
| 190 |
* |
| 191 |
* This is why we're reverting polarity in this case. |
| 192 |
*/ |
| 193 |
if (tcbpwm->duty == 0) |
| 194 |
polarity = !polarity; |
| 195 |
|
| 196 |
regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), &cmr); |
| 197 |
|
| 198 |
/* flush old setting and set the new one */ |
| 199 |
cmr &= ~ATMEL_TC_TCCLKS; |
| 200 |
|
| 201 |
if (pwm->hwpwm == 0) { |
| 202 |
cmr &= ~ATMEL_TC_ACMR_MASK; |
| 203 |
|
| 204 |
/* Set CMR flags according to given polarity */ |
| 205 |
if (polarity == PWM_POLARITY_INVERSED) |
| 206 |
cmr |= ATMEL_TC_ASWTRG_CLEAR; |
| 207 |
else |
| 208 |
cmr |= ATMEL_TC_ASWTRG_SET; |
| 209 |
} else { |
| 210 |
cmr &= ~ATMEL_TC_BCMR_MASK; |
| 211 |
if (polarity == PWM_POLARITY_INVERSED) |
| 212 |
cmr |= ATMEL_TC_BSWTRG_CLEAR; |
| 213 |
else |
| 214 |
cmr |= ATMEL_TC_BSWTRG_SET; |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
* If duty is 0 or equal to period there's no need to register |
| 219 |
* a specific action on RA/RB and RC compare. |
| 220 |
* The output will be configured on software trigger and keep |
| 221 |
* this config till next config call. |
| 222 |
*/ |
| 223 |
if (tcbpwm->duty != tcbpwm->period && tcbpwm->duty > 0) { |
| 224 |
if (pwm->hwpwm == 0) { |
| 225 |
if (polarity == PWM_POLARITY_INVERSED) |
| 226 |
cmr |= ATMEL_TC_ACPA_SET | ATMEL_TC_ACPC_CLEAR; |
| 227 |
else |
| 228 |
cmr |= ATMEL_TC_ACPA_CLEAR | ATMEL_TC_ACPC_SET; |
| 229 |
} else { |
| 230 |
if (polarity == PWM_POLARITY_INVERSED) |
| 231 |
cmr |= ATMEL_TC_BCPB_SET | ATMEL_TC_BCPC_CLEAR; |
| 232 |
else |
| 233 |
cmr |= ATMEL_TC_BCPB_CLEAR | ATMEL_TC_BCPC_SET; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
cmr |= (tcbpwm->div & ATMEL_TC_TCCLKS); |
| 238 |
|
| 239 |
regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CMR), cmr); |
| 240 |
|
| 241 |
if (pwm->hwpwm == 0) |
| 242 |
regmap_write(tcbpwmc->regmap, |
| 243 |
ATMEL_TC_REG(tcbpwmc->channel, RA), |
| 244 |
tcbpwm->duty); |
| 245 |
else |
| 246 |
regmap_write(tcbpwmc->regmap, |
| 247 |
ATMEL_TC_REG(tcbpwmc->channel, RB), |
| 248 |
tcbpwm->duty); |
| 249 |
|
| 250 |
regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, RC), |
| 251 |
tcbpwm->period); |
| 252 |
|
| 253 |
/* Use software trigger to apply the new setting */ |
| 254 |
regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(tcbpwmc->channel, CCR), |
| 255 |
ATMEL_TC_SWTRG | ATMEL_TC_CLKEN); |
| 256 |
tcbpwmc->bkup.enabled = 1; |
| 257 |
return 0; |
| 258 |
} |
| 259 |
|
| 260 |
static int atmel_tcb_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, |
| 261 |
int duty_ns, int period_ns) |
| 262 |
{ |
| 263 |
struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip); |
| 264 |
struct atmel_tcb_pwm_device *tcbpwm = &tcbpwmc->pwms[pwm->hwpwm]; |
| 265 |
/* companion PWM sharing register values period and div */ |
| 266 |
struct atmel_tcb_pwm_device *atcbpwm = &tcbpwmc->pwms[pwm->hwpwm ^ 1]; |
| 267 |
int i = 0; |
| 268 |
int slowclk = 0; |
| 269 |
unsigned period; |
| 270 |
unsigned duty; |
| 271 |
unsigned long rate = tcbpwmc->rate; |
| 272 |
unsigned long long min; |
| 273 |
unsigned long long max; |
| 274 |
|
| 275 |
/* |
| 276 |
* Find best clk divisor: |
| 277 |
* the smallest divisor which can fulfill the period_ns requirements. |
| 278 |
* If there is a gclk, the first divisor is actually the gclk selector |
| 279 |
*/ |
| 280 |
if (tcbpwmc->gclk) |
| 281 |
i = 1; |
| 282 |
for (; i < ARRAY_SIZE(atmel_tcb_divisors); ++i) { |
| 283 |
if (atmel_tcb_divisors[i] == 0) { |
| 284 |
slowclk = i; |
| 285 |
continue; |
| 286 |
} |
| 287 |
min = div_u64((u64)NSEC_PER_SEC * atmel_tcb_divisors[i], rate); |
| 288 |
max = min << tcbpwmc->width; |
| 289 |
if (max >= period_ns) |
| 290 |
break; |
| 291 |
} |
| 292 |
|
| 293 |
/* |
| 294 |
* If none of the divisor are small enough to represent period_ns |
| 295 |
* take slow clock (32KHz). |
| 296 |
*/ |
| 297 |
if (i == ARRAY_SIZE(atmel_tcb_divisors)) { |
| 298 |
i = slowclk; |
| 299 |
rate = tcbpwmc->slow_rate; |
| 300 |
min = div_u64(NSEC_PER_SEC, rate); |
| 301 |
max = min << tcbpwmc->width; |
| 302 |
|
| 303 |
/* If period is too big return ERANGE error */ |
| 304 |
if (max < period_ns) |
| 305 |
return -ERANGE; |
| 306 |
} |
| 307 |
|
| 308 |
duty = div_u64(duty_ns, min); |
| 309 |
period = div_u64(period_ns, min); |
| 310 |
|
| 311 |
/* |
| 312 |
* PWM devices provided by the TCB driver are grouped by 2. |
| 313 |
* PWM devices in a given group must be configured with the |
| 314 |
* same period_ns. |
| 315 |
* |
| 316 |
* We're checking the period value of the second PWM device |
| 317 |
* in this group before applying the new config. |
| 318 |
*/ |
| 319 |
if ((atcbpwm->duty > 0 && atcbpwm->duty != atcbpwm->period) && |
| 320 |
(atcbpwm->div != i || atcbpwm->period != period)) { |
| 321 |
dev_err(pwmchip_parent(chip), |
| 322 |
"failed to configure period_ns: PWM group already configured with a different value\n"); |
| 323 |
return -EINVAL; |
| 324 |
} |
| 325 |
|
| 326 |
tcbpwm->period = period; |
| 327 |
tcbpwm->div = i; |
| 328 |
tcbpwm->duty = duty; |
| 329 |
|
| 330 |
return 0; |
| 331 |
} |
| 332 |
|
| 333 |
static int atmel_tcb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, |
| 334 |
const struct pwm_state *state) |
| 335 |
{ |
| 336 |
struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip); |
| 337 |
int duty_cycle, period; |
| 338 |
int ret; |
| 339 |
|
| 340 |
guard(spinlock)(&tcbpwmc->lock); |
| 341 |
|
| 342 |
if (!state->enabled) { |
| 343 |
atmel_tcb_pwm_disable(chip, pwm, state->polarity); |
| 344 |
return 0; |
| 345 |
} |
| 346 |
|
| 347 |
period = min(state->period, INT_MAX); |
| 348 |
duty_cycle = min(state->duty_cycle, INT_MAX); |
| 349 |
|
| 350 |
ret = atmel_tcb_pwm_config(chip, pwm, duty_cycle, period); |
| 351 |
if (ret) |
| 352 |
return ret; |
| 353 |
|
| 354 |
return atmel_tcb_pwm_enable(chip, pwm, state->polarity); |
| 355 |
} |
| 356 |
|
| 357 |
static const struct pwm_ops atmel_tcb_pwm_ops = { |
| 358 |
.request = atmel_tcb_pwm_request, |
| 359 |
.free = atmel_tcb_pwm_free, |
| 360 |
.apply = atmel_tcb_pwm_apply, |
| 361 |
}; |
| 362 |
|
| 363 |
static struct atmel_tcb_config tcb_rm9200_config = { |
| 364 |
.counter_width = 16, |
| 365 |
}; |
| 366 |
|
| 367 |
static struct atmel_tcb_config tcb_sam9x5_config = { |
| 368 |
.counter_width = 32, |
| 369 |
}; |
| 370 |
|
| 371 |
static struct atmel_tcb_config tcb_sama5d2_config = { |
| 372 |
.counter_width = 32, |
| 373 |
.has_gclk = 1, |
| 374 |
}; |
| 375 |
|
| 376 |
static const struct of_device_id atmel_tcb_of_match[] = { |
| 377 |
{ .compatible = "atmel,at91rm9200-tcb", .data = &tcb_rm9200_config, }, |
| 378 |
{ .compatible = "atmel,at91sam9x5-tcb", .data = &tcb_sam9x5_config, }, |
| 379 |
{ .compatible = "atmel,sama5d2-tcb", .data = &tcb_sama5d2_config, }, |
| 380 |
{ /* sentinel */ } |
| 381 |
}; |
| 382 |
|
| 383 |
static int atmel_tcb_pwm_probe(struct platform_device *pdev) |
| 384 |
{ |
| 385 |
struct pwm_chip *chip; |
| 386 |
const struct of_device_id *match; |
| 387 |
struct atmel_tcb_pwm_chip *tcbpwmc; |
| 388 |
const struct atmel_tcb_config *config; |
| 389 |
struct device_node *np = pdev->dev.of_node; |
| 390 |
char clk_name[] = "t0_clk"; |
| 391 |
int err; |
| 392 |
int channel; |
| 393 |
|
| 394 |
chip = devm_pwmchip_alloc(&pdev->dev, NPWM, sizeof(*tcbpwmc)); |
| 395 |
if (IS_ERR(chip)) |
| 396 |
return PTR_ERR(chip); |
| 397 |
tcbpwmc = to_tcb_chip(chip); |
| 398 |
|
| 399 |
err = of_property_read_u32(np, "reg", &channel); |
| 400 |
if (err < 0) { |
| 401 |
dev_err(&pdev->dev, |
| 402 |
"failed to get Timer Counter Block channel from device tree (error: %d)\n", |
| 403 |
err); |
| 404 |
return err; |
| 405 |
} |
| 406 |
|
| 407 |
tcbpwmc->regmap = syscon_node_to_regmap(np->parent); |
| 408 |
if (IS_ERR(tcbpwmc->regmap)) |
| 409 |
return PTR_ERR(tcbpwmc->regmap); |
| 410 |
|
| 411 |
tcbpwmc->slow_clk = of_clk_get_by_name(np->parent, "slow_clk"); |
| 412 |
if (IS_ERR(tcbpwmc->slow_clk)) |
| 413 |
return PTR_ERR(tcbpwmc->slow_clk); |
| 414 |
|
| 415 |
clk_name[1] += channel; |
| 416 |
tcbpwmc->clk = of_clk_get_by_name(np->parent, clk_name); |
| 417 |
if (IS_ERR(tcbpwmc->clk)) |
| 418 |
tcbpwmc->clk = of_clk_get_by_name(np->parent, "t0_clk"); |
| 419 |
if (IS_ERR(tcbpwmc->clk)) { |
| 420 |
err = PTR_ERR(tcbpwmc->clk); |
| 421 |
goto err_slow_clk; |
| 422 |
} |
| 423 |
|
| 424 |
match = of_match_node(atmel_tcb_of_match, np->parent); |
| 425 |
config = match->data; |
| 426 |
|
| 427 |
if (config->has_gclk) { |
| 428 |
tcbpwmc->gclk = of_clk_get_by_name(np->parent, "gclk"); |
| 429 |
if (IS_ERR(tcbpwmc->gclk)) { |
| 430 |
err = PTR_ERR(tcbpwmc->gclk); |
| 431 |
goto err_clk; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
chip->ops = &atmel_tcb_pwm_ops; |
| 436 |
chip->atomic = true; |
| 437 |
tcbpwmc->channel = channel; |
| 438 |
tcbpwmc->width = config->counter_width; |
| 439 |
|
| 440 |
err = clk_prepare_enable(tcbpwmc->clk); |
| 441 |
if (err) |
| 442 |
goto err_gclk; |
| 443 |
|
| 444 |
err = clk_prepare_enable(tcbpwmc->slow_clk); |
| 445 |
if (err) |
| 446 |
goto err_disable_clk;; |
| 447 |
|
| 448 |
err = clk_rate_exclusive_get(tcbpwmc->clk); |
| 449 |
if (err) |
| 450 |
goto err_disable_slow_clk; |
| 451 |
|
| 452 |
err = clk_rate_exclusive_get(tcbpwmc->slow_clk); |
| 453 |
if (err) |
| 454 |
goto err_clk_unlock; |
| 455 |
|
| 456 |
tcbpwmc->rate = clk_get_rate(tcbpwmc->clk); |
| 457 |
tcbpwmc->slow_rate = clk_get_rate(tcbpwmc->slow_clk); |
| 458 |
|
| 459 |
spin_lock_init(&tcbpwmc->lock); |
| 460 |
|
| 461 |
err = pwmchip_add(chip); |
| 462 |
if (err < 0) |
| 463 |
goto err_slow_clk_unlock; |
| 464 |
|
| 465 |
platform_set_drvdata(pdev, chip); |
| 466 |
|
| 467 |
return 0; |
| 468 |
|
| 469 |
err_slow_clk_unlock: |
| 470 |
clk_rate_exclusive_put(tcbpwmc->slow_clk); |
| 471 |
|
| 472 |
err_clk_unlock: |
| 473 |
clk_rate_exclusive_put(tcbpwmc->clk); |
| 474 |
|
| 475 |
err_disable_clk: |
| 476 |
clk_disable_unprepare(tcbpwmc->clk); |
| 477 |
|
| 478 |
err_disable_slow_clk: |
| 479 |
clk_disable_unprepare(tcbpwmc->slow_clk); |
| 480 |
|
| 481 |
err_gclk: |
| 482 |
clk_put(tcbpwmc->gclk); |
| 483 |
|
| 484 |
err_clk: |
| 485 |
clk_put(tcbpwmc->clk); |
| 486 |
|
| 487 |
err_slow_clk: |
| 488 |
clk_put(tcbpwmc->slow_clk); |
| 489 |
|
| 490 |
return err; |
| 491 |
} |
| 492 |
|
| 493 |
static void atmel_tcb_pwm_remove(struct platform_device *pdev) |
| 494 |
{ |
| 495 |
struct pwm_chip *chip = platform_get_drvdata(pdev); |
| 496 |
struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip); |
| 497 |
|
| 498 |
pwmchip_remove(chip); |
| 499 |
|
| 500 |
clk_rate_exclusive_put(tcbpwmc->slow_clk); |
| 501 |
clk_rate_exclusive_put(tcbpwmc->clk); |
| 502 |
clk_disable_unprepare(tcbpwmc->clk); |
| 503 |
clk_disable_unprepare(tcbpwmc->slow_clk); |
| 504 |
clk_put(tcbpwmc->gclk); |
| 505 |
clk_put(tcbpwmc->clk); |
| 506 |
clk_put(tcbpwmc->slow_clk); |
| 507 |
} |
| 508 |
|
| 509 |
static const struct of_device_id atmel_tcb_pwm_dt_ids[] = { |
| 510 |
{ .compatible = "atmel,tcb-pwm", }, |
| 511 |
{ /* sentinel */ } |
| 512 |
}; |
| 513 |
MODULE_DEVICE_TABLE(of, atmel_tcb_pwm_dt_ids); |
| 514 |
|
| 515 |
static int atmel_tcb_pwm_suspend(struct device *dev) |
| 516 |
{ |
| 517 |
struct pwm_chip *chip = dev_get_drvdata(dev); |
| 518 |
struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip); |
| 519 |
struct atmel_tcb_channel *chan = &tcbpwmc->bkup; |
| 520 |
unsigned int channel = tcbpwmc->channel; |
| 521 |
|
| 522 |
regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(channel, CMR), &chan->cmr); |
| 523 |
regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(channel, RA), &chan->ra); |
| 524 |
regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(channel, RB), &chan->rb); |
| 525 |
regmap_read(tcbpwmc->regmap, ATMEL_TC_REG(channel, RC), &chan->rc); |
| 526 |
|
| 527 |
return 0; |
| 528 |
} |
| 529 |
|
| 530 |
static int atmel_tcb_pwm_resume(struct device *dev) |
| 531 |
{ |
| 532 |
struct pwm_chip *chip = dev_get_drvdata(dev); |
| 533 |
struct atmel_tcb_pwm_chip *tcbpwmc = to_tcb_chip(chip); |
| 534 |
struct atmel_tcb_channel *chan = &tcbpwmc->bkup; |
| 535 |
unsigned int channel = tcbpwmc->channel; |
| 536 |
|
| 537 |
regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(channel, CMR), chan->cmr); |
| 538 |
regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(channel, RA), chan->ra); |
| 539 |
regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(channel, RB), chan->rb); |
| 540 |
regmap_write(tcbpwmc->regmap, ATMEL_TC_REG(channel, RC), chan->rc); |
| 541 |
|
| 542 |
if (chan->enabled) |
| 543 |
regmap_write(tcbpwmc->regmap, |
| 544 |
ATMEL_TC_CLKEN | ATMEL_TC_SWTRG, |
| 545 |
ATMEL_TC_REG(channel, CCR)); |
| 546 |
|
| 547 |
return 0; |
| 548 |
} |
| 549 |
|
| 550 |
static DEFINE_SIMPLE_DEV_PM_OPS(atmel_tcb_pwm_pm_ops, atmel_tcb_pwm_suspend, |
| 551 |
atmel_tcb_pwm_resume); |
| 552 |
|
| 553 |
static struct platform_driver atmel_tcb_pwm_driver = { |
| 554 |
.driver = { |
| 555 |
.name = "atmel-tcb-pwm", |
| 556 |
.of_match_table = atmel_tcb_pwm_dt_ids, |
| 557 |
.pm = pm_ptr(&atmel_tcb_pwm_pm_ops), |
| 558 |
}, |
| 559 |
.probe = atmel_tcb_pwm_probe, |
| 560 |
.remove = atmel_tcb_pwm_remove, |
| 561 |
}; |
| 562 |
module_platform_driver(atmel_tcb_pwm_driver); |
| 563 |
|
| 564 |
MODULE_AUTHOR("Boris BREZILLON <[email protected]>"); |
| 565 |
MODULE_DESCRIPTION("Atmel Timer Counter Pulse Width Modulation Driver"); |
| 566 |
MODULE_LICENSE("GPL v2"); |