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

Merge tag 'pwm/for-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux

Pull pwm updates from Uwe Kleine-König:
"The core highlights for this cycle are:

- The pca9586 driver was converted to the waveform API

- Waveform drivers automatically provide a gpio chip to make PWMs
usable as GPIOs (The pca9586 driver did that in a driver specific
implementation before)

Otherwise it's the usual mix of fixes and device tree and driver
changes to support new hardware variants"

* tag 'pwm/for-6.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux: (30 commits)
pwm: cros-ec: Avoid -Wflex-array-member-not-at-end warnings
dt-bindings: pwm: samsung: add exynos8890 compatible
dt-bindings: pwm: apple,s5l-fpwm: Add t6020-fpwm compatible
dt-bindings: pwm: nxp,lpc1850-sct-pwm: Minor whitespace cleanup in example
pwm: pca9586: Convert to waveform API
pwm: pca9685: Drop GPIO support
pwm: pca9685: Make use of register caching in regmap
pwm: pca9685: Use bulk write to atomicially update registers
pwm: pca9685: Don't disable hardware in .free()
pwm: Add the S32G support in the Freescale FTM driver
dt-bindings: pwm: fsl,vf610-ftm-pwm: Add compatible for s32g2 and s32g3
pwm: mediatek: Lock and cache clock rate
pwm: mediatek: Fix various issues in the .apply() callback
pwm: mediatek: Implement .get_state() callback
pwm: mediatek: Initialize clks when the hardware is enabled at probe time
pwm: mediatek: Rework parameters for clk helper function
pwm: mediatek: Introduce and use a few more register defines
pwm: mediatek: Simplify representation of channel offsets
pwm: tiecap: Document behaviour of hardware disable
pwm: Provide a gpio device for waveform drivers
...

