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

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

Pull pwm updates from Thierry Reding:
"This adds support for the PWM controller found on Toshiba Visconti
SoCs and converts a couple of drivers to the atomic API.

There's also a bunch of cleanups and minor fixes across the board"

* tag 'pwm/for-5.13-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry.reding/linux-pwm: (35 commits)
pwm: Reword docs about pwm_apply_state()
pwm: atmel: Improve duty cycle calculation in .apply()
pwm: atmel: Fix duty cycle calculation in .get_state()
pwm: visconti: Add Toshiba Visconti SoC PWM support
dt-bindings: pwm: Add bindings for Toshiba Visconti PWM Controller
arm64: dts: rockchip: Remove clock-names from PWM nodes
ARM: dts: rockchip: Remove clock-names from PWM nodes
dt-bindings: pwm: rockchip: Add more compatible strings
dt-bindings: pwm: Convert pwm-rockchip.txt to YAML
pwm: mediatek: Remove unused function
pwm: pca9685: Improve runtime PM behavior
pwm: pca9685: Support hardware readout
pwm: pca9685: Switch to atomic API
pwm: lpss: Don't modify HW state in .remove callback
pwm: sti: Free resources only after pwmchip_remove()
pwm: sti: Don't modify HW state in .remove callback
pwm: lpc3200: Don't modify HW state in .remove callback
pwm: lpc18xx-sct: Free resources only after pwmchip_remove()
pwm: bcm-kona: Don't modify HW state in .remove callback
pwm: bcm2835: Free resources only after pwmchip_remove()
...

