blob: 4701f0c9b9219aac6710f99399c98d181b8fbabe [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +05302/*
3 * ECAP PWM driver
4 *
Alexander A. Klimov216a0942020-07-08 19:59:24 +02005 * Copyright (C) 2012 Texas Instruments, Inc. - https://www.ti.com/
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +05306 */
7
8#include <linux/module.h>
9#include <linux/platform_device.h>
10#include <linux/io.h>
11#include <linux/err.h>
12#include <linux/clk.h>
13#include <linux/pm_runtime.h>
14#include <linux/pwm.h>
Philip, Avinash333b08e2012-11-27 14:18:09 +053015#include <linux/of_device.h>
16
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053017/* ECAP registers and bits definitions */
18#define CAP1 0x08
19#define CAP2 0x0C
20#define CAP3 0x10
21#define CAP4 0x14
22#define ECCTL2 0x2A
Philip, Avinash454870a2012-09-06 10:40:02 +053023#define ECCTL2_APWM_POL_LOW BIT(10)
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053024#define ECCTL2_APWM_MODE BIT(9)
25#define ECCTL2_SYNC_SEL_DISA (BIT(7) | BIT(6))
26#define ECCTL2_TSCTR_FREERUN BIT(4)
27
Philip Avinash0d75c202013-01-17 14:50:03 +053028struct ecap_context {
Thierry Reding53c79722017-08-21 08:29:41 +020029 u32 cap3;
30 u32 cap4;
31 u16 ecctl2;
Philip Avinash0d75c202013-01-17 14:50:03 +053032};
33
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053034struct ecap_pwm_chip {
Thierry Reding53c79722017-08-21 08:29:41 +020035 struct pwm_chip chip;
36 unsigned int clk_rate;
37 void __iomem *mmio_base;
Philip Avinash0d75c202013-01-17 14:50:03 +053038 struct ecap_context ctx;
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053039};
40
41static inline struct ecap_pwm_chip *to_ecap_pwm_chip(struct pwm_chip *chip)
42{
43 return container_of(chip, struct ecap_pwm_chip, chip);
44}
45
46/*
47 * period_ns = 10^9 * period_cycles / PWM_CLK_RATE
48 * duty_ns = 10^9 * duty_cycles / PWM_CLK_RATE
49 */
50static int ecap_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
Uwe Kleine-König0ca7acd2021-05-01 18:01:39 +020051 int duty_ns, int period_ns, int enabled)
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053052{
53 struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
Thierry Reding53c79722017-08-21 08:29:41 +020054 u32 period_cycles, duty_cycles;
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053055 unsigned long long c;
Thierry Reding53c79722017-08-21 08:29:41 +020056 u16 value;
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053057
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053058 c = pc->clk_rate;
59 c = c * period_ns;
60 do_div(c, NSEC_PER_SEC);
Thierry Reding53c79722017-08-21 08:29:41 +020061 period_cycles = (u32)c;
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053062
63 if (period_cycles < 1) {
64 period_cycles = 1;
65 duty_cycles = 1;
66 } else {
67 c = pc->clk_rate;
68 c = c * duty_ns;
69 do_div(c, NSEC_PER_SEC);
Thierry Reding53c79722017-08-21 08:29:41 +020070 duty_cycles = (u32)c;
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053071 }
72
73 pm_runtime_get_sync(pc->chip.dev);
74
Thierry Reding53c79722017-08-21 08:29:41 +020075 value = readw(pc->mmio_base + ECCTL2);
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053076
77 /* Configure APWM mode & disable sync option */
Thierry Reding53c79722017-08-21 08:29:41 +020078 value |= ECCTL2_APWM_MODE | ECCTL2_SYNC_SEL_DISA;
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053079
Thierry Reding53c79722017-08-21 08:29:41 +020080 writew(value, pc->mmio_base + ECCTL2);
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053081
Uwe Kleine-König0ca7acd2021-05-01 18:01:39 +020082 if (!enabled) {
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +053083 /* Update active registers if not running */
84 writel(duty_cycles, pc->mmio_base + CAP2);
85 writel(period_cycles, pc->mmio_base + CAP1);
86 } else {
87 /*
88 * Update shadow registers to configure period and
89 * compare values. This helps current PWM period to
90 * complete on reconfiguring
91 */
92 writel(duty_cycles, pc->mmio_base + CAP4);
93 writel(period_cycles, pc->mmio_base + CAP3);
94 }
95
Uwe Kleine-König0ca7acd2021-05-01 18:01:39 +020096 if (!enabled) {
Thierry Reding53c79722017-08-21 08:29:41 +020097 value = readw(pc->mmio_base + ECCTL2);
Philip, Avinashc06fad92012-08-23 12:29:46 +053098 /* Disable APWM mode to put APWM output Low */
Thierry Reding53c79722017-08-21 08:29:41 +020099 value &= ~ECCTL2_APWM_MODE;
100 writew(value, pc->mmio_base + ECCTL2);
Philip, Avinashc06fad92012-08-23 12:29:46 +0530101 }
102
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530103 pm_runtime_put_sync(pc->chip.dev);
Thierry Reding53c79722017-08-21 08:29:41 +0200104
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530105 return 0;
106}
107
Philip, Avinash454870a2012-09-06 10:40:02 +0530108static int ecap_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
Thierry Reding53c79722017-08-21 08:29:41 +0200109 enum pwm_polarity polarity)
Philip, Avinash454870a2012-09-06 10:40:02 +0530110{
111 struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
Thierry Reding53c79722017-08-21 08:29:41 +0200112 u16 value;
Philip, Avinash454870a2012-09-06 10:40:02 +0530113
114 pm_runtime_get_sync(pc->chip.dev);
Thierry Reding53c79722017-08-21 08:29:41 +0200115
116 value = readw(pc->mmio_base + ECCTL2);
117
Philip, Avinash454870a2012-09-06 10:40:02 +0530118 if (polarity == PWM_POLARITY_INVERSED)
119 /* Duty cycle defines LOW period of PWM */
Thierry Reding53c79722017-08-21 08:29:41 +0200120 value |= ECCTL2_APWM_POL_LOW;
Philip, Avinash454870a2012-09-06 10:40:02 +0530121 else
122 /* Duty cycle defines HIGH period of PWM */
Thierry Reding53c79722017-08-21 08:29:41 +0200123 value &= ~ECCTL2_APWM_POL_LOW;
Philip, Avinash454870a2012-09-06 10:40:02 +0530124
Thierry Reding53c79722017-08-21 08:29:41 +0200125 writew(value, pc->mmio_base + ECCTL2);
126
Philip, Avinash454870a2012-09-06 10:40:02 +0530127 pm_runtime_put_sync(pc->chip.dev);
Thierry Reding53c79722017-08-21 08:29:41 +0200128
Philip, Avinash454870a2012-09-06 10:40:02 +0530129 return 0;
130}
131
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530132static int ecap_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
133{
134 struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
Thierry Reding53c79722017-08-21 08:29:41 +0200135 u16 value;
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530136
137 /* Leave clock enabled on enabling PWM */
138 pm_runtime_get_sync(pc->chip.dev);
139
140 /*
141 * Enable 'Free run Time stamp counter mode' to start counter
142 * and 'APWM mode' to enable APWM output
143 */
Thierry Reding53c79722017-08-21 08:29:41 +0200144 value = readw(pc->mmio_base + ECCTL2);
145 value |= ECCTL2_TSCTR_FREERUN | ECCTL2_APWM_MODE;
146 writew(value, pc->mmio_base + ECCTL2);
147
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530148 return 0;
149}
150
151static void ecap_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
152{
153 struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip);
Thierry Reding53c79722017-08-21 08:29:41 +0200154 u16 value;
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530155
156 /*
157 * Disable 'Free run Time stamp counter mode' to stop counter
158 * and 'APWM mode' to put APWM output to low
159 */
Thierry Reding53c79722017-08-21 08:29:41 +0200160 value = readw(pc->mmio_base + ECCTL2);
161 value &= ~(ECCTL2_TSCTR_FREERUN | ECCTL2_APWM_MODE);
162 writew(value, pc->mmio_base + ECCTL2);
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530163
164 /* Disable clock on PWM disable */
165 pm_runtime_put_sync(pc->chip.dev);
166}
167
Uwe Kleine-König0ca7acd2021-05-01 18:01:39 +0200168static int ecap_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
169 const struct pwm_state *state)
170{
171 int err;
172 int enabled = pwm->state.enabled;
173
174 if (state->polarity != pwm->state.polarity) {
175
176 if (enabled) {
177 ecap_pwm_disable(chip, pwm);
178 enabled = false;
179 }
180
181 err = ecap_pwm_set_polarity(chip, pwm, state->polarity);
182 if (err)
183 return err;
184 }
185
186 if (!state->enabled) {
187 if (enabled)
188 ecap_pwm_disable(chip, pwm);
189 return 0;
190 }
191
Uwe Kleine-König25f70b82021-07-01 10:27:53 +0200192 if (state->period > NSEC_PER_SEC)
193 return -ERANGE;
Uwe Kleine-König0ca7acd2021-05-01 18:01:39 +0200194
Uwe Kleine-König25f70b82021-07-01 10:27:53 +0200195 err = ecap_pwm_config(chip, pwm, state->duty_cycle,
196 state->period, enabled);
197 if (err)
198 return err;
Uwe Kleine-König0ca7acd2021-05-01 18:01:39 +0200199
200 if (!enabled)
201 return ecap_pwm_enable(chip, pwm);
202
203 return 0;
204}
205
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530206static const struct pwm_ops ecap_pwm_ops = {
Uwe Kleine-König0ca7acd2021-05-01 18:01:39 +0200207 .apply = ecap_pwm_apply,
Thierry Reding53c79722017-08-21 08:29:41 +0200208 .owner = THIS_MODULE,
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530209};
210
Philip, Avinash333b08e2012-11-27 14:18:09 +0530211static const struct of_device_id ecap_of_match[] = {
Cooper Jr., Franklinae5200d2016-05-03 10:56:52 -0500212 { .compatible = "ti,am3352-ecap" },
Philip, Avinash333b08e2012-11-27 14:18:09 +0530213 { .compatible = "ti,am33xx-ecap" },
214 {},
215};
216MODULE_DEVICE_TABLE(of, ecap_of_match);
217
Bill Pemberton3e9fe832012-11-19 13:23:14 -0500218static int ecap_pwm_probe(struct platform_device *pdev)
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530219{
Cooper Jr., Franklinae5200d2016-05-03 10:56:52 -0500220 struct device_node *np = pdev->dev.of_node;
Thierry Reding53c79722017-08-21 08:29:41 +0200221 struct ecap_pwm_chip *pc;
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530222 struct clk *clk;
Thierry Reding53c79722017-08-21 08:29:41 +0200223 int ret;
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530224
225 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
Jingoo Hanc10d5062014-04-23 18:41:27 +0900226 if (!pc)
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530227 return -ENOMEM;
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530228
229 clk = devm_clk_get(&pdev->dev, "fck");
230 if (IS_ERR(clk)) {
Cooper Jr., Franklinae5200d2016-05-03 10:56:52 -0500231 if (of_device_is_compatible(np, "ti,am33xx-ecap")) {
232 dev_warn(&pdev->dev, "Binding is obsolete.\n");
233 clk = devm_clk_get(pdev->dev.parent, "fck");
234 }
235 }
236
237 if (IS_ERR(clk)) {
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530238 dev_err(&pdev->dev, "failed to get clock\n");
239 return PTR_ERR(clk);
240 }
241
242 pc->clk_rate = clk_get_rate(clk);
243 if (!pc->clk_rate) {
244 dev_err(&pdev->dev, "failed to get clock rate\n");
245 return -EINVAL;
246 }
247
248 pc->chip.dev = &pdev->dev;
249 pc->chip.ops = &ecap_pwm_ops;
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530250 pc->chip.npwm = 1;
251
Yangtao Li1dcf0522019-12-29 08:05:59 +0000252 pc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
Thierry Reding6d4294d2013-01-21 11:09:16 +0100253 if (IS_ERR(pc->mmio_base))
254 return PTR_ERR(pc->mmio_base);
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530255
Uwe Kleine-Königa64a5852021-07-07 18:28:18 +0200256 ret = devm_pwmchip_add(&pdev->dev, &pc->chip);
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530257 if (ret < 0) {
258 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
259 return ret;
260 }
261
Thierry Reding23f373e2017-08-21 08:31:37 +0200262 platform_set_drvdata(pdev, pc);
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530263 pm_runtime_enable(&pdev->dev);
Philip, Avinash333b08e2012-11-27 14:18:09 +0530264
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530265 return 0;
266}
267
Bill Pemberton77f37912012-11-19 13:26:09 -0500268static int ecap_pwm_remove(struct platform_device *pdev)
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530269{
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530270 pm_runtime_disable(&pdev->dev);
Thierry Reding53c79722017-08-21 08:29:41 +0200271
Uwe Kleine-Königa64a5852021-07-07 18:28:18 +0200272 return 0;
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530273}
274
Jingoo Han3943a652013-08-02 15:11:18 +0900275#ifdef CONFIG_PM_SLEEP
Axel Lina38c9892013-03-26 22:54:58 +0800276static void ecap_pwm_save_context(struct ecap_pwm_chip *pc)
Philip Avinash0d75c202013-01-17 14:50:03 +0530277{
278 pm_runtime_get_sync(pc->chip.dev);
279 pc->ctx.ecctl2 = readw(pc->mmio_base + ECCTL2);
280 pc->ctx.cap4 = readl(pc->mmio_base + CAP4);
281 pc->ctx.cap3 = readl(pc->mmio_base + CAP3);
282 pm_runtime_put_sync(pc->chip.dev);
283}
284
Axel Lina38c9892013-03-26 22:54:58 +0800285static void ecap_pwm_restore_context(struct ecap_pwm_chip *pc)
Philip Avinash0d75c202013-01-17 14:50:03 +0530286{
287 writel(pc->ctx.cap3, pc->mmio_base + CAP3);
288 writel(pc->ctx.cap4, pc->mmio_base + CAP4);
289 writew(pc->ctx.ecctl2, pc->mmio_base + ECCTL2);
290}
291
292static int ecap_pwm_suspend(struct device *dev)
293{
294 struct ecap_pwm_chip *pc = dev_get_drvdata(dev);
295 struct pwm_device *pwm = pc->chip.pwms;
296
297 ecap_pwm_save_context(pc);
298
299 /* Disable explicitly if PWM is running */
Boris Brezillon5c312522015-07-01 10:21:47 +0200300 if (pwm_is_enabled(pwm))
Philip Avinash0d75c202013-01-17 14:50:03 +0530301 pm_runtime_put_sync(dev);
302
303 return 0;
304}
305
306static int ecap_pwm_resume(struct device *dev)
307{
308 struct ecap_pwm_chip *pc = dev_get_drvdata(dev);
309 struct pwm_device *pwm = pc->chip.pwms;
310
311 /* Enable explicitly if PWM was running */
Boris Brezillon5c312522015-07-01 10:21:47 +0200312 if (pwm_is_enabled(pwm))
Philip Avinash0d75c202013-01-17 14:50:03 +0530313 pm_runtime_get_sync(dev);
314
315 ecap_pwm_restore_context(pc);
316 return 0;
317}
Jingoo Hanb78f5fc2013-03-11 11:12:58 +0900318#endif
Philip Avinash0d75c202013-01-17 14:50:03 +0530319
320static SIMPLE_DEV_PM_OPS(ecap_pwm_pm_ops, ecap_pwm_suspend, ecap_pwm_resume);
321
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530322static struct platform_driver ecap_pwm_driver = {
323 .driver = {
Thierry Reding53c79722017-08-21 08:29:41 +0200324 .name = "ecap",
Philip, Avinash333b08e2012-11-27 14:18:09 +0530325 .of_match_table = ecap_of_match,
Thierry Reding53c79722017-08-21 08:29:41 +0200326 .pm = &ecap_pwm_pm_ops,
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530327 },
328 .probe = ecap_pwm_probe,
Bill Pembertonfd109112012-11-19 13:21:28 -0500329 .remove = ecap_pwm_remove,
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530330};
Philip, Avinash8e0cb05b2012-07-25 16:58:18 +0530331module_platform_driver(ecap_pwm_driver);
332
333MODULE_DESCRIPTION("ECAP PWM driver");
334MODULE_AUTHOR("Texas Instruments");
335MODULE_LICENSE("GPL");