blob: 247156149b448b214d075930dd7a42cab039eb76 [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>
Rohith Seelaboyina5dfbd2b2016-06-22 17:17:19 +053032#include <linux/reset.h>
Thierry Reding0134b932011-12-21 07:47:07 +010033
34#define PWM_ENABLE (1 << 31)
35#define PWM_DUTY_WIDTH 8
36#define PWM_DUTY_SHIFT 16
37#define PWM_SCALE_WIDTH 13
38#define PWM_SCALE_SHIFT 0
39
Thierry Reding0134b932011-12-21 07:47:07 +010040struct tegra_pwm_chip {
Thierry Redinge17c0b22016-07-11 11:26:52 +020041 struct pwm_chip chip;
42 struct device *dev;
Thierry Reding0134b932011-12-21 07:47:07 +010043
Thierry Redinge17c0b22016-07-11 11:26:52 +020044 struct clk *clk;
Rohith Seelaboyina5dfbd2b2016-06-22 17:17:19 +053045 struct reset_control*rst;
Thierry Reding0134b932011-12-21 07:47:07 +010046
Thierry Reding4f57f5a2016-07-11 11:27:29 +020047 void __iomem *regs;
Thierry Reding0134b932011-12-21 07:47:07 +010048};
49
50static inline struct tegra_pwm_chip *to_tegra_pwm_chip(struct pwm_chip *chip)
51{
52 return container_of(chip, struct tegra_pwm_chip, chip);
53}
54
55static inline u32 pwm_readl(struct tegra_pwm_chip *chip, unsigned int num)
56{
Thierry Reding4f57f5a2016-07-11 11:27:29 +020057 return readl(chip->regs + (num << 4));
Thierry Reding0134b932011-12-21 07:47:07 +010058}
59
60static inline void pwm_writel(struct tegra_pwm_chip *chip, unsigned int num,
61 unsigned long val)
62{
Thierry Reding4f57f5a2016-07-11 11:27:29 +020063 writel(val, chip->regs + (num << 4));
Thierry Reding0134b932011-12-21 07:47:07 +010064}
65
66static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
67 int duty_ns, int period_ns)
68{
69 struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
Hyong Bin Kimb979ed52016-06-22 17:17:21 +053070 unsigned long long c = duty_ns;
Thierry Reding0134b932011-12-21 07:47:07 +010071 unsigned long rate, hz;
72 u32 val = 0;
73 int err;
74
75 /*
76 * Convert from duty_ns / period_ns to a fixed number of duty ticks
77 * per (1 << PWM_DUTY_WIDTH) cycles and make sure to round to the
78 * nearest integer during division.
79 */
Hyong Bin Kimb979ed52016-06-22 17:17:21 +053080 c *= (1 << PWM_DUTY_WIDTH);
81 c += period_ns / 2;
Thierry Reding0134b932011-12-21 07:47:07 +010082 do_div(c, period_ns);
83
84 val = (u32)c << PWM_DUTY_SHIFT;
85
86 /*
87 * Compute the prescaler value for which (1 << PWM_DUTY_WIDTH)
88 * cycles at the PWM clock rate will take period_ns nanoseconds.
89 */
90 rate = clk_get_rate(pc->clk) >> PWM_DUTY_WIDTH;
Thierry Redingb65af272015-02-18 08:40:29 +010091 hz = NSEC_PER_SEC / period_ns;
Thierry Reding0134b932011-12-21 07:47:07 +010092
93 rate = (rate + (hz / 2)) / hz;
94
95 /*
96 * 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
98 * write to the register.
99 */
100 if (rate > 0)
101 rate--;
102
103 /*
104 * Make sure that the rate will fit in the register's frequency
105 * divider field.
106 */
107 if (rate >> PWM_SCALE_WIDTH)
108 return -EINVAL;
109
110 val |= rate << PWM_SCALE_SHIFT;
111
112 /*
113 * If the PWM channel is disabled, make sure to turn on the clock
114 * before writing the register. Otherwise, keep it enabled.
115 */
Boris Brezillon5c312522015-07-01 10:21:47 +0200116 if (!pwm_is_enabled(pwm)) {
Thierry Reding0134b932011-12-21 07:47:07 +0100117 err = clk_prepare_enable(pc->clk);
118 if (err < 0)
119 return err;
120 } else
121 val |= PWM_ENABLE;
122
123 pwm_writel(pc, pwm->hwpwm, val);
124
125 /*
126 * If the PWM is not enabled, turn the clock off again to save power.
127 */
Boris Brezillon5c312522015-07-01 10:21:47 +0200128 if (!pwm_is_enabled(pwm))
Thierry Reding0134b932011-12-21 07:47:07 +0100129 clk_disable_unprepare(pc->clk);
130
131 return 0;
132}
133
134static int tegra_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
135{
136 struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
137 int rc = 0;
138 u32 val;
139
140 rc = clk_prepare_enable(pc->clk);
141 if (rc < 0)
142 return rc;
143
144 val = pwm_readl(pc, pwm->hwpwm);
145 val |= PWM_ENABLE;
146 pwm_writel(pc, pwm->hwpwm, val);
147
148 return 0;
149}
150
151static void tegra_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
152{
153 struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
154 u32 val;
155
156 val = pwm_readl(pc, pwm->hwpwm);
157 val &= ~PWM_ENABLE;
158 pwm_writel(pc, pwm->hwpwm, val);
159
160 clk_disable_unprepare(pc->clk);
161}
162
163static const struct pwm_ops tegra_pwm_ops = {
164 .config = tegra_pwm_config,
165 .enable = tegra_pwm_enable,
166 .disable = tegra_pwm_disable,
167 .owner = THIS_MODULE,
168};
169
170static int tegra_pwm_probe(struct platform_device *pdev)
171{
172 struct tegra_pwm_chip *pwm;
173 struct resource *r;
174 int ret;
175
176 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
Jingoo Han474b6902014-04-23 18:41:10 +0900177 if (!pwm)
Thierry Reding0134b932011-12-21 07:47:07 +0100178 return -ENOMEM;
Thierry Reding0134b932011-12-21 07:47:07 +0100179
180 pwm->dev = &pdev->dev;
181
182 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding4f57f5a2016-07-11 11:27:29 +0200183 pwm->regs = devm_ioremap_resource(&pdev->dev, r);
184 if (IS_ERR(pwm->regs))
185 return PTR_ERR(pwm->regs);
Thierry Reding0134b932011-12-21 07:47:07 +0100186
187 platform_set_drvdata(pdev, pwm);
188
Axel Lin0c8f5272012-07-01 13:00:51 +0800189 pwm->clk = devm_clk_get(&pdev->dev, NULL);
Thierry Reding0134b932011-12-21 07:47:07 +0100190 if (IS_ERR(pwm->clk))
191 return PTR_ERR(pwm->clk);
192
Rohith Seelaboyina5dfbd2b2016-06-22 17:17:19 +0530193 pwm->rst = devm_reset_control_get(&pdev->dev, "pwm");
194 if (IS_ERR(pwm->rst)) {
195 ret = PTR_ERR(pwm->rst);
196 dev_err(&pdev->dev, "Reset control is not found: %d\n", ret);
197 return ret;
198 }
199
200 reset_control_deassert(pwm->rst);
201
Thierry Reding0134b932011-12-21 07:47:07 +0100202 pwm->chip.dev = &pdev->dev;
203 pwm->chip.ops = &tegra_pwm_ops;
204 pwm->chip.base = -1;
Thierry Redingc009c562016-07-11 11:08:29 +0200205 pwm->chip.npwm = 4;
Thierry Reding0134b932011-12-21 07:47:07 +0100206
207 ret = pwmchip_add(&pwm->chip);
208 if (ret < 0) {
209 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
Rohith Seelaboyina5dfbd2b2016-06-22 17:17:19 +0530210 reset_control_assert(pwm->rst);
Thierry Reding0134b932011-12-21 07:47:07 +0100211 return ret;
212 }
213
214 return 0;
215}
216
Bill Pemberton77f37912012-11-19 13:26:09 -0500217static int tegra_pwm_remove(struct platform_device *pdev)
Thierry Reding0134b932011-12-21 07:47:07 +0100218{
219 struct tegra_pwm_chip *pc = platform_get_drvdata(pdev);
Thierry Redingc009c562016-07-11 11:08:29 +0200220 unsigned int i;
Rohith Seelaboyina5dfbd2b2016-06-22 17:17:19 +0530221 int err;
Thierry Reding0134b932011-12-21 07:47:07 +0100222
223 if (WARN_ON(!pc))
224 return -ENODEV;
225
Rohith Seelaboyina5dfbd2b2016-06-22 17:17:19 +0530226 err = clk_prepare_enable(pc->clk);
227 if (err < 0)
228 return err;
229
Thierry Redingc009c562016-07-11 11:08:29 +0200230 for (i = 0; i < pc->chip.npwm; i++) {
Thierry Reding0134b932011-12-21 07:47:07 +0100231 struct pwm_device *pwm = &pc->chip.pwms[i];
232
Boris Brezillon5c312522015-07-01 10:21:47 +0200233 if (!pwm_is_enabled(pwm))
Thierry Reding0134b932011-12-21 07:47:07 +0100234 if (clk_prepare_enable(pc->clk) < 0)
235 continue;
236
237 pwm_writel(pc, i, 0);
238
239 clk_disable_unprepare(pc->clk);
240 }
241
Rohith Seelaboyina5dfbd2b2016-06-22 17:17:19 +0530242 reset_control_assert(pc->rst);
243 clk_disable_unprepare(pc->clk);
244
Axel Lin0c8f5272012-07-01 13:00:51 +0800245 return pwmchip_remove(&pc->chip);
Thierry Reding0134b932011-12-21 07:47:07 +0100246}
247
Thierry Redingf1a88702013-04-18 10:04:14 +0200248static const struct of_device_id tegra_pwm_of_match[] = {
Thierry Reding140fd972011-12-21 08:04:13 +0100249 { .compatible = "nvidia,tegra20-pwm" },
250 { .compatible = "nvidia,tegra30-pwm" },
251 { }
252};
253
254MODULE_DEVICE_TABLE(of, tegra_pwm_of_match);
Thierry Reding140fd972011-12-21 08:04:13 +0100255
Thierry Reding0134b932011-12-21 07:47:07 +0100256static struct platform_driver tegra_pwm_driver = {
257 .driver = {
258 .name = "tegra-pwm",
Stephen Warren838bf092013-02-15 15:02:22 -0700259 .of_match_table = tegra_pwm_of_match,
Thierry Reding0134b932011-12-21 07:47:07 +0100260 },
261 .probe = tegra_pwm_probe,
Bill Pembertonfd109112012-11-19 13:21:28 -0500262 .remove = tegra_pwm_remove,
Thierry Reding0134b932011-12-21 07:47:07 +0100263};
264
265module_platform_driver(tegra_pwm_driver);
266
267MODULE_LICENSE("GPL");
268MODULE_AUTHOR("NVIDIA Corporation");
269MODULE_ALIAS("platform:tegra-pwm");