Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'pwm/for-5.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry.reding/linux-pwm

Pull pwm updates from Thierry Reding:
"Nothing too exciting for this cycle. A couple of fixes across the
board, and Lee volunteered to help with patch review"

* tag 'pwm/for-5.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry.reding/linux-pwm:
pwm: Add missing "CONFIG_" prefix
MAINTAINERS: Add Lee Jones as reviewer for the PWM subsystem
pwm: imx27: Fix rounding behavior
pwm: rockchip: Simplify rockchip_pwm_get_state()
pwm: img: Call pm_runtime_put() in pm_runtime_get_sync() failed case
pwm: tegra: Support dynamic clock frequency configuration
pwm: jz4740: Add support for the JZ4725B
pwm: jz4740: Make PWM start with the active part
pwm: jz4740: Enhance precision in calculation of duty cycle
pwm: jz4740: Drop dependency on MACH_INGENIC
pwm: lpss: Fix get_state runtime-pm reference handling
pwm: sun4i: Support direct clock output on Allwinner A64
pwm: Add support for Azoteq IQS620A PWM generator
dt-bindings: pwm: rcar: add r8a77961 support
pwm: Add missing '\n' in log messages

+438 -45
+1
Documentation/devicetree/bindings/pwm/renesas,pwm-rcar.yaml
··· 27 27 - renesas,pwm-r8a7794 # R-Car E2 28 28 - renesas,pwm-r8a7795 # R-Car H3 29 29 - renesas,pwm-r8a7796 # R-Car M3-W 30 + - renesas,pwm-r8a77961 # R-Car M3-W+ 30 31 - renesas,pwm-r8a77965 # R-Car M3-N 31 32 - renesas,pwm-r8a77970 # R-Car V3M 32 33 - renesas,pwm-r8a77980 # R-Car V3H
+1
MAINTAINERS
··· 13919 13919 PWM SUBSYSTEM 13920 13920 M: Thierry Reding <thierry.reding@gmail.com> 13921 13921 R: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> 13922 + M: Lee Jones <lee.jones@linaro.org> 13922 13923 L: linux-pwm@vger.kernel.org 13923 13924 S: Maintained 13924 13925 Q: https://patchwork.ozlabs.org/project/linux-pwm/list/
+11 -1
drivers/pwm/Kconfig
··· 232 232 To compile this driver as a module, choose M here: the module 233 233 will be called pwm-imx-tpm. 234 234 235 + config PWM_IQS620A 236 + tristate "Azoteq IQS620A PWM support" 237 + depends on MFD_IQS62X || COMPILE_TEST 238 + help 239 + Generic PWM framework driver for the Azoteq IQS620A multi-function 240 + sensor. 241 + 242 + To compile this driver as a module, choose M here: the module will 243 + be called pwm-iqs620a. 244 + 235 245 config PWM_JZ4740 236 246 tristate "Ingenic JZ47xx PWM support" 237 - depends on MACH_INGENIC 247 + depends on MIPS 238 248 depends on COMMON_CLK 239 249 select MFD_SYSCON 240 250 help
+1
drivers/pwm/Makefile
··· 20 20 obj-$(CONFIG_PWM_IMX1) += pwm-imx1.o 21 21 obj-$(CONFIG_PWM_IMX27) += pwm-imx27.o 22 22 obj-$(CONFIG_PWM_IMX_TPM) += pwm-imx-tpm.o 23 + obj-$(CONFIG_PWM_IQS620A) += pwm-iqs620a.o 23 24 obj-$(CONFIG_PWM_JZ4740) += pwm-jz4740.o 24 25 obj-$(CONFIG_PWM_LP3943) += pwm-lp3943.o 25 26 obj-$(CONFIG_PWM_LPC18XX_SCT) += pwm-lpc18xx-sct.o
+2 -2
drivers/pwm/core.c
··· 121 121 pwm->chip->ops->get_state(pwm->chip, pwm, &pwm->state); 122 122 trace_pwm_get(pwm, &pwm->state); 123 123 124 - if (IS_ENABLED(PWM_DEBUG)) 124 + if (IS_ENABLED(CONFIG_PWM_DEBUG)) 125 125 pwm->last = pwm->state; 126 126 } 127 127 ··· 537 537 538 538 if (!state->enabled && s2.enabled && s2.duty_cycle > 0) 539 539 dev_warn(chip->dev, 540 - "requested disabled, but yielded enabled with duty > 0"); 540 + "requested disabled, but yielded enabled with duty > 0\n"); 541 541 542 542 /* reapply the state that the driver reported being configured. */ 543 543 err = chip->ops->apply(chip, pwm, &s1);
+6 -2
drivers/pwm/pwm-img.c
··· 129 129 duty = DIV_ROUND_UP(timebase * duty_ns, period_ns); 130 130 131 131 ret = pm_runtime_get_sync(chip->dev); 132 - if (ret < 0) 132 + if (ret < 0) { 133 + pm_runtime_put_autosuspend(chip->dev); 133 134 return ret; 135 + } 134 136 135 137 val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG); 136 138 val &= ~(PWM_CTRL_CFG_DIV_MASK << PWM_CTRL_CFG_DIV_SHIFT(pwm->hwpwm)); ··· 333 331 int ret; 334 332 335 333 ret = pm_runtime_get_sync(&pdev->dev); 336 - if (ret < 0) 334 + if (ret < 0) { 335 + pm_runtime_put(&pdev->dev); 337 336 return ret; 337 + } 338 338 339 339 for (i = 0; i < pwm_chip->chip.npwm; i++) { 340 340 val = img_pwm_readl(pwm_chip, PWM_CTRL_CFG);
+10 -10
drivers/pwm/pwm-imx27.c
··· 150 150 151 151 prescaler = MX3_PWMCR_PRESCALER_GET(val); 152 152 pwm_clk = clk_get_rate(imx->clk_per); 153 - pwm_clk = DIV_ROUND_CLOSEST_ULL(pwm_clk, prescaler); 154 153 val = readl(imx->mmio_base + MX3_PWMPR); 155 154 period = val >= MX3_PWMPR_MAX ? MX3_PWMPR_MAX : val; 156 155 157 156 /* PWMOUT (Hz) = PWMCLK / (PWMPR + 2) */ 158 - tmp = NSEC_PER_SEC * (u64)(period + 2); 159 - state->period = DIV_ROUND_CLOSEST_ULL(tmp, pwm_clk); 157 + tmp = NSEC_PER_SEC * (u64)(period + 2) * prescaler; 158 + state->period = DIV_ROUND_UP_ULL(tmp, pwm_clk); 160 159 161 160 /* 162 161 * PWMSAR can be read only if PWM is enabled. If the PWM is disabled, ··· 166 167 else 167 168 val = imx->duty_cycle; 168 169 169 - tmp = NSEC_PER_SEC * (u64)(val); 170 - state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, pwm_clk); 170 + tmp = NSEC_PER_SEC * (u64)(val) * prescaler; 171 + state->duty_cycle = DIV_ROUND_UP_ULL(tmp, pwm_clk); 171 172 172 173 pwm_imx27_clk_disable_unprepare(imx); 173 174 } ··· 219 220 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip); 220 221 struct pwm_state cstate; 221 222 unsigned long long c; 223 + unsigned long long clkrate; 222 224 int ret; 223 225 u32 cr; 224 226 225 227 pwm_get_state(pwm, &cstate); 226 228 227 - c = clk_get_rate(imx->clk_per); 228 - c *= state->period; 229 + clkrate = clk_get_rate(imx->clk_per); 230 + c = clkrate * state->period; 229 231 230 - do_div(c, 1000000000); 232 + do_div(c, NSEC_PER_SEC); 231 233 period_cycles = c; 232 234 233 235 prescale = period_cycles / 0x10000 + 1; 234 236 235 237 period_cycles /= prescale; 236 - c = (unsigned long long)period_cycles * state->duty_cycle; 237 - do_div(c, state->period); 238 + c = clkrate * state->duty_cycle; 239 + do_div(c, NSEC_PER_SEC * prescale); 238 240 duty_cycles = c; 239 241 240 242 /*
+270
drivers/pwm/pwm-iqs620a.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * Azoteq IQS620A PWM Generator 4 + * 5 + * Copyright (C) 2019 Jeff LaBundy <jeff@labundy.com> 6 + * 7 + * Limitations: 8 + * - The period is fixed to 1 ms and is generated continuously despite changes 9 + * to the duty cycle or enable/disable state. 10 + * - Changes to the duty cycle or enable/disable state take effect immediately 11 + * and may result in a glitch during the period in which the change is made. 12 + * - The device cannot generate a 0% duty cycle. For duty cycles below 1 / 256 13 + * ms, the output is disabled and relies upon an external pull-down resistor 14 + * to hold the GPIO3/LTX pin low. 15 + */ 16 + 17 + #include <linux/device.h> 18 + #include <linux/kernel.h> 19 + #include <linux/mfd/iqs62x.h> 20 + #include <linux/module.h> 21 + #include <linux/mutex.h> 22 + #include <linux/notifier.h> 23 + #include <linux/platform_device.h> 24 + #include <linux/pwm.h> 25 + #include <linux/regmap.h> 26 + #include <linux/slab.h> 27 + 28 + #define IQS620_PWR_SETTINGS 0xD2 29 + #define IQS620_PWR_SETTINGS_PWM_OUT BIT(7) 30 + 31 + #define IQS620_PWM_DUTY_CYCLE 0xD8 32 + 33 + #define IQS620_PWM_PERIOD_NS 1000000 34 + 35 + struct iqs620_pwm_private { 36 + struct iqs62x_core *iqs62x; 37 + struct pwm_chip chip; 38 + struct notifier_block notifier; 39 + struct mutex lock; 40 + bool out_en; 41 + u8 duty_val; 42 + }; 43 + 44 + static int iqs620_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 45 + const struct pwm_state *state) 46 + { 47 + struct iqs620_pwm_private *iqs620_pwm; 48 + struct iqs62x_core *iqs62x; 49 + int duty_scale, ret; 50 + 51 + if (state->polarity != PWM_POLARITY_NORMAL) 52 + return -ENOTSUPP; 53 + 54 + if (state->period < IQS620_PWM_PERIOD_NS) 55 + return -EINVAL; 56 + 57 + iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); 58 + iqs62x = iqs620_pwm->iqs62x; 59 + 60 + /* 61 + * The duty cycle generated by the device is calculated as follows: 62 + * 63 + * duty_cycle = (IQS620_PWM_DUTY_CYCLE + 1) / 256 * 1 ms 64 + * 65 + * ...where IQS620_PWM_DUTY_CYCLE is a register value between 0 and 255 66 + * (inclusive). Therefore the lowest duty cycle the device can generate 67 + * while the output is enabled is 1 / 256 ms. 68 + * 69 + * For lower duty cycles (e.g. 0), the PWM output is simply disabled to 70 + * allow an external pull-down resistor to hold the GPIO3/LTX pin low. 71 + */ 72 + duty_scale = state->duty_cycle * 256 / IQS620_PWM_PERIOD_NS; 73 + 74 + mutex_lock(&iqs620_pwm->lock); 75 + 76 + if (!state->enabled || !duty_scale) { 77 + ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS, 78 + IQS620_PWR_SETTINGS_PWM_OUT, 0); 79 + if (ret) 80 + goto err_mutex; 81 + } 82 + 83 + if (duty_scale) { 84 + u8 duty_val = min(duty_scale - 1, 0xFF); 85 + 86 + ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE, 87 + duty_val); 88 + if (ret) 89 + goto err_mutex; 90 + 91 + iqs620_pwm->duty_val = duty_val; 92 + } 93 + 94 + if (state->enabled && duty_scale) { 95 + ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS, 96 + IQS620_PWR_SETTINGS_PWM_OUT, 0xFF); 97 + if (ret) 98 + goto err_mutex; 99 + } 100 + 101 + iqs620_pwm->out_en = state->enabled; 102 + 103 + err_mutex: 104 + mutex_unlock(&iqs620_pwm->lock); 105 + 106 + return ret; 107 + } 108 + 109 + static void iqs620_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, 110 + struct pwm_state *state) 111 + { 112 + struct iqs620_pwm_private *iqs620_pwm; 113 + 114 + iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); 115 + 116 + mutex_lock(&iqs620_pwm->lock); 117 + 118 + /* 119 + * Since the device cannot generate a 0% duty cycle, requests to do so 120 + * cause subsequent calls to iqs620_pwm_get_state to report the output 121 + * as disabled with duty cycle equal to that which was in use prior to 122 + * the request. This is not ideal, but is the best compromise based on 123 + * the capabilities of the device. 124 + */ 125 + state->enabled = iqs620_pwm->out_en; 126 + state->duty_cycle = DIV_ROUND_UP((iqs620_pwm->duty_val + 1) * 127 + IQS620_PWM_PERIOD_NS, 256); 128 + 129 + mutex_unlock(&iqs620_pwm->lock); 130 + 131 + state->period = IQS620_PWM_PERIOD_NS; 132 + } 133 + 134 + static int iqs620_pwm_notifier(struct notifier_block *notifier, 135 + unsigned long event_flags, void *context) 136 + { 137 + struct iqs620_pwm_private *iqs620_pwm; 138 + struct iqs62x_core *iqs62x; 139 + int ret; 140 + 141 + if (!(event_flags & BIT(IQS62X_EVENT_SYS_RESET))) 142 + return NOTIFY_DONE; 143 + 144 + iqs620_pwm = container_of(notifier, struct iqs620_pwm_private, 145 + notifier); 146 + iqs62x = iqs620_pwm->iqs62x; 147 + 148 + mutex_lock(&iqs620_pwm->lock); 149 + 150 + /* 151 + * The parent MFD driver already prints an error message in the event 152 + * of a device reset, so nothing else is printed here unless there is 153 + * an additional failure. 154 + */ 155 + ret = regmap_write(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE, 156 + iqs620_pwm->duty_val); 157 + if (ret) 158 + goto err_mutex; 159 + 160 + ret = regmap_update_bits(iqs62x->regmap, IQS620_PWR_SETTINGS, 161 + IQS620_PWR_SETTINGS_PWM_OUT, 162 + iqs620_pwm->out_en ? 0xFF : 0); 163 + 164 + err_mutex: 165 + mutex_unlock(&iqs620_pwm->lock); 166 + 167 + if (ret) { 168 + dev_err(iqs620_pwm->chip.dev, 169 + "Failed to re-initialize device: %d\n", ret); 170 + return NOTIFY_BAD; 171 + } 172 + 173 + return NOTIFY_OK; 174 + } 175 + 176 + static const struct pwm_ops iqs620_pwm_ops = { 177 + .apply = iqs620_pwm_apply, 178 + .get_state = iqs620_pwm_get_state, 179 + .owner = THIS_MODULE, 180 + }; 181 + 182 + static void iqs620_pwm_notifier_unregister(void *context) 183 + { 184 + struct iqs620_pwm_private *iqs620_pwm = context; 185 + int ret; 186 + 187 + ret = blocking_notifier_chain_unregister(&iqs620_pwm->iqs62x->nh, 188 + &iqs620_pwm->notifier); 189 + if (ret) 190 + dev_err(iqs620_pwm->chip.dev, 191 + "Failed to unregister notifier: %d\n", ret); 192 + } 193 + 194 + static int iqs620_pwm_probe(struct platform_device *pdev) 195 + { 196 + struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent); 197 + struct iqs620_pwm_private *iqs620_pwm; 198 + unsigned int val; 199 + int ret; 200 + 201 + iqs620_pwm = devm_kzalloc(&pdev->dev, sizeof(*iqs620_pwm), GFP_KERNEL); 202 + if (!iqs620_pwm) 203 + return -ENOMEM; 204 + 205 + platform_set_drvdata(pdev, iqs620_pwm); 206 + iqs620_pwm->iqs62x = iqs62x; 207 + 208 + ret = regmap_read(iqs62x->regmap, IQS620_PWR_SETTINGS, &val); 209 + if (ret) 210 + return ret; 211 + iqs620_pwm->out_en = val & IQS620_PWR_SETTINGS_PWM_OUT; 212 + 213 + ret = regmap_read(iqs62x->regmap, IQS620_PWM_DUTY_CYCLE, &val); 214 + if (ret) 215 + return ret; 216 + iqs620_pwm->duty_val = val; 217 + 218 + iqs620_pwm->chip.dev = &pdev->dev; 219 + iqs620_pwm->chip.ops = &iqs620_pwm_ops; 220 + iqs620_pwm->chip.base = -1; 221 + iqs620_pwm->chip.npwm = 1; 222 + 223 + mutex_init(&iqs620_pwm->lock); 224 + 225 + iqs620_pwm->notifier.notifier_call = iqs620_pwm_notifier; 226 + ret = blocking_notifier_chain_register(&iqs620_pwm->iqs62x->nh, 227 + &iqs620_pwm->notifier); 228 + if (ret) { 229 + dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret); 230 + return ret; 231 + } 232 + 233 + ret = devm_add_action_or_reset(&pdev->dev, 234 + iqs620_pwm_notifier_unregister, 235 + iqs620_pwm); 236 + if (ret) 237 + return ret; 238 + 239 + ret = pwmchip_add(&iqs620_pwm->chip); 240 + if (ret) 241 + dev_err(&pdev->dev, "Failed to add device: %d\n", ret); 242 + 243 + return ret; 244 + } 245 + 246 + static int iqs620_pwm_remove(struct platform_device *pdev) 247 + { 248 + struct iqs620_pwm_private *iqs620_pwm = platform_get_drvdata(pdev); 249 + int ret; 250 + 251 + ret = pwmchip_remove(&iqs620_pwm->chip); 252 + if (ret) 253 + dev_err(&pdev->dev, "Failed to remove device: %d\n", ret); 254 + 255 + return ret; 256 + } 257 + 258 + static struct platform_driver iqs620_pwm_platform_driver = { 259 + .driver = { 260 + .name = "iqs620a-pwm", 261 + }, 262 + .probe = iqs620_pwm_probe, 263 + .remove = iqs620_pwm_remove, 264 + }; 265 + module_platform_driver(iqs620_pwm_platform_driver); 266 + 267 + MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>"); 268 + MODULE_DESCRIPTION("Azoteq IQS620A PWM Generator"); 269 + MODULE_LICENSE("GPL"); 270 + MODULE_ALIAS("platform:iqs620a-pwm");
+39 -16
drivers/pwm/pwm-jz4740.c
··· 6 6 * Limitations: 7 7 * - The .apply callback doesn't complete the currently running period before 8 8 * reconfiguring the hardware. 9 - * - Each period starts with the inactive part. 10 9 */ 11 10 12 11 #include <linux/clk.h> ··· 20 21 #include <linux/pwm.h> 21 22 #include <linux/regmap.h> 22 23 23 - #define NUM_PWM 8 24 + struct soc_info { 25 + unsigned int num_pwms; 26 + }; 24 27 25 28 struct jz4740_pwm_chip { 26 29 struct pwm_chip chip; ··· 38 37 unsigned int channel) 39 38 { 40 39 /* Enable all TCU channels for PWM use by default except channels 0/1 */ 41 - u32 pwm_channels_mask = GENMASK(NUM_PWM - 1, 2); 40 + u32 pwm_channels_mask = GENMASK(jz->chip.npwm - 1, 2); 42 41 43 42 device_property_read_u32(jz->chip.dev->parent, 44 43 "ingenic,pwm-channels-mask", ··· 159 158 /* Calculate period value */ 160 159 tmp = (unsigned long long)rate * state->period; 161 160 do_div(tmp, NSEC_PER_SEC); 162 - period = (unsigned long)tmp; 161 + period = tmp; 163 162 164 163 /* Calculate duty value */ 165 - tmp = (unsigned long long)period * state->duty_cycle; 166 - do_div(tmp, state->period); 167 - duty = period - tmp; 164 + tmp = (unsigned long long)rate * state->duty_cycle; 165 + do_div(tmp, NSEC_PER_SEC); 166 + duty = tmp; 168 167 169 168 if (duty >= period) 170 169 duty = period - 1; ··· 190 189 regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), 191 190 TCU_TCSR_PWM_SD, TCU_TCSR_PWM_SD); 192 191 193 - /* Set polarity */ 194 - switch (state->polarity) { 195 - case PWM_POLARITY_NORMAL: 192 + /* 193 + * Set polarity. 194 + * 195 + * The PWM starts in inactive state until the internal timer reaches the 196 + * duty value, then becomes active until the timer reaches the period 197 + * value. In theory, we should then use (period - duty) as the real duty 198 + * value, as a high duty value would otherwise result in the PWM pin 199 + * being inactive most of the time. 200 + * 201 + * Here, we don't do that, and instead invert the polarity of the PWM 202 + * when it is active. This trick makes the PWM start with its active 203 + * state instead of its inactive state. 204 + */ 205 + if ((state->polarity == PWM_POLARITY_NORMAL) ^ state->enabled) 196 206 regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), 197 207 TCU_TCSR_PWM_INITL_HIGH, 0); 198 - break; 199 - case PWM_POLARITY_INVERSED: 208 + else 200 209 regmap_update_bits(jz4740->map, TCU_REG_TCSRc(pwm->hwpwm), 201 210 TCU_TCSR_PWM_INITL_HIGH, 202 211 TCU_TCSR_PWM_INITL_HIGH); 203 - break; 204 - } 205 212 206 213 if (state->enabled) 207 214 jz4740_pwm_enable(chip, pwm); ··· 228 219 { 229 220 struct device *dev = &pdev->dev; 230 221 struct jz4740_pwm_chip *jz4740; 222 + const struct soc_info *info; 223 + 224 + info = device_get_match_data(dev); 225 + if (!info) 226 + return -EINVAL; 231 227 232 228 jz4740 = devm_kzalloc(dev, sizeof(*jz4740), GFP_KERNEL); 233 229 if (!jz4740) ··· 246 232 247 233 jz4740->chip.dev = dev; 248 234 jz4740->chip.ops = &jz4740_pwm_ops; 249 - jz4740->chip.npwm = NUM_PWM; 235 + jz4740->chip.npwm = info->num_pwms; 250 236 jz4740->chip.base = -1; 251 237 jz4740->chip.of_xlate = of_pwm_xlate_with_flags; 252 238 jz4740->chip.of_pwm_n_cells = 3; ··· 263 249 return pwmchip_remove(&jz4740->chip); 264 250 } 265 251 252 + static const struct soc_info __maybe_unused jz4740_soc_info = { 253 + .num_pwms = 8, 254 + }; 255 + 256 + static const struct soc_info __maybe_unused jz4725b_soc_info = { 257 + .num_pwms = 6, 258 + }; 259 + 266 260 #ifdef CONFIG_OF 267 261 static const struct of_device_id jz4740_pwm_dt_ids[] = { 268 - { .compatible = "ingenic,jz4740-pwm", }, 262 + { .compatible = "ingenic,jz4740-pwm", .data = &jz4740_soc_info }, 263 + { .compatible = "ingenic,jz4725b-pwm", .data = &jz4725b_soc_info }, 269 264 {}, 270 265 }; 271 266 MODULE_DEVICE_TABLE(of, jz4740_pwm_dt_ids);
+11 -4
drivers/pwm/pwm-lpss.c
··· 158 158 return 0; 159 159 } 160 160 161 - /* This function gets called once from pwmchip_add to get the initial state */ 162 161 static void pwm_lpss_get_state(struct pwm_chip *chip, struct pwm_device *pwm, 163 162 struct pwm_state *state) 164 163 { ··· 165 166 unsigned long base_unit_range; 166 167 unsigned long long base_unit, freq, on_time_div; 167 168 u32 ctrl; 169 + 170 + pm_runtime_get_sync(chip->dev); 168 171 169 172 base_unit_range = BIT(lpwm->info->base_unit_bits); 170 173 ··· 188 187 state->polarity = PWM_POLARITY_NORMAL; 189 188 state->enabled = !!(ctrl & PWM_ENABLE); 190 189 191 - if (state->enabled) 192 - pm_runtime_get(chip->dev); 190 + pm_runtime_put(chip->dev); 193 191 } 194 192 195 193 static const struct pwm_ops pwm_lpss_ops = { ··· 202 202 { 203 203 struct pwm_lpss_chip *lpwm; 204 204 unsigned long c; 205 - int ret; 205 + int i, ret; 206 + u32 ctrl; 206 207 207 208 if (WARN_ON(info->npwm > MAX_PWMS)) 208 209 return ERR_PTR(-ENODEV); ··· 231 230 if (ret) { 232 231 dev_err(dev, "failed to add PWM chip: %d\n", ret); 233 232 return ERR_PTR(ret); 233 + } 234 + 235 + for (i = 0; i < lpwm->info->npwm; i++) { 236 + ctrl = pwm_lpss_read(&lpwm->chip.pwms[i]); 237 + if (ctrl & PWM_ENABLE) 238 + pm_runtime_get(dev); 234 239 } 235 240 236 241 return lpwm;
+1 -6
drivers/pwm/pwm-rockchip.c
··· 83 83 state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate); 84 84 85 85 val = readl_relaxed(pc->base + pc->data->regs.ctrl); 86 - if (pc->data->supports_polarity) 87 - state->enabled = ((val & enable_conf) != enable_conf) ? 88 - false : true; 89 - else 90 - state->enabled = ((val & enable_conf) == enable_conf) ? 91 - true : false; 86 + state->enabled = (val & enable_conf) == enable_conf; 92 87 93 88 if (pc->data->supports_polarity && !(val & PWM_DUTY_POSITIVE)) 94 89 state->polarity = PWM_POLARITY_INVERSED;
+9
drivers/pwm/pwm-sun4i.c
··· 352 352 .npwm = 1, 353 353 }; 354 354 355 + static const struct sun4i_pwm_data sun50i_a64_pwm_data = { 356 + .has_prescaler_bypass = true, 357 + .has_direct_mod_clk_output = true, 358 + .npwm = 1, 359 + }; 360 + 355 361 static const struct sun4i_pwm_data sun50i_h6_pwm_data = { 356 362 .has_prescaler_bypass = true, 357 363 .has_direct_mod_clk_output = true, ··· 380 374 }, { 381 375 .compatible = "allwinner,sun8i-h3-pwm", 382 376 .data = &sun4i_pwm_single_bypass, 377 + }, { 378 + .compatible = "allwinner,sun50i-a64-pwm", 379 + .data = &sun50i_a64_pwm_data, 383 380 }, { 384 381 .compatible = "allwinner,sun50i-h6-pwm", 385 382 .data = &sun50i_h6_pwm_data,
+76 -4
drivers/pwm/pwm-tegra.c
··· 4 4 * 5 5 * Tegra pulse-width-modulation controller driver 6 6 * 7 - * Copyright (c) 2010, NVIDIA Corporation. 7 + * Copyright (c) 2010-2020, NVIDIA Corporation. 8 8 * Based on arch/arm/plat-mxc/pwm.c by Sascha Hauer <s.hauer@pengutronix.de> 9 + * 10 + * Overview of Tegra Pulse Width Modulator Register: 11 + * 1. 13-bit: Frequency division (SCALE) 12 + * 2. 8-bit : Pulse division (DUTY) 13 + * 3. 1-bit : Enable bit 14 + * 15 + * The PWM clock frequency is divided by 256 before subdividing it based 16 + * on the programmable frequency division value to generate the required 17 + * frequency for PWM output. The maximum output frequency that can be 18 + * achieved is (max rate of source clock) / 256. 19 + * e.g. if source clock rate is 408 MHz, maximum output frequency can be: 20 + * 408 MHz/256 = 1.6 MHz. 21 + * This 1.6 MHz frequency can further be divided using SCALE value in PWM. 22 + * 23 + * PWM pulse width: 8 bits are usable [23:16] for varying pulse width. 24 + * To achieve 100% duty cycle, program Bit [24] of this register to 25 + * 1’b1. In which case the other bits [23:16] are set to don't care. 26 + * 27 + * Limitations: 28 + * - When PWM is disabled, the output is driven to inactive. 29 + * - It does not allow the current PWM period to complete and 30 + * stops abruptly. 31 + * 32 + * - If the register is reconfigured while PWM is running, 33 + * it does not complete the currently running period. 34 + * 35 + * - If the user input duty is beyond acceptible limits, 36 + * -EINVAL is returned. 9 37 */ 10 38 11 39 #include <linux/clk.h> ··· 69 41 struct reset_control*rst; 70 42 71 43 unsigned long clk_rate; 44 + unsigned long min_period_ns; 72 45 73 46 void __iomem *regs; 74 47 ··· 97 68 { 98 69 struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip); 99 70 unsigned long long c = duty_ns, hz; 100 - unsigned long rate; 71 + unsigned long rate, required_clk_rate; 101 72 u32 val = 0; 102 73 int err; 103 74 ··· 112 83 val = (u32)c << PWM_DUTY_SHIFT; 113 84 114 85 /* 86 + * min period = max clock limit >> PWM_DUTY_WIDTH 87 + */ 88 + if (period_ns < pc->min_period_ns) 89 + return -EINVAL; 90 + 91 + /* 115 92 * Compute the prescaler value for which (1 << PWM_DUTY_WIDTH) 116 93 * cycles at the PWM clock rate will take period_ns nanoseconds. 94 + * 95 + * num_channels: If single instance of PWM controller has multiple 96 + * channels (e.g. Tegra210 or older) then it is not possible to 97 + * configure separate clock rates to each of the channels, in such 98 + * case the value stored during probe will be referred. 99 + * 100 + * If every PWM controller instance has one channel respectively, i.e. 101 + * nums_channels == 1 then only the clock rate can be modified 102 + * dynamically (e.g. Tegra186 or Tegra194). 117 103 */ 104 + if (pc->soc->num_channels == 1) { 105 + /* 106 + * Rate is multiplied with 2^PWM_DUTY_WIDTH so that it matches 107 + * with the maximum possible rate that the controller can 108 + * provide. Any further lower value can be derived by setting 109 + * PFM bits[0:12]. 110 + * 111 + * required_clk_rate is a reference rate for source clock and 112 + * it is derived based on user requested period. By setting the 113 + * source clock rate as required_clk_rate, PWM controller will 114 + * be able to configure the requested period. 115 + */ 116 + required_clk_rate = 117 + (NSEC_PER_SEC / period_ns) << PWM_DUTY_WIDTH; 118 + 119 + err = clk_set_rate(pc->clk, required_clk_rate); 120 + if (err < 0) 121 + return -EINVAL; 122 + 123 + /* Store the new rate for further references */ 124 + pc->clk_rate = clk_get_rate(pc->clk); 125 + } 126 + 118 127 rate = pc->clk_rate >> PWM_DUTY_WIDTH; 119 128 120 129 /* Consider precision in PWM_SCALE_WIDTH rate calculation */ ··· 161 94 162 95 /* 163 96 * Since the actual PWM divider is the register's frequency divider 164 - * field minus 1, we need to decrement to get the correct value to 97 + * field plus 1, we need to decrement to get the correct value to 165 98 * write to the register. 166 99 */ 167 100 if (rate > 0) ··· 272 205 */ 273 206 pwm->clk_rate = clk_get_rate(pwm->clk); 274 207 208 + /* Set minimum limit of PWM period for the IP */ 209 + pwm->min_period_ns = 210 + (NSEC_PER_SEC / (pwm->soc->max_frequency >> PWM_DUTY_WIDTH)) + 1; 211 + 275 212 pwm->rst = devm_reset_control_get_exclusive(&pdev->dev, "pwm"); 276 213 if (IS_ERR(pwm->rst)) { 277 214 ret = PTR_ERR(pwm->rst); ··· 383 312 module_platform_driver(tegra_pwm_driver); 384 313 385 314 MODULE_LICENSE("GPL"); 386 - MODULE_AUTHOR("NVIDIA Corporation"); 315 + MODULE_AUTHOR("Sandipan Patra <spatra@nvidia.com>"); 316 + MODULE_DESCRIPTION("Tegra PWM controller driver"); 387 317 MODULE_ALIAS("platform:tegra-pwm");