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/pinctrl/consumer.h>
20 #include <linux/platform_device.h>
21 #include <linux/pwm.h>
22 #include <linux/slab.h>
23 #include <linux/stmp_device.h>
30 #define PWM_ACTIVE0 0x10
31 #define PWM_PERIOD0 0x20
32 #define PERIOD_PERIOD(p) ((p) & 0xffff)
33 #define PERIOD_PERIOD_MAX 0x10000
34 #define PERIOD_ACTIVE_HIGH (3 << 16)
35 #define PERIOD_INACTIVE_LOW (2 << 18)
36 #define PERIOD_CDIV(div) (((div) & 0x7) << 20)
37 #define PERIOD_CDIV_MAX 8
45 #define to_mxs_pwm_chip(_chip) container_of(_chip, struct mxs_pwm_chip, chip)
47 static int mxs_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
48 int duty_ns
, int period_ns
)
50 struct mxs_pwm_chip
*mxs
= to_mxs_pwm_chip(chip
);
52 unsigned int period_cycles
, duty_cycles
;
56 rate
= clk_get_rate(mxs
->clk
);
58 c
= rate
/ (1 << div
);
60 do_div(c
, 1000000000);
61 if (c
< PERIOD_PERIOD_MAX
)
64 if (div
> PERIOD_CDIV_MAX
)
74 * If the PWM channel is disabled, make sure to turn on the clock
75 * before writing the register. Otherwise, keep it enabled.
77 if (!test_bit(PWMF_ENABLED
, &pwm
->flags
)) {
78 ret
= clk_prepare_enable(mxs
->clk
);
83 writel(duty_cycles
<< 16,
84 mxs
->base
+ PWM_ACTIVE0
+ pwm
->hwpwm
* 0x20);
85 writel(PERIOD_PERIOD(period_cycles
) | PERIOD_ACTIVE_HIGH
|
86 PERIOD_INACTIVE_LOW
| PERIOD_CDIV(div
),
87 mxs
->base
+ PWM_PERIOD0
+ pwm
->hwpwm
* 0x20);
90 * If the PWM is not enabled, turn the clock off again to save power.
92 if (!test_bit(PWMF_ENABLED
, &pwm
->flags
))
93 clk_disable_unprepare(mxs
->clk
);
98 static int mxs_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
100 struct mxs_pwm_chip
*mxs
= to_mxs_pwm_chip(chip
);
103 ret
= clk_prepare_enable(mxs
->clk
);
107 writel(1 << pwm
->hwpwm
, mxs
->base
+ PWM_CTRL
+ SET
);
112 static void mxs_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
114 struct mxs_pwm_chip
*mxs
= to_mxs_pwm_chip(chip
);
116 writel(1 << pwm
->hwpwm
, mxs
->base
+ PWM_CTRL
+ CLR
);
118 clk_disable_unprepare(mxs
->clk
);
121 static const struct pwm_ops mxs_pwm_ops
= {
122 .config
= mxs_pwm_config
,
123 .enable
= mxs_pwm_enable
,
124 .disable
= mxs_pwm_disable
,
125 .owner
= THIS_MODULE
,
128 static int mxs_pwm_probe(struct platform_device
*pdev
)
130 struct device_node
*np
= pdev
->dev
.of_node
;
131 struct mxs_pwm_chip
*mxs
;
132 struct resource
*res
;
133 struct pinctrl
*pinctrl
;
136 mxs
= devm_kzalloc(&pdev
->dev
, sizeof(*mxs
), GFP_KERNEL
);
140 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
141 mxs
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
142 if (IS_ERR(mxs
->base
))
143 return PTR_ERR(mxs
->base
);
145 pinctrl
= devm_pinctrl_get_select_default(&pdev
->dev
);
147 return PTR_ERR(pinctrl
);
149 mxs
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
150 if (IS_ERR(mxs
->clk
))
151 return PTR_ERR(mxs
->clk
);
153 mxs
->chip
.dev
= &pdev
->dev
;
154 mxs
->chip
.ops
= &mxs_pwm_ops
;
156 ret
= of_property_read_u32(np
, "fsl,pwm-number", &mxs
->chip
.npwm
);
158 dev_err(&pdev
->dev
, "failed to get pwm number: %d\n", ret
);
162 ret
= pwmchip_add(&mxs
->chip
);
164 dev_err(&pdev
->dev
, "failed to add pwm chip %d\n", ret
);
168 platform_set_drvdata(pdev
, mxs
);
170 stmp_reset_block(mxs
->base
);
175 static int mxs_pwm_remove(struct platform_device
*pdev
)
177 struct mxs_pwm_chip
*mxs
= platform_get_drvdata(pdev
);
179 return pwmchip_remove(&mxs
->chip
);
182 static const struct of_device_id mxs_pwm_dt_ids
[] = {
183 { .compatible
= "fsl,imx23-pwm", },
186 MODULE_DEVICE_TABLE(of
, mxs_pwm_dt_ids
);
188 static struct platform_driver mxs_pwm_driver
= {
191 .of_match_table
= of_match_ptr(mxs_pwm_dt_ids
),
193 .probe
= mxs_pwm_probe
,
194 .remove
= mxs_pwm_remove
,
196 module_platform_driver(mxs_pwm_driver
);
198 MODULE_ALIAS("platform:mxs-pwm");
199 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
200 MODULE_DESCRIPTION("Freescale MXS PWM Driver");
201 MODULE_LICENSE("GPL v2");