Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 1 | /* |
| 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, Avinash | 53ad9e8 | 2012-11-27 14:18:13 +0530 | [diff] [blame] | 28 | #include <linux/of_device.h> |
Philip, Avinash | 98ccf49 | 2012-11-27 14:18:14 +0530 | [diff] [blame] | 29 | #include <linux/pinctrl/consumer.h> |
Philip, Avinash | 53ad9e8 | 2012-11-27 14:18:13 +0530 | [diff] [blame] | 30 | |
| 31 | #include "pwm-tipwmss.h" |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 32 | |
| 33 | /* EHRPWM registers and bits definitions */ |
| 34 | |
| 35 | /* Time base module registers */ |
| 36 | #define TBCTL 0x00 |
| 37 | #define TBPRD 0x0A |
| 38 | |
| 39 | #define TBCTL_RUN_MASK (BIT(15) | BIT(14)) |
| 40 | #define TBCTL_STOP_NEXT 0 |
| 41 | #define TBCTL_STOP_ON_CYCLE BIT(14) |
| 42 | #define TBCTL_FREE_RUN (BIT(15) | BIT(14)) |
| 43 | #define TBCTL_PRDLD_MASK BIT(3) |
| 44 | #define TBCTL_PRDLD_SHDW 0 |
| 45 | #define TBCTL_PRDLD_IMDT BIT(3) |
| 46 | #define TBCTL_CLKDIV_MASK (BIT(12) | BIT(11) | BIT(10) | BIT(9) | \ |
| 47 | BIT(8) | BIT(7)) |
| 48 | #define TBCTL_CTRMODE_MASK (BIT(1) | BIT(0)) |
| 49 | #define TBCTL_CTRMODE_UP 0 |
| 50 | #define TBCTL_CTRMODE_DOWN BIT(0) |
| 51 | #define TBCTL_CTRMODE_UPDOWN BIT(1) |
| 52 | #define TBCTL_CTRMODE_FREEZE (BIT(1) | BIT(0)) |
| 53 | |
| 54 | #define TBCTL_HSPCLKDIV_SHIFT 7 |
| 55 | #define TBCTL_CLKDIV_SHIFT 10 |
| 56 | |
| 57 | #define CLKDIV_MAX 7 |
| 58 | #define HSPCLKDIV_MAX 7 |
| 59 | #define PERIOD_MAX 0xFFFF |
| 60 | |
| 61 | /* compare module registers */ |
| 62 | #define CMPA 0x12 |
| 63 | #define CMPB 0x14 |
| 64 | |
| 65 | /* Action qualifier module registers */ |
| 66 | #define AQCTLA 0x16 |
| 67 | #define AQCTLB 0x18 |
| 68 | #define AQSFRC 0x1A |
| 69 | #define AQCSFRC 0x1C |
| 70 | |
| 71 | #define AQCTL_CBU_MASK (BIT(9) | BIT(8)) |
| 72 | #define AQCTL_CBU_FRCLOW BIT(8) |
| 73 | #define AQCTL_CBU_FRCHIGH BIT(9) |
| 74 | #define AQCTL_CBU_FRCTOGGLE (BIT(9) | BIT(8)) |
| 75 | #define AQCTL_CAU_MASK (BIT(5) | BIT(4)) |
| 76 | #define AQCTL_CAU_FRCLOW BIT(4) |
| 77 | #define AQCTL_CAU_FRCHIGH BIT(5) |
| 78 | #define AQCTL_CAU_FRCTOGGLE (BIT(5) | BIT(4)) |
| 79 | #define AQCTL_PRD_MASK (BIT(3) | BIT(2)) |
| 80 | #define AQCTL_PRD_FRCLOW BIT(2) |
| 81 | #define AQCTL_PRD_FRCHIGH BIT(3) |
| 82 | #define AQCTL_PRD_FRCTOGGLE (BIT(3) | BIT(2)) |
| 83 | #define AQCTL_ZRO_MASK (BIT(1) | BIT(0)) |
| 84 | #define AQCTL_ZRO_FRCLOW BIT(0) |
| 85 | #define AQCTL_ZRO_FRCHIGH BIT(1) |
| 86 | #define AQCTL_ZRO_FRCTOGGLE (BIT(1) | BIT(0)) |
| 87 | |
Philip, Avinash | daa5629 | 2012-09-06 10:40:03 +0530 | [diff] [blame] | 88 | #define AQCTL_CHANA_POLNORMAL (AQCTL_CAU_FRCLOW | AQCTL_PRD_FRCHIGH | \ |
| 89 | AQCTL_ZRO_FRCHIGH) |
| 90 | #define AQCTL_CHANA_POLINVERSED (AQCTL_CAU_FRCHIGH | AQCTL_PRD_FRCLOW | \ |
| 91 | AQCTL_ZRO_FRCLOW) |
| 92 | #define AQCTL_CHANB_POLNORMAL (AQCTL_CBU_FRCLOW | AQCTL_PRD_FRCHIGH | \ |
| 93 | AQCTL_ZRO_FRCHIGH) |
| 94 | #define AQCTL_CHANB_POLINVERSED (AQCTL_CBU_FRCHIGH | AQCTL_PRD_FRCLOW | \ |
| 95 | AQCTL_ZRO_FRCLOW) |
| 96 | |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 97 | #define AQSFRC_RLDCSF_MASK (BIT(7) | BIT(6)) |
| 98 | #define AQSFRC_RLDCSF_ZRO 0 |
| 99 | #define AQSFRC_RLDCSF_PRD BIT(6) |
| 100 | #define AQSFRC_RLDCSF_ZROPRD BIT(7) |
| 101 | #define AQSFRC_RLDCSF_IMDT (BIT(7) | BIT(6)) |
| 102 | |
| 103 | #define AQCSFRC_CSFB_MASK (BIT(3) | BIT(2)) |
| 104 | #define AQCSFRC_CSFB_FRCDIS 0 |
| 105 | #define AQCSFRC_CSFB_FRCLOW BIT(2) |
| 106 | #define AQCSFRC_CSFB_FRCHIGH BIT(3) |
| 107 | #define AQCSFRC_CSFB_DISSWFRC (BIT(3) | BIT(2)) |
| 108 | #define AQCSFRC_CSFA_MASK (BIT(1) | BIT(0)) |
| 109 | #define AQCSFRC_CSFA_FRCDIS 0 |
| 110 | #define AQCSFRC_CSFA_FRCLOW BIT(0) |
| 111 | #define AQCSFRC_CSFA_FRCHIGH BIT(1) |
| 112 | #define AQCSFRC_CSFA_DISSWFRC (BIT(1) | BIT(0)) |
| 113 | |
| 114 | #define NUM_PWM_CHANNEL 2 /* EHRPWM channels */ |
| 115 | |
| 116 | struct ehrpwm_pwm_chip { |
| 117 | struct pwm_chip chip; |
| 118 | unsigned int clk_rate; |
| 119 | void __iomem *mmio_base; |
Philip, Avinash | 01b2d45 | 2012-09-06 10:44:25 +0530 | [diff] [blame] | 120 | unsigned long period_cycles[NUM_PWM_CHANNEL]; |
Philip, Avinash | daa5629 | 2012-09-06 10:40:03 +0530 | [diff] [blame] | 121 | enum pwm_polarity polarity[NUM_PWM_CHANNEL]; |
Philip, Avinash | d91861d | 2012-11-27 14:18:12 +0530 | [diff] [blame] | 122 | struct clk *tbclk; |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static inline struct ehrpwm_pwm_chip *to_ehrpwm_pwm_chip(struct pwm_chip *chip) |
| 126 | { |
| 127 | return container_of(chip, struct ehrpwm_pwm_chip, chip); |
| 128 | } |
| 129 | |
| 130 | static void ehrpwm_write(void *base, int offset, unsigned int val) |
| 131 | { |
| 132 | writew(val & 0xFFFF, base + offset); |
| 133 | } |
| 134 | |
| 135 | static void ehrpwm_modify(void *base, int offset, |
| 136 | unsigned short mask, unsigned short val) |
| 137 | { |
| 138 | unsigned short regval; |
| 139 | |
| 140 | regval = readw(base + offset); |
| 141 | regval &= ~mask; |
| 142 | regval |= val & mask; |
| 143 | writew(regval, base + offset); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * set_prescale_div - Set up the prescaler divider function |
| 148 | * @rqst_prescaler: prescaler value min |
| 149 | * @prescale_div: prescaler value set |
| 150 | * @tb_clk_div: Time Base Control prescaler bits |
| 151 | */ |
| 152 | static int set_prescale_div(unsigned long rqst_prescaler, |
| 153 | unsigned short *prescale_div, unsigned short *tb_clk_div) |
| 154 | { |
| 155 | unsigned int clkdiv, hspclkdiv; |
| 156 | |
| 157 | for (clkdiv = 0; clkdiv <= CLKDIV_MAX; clkdiv++) { |
| 158 | for (hspclkdiv = 0; hspclkdiv <= HSPCLKDIV_MAX; hspclkdiv++) { |
| 159 | |
| 160 | /* |
| 161 | * calculations for prescaler value : |
| 162 | * prescale_div = HSPCLKDIVIDER * CLKDIVIDER. |
| 163 | * HSPCLKDIVIDER = 2 ** hspclkdiv |
| 164 | * CLKDIVIDER = (1), if clkdiv == 0 *OR* |
| 165 | * (2 * clkdiv), if clkdiv != 0 |
| 166 | * |
| 167 | * Configure prescale_div value such that period |
| 168 | * register value is less than 65535. |
| 169 | */ |
| 170 | |
| 171 | *prescale_div = (1 << clkdiv) * |
| 172 | (hspclkdiv ? (hspclkdiv * 2) : 1); |
| 173 | if (*prescale_div > rqst_prescaler) { |
| 174 | *tb_clk_div = (clkdiv << TBCTL_CLKDIV_SHIFT) | |
| 175 | (hspclkdiv << TBCTL_HSPCLKDIV_SHIFT); |
| 176 | return 0; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | return 1; |
| 181 | } |
| 182 | |
Philip, Avinash | daa5629 | 2012-09-06 10:40:03 +0530 | [diff] [blame] | 183 | static void configure_polarity(struct ehrpwm_pwm_chip *pc, int chan) |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 184 | { |
Philip, Avinash | daa5629 | 2012-09-06 10:40:03 +0530 | [diff] [blame] | 185 | int aqctl_reg; |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 186 | unsigned short aqctl_val, aqctl_mask; |
| 187 | |
| 188 | /* |
Philip, Avinash | daa5629 | 2012-09-06 10:40:03 +0530 | [diff] [blame] | 189 | * Configure PWM output to HIGH/LOW level on counter |
| 190 | * reaches compare register value and LOW/HIGH level |
| 191 | * on counter value reaches period register value and |
| 192 | * zero value on counter |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 193 | */ |
| 194 | if (chan == 1) { |
| 195 | aqctl_reg = AQCTLB; |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 196 | aqctl_mask = AQCTL_CBU_MASK; |
Philip, Avinash | daa5629 | 2012-09-06 10:40:03 +0530 | [diff] [blame] | 197 | |
| 198 | if (pc->polarity[chan] == PWM_POLARITY_INVERSED) |
| 199 | aqctl_val = AQCTL_CHANB_POLINVERSED; |
| 200 | else |
| 201 | aqctl_val = AQCTL_CHANB_POLNORMAL; |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 202 | } else { |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 203 | aqctl_reg = AQCTLA; |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 204 | aqctl_mask = AQCTL_CAU_MASK; |
Philip, Avinash | daa5629 | 2012-09-06 10:40:03 +0530 | [diff] [blame] | 205 | |
| 206 | if (pc->polarity[chan] == PWM_POLARITY_INVERSED) |
| 207 | aqctl_val = AQCTL_CHANA_POLINVERSED; |
| 208 | else |
| 209 | aqctl_val = AQCTL_CHANA_POLNORMAL; |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 210 | } |
| 211 | |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 212 | aqctl_mask |= AQCTL_PRD_MASK | AQCTL_ZRO_MASK; |
Philip, Avinash | daa5629 | 2012-09-06 10:40:03 +0530 | [diff] [blame] | 213 | ehrpwm_modify(pc->mmio_base, aqctl_reg, aqctl_mask, aqctl_val); |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* |
| 217 | * period_ns = 10^9 * (ps_divval * period_cycles) / PWM_CLK_RATE |
| 218 | * duty_ns = 10^9 * (ps_divval * duty_cycles) / PWM_CLK_RATE |
| 219 | */ |
| 220 | static int ehrpwm_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, |
| 221 | int duty_ns, int period_ns) |
| 222 | { |
| 223 | struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip); |
| 224 | unsigned long long c; |
| 225 | unsigned long period_cycles, duty_cycles; |
| 226 | unsigned short ps_divval, tb_divval; |
Philip, Avinash | daa5629 | 2012-09-06 10:40:03 +0530 | [diff] [blame] | 227 | int i, cmp_reg; |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 228 | |
Thierry Reding | c2d476a | 2012-09-02 22:13:40 +0200 | [diff] [blame] | 229 | if (period_ns > NSEC_PER_SEC) |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 230 | return -ERANGE; |
| 231 | |
| 232 | c = pc->clk_rate; |
| 233 | c = c * period_ns; |
| 234 | do_div(c, NSEC_PER_SEC); |
| 235 | period_cycles = (unsigned long)c; |
| 236 | |
| 237 | if (period_cycles < 1) { |
| 238 | period_cycles = 1; |
| 239 | duty_cycles = 1; |
| 240 | } else { |
| 241 | c = pc->clk_rate; |
| 242 | c = c * duty_ns; |
| 243 | do_div(c, NSEC_PER_SEC); |
| 244 | duty_cycles = (unsigned long)c; |
| 245 | } |
| 246 | |
Philip, Avinash | 01b2d45 | 2012-09-06 10:44:25 +0530 | [diff] [blame] | 247 | /* |
| 248 | * Period values should be same for multiple PWM channels as IP uses |
| 249 | * same period register for multiple channels. |
| 250 | */ |
| 251 | for (i = 0; i < NUM_PWM_CHANNEL; i++) { |
| 252 | if (pc->period_cycles[i] && |
| 253 | (pc->period_cycles[i] != period_cycles)) { |
| 254 | /* |
| 255 | * Allow channel to reconfigure period if no other |
| 256 | * channels being configured. |
| 257 | */ |
| 258 | if (i == pwm->hwpwm) |
| 259 | continue; |
| 260 | |
| 261 | dev_err(chip->dev, "Period value conflicts with channel %d\n", |
| 262 | i); |
| 263 | return -EINVAL; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | pc->period_cycles[pwm->hwpwm] = period_cycles; |
| 268 | |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 269 | /* Configure clock prescaler to support Low frequency PWM wave */ |
| 270 | if (set_prescale_div(period_cycles/PERIOD_MAX, &ps_divval, |
| 271 | &tb_divval)) { |
| 272 | dev_err(chip->dev, "Unsupported values\n"); |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | |
| 276 | pm_runtime_get_sync(chip->dev); |
| 277 | |
| 278 | /* Update clock prescaler values */ |
| 279 | ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CLKDIV_MASK, tb_divval); |
| 280 | |
| 281 | /* Update period & duty cycle with presacler division */ |
| 282 | period_cycles = period_cycles / ps_divval; |
| 283 | duty_cycles = duty_cycles / ps_divval; |
| 284 | |
| 285 | /* Configure shadow loading on Period register */ |
| 286 | ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_PRDLD_MASK, TBCTL_PRDLD_SHDW); |
| 287 | |
| 288 | ehrpwm_write(pc->mmio_base, TBPRD, period_cycles); |
| 289 | |
| 290 | /* Configure ehrpwm counter for up-count mode */ |
| 291 | ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CTRMODE_MASK, |
| 292 | TBCTL_CTRMODE_UP); |
| 293 | |
Philip, Avinash | daa5629 | 2012-09-06 10:40:03 +0530 | [diff] [blame] | 294 | if (pwm->hwpwm == 1) |
| 295 | /* Channel 1 configured with compare B register */ |
| 296 | cmp_reg = CMPB; |
| 297 | else |
| 298 | /* Channel 0 configured with compare A register */ |
| 299 | cmp_reg = CMPA; |
| 300 | |
| 301 | ehrpwm_write(pc->mmio_base, cmp_reg, duty_cycles); |
| 302 | |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 303 | pm_runtime_put_sync(chip->dev); |
| 304 | return 0; |
| 305 | } |
| 306 | |
Philip, Avinash | daa5629 | 2012-09-06 10:40:03 +0530 | [diff] [blame] | 307 | static int ehrpwm_pwm_set_polarity(struct pwm_chip *chip, |
| 308 | struct pwm_device *pwm, enum pwm_polarity polarity) |
| 309 | { |
| 310 | struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip); |
| 311 | |
| 312 | /* Configuration of polarity in hardware delayed, do at enable */ |
| 313 | pc->polarity[pwm->hwpwm] = polarity; |
| 314 | return 0; |
| 315 | } |
| 316 | |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 317 | static int ehrpwm_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) |
| 318 | { |
| 319 | struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip); |
| 320 | unsigned short aqcsfrc_val, aqcsfrc_mask; |
Philip, Avinash | 0074b49 | 2013-01-10 18:35:26 +0530 | [diff] [blame^] | 321 | int ret; |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 322 | |
| 323 | /* Leave clock enabled on enabling PWM */ |
| 324 | pm_runtime_get_sync(chip->dev); |
| 325 | |
| 326 | /* Disabling Action Qualifier on PWM output */ |
| 327 | if (pwm->hwpwm) { |
| 328 | aqcsfrc_val = AQCSFRC_CSFB_FRCDIS; |
| 329 | aqcsfrc_mask = AQCSFRC_CSFB_MASK; |
| 330 | } else { |
| 331 | aqcsfrc_val = AQCSFRC_CSFA_FRCDIS; |
| 332 | aqcsfrc_mask = AQCSFRC_CSFA_MASK; |
| 333 | } |
| 334 | |
| 335 | /* Changes to shadow mode */ |
| 336 | ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK, |
| 337 | AQSFRC_RLDCSF_ZRO); |
| 338 | |
| 339 | ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val); |
| 340 | |
Philip, Avinash | daa5629 | 2012-09-06 10:40:03 +0530 | [diff] [blame] | 341 | /* Channels polarity can be configured from action qualifier module */ |
| 342 | configure_polarity(pc, pwm->hwpwm); |
| 343 | |
Philip, Avinash | d91861d | 2012-11-27 14:18:12 +0530 | [diff] [blame] | 344 | /* Enable TBCLK before enabling PWM device */ |
Philip, Avinash | 0074b49 | 2013-01-10 18:35:26 +0530 | [diff] [blame^] | 345 | ret = clk_prepare_enable(pc->tbclk); |
| 346 | if (ret) { |
| 347 | pr_err("Failed to enable TBCLK for %s\n", |
| 348 | dev_name(pc->chip.dev)); |
| 349 | return ret; |
| 350 | } |
Philip, Avinash | d91861d | 2012-11-27 14:18:12 +0530 | [diff] [blame] | 351 | |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 352 | /* Enable time counter for free_run */ |
| 353 | ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_RUN_MASK, TBCTL_FREE_RUN); |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static void ehrpwm_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) |
| 358 | { |
| 359 | struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip); |
| 360 | unsigned short aqcsfrc_val, aqcsfrc_mask; |
| 361 | |
| 362 | /* Action Qualifier puts PWM output low forcefully */ |
| 363 | if (pwm->hwpwm) { |
| 364 | aqcsfrc_val = AQCSFRC_CSFB_FRCLOW; |
| 365 | aqcsfrc_mask = AQCSFRC_CSFB_MASK; |
| 366 | } else { |
| 367 | aqcsfrc_val = AQCSFRC_CSFA_FRCLOW; |
| 368 | aqcsfrc_mask = AQCSFRC_CSFA_MASK; |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Changes to immediate action on Action Qualifier. This puts |
| 373 | * Action Qualifier control on PWM output from next TBCLK |
| 374 | */ |
| 375 | ehrpwm_modify(pc->mmio_base, AQSFRC, AQSFRC_RLDCSF_MASK, |
| 376 | AQSFRC_RLDCSF_IMDT); |
| 377 | |
| 378 | ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val); |
| 379 | |
Philip, Avinash | d91861d | 2012-11-27 14:18:12 +0530 | [diff] [blame] | 380 | /* Disabling TBCLK on PWM disable */ |
Philip, Avinash | 0074b49 | 2013-01-10 18:35:26 +0530 | [diff] [blame^] | 381 | clk_disable_unprepare(pc->tbclk); |
Philip, Avinash | d91861d | 2012-11-27 14:18:12 +0530 | [diff] [blame] | 382 | |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 383 | /* Stop Time base counter */ |
| 384 | ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_RUN_MASK, TBCTL_STOP_NEXT); |
| 385 | |
| 386 | /* Disable clock on PWM disable */ |
| 387 | pm_runtime_put_sync(chip->dev); |
| 388 | } |
| 389 | |
| 390 | static void ehrpwm_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) |
| 391 | { |
Philip, Avinash | 01b2d45 | 2012-09-06 10:44:25 +0530 | [diff] [blame] | 392 | struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip); |
| 393 | |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 394 | if (test_bit(PWMF_ENABLED, &pwm->flags)) { |
| 395 | dev_warn(chip->dev, "Removing PWM device without disabling\n"); |
| 396 | pm_runtime_put_sync(chip->dev); |
| 397 | } |
Philip, Avinash | 01b2d45 | 2012-09-06 10:44:25 +0530 | [diff] [blame] | 398 | |
| 399 | /* set period value to zero on free */ |
| 400 | pc->period_cycles[pwm->hwpwm] = 0; |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static const struct pwm_ops ehrpwm_pwm_ops = { |
| 404 | .free = ehrpwm_pwm_free, |
| 405 | .config = ehrpwm_pwm_config, |
Philip, Avinash | daa5629 | 2012-09-06 10:40:03 +0530 | [diff] [blame] | 406 | .set_polarity = ehrpwm_pwm_set_polarity, |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 407 | .enable = ehrpwm_pwm_enable, |
| 408 | .disable = ehrpwm_pwm_disable, |
| 409 | .owner = THIS_MODULE, |
| 410 | }; |
| 411 | |
Philip, Avinash | 53ad9e8 | 2012-11-27 14:18:13 +0530 | [diff] [blame] | 412 | static const struct of_device_id ehrpwm_of_match[] = { |
| 413 | { .compatible = "ti,am33xx-ehrpwm" }, |
| 414 | {}, |
| 415 | }; |
| 416 | MODULE_DEVICE_TABLE(of, ehrpwm_of_match); |
| 417 | |
Bill Pemberton | 3e9fe83 | 2012-11-19 13:23:14 -0500 | [diff] [blame] | 418 | static int ehrpwm_pwm_probe(struct platform_device *pdev) |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 419 | { |
| 420 | int ret; |
| 421 | struct resource *r; |
| 422 | struct clk *clk; |
| 423 | struct ehrpwm_pwm_chip *pc; |
Philip, Avinash | 53ad9e8 | 2012-11-27 14:18:13 +0530 | [diff] [blame] | 424 | u16 status; |
Philip, Avinash | 98ccf49 | 2012-11-27 14:18:14 +0530 | [diff] [blame] | 425 | struct pinctrl *pinctrl; |
| 426 | |
| 427 | pinctrl = devm_pinctrl_get_select_default(&pdev->dev); |
| 428 | if (IS_ERR(pinctrl)) |
| 429 | dev_warn(&pdev->dev, "unable to select pin group\n"); |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 430 | |
| 431 | pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); |
| 432 | if (!pc) { |
| 433 | dev_err(&pdev->dev, "failed to allocate memory\n"); |
| 434 | return -ENOMEM; |
| 435 | } |
| 436 | |
| 437 | clk = devm_clk_get(&pdev->dev, "fck"); |
| 438 | if (IS_ERR(clk)) { |
| 439 | dev_err(&pdev->dev, "failed to get clock\n"); |
| 440 | return PTR_ERR(clk); |
| 441 | } |
| 442 | |
| 443 | pc->clk_rate = clk_get_rate(clk); |
| 444 | if (!pc->clk_rate) { |
| 445 | dev_err(&pdev->dev, "failed to get clock rate\n"); |
| 446 | return -EINVAL; |
| 447 | } |
| 448 | |
| 449 | pc->chip.dev = &pdev->dev; |
| 450 | pc->chip.ops = &ehrpwm_pwm_ops; |
Philip, Avinash | 53ad9e8 | 2012-11-27 14:18:13 +0530 | [diff] [blame] | 451 | pc->chip.of_xlate = of_pwm_xlate_with_flags; |
| 452 | pc->chip.of_pwm_n_cells = 3; |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 453 | pc->chip.base = -1; |
| 454 | pc->chip.npwm = NUM_PWM_CHANNEL; |
| 455 | |
| 456 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 457 | if (!r) { |
| 458 | dev_err(&pdev->dev, "no memory resource defined\n"); |
| 459 | return -ENODEV; |
| 460 | } |
| 461 | |
| 462 | pc->mmio_base = devm_request_and_ioremap(&pdev->dev, r); |
Axel Lin | 2ffdc9a | 2012-08-03 21:43:54 +0800 | [diff] [blame] | 463 | if (!pc->mmio_base) |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 464 | return -EADDRNOTAVAIL; |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 465 | |
Philip, Avinash | d91861d | 2012-11-27 14:18:12 +0530 | [diff] [blame] | 466 | /* Acquire tbclk for Time Base EHRPWM submodule */ |
| 467 | pc->tbclk = devm_clk_get(&pdev->dev, "tbclk"); |
| 468 | if (IS_ERR(pc->tbclk)) { |
| 469 | dev_err(&pdev->dev, "Failed to get tbclk\n"); |
| 470 | return PTR_ERR(pc->tbclk); |
| 471 | } |
| 472 | |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 473 | ret = pwmchip_add(&pc->chip); |
| 474 | if (ret < 0) { |
| 475 | dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | pm_runtime_enable(&pdev->dev); |
Philip, Avinash | 53ad9e8 | 2012-11-27 14:18:13 +0530 | [diff] [blame] | 480 | pm_runtime_get_sync(&pdev->dev); |
| 481 | |
| 482 | status = pwmss_submodule_state_change(pdev->dev.parent, |
| 483 | PWMSS_EPWMCLK_EN); |
| 484 | if (!(status & PWMSS_EPWMCLK_EN_ACK)) { |
| 485 | dev_err(&pdev->dev, "PWMSS config space clock enable failed\n"); |
| 486 | ret = -EINVAL; |
| 487 | goto pwmss_clk_failure; |
| 488 | } |
| 489 | |
| 490 | pm_runtime_put_sync(&pdev->dev); |
| 491 | |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 492 | platform_set_drvdata(pdev, pc); |
| 493 | return 0; |
Philip, Avinash | 53ad9e8 | 2012-11-27 14:18:13 +0530 | [diff] [blame] | 494 | |
| 495 | pwmss_clk_failure: |
| 496 | pm_runtime_put_sync(&pdev->dev); |
| 497 | pm_runtime_disable(&pdev->dev); |
| 498 | pwmchip_remove(&pc->chip); |
| 499 | return ret; |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 500 | } |
| 501 | |
Bill Pemberton | 77f3791 | 2012-11-19 13:26:09 -0500 | [diff] [blame] | 502 | static int ehrpwm_pwm_remove(struct platform_device *pdev) |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 503 | { |
| 504 | struct ehrpwm_pwm_chip *pc = platform_get_drvdata(pdev); |
| 505 | |
Philip, Avinash | 53ad9e8 | 2012-11-27 14:18:13 +0530 | [diff] [blame] | 506 | pm_runtime_get_sync(&pdev->dev); |
| 507 | /* |
| 508 | * Due to hardware misbehaviour, acknowledge of the stop_req |
| 509 | * is missing. Hence checking of the status bit skipped. |
| 510 | */ |
| 511 | pwmss_submodule_state_change(pdev->dev.parent, PWMSS_EPWMCLK_STOP_REQ); |
| 512 | pm_runtime_put_sync(&pdev->dev); |
| 513 | |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 514 | pm_runtime_put_sync(&pdev->dev); |
| 515 | pm_runtime_disable(&pdev->dev); |
| 516 | return pwmchip_remove(&pc->chip); |
| 517 | } |
| 518 | |
| 519 | static struct platform_driver ehrpwm_pwm_driver = { |
| 520 | .driver = { |
Philip, Avinash | 53ad9e8 | 2012-11-27 14:18:13 +0530 | [diff] [blame] | 521 | .name = "ehrpwm", |
| 522 | .owner = THIS_MODULE, |
| 523 | .of_match_table = ehrpwm_of_match, |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 524 | }, |
| 525 | .probe = ehrpwm_pwm_probe, |
Bill Pemberton | fd10911 | 2012-11-19 13:21:28 -0500 | [diff] [blame] | 526 | .remove = ehrpwm_pwm_remove, |
Philip, Avinash | 19891b2 | 2012-07-25 16:58:19 +0530 | [diff] [blame] | 527 | }; |
| 528 | |
| 529 | module_platform_driver(ehrpwm_pwm_driver); |
| 530 | |
| 531 | MODULE_DESCRIPTION("EHRPWM PWM driver"); |
| 532 | MODULE_AUTHOR("Texas Instruments"); |
| 533 | MODULE_LICENSE("GPL"); |