Skip to content

Commit 1d7796b

Browse files
Sandipan Patrathierryreding
authored andcommitted
pwm: tegra: Support dynamic clock frequency configuration
Added support for dynamic clock freq configuration in PWM kernel driver. Earlier the PWM driver used to cache boot time clock rate by PWM clock parent during probe. Hence dynamically changing PWM frequency was not possible for all the possible ranges. With this change, dynamic calculation is enabled and it is able to set the requested period from sysfs knob provided the value is supported by clock source. Changes mainly have 2 parts: - Tegra186 and later chips [1] - Tegra210 and prior chips [2] For [1] - Changes implemented to set pwm period dynamically and also checks added to allow only if requested period(ns) is below or equals to higher range. For [2] - Only checks if the requested period(ns) is below or equals to higher range defined by max clock limit. The limitation in Tegra210 or prior chips are due to the reason of having only one PWM controller supporting multiple channels. But later chips have multiple PWM controller instances each having single channel support. Signed-off-by: Sandipan Patra <[email protected]> Reviewed-by: Jon Hunter <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent 74db728 commit 1d7796b

File tree

1 file changed

+76
-4
lines changed

1 file changed

+76
-4
lines changed

drivers/pwm/pwm-tegra.c

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,36 @@
44
*
55
* Tegra pulse-width-modulation controller driver
66
*
7-
* Copyright (c) 2010, NVIDIA Corporation.
7+
* Copyright (c) 2010-2020, NVIDIA Corporation.
88
* Based on arch/arm/plat-mxc/pwm.c by Sascha Hauer <[email protected]>
9+
*
10+
* Overview of Tegra Pulse Width Modulator Register:
11+
* 1. 13-bit: Frequency division (SCALE)
12+
* 2. 8-bit : Pulse division (DUTY)
13+
* 3. 1-bit : Enable bit
14+
*
15+
* The PWM clock frequency is divided by 256 before subdividing it based
16+
* on the programmable frequency division value to generate the required
17+
* frequency for PWM output. The maximum output frequency that can be
18+
* achieved is (max rate of source clock) / 256.
19+
* e.g. if source clock rate is 408 MHz, maximum output frequency can be:
20+
* 408 MHz/256 = 1.6 MHz.
21+
* This 1.6 MHz frequency can further be divided using SCALE value in PWM.
22+
*
23+
* PWM pulse width: 8 bits are usable [23:16] for varying pulse width.
24+
* To achieve 100% duty cycle, program Bit [24] of this register to
25+
* 1’b1. In which case the other bits [23:16] are set to don't care.
26+
*
27+
* Limitations:
28+
* - When PWM is disabled, the output is driven to inactive.
29+
* - It does not allow the current PWM period to complete and
30+
* stops abruptly.
31+
*
32+
* - If the register is reconfigured while PWM is running,
33+
* it does not complete the currently running period.
34+
*
35+
* - If the user input duty is beyond acceptible limits,
36+
* -EINVAL is returned.
937
*/
1038

1139
#include <linux/clk.h>
@@ -41,6 +69,7 @@ struct tegra_pwm_chip {
4169
struct reset_control*rst;
4270

4371
unsigned long clk_rate;
72+
unsigned long min_period_ns;
4473

4574
void __iomem *regs;
4675

@@ -68,7 +97,7 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
6897
{
6998
struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
7099
unsigned long long c = duty_ns, hz;
71-
unsigned long rate;
100+
unsigned long rate, required_clk_rate;
72101
u32 val = 0;
73102
int err;
74103

@@ -82,10 +111,48 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
82111

83112
val = (u32)c << PWM_DUTY_SHIFT;
84113

114+
/*
115+
* min period = max clock limit >> PWM_DUTY_WIDTH
116+
*/
117+
if (period_ns < pc->min_period_ns)
118+
return -EINVAL;
119+
85120
/*
86121
* Compute the prescaler value for which (1 << PWM_DUTY_WIDTH)
87122
* cycles at the PWM clock rate will take period_ns nanoseconds.
123+
*
124+
* num_channels: If single instance of PWM controller has multiple
125+
* channels (e.g. Tegra210 or older) then it is not possible to
126+
* configure separate clock rates to each of the channels, in such
127+
* case the value stored during probe will be referred.
128+
*
129+
* If every PWM controller instance has one channel respectively, i.e.
130+
* nums_channels == 1 then only the clock rate can be modified
131+
* dynamically (e.g. Tegra186 or Tegra194).
88132
*/
133+
if (pc->soc->num_channels == 1) {
134+
/*
135+
* Rate is multiplied with 2^PWM_DUTY_WIDTH so that it matches
136+
* with the maximum possible rate that the controller can
137+
* provide. Any further lower value can be derived by setting
138+
* PFM bits[0:12].
139+
*
140+
* required_clk_rate is a reference rate for source clock and
141+
* it is derived based on user requested period. By setting the
142+
* source clock rate as required_clk_rate, PWM controller will
143+
* be able to configure the requested period.
144+
*/
145+
required_clk_rate =
146+
(NSEC_PER_SEC / period_ns) << PWM_DUTY_WIDTH;
147+
148+
err = clk_set_rate(pc->clk, required_clk_rate);
149+
if (err < 0)
150+
return -EINVAL;
151+
152+
/* Store the new rate for further references */
153+
pc->clk_rate = clk_get_rate(pc->clk);
154+
}
155+
89156
rate = pc->clk_rate >> PWM_DUTY_WIDTH;
90157

91158
/* Consider precision in PWM_SCALE_WIDTH rate calculation */
@@ -94,7 +161,7 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
94161

95162
/*
96163
* Since the actual PWM divider is the register's frequency divider
97-
* field minus 1, we need to decrement to get the correct value to
164+
* field plus 1, we need to decrement to get the correct value to
98165
* write to the register.
99166
*/
100167
if (rate > 0)
@@ -205,6 +272,10 @@ static int tegra_pwm_probe(struct platform_device *pdev)
205272
*/
206273
pwm->clk_rate = clk_get_rate(pwm->clk);
207274

275+
/* Set minimum limit of PWM period for the IP */
276+
pwm->min_period_ns =
277+
(NSEC_PER_SEC / (pwm->soc->max_frequency >> PWM_DUTY_WIDTH)) + 1;
278+
208279
pwm->rst = devm_reset_control_get_exclusive(&pdev->dev, "pwm");
209280
if (IS_ERR(pwm->rst)) {
210281
ret = PTR_ERR(pwm->rst);
@@ -312,5 +383,6 @@ static struct platform_driver tegra_pwm_driver = {
312383
module_platform_driver(tegra_pwm_driver);
313384

314385
MODULE_LICENSE("GPL");
315-
MODULE_AUTHOR("NVIDIA Corporation");
386+
MODULE_AUTHOR("Sandipan Patra <[email protected]>");
387+
MODULE_DESCRIPTION("Tegra PWM controller driver");
316388
MODULE_ALIAS("platform:tegra-pwm");

0 commit comments

Comments
 (0)