blob: 10efd3de0bb327fceb9529b95b6b6afaf4c62691 [file] [log] [blame]
Fabio Estevam19ad2b72018-07-09 14:55:58 -03001// SPDX-License-Identifier: GPL-2.0+
Shawn Guo4dce82c2012-04-04 10:50:52 +08002/*
3 * Copyright 2012 Freescale Semiconductor, Inc.
Shawn Guo4dce82c2012-04-04 10:50:52 +08004 */
5
6#include <linux/clk.h>
7#include <linux/err.h>
8#include <linux/io.h>
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/of.h>
12#include <linux/of_address.h>
13#include <linux/platform_device.h>
14#include <linux/pwm.h>
15#include <linux/slab.h>
Shawn Guo01bf32e2012-06-26 16:58:09 +080016#include <linux/stmp_device.h>
Shawn Guo4dce82c2012-04-04 10:50:52 +080017
18#define SET 0x4
19#define CLR 0x8
20#define TOG 0xc
21
22#define PWM_CTRL 0x0
23#define PWM_ACTIVE0 0x10
24#define PWM_PERIOD0 0x20
25#define PERIOD_PERIOD(p) ((p) & 0xffff)
26#define PERIOD_PERIOD_MAX 0x10000
27#define PERIOD_ACTIVE_HIGH (3 << 16)
28#define PERIOD_INACTIVE_LOW (2 << 18)
Rasmus Villemoesbf29c2f2019-10-04 15:32:02 +020029#define PERIOD_POLARITY_NORMAL (PERIOD_ACTIVE_HIGH | PERIOD_INACTIVE_LOW)
Shawn Guo4dce82c2012-04-04 10:50:52 +080030#define PERIOD_CDIV(div) (((div) & 0x7) << 20)
31#define PERIOD_CDIV_MAX 8
32
Gaetan Hug24ccea12015-03-11 13:08:12 +010033static const unsigned int cdiv[PERIOD_CDIV_MAX] = {
34 1, 2, 4, 8, 16, 64, 256, 1024
35};
36
Shawn Guo4dce82c2012-04-04 10:50:52 +080037struct mxs_pwm_chip {
38 struct pwm_chip chip;
Shawn Guo4dce82c2012-04-04 10:50:52 +080039 struct clk *clk;
40 void __iomem *base;
41};
42
43#define to_mxs_pwm_chip(_chip) container_of(_chip, struct mxs_pwm_chip, chip)
44
Rasmus Villemoesbf29c2f2019-10-04 15:32:02 +020045static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
46 const struct pwm_state *state)
47{
48 struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
49 int ret, div = 0;
50 unsigned int period_cycles, duty_cycles;
51 unsigned long rate;
52 unsigned long long c;
53
54 if (state->polarity != PWM_POLARITY_NORMAL)
55 return -ENOTSUPP;
56
57 /*
58 * If the PWM channel is disabled, make sure to turn on the
59 * clock before calling clk_get_rate() and writing to the
60 * registers. Otherwise, just keep it enabled.
61 */
62 if (!pwm_is_enabled(pwm)) {
63 ret = clk_prepare_enable(mxs->clk);
64 if (ret)
65 return ret;
66 }
67
68 if (!state->enabled && pwm_is_enabled(pwm))
69 writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
70
71 rate = clk_get_rate(mxs->clk);
72 while (1) {
73 c = rate / cdiv[div];
74 c = c * state->period;
75 do_div(c, 1000000000);
76 if (c < PERIOD_PERIOD_MAX)
77 break;
78 div++;
79 if (div >= PERIOD_CDIV_MAX)
80 return -EINVAL;
81 }
82
83 period_cycles = c;
84 c *= state->duty_cycle;
85 do_div(c, state->period);
86 duty_cycles = c;
87
88 /*
89 * The data sheet the says registers must be written to in
90 * this order (ACTIVEn, then PERIODn). Also, the new settings
91 * only take effect at the beginning of a new period, avoiding
92 * glitches.
93 */
94 writel(duty_cycles << 16,
95 mxs->base + PWM_ACTIVE0 + pwm->hwpwm * 0x20);
96 writel(PERIOD_PERIOD(period_cycles) | PERIOD_POLARITY_NORMAL | PERIOD_CDIV(div),
97 mxs->base + PWM_PERIOD0 + pwm->hwpwm * 0x20);
98
99 if (state->enabled) {
100 if (!pwm_is_enabled(pwm)) {
101 /*
102 * The clock was enabled above. Just enable
103 * the channel in the control register.
104 */
105 writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
106 }
107 } else {
108 clk_disable_unprepare(mxs->clk);
109 }
110 return 0;
111}
112
Shawn Guo4dce82c2012-04-04 10:50:52 +0800113static int mxs_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
114 int duty_ns, int period_ns)
115{
116 struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
117 int ret, div = 0;
118 unsigned int period_cycles, duty_cycles;
119 unsigned long rate;
120 unsigned long long c;
121
122 rate = clk_get_rate(mxs->clk);
123 while (1) {
Gaetan Hug24ccea12015-03-11 13:08:12 +0100124 c = rate / cdiv[div];
Shawn Guo4dce82c2012-04-04 10:50:52 +0800125 c = c * period_ns;
126 do_div(c, 1000000000);
127 if (c < PERIOD_PERIOD_MAX)
128 break;
129 div++;
Gaetan Hug24ccea12015-03-11 13:08:12 +0100130 if (div >= PERIOD_CDIV_MAX)
Shawn Guo4dce82c2012-04-04 10:50:52 +0800131 return -EINVAL;
132 }
133
134 period_cycles = c;
135 c *= duty_ns;
136 do_div(c, period_ns);
137 duty_cycles = c;
138
139 /*
140 * If the PWM channel is disabled, make sure to turn on the clock
141 * before writing the register. Otherwise, keep it enabled.
142 */
Boris Brezillon5c312522015-07-01 10:21:47 +0200143 if (!pwm_is_enabled(pwm)) {
Shawn Guo4dce82c2012-04-04 10:50:52 +0800144 ret = clk_prepare_enable(mxs->clk);
145 if (ret)
146 return ret;
147 }
148
149 writel(duty_cycles << 16,
150 mxs->base + PWM_ACTIVE0 + pwm->hwpwm * 0x20);
151 writel(PERIOD_PERIOD(period_cycles) | PERIOD_ACTIVE_HIGH |
152 PERIOD_INACTIVE_LOW | PERIOD_CDIV(div),
153 mxs->base + PWM_PERIOD0 + pwm->hwpwm * 0x20);
154
155 /*
156 * If the PWM is not enabled, turn the clock off again to save power.
157 */
Boris Brezillon5c312522015-07-01 10:21:47 +0200158 if (!pwm_is_enabled(pwm))
Shawn Guo4dce82c2012-04-04 10:50:52 +0800159 clk_disable_unprepare(mxs->clk);
160
161 return 0;
162}
163
164static int mxs_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
165{
166 struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
167 int ret;
168
169 ret = clk_prepare_enable(mxs->clk);
170 if (ret)
171 return ret;
172
173 writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + SET);
174
175 return 0;
176}
177
178static void mxs_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
179{
180 struct mxs_pwm_chip *mxs = to_mxs_pwm_chip(chip);
181
182 writel(1 << pwm->hwpwm, mxs->base + PWM_CTRL + CLR);
183
184 clk_disable_unprepare(mxs->clk);
185}
186
187static const struct pwm_ops mxs_pwm_ops = {
Rasmus Villemoesbf29c2f2019-10-04 15:32:02 +0200188 .apply = mxs_pwm_apply,
Shawn Guo4dce82c2012-04-04 10:50:52 +0800189 .config = mxs_pwm_config,
190 .enable = mxs_pwm_enable,
191 .disable = mxs_pwm_disable,
192 .owner = THIS_MODULE,
193};
194
195static int mxs_pwm_probe(struct platform_device *pdev)
196{
197 struct device_node *np = pdev->dev.of_node;
198 struct mxs_pwm_chip *mxs;
199 int ret;
200
201 mxs = devm_kzalloc(&pdev->dev, sizeof(*mxs), GFP_KERNEL);
202 if (!mxs)
203 return -ENOMEM;
204
Anson Huanga3156142019-07-18 09:32:05 +0800205 mxs->base = devm_platform_ioremap_resource(pdev, 0);
Thierry Reding6d4294d2013-01-21 11:09:16 +0100206 if (IS_ERR(mxs->base))
207 return PTR_ERR(mxs->base);
Shawn Guo4dce82c2012-04-04 10:50:52 +0800208
Shawn Guo22d260b2012-06-26 16:58:10 +0800209 mxs->clk = devm_clk_get(&pdev->dev, NULL);
210 if (IS_ERR(mxs->clk))
211 return PTR_ERR(mxs->clk);
Shawn Guo4dce82c2012-04-04 10:50:52 +0800212
213 mxs->chip.dev = &pdev->dev;
214 mxs->chip.ops = &mxs_pwm_ops;
215 mxs->chip.base = -1;
Thierry Reding8c0216f2017-01-04 09:40:54 +0100216
Shawn Guo4dce82c2012-04-04 10:50:52 +0800217 ret = of_property_read_u32(np, "fsl,pwm-number", &mxs->chip.npwm);
218 if (ret < 0) {
219 dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
Shawn Guo22d260b2012-06-26 16:58:10 +0800220 return ret;
Shawn Guo4dce82c2012-04-04 10:50:52 +0800221 }
222
223 ret = pwmchip_add(&mxs->chip);
224 if (ret < 0) {
225 dev_err(&pdev->dev, "failed to add pwm chip %d\n", ret);
Shawn Guo22d260b2012-06-26 16:58:10 +0800226 return ret;
Shawn Guo4dce82c2012-04-04 10:50:52 +0800227 }
228
Shawn Guo4dce82c2012-04-04 10:50:52 +0800229 platform_set_drvdata(pdev, mxs);
230
Fabio Estevamcfb9e4c2013-07-09 23:25:37 -0300231 ret = stmp_reset_block(mxs->base);
232 if (ret)
233 goto pwm_remove;
Shawn Guo4dce82c2012-04-04 10:50:52 +0800234
235 return 0;
Fabio Estevamcfb9e4c2013-07-09 23:25:37 -0300236
237pwm_remove:
238 pwmchip_remove(&mxs->chip);
239 return ret;
Shawn Guo4dce82c2012-04-04 10:50:52 +0800240}
241
Bill Pemberton77f37912012-11-19 13:26:09 -0500242static int mxs_pwm_remove(struct platform_device *pdev)
Shawn Guo4dce82c2012-04-04 10:50:52 +0800243{
244 struct mxs_pwm_chip *mxs = platform_get_drvdata(pdev);
245
Axel Lin457fd762012-07-01 12:58:00 +0800246 return pwmchip_remove(&mxs->chip);
Shawn Guo4dce82c2012-04-04 10:50:52 +0800247}
248
Thierry Redingf1a88702013-04-18 10:04:14 +0200249static const struct of_device_id mxs_pwm_dt_ids[] = {
Shawn Guo071407e2012-06-26 16:58:08 +0800250 { .compatible = "fsl,imx23-pwm", },
Shawn Guo4dce82c2012-04-04 10:50:52 +0800251 { /* sentinel */ }
252};
253MODULE_DEVICE_TABLE(of, mxs_pwm_dt_ids);
254
255static struct platform_driver mxs_pwm_driver = {
256 .driver = {
257 .name = "mxs-pwm",
Sachin Kamatde02cb82013-09-30 08:56:39 +0530258 .of_match_table = mxs_pwm_dt_ids,
Shawn Guo4dce82c2012-04-04 10:50:52 +0800259 },
260 .probe = mxs_pwm_probe,
Bill Pembertonfd109112012-11-19 13:21:28 -0500261 .remove = mxs_pwm_remove,
Shawn Guo4dce82c2012-04-04 10:50:52 +0800262};
263module_platform_driver(mxs_pwm_driver);
264
265MODULE_ALIAS("platform:mxs-pwm");
266MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
267MODULE_DESCRIPTION("Freescale MXS PWM Driver");
268MODULE_LICENSE("GPL v2");