+683 -537
+2 -1
Documentation/devicetree/bindings/pwm/apple,s5l-fpwm.yaml
··· 17 17 items: 18 18 - enum: 19 19 - apple,t8103-fpwm 20 - - apple,t6000-fpwm 21 20 - apple,t8112-fpwm 21 + - apple,t6000-fpwm 22 + - apple,t6020-fpwm 22 23 - const: apple,s5l-fpwm 23 24 24 25 reg:
+8 -3
Documentation/devicetree/bindings/pwm/fsl,vf610-ftm-pwm.yaml
··· 26 26 27 27 properties: 28 28 compatible: 29 - enum: 30 - - fsl,vf610-ftm-pwm 31 - - fsl,imx8qm-ftm-pwm 29 + oneOf: 30 + - enum: 31 + - fsl,vf610-ftm-pwm 32 + - fsl,imx8qm-ftm-pwm 33 + - nxp,s32g2-ftm-pwm 34 + - items: 35 + - const: nxp,s32g3-ftm-pwm 36 + - const: nxp,s32g2-ftm-pwm 32 37 33 38 reg: 34 39 maxItems: 1
+1 -1
Documentation/devicetree/bindings/pwm/nxp,lpc1850-sct-pwm.yaml
··· 48 48 pwm@40000000 { 49 49 compatible = "nxp,lpc1850-sct-pwm"; 50 50 reg = <0x40000000 0x1000>; 51 - clocks =<&ccu1 CLK_CPU_SCT>; 51 + clocks = <&ccu1 CLK_CPU_SCT>; 52 52 clock-names = "pwm"; 53 53 #pwm-cells = <3>; 54 54 };
+1
Documentation/devicetree/bindings/pwm/pwm-samsung.yaml
··· 31 31 - enum: 32 32 - samsung,exynos5433-pwm 33 33 - samsung,exynos7-pwm 34 + - samsung,exynos8890-pwm 34 35 - samsung,exynosautov9-pwm 35 36 - samsung,exynosautov920-pwm 36 37 - tesla,fsd-pwm
+5 -2
Documentation/devicetree/bindings/timer/renesas,rz-mtu3.yaml
··· 221 221 maxItems: 1 222 222 223 223 "#pwm-cells": 224 - const: 2 224 + oneOf: 225 + - const: 2 226 + deprecated: true 227 + - const: 3 225 228 226 229 required: 227 230 - compatible ··· 302 299 clocks = <&cpg CPG_MOD R9A07G044_MTU_X_MCK_MTU3>; 303 300 power-domains = <&cpg>; 304 301 resets = <&cpg R9A07G044_MTU_X_PRESET_MTU3>; 305 - #pwm-cells = <2>; 302 + #pwm-cells = <3>; 306 303 };
+9
drivers/pwm/Kconfig
··· 38 38 It is expected to introduce some runtime overhead and diagnostic 39 39 output to the kernel log, so only enable while working on a driver. 40 40 41 + config PWM_PROVIDE_GPIO 42 + bool "Provide a GPIO chip for each PWM chip" 43 + depends on GPIOLIB 44 + help 45 + Most PWMs can emit both a constant active high and a constant active 46 + low signal and so they can be used as GPIO. Say Y here to let each 47 + PWM chip provide a GPIO chip and so be easily plugged into consumers 48 + that know how to handle GPIOs but not PWMs. 49 + 41 50 config PWM_AB8500 42 51 tristate "AB8500 PWM support" 43 52 depends on AB8500_CORE && ARCH_U8500
+94 -14
drivers/pwm/core.c
··· 276 276 277 277 if (IS_ENABLED(CONFIG_PWM_DEBUG) && ret_fromhw > 0) 278 278 dev_err(&chip->dev, "Unexpected return value from __pwm_round_waveform_fromhw: requested %llu/%llu [+%llu], return value %d\n", 279 - wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns, ret_tohw); 279 + wf_req.duty_length_ns, wf_req.period_length_ns, wf_req.duty_offset_ns, ret_fromhw); 280 280 281 281 if (IS_ENABLED(CONFIG_PWM_DEBUG) && 282 282 (ret_tohw == 0) != pwm_check_rounding(&wf_req, wf)) ··· 497 497 return; 498 498 499 499 /* 500 + * If a disabled PWM was requested the result is unspecified, so nothing 501 + * to check. 502 + */ 503 + if (!state->enabled) 504 + return; 505 + 506 + /* 500 507 * *state was just applied. Read out the hardware state and do some 501 508 * checks. 502 509 */ ··· 515 508 return; 516 509 517 510 /* 511 + * If the PWM was disabled that's maybe strange but there is nothing 512 + * that can be sensibly checked then. So return early. 513 + */ 514 + if (!s1.enabled) 515 + return; 516 + 517 + /* 518 518 * The lowlevel driver either ignored .polarity (which is a bug) or as 519 519 * best effort inverted .polarity and fixed .duty_cycle respectively. 520 520 * Undo this inversion and fixup for further tests. 521 521 */ 522 - if (s1.enabled && s1.polarity != state->polarity) { 522 + if (s1.polarity != state->polarity) { 523 523 s2.polarity = state->polarity; 524 524 s2.duty_cycle = s1.period - s1.duty_cycle; 525 525 s2.period = s1.period; 526 - s2.enabled = s1.enabled; 526 + s2.enabled = true; 527 527 } else { 528 528 s2 = s1; 529 529 } 530 530 531 531 if (s2.polarity != state->polarity && 532 - state->duty_cycle < state->period) 532 + s2.duty_cycle < s2.period) 533 533 dev_warn(pwmchip_parent(chip), ".apply ignored .polarity\n"); 534 534 535 - if (state->enabled && s2.enabled && 536 - last->polarity == state->polarity && 535 + if (last->polarity == state->polarity && 537 536 last->period > s2.period && 538 537 last->period <= state->period) 539 538 dev_warn(pwmchip_parent(chip), ··· 550 537 * Rounding period up is fine only if duty_cycle is 0 then, because a 551 538 * flat line doesn't have a characteristic period. 552 539 */ 553 - if (state->enabled && s2.enabled && state->period < s2.period && s2.duty_cycle) 540 + if (state->period < s2.period && s2.duty_cycle) 554 541 dev_warn(pwmchip_parent(chip), 555 542 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n", 556 543 state->period, s2.period); 557 544 558 - if (state->enabled && 559 - last->polarity == state->polarity && 545 + if (last->polarity == state->polarity && 560 546 last->period == s2.period && 561 547 last->duty_cycle > s2.duty_cycle && 562 548 last->duty_cycle <= state->duty_cycle) ··· 565 553 s2.duty_cycle, s2.period, 566 554 last->duty_cycle, last->period); 567 555 568 - if (state->enabled && s2.enabled && state->duty_cycle < s2.duty_cycle) 556 + if (state->duty_cycle < s2.duty_cycle) 569 557 dev_warn(pwmchip_parent(chip), 570 558 ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n", 571 559 state->duty_cycle, state->period, 572 560 s2.duty_cycle, s2.period); 573 - 574 - if (!state->enabled && s2.enabled && s2.duty_cycle > 0) 575 - dev_warn(pwmchip_parent(chip), 576 - "requested disabled, but yielded enabled with duty > 0\n"); 577 561 578 562 /* reapply the state that the driver reported being configured. */ 579 563 err = chip->ops->apply(chip, pwm, &s1); ··· 2391 2383 2392 2384 static dev_t pwm_devt; 2393 2385 2386 + static int pwm_gpio_request(struct gpio_chip *gc, unsigned int offset) 2387 + { 2388 + struct pwm_chip *chip = gpiochip_get_data(gc); 2389 + struct pwm_device *pwm; 2390 + 2391 + pwm = pwm_request_from_chip(chip, offset, "pwm-gpio"); 2392 + if (IS_ERR(pwm)) 2393 + return PTR_ERR(pwm); 2394 + 2395 + return 0; 2396 + } 2397 + 2398 + static void pwm_gpio_free(struct gpio_chip *gc, unsigned int offset) 2399 + { 2400 + struct pwm_chip *chip = gpiochip_get_data(gc); 2401 + 2402 + pwm_put(&chip->pwms[offset]); 2403 + } 2404 + 2405 + static int pwm_gpio_get_direction(struct gpio_chip *gc, unsigned int offset) 2406 + { 2407 + return GPIO_LINE_DIRECTION_OUT; 2408 + } 2409 + 2410 + static int pwm_gpio_set(struct gpio_chip *gc, unsigned int offset, int value) 2411 + { 2412 + struct pwm_chip *chip = gpiochip_get_data(gc); 2413 + struct pwm_device *pwm = &chip->pwms[offset]; 2414 + int ret; 2415 + struct pwm_waveform wf = { 2416 + .period_length_ns = 1, 2417 + }; 2418 + 2419 + ret = pwm_round_waveform_might_sleep(pwm, &wf); 2420 + if (ret < 0) 2421 + return ret; 2422 + 2423 + if (value) 2424 + wf.duty_length_ns = wf.period_length_ns; 2425 + else 2426 + wf.duty_length_ns = 0; 2427 + 2428 + return pwm_set_waveform_might_sleep(pwm, &wf, true); 2429 + } 2430 + 2394 2431 /** 2395 2432 * __pwmchip_add() - register a new PWM chip 2396 2433 * @chip: the PWM chip to add ··· 2502 2449 if (ret) 2503 2450 goto err_device_add; 2504 2451 2452 + if (IS_ENABLED(CONFIG_PWM_PROVIDE_GPIO) && chip->ops->write_waveform) { 2453 + struct device *parent = pwmchip_parent(chip); 2454 + 2455 + chip->gpio = (typeof(chip->gpio)){ 2456 + .label = dev_name(parent), 2457 + .parent = parent, 2458 + .request = pwm_gpio_request, 2459 + .free = pwm_gpio_free, 2460 + .get_direction = pwm_gpio_get_direction, 2461 + .set = pwm_gpio_set, 2462 + .base = -1, 2463 + .ngpio = chip->npwm, 2464 + .can_sleep = true, 2465 + }; 2466 + 2467 + ret = gpiochip_add_data(&chip->gpio, chip); 2468 + if (ret) 2469 + goto err_gpiochip_add; 2470 + } 2471 + 2505 2472 return 0; 2506 2473 2474 + err_gpiochip_add: 2475 + 2476 + cdev_device_del(&chip->cdev, &chip->dev); 2507 2477 err_device_add: 2478 + 2508 2479 scoped_guard(pwmchip, chip) 2509 2480 chip->operational = false; 2510 2481 ··· 2549 2472 */ 2550 2473 void pwmchip_remove(struct pwm_chip *chip) 2551 2474 { 2475 + if (IS_ENABLED(CONFIG_PWM_PROVIDE_GPIO) && chip->ops->write_waveform) 2476 + gpiochip_remove(&chip->gpio); 2477 + 2552 2478 pwmchip_sysfs_unexport(chip); 2553 2479 2554 2480 scoped_guard(mutex, &pwm_lock) {
+2 -2
drivers/pwm/pwm-berlin.c
··· 234 234 for (i = 0; i < chip->npwm; i++) { 235 235 struct berlin_pwm_channel *channel = &bpc->channel[i]; 236 236 237 - channel->enable = berlin_pwm_readl(bpc, i, BERLIN_PWM_ENABLE); 237 + channel->enable = berlin_pwm_readl(bpc, i, BERLIN_PWM_EN); 238 238 channel->ctrl = berlin_pwm_readl(bpc, i, BERLIN_PWM_CONTROL); 239 239 channel->duty = berlin_pwm_readl(bpc, i, BERLIN_PWM_DUTY); 240 240 channel->tcnt = berlin_pwm_readl(bpc, i, BERLIN_PWM_TCNT); ··· 262 262 berlin_pwm_writel(bpc, i, channel->ctrl, BERLIN_PWM_CONTROL); 263 263 berlin_pwm_writel(bpc, i, channel->duty, BERLIN_PWM_DUTY); 264 264 berlin_pwm_writel(bpc, i, channel->tcnt, BERLIN_PWM_TCNT); 265 - berlin_pwm_writel(bpc, i, channel->enable, BERLIN_PWM_ENABLE); 265 + berlin_pwm_writel(bpc, i, channel->enable, BERLIN_PWM_EN); 266 266 } 267 267 268 268 return 0;
+4 -6
drivers/pwm/pwm-cros-ec.c
··· 49 49 u16 duty) 50 50 { 51 51 struct cros_ec_device *ec = ec_pwm->ec; 52 - struct { 53 - struct cros_ec_command msg; 52 + TRAILING_OVERLAP(struct cros_ec_command, msg, data, 54 53 struct ec_params_pwm_set_duty params; 55 - } __packed buf; 54 + ) __packed buf; 56 55 struct ec_params_pwm_set_duty *params = &buf.params; 57 56 struct cros_ec_command *msg = &buf.msg; 58 57 int ret; ··· 82 83 83 84 static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, bool use_pwm_type, u8 index) 84 85 { 85 - struct { 86 - struct cros_ec_command msg; 86 + TRAILING_OVERLAP(struct cros_ec_command, msg, data, 87 87 union { 88 88 struct ec_params_pwm_get_duty params; 89 89 struct ec_response_pwm_get_duty resp; 90 90 }; 91 - } __packed buf; 91 + ) __packed buf; 92 92 struct ec_params_pwm_get_duty *params = &buf.params; 93 93 struct ec_response_pwm_get_duty *resp = &buf.resp; 94 94 struct cros_ec_command *msg = &buf.msg;
+33 -2
drivers/pwm/pwm-fsl-ftm.c
··· 3 3 * Freescale FlexTimer Module (FTM) PWM Driver 4 4 * 5 5 * Copyright 2012-2013 Freescale Semiconductor, Inc. 6 + * Copyright 2020-2025 NXP 6 7 */ 7 8 8 9 #include <linux/clk.h> ··· 31 30 32 31 struct fsl_ftm_soc { 33 32 bool has_enable_bits; 33 + bool has_flt_reg; 34 + unsigned int npwm; 34 35 }; 35 36 36 37 struct fsl_pwm_periodcfg { ··· 377 374 return false; 378 375 } 379 376 377 + static bool fsl_pwm_is_reg(struct device *dev, unsigned int reg) 378 + { 379 + struct pwm_chip *chip = dev_get_drvdata(dev); 380 + struct fsl_pwm_chip *fpc = to_fsl_chip(chip); 381 + 382 + if (reg >= FTM_CSC(fpc->soc->npwm) && reg < FTM_CNTIN) 383 + return false; 384 + 385 + if ((reg == FTM_FLTCTRL || reg == FTM_FLTPOL) && !fpc->soc->has_flt_reg) 386 + return false; 387 + 388 + return true; 389 + } 390 + 380 391 static const struct regmap_config fsl_pwm_regmap_config = { 381 392 .reg_bits = 32, 382 393 .reg_stride = 4, ··· 399 382 .max_register = FTM_PWMLOAD, 400 383 .volatile_reg = fsl_pwm_volatile_reg, 401 384 .cache_type = REGCACHE_FLAT, 385 + .writeable_reg = fsl_pwm_is_reg, 386 + .readable_reg = fsl_pwm_is_reg, 402 387 }; 403 388 404 389 static int fsl_pwm_probe(struct platform_device *pdev) 405 390 { 391 + const struct fsl_ftm_soc *soc = of_device_get_match_data(&pdev->dev); 406 392 struct pwm_chip *chip; 407 393 struct fsl_pwm_chip *fpc; 408 394 void __iomem *base; 409 395 int ret; 410 396 411 - chip = devm_pwmchip_alloc(&pdev->dev, 8, sizeof(*fpc)); 397 + chip = devm_pwmchip_alloc(&pdev->dev, soc->npwm, sizeof(*fpc)); 412 398 if (IS_ERR(chip)) 413 399 return PTR_ERR(chip); 414 400 fpc = to_fsl_chip(chip); 415 401 416 - fpc->soc = of_device_get_match_data(&pdev->dev); 402 + fpc->soc = soc; 417 403 418 404 base = devm_platform_ioremap_resource(pdev, 0); 419 405 if (IS_ERR(base)) ··· 532 512 533 513 static const struct fsl_ftm_soc vf610_ftm_pwm = { 534 514 .has_enable_bits = false, 515 + .has_flt_reg = true, 516 + .npwm = 8, 535 517 }; 536 518 537 519 static const struct fsl_ftm_soc imx8qm_ftm_pwm = { 538 520 .has_enable_bits = true, 521 + .has_flt_reg = true, 522 + .npwm = 8, 523 + }; 524 + 525 + static const struct fsl_ftm_soc s32g2_ftm_pwm = { 526 + .has_enable_bits = true, 527 + .has_flt_reg = false, 528 + .npwm = 6, 539 529 }; 540 530 541 531 static const struct of_device_id fsl_pwm_dt_ids[] = { 542 532 { .compatible = "fsl,vf610-ftm-pwm", .data = &vf610_ftm_pwm }, 543 533 { .compatible = "fsl,imx8qm-ftm-pwm", .data = &imx8qm_ftm_pwm }, 534 + { .compatible = "nxp,s32g2-ftm-pwm", .data = &s32g2_ftm_pwm }, 544 535 { /* sentinel */ } 545 536 }; 546 537 MODULE_DEVICE_TABLE(of, fsl_pwm_dt_ids);
+1 -1
drivers/pwm/pwm-loongson.c
··· 49 49 #define LOONGSON_PWM_CTRL_REG_DZONE BIT(10) /* Anti-dead Zone Enable Bit */ 50 50 51 51 /* default input clk frequency for the ACPI case */ 52 - #define LOONGSON_PWM_FREQ_DEFAULT 50000 /* Hz */ 52 + #define LOONGSON_PWM_FREQ_DEFAULT 50000000 /* Hz */ 53 53 54 54 struct pwm_loongson_ddata { 55 55 struct clk *clk;
+226 -86
drivers/pwm/pwm-mediatek.c
··· 7 7 * 8 8 */ 9 9 10 + #include <linux/bitfield.h> 10 11 #include <linux/err.h> 11 12 #include <linux/io.h> 12 13 #include <linux/ioport.h> ··· 22 21 23 22 /* PWM registers and bits definitions */ 24 23 #define PWMCON 0x00 24 + #define PWMCON_CLKDIV GENMASK(2, 0) 25 25 #define PWMHDUR 0x04 26 26 #define PWMLDUR 0x08 27 27 #define PWMGDUR 0x0c 28 28 #define PWMWAVENUM 0x28 29 29 #define PWMDWIDTH 0x2c 30 + #define PWMDWIDTH_PERIOD GENMASK(12, 0) 30 31 #define PWM45DWIDTH_FIXUP 0x30 31 32 #define PWMTHRES 0x30 33 + #define PWMTHRES_DUTY GENMASK(12, 0) 32 34 #define PWM45THRES_FIXUP 0x34 33 35 #define PWM_CK_26M_SEL_V3 0x74 34 36 #define PWM_CK_26M_SEL 0x210 35 - 36 - #define PWM_CLK_DIV_MAX 7 37 37 38 38 struct pwm_mediatek_of_data { 39 39 unsigned int num_pwms; 40 40 bool pwm45_fixup; 41 41 u16 pwm_ck_26m_sel_reg; 42 - const unsigned int *reg_offset; 42 + unsigned int chanreg_base; 43 + unsigned int chanreg_width; 43 44 }; 44 45 45 46 /** ··· 49 46 * @regs: base address of PWM chip 50 47 * @clk_top: the top clock generator 51 48 * @clk_main: the clock used by PWM core 52 - * @clk_pwms: the clock used by each PWM channel 53 49 * @soc: pointer to chip's platform data 50 + * @clk_pwms: the clock and clkrate used by each PWM channel 54 51 */ 55 52 struct pwm_mediatek_chip { 56 53 void __iomem *regs; 57 54 struct clk *clk_top; 58 55 struct clk *clk_main; 59 - struct clk **clk_pwms; 60 56 const struct pwm_mediatek_of_data *soc; 61 - }; 62 - 63 - static const unsigned int mtk_pwm_reg_offset_v1[] = { 64 - 0x0010, 0x0050, 0x0090, 0x00d0, 0x0110, 0x0150, 0x0190, 0x0220 65 - }; 66 - 67 - static const unsigned int mtk_pwm_reg_offset_v2[] = { 68 - 0x0080, 0x00c0, 0x0100, 0x0140, 0x0180, 0x01c0, 0x0200, 0x0240 69 - }; 70 - 71 - /* PWM IP Version 3.0.2 */ 72 - static const unsigned int mtk_pwm_reg_offset_v3[] = { 73 - 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600, 0x0700, 0x0800 57 + struct { 58 + struct clk *clk; 59 + unsigned long rate; 60 + } clk_pwms[]; 74 61 }; 75 62 76 63 static inline struct pwm_mediatek_chip * ··· 69 76 return pwmchip_get_drvdata(chip); 70 77 } 71 78 72 - static int pwm_mediatek_clk_enable(struct pwm_chip *chip, 73 - struct pwm_device *pwm) 79 + static int pwm_mediatek_clk_enable(struct pwm_mediatek_chip *pc, 80 + unsigned int hwpwm) 74 81 { 75 - struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip); 76 82 int ret; 77 83 78 84 ret = clk_prepare_enable(pc->clk_top); ··· 82 90 if (ret < 0) 83 91 goto disable_clk_top; 84 92 85 - ret = clk_prepare_enable(pc->clk_pwms[pwm->hwpwm]); 93 + ret = clk_prepare_enable(pc->clk_pwms[hwpwm].clk); 86 94 if (ret < 0) 87 95 goto disable_clk_main; 88 96 97 + if (!pc->clk_pwms[hwpwm].rate) { 98 + pc->clk_pwms[hwpwm].rate = clk_get_rate(pc->clk_pwms[hwpwm].clk); 99 + 100 + /* 101 + * With the clk running with not more than 1 GHz the 102 + * calculations in .apply() won't overflow. 103 + */ 104 + if (!pc->clk_pwms[hwpwm].rate || 105 + pc->clk_pwms[hwpwm].rate > 1000000000) { 106 + ret = -EINVAL; 107 + goto disable_clk_hwpwm; 108 + } 109 + } 110 + 89 111 return 0; 90 112 113 + disable_clk_hwpwm: 114 + clk_disable_unprepare(pc->clk_pwms[hwpwm].clk); 91 115 disable_clk_main: 92 116 clk_disable_unprepare(pc->clk_main); 93 117 disable_clk_top: ··· 112 104 return ret; 113 105 } 114 106 115 - static void pwm_mediatek_clk_disable(struct pwm_chip *chip, 116 - struct pwm_device *pwm) 107 + static void pwm_mediatek_clk_disable(struct pwm_mediatek_chip *pc, 108 + unsigned int hwpwm) 117 109 { 118 - struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip); 119 - 120 - clk_disable_unprepare(pc->clk_pwms[pwm->hwpwm]); 110 + clk_disable_unprepare(pc->clk_pwms[hwpwm].clk); 121 111 clk_disable_unprepare(pc->clk_main); 122 112 clk_disable_unprepare(pc->clk_top); 123 113 } ··· 124 118 unsigned int num, unsigned int offset, 125 119 u32 value) 126 120 { 127 - writel(value, chip->regs + chip->soc->reg_offset[num] + offset); 121 + writel(value, chip->regs + chip->soc->chanreg_base + 122 + num * chip->soc->chanreg_width + offset); 123 + } 124 + 125 + static inline u32 pwm_mediatek_readl(struct pwm_mediatek_chip *chip, 126 + unsigned int num, unsigned int offset) 127 + { 128 + return readl(chip->regs + chip->soc->chanreg_base + 129 + num * chip->soc->chanreg_width + offset); 128 130 } 129 131 130 132 static void pwm_mediatek_enable(struct pwm_chip *chip, struct pwm_device *pwm) ··· 156 142 } 157 143 158 144 static int pwm_mediatek_config(struct pwm_chip *chip, struct pwm_device *pwm, 159 - int duty_ns, int period_ns) 145 + u64 duty_ns, u64 period_ns) 160 146 { 161 147 struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip); 162 - u32 clkdiv = 0, cnt_period, cnt_duty, reg_width = PWMDWIDTH, 163 - reg_thres = PWMTHRES; 148 + u32 clkdiv, enable; 149 + u32 reg_width = PWMDWIDTH, reg_thres = PWMTHRES; 150 + u64 cnt_period, cnt_duty; 164 151 unsigned long clk_rate; 165 - u64 resolution; 166 152 int ret; 167 153 168 - ret = pwm_mediatek_clk_enable(chip, pwm); 154 + ret = pwm_mediatek_clk_enable(pc, pwm->hwpwm); 169 155 if (ret < 0) 170 156 return ret; 171 157 172 - clk_rate = clk_get_rate(pc->clk_pwms[pwm->hwpwm]); 173 - if (!clk_rate) { 174 - ret = -EINVAL; 175 - goto out; 176 - } 158 + clk_rate = pc->clk_pwms[pwm->hwpwm].rate; 177 159 178 160 /* Make sure we use the bus clock and not the 26MHz clock */ 179 161 if (pc->soc->pwm_ck_26m_sel_reg) 180 162 writel(0, pc->regs + pc->soc->pwm_ck_26m_sel_reg); 181 163 182 - /* Using resolution in picosecond gets accuracy higher */ 183 - resolution = (u64)NSEC_PER_SEC * 1000; 184 - do_div(resolution, clk_rate); 185 - 186 - cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000, resolution); 187 - if (!cnt_period) 188 - return -EINVAL; 189 - 190 - while (cnt_period > 8192) { 191 - resolution *= 2; 192 - clkdiv++; 193 - cnt_period = DIV_ROUND_CLOSEST_ULL((u64)period_ns * 1000, 194 - resolution); 195 - } 196 - 197 - if (clkdiv > PWM_CLK_DIV_MAX) { 198 - dev_err(pwmchip_parent(chip), "period of %d ns not supported\n", period_ns); 199 - ret = -EINVAL; 164 + cnt_period = mul_u64_u64_div_u64(period_ns, clk_rate, NSEC_PER_SEC); 165 + if (cnt_period == 0) { 166 + ret = -ERANGE; 200 167 goto out; 201 168 } 169 + 170 + if (cnt_period > FIELD_MAX(PWMDWIDTH_PERIOD) + 1) { 171 + if (cnt_period >= ((FIELD_MAX(PWMDWIDTH_PERIOD) + 1) << FIELD_MAX(PWMCON_CLKDIV))) { 172 + clkdiv = FIELD_MAX(PWMCON_CLKDIV); 173 + cnt_period = FIELD_MAX(PWMDWIDTH_PERIOD) + 1; 174 + } else { 175 + clkdiv = ilog2(cnt_period) - ilog2(FIELD_MAX(PWMDWIDTH_PERIOD)); 176 + cnt_period >>= clkdiv; 177 + } 178 + } else { 179 + clkdiv = 0; 180 + } 181 + 182 + cnt_duty = mul_u64_u64_div_u64(duty_ns, clk_rate, NSEC_PER_SEC) >> clkdiv; 183 + if (cnt_duty > cnt_period) 184 + cnt_duty = cnt_period; 185 + 186 + if (cnt_duty) { 187 + cnt_duty -= 1; 188 + enable = BIT(pwm->hwpwm); 189 + } else { 190 + enable = 0; 191 + } 192 + 193 + cnt_period -= 1; 194 + 195 + dev_dbg(&chip->dev, "pwm#%u: %lld/%lld @%lu -> CON: %x, PERIOD: %llx, DUTY: %llx\n", 196 + pwm->hwpwm, duty_ns, period_ns, clk_rate, clkdiv, cnt_period, cnt_duty); 202 197 203 198 if (pc->soc->pwm45_fixup && pwm->hwpwm > 2) { 204 199 /* ··· 218 195 reg_thres = PWM45THRES_FIXUP; 219 196 } 220 197 221 - cnt_duty = DIV_ROUND_CLOSEST_ULL((u64)duty_ns * 1000, resolution); 222 - 223 198 pwm_mediatek_writel(pc, pwm->hwpwm, PWMCON, BIT(15) | clkdiv); 224 - pwm_mediatek_writel(pc, pwm->hwpwm, reg_width, cnt_period - 1); 199 + pwm_mediatek_writel(pc, pwm->hwpwm, reg_width, cnt_period); 225 200 226 - if (cnt_duty) { 227 - pwm_mediatek_writel(pc, pwm->hwpwm, reg_thres, cnt_duty - 1); 201 + if (enable) { 202 + pwm_mediatek_writel(pc, pwm->hwpwm, reg_thres, cnt_duty); 228 203 pwm_mediatek_enable(chip, pwm); 229 204 } else { 230 205 pwm_mediatek_disable(chip, pwm); 231 206 } 232 207 233 208 out: 234 - pwm_mediatek_clk_disable(chip, pwm); 209 + pwm_mediatek_clk_disable(pc, pwm->hwpwm); 235 210 236 211 return ret; 237 212 } ··· 237 216 static int pwm_mediatek_apply(struct pwm_chip *chip, struct pwm_device *pwm, 238 217 const struct pwm_state *state) 239 218 { 219 + struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip); 240 220 int err; 241 221 242 222 if (state->polarity != PWM_POLARITY_NORMAL) ··· 246 224 if (!state->enabled) { 247 225 if (pwm->state.enabled) { 248 226 pwm_mediatek_disable(chip, pwm); 249 - pwm_mediatek_clk_disable(chip, pwm); 227 + pwm_mediatek_clk_disable(pc, pwm->hwpwm); 250 228 } 251 229 252 230 return 0; ··· 257 235 return err; 258 236 259 237 if (!pwm->state.enabled) 260 - err = pwm_mediatek_clk_enable(chip, pwm); 238 + err = pwm_mediatek_clk_enable(pc, pwm->hwpwm); 261 239 262 240 return err; 263 241 } 264 242 243 + static int pwm_mediatek_get_state(struct pwm_chip *chip, struct pwm_device *pwm, 244 + struct pwm_state *state) 245 + { 246 + struct pwm_mediatek_chip *pc = to_pwm_mediatek_chip(chip); 247 + int ret; 248 + u32 enable; 249 + u32 reg_width = PWMDWIDTH, reg_thres = PWMTHRES; 250 + 251 + if (pc->soc->pwm45_fixup && pwm->hwpwm > 2) { 252 + /* 253 + * PWM[4,5] has distinct offset for PWMDWIDTH and PWMTHRES 254 + * from the other PWMs on MT7623. 255 + */ 256 + reg_width = PWM45DWIDTH_FIXUP; 257 + reg_thres = PWM45THRES_FIXUP; 258 + } 259 + 260 + ret = pwm_mediatek_clk_enable(pc, pwm->hwpwm); 261 + if (ret < 0) 262 + return ret; 263 + 264 + enable = readl(pc->regs); 265 + if (enable & BIT(pwm->hwpwm)) { 266 + u32 clkdiv, cnt_period, cnt_duty; 267 + unsigned long clk_rate; 268 + 269 + clk_rate = pc->clk_pwms[pwm->hwpwm].rate; 270 + 271 + state->enabled = true; 272 + state->polarity = PWM_POLARITY_NORMAL; 273 + 274 + clkdiv = FIELD_GET(PWMCON_CLKDIV, 275 + pwm_mediatek_readl(pc, pwm->hwpwm, PWMCON)); 276 + cnt_period = FIELD_GET(PWMDWIDTH_PERIOD, 277 + pwm_mediatek_readl(pc, pwm->hwpwm, reg_width)); 278 + cnt_duty = FIELD_GET(PWMTHRES_DUTY, 279 + pwm_mediatek_readl(pc, pwm->hwpwm, reg_thres)); 280 + 281 + /* 282 + * cnt_period is a 13 bit value, NSEC_PER_SEC is 30 bits wide 283 + * and clkdiv is less than 8, so the multiplication doesn't 284 + * overflow an u64. 285 + */ 286 + state->period = 287 + DIV_ROUND_UP_ULL((u64)cnt_period * NSEC_PER_SEC << clkdiv, clk_rate); 288 + state->duty_cycle = 289 + DIV_ROUND_UP_ULL((u64)cnt_duty * NSEC_PER_SEC << clkdiv, clk_rate); 290 + } else { 291 + state->enabled = false; 292 + } 293 + 294 + pwm_mediatek_clk_disable(pc, pwm->hwpwm); 295 + 296 + return ret; 297 + } 298 + 265 299 static const struct pwm_ops pwm_mediatek_ops = { 266 300 .apply = pwm_mediatek_apply, 301 + .get_state = pwm_mediatek_get_state, 267 302 }; 303 + 304 + static int pwm_mediatek_init_used_clks(struct pwm_mediatek_chip *pc) 305 + { 306 + const struct pwm_mediatek_of_data *soc = pc->soc; 307 + unsigned int hwpwm; 308 + u32 enabled, handled = 0; 309 + int ret; 310 + 311 + ret = clk_prepare_enable(pc->clk_top); 312 + if (ret) 313 + return ret; 314 + 315 + ret = clk_prepare_enable(pc->clk_main); 316 + if (ret) 317 + goto err_enable_main; 318 + 319 + enabled = readl(pc->regs) & GENMASK(soc->num_pwms - 1, 0); 320 + 321 + while (enabled & ~handled) { 322 + hwpwm = ilog2(enabled & ~handled); 323 + 324 + ret = pwm_mediatek_clk_enable(pc, hwpwm); 325 + if (ret) { 326 + while (handled) { 327 + hwpwm = ilog2(handled); 328 + 329 + pwm_mediatek_clk_disable(pc, hwpwm); 330 + handled &= ~BIT(hwpwm); 331 + } 332 + 333 + break; 334 + } 335 + 336 + handled |= BIT(hwpwm); 337 + } 338 + 339 + clk_disable_unprepare(pc->clk_main); 340 + err_enable_main: 341 + 342 + clk_disable_unprepare(pc->clk_top); 343 + 344 + return ret; 345 + } 268 346 269 347 static int pwm_mediatek_probe(struct platform_device *pdev) 270 348 { ··· 376 254 377 255 soc = of_device_get_match_data(&pdev->dev); 378 256 379 - chip = devm_pwmchip_alloc(&pdev->dev, soc->num_pwms, sizeof(*pc)); 257 + chip = devm_pwmchip_alloc(&pdev->dev, soc->num_pwms, 258 + sizeof(*pc) + soc->num_pwms * sizeof(*pc->clk_pwms)); 380 259 if (IS_ERR(chip)) 381 260 return PTR_ERR(chip); 382 261 pc = to_pwm_mediatek_chip(chip); ··· 387 264 pc->regs = devm_platform_ioremap_resource(pdev, 0); 388 265 if (IS_ERR(pc->regs)) 389 266 return PTR_ERR(pc->regs); 390 - 391 - pc->clk_pwms = devm_kmalloc_array(&pdev->dev, soc->num_pwms, 392 - sizeof(*pc->clk_pwms), GFP_KERNEL); 393 - if (!pc->clk_pwms) 394 - return -ENOMEM; 395 267 396 268 pc->clk_top = devm_clk_get(&pdev->dev, "top"); 397 269 if (IS_ERR(pc->clk_top)) ··· 403 285 404 286 snprintf(name, sizeof(name), "pwm%d", i + 1); 405 287 406 - pc->clk_pwms[i] = devm_clk_get(&pdev->dev, name); 407 - if (IS_ERR(pc->clk_pwms[i])) 408 - return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk_pwms[i]), 288 + pc->clk_pwms[i].clk = devm_clk_get(&pdev->dev, name); 289 + if (IS_ERR(pc->clk_pwms[i].clk)) 290 + return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk_pwms[i].clk), 409 291 "Failed to get %s clock\n", name); 292 + 293 + ret = devm_clk_rate_exclusive_get(&pdev->dev, pc->clk_pwms[i].clk); 294 + if (ret) 295 + return dev_err_probe(&pdev->dev, ret, 296 + "Failed to lock clock rate for %s\n", name); 410 297 } 298 + 299 + ret = pwm_mediatek_init_used_clks(pc); 300 + if (ret) 301 + return dev_err_probe(&pdev->dev, ret, "Failed to initialize used clocks\n"); 411 302 412 303 chip->ops = &pwm_mediatek_ops; 413 304 ··· 430 303 static const struct pwm_mediatek_of_data mt2712_pwm_data = { 431 304 .num_pwms = 8, 432 305 .pwm45_fixup = false, 433 - .reg_offset = mtk_pwm_reg_offset_v1, 306 + .chanreg_base = 0x10, 307 + .chanreg_width = 0x40, 434 308 }; 435 309 436 310 static const struct pwm_mediatek_of_data mt6795_pwm_data = { 437 311 .num_pwms = 7, 438 312 .pwm45_fixup = false, 439 - .reg_offset = mtk_pwm_reg_offset_v1, 313 + .chanreg_base = 0x10, 314 + .chanreg_width = 0x40, 440 315 }; 441 316 442 317 static const struct pwm_mediatek_of_data mt7622_pwm_data = { 443 318 .num_pwms = 6, 444 319 .pwm45_fixup = false, 445 320 .pwm_ck_26m_sel_reg = PWM_CK_26M_SEL, 446 - .reg_offset = mtk_pwm_reg_offset_v1, 321 + .chanreg_base = 0x10, 322 + .chanreg_width = 0x40, 447 323 }; 448 324 449 325 static const struct pwm_mediatek_of_data mt7623_pwm_data = { 450 326 .num_pwms = 5, 451 327 .pwm45_fixup = true, 452 - .reg_offset = mtk_pwm_reg_offset_v1, 328 + .chanreg_base = 0x10, 329 + .chanreg_width = 0x40, 453 330 }; 454 331 455 332 static const struct pwm_mediatek_of_data mt7628_pwm_data = { 456 333 .num_pwms = 4, 457 334 .pwm45_fixup = true, 458 - .reg_offset = mtk_pwm_reg_offset_v1, 335 + .chanreg_base = 0x10, 336 + .chanreg_width = 0x40, 459 337 }; 460 338 461 339 static const struct pwm_mediatek_of_data mt7629_pwm_data = { 462 340 .num_pwms = 1, 463 341 .pwm45_fixup = false, 464 - .reg_offset = mtk_pwm_reg_offset_v1, 342 + .chanreg_base = 0x10, 343 + .chanreg_width = 0x40, 465 344 }; 466 345 467 346 static const struct pwm_mediatek_of_data mt7981_pwm_data = { 468 347 .num_pwms = 3, 469 348 .pwm45_fixup = false, 470 349 .pwm_ck_26m_sel_reg = PWM_CK_26M_SEL, 471 - .reg_offset = mtk_pwm_reg_offset_v2, 350 + .chanreg_base = 0x80, 351 + .chanreg_width = 0x40, 472 352 }; 473 353 474 354 static const struct pwm_mediatek_of_data mt7986_pwm_data = { 475 355 .num_pwms = 2, 476 356 .pwm45_fixup = false, 477 357 .pwm_ck_26m_sel_reg = PWM_CK_26M_SEL, 478 - .reg_offset = mtk_pwm_reg_offset_v1, 358 + .chanreg_base = 0x10, 359 + .chanreg_width = 0x40, 479 360 }; 480 361 481 362 static const struct pwm_mediatek_of_data mt7988_pwm_data = { 482 363 .num_pwms = 8, 483 364 .pwm45_fixup = false, 484 - .reg_offset = mtk_pwm_reg_offset_v2, 365 + .chanreg_base = 0x80, 366 + .chanreg_width = 0x40, 485 367 }; 486 368 487 369 static const struct pwm_mediatek_of_data mt8183_pwm_data = { 488 370 .num_pwms = 4, 489 371 .pwm45_fixup = false, 490 372 .pwm_ck_26m_sel_reg = PWM_CK_26M_SEL, 491 - .reg_offset = mtk_pwm_reg_offset_v1, 373 + .chanreg_base = 0x10, 374 + .chanreg_width = 0x40, 492 375 }; 493 376 494 377 static const struct pwm_mediatek_of_data mt8365_pwm_data = { 495 378 .num_pwms = 3, 496 379 .pwm45_fixup = false, 497 380 .pwm_ck_26m_sel_reg = PWM_CK_26M_SEL, 498 - .reg_offset = mtk_pwm_reg_offset_v1, 381 + .chanreg_base = 0x10, 382 + .chanreg_width = 0x40, 499 383 }; 500 384 501 385 static const struct pwm_mediatek_of_data mt8516_pwm_data = { 502 386 .num_pwms = 5, 503 387 .pwm45_fixup = false, 504 388 .pwm_ck_26m_sel_reg = PWM_CK_26M_SEL, 505 - .reg_offset = mtk_pwm_reg_offset_v1, 389 + .chanreg_base = 0x10, 390 + .chanreg_width = 0x40, 506 391 }; 507 392 508 393 static const struct pwm_mediatek_of_data mt6991_pwm_data = { 509 394 .num_pwms = 4, 510 395 .pwm45_fixup = false, 511 396 .pwm_ck_26m_sel_reg = PWM_CK_26M_SEL_V3, 512 - .reg_offset = mtk_pwm_reg_offset_v3, 397 + .chanreg_base = 0x100, 398 + .chanreg_width = 0x100, 513 399 }; 514 400 515 401 static const struct of_device_id pwm_mediatek_of_match[] = {
+229 -326
drivers/pwm/pwm-pca9685.c
··· 26 26 * that is enabled is allowed to change the prescale register. 27 27 * PWM channels requested afterwards must use a period that results in the same 28 28 * prescale setting as the one set by the first requested channel. 29 - * GPIOs do not count as enabled PWMs as they are not using the prescaler. 30 29 */ 31 30 32 31 #define PCA9685_MODE1 0x00 ··· 49 50 #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */ 50 51 51 52 #define PCA9685_COUNTER_RANGE 4096 52 - #define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */ 53 + #define PCA9685_OSC_CLOCK_HZ 25000000 /* Internal oscillator with 25 MHz */ 54 + 55 + /* 56 + * The time value of one counter tick. Note that NSEC_PER_SEC is an integer 57 + * multiple of PCA9685_OSC_CLOCK_HZ, so there is no rounding involved and we're 58 + * not loosing precision due to the early division. 59 + */ 60 + #define PCA9685_QUANTUM_NS(_prescale) ((NSEC_PER_SEC / PCA9685_OSC_CLOCK_HZ) * (_prescale + 1)) 53 61 54 62 #define PCA9685_NUMREGS 0xFF 55 63 #define PCA9685_MAXCHAN 0x10 ··· 67 61 #define MODE1_SUB2 BIT(2) 68 62 #define MODE1_SUB1 BIT(3) 69 63 #define MODE1_SLEEP BIT(4) 64 + #define MODE1_AI BIT(5) 65 + 70 66 #define MODE2_INVRT BIT(4) 71 67 #define MODE2_OUTDRV BIT(2) 72 68 ··· 86 78 struct regmap *regmap; 87 79 struct mutex lock; 88 80 DECLARE_BITMAP(pwms_enabled, PCA9685_MAXCHAN + 1); 89 - #if IS_ENABLED(CONFIG_GPIOLIB) 90 - struct gpio_chip gpio; 91 - DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1); 92 - #endif 93 81 }; 94 82 95 83 static inline struct pca9685 *to_pca(struct pwm_chip *chip) ··· 135 131 return err; 136 132 } 137 133 138 - /* Helper function to set the duty cycle ratio to duty/4096 (e.g. duty=2048 -> 50%) */ 139 - static void pca9685_pwm_set_duty(struct pwm_chip *chip, int channel, unsigned int duty) 140 - { 141 - struct pwm_device *pwm = &chip->pwms[channel]; 142 - unsigned int on, off; 143 - 144 - if (duty == 0) { 145 - /* Set the full OFF bit, which has the highest precedence */ 146 - pca9685_write_reg(chip, REG_OFF_H(channel), LED_FULL); 147 - return; 148 - } else if (duty >= PCA9685_COUNTER_RANGE) { 149 - /* Set the full ON bit and clear the full OFF bit */ 150 - pca9685_write_reg(chip, REG_ON_H(channel), LED_FULL); 151 - pca9685_write_reg(chip, REG_OFF_H(channel), 0); 152 - return; 153 - } 154 - 155 - 156 - if (pwm->state.usage_power && channel < PCA9685_MAXCHAN) { 157 - /* 158 - * If usage_power is set, the pca9685 driver will phase shift 159 - * the individual channels relative to their channel number. 160 - * This improves EMI because the enabled channels no longer 161 - * turn on at the same time, while still maintaining the 162 - * configured duty cycle / power output. 163 - */ 164 - on = channel * PCA9685_COUNTER_RANGE / PCA9685_MAXCHAN; 165 - } else 166 - on = 0; 167 - 168 - off = (on + duty) % PCA9685_COUNTER_RANGE; 169 - 170 - /* Set ON time (clears full ON bit) */ 171 - pca9685_write_reg(chip, REG_ON_L(channel), on & 0xff); 172 - pca9685_write_reg(chip, REG_ON_H(channel), (on >> 8) & 0xf); 173 - /* Set OFF time (clears full OFF bit) */ 174 - pca9685_write_reg(chip, REG_OFF_L(channel), off & 0xff); 175 - pca9685_write_reg(chip, REG_OFF_H(channel), (off >> 8) & 0xf); 176 - } 177 - 178 - static unsigned int pca9685_pwm_get_duty(struct pwm_chip *chip, int channel) 179 - { 180 - struct pwm_device *pwm = &chip->pwms[channel]; 181 - unsigned int off = 0, on = 0, val = 0; 182 - 183 - if (WARN_ON(channel >= PCA9685_MAXCHAN)) { 184 - /* HW does not support reading state of "all LEDs" channel */ 185 - return 0; 186 - } 187 - 188 - pca9685_read_reg(chip, LED_N_OFF_H(channel), &off); 189 - if (off & LED_FULL) { 190 - /* Full OFF bit is set */ 191 - return 0; 192 - } 193 - 194 - pca9685_read_reg(chip, LED_N_ON_H(channel), &on); 195 - if (on & LED_FULL) { 196 - /* Full ON bit is set */ 197 - return PCA9685_COUNTER_RANGE; 198 - } 199 - 200 - pca9685_read_reg(chip, LED_N_OFF_L(channel), &val); 201 - off = ((off & 0xf) << 8) | (val & 0xff); 202 - if (!pwm->state.usage_power) 203 - return off; 204 - 205 - /* Read ON register to calculate duty cycle of staggered output */ 206 - if (pca9685_read_reg(chip, LED_N_ON_L(channel), &val)) { 207 - /* Reset val to 0 in case reading LED_N_ON_L failed */ 208 - val = 0; 209 - } 210 - on = ((on & 0xf) << 8) | (val & 0xff); 211 - return (off - on) & (PCA9685_COUNTER_RANGE - 1); 212 - } 213 - 214 - #if IS_ENABLED(CONFIG_GPIOLIB) 215 - static bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, int pwm_idx) 216 - { 217 - bool is_inuse; 218 - 219 - mutex_lock(&pca->lock); 220 - if (pwm_idx >= PCA9685_MAXCHAN) { 221 - /* 222 - * "All LEDs" channel: 223 - * pretend already in use if any of the PWMs are requested 224 - */ 225 - if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) { 226 - is_inuse = true; 227 - goto out; 228 - } 229 - } else { 230 - /* 231 - * Regular channel: 232 - * pretend already in use if the "all LEDs" channel is requested 233 - */ 234 - if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) { 235 - is_inuse = true; 236 - goto out; 237 - } 238 - } 239 - is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse); 240 - out: 241 - mutex_unlock(&pca->lock); 242 - return is_inuse; 243 - } 244 - 245 - static void pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx) 246 - { 247 - mutex_lock(&pca->lock); 248 - clear_bit(pwm_idx, pca->pwms_inuse); 249 - mutex_unlock(&pca->lock); 250 - } 251 - 252 - static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset) 253 - { 254 - struct pwm_chip *chip = gpiochip_get_data(gpio); 255 - struct pca9685 *pca = to_pca(chip); 256 - 257 - if (pca9685_pwm_test_and_set_inuse(pca, offset)) 258 - return -EBUSY; 259 - pm_runtime_get_sync(pwmchip_parent(chip)); 260 - return 0; 261 - } 262 - 263 - static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset) 264 - { 265 - struct pwm_chip *chip = gpiochip_get_data(gpio); 266 - 267 - return pca9685_pwm_get_duty(chip, offset) != 0; 268 - } 269 - 270 - static int pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset, 271 - int value) 272 - { 273 - struct pwm_chip *chip = gpiochip_get_data(gpio); 274 - 275 - pca9685_pwm_set_duty(chip, offset, value ? PCA9685_COUNTER_RANGE : 0); 276 - 277 - return 0; 278 - } 279 - 280 - static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset) 281 - { 282 - struct pwm_chip *chip = gpiochip_get_data(gpio); 283 - struct pca9685 *pca = to_pca(chip); 284 - 285 - pca9685_pwm_set_duty(chip, offset, 0); 286 - pm_runtime_put(pwmchip_parent(chip)); 287 - pca9685_pwm_clear_inuse(pca, offset); 288 - } 289 - 290 - static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip, 291 - unsigned int offset) 292 - { 293 - /* Always out */ 294 - return GPIO_LINE_DIRECTION_OUT; 295 - } 296 - 297 - static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio, 298 - unsigned int offset) 299 - { 300 - return -EINVAL; 301 - } 302 - 303 - static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio, 304 - unsigned int offset, int value) 305 - { 306 - pca9685_pwm_gpio_set(gpio, offset, value); 307 - 308 - return 0; 309 - } 310 - 311 - /* 312 - * The PCA9685 has a bit for turning the PWM output full off or on. Some 313 - * boards like Intel Galileo actually uses these as normal GPIOs so we 314 - * expose a GPIO chip here which can exclusively take over the underlying 315 - * PWM channel. 316 - */ 317 - static int pca9685_pwm_gpio_probe(struct pwm_chip *chip) 134 + static int pca9685_write_4reg(struct pwm_chip *chip, unsigned int reg, u8 val[4]) 318 135 { 319 136 struct pca9685 *pca = to_pca(chip); 320 137 struct device *dev = pwmchip_parent(chip); 138 + int err; 321 139 322 - pca->gpio.label = dev_name(dev); 323 - pca->gpio.parent = dev; 324 - pca->gpio.request = pca9685_pwm_gpio_request; 325 - pca->gpio.free = pca9685_pwm_gpio_free; 326 - pca->gpio.get_direction = pca9685_pwm_gpio_get_direction; 327 - pca->gpio.direction_input = pca9685_pwm_gpio_direction_input; 328 - pca->gpio.direction_output = pca9685_pwm_gpio_direction_output; 329 - pca->gpio.get = pca9685_pwm_gpio_get; 330 - pca->gpio.set = pca9685_pwm_gpio_set; 331 - pca->gpio.base = -1; 332 - pca->gpio.ngpio = PCA9685_MAXCHAN; 333 - pca->gpio.can_sleep = true; 140 + err = regmap_bulk_write(pca->regmap, reg, val, 4); 141 + if (err) 142 + dev_err(dev, "regmap_write to register 0x%x failed: %pe\n", reg, ERR_PTR(err)); 334 143 335 - return devm_gpiochip_add_data(dev, &pca->gpio, chip); 336 - } 337 - #else 338 - static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, 339 - int pwm_idx) 340 - { 341 - return false; 144 + return err; 342 145 } 343 146 344 - static inline void 345 - pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx) 147 + static int pca9685_set_sleep_mode(struct pwm_chip *chip, bool enable) 346 148 { 347 - } 348 - 349 - static inline int pca9685_pwm_gpio_probe(struct pwm_chip *chip) 350 - { 351 - return 0; 352 - } 353 - #endif 354 - 355 - static void pca9685_set_sleep_mode(struct pwm_chip *chip, bool enable) 356 - { 357 - struct device *dev = pwmchip_parent(chip); 358 149 struct pca9685 *pca = to_pca(chip); 359 - int err = regmap_update_bits(pca->regmap, PCA9685_MODE1, 360 - MODE1_SLEEP, enable ? MODE1_SLEEP : 0); 361 - if (err) { 362 - dev_err(dev, "regmap_update_bits of register 0x%x failed: %pe\n", 363 - PCA9685_MODE1, ERR_PTR(err)); 364 - return; 365 - } 150 + int err; 151 + 152 + err = regmap_update_bits(pca->regmap, PCA9685_MODE1, 153 + MODE1_SLEEP, enable ? MODE1_SLEEP : 0); 154 + if (err) 155 + return err; 366 156 367 157 if (!enable) { 368 158 /* Wait 500us for the oscillator to be back up */ 369 159 udelay(500); 370 160 } 161 + 162 + return 0; 371 163 } 372 164 373 - static int __pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 374 - const struct pwm_state *state) 165 + struct pca9685_waveform { 166 + u8 onoff[4]; 167 + u8 prescale; 168 + }; 169 + 170 + static int pca9685_round_waveform_tohw(struct pwm_chip *chip, struct pwm_device *pwm, const struct pwm_waveform *wf, void *_wfhw) 375 171 { 172 + struct pca9685_waveform *wfhw = _wfhw; 376 173 struct pca9685 *pca = to_pca(chip); 377 - unsigned long long duty, prescale; 378 - unsigned int val = 0; 174 + unsigned int best_prescale; 175 + u8 prescale; 176 + unsigned int period_ns, duty; 177 + int ret_tohw = 0; 379 178 380 - if (state->polarity != PWM_POLARITY_NORMAL) 381 - return -EINVAL; 179 + if (!wf->period_length_ns) { 180 + *wfhw = (typeof(*wfhw)){ 181 + .onoff = { 0, 0, 0, LED_FULL, }, 182 + .prescale = 0, 183 + }; 382 184 383 - prescale = DIV_ROUND_CLOSEST_ULL(PCA9685_OSC_CLOCK_MHZ * state->period, 384 - PCA9685_COUNTER_RANGE * 1000) - 1; 385 - if (prescale < PCA9685_PRESCALE_MIN || prescale > PCA9685_PRESCALE_MAX) { 386 - dev_err(pwmchip_parent(chip), "pwm not changed: period out of bounds!\n"); 387 - return -EINVAL; 388 - } 185 + dev_dbg(&chip->dev, "pwm#%u: %lld/%lld [+%lld] -> [%hhx %hhx %hhx %hhx] PSC:%hhx\n", 186 + pwm->hwpwm, wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, 187 + wfhw->onoff[0], wfhw->onoff[1], wfhw->onoff[2], wfhw->onoff[3], wfhw->prescale); 389 188 390 - if (!state->enabled) { 391 - pca9685_pwm_set_duty(chip, pwm->hwpwm, 0); 392 189 return 0; 393 190 } 394 191 395 - pca9685_read_reg(chip, PCA9685_PRESCALE, &val); 396 - if (prescale != val) { 397 - if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) { 398 - dev_err(pwmchip_parent(chip), 399 - "pwm not changed: periods of enabled pwms must match!\n"); 400 - return -EBUSY; 192 + if (wf->period_length_ns >= PCA9685_COUNTER_RANGE * PCA9685_QUANTUM_NS(255)) { 193 + best_prescale = 255; 194 + } else if (wf->period_length_ns < PCA9685_COUNTER_RANGE * PCA9685_QUANTUM_NS(3)) { 195 + best_prescale = 3; 196 + ret_tohw = 1; 197 + } else { 198 + best_prescale = (unsigned int)wf->period_length_ns / (PCA9685_COUNTER_RANGE * (NSEC_PER_SEC / PCA9685_OSC_CLOCK_HZ)) - 1; 199 + } 200 + 201 + guard(mutex)(&pca->lock); 202 + 203 + if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) { 204 + unsigned int current_prescale; 205 + int ret; 206 + 207 + ret = regmap_read(pca->regmap, PCA9685_PRESCALE, &current_prescale); 208 + if (ret) 209 + return ret; 210 + 211 + if (current_prescale > best_prescale) 212 + ret_tohw = 1; 213 + 214 + prescale = current_prescale; 215 + } else { 216 + prescale = best_prescale; 217 + } 218 + 219 + period_ns = PCA9685_COUNTER_RANGE * PCA9685_QUANTUM_NS(prescale); 220 + 221 + duty = (unsigned)min_t(u64, wf->duty_length_ns, period_ns) / PCA9685_QUANTUM_NS(prescale); 222 + 223 + if (duty < PCA9685_COUNTER_RANGE) { 224 + unsigned int on, off; 225 + 226 + on = (unsigned)min_t(u64, wf->duty_offset_ns, period_ns) / PCA9685_QUANTUM_NS(prescale); 227 + off = (on + duty) % PCA9685_COUNTER_RANGE; 228 + 229 + /* 230 + * With a zero duty cycle, it doesn't matter if period was 231 + * rounded up 232 + */ 233 + if (!duty) 234 + ret_tohw = 0; 235 + 236 + *wfhw = (typeof(*wfhw)){ 237 + .onoff = { on & 0xff, (on >> 8) & 0xf, off & 0xff, (off >> 8) & 0xf }, 238 + .prescale = prescale, 239 + }; 240 + } else { 241 + *wfhw = (typeof(*wfhw)){ 242 + .onoff = { 0, LED_FULL, 0, 0, }, 243 + .prescale = prescale, 244 + }; 245 + } 246 + 247 + dev_dbg(&chip->dev, "pwm#%u: %lld/%lld [+%lld] -> %s[%hhx %hhx %hhx %hhx] PSC:%hhx\n", 248 + pwm->hwpwm, wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns, 249 + ret_tohw ? "#" : "", wfhw->onoff[0], wfhw->onoff[1], wfhw->onoff[2], wfhw->onoff[3], wfhw->prescale); 250 + 251 + return ret_tohw; 252 + } 253 + 254 + static int pca9685_round_waveform_fromhw(struct pwm_chip *chip, struct pwm_device *pwm, 255 + const void *_wfhw, struct pwm_waveform *wf) 256 + { 257 + const struct pca9685_waveform *wfhw = _wfhw; 258 + struct pca9685 *pca = to_pca(chip); 259 + unsigned int prescale; 260 + 261 + if (wfhw->prescale) 262 + prescale = wfhw->prescale; 263 + else 264 + scoped_guard(mutex, &pca->lock) { 265 + int ret; 266 + 267 + ret = regmap_read(pca->regmap, PCA9685_PRESCALE, &prescale); 268 + if (ret) 269 + return ret; 401 270 } 402 271 403 - /* 404 - * Putting the chip briefly into SLEEP mode 405 - * at this point won't interfere with the 406 - * pm_runtime framework, because the pm_runtime 407 - * state is guaranteed active here. 408 - */ 409 - /* Put chip into sleep mode */ 410 - pca9685_set_sleep_mode(chip, true); 272 + wf->period_length_ns = PCA9685_COUNTER_RANGE * PCA9685_QUANTUM_NS(prescale); 411 273 412 - /* Change the chip-wide output frequency */ 413 - pca9685_write_reg(chip, PCA9685_PRESCALE, prescale); 274 + if (wfhw->onoff[3] & LED_FULL) { 275 + wf->duty_length_ns = 0; 276 + wf->duty_offset_ns = 0; 277 + } else if (wfhw->onoff[1] & LED_FULL) { 278 + wf->duty_length_ns = wf->period_length_ns; 279 + wf->duty_offset_ns = 0; 280 + } else { 281 + unsigned int on = wfhw->onoff[0] | (wfhw->onoff[1] & 0xf) << 8; 282 + unsigned int off = wfhw->onoff[2] | (wfhw->onoff[3] & 0xf) << 8; 414 283 415 - /* Wake the chip up */ 416 - pca9685_set_sleep_mode(chip, false); 284 + wf->duty_length_ns = (off - on) % PCA9685_COUNTER_RANGE * PCA9685_QUANTUM_NS(prescale); 285 + wf->duty_offset_ns = on * PCA9685_QUANTUM_NS(prescale); 417 286 } 418 287 419 - duty = PCA9685_COUNTER_RANGE * state->duty_cycle; 420 - duty = DIV_ROUND_UP_ULL(duty, state->period); 421 - pca9685_pwm_set_duty(chip, pwm->hwpwm, duty); 288 + dev_dbg(&chip->dev, "pwm#%u: [%hhx %hhx %hhx %hhx] PSC:%hhx -> %lld/%lld [+%lld]\n", 289 + pwm->hwpwm, 290 + wfhw->onoff[0], wfhw->onoff[1], wfhw->onoff[2], wfhw->onoff[3], wfhw->prescale, 291 + wf->duty_length_ns, wf->period_length_ns, wf->duty_offset_ns); 292 + 422 293 return 0; 423 294 } 424 295 425 - static int pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 426 - const struct pwm_state *state) 296 + static int pca9685_read_waveform(struct pwm_chip *chip, struct pwm_device *pwm, void *_wfhw) 427 297 { 298 + struct pca9685_waveform *wfhw = _wfhw; 428 299 struct pca9685 *pca = to_pca(chip); 300 + unsigned int prescale; 429 301 int ret; 430 302 431 - mutex_lock(&pca->lock); 432 - ret = __pca9685_pwm_apply(chip, pwm, state); 433 - if (ret == 0) { 434 - if (state->enabled) 435 - set_bit(pwm->hwpwm, pca->pwms_enabled); 436 - else 437 - clear_bit(pwm->hwpwm, pca->pwms_enabled); 438 - } 439 - mutex_unlock(&pca->lock); 303 + guard(mutex)(&pca->lock); 440 304 441 - return ret; 442 - } 305 + ret = regmap_bulk_read(pca->regmap, REG_ON_L(pwm->hwpwm), &wfhw->onoff, 4); 306 + if (ret) 307 + return ret; 443 308 444 - static int pca9685_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, 445 - struct pwm_state *state) 446 - { 447 - unsigned long long duty; 448 - unsigned int val = 0; 309 + ret = regmap_read(pca->regmap, PCA9685_PRESCALE, &prescale); 310 + if (ret) 311 + return ret; 449 312 450 - /* Calculate (chip-wide) period from prescale value */ 451 - pca9685_read_reg(chip, PCA9685_PRESCALE, &val); 452 - /* 453 - * PCA9685_OSC_CLOCK_MHZ is 25, i.e. an integer divider of 1000. 454 - * The following calculation is therefore only a multiplication 455 - * and we are not losing precision. 456 - */ 457 - state->period = (PCA9685_COUNTER_RANGE * 1000 / PCA9685_OSC_CLOCK_MHZ) * 458 - (val + 1); 459 - 460 - /* The (per-channel) polarity is fixed */ 461 - state->polarity = PWM_POLARITY_NORMAL; 462 - 463 - if (pwm->hwpwm >= PCA9685_MAXCHAN) { 464 - /* 465 - * The "all LEDs" channel does not support HW readout 466 - * Return 0 and disabled for backwards compatibility 467 - */ 468 - state->duty_cycle = 0; 469 - state->enabled = false; 470 - return 0; 471 - } 472 - 473 - state->enabled = true; 474 - duty = pca9685_pwm_get_duty(chip, pwm->hwpwm); 475 - state->duty_cycle = DIV_ROUND_DOWN_ULL(duty * state->period, PCA9685_COUNTER_RANGE); 313 + wfhw->prescale = prescale; 476 314 477 315 return 0; 316 + } 317 + 318 + static int pca9685_write_waveform(struct pwm_chip *chip, struct pwm_device *pwm, const void *_wfhw) 319 + { 320 + const struct pca9685_waveform *wfhw = _wfhw; 321 + struct pca9685 *pca = to_pca(chip); 322 + unsigned int current_prescale; 323 + int ret; 324 + 325 + guard(mutex)(&pca->lock); 326 + 327 + if (wfhw->prescale) { 328 + ret = regmap_read(pca->regmap, PCA9685_PRESCALE, &current_prescale); 329 + if (ret) 330 + return ret; 331 + 332 + if (current_prescale != wfhw->prescale) { 333 + if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) 334 + return -EBUSY; 335 + 336 + /* Put chip into sleep mode */ 337 + ret = pca9685_set_sleep_mode(chip, true); 338 + if (ret) 339 + return ret; 340 + 341 + /* Change the chip-wide output frequency */ 342 + ret = regmap_write(pca->regmap, PCA9685_PRESCALE, wfhw->prescale); 343 + if (ret) 344 + return ret; 345 + 346 + /* Wake the chip up */ 347 + ret = pca9685_set_sleep_mode(chip, false); 348 + if (ret) 349 + return ret; 350 + } 351 + } 352 + 353 + return regmap_bulk_write(pca->regmap, REG_ON_L(pwm->hwpwm), &wfhw->onoff, 4); 478 354 } 479 355 480 356 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) 481 357 { 482 358 struct pca9685 *pca = to_pca(chip); 483 - 484 - if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm)) 485 - return -EBUSY; 486 359 487 360 if (pwm->hwpwm < PCA9685_MAXCHAN) { 488 361 /* PWMs - except the "all LEDs" channel - default to enabled */ ··· 378 497 struct pca9685 *pca = to_pca(chip); 379 498 380 499 mutex_lock(&pca->lock); 381 - pca9685_pwm_set_duty(chip, pwm->hwpwm, 0); 382 500 clear_bit(pwm->hwpwm, pca->pwms_enabled); 383 501 mutex_unlock(&pca->lock); 384 502 385 503 pm_runtime_put(pwmchip_parent(chip)); 386 - pca9685_pwm_clear_inuse(pca, pwm->hwpwm); 387 504 } 388 505 389 506 static const struct pwm_ops pca9685_pwm_ops = { 390 - .apply = pca9685_pwm_apply, 391 - .get_state = pca9685_pwm_get_state, 507 + .sizeof_wfhw = sizeof(struct pca9685_waveform), 508 + .round_waveform_tohw = pca9685_round_waveform_tohw, 509 + .round_waveform_fromhw = pca9685_round_waveform_fromhw, 510 + .read_waveform = pca9685_read_waveform, 511 + .write_waveform = pca9685_write_waveform, 392 512 .request = pca9685_pwm_request, 393 513 .free = pca9685_pwm_free, 394 514 }; 395 515 516 + static bool pca9685_readable_reg(struct device *dev, unsigned int reg) 517 + { 518 + /* The ALL_LED registers are readable but read as zero */ 519 + return reg <= REG_OFF_H(15) || reg >= PCA9685_PRESCALE; 520 + } 521 + 522 + static bool pca9685_writeable_reg(struct device *dev, unsigned int reg) 523 + { 524 + return reg <= REG_OFF_H(15) || reg >= PCA9685_ALL_LED_ON_L; 525 + } 526 + 527 + static bool pca9685_volatile_reg(struct device *dev, unsigned int reg) 528 + { 529 + /* 530 + * Writing to an ALL_LED register affects all LEDi registers, so they 531 + * are not cachable. :-\ 532 + */ 533 + return reg < PCA9685_PRESCALE; 534 + } 535 + 396 536 static const struct regmap_config pca9685_regmap_i2c_config = { 397 537 .reg_bits = 8, 398 538 .val_bits = 8, 539 + 540 + .readable_reg = pca9685_readable_reg, 541 + .writeable_reg = pca9685_writeable_reg, 542 + .volatile_reg = pca9685_volatile_reg, 543 + 399 544 .max_register = PCA9685_NUMREGS, 400 - .cache_type = REGCACHE_NONE, 545 + .cache_type = REGCACHE_MAPLE, 401 546 }; 402 547 403 548 static int pca9685_pwm_probe(struct i2c_client *client) ··· 451 544 452 545 mutex_init(&pca->lock); 453 546 454 - ret = pca9685_read_reg(chip, PCA9685_MODE2, &reg); 455 - if (ret) 456 - return ret; 547 + /* clear MODE2_OCH */ 548 + reg = 0; 457 549 458 550 if (device_property_read_bool(&client->dev, "invert")) 459 551 reg |= MODE2_INVRT; ··· 468 562 if (ret) 469 563 return ret; 470 564 471 - /* Disable all LED ALLCALL and SUBx addresses to avoid bus collisions */ 565 + /* 566 + * Disable all LED ALLCALL and SUBx addresses to avoid bus collisions, 567 + * enable Auto-Increment. 568 + */ 472 569 pca9685_read_reg(chip, PCA9685_MODE1, &reg); 473 570 reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3); 571 + reg |= MODE1_AI; 474 572 pca9685_write_reg(chip, PCA9685_MODE1, reg); 475 573 476 574 /* Reset OFF/ON registers to POR default */ 477 - pca9685_write_reg(chip, PCA9685_ALL_LED_OFF_L, 0); 478 - pca9685_write_reg(chip, PCA9685_ALL_LED_OFF_H, LED_FULL); 479 - pca9685_write_reg(chip, PCA9685_ALL_LED_ON_L, 0); 480 - pca9685_write_reg(chip, PCA9685_ALL_LED_ON_H, LED_FULL); 575 + ret = pca9685_write_4reg(chip, PCA9685_ALL_LED_ON_L, (u8[]){ 0, LED_FULL, 0, LED_FULL }); 576 + if (ret < 0) 577 + return dev_err_probe(&client->dev, ret, "Failed to reset ON/OFF registers\n"); 481 578 482 579 chip->ops = &pca9685_pwm_ops; 483 580 484 581 ret = pwmchip_add(chip); 485 582 if (ret < 0) 486 583 return ret; 487 - 488 - ret = pca9685_pwm_gpio_probe(chip); 489 - if (ret < 0) { 490 - pwmchip_remove(chip); 491 - return ret; 492 - } 493 584 494 585 pm_runtime_enable(&client->dev); 495 586
+4
drivers/pwm/pwm-tiecap.c
··· 3 3 * ECAP PWM driver 4 4 * 5 5 * Copyright (C) 2012 Texas Instruments, Inc. - https://www.ti.com/ 6 + * 7 + * Hardware properties: 8 + * - On disable the PWM pin becomes an input, so the behaviour depends on 9 + * external wiring. 6 10 */ 7 11 8 12 #include <linux/module.h>
+61 -93
drivers/pwm/pwm-tiehrpwm.c
··· 36 36 37 37 #define CLKDIV_MAX 7 38 38 #define HSPCLKDIV_MAX 7 39 - #define PERIOD_MAX 0xFFFF 39 + #define PERIOD_MAX 0x10000 40 40 41 41 /* compare module registers */ 42 42 #define CMPA 0x12 ··· 65 65 #define AQCTL_ZRO_FRCHIGH BIT(1) 66 66 #define AQCTL_ZRO_FRCTOGGLE (BIT(1) | BIT(0)) 67 67 68 - #define AQCTL_CHANA_POLNORMAL (AQCTL_CAU_FRCLOW | AQCTL_PRD_FRCHIGH | \ 69 - AQCTL_ZRO_FRCHIGH) 70 - #define AQCTL_CHANA_POLINVERSED (AQCTL_CAU_FRCHIGH | AQCTL_PRD_FRCLOW | \ 71 - AQCTL_ZRO_FRCLOW) 72 - #define AQCTL_CHANB_POLNORMAL (AQCTL_CBU_FRCLOW | AQCTL_PRD_FRCHIGH | \ 73 - AQCTL_ZRO_FRCHIGH) 74 - #define AQCTL_CHANB_POLINVERSED (AQCTL_CBU_FRCHIGH | AQCTL_PRD_FRCLOW | \ 75 - AQCTL_ZRO_FRCLOW) 68 + #define AQCTL_CHANA_POLNORMAL (AQCTL_CAU_FRCLOW | AQCTL_ZRO_FRCHIGH) 69 + #define AQCTL_CHANA_POLINVERSED (AQCTL_CAU_FRCHIGH | AQCTL_ZRO_FRCLOW) 70 + #define AQCTL_CHANB_POLNORMAL (AQCTL_CBU_FRCLOW | AQCTL_ZRO_FRCHIGH) 71 + #define AQCTL_CHANB_POLINVERSED (AQCTL_CBU_FRCHIGH | AQCTL_ZRO_FRCLOW) 76 72 77 73 #define AQSFRC_RLDCSF_MASK (BIT(7) | BIT(6)) 78 74 #define AQSFRC_RLDCSF_ZRO 0 ··· 104 108 unsigned long clk_rate; 105 109 void __iomem *mmio_base; 106 110 unsigned long period_cycles[NUM_PWM_CHANNEL]; 107 - enum pwm_polarity polarity[NUM_PWM_CHANNEL]; 108 111 struct clk *tbclk; 109 112 struct ehrpwm_context ctx; 110 113 }; ··· 161 166 162 167 *prescale_div = (1 << clkdiv) * 163 168 (hspclkdiv ? (hspclkdiv * 2) : 1); 164 - if (*prescale_div > rqst_prescaler) { 169 + if (*prescale_div >= rqst_prescaler) { 165 170 *tb_clk_div = (clkdiv << TBCTL_CLKDIV_SHIFT) | 166 171 (hspclkdiv << TBCTL_HSPCLKDIV_SHIFT); 167 172 return 0; ··· 172 177 return 1; 173 178 } 174 179 175 - static void configure_polarity(struct ehrpwm_pwm_chip *pc, int chan) 176 - { 177 - u16 aqctl_val, aqctl_mask; 178 - unsigned int aqctl_reg; 179 - 180 - /* 181 - * Configure PWM output to HIGH/LOW level on counter 182 - * reaches compare register value and LOW/HIGH level 183 - * on counter value reaches period register value and 184 - * zero value on counter 185 - */ 186 - if (chan == 1) { 187 - aqctl_reg = AQCTLB; 188 - aqctl_mask = AQCTL_CBU_MASK; 189 - 190 - if (pc->polarity[chan] == PWM_POLARITY_INVERSED) 191 - aqctl_val = AQCTL_CHANB_POLINVERSED; 192 - else 193 - aqctl_val = AQCTL_CHANB_POLNORMAL; 194 - } else { 195 - aqctl_reg = AQCTLA; 196 - aqctl_mask = AQCTL_CAU_MASK; 197 - 198 - if (pc->polarity[chan] == PWM_POLARITY_INVERSED) 199 - aqctl_val = AQCTL_CHANA_POLINVERSED; 200 - else 201 - aqctl_val = AQCTL_CHANA_POLNORMAL; 202 - } 203 - 204 - aqctl_mask |= AQCTL_PRD_MASK | AQCTL_ZRO_MASK; 205 - ehrpwm_modify(pc->mmio_base, aqctl_reg, aqctl_mask, aqctl_val); 206 - } 207 - 208 180 /* 209 181 * period_ns = 10^9 * (ps_divval * period_cycles) / PWM_CLK_RATE 210 182 * duty_ns = 10^9 * (ps_divval * duty_cycles) / PWM_CLK_RATE 211 183 */ 212 184 static int ehrpwm_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 213 - u64 duty_ns, u64 period_ns) 185 + u64 duty_ns, u64 period_ns, enum pwm_polarity polarity) 214 186 { 215 187 struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip); 216 188 u32 period_cycles, duty_cycles; 217 189 u16 ps_divval, tb_divval; 218 190 unsigned int i, cmp_reg; 219 191 unsigned long long c; 192 + u16 aqctl_val, aqctl_mask; 193 + unsigned int aqctl_reg; 220 194 221 195 if (period_ns > NSEC_PER_SEC) 222 196 return -ERANGE; ··· 195 231 do_div(c, NSEC_PER_SEC); 196 232 period_cycles = (unsigned long)c; 197 233 198 - if (period_cycles < 1) { 199 - period_cycles = 1; 200 - duty_cycles = 1; 201 - } else { 202 - c = pc->clk_rate; 203 - c = c * duty_ns; 204 - do_div(c, NSEC_PER_SEC); 205 - duty_cycles = (unsigned long)c; 206 - } 234 + c = pc->clk_rate; 235 + c = c * duty_ns; 236 + do_div(c, NSEC_PER_SEC); 237 + duty_cycles = (unsigned long)c; 207 238 208 239 /* 209 240 * Period values should be same for multiple PWM channels as IP uses ··· 224 265 pc->period_cycles[pwm->hwpwm] = period_cycles; 225 266 226 267 /* Configure clock prescaler to support Low frequency PWM wave */ 227 - if (set_prescale_div(period_cycles/PERIOD_MAX, &ps_divval, 268 + if (set_prescale_div(DIV_ROUND_UP(period_cycles, PERIOD_MAX), &ps_divval, 228 269 &tb_divval)) { 229 270 dev_err(pwmchip_parent(chip), "Unsupported values\n"); 230 271 return -EINVAL; 231 272 } 273 + 274 + /* Update period & duty cycle with presacler division */ 275 + period_cycles = period_cycles / ps_divval; 276 + duty_cycles = duty_cycles / ps_divval; 277 + 278 + if (period_cycles < 1) 279 + period_cycles = 1; 232 280 233 281 pm_runtime_get_sync(pwmchip_parent(chip)); 234 282 235 283 /* Update clock prescaler values */ 236 284 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CLKDIV_MASK, tb_divval); 237 285 238 - /* Update period & duty cycle with presacler division */ 239 - period_cycles = period_cycles / ps_divval; 240 - duty_cycles = duty_cycles / ps_divval; 286 + if (pwm->hwpwm == 1) { 287 + /* Channel 1 configured with compare B register */ 288 + cmp_reg = CMPB; 289 + 290 + aqctl_reg = AQCTLB; 291 + aqctl_mask = AQCTL_CBU_MASK; 292 + 293 + if (polarity == PWM_POLARITY_INVERSED) 294 + aqctl_val = AQCTL_CHANB_POLINVERSED; 295 + else 296 + aqctl_val = AQCTL_CHANB_POLNORMAL; 297 + 298 + /* if duty_cycle is big, don't toggle on CBU */ 299 + if (duty_cycles > period_cycles) 300 + aqctl_val &= ~AQCTL_CBU_MASK; 301 + 302 + } else { 303 + /* Channel 0 configured with compare A register */ 304 + cmp_reg = CMPA; 305 + 306 + aqctl_reg = AQCTLA; 307 + aqctl_mask = AQCTL_CAU_MASK; 308 + 309 + if (polarity == PWM_POLARITY_INVERSED) 310 + aqctl_val = AQCTL_CHANA_POLINVERSED; 311 + else 312 + aqctl_val = AQCTL_CHANA_POLNORMAL; 313 + 314 + /* if duty_cycle is big, don't toggle on CAU */ 315 + if (duty_cycles > period_cycles) 316 + aqctl_val &= ~AQCTL_CAU_MASK; 317 + } 318 + 319 + aqctl_mask |= AQCTL_PRD_MASK | AQCTL_ZRO_MASK; 320 + ehrpwm_modify(pc->mmio_base, aqctl_reg, aqctl_mask, aqctl_val); 241 321 242 322 /* Configure shadow loading on Period register */ 243 323 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_PRDLD_MASK, TBCTL_PRDLD_SHDW); 244 324 245 - ehrpwm_write(pc->mmio_base, TBPRD, period_cycles); 325 + ehrpwm_write(pc->mmio_base, TBPRD, period_cycles - 1); 246 326 247 327 /* Configure ehrpwm counter for up-count mode */ 248 328 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CTRMODE_MASK, 249 329 TBCTL_CTRMODE_UP); 250 330 251 - if (pwm->hwpwm == 1) 252 - /* Channel 1 configured with compare B register */ 253 - cmp_reg = CMPB; 254 - else 255 - /* Channel 0 configured with compare A register */ 256 - cmp_reg = CMPA; 257 - 258 - ehrpwm_write(pc->mmio_base, cmp_reg, duty_cycles); 331 + if (!(duty_cycles > period_cycles)) 332 + ehrpwm_write(pc->mmio_base, cmp_reg, duty_cycles); 259 333 260 334 pm_runtime_put_sync(pwmchip_parent(chip)); 261 - 262 - return 0; 263 - } 264 - 265 - static int ehrpwm_pwm_set_polarity(struct pwm_chip *chip, 266 - struct pwm_device *pwm, 267 - enum pwm_polarity polarity) 268 - { 269 - struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip); 270 - 271 - /* Configuration of polarity in hardware delayed, do at enable */ 272 - pc->polarity[pwm->hwpwm] = polarity; 273 335 274 336 return 0; 275 337 } ··· 318 338 AQSFRC_RLDCSF_ZRO); 319 339 320 340 ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val); 321 - 322 - /* Channels polarity can be configured from action qualifier module */ 323 - configure_polarity(pc, pwm->hwpwm); 324 341 325 342 /* Enable TBCLK */ 326 343 ret = clk_enable(pc->tbclk); ··· 368 391 { 369 392 struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip); 370 393 371 - if (pwm_is_enabled(pwm)) { 372 - dev_warn(pwmchip_parent(chip), "Removing PWM device without disabling\n"); 373 - pm_runtime_put_sync(pwmchip_parent(chip)); 374 - } 375 - 376 - /* set period value to zero on free */ 394 + /* Don't let a pwm without consumer block requests to the other channel */ 377 395 pc->period_cycles[pwm->hwpwm] = 0; 378 396 } 379 397 ··· 383 411 ehrpwm_pwm_disable(chip, pwm); 384 412 enabled = false; 385 413 } 386 - 387 - err = ehrpwm_pwm_set_polarity(chip, pwm, state->polarity); 388 - if (err) 389 - return err; 390 414 } 391 415 392 416 if (!state->enabled) { ··· 391 423 return 0; 392 424 } 393 425 394 - err = ehrpwm_pwm_config(chip, pwm, state->duty_cycle, state->period); 426 + err = ehrpwm_pwm_config(chip, pwm, state->duty_cycle, state->period, state->polarity); 395 427 if (err) 396 428 return err; 397 429
+3
include/linux/pwm.h
··· 5 5 #include <linux/cdev.h> 6 6 #include <linux/device.h> 7 7 #include <linux/err.h> 8 + #include <linux/gpio/driver.h> 8 9 #include <linux/module.h> 9 10 #include <linux/mutex.h> 10 11 #include <linux/of.h> ··· 322 321 * @npwm: number of PWMs controlled by this chip 323 322 * @of_xlate: request a PWM device given a device tree PWM specifier 324 323 * @atomic: can the driver's ->apply() be called in atomic context 324 + * @gpio: &struct gpio_chip to operate this PWM chip's lines as GPO 325 325 * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip 326 326 * @operational: signals if the chip can be used (or is already deregistered) 327 327 * @nonatomic_lock: mutex for nonatomic chips ··· 342 340 bool atomic; 343 341 344 342 /* only used internally by the PWM framework */ 343 + struct gpio_chip gpio; 345 344 bool uses_pwmchip_alloc; 346 345 bool operational; 347 346 union {