174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
219891b20SPhilip, Avinash /*
319891b20SPhilip, Avinash * EHRPWM PWM driver
419891b20SPhilip, Avinash *
5216a094dSAlexander A. Klimov * Copyright (C) 2012 Texas Instruments, Inc. - https://www.ti.com/
619891b20SPhilip, Avinash */
719891b20SPhilip, Avinash
819891b20SPhilip, Avinash #include <linux/module.h>
919891b20SPhilip, Avinash #include <linux/platform_device.h>
1019891b20SPhilip, Avinash #include <linux/pwm.h>
1119891b20SPhilip, Avinash #include <linux/io.h>
1219891b20SPhilip, Avinash #include <linux/err.h>
1319891b20SPhilip, Avinash #include <linux/clk.h>
1419891b20SPhilip, Avinash #include <linux/pm_runtime.h>
15*0a41b0c5SRob Herring #include <linux/of.h>
1653ad9e8dSPhilip, Avinash
1719891b20SPhilip, Avinash /* EHRPWM registers and bits definitions */
1819891b20SPhilip, Avinash
1919891b20SPhilip, Avinash /* Time base module registers */
2019891b20SPhilip, Avinash #define TBCTL 0x00
2119891b20SPhilip, Avinash #define TBPRD 0x0A
2219891b20SPhilip, Avinash
2319891b20SPhilip, Avinash #define TBCTL_PRDLD_MASK BIT(3)
2419891b20SPhilip, Avinash #define TBCTL_PRDLD_SHDW 0
2519891b20SPhilip, Avinash #define TBCTL_PRDLD_IMDT BIT(3)
2619891b20SPhilip, Avinash #define TBCTL_CLKDIV_MASK (BIT(12) | BIT(11) | BIT(10) | BIT(9) | \
2719891b20SPhilip, Avinash BIT(8) | BIT(7))
2819891b20SPhilip, Avinash #define TBCTL_CTRMODE_MASK (BIT(1) | BIT(0))
2919891b20SPhilip, Avinash #define TBCTL_CTRMODE_UP 0
3019891b20SPhilip, Avinash #define TBCTL_CTRMODE_DOWN BIT(0)
3119891b20SPhilip, Avinash #define TBCTL_CTRMODE_UPDOWN BIT(1)
3219891b20SPhilip, Avinash #define TBCTL_CTRMODE_FREEZE (BIT(1) | BIT(0))
3319891b20SPhilip, Avinash
3419891b20SPhilip, Avinash #define TBCTL_HSPCLKDIV_SHIFT 7
3519891b20SPhilip, Avinash #define TBCTL_CLKDIV_SHIFT 10
3619891b20SPhilip, Avinash
3719891b20SPhilip, Avinash #define CLKDIV_MAX 7
3819891b20SPhilip, Avinash #define HSPCLKDIV_MAX 7
3919891b20SPhilip, Avinash #define PERIOD_MAX 0xFFFF
4019891b20SPhilip, Avinash
4119891b20SPhilip, Avinash /* compare module registers */
4219891b20SPhilip, Avinash #define CMPA 0x12
4319891b20SPhilip, Avinash #define CMPB 0x14
4419891b20SPhilip, Avinash
4519891b20SPhilip, Avinash /* Action qualifier module registers */
4619891b20SPhilip, Avinash #define AQCTLA 0x16
4719891b20SPhilip, Avinash #define AQCTLB 0x18
4819891b20SPhilip, Avinash #define AQSFRC 0x1A
4919891b20SPhilip, Avinash #define AQCSFRC 0x1C
5019891b20SPhilip, Avinash
5119891b20SPhilip, Avinash #define AQCTL_CBU_MASK (BIT(9) | BIT(8))
5219891b20SPhilip, Avinash #define AQCTL_CBU_FRCLOW BIT(8)
5319891b20SPhilip, Avinash #define AQCTL_CBU_FRCHIGH BIT(9)
5419891b20SPhilip, Avinash #define AQCTL_CBU_FRCTOGGLE (BIT(9) | BIT(8))
5519891b20SPhilip, Avinash #define AQCTL_CAU_MASK (BIT(5) | BIT(4))
5619891b20SPhilip, Avinash #define AQCTL_CAU_FRCLOW BIT(4)
5719891b20SPhilip, Avinash #define AQCTL_CAU_FRCHIGH BIT(5)
5819891b20SPhilip, Avinash #define AQCTL_CAU_FRCTOGGLE (BIT(5) | BIT(4))
5919891b20SPhilip, Avinash #define AQCTL_PRD_MASK (BIT(3) | BIT(2))
6019891b20SPhilip, Avinash #define AQCTL_PRD_FRCLOW BIT(2)
6119891b20SPhilip, Avinash #define AQCTL_PRD_FRCHIGH BIT(3)
6219891b20SPhilip, Avinash #define AQCTL_PRD_FRCTOGGLE (BIT(3) | BIT(2))
6319891b20SPhilip, Avinash #define AQCTL_ZRO_MASK (BIT(1) | BIT(0))
6419891b20SPhilip, Avinash #define AQCTL_ZRO_FRCLOW BIT(0)
6519891b20SPhilip, Avinash #define AQCTL_ZRO_FRCHIGH BIT(1)
6619891b20SPhilip, Avinash #define AQCTL_ZRO_FRCTOGGLE (BIT(1) | BIT(0))
6719891b20SPhilip, Avinash
68daa5629bSPhilip, Avinash #define AQCTL_CHANA_POLNORMAL (AQCTL_CAU_FRCLOW | AQCTL_PRD_FRCHIGH | \
69daa5629bSPhilip, Avinash AQCTL_ZRO_FRCHIGH)
70daa5629bSPhilip, Avinash #define AQCTL_CHANA_POLINVERSED (AQCTL_CAU_FRCHIGH | AQCTL_PRD_FRCLOW | \
71daa5629bSPhilip, Avinash AQCTL_ZRO_FRCLOW)
72daa5629bSPhilip, Avinash #define AQCTL_CHANB_POLNORMAL (AQCTL_CBU_FRCLOW | AQCTL_PRD_FRCHIGH | \
73daa5629bSPhilip, Avinash AQCTL_ZRO_FRCHIGH)
74daa5629bSPhilip, Avinash #define AQCTL_CHANB_POLINVERSED (AQCTL_CBU_FRCHIGH | AQCTL_PRD_FRCLOW | \
75daa5629bSPhilip, Avinash AQCTL_ZRO_FRCLOW)
76daa5629bSPhilip, Avinash
7719891b20SPhilip, Avinash #define AQSFRC_RLDCSF_MASK (BIT(7) | BIT(6))
7819891b20SPhilip, Avinash #define AQSFRC_RLDCSF_ZRO 0
7919891b20SPhilip, Avinash #define AQSFRC_RLDCSF_PRD BIT(6)
8019891b20SPhilip, Avinash #define AQSFRC_RLDCSF_ZROPRD BIT(7)
8119891b20SPhilip, Avinash #define AQSFRC_RLDCSF_IMDT (BIT(7) | BIT(6))
8219891b20SPhilip, Avinash
8319891b20SPhilip, Avinash #define AQCSFRC_CSFB_MASK (BIT(3) | BIT(2))
8419891b20SPhilip, Avinash #define AQCSFRC_CSFB_FRCDIS 0
8519891b20SPhilip, Avinash #define AQCSFRC_CSFB_FRCLOW BIT(2)
8619891b20SPhilip, Avinash #define AQCSFRC_CSFB_FRCHIGH BIT(3)
8719891b20SPhilip, Avinash #define AQCSFRC_CSFB_DISSWFRC (BIT(3) | BIT(2))
8819891b20SPhilip, Avinash #define AQCSFRC_CSFA_MASK (BIT(1) | BIT(0))
8919891b20SPhilip, Avinash #define AQCSFRC_CSFA_FRCDIS 0
9019891b20SPhilip, Avinash #define AQCSFRC_CSFA_FRCLOW BIT(0)
9119891b20SPhilip, Avinash #define AQCSFRC_CSFA_FRCHIGH BIT(1)
9219891b20SPhilip, Avinash #define AQCSFRC_CSFA_DISSWFRC (BIT(1) | BIT(0))
9319891b20SPhilip, Avinash
9419891b20SPhilip, Avinash #define NUM_PWM_CHANNEL 2 /* EHRPWM channels */
9519891b20SPhilip, Avinash
960e2feb17SPhilip Avinash struct ehrpwm_context {
970e2feb17SPhilip Avinash u16 tbctl;
980e2feb17SPhilip Avinash u16 tbprd;
990e2feb17SPhilip Avinash u16 cmpa;
1000e2feb17SPhilip Avinash u16 cmpb;
1010e2feb17SPhilip Avinash u16 aqctla;
1020e2feb17SPhilip Avinash u16 aqctlb;
1030e2feb17SPhilip Avinash u16 aqsfrc;
1040e2feb17SPhilip Avinash u16 aqcsfrc;
1050e2feb17SPhilip Avinash };
1060e2feb17SPhilip Avinash
10719891b20SPhilip, Avinash struct ehrpwm_pwm_chip {
10819891b20SPhilip, Avinash struct pwm_chip chip;
109d2c95e47SThierry Reding unsigned long clk_rate;
11019891b20SPhilip, Avinash void __iomem *mmio_base;
11101b2d453SPhilip, Avinash unsigned long period_cycles[NUM_PWM_CHANNEL];
112daa5629bSPhilip, Avinash enum pwm_polarity polarity[NUM_PWM_CHANNEL];
113d91861daSPhilip, Avinash struct clk *tbclk;
1140e2feb17SPhilip Avinash struct ehrpwm_context ctx;
11519891b20SPhilip, Avinash };
11619891b20SPhilip, Avinash
to_ehrpwm_pwm_chip(struct pwm_chip * chip)11719891b20SPhilip, Avinash static inline struct ehrpwm_pwm_chip *to_ehrpwm_pwm_chip(struct pwm_chip *chip)
11819891b20SPhilip, Avinash {
11919891b20SPhilip, Avinash return container_of(chip, struct ehrpwm_pwm_chip, chip);
12019891b20SPhilip, Avinash }
12119891b20SPhilip, Avinash
ehrpwm_read(void __iomem * base,unsigned int offset)122d2c95e47SThierry Reding static inline u16 ehrpwm_read(void __iomem *base, unsigned int offset)
1230e2feb17SPhilip Avinash {
1240e2feb17SPhilip Avinash return readw(base + offset);
1250e2feb17SPhilip Avinash }
1260e2feb17SPhilip Avinash
ehrpwm_write(void __iomem * base,unsigned int offset,u16 value)127d2c95e47SThierry Reding static inline void ehrpwm_write(void __iomem *base, unsigned int offset,
128d2c95e47SThierry Reding u16 value)
12919891b20SPhilip, Avinash {
130d2c95e47SThierry Reding writew(value, base + offset);
13119891b20SPhilip, Avinash }
13219891b20SPhilip, Avinash
ehrpwm_modify(void __iomem * base,unsigned int offset,u16 mask,u16 value)133d2c95e47SThierry Reding static void ehrpwm_modify(void __iomem *base, unsigned int offset, u16 mask,
134d2c95e47SThierry Reding u16 value)
13519891b20SPhilip, Avinash {
136d2c95e47SThierry Reding unsigned short val;
13719891b20SPhilip, Avinash
138d2c95e47SThierry Reding val = readw(base + offset);
139d2c95e47SThierry Reding val &= ~mask;
140d2c95e47SThierry Reding val |= value & mask;
141d2c95e47SThierry Reding writew(val, base + offset);
14219891b20SPhilip, Avinash }
14319891b20SPhilip, Avinash
14419891b20SPhilip, Avinash /**
14519891b20SPhilip, Avinash * set_prescale_div - Set up the prescaler divider function
14619891b20SPhilip, Avinash * @rqst_prescaler: prescaler value min
14719891b20SPhilip, Avinash * @prescale_div: prescaler value set
14819891b20SPhilip, Avinash * @tb_clk_div: Time Base Control prescaler bits
14919891b20SPhilip, Avinash */
set_prescale_div(unsigned long rqst_prescaler,u16 * prescale_div,u16 * tb_clk_div)150d2c95e47SThierry Reding static int set_prescale_div(unsigned long rqst_prescaler, u16 *prescale_div,
151d2c95e47SThierry Reding u16 *tb_clk_div)
15219891b20SPhilip, Avinash {
15319891b20SPhilip, Avinash unsigned int clkdiv, hspclkdiv;
15419891b20SPhilip, Avinash
15519891b20SPhilip, Avinash for (clkdiv = 0; clkdiv <= CLKDIV_MAX; clkdiv++) {
15619891b20SPhilip, Avinash for (hspclkdiv = 0; hspclkdiv <= HSPCLKDIV_MAX; hspclkdiv++) {
15719891b20SPhilip, Avinash /*
15819891b20SPhilip, Avinash * calculations for prescaler value :
15919891b20SPhilip, Avinash * prescale_div = HSPCLKDIVIDER * CLKDIVIDER.
16019891b20SPhilip, Avinash * HSPCLKDIVIDER = 2 ** hspclkdiv
16119891b20SPhilip, Avinash * CLKDIVIDER = (1), if clkdiv == 0 *OR*
16219891b20SPhilip, Avinash * (2 * clkdiv), if clkdiv != 0
16319891b20SPhilip, Avinash *
16419891b20SPhilip, Avinash * Configure prescale_div value such that period
16519891b20SPhilip, Avinash * register value is less than 65535.
16619891b20SPhilip, Avinash */
16719891b20SPhilip, Avinash
16819891b20SPhilip, Avinash *prescale_div = (1 << clkdiv) *
16919891b20SPhilip, Avinash (hspclkdiv ? (hspclkdiv * 2) : 1);
17019891b20SPhilip, Avinash if (*prescale_div > rqst_prescaler) {
17119891b20SPhilip, Avinash *tb_clk_div = (clkdiv << TBCTL_CLKDIV_SHIFT) |
17219891b20SPhilip, Avinash (hspclkdiv << TBCTL_HSPCLKDIV_SHIFT);
17319891b20SPhilip, Avinash return 0;
17419891b20SPhilip, Avinash }
17519891b20SPhilip, Avinash }
17619891b20SPhilip, Avinash }
177d2c95e47SThierry Reding
17819891b20SPhilip, Avinash return 1;
17919891b20SPhilip, Avinash }
18019891b20SPhilip, Avinash
configure_polarity(struct ehrpwm_pwm_chip * pc,int chan)181daa5629bSPhilip, Avinash static void configure_polarity(struct ehrpwm_pwm_chip *pc, int chan)
18219891b20SPhilip, Avinash {
183d2c95e47SThierry Reding u16 aqctl_val, aqctl_mask;
184d2c95e47SThierry Reding unsigned int aqctl_reg;
18519891b20SPhilip, Avinash
18619891b20SPhilip, Avinash /*
187daa5629bSPhilip, Avinash * Configure PWM output to HIGH/LOW level on counter
188daa5629bSPhilip, Avinash * reaches compare register value and LOW/HIGH level
189daa5629bSPhilip, Avinash * on counter value reaches period register value and
190daa5629bSPhilip, Avinash * zero value on counter
19119891b20SPhilip, Avinash */
19219891b20SPhilip, Avinash if (chan == 1) {
19319891b20SPhilip, Avinash aqctl_reg = AQCTLB;
19419891b20SPhilip, Avinash aqctl_mask = AQCTL_CBU_MASK;
195daa5629bSPhilip, Avinash
196daa5629bSPhilip, Avinash if (pc->polarity[chan] == PWM_POLARITY_INVERSED)
197daa5629bSPhilip, Avinash aqctl_val = AQCTL_CHANB_POLINVERSED;
198daa5629bSPhilip, Avinash else
199daa5629bSPhilip, Avinash aqctl_val = AQCTL_CHANB_POLNORMAL;
20019891b20SPhilip, Avinash } else {
20119891b20SPhilip, Avinash aqctl_reg = AQCTLA;
20219891b20SPhilip, Avinash aqctl_mask = AQCTL_CAU_MASK;
203daa5629bSPhilip, Avinash
204daa5629bSPhilip, Avinash if (pc->polarity[chan] == PWM_POLARITY_INVERSED)
205daa5629bSPhilip, Avinash aqctl_val = AQCTL_CHANA_POLINVERSED;
206daa5629bSPhilip, Avinash else
207daa5629bSPhilip, Avinash aqctl_val = AQCTL_CHANA_POLNORMAL;
20819891b20SPhilip, Avinash }
20919891b20SPhilip, Avinash
21019891b20SPhilip, Avinash aqctl_mask |= AQCTL_PRD_MASK | AQCTL_ZRO_MASK;
21119891b20SPhilip, Avinash ehrpwm_modify(pc->mmio_base, aqctl_reg, aqctl_mask, aqctl_val);
21219891b20SPhilip, Avinash }
21319891b20SPhilip, Avinash
21419891b20SPhilip, Avinash /*
21519891b20SPhilip, Avinash * period_ns = 10^9 * (ps_divval * period_cycles) / PWM_CLK_RATE
21619891b20SPhilip, Avinash * duty_ns = 10^9 * (ps_divval * duty_cycles) / PWM_CLK_RATE
21719891b20SPhilip, Avinash */
ehrpwm_pwm_config(struct pwm_chip * chip,struct pwm_device * pwm,u64 duty_ns,u64 period_ns)21819891b20SPhilip, Avinash static int ehrpwm_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
2195f027d9bSUwe Kleine-König u64 duty_ns, u64 period_ns)
22019891b20SPhilip, Avinash {
22119891b20SPhilip, Avinash struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
222d2c95e47SThierry Reding u32 period_cycles, duty_cycles;
223d2c95e47SThierry Reding u16 ps_divval, tb_divval;
224d2c95e47SThierry Reding unsigned int i, cmp_reg;
22519891b20SPhilip, Avinash unsigned long long c;
22619891b20SPhilip, Avinash
227c2d476a9SThierry Reding if (period_ns > NSEC_PER_SEC)
22819891b20SPhilip, Avinash return -ERANGE;
22919891b20SPhilip, Avinash
23019891b20SPhilip, Avinash c = pc->clk_rate;
23119891b20SPhilip, Avinash c = c * period_ns;
23219891b20SPhilip, Avinash do_div(c, NSEC_PER_SEC);
23319891b20SPhilip, Avinash period_cycles = (unsigned long)c;
23419891b20SPhilip, Avinash
23519891b20SPhilip, Avinash if (period_cycles < 1) {
23619891b20SPhilip, Avinash period_cycles = 1;
23719891b20SPhilip, Avinash duty_cycles = 1;
23819891b20SPhilip, Avinash } else {
23919891b20SPhilip, Avinash c = pc->clk_rate;
24019891b20SPhilip, Avinash c = c * duty_ns;
24119891b20SPhilip, Avinash do_div(c, NSEC_PER_SEC);
24219891b20SPhilip, Avinash duty_cycles = (unsigned long)c;
24319891b20SPhilip, Avinash }
24419891b20SPhilip, Avinash
24501b2d453SPhilip, Avinash /*
24601b2d453SPhilip, Avinash * Period values should be same for multiple PWM channels as IP uses
24701b2d453SPhilip, Avinash * same period register for multiple channels.
24801b2d453SPhilip, Avinash */
24901b2d453SPhilip, Avinash for (i = 0; i < NUM_PWM_CHANNEL; i++) {
25001b2d453SPhilip, Avinash if (pc->period_cycles[i] &&
25101b2d453SPhilip, Avinash (pc->period_cycles[i] != period_cycles)) {
25201b2d453SPhilip, Avinash /*
25301b2d453SPhilip, Avinash * Allow channel to reconfigure period if no other
25401b2d453SPhilip, Avinash * channels being configured.
25501b2d453SPhilip, Avinash */
25601b2d453SPhilip, Avinash if (i == pwm->hwpwm)
25701b2d453SPhilip, Avinash continue;
25801b2d453SPhilip, Avinash
259d2c95e47SThierry Reding dev_err(chip->dev,
260d2c95e47SThierry Reding "period value conflicts with channel %u\n",
26101b2d453SPhilip, Avinash i);
26201b2d453SPhilip, Avinash return -EINVAL;
26301b2d453SPhilip, Avinash }
26401b2d453SPhilip, Avinash }
26501b2d453SPhilip, Avinash
26601b2d453SPhilip, Avinash pc->period_cycles[pwm->hwpwm] = period_cycles;
26701b2d453SPhilip, Avinash
26819891b20SPhilip, Avinash /* Configure clock prescaler to support Low frequency PWM wave */
26919891b20SPhilip, Avinash if (set_prescale_div(period_cycles/PERIOD_MAX, &ps_divval,
27019891b20SPhilip, Avinash &tb_divval)) {
27119891b20SPhilip, Avinash dev_err(chip->dev, "Unsupported values\n");
27219891b20SPhilip, Avinash return -EINVAL;
27319891b20SPhilip, Avinash }
27419891b20SPhilip, Avinash
27519891b20SPhilip, Avinash pm_runtime_get_sync(chip->dev);
27619891b20SPhilip, Avinash
27719891b20SPhilip, Avinash /* Update clock prescaler values */
27819891b20SPhilip, Avinash ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CLKDIV_MASK, tb_divval);
27919891b20SPhilip, Avinash
28019891b20SPhilip, Avinash /* Update period & duty cycle with presacler division */
28119891b20SPhilip, Avinash period_cycles = period_cycles / ps_divval;
28219891b20SPhilip, Avinash duty_cycles = duty_cycles / ps_divval;
28319891b20SPhilip, Avinash
28419891b20SPhilip, Avinash /* Configure shadow loading on Period register */
28519891b20SPhilip, Avinash ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_PRDLD_MASK, TBCTL_PRDLD_SHDW);
28619891b20SPhilip, Avinash
28719891b20SPhilip, Avinash ehrpwm_write(pc->mmio_base, TBPRD, period_cycles);
28819891b20SPhilip, Avinash
28919891b20SPhilip, Avinash /* Configure ehrpwm counter for up-count mode */
29019891b20SPhilip, Avinash ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CTRMODE_MASK,
29119891b20SPhilip, Avinash TBCTL_CTRMODE_UP);
29219891b20SPhilip, Avinash
293daa5629bSPhilip, Avinash if (pwm->hwpwm == 1)
294daa5629bSPhilip, Avinash /* Channel 1 configured with compare B register */
295daa5629bSPhilip, Avinash cmp_reg = CMPB;
296daa5629bSPhilip, Avinash else
297daa5629bSPhilip, Avinash /* Channel 0 configured with compare A register */
298daa5629bSPhilip, Avinash cmp_reg = CMPA;
299daa5629bSPhilip, Avinash
300daa5629bSPhilip, Avinash ehrpwm_write(pc->mmio_base, cmp_reg, duty_cycles);
301daa5629bSPhilip, Avinash
30219891b20SPhilip, Avinash pm_runtime_put_sync(chip->dev);
303d2c95e47SThierry Reding
30419891b20SPhilip, Avinash return 0;
30519891b20SPhilip, Avinash }
30619891b20SPhilip, Avinash
ehrpwm_pwm_set_polarity(struct pwm_chip * chip,struct pwm_device * pwm,enum pwm_polarity polarity)307daa5629bSPhilip, Avinash static int ehrpwm_pwm_set_polarity(struct pwm_chip *chip,
308d2c95e47SThierry Reding struct pwm_device *pwm,
309d2c95e47SThierry Reding enum pwm_polarity polarity)
310daa5629bSPhilip, Avinash {
311daa5629bSPhilip, Avinash struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
312daa5629bSPhilip, Avinash
313daa5629bSPhilip, Avinash /* Configuration of polarity in hardware delayed, do at enable */
314daa5629bSPhilip, Avinash pc->polarity[pwm->hwpwm] = polarity;
315d2c95e47SThierry Reding
316daa5629bSPhilip, Avinash return 0;
317daa5629bSPhilip, Avinash }
318daa5629bSPhilip, Avinash
ehrpwm_pwm_enable(struct pwm_chip * chip,struct pwm_device * pwm)31919891b20SPhilip, Avinash static int ehrpwm_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
32019891b20SPhilip, Avinash {
32119891b20SPhilip, Avinash struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
322d2c95e47SThierry Reding u16 aqcsfrc_val, aqcsfrc_mask;
3230074b49bSPhilip, Avinash int ret;
32419891b20SPhilip, Avinash
32519891b20SPhilip, Avinash /* Leave clock enabled on enabling PWM */
32619891b20SPhilip, Avinash pm_runtime_get_sync(chip->dev);
32719891b20SPhilip, Avinash
32819891b20SPhilip, Avinash /* Disabling Action Qualifier on PWM output */
32919891b20SPhilip, Avinash if (pwm->hwpwm) {
33019891b20SPhilip, Avinash aqcsfrc_val = AQCSFRC_CSFB_FRCDIS;
33119891b20SPhilip, Avinash aqcsfrc_mask = AQCSFRC_CSFB_MASK;
33219891b20SPhilip, Avinash } else {
33319891b20SPhilip, Avinash aqcsfrc_val = AQCSFRC_CSFA_FRCDIS;
33419891b20SPhilip, Avinash aqcsfrc_mask = AQCSFRC_CSFA_MASK;
33519891b20SPhilip, Avinash }
33619891b20SPhilip, Avinash
33719891b20SPhilip, Avinash /* Changes to shadow mode */
33819891b20SPhilip, Avinash ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
33919891b20SPhilip, Avinash AQSFRC_RLDCSF_ZRO);
34019891b20SPhilip, Avinash
34119891b20SPhilip, Avinash ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
34219891b20SPhilip, Avinash
343daa5629bSPhilip, Avinash /* Channels polarity can be configured from action qualifier module */
344daa5629bSPhilip, Avinash configure_polarity(pc, pwm->hwpwm);
345daa5629bSPhilip, Avinash
346aa49d628SVignesh R /* Enable TBCLK */
347b388f15fSMarek Belisko ret = clk_enable(pc->tbclk);
3480074b49bSPhilip, Avinash if (ret) {
349d2c95e47SThierry Reding dev_err(chip->dev, "Failed to enable TBCLK for %s: %d\n",
350d2c95e47SThierry Reding dev_name(pc->chip.dev), ret);
3510074b49bSPhilip, Avinash return ret;
3520074b49bSPhilip, Avinash }
353d91861daSPhilip, Avinash
35419891b20SPhilip, Avinash return 0;
35519891b20SPhilip, Avinash }
35619891b20SPhilip, Avinash
ehrpwm_pwm_disable(struct pwm_chip * chip,struct pwm_device * pwm)35719891b20SPhilip, Avinash static void ehrpwm_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
35819891b20SPhilip, Avinash {
35919891b20SPhilip, Avinash struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
360d2c95e47SThierry Reding u16 aqcsfrc_val, aqcsfrc_mask;
36119891b20SPhilip, Avinash
36219891b20SPhilip, Avinash /* Action Qualifier puts PWM output low forcefully */
36319891b20SPhilip, Avinash if (pwm->hwpwm) {
36419891b20SPhilip, Avinash aqcsfrc_val = AQCSFRC_CSFB_FRCLOW;
36519891b20SPhilip, Avinash aqcsfrc_mask = AQCSFRC_CSFB_MASK;
36619891b20SPhilip, Avinash } else {
36719891b20SPhilip, Avinash aqcsfrc_val = AQCSFRC_CSFA_FRCLOW;
36819891b20SPhilip, Avinash aqcsfrc_mask = AQCSFRC_CSFA_MASK;
36919891b20SPhilip, Avinash }
37019891b20SPhilip, Avinash
37138dabd91SVignesh R /* Update shadow register first before modifying active register */
372b00ef530SChristoph Vogtländer ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
373b00ef530SChristoph Vogtländer AQSFRC_RLDCSF_ZRO);
37438dabd91SVignesh R ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
37519891b20SPhilip, Avinash /*
37619891b20SPhilip, Avinash * Changes to immediate action on Action Qualifier. This puts
37719891b20SPhilip, Avinash * Action Qualifier control on PWM output from next TBCLK
37819891b20SPhilip, Avinash */
37919891b20SPhilip, Avinash ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK,
38019891b20SPhilip, Avinash AQSFRC_RLDCSF_IMDT);
38119891b20SPhilip, Avinash
38219891b20SPhilip, Avinash ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
38319891b20SPhilip, Avinash
384d91861daSPhilip, Avinash /* Disabling TBCLK on PWM disable */
385b388f15fSMarek Belisko clk_disable(pc->tbclk);
386d91861daSPhilip, Avinash
38719891b20SPhilip, Avinash /* Disable clock on PWM disable */
38819891b20SPhilip, Avinash pm_runtime_put_sync(chip->dev);
38919891b20SPhilip, Avinash }
39019891b20SPhilip, Avinash
ehrpwm_pwm_free(struct pwm_chip * chip,struct pwm_device * pwm)39119891b20SPhilip, Avinash static void ehrpwm_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
39219891b20SPhilip, Avinash {
39301b2d453SPhilip, Avinash struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip);
39401b2d453SPhilip, Avinash
3955c31252cSBoris Brezillon if (pwm_is_enabled(pwm)) {
39619891b20SPhilip, Avinash dev_warn(chip->dev, "Removing PWM device without disabling\n");
39719891b20SPhilip, Avinash pm_runtime_put_sync(chip->dev);
39819891b20SPhilip, Avinash }
39901b2d453SPhilip, Avinash
40001b2d453SPhilip, Avinash /* set period value to zero on free */
40101b2d453SPhilip, Avinash pc->period_cycles[pwm->hwpwm] = 0;
40219891b20SPhilip, Avinash }
40319891b20SPhilip, Avinash
ehrpwm_pwm_apply(struct pwm_chip * chip,struct pwm_device * pwm,const struct pwm_state * state)4045f027d9bSUwe Kleine-König static int ehrpwm_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
4055f027d9bSUwe Kleine-König const struct pwm_state *state)
4065f027d9bSUwe Kleine-König {
4075f027d9bSUwe Kleine-König int err;
4085f027d9bSUwe Kleine-König bool enabled = pwm->state.enabled;
4095f027d9bSUwe Kleine-König
4105f027d9bSUwe Kleine-König if (state->polarity != pwm->state.polarity) {
4115f027d9bSUwe Kleine-König if (enabled) {
4125f027d9bSUwe Kleine-König ehrpwm_pwm_disable(chip, pwm);
4135f027d9bSUwe Kleine-König enabled = false;
4145f027d9bSUwe Kleine-König }
4155f027d9bSUwe Kleine-König
4165f027d9bSUwe Kleine-König err = ehrpwm_pwm_set_polarity(chip, pwm, state->polarity);
4175f027d9bSUwe Kleine-König if (err)
4185f027d9bSUwe Kleine-König return err;
4195f027d9bSUwe Kleine-König }
4205f027d9bSUwe Kleine-König
4215f027d9bSUwe Kleine-König if (!state->enabled) {
4225f027d9bSUwe Kleine-König if (enabled)
4235f027d9bSUwe Kleine-König ehrpwm_pwm_disable(chip, pwm);
4245f027d9bSUwe Kleine-König return 0;
4255f027d9bSUwe Kleine-König }
4265f027d9bSUwe Kleine-König
4275f027d9bSUwe Kleine-König err = ehrpwm_pwm_config(chip, pwm, state->duty_cycle, state->period);
4285f027d9bSUwe Kleine-König if (err)
4295f027d9bSUwe Kleine-König return err;
4305f027d9bSUwe Kleine-König
4315f027d9bSUwe Kleine-König if (!enabled)
4325f027d9bSUwe Kleine-König err = ehrpwm_pwm_enable(chip, pwm);
4335f027d9bSUwe Kleine-König
4345f027d9bSUwe Kleine-König return err;
4355f027d9bSUwe Kleine-König }
4365f027d9bSUwe Kleine-König
43719891b20SPhilip, Avinash static const struct pwm_ops ehrpwm_pwm_ops = {
43819891b20SPhilip, Avinash .free = ehrpwm_pwm_free,
4395f027d9bSUwe Kleine-König .apply = ehrpwm_pwm_apply,
44019891b20SPhilip, Avinash .owner = THIS_MODULE,
44119891b20SPhilip, Avinash };
44219891b20SPhilip, Avinash
44353ad9e8dSPhilip, Avinash static const struct of_device_id ehrpwm_of_match[] = {
444ae5200d2SCooper Jr., Franklin { .compatible = "ti,am3352-ehrpwm" },
44553ad9e8dSPhilip, Avinash { .compatible = "ti,am33xx-ehrpwm" },
44653ad9e8dSPhilip, Avinash {},
44753ad9e8dSPhilip, Avinash };
44853ad9e8dSPhilip, Avinash MODULE_DEVICE_TABLE(of, ehrpwm_of_match);
44953ad9e8dSPhilip, Avinash
ehrpwm_pwm_probe(struct platform_device * pdev)4503e9fe83dSBill Pemberton static int ehrpwm_pwm_probe(struct platform_device *pdev)
45119891b20SPhilip, Avinash {
452ae5200d2SCooper Jr., Franklin struct device_node *np = pdev->dev.of_node;
453d2c95e47SThierry Reding struct ehrpwm_pwm_chip *pc;
45419891b20SPhilip, Avinash struct clk *clk;
455d2c95e47SThierry Reding int ret;
45619891b20SPhilip, Avinash
45719891b20SPhilip, Avinash pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
4585e348953SJingoo Han if (!pc)
45919891b20SPhilip, Avinash return -ENOMEM;
46019891b20SPhilip, Avinash
46119891b20SPhilip, Avinash clk = devm_clk_get(&pdev->dev, "fck");
46219891b20SPhilip, Avinash if (IS_ERR(clk)) {
463ae5200d2SCooper Jr., Franklin if (of_device_is_compatible(np, "ti,am33xx-ecap")) {
464ae5200d2SCooper Jr., Franklin dev_warn(&pdev->dev, "Binding is obsolete.\n");
465ae5200d2SCooper Jr., Franklin clk = devm_clk_get(pdev->dev.parent, "fck");
466ae5200d2SCooper Jr., Franklin }
467ae5200d2SCooper Jr., Franklin }
468ae5200d2SCooper Jr., Franklin
46944db5363SGrygorii Strashko if (IS_ERR(clk))
47044db5363SGrygorii Strashko return dev_err_probe(&pdev->dev, PTR_ERR(clk), "Failed to get fck\n");
47119891b20SPhilip, Avinash
47219891b20SPhilip, Avinash pc->clk_rate = clk_get_rate(clk);
47319891b20SPhilip, Avinash if (!pc->clk_rate) {
47419891b20SPhilip, Avinash dev_err(&pdev->dev, "failed to get clock rate\n");
47519891b20SPhilip, Avinash return -EINVAL;
47619891b20SPhilip, Avinash }
47719891b20SPhilip, Avinash
47819891b20SPhilip, Avinash pc->chip.dev = &pdev->dev;
47919891b20SPhilip, Avinash pc->chip.ops = &ehrpwm_pwm_ops;
48019891b20SPhilip, Avinash pc->chip.npwm = NUM_PWM_CHANNEL;
48119891b20SPhilip, Avinash
482dc13c0f6SYangtao Li pc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
4836d4294d1SThierry Reding if (IS_ERR(pc->mmio_base))
4846d4294d1SThierry Reding return PTR_ERR(pc->mmio_base);
48519891b20SPhilip, Avinash
486d91861daSPhilip, Avinash /* Acquire tbclk for Time Base EHRPWM submodule */
487d91861daSPhilip, Avinash pc->tbclk = devm_clk_get(&pdev->dev, "tbclk");
48844db5363SGrygorii Strashko if (IS_ERR(pc->tbclk))
48944db5363SGrygorii Strashko return dev_err_probe(&pdev->dev, PTR_ERR(pc->tbclk), "Failed to get tbclk\n");
490d91861daSPhilip, Avinash
491b388f15fSMarek Belisko ret = clk_prepare(pc->tbclk);
492b388f15fSMarek Belisko if (ret < 0) {
493b388f15fSMarek Belisko dev_err(&pdev->dev, "clk_prepare() failed: %d\n", ret);
494b388f15fSMarek Belisko return ret;
495b388f15fSMarek Belisko }
496b388f15fSMarek Belisko
49719891b20SPhilip, Avinash ret = pwmchip_add(&pc->chip);
49819891b20SPhilip, Avinash if (ret < 0) {
49919891b20SPhilip, Avinash dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
500e2b5602aSJohan Hovold goto err_clk_unprepare;
50119891b20SPhilip, Avinash }
50219891b20SPhilip, Avinash
503d870c80eSThierry Reding platform_set_drvdata(pdev, pc);
50419891b20SPhilip, Avinash pm_runtime_enable(&pdev->dev);
50553ad9e8dSPhilip, Avinash
50619891b20SPhilip, Avinash return 0;
507e2b5602aSJohan Hovold
508e2b5602aSJohan Hovold err_clk_unprepare:
509e2b5602aSJohan Hovold clk_unprepare(pc->tbclk);
510e2b5602aSJohan Hovold
511e2b5602aSJohan Hovold return ret;
51219891b20SPhilip, Avinash }
51319891b20SPhilip, Avinash
ehrpwm_pwm_remove(struct platform_device * pdev)51491e92e82SUwe Kleine-König static void ehrpwm_pwm_remove(struct platform_device *pdev)
51519891b20SPhilip, Avinash {
51619891b20SPhilip, Avinash struct ehrpwm_pwm_chip *pc = platform_get_drvdata(pdev);
51719891b20SPhilip, Avinash
5181a0c97b6SUwe Kleine-König pwmchip_remove(&pc->chip);
5191a0c97b6SUwe Kleine-König
520b388f15fSMarek Belisko clk_unprepare(pc->tbclk);
521b388f15fSMarek Belisko
52219891b20SPhilip, Avinash pm_runtime_disable(&pdev->dev);
52319891b20SPhilip, Avinash }
52419891b20SPhilip, Avinash
525af5935ecSWolfram Sang #ifdef CONFIG_PM_SLEEP
ehrpwm_pwm_save_context(struct ehrpwm_pwm_chip * pc)526b343a188SAxel Lin static void ehrpwm_pwm_save_context(struct ehrpwm_pwm_chip *pc)
5270e2feb17SPhilip Avinash {
5280e2feb17SPhilip Avinash pm_runtime_get_sync(pc->chip.dev);
529d2c95e47SThierry Reding
5300e2feb17SPhilip Avinash pc->ctx.tbctl = ehrpwm_read(pc->mmio_base, TBCTL);
5310e2feb17SPhilip Avinash pc->ctx.tbprd = ehrpwm_read(pc->mmio_base, TBPRD);
5320e2feb17SPhilip Avinash pc->ctx.cmpa = ehrpwm_read(pc->mmio_base, CMPA);
5330e2feb17SPhilip Avinash pc->ctx.cmpb = ehrpwm_read(pc->mmio_base, CMPB);
5340e2feb17SPhilip Avinash pc->ctx.aqctla = ehrpwm_read(pc->mmio_base, AQCTLA);
5350e2feb17SPhilip Avinash pc->ctx.aqctlb = ehrpwm_read(pc->mmio_base, AQCTLB);
5360e2feb17SPhilip Avinash pc->ctx.aqsfrc = ehrpwm_read(pc->mmio_base, AQSFRC);
5370e2feb17SPhilip Avinash pc->ctx.aqcsfrc = ehrpwm_read(pc->mmio_base, AQCSFRC);
538d2c95e47SThierry Reding
5390e2feb17SPhilip Avinash pm_runtime_put_sync(pc->chip.dev);
5400e2feb17SPhilip Avinash }
5410e2feb17SPhilip Avinash
ehrpwm_pwm_restore_context(struct ehrpwm_pwm_chip * pc)542b343a188SAxel Lin static void ehrpwm_pwm_restore_context(struct ehrpwm_pwm_chip *pc)
5430e2feb17SPhilip Avinash {
5440e2feb17SPhilip Avinash ehrpwm_write(pc->mmio_base, TBPRD, pc->ctx.tbprd);
5450e2feb17SPhilip Avinash ehrpwm_write(pc->mmio_base, CMPA, pc->ctx.cmpa);
5460e2feb17SPhilip Avinash ehrpwm_write(pc->mmio_base, CMPB, pc->ctx.cmpb);
5470e2feb17SPhilip Avinash ehrpwm_write(pc->mmio_base, AQCTLA, pc->ctx.aqctla);
5480e2feb17SPhilip Avinash ehrpwm_write(pc->mmio_base, AQCTLB, pc->ctx.aqctlb);
5490e2feb17SPhilip Avinash ehrpwm_write(pc->mmio_base, AQSFRC, pc->ctx.aqsfrc);
5500e2feb17SPhilip Avinash ehrpwm_write(pc->mmio_base, AQCSFRC, pc->ctx.aqcsfrc);
5510e2feb17SPhilip Avinash ehrpwm_write(pc->mmio_base, TBCTL, pc->ctx.tbctl);
5520e2feb17SPhilip Avinash }
5530e2feb17SPhilip Avinash
ehrpwm_pwm_suspend(struct device * dev)5540e2feb17SPhilip Avinash static int ehrpwm_pwm_suspend(struct device *dev)
5550e2feb17SPhilip Avinash {
5560e2feb17SPhilip Avinash struct ehrpwm_pwm_chip *pc = dev_get_drvdata(dev);
557d2c95e47SThierry Reding unsigned int i;
5580e2feb17SPhilip Avinash
5590e2feb17SPhilip Avinash ehrpwm_pwm_save_context(pc);
560d2c95e47SThierry Reding
5610e2feb17SPhilip Avinash for (i = 0; i < pc->chip.npwm; i++) {
5620e2feb17SPhilip Avinash struct pwm_device *pwm = &pc->chip.pwms[i];
5630e2feb17SPhilip Avinash
5645c31252cSBoris Brezillon if (!pwm_is_enabled(pwm))
5650e2feb17SPhilip Avinash continue;
5660e2feb17SPhilip Avinash
5670e2feb17SPhilip Avinash /* Disable explicitly if PWM is running */
5680e2feb17SPhilip Avinash pm_runtime_put_sync(dev);
5690e2feb17SPhilip Avinash }
570d2c95e47SThierry Reding
5710e2feb17SPhilip Avinash return 0;
5720e2feb17SPhilip Avinash }
5730e2feb17SPhilip Avinash
ehrpwm_pwm_resume(struct device * dev)5740e2feb17SPhilip Avinash static int ehrpwm_pwm_resume(struct device *dev)
5750e2feb17SPhilip Avinash {
5760e2feb17SPhilip Avinash struct ehrpwm_pwm_chip *pc = dev_get_drvdata(dev);
577d2c95e47SThierry Reding unsigned int i;
5780e2feb17SPhilip Avinash
5790e2feb17SPhilip Avinash for (i = 0; i < pc->chip.npwm; i++) {
5800e2feb17SPhilip Avinash struct pwm_device *pwm = &pc->chip.pwms[i];
5810e2feb17SPhilip Avinash
5825c31252cSBoris Brezillon if (!pwm_is_enabled(pwm))
5830e2feb17SPhilip Avinash continue;
5840e2feb17SPhilip Avinash
5850e2feb17SPhilip Avinash /* Enable explicitly if PWM was running */
5860e2feb17SPhilip Avinash pm_runtime_get_sync(dev);
5870e2feb17SPhilip Avinash }
588d2c95e47SThierry Reding
5890e2feb17SPhilip Avinash ehrpwm_pwm_restore_context(pc);
590d2c95e47SThierry Reding
5910e2feb17SPhilip Avinash return 0;
5920e2feb17SPhilip Avinash }
59329258b21SJingoo Han #endif
5940e2feb17SPhilip Avinash
5950e2feb17SPhilip Avinash static SIMPLE_DEV_PM_OPS(ehrpwm_pwm_pm_ops, ehrpwm_pwm_suspend,
5960e2feb17SPhilip Avinash ehrpwm_pwm_resume);
5970e2feb17SPhilip Avinash
59819891b20SPhilip, Avinash static struct platform_driver ehrpwm_pwm_driver = {
59919891b20SPhilip, Avinash .driver = {
60019891b20SPhilip, Avinash .name = "ehrpwm",
60153ad9e8dSPhilip, Avinash .of_match_table = ehrpwm_of_match,
6020e2feb17SPhilip Avinash .pm = &ehrpwm_pwm_pm_ops,
60319891b20SPhilip, Avinash },
60419891b20SPhilip, Avinash .probe = ehrpwm_pwm_probe,
60591e92e82SUwe Kleine-König .remove_new = ehrpwm_pwm_remove,
60619891b20SPhilip, Avinash };
60719891b20SPhilip, Avinash module_platform_driver(ehrpwm_pwm_driver);
60819891b20SPhilip, Avinash
60919891b20SPhilip, Avinash MODULE_DESCRIPTION("EHRPWM PWM driver");
61019891b20SPhilip, Avinash MODULE_AUTHOR("Texas Instruments");
61119891b20SPhilip, Avinash MODULE_LICENSE("GPL");
612