2 * Copyright 2012 Freescale Semiconductor, Inc.
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:
8 * http://www.opensource.org/licenses/gpl-license.html
9 * http://www.gnu.org/copyleft/gpl.html
12 #include <linux/clk.h>
13 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/of_address.h>
19 #include <linux/platform_device.h>
20 #include <linux/pwm.h>
21 #include <linux/slab.h>
22 #include <linux/stmp_device.h>
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
38 static const unsigned int cdiv[PERIOD_CDIV_MAX] = {
39 1, 2, 4, 8, 16, 64, 256, 1024
48 #define to_mxs_pwm_chip(_chip) container_of(_chip, struct mxs_pwm_chip, chip)
50 static int mxs_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
51 int duty_ns, int period_ns)
53 struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
55 unsigned int period_cycles, duty_cycles;
59 rate = clk_get_rate(mxs->clk);
63 do_div(c, 1000000000);
64 if (c < PERIOD_PERIOD_MAX)
67 if (div >= PERIOD_CDIV_MAX)
77 * If the PWM channel is disabled, make sure to turn on the clock
78 * before writing the register. Otherwise, keep it enabled.
80 if (!pwm_is_enabled(pwm)) {
81 ret = clk_prepare_enable(mxs->clk);
86 writel(duty_cycles << 16,
87 mxs->base + PWM_ACTIVE0 + pwm->hwpwm * 0x20);
88 writel(PERIOD_PERIOD(period_cycles) | PERIOD_ACTIVE_HIGH |
89 PERIOD_INACTIVE_LOW | PERIOD_CDIV(div),
90 mxs->base + PWM_PERIOD0 + pwm->hwpwm * 0x20);
93 * If the PWM is not enabled, turn the clock off again to save power.
95 if (!pwm_is_enabled(pwm))
96 clk_disable_unprepare(mxs->clk);
101 static int mxs_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
103 struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
106 ret = clk_prepare_enable(mxs->clk);
110 writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
115 static void mxs_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
117 struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
119 writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
121 clk_disable_unprepare(mxs->clk);
124 static const struct pwm_ops mxs_pwm_ops = {
125 .config = mxs_pwm_config,
126 .enable = mxs_pwm_enable,
127 .disable = mxs_pwm_disable,
128 .owner = THIS_MODULE,
131 static int mxs_pwm_probe(struct platform_device *pdev)
133 struct device_node *np = pdev->dev.of_node;
134 struct mxs_pwm_chip *mxs;
135 struct resource *res;
138 mxs = devm_kzalloc(&pdev->dev, sizeof(*mxs), GFP_KERNEL);
142 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
143 mxs->base = devm_ioremap_resource(&pdev->dev, res);
144 if (IS_ERR(mxs->base))
145 return PTR_ERR(mxs->base);
147 mxs->clk = devm_clk_get(&pdev->dev, NULL);
148 if (IS_ERR(mxs->clk))
149 return PTR_ERR(mxs->clk);
151 mxs->chip.dev = &pdev->dev;
152 mxs->chip.ops = &mxs_pwm_ops;
155 ret = of_property_read_u32(np, "fsl,pwm-number", &mxs->chip.npwm);
157 dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
161 ret = pwmchip_add(&mxs->chip);
163 dev_err(&pdev->dev, "failed to add pwm chip %d\n", ret);
167 platform_set_drvdata(pdev, mxs);
169 ret = stmp_reset_block(mxs->base);
176 pwmchip_remove(&mxs->chip);
180 static int mxs_pwm_remove(struct platform_device *pdev)
182 struct mxs_pwm_chip *mxs = platform_get_drvdata(pdev);
184 return pwmchip_remove(&mxs->chip);
187 static const struct of_device_id mxs_pwm_dt_ids[] = {
188 { .compatible = "fsl,imx23-pwm", },
191 MODULE_DEVICE_TABLE(of, mxs_pwm_dt_ids);
193 static struct platform_driver mxs_pwm_driver = {
196 .of_match_table = mxs_pwm_dt_ids,
198 .probe = mxs_pwm_probe,
199 .remove = mxs_pwm_remove,
201 module_platform_driver(mxs_pwm_driver);
203 MODULE_ALIAS("platform:mxs-pwm");
204 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
205 MODULE_DESCRIPTION("Freescale MXS PWM Driver");
206 MODULE_LICENSE("GPL v2");