blob: 7170a315535bcfeb9cbecb0e1982e76b923e15b0 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Sascha Hauera245cce2012-03-15 10:04:37 +01002/*
Axel Lin261995dd2012-07-01 08:29:28 +08003 * drivers/pwm/pwm-vt8500.c
Sascha Hauera245cce2012-03-15 10:04:37 +01004 *
Tony Prisk63e1ed22012-10-27 14:49:57 +13005 * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
6 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
Sascha Hauera245cce2012-03-15 10:04:37 +01007 */
8
9#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/platform_device.h>
12#include <linux/slab.h>
13#include <linux/err.h>
14#include <linux/io.h>
15#include <linux/pwm.h>
16#include <linux/delay.h>
Tony Prisk63e1ed22012-10-27 14:49:57 +130017#include <linux/clk.h>
Sascha Hauera245cce2012-03-15 10:04:37 +010018
19#include <asm/div64.h>
20
Tony Prisk63e1ed22012-10-27 14:49:57 +130021#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/of_address.h>
24
25/*
26 * SoC architecture allocates register space for 4 PWMs but only
27 * 2 are currently implemented.
28 */
29#define VT8500_NR_PWMS 2
Sascha Hauera245cce2012-03-15 10:04:37 +010030
Tony Prisk8ab432c2013-01-03 08:44:15 +130031#define REG_CTRL(pwm) (((pwm) << 4) + 0x00)
32#define REG_SCALAR(pwm) (((pwm) << 4) + 0x04)
33#define REG_PERIOD(pwm) (((pwm) << 4) + 0x08)
34#define REG_DUTY(pwm) (((pwm) << 4) + 0x0C)
35#define REG_STATUS 0x40
36
37#define CTRL_ENABLE BIT(0)
38#define CTRL_INVERT BIT(1)
39#define CTRL_AUTOLOAD BIT(2)
40#define CTRL_STOP_IMM BIT(3)
41#define CTRL_LOAD_PRESCALE BIT(4)
42#define CTRL_LOAD_PERIOD BIT(5)
43
44#define STATUS_CTRL_UPDATE BIT(0)
45#define STATUS_SCALAR_UPDATE BIT(1)
46#define STATUS_PERIOD_UPDATE BIT(2)
47#define STATUS_DUTY_UPDATE BIT(3)
48#define STATUS_ALL_UPDATE 0x0F
49
Sascha Hauera245cce2012-03-15 10:04:37 +010050struct vt8500_chip {
51 struct pwm_chip chip;
52 void __iomem *base;
Tony Prisk63e1ed22012-10-27 14:49:57 +130053 struct clk *clk;
Sascha Hauera245cce2012-03-15 10:04:37 +010054};
55
56#define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip)
57
58#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
Uwe Kleine-Könige9d866d2021-11-02 10:28:04 +010059static inline void vt8500_pwm_busy_wait(struct vt8500_chip *vt8500, int nr, u8 bitmask)
Sascha Hauera245cce2012-03-15 10:04:37 +010060{
61 int loops = msecs_to_loops(10);
Tony Prisk8ab432c2013-01-03 08:44:15 +130062 u32 mask = bitmask << (nr << 8);
63
64 while ((readl(vt8500->base + REG_STATUS) & mask) && --loops)
Sascha Hauera245cce2012-03-15 10:04:37 +010065 cpu_relax();
66
67 if (unlikely(!loops))
Tony Prisk8ab432c2013-01-03 08:44:15 +130068 dev_warn(vt8500->chip.dev, "Waiting for status bits 0x%x to clear timed out\n",
69 mask);
Sascha Hauera245cce2012-03-15 10:04:37 +010070}
71
72static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
Uwe Kleine-König14d89562021-11-02 10:28:03 +010073 u64 duty_ns, u64 period_ns)
Sascha Hauera245cce2012-03-15 10:04:37 +010074{
75 struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
76 unsigned long long c;
77 unsigned long period_cycles, prescale, pv, dc;
Tony Prisk422470a2012-11-20 06:44:46 +130078 int err;
Tony Prisk8ab432c2013-01-03 08:44:15 +130079 u32 val;
Tony Prisk422470a2012-11-20 06:44:46 +130080
81 err = clk_enable(vt8500->clk);
82 if (err < 0) {
83 dev_err(chip->dev, "failed to enable clock\n");
84 return err;
85 }
Sascha Hauera245cce2012-03-15 10:04:37 +010086
Tony Prisk63e1ed22012-10-27 14:49:57 +130087 c = clk_get_rate(vt8500->clk);
Sascha Hauera245cce2012-03-15 10:04:37 +010088 c = c * period_ns;
89 do_div(c, 1000000000);
90 period_cycles = c;
91
92 if (period_cycles < 1)
93 period_cycles = 1;
94 prescale = (period_cycles - 1) / 4096;
95 pv = period_cycles / (prescale + 1) - 1;
96 if (pv > 4095)
97 pv = 4095;
98
Tony Prisk422470a2012-11-20 06:44:46 +130099 if (prescale > 1023) {
100 clk_disable(vt8500->clk);
Sascha Hauera245cce2012-03-15 10:04:37 +0100101 return -EINVAL;
Tony Prisk422470a2012-11-20 06:44:46 +1300102 }
Sascha Hauera245cce2012-03-15 10:04:37 +0100103
104 c = (unsigned long long)pv * duty_ns;
Uwe Kleine-König14d89562021-11-02 10:28:03 +0100105
106 dc = div64_u64(c, period_ns);
Sascha Hauera245cce2012-03-15 10:04:37 +0100107
Tony Prisk8ab432c2013-01-03 08:44:15 +1300108 writel(prescale, vt8500->base + REG_SCALAR(pwm->hwpwm));
Uwe Kleine-Könige9d866d2021-11-02 10:28:04 +0100109 vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_SCALAR_UPDATE);
Sascha Hauera245cce2012-03-15 10:04:37 +0100110
Tony Prisk8ab432c2013-01-03 08:44:15 +1300111 writel(pv, vt8500->base + REG_PERIOD(pwm->hwpwm));
Uwe Kleine-Könige9d866d2021-11-02 10:28:04 +0100112 vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_PERIOD_UPDATE);
Sascha Hauera245cce2012-03-15 10:04:37 +0100113
Tony Prisk8ab432c2013-01-03 08:44:15 +1300114 writel(dc, vt8500->base + REG_DUTY(pwm->hwpwm));
Uwe Kleine-Könige9d866d2021-11-02 10:28:04 +0100115 vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_DUTY_UPDATE);
Tony Prisk8ab432c2013-01-03 08:44:15 +1300116
117 val = readl(vt8500->base + REG_CTRL(pwm->hwpwm));
118 val |= CTRL_AUTOLOAD;
119 writel(val, vt8500->base + REG_CTRL(pwm->hwpwm));
Uwe Kleine-Könige9d866d2021-11-02 10:28:04 +0100120 vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE);
Sascha Hauera245cce2012-03-15 10:04:37 +0100121
Tony Prisk422470a2012-11-20 06:44:46 +1300122 clk_disable(vt8500->clk);
Sascha Hauera245cce2012-03-15 10:04:37 +0100123 return 0;
124}
125
126static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
127{
128 struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
Tony Prisk8ab432c2013-01-03 08:44:15 +1300129 int err;
130 u32 val;
Sascha Hauera245cce2012-03-15 10:04:37 +0100131
Tony Prisk63e1ed22012-10-27 14:49:57 +1300132 err = clk_enable(vt8500->clk);
Tony Prisk2f9569f2012-11-20 06:44:45 +1300133 if (err < 0) {
Tony Prisk63e1ed22012-10-27 14:49:57 +1300134 dev_err(chip->dev, "failed to enable clock\n");
135 return err;
Tony Prisk422470a2012-11-20 06:44:46 +1300136 }
Tony Prisk63e1ed22012-10-27 14:49:57 +1300137
Tony Prisk8ab432c2013-01-03 08:44:15 +1300138 val = readl(vt8500->base + REG_CTRL(pwm->hwpwm));
139 val |= CTRL_ENABLE;
140 writel(val, vt8500->base + REG_CTRL(pwm->hwpwm));
Uwe Kleine-Könige9d866d2021-11-02 10:28:04 +0100141 vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE);
Tony Prisk8ab432c2013-01-03 08:44:15 +1300142
Sascha Hauera245cce2012-03-15 10:04:37 +0100143 return 0;
144}
145
146static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
147{
148 struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
Tony Prisk8ab432c2013-01-03 08:44:15 +1300149 u32 val;
Sascha Hauera245cce2012-03-15 10:04:37 +0100150
Tony Prisk8ab432c2013-01-03 08:44:15 +1300151 val = readl(vt8500->base + REG_CTRL(pwm->hwpwm));
152 val &= ~CTRL_ENABLE;
153 writel(val, vt8500->base + REG_CTRL(pwm->hwpwm));
Uwe Kleine-Könige9d866d2021-11-02 10:28:04 +0100154 vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE);
Tony Prisk63e1ed22012-10-27 14:49:57 +1300155
156 clk_disable(vt8500->clk);
Sascha Hauera245cce2012-03-15 10:04:37 +0100157}
158
Tony Prisk3ccb1c12013-01-03 08:44:16 +1300159static int vt8500_pwm_set_polarity(struct pwm_chip *chip,
160 struct pwm_device *pwm,
161 enum pwm_polarity polarity)
162{
163 struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
164 u32 val;
165
166 val = readl(vt8500->base + REG_CTRL(pwm->hwpwm));
167
168 if (polarity == PWM_POLARITY_INVERSED)
169 val |= CTRL_INVERT;
170 else
171 val &= ~CTRL_INVERT;
172
173 writel(val, vt8500->base + REG_CTRL(pwm->hwpwm));
Uwe Kleine-Könige9d866d2021-11-02 10:28:04 +0100174 vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE);
Tony Prisk3ccb1c12013-01-03 08:44:16 +1300175
176 return 0;
177}
178
Uwe Kleine-König14d89562021-11-02 10:28:03 +0100179static int vt8500_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
180 const struct pwm_state *state)
181{
182 int err;
183 bool enabled = pwm->state.enabled;
184
185 if (state->polarity != pwm->state.polarity) {
186 /*
187 * Changing the polarity of a running PWM is only allowed when
188 * the PWM driver implements ->apply().
189 */
190 if (enabled) {
191 vt8500_pwm_disable(chip, pwm);
192
193 enabled = false;
194 }
195
196 err = vt8500_pwm_set_polarity(chip, pwm, state->polarity);
197 if (err)
198 return err;
199 }
200
201 if (!state->enabled) {
202 if (enabled)
203 vt8500_pwm_disable(chip, pwm);
204
205 return 0;
206 }
207
208 /*
209 * We cannot skip calling ->config even if state->period ==
210 * pwm->state.period && state->duty_cycle == pwm->state.duty_cycle
211 * because we might have exited early in the last call to
212 * pwm_apply_state because of !state->enabled and so the two values in
213 * pwm->state might not be configured in hardware.
214 */
215 err = vt8500_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period);
216 if (err)
217 return err;
218
219 if (!enabled)
220 err = vt8500_pwm_enable(chip, pwm);
221
222 return err;
223}
224
Bhumika Goyalb2ec9ef2017-01-10 23:42:06 +0530225static const struct pwm_ops vt8500_pwm_ops = {
Uwe Kleine-König14d89562021-11-02 10:28:03 +0100226 .apply = vt8500_pwm_apply,
Sascha Hauera245cce2012-03-15 10:04:37 +0100227 .owner = THIS_MODULE,
228};
229
Tony Prisk63e1ed22012-10-27 14:49:57 +1300230static const struct of_device_id vt8500_pwm_dt_ids[] = {
231 { .compatible = "via,vt8500-pwm", },
232 { /* Sentinel */ }
233};
234MODULE_DEVICE_TABLE(of, vt8500_pwm_dt_ids);
235
236static int vt8500_pwm_probe(struct platform_device *pdev)
Sascha Hauera245cce2012-03-15 10:04:37 +0100237{
238 struct vt8500_chip *chip;
Tony Prisk63e1ed22012-10-27 14:49:57 +1300239 struct device_node *np = pdev->dev.of_node;
Sascha Hauera245cce2012-03-15 10:04:37 +0100240 int ret;
241
Tony Prisk63e1ed22012-10-27 14:49:57 +1300242 if (!np) {
243 dev_err(&pdev->dev, "invalid devicetree node\n");
244 return -EINVAL;
245 }
246
Axel Lin261995dd2012-07-01 08:29:28 +0800247 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
Jingoo Hanb9f87402014-04-23 18:42:10 +0900248 if (chip == NULL)
Sascha Hauera245cce2012-03-15 10:04:37 +0100249 return -ENOMEM;
Sascha Hauera245cce2012-03-15 10:04:37 +0100250
251 chip->chip.dev = &pdev->dev;
252 chip->chip.ops = &vt8500_pwm_ops;
Sascha Hauera245cce2012-03-15 10:04:37 +0100253 chip->chip.npwm = VT8500_NR_PWMS;
254
Tony Prisk63e1ed22012-10-27 14:49:57 +1300255 chip->clk = devm_clk_get(&pdev->dev, NULL);
256 if (IS_ERR(chip->clk)) {
257 dev_err(&pdev->dev, "clock source not specified\n");
258 return PTR_ERR(chip->clk);
259 }
260
Yangtao Li4906bf52019-12-29 08:06:02 +0000261 chip->base = devm_platform_ioremap_resource(pdev, 0);
Thierry Reding6d4294d2013-01-21 11:09:16 +0100262 if (IS_ERR(chip->base))
263 return PTR_ERR(chip->base);
Sascha Hauera245cce2012-03-15 10:04:37 +0100264
Tony Prisk63e1ed22012-10-27 14:49:57 +1300265 ret = clk_prepare(chip->clk);
266 if (ret < 0) {
267 dev_err(&pdev->dev, "failed to prepare clock\n");
Axel Lin261995dd2012-07-01 08:29:28 +0800268 return ret;
Tony Prisk63e1ed22012-10-27 14:49:57 +1300269 }
270
271 ret = pwmchip_add(&chip->chip);
272 if (ret < 0) {
273 dev_err(&pdev->dev, "failed to add PWM chip\n");
Arvind Yadav0bd24f92017-05-05 13:30:14 +0530274 clk_unprepare(chip->clk);
Tony Prisk63e1ed22012-10-27 14:49:57 +1300275 return ret;
276 }
Sascha Hauera245cce2012-03-15 10:04:37 +0100277
278 platform_set_drvdata(pdev, chip);
279 return ret;
Sascha Hauera245cce2012-03-15 10:04:37 +0100280}
281
Tony Prisk63e1ed22012-10-27 14:49:57 +1300282static int vt8500_pwm_remove(struct platform_device *pdev)
Sascha Hauera245cce2012-03-15 10:04:37 +0100283{
Uwe Kleine-Königfb2cb3b2021-06-21 15:04:57 +0200284 struct vt8500_chip *chip = platform_get_drvdata(pdev);
Sascha Hauera245cce2012-03-15 10:04:37 +0100285
Uwe Kleine-König868f13b2021-06-21 15:04:58 +0200286 pwmchip_remove(&chip->chip);
Sascha Hauera245cce2012-03-15 10:04:37 +0100287
Tony Prisk63e1ed22012-10-27 14:49:57 +1300288 clk_unprepare(chip->clk);
289
Uwe Kleine-König868f13b2021-06-21 15:04:58 +0200290 return 0;
Sascha Hauera245cce2012-03-15 10:04:37 +0100291}
292
Tony Prisk63e1ed22012-10-27 14:49:57 +1300293static struct platform_driver vt8500_pwm_driver = {
294 .probe = vt8500_pwm_probe,
295 .remove = vt8500_pwm_remove,
Sascha Hauera245cce2012-03-15 10:04:37 +0100296 .driver = {
297 .name = "vt8500-pwm",
Tony Prisk63e1ed22012-10-27 14:49:57 +1300298 .of_match_table = vt8500_pwm_dt_ids,
Sascha Hauera245cce2012-03-15 10:04:37 +0100299 },
Sascha Hauera245cce2012-03-15 10:04:37 +0100300};
Tony Prisk63e1ed22012-10-27 14:49:57 +1300301module_platform_driver(vt8500_pwm_driver);
Sascha Hauera245cce2012-03-15 10:04:37 +0100302
Tony Prisk63e1ed22012-10-27 14:49:57 +1300303MODULE_DESCRIPTION("VT8500 PWM Driver");
304MODULE_AUTHOR("Tony Prisk <linux@prisktech.co.nz>");
305MODULE_LICENSE("GPL v2");