.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright 2014 Bart Tanghe <bart.tanghe@thomasmore.be> |
---|
3 | | - * |
---|
4 | | - * This program is free software; you can redistribute it and/or modify |
---|
5 | | - * it under the terms of the GNU General Public License as published by |
---|
6 | | - * the Free Software Foundation; version 2. |
---|
7 | 4 | */ |
---|
8 | 5 | |
---|
9 | 6 | #include <linux/clk.h> |
---|
.. | .. |
---|
24 | 21 | #define PERIOD(x) (((x) * 0x10) + 0x10) |
---|
25 | 22 | #define DUTY(x) (((x) * 0x10) + 0x14) |
---|
26 | 23 | |
---|
27 | | -#define MIN_PERIOD 108 /* 9.2 MHz max. PWM clock */ |
---|
| 24 | +#define PERIOD_MIN 0x2 |
---|
28 | 25 | |
---|
29 | 26 | struct bcm2835_pwm { |
---|
30 | 27 | struct pwm_chip chip; |
---|
.. | .. |
---|
67 | 64 | struct bcm2835_pwm *pc = to_bcm2835_pwm(chip); |
---|
68 | 65 | unsigned long rate = clk_get_rate(pc->clk); |
---|
69 | 66 | unsigned long scaler; |
---|
| 67 | + u32 period; |
---|
70 | 68 | |
---|
71 | 69 | if (!rate) { |
---|
72 | 70 | dev_err(pc->dev, "failed to get clock rate\n"); |
---|
73 | 71 | return -EINVAL; |
---|
74 | 72 | } |
---|
75 | 73 | |
---|
76 | | - scaler = NSEC_PER_SEC / rate; |
---|
| 74 | + scaler = DIV_ROUND_CLOSEST(NSEC_PER_SEC, rate); |
---|
| 75 | + period = DIV_ROUND_CLOSEST(period_ns, scaler); |
---|
77 | 76 | |
---|
78 | | - if (period_ns <= MIN_PERIOD) { |
---|
79 | | - dev_err(pc->dev, "period %d not supported, minimum %d\n", |
---|
80 | | - period_ns, MIN_PERIOD); |
---|
| 77 | + if (period < PERIOD_MIN) |
---|
81 | 78 | return -EINVAL; |
---|
82 | | - } |
---|
83 | 79 | |
---|
84 | | - writel(duty_ns / scaler, pc->base + DUTY(pwm->hwpwm)); |
---|
85 | | - writel(period_ns / scaler, pc->base + PERIOD(pwm->hwpwm)); |
---|
| 80 | + writel(DIV_ROUND_CLOSEST(duty_ns, scaler), |
---|
| 81 | + pc->base + DUTY(pwm->hwpwm)); |
---|
| 82 | + writel(period, pc->base + PERIOD(pwm->hwpwm)); |
---|
86 | 83 | |
---|
87 | 84 | return 0; |
---|
88 | 85 | } |
---|
.. | .. |
---|
155 | 152 | return PTR_ERR(pc->base); |
---|
156 | 153 | |
---|
157 | 154 | pc->clk = devm_clk_get(&pdev->dev, NULL); |
---|
158 | | - if (IS_ERR(pc->clk)) { |
---|
159 | | - dev_err(&pdev->dev, "clock not found: %ld\n", PTR_ERR(pc->clk)); |
---|
160 | | - return PTR_ERR(pc->clk); |
---|
161 | | - } |
---|
| 155 | + if (IS_ERR(pc->clk)) |
---|
| 156 | + return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk), |
---|
| 157 | + "clock not found\n"); |
---|
162 | 158 | |
---|
163 | 159 | ret = clk_prepare_enable(pc->clk); |
---|
164 | 160 | if (ret) |
---|