blob: b54daf2917bfdb02501fa67b2241812e4f743e9f [file] [log] [blame]
Naidu Tellapati277bb6a2015-01-09 14:54:47 -03001/*
2 * Imagination Technologies Pulse Width Modulator driver
3 *
4 * Copyright (c) 2014-2015, Imagination Technologies
5 *
6 * Based on drivers/pwm/pwm-tegra.c, Copyright (c) 2010, NVIDIA Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License.
11 */
12
13#include <linux/clk.h>
14#include <linux/err.h>
15#include <linux/io.h>
16#include <linux/mfd/syscon.h>
17#include <linux/module.h>
18#include <linux/of.h>
Naidu Tellapati1e708972015-05-08 18:47:31 -030019#include <linux/of_device.h>
Naidu Tellapati277bb6a2015-01-09 14:54:47 -030020#include <linux/platform_device.h>
21#include <linux/pwm.h>
22#include <linux/regmap.h>
23#include <linux/slab.h>
24
25/* PWM registers */
26#define PWM_CTRL_CFG 0x0000
27#define PWM_CTRL_CFG_NO_SUB_DIV 0
28#define PWM_CTRL_CFG_SUB_DIV0 1
29#define PWM_CTRL_CFG_SUB_DIV1 2
30#define PWM_CTRL_CFG_SUB_DIV0_DIV1 3
31#define PWM_CTRL_CFG_DIV_SHIFT(ch) ((ch) * 2 + 4)
32#define PWM_CTRL_CFG_DIV_MASK 0x3
33
34#define PWM_CH_CFG(ch) (0x4 + (ch) * 4)
35#define PWM_CH_CFG_TMBASE_SHIFT 0
36#define PWM_CH_CFG_DUTY_SHIFT 16
37
38#define PERIP_PWM_PDM_CONTROL 0x0140
39#define PERIP_PWM_PDM_CONTROL_CH_MASK 0x1
40#define PERIP_PWM_PDM_CONTROL_CH_SHIFT(ch) ((ch) * 4)
41
Naidu Tellapati1e708972015-05-08 18:47:31 -030042/*
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
Ed Blakea18afce2017-10-02 10:51:47 +010055#define IMG_PWM_NPWM 4
56
Naidu Tellapati1e708972015-05-08 18:47:31 -030057struct img_pwm_soc_data {
58 u32 max_timebase;
59};
Naidu Tellapati277bb6a2015-01-09 14:54:47 -030060
61struct img_pwm_chip {
62 struct device *dev;
63 struct pwm_chip chip;
64 struct clk *pwm_clk;
65 struct clk *sys_clk;
66 void __iomem *base;
67 struct regmap *periph_regs;
Naidu Tellapati1e708972015-05-08 18:47:31 -030068 int max_period_ns;
69 int min_period_ns;
70 const struct img_pwm_soc_data *data;
Ed Blakea18afce2017-10-02 10:51:47 +010071 u32 suspend_ctrl_cfg;
72 u32 suspend_ch_cfg[IMG_PWM_NPWM];
Naidu Tellapati277bb6a2015-01-09 14:54:47 -030073};
74
75static inline struct img_pwm_chip *to_img_pwm_chip(struct pwm_chip *chip)
76{
77 return container_of(chip, struct img_pwm_chip, chip);
78}
79
80static inline void img_pwm_writel(struct img_pwm_chip *chip,
81 u32 reg, u32 val)
82{
83 writel(val, chip->base + reg);
84}
85
86static inline u32 img_pwm_readl(struct img_pwm_chip *chip,
87 u32 reg)
88{
89 return readl(chip->base + reg);
90}
91
92static int img_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
93 int duty_ns, int period_ns)
94{
95 u32 val, div, duty, timebase;
96 unsigned long mul, output_clk_hz, input_clk_hz;
97 struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
Naidu Tellapati1e708972015-05-08 18:47:31 -030098 unsigned int max_timebase = pwm_chip->data->max_timebase;
99
100 if (period_ns < pwm_chip->min_period_ns ||
101 period_ns > pwm_chip->max_period_ns) {
102 dev_err(chip->dev, "configured period not in range\n");
103 return -ERANGE;
104 }
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300105
106 input_clk_hz = clk_get_rate(pwm_chip->pwm_clk);
107 output_clk_hz = DIV_ROUND_UP(NSEC_PER_SEC, period_ns);
108
109 mul = DIV_ROUND_UP(input_clk_hz, output_clk_hz);
Naidu Tellapati1e708972015-05-08 18:47:31 -0300110 if (mul <= max_timebase) {
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300111 div = PWM_CTRL_CFG_NO_SUB_DIV;
112 timebase = DIV_ROUND_UP(mul, 1);
Naidu Tellapati1e708972015-05-08 18:47:31 -0300113 } else if (mul <= max_timebase * 8) {
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300114 div = PWM_CTRL_CFG_SUB_DIV0;
115 timebase = DIV_ROUND_UP(mul, 8);
Naidu Tellapati1e708972015-05-08 18:47:31 -0300116 } else if (mul <= max_timebase * 64) {
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300117 div = PWM_CTRL_CFG_SUB_DIV1;
118 timebase = DIV_ROUND_UP(mul, 64);
Naidu Tellapati1e708972015-05-08 18:47:31 -0300119 } else if (mul <= max_timebase * 512) {
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300120 div = PWM_CTRL_CFG_SUB_DIV0_DIV1;
121 timebase = DIV_ROUND_UP(mul, 512);
Naidu Tellapati1e708972015-05-08 18:47:31 -0300122 } else if (mul > max_timebase * 512) {
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300123 dev_err(chip->dev,
124 "failed to configure timebase steps/divider value\n");
125 return -EINVAL;
126 }
127
128 duty = DIV_ROUND_UP(timebase * duty_ns, period_ns);
129
130 val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
131 val &= ~(PWM_CTRL_CFG_DIV_MASK << PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm));
132 val |= (div & PWM_CTRL_CFG_DIV_MASK) <<
133 PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm);
134 img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
135
136 val = (duty << PWM_CH_CFG_DUTY_SHIFT) |
137 (timebase << PWM_CH_CFG_TMBASE_SHIFT);
138 img_pwm_writel(pwm_chip, PWM_CH_CFG(pwm->hwpwm), val);
139
140 return 0;
141}
142
143static int img_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
144{
145 u32 val;
146 struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
147
148 val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
149 val |= BIT(pwm->hwpwm);
150 img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
151
152 regmap_update_bits(pwm_chip->periph_regs, PERIP_PWM_PDM_CONTROL,
153 PERIP_PWM_PDM_CONTROL_CH_MASK <<
154 PERIP_PWM_PDM_CONTROL_CH_SHIFT(pwm->hwpwm), 0);
155
156 return 0;
157}
158
159static void img_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
160{
161 u32 val;
162 struct img_pwm_chip *pwm_chip = to_img_pwm_chip(chip);
163
164 val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
165 val &= ~BIT(pwm->hwpwm);
166 img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
167}
168
169static const struct pwm_ops img_pwm_ops = {
170 .config = img_pwm_config,
171 .enable = img_pwm_enable,
172 .disable = img_pwm_disable,
173 .owner = THIS_MODULE,
174};
175
Naidu Tellapati1e708972015-05-08 18:47:31 -0300176static const struct img_pwm_soc_data pistachio_pwm = {
177 .max_timebase = 255,
178};
179
180static const struct of_device_id img_pwm_of_match[] = {
181 {
182 .compatible = "img,pistachio-pwm",
183 .data = &pistachio_pwm,
184 },
185 { }
186};
187MODULE_DEVICE_TABLE(of, img_pwm_of_match);
188
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300189static int img_pwm_probe(struct platform_device *pdev)
190{
191 int ret;
Naidu Tellapati1e708972015-05-08 18:47:31 -0300192 u64 val;
193 unsigned long clk_rate;
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300194 struct resource *res;
195 struct img_pwm_chip *pwm;
Naidu Tellapati1e708972015-05-08 18:47:31 -0300196 const struct of_device_id *of_dev_id;
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300197
198 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
199 if (!pwm)
200 return -ENOMEM;
201
202 pwm->dev = &pdev->dev;
203
204 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
205 pwm->base = devm_ioremap_resource(&pdev->dev, res);
206 if (IS_ERR(pwm->base))
207 return PTR_ERR(pwm->base);
208
Naidu Tellapati1e708972015-05-08 18:47:31 -0300209 of_dev_id = of_match_device(img_pwm_of_match, &pdev->dev);
210 if (!of_dev_id)
211 return -ENODEV;
212 pwm->data = of_dev_id->data;
213
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300214 pwm->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
215 "img,cr-periph");
216 if (IS_ERR(pwm->periph_regs))
217 return PTR_ERR(pwm->periph_regs);
218
219 pwm->sys_clk = devm_clk_get(&pdev->dev, "sys");
220 if (IS_ERR(pwm->sys_clk)) {
221 dev_err(&pdev->dev, "failed to get system clock\n");
222 return PTR_ERR(pwm->sys_clk);
223 }
224
225 pwm->pwm_clk = devm_clk_get(&pdev->dev, "pwm");
226 if (IS_ERR(pwm->pwm_clk)) {
227 dev_err(&pdev->dev, "failed to get pwm clock\n");
228 return PTR_ERR(pwm->pwm_clk);
229 }
230
231 ret = clk_prepare_enable(pwm->sys_clk);
232 if (ret < 0) {
233 dev_err(&pdev->dev, "could not prepare or enable sys clock\n");
234 return ret;
235 }
236
237 ret = clk_prepare_enable(pwm->pwm_clk);
238 if (ret < 0) {
239 dev_err(&pdev->dev, "could not prepare or enable pwm clock\n");
240 goto disable_sysclk;
241 }
242
Naidu Tellapati1e708972015-05-08 18:47:31 -0300243 clk_rate = clk_get_rate(pwm->pwm_clk);
Wolfram Sangbea307c2016-03-02 23:33:34 +0100244 if (!clk_rate) {
245 dev_err(&pdev->dev, "pwm clock has no frequency\n");
246 ret = -EINVAL;
247 goto disable_pwmclk;
248 }
Naidu Tellapati1e708972015-05-08 18:47:31 -0300249
250 /* The maximum input clock divider is 512 */
251 val = (u64)NSEC_PER_SEC * 512 * pwm->data->max_timebase;
252 do_div(val, clk_rate);
253 pwm->max_period_ns = val;
254
255 val = (u64)NSEC_PER_SEC * MIN_TMBASE_STEPS;
256 do_div(val, clk_rate);
257 pwm->min_period_ns = val;
258
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300259 pwm->chip.dev = &pdev->dev;
260 pwm->chip.ops = &img_pwm_ops;
261 pwm->chip.base = -1;
Ed Blakea18afce2017-10-02 10:51:47 +0100262 pwm->chip.npwm = IMG_PWM_NPWM;
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300263
264 ret = pwmchip_add(&pwm->chip);
265 if (ret < 0) {
266 dev_err(&pdev->dev, "pwmchip_add failed: %d\n", ret);
267 goto disable_pwmclk;
268 }
269
270 platform_set_drvdata(pdev, pwm);
271 return 0;
272
273disable_pwmclk:
274 clk_disable_unprepare(pwm->pwm_clk);
275disable_sysclk:
276 clk_disable_unprepare(pwm->sys_clk);
277 return ret;
278}
279
280static int img_pwm_remove(struct platform_device *pdev)
281{
282 struct img_pwm_chip *pwm_chip = platform_get_drvdata(pdev);
283 u32 val;
284 unsigned int i;
285
286 for (i = 0; i < pwm_chip->chip.npwm; i++) {
287 val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
288 val &= ~BIT(i);
289 img_pwm_writel(pwm_chip, PWM_CTRL_CFG, val);
290 }
291
292 clk_disable_unprepare(pwm_chip->pwm_clk);
293 clk_disable_unprepare(pwm_chip->sys_clk);
294
295 return pwmchip_remove(&pwm_chip->chip);
296}
297
Ed Blakea18afce2017-10-02 10:51:47 +0100298#ifdef CONFIG_PM_SLEEP
299static int img_pwm_suspend(struct device *dev)
300{
301 struct platform_device *pdev = to_platform_device(dev);
302 struct img_pwm_chip *pwm_chip = platform_get_drvdata(pdev);
303 int i;
304
305 for (i = 0; i < pwm_chip->chip.npwm; i++)
306 pwm_chip->suspend_ch_cfg[i] = img_pwm_readl(pwm_chip,
307 PWM_CH_CFG(i));
308
309 pwm_chip->suspend_ctrl_cfg = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
310
311 clk_disable_unprepare(pwm_chip->pwm_clk);
312 clk_disable_unprepare(pwm_chip->sys_clk);
313
314 return 0;
315}
316
317static int img_pwm_resume(struct device *dev)
318{
319 struct platform_device *pdev = to_platform_device(dev);
320 struct img_pwm_chip *pwm_chip = platform_get_drvdata(pdev);
321 int ret;
322 int i;
323
324 ret = clk_prepare_enable(pwm_chip->sys_clk);
325 if (ret < 0) {
326 dev_err(&pdev->dev, "could not prepare or enable sys clock\n");
327 return ret;
328 }
329
330 ret = clk_prepare_enable(pwm_chip->pwm_clk);
331 if (ret < 0) {
332 dev_err(&pdev->dev, "could not prepare or enable pwm clock\n");
333 clk_disable_unprepare(pwm_chip->sys_clk);
334 return ret;
335 }
336
337 for (i = 0; i < pwm_chip->chip.npwm; i++)
338 img_pwm_writel(pwm_chip, PWM_CH_CFG(i),
339 pwm_chip->suspend_ch_cfg[i]);
340
341 img_pwm_writel(pwm_chip, PWM_CTRL_CFG, pwm_chip->suspend_ctrl_cfg);
342
343 for (i = 0; i < pwm_chip->chip.npwm; i++)
344 if (pwm_chip->suspend_ctrl_cfg & BIT(i))
345 regmap_update_bits(pwm_chip->periph_regs,
346 PERIP_PWM_PDM_CONTROL,
347 PERIP_PWM_PDM_CONTROL_CH_MASK <<
348 PERIP_PWM_PDM_CONTROL_CH_SHIFT(i),
349 0);
350
351 return 0;
352}
353#endif /* CONFIG_PM */
354
355SIMPLE_DEV_PM_OPS(img_pwm_pm_ops, img_pwm_suspend, img_pwm_resume);
356
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300357static struct platform_driver img_pwm_driver = {
358 .driver = {
359 .name = "img-pwm",
Ed Blakea18afce2017-10-02 10:51:47 +0100360 .pm = &img_pwm_pm_ops,
Naidu Tellapati277bb6a2015-01-09 14:54:47 -0300361 .of_match_table = img_pwm_of_match,
362 },
363 .probe = img_pwm_probe,
364 .remove = img_pwm_remove,
365};
366module_platform_driver(img_pwm_driver);
367
368MODULE_AUTHOR("Sai Masarapu <Sai.Masarapu@imgtec.com>");
369MODULE_DESCRIPTION("Imagination Technologies PWM DAC driver");
370MODULE_LICENSE("GPL v2");