diff options
author | Naidu Tellapati <naidu.tellapati@imgtec.com> | 2015-05-08 17:47:31 -0400 |
---|---|---|
committer | Thierry Reding <thierry.reding@gmail.com> | 2015-05-19 10:07:40 -0400 |
commit | 1e70897d0e20f988abedcf73b33684ecd2be9511 (patch) | |
tree | 36e3625bf964d0a435c84165b1571ac3da669199 | |
parent | b787f68c36d49bb1d9236f403813641efa74a031 (diff) |
pwm: img: Impose upper and lower timebase steps value
The PWM hardware on Pistachio platform has a maximum timebase steps
value to 255. To fix it, let's introduce a compatible-specific
data structure to contain the SoC-specific details and use it to
specify a maximum timebase.
Also, let's limit the minimum timebase to 16 steps, to allow a sane
range of duty cycle steps.
Fixes: 277bb6a29e00 ("pwm: Imagination Technologies PWM DAC driver")
Signed-off-by: Naidu Tellapati <naidu.tellapati@imgtec.com>
Signed-off-by: Ezequiel Garcia <ezequiel.garcia@imgtec.com>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
-rw-r--r-- | drivers/pwm/pwm-img.c | 76 |
1 files changed, 64 insertions, 12 deletions
diff --git a/drivers/pwm/pwm-img.c b/drivers/pwm/pwm-img.c index 476171a768d6..8a029f9bc18c 100644 --- a/drivers/pwm/pwm-img.c +++ b/drivers/pwm/pwm-img.c | |||
@@ -16,6 +16,7 @@ | |||
16 | #include <linux/mfd/syscon.h> | 16 | #include <linux/mfd/syscon.h> |
17 | #include <linux/module.h> | 17 | #include <linux/module.h> |
18 | #include <linux/of.h> | 18 | #include <linux/of.h> |
19 | #include <linux/of_device.h> | ||
19 | #include <linux/platform_device.h> | 20 | #include <linux/platform_device.h> |
20 | #include <linux/pwm.h> | 21 | #include <linux/pwm.h> |
21 | #include <linux/regmap.h> | 22 | #include <linux/regmap.h> |
@@ -38,7 +39,22 @@ | |||
38 | #define PERIP_PWM_PDM_CONTROL_CH_MASK 0x1 | 39 | #define PERIP_PWM_PDM_CONTROL_CH_MASK 0x1 |
39 | #define PERIP_PWM_PDM_CONTROL_CH_SHIFT(ch) ((ch) * 4) | 40 | #define PERIP_PWM_PDM_CONTROL_CH_SHIFT(ch) ((ch) * 4) |
40 | 41 | ||
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 | }; | ||
42 | 58 | ||
43 | struct img_pwm_chip { | 59 | struct img_pwm_chip { |
44 | struct device *dev; | 60 | struct device *dev; |
@@ -47,6 +63,9 @@ struct img_pwm_chip { | |||
47 | struct clk *sys_clk; | 63 | struct clk *sys_clk; |
48 | void __iomem *base; | 64 | void __iomem *base; |
49 | struct regmap *periph_regs; | 65 | struct regmap *periph_regs; |
66 | int max_period_ns; | ||
67 | int min_period_ns; | ||
68 | const struct img_pwm_soc_data *data; | ||
50 | }; | 69 | }; |
51 | 70 | ||
52 | static inline struct img_pwm_chip *to_img_pwm_chip(struct pwm_chip *chip) | 71 | 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, | |||
72 | u32 val, div, duty, timebase; | 91 | u32 val, div, duty, timebase; |
73 | unsigned long mul, output_clk_hz, input_clk_hz; | 92 | unsigned long mul, output_clk_hz, input_clk_hz; |
74 | struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip); | 93 | 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 | } | ||
75 | 101 | ||
76 | input_clk_hz = clk_get_rate(pwm_chip->pwm_clk); | 102 | input_clk_hz = clk_get_rate(pwm_chip->pwm_clk); |
77 | output_clk_hz = DIV_ROUND_UP(NSEC_PER_SEC, period_ns); | 103 | output_clk_hz = DIV_ROUND_UP(NSEC_PER_SEC, period_ns); |
78 | 104 | ||
79 | mul = DIV_ROUND_UP(input_clk_hz, output_clk_hz); | 105 | mul = DIV_ROUND_UP(input_clk_hz, output_clk_hz); |
80 | if (mul <= MAX_TMBASE_STEPS) { | 106 | if (mul <= max_timebase) { |
81 | div = PWM_CTRL_CFG_NO_SUB_DIV; | 107 | div = PWM_CTRL_CFG_NO_SUB_DIV; |
82 | timebase = DIV_ROUND_UP(mul, 1); | 108 | timebase = DIV_ROUND_UP(mul, 1); |
83 | } else if (mul <= MAX_TMBASE_STEPS * 8) { | 109 | } else if (mul <= max_timebase * 8) { |
84 | div = PWM_CTRL_CFG_SUB_DIV0; | 110 | div = PWM_CTRL_CFG_SUB_DIV0; |
85 | timebase = DIV_ROUND_UP(mul, 8); | 111 | timebase = DIV_ROUND_UP(mul, 8); |
86 | } else if (mul <= MAX_TMBASE_STEPS * 64) { | 112 | } else if (mul <= max_timebase * 64) { |
87 | div = PWM_CTRL_CFG_SUB_DIV1; | 113 | div = PWM_CTRL_CFG_SUB_DIV1; |
88 | timebase = DIV_ROUND_UP(mul, 64); | 114 | timebase = DIV_ROUND_UP(mul, 64); |
89 | } else if (mul <= MAX_TMBASE_STEPS * 512) { | 115 | } else if (mul <= max_timebase * 512) { |
90 | div = PWM_CTRL_CFG_SUB_DIV0_DIV1; | 116 | div = PWM_CTRL_CFG_SUB_DIV0_DIV1; |
91 | timebase = DIV_ROUND_UP(mul, 512); | 117 | timebase = DIV_ROUND_UP(mul, 512); |
92 | } else if (mul > MAX_TMBASE_STEPS * 512) { | 118 | } else if (mul > max_timebase * 512) { |
93 | dev_err(chip->dev, | 119 | dev_err(chip->dev, |
94 | "failed to configure timebase steps/divider value\n"); | 120 | "failed to configure timebase steps/divider value\n"); |
95 | return -EINVAL; | 121 | return -EINVAL; |
@@ -143,11 +169,27 @@ static const struct pwm_ops img_pwm_ops = { | |||
143 | .owner = THIS_MODULE, | 169 | .owner = THIS_MODULE, |
144 | }; | 170 | }; |
145 | 171 | ||
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 | |||
146 | static int img_pwm_probe(struct platform_device *pdev) | 185 | static int img_pwm_probe(struct platform_device *pdev) |
147 | { | 186 | { |
148 | int ret; | 187 | int ret; |
188 | u64 val; | ||
189 | unsigned long clk_rate; | ||
149 | struct resource *res; | 190 | struct resource *res; |
150 | struct img_pwm_chip *pwm; | 191 | struct img_pwm_chip *pwm; |
192 | const struct of_device_id *of_dev_id; | ||
151 | 193 | ||
152 | pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL); | 194 | pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL); |
153 | if (!pwm) | 195 | if (!pwm) |
@@ -160,6 +202,11 @@ static int img_pwm_probe(struct platform_device *pdev) | |||
160 | if (IS_ERR(pwm->base)) | 202 | if (IS_ERR(pwm->base)) |
161 | return PTR_ERR(pwm->base); | 203 | return PTR_ERR(pwm->base); |
162 | 204 | ||
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 | |||
163 | pwm->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, | 210 | pwm->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, |
164 | "img,cr-periph"); | 211 | "img,cr-periph"); |
165 | if (IS_ERR(pwm->periph_regs)) | 212 | if (IS_ERR(pwm->periph_regs)) |
@@ -189,6 +236,17 @@ static int img_pwm_probe(struct platform_device *pdev) | |||
189 | goto disable_sysclk; | 236 | goto disable_sysclk; |
190 | } | 237 | } |
191 | 238 | ||
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 | |||
192 | pwm->chip.dev = &pdev->dev; | 250 | pwm->chip.dev = &pdev->dev; |
193 | pwm->chip.ops = &img_pwm_ops; | 251 | pwm->chip.ops = &img_pwm_ops; |
194 | pwm->chip.base = -1; | 252 | pwm->chip.base = -1; |
@@ -228,12 +286,6 @@ static int img_pwm_remove(struct platform_device *pdev) | |||
228 | return pwmchip_remove(&pwm_chip->chip); | 286 | return pwmchip_remove(&pwm_chip->chip); |
229 | } | 287 | } |
230 | 288 | ||
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 | |||
237 | static struct platform_driver img_pwm_driver = { | 289 | static struct platform_driver img_pwm_driver = { |
238 | .driver = { | 290 | .driver = { |
239 | .name = "img-pwm", | 291 | .name = "img-pwm", |