blob: 3cbb32d8064f7eb2f782e8030deb7bad408523fd [file] [log] [blame]
Thierry Reding0134b932011-12-21 07:47:07 +01001/*
2 * drivers/pwm/pwm-tegra.c
3 *
4 * Tegra pulse-width-modulation controller driver
5 *
6 * Copyright (c) 2010, NVIDIA Corporation.
7 * Based on arch/arm/plat-mxc/pwm.c by Sascha Hauer <s.hauer@pengutronix.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 */
23
24#include <linux/clk.h>
25#include <linux/err.h>
26#include <linux/io.h>
27#include <linux/module.h>
28#include <linux/of.h>
29#include <linux/pwm.h>
30#include <linux/platform_device.h>
31#include <linux/slab.h>
32
33#define PWM_ENABLE (1 << 31)
34#define PWM_DUTY_WIDTH 8
35#define PWM_DUTY_SHIFT 16
36#define PWM_SCALE_WIDTH 13
37#define PWM_SCALE_SHIFT 0
38
Thierry Reding0134b932011-12-21 07:47:07 +010039struct tegra_pwm_chip {
Thierry Redinge17c0b22016-07-11 11:26:52 +020040 struct pwm_chip chip;
41 struct device *dev;
Thierry Reding0134b932011-12-21 07:47:07 +010042
Thierry Redinge17c0b22016-07-11 11:26:52 +020043 struct clk *clk;
Thierry Reding0134b932011-12-21 07:47:07 +010044
Thierry Reding4f57f5a2016-07-11 11:27:29 +020045 void __iomem *regs;
Thierry Reding0134b932011-12-21 07:47:07 +010046};
47
48static inline struct tegra_pwm_chip *to_tegra_pwm_chip(struct pwm_chip *chip)
49{
50 return container_of(chip, struct tegra_pwm_chip, chip);
51}
52
53static inline u32 pwm_readl(struct tegra_pwm_chip *chip, unsigned int num)
54{
Thierry Reding4f57f5a2016-07-11 11:27:29 +020055 return readl(chip->regs + (num << 4));
Thierry Reding0134b932011-12-21 07:47:07 +010056}
57
58static inline void pwm_writel(struct tegra_pwm_chip *chip, unsigned int num,
59 unsigned long val)
60{
Thierry Reding4f57f5a2016-07-11 11:27:29 +020061 writel(val, chip->regs + (num << 4));
Thierry Reding0134b932011-12-21 07:47:07 +010062}
63
64static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
65 int duty_ns, int period_ns)
66{
67 struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
68 unsigned long long c;
69 unsigned long rate, hz;
70 u32 val = 0;
71 int err;
72
73 /*
74 * Convert from duty_ns / period_ns to a fixed number of duty ticks
75 * per (1 << PWM_DUTY_WIDTH) cycles and make sure to round to the
76 * nearest integer during division.
77 */
78 c = duty_ns * ((1 << PWM_DUTY_WIDTH) - 1) + period_ns / 2;
79 do_div(c, period_ns);
80
81 val = (u32)c << PWM_DUTY_SHIFT;
82
83 /*
84 * Compute the prescaler value for which (1 << PWM_DUTY_WIDTH)
85 * cycles at the PWM clock rate will take period_ns nanoseconds.
86 */
87 rate = clk_get_rate(pc->clk) >> PWM_DUTY_WIDTH;
Thierry Redingb65af272015-02-18 08:40:29 +010088 hz = NSEC_PER_SEC / period_ns;
Thierry Reding0134b932011-12-21 07:47:07 +010089
90 rate = (rate + (hz / 2)) / hz;
91
92 /*
93 * Since the actual PWM divider is the register's frequency divider
94 * field minus 1, we need to decrement to get the correct value to
95 * write to the register.
96 */
97 if (rate > 0)
98 rate--;
99
100 /*
101 * Make sure that the rate will fit in the register's frequency
102 * divider field.
103 */
104 if (rate >> PWM_SCALE_WIDTH)
105 return -EINVAL;
106
107 val |= rate << PWM_SCALE_SHIFT;
108
109 /*
110 * If the PWM channel is disabled, make sure to turn on the clock
111 * before writing the register. Otherwise, keep it enabled.
112 */
Boris Brezillon5c312522015-07-01 10:21:47 +0200113 if (!pwm_is_enabled(pwm)) {
Thierry Reding0134b932011-12-21 07:47:07 +0100114 err = clk_prepare_enable(pc->clk);
115 if (err < 0)
116 return err;
117 } else
118 val |= PWM_ENABLE;
119
120 pwm_writel(pc, pwm->hwpwm, val);
121
122 /*
123 * If the PWM is not enabled, turn the clock off again to save power.
124 */
Boris Brezillon5c312522015-07-01 10:21:47 +0200125 if (!pwm_is_enabled(pwm))
Thierry Reding0134b932011-12-21 07:47:07 +0100126 clk_disable_unprepare(pc->clk);
127
128 return 0;
129}
130
131static int tegra_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
132{
133 struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
134 int rc = 0;
135 u32 val;
136
137 rc = clk_prepare_enable(pc->clk);
138 if (rc < 0)
139 return rc;
140
141 val = pwm_readl(pc, pwm->hwpwm);
142 val |= PWM_ENABLE;
143 pwm_writel(pc, pwm->hwpwm, val);
144
145 return 0;
146}
147
148static void tegra_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
149{
150 struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
151 u32 val;
152
153 val = pwm_readl(pc, pwm->hwpwm);
154 val &= ~PWM_ENABLE;
155 pwm_writel(pc, pwm->hwpwm, val);
156
157 clk_disable_unprepare(pc->clk);
158}
159
160static const struct pwm_ops tegra_pwm_ops = {
161 .config = tegra_pwm_config,
162 .enable = tegra_pwm_enable,
163 .disable = tegra_pwm_disable,
164 .owner = THIS_MODULE,
165};
166
167static int tegra_pwm_probe(struct platform_device *pdev)
168{
169 struct tegra_pwm_chip *pwm;
170 struct resource *r;
171 int ret;
172
173 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
Jingoo Han474b6902014-04-23 18:41:10 +0900174 if (!pwm)
Thierry Reding0134b932011-12-21 07:47:07 +0100175 return -ENOMEM;
Thierry Reding0134b932011-12-21 07:47:07 +0100176
177 pwm->dev = &pdev->dev;
178
179 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding4f57f5a2016-07-11 11:27:29 +0200180 pwm->regs = devm_ioremap_resource(&pdev->dev, r);
181 if (IS_ERR(pwm->regs))
182 return PTR_ERR(pwm->regs);
Thierry Reding0134b932011-12-21 07:47:07 +0100183
184 platform_set_drvdata(pdev, pwm);
185
Axel Lin0c8f5272012-07-01 13:00:51 +0800186 pwm->clk = devm_clk_get(&pdev->dev, NULL);
Thierry Reding0134b932011-12-21 07:47:07 +0100187 if (IS_ERR(pwm->clk))
188 return PTR_ERR(pwm->clk);
189
190 pwm->chip.dev = &pdev->dev;
191 pwm->chip.ops = &tegra_pwm_ops;
192 pwm->chip.base = -1;
Thierry Redingc009c562016-07-11 11:08:29 +0200193 pwm->chip.npwm = 4;
Thierry Reding0134b932011-12-21 07:47:07 +0100194
195 ret = pwmchip_add(&pwm->chip);
196 if (ret < 0) {
197 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
Thierry Reding0134b932011-12-21 07:47:07 +0100198 return ret;
199 }
200
201 return 0;
202}
203
Bill Pemberton77f37912012-11-19 13:26:09 -0500204static int tegra_pwm_remove(struct platform_device *pdev)
Thierry Reding0134b932011-12-21 07:47:07 +0100205{
206 struct tegra_pwm_chip *pc = platform_get_drvdata(pdev);
Thierry Redingc009c562016-07-11 11:08:29 +0200207 unsigned int i;
Thierry Reding0134b932011-12-21 07:47:07 +0100208
209 if (WARN_ON(!pc))
210 return -ENODEV;
211
Thierry Redingc009c562016-07-11 11:08:29 +0200212 for (i = 0; i < pc->chip.npwm; i++) {
Thierry Reding0134b932011-12-21 07:47:07 +0100213 struct pwm_device *pwm = &pc->chip.pwms[i];
214
Boris Brezillon5c312522015-07-01 10:21:47 +0200215 if (!pwm_is_enabled(pwm))
Thierry Reding0134b932011-12-21 07:47:07 +0100216 if (clk_prepare_enable(pc->clk) < 0)
217 continue;
218
219 pwm_writel(pc, i, 0);
220
221 clk_disable_unprepare(pc->clk);
222 }
223
Axel Lin0c8f5272012-07-01 13:00:51 +0800224 return pwmchip_remove(&pc->chip);
Thierry Reding0134b932011-12-21 07:47:07 +0100225}
226
Thierry Redingf1a88702013-04-18 10:04:14 +0200227static const struct of_device_id tegra_pwm_of_match[] = {
Thierry Reding140fd972011-12-21 08:04:13 +0100228 { .compatible = "nvidia,tegra20-pwm" },
229 { .compatible = "nvidia,tegra30-pwm" },
230 { }
231};
232
233MODULE_DEVICE_TABLE(of, tegra_pwm_of_match);
Thierry Reding140fd972011-12-21 08:04:13 +0100234
Thierry Reding0134b932011-12-21 07:47:07 +0100235static struct platform_driver tegra_pwm_driver = {
236 .driver = {
237 .name = "tegra-pwm",
Stephen Warren838bf092013-02-15 15:02:22 -0700238 .of_match_table = tegra_pwm_of_match,
Thierry Reding0134b932011-12-21 07:47:07 +0100239 },
240 .probe = tegra_pwm_probe,
Bill Pembertonfd109112012-11-19 13:21:28 -0500241 .remove = tegra_pwm_remove,
Thierry Reding0134b932011-12-21 07:47:07 +0100242};
243
244module_platform_driver(tegra_pwm_driver);
245
246MODULE_LICENSE("GPL");
247MODULE_AUTHOR("NVIDIA Corporation");
248MODULE_ALIAS("platform:tegra-pwm");