Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2016 Linaro Ltd.
0004  *
0005  * Author: Linus Walleij <linus.walleij@linaro.org>
0006  */
0007 
0008 #include <linux/bitops.h>
0009 #include <linux/delay.h>
0010 #include <linux/err.h>
0011 #include <linux/mfd/stmpe.h>
0012 #include <linux/module.h>
0013 #include <linux/of.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/pwm.h>
0016 #include <linux/slab.h>
0017 
0018 #define STMPE24XX_PWMCS     0x30
0019 #define PWMCS_EN_PWM0       BIT(0)
0020 #define PWMCS_EN_PWM1       BIT(1)
0021 #define PWMCS_EN_PWM2       BIT(2)
0022 #define STMPE24XX_PWMIC0    0x38
0023 #define STMPE24XX_PWMIC1    0x39
0024 #define STMPE24XX_PWMIC2    0x3a
0025 
0026 #define STMPE_PWM_24XX_PINBASE  21
0027 
0028 struct stmpe_pwm {
0029     struct stmpe *stmpe;
0030     struct pwm_chip chip;
0031     u8 last_duty;
0032 };
0033 
0034 static inline struct stmpe_pwm *to_stmpe_pwm(struct pwm_chip *chip)
0035 {
0036     return container_of(chip, struct stmpe_pwm, chip);
0037 }
0038 
0039 static int stmpe_24xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
0040 {
0041     struct stmpe_pwm *stmpe_pwm = to_stmpe_pwm(chip);
0042     u8 value;
0043     int ret;
0044 
0045     ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS);
0046     if (ret < 0) {
0047         dev_err(chip->dev, "error reading PWM#%u control\n",
0048             pwm->hwpwm);
0049         return ret;
0050     }
0051 
0052     value = ret | BIT(pwm->hwpwm);
0053 
0054     ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value);
0055     if (ret) {
0056         dev_err(chip->dev, "error writing PWM#%u control\n",
0057             pwm->hwpwm);
0058         return ret;
0059     }
0060 
0061     return 0;
0062 }
0063 
0064 static void stmpe_24xx_pwm_disable(struct pwm_chip *chip,
0065                    struct pwm_device *pwm)
0066 {
0067     struct stmpe_pwm *stmpe_pwm = to_stmpe_pwm(chip);
0068     u8 value;
0069     int ret;
0070 
0071     ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS);
0072     if (ret < 0) {
0073         dev_err(chip->dev, "error reading PWM#%u control\n",
0074             pwm->hwpwm);
0075         return;
0076     }
0077 
0078     value = ret & ~BIT(pwm->hwpwm);
0079 
0080     ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value);
0081     if (ret) {
0082         dev_err(chip->dev, "error writing PWM#%u control\n",
0083             pwm->hwpwm);
0084         return;
0085     }
0086 }
0087 
0088 /* STMPE 24xx PWM instructions */
0089 #define SMAX        0x007f
0090 #define SMIN        0x00ff
0091 #define GTS     0x0000
0092 #define LOAD        BIT(14) /* Only available on 2403 */
0093 #define RAMPUP      0x0000
0094 #define RAMPDOWN    BIT(7)
0095 #define PRESCALE_512    BIT(14)
0096 #define STEPTIME_1  BIT(8)
0097 #define BRANCH      (BIT(15) | BIT(13))
0098 
0099 static int stmpe_24xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
0100                  int duty_ns, int period_ns)
0101 {
0102     struct stmpe_pwm *stmpe_pwm = to_stmpe_pwm(chip);
0103     unsigned int i, pin;
0104     u16 program[3] = {
0105         SMAX,
0106         GTS,
0107         GTS,
0108     };
0109     u8 offset;
0110     int ret;
0111 
0112     /* Make sure we are disabled */
0113     if (pwm_is_enabled(pwm)) {
0114         stmpe_24xx_pwm_disable(chip, pwm);
0115     } else {
0116         /* Connect the PWM to the pin */
0117         pin = pwm->hwpwm;
0118 
0119         /* On STMPE2401 and 2403 pins 21,22,23 are used */
0120         if (stmpe_pwm->stmpe->partnum == STMPE2401 ||
0121             stmpe_pwm->stmpe->partnum == STMPE2403)
0122             pin += STMPE_PWM_24XX_PINBASE;
0123 
0124         ret = stmpe_set_altfunc(stmpe_pwm->stmpe, BIT(pin),
0125                     STMPE_BLOCK_PWM);
0126         if (ret) {
0127             dev_err(chip->dev, "unable to connect PWM#%u to pin\n",
0128                 pwm->hwpwm);
0129             return ret;
0130         }
0131     }
0132 
0133     /* STMPE24XX */
0134     switch (pwm->hwpwm) {
0135     case 0:
0136         offset = STMPE24XX_PWMIC0;
0137         break;
0138 
0139     case 1:
0140         offset = STMPE24XX_PWMIC1;
0141         break;
0142 
0143     case 2:
0144         offset = STMPE24XX_PWMIC2;
0145         break;
0146 
0147     default:
0148         /* Should not happen as npwm is 3 */
0149         return -ENODEV;
0150     }
0151 
0152     dev_dbg(chip->dev, "PWM#%u: config duty %d ns, period %d ns\n",
0153         pwm->hwpwm, duty_ns, period_ns);
0154 
0155     if (duty_ns == 0) {
0156         if (stmpe_pwm->stmpe->partnum == STMPE2401)
0157             program[0] = SMAX; /* off all the time */
0158 
0159         if (stmpe_pwm->stmpe->partnum == STMPE2403)
0160             program[0] = LOAD | 0xff; /* LOAD 0xff */
0161 
0162         stmpe_pwm->last_duty = 0x00;
0163     } else if (duty_ns == period_ns) {
0164         if (stmpe_pwm->stmpe->partnum == STMPE2401)
0165             program[0] = SMIN; /* on all the time */
0166 
0167         if (stmpe_pwm->stmpe->partnum == STMPE2403)
0168             program[0] = LOAD | 0x00; /* LOAD 0x00 */
0169 
0170         stmpe_pwm->last_duty = 0xff;
0171     } else {
0172         u8 value, last = stmpe_pwm->last_duty;
0173         unsigned long duty;
0174 
0175         /*
0176          * Counter goes from 0x00 to 0xff repeatedly at 32768 Hz,
0177          * (means a period of 30517 ns) then this is compared to the
0178          * counter from the ramp, if this is >= PWM counter the output
0179          * is high. With LOAD we can define how much of the cycle it
0180          * is on.
0181          *
0182          * Prescale = 0 -> 2 kHz -> T = 1/f = 488281.25 ns
0183          */
0184 
0185         /* Scale to 0..0xff */
0186         duty = duty_ns * 256;
0187         duty = DIV_ROUND_CLOSEST(duty, period_ns);
0188         value = duty;
0189 
0190         if (value == last) {
0191             /* Run the old program */
0192             if (pwm_is_enabled(pwm))
0193                 stmpe_24xx_pwm_enable(chip, pwm);
0194 
0195             return 0;
0196         } else if (stmpe_pwm->stmpe->partnum == STMPE2403) {
0197             /* STMPE2403 can simply set the right PWM value */
0198             program[0] = LOAD | value;
0199             program[1] = 0x0000;
0200         } else if (stmpe_pwm->stmpe->partnum == STMPE2401) {
0201             /* STMPE2401 need a complex program */
0202             u16 incdec = 0x0000;
0203 
0204             if (last < value)
0205                 /* Count up */
0206                 incdec = RAMPUP | (value - last);
0207             else
0208                 /* Count down */
0209                 incdec = RAMPDOWN | (last - value);
0210 
0211             /* Step to desired value, smoothly */
0212             program[0] = PRESCALE_512 | STEPTIME_1 | incdec;
0213 
0214             /* Loop eternally to 0x00 */
0215             program[1] = BRANCH;
0216         }
0217 
0218         dev_dbg(chip->dev,
0219             "PWM#%u: value = %02x, last_duty = %02x, program=%04x,%04x,%04x\n",
0220             pwm->hwpwm, value, last, program[0], program[1],
0221             program[2]);
0222         stmpe_pwm->last_duty = value;
0223     }
0224 
0225     /*
0226      * We can write programs of up to 64 16-bit words into this channel.
0227      */
0228     for (i = 0; i < ARRAY_SIZE(program); i++) {
0229         u8 value;
0230 
0231         value = (program[i] >> 8) & 0xff;
0232 
0233         ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value);
0234         if (ret) {
0235             dev_err(chip->dev, "error writing register %02x: %d\n",
0236                 offset, ret);
0237             return ret;
0238         }
0239 
0240         value = program[i] & 0xff;
0241 
0242         ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value);
0243         if (ret) {
0244             dev_err(chip->dev, "error writing register %02x: %d\n",
0245                 offset, ret);
0246             return ret;
0247         }
0248     }
0249 
0250     /* If we were enabled, re-enable this PWM */
0251     if (pwm_is_enabled(pwm))
0252         stmpe_24xx_pwm_enable(chip, pwm);
0253 
0254     /* Sleep for 200ms so we're sure it will take effect */
0255     msleep(200);
0256 
0257     dev_dbg(chip->dev, "programmed PWM#%u, %u bytes\n", pwm->hwpwm, i);
0258 
0259     return 0;
0260 }
0261 
0262 static int stmpe_24xx_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
0263                 const struct pwm_state *state)
0264 {
0265     int err;
0266 
0267     if (state->polarity != PWM_POLARITY_NORMAL)
0268         return -EINVAL;
0269 
0270     if (!state->enabled) {
0271         if (pwm->state.enabled)
0272             stmpe_24xx_pwm_disable(chip, pwm);
0273 
0274         return 0;
0275     }
0276 
0277     err = stmpe_24xx_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period);
0278     if (err)
0279         return err;
0280 
0281     if (!pwm->state.enabled)
0282         err = stmpe_24xx_pwm_enable(chip, pwm);
0283 
0284     return err;
0285 }
0286 
0287 static const struct pwm_ops stmpe_24xx_pwm_ops = {
0288     .apply = stmpe_24xx_pwm_apply,
0289     .owner = THIS_MODULE,
0290 };
0291 
0292 static int __init stmpe_pwm_probe(struct platform_device *pdev)
0293 {
0294     struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
0295     struct stmpe_pwm *stmpe_pwm;
0296     int ret;
0297 
0298     stmpe_pwm = devm_kzalloc(&pdev->dev, sizeof(*stmpe_pwm), GFP_KERNEL);
0299     if (!stmpe_pwm)
0300         return -ENOMEM;
0301 
0302     stmpe_pwm->stmpe = stmpe;
0303     stmpe_pwm->chip.dev = &pdev->dev;
0304 
0305     if (stmpe->partnum == STMPE2401 || stmpe->partnum == STMPE2403) {
0306         stmpe_pwm->chip.ops = &stmpe_24xx_pwm_ops;
0307         stmpe_pwm->chip.npwm = 3;
0308     } else {
0309         if (stmpe->partnum == STMPE1601)
0310             dev_err(&pdev->dev, "STMPE1601 not yet supported\n");
0311         else
0312             dev_err(&pdev->dev, "Unknown STMPE PWM\n");
0313 
0314         return -ENODEV;
0315     }
0316 
0317     ret = stmpe_enable(stmpe, STMPE_BLOCK_PWM);
0318     if (ret)
0319         return ret;
0320 
0321     ret = pwmchip_add(&stmpe_pwm->chip);
0322     if (ret) {
0323         stmpe_disable(stmpe, STMPE_BLOCK_PWM);
0324         return ret;
0325     }
0326 
0327     return 0;
0328 }
0329 
0330 static struct platform_driver stmpe_pwm_driver = {
0331     .driver = {
0332         .name = "stmpe-pwm",
0333     },
0334 };
0335 builtin_platform_driver_probe(stmpe_pwm_driver, stmpe_pwm_probe);