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

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

Pull pwm updates from Thierry Reding:
"This release cycle's changes include mostly updates and cleanups to
existing drivers along with a few cleanups to the core, documentation
and device tree bindings"

* tag 'pwm/for-4.13-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry.reding/linux-pwm:
pwm: cros-ec: Fix transposed param settings
pwm: meson: Improve PWM calculation precision
dt-bindings: pwm: meson: Add compatible for gxbb ao PWMs
pwm: meson: Add compatible for the gxbb ao PWMs
pwm: sun4i: Drop legacy callbacks
pwm: sun4i: Switch to atomic PWM
pwm: sun4i: Improve hardware read out
pwm: hibvt: Constify hibvt_pwm_ops
pwm: Silently error out on EPROBE_DEFER
pwm: Standardize document format
pwm: bfin: Remove unneeded error message
dt-bindings: pwm: Update STM32 timers clock names
dt-bindings: pwm: Add R-Car M3-W device tree bindings
pwm: tegra: Set maximum pwm clock source per SoC tapeout

+256 -164
+3 -1
Documentation/devicetree/bindings/pwm/pwm-meson.txt
··· 2 2 ============================ 3 3 4 4 Required properties: 5 - - compatible: Shall contain "amlogic,meson8b-pwm" or "amlogic,meson-gxbb-pwm". 5 + - compatible: Shall contain "amlogic,meson8b-pwm" 6 + or "amlogic,meson-gxbb-pwm" 7 + or "amlogic,meson-gxbb-ao-pwm" 6 8 - #pwm-cells: Should be 3. See pwm.txt in this directory for a description of 7 9 the cells format. 8 10
+1 -1
Documentation/devicetree/bindings/pwm/pwm-stm32.txt
··· 24 24 compatible = "st,stm32-timers"; 25 25 reg = <0x40010000 0x400>; 26 26 clocks = <&rcc 0 160>; 27 - clock-names = "clk_int"; 27 + clock-names = "int"; 28 28 29 29 pwm { 30 30 compatible = "st,stm32-pwm";
+1
Documentation/devicetree/bindings/pwm/renesas,pwm-rcar.txt
··· 8 8 - "renesas,pwm-r8a7791": for R-Car M2-W 9 9 - "renesas,pwm-r8a7794": for R-Car E2 10 10 - "renesas,pwm-r8a7795": for R-Car H3 11 + - "renesas,pwm-r8a7796": for R-Car M3-W 11 12 - reg: base address and length of the registers block for the PWM. 12 13 - #pwm-cells: should be 2. See pwm.txt in this directory for a description of 13 14 the cells format.
+28 -18
Documentation/pwm.txt
··· 1 + ====================================== 1 2 Pulse Width Modulation (PWM) interface 3 + ====================================== 2 4 3 5 This provides an overview about the Linux PWM interface 4 6 ··· 18 16 19 17 Instead of referring to a PWM device via its unique ID, board setup code 20 18 should instead register a static mapping that can be used to match PWM 21 - consumers to providers, as given in the following example: 19 + consumers to providers, as given in the following example:: 22 20 23 21 static struct pwm_lookup board_pwm_lookup[] = { 24 22 PWM_LOOKUP("tegra-pwm", 0, "pwm-backlight", NULL, ··· 42 40 device or a consumer name. pwm_put() is used to free the PWM device. Managed 43 41 variants of these functions, devm_pwm_get() and devm_pwm_put(), also exist. 44 42 45 - After being requested, a PWM has to be configured using: 43 + After being requested, a PWM has to be configured using:: 46 44 47 - int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state); 45 + int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state); 48 46 49 47 This API controls both the PWM period/duty_cycle config and the 50 48 enable/disable state. ··· 74 72 pwmchipN, where N is the base of the PWM chip. Inside the directory you 75 73 will find: 76 74 77 - npwm - The number of PWM channels this chip supports (read-only). 75 + npwm 76 + The number of PWM channels this chip supports (read-only). 78 77 79 - export - Exports a PWM channel for use with sysfs (write-only). 78 + export 79 + Exports a PWM channel for use with sysfs (write-only). 80 80 81 - unexport - Unexports a PWM channel from sysfs (write-only). 81 + unexport 82 + Unexports a PWM channel from sysfs (write-only). 82 83 83 84 The PWM channels are numbered using a per-chip index from 0 to npwm-1. 84 85 ··· 89 84 pwmchipN directory it is associated with, where X is the number of the 90 85 channel that was exported. The following properties will then be available: 91 86 92 - period - The total period of the PWM signal (read/write). 93 - Value is in nanoseconds and is the sum of the active and inactive 94 - time of the PWM. 87 + period 88 + The total period of the PWM signal (read/write). 89 + Value is in nanoseconds and is the sum of the active and inactive 90 + time of the PWM. 95 91 96 - duty_cycle - The active time of the PWM signal (read/write). 97 - Value is in nanoseconds and must be less than the period. 92 + duty_cycle 93 + The active time of the PWM signal (read/write). 94 + Value is in nanoseconds and must be less than the period. 98 95 99 - polarity - Changes the polarity of the PWM signal (read/write). 100 - Writes to this property only work if the PWM chip supports changing 101 - the polarity. The polarity can only be changed if the PWM is not 102 - enabled. Value is the string "normal" or "inversed". 96 + polarity 97 + Changes the polarity of the PWM signal (read/write). 98 + Writes to this property only work if the PWM chip supports changing 99 + the polarity. The polarity can only be changed if the PWM is not 100 + enabled. Value is the string "normal" or "inversed". 103 101 104 - enable - Enable/disable the PWM signal (read/write). 105 - 0 - disabled 106 - 1 - enabled 102 + enable 103 + Enable/disable the PWM signal (read/write). 104 + 105 + - 0 - disabled 106 + - 1 - enabled 107 107 108 108 Implementing a PWM driver 109 109 -------------------------
+3 -1
drivers/pwm/core.c
··· 678 678 679 679 pc = of_node_to_pwmchip(args.np); 680 680 if (IS_ERR(pc)) { 681 - pr_err("%s(): PWM chip not found\n", __func__); 681 + if (PTR_ERR(pc) != -EPROBE_DEFER) 682 + pr_err("%s(): PWM chip not found\n", __func__); 683 + 682 684 pwm = ERR_CAST(pc); 683 685 goto put; 684 686 }
+1 -3
drivers/pwm/pwm-bfin.c
··· 118 118 int ret; 119 119 120 120 pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL); 121 - if (!pwm) { 122 - dev_err(&pdev->dev, "failed to allocate memory\n"); 121 + if (!pwm) 123 122 return -ENOMEM; 124 - } 125 123 126 124 platform_set_drvdata(pdev, pwm); 127 125
+2 -2
drivers/pwm/pwm-cros-ec.c
··· 75 75 76 76 msg->version = 0; 77 77 msg->command = EC_CMD_PWM_GET_DUTY; 78 - msg->insize = sizeof(*params); 79 - msg->outsize = sizeof(*resp); 78 + msg->insize = sizeof(*resp); 79 + msg->outsize = sizeof(*params); 80 80 81 81 params->pwm_type = EC_PWM_TYPE_GENERIC; 82 82 params->index = index;
+1 -1
drivers/pwm/pwm-hibvt.c
··· 165 165 return 0; 166 166 } 167 167 168 - static struct pwm_ops hibvt_pwm_ops = { 168 + static const struct pwm_ops hibvt_pwm_ops = { 169 169 .get_state = hibvt_pwm_get_state, 170 170 .apply = hibvt_pwm_apply, 171 171
+39 -9
drivers/pwm/pwm-meson.c
··· 103 103 104 104 struct meson_pwm_data { 105 105 const char * const *parent_names; 106 + unsigned int num_parents; 106 107 }; 107 108 108 109 struct meson_pwm { ··· 163 162 unsigned int duty, unsigned int period) 164 163 { 165 164 unsigned int pre_div, cnt, duty_cnt; 166 - unsigned long fin_freq = -1, fin_ns; 165 + unsigned long fin_freq = -1; 166 + u64 fin_ps; 167 167 168 168 if (~(meson->inverter_mask >> id) & 0x1) 169 169 duty = period - duty; ··· 180 178 } 181 179 182 180 dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq); 183 - fin_ns = NSEC_PER_SEC / fin_freq; 181 + fin_ps = (u64)NSEC_PER_SEC * 1000; 182 + do_div(fin_ps, fin_freq); 184 183 185 184 /* Calc pre_div with the period */ 186 185 for (pre_div = 0; pre_div < MISC_CLK_DIV_MASK; pre_div++) { 187 - cnt = DIV_ROUND_CLOSEST(period, fin_ns * (pre_div + 1)); 188 - dev_dbg(meson->chip.dev, "fin_ns=%lu pre_div=%u cnt=%u\n", 189 - fin_ns, pre_div, cnt); 186 + cnt = DIV_ROUND_CLOSEST_ULL((u64)period * 1000, 187 + fin_ps * (pre_div + 1)); 188 + dev_dbg(meson->chip.dev, "fin_ps=%llu pre_div=%u cnt=%u\n", 189 + fin_ps, pre_div, cnt); 190 190 if (cnt <= 0xffff) 191 191 break; 192 192 } ··· 211 207 channel->lo = cnt; 212 208 } else { 213 209 /* Then check is we can have the duty with the same pre_div */ 214 - duty_cnt = DIV_ROUND_CLOSEST(duty, fin_ns * (pre_div + 1)); 210 + duty_cnt = DIV_ROUND_CLOSEST_ULL((u64)duty * 1000, 211 + fin_ps * (pre_div + 1)); 215 212 if (duty_cnt > 0xffff) { 216 213 dev_err(meson->chip.dev, "unable to get duty cycle\n"); 217 214 return -EINVAL; ··· 386 381 387 382 static const struct meson_pwm_data pwm_meson8b_data = { 388 383 .parent_names = pwm_meson8b_parent_names, 384 + .num_parents = ARRAY_SIZE(pwm_meson8b_parent_names), 389 385 }; 390 386 391 387 static const char * const pwm_gxbb_parent_names[] = { ··· 395 389 396 390 static const struct meson_pwm_data pwm_gxbb_data = { 397 391 .parent_names = pwm_gxbb_parent_names, 392 + .num_parents = ARRAY_SIZE(pwm_gxbb_parent_names), 393 + }; 394 + 395 + /* 396 + * Only the 2 first inputs of the GXBB AO PWMs are valid 397 + * The last 2 are grounded 398 + */ 399 + static const char * const pwm_gxbb_ao_parent_names[] = { 400 + "xtal", "clk81" 401 + }; 402 + 403 + static const struct meson_pwm_data pwm_gxbb_ao_data = { 404 + .parent_names = pwm_gxbb_ao_parent_names, 405 + .num_parents = ARRAY_SIZE(pwm_gxbb_ao_parent_names), 398 406 }; 399 407 400 408 static const struct of_device_id meson_pwm_matches[] = { 401 - { .compatible = "amlogic,meson8b-pwm", .data = &pwm_meson8b_data }, 402 - { .compatible = "amlogic,meson-gxbb-pwm", .data = &pwm_gxbb_data }, 409 + { 410 + .compatible = "amlogic,meson8b-pwm", 411 + .data = &pwm_meson8b_data 412 + }, 413 + { 414 + .compatible = "amlogic,meson-gxbb-pwm", 415 + .data = &pwm_gxbb_data 416 + }, 417 + { 418 + .compatible = "amlogic,meson-gxbb-ao-pwm", 419 + .data = &pwm_gxbb_ao_data 420 + }, 403 421 {}, 404 422 }; 405 423 MODULE_DEVICE_TABLE(of, meson_pwm_matches); ··· 447 417 init.ops = &clk_mux_ops; 448 418 init.flags = CLK_IS_BASIC; 449 419 init.parent_names = meson->data->parent_names; 450 - init.num_parents = 1 << MISC_CLK_SEL_WIDTH; 420 + init.num_parents = meson->data->num_parents; 451 421 452 422 channel->mux.reg = meson->base + REG_MISC_AB; 453 423 channel->mux.shift = mux_reg_shifts[i];
+160 -127
drivers/pwm/pwm-sun4i.c
··· 8 8 9 9 #include <linux/bitops.h> 10 10 #include <linux/clk.h> 11 + #include <linux/delay.h> 11 12 #include <linux/err.h> 12 13 #include <linux/io.h> 14 + #include <linux/jiffies.h> 13 15 #include <linux/module.h> 14 16 #include <linux/of.h> 15 17 #include <linux/of_device.h> ··· 46 44 47 45 #define PWM_DTY_MASK GENMASK(15, 0) 48 46 47 + #define PWM_REG_PRD(reg) ((((reg) >> 16) & PWM_PRD_MASK) + 1) 48 + #define PWM_REG_DTY(reg) ((reg) & PWM_DTY_MASK) 49 + #define PWM_REG_PRESCAL(reg, chan) (((reg) >> ((chan) * PWMCH_OFFSET)) & PWM_PRESCAL_MASK) 50 + 49 51 #define BIT_CH(bit, chan) ((bit) << ((chan) * PWMCH_OFFSET)) 50 52 51 53 static const u32 prescaler_table[] = { ··· 83 77 void __iomem *base; 84 78 spinlock_t ctrl_lock; 85 79 const struct sun4i_pwm_data *data; 80 + unsigned long next_period[2]; 81 + bool needs_delay[2]; 86 82 }; 87 83 88 84 static inline struct sun4i_pwm_chip *to_sun4i_pwm_chip(struct pwm_chip *chip) ··· 104 96 writel(val, chip->base + offset); 105 97 } 106 98 107 - static int sun4i_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 108 - int duty_ns, int period_ns) 99 + static void sun4i_pwm_get_state(struct pwm_chip *chip, 100 + struct pwm_device *pwm, 101 + struct pwm_state *state) 109 102 { 110 103 struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip); 111 - u32 prd, dty, val, clk_gate; 104 + u64 clk_rate, tmp; 105 + u32 val; 106 + unsigned int prescaler; 107 + 108 + clk_rate = clk_get_rate(sun4i_pwm->clk); 109 + 110 + val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG); 111 + 112 + if ((val == PWM_PRESCAL_MASK) && sun4i_pwm->data->has_prescaler_bypass) 113 + prescaler = 1; 114 + else 115 + prescaler = prescaler_table[PWM_REG_PRESCAL(val, pwm->hwpwm)]; 116 + 117 + if (prescaler == 0) 118 + return; 119 + 120 + if (val & BIT_CH(PWM_ACT_STATE, pwm->hwpwm)) 121 + state->polarity = PWM_POLARITY_NORMAL; 122 + else 123 + state->polarity = PWM_POLARITY_INVERSED; 124 + 125 + if (val & BIT_CH(PWM_CLK_GATING | PWM_EN, pwm->hwpwm)) 126 + state->enabled = true; 127 + else 128 + state->enabled = false; 129 + 130 + val = sun4i_pwm_readl(sun4i_pwm, PWM_CH_PRD(pwm->hwpwm)); 131 + 132 + tmp = prescaler * NSEC_PER_SEC * PWM_REG_DTY(val); 133 + state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate); 134 + 135 + tmp = prescaler * NSEC_PER_SEC * PWM_REG_PRD(val); 136 + state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate); 137 + } 138 + 139 + static int sun4i_pwm_calculate(struct sun4i_pwm_chip *sun4i_pwm, 140 + struct pwm_state *state, 141 + u32 *dty, u32 *prd, unsigned int *prsclr) 142 + { 112 143 u64 clk_rate, div = 0; 113 - unsigned int prescaler = 0; 114 - int err; 144 + unsigned int pval, prescaler = 0; 115 145 116 146 clk_rate = clk_get_rate(sun4i_pwm->clk); 117 147 118 148 if (sun4i_pwm->data->has_prescaler_bypass) { 119 149 /* First, test without any prescaler when available */ 120 150 prescaler = PWM_PRESCAL_MASK; 151 + pval = 1; 121 152 /* 122 153 * When not using any prescaler, the clock period in nanoseconds 123 154 * is not an integer so round it half up instead of 124 155 * truncating to get less surprising values. 125 156 */ 126 - div = clk_rate * period_ns + NSEC_PER_SEC / 2; 157 + div = clk_rate * state->period + NSEC_PER_SEC / 2; 127 158 do_div(div, NSEC_PER_SEC); 128 159 if (div - 1 > PWM_PRD_MASK) 129 160 prescaler = 0; ··· 173 126 for (prescaler = 0; prescaler < PWM_PRESCAL_MASK; prescaler++) { 174 127 if (!prescaler_table[prescaler]) 175 128 continue; 129 + pval = prescaler_table[prescaler]; 176 130 div = clk_rate; 177 - do_div(div, prescaler_table[prescaler]); 178 - div = div * period_ns; 131 + do_div(div, pval); 132 + div = div * state->period; 179 133 do_div(div, NSEC_PER_SEC); 180 134 if (div - 1 <= PWM_PRD_MASK) 181 135 break; 182 136 } 183 137 184 - if (div - 1 > PWM_PRD_MASK) { 185 - dev_err(chip->dev, "period exceeds the maximum value\n"); 138 + if (div - 1 > PWM_PRD_MASK) 186 139 return -EINVAL; 140 + } 141 + 142 + *prd = div; 143 + div *= state->duty_cycle; 144 + do_div(div, state->period); 145 + *dty = div; 146 + *prsclr = prescaler; 147 + 148 + div = (u64)pval * NSEC_PER_SEC * *prd; 149 + state->period = DIV_ROUND_CLOSEST_ULL(div, clk_rate); 150 + 151 + div = (u64)pval * NSEC_PER_SEC * *dty; 152 + state->duty_cycle = DIV_ROUND_CLOSEST_ULL(div, clk_rate); 153 + 154 + return 0; 155 + } 156 + 157 + static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 158 + struct pwm_state *state) 159 + { 160 + struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip); 161 + struct pwm_state cstate; 162 + u32 ctrl; 163 + int ret; 164 + unsigned int delay_us; 165 + unsigned long now; 166 + 167 + pwm_get_state(pwm, &cstate); 168 + 169 + if (!cstate.enabled) { 170 + ret = clk_prepare_enable(sun4i_pwm->clk); 171 + if (ret) { 172 + dev_err(chip->dev, "failed to enable PWM clock\n"); 173 + return ret; 187 174 } 188 175 } 189 176 190 - prd = div; 191 - div *= duty_ns; 192 - do_div(div, period_ns); 193 - dty = div; 194 - 195 - err = clk_prepare_enable(sun4i_pwm->clk); 196 - if (err) { 197 - dev_err(chip->dev, "failed to enable PWM clock\n"); 198 - return err; 199 - } 200 - 201 177 spin_lock(&sun4i_pwm->ctrl_lock); 202 - val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG); 178 + ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG); 203 179 204 - if (sun4i_pwm->data->has_rdy && (val & PWM_RDY(pwm->hwpwm))) { 205 - spin_unlock(&sun4i_pwm->ctrl_lock); 206 - clk_disable_unprepare(sun4i_pwm->clk); 207 - return -EBUSY; 180 + if ((cstate.period != state->period) || 181 + (cstate.duty_cycle != state->duty_cycle)) { 182 + u32 period, duty, val; 183 + unsigned int prescaler; 184 + 185 + ret = sun4i_pwm_calculate(sun4i_pwm, state, 186 + &duty, &period, &prescaler); 187 + if (ret) { 188 + dev_err(chip->dev, "period exceeds the maximum value\n"); 189 + spin_unlock(&sun4i_pwm->ctrl_lock); 190 + if (!cstate.enabled) 191 + clk_disable_unprepare(sun4i_pwm->clk); 192 + return ret; 193 + } 194 + 195 + if (PWM_REG_PRESCAL(ctrl, pwm->hwpwm) != prescaler) { 196 + /* Prescaler changed, the clock has to be gated */ 197 + ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm); 198 + sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG); 199 + 200 + ctrl &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm); 201 + ctrl |= BIT_CH(prescaler, pwm->hwpwm); 202 + } 203 + 204 + val = (duty & PWM_DTY_MASK) | PWM_PRD(period); 205 + sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm)); 206 + sun4i_pwm->next_period[pwm->hwpwm] = jiffies + 207 + usecs_to_jiffies(cstate.period / 1000 + 1); 208 + sun4i_pwm->needs_delay[pwm->hwpwm] = true; 208 209 } 209 210 210 - clk_gate = val & BIT_CH(PWM_CLK_GATING, pwm->hwpwm); 211 - if (clk_gate) { 212 - val &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm); 213 - sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG); 214 - } 215 - 216 - val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG); 217 - val &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm); 218 - val |= BIT_CH(prescaler, pwm->hwpwm); 219 - sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG); 220 - 221 - val = (dty & PWM_DTY_MASK) | PWM_PRD(prd); 222 - sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm)); 223 - 224 - if (clk_gate) { 225 - val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG); 226 - val |= clk_gate; 227 - sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG); 228 - } 229 - 230 - spin_unlock(&sun4i_pwm->ctrl_lock); 231 - clk_disable_unprepare(sun4i_pwm->clk); 232 - 233 - return 0; 234 - } 235 - 236 - static int sun4i_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm, 237 - enum pwm_polarity polarity) 238 - { 239 - struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip); 240 - u32 val; 241 - int ret; 242 - 243 - ret = clk_prepare_enable(sun4i_pwm->clk); 244 - if (ret) { 245 - dev_err(chip->dev, "failed to enable PWM clock\n"); 246 - return ret; 247 - } 248 - 249 - spin_lock(&sun4i_pwm->ctrl_lock); 250 - val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG); 251 - 252 - if (polarity != PWM_POLARITY_NORMAL) 253 - val &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm); 211 + if (state->polarity != PWM_POLARITY_NORMAL) 212 + ctrl &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm); 254 213 else 255 - val |= BIT_CH(PWM_ACT_STATE, pwm->hwpwm); 214 + ctrl |= BIT_CH(PWM_ACT_STATE, pwm->hwpwm); 256 215 257 - sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG); 258 - 259 - spin_unlock(&sun4i_pwm->ctrl_lock); 260 - clk_disable_unprepare(sun4i_pwm->clk); 261 - 262 - return 0; 263 - } 264 - 265 - static int sun4i_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) 266 - { 267 - struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip); 268 - u32 val; 269 - int ret; 270 - 271 - ret = clk_prepare_enable(sun4i_pwm->clk); 272 - if (ret) { 273 - dev_err(chip->dev, "failed to enable PWM clock\n"); 274 - return ret; 216 + ctrl |= BIT_CH(PWM_CLK_GATING, pwm->hwpwm); 217 + if (state->enabled) { 218 + ctrl |= BIT_CH(PWM_EN, pwm->hwpwm); 219 + } else if (!sun4i_pwm->needs_delay[pwm->hwpwm]) { 220 + ctrl &= ~BIT_CH(PWM_EN, pwm->hwpwm); 221 + ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm); 275 222 } 276 223 277 - spin_lock(&sun4i_pwm->ctrl_lock); 278 - val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG); 279 - val |= BIT_CH(PWM_EN, pwm->hwpwm); 280 - val |= BIT_CH(PWM_CLK_GATING, pwm->hwpwm); 281 - sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG); 224 + sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG); 225 + 282 226 spin_unlock(&sun4i_pwm->ctrl_lock); 283 227 284 - return 0; 285 - } 228 + if (state->enabled) 229 + return 0; 286 230 287 - static void sun4i_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) 288 - { 289 - struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip); 290 - u32 val; 231 + if (!sun4i_pwm->needs_delay[pwm->hwpwm]) { 232 + clk_disable_unprepare(sun4i_pwm->clk); 233 + return 0; 234 + } 235 + 236 + /* We need a full period to elapse before disabling the channel. */ 237 + now = jiffies; 238 + if (sun4i_pwm->needs_delay[pwm->hwpwm] && 239 + time_before(now, sun4i_pwm->next_period[pwm->hwpwm])) { 240 + delay_us = jiffies_to_usecs(sun4i_pwm->next_period[pwm->hwpwm] - 241 + now); 242 + if ((delay_us / 500) > MAX_UDELAY_MS) 243 + msleep(delay_us / 1000 + 1); 244 + else 245 + usleep_range(delay_us, delay_us * 2); 246 + } 247 + sun4i_pwm->needs_delay[pwm->hwpwm] = false; 291 248 292 249 spin_lock(&sun4i_pwm->ctrl_lock); 293 - val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG); 294 - val &= ~BIT_CH(PWM_EN, pwm->hwpwm); 295 - val &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm); 296 - sun4i_pwm_writel(sun4i_pwm, val, PWM_CTRL_REG); 250 + ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG); 251 + ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm); 252 + ctrl &= ~BIT_CH(PWM_EN, pwm->hwpwm); 253 + sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG); 297 254 spin_unlock(&sun4i_pwm->ctrl_lock); 298 255 299 256 clk_disable_unprepare(sun4i_pwm->clk); 257 + 258 + return 0; 300 259 } 301 260 302 261 static const struct pwm_ops sun4i_pwm_ops = { 303 - .config = sun4i_pwm_config, 304 - .set_polarity = sun4i_pwm_set_polarity, 305 - .enable = sun4i_pwm_enable, 306 - .disable = sun4i_pwm_disable, 262 + .apply = sun4i_pwm_apply, 263 + .get_state = sun4i_pwm_get_state, 307 264 .owner = THIS_MODULE, 308 265 }; 309 266 ··· 367 316 { 368 317 struct sun4i_pwm_chip *pwm; 369 318 struct resource *res; 370 - u32 val; 371 - int i, ret; 319 + int ret; 372 320 const struct of_device_id *match; 373 321 374 322 match = of_match_device(sun4i_pwm_dt_ids, &pdev->dev); ··· 403 353 404 354 platform_set_drvdata(pdev, pwm); 405 355 406 - ret = clk_prepare_enable(pwm->clk); 407 - if (ret) { 408 - dev_err(&pdev->dev, "failed to enable PWM clock\n"); 409 - goto clk_error; 410 - } 411 - 412 - val = sun4i_pwm_readl(pwm, PWM_CTRL_REG); 413 - for (i = 0; i < pwm->chip.npwm; i++) 414 - if (!(val & BIT_CH(PWM_ACT_STATE, i))) 415 - pwm_set_polarity(&pwm->chip.pwms[i], 416 - PWM_POLARITY_INVERSED); 417 - clk_disable_unprepare(pwm->clk); 418 - 419 356 return 0; 420 - 421 - clk_error: 422 - pwmchip_remove(&pwm->chip); 423 - return ret; 424 357 } 425 358 426 359 static int sun4i_pwm_remove(struct platform_device *pdev)
+17 -1
drivers/pwm/pwm-tegra.c
··· 41 41 42 42 struct tegra_pwm_soc { 43 43 unsigned int num_channels; 44 + 45 + /* Maximum IP frequency for given SoCs */ 46 + unsigned long max_frequency; 44 47 }; 45 48 46 49 struct tegra_pwm_chip { ··· 204 201 if (IS_ERR(pwm->clk)) 205 202 return PTR_ERR(pwm->clk); 206 203 207 - /* Read PWM clock rate from source */ 204 + /* Set maximum frequency of the IP */ 205 + ret = clk_set_rate(pwm->clk, pwm->soc->max_frequency); 206 + if (ret < 0) { 207 + dev_err(&pdev->dev, "Failed to set max frequency: %d\n", ret); 208 + return ret; 209 + } 210 + 211 + /* 212 + * The requested and configured frequency may differ due to 213 + * clock register resolutions. Get the configured frequency 214 + * so that PWM period can be calculated more accurately. 215 + */ 208 216 pwm->clk_rate = clk_get_rate(pwm->clk); 209 217 210 218 pwm->rst = devm_reset_control_get(&pdev->dev, "pwm"); ··· 287 273 288 274 static const struct tegra_pwm_soc tegra20_pwm_soc = { 289 275 .num_channels = 4, 276 + .max_frequency = 48000000UL, 290 277 }; 291 278 292 279 static const struct tegra_pwm_soc tegra186_pwm_soc = { 293 280 .num_channels = 1, 281 + .max_frequency = 102000000UL, 294 282 }; 295 283 296 284 static const struct of_device_id tegra_pwm_of_match[] = {