blob: 9a232ebbbf967cc048a873b0aef002557114141a [file] [log] [blame]
Philip, Avinash19891b22012-07-25 16:58:19 +05301/*
2 * EHRPWM PWM driver
3 *
4 * Copyright (C) 2012 Texas Instruments, Inc. - http://www.ti.com/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/pwm.h>
24#include <linux/io.h>
25#include <linux/err.h>
26#include <linux/clk.h>
27#include <linux/pm_runtime.h>
Philip, Avinash53ad9e82012-11-27 14:18:13 +053028#include <linux/of_device.h>
29
Philip, Avinash19891b22012-07-25 16:58:19 +053030/* EHRPWM registers and bits definitions */
31
32/* Time base module registers */
33#define TBCTL 0x00
34#define TBPRD 0x0A
35
36#define TBCTL_RUN_MASK (BIT(15) | BIT(14))
37#define TBCTL_STOP_NEXT 0
38#define TBCTL_STOP_ON_CYCLE BIT(14)
39#define TBCTL_FREE_RUN (BIT(15) | BIT(14))
40#define TBCTL_PRDLD_MASK BIT(3)
41#define TBCTL_PRDLD_SHDW 0
42#define TBCTL_PRDLD_IMDT BIT(3)
43#define TBCTL_CLKDIV_MASK (BIT(12) | BIT(11) | BIT(10) | BIT(9) | \
44 BIT(8) | BIT(7))
45#define TBCTL_CTRMODE_MASK (BIT(1) | BIT(0))
46#define TBCTL_CTRMODE_UP 0
47#define TBCTL_CTRMODE_DOWN BIT(0)
48#define TBCTL_CTRMODE_UPDOWN BIT(1)
49#define TBCTL_CTRMODE_FREEZE (BIT(1) | BIT(0))
50
51#define TBCTL_HSPCLKDIV_SHIFT 7
52#define TBCTL_CLKDIV_SHIFT 10
53
54#define CLKDIV_MAX 7
55#define HSPCLKDIV_MAX 7
56#define PERIOD_MAX 0xFFFF
57
58/* compare module registers */
59#define CMPA 0x12
60#define CMPB 0x14
61
62/* Action qualifier module registers */
63#define AQCTLA 0x16
64#define AQCTLB 0x18
65#define AQSFRC 0x1A
66#define AQCSFRC 0x1C
67
68#define AQCTL_CBU_MASK (BIT(9) | BIT(8))
69#define AQCTL_CBU_FRCLOW BIT(8)
70#define AQCTL_CBU_FRCHIGH BIT(9)
71#define AQCTL_CBU_FRCTOGGLE (BIT(9) | BIT(8))
72#define AQCTL_CAU_MASK (BIT(5) | BIT(4))
73#define AQCTL_CAU_FRCLOW BIT(4)
74#define AQCTL_CAU_FRCHIGH BIT(5)
75#define AQCTL_CAU_FRCTOGGLE (BIT(5) | BIT(4))
76#define AQCTL_PRD_MASK (BIT(3) | BIT(2))
77#define AQCTL_PRD_FRCLOW BIT(2)
78#define AQCTL_PRD_FRCHIGH BIT(3)
79#define AQCTL_PRD_FRCTOGGLE (BIT(3) | BIT(2))
80#define AQCTL_ZRO_MASK (BIT(1) | BIT(0))
81#define AQCTL_ZRO_FRCLOW BIT(0)
82#define AQCTL_ZRO_FRCHIGH BIT(1)
83#define AQCTL_ZRO_FRCTOGGLE (BIT(1) | BIT(0))
84
Philip, Avinashdaa56292012-09-06 10:40:03 +053085#define AQCTL_CHANA_POLNORMAL (AQCTL_CAU_FRCLOW | AQCTL_PRD_FRCHIGH | \
86 AQCTL_ZRO_FRCHIGH)
87#define AQCTL_CHANA_POLINVERSED (AQCTL_CAU_FRCHIGH | AQCTL_PRD_FRCLOW | \
88 AQCTL_ZRO_FRCLOW)
89#define AQCTL_CHANB_POLNORMAL (AQCTL_CBU_FRCLOW | AQCTL_PRD_FRCHIGH | \
90 AQCTL_ZRO_FRCHIGH)
91#define AQCTL_CHANB_POLINVERSED (AQCTL_CBU_FRCHIGH | AQCTL_PRD_FRCLOW | \
92 AQCTL_ZRO_FRCLOW)
93
Philip, Avinash19891b22012-07-25 16:58:19 +053094#define AQSFRC_RLDCSF_MASK (BIT(7) | BIT(6))
95#define AQSFRC_RLDCSF_ZRO 0
96#define AQSFRC_RLDCSF_PRD BIT(6)
97#define AQSFRC_RLDCSF_ZROPRD BIT(7)
98#define AQSFRC_RLDCSF_IMDT (BIT(7) | BIT(6))
99
100#define AQCSFRC_CSFB_MASK (BIT(3) | BIT(2))
101#define AQCSFRC_CSFB_FRCDIS 0
102#define AQCSFRC_CSFB_FRCLOW BIT(2)
103#define AQCSFRC_CSFB_FRCHIGH BIT(3)
104#define AQCSFRC_CSFB_DISSWFRC (BIT(3) | BIT(2))
105#define AQCSFRC_CSFA_MASK (BIT(1) | BIT(0))
106#define AQCSFRC_CSFA_FRCDIS 0
107#define AQCSFRC_CSFA_FRCLOW BIT(0)
108#define AQCSFRC_CSFA_FRCHIGH BIT(1)
109#define AQCSFRC_CSFA_DISSWFRC (BIT(1) | BIT(0))
110
111#define NUM_PWM_CHANNEL 2 /* EHRPWM channels */
112
Philip Avinash0e2feb12013-01-17 14:50:02 +0530113struct ehrpwm_context {
114 u16 tbctl;
115 u16 tbprd;
116 u16 cmpa;
117 u16 cmpb;
118 u16 aqctla;
119 u16 aqctlb;
120 u16 aqsfrc;
121 u16 aqcsfrc;
122};
123
Philip, Avinash19891b22012-07-25 16:58:19 +0530124struct ehrpwm_pwm_chip {
125 struct pwm_chip chip;
126 unsigned int clk_rate;
127 void __iomem *mmio_base;
Philip, Avinash01b2d452012-09-06 10:44:25 +0530128 unsigned long period_cycles[NUM_PWM_CHANNEL];
Philip, Avinashdaa56292012-09-06 10:40:03 +0530129 enum pwm_polarity polarity[NUM_PWM_CHANNEL];
Philip, Avinashd91861d2012-11-27 14:18:12 +0530130 struct clk *tbclk;
Philip Avinash0e2feb12013-01-17 14:50:02 +0530131 struct ehrpwm_context ctx;
Philip, Avinash19891b22012-07-25 16:58:19 +0530132};
133
134static inline struct ehrpwm_pwm_chip *to_ehrpwm_pwm_chip(struct pwm_chip *chip)
135{
136 return container_of(chip, struct ehrpwm_pwm_chip, chip);
137}
138
Wolfram Sang3ea57ea2014-04-02 13:56:22 +0200139static inline u16 ehrpwm_read(void __iomem *base, int offset)
Philip Avinash0e2feb12013-01-17 14:50:02 +0530140{
141 return readw(base + offset);
142}
143
Wolfram Sang3ea57ea2014-04-02 13:56:22 +0200144static inline void ehrpwm_write(void __iomem *base, int offset, unsigned int val)
Philip, Avinash19891b22012-07-25 16:58:19 +0530145{
146 writew(val & 0xFFFF, base + offset);
147}
148
Jingoo Hanac872bc2013-08-02 15:22:03 +0900149static void ehrpwm_modify(void __iomem *base, int offset,
Philip, Avinash19891b22012-07-25 16:58:19 +0530150 unsigned short mask, unsigned short val)
151{
152 unsigned short regval;
153
154 regval = readw(base + offset);
155 regval &= ~mask;
156 regval |= val & mask;
157 writew(regval, base + offset);
158}
159
160/**
161 * set_prescale_div - Set up the prescaler divider function
162 * @rqst_prescaler: prescaler value min
163 * @prescale_div: prescaler value set
164 * @tb_clk_div: Time Base Control prescaler bits
165 */
166static int set_prescale_div(unsigned long rqst_prescaler,
167 unsigned short *prescale_div, unsigned short *tb_clk_div)
168{
169 unsigned int clkdiv, hspclkdiv;
170
171 for (clkdiv = 0; clkdiv <= CLKDIV_MAX; clkdiv++) {
172 for (hspclkdiv = 0; hspclkdiv <= HSPCLKDIV_MAX; hspclkdiv++) {
173
174 /*
175 * calculations for prescaler value :
176 * prescale_div = HSPCLKDIVIDER * CLKDIVIDER.
177 * HSPCLKDIVIDER = 2 ** hspclkdiv
178 * CLKDIVIDER = (1), if clkdiv == 0 *OR*
179 * (2 * clkdiv), if clkdiv != 0
180 *
181 * Configure prescale_div value such that period
182 * register value is less than 65535.
183 */
184
185 *prescale_div = (1 << clkdiv) *
186 (hspclkdiv ? (hspclkdiv * 2) : 1);
187 if (*prescale_div > rqst_prescaler) {
188 *tb_clk_div = (clkdiv << TBCTL_CLKDIV_SHIFT) |
189 (hspclkdiv << TBCTL_HSPCLKDIV_SHIFT);
190 return 0;
191 }
192 }
193 }
194 return 1;
195}
196
Philip, Avinashdaa56292012-09-06 10:40:03 +0530197static void configure_polarity(struct ehrpwm_pwm_chip *pc, int chan)
Philip, Avinash19891b22012-07-25 16:58:19 +0530198{
Philip, Avinashdaa56292012-09-06 10:40:03 +0530199 int aqctl_reg;
Philip, Avinash19891b22012-07-25 16:58:19 +0530200 unsigned short aqctl_val, aqctl_mask;
201
202 /*
Philip, Avinashdaa56292012-09-06 10:40:03 +0530203 * Configure PWM output to HIGH/LOW level on counter
204 * reaches compare register value and LOW/HIGH level
205 * on counter value reaches period register value and
206 * zero value on counter
Philip, Avinash19891b22012-07-25 16:58:19 +0530207 */
208 if (chan == 1) {
209 aqctl_reg = AQCTLB;
Philip, Avinash19891b22012-07-25 16:58:19 +0530210 aqctl_mask = AQCTL_CBU_MASK;
Philip, Avinashdaa56292012-09-06 10:40:03 +0530211
212 if (pc->polarity[chan] == PWM_POLARITY_INVERSED)
213 aqctl_val = AQCTL_CHANB_POLINVERSED;
214 else
215 aqctl_val = AQCTL_CHANB_POLNORMAL;
Philip, Avinash19891b22012-07-25 16:58:19 +0530216 } else {
Philip, Avinash19891b22012-07-25 16:58:19 +0530217 aqctl_reg = AQCTLA;
Philip, Avinash19891b22012-07-25 16:58:19 +0530218 aqctl_mask = AQCTL_CAU_MASK;
Philip, Avinashdaa56292012-09-06 10:40:03 +0530219
220 if (pc->polarity[chan] == PWM_POLARITY_INVERSED)
221 aqctl_val = AQCTL_CHANA_POLINVERSED;
222 else
223 aqctl_val = AQCTL_CHANA_POLNORMAL;
Philip, Avinash19891b22012-07-25 16:58:19 +0530224 }
225
Philip, Avinash19891b22012-07-25 16:58:19 +0530226 aqctl_mask |= AQCTL_PRD_MASK | AQCTL_ZRO_MASK;
Philip, Avinashdaa56292012-09-06 10:40:03 +0530227 ehrpwm_modify(pc->mmio_base, aqctl_reg, aqctl_mask, aqctl_val);
Philip, Avinash19891b22012-07-25 16:58:19 +0530228}
229
230/*
231 * period_ns = 10^9 * (ps_divval * period_cycles) / PWM_CLK_RATE
232 * duty_ns = 10^9 * (ps_divval * duty_cycles) / PWM_CLK_RATE
233 */
234static int ehrpwm_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
235 int duty_ns, int period_ns)
236{
237 struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
238 unsigned long long c;
239 unsigned long period_cycles, duty_cycles;
240 unsigned short ps_divval, tb_divval;
Philip, Avinashdaa56292012-09-06 10:40:03 +0530241 int i, cmp_reg;
Philip, Avinash19891b22012-07-25 16:58:19 +0530242
Thierry Redingc2d476a2012-09-02 22:13:40 +0200243 if (period_ns > NSEC_PER_SEC)
Philip, Avinash19891b22012-07-25 16:58:19 +0530244 return -ERANGE;
245
246 c = pc->clk_rate;
247 c = c * period_ns;
248 do_div(c, NSEC_PER_SEC);
249 period_cycles = (unsigned long)c;
250
251 if (period_cycles < 1) {
252 period_cycles = 1;
253 duty_cycles = 1;
254 } else {
255 c = pc->clk_rate;
256 c = c * duty_ns;
257 do_div(c, NSEC_PER_SEC);
258 duty_cycles = (unsigned long)c;
259 }
260
Philip, Avinash01b2d452012-09-06 10:44:25 +0530261 /*
262 * Period values should be same for multiple PWM channels as IP uses
263 * same period register for multiple channels.
264 */
265 for (i = 0; i < NUM_PWM_CHANNEL; i++) {
266 if (pc->period_cycles[i] &&
267 (pc->period_cycles[i] != period_cycles)) {
268 /*
269 * Allow channel to reconfigure period if no other
270 * channels being configured.
271 */
272 if (i == pwm->hwpwm)
273 continue;
274
275 dev_err(chip->dev, "Period value conflicts with channel %d\n",
276 i);
277 return -EINVAL;
278 }
279 }
280
281 pc->period_cycles[pwm->hwpwm] = period_cycles;
282
Philip, Avinash19891b22012-07-25 16:58:19 +0530283 /* Configure clock prescaler to support Low frequency PWM wave */
284 if (set_prescale_div(period_cycles/PERIOD_MAX, &ps_divval,
285 &tb_divval)) {
286 dev_err(chip->dev, "Unsupported values\n");
287 return -EINVAL;
288 }
289
290 pm_runtime_get_sync(chip->dev);
291
292 /* Update clock prescaler values */
293 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CLKDIV_MASK, tb_divval);
294
295 /* Update period & duty cycle with presacler division */
296 period_cycles = period_cycles / ps_divval;
297 duty_cycles = duty_cycles / ps_divval;
298
299 /* Configure shadow loading on Period register */
300 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_PRDLD_MASK, TBCTL_PRDLD_SHDW);
301
302 ehrpwm_write(pc->mmio_base, TBPRD, period_cycles);
303
304 /* Configure ehrpwm counter for up-count mode */
305 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CTRMODE_MASK,
306 TBCTL_CTRMODE_UP);
307
Philip, Avinashdaa56292012-09-06 10:40:03 +0530308 if (pwm->hwpwm == 1)
309 /* Channel 1 configured with compare B register */
310 cmp_reg = CMPB;
311 else
312 /* Channel 0 configured with compare A register */
313 cmp_reg = CMPA;
314
315 ehrpwm_write(pc->mmio_base, cmp_reg, duty_cycles);
316
Philip, Avinash19891b22012-07-25 16:58:19 +0530317 pm_runtime_put_sync(chip->dev);
318 return 0;
319}
320
Philip, Avinashdaa56292012-09-06 10:40:03 +0530321static int ehrpwm_pwm_set_polarity(struct pwm_chip *chip,
322 struct pwm_device *pwm, enum pwm_polarity polarity)
323{
324 struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
325
326 /* Configuration of polarity in hardware delayed, do at enable */
327 pc->polarity[pwm->hwpwm] = polarity;
328 return 0;
329}
330
Philip, Avinash19891b22012-07-25 16:58:19 +0530331static int ehrpwm_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
332{
333 struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
334 unsigned short aqcsfrc_val, aqcsfrc_mask;
Philip, Avinash0074b492013-01-10 18:35:26 +0530335 int ret;
Philip, Avinash19891b22012-07-25 16:58:19 +0530336
337 /* Leave clock enabled on enabling PWM */
338 pm_runtime_get_sync(chip->dev);
339
340 /* Disabling Action Qualifier on PWM output */
341 if (pwm->hwpwm) {
342 aqcsfrc_val = AQCSFRC_CSFB_FRCDIS;
343 aqcsfrc_mask = AQCSFRC_CSFB_MASK;
344 } else {
345 aqcsfrc_val = AQCSFRC_CSFA_FRCDIS;
346 aqcsfrc_mask = AQCSFRC_CSFA_MASK;
347 }
348
349 /* Changes to shadow mode */
350 ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
351 AQSFRC_RLDCSF_ZRO);
352
353 ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
354
Philip, Avinashdaa56292012-09-06 10:40:03 +0530355 /* Channels polarity can be configured from action qualifier module */
356 configure_polarity(pc, pwm->hwpwm);
357
Philip, Avinashd91861d2012-11-27 14:18:12 +0530358 /* Enable TBCLK before enabling PWM device */
Marek Beliskob388f152013-06-26 14:38:04 +0200359 ret = clk_enable(pc->tbclk);
Philip, Avinash0074b492013-01-10 18:35:26 +0530360 if (ret) {
Jingoo Han82569e52013-12-19 14:12:05 +0900361 dev_err(chip->dev, "Failed to enable TBCLK for %s\n",
362 dev_name(pc->chip.dev));
Philip, Avinash0074b492013-01-10 18:35:26 +0530363 return ret;
364 }
Philip, Avinashd91861d2012-11-27 14:18:12 +0530365
Philip, Avinash19891b22012-07-25 16:58:19 +0530366 /* Enable time counter for free_run */
367 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_RUN_MASK, TBCTL_FREE_RUN);
368 return 0;
369}
370
371static void ehrpwm_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
372{
373 struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
374 unsigned short aqcsfrc_val, aqcsfrc_mask;
375
376 /* Action Qualifier puts PWM output low forcefully */
377 if (pwm->hwpwm) {
378 aqcsfrc_val = AQCSFRC_CSFB_FRCLOW;
379 aqcsfrc_mask = AQCSFRC_CSFB_MASK;
380 } else {
381 aqcsfrc_val = AQCSFRC_CSFA_FRCLOW;
382 aqcsfrc_mask = AQCSFRC_CSFA_MASK;
383 }
384
Vignesh R80013172018-06-11 11:39:56 +0530385 /* Update shadow register first before modifying active register */
Christoph Vogtländer2ed35402019-03-12 14:38:46 +0530386 ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
387 AQSFRC_RLDCSF_ZRO);
Vignesh R80013172018-06-11 11:39:56 +0530388 ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
Philip, Avinash19891b22012-07-25 16:58:19 +0530389 /*
390 * Changes to immediate action on Action Qualifier. This puts
391 * Action Qualifier control on PWM output from next TBCLK
392 */
393 ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
394 AQSFRC_RLDCSF_IMDT);
395
396 ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
397
Philip, Avinashd91861d2012-11-27 14:18:12 +0530398 /* Disabling TBCLK on PWM disable */
Marek Beliskob388f152013-06-26 14:38:04 +0200399 clk_disable(pc->tbclk);
Philip, Avinashd91861d2012-11-27 14:18:12 +0530400
Philip, Avinash19891b22012-07-25 16:58:19 +0530401 /* Stop Time base counter */
402 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_RUN_MASK, TBCTL_STOP_NEXT);
403
404 /* Disable clock on PWM disable */
405 pm_runtime_put_sync(chip->dev);
406}
407
408static void ehrpwm_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
409{
Philip, Avinash01b2d452012-09-06 10:44:25 +0530410 struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
411
Boris Brezillon5c312522015-07-01 10:21:47 +0200412 if (pwm_is_enabled(pwm)) {
Philip, Avinash19891b22012-07-25 16:58:19 +0530413 dev_warn(chip->dev, "Removing PWM device without disabling\n");
414 pm_runtime_put_sync(chip->dev);
415 }
Philip, Avinash01b2d452012-09-06 10:44:25 +0530416
417 /* set period value to zero on free */
418 pc->period_cycles[pwm->hwpwm] = 0;
Philip, Avinash19891b22012-07-25 16:58:19 +0530419}
420
421static const struct pwm_ops ehrpwm_pwm_ops = {
422 .free = ehrpwm_pwm_free,
423 .config = ehrpwm_pwm_config,
Philip, Avinashdaa56292012-09-06 10:40:03 +0530424 .set_polarity = ehrpwm_pwm_set_polarity,
Philip, Avinash19891b22012-07-25 16:58:19 +0530425 .enable = ehrpwm_pwm_enable,
426 .disable = ehrpwm_pwm_disable,
427 .owner = THIS_MODULE,
428};
429
Philip, Avinash53ad9e82012-11-27 14:18:13 +0530430static const struct of_device_id ehrpwm_of_match[] = {
Cooper Jr., Franklinae5200d2016-05-03 10:56:52 -0500431 { .compatible = "ti,am3352-ehrpwm" },
Philip, Avinash53ad9e82012-11-27 14:18:13 +0530432 { .compatible = "ti,am33xx-ehrpwm" },
433 {},
434};
435MODULE_DEVICE_TABLE(of, ehrpwm_of_match);
436
Bill Pemberton3e9fe832012-11-19 13:23:14 -0500437static int ehrpwm_pwm_probe(struct platform_device *pdev)
Philip, Avinash19891b22012-07-25 16:58:19 +0530438{
Cooper Jr., Franklinae5200d2016-05-03 10:56:52 -0500439 struct device_node *np = pdev->dev.of_node;
Philip, Avinash19891b22012-07-25 16:58:19 +0530440 int ret;
441 struct resource *r;
442 struct clk *clk;
443 struct ehrpwm_pwm_chip *pc;
444
445 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
Jingoo Han5e348952014-04-23 18:41:48 +0900446 if (!pc)
Philip, Avinash19891b22012-07-25 16:58:19 +0530447 return -ENOMEM;
Philip, Avinash19891b22012-07-25 16:58:19 +0530448
449 clk = devm_clk_get(&pdev->dev, "fck");
450 if (IS_ERR(clk)) {
Cooper Jr., Franklinae5200d2016-05-03 10:56:52 -0500451 if (of_device_is_compatible(np, "ti,am33xx-ecap")) {
452 dev_warn(&pdev->dev, "Binding is obsolete.\n");
453 clk = devm_clk_get(pdev->dev.parent, "fck");
454 }
455 }
456
457 if (IS_ERR(clk)) {
Philip, Avinash19891b22012-07-25 16:58:19 +0530458 dev_err(&pdev->dev, "failed to get clock\n");
459 return PTR_ERR(clk);
460 }
461
462 pc->clk_rate = clk_get_rate(clk);
463 if (!pc->clk_rate) {
464 dev_err(&pdev->dev, "failed to get clock rate\n");
465 return -EINVAL;
466 }
467
468 pc->chip.dev = &pdev->dev;
469 pc->chip.ops = &ehrpwm_pwm_ops;
Philip, Avinash53ad9e82012-11-27 14:18:13 +0530470 pc->chip.of_xlate = of_pwm_xlate_with_flags;
471 pc->chip.of_pwm_n_cells = 3;
Philip, Avinash19891b22012-07-25 16:58:19 +0530472 pc->chip.base = -1;
473 pc->chip.npwm = NUM_PWM_CHANNEL;
474
475 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding6d4294d2013-01-21 11:09:16 +0100476 pc->mmio_base = devm_ioremap_resource(&pdev->dev, r);
477 if (IS_ERR(pc->mmio_base))
478 return PTR_ERR(pc->mmio_base);
Philip, Avinash19891b22012-07-25 16:58:19 +0530479
Philip, Avinashd91861d2012-11-27 14:18:12 +0530480 /* Acquire tbclk for Time Base EHRPWM submodule */
481 pc->tbclk = devm_clk_get(&pdev->dev, "tbclk");
482 if (IS_ERR(pc->tbclk)) {
483 dev_err(&pdev->dev, "Failed to get tbclk\n");
484 return PTR_ERR(pc->tbclk);
485 }
486
Marek Beliskob388f152013-06-26 14:38:04 +0200487 ret = clk_prepare(pc->tbclk);
488 if (ret < 0) {
489 dev_err(&pdev->dev, "clk_prepare() failed: %d\n", ret);
490 return ret;
491 }
492
Philip, Avinash19891b22012-07-25 16:58:19 +0530493 ret = pwmchip_add(&pc->chip);
494 if (ret < 0) {
495 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
496 return ret;
497 }
498
499 pm_runtime_enable(&pdev->dev);
Philip, Avinash53ad9e82012-11-27 14:18:13 +0530500
Philip, Avinash19891b22012-07-25 16:58:19 +0530501 platform_set_drvdata(pdev, pc);
502 return 0;
503}
504
Bill Pemberton77f37912012-11-19 13:26:09 -0500505static int ehrpwm_pwm_remove(struct platform_device *pdev)
Philip, Avinash19891b22012-07-25 16:58:19 +0530506{
507 struct ehrpwm_pwm_chip *pc = platform_get_drvdata(pdev);
508
Marek Beliskob388f152013-06-26 14:38:04 +0200509 clk_unprepare(pc->tbclk);
510
Philip, Avinash19891b22012-07-25 16:58:19 +0530511 pm_runtime_put_sync(&pdev->dev);
512 pm_runtime_disable(&pdev->dev);
513 return pwmchip_remove(&pc->chip);
514}
515
Wolfram Sangaf5935e2014-04-02 13:56:21 +0200516#ifdef CONFIG_PM_SLEEP
Axel Linb343a182013-03-26 22:55:57 +0800517static void ehrpwm_pwm_save_context(struct ehrpwm_pwm_chip *pc)
Philip Avinash0e2feb12013-01-17 14:50:02 +0530518{
519 pm_runtime_get_sync(pc->chip.dev);
520 pc->ctx.tbctl = ehrpwm_read(pc->mmio_base, TBCTL);
521 pc->ctx.tbprd = ehrpwm_read(pc->mmio_base, TBPRD);
522 pc->ctx.cmpa = ehrpwm_read(pc->mmio_base, CMPA);
523 pc->ctx.cmpb = ehrpwm_read(pc->mmio_base, CMPB);
524 pc->ctx.aqctla = ehrpwm_read(pc->mmio_base, AQCTLA);
525 pc->ctx.aqctlb = ehrpwm_read(pc->mmio_base, AQCTLB);
526 pc->ctx.aqsfrc = ehrpwm_read(pc->mmio_base, AQSFRC);
527 pc->ctx.aqcsfrc = ehrpwm_read(pc->mmio_base, AQCSFRC);
528 pm_runtime_put_sync(pc->chip.dev);
529}
530
Axel Linb343a182013-03-26 22:55:57 +0800531static void ehrpwm_pwm_restore_context(struct ehrpwm_pwm_chip *pc)
Philip Avinash0e2feb12013-01-17 14:50:02 +0530532{
533 ehrpwm_write(pc->mmio_base, TBPRD, pc->ctx.tbprd);
534 ehrpwm_write(pc->mmio_base, CMPA, pc->ctx.cmpa);
535 ehrpwm_write(pc->mmio_base, CMPB, pc->ctx.cmpb);
536 ehrpwm_write(pc->mmio_base, AQCTLA, pc->ctx.aqctla);
537 ehrpwm_write(pc->mmio_base, AQCTLB, pc->ctx.aqctlb);
538 ehrpwm_write(pc->mmio_base, AQSFRC, pc->ctx.aqsfrc);
539 ehrpwm_write(pc->mmio_base, AQCSFRC, pc->ctx.aqcsfrc);
540 ehrpwm_write(pc->mmio_base, TBCTL, pc->ctx.tbctl);
541}
542
543static int ehrpwm_pwm_suspend(struct device *dev)
544{
545 struct ehrpwm_pwm_chip *pc = dev_get_drvdata(dev);
546 int i;
547
548 ehrpwm_pwm_save_context(pc);
549 for (i = 0; i < pc->chip.npwm; i++) {
550 struct pwm_device *pwm = &pc->chip.pwms[i];
551
Boris Brezillon5c312522015-07-01 10:21:47 +0200552 if (!pwm_is_enabled(pwm))
Philip Avinash0e2feb12013-01-17 14:50:02 +0530553 continue;
554
555 /* Disable explicitly if PWM is running */
556 pm_runtime_put_sync(dev);
557 }
558 return 0;
559}
560
561static int ehrpwm_pwm_resume(struct device *dev)
562{
563 struct ehrpwm_pwm_chip *pc = dev_get_drvdata(dev);
564 int i;
565
566 for (i = 0; i < pc->chip.npwm; i++) {
567 struct pwm_device *pwm = &pc->chip.pwms[i];
568
Boris Brezillon5c312522015-07-01 10:21:47 +0200569 if (!pwm_is_enabled(pwm))
Philip Avinash0e2feb12013-01-17 14:50:02 +0530570 continue;
571
572 /* Enable explicitly if PWM was running */
573 pm_runtime_get_sync(dev);
574 }
575 ehrpwm_pwm_restore_context(pc);
576 return 0;
577}
Jingoo Han29258b22013-03-11 11:14:41 +0900578#endif
Philip Avinash0e2feb12013-01-17 14:50:02 +0530579
580static SIMPLE_DEV_PM_OPS(ehrpwm_pwm_pm_ops, ehrpwm_pwm_suspend,
581 ehrpwm_pwm_resume);
582
Philip, Avinash19891b22012-07-25 16:58:19 +0530583static struct platform_driver ehrpwm_pwm_driver = {
584 .driver = {
Philip, Avinash53ad9e82012-11-27 14:18:13 +0530585 .name = "ehrpwm",
Philip, Avinash53ad9e82012-11-27 14:18:13 +0530586 .of_match_table = ehrpwm_of_match,
Philip Avinash0e2feb12013-01-17 14:50:02 +0530587 .pm = &ehrpwm_pwm_pm_ops,
Philip, Avinash19891b22012-07-25 16:58:19 +0530588 },
589 .probe = ehrpwm_pwm_probe,
Bill Pembertonfd109112012-11-19 13:21:28 -0500590 .remove = ehrpwm_pwm_remove,
Philip, Avinash19891b22012-07-25 16:58:19 +0530591};
592
593module_platform_driver(ehrpwm_pwm_driver);
594
595MODULE_DESCRIPTION("EHRPWM PWM driver");
596MODULE_AUTHOR("Texas Instruments");
597MODULE_LICENSE("GPL");