Skip to content

Commit 68465bb

Browse files
committed
Merge tag 'pwm/for-4.1-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry.reding/linux-pwm
Pull pwm fix from Thierry Reding: "A single fix to make the Pistachio driver respect the limits imposed by hardware" * tag 'pwm/for-4.1-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry.reding/linux-pwm: pwm: img: Impose upper and lower timebase steps value
2 parents 1173ff0 + 1e70897 commit 68465bb

File tree

1 file changed

+64
-12
lines changed

1 file changed

+64
-12
lines changed

drivers/pwm/pwm-img.c

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/mfd/syscon.h>
1717
#include <linux/module.h>
1818
#include <linux/of.h>
19+
#include <linux/of_device.h>
1920
#include <linux/platform_device.h>
2021
#include <linux/pwm.h>
2122
#include <linux/regmap.h>
@@ -38,7 +39,22 @@
3839
#define PERIP_PWM_PDM_CONTROL_CH_MASK 0x1
3940
#define PERIP_PWM_PDM_CONTROL_CH_SHIFT(ch) ((ch) * 4)
4041

41-
#define MAX_TMBASE_STEPS 65536
42+
/*
43+
* PWM period is specified with a timebase register,
44+
* in number of step periods. The PWM duty cycle is also
45+
* specified in step periods, in the [0, $timebase] range.
46+
* In other words, the timebase imposes the duty cycle
47+
* resolution. Therefore, let's constraint the timebase to
48+
* a minimum value to allow a sane range of duty cycle values.
49+
* Imposing a minimum timebase, will impose a maximum PWM frequency.
50+
*
51+
* The value chosen is completely arbitrary.
52+
*/
53+
#define MIN_TMBASE_STEPS 16
54+
55+
struct img_pwm_soc_data {
56+
u32 max_timebase;
57+
};
4258

4359
struct img_pwm_chip {
4460
struct device *dev;
@@ -47,6 +63,9 @@ struct img_pwm_chip {
4763
struct clk *sys_clk;
4864
void __iomem *base;
4965
struct regmap *periph_regs;
66+
int max_period_ns;
67+
int min_period_ns;
68+
const struct img_pwm_soc_data *data;
5069
};
5170

5271
static inline struct img_pwm_chip *to_img_pwm_chip(struct pwm_chip *chip)
@@ -72,24 +91,31 @@ static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
7291
u32 val, div, duty, timebase;
7392
unsigned long mul, output_clk_hz, input_clk_hz;
7493
struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
94+
unsigned int max_timebase = pwm_chip->data->max_timebase;
95+
96+
if (period_ns < pwm_chip->min_period_ns ||
97+
period_ns > pwm_chip->max_period_ns) {
98+
dev_err(chip->dev, "configured period not in range\n");
99+
return -ERANGE;
100+
}
75101

76102
input_clk_hz = clk_get_rate(pwm_chip->pwm_clk);
77103
output_clk_hz = DIV_ROUND_UP(NSEC_PER_SEC, period_ns);
78104

79105
mul = DIV_ROUND_UP(input_clk_hz, output_clk_hz);
80-
if (mul <= MAX_TMBASE_STEPS) {
106+
if (mul <= max_timebase) {
81107
div = PWM_CTRL_CFG_NO_SUB_DIV;
82108
timebase = DIV_ROUND_UP(mul, 1);
83-
} else if (mul <= MAX_TMBASE_STEPS * 8) {
109+
} else if (mul <= max_timebase * 8) {
84110
div = PWM_CTRL_CFG_SUB_DIV0;
85111
timebase = DIV_ROUND_UP(mul, 8);
86-
} else if (mul <= MAX_TMBASE_STEPS * 64) {
112+
} else if (mul <= max_timebase * 64) {
87113
div = PWM_CTRL_CFG_SUB_DIV1;
88114
timebase = DIV_ROUND_UP(mul, 64);
89-
} else if (mul <= MAX_TMBASE_STEPS * 512) {
115+
} else if (mul <= max_timebase * 512) {
90116
div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
91117
timebase = DIV_ROUND_UP(mul, 512);
92-
} else if (mul > MAX_TMBASE_STEPS * 512) {
118+
} else if (mul > max_timebase * 512) {
93119
dev_err(chip->dev,
94120
"failed to configure timebase steps/divider value\n");
95121
return -EINVAL;
@@ -143,11 +169,27 @@ static const struct pwm_ops img_pwm_ops = {
143169
.owner = THIS_MODULE,
144170
};
145171

172+
static const struct img_pwm_soc_data pistachio_pwm = {
173+
.max_timebase = 255,
174+
};
175+
176+
static const struct of_device_id img_pwm_of_match[] = {
177+
{
178+
.compatible = "img,pistachio-pwm",
179+
.data = &pistachio_pwm,
180+
},
181+
{ }
182+
};
183+
MODULE_DEVICE_TABLE(of, img_pwm_of_match);
184+
146185
static int img_pwm_probe(struct platform_device *pdev)
147186
{
148187
int ret;
188+
u64 val;
189+
unsigned long clk_rate;
149190
struct resource *res;
150191
struct img_pwm_chip *pwm;
192+
const struct of_device_id *of_dev_id;
151193

152194
pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
153195
if (!pwm)
@@ -160,6 +202,11 @@ static int img_pwm_probe(struct platform_device *pdev)
160202
if (IS_ERR(pwm->base))
161203
return PTR_ERR(pwm->base);
162204

205+
of_dev_id = of_match_device(img_pwm_of_match, &pdev->dev);
206+
if (!of_dev_id)
207+
return -ENODEV;
208+
pwm->data = of_dev_id->data;
209+
163210
pwm->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
164211
"img,cr-periph");
165212
if (IS_ERR(pwm->periph_regs))
@@ -189,6 +236,17 @@ static int img_pwm_probe(struct platform_device *pdev)
189236
goto disable_sysclk;
190237
}
191238

239+
clk_rate = clk_get_rate(pwm->pwm_clk);
240+
241+
/* The maximum input clock divider is 512 */
242+
val = (u64)NSEC_PER_SEC * 512 * pwm->data->max_timebase;
243+
do_div(val, clk_rate);
244+
pwm->max_period_ns = val;
245+
246+
val = (u64)NSEC_PER_SEC * MIN_TMBASE_STEPS;
247+
do_div(val, clk_rate);
248+
pwm->min_period_ns = val;
249+
192250
pwm->chip.dev = &pdev->dev;
193251
pwm->chip.ops = &img_pwm_ops;
194252
pwm->chip.base = -1;
@@ -228,12 +286,6 @@ static int img_pwm_remove(struct platform_device *pdev)
228286
return pwmchip_remove(&pwm_chip->chip);
229287
}
230288

231-
static const struct of_device_id img_pwm_of_match[] = {
232-
{ .compatible = "img,pistachio-pwm", },
233-
{ }
234-
};
235-
MODULE_DEVICE_TABLE(of, img_pwm_of_match);
236-
237289
static struct platform_driver img_pwm_driver = {
238290
.driver = {
239291
.name = "img-pwm",

0 commit comments

Comments
 (0)