blob: 9475bc7a6f97ded7ec293881151747073c198ff3 [file] [log] [blame]
Shawn Guo4dce82c2012-04-04 10:50:52 +08001/*
2 * Copyright 2012 Freescale Semiconductor, Inc.
3 *
4 * The code contained herein is licensed under the GNU General Public
5 * License. You may obtain a copy of the GNU General Public License
6 * Version 2 or later at the following locations:
7 *
8 * http://www.opensource.org/licenses/gpl-license.html
9 * http://www.gnu.org/copyleft/gpl.html
10 */
11
12#include <linux/clk.h>
13#include <linux/err.h>
14#include <linux/io.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/of_address.h>
19#include <linux/platform_device.h>
20#include <linux/pwm.h>
21#include <linux/slab.h>
Shawn Guo01bf32e2012-06-26 16:58:09 +080022#include <linux/stmp_device.h>
Shawn Guo4dce82c2012-04-04 10:50:52 +080023
24#define SET 0x4
25#define CLR 0x8
26#define TOG 0xc
27
28#define PWM_CTRL 0x0
29#define PWM_ACTIVE0 0x10
30#define PWM_PERIOD0 0x20
31#define PERIOD_PERIOD(p) ((p) & 0xffff)
32#define PERIOD_PERIOD_MAX 0x10000
33#define PERIOD_ACTIVE_HIGH (3 << 16)
34#define PERIOD_INACTIVE_LOW (2 << 18)
35#define PERIOD_CDIV(div) (((div) & 0x7) << 20)
36#define PERIOD_CDIV_MAX 8
37
38struct mxs_pwm_chip {
39 struct pwm_chip chip;
Shawn Guo4dce82c2012-04-04 10:50:52 +080040 struct clk *clk;
41 void __iomem *base;
42};
43
44#define to_mxs_pwm_chip(_chip) container_of(_chip, struct mxs_pwm_chip, chip)
45
46static int mxs_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
47 int duty_ns, int period_ns)
48{
49 struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
50 int ret, div = 0;
51 unsigned int period_cycles, duty_cycles;
52 unsigned long rate;
53 unsigned long long c;
54
55 rate = clk_get_rate(mxs->clk);
56 while (1) {
57 c = rate / (1 << div);
58 c = c * period_ns;
59 do_div(c, 1000000000);
60 if (c < PERIOD_PERIOD_MAX)
61 break;
62 div++;
63 if (div > PERIOD_CDIV_MAX)
64 return -EINVAL;
65 }
66
67 period_cycles = c;
68 c *= duty_ns;
69 do_div(c, period_ns);
70 duty_cycles = c;
71
72 /*
73 * If the PWM channel is disabled, make sure to turn on the clock
74 * before writing the register. Otherwise, keep it enabled.
75 */
76 if (!test_bit(PWMF_ENABLED, &pwm->flags)) {
77 ret = clk_prepare_enable(mxs->clk);
78 if (ret)
79 return ret;
80 }
81
82 writel(duty_cycles << 16,
83 mxs->base + PWM_ACTIVE0 + pwm->hwpwm * 0x20);
84 writel(PERIOD_PERIOD(period_cycles) | PERIOD_ACTIVE_HIGH |
85 PERIOD_INACTIVE_LOW | PERIOD_CDIV(div),
86 mxs->base + PWM_PERIOD0 + pwm->hwpwm * 0x20);
87
88 /*
89 * If the PWM is not enabled, turn the clock off again to save power.
90 */
91 if (!test_bit(PWMF_ENABLED, &pwm->flags))
92 clk_disable_unprepare(mxs->clk);
93
94 return 0;
95}
96
97static int mxs_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
98{
99 struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
100 int ret;
101
102 ret = clk_prepare_enable(mxs->clk);
103 if (ret)
104 return ret;
105
106 writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
107
108 return 0;
109}
110
111static void mxs_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
112{
113 struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
114
115 writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
116
117 clk_disable_unprepare(mxs->clk);
118}
119
120static const struct pwm_ops mxs_pwm_ops = {
121 .config = mxs_pwm_config,
122 .enable = mxs_pwm_enable,
123 .disable = mxs_pwm_disable,
124 .owner = THIS_MODULE,
125};
126
127static int mxs_pwm_probe(struct platform_device *pdev)
128{
129 struct device_node *np = pdev->dev.of_node;
130 struct mxs_pwm_chip *mxs;
Shawn Guo22d260b2012-06-26 16:58:10 +0800131 struct resource *res;
Shawn Guo4dce82c2012-04-04 10:50:52 +0800132 int ret;
133
134 mxs = devm_kzalloc(&pdev->dev, sizeof(*mxs), GFP_KERNEL);
135 if (!mxs)
136 return -ENOMEM;
137
Shawn Guo22d260b2012-06-26 16:58:10 +0800138 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding6d4294d2013-01-21 11:09:16 +0100139 mxs->base = devm_ioremap_resource(&pdev->dev, res);
140 if (IS_ERR(mxs->base))
141 return PTR_ERR(mxs->base);
Shawn Guo4dce82c2012-04-04 10:50:52 +0800142
Shawn Guo22d260b2012-06-26 16:58:10 +0800143 mxs->clk = devm_clk_get(&pdev->dev, NULL);
144 if (IS_ERR(mxs->clk))
145 return PTR_ERR(mxs->clk);
Shawn Guo4dce82c2012-04-04 10:50:52 +0800146
147 mxs->chip.dev = &pdev->dev;
148 mxs->chip.ops = &mxs_pwm_ops;
149 mxs->chip.base = -1;
150 ret = of_property_read_u32(np, "fsl,pwm-number", &mxs->chip.npwm);
151 if (ret < 0) {
152 dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
Shawn Guo22d260b2012-06-26 16:58:10 +0800153 return ret;
Shawn Guo4dce82c2012-04-04 10:50:52 +0800154 }
155
156 ret = pwmchip_add(&mxs->chip);
157 if (ret < 0) {
158 dev_err(&pdev->dev, "failed to add pwm chip %d\n", ret);
Shawn Guo22d260b2012-06-26 16:58:10 +0800159 return ret;
Shawn Guo4dce82c2012-04-04 10:50:52 +0800160 }
161
Shawn Guo4dce82c2012-04-04 10:50:52 +0800162 platform_set_drvdata(pdev, mxs);
163
Fabio Estevamcfb9e4c2013-07-09 23:25:37 -0300164 ret = stmp_reset_block(mxs->base);
165 if (ret)
166 goto pwm_remove;
Shawn Guo4dce82c2012-04-04 10:50:52 +0800167
168 return 0;
Fabio Estevamcfb9e4c2013-07-09 23:25:37 -0300169
170pwm_remove:
171 pwmchip_remove(&mxs->chip);
172 return ret;
Shawn Guo4dce82c2012-04-04 10:50:52 +0800173}
174
Bill Pemberton77f37912012-11-19 13:26:09 -0500175static int mxs_pwm_remove(struct platform_device *pdev)
Shawn Guo4dce82c2012-04-04 10:50:52 +0800176{
177 struct mxs_pwm_chip *mxs = platform_get_drvdata(pdev);
178
Axel Lin457fd762012-07-01 12:58:00 +0800179 return pwmchip_remove(&mxs->chip);
Shawn Guo4dce82c2012-04-04 10:50:52 +0800180}
181
Thierry Redingf1a88702013-04-18 10:04:14 +0200182static const struct of_device_id mxs_pwm_dt_ids[] = {
Shawn Guo071407e2012-06-26 16:58:08 +0800183 { .compatible = "fsl,imx23-pwm", },
Shawn Guo4dce82c2012-04-04 10:50:52 +0800184 { /* sentinel */ }
185};
186MODULE_DEVICE_TABLE(of, mxs_pwm_dt_ids);
187
188static struct platform_driver mxs_pwm_driver = {
189 .driver = {
190 .name = "mxs-pwm",
Thierry Reding3dd0a902013-06-12 13:18:29 +0200191 .owner = THIS_MODULE,
Sachin Kamatde02cb82013-09-30 08:56:39 +0530192 .of_match_table = mxs_pwm_dt_ids,
Shawn Guo4dce82c2012-04-04 10:50:52 +0800193 },
194 .probe = mxs_pwm_probe,
Bill Pembertonfd109112012-11-19 13:21:28 -0500195 .remove = mxs_pwm_remove,
Shawn Guo4dce82c2012-04-04 10:50:52 +0800196};
197module_platform_driver(mxs_pwm_driver);
198
199MODULE_ALIAS("platform:mxs-pwm");
200MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
201MODULE_DESCRIPTION("Freescale MXS PWM Driver");
202MODULE_LICENSE("GPL v2");