blob: b0ba2d403439b0a77b889f88cb210f0eaae789ad [file] [log] [blame]
Sascha Hauera245cce2012-03-15 10:04:37 +01001/*
Axel Lin261995d2012-07-01 08:29:28 +08002 * drivers/pwm/pwm-vt8500.c
Sascha Hauera245cce2012-03-15 10:04:37 +01003 *
Tony Prisk63e1ed22012-10-27 14:49:57 +13004 * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
5 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
Sascha Hauera245cce2012-03-15 10:04:37 +01006 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/platform_device.h>
20#include <linux/slab.h>
21#include <linux/err.h>
22#include <linux/io.h>
23#include <linux/pwm.h>
24#include <linux/delay.h>
Tony Prisk63e1ed22012-10-27 14:49:57 +130025#include <linux/clk.h>
Sascha Hauera245cce2012-03-15 10:04:37 +010026
27#include <asm/div64.h>
28
Tony Prisk63e1ed22012-10-27 14:49:57 +130029#include <linux/of.h>
30#include <linux/of_device.h>
31#include <linux/of_address.h>
32
33/*
34 * SoC architecture allocates register space for 4 PWMs but only
35 * 2 are currently implemented.
36 */
37#define VT8500_NR_PWMS 2
Sascha Hauera245cce2012-03-15 10:04:37 +010038
39struct vt8500_chip {
40 struct pwm_chip chip;
41 void __iomem *base;
Tony Prisk63e1ed22012-10-27 14:49:57 +130042 struct clk *clk;
Sascha Hauera245cce2012-03-15 10:04:37 +010043};
44
45#define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip)
46
47#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
48static inline void pwm_busy_wait(void __iomem *reg, u8 bitmask)
49{
50 int loops = msecs_to_loops(10);
51 while ((readb(reg) & bitmask) && --loops)
52 cpu_relax();
53
54 if (unlikely(!loops))
Sachin Kamateba7cbe2012-08-10 10:12:09 +053055 pr_warn("Waiting for status bits 0x%x to clear timed out\n",
Sascha Hauera245cce2012-03-15 10:04:37 +010056 bitmask);
57}
58
59static int vt8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
60 int duty_ns, int period_ns)
61{
62 struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
63 unsigned long long c;
64 unsigned long period_cycles, prescale, pv, dc;
Tony Prisk422470a2012-11-20 06:44:46 +130065 int err;
66
67 err = clk_enable(vt8500->clk);
68 if (err < 0) {
69 dev_err(chip->dev, "failed to enable clock\n");
70 return err;
71 }
Sascha Hauera245cce2012-03-15 10:04:37 +010072
Tony Prisk63e1ed22012-10-27 14:49:57 +130073 c = clk_get_rate(vt8500->clk);
Sascha Hauera245cce2012-03-15 10:04:37 +010074 c = c * period_ns;
75 do_div(c, 1000000000);
76 period_cycles = c;
77
78 if (period_cycles < 1)
79 period_cycles = 1;
80 prescale = (period_cycles - 1) / 4096;
81 pv = period_cycles / (prescale + 1) - 1;
82 if (pv > 4095)
83 pv = 4095;
84
Tony Prisk422470a2012-11-20 06:44:46 +130085 if (prescale > 1023) {
86 clk_disable(vt8500->clk);
Sascha Hauera245cce2012-03-15 10:04:37 +010087 return -EINVAL;
Tony Prisk422470a2012-11-20 06:44:46 +130088 }
Sascha Hauera245cce2012-03-15 10:04:37 +010089
90 c = (unsigned long long)pv * duty_ns;
91 do_div(c, period_ns);
92 dc = c;
93
94 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 1));
95 writel(prescale, vt8500->base + 0x4 + (pwm->hwpwm << 4));
96
97 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 2));
98 writel(pv, vt8500->base + 0x8 + (pwm->hwpwm << 4));
99
100 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 3));
101 writel(dc, vt8500->base + 0xc + (pwm->hwpwm << 4));
102
Tony Prisk422470a2012-11-20 06:44:46 +1300103 clk_disable(vt8500->clk);
Sascha Hauera245cce2012-03-15 10:04:37 +0100104 return 0;
105}
106
107static int vt8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
108{
Tony Prisk63e1ed22012-10-27 14:49:57 +1300109 int err;
Sascha Hauera245cce2012-03-15 10:04:37 +0100110 struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
111
Tony Prisk63e1ed22012-10-27 14:49:57 +1300112 err = clk_enable(vt8500->clk);
Tony Prisk2f9569f2012-11-20 06:44:45 +1300113 if (err < 0) {
Tony Prisk63e1ed22012-10-27 14:49:57 +1300114 dev_err(chip->dev, "failed to enable clock\n");
115 return err;
Tony Prisk422470a2012-11-20 06:44:46 +1300116 }
Tony Prisk63e1ed22012-10-27 14:49:57 +1300117
Sascha Hauera245cce2012-03-15 10:04:37 +0100118 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
119 writel(5, vt8500->base + (pwm->hwpwm << 4));
120 return 0;
121}
122
123static void vt8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
124{
125 struct vt8500_chip *vt8500 = to_vt8500_chip(chip);
126
127 pwm_busy_wait(vt8500->base + 0x40 + pwm->hwpwm, (1 << 0));
128 writel(0, vt8500->base + (pwm->hwpwm << 4));
Tony Prisk63e1ed22012-10-27 14:49:57 +1300129
130 clk_disable(vt8500->clk);
Sascha Hauera245cce2012-03-15 10:04:37 +0100131}
132
133static struct pwm_ops vt8500_pwm_ops = {
134 .enable = vt8500_pwm_enable,
135 .disable = vt8500_pwm_disable,
136 .config = vt8500_pwm_config,
137 .owner = THIS_MODULE,
138};
139
Tony Prisk63e1ed22012-10-27 14:49:57 +1300140static const struct of_device_id vt8500_pwm_dt_ids[] = {
141 { .compatible = "via,vt8500-pwm", },
142 { /* Sentinel */ }
143};
144MODULE_DEVICE_TABLE(of, vt8500_pwm_dt_ids);
145
146static int vt8500_pwm_probe(struct platform_device *pdev)
Sascha Hauera245cce2012-03-15 10:04:37 +0100147{
148 struct vt8500_chip *chip;
149 struct resource *r;
Tony Prisk63e1ed22012-10-27 14:49:57 +1300150 struct device_node *np = pdev->dev.of_node;
Sascha Hauera245cce2012-03-15 10:04:37 +0100151 int ret;
152
Tony Prisk63e1ed22012-10-27 14:49:57 +1300153 if (!np) {
154 dev_err(&pdev->dev, "invalid devicetree node\n");
155 return -EINVAL;
156 }
157
Axel Lin261995d2012-07-01 08:29:28 +0800158 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
Sascha Hauera245cce2012-03-15 10:04:37 +0100159 if (chip == NULL) {
160 dev_err(&pdev->dev, "failed to allocate memory\n");
161 return -ENOMEM;
162 }
163
164 chip->chip.dev = &pdev->dev;
165 chip->chip.ops = &vt8500_pwm_ops;
166 chip->chip.base = -1;
167 chip->chip.npwm = VT8500_NR_PWMS;
168
Tony Prisk63e1ed22012-10-27 14:49:57 +1300169 chip->clk = devm_clk_get(&pdev->dev, NULL);
170 if (IS_ERR(chip->clk)) {
171 dev_err(&pdev->dev, "clock source not specified\n");
172 return PTR_ERR(chip->clk);
173 }
174
Sascha Hauera245cce2012-03-15 10:04:37 +0100175 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
176 if (r == NULL) {
177 dev_err(&pdev->dev, "no memory resource defined\n");
Axel Lin261995d2012-07-01 08:29:28 +0800178 return -ENODEV;
Sascha Hauera245cce2012-03-15 10:04:37 +0100179 }
180
Axel Lin261995d2012-07-01 08:29:28 +0800181 chip->base = devm_request_and_ioremap(&pdev->dev, r);
Tony Prisk63e1ed22012-10-27 14:49:57 +1300182 if (!chip->base)
Axel Lin261995d2012-07-01 08:29:28 +0800183 return -EADDRNOTAVAIL;
Sascha Hauera245cce2012-03-15 10:04:37 +0100184
Tony Prisk63e1ed22012-10-27 14:49:57 +1300185 ret = clk_prepare(chip->clk);
186 if (ret < 0) {
187 dev_err(&pdev->dev, "failed to prepare clock\n");
Axel Lin261995d2012-07-01 08:29:28 +0800188 return ret;
Tony Prisk63e1ed22012-10-27 14:49:57 +1300189 }
190
191 ret = pwmchip_add(&chip->chip);
192 if (ret < 0) {
193 dev_err(&pdev->dev, "failed to add PWM chip\n");
194 return ret;
195 }
Sascha Hauera245cce2012-03-15 10:04:37 +0100196
197 platform_set_drvdata(pdev, chip);
198 return ret;
Sascha Hauera245cce2012-03-15 10:04:37 +0100199}
200
Tony Prisk63e1ed22012-10-27 14:49:57 +1300201static int vt8500_pwm_remove(struct platform_device *pdev)
Sascha Hauera245cce2012-03-15 10:04:37 +0100202{
203 struct vt8500_chip *chip;
Sascha Hauera245cce2012-03-15 10:04:37 +0100204
205 chip = platform_get_drvdata(pdev);
206 if (chip == NULL)
207 return -ENODEV;
208
Tony Prisk63e1ed22012-10-27 14:49:57 +1300209 clk_unprepare(chip->clk);
210
Axel Lin261995d2012-07-01 08:29:28 +0800211 return pwmchip_remove(&chip->chip);
Sascha Hauera245cce2012-03-15 10:04:37 +0100212}
213
Tony Prisk63e1ed22012-10-27 14:49:57 +1300214static struct platform_driver vt8500_pwm_driver = {
215 .probe = vt8500_pwm_probe,
216 .remove = vt8500_pwm_remove,
Sascha Hauera245cce2012-03-15 10:04:37 +0100217 .driver = {
218 .name = "vt8500-pwm",
219 .owner = THIS_MODULE,
Tony Prisk63e1ed22012-10-27 14:49:57 +1300220 .of_match_table = vt8500_pwm_dt_ids,
Sascha Hauera245cce2012-03-15 10:04:37 +0100221 },
Sascha Hauera245cce2012-03-15 10:04:37 +0100222};
Tony Prisk63e1ed22012-10-27 14:49:57 +1300223module_platform_driver(vt8500_pwm_driver);
Sascha Hauera245cce2012-03-15 10:04:37 +0100224
Tony Prisk63e1ed22012-10-27 14:49:57 +1300225MODULE_DESCRIPTION("VT8500 PWM Driver");
226MODULE_AUTHOR("Tony Prisk <linux@prisktech.co.nz>");
227MODULE_LICENSE("GPL v2");