Implement the ti,twl4030-pwm-frame-64 device tree option in the TWL4030 PWM driver. When enabled, program bit 7 of PWMx_ON to select a 64-clock-cycle frame and clamp the OFF counter to 63.
The shorter frame doubles the output frequency at the expense of duty-cycle resolution. With ON = 1 and OFF = 63, a full-duty request is approximated rather than producing a constant HIGH level; the remaining LOW interval is short enough for the intended LED driver startup mode detection. Keep the existing 128-cycle behaviour when the property is absent. As before, the driver does not implement arbitrary consumer-requested PWM periods. Signed-off-by: H. Nikolaus Schaller <[email protected]> --- Notes: Changes against V1: - Add optional selection of the TWL4030 64-cycle PWM frame through ti,twl4030-pwm-frame-64; retain the default 128-cycle mode. - Set the frame-selection bit in PWMx_ON and limit the OFF counter to 63 in 64-cycle mode, including for full-duty requests. - Retain the duty-cycle calculation and register encoding of the hardware-tested working series rather than the later review proposal using ON = OFF = 1 for full duty. - Document that ON = 1, OFF = 63 does not produce constant HIGH; the resulting short LOW intervals are relevant for TPS61160/61. - Revised commit message to distinguish driver implementation from the preceding binding documentation. - PWM encoding and commit-message review assisted by ChatGPT. drivers/pwm/pwm-twl.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/drivers/pwm/pwm-twl.c b/drivers/pwm/pwm-twl.c index 99c1caf4f19c8..70567d5805f43 100644 --- a/drivers/pwm/pwm-twl.c +++ b/drivers/pwm/pwm-twl.c @@ -25,6 +25,9 @@ #define TWL4030_GPBR1_REG 0x0c #define TWL4030_PMBR1_REG 0x0d +/* Bit 7 of the PWMx_ON register selects the short (64 cycles) frame */ +#define TWL4030_PWM_ON_FRAME_64 BIT(7) + /* GPBR1 register bits */ #define TWL4030_PWMXCLK_ENABLE (1 << 0) #define TWL4030_PWMX_ENABLE (1 << 2) @@ -49,6 +52,7 @@ struct twl_pwm_chip { struct mutex mutex; u8 twl6030_toggle3; u8 twl4030_pwm_mux; + bool twl4030_frame_64; }; static inline struct twl_pwm_chip *to_twl(struct pwm_chip *chip) @@ -56,13 +60,35 @@ static inline struct twl_pwm_chip *to_twl(struct pwm_chip *chip) return pwmchip_get_drvdata(chip); } +/* + * Note: this hardware (and driver) does not honor arbitrary periods. + * Only the duty ratio is implemented; the frame length is either + * 128 clock cycles (default) or - if selected through device tree - + * 64 clock cycles. Consumers requiring an accurate period must verify + * the actually provided period. + */ static int twl_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, u64 duty_ns, u64 period_ns) { + struct twl_pwm_chip *twl = to_twl(chip); int duty_cycle = DIV64_U64_ROUND_UP(duty_ns * TWL_PWM_MAX, period_ns) + 1; + bool frame_64 = twl_class_is_4030() && twl->twl4030_frame_64; u8 pwm_config[2] = { 1, 0 }; int base, ret; + if (frame_64) + pwm_config[0] |= TWL4030_PWM_ON_FRAME_64; + + /* + * In the 64-cycle frame the OFF counter wraps at 64. A requested + * duty_cycle of 64 (full duty) would therefore wrap to a constant + * high output. Saturate to the largest value that still produces + * a low pulse (OFF = 63, approximately 62/64 high) instead. + * This also applies to full-duty requests from PWM consumers. + */ + if (frame_64 && duty_cycle > TWL_PWM_MAX) + duty_cycle = TWL_PWM_MAX; + /* * To configure the duty period: * On-cycle is set to 1 (the minimum allowed value) @@ -353,6 +379,17 @@ static int twl_pwm_probe(struct platform_device *pdev) else chip->ops = &twl6030_pwm_ops; + /* + * Optional short PWM frame (64 instead of 128 clock cycles). + * Doubles the output frequency; useful for LED dimming + * inputs requiring short low intervals during startup + * mode detection (e.g. TI TPS61160/61). + */ + twl->twl4030_frame_64 = + twl_class_is_4030() && + device_property_read_bool(&pdev->dev, + "ti,twl4030-pwm-frame-64"); + mutex_init(&twl->mutex); return devm_pwmchip_add(&pdev->dev, chip); -- 2.56.0