+634 -410
-27
Documentation/devicetree/bindings/pwm/pwm-rockchip.txt
··· 1 - Rockchip PWM controller 2 - 3 - Required properties: 4 - - compatible: should be "rockchip,<name>-pwm" 5 - "rockchip,rk2928-pwm": found on RK29XX,RK3066 and RK3188 SoCs 6 - "rockchip,rk3288-pwm": found on RK3288 SOC 7 - "rockchip,rv1108-pwm", "rockchip,rk3288-pwm": found on RV1108 SoC 8 - "rockchip,vop-pwm": found integrated in VOP on RK3288 SoC 9 - - reg: physical base address and length of the controller's registers 10 - - clocks: See ../clock/clock-bindings.txt 11 - - For older hardware (rk2928, rk3066, rk3188, rk3228, rk3288, rk3399): 12 - - There is one clock that's used both to derive the functional clock 13 - for the device and as the bus clock. 14 - - For newer hardware (rk3328 and future socs): specified by name 15 - - "pwm": This is used to derive the functional clock. 16 - - "pclk": This is the APB bus clock. 17 - - #pwm-cells: must be 2 (rk2928) or 3 (rk3288). See pwm.yaml in this directory 18 - for a description of the cell format. 19 - 20 - Example: 21 - 22 - pwm0: pwm@20030000 { 23 - compatible = "rockchip,rk2928-pwm"; 24 - reg = <0x20030000 0x10>; 25 - clocks = <&cru PCLK_PWM01>; 26 - #pwm-cells = <2>; 27 - };
+100
Documentation/devicetree/bindings/pwm/pwm-rockchip.yaml
··· 1 + # SPDX-License-Identifier: GPL-2.0 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/pwm/pwm-rockchip.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Rockchip PWM controller 8 + 9 + maintainers: 10 + - Heiko Stuebner <heiko@sntech.de> 11 + 12 + properties: 13 + compatible: 14 + oneOf: 15 + - const: rockchip,rk2928-pwm 16 + - const: rockchip,rk3288-pwm 17 + - const: rockchip,rk3328-pwm 18 + - const: rockchip,vop-pwm 19 + - items: 20 + - const: rockchip,rk3036-pwm 21 + - const: rockchip,rk2928-pwm 22 + - items: 23 + - enum: 24 + - rockchip,rk3368-pwm 25 + - rockchip,rk3399-pwm 26 + - rockchip,rv1108-pwm 27 + - const: rockchip,rk3288-pwm 28 + - items: 29 + - enum: 30 + - rockchip,px30-pwm 31 + - rockchip,rk3308-pwm 32 + - const: rockchip,rk3328-pwm 33 + 34 + reg: 35 + maxItems: 1 36 + 37 + clocks: 38 + minItems: 1 39 + maxItems: 2 40 + 41 + clock-names: 42 + maxItems: 2 43 + 44 + "#pwm-cells": 45 + enum: [2, 3] 46 + description: 47 + Must be 2 (rk2928) or 3 (rk3288 and later). 48 + See pwm.yaml for a description of the cell format. 49 + 50 + required: 51 + - compatible 52 + - reg 53 + - "#pwm-cells" 54 + 55 + if: 56 + properties: 57 + compatible: 58 + contains: 59 + enum: 60 + - rockchip,rk3328-pwm 61 + - rockchip,rv1108-pwm 62 + 63 + then: 64 + properties: 65 + clocks: 66 + items: 67 + - description: Used to derive the functional clock for the device. 68 + - description: Used as the APB bus clock. 69 + 70 + clock-names: 71 + items: 72 + - const: pwm 73 + - const: pclk 74 + 75 + required: 76 + - clocks 77 + - clock-names 78 + 79 + else: 80 + properties: 81 + clocks: 82 + maxItems: 1 83 + description: 84 + Used both to derive the functional clock 85 + for the device and as the bus clock. 86 + 87 + required: 88 + - clocks 89 + 90 + additionalProperties: false 91 + 92 + examples: 93 + - | 94 + #include <dt-bindings/clock/rk3188-cru-common.h> 95 + pwm0: pwm@20030000 { 96 + compatible = "rockchip,rk2928-pwm"; 97 + reg = <0x20030000 0x10>; 98 + clocks = <&cru PCLK_PWM01>; 99 + #pwm-cells = <2>; 100 + };
+43
Documentation/devicetree/bindings/pwm/toshiba,pwm-visconti.yaml
··· 1 + # SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/pwm/toshiba,pwm-visconti.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Toshiba Visconti PWM Controller 8 + 9 + maintainers: 10 + - Nobuhiro Iwamatsu <nobuhiro1.iwamatsu@toshiba.co.jp> 11 + 12 + properties: 13 + compatible: 14 + items: 15 + - const: toshiba,visconti-pwm 16 + 17 + reg: 18 + maxItems: 1 19 + 20 + '#pwm-cells': 21 + const: 2 22 + 23 + required: 24 + - compatible 25 + - reg 26 + - '#pwm-cells' 27 + 28 + additionalProperties: false 29 + 30 + examples: 31 + - | 32 + soc { 33 + #address-cells = <2>; 34 + #size-cells = <2>; 35 + 36 + pwm: pwm@241c0000 { 37 + compatible = "toshiba,visconti-pwm"; 38 + reg = <0 0x241c0000 0 0x1000>; 39 + pinctrl-names = "default"; 40 + pinctrl-0 = <&pwm_mux>; 41 + #pwm-cells = <2>; 42 + }; 43 + };
+5 -1
Documentation/driver-api/pwm.rst
··· 55 55 pwm_{enable,disable}() calls in the same function, this probably means you 56 56 should switch to pwm_apply_state(). 57 57 58 - The PWM user API also allows one to query the PWM state with pwm_get_state(). 58 + The PWM user API also allows one to query the PWM state that was passed to the 59 + last invocation of pwm_apply_state() using pwm_get_state(). Note this is 60 + different to what the driver has actually implemented if the request cannot be 61 + satisfied exactly with the hardware in use. There is currently no way for 62 + consumers to get the actually implemented settings. 59 63 60 64 In addition to the PWM state, the PWM API also exposes PWM arguments, which 61 65 are the reference PWM config one should use on this PWM.
-4
arch/arm/boot/dts/rk3036.dtsi
··· 355 355 reg = <0x20050000 0x10>; 356 356 #pwm-cells = <3>; 357 357 clocks = <&cru PCLK_PWM>; 358 - clock-names = "pwm"; 359 358 pinctrl-names = "default"; 360 359 pinctrl-0 = <&pwm0_pin>; 361 360 status = "disabled"; ··· 365 366 reg = <0x20050010 0x10>; 366 367 #pwm-cells = <3>; 367 368 clocks = <&cru PCLK_PWM>; 368 - clock-names = "pwm"; 369 369 pinctrl-names = "default"; 370 370 pinctrl-0 = <&pwm1_pin>; 371 371 status = "disabled"; ··· 375 377 reg = <0x20050020 0x10>; 376 378 #pwm-cells = <3>; 377 379 clocks = <&cru PCLK_PWM>; 378 - clock-names = "pwm"; 379 380 pinctrl-names = "default"; 380 381 pinctrl-0 = <&pwm2_pin>; 381 382 status = "disabled"; ··· 385 388 reg = <0x20050030 0x10>; 386 389 #pwm-cells = <2>; 387 390 clocks = <&cru PCLK_PWM>; 388 - clock-names = "pwm"; 389 391 pinctrl-names = "default"; 390 392 pinctrl-0 = <&pwm3_pin>; 391 393 status = "disabled";
-4
arch/arm/boot/dts/rk3288.dtsi
··· 679 679 pinctrl-names = "default"; 680 680 pinctrl-0 = <&pwm0_pin>; 681 681 clocks = <&cru PCLK_RKPWM>; 682 - clock-names = "pwm"; 683 682 status = "disabled"; 684 683 }; 685 684 ··· 689 690 pinctrl-names = "default"; 690 691 pinctrl-0 = <&pwm1_pin>; 691 692 clocks = <&cru PCLK_RKPWM>; 692 - clock-names = "pwm"; 693 693 status = "disabled"; 694 694 }; 695 695 ··· 699 701 pinctrl-names = "default"; 700 702 pinctrl-0 = <&pwm2_pin>; 701 703 clocks = <&cru PCLK_RKPWM>; 702 - clock-names = "pwm"; 703 704 status = "disabled"; 704 705 }; 705 706 ··· 709 712 pinctrl-names = "default"; 710 713 pinctrl-0 = <&pwm3_pin>; 711 714 clocks = <&cru PCLK_RKPWM>; 712 - clock-names = "pwm"; 713 715 status = "disabled"; 714 716 }; 715 717
-4
arch/arm64/boot/dts/rockchip/rk3368.dtsi
··· 558 558 pinctrl-names = "default"; 559 559 pinctrl-0 = <&pwm0_pin>; 560 560 clocks = <&cru PCLK_PWM1>; 561 - clock-names = "pwm"; 562 561 status = "disabled"; 563 562 }; 564 563 ··· 568 569 pinctrl-names = "default"; 569 570 pinctrl-0 = <&pwm1_pin>; 570 571 clocks = <&cru PCLK_PWM1>; 571 - clock-names = "pwm"; 572 572 status = "disabled"; 573 573 }; 574 574 ··· 576 578 reg = <0x0 0xff680020 0x0 0x10>; 577 579 #pwm-cells = <3>; 578 580 clocks = <&cru PCLK_PWM1>; 579 - clock-names = "pwm"; 580 581 status = "disabled"; 581 582 }; 582 583 ··· 586 589 pinctrl-names = "default"; 587 590 pinctrl-0 = <&pwm3_pin>; 588 591 clocks = <&cru PCLK_PWM1>; 589 - clock-names = "pwm"; 590 592 status = "disabled"; 591 593 }; 592 594
-4
arch/arm64/boot/dts/rockchip/rk3399.dtsi
··· 1182 1182 pinctrl-names = "default"; 1183 1183 pinctrl-0 = <&pwm0_pin>; 1184 1184 clocks = <&pmucru PCLK_RKPWM_PMU>; 1185 - clock-names = "pwm"; 1186 1185 status = "disabled"; 1187 1186 }; 1188 1187 ··· 1192 1193 pinctrl-names = "default"; 1193 1194 pinctrl-0 = <&pwm1_pin>; 1194 1195 clocks = <&pmucru PCLK_RKPWM_PMU>; 1195 - clock-names = "pwm"; 1196 1196 status = "disabled"; 1197 1197 }; 1198 1198 ··· 1202 1204 pinctrl-names = "default"; 1203 1205 pinctrl-0 = <&pwm2_pin>; 1204 1206 clocks = <&pmucru PCLK_RKPWM_PMU>; 1205 - clock-names = "pwm"; 1206 1207 status = "disabled"; 1207 1208 }; 1208 1209 ··· 1212 1215 pinctrl-names = "default"; 1213 1216 pinctrl-0 = <&pwm3a_pin>; 1214 1217 clocks = <&pmucru PCLK_RKPWM_PMU>; 1215 - clock-names = "pwm"; 1216 1218 status = "disabled"; 1217 1219 }; 1218 1220
+9
drivers/pwm/Kconfig
··· 618 618 To compile this driver as a module, choose M here: the module 619 619 will be called pwm-twl-led. 620 620 621 + config PWM_VISCONTI 622 + tristate "Toshiba Visconti PWM support" 623 + depends on ARCH_VISCONTI || COMPILE_TEST 624 + help 625 + PWM Subsystem driver support for Toshiba Visconti SoCs. 626 + 627 + To compile this driver as a module, choose M here: the module 628 + will be called pwm-visconti. 629 + 621 630 config PWM_VT8500 622 631 tristate "vt8500 PWM support" 623 632 depends on ARCH_VT8500 || COMPILE_TEST
+1
drivers/pwm/Makefile
··· 58 58 obj-$(CONFIG_PWM_TIEHRPWM) += pwm-tiehrpwm.o 59 59 obj-$(CONFIG_PWM_TWL) += pwm-twl.o 60 60 obj-$(CONFIG_PWM_TWL_LED) += pwm-twl-led.o 61 + obj-$(CONFIG_PWM_VISCONTI) += pwm-visconti.o 61 62 obj-$(CONFIG_PWM_VT8500) += pwm-vt8500.o
+9 -39
drivers/pwm/core.c
··· 37 37 return radix_tree_lookup(&pwm_tree, pwm); 38 38 } 39 39 40 - static int alloc_pwms(int pwm, unsigned int count) 40 + static int alloc_pwms(unsigned int count) 41 41 { 42 - unsigned int from = 0; 43 42 unsigned int start; 44 43 45 - if (pwm >= MAX_PWMS) 46 - return -EINVAL; 47 - 48 - if (pwm >= 0) 49 - from = pwm; 50 - 51 - start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, from, 44 + start = bitmap_find_next_zero_area(allocated_pwms, MAX_PWMS, 0, 52 45 count, 0); 53 - 54 - if (pwm >= 0 && start != pwm) 55 - return -EEXIST; 56 46 57 47 if (start + count > MAX_PWMS) 58 48 return -ENOSPC; ··· 250 260 } 251 261 252 262 /** 253 - * pwmchip_add_with_polarity() - register a new PWM chip 263 + * pwmchip_add() - register a new PWM chip 254 264 * @chip: the PWM chip to add 255 - * @polarity: initial polarity of PWM channels 256 265 * 257 - * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base 258 - * will be used. The initial polarity for all channels is specified by the 259 - * @polarity parameter. 266 + * Register a new PWM chip. 260 267 * 261 268 * Returns: 0 on success or a negative error code on failure. 262 269 */ 263 - int pwmchip_add_with_polarity(struct pwm_chip *chip, 264 - enum pwm_polarity polarity) 270 + int pwmchip_add(struct pwm_chip *chip) 265 271 { 266 272 struct pwm_device *pwm; 267 273 unsigned int i; ··· 271 285 272 286 mutex_lock(&pwm_lock); 273 287 274 - ret = alloc_pwms(chip->base, chip->npwm); 288 + ret = alloc_pwms(chip->npwm); 275 289 if (ret < 0) 276 290 goto out; 291 + 292 + chip->base = ret; 277 293 278 294 chip->pwms = kcalloc(chip->npwm, sizeof(*pwm), GFP_KERNEL); 279 295 if (!chip->pwms) { ··· 283 295 goto out; 284 296 } 285 297 286 - chip->base = ret; 287 - 288 298 for (i = 0; i < chip->npwm; i++) { 289 299 pwm = &chip->pwms[i]; 290 300 291 301 pwm->chip = chip; 292 302 pwm->pwm = chip->base + i; 293 303 pwm->hwpwm = i; 294 - pwm->state.polarity = polarity; 295 304 296 305 radix_tree_insert(&pwm_tree, pwm->pwm, pwm); 297 306 } ··· 310 325 pwmchip_sysfs_export(chip); 311 326 312 327 return ret; 313 - } 314 - EXPORT_SYMBOL_GPL(pwmchip_add_with_polarity); 315 - 316 - /** 317 - * pwmchip_add() - register a new PWM chip 318 - * @chip: the PWM chip to add 319 - * 320 - * Register a new PWM chip. If chip->base < 0 then a dynamically assigned base 321 - * will be used. The initial polarity for all channels is normal. 322 - * 323 - * Returns: 0 on success or a negative error code on failure. 324 - */ 325 - int pwmchip_add(struct pwm_chip *chip) 326 - { 327 - return pwmchip_add_with_polarity(chip, PWM_POLARITY_NORMAL); 328 328 } 329 329 EXPORT_SYMBOL_GPL(pwmchip_add); 330 330 ··· 577 607 */ 578 608 if (state->polarity != pwm->state.polarity) { 579 609 if (!chip->ops->set_polarity) 580 - return -ENOTSUPP; 610 + return -EINVAL; 581 611 582 612 /* 583 613 * Changing the polarity of a running PWM is
+25 -29
drivers/pwm/pwm-ab8500.c
··· 24 24 struct pwm_chip chip; 25 25 }; 26 26 27 - static int ab8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 28 - int duty_ns, int period_ns) 27 + static int ab8500_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 28 + const struct pwm_state *state) 29 29 { 30 - int ret = 0; 31 - unsigned int higher_val, lower_val; 30 + int ret; 32 31 u8 reg; 32 + unsigned int higher_val, lower_val; 33 + 34 + if (state->polarity != PWM_POLARITY_NORMAL) 35 + return -EINVAL; 36 + 37 + if (!state->enabled) { 38 + ret = abx500_mask_and_set_register_interruptible(chip->dev, 39 + AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG, 40 + 1 << (chip->base - 1), 0); 41 + 42 + if (ret < 0) 43 + dev_err(chip->dev, "%s: Failed to disable PWM, Error %d\n", 44 + pwm->label, ret); 45 + return ret; 46 + } 33 47 34 48 /* 35 49 * get the first 8 bits that are be written to 36 50 * AB8500_PWM_OUT_CTRL1_REG[0:7] 37 51 */ 38 - lower_val = duty_ns & 0x00FF; 52 + lower_val = state->duty_cycle & 0x00FF; 39 53 /* 40 54 * get bits [9:10] that are to be written to 41 55 * AB8500_PWM_OUT_CTRL2_REG[0:1] 42 56 */ 43 - higher_val = ((duty_ns & 0x0300) >> 8); 57 + higher_val = ((state->duty_cycle & 0x0300) >> 8); 44 58 45 59 reg = AB8500_PWM_OUT_CTRL1_REG + ((chip->base - 1) * 2); 46 60 ··· 62 48 reg, (u8)lower_val); 63 49 if (ret < 0) 64 50 return ret; 51 + 65 52 ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC, 66 53 (reg + 1), (u8)higher_val); 67 - 68 - return ret; 69 - } 70 - 71 - static int ab8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) 72 - { 73 - int ret; 54 + if (ret < 0) 55 + return ret; 74 56 75 57 ret = abx500_mask_and_set_register_interruptible(chip->dev, 76 58 AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG, ··· 74 64 if (ret < 0) 75 65 dev_err(chip->dev, "%s: Failed to enable PWM, Error %d\n", 76 66 pwm->label, ret); 67 + 77 68 return ret; 78 69 } 79 70 80 - static void ab8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) 81 - { 82 - int ret; 83 - 84 - ret = abx500_mask_and_set_register_interruptible(chip->dev, 85 - AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG, 86 - 1 << (chip->base - 1), 0); 87 - if (ret < 0) 88 - dev_err(chip->dev, "%s: Failed to disable PWM, Error %d\n", 89 - pwm->label, ret); 90 - } 91 - 92 71 static const struct pwm_ops ab8500_pwm_ops = { 93 - .config = ab8500_pwm_config, 94 - .enable = ab8500_pwm_enable, 95 - .disable = ab8500_pwm_disable, 72 + .apply = ab8500_pwm_apply, 96 73 .owner = THIS_MODULE, 97 74 }; 98 75 ··· 98 101 99 102 ab8500->chip.dev = &pdev->dev; 100 103 ab8500->chip.ops = &ab8500_pwm_ops; 101 - ab8500->chip.base = -1; 102 104 ab8500->chip.npwm = 1; 103 105 104 106 err = pwmchip_add(&ab8500->chip);
+1 -2
drivers/pwm/pwm-atmel-hlcdc.c
··· 265 265 chip->hlcdc = hlcdc; 266 266 chip->chip.ops = &atmel_hlcdc_pwm_ops; 267 267 chip->chip.dev = dev; 268 - chip->chip.base = -1; 269 268 chip->chip.npwm = 1; 270 269 chip->chip.of_xlate = of_pwm_xlate_with_flags; 271 270 chip->chip.of_pwm_n_cells = 3; 272 271 273 - ret = pwmchip_add_with_polarity(&chip->chip, PWM_POLARITY_INVERSED); 272 + ret = pwmchip_add(&chip->chip); 274 273 if (ret) { 275 274 clk_disable_unprepare(hlcdc->periph_clk); 276 275 return ret;
+29 -13
drivers/pwm/pwm-atmel-tcb.c
··· 362 362 tcbpwm->div = i; 363 363 tcbpwm->duty = duty; 364 364 365 - /* If the PWM is enabled, call enable to apply the new conf */ 366 - if (pwm_is_enabled(pwm)) 367 - atmel_tcb_pwm_enable(chip, pwm); 368 - 369 365 return 0; 366 + } 367 + 368 + static int atmel_tcb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 369 + const struct pwm_state *state) 370 + { 371 + int duty_cycle, period; 372 + int ret; 373 + 374 + /* This function only sets a flag in driver data */ 375 + atmel_tcb_pwm_set_polarity(chip, pwm, state->polarity); 376 + 377 + if (!state->enabled) { 378 + atmel_tcb_pwm_disable(chip, pwm); 379 + return 0; 380 + } 381 + 382 + period = state->period < INT_MAX ? state->period : INT_MAX; 383 + duty_cycle = state->duty_cycle < INT_MAX ? state->duty_cycle : INT_MAX; 384 + 385 + ret = atmel_tcb_pwm_config(chip, pwm, duty_cycle, period); 386 + if (ret) 387 + return ret; 388 + 389 + return atmel_tcb_pwm_enable(chip, pwm); 370 390 } 371 391 372 392 static const struct pwm_ops atmel_tcb_pwm_ops = { 373 393 .request = atmel_tcb_pwm_request, 374 394 .free = atmel_tcb_pwm_free, 375 - .config = atmel_tcb_pwm_config, 376 - .set_polarity = atmel_tcb_pwm_set_polarity, 377 - .enable = atmel_tcb_pwm_enable, 378 - .disable = atmel_tcb_pwm_disable, 395 + .apply = atmel_tcb_pwm_apply, 379 396 .owner = THIS_MODULE, 380 397 }; 381 398 ··· 471 454 tcbpwm->chip.ops = &atmel_tcb_pwm_ops; 472 455 tcbpwm->chip.of_xlate = of_pwm_xlate_with_flags; 473 456 tcbpwm->chip.of_pwm_n_cells = 3; 474 - tcbpwm->chip.base = -1; 475 457 tcbpwm->chip.npwm = NPWM; 476 458 tcbpwm->channel = channel; 477 459 tcbpwm->regmap = regmap; ··· 507 491 struct atmel_tcb_pwm_chip *tcbpwm = platform_get_drvdata(pdev); 508 492 int err; 509 493 510 - clk_disable_unprepare(tcbpwm->slow_clk); 511 - clk_put(tcbpwm->slow_clk); 512 - clk_put(tcbpwm->clk); 513 - 514 494 err = pwmchip_remove(&tcbpwm->chip); 515 495 if (err < 0) 516 496 return err; 497 + 498 + clk_disable_unprepare(tcbpwm->slow_clk); 499 + clk_put(tcbpwm->slow_clk); 500 + clk_put(tcbpwm->clk); 517 501 518 502 return 0; 519 503 }
+20 -10
drivers/pwm/pwm-atmel.c
··· 124 124 } 125 125 126 126 static int atmel_pwm_calculate_cprd_and_pres(struct pwm_chip *chip, 127 + unsigned long clkrate, 127 128 const struct pwm_state *state, 128 129 unsigned long *cprd, u32 *pres) 129 130 { ··· 133 132 int shift; 134 133 135 134 /* Calculate the period cycles and prescale value */ 136 - cycles *= clk_get_rate(atmel_pwm->clk); 135 + cycles *= clkrate; 137 136 do_div(cycles, NSEC_PER_SEC); 138 137 139 138 /* ··· 159 158 } 160 159 161 160 static void atmel_pwm_calculate_cdty(const struct pwm_state *state, 162 - unsigned long cprd, unsigned long *cdty) 161 + unsigned long clkrate, unsigned long cprd, 162 + u32 pres, unsigned long *cdty) 163 163 { 164 164 unsigned long long cycles = state->duty_cycle; 165 165 166 - cycles *= cprd; 167 - do_div(cycles, state->period); 166 + cycles *= clkrate; 167 + do_div(cycles, NSEC_PER_SEC); 168 + cycles >>= pres; 168 169 *cdty = cprd - cycles; 169 170 } 170 171 ··· 247 244 pwm_get_state(pwm, &cstate); 248 245 249 246 if (state->enabled) { 247 + unsigned long clkrate = clk_get_rate(atmel_pwm->clk); 248 + 250 249 if (cstate.enabled && 251 250 cstate.polarity == state->polarity && 252 251 cstate.period == state->period) { 252 + u32 cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR); 253 + 253 254 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, 254 255 atmel_pwm->data->regs.period); 255 - atmel_pwm_calculate_cdty(state, cprd, &cdty); 256 + pres = cmr & PWM_CMR_CPRE_MSK; 257 + 258 + atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty); 256 259 atmel_pwm_update_cdty(chip, pwm, cdty); 257 260 return 0; 258 261 } 259 262 260 - ret = atmel_pwm_calculate_cprd_and_pres(chip, state, &cprd, 263 + ret = atmel_pwm_calculate_cprd_and_pres(chip, clkrate, state, &cprd, 261 264 &pres); 262 265 if (ret) { 263 266 dev_err(chip->dev, ··· 271 262 return ret; 272 263 } 273 264 274 - atmel_pwm_calculate_cdty(state, cprd, &cdty); 265 + atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty); 275 266 276 267 if (cstate.enabled) { 277 268 atmel_pwm_disable(chip, pwm, false); ··· 328 319 329 320 cdty = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, 330 321 atmel_pwm->data->regs.duty); 331 - tmp = (u64)cdty * NSEC_PER_SEC; 322 + tmp = (u64)(cprd - cdty) * NSEC_PER_SEC; 332 323 tmp <<= pres; 333 324 state->duty_cycle = DIV64_U64_ROUND_UP(tmp, rate); 334 325 ··· 438 429 atmel_pwm->chip.ops = &atmel_pwm_ops; 439 430 atmel_pwm->chip.of_xlate = of_pwm_xlate_with_flags; 440 431 atmel_pwm->chip.of_pwm_n_cells = 3; 441 - atmel_pwm->chip.base = -1; 442 432 atmel_pwm->chip.npwm = 4; 443 433 444 434 ret = pwmchip_add(&atmel_pwm->chip); ··· 459 451 { 460 452 struct atmel_pwm_chip *atmel_pwm = platform_get_drvdata(pdev); 461 453 454 + pwmchip_remove(&atmel_pwm->chip); 455 + 462 456 clk_unprepare(atmel_pwm->clk); 463 457 mutex_destroy(&atmel_pwm->isr_lock); 464 458 465 - return pwmchip_remove(&atmel_pwm->chip); 459 + return 0; 466 460 } 467 461 468 462 static struct platform_driver atmel_pwm_driver = {
+3 -2
drivers/pwm/pwm-bcm-iproc.c
··· 209 209 210 210 ip->chip.dev = &pdev->dev; 211 211 ip->chip.ops = &iproc_pwm_ops; 212 - ip->chip.base = -1; 213 212 ip->chip.npwm = 4; 214 213 ip->chip.of_xlate = of_pwm_xlate_with_flags; 215 214 ip->chip.of_pwm_n_cells = 3; ··· 253 254 { 254 255 struct iproc_pwmc *ip = platform_get_drvdata(pdev); 255 256 257 + pwmchip_remove(&ip->chip); 258 + 256 259 clk_disable_unprepare(ip->clk); 257 260 258 - return pwmchip_remove(&ip->chip); 261 + return 0; 259 262 } 260 263 261 264 static const struct of_device_id bcm_iproc_pwmc_dt[] = {
+1 -7
drivers/pwm/pwm-bcm-kona.c
··· 271 271 272 272 kp->chip.dev = &pdev->dev; 273 273 kp->chip.ops = &kona_pwm_ops; 274 - kp->chip.base = -1; 275 274 kp->chip.npwm = 6; 276 275 kp->chip.of_xlate = of_pwm_xlate_with_flags; 277 276 kp->chip.of_pwm_n_cells = 3; ··· 300 301 301 302 clk_disable_unprepare(kp->clk); 302 303 303 - ret = pwmchip_add_with_polarity(&kp->chip, PWM_POLARITY_INVERSED); 304 + ret = pwmchip_add(&kp->chip); 304 305 if (ret < 0) 305 306 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret); 306 307 ··· 310 311 static int kona_pwmc_remove(struct platform_device *pdev) 311 312 { 312 313 struct kona_pwmc *kp = platform_get_drvdata(pdev); 313 - unsigned int chan; 314 - 315 - for (chan = 0; chan < kp->chip.npwm; chan++) 316 - if (pwm_is_enabled(&kp->chip.pwms[chan])) 317 - clk_disable_unprepare(kp->clk); 318 314 319 315 return pwmchip_remove(&kp->chip); 320 316 }
+31 -11
drivers/pwm/pwm-bcm2835.c
··· 64 64 65 65 struct bcm2835_pwm *pc = to_bcm2835_pwm(chip); 66 66 unsigned long rate = clk_get_rate(pc->clk); 67 - unsigned long long period; 68 - unsigned long scaler; 67 + unsigned long long period_cycles; 68 + u64 max_period; 69 + 69 70 u32 val; 70 71 71 72 if (!rate) { ··· 74 73 return -EINVAL; 75 74 } 76 75 77 - scaler = DIV_ROUND_CLOSEST(NSEC_PER_SEC, rate); 78 - /* set period */ 79 - period = DIV_ROUND_CLOSEST_ULL(state->period, scaler); 76 + /* 77 + * period_cycles must be a 32 bit value, so period * rate / NSEC_PER_SEC 78 + * must be <= U32_MAX. As U32_MAX * NSEC_PER_SEC < U64_MAX the 79 + * multiplication period * rate doesn't overflow. 80 + * To calculate the maximal possible period that guarantees the 81 + * above inequality: 82 + * 83 + * round(period * rate / NSEC_PER_SEC) <= U32_MAX 84 + * <=> period * rate / NSEC_PER_SEC < U32_MAX + 0.5 85 + * <=> period * rate < (U32_MAX + 0.5) * NSEC_PER_SEC 86 + * <=> period < ((U32_MAX + 0.5) * NSEC_PER_SEC) / rate 87 + * <=> period < ((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate 88 + * <=> period <= ceil((U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC/2) / rate) - 1 89 + */ 90 + max_period = DIV_ROUND_UP_ULL((u64)U32_MAX * NSEC_PER_SEC + NSEC_PER_SEC / 2, rate) - 1; 80 91 81 - /* dont accept a period that is too small or has been truncated */ 82 - if ((period < PERIOD_MIN) || (period > U32_MAX)) 92 + if (state->period > max_period) 83 93 return -EINVAL; 84 94 85 - writel(period, pc->base + PERIOD(pwm->hwpwm)); 95 + /* set period */ 96 + period_cycles = DIV_ROUND_CLOSEST_ULL(state->period * rate, NSEC_PER_SEC); 97 + 98 + /* don't accept a period that is too small */ 99 + if (period_cycles < PERIOD_MIN) 100 + return -EINVAL; 101 + 102 + writel(period_cycles, pc->base + PERIOD(pwm->hwpwm)); 86 103 87 104 /* set duty cycle */ 88 - val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle, scaler); 105 + val = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * rate, NSEC_PER_SEC); 89 106 writel(val, pc->base + DUTY(pwm->hwpwm)); 90 107 91 108 /* set polarity */ ··· 158 139 159 140 pc->chip.dev = &pdev->dev; 160 141 pc->chip.ops = &bcm2835_pwm_ops; 161 - pc->chip.base = -1; 162 142 pc->chip.npwm = 2; 163 143 pc->chip.of_xlate = of_pwm_xlate_with_flags; 164 144 pc->chip.of_pwm_n_cells = 3; ··· 179 161 { 180 162 struct bcm2835_pwm *pc = platform_get_drvdata(pdev); 181 163 164 + pwmchip_remove(&pc->chip); 165 + 182 166 clk_disable_unprepare(pc->clk); 183 167 184 - return pwmchip_remove(&pc->chip); 168 + return 0; 185 169 } 186 170 187 171 static const struct of_device_id bcm2835_pwm_of_match[] = {
-1
drivers/pwm/pwm-berlin.c
··· 206 206 207 207 pwm->chip.dev = &pdev->dev; 208 208 pwm->chip.ops = &berlin_pwm_ops; 209 - pwm->chip.base = -1; 210 209 pwm->chip.npwm = 4; 211 210 pwm->chip.of_xlate = of_pwm_xlate_with_flags; 212 211 pwm->chip.of_pwm_n_cells = 3;
-1
drivers/pwm/pwm-brcmstb.c
··· 258 258 259 259 p->chip.dev = &pdev->dev; 260 260 p->chip.ops = &brcmstb_pwm_ops; 261 - p->chip.base = -1; 262 261 p->chip.npwm = 2; 263 262 264 263 p->base = devm_platform_ioremap_resource(pdev, 0);
-1
drivers/pwm/pwm-clps711x.c
··· 128 128 129 129 priv->chip.ops = &clps711x_pwm_ops; 130 130 priv->chip.dev = &pdev->dev; 131 - priv->chip.base = -1; 132 131 priv->chip.npwm = 2; 133 132 priv->chip.of_xlate = clps711x_pwm_xlate; 134 133 priv->chip.of_pwm_n_cells = 1;
-1
drivers/pwm/pwm-crc.c
··· 168 168 169 169 pwm->chip.dev = &pdev->dev; 170 170 pwm->chip.ops = &crc_pwm_ops; 171 - pwm->chip.base = -1; 172 171 pwm->chip.npwm = 1; 173 172 174 173 /* get the PMIC regmap */
+3 -1
drivers/pwm/pwm-cros-ec.c
··· 124 124 if (state->period != EC_PWM_MAX_DUTY) 125 125 return -EINVAL; 126 126 127 + if (state->polarity != PWM_POLARITY_NORMAL) 128 + return -EINVAL; 129 + 127 130 /* 128 131 * EC doesn't separate the concept of duty cycle and enabled, but 129 132 * kernel does. Translate. ··· 256 253 chip->ops = &cros_ec_pwm_ops; 257 254 chip->of_xlate = cros_ec_pwm_xlate; 258 255 chip->of_pwm_n_cells = 1; 259 - chip->base = -1; 260 256 ret = cros_ec_num_pwms(ec); 261 257 if (ret < 0) { 262 258 dev_err(dev, "Couldn't find PWMs: %d\n", ret);
-1
drivers/pwm/pwm-dwc.c
··· 233 233 dwc->chip.dev = dev; 234 234 dwc->chip.ops = &dwc_pwm_ops; 235 235 dwc->chip.npwm = DWC_TIMERS_TOTAL; 236 - dwc->chip.base = -1; 237 236 238 237 ret = pwmchip_add(&dwc->chip); 239 238 if (ret)
-1
drivers/pwm/pwm-ep93xx.c
··· 185 185 186 186 ep93xx_pwm->chip.dev = &pdev->dev; 187 187 ep93xx_pwm->chip.ops = &ep93xx_pwm_ops; 188 - ep93xx_pwm->chip.base = -1; 189 188 ep93xx_pwm->chip.npwm = 1; 190 189 191 190 ret = pwmchip_add(&ep93xx_pwm->chip);
-1
drivers/pwm/pwm-fsl-ftm.c
··· 453 453 fpc->chip.ops = &fsl_pwm_ops; 454 454 fpc->chip.of_xlate = of_pwm_xlate_with_flags; 455 455 fpc->chip.of_pwm_n_cells = 3; 456 - fpc->chip.base = -1; 457 456 fpc->chip.npwm = 8; 458 457 459 458 ret = pwmchip_add(&fpc->chip);
-1
drivers/pwm/pwm-hibvt.c
··· 205 205 206 206 pwm_chip->chip.ops = &hibvt_pwm_ops; 207 207 pwm_chip->chip.dev = &pdev->dev; 208 - pwm_chip->chip.base = -1; 209 208 pwm_chip->chip.npwm = soc->num_pwms; 210 209 pwm_chip->chip.of_xlate = of_pwm_xlate_with_flags; 211 210 pwm_chip->chip.of_pwm_n_cells = 3;
-1
drivers/pwm/pwm-img.c
··· 304 304 305 305 pwm->chip.dev = &pdev->dev; 306 306 pwm->chip.ops = &img_pwm_ops; 307 - pwm->chip.base = -1; 308 307 pwm->chip.npwm = IMG_PWM_NPWM; 309 308 310 309 ret = pwmchip_add(&pwm->chip);
+1 -4
drivers/pwm/pwm-imx-tpm.c
··· 363 363 364 364 tpm->chip.dev = &pdev->dev; 365 365 tpm->chip.ops = &imx_tpm_pwm_ops; 366 - tpm->chip.base = -1; 367 366 tpm->chip.of_xlate = of_pwm_xlate_with_flags; 368 367 tpm->chip.of_pwm_n_cells = 3; 369 368 ··· 410 411 411 412 ret = clk_prepare_enable(tpm->clk); 412 413 if (ret) 413 - dev_err(dev, 414 - "failed to prepare or enable clock: %d\n", 415 - ret); 414 + dev_err(dev, "failed to prepare or enable clock: %d\n", ret); 416 415 417 416 return ret; 418 417 }
-1
drivers/pwm/pwm-imx1.c
··· 155 155 156 156 imx->chip.ops = &pwm_imx1_ops; 157 157 imx->chip.dev = &pdev->dev; 158 - imx->chip.base = -1; 159 158 imx->chip.npwm = 1; 160 159 161 160 imx->mmio_base = devm_platform_ioremap_resource(pdev, 0);
-1
drivers/pwm/pwm-imx27.c
··· 327 327 328 328 imx->chip.ops = &pwm_imx27_ops; 329 329 imx->chip.dev = &pdev->dev; 330 - imx->chip.base = -1; 331 330 imx->chip.npwm = 1; 332 331 333 332 imx->chip.of_xlate = of_pwm_xlate_with_flags;
-1
drivers/pwm/pwm-intel-lgm.c
··· 207 207 pc->chip.dev = dev; 208 208 pc->chip.ops = &lgm_pwm_ops; 209 209 pc->chip.npwm = 1; 210 - pc->chip.base = -1; 211 210 212 211 lgm_pwm_init(pc); 213 212
-1
drivers/pwm/pwm-iqs620a.c
··· 206 206 207 207 iqs620_pwm->chip.dev = &pdev->dev; 208 208 iqs620_pwm->chip.ops = &iqs620_pwm_ops; 209 - iqs620_pwm->chip.base = -1; 210 209 iqs620_pwm->chip.npwm = 1; 211 210 212 211 mutex_init(&iqs620_pwm->lock);
-1
drivers/pwm/pwm-jz4740.c
··· 244 244 jz4740->chip.dev = dev; 245 245 jz4740->chip.ops = &jz4740_pwm_ops; 246 246 jz4740->chip.npwm = info->num_pwms; 247 - jz4740->chip.base = -1; 248 247 jz4740->chip.of_xlate = of_pwm_xlate_with_flags; 249 248 jz4740->chip.of_pwm_n_cells = 3; 250 249
-1
drivers/pwm/pwm-keembay.c
··· 203 203 if (ret) 204 204 return ret; 205 205 206 - priv->chip.base = -1; 207 206 priv->chip.dev = dev; 208 207 priv->chip.ops = &keembay_pwm_ops; 209 208 priv->chip.npwm = KMB_TOTAL_PWM_CHANNELS;
-1
drivers/pwm/pwm-lp3943.c
··· 275 275 lp3943_pwm->chip.dev = &pdev->dev; 276 276 lp3943_pwm->chip.ops = &lp3943_pwm_ops; 277 277 lp3943_pwm->chip.npwm = LP3943_NUM_PWMS; 278 - lp3943_pwm->chip.base = -1; 279 278 280 279 platform_set_drvdata(pdev, lp3943_pwm); 281 280
+3 -2
drivers/pwm/pwm-lpc18xx-sct.c
··· 370 370 371 371 lpc18xx_pwm->chip.dev = &pdev->dev; 372 372 lpc18xx_pwm->chip.ops = &lpc18xx_pwm_ops; 373 - lpc18xx_pwm->chip.base = -1; 374 373 lpc18xx_pwm->chip.npwm = 16; 375 374 lpc18xx_pwm->chip.of_xlate = of_pwm_xlate_with_flags; 376 375 lpc18xx_pwm->chip.of_pwm_n_cells = 3; ··· 441 442 struct lpc18xx_pwm_chip *lpc18xx_pwm = platform_get_drvdata(pdev); 442 443 u32 val; 443 444 445 + pwmchip_remove(&lpc18xx_pwm->chip); 446 + 444 447 val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_CTRL); 445 448 lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CTRL, 446 449 val | LPC18XX_PWM_CTRL_HALT); 447 450 448 451 clk_disable_unprepare(lpc18xx_pwm->pwm_clk); 449 452 450 - return pwmchip_remove(&lpc18xx_pwm->chip); 453 + return 0; 451 454 } 452 455 453 456 static struct platform_driver lpc18xx_pwm_driver = {
-5
drivers/pwm/pwm-lpc32xx.c
··· 116 116 lpc32xx->chip.dev = &pdev->dev; 117 117 lpc32xx->chip.ops = &lpc32xx_pwm_ops; 118 118 lpc32xx->chip.npwm = 1; 119 - lpc32xx->chip.base = -1; 120 119 121 120 ret = pwmchip_add(&lpc32xx->chip); 122 121 if (ret < 0) { ··· 136 137 static int lpc32xx_pwm_remove(struct platform_device *pdev) 137 138 { 138 139 struct lpc32xx_pwm_chip *lpc32xx = platform_get_drvdata(pdev); 139 - unsigned int i; 140 - 141 - for (i = 0; i < lpc32xx->chip.npwm; i++) 142 - pwm_disable(&lpc32xx->chip.pwms[i]); 143 140 144 141 return pwmchip_remove(&lpc32xx->chip); 145 142 }
-7
drivers/pwm/pwm-lpss.c
··· 234 234 235 235 lpwm->chip.dev = dev; 236 236 lpwm->chip.ops = &pwm_lpss_ops; 237 - lpwm->chip.base = -1; 238 237 lpwm->chip.npwm = info->npwm; 239 238 240 239 ret = pwmchip_add(&lpwm->chip); ··· 254 255 255 256 int pwm_lpss_remove(struct pwm_lpss_chip *lpwm) 256 257 { 257 - int i; 258 - 259 - for (i = 0; i < lpwm->info->npwm; i++) { 260 - if (pwm_is_enabled(&lpwm->chip.pwms[i])) 261 - pm_runtime_put(lpwm->chip.dev); 262 - } 263 258 return pwmchip_remove(&lpwm->chip); 264 259 } 265 260 EXPORT_SYMBOL_GPL(pwm_lpss_remove);
-7
drivers/pwm/pwm-mediatek.c
··· 107 107 clk_disable_unprepare(pc->clk_top); 108 108 } 109 109 110 - static inline u32 pwm_mediatek_readl(struct pwm_mediatek_chip *chip, 111 - unsigned int num, unsigned int offset) 112 - { 113 - return readl(chip->regs + pwm_mediatek_reg_offset[num] + offset); 114 - } 115 - 116 110 static inline void pwm_mediatek_writel(struct pwm_mediatek_chip *chip, 117 111 unsigned int num, unsigned int offset, 118 112 u32 value) ··· 257 263 258 264 pc->chip.dev = &pdev->dev; 259 265 pc->chip.ops = &pwm_mediatek_ops; 260 - pc->chip.base = -1; 261 266 pc->chip.npwm = pc->soc->num_pwms; 262 267 263 268 ret = pwmchip_add(&pc->chip);
-1
drivers/pwm/pwm-meson.c
··· 550 550 spin_lock_init(&meson->lock); 551 551 meson->chip.dev = &pdev->dev; 552 552 meson->chip.ops = &meson_pwm_ops; 553 - meson->chip.base = -1; 554 553 meson->chip.npwm = MESON_NUM_PWMS; 555 554 meson->chip.of_xlate = of_pwm_xlate_with_flags; 556 555 meson->chip.of_pwm_n_cells = 3;
-1
drivers/pwm/pwm-mtk-disp.c
··· 202 202 203 203 mdp->chip.dev = &pdev->dev; 204 204 mdp->chip.ops = &mtk_disp_pwm_ops; 205 - mdp->chip.base = -1; 206 205 mdp->chip.npwm = 1; 207 206 208 207 ret = pwmchip_add(&mdp->chip);
-1
drivers/pwm/pwm-mxs.c
··· 140 140 mxs->chip.ops = &mxs_pwm_ops; 141 141 mxs->chip.of_xlate = of_pwm_xlate_with_flags; 142 142 mxs->chip.of_pwm_n_cells = 3; 143 - mxs->chip.base = -1; 144 143 145 144 ret = of_property_read_u32(np, "fsl,pwm-number", &mxs->chip.npwm); 146 145 if (ret < 0) {
-1
drivers/pwm/pwm-omap-dmtimer.c
··· 403 403 404 404 omap->chip.dev = &pdev->dev; 405 405 omap->chip.ops = &pwm_omap_dmtimer_ops; 406 - omap->chip.base = -1; 407 406 omap->chip.npwm = 1; 408 407 omap->chip.of_xlate = of_pwm_xlate_with_flags; 409 408 omap->chip.of_pwm_n_cells = 3;
+150 -181
drivers/pwm/pwm-pca9685.c
··· 51 51 #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */ 52 52 53 53 #define PCA9685_COUNTER_RANGE 4096 54 - #define PCA9685_DEFAULT_PERIOD 5000000 /* Default period_ns = 1/200 Hz */ 55 54 #define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */ 56 55 57 56 #define PCA9685_NUMREGS 0xFF ··· 70 71 #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N))) 71 72 #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N))) 72 73 74 + #define REG_ON_H(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_ON_H : LED_N_ON_H((C))) 75 + #define REG_ON_L(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_ON_L : LED_N_ON_L((C))) 76 + #define REG_OFF_H(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_OFF_H : LED_N_OFF_H((C))) 77 + #define REG_OFF_L(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_OFF_L : LED_N_OFF_L((C))) 78 + 73 79 struct pca9685 { 74 80 struct pwm_chip chip; 75 81 struct regmap *regmap; 76 - int period_ns; 77 82 #if IS_ENABLED(CONFIG_GPIOLIB) 78 83 struct mutex lock; 79 84 struct gpio_chip gpio; ··· 88 85 static inline struct pca9685 *to_pca(struct pwm_chip *chip) 89 86 { 90 87 return container_of(chip, struct pca9685, chip); 88 + } 89 + 90 + /* Helper function to set the duty cycle ratio to duty/4096 (e.g. duty=2048 -> 50%) */ 91 + static void pca9685_pwm_set_duty(struct pca9685 *pca, int channel, unsigned int duty) 92 + { 93 + if (duty == 0) { 94 + /* Set the full OFF bit, which has the highest precedence */ 95 + regmap_write(pca->regmap, REG_OFF_H(channel), LED_FULL); 96 + } else if (duty >= PCA9685_COUNTER_RANGE) { 97 + /* Set the full ON bit and clear the full OFF bit */ 98 + regmap_write(pca->regmap, REG_ON_H(channel), LED_FULL); 99 + regmap_write(pca->regmap, REG_OFF_H(channel), 0); 100 + } else { 101 + /* Set OFF time (clears the full OFF bit) */ 102 + regmap_write(pca->regmap, REG_OFF_L(channel), duty & 0xff); 103 + regmap_write(pca->regmap, REG_OFF_H(channel), (duty >> 8) & 0xf); 104 + /* Clear the full ON bit */ 105 + regmap_write(pca->regmap, REG_ON_H(channel), 0); 106 + } 107 + } 108 + 109 + static unsigned int pca9685_pwm_get_duty(struct pca9685 *pca, int channel) 110 + { 111 + unsigned int off_h = 0, val = 0; 112 + 113 + if (WARN_ON(channel >= PCA9685_MAXCHAN)) { 114 + /* HW does not support reading state of "all LEDs" channel */ 115 + return 0; 116 + } 117 + 118 + regmap_read(pca->regmap, LED_N_OFF_H(channel), &off_h); 119 + if (off_h & LED_FULL) { 120 + /* Full OFF bit is set */ 121 + return 0; 122 + } 123 + 124 + regmap_read(pca->regmap, LED_N_ON_H(channel), &val); 125 + if (val & LED_FULL) { 126 + /* Full ON bit is set */ 127 + return PCA9685_COUNTER_RANGE; 128 + } 129 + 130 + if (regmap_read(pca->regmap, LED_N_OFF_L(channel), &val)) { 131 + /* Reset val to 0 in case reading LED_N_OFF_L failed */ 132 + val = 0; 133 + } 134 + return ((off_h & 0xf) << 8) | (val & 0xff); 91 135 } 92 136 93 137 #if IS_ENABLED(CONFIG_GPIOLIB) ··· 188 138 static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset) 189 139 { 190 140 struct pca9685 *pca = gpiochip_get_data(gpio); 191 - struct pwm_device *pwm = &pca->chip.pwms[offset]; 192 - unsigned int value; 193 141 194 - regmap_read(pca->regmap, LED_N_ON_H(pwm->hwpwm), &value); 195 - 196 - return value & LED_FULL; 142 + return pca9685_pwm_get_duty(pca, offset) != 0; 197 143 } 198 144 199 145 static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset, 200 146 int value) 201 147 { 202 148 struct pca9685 *pca = gpiochip_get_data(gpio); 203 - struct pwm_device *pwm = &pca->chip.pwms[offset]; 204 - unsigned int on = value ? LED_FULL : 0; 205 149 206 - /* Clear both OFF registers */ 207 - regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm), 0); 208 - regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm), 0); 209 - 210 - /* Set the full ON bit */ 211 - regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), on); 150 + pca9685_pwm_set_duty(pca, offset, value ? PCA9685_COUNTER_RANGE : 0); 212 151 } 213 152 214 153 static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset) 215 154 { 216 155 struct pca9685 *pca = gpiochip_get_data(gpio); 217 156 218 - pca9685_pwm_gpio_set(gpio, offset, 0); 157 + pca9685_pwm_set_duty(pca, offset, 0); 219 158 pm_runtime_put(pca->chip.dev); 220 159 pca9685_pwm_clear_inuse(pca, offset); 221 160 } ··· 285 246 } 286 247 } 287 248 288 - static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 289 - int duty_ns, int period_ns) 249 + static int pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 250 + const struct pwm_state *state) 251 + { 252 + struct pca9685 *pca = to_pca(chip); 253 + unsigned long long duty, prescale; 254 + unsigned int val = 0; 255 + 256 + if (state->polarity != PWM_POLARITY_NORMAL) 257 + return -EINVAL; 258 + 259 + prescale = DIV_ROUND_CLOSEST_ULL(PCA9685_OSC_CLOCK_MHZ * state->period, 260 + PCA9685_COUNTER_RANGE * 1000) - 1; 261 + if (prescale < PCA9685_PRESCALE_MIN || prescale > PCA9685_PRESCALE_MAX) { 262 + dev_err(chip->dev, "pwm not changed: period out of bounds!\n"); 263 + return -EINVAL; 264 + } 265 + 266 + if (!state->enabled) { 267 + pca9685_pwm_set_duty(pca, pwm->hwpwm, 0); 268 + return 0; 269 + } 270 + 271 + regmap_read(pca->regmap, PCA9685_PRESCALE, &val); 272 + if (prescale != val) { 273 + /* 274 + * Putting the chip briefly into SLEEP mode 275 + * at this point won't interfere with the 276 + * pm_runtime framework, because the pm_runtime 277 + * state is guaranteed active here. 278 + */ 279 + /* Put chip into sleep mode */ 280 + pca9685_set_sleep_mode(pca, true); 281 + 282 + /* Change the chip-wide output frequency */ 283 + regmap_write(pca->regmap, PCA9685_PRESCALE, prescale); 284 + 285 + /* Wake the chip up */ 286 + pca9685_set_sleep_mode(pca, false); 287 + } 288 + 289 + duty = PCA9685_COUNTER_RANGE * state->duty_cycle; 290 + duty = DIV_ROUND_UP_ULL(duty, state->period); 291 + pca9685_pwm_set_duty(pca, pwm->hwpwm, duty); 292 + return 0; 293 + } 294 + 295 + static void pca9685_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, 296 + struct pwm_state *state) 290 297 { 291 298 struct pca9685 *pca = to_pca(chip); 292 299 unsigned long long duty; 293 - unsigned int reg; 294 - int prescale; 300 + unsigned int val = 0; 295 301 296 - if (period_ns != pca->period_ns) { 297 - prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns, 298 - PCA9685_COUNTER_RANGE * 1000) - 1; 299 - 300 - if (prescale >= PCA9685_PRESCALE_MIN && 301 - prescale <= PCA9685_PRESCALE_MAX) { 302 - /* 303 - * Putting the chip briefly into SLEEP mode 304 - * at this point won't interfere with the 305 - * pm_runtime framework, because the pm_runtime 306 - * state is guaranteed active here. 307 - */ 308 - /* Put chip into sleep mode */ 309 - pca9685_set_sleep_mode(pca, true); 310 - 311 - /* Change the chip-wide output frequency */ 312 - regmap_write(pca->regmap, PCA9685_PRESCALE, prescale); 313 - 314 - /* Wake the chip up */ 315 - pca9685_set_sleep_mode(pca, false); 316 - 317 - pca->period_ns = period_ns; 318 - } else { 319 - dev_err(chip->dev, 320 - "prescaler not set: period out of bounds!\n"); 321 - return -EINVAL; 322 - } 323 - } 324 - 325 - if (duty_ns < 1) { 326 - if (pwm->hwpwm >= PCA9685_MAXCHAN) 327 - reg = PCA9685_ALL_LED_OFF_H; 328 - else 329 - reg = LED_N_OFF_H(pwm->hwpwm); 330 - 331 - regmap_write(pca->regmap, reg, LED_FULL); 332 - 333 - return 0; 334 - } 335 - 336 - if (duty_ns == period_ns) { 337 - /* Clear both OFF registers */ 338 - if (pwm->hwpwm >= PCA9685_MAXCHAN) 339 - reg = PCA9685_ALL_LED_OFF_L; 340 - else 341 - reg = LED_N_OFF_L(pwm->hwpwm); 342 - 343 - regmap_write(pca->regmap, reg, 0x0); 344 - 345 - if (pwm->hwpwm >= PCA9685_MAXCHAN) 346 - reg = PCA9685_ALL_LED_OFF_H; 347 - else 348 - reg = LED_N_OFF_H(pwm->hwpwm); 349 - 350 - regmap_write(pca->regmap, reg, 0x0); 351 - 352 - /* Set the full ON bit */ 353 - if (pwm->hwpwm >= PCA9685_MAXCHAN) 354 - reg = PCA9685_ALL_LED_ON_H; 355 - else 356 - reg = LED_N_ON_H(pwm->hwpwm); 357 - 358 - regmap_write(pca->regmap, reg, LED_FULL); 359 - 360 - return 0; 361 - } 362 - 363 - duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns; 364 - duty = DIV_ROUND_UP_ULL(duty, period_ns); 365 - 366 - if (pwm->hwpwm >= PCA9685_MAXCHAN) 367 - reg = PCA9685_ALL_LED_OFF_L; 368 - else 369 - reg = LED_N_OFF_L(pwm->hwpwm); 370 - 371 - regmap_write(pca->regmap, reg, (int)duty & 0xff); 372 - 373 - if (pwm->hwpwm >= PCA9685_MAXCHAN) 374 - reg = PCA9685_ALL_LED_OFF_H; 375 - else 376 - reg = LED_N_OFF_H(pwm->hwpwm); 377 - 378 - regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf); 379 - 380 - /* Clear the full ON bit, otherwise the set OFF time has no effect */ 381 - if (pwm->hwpwm >= PCA9685_MAXCHAN) 382 - reg = PCA9685_ALL_LED_ON_H; 383 - else 384 - reg = LED_N_ON_H(pwm->hwpwm); 385 - 386 - regmap_write(pca->regmap, reg, 0); 387 - 388 - return 0; 389 - } 390 - 391 - static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) 392 - { 393 - struct pca9685 *pca = to_pca(chip); 394 - unsigned int reg; 395 - 302 + /* Calculate (chip-wide) period from prescale value */ 303 + regmap_read(pca->regmap, PCA9685_PRESCALE, &val); 396 304 /* 397 - * The PWM subsystem does not support a pre-delay. 398 - * So, set the ON-timeout to 0 305 + * PCA9685_OSC_CLOCK_MHZ is 25, i.e. an integer divider of 1000. 306 + * The following calculation is therefore only a multiplication 307 + * and we are not losing precision. 399 308 */ 400 - if (pwm->hwpwm >= PCA9685_MAXCHAN) 401 - reg = PCA9685_ALL_LED_ON_L; 402 - else 403 - reg = LED_N_ON_L(pwm->hwpwm); 309 + state->period = (PCA9685_COUNTER_RANGE * 1000 / PCA9685_OSC_CLOCK_MHZ) * 310 + (val + 1); 404 311 405 - regmap_write(pca->regmap, reg, 0); 312 + /* The (per-channel) polarity is fixed */ 313 + state->polarity = PWM_POLARITY_NORMAL; 406 314 407 - if (pwm->hwpwm >= PCA9685_MAXCHAN) 408 - reg = PCA9685_ALL_LED_ON_H; 409 - else 410 - reg = LED_N_ON_H(pwm->hwpwm); 315 + if (pwm->hwpwm >= PCA9685_MAXCHAN) { 316 + /* 317 + * The "all LEDs" channel does not support HW readout 318 + * Return 0 and disabled for backwards compatibility 319 + */ 320 + state->duty_cycle = 0; 321 + state->enabled = false; 322 + return; 323 + } 411 324 412 - regmap_write(pca->regmap, reg, 0); 413 - 414 - /* 415 - * Clear the full-off bit. 416 - * It has precedence over the others and must be off. 417 - */ 418 - if (pwm->hwpwm >= PCA9685_MAXCHAN) 419 - reg = PCA9685_ALL_LED_OFF_H; 420 - else 421 - reg = LED_N_OFF_H(pwm->hwpwm); 422 - 423 - regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0); 424 - 425 - return 0; 426 - } 427 - 428 - static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) 429 - { 430 - struct pca9685 *pca = to_pca(chip); 431 - unsigned int reg; 432 - 433 - if (pwm->hwpwm >= PCA9685_MAXCHAN) 434 - reg = PCA9685_ALL_LED_OFF_H; 435 - else 436 - reg = LED_N_OFF_H(pwm->hwpwm); 437 - 438 - regmap_write(pca->regmap, reg, LED_FULL); 439 - 440 - /* Clear the LED_OFF counter. */ 441 - if (pwm->hwpwm >= PCA9685_MAXCHAN) 442 - reg = PCA9685_ALL_LED_OFF_L; 443 - else 444 - reg = LED_N_OFF_L(pwm->hwpwm); 445 - 446 - regmap_write(pca->regmap, reg, 0x0); 325 + state->enabled = true; 326 + duty = pca9685_pwm_get_duty(pca, pwm->hwpwm); 327 + state->duty_cycle = DIV_ROUND_DOWN_ULL(duty * state->period, PCA9685_COUNTER_RANGE); 447 328 } 448 329 449 330 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) ··· 381 422 { 382 423 struct pca9685 *pca = to_pca(chip); 383 424 384 - pca9685_pwm_disable(chip, pwm); 425 + pca9685_pwm_set_duty(pca, pwm->hwpwm, 0); 385 426 pm_runtime_put(chip->dev); 386 427 pca9685_pwm_clear_inuse(pca, pwm->hwpwm); 387 428 } 388 429 389 430 static const struct pwm_ops pca9685_pwm_ops = { 390 - .enable = pca9685_pwm_enable, 391 - .disable = pca9685_pwm_disable, 392 - .config = pca9685_pwm_config, 431 + .apply = pca9685_pwm_apply, 432 + .get_state = pca9685_pwm_get_state, 393 433 .request = pca9685_pwm_request, 394 434 .free = pca9685_pwm_free, 395 435 .owner = THIS_MODULE, ··· 419 461 ret); 420 462 return ret; 421 463 } 422 - pca->period_ns = PCA9685_DEFAULT_PERIOD; 423 464 424 465 i2c_set_clientdata(client, pca); 425 466 ··· 441 484 reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3); 442 485 regmap_write(pca->regmap, PCA9685_MODE1, reg); 443 486 444 - /* Clear all "full off" bits */ 445 - regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0); 446 - regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0); 487 + /* Reset OFF registers to POR default */ 488 + regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, LED_FULL); 489 + regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, LED_FULL); 447 490 448 491 pca->chip.ops = &pca9685_pwm_ops; 449 492 /* Add an extra channel for ALL_LED */ 450 493 pca->chip.npwm = PCA9685_MAXCHAN + 1; 451 494 452 495 pca->chip.dev = &client->dev; 453 - pca->chip.base = -1; 454 496 455 497 ret = pwmchip_add(&pca->chip); 456 498 if (ret < 0) ··· 461 505 return ret; 462 506 } 463 507 464 - /* The chip comes out of power-up in the active state */ 465 - pm_runtime_set_active(&client->dev); 466 - /* 467 - * Enable will put the chip into suspend, which is what we 468 - * want as all outputs are disabled at this point 469 - */ 470 508 pm_runtime_enable(&client->dev); 509 + 510 + if (pm_runtime_enabled(&client->dev)) { 511 + /* 512 + * Although the chip comes out of power-up in the sleep state, 513 + * we force it to sleep in case it was woken up before 514 + */ 515 + pca9685_set_sleep_mode(pca, true); 516 + pm_runtime_set_suspended(&client->dev); 517 + } else { 518 + /* Wake the chip up if runtime PM is disabled */ 519 + pca9685_set_sleep_mode(pca, false); 520 + } 471 521 472 522 return 0; 473 523 } ··· 486 524 ret = pwmchip_remove(&pca->chip); 487 525 if (ret) 488 526 return ret; 527 + 528 + if (!pm_runtime_enabled(&client->dev)) { 529 + /* Put chip in sleep state if runtime PM is disabled */ 530 + pca9685_set_sleep_mode(pca, true); 531 + } 532 + 489 533 pm_runtime_disable(&client->dev); 534 + 490 535 return 0; 491 536 } 492 537
-1
drivers/pwm/pwm-pxa.c
··· 184 184 185 185 pwm->chip.dev = &pdev->dev; 186 186 pwm->chip.ops = &pxa_pwm_ops; 187 - pwm->chip.base = -1; 188 187 pwm->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1; 189 188 190 189 if (IS_ENABLED(CONFIG_OF)) {
-1
drivers/pwm/pwm-rcar.c
··· 224 224 225 225 rcar_pwm->chip.dev = &pdev->dev; 226 226 rcar_pwm->chip.ops = &rcar_pwm_ops; 227 - rcar_pwm->chip.base = -1; 228 227 rcar_pwm->chip.npwm = 1; 229 228 230 229 pm_runtime_enable(&pdev->dev);
-1
drivers/pwm/pwm-renesas-tpu.c
··· 410 410 tpu->chip.ops = &tpu_pwm_ops; 411 411 tpu->chip.of_xlate = of_pwm_xlate_with_flags; 412 412 tpu->chip.of_pwm_n_cells = 3; 413 - tpu->chip.base = -1; 414 413 tpu->chip.npwm = TPU_CHANNEL_MAX; 415 414 416 415 pm_runtime_enable(&pdev->dev);
-1
drivers/pwm/pwm-rockchip.c
··· 352 352 pc->data = id->data; 353 353 pc->chip.dev = &pdev->dev; 354 354 pc->chip.ops = &rockchip_pwm_ops; 355 - pc->chip.base = -1; 356 355 pc->chip.npwm = 1; 357 356 358 357 if (pc->data->supports_polarity) {
-1
drivers/pwm/pwm-samsung.c
··· 519 519 520 520 chip->chip.dev = &pdev->dev; 521 521 chip->chip.ops = &pwm_samsung_ops; 522 - chip->chip.base = -1; 523 522 chip->chip.npwm = SAMSUNG_PWM_NUM; 524 523 chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1; 525 524
-1
drivers/pwm/pwm-sifive.c
··· 244 244 chip->ops = &pwm_sifive_ops; 245 245 chip->of_xlate = of_pwm_xlate_with_flags; 246 246 chip->of_pwm_n_cells = 3; 247 - chip->base = -1; 248 247 chip->npwm = 4; 249 248 250 249 ddata->regs = devm_platform_ioremap_resource(pdev, 0);
-1
drivers/pwm/pwm-sl28cpld.c
··· 229 229 chip = &priv->pwm_chip; 230 230 chip->dev = &pdev->dev; 231 231 chip->ops = &sl28cpld_pwm_ops; 232 - chip->base = -1; 233 232 chip->npwm = 1; 234 233 235 234 platform_set_drvdata(pdev, priv);
-1
drivers/pwm/pwm-spear.c
··· 193 193 194 194 pc->chip.dev = &pdev->dev; 195 195 pc->chip.ops = &spear_pwm_ops; 196 - pc->chip.base = -1; 197 196 pc->chip.npwm = NUM_PWM; 198 197 199 198 ret = clk_prepare(pc->clk);
+3 -1
drivers/pwm/pwm-sprd.c
··· 164 164 struct pwm_state *cstate = &pwm->state; 165 165 int ret; 166 166 167 + if (state->polarity != PWM_POLARITY_NORMAL) 168 + return -EINVAL; 169 + 167 170 if (state->enabled) { 168 171 if (!cstate->enabled) { 169 172 /* ··· 271 268 272 269 spc->chip.dev = &pdev->dev; 273 270 spc->chip.ops = &sprd_pwm_ops; 274 - spc->chip.base = -1; 275 271 spc->chip.npwm = spc->num_pwms; 276 272 277 273 ret = pwmchip_add(&spc->chip);
+2 -5
drivers/pwm/pwm-sti.c
··· 619 619 620 620 pc->chip.dev = dev; 621 621 pc->chip.ops = &sti_pwm_ops; 622 - pc->chip.base = -1; 623 622 pc->chip.npwm = pc->cdata->pwm_num_devs; 624 623 625 624 ret = pwmchip_add(&pc->chip); ··· 649 650 static int sti_pwm_remove(struct platform_device *pdev) 650 651 { 651 652 struct sti_pwm_chip *pc = platform_get_drvdata(pdev); 652 - unsigned int i; 653 653 654 - for (i = 0; i < pc->cdata->pwm_num_devs; i++) 655 - pwm_disable(&pc->chip.pwms[i]); 654 + pwmchip_remove(&pc->chip); 656 655 657 656 clk_unprepare(pc->pwm_clk); 658 657 clk_unprepare(pc->cpt_clk); 659 658 660 - return pwmchip_remove(&pc->chip); 659 + return 0; 661 660 } 662 661 663 662 static const struct of_device_id sti_pwm_of_match[] = {
-1
drivers/pwm/pwm-stm32-lp.c
··· 205 205 206 206 priv->regmap = ddata->regmap; 207 207 priv->clk = ddata->clk; 208 - priv->chip.base = -1; 209 208 priv->chip.dev = &pdev->dev; 210 209 priv->chip.ops = &stm32_pwm_lp_ops; 211 210 priv->chip.npwm = 1;
-1
drivers/pwm/pwm-stm32.c
··· 633 633 634 634 stm32_pwm_detect_complementary(priv); 635 635 636 - priv->chip.base = -1; 637 636 priv->chip.dev = dev; 638 637 priv->chip.ops = &stm32pwm_ops; 639 638 priv->chip.npwm = stm32_pwm_detect_channels(priv);
-1
drivers/pwm/pwm-stmpe.c
··· 278 278 279 279 pwm->stmpe = stmpe; 280 280 pwm->chip.dev = &pdev->dev; 281 - pwm->chip.base = -1; 282 281 283 282 if (stmpe->partnum == STMPE2401 || stmpe->partnum == STMPE2403) { 284 283 pwm->chip.ops = &stmpe_24xx_pwm_ops;
-1
drivers/pwm/pwm-sun4i.c
··· 459 459 460 460 pwm->chip.dev = &pdev->dev; 461 461 pwm->chip.ops = &sun4i_pwm_ops; 462 - pwm->chip.base = -1; 463 462 pwm->chip.npwm = pwm->data->npwm; 464 463 pwm->chip.of_xlate = of_pwm_xlate_with_flags; 465 464 pwm->chip.of_pwm_n_cells = 3;
-1
drivers/pwm/pwm-tegra.c
··· 285 285 286 286 pwm->chip.dev = &pdev->dev; 287 287 pwm->chip.ops = &tegra_pwm_ops; 288 - pwm->chip.base = -1; 289 288 pwm->chip.npwm = pwm->soc->num_channels; 290 289 291 290 ret = pwmchip_add(&pwm->chip);
-1
drivers/pwm/pwm-tiecap.c
··· 226 226 pc->chip.ops = &ecap_pwm_ops; 227 227 pc->chip.of_xlate = of_pwm_xlate_with_flags; 228 228 pc->chip.of_pwm_n_cells = 3; 229 - pc->chip.base = -1; 230 229 pc->chip.npwm = 1; 231 230 232 231 pc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
-1
drivers/pwm/pwm-tiehrpwm.c
··· 449 449 pc->chip.ops = &ehrpwm_pwm_ops; 450 450 pc->chip.of_xlate = of_pwm_xlate_with_flags; 451 451 pc->chip.of_pwm_n_cells = 3; 452 - pc->chip.base = -1; 453 452 pc->chip.npwm = NUM_PWM_CHANNEL; 454 453 455 454 pc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
-1
drivers/pwm/pwm-twl-led.c
··· 291 291 } 292 292 293 293 twl->chip.dev = &pdev->dev; 294 - twl->chip.base = -1; 295 294 296 295 mutex_init(&twl->mutex); 297 296
-1
drivers/pwm/pwm-twl.c
··· 310 310 twl->chip.ops = &twl6030_pwm_ops; 311 311 312 312 twl->chip.dev = &pdev->dev; 313 - twl->chip.base = -1; 314 313 twl->chip.npwm = 2; 315 314 316 315 mutex_init(&twl->mutex);
+190
drivers/pwm/pwm-visconti.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Toshiba Visconti pulse-width-modulation controller driver 4 + * 5 + * Copyright (c) 2020 - 2021 TOSHIBA CORPORATION 6 + * Copyright (c) 2020 - 2021 Toshiba Electronic Devices & Storage Corporation 7 + * 8 + * Authors: Nobuhiro Iwamatsu <nobuhiro1.iwamatsu@toshiba.co.jp> 9 + * 10 + * Limitations: 11 + * - The fixed input clock is running at 1 MHz and is divided by either 1, 12 + * 2, 4 or 8. 13 + * - When the settings of the PWM are modified, the new values are shadowed 14 + * in hardware until the PIPGM_PCSR register is written and the currently 15 + * running period is completed. This way the hardware switches atomically 16 + * from the old setting to the new. 17 + * - Disabling the hardware completes the currently running period and keeps 18 + * the output at low level at all times. 19 + */ 20 + 21 + #include <linux/err.h> 22 + #include <linux/io.h> 23 + #include <linux/module.h> 24 + #include <linux/of_device.h> 25 + #include <linux/platform_device.h> 26 + #include <linux/pwm.h> 27 + 28 + #define PIPGM_PCSR(ch) (0x400 + 4 * (ch)) 29 + #define PIPGM_PDUT(ch) (0x420 + 4 * (ch)) 30 + #define PIPGM_PWMC(ch) (0x440 + 4 * (ch)) 31 + 32 + #define PIPGM_PWMC_PWMACT BIT(5) 33 + #define PIPGM_PWMC_CLK_MASK GENMASK(1, 0) 34 + #define PIPGM_PWMC_POLARITY_MASK GENMASK(5, 5) 35 + 36 + struct visconti_pwm_chip { 37 + struct pwm_chip chip; 38 + void __iomem *base; 39 + }; 40 + 41 + static inline struct visconti_pwm_chip *visconti_pwm_from_chip(struct pwm_chip *chip) 42 + { 43 + return container_of(chip, struct visconti_pwm_chip, chip); 44 + } 45 + 46 + static int visconti_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 47 + const struct pwm_state *state) 48 + { 49 + struct visconti_pwm_chip *priv = visconti_pwm_from_chip(chip); 50 + u32 period, duty_cycle, pwmc0; 51 + 52 + if (!state->enabled) { 53 + writel(0, priv->base + PIPGM_PCSR(pwm->hwpwm)); 54 + return 0; 55 + } 56 + 57 + /* 58 + * The biggest period the hardware can provide is 59 + * (0xffff << 3) * 1000 ns 60 + * This value fits easily in an u32, so simplify the maths by 61 + * capping the values to 32 bit integers. 62 + */ 63 + if (state->period > (0xffff << 3) * 1000) 64 + period = (0xffff << 3) * 1000; 65 + else 66 + period = state->period; 67 + 68 + if (state->duty_cycle > period) 69 + duty_cycle = period; 70 + else 71 + duty_cycle = state->duty_cycle; 72 + 73 + /* 74 + * The input clock runs fixed at 1 MHz, so we have only 75 + * microsecond resolution and so can divide by 76 + * NSEC_PER_SEC / CLKFREQ = 1000 without losing precision. 77 + */ 78 + period /= 1000; 79 + duty_cycle /= 1000; 80 + 81 + if (!period) 82 + return -ERANGE; 83 + 84 + /* 85 + * PWMC controls a divider that divides the input clk by a 86 + * power of two between 1 and 8. As a smaller divider yields 87 + * higher precision, pick the smallest possible one. 88 + */ 89 + if (period > 0xffff) { 90 + pwmc0 = ilog2(period >> 16); 91 + if (WARN_ON(pwmc0 > 3)) 92 + return -EINVAL; 93 + } else { 94 + pwmc0 = 0; 95 + } 96 + 97 + period >>= pwmc0; 98 + duty_cycle >>= pwmc0; 99 + 100 + if (state->polarity == PWM_POLARITY_INVERSED) 101 + pwmc0 |= PIPGM_PWMC_PWMACT; 102 + writel(pwmc0, priv->base + PIPGM_PWMC(pwm->hwpwm)); 103 + writel(duty_cycle, priv->base + PIPGM_PDUT(pwm->hwpwm)); 104 + writel(period, priv->base + PIPGM_PCSR(pwm->hwpwm)); 105 + 106 + return 0; 107 + } 108 + 109 + static void visconti_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, 110 + struct pwm_state *state) 111 + { 112 + struct visconti_pwm_chip *priv = visconti_pwm_from_chip(chip); 113 + u32 period, duty, pwmc0, pwmc0_clk; 114 + 115 + period = readl(priv->base + PIPGM_PCSR(pwm->hwpwm)); 116 + duty = readl(priv->base + PIPGM_PDUT(pwm->hwpwm)); 117 + pwmc0 = readl(priv->base + PIPGM_PWMC(pwm->hwpwm)); 118 + pwmc0_clk = pwmc0 & PIPGM_PWMC_CLK_MASK; 119 + 120 + state->period = (period << pwmc0_clk) * NSEC_PER_USEC; 121 + state->duty_cycle = (duty << pwmc0_clk) * NSEC_PER_USEC; 122 + if (pwmc0 & PIPGM_PWMC_POLARITY_MASK) 123 + state->polarity = PWM_POLARITY_INVERSED; 124 + else 125 + state->polarity = PWM_POLARITY_NORMAL; 126 + 127 + state->enabled = true; 128 + } 129 + 130 + static const struct pwm_ops visconti_pwm_ops = { 131 + .apply = visconti_pwm_apply, 132 + .get_state = visconti_pwm_get_state, 133 + .owner = THIS_MODULE, 134 + }; 135 + 136 + static int visconti_pwm_probe(struct platform_device *pdev) 137 + { 138 + struct device *dev = &pdev->dev; 139 + struct visconti_pwm_chip *priv; 140 + int ret; 141 + 142 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 143 + if (!priv) 144 + return -ENOMEM; 145 + 146 + priv->base = devm_platform_ioremap_resource(pdev, 0); 147 + if (IS_ERR(priv->base)) 148 + return PTR_ERR(priv->base); 149 + 150 + platform_set_drvdata(pdev, priv); 151 + 152 + priv->chip.dev = dev; 153 + priv->chip.ops = &visconti_pwm_ops; 154 + priv->chip.npwm = 4; 155 + 156 + ret = pwmchip_add(&priv->chip); 157 + if (ret < 0) 158 + return dev_err_probe(&pdev->dev, ret, "Cannot register visconti PWM\n"); 159 + 160 + return 0; 161 + } 162 + 163 + static int visconti_pwm_remove(struct platform_device *pdev) 164 + { 165 + struct visconti_pwm_chip *priv = platform_get_drvdata(pdev); 166 + 167 + pwmchip_remove(&priv->chip); 168 + 169 + return 0; 170 + } 171 + 172 + static const struct of_device_id visconti_pwm_of_match[] = { 173 + { .compatible = "toshiba,visconti-pwm", }, 174 + { } 175 + }; 176 + MODULE_DEVICE_TABLE(of, visconti_pwm_of_match); 177 + 178 + static struct platform_driver visconti_pwm_driver = { 179 + .driver = { 180 + .name = "pwm-visconti", 181 + .of_match_table = visconti_pwm_of_match, 182 + }, 183 + .probe = visconti_pwm_probe, 184 + .remove = visconti_pwm_remove, 185 + }; 186 + module_platform_driver(visconti_pwm_driver); 187 + 188 + MODULE_LICENSE("GPL v2"); 189 + MODULE_AUTHOR("Nobuhiro Iwamatsu <nobuhiro1.iwamatsu@toshiba.co.jp>"); 190 + MODULE_ALIAS("platform:pwm-visconti");
-1
drivers/pwm/pwm-vt8500.c
··· 209 209 chip->chip.ops = &vt8500_pwm_ops; 210 210 chip->chip.of_xlate = of_pwm_xlate_with_flags; 211 211 chip->chip.of_pwm_n_cells = 3; 212 - chip->chip.base = -1; 213 212 chip->chip.npwm = VT8500_NR_PWMS; 214 213 215 214 chip->clk = devm_clk_get(&pdev->dev, NULL);
+5 -2
include/linux/pwm.h
··· 91 91 * pwm_get_state() - retrieve the current PWM state 92 92 * @pwm: PWM device 93 93 * @state: state to fill with the current PWM state 94 + * 95 + * The returned PWM state represents the state that was applied by a previous call to 96 + * pwm_apply_state(). Drivers may have to slightly tweak that state before programming it to 97 + * hardware. If pwm_apply_state() was never called, this returns either the current hardware 98 + * state (if supported) or the default settings. 94 99 */ 95 100 static inline void pwm_get_state(const struct pwm_device *pwm, 96 101 struct pwm_state *state) ··· 397 392 int pwm_set_chip_data(struct pwm_device *pwm, void *data); 398 393 void *pwm_get_chip_data(struct pwm_device *pwm); 399 394 400 - int pwmchip_add_with_polarity(struct pwm_chip *chip, 401 - enum pwm_polarity polarity); 402 395 int pwmchip_add(struct pwm_chip *chip); 403 396 int pwmchip_remove(struct pwm_chip *chip); 404 397 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,