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

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

Pull pwm updates from Uwe Kleine-König:
"This contains the usual amount of driver and device tree changes.
Additionally there is a big rework of how pwm lowlevel drivers are
registered to prepare adding character device support.

Thanks to Dharma Balasubiramani, Dong Aisheng, Duje Mihanović, Jerome
Brunet, Raag Jadav and Rafał Miłecki for their contributions. And
sorry for those who still need some patience because I didn't manage
to empty my review queue"

* tag 'pwm/for-6.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux: (185 commits)
pwm: imx-tpm: fix probe crash due to access registers without clock
pwm: meson: generalize 4 inputs clock on meson8 pwm type
dt-bindings: pwm: amlogic: Add a new binding for meson8 pwm types
dt-bindings: pwm: amlogic: fix s4 bindings
pwm: dwc: simplify error handling
pwm: dwc: Add 16 channel support for Intel Elkhart Lake
pwm: dwc: drop redundant error check
staging: greybus: pwm: Make use of devm_pwmchip_alloc() function
staging: greybus: pwm: Rework how the number of PWM lines is determined
staging: greybus: pwm: Drop unused gb_connection_set_data()
staging: greybus: pwm: Rely on pwm framework to pass a valid hwpwm
staging: greybus: pwm: Make use of pwmchip_parent() accessor
staging: greybus: pwm: Change prototype of helpers to prepare further changes
leds: qcom-lpg: Make use of devm_pwmchip_alloc() function
drm/bridge: ti-sn65dsi86: Make use of devm_pwmchip_alloc() function
drm/bridge: ti-sn65dsi86: Make use of pwmchip_parent() accessor
gpio: mvebu: Make use of devm_pwmchip_alloc() function
pwm: xilinx: Make use of devm_pwmchip_alloc() function
pwm: xilinx: Prepare removing pwm_chip from driver data
pwm: vt8500: Make use of devm_pwmchip_alloc() function
...

+2020 -1850
+35
Documentation/devicetree/bindings/pwm/atmel,hlcdc-pwm.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/atmel,hlcdc-pwm.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Atmel's HLCDC's PWM controller 8 + 9 + maintainers: 10 + - Nicolas Ferre <nicolas.ferre@microchip.com> 11 + - Alexandre Belloni <alexandre.belloni@bootlin.com> 12 + - Claudiu Beznea <claudiu.beznea@tuxon.dev> 13 + 14 + description: 15 + The LCDC integrates a Pulse Width Modulation (PWM) Controller. This block 16 + generates the LCD contrast control signal (LCD_PWM) that controls the 17 + display's contrast by software. LCDC_PWM is an 8-bit PWM signal that can be 18 + converted to an analog voltage with a simple passive filter. LCD display 19 + panels have different backlight specifications in terms of minimum/maximum 20 + values for PWM frequency. If the LCDC PWM frequency range does not match the 21 + LCD display panel, it is possible to use the standalone PWM Controller to 22 + drive the backlight. 23 + 24 + properties: 25 + compatible: 26 + const: atmel,hlcdc-pwm 27 + 28 + "#pwm-cells": 29 + const: 3 30 + 31 + required: 32 + - compatible 33 + - "#pwm-cells" 34 + 35 + additionalProperties: false
-29
Documentation/devicetree/bindings/pwm/atmel-hlcdc-pwm.txt
··· 1 - Device-Tree bindings for Atmel's HLCDC (High-end LCD Controller) PWM driver 2 - 3 - The Atmel HLCDC PWM is subdevice of the HLCDC MFD device. 4 - See ../mfd/atmel-hlcdc.txt for more details. 5 - 6 - Required properties: 7 - - compatible: value should be one of the following: 8 - "atmel,hlcdc-pwm" 9 - - pinctr-names: the pin control state names. Should contain "default". 10 - - pinctrl-0: should contain the pinctrl states described by pinctrl 11 - default. 12 - - #pwm-cells: should be set to 3. This PWM chip use the default 3 cells 13 - bindings defined in pwm.yaml in this directory. 14 - 15 - Example: 16 - 17 - hlcdc: hlcdc@f0030000 { 18 - compatible = "atmel,sama5d3-hlcdc"; 19 - reg = <0xf0030000 0x2000>; 20 - clocks = <&lcdc_clk>, <&lcdck>, <&clk32k>; 21 - clock-names = "periph_clk","sys_clk", "slow_clk"; 22 - 23 - hlcdc_pwm: hlcdc-pwm { 24 - compatible = "atmel,hlcdc-pwm"; 25 - pinctrl-names = "default"; 26 - pinctrl-0 = <&pinctrl_lcd_pwm>; 27 - #pwm-cells = <3>; 28 - }; 29 - };
+51
Documentation/devicetree/bindings/pwm/marvell,pxa-pwm.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/marvell,pxa-pwm.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Marvell PXA PWM 8 + 9 + maintainers: 10 + - Duje Mihanović <duje.mihanovic@skole.hr> 11 + 12 + allOf: 13 + - $ref: pwm.yaml# 14 + 15 + properties: 16 + compatible: 17 + enum: 18 + - marvell,pxa250-pwm 19 + - marvell,pxa270-pwm 20 + - marvell,pxa168-pwm 21 + - marvell,pxa910-pwm 22 + 23 + reg: 24 + # Length should be 0x10 25 + maxItems: 1 26 + 27 + "#pwm-cells": 28 + # Used for specifying the period length in nanoseconds 29 + const: 1 30 + 31 + clocks: 32 + maxItems: 1 33 + 34 + required: 35 + - compatible 36 + - reg 37 + - "#pwm-cells" 38 + - clocks 39 + 40 + additionalProperties: false 41 + 42 + examples: 43 + - | 44 + #include <dt-bindings/clock/pxa-clock.h> 45 + 46 + pwm0: pwm@40b00000 { 47 + compatible = "marvell,pxa250-pwm"; 48 + reg = <0x40b00000 0x10>; 49 + #pwm-cells = <1>; 50 + clocks = <&clks CLK_PWM0>; 51 + };
+1
Documentation/devicetree/bindings/pwm/mediatek,mt2712-pwm.yaml
··· 24 24 - mediatek,mt7629-pwm 25 25 - mediatek,mt7981-pwm 26 26 - mediatek,mt7986-pwm 27 + - mediatek,mt7988-pwm 27 28 - mediatek,mt8183-pwm 28 29 - mediatek,mt8365-pwm 29 30 - mediatek,mt8516-pwm
+104 -11
Documentation/devicetree/bindings/pwm/pwm-amlogic.yaml
··· 9 9 maintainers: 10 10 - Heiner Kallweit <hkallweit1@gmail.com> 11 11 12 - allOf: 13 - - $ref: pwm.yaml# 14 - 15 12 properties: 16 13 compatible: 17 14 oneOf: ··· 21 24 - amlogic,meson-g12a-ee-pwm 22 25 - amlogic,meson-g12a-ao-pwm-ab 23 26 - amlogic,meson-g12a-ao-pwm-cd 24 - - amlogic,meson-s4-pwm 27 + deprecated: true 25 28 - items: 26 29 - const: amlogic,meson-gx-pwm 27 30 - const: amlogic,meson-gxbb-pwm 31 + deprecated: true 28 32 - items: 29 33 - const: amlogic,meson-gx-ao-pwm 30 34 - const: amlogic,meson-gxbb-ao-pwm 35 + deprecated: true 31 36 - items: 32 37 - const: amlogic,meson8-pwm 33 38 - const: amlogic,meson8b-pwm 39 + deprecated: true 40 + - enum: 41 + - amlogic,meson8-pwm-v2 42 + - amlogic,meson-s4-pwm 43 + - items: 44 + - enum: 45 + - amlogic,meson8b-pwm-v2 46 + - amlogic,meson-gxbb-pwm-v2 47 + - amlogic,meson-axg-pwm-v2 48 + - amlogic,meson-g12-pwm-v2 49 + - const: amlogic,meson8-pwm-v2 34 50 35 51 reg: 36 52 maxItems: 1 37 53 38 54 clocks: 39 55 minItems: 1 40 - maxItems: 2 56 + maxItems: 4 41 57 42 58 clock-names: 43 - oneOf: 44 - - items: 45 - - enum: [clkin0, clkin1] 46 - - items: 47 - - const: clkin0 48 - - const: clkin1 59 + minItems: 1 60 + maxItems: 2 49 61 50 62 "#pwm-cells": 51 63 const: 3 ··· 62 56 required: 63 57 - compatible 64 58 - reg 59 + 60 + allOf: 61 + - $ref: pwm.yaml# 62 + 63 + - if: 64 + properties: 65 + compatible: 66 + contains: 67 + enum: 68 + - amlogic,meson8-pwm 69 + - amlogic,meson8b-pwm 70 + - amlogic,meson-gxbb-pwm 71 + - amlogic,meson-gxbb-ao-pwm 72 + - amlogic,meson-axg-ee-pwm 73 + - amlogic,meson-axg-ao-pwm 74 + - amlogic,meson-g12a-ee-pwm 75 + - amlogic,meson-g12a-ao-pwm-ab 76 + - amlogic,meson-g12a-ao-pwm-cd 77 + then: 78 + # Obsolete historic bindings tied to the driver implementation 79 + # The clocks provided here are meant to be matched with the input 80 + # known (hard-coded) in the driver and used to select pwm clock 81 + # source. Currently, the linux driver ignores this. 82 + # This is kept to maintain ABI backward compatibility. 83 + properties: 84 + clocks: 85 + maxItems: 2 86 + clock-names: 87 + oneOf: 88 + - items: 89 + - enum: [clkin0, clkin1] 90 + - items: 91 + - const: clkin0 92 + - const: clkin1 93 + 94 + # Newer binding where clock describe the actual clock inputs of the pwm 95 + # block. These are necessary but some inputs may be grounded. 96 + - if: 97 + properties: 98 + compatible: 99 + contains: 100 + enum: 101 + - amlogic,meson8-pwm-v2 102 + then: 103 + properties: 104 + clocks: 105 + minItems: 1 106 + items: 107 + - description: input clock 0 of the pwm block 108 + - description: input clock 1 of the pwm block 109 + - description: input clock 2 of the pwm block 110 + - description: input clock 3 of the pwm block 111 + clock-names: false 112 + required: 113 + - clocks 114 + 115 + # Newer IP block take a single input per channel, instead of 4 inputs 116 + # for both channels 117 + - if: 118 + properties: 119 + compatible: 120 + contains: 121 + enum: 122 + - amlogic,meson-s4-pwm 123 + then: 124 + properties: 125 + clocks: 126 + items: 127 + - description: input clock of PWM channel A 128 + - description: input clock of PWM channel B 129 + clock-names: false 130 + required: 131 + - clocks 65 132 66 133 additionalProperties: false 67 134 ··· 145 66 reg = <0x08550 0x10>; 146 67 clocks = <&xtal>, <&xtal>; 147 68 clock-names = "clkin0", "clkin1"; 69 + #pwm-cells = <3>; 70 + }; 71 + - | 72 + pwm@2000 { 73 + compatible = "amlogic,meson8-pwm-v2"; 74 + reg = <0x1000 0x10>; 75 + clocks = <&xtal>, <0>, <&fdiv4>, <&fdiv5>; 76 + #pwm-cells = <3>; 77 + }; 78 + - | 79 + pwm@1000 { 80 + compatible = "amlogic,meson-s4-pwm"; 81 + reg = <0x1000 0x10>; 82 + clocks = <&pwm_src_a>, <&pwm_src_b>; 148 83 #pwm-cells = <3>; 149 84 };
-30
Documentation/devicetree/bindings/pwm/pxa-pwm.txt
··· 1 - Marvell PWM controller 2 - 3 - Required properties: 4 - - compatible: should be one or more of: 5 - - "marvell,pxa250-pwm" 6 - - "marvell,pxa270-pwm" 7 - - "marvell,pxa168-pwm" 8 - - "marvell,pxa910-pwm" 9 - - reg: Physical base address and length of the registers used by the PWM channel 10 - Note that one device instance must be created for each PWM that is used, so the 11 - length covers only the register window for one PWM output, not that of the 12 - entire PWM controller. Currently length is 0x10 for all supported devices. 13 - - #pwm-cells: Should be 1. This cell is used to specify the period in 14 - nanoseconds. 15 - 16 - Example PWM device node: 17 - 18 - pwm0: pwm@40b00000 { 19 - compatible = "marvell,pxa250-pwm"; 20 - reg = <0x40b00000 0x10>; 21 - #pwm-cells = <1>; 22 - }; 23 - 24 - Example PWM client node: 25 - 26 - backlight { 27 - compatible = "pwm-backlight"; 28 - pwms = <&pwm0 5000000>; 29 - ... 30 - }
+1
Documentation/driver-api/driver-model/devres.rst
··· 420 420 devm_reboot_mode_unregister() 421 421 422 422 PWM 423 + devm_pwmchip_alloc() 423 424 devm_pwmchip_add() 424 425 devm_pwm_get() 425 426 devm_fwnode_pwm_get()
+6 -5
Documentation/driver-api/pwm.rst
··· 143 143 to have multiple PWM drivers in the system. For this reason it's mandatory 144 144 for new drivers to use the generic PWM framework. 145 145 146 - A new PWM controller/chip can be added using pwmchip_add() and removed 147 - again with pwmchip_remove(). pwmchip_add() takes a filled in struct 148 - pwm_chip as argument which provides a description of the PWM chip, the 149 - number of PWM devices provided by the chip and the chip-specific 150 - implementation of the supported PWM operations to the framework. 146 + A new PWM controller/chip can be allocated using pwmchip_alloc(), then 147 + registered using pwmchip_add() and removed again with pwmchip_remove(). To undo 148 + pwmchip_alloc() use pwmchip_put(). pwmchip_add() takes a filled in struct 149 + pwm_chip as argument which provides a description of the PWM chip, the number 150 + of PWM devices provided by the chip and the chip-specific implementation of the 151 + supported PWM operations to the framework. 151 152 152 153 When implementing polarity support in a PWM driver, make sure to respect the 153 154 signal conventions in the PWM framework. By definition, normal polarity
+9 -9
drivers/gpio/gpio-mvebu.c
··· 99 99 u32 offset; 100 100 unsigned long clk_rate; 101 101 struct gpio_desc *gpiod; 102 - struct pwm_chip chip; 103 102 spinlock_t lock; 104 103 struct mvebu_gpio_chip *mvchip; 105 104 ··· 614 615 */ 615 616 static struct mvebu_pwm *to_mvebu_pwm(struct pwm_chip *chip) 616 617 { 617 - return container_of(chip, struct mvebu_pwm, chip); 618 + return pwmchip_get_drvdata(chip); 618 619 } 619 620 620 621 static int mvebu_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) ··· 788 789 { 789 790 struct device *dev = &pdev->dev; 790 791 struct mvebu_pwm *mvpwm; 792 + struct pwm_chip *chip; 791 793 void __iomem *base; 792 794 u32 offset; 793 795 u32 set; ··· 813 813 if (IS_ERR(mvchip->clk)) 814 814 return PTR_ERR(mvchip->clk); 815 815 816 - mvpwm = devm_kzalloc(dev, sizeof(struct mvebu_pwm), GFP_KERNEL); 817 - if (!mvpwm) 818 - return -ENOMEM; 816 + chip = devm_pwmchip_alloc(dev, mvchip->chip.ngpio, sizeof(*mvpwm)); 817 + if (IS_ERR(chip)) 818 + return PTR_ERR(chip); 819 + mvpwm = to_mvebu_pwm(chip); 820 + 819 821 mvchip->mvpwm = mvpwm; 820 822 mvpwm->mvchip = mvchip; 821 823 mvpwm->offset = offset; ··· 870 868 return -EINVAL; 871 869 } 872 870 873 - mvpwm->chip.dev = dev; 874 - mvpwm->chip.ops = &mvebu_pwm_ops; 875 - mvpwm->chip.npwm = mvchip->chip.ngpio; 871 + chip->ops = &mvebu_pwm_ops; 876 872 877 873 spin_lock_init(&mvpwm->lock); 878 874 879 - return devm_pwmchip_add(dev, &mvpwm->chip); 875 + return devm_pwmchip_add(dev, chip); 880 876 } 881 877 882 878 #ifdef CONFIG_DEBUG_FS
+18 -14
drivers/gpu/drm/bridge/ti-sn65dsi86.c
··· 197 197 DECLARE_BITMAP(gchip_output, SN_NUM_GPIOS); 198 198 #endif 199 199 #if defined(CONFIG_PWM) 200 - struct pwm_chip pchip; 200 + struct pwm_chip *pchip; 201 201 bool pwm_enabled; 202 202 atomic_t pwm_pin_busy; 203 203 #endif ··· 1374 1374 1375 1375 static struct ti_sn65dsi86 *pwm_chip_to_ti_sn_bridge(struct pwm_chip *chip) 1376 1376 { 1377 - return container_of(chip, struct ti_sn65dsi86, pchip); 1377 + return pwmchip_get_drvdata(chip); 1378 1378 } 1379 1379 1380 1380 static int ti_sn_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) ··· 1415 1415 int ret; 1416 1416 1417 1417 if (!pdata->pwm_enabled) { 1418 - ret = pm_runtime_resume_and_get(chip->dev); 1418 + ret = pm_runtime_resume_and_get(pwmchip_parent(chip)); 1419 1419 if (ret < 0) 1420 1420 return ret; 1421 1421 } ··· 1431 1431 SN_GPIO_MUX_MASK << (2 * SN_PWM_GPIO_IDX), 1432 1432 SN_GPIO_MUX_SPECIAL << (2 * SN_PWM_GPIO_IDX)); 1433 1433 if (ret) { 1434 - dev_err(chip->dev, "failed to mux in PWM function\n"); 1434 + dev_err(pwmchip_parent(chip), "failed to mux in PWM function\n"); 1435 1435 goto out; 1436 1436 } 1437 1437 } ··· 1507 1507 1508 1508 ret = regmap_write(pdata->regmap, SN_PWM_PRE_DIV_REG, pre_div); 1509 1509 if (ret) { 1510 - dev_err(chip->dev, "failed to update PWM_PRE_DIV\n"); 1510 + dev_err(pwmchip_parent(chip), "failed to update PWM_PRE_DIV\n"); 1511 1511 goto out; 1512 1512 } 1513 1513 ··· 1519 1519 FIELD_PREP(SN_PWM_INV_MASK, state->polarity == PWM_POLARITY_INVERSED); 1520 1520 ret = regmap_write(pdata->regmap, SN_PWM_EN_INV_REG, pwm_en_inv); 1521 1521 if (ret) { 1522 - dev_err(chip->dev, "failed to update PWM_EN/PWM_INV\n"); 1522 + dev_err(pwmchip_parent(chip), "failed to update PWM_EN/PWM_INV\n"); 1523 1523 goto out; 1524 1524 } 1525 1525 ··· 1527 1527 out: 1528 1528 1529 1529 if (!pdata->pwm_enabled) 1530 - pm_runtime_put_sync(chip->dev); 1530 + pm_runtime_put_sync(pwmchip_parent(chip)); 1531 1531 1532 1532 return ret; 1533 1533 } ··· 1585 1585 static int ti_sn_pwm_probe(struct auxiliary_device *adev, 1586 1586 const struct auxiliary_device_id *id) 1587 1587 { 1588 + struct pwm_chip *chip; 1588 1589 struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent); 1589 1590 1590 - pdata->pchip.dev = &adev->dev; 1591 - pdata->pchip.ops = &ti_sn_pwm_ops; 1592 - pdata->pchip.npwm = 1; 1593 - pdata->pchip.of_xlate = of_pwm_single_xlate; 1594 - pdata->pchip.of_pwm_n_cells = 1; 1591 + pdata->pchip = chip = devm_pwmchip_alloc(&adev->dev, 1, 0); 1592 + if (IS_ERR(chip)) 1593 + return PTR_ERR(chip); 1594 + 1595 + pwmchip_set_drvdata(chip, pdata); 1596 + 1597 + chip->ops = &ti_sn_pwm_ops; 1598 + chip->of_xlate = of_pwm_single_xlate; 1595 1599 1596 1600 devm_pm_runtime_enable(&adev->dev); 1597 1601 1598 - return pwmchip_add(&pdata->pchip); 1602 + return pwmchip_add(chip); 1599 1603 } 1600 1604 1601 1605 static void ti_sn_pwm_remove(struct auxiliary_device *adev) 1602 1606 { 1603 1607 struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent); 1604 1608 1605 - pwmchip_remove(&pdata->pchip); 1609 + pwmchip_remove(pdata->pchip); 1606 1610 1607 1611 if (pdata->pwm_enabled) 1608 1612 pm_runtime_put_sync(&adev->dev);
+10 -6
drivers/leds/rgb/leds-qcom-lpg.c
··· 77 77 78 78 struct mutex lock; 79 79 80 - struct pwm_chip pwm; 80 + struct pwm_chip *pwm; 81 81 82 82 const struct lpg_data *data; 83 83 ··· 978 978 979 979 static inline struct lpg *lpg_pwm_from_chip(struct pwm_chip *chip) 980 980 { 981 - return container_of(chip, struct lpg, pwm); 981 + return pwmchip_get_drvdata(chip); 982 982 } 983 983 984 984 static int lpg_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) ··· 1093 1093 1094 1094 static int lpg_add_pwm(struct lpg *lpg) 1095 1095 { 1096 + struct pwm_chip *chip; 1096 1097 int ret; 1097 1098 1098 - lpg->pwm.dev = lpg->dev; 1099 - lpg->pwm.npwm = lpg->num_channels; 1100 - lpg->pwm.ops = &lpg_pwm_ops; 1099 + lpg->pwm = chip = devm_pwmchip_alloc(lpg->dev, lpg->num_channels, 0); 1100 + if (IS_ERR(chip)) 1101 + return PTR_ERR(chip); 1101 1102 1102 - ret = devm_pwmchip_add(lpg->dev, &lpg->pwm); 1103 + chip->ops = &lpg_pwm_ops; 1104 + pwmchip_set_drvdata(chip, lpg); 1105 + 1106 + ret = devm_pwmchip_add(lpg->dev, chip); 1103 1107 if (ret) 1104 1108 dev_err_probe(lpg->dev, ret, "failed to add PWM chip\n"); 1105 1109
+3 -3
drivers/pinctrl/intel/pinctrl-intel.c
··· 1492 1492 .base_unit_bits = 22, 1493 1493 .bypass = true, 1494 1494 }; 1495 - struct pwm_lpss_chip *pwm; 1495 + struct pwm_chip *chip; 1496 1496 1497 1497 if (!(community->features & PINCTRL_FEATURE_PWM)) 1498 1498 return 0; ··· 1500 1500 if (!IS_REACHABLE(CONFIG_PWM_LPSS)) 1501 1501 return 0; 1502 1502 1503 - pwm = devm_pwm_lpss_probe(pctrl->dev, community->regs + PWMC, &info); 1504 - return PTR_ERR_OR_ZERO(pwm); 1503 + chip = devm_pwm_lpss_probe(pctrl->dev, community->regs + PWMC, &info); 1504 + return PTR_ERR_OR_ZERO(chip); 1505 1505 } 1506 1506 1507 1507 int intel_pinctrl_probe(struct platform_device *pdev,
+381 -343
drivers/pwm/core.c
··· 24 24 #define CREATE_TRACE_POINTS 25 25 #include <trace/events/pwm.h> 26 26 27 - static DEFINE_MUTEX(pwm_lookup_lock); 28 - static LIST_HEAD(pwm_lookup_list); 29 - 30 27 /* protects access to pwm_chips */ 31 28 static DEFINE_MUTEX(pwm_lock); 32 29 33 30 static DEFINE_IDR(pwm_chips); 34 - 35 - static struct pwm_chip *pwmchip_find_by_name(const char *name) 36 - { 37 - struct pwm_chip *chip; 38 - unsigned long id, tmp; 39 - 40 - if (!name) 41 - return NULL; 42 - 43 - mutex_lock(&pwm_lock); 44 - 45 - idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) { 46 - const char *chip_name = dev_name(chip->dev); 47 - 48 - if (chip_name && strcmp(chip_name, name) == 0) { 49 - mutex_unlock(&pwm_lock); 50 - return chip; 51 - } 52 - } 53 - 54 - mutex_unlock(&pwm_lock); 55 - 56 - return NULL; 57 - } 58 - 59 - static int pwm_device_request(struct pwm_device *pwm, const char *label) 60 - { 61 - int err; 62 - struct pwm_chip *chip = pwm->chip; 63 - const struct pwm_ops *ops = chip->ops; 64 - 65 - if (test_bit(PWMF_REQUESTED, &pwm->flags)) 66 - return -EBUSY; 67 - 68 - if (!try_module_get(chip->owner)) 69 - return -ENODEV; 70 - 71 - if (ops->request) { 72 - err = ops->request(chip, pwm); 73 - if (err) { 74 - module_put(chip->owner); 75 - return err; 76 - } 77 - } 78 - 79 - if (ops->get_state) { 80 - /* 81 - * Zero-initialize state because most drivers are unaware of 82 - * .usage_power. The other members of state are supposed to be 83 - * set by lowlevel drivers. We still initialize the whole 84 - * structure for simplicity even though this might paper over 85 - * faulty implementations of .get_state(). 86 - */ 87 - struct pwm_state state = { 0, }; 88 - 89 - err = ops->get_state(chip, pwm, &state); 90 - trace_pwm_get(pwm, &state, err); 91 - 92 - if (!err) 93 - pwm->state = state; 94 - 95 - if (IS_ENABLED(CONFIG_PWM_DEBUG)) 96 - pwm->last = pwm->state; 97 - } 98 - 99 - set_bit(PWMF_REQUESTED, &pwm->flags); 100 - pwm->label = label; 101 - 102 - return 0; 103 - } 104 - 105 - struct pwm_device * 106 - of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args) 107 - { 108 - struct pwm_device *pwm; 109 - 110 - if (chip->of_pwm_n_cells < 2) 111 - return ERR_PTR(-EINVAL); 112 - 113 - /* flags in the third cell are optional */ 114 - if (args->args_count < 2) 115 - return ERR_PTR(-EINVAL); 116 - 117 - if (args->args[0] >= chip->npwm) 118 - return ERR_PTR(-EINVAL); 119 - 120 - pwm = pwm_request_from_chip(chip, args->args[0], NULL); 121 - if (IS_ERR(pwm)) 122 - return pwm; 123 - 124 - pwm->args.period = args->args[1]; 125 - pwm->args.polarity = PWM_POLARITY_NORMAL; 126 - 127 - if (chip->of_pwm_n_cells >= 3) { 128 - if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) 129 - pwm->args.polarity = PWM_POLARITY_INVERSED; 130 - } 131 - 132 - return pwm; 133 - } 134 - EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags); 135 - 136 - struct pwm_device * 137 - of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args) 138 - { 139 - struct pwm_device *pwm; 140 - 141 - if (chip->of_pwm_n_cells < 1) 142 - return ERR_PTR(-EINVAL); 143 - 144 - /* validate that one cell is specified, optionally with flags */ 145 - if (args->args_count != 1 && args->args_count != 2) 146 - return ERR_PTR(-EINVAL); 147 - 148 - pwm = pwm_request_from_chip(chip, 0, NULL); 149 - if (IS_ERR(pwm)) 150 - return pwm; 151 - 152 - pwm->args.period = args->args[0]; 153 - pwm->args.polarity = PWM_POLARITY_NORMAL; 154 - 155 - if (args->args_count == 2 && args->args[1] & PWM_POLARITY_INVERTED) 156 - pwm->args.polarity = PWM_POLARITY_INVERSED; 157 - 158 - return pwm; 159 - } 160 - EXPORT_SYMBOL_GPL(of_pwm_single_xlate); 161 - 162 - static void of_pwmchip_add(struct pwm_chip *chip) 163 - { 164 - if (!chip->dev || !chip->dev->of_node) 165 - return; 166 - 167 - if (!chip->of_xlate) { 168 - u32 pwm_cells; 169 - 170 - if (of_property_read_u32(chip->dev->of_node, "#pwm-cells", 171 - &pwm_cells)) 172 - pwm_cells = 2; 173 - 174 - chip->of_xlate = of_pwm_xlate_with_flags; 175 - chip->of_pwm_n_cells = pwm_cells; 176 - } 177 - 178 - of_node_get(chip->dev->of_node); 179 - } 180 - 181 - static void of_pwmchip_remove(struct pwm_chip *chip) 182 - { 183 - if (chip->dev) 184 - of_node_put(chip->dev->of_node); 185 - } 186 - 187 - static bool pwm_ops_check(const struct pwm_chip *chip) 188 - { 189 - const struct pwm_ops *ops = chip->ops; 190 - 191 - if (!ops->apply) 192 - return false; 193 - 194 - if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) 195 - dev_warn(chip->dev, 196 - "Please implement the .get_state() callback\n"); 197 - 198 - return true; 199 - } 200 - 201 - /** 202 - * __pwmchip_add() - register a new PWM chip 203 - * @chip: the PWM chip to add 204 - * @owner: reference to the module providing the chip. 205 - * 206 - * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the 207 - * pwmchip_add wrapper to do this right. 208 - * 209 - * Returns: 0 on success or a negative error code on failure. 210 - */ 211 - int __pwmchip_add(struct pwm_chip *chip, struct module *owner) 212 - { 213 - unsigned int i; 214 - int ret; 215 - 216 - if (!chip || !chip->dev || !chip->ops || !chip->npwm) 217 - return -EINVAL; 218 - 219 - if (!pwm_ops_check(chip)) 220 - return -EINVAL; 221 - 222 - chip->owner = owner; 223 - 224 - chip->pwms = kcalloc(chip->npwm, sizeof(*chip->pwms), GFP_KERNEL); 225 - if (!chip->pwms) 226 - return -ENOMEM; 227 - 228 - mutex_lock(&pwm_lock); 229 - 230 - ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL); 231 - if (ret < 0) { 232 - mutex_unlock(&pwm_lock); 233 - kfree(chip->pwms); 234 - return ret; 235 - } 236 - 237 - chip->id = ret; 238 - 239 - for (i = 0; i < chip->npwm; i++) { 240 - struct pwm_device *pwm = &chip->pwms[i]; 241 - 242 - pwm->chip = chip; 243 - pwm->hwpwm = i; 244 - } 245 - 246 - mutex_unlock(&pwm_lock); 247 - 248 - if (IS_ENABLED(CONFIG_OF)) 249 - of_pwmchip_add(chip); 250 - 251 - pwmchip_sysfs_export(chip); 252 - 253 - return 0; 254 - } 255 - EXPORT_SYMBOL_GPL(__pwmchip_add); 256 - 257 - /** 258 - * pwmchip_remove() - remove a PWM chip 259 - * @chip: the PWM chip to remove 260 - * 261 - * Removes a PWM chip. 262 - */ 263 - void pwmchip_remove(struct pwm_chip *chip) 264 - { 265 - pwmchip_sysfs_unexport(chip); 266 - 267 - if (IS_ENABLED(CONFIG_OF)) 268 - of_pwmchip_remove(chip); 269 - 270 - mutex_lock(&pwm_lock); 271 - 272 - idr_remove(&pwm_chips, chip->id); 273 - 274 - mutex_unlock(&pwm_lock); 275 - 276 - kfree(chip->pwms); 277 - } 278 - EXPORT_SYMBOL_GPL(pwmchip_remove); 279 - 280 - static void devm_pwmchip_remove(void *data) 281 - { 282 - struct pwm_chip *chip = data; 283 - 284 - pwmchip_remove(chip); 285 - } 286 - 287 - int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner) 288 - { 289 - int ret; 290 - 291 - ret = __pwmchip_add(chip, owner); 292 - if (ret) 293 - return ret; 294 - 295 - return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip); 296 - } 297 - EXPORT_SYMBOL_GPL(__devm_pwmchip_add); 298 - 299 - /** 300 - * pwm_request_from_chip() - request a PWM device relative to a PWM chip 301 - * @chip: PWM chip 302 - * @index: per-chip index of the PWM to request 303 - * @label: a literal description string of this PWM 304 - * 305 - * Returns: A pointer to the PWM device at the given index of the given PWM 306 - * chip. A negative error code is returned if the index is not valid for the 307 - * specified PWM chip or if the PWM device cannot be requested. 308 - */ 309 - struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 310 - unsigned int index, 311 - const char *label) 312 - { 313 - struct pwm_device *pwm; 314 - int err; 315 - 316 - if (!chip || index >= chip->npwm) 317 - return ERR_PTR(-EINVAL); 318 - 319 - mutex_lock(&pwm_lock); 320 - pwm = &chip->pwms[index]; 321 - 322 - err = pwm_device_request(pwm, label); 323 - if (err < 0) 324 - pwm = ERR_PTR(err); 325 - 326 - mutex_unlock(&pwm_lock); 327 - return pwm; 328 - } 329 - EXPORT_SYMBOL_GPL(pwm_request_from_chip); 330 31 331 32 static void pwm_apply_debug(struct pwm_device *pwm, 332 33 const struct pwm_state *state) ··· 71 370 72 371 if (s2.polarity != state->polarity && 73 372 state->duty_cycle < state->period) 74 - dev_warn(chip->dev, ".apply ignored .polarity\n"); 373 + dev_warn(pwmchip_parent(chip), ".apply ignored .polarity\n"); 75 374 76 375 if (state->enabled && 77 376 last->polarity == state->polarity && 78 377 last->period > s2.period && 79 378 last->period <= state->period) 80 - dev_warn(chip->dev, 379 + dev_warn(pwmchip_parent(chip), 81 380 ".apply didn't pick the best available period (requested: %llu, applied: %llu, possible: %llu)\n", 82 381 state->period, s2.period, last->period); 83 382 84 383 if (state->enabled && state->period < s2.period) 85 - dev_warn(chip->dev, 384 + dev_warn(pwmchip_parent(chip), 86 385 ".apply is supposed to round down period (requested: %llu, applied: %llu)\n", 87 386 state->period, s2.period); 88 387 ··· 91 390 last->period == s2.period && 92 391 last->duty_cycle > s2.duty_cycle && 93 392 last->duty_cycle <= state->duty_cycle) 94 - dev_warn(chip->dev, 393 + dev_warn(pwmchip_parent(chip), 95 394 ".apply didn't pick the best available duty cycle (requested: %llu/%llu, applied: %llu/%llu, possible: %llu/%llu)\n", 96 395 state->duty_cycle, state->period, 97 396 s2.duty_cycle, s2.period, 98 397 last->duty_cycle, last->period); 99 398 100 399 if (state->enabled && state->duty_cycle < s2.duty_cycle) 101 - dev_warn(chip->dev, 400 + dev_warn(pwmchip_parent(chip), 102 401 ".apply is supposed to round down duty_cycle (requested: %llu/%llu, applied: %llu/%llu)\n", 103 402 state->duty_cycle, state->period, 104 403 s2.duty_cycle, s2.period); 105 404 106 405 if (!state->enabled && s2.enabled && s2.duty_cycle > 0) 107 - dev_warn(chip->dev, 406 + dev_warn(pwmchip_parent(chip), 108 407 "requested disabled, but yielded enabled with duty > 0\n"); 109 408 110 409 /* reapply the state that the driver reported being configured. */ ··· 112 411 trace_pwm_apply(pwm, &s1, err); 113 412 if (err) { 114 413 *last = s1; 115 - dev_err(chip->dev, "failed to reapply current setting\n"); 414 + dev_err(pwmchip_parent(chip), "failed to reapply current setting\n"); 116 415 return; 117 416 } 118 417 ··· 127 426 s1.polarity != last->polarity || 128 427 (s1.enabled && s1.period != last->period) || 129 428 (s1.enabled && s1.duty_cycle != last->duty_cycle)) { 130 - dev_err(chip->dev, 429 + dev_err(pwmchip_parent(chip), 131 430 ".apply is not idempotent (ena=%d pol=%d %llu/%llu) -> (ena=%d pol=%d %llu/%llu)\n", 132 431 s1.enabled, s1.polarity, s1.duty_cycle, s1.period, 133 432 last->enabled, last->polarity, last->duty_cycle, ··· 225 524 EXPORT_SYMBOL_GPL(pwm_apply_atomic); 226 525 227 526 /** 228 - * pwm_capture() - capture and report a PWM signal 229 - * @pwm: PWM device 230 - * @result: structure to fill with capture result 231 - * @timeout: time to wait, in milliseconds, before giving up on capture 232 - * 233 - * Returns: 0 on success or a negative error code on failure. 234 - */ 235 - int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, 236 - unsigned long timeout) 237 - { 238 - int err; 239 - 240 - if (!pwm || !pwm->chip->ops) 241 - return -EINVAL; 242 - 243 - if (!pwm->chip->ops->capture) 244 - return -ENOSYS; 245 - 246 - mutex_lock(&pwm_lock); 247 - err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); 248 - mutex_unlock(&pwm_lock); 249 - 250 - return err; 251 - } 252 - EXPORT_SYMBOL_GPL(pwm_capture); 253 - 254 - /** 255 527 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments 256 528 * @pwm: PWM device 257 529 * ··· 280 606 } 281 607 EXPORT_SYMBOL_GPL(pwm_adjust_config); 282 608 283 - static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode) 609 + /** 610 + * pwm_capture() - capture and report a PWM signal 611 + * @pwm: PWM device 612 + * @result: structure to fill with capture result 613 + * @timeout: time to wait, in milliseconds, before giving up on capture 614 + * 615 + * Returns: 0 on success or a negative error code on failure. 616 + */ 617 + int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, 618 + unsigned long timeout) 619 + { 620 + int err; 621 + 622 + if (!pwm || !pwm->chip->ops) 623 + return -EINVAL; 624 + 625 + if (!pwm->chip->ops->capture) 626 + return -ENOSYS; 627 + 628 + mutex_lock(&pwm_lock); 629 + err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout); 630 + mutex_unlock(&pwm_lock); 631 + 632 + return err; 633 + } 634 + EXPORT_SYMBOL_GPL(pwm_capture); 635 + 636 + static struct pwm_chip *pwmchip_find_by_name(const char *name) 284 637 { 285 638 struct pwm_chip *chip; 286 639 unsigned long id, tmp; 287 640 641 + if (!name) 642 + return NULL; 643 + 288 644 mutex_lock(&pwm_lock); 289 645 290 - idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) 291 - if (chip->dev && device_match_fwnode(chip->dev, fwnode)) { 646 + idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) { 647 + const char *chip_name = dev_name(pwmchip_parent(chip)); 648 + 649 + if (chip_name && strcmp(chip_name, name) == 0) { 292 650 mutex_unlock(&pwm_lock); 293 651 return chip; 294 652 } 653 + } 295 654 296 655 mutex_unlock(&pwm_lock); 297 656 298 - return ERR_PTR(-EPROBE_DEFER); 657 + return NULL; 299 658 } 659 + 660 + static int pwm_device_request(struct pwm_device *pwm, const char *label) 661 + { 662 + int err; 663 + struct pwm_chip *chip = pwm->chip; 664 + const struct pwm_ops *ops = chip->ops; 665 + 666 + if (test_bit(PWMF_REQUESTED, &pwm->flags)) 667 + return -EBUSY; 668 + 669 + if (!try_module_get(chip->owner)) 670 + return -ENODEV; 671 + 672 + if (ops->request) { 673 + err = ops->request(chip, pwm); 674 + if (err) { 675 + module_put(chip->owner); 676 + return err; 677 + } 678 + } 679 + 680 + if (ops->get_state) { 681 + /* 682 + * Zero-initialize state because most drivers are unaware of 683 + * .usage_power. The other members of state are supposed to be 684 + * set by lowlevel drivers. We still initialize the whole 685 + * structure for simplicity even though this might paper over 686 + * faulty implementations of .get_state(). 687 + */ 688 + struct pwm_state state = { 0, }; 689 + 690 + err = ops->get_state(chip, pwm, &state); 691 + trace_pwm_get(pwm, &state, err); 692 + 693 + if (!err) 694 + pwm->state = state; 695 + 696 + if (IS_ENABLED(CONFIG_PWM_DEBUG)) 697 + pwm->last = pwm->state; 698 + } 699 + 700 + set_bit(PWMF_REQUESTED, &pwm->flags); 701 + pwm->label = label; 702 + 703 + return 0; 704 + } 705 + 706 + /** 707 + * pwm_request_from_chip() - request a PWM device relative to a PWM chip 708 + * @chip: PWM chip 709 + * @index: per-chip index of the PWM to request 710 + * @label: a literal description string of this PWM 711 + * 712 + * Returns: A pointer to the PWM device at the given index of the given PWM 713 + * chip. A negative error code is returned if the index is not valid for the 714 + * specified PWM chip or if the PWM device cannot be requested. 715 + */ 716 + struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip, 717 + unsigned int index, 718 + const char *label) 719 + { 720 + struct pwm_device *pwm; 721 + int err; 722 + 723 + if (!chip || index >= chip->npwm) 724 + return ERR_PTR(-EINVAL); 725 + 726 + mutex_lock(&pwm_lock); 727 + pwm = &chip->pwms[index]; 728 + 729 + err = pwm_device_request(pwm, label); 730 + if (err < 0) 731 + pwm = ERR_PTR(err); 732 + 733 + mutex_unlock(&pwm_lock); 734 + return pwm; 735 + } 736 + EXPORT_SYMBOL_GPL(pwm_request_from_chip); 737 + 738 + 739 + struct pwm_device * 740 + of_pwm_xlate_with_flags(struct pwm_chip *chip, const struct of_phandle_args *args) 741 + { 742 + struct pwm_device *pwm; 743 + 744 + /* period in the second cell and flags in the third cell are optional */ 745 + if (args->args_count < 1) 746 + return ERR_PTR(-EINVAL); 747 + 748 + pwm = pwm_request_from_chip(chip, args->args[0], NULL); 749 + if (IS_ERR(pwm)) 750 + return pwm; 751 + 752 + if (args->args_count > 1) 753 + pwm->args.period = args->args[1]; 754 + 755 + pwm->args.polarity = PWM_POLARITY_NORMAL; 756 + if (args->args_count > 2 && args->args[2] & PWM_POLARITY_INVERTED) 757 + pwm->args.polarity = PWM_POLARITY_INVERSED; 758 + 759 + return pwm; 760 + } 761 + EXPORT_SYMBOL_GPL(of_pwm_xlate_with_flags); 762 + 763 + struct pwm_device * 764 + of_pwm_single_xlate(struct pwm_chip *chip, const struct of_phandle_args *args) 765 + { 766 + struct pwm_device *pwm; 767 + 768 + pwm = pwm_request_from_chip(chip, 0, NULL); 769 + if (IS_ERR(pwm)) 770 + return pwm; 771 + 772 + if (args->args_count > 1) 773 + pwm->args.period = args->args[0]; 774 + 775 + pwm->args.polarity = PWM_POLARITY_NORMAL; 776 + if (args->args_count > 1 && args->args[1] & PWM_POLARITY_INVERTED) 777 + pwm->args.polarity = PWM_POLARITY_INVERSED; 778 + 779 + return pwm; 780 + } 781 + EXPORT_SYMBOL_GPL(of_pwm_single_xlate); 782 + 783 + #define PWMCHIP_ALIGN ARCH_DMA_MINALIGN 784 + 785 + static void *pwmchip_priv(struct pwm_chip *chip) 786 + { 787 + return (void *)chip + ALIGN(sizeof(*chip), PWMCHIP_ALIGN); 788 + } 789 + 790 + /* This is the counterpart to pwmchip_alloc() */ 791 + void pwmchip_put(struct pwm_chip *chip) 792 + { 793 + kfree(chip); 794 + } 795 + EXPORT_SYMBOL_GPL(pwmchip_put); 796 + 797 + struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv) 798 + { 799 + struct pwm_chip *chip; 800 + size_t alloc_size; 801 + 802 + alloc_size = size_add(ALIGN(sizeof(*chip), PWMCHIP_ALIGN), sizeof_priv); 803 + 804 + chip = kzalloc(alloc_size, GFP_KERNEL); 805 + if (!chip) 806 + return ERR_PTR(-ENOMEM); 807 + 808 + chip->dev = parent; 809 + chip->npwm = npwm; 810 + 811 + pwmchip_set_drvdata(chip, pwmchip_priv(chip)); 812 + 813 + return chip; 814 + } 815 + EXPORT_SYMBOL_GPL(pwmchip_alloc); 816 + 817 + static void devm_pwmchip_put(void *data) 818 + { 819 + struct pwm_chip *chip = data; 820 + 821 + pwmchip_put(chip); 822 + } 823 + 824 + struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv) 825 + { 826 + struct pwm_chip *chip; 827 + int ret; 828 + 829 + chip = pwmchip_alloc(parent, npwm, sizeof_priv); 830 + if (IS_ERR(chip)) 831 + return chip; 832 + 833 + ret = devm_add_action_or_reset(parent, devm_pwmchip_put, chip); 834 + if (ret) 835 + return ERR_PTR(ret); 836 + 837 + return chip; 838 + } 839 + EXPORT_SYMBOL_GPL(devm_pwmchip_alloc); 840 + 841 + static void of_pwmchip_add(struct pwm_chip *chip) 842 + { 843 + if (!pwmchip_parent(chip) || !pwmchip_parent(chip)->of_node) 844 + return; 845 + 846 + if (!chip->of_xlate) 847 + chip->of_xlate = of_pwm_xlate_with_flags; 848 + 849 + of_node_get(pwmchip_parent(chip)->of_node); 850 + } 851 + 852 + static void of_pwmchip_remove(struct pwm_chip *chip) 853 + { 854 + if (pwmchip_parent(chip)) 855 + of_node_put(pwmchip_parent(chip)->of_node); 856 + } 857 + 858 + static bool pwm_ops_check(const struct pwm_chip *chip) 859 + { 860 + const struct pwm_ops *ops = chip->ops; 861 + 862 + if (!ops->apply) 863 + return false; 864 + 865 + if (IS_ENABLED(CONFIG_PWM_DEBUG) && !ops->get_state) 866 + dev_warn(pwmchip_parent(chip), 867 + "Please implement the .get_state() callback\n"); 868 + 869 + return true; 870 + } 871 + 872 + /** 873 + * __pwmchip_add() - register a new PWM chip 874 + * @chip: the PWM chip to add 875 + * @owner: reference to the module providing the chip. 876 + * 877 + * Register a new PWM chip. @owner is supposed to be THIS_MODULE, use the 878 + * pwmchip_add wrapper to do this right. 879 + * 880 + * Returns: 0 on success or a negative error code on failure. 881 + */ 882 + int __pwmchip_add(struct pwm_chip *chip, struct module *owner) 883 + { 884 + unsigned int i; 885 + int ret; 886 + 887 + if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm) 888 + return -EINVAL; 889 + 890 + if (!pwm_ops_check(chip)) 891 + return -EINVAL; 892 + 893 + chip->owner = owner; 894 + 895 + chip->pwms = kcalloc(chip->npwm, sizeof(*chip->pwms), GFP_KERNEL); 896 + if (!chip->pwms) 897 + return -ENOMEM; 898 + 899 + mutex_lock(&pwm_lock); 900 + 901 + ret = idr_alloc(&pwm_chips, chip, 0, 0, GFP_KERNEL); 902 + if (ret < 0) { 903 + mutex_unlock(&pwm_lock); 904 + kfree(chip->pwms); 905 + return ret; 906 + } 907 + 908 + chip->id = ret; 909 + 910 + for (i = 0; i < chip->npwm; i++) { 911 + struct pwm_device *pwm = &chip->pwms[i]; 912 + 913 + pwm->chip = chip; 914 + pwm->hwpwm = i; 915 + } 916 + 917 + mutex_unlock(&pwm_lock); 918 + 919 + if (IS_ENABLED(CONFIG_OF)) 920 + of_pwmchip_add(chip); 921 + 922 + pwmchip_sysfs_export(chip); 923 + 924 + return 0; 925 + } 926 + EXPORT_SYMBOL_GPL(__pwmchip_add); 927 + 928 + /** 929 + * pwmchip_remove() - remove a PWM chip 930 + * @chip: the PWM chip to remove 931 + * 932 + * Removes a PWM chip. 933 + */ 934 + void pwmchip_remove(struct pwm_chip *chip) 935 + { 936 + pwmchip_sysfs_unexport(chip); 937 + 938 + if (IS_ENABLED(CONFIG_OF)) 939 + of_pwmchip_remove(chip); 940 + 941 + mutex_lock(&pwm_lock); 942 + 943 + idr_remove(&pwm_chips, chip->id); 944 + 945 + mutex_unlock(&pwm_lock); 946 + 947 + kfree(chip->pwms); 948 + } 949 + EXPORT_SYMBOL_GPL(pwmchip_remove); 950 + 951 + static void devm_pwmchip_remove(void *data) 952 + { 953 + struct pwm_chip *chip = data; 954 + 955 + pwmchip_remove(chip); 956 + } 957 + 958 + int __devm_pwmchip_add(struct device *dev, struct pwm_chip *chip, struct module *owner) 959 + { 960 + int ret; 961 + 962 + ret = __pwmchip_add(chip, owner); 963 + if (ret) 964 + return ret; 965 + 966 + return devm_add_action_or_reset(dev, devm_pwmchip_remove, chip); 967 + } 968 + EXPORT_SYMBOL_GPL(__devm_pwmchip_add); 300 969 301 970 static struct device_link *pwm_device_link_add(struct device *dev, 302 971 struct pwm_device *pwm) ··· 652 635 * impact the PM sequence ordering: the PWM supplier may get 653 636 * suspended before the consumer. 654 637 */ 655 - dev_warn(pwm->chip->dev, 638 + dev_warn(pwmchip_parent(pwm->chip), 656 639 "No consumer device specified to create a link to\n"); 657 640 return NULL; 658 641 } 659 642 660 - dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER); 643 + dl = device_link_add(dev, pwmchip_parent(pwm->chip), DL_FLAG_AUTOREMOVE_CONSUMER); 661 644 if (!dl) { 662 645 dev_err(dev, "failed to create device link to %s\n", 663 - dev_name(pwm->chip->dev)); 646 + dev_name(pwmchip_parent(pwm->chip))); 664 647 return ERR_PTR(-EINVAL); 665 648 } 666 649 667 650 return dl; 651 + } 652 + 653 + static struct pwm_chip *fwnode_to_pwmchip(struct fwnode_handle *fwnode) 654 + { 655 + struct pwm_chip *chip; 656 + unsigned long id, tmp; 657 + 658 + mutex_lock(&pwm_lock); 659 + 660 + idr_for_each_entry_ul(&pwm_chips, chip, tmp, id) 661 + if (pwmchip_parent(chip) && device_match_fwnode(pwmchip_parent(chip), fwnode)) { 662 + mutex_unlock(&pwm_lock); 663 + return chip; 664 + } 665 + 666 + mutex_unlock(&pwm_lock); 667 + 668 + return ERR_PTR(-EPROBE_DEFER); 668 669 } 669 670 670 671 /** ··· 818 783 819 784 return pwm; 820 785 } 786 + 787 + static DEFINE_MUTEX(pwm_lookup_lock); 788 + static LIST_HEAD(pwm_lookup_list); 821 789 822 790 /** 823 791 * pwm_add_table() - register PWM device consumers ··· 1143 1105 1144 1106 seq_printf(s, "%s%d: %s/%s, %d PWM device%s\n", 1145 1107 (char *)s->private, chip->id, 1146 - chip->dev->bus ? chip->dev->bus->name : "no-bus", 1147 - dev_name(chip->dev), chip->npwm, 1108 + pwmchip_parent(chip)->bus ? pwmchip_parent(chip)->bus->name : "no-bus", 1109 + dev_name(pwmchip_parent(chip)), chip->npwm, 1148 1110 (chip->npwm != 1) ? "s" : ""); 1149 1111 1150 1112 pwm_dbg_show(chip, s);
+18 -18
drivers/pwm/pwm-ab8500.c
··· 24 24 #define AB8500_PWM_CLKRATE 9600000 25 25 26 26 struct ab8500_pwm_chip { 27 - struct pwm_chip chip; 28 27 unsigned int hwid; 29 28 }; 30 29 31 30 static struct ab8500_pwm_chip *ab8500_pwm_from_chip(struct pwm_chip *chip) 32 31 { 33 - return container_of(chip, struct ab8500_pwm_chip, chip); 32 + return pwmchip_get_drvdata(chip); 34 33 } 35 34 36 35 static int ab8500_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, ··· 91 92 * when disabled. 92 93 */ 93 94 if (!state->enabled || duty_steps == 0) { 94 - ret = abx500_mask_and_set_register_interruptible(chip->dev, 95 + ret = abx500_mask_and_set_register_interruptible(pwmchip_parent(chip), 95 96 AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG, 96 97 1 << ab8500->hwid, 0); 97 98 98 99 if (ret < 0) 99 - dev_err(chip->dev, "%s: Failed to disable PWM, Error %d\n", 100 + dev_err(pwmchip_parent(chip), "%s: Failed to disable PWM, Error %d\n", 100 101 pwm->label, ret); 101 102 return ret; 102 103 } ··· 114 115 115 116 reg = AB8500_PWM_OUT_CTRL1_REG + (ab8500->hwid * 2); 116 117 117 - ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC, 118 + ret = abx500_set_register_interruptible(pwmchip_parent(chip), AB8500_MISC, 118 119 reg, lower_val); 119 120 if (ret < 0) 120 121 return ret; 121 122 122 - ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC, 123 + ret = abx500_set_register_interruptible(pwmchip_parent(chip), AB8500_MISC, 123 124 (reg + 1), higher_val); 124 125 if (ret < 0) 125 126 return ret; 126 127 127 128 /* enable */ 128 - ret = abx500_mask_and_set_register_interruptible(chip->dev, 129 + ret = abx500_mask_and_set_register_interruptible(pwmchip_parent(chip), 129 130 AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG, 130 131 1 << ab8500->hwid, 1 << ab8500->hwid); 131 132 if (ret < 0) 132 - dev_err(chip->dev, "%s: Failed to enable PWM, Error %d\n", 133 + dev_err(pwmchip_parent(chip), "%s: Failed to enable PWM, Error %d\n", 133 134 pwm->label, ret); 134 135 135 136 return ret; ··· 143 144 struct ab8500_pwm_chip *ab8500 = ab8500_pwm_from_chip(chip); 144 145 unsigned int div, duty_steps; 145 146 146 - ret = abx500_get_register_interruptible(chip->dev, AB8500_MISC, 147 + ret = abx500_get_register_interruptible(pwmchip_parent(chip), AB8500_MISC, 147 148 AB8500_PWM_OUT_CTRL7_REG, 148 149 &ctrl7); 149 150 if (ret) ··· 156 157 return 0; 157 158 } 158 159 159 - ret = abx500_get_register_interruptible(chip->dev, AB8500_MISC, 160 + ret = abx500_get_register_interruptible(pwmchip_parent(chip), AB8500_MISC, 160 161 AB8500_PWM_OUT_CTRL1_REG + (ab8500->hwid * 2), 161 162 &lower_val); 162 163 if (ret) 163 164 return ret; 164 165 165 - ret = abx500_get_register_interruptible(chip->dev, AB8500_MISC, 166 + ret = abx500_get_register_interruptible(pwmchip_parent(chip), AB8500_MISC, 166 167 AB8500_PWM_OUT_CTRL2_REG + (ab8500->hwid * 2), 167 168 &higher_val); 168 169 if (ret) ··· 184 185 185 186 static int ab8500_pwm_probe(struct platform_device *pdev) 186 187 { 188 + struct pwm_chip *chip; 187 189 struct ab8500_pwm_chip *ab8500; 188 190 int err; 189 191 ··· 195 195 * Nothing to be done in probe, this is required to get the 196 196 * device which is required for ab8500 read and write 197 197 */ 198 - ab8500 = devm_kzalloc(&pdev->dev, sizeof(*ab8500), GFP_KERNEL); 199 - if (ab8500 == NULL) 200 - return -ENOMEM; 198 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*ab8500)); 199 + if (IS_ERR(chip)) 200 + return PTR_ERR(chip); 201 201 202 - ab8500->chip.dev = &pdev->dev; 203 - ab8500->chip.ops = &ab8500_pwm_ops; 204 - ab8500->chip.npwm = 1; 202 + ab8500 = ab8500_pwm_from_chip(chip); 203 + 204 + chip->ops = &ab8500_pwm_ops; 205 205 ab8500->hwid = pdev->id - 1; 206 206 207 - err = devm_pwmchip_add(&pdev->dev, &ab8500->chip); 207 + err = devm_pwmchip_add(&pdev->dev, chip); 208 208 if (err < 0) 209 209 return dev_err_probe(&pdev->dev, err, "Failed to add pwm chip\n"); 210 210
+9 -9
drivers/pwm/pwm-apple.c
··· 32 32 #define APPLE_PWM_CTRL_OUTPUT_ENABLE BIT(14) 33 33 34 34 struct apple_pwm { 35 - struct pwm_chip chip; 36 35 void __iomem *base; 37 36 u64 clkrate; 38 37 }; 39 38 40 39 static inline struct apple_pwm *to_apple_pwm(struct pwm_chip *chip) 41 40 { 42 - return container_of(chip, struct apple_pwm, chip); 41 + return pwmchip_get_drvdata(chip); 43 42 } 44 43 45 44 static int apple_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, ··· 102 103 103 104 static int apple_pwm_probe(struct platform_device *pdev) 104 105 { 106 + struct pwm_chip *chip; 105 107 struct apple_pwm *fpwm; 106 108 struct clk *clk; 107 109 int ret; 108 110 109 - fpwm = devm_kzalloc(&pdev->dev, sizeof(*fpwm), GFP_KERNEL); 110 - if (!fpwm) 111 - return -ENOMEM; 111 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*fpwm)); 112 + if (IS_ERR(chip)) 113 + return PTR_ERR(chip); 114 + 115 + fpwm = to_apple_pwm(chip); 112 116 113 117 fpwm->base = devm_platform_ioremap_resource(pdev, 0); 114 118 if (IS_ERR(fpwm->base)) ··· 131 129 if (fpwm->clkrate > NSEC_PER_SEC) 132 130 return dev_err_probe(&pdev->dev, -EINVAL, "pwm clock out of range"); 133 131 134 - fpwm->chip.dev = &pdev->dev; 135 - fpwm->chip.npwm = 1; 136 - fpwm->chip.ops = &apple_pwm_ops; 132 + chip->ops = &apple_pwm_ops; 137 133 138 - ret = devm_pwmchip_add(&pdev->dev, &fpwm->chip); 134 + ret = devm_pwmchip_add(&pdev->dev, chip); 139 135 if (ret < 0) 140 136 return dev_err_probe(&pdev->dev, ret, "unable to add pwm chip"); 141 137
+21 -21
drivers/pwm/pwm-atmel-hlcdc.c
··· 28 28 }; 29 29 30 30 struct atmel_hlcdc_pwm { 31 - struct pwm_chip chip; 32 31 struct atmel_hlcdc *hlcdc; 33 32 struct clk *cur_clk; 34 33 const struct atmel_hlcdc_pwm_errata *errata; ··· 35 36 36 37 static inline struct atmel_hlcdc_pwm *to_atmel_hlcdc_pwm(struct pwm_chip *chip) 37 38 { 38 - return container_of(chip, struct atmel_hlcdc_pwm, chip); 39 + return pwmchip_get_drvdata(chip); 39 40 } 40 41 41 42 static int atmel_hlcdc_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, ··· 181 182 182 183 static int atmel_hlcdc_pwm_suspend(struct device *dev) 183 184 { 184 - struct atmel_hlcdc_pwm *atmel = dev_get_drvdata(dev); 185 + struct pwm_chip *chip = dev_get_drvdata(dev); 186 + struct atmel_hlcdc_pwm *atmel = to_atmel_hlcdc_pwm(chip); 187 + struct pwm_device *pwm = &chip->pwms[0]; 185 188 186 189 /* Keep the periph clock enabled if the PWM is still running. */ 187 - if (pwm_is_enabled(&atmel->chip.pwms[0])) 190 + if (!pwm->state.enabled) 188 191 clk_disable_unprepare(atmel->hlcdc->periph_clk); 189 192 190 193 return 0; ··· 194 193 195 194 static int atmel_hlcdc_pwm_resume(struct device *dev) 196 195 { 197 - struct atmel_hlcdc_pwm *atmel = dev_get_drvdata(dev); 198 - struct pwm_state state; 196 + struct pwm_chip *chip = dev_get_drvdata(dev); 197 + struct atmel_hlcdc_pwm *atmel = to_atmel_hlcdc_pwm(chip); 198 + struct pwm_device *pwm = &chip->pwms[0]; 199 199 int ret; 200 200 201 - pwm_get_state(&atmel->chip.pwms[0], &state); 202 - 203 201 /* Re-enable the periph clock it was stopped during suspend. */ 204 - if (!state.enabled) { 202 + if (!pwm->state.enabled) { 205 203 ret = clk_prepare_enable(atmel->hlcdc->periph_clk); 206 204 if (ret) 207 205 return ret; 208 206 } 209 207 210 - return atmel_hlcdc_pwm_apply(&atmel->chip, &atmel->chip.pwms[0], 211 - &state); 208 + return atmel_hlcdc_pwm_apply(chip, pwm, &pwm->state); 212 209 } 213 210 214 211 static DEFINE_SIMPLE_DEV_PM_OPS(atmel_hlcdc_pwm_pm_ops, ··· 242 243 { 243 244 const struct of_device_id *match; 244 245 struct device *dev = &pdev->dev; 246 + struct pwm_chip *chip; 245 247 struct atmel_hlcdc_pwm *atmel; 246 248 struct atmel_hlcdc *hlcdc; 247 249 int ret; 248 250 249 251 hlcdc = dev_get_drvdata(dev->parent); 250 252 251 - atmel = devm_kzalloc(dev, sizeof(*atmel), GFP_KERNEL); 252 - if (!atmel) 253 - return -ENOMEM; 253 + chip = devm_pwmchip_alloc(dev, 1, sizeof(*atmel)); 254 + if (IS_ERR(chip)) 255 + return PTR_ERR(chip); 256 + atmel = to_atmel_hlcdc_pwm(chip); 254 257 255 258 ret = clk_prepare_enable(hlcdc->periph_clk); 256 259 if (ret) ··· 263 262 atmel->errata = match->data; 264 263 265 264 atmel->hlcdc = hlcdc; 266 - atmel->chip.ops = &atmel_hlcdc_pwm_ops; 267 - atmel->chip.dev = dev; 268 - atmel->chip.npwm = 1; 265 + chip->ops = &atmel_hlcdc_pwm_ops; 269 266 270 - ret = pwmchip_add(&atmel->chip); 267 + ret = pwmchip_add(chip); 271 268 if (ret) { 272 269 clk_disable_unprepare(hlcdc->periph_clk); 273 270 return ret; 274 271 } 275 272 276 - platform_set_drvdata(pdev, atmel); 273 + platform_set_drvdata(pdev, chip); 277 274 278 275 return 0; 279 276 } 280 277 281 278 static void atmel_hlcdc_pwm_remove(struct platform_device *pdev) 282 279 { 283 - struct atmel_hlcdc_pwm *atmel = platform_get_drvdata(pdev); 280 + struct pwm_chip *chip = platform_get_drvdata(pdev); 281 + struct atmel_hlcdc_pwm *atmel = to_atmel_hlcdc_pwm(chip); 284 282 285 - pwmchip_remove(&atmel->chip); 283 + pwmchip_remove(chip); 286 284 287 285 clk_disable_unprepare(atmel->hlcdc->periph_clk); 288 286 }
+17 -15
drivers/pwm/pwm-atmel-tcb.c
··· 47 47 }; 48 48 49 49 struct atmel_tcb_pwm_chip { 50 - struct pwm_chip chip; 51 50 spinlock_t lock; 52 51 u8 channel; 53 52 u8 width; ··· 62 63 63 64 static inline struct atmel_tcb_pwm_chip *to_tcb_chip(struct pwm_chip *chip) 64 65 { 65 - return container_of(chip, struct atmel_tcb_pwm_chip, chip); 66 + return pwmchip_get_drvdata(chip); 66 67 } 67 68 68 69 static int atmel_tcb_pwm_request(struct pwm_chip *chip, ··· 326 327 if ((atcbpwm && atcbpwm->duty > 0 && 327 328 atcbpwm->duty != atcbpwm->period) && 328 329 (atcbpwm->div != i || atcbpwm->period != period)) { 329 - dev_err(chip->dev, 330 + dev_err(pwmchip_parent(chip), 330 331 "failed to configure period_ns: PWM group already configured with a different value\n"); 331 332 return -EINVAL; 332 333 } ··· 387 388 388 389 static int atmel_tcb_pwm_probe(struct platform_device *pdev) 389 390 { 391 + struct pwm_chip *chip; 390 392 const struct of_device_id *match; 391 393 struct atmel_tcb_pwm_chip *tcbpwm; 392 394 const struct atmel_tcb_config *config; ··· 396 396 int err; 397 397 int channel; 398 398 399 - tcbpwm = devm_kzalloc(&pdev->dev, sizeof(*tcbpwm), GFP_KERNEL); 400 - if (tcbpwm == NULL) 401 - return -ENOMEM; 399 + chip = devm_pwmchip_alloc(&pdev->dev, NPWM, sizeof(*tcbpwm)); 400 + if (IS_ERR(chip)) 401 + return PTR_ERR(chip); 402 + tcbpwm = to_tcb_chip(chip); 402 403 403 404 err = of_property_read_u32(np, "reg", &channel); 404 405 if (err < 0) { ··· 437 436 } 438 437 } 439 438 440 - tcbpwm->chip.dev = &pdev->dev; 441 - tcbpwm->chip.ops = &atmel_tcb_pwm_ops; 442 - tcbpwm->chip.npwm = NPWM; 439 + chip->ops = &atmel_tcb_pwm_ops; 443 440 tcbpwm->channel = channel; 444 441 tcbpwm->width = config->counter_width; 445 442 ··· 447 448 448 449 spin_lock_init(&tcbpwm->lock); 449 450 450 - err = pwmchip_add(&tcbpwm->chip); 451 + err = pwmchip_add(chip); 451 452 if (err < 0) 452 453 goto err_disable_clk; 453 454 454 - platform_set_drvdata(pdev, tcbpwm); 455 + platform_set_drvdata(pdev, chip); 455 456 456 457 return 0; 457 458 ··· 472 473 473 474 static void atmel_tcb_pwm_remove(struct platform_device *pdev) 474 475 { 475 - struct atmel_tcb_pwm_chip *tcbpwm = platform_get_drvdata(pdev); 476 + struct pwm_chip *chip = platform_get_drvdata(pdev); 477 + struct atmel_tcb_pwm_chip *tcbpwm = to_tcb_chip(chip); 476 478 477 - pwmchip_remove(&tcbpwm->chip); 479 + pwmchip_remove(chip); 478 480 479 481 clk_disable_unprepare(tcbpwm->slow_clk); 480 482 clk_put(tcbpwm->gclk); ··· 491 491 492 492 static int atmel_tcb_pwm_suspend(struct device *dev) 493 493 { 494 - struct atmel_tcb_pwm_chip *tcbpwm = dev_get_drvdata(dev); 494 + struct pwm_chip *chip = dev_get_drvdata(dev); 495 + struct atmel_tcb_pwm_chip *tcbpwm = to_tcb_chip(chip); 495 496 struct atmel_tcb_channel *chan = &tcbpwm->bkup; 496 497 unsigned int channel = tcbpwm->channel; 497 498 ··· 506 505 507 506 static int atmel_tcb_pwm_resume(struct device *dev) 508 507 { 509 - struct atmel_tcb_pwm_chip *tcbpwm = dev_get_drvdata(dev); 508 + struct pwm_chip *chip = dev_get_drvdata(dev); 509 + struct atmel_tcb_pwm_chip *tcbpwm = to_tcb_chip(chip); 510 510 struct atmel_tcb_channel *chan = &tcbpwm->bkup; 511 511 unsigned int channel = tcbpwm->channel; 512 512
+22 -25
drivers/pwm/pwm-atmel.c
··· 77 77 }; 78 78 79 79 struct atmel_pwm_chip { 80 - struct pwm_chip chip; 81 80 struct clk *clk; 82 81 void __iomem *base; 83 82 const struct atmel_pwm_data *data; ··· 98 99 99 100 static inline struct atmel_pwm_chip *to_atmel_pwm_chip(struct pwm_chip *chip) 100 101 { 101 - return container_of(chip, struct atmel_pwm_chip, chip); 102 + return pwmchip_get_drvdata(chip); 102 103 } 103 104 104 105 static inline u32 atmel_pwm_readl(struct atmel_pwm_chip *chip, ··· 209 210 shift = fls(cycles) - atmel_pwm->data->cfg.period_bits; 210 211 211 212 if (shift > PWM_MAX_PRES) { 212 - dev_err(chip->dev, "pres exceeds the maximum value\n"); 213 + dev_err(pwmchip_parent(chip), "pres exceeds the maximum value\n"); 213 214 return -EINVAL; 214 215 } else if (shift > 0) { 215 216 *pres = shift; ··· 293 294 const struct pwm_state *state) 294 295 { 295 296 struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip); 296 - struct pwm_state cstate; 297 297 unsigned long cprd, cdty; 298 298 u32 pres, val; 299 299 int ret; 300 300 301 - pwm_get_state(pwm, &cstate); 302 - 303 301 if (state->enabled) { 304 302 unsigned long clkrate = clk_get_rate(atmel_pwm->clk); 305 303 306 - if (cstate.enabled && 307 - cstate.polarity == state->polarity && 308 - cstate.period == state->period) { 304 + if (pwm->state.enabled && 305 + pwm->state.polarity == state->polarity && 306 + pwm->state.period == state->period) { 309 307 u32 cmr = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, PWM_CMR); 310 308 311 309 cprd = atmel_pwm_ch_readl(atmel_pwm, pwm->hwpwm, ··· 317 321 ret = atmel_pwm_calculate_cprd_and_pres(chip, clkrate, state, &cprd, 318 322 &pres); 319 323 if (ret) { 320 - dev_err(chip->dev, 324 + dev_err(pwmchip_parent(chip), 321 325 "failed to calculate cprd and prescaler\n"); 322 326 return ret; 323 327 } 324 328 325 329 atmel_pwm_calculate_cdty(state, clkrate, cprd, pres, &cdty); 326 330 327 - if (cstate.enabled) { 331 + if (pwm->state.enabled) { 328 332 atmel_pwm_disable(chip, pwm, false); 329 333 } else { 330 334 ret = clk_enable(atmel_pwm->clk); 331 335 if (ret) { 332 - dev_err(chip->dev, "failed to enable clock\n"); 336 + dev_err(pwmchip_parent(chip), "failed to enable clock\n"); 333 337 return ret; 334 338 } 335 339 } ··· 344 348 atmel_pwm_ch_writel(atmel_pwm, pwm->hwpwm, PWM_CMR, val); 345 349 atmel_pwm_set_cprd_cdty(chip, pwm, cprd, cdty); 346 350 atmel_pwm_writel(atmel_pwm, PWM_ENA, 1 << pwm->hwpwm); 347 - } else if (cstate.enabled) { 351 + } else if (pwm->state.enabled) { 348 352 atmel_pwm_disable(chip, pwm, true); 349 353 } 350 354 ··· 458 462 }; 459 463 MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids); 460 464 461 - static int atmel_pwm_enable_clk_if_on(struct atmel_pwm_chip *atmel_pwm, bool on) 465 + static int atmel_pwm_enable_clk_if_on(struct pwm_chip *chip, bool on) 462 466 { 467 + struct atmel_pwm_chip *atmel_pwm = to_atmel_pwm_chip(chip); 463 468 unsigned int i, cnt = 0; 464 469 unsigned long sr; 465 470 int ret = 0; ··· 469 472 if (!sr) 470 473 return 0; 471 474 472 - cnt = bitmap_weight(&sr, atmel_pwm->chip.npwm); 475 + cnt = bitmap_weight(&sr, chip->npwm); 473 476 474 477 if (!on) 475 478 goto disable_clk; ··· 477 480 for (i = 0; i < cnt; i++) { 478 481 ret = clk_enable(atmel_pwm->clk); 479 482 if (ret) { 480 - dev_err(atmel_pwm->chip.dev, 483 + dev_err(pwmchip_parent(chip), 481 484 "failed to enable clock for pwm %pe\n", 482 485 ERR_PTR(ret)); 483 486 ··· 498 501 static int atmel_pwm_probe(struct platform_device *pdev) 499 502 { 500 503 struct atmel_pwm_chip *atmel_pwm; 504 + struct pwm_chip *chip; 501 505 int ret; 502 506 503 - atmel_pwm = devm_kzalloc(&pdev->dev, sizeof(*atmel_pwm), GFP_KERNEL); 504 - if (!atmel_pwm) 505 - return -ENOMEM; 507 + chip = devm_pwmchip_alloc(&pdev->dev, 4, sizeof(*atmel_pwm)); 508 + if (IS_ERR(chip)) 509 + return PTR_ERR(chip); 506 510 511 + atmel_pwm = to_atmel_pwm_chip(chip); 507 512 atmel_pwm->data = of_device_get_match_data(&pdev->dev); 508 513 509 514 atmel_pwm->update_pending = 0; ··· 520 521 return dev_err_probe(&pdev->dev, PTR_ERR(atmel_pwm->clk), 521 522 "failed to get prepared PWM clock\n"); 522 523 523 - atmel_pwm->chip.dev = &pdev->dev; 524 - atmel_pwm->chip.ops = &atmel_pwm_ops; 525 - atmel_pwm->chip.npwm = 4; 524 + chip->ops = &atmel_pwm_ops; 526 525 527 - ret = atmel_pwm_enable_clk_if_on(atmel_pwm, true); 526 + ret = atmel_pwm_enable_clk_if_on(chip, true); 528 527 if (ret < 0) 529 528 return ret; 530 529 531 - ret = devm_pwmchip_add(&pdev->dev, &atmel_pwm->chip); 530 + ret = devm_pwmchip_add(&pdev->dev, chip); 532 531 if (ret < 0) { 533 532 dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n"); 534 533 goto disable_clk; ··· 535 538 return 0; 536 539 537 540 disable_clk: 538 - atmel_pwm_enable_clk_if_on(atmel_pwm, false); 541 + atmel_pwm_enable_clk_if_on(chip, false); 539 542 540 543 return ret; 541 544 }
+9 -10
drivers/pwm/pwm-bcm-iproc.c
··· 34 34 #define IPROC_PWM_PRESCALE_MAX 0x3f 35 35 36 36 struct iproc_pwmc { 37 - struct pwm_chip chip; 38 37 void __iomem *base; 39 38 struct clk *clk; 40 39 }; 41 40 42 41 static inline struct iproc_pwmc *to_iproc_pwmc(struct pwm_chip *chip) 43 42 { 44 - return container_of(chip, struct iproc_pwmc, chip); 43 + return pwmchip_get_drvdata(chip); 45 44 } 46 45 47 46 static void iproc_pwmc_enable(struct iproc_pwmc *ip, unsigned int channel) ··· 186 187 187 188 static int iproc_pwmc_probe(struct platform_device *pdev) 188 189 { 190 + struct pwm_chip *chip; 189 191 struct iproc_pwmc *ip; 190 192 unsigned int i; 191 193 u32 value; 192 194 int ret; 193 195 194 - ip = devm_kzalloc(&pdev->dev, sizeof(*ip), GFP_KERNEL); 195 - if (!ip) 196 - return -ENOMEM; 196 + chip = devm_pwmchip_alloc(&pdev->dev, 4, sizeof(*ip)); 197 + if (IS_ERR(chip)) 198 + return PTR_ERR(chip); 199 + ip = to_iproc_pwmc(chip); 197 200 198 201 platform_set_drvdata(pdev, ip); 199 202 200 - ip->chip.dev = &pdev->dev; 201 - ip->chip.ops = &iproc_pwm_ops; 202 - ip->chip.npwm = 4; 203 + chip->ops = &iproc_pwm_ops; 203 204 204 205 ip->base = devm_platform_ioremap_resource(pdev, 0); 205 206 if (IS_ERR(ip->base)) ··· 213 214 /* Set full drive and normal polarity for all channels */ 214 215 value = readl(ip->base + IPROC_PWM_CTRL_OFFSET); 215 216 216 - for (i = 0; i < ip->chip.npwm; i++) { 217 + for (i = 0; i < chip->npwm; i++) { 217 218 value &= ~(1 << IPROC_PWM_CTRL_TYPE_SHIFT(i)); 218 219 value |= 1 << IPROC_PWM_CTRL_POLARITY_SHIFT(i); 219 220 } 220 221 221 222 writel(value, ip->base + IPROC_PWM_CTRL_OFFSET); 222 223 223 - ret = devm_pwmchip_add(&pdev->dev, &ip->chip); 224 + ret = devm_pwmchip_add(&pdev->dev, chip); 224 225 if (ret < 0) 225 226 return dev_err_probe(&pdev->dev, ret, 226 227 "failed to add PWM chip\n");
+11 -12
drivers/pwm/pwm-bcm-kona.c
··· 56 56 #define DUTY_CYCLE_HIGH_MAX 0x00ffffff 57 57 58 58 struct kona_pwmc { 59 - struct pwm_chip chip; 60 59 void __iomem *base; 61 60 struct clk *clk; 62 61 }; 63 62 64 63 static inline struct kona_pwmc *to_kona_pwmc(struct pwm_chip *chip) 65 64 { 66 - return container_of(chip, struct kona_pwmc, chip); 65 + return pwmchip_get_drvdata(chip); 67 66 } 68 67 69 68 /* ··· 163 164 164 165 ret = clk_prepare_enable(kp->clk); 165 166 if (ret < 0) { 166 - dev_err(chip->dev, "failed to enable clock: %d\n", ret); 167 + dev_err(pwmchip_parent(chip), "failed to enable clock: %d\n", ret); 167 168 return ret; 168 169 } 169 170 ··· 192 193 193 194 ret = clk_prepare_enable(kp->clk); 194 195 if (ret < 0) { 195 - dev_err(chip->dev, "failed to enable clock: %d\n", ret); 196 + dev_err(pwmchip_parent(chip), "failed to enable clock: %d\n", ret); 196 197 return ret; 197 198 } 198 199 ··· 272 273 273 274 static int kona_pwmc_probe(struct platform_device *pdev) 274 275 { 276 + struct pwm_chip *chip; 275 277 struct kona_pwmc *kp; 276 278 unsigned int chan; 277 279 unsigned int value = 0; 278 280 int ret = 0; 279 281 280 - kp = devm_kzalloc(&pdev->dev, sizeof(*kp), GFP_KERNEL); 281 - if (kp == NULL) 282 - return -ENOMEM; 282 + chip = devm_pwmchip_alloc(&pdev->dev, 6, sizeof(*kp)); 283 + if (IS_ERR(chip)) 284 + return PTR_ERR(chip); 285 + kp = to_kona_pwmc(chip); 283 286 284 - kp->chip.dev = &pdev->dev; 285 - kp->chip.ops = &kona_pwm_ops; 286 - kp->chip.npwm = 6; 287 + chip->ops = &kona_pwm_ops; 287 288 288 289 kp->base = devm_platform_ioremap_resource(pdev, 0); 289 290 if (IS_ERR(kp->base)) ··· 303 304 } 304 305 305 306 /* Set push/pull for all channels */ 306 - for (chan = 0; chan < kp->chip.npwm; chan++) 307 + for (chan = 0; chan < chip->npwm; chan++) 307 308 value |= (1 << PWM_CONTROL_TYPE_SHIFT(chan)); 308 309 309 310 writel(value, kp->base + PWM_CONTROL_OFFSET); 310 311 311 312 clk_disable_unprepare(kp->clk); 312 313 313 - ret = devm_pwmchip_add(&pdev->dev, &kp->chip); 314 + ret = devm_pwmchip_add(&pdev->dev, chip); 314 315 if (ret < 0) 315 316 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret); 316 317
+9 -13
drivers/pwm/pwm-bcm2835.c
··· 24 24 #define PERIOD_MIN 0x2 25 25 26 26 struct bcm2835_pwm { 27 - struct pwm_chip chip; 28 - struct device *dev; 29 27 void __iomem *base; 30 28 struct clk *clk; 31 29 unsigned long rate; ··· 31 33 32 34 static inline struct bcm2835_pwm *to_bcm2835_pwm(struct pwm_chip *chip) 33 35 { 34 - return container_of(chip, struct bcm2835_pwm, chip); 36 + return pwmchip_get_drvdata(chip); 35 37 } 36 38 37 39 static int bcm2835_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) ··· 133 135 134 136 static int bcm2835_pwm_probe(struct platform_device *pdev) 135 137 { 138 + struct pwm_chip *chip; 136 139 struct bcm2835_pwm *pc; 137 140 int ret; 138 141 139 - pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); 140 - if (!pc) 141 - return -ENOMEM; 142 - 143 - pc->dev = &pdev->dev; 142 + chip = devm_pwmchip_alloc(&pdev->dev, 2, sizeof(*pc)); 143 + if (IS_ERR(chip)) 144 + return PTR_ERR(chip); 145 + pc = to_bcm2835_pwm(chip); 144 146 145 147 pc->base = devm_platform_ioremap_resource(pdev, 0); 146 148 if (IS_ERR(pc->base)) ··· 166 168 return dev_err_probe(&pdev->dev, -EINVAL, 167 169 "failed to get clock rate\n"); 168 170 169 - pc->chip.dev = &pdev->dev; 170 - pc->chip.ops = &bcm2835_pwm_ops; 171 - pc->chip.atomic = true; 172 - pc->chip.npwm = 2; 171 + chip->ops = &bcm2835_pwm_ops; 172 + chip->atomic = true; 173 173 174 174 platform_set_drvdata(pdev, pc); 175 175 176 - ret = devm_pwmchip_add(&pdev->dev, &pc->chip); 176 + ret = devm_pwmchip_add(&pdev->dev, chip); 177 177 if (ret < 0) 178 178 return dev_err_probe(&pdev->dev, ret, 179 179 "failed to add pwmchip\n");
+15 -14
drivers/pwm/pwm-berlin.c
··· 49 49 }; 50 50 51 51 struct berlin_pwm_chip { 52 - struct pwm_chip chip; 53 52 struct clk *clk; 54 53 void __iomem *base; 55 54 struct berlin_pwm_channel channel[BERLIN_PWM_NUMPWMS]; ··· 56 57 57 58 static inline struct berlin_pwm_chip *to_berlin_pwm_chip(struct pwm_chip *chip) 58 59 { 59 - return container_of(chip, struct berlin_pwm_chip, chip); 60 + return pwmchip_get_drvdata(chip); 60 61 } 61 62 62 63 static inline u32 berlin_pwm_readl(struct berlin_pwm_chip *bpc, ··· 197 198 198 199 static int berlin_pwm_probe(struct platform_device *pdev) 199 200 { 201 + struct pwm_chip *chip; 200 202 struct berlin_pwm_chip *bpc; 201 203 int ret; 202 204 203 - bpc = devm_kzalloc(&pdev->dev, sizeof(*bpc), GFP_KERNEL); 204 - if (!bpc) 205 - return -ENOMEM; 205 + chip = devm_pwmchip_alloc(&pdev->dev, BERLIN_PWM_NUMPWMS, sizeof(*bpc)); 206 + if (IS_ERR(chip)) 207 + return PTR_ERR(chip); 208 + bpc = to_berlin_pwm_chip(chip); 206 209 207 210 bpc->base = devm_platform_ioremap_resource(pdev, 0); 208 211 if (IS_ERR(bpc->base)) ··· 214 213 if (IS_ERR(bpc->clk)) 215 214 return PTR_ERR(bpc->clk); 216 215 217 - bpc->chip.dev = &pdev->dev; 218 - bpc->chip.ops = &berlin_pwm_ops; 219 - bpc->chip.npwm = BERLIN_PWM_NUMPWMS; 216 + chip->ops = &berlin_pwm_ops; 220 217 221 - ret = devm_pwmchip_add(&pdev->dev, &bpc->chip); 218 + ret = devm_pwmchip_add(&pdev->dev, chip); 222 219 if (ret < 0) 223 220 return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n"); 224 221 225 - platform_set_drvdata(pdev, bpc); 222 + platform_set_drvdata(pdev, chip); 226 223 227 224 return 0; 228 225 } 229 226 230 227 static int berlin_pwm_suspend(struct device *dev) 231 228 { 232 - struct berlin_pwm_chip *bpc = dev_get_drvdata(dev); 229 + struct pwm_chip *chip = dev_get_drvdata(dev); 230 + struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip); 233 231 unsigned int i; 234 232 235 - for (i = 0; i < bpc->chip.npwm; i++) { 233 + for (i = 0; i < chip->npwm; i++) { 236 234 struct berlin_pwm_channel *channel = &bpc->channel[i]; 237 235 238 236 channel->enable = berlin_pwm_readl(bpc, i, BERLIN_PWM_ENABLE); ··· 247 247 248 248 static int berlin_pwm_resume(struct device *dev) 249 249 { 250 - struct berlin_pwm_chip *bpc = dev_get_drvdata(dev); 250 + struct pwm_chip *chip = dev_get_drvdata(dev); 251 + struct berlin_pwm_chip *bpc = to_berlin_pwm_chip(chip); 251 252 unsigned int i; 252 253 int ret; 253 254 ··· 256 255 if (ret) 257 256 return ret; 258 257 259 - for (i = 0; i < bpc->chip.npwm; i++) { 258 + for (i = 0; i < chip->npwm; i++) { 260 259 struct berlin_pwm_channel *channel = &bpc->channel[i]; 261 260 262 261 berlin_pwm_writel(bpc, i, channel->ctrl, BERLIN_PWM_CONTROL);
+8 -9
drivers/pwm/pwm-brcmstb.c
··· 54 54 struct brcmstb_pwm { 55 55 void __iomem *base; 56 56 struct clk *clk; 57 - struct pwm_chip chip; 58 57 }; 59 58 60 59 static inline u32 brcmstb_pwm_readl(struct brcmstb_pwm *p, ··· 76 77 77 78 static inline struct brcmstb_pwm *to_brcmstb_pwm(struct pwm_chip *chip) 78 79 { 79 - return container_of(chip, struct brcmstb_pwm, chip); 80 + return pwmchip_get_drvdata(chip); 80 81 } 81 82 82 83 /* ··· 229 230 230 231 static int brcmstb_pwm_probe(struct platform_device *pdev) 231 232 { 233 + struct pwm_chip *chip; 232 234 struct brcmstb_pwm *p; 233 235 int ret; 234 236 235 - p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL); 236 - if (!p) 237 - return -ENOMEM; 237 + chip = devm_pwmchip_alloc(&pdev->dev, 2, sizeof(*p)); 238 + if (IS_ERR(chip)) 239 + return PTR_ERR(chip); 240 + p = to_brcmstb_pwm(chip); 238 241 239 242 p->clk = devm_clk_get_enabled(&pdev->dev, NULL); 240 243 if (IS_ERR(p->clk)) ··· 245 244 246 245 platform_set_drvdata(pdev, p); 247 246 248 - p->chip.dev = &pdev->dev; 249 - p->chip.ops = &brcmstb_pwm_ops; 250 - p->chip.npwm = 2; 247 + chip->ops = &brcmstb_pwm_ops; 251 248 252 249 p->base = devm_platform_ioremap_resource(pdev, 0); 253 250 if (IS_ERR(p->base)) 254 251 return PTR_ERR(p->base); 255 252 256 - ret = devm_pwmchip_add(&pdev->dev, &p->chip); 253 + ret = devm_pwmchip_add(&pdev->dev, chip); 257 254 if (ret) 258 255 return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n"); 259 256
+15 -12
drivers/pwm/pwm-clk.c
··· 28 28 #include <linux/pwm.h> 29 29 30 30 struct pwm_clk_chip { 31 - struct pwm_chip chip; 32 31 struct clk *clk; 33 32 bool clk_enabled; 34 33 }; 35 34 36 - #define to_pwm_clk_chip(_chip) container_of(_chip, struct pwm_clk_chip, chip) 35 + static inline struct pwm_clk_chip *to_pwm_clk_chip(struct pwm_chip *chip) 36 + { 37 + return pwmchip_get_drvdata(chip); 38 + } 37 39 38 40 static int pwm_clk_apply(struct pwm_chip *chip, struct pwm_device *pwm, 39 41 const struct pwm_state *state) ··· 83 81 84 82 static int pwm_clk_probe(struct platform_device *pdev) 85 83 { 84 + struct pwm_chip *chip; 86 85 struct pwm_clk_chip *pcchip; 87 86 int ret; 88 87 89 - pcchip = devm_kzalloc(&pdev->dev, sizeof(*pcchip), GFP_KERNEL); 90 - if (!pcchip) 91 - return -ENOMEM; 88 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*pcchip)); 89 + if (IS_ERR(chip)) 90 + return PTR_ERR(chip); 91 + pcchip = to_pwm_clk_chip(chip); 92 92 93 93 pcchip->clk = devm_clk_get_prepared(&pdev->dev, NULL); 94 94 if (IS_ERR(pcchip->clk)) 95 95 return dev_err_probe(&pdev->dev, PTR_ERR(pcchip->clk), 96 96 "Failed to get clock\n"); 97 97 98 - pcchip->chip.dev = &pdev->dev; 99 - pcchip->chip.ops = &pwm_clk_ops; 100 - pcchip->chip.npwm = 1; 98 + chip->ops = &pwm_clk_ops; 101 99 102 - ret = pwmchip_add(&pcchip->chip); 100 + ret = pwmchip_add(chip); 103 101 if (ret < 0) 104 102 return dev_err_probe(&pdev->dev, ret, "Failed to add pwm chip\n"); 105 103 106 - platform_set_drvdata(pdev, pcchip); 104 + platform_set_drvdata(pdev, chip); 107 105 return 0; 108 106 } 109 107 110 108 static void pwm_clk_remove(struct platform_device *pdev) 111 109 { 112 - struct pwm_clk_chip *pcchip = platform_get_drvdata(pdev); 110 + struct pwm_chip *chip = platform_get_drvdata(pdev); 111 + struct pwm_clk_chip *pcchip = to_pwm_clk_chip(chip); 113 112 114 - pwmchip_remove(&pcchip->chip); 113 + pwmchip_remove(chip); 115 114 116 115 if (pcchip->clk_enabled) 117 116 clk_disable(pcchip->clk);
+8 -20
drivers/pwm/pwm-clps711x.c
··· 12 12 #include <linux/pwm.h> 13 13 14 14 struct clps711x_chip { 15 - struct pwm_chip chip; 16 15 void __iomem *pmpcon; 17 16 struct clk *clk; 18 17 spinlock_t lock; ··· 19 20 20 21 static inline struct clps711x_chip *to_clps711x_chip(struct pwm_chip *chip) 21 22 { 22 - return container_of(chip, struct clps711x_chip, chip); 23 + return pwmchip_get_drvdata(chip); 23 24 } 24 25 25 26 static int clps711x_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) ··· 73 74 .apply = clps711x_pwm_apply, 74 75 }; 75 76 76 - static struct pwm_device *clps711x_pwm_xlate(struct pwm_chip *chip, 77 - const struct of_phandle_args *args) 78 - { 79 - if (args->args[0] >= chip->npwm) 80 - return ERR_PTR(-EINVAL); 81 - 82 - return pwm_request_from_chip(chip, args->args[0], NULL); 83 - } 84 - 85 77 static int clps711x_pwm_probe(struct platform_device *pdev) 86 78 { 79 + struct pwm_chip *chip; 87 80 struct clps711x_chip *priv; 88 81 89 - priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 90 - if (!priv) 91 - return -ENOMEM; 82 + chip = devm_pwmchip_alloc(&pdev->dev, 2, sizeof(*priv)); 83 + if (IS_ERR(chip)) 84 + return PTR_ERR(chip); 85 + priv = to_clps711x_chip(chip); 92 86 93 87 priv->pmpcon = devm_platform_ioremap_resource(pdev, 0); 94 88 if (IS_ERR(priv->pmpcon)) ··· 91 99 if (IS_ERR(priv->clk)) 92 100 return PTR_ERR(priv->clk); 93 101 94 - priv->chip.ops = &clps711x_pwm_ops; 95 - priv->chip.dev = &pdev->dev; 96 - priv->chip.npwm = 2; 97 - priv->chip.of_xlate = clps711x_pwm_xlate; 98 - priv->chip.of_pwm_n_cells = 1; 102 + chip->ops = &clps711x_pwm_ops; 99 103 100 104 spin_lock_init(&priv->lock); 101 105 102 - return devm_pwmchip_add(&pdev->dev, &priv->chip); 106 + return devm_pwmchip_add(&pdev->dev, chip); 103 107 } 104 108 105 109 static const struct of_device_id __maybe_unused clps711x_pwm_dt_ids[] = {
+10 -12
drivers/pwm/pwm-crc.c
··· 26 26 27 27 /** 28 28 * struct crystalcove_pwm - Crystal Cove PWM controller 29 - * @chip: the abstract pwm_chip structure. 30 29 * @regmap: the regmap from the parent device. 31 30 */ 32 31 struct crystalcove_pwm { 33 - struct pwm_chip chip; 34 32 struct regmap *regmap; 35 33 }; 36 34 37 35 static inline struct crystalcove_pwm *to_crc_pwm(struct pwm_chip *chip) 38 36 { 39 - return container_of(chip, struct crystalcove_pwm, chip); 37 + return pwmchip_get_drvdata(chip); 40 38 } 41 39 42 40 static int crc_pwm_calc_clk_div(int period_ns) ··· 53 55 const struct pwm_state *state) 54 56 { 55 57 struct crystalcove_pwm *crc_pwm = to_crc_pwm(chip); 56 - struct device *dev = crc_pwm->chip.dev; 58 + struct device *dev = pwmchip_parent(chip); 57 59 int err; 58 60 59 61 if (state->period > PWM_MAX_PERIOD_NS) { ··· 123 125 struct pwm_state *state) 124 126 { 125 127 struct crystalcove_pwm *crc_pwm = to_crc_pwm(chip); 126 - struct device *dev = crc_pwm->chip.dev; 128 + struct device *dev = pwmchip_parent(chip); 127 129 unsigned int clk_div, clk_div_reg, duty_cycle_reg; 128 130 int error; 129 131 ··· 158 160 159 161 static int crystalcove_pwm_probe(struct platform_device *pdev) 160 162 { 163 + struct pwm_chip *chip; 161 164 struct crystalcove_pwm *crc_pwm; 162 165 struct device *dev = pdev->dev.parent; 163 166 struct intel_soc_pmic *pmic = dev_get_drvdata(dev); 164 167 165 - crc_pwm = devm_kzalloc(&pdev->dev, sizeof(*crc_pwm), GFP_KERNEL); 166 - if (!crc_pwm) 167 - return -ENOMEM; 168 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*crc_pwm)); 169 + if (IS_ERR(chip)) 170 + return PTR_ERR(chip); 171 + crc_pwm = to_crc_pwm(chip); 168 172 169 - crc_pwm->chip.dev = &pdev->dev; 170 - crc_pwm->chip.ops = &crc_pwm_ops; 171 - crc_pwm->chip.npwm = 1; 173 + chip->ops = &crc_pwm_ops; 172 174 173 175 /* get the PMIC regmap */ 174 176 crc_pwm->regmap = pmic->regmap; 175 177 176 - return devm_pwmchip_add(&pdev->dev, &crc_pwm->chip); 178 + return devm_pwmchip_add(&pdev->dev, chip); 177 179 } 178 180 179 181 static struct platform_driver crystalcove_pwm_driver = {
+30 -29
drivers/pwm/pwm-cros-ec.c
··· 19 19 * struct cros_ec_pwm_device - Driver data for EC PWM 20 20 * 21 21 * @ec: Pointer to EC device 22 - * @chip: PWM controller chip 23 22 * @use_pwm_type: Use PWM types instead of generic channels 24 23 * @channel: array with per-channel data 25 24 */ 26 25 struct cros_ec_pwm_device { 27 26 struct cros_ec_device *ec; 28 - struct pwm_chip chip; 29 27 bool use_pwm_type; 30 28 struct cros_ec_pwm *channel; 31 29 }; ··· 38 40 39 41 static inline struct cros_ec_pwm_device *pwm_to_cros_ec_pwm(struct pwm_chip *chip) 40 42 { 41 - return container_of(chip, struct cros_ec_pwm_device, chip); 43 + return pwmchip_get_drvdata(chip); 42 44 } 43 45 44 46 static int cros_ec_dt_type_to_pwm_type(u8 dt_index, u8 *pwm_type) ··· 91 93 return cros_ec_cmd_xfer_status(ec, msg); 92 94 } 93 95 94 - static int cros_ec_pwm_get_duty(struct cros_ec_pwm_device *ec_pwm, u8 index) 96 + static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, bool use_pwm_type, u8 index) 95 97 { 96 - struct cros_ec_device *ec = ec_pwm->ec; 97 98 struct { 98 99 struct cros_ec_command msg; 99 100 union { ··· 112 115 msg->insize = sizeof(*resp); 113 116 msg->outsize = sizeof(*params); 114 117 115 - if (ec_pwm->use_pwm_type) { 118 + if (use_pwm_type) { 116 119 ret = cros_ec_dt_type_to_pwm_type(index, &params->pwm_type); 117 120 if (ret) { 118 121 dev_err(ec->dev, "Invalid PWM type index: %d\n", index); ··· 168 171 struct cros_ec_pwm *channel = &ec_pwm->channel[pwm->hwpwm]; 169 172 int ret; 170 173 171 - ret = cros_ec_pwm_get_duty(ec_pwm, pwm->hwpwm); 174 + ret = cros_ec_pwm_get_duty(ec_pwm->ec, ec_pwm->use_pwm_type, pwm->hwpwm); 172 175 if (ret < 0) { 173 - dev_err(chip->dev, "error getting initial duty: %d\n", ret); 176 + dev_err(pwmchip_parent(chip), "error getting initial duty: %d\n", ret); 174 177 return ret; 175 178 } 176 179 ··· 223 226 * of PWMs it supports directly, so we have to read the pwm duty cycle for 224 227 * subsequent channels until we get an error. 225 228 */ 226 - static int cros_ec_num_pwms(struct cros_ec_pwm_device *ec_pwm) 229 + static int cros_ec_num_pwms(struct cros_ec_device *ec) 227 230 { 228 231 int i, ret; 229 232 230 233 /* The index field is only 8 bits */ 231 234 for (i = 0; i <= U8_MAX; i++) { 232 - ret = cros_ec_pwm_get_duty(ec_pwm, i); 235 + /* 236 + * Note that this function is only called when use_pwm_type is 237 + * false. With use_pwm_type == true the number of PWMs is fixed. 238 + */ 239 + ret = cros_ec_pwm_get_duty(ec, false, i); 233 240 /* 234 241 * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM 235 242 * responses; everything else is treated as an error. ··· 262 261 struct device_node *np = pdev->dev.of_node; 263 262 struct cros_ec_pwm_device *ec_pwm; 264 263 struct pwm_chip *chip; 264 + bool use_pwm_type = false; 265 + unsigned int npwm; 265 266 int ret; 266 267 267 268 if (!ec) 268 269 return dev_err_probe(dev, -EINVAL, "no parent EC device\n"); 269 270 270 - ec_pwm = devm_kzalloc(dev, sizeof(*ec_pwm), GFP_KERNEL); 271 - if (!ec_pwm) 272 - return -ENOMEM; 273 - chip = &ec_pwm->chip; 274 - ec_pwm->ec = ec; 275 - 276 - if (of_device_is_compatible(np, "google,cros-ec-pwm-type")) 277 - ec_pwm->use_pwm_type = true; 278 - 279 - /* PWM chip */ 280 - chip->dev = dev; 281 - chip->ops = &cros_ec_pwm_ops; 282 - chip->of_xlate = cros_ec_pwm_xlate; 283 - chip->of_pwm_n_cells = 1; 284 - 285 - if (ec_pwm->use_pwm_type) { 286 - chip->npwm = CROS_EC_PWM_DT_COUNT; 271 + if (of_device_is_compatible(np, "google,cros-ec-pwm-type")) { 272 + use_pwm_type = true; 273 + npwm = CROS_EC_PWM_DT_COUNT; 287 274 } else { 288 - ret = cros_ec_num_pwms(ec_pwm); 275 + ret = cros_ec_num_pwms(ec); 289 276 if (ret < 0) 290 277 return dev_err_probe(dev, ret, "Couldn't find PWMs\n"); 291 - chip->npwm = ret; 278 + npwm = ret; 292 279 } 280 + 281 + chip = devm_pwmchip_alloc(dev, npwm, sizeof(*ec_pwm)); 282 + if (IS_ERR(chip)) 283 + return PTR_ERR(chip); 284 + 285 + ec_pwm = pwm_to_cros_ec_pwm(chip); 286 + ec_pwm->use_pwm_type = use_pwm_type; 287 + ec_pwm->ec = ec; 288 + 289 + /* PWM chip */ 290 + chip->ops = &cros_ec_pwm_ops; 291 + chip->of_xlate = cros_ec_pwm_xlate; 293 292 294 293 ec_pwm->channel = devm_kcalloc(dev, chip->npwm, sizeof(*ec_pwm->channel), 295 294 GFP_KERNEL);
+13 -13
drivers/pwm/pwm-dwc-core.c
··· 105 105 106 106 if (state->enabled) { 107 107 if (!pwm->state.enabled) 108 - pm_runtime_get_sync(chip->dev); 108 + pm_runtime_get_sync(pwmchip_parent(chip)); 109 109 return __dwc_pwm_configure_timer(dwc, pwm, state); 110 110 } else { 111 111 if (pwm->state.enabled) { 112 112 __dwc_pwm_set_enable(dwc, pwm->hwpwm, false); 113 - pm_runtime_put_sync(chip->dev); 113 + pm_runtime_put_sync(pwmchip_parent(chip)); 114 114 } 115 115 } 116 116 ··· 124 124 u64 duty, period; 125 125 u32 ctrl, ld, ld2; 126 126 127 - pm_runtime_get_sync(chip->dev); 127 + pm_runtime_get_sync(pwmchip_parent(chip)); 128 128 129 129 ctrl = dwc_pwm_readl(dwc, DWC_TIM_CTRL(pwm->hwpwm)); 130 130 ld = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT(pwm->hwpwm)); ··· 149 149 state->period = period; 150 150 state->duty_cycle = duty; 151 151 152 - pm_runtime_put_sync(chip->dev); 152 + pm_runtime_put_sync(pwmchip_parent(chip)); 153 153 154 154 return 0; 155 155 } ··· 159 159 .get_state = dwc_pwm_get_state, 160 160 }; 161 161 162 - struct dwc_pwm *dwc_pwm_alloc(struct device *dev) 162 + struct pwm_chip *dwc_pwm_alloc(struct device *dev) 163 163 { 164 + struct pwm_chip *chip; 164 165 struct dwc_pwm *dwc; 165 166 166 - dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL); 167 - if (!dwc) 168 - return NULL; 167 + chip = devm_pwmchip_alloc(dev, DWC_TIMERS_TOTAL, sizeof(*dwc)); 168 + if (IS_ERR(chip)) 169 + return chip; 170 + dwc = to_dwc_pwm(chip); 169 171 170 172 dwc->clk_ns = 10; 171 - dwc->chip.dev = dev; 172 - dwc->chip.ops = &dwc_pwm_ops; 173 - dwc->chip.npwm = DWC_TIMERS_TOTAL; 173 + chip->ops = &dwc_pwm_ops; 174 174 175 - dev_set_drvdata(dev, dwc); 176 - return dwc; 175 + dev_set_drvdata(dev, chip); 176 + return chip; 177 177 } 178 178 EXPORT_SYMBOL_GPL(dwc_pwm_alloc); 179 179
+46 -31
drivers/pwm/pwm-dwc.c
··· 25 25 26 26 #include "pwm-dwc.h" 27 27 28 + /* Elkhart Lake */ 29 + static const struct dwc_pwm_info ehl_pwm_info = { 30 + .nr = 2, 31 + .size = 0x1000, 32 + }; 33 + 34 + static int dwc_pwm_init_one(struct device *dev, void __iomem *base, unsigned int offset) 35 + { 36 + struct pwm_chip *chip; 37 + struct dwc_pwm *dwc; 38 + 39 + chip = dwc_pwm_alloc(dev); 40 + if (IS_ERR(chip)) 41 + return PTR_ERR(chip); 42 + 43 + dwc = to_dwc_pwm(chip); 44 + dwc->base = base + offset; 45 + 46 + return devm_pwmchip_add(dev, chip); 47 + } 48 + 28 49 static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id) 29 50 { 51 + const struct dwc_pwm_info *info; 30 52 struct device *dev = &pci->dev; 31 - struct dwc_pwm *dwc; 32 - int ret; 33 - 34 - dwc = dwc_pwm_alloc(dev); 35 - if (!dwc) 36 - return -ENOMEM; 53 + int i, ret; 37 54 38 55 ret = pcim_enable_device(pci); 39 - if (ret) { 40 - dev_err(dev, "Failed to enable device (%pe)\n", ERR_PTR(ret)); 41 - return ret; 42 - } 56 + if (ret) 57 + return dev_err_probe(dev, ret, "Failed to enable device\n"); 43 58 44 59 pci_set_master(pci); 45 60 46 61 ret = pcim_iomap_regions(pci, BIT(0), pci_name(pci)); 47 - if (ret) { 48 - dev_err(dev, "Failed to iomap PCI BAR (%pe)\n", ERR_PTR(ret)); 49 - return ret; 50 - } 51 - 52 - dwc->base = pcim_iomap_table(pci)[0]; 53 - if (!dwc->base) { 54 - dev_err(dev, "Base address missing\n"); 55 - return -ENOMEM; 56 - } 57 - 58 - ret = devm_pwmchip_add(dev, &dwc->chip); 59 62 if (ret) 60 - return ret; 63 + return dev_err_probe(dev, ret, "Failed to iomap PCI BAR\n"); 64 + 65 + info = (const struct dwc_pwm_info *)id->driver_data; 66 + 67 + for (i = 0; i < info->nr; i++) { 68 + /* 69 + * No need to check for pcim_iomap_table() failure, 70 + * pcim_iomap_regions() already does it for us. 71 + */ 72 + ret = dwc_pwm_init_one(dev, pcim_iomap_table(pci)[0], i * info->size); 73 + if (ret) 74 + return ret; 75 + } 61 76 62 77 pm_runtime_put(dev); 63 78 pm_runtime_allow(dev); ··· 88 73 89 74 static int dwc_pwm_suspend(struct device *dev) 90 75 { 91 - struct pci_dev *pdev = container_of(dev, struct pci_dev, dev); 92 - struct dwc_pwm *dwc = pci_get_drvdata(pdev); 76 + struct pwm_chip *chip = dev_get_drvdata(dev); 77 + struct dwc_pwm *dwc = to_dwc_pwm(chip); 93 78 int i; 94 79 95 80 for (i = 0; i < DWC_TIMERS_TOTAL; i++) { 96 - if (dwc->chip.pwms[i].state.enabled) { 81 + if (chip->pwms[i].state.enabled) { 97 82 dev_err(dev, "PWM %u in use by consumer (%s)\n", 98 - i, dwc->chip.pwms[i].label); 83 + i, chip->pwms[i].label); 99 84 return -EBUSY; 100 85 } 101 86 dwc->ctx[i].cnt = dwc_pwm_readl(dwc, DWC_TIM_LD_CNT(i)); ··· 108 93 109 94 static int dwc_pwm_resume(struct device *dev) 110 95 { 111 - struct pci_dev *pdev = container_of(dev, struct pci_dev, dev); 112 - struct dwc_pwm *dwc = pci_get_drvdata(pdev); 96 + struct pwm_chip *chip = dev_get_drvdata(dev); 97 + struct dwc_pwm *dwc = to_dwc_pwm(chip); 113 98 int i; 114 99 115 100 for (i = 0; i < DWC_TIMERS_TOTAL; i++) { ··· 124 109 static DEFINE_SIMPLE_DEV_PM_OPS(dwc_pwm_pm_ops, dwc_pwm_suspend, dwc_pwm_resume); 125 110 126 111 static const struct pci_device_id dwc_pwm_id_table[] = { 127 - { PCI_VDEVICE(INTEL, 0x4bb7) }, /* Elkhart Lake */ 112 + { PCI_VDEVICE(INTEL, 0x4bb7), (kernel_ulong_t)&ehl_pwm_info }, 128 113 { } /* Terminating Entry */ 129 114 }; 130 115 MODULE_DEVICE_TABLE(pci, dwc_pwm_id_table); ··· 135 120 .remove = dwc_pwm_remove, 136 121 .id_table = dwc_pwm_id_table, 137 122 .driver = { 138 - .pm = pm_ptr(&dwc_pwm_pm_ops), 123 + .pm = pm_sleep_ptr(&dwc_pwm_pm_ops), 139 124 }, 140 125 }; 141 126
+11 -3
drivers/pwm/pwm-dwc.h
··· 33 33 #define DWC_TIM_CTRL_INT_MASK BIT(2) 34 34 #define DWC_TIM_CTRL_PWM BIT(3) 35 35 36 + struct dwc_pwm_info { 37 + unsigned int nr; 38 + unsigned int size; 39 + }; 40 + 36 41 struct dwc_pwm_ctx { 37 42 u32 cnt; 38 43 u32 cnt2; ··· 45 40 }; 46 41 47 42 struct dwc_pwm { 48 - struct pwm_chip chip; 49 43 void __iomem *base; 50 44 unsigned int clk_ns; 51 45 struct dwc_pwm_ctx ctx[DWC_TIMERS_TOTAL]; 52 46 }; 53 - #define to_dwc_pwm(p) (container_of((p), struct dwc_pwm, chip)) 47 + 48 + static inline struct dwc_pwm *to_dwc_pwm(struct pwm_chip *chip) 49 + { 50 + return pwmchip_get_drvdata(chip); 51 + } 54 52 55 53 static inline u32 dwc_pwm_readl(struct dwc_pwm *dwc, u32 offset) 56 54 { ··· 65 57 writel(value, dwc->base + offset); 66 58 } 67 59 68 - extern struct dwc_pwm *dwc_pwm_alloc(struct device *dev); 60 + extern struct pwm_chip *dwc_pwm_alloc(struct device *dev);
+10 -11
drivers/pwm/pwm-ep93xx.c
··· 36 36 struct ep93xx_pwm { 37 37 void __iomem *base; 38 38 struct clk *clk; 39 - struct pwm_chip chip; 40 39 }; 41 40 42 41 static inline struct ep93xx_pwm *to_ep93xx_pwm(struct pwm_chip *chip) 43 42 { 44 - return container_of(chip, struct ep93xx_pwm, chip); 43 + return pwmchip_get_drvdata(chip); 45 44 } 46 45 47 46 static int ep93xx_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) 48 47 { 49 - struct platform_device *pdev = to_platform_device(chip->dev); 48 + struct platform_device *pdev = to_platform_device(pwmchip_parent(chip)); 50 49 51 50 return ep93xx_pwm_acquire_gpio(pdev); 52 51 } 53 52 54 53 static void ep93xx_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) 55 54 { 56 - struct platform_device *pdev = to_platform_device(chip->dev); 55 + struct platform_device *pdev = to_platform_device(pwmchip_parent(chip)); 57 56 58 57 ep93xx_pwm_release_gpio(pdev); 59 58 } ··· 162 163 163 164 static int ep93xx_pwm_probe(struct platform_device *pdev) 164 165 { 166 + struct pwm_chip *chip; 165 167 struct ep93xx_pwm *ep93xx_pwm; 166 168 int ret; 167 169 168 - ep93xx_pwm = devm_kzalloc(&pdev->dev, sizeof(*ep93xx_pwm), GFP_KERNEL); 169 - if (!ep93xx_pwm) 170 - return -ENOMEM; 170 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*ep93xx_pwm)); 171 + if (IS_ERR(chip)) 172 + return PTR_ERR(chip); 173 + ep93xx_pwm = to_ep93xx_pwm(chip); 171 174 172 175 ep93xx_pwm->base = devm_platform_ioremap_resource(pdev, 0); 173 176 if (IS_ERR(ep93xx_pwm->base)) ··· 179 178 if (IS_ERR(ep93xx_pwm->clk)) 180 179 return PTR_ERR(ep93xx_pwm->clk); 181 180 182 - ep93xx_pwm->chip.dev = &pdev->dev; 183 - ep93xx_pwm->chip.ops = &ep93xx_pwm_ops; 184 - ep93xx_pwm->chip.npwm = 1; 181 + chip->ops = &ep93xx_pwm_ops; 185 182 186 - ret = devm_pwmchip_add(&pdev->dev, &ep93xx_pwm->chip); 183 + ret = devm_pwmchip_add(&pdev->dev, chip); 187 184 if (ret < 0) 188 185 return ret; 189 186
+25 -24
drivers/pwm/pwm-fsl-ftm.c
··· 40 40 }; 41 41 42 42 struct fsl_pwm_chip { 43 - struct pwm_chip chip; 44 43 struct mutex lock; 45 44 struct regmap *regmap; 46 45 ··· 54 55 55 56 static inline struct fsl_pwm_chip *to_fsl_chip(struct pwm_chip *chip) 56 57 { 57 - return container_of(chip, struct fsl_pwm_chip, chip); 58 + return pwmchip_get_drvdata(chip); 58 59 } 59 60 60 61 static void ftm_clear_write_protection(struct fsl_pwm_chip *fpc) ··· 220 221 return false; 221 222 } 222 223 223 - static int fsl_pwm_apply_config(struct fsl_pwm_chip *fpc, 224 + static int fsl_pwm_apply_config(struct pwm_chip *chip, 224 225 struct pwm_device *pwm, 225 226 const struct pwm_state *newstate) 226 227 { 228 + struct fsl_pwm_chip *fpc = to_fsl_chip(chip); 227 229 unsigned int duty; 228 230 u32 reg_polarity; 229 231 ··· 232 232 bool do_write_period = false; 233 233 234 234 if (!fsl_pwm_calculate_period(fpc, newstate->period, &periodcfg)) { 235 - dev_err(fpc->chip.dev, "failed to calculate new period\n"); 235 + dev_err(pwmchip_parent(chip), "failed to calculate new period\n"); 236 236 return -EINVAL; 237 237 } 238 238 ··· 246 246 */ 247 247 else if (!fsl_pwm_periodcfg_are_equal(&fpc->period, &periodcfg)) { 248 248 if (fsl_pwm_is_other_pwm_enabled(fpc, pwm)) { 249 - dev_err(fpc->chip.dev, 249 + dev_err(pwmchip_parent(chip), 250 250 "Cannot change period for PWM %u, disable other PWMs first\n", 251 251 pwm->hwpwm); 252 252 return -EBUSY; ··· 322 322 goto end_mutex; 323 323 } 324 324 325 - ret = fsl_pwm_apply_config(fpc, pwm, newstate); 325 + ret = fsl_pwm_apply_config(chip, pwm, newstate); 326 326 if (ret) 327 327 goto end_mutex; 328 328 ··· 392 392 393 393 static int fsl_pwm_probe(struct platform_device *pdev) 394 394 { 395 + struct pwm_chip *chip; 395 396 struct fsl_pwm_chip *fpc; 396 397 void __iomem *base; 397 398 int ret; 398 399 399 - fpc = devm_kzalloc(&pdev->dev, sizeof(*fpc), GFP_KERNEL); 400 - if (!fpc) 401 - return -ENOMEM; 400 + chip = devm_pwmchip_alloc(&pdev->dev, 8, sizeof(*fpc)); 401 + if (IS_ERR(chip)) 402 + return PTR_ERR(chip); 403 + fpc = to_fsl_chip(chip); 402 404 403 405 mutex_init(&fpc->lock); 404 406 405 407 fpc->soc = of_device_get_match_data(&pdev->dev); 406 - fpc->chip.dev = &pdev->dev; 407 408 408 409 base = devm_platform_ioremap_resource(pdev, 0); 409 410 if (IS_ERR(base)) ··· 423 422 return PTR_ERR(fpc->clk[FSL_PWM_CLK_SYS]); 424 423 } 425 424 426 - fpc->clk[FSL_PWM_CLK_FIX] = devm_clk_get(fpc->chip.dev, "ftm_fix"); 425 + fpc->clk[FSL_PWM_CLK_FIX] = devm_clk_get(&pdev->dev, "ftm_fix"); 427 426 if (IS_ERR(fpc->clk[FSL_PWM_CLK_FIX])) 428 427 return PTR_ERR(fpc->clk[FSL_PWM_CLK_FIX]); 429 428 430 - fpc->clk[FSL_PWM_CLK_EXT] = devm_clk_get(fpc->chip.dev, "ftm_ext"); 429 + fpc->clk[FSL_PWM_CLK_EXT] = devm_clk_get(&pdev->dev, "ftm_ext"); 431 430 if (IS_ERR(fpc->clk[FSL_PWM_CLK_EXT])) 432 431 return PTR_ERR(fpc->clk[FSL_PWM_CLK_EXT]); 433 432 434 433 fpc->clk[FSL_PWM_CLK_CNTEN] = 435 - devm_clk_get(fpc->chip.dev, "ftm_cnt_clk_en"); 434 + devm_clk_get(&pdev->dev, "ftm_cnt_clk_en"); 436 435 if (IS_ERR(fpc->clk[FSL_PWM_CLK_CNTEN])) 437 436 return PTR_ERR(fpc->clk[FSL_PWM_CLK_CNTEN]); 438 437 ··· 444 443 if (IS_ERR(fpc->ipg_clk)) 445 444 fpc->ipg_clk = fpc->clk[FSL_PWM_CLK_SYS]; 446 445 446 + chip->ops = &fsl_pwm_ops; 447 447 448 - fpc->chip.ops = &fsl_pwm_ops; 449 - fpc->chip.npwm = 8; 450 - 451 - ret = devm_pwmchip_add(&pdev->dev, &fpc->chip); 448 + ret = devm_pwmchip_add(&pdev->dev, chip); 452 449 if (ret < 0) { 453 450 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret); 454 451 return ret; 455 452 } 456 453 457 - platform_set_drvdata(pdev, fpc); 454 + platform_set_drvdata(pdev, chip); 458 455 459 456 return fsl_pwm_init(fpc); 460 457 } ··· 460 461 #ifdef CONFIG_PM_SLEEP 461 462 static int fsl_pwm_suspend(struct device *dev) 462 463 { 463 - struct fsl_pwm_chip *fpc = dev_get_drvdata(dev); 464 + struct pwm_chip *chip = dev_get_drvdata(dev); 465 + struct fsl_pwm_chip *fpc = to_fsl_chip(chip); 464 466 int i; 465 467 466 468 regcache_cache_only(fpc->regmap, true); 467 469 regcache_mark_dirty(fpc->regmap); 468 470 469 - for (i = 0; i < fpc->chip.npwm; i++) { 470 - struct pwm_device *pwm = &fpc->chip.pwms[i]; 471 + for (i = 0; i < chip->npwm; i++) { 472 + struct pwm_device *pwm = &chip->pwms[i]; 471 473 472 474 if (!test_bit(PWMF_REQUESTED, &pwm->flags)) 473 475 continue; ··· 487 487 488 488 static int fsl_pwm_resume(struct device *dev) 489 489 { 490 - struct fsl_pwm_chip *fpc = dev_get_drvdata(dev); 490 + struct pwm_chip *chip = dev_get_drvdata(dev); 491 + struct fsl_pwm_chip *fpc = to_fsl_chip(chip); 491 492 int i; 492 493 493 - for (i = 0; i < fpc->chip.npwm; i++) { 494 - struct pwm_device *pwm = &fpc->chip.pwms[i]; 494 + for (i = 0; i < chip->npwm; i++) { 495 + struct pwm_device *pwm = &chip->pwms[i]; 495 496 496 497 if (!test_bit(PWMF_REQUESTED, &pwm->flags)) 497 498 continue;
+34 -36
drivers/pwm/pwm-hibvt.c
··· 33 33 #define PWM_DUTY_MASK GENMASK(31, 0) 34 34 35 35 struct hibvt_pwm_chip { 36 - struct pwm_chip chip; 37 36 struct clk *clk; 38 37 void __iomem *base; 39 38 struct reset_control *rstc; ··· 64 65 65 66 static inline struct hibvt_pwm_chip *to_hibvt_pwm_chip(struct pwm_chip *chip) 66 67 { 67 - return container_of(chip, struct hibvt_pwm_chip, chip); 68 + return pwmchip_get_drvdata(chip); 68 69 } 69 70 70 71 static void hibvt_pwm_set_bits(void __iomem *base, u32 offset, ··· 190 191 { 191 192 const struct hibvt_pwm_soc *soc = 192 193 of_device_get_match_data(&pdev->dev); 193 - struct hibvt_pwm_chip *pwm_chip; 194 + struct pwm_chip *chip; 195 + struct hibvt_pwm_chip *hi_pwm_chip; 194 196 int ret, i; 195 197 196 - pwm_chip = devm_kzalloc(&pdev->dev, sizeof(*pwm_chip), GFP_KERNEL); 197 - if (pwm_chip == NULL) 198 - return -ENOMEM; 198 + chip = devm_pwmchip_alloc(&pdev->dev, soc->num_pwms, sizeof(*hi_pwm_chip)); 199 + if (IS_ERR(chip)) 200 + return PTR_ERR(chip); 201 + hi_pwm_chip = to_hibvt_pwm_chip(chip); 199 202 200 - pwm_chip->clk = devm_clk_get(&pdev->dev, NULL); 201 - if (IS_ERR(pwm_chip->clk)) { 203 + hi_pwm_chip->clk = devm_clk_get(&pdev->dev, NULL); 204 + if (IS_ERR(hi_pwm_chip->clk)) { 202 205 dev_err(&pdev->dev, "getting clock failed with %ld\n", 203 - PTR_ERR(pwm_chip->clk)); 204 - return PTR_ERR(pwm_chip->clk); 206 + PTR_ERR(hi_pwm_chip->clk)); 207 + return PTR_ERR(hi_pwm_chip->clk); 205 208 } 206 209 207 - pwm_chip->chip.ops = &hibvt_pwm_ops; 208 - pwm_chip->chip.dev = &pdev->dev; 209 - pwm_chip->chip.npwm = soc->num_pwms; 210 - pwm_chip->soc = soc; 210 + chip->ops = &hibvt_pwm_ops; 211 + hi_pwm_chip->soc = soc; 211 212 212 - pwm_chip->base = devm_platform_ioremap_resource(pdev, 0); 213 - if (IS_ERR(pwm_chip->base)) 214 - return PTR_ERR(pwm_chip->base); 213 + hi_pwm_chip->base = devm_platform_ioremap_resource(pdev, 0); 214 + if (IS_ERR(hi_pwm_chip->base)) 215 + return PTR_ERR(hi_pwm_chip->base); 215 216 216 - ret = clk_prepare_enable(pwm_chip->clk); 217 + ret = clk_prepare_enable(hi_pwm_chip->clk); 217 218 if (ret < 0) 218 219 return ret; 219 220 220 - pwm_chip->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL); 221 - if (IS_ERR(pwm_chip->rstc)) { 222 - clk_disable_unprepare(pwm_chip->clk); 223 - return PTR_ERR(pwm_chip->rstc); 221 + hi_pwm_chip->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL); 222 + if (IS_ERR(hi_pwm_chip->rstc)) { 223 + clk_disable_unprepare(hi_pwm_chip->clk); 224 + return PTR_ERR(hi_pwm_chip->rstc); 224 225 } 225 226 226 - reset_control_assert(pwm_chip->rstc); 227 + reset_control_assert(hi_pwm_chip->rstc); 227 228 msleep(30); 228 - reset_control_deassert(pwm_chip->rstc); 229 + reset_control_deassert(hi_pwm_chip->rstc); 229 230 230 - ret = pwmchip_add(&pwm_chip->chip); 231 + ret = pwmchip_add(chip); 231 232 if (ret < 0) { 232 - clk_disable_unprepare(pwm_chip->clk); 233 + clk_disable_unprepare(hi_pwm_chip->clk); 233 234 return ret; 234 235 } 235 236 236 - for (i = 0; i < pwm_chip->chip.npwm; i++) { 237 - hibvt_pwm_set_bits(pwm_chip->base, PWM_CTRL_ADDR(i), 237 + for (i = 0; i < chip->npwm; i++) { 238 + hibvt_pwm_set_bits(hi_pwm_chip->base, PWM_CTRL_ADDR(i), 238 239 PWM_KEEP_MASK, (0x1 << PWM_KEEP_SHIFT)); 239 240 } 240 241 241 - platform_set_drvdata(pdev, pwm_chip); 242 + platform_set_drvdata(pdev, chip); 242 243 243 244 return 0; 244 245 } 245 246 246 247 static void hibvt_pwm_remove(struct platform_device *pdev) 247 248 { 248 - struct hibvt_pwm_chip *pwm_chip; 249 + struct pwm_chip *chip = platform_get_drvdata(pdev); 250 + struct hibvt_pwm_chip *hi_pwm_chip = to_hibvt_pwm_chip(chip); 249 251 250 - pwm_chip = platform_get_drvdata(pdev); 252 + pwmchip_remove(chip); 251 253 252 - pwmchip_remove(&pwm_chip->chip); 253 - 254 - reset_control_assert(pwm_chip->rstc); 254 + reset_control_assert(hi_pwm_chip->rstc); 255 255 msleep(30); 256 - reset_control_deassert(pwm_chip->rstc); 256 + reset_control_deassert(hi_pwm_chip->rstc); 257 257 258 - clk_disable_unprepare(pwm_chip->clk); 258 + clk_disable_unprepare(hi_pwm_chip->clk); 259 259 } 260 260 261 261 static const struct of_device_id hibvt_pwm_of_match[] = {
+30 -30
drivers/pwm/pwm-img.c
··· 59 59 }; 60 60 61 61 struct img_pwm_chip { 62 - struct device *dev; 63 - struct pwm_chip chip; 64 62 struct clk *pwm_clk; 65 63 struct clk *sys_clk; 66 64 void __iomem *base; ··· 72 74 73 75 static inline struct img_pwm_chip *to_img_pwm_chip(struct pwm_chip *chip) 74 76 { 75 - return container_of(chip, struct img_pwm_chip, chip); 77 + return pwmchip_get_drvdata(chip); 76 78 } 77 79 78 80 static inline void img_pwm_writel(struct img_pwm_chip *imgchip, ··· 97 99 98 100 if (period_ns < imgchip->min_period_ns || 99 101 period_ns > imgchip->max_period_ns) { 100 - dev_err(chip->dev, "configured period not in range\n"); 102 + dev_err(pwmchip_parent(chip), "configured period not in range\n"); 101 103 return -ERANGE; 102 104 } 103 105 ··· 118 120 div = PWM_CTRL_CFG_SUB_DIV0_DIV1; 119 121 timebase = DIV_ROUND_UP(mul, 512); 120 122 } else { 121 - dev_err(chip->dev, 123 + dev_err(pwmchip_parent(chip), 122 124 "failed to configure timebase steps/divider value\n"); 123 125 return -EINVAL; 124 126 } 125 127 126 128 duty = DIV_ROUND_UP(timebase * duty_ns, period_ns); 127 129 128 - ret = pm_runtime_resume_and_get(chip->dev); 130 + ret = pm_runtime_resume_and_get(pwmchip_parent(chip)); 129 131 if (ret < 0) 130 132 return ret; 131 133 ··· 139 141 (timebase << PWM_CH_CFG_TMBASE_SHIFT); 140 142 img_pwm_writel(imgchip, PWM_CH_CFG(pwm->hwpwm), val); 141 143 142 - pm_runtime_mark_last_busy(chip->dev); 143 - pm_runtime_put_autosuspend(chip->dev); 144 + pm_runtime_mark_last_busy(pwmchip_parent(chip)); 145 + pm_runtime_put_autosuspend(pwmchip_parent(chip)); 144 146 145 147 return 0; 146 148 } ··· 151 153 struct img_pwm_chip *imgchip = to_img_pwm_chip(chip); 152 154 int ret; 153 155 154 - ret = pm_runtime_resume_and_get(chip->dev); 156 + ret = pm_runtime_resume_and_get(pwmchip_parent(chip)); 155 157 if (ret < 0) 156 158 return ret; 157 159 ··· 175 177 val &= ~BIT(pwm->hwpwm); 176 178 img_pwm_writel(imgchip, PWM_CTRL_CFG, val); 177 179 178 - pm_runtime_mark_last_busy(chip->dev); 179 - pm_runtime_put_autosuspend(chip->dev); 180 + pm_runtime_mark_last_busy(pwmchip_parent(chip)); 181 + pm_runtime_put_autosuspend(pwmchip_parent(chip)); 180 182 } 181 183 182 184 static int img_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, ··· 223 225 224 226 static int img_pwm_runtime_suspend(struct device *dev) 225 227 { 226 - struct img_pwm_chip *imgchip = dev_get_drvdata(dev); 228 + struct pwm_chip *chip = dev_get_drvdata(dev); 229 + struct img_pwm_chip *imgchip = to_img_pwm_chip(chip); 227 230 228 231 clk_disable_unprepare(imgchip->pwm_clk); 229 232 clk_disable_unprepare(imgchip->sys_clk); ··· 234 235 235 236 static int img_pwm_runtime_resume(struct device *dev) 236 237 { 237 - struct img_pwm_chip *imgchip = dev_get_drvdata(dev); 238 + struct pwm_chip *chip = dev_get_drvdata(dev); 239 + struct img_pwm_chip *imgchip = to_img_pwm_chip(chip); 238 240 int ret; 239 241 240 242 ret = clk_prepare_enable(imgchip->sys_clk); ··· 259 259 int ret; 260 260 u64 val; 261 261 unsigned long clk_rate; 262 + struct pwm_chip *chip; 262 263 struct img_pwm_chip *imgchip; 263 264 264 - imgchip = devm_kzalloc(&pdev->dev, sizeof(*imgchip), GFP_KERNEL); 265 - if (!imgchip) 266 - return -ENOMEM; 267 - 268 - imgchip->dev = &pdev->dev; 265 + chip = devm_pwmchip_alloc(&pdev->dev, IMG_PWM_NPWM, sizeof(*imgchip)); 266 + if (IS_ERR(chip)) 267 + return PTR_ERR(chip); 268 + imgchip = to_img_pwm_chip(chip); 269 269 270 270 imgchip->base = devm_platform_ioremap_resource(pdev, 0); 271 271 if (IS_ERR(imgchip->base)) ··· 290 290 return PTR_ERR(imgchip->pwm_clk); 291 291 } 292 292 293 - platform_set_drvdata(pdev, imgchip); 293 + platform_set_drvdata(pdev, chip); 294 294 295 295 pm_runtime_set_autosuspend_delay(&pdev->dev, IMG_PWM_PM_TIMEOUT); 296 296 pm_runtime_use_autosuspend(&pdev->dev); ··· 317 317 do_div(val, clk_rate); 318 318 imgchip->min_period_ns = val; 319 319 320 - imgchip->chip.dev = &pdev->dev; 321 - imgchip->chip.ops = &img_pwm_ops; 322 - imgchip->chip.npwm = IMG_PWM_NPWM; 320 + chip->ops = &img_pwm_ops; 323 321 324 - ret = pwmchip_add(&imgchip->chip); 322 + ret = pwmchip_add(chip); 325 323 if (ret < 0) { 326 324 dev_err(&pdev->dev, "pwmchip_add failed: %d\n", ret); 327 325 goto err_suspend; ··· 338 340 339 341 static void img_pwm_remove(struct platform_device *pdev) 340 342 { 341 - struct img_pwm_chip *imgchip = platform_get_drvdata(pdev); 343 + struct pwm_chip *chip = platform_get_drvdata(pdev); 342 344 343 345 pm_runtime_disable(&pdev->dev); 344 346 if (!pm_runtime_status_suspended(&pdev->dev)) 345 347 img_pwm_runtime_suspend(&pdev->dev); 346 348 347 - pwmchip_remove(&imgchip->chip); 349 + pwmchip_remove(chip); 348 350 } 349 351 350 352 #ifdef CONFIG_PM_SLEEP 351 353 static int img_pwm_suspend(struct device *dev) 352 354 { 353 - struct img_pwm_chip *imgchip = dev_get_drvdata(dev); 355 + struct pwm_chip *chip = dev_get_drvdata(dev); 356 + struct img_pwm_chip *imgchip = to_img_pwm_chip(chip); 354 357 int i, ret; 355 358 356 359 if (pm_runtime_status_suspended(dev)) { ··· 360 361 return ret; 361 362 } 362 363 363 - for (i = 0; i < imgchip->chip.npwm; i++) 364 + for (i = 0; i < chip->npwm; i++) 364 365 imgchip->suspend_ch_cfg[i] = img_pwm_readl(imgchip, 365 366 PWM_CH_CFG(i)); 366 367 ··· 373 374 374 375 static int img_pwm_resume(struct device *dev) 375 376 { 376 - struct img_pwm_chip *imgchip = dev_get_drvdata(dev); 377 + struct pwm_chip *chip = dev_get_drvdata(dev); 378 + struct img_pwm_chip *imgchip = to_img_pwm_chip(chip); 377 379 int ret; 378 380 int i; 379 381 ··· 382 382 if (ret) 383 383 return ret; 384 384 385 - for (i = 0; i < imgchip->chip.npwm; i++) 385 + for (i = 0; i < chip->npwm; i++) 386 386 img_pwm_writel(imgchip, PWM_CH_CFG(i), 387 387 imgchip->suspend_ch_cfg[i]); 388 388 389 389 img_pwm_writel(imgchip, PWM_CTRL_CFG, imgchip->suspend_ctrl_cfg); 390 390 391 - for (i = 0; i < imgchip->chip.npwm; i++) 391 + for (i = 0; i < chip->npwm; i++) 392 392 if (imgchip->suspend_ctrl_cfg & BIT(i)) 393 393 regmap_clear_bits(imgchip->periph_regs, 394 394 PERIP_PWM_PDM_CONTROL,
+26 -20
drivers/pwm/pwm-imx-tpm.c
··· 57 57 #define PWM_IMX_TPM_MOD_MOD GENMASK(PWM_IMX_TPM_MOD_WIDTH - 1, 0) 58 58 59 59 struct imx_tpm_pwm_chip { 60 - struct pwm_chip chip; 61 60 struct clk *clk; 62 61 void __iomem *base; 63 62 struct mutex lock; ··· 74 75 static inline struct imx_tpm_pwm_chip * 75 76 to_imx_tpm_pwm_chip(struct pwm_chip *chip) 76 77 { 77 - return container_of(chip, struct imx_tpm_pwm_chip, chip); 78 + return pwmchip_get_drvdata(chip); 78 79 } 79 80 80 81 /* ··· 335 336 336 337 static int pwm_imx_tpm_probe(struct platform_device *pdev) 337 338 { 339 + struct pwm_chip *chip; 338 340 struct imx_tpm_pwm_chip *tpm; 341 + struct clk *clk; 342 + void __iomem *base; 339 343 int ret; 344 + unsigned int npwm; 340 345 u32 val; 341 346 342 - tpm = devm_kzalloc(&pdev->dev, sizeof(*tpm), GFP_KERNEL); 343 - if (!tpm) 344 - return -ENOMEM; 347 + base = devm_platform_ioremap_resource(pdev, 0); 348 + if (IS_ERR(base)) 349 + return PTR_ERR(base); 350 + 351 + clk = devm_clk_get_enabled(&pdev->dev, NULL); 352 + if (IS_ERR(clk)) 353 + return dev_err_probe(&pdev->dev, PTR_ERR(clk), 354 + "failed to get PWM clock\n"); 355 + 356 + /* get number of channels */ 357 + val = readl(base + PWM_IMX_TPM_PARAM); 358 + npwm = FIELD_GET(PWM_IMX_TPM_PARAM_CHAN, val); 359 + 360 + chip = devm_pwmchip_alloc(&pdev->dev, npwm, sizeof(*tpm)); 361 + if (IS_ERR(chip)) 362 + return PTR_ERR(chip); 363 + tpm = to_imx_tpm_pwm_chip(chip); 345 364 346 365 platform_set_drvdata(pdev, tpm); 347 366 348 - tpm->base = devm_platform_ioremap_resource(pdev, 0); 349 - if (IS_ERR(tpm->base)) 350 - return PTR_ERR(tpm->base); 367 + tpm->base = base; 368 + tpm->clk = clk; 351 369 352 - tpm->clk = devm_clk_get_enabled(&pdev->dev, NULL); 353 - if (IS_ERR(tpm->clk)) 354 - return dev_err_probe(&pdev->dev, PTR_ERR(tpm->clk), 355 - "failed to get PWM clock\n"); 356 - 357 - tpm->chip.dev = &pdev->dev; 358 - tpm->chip.ops = &imx_tpm_pwm_ops; 359 - 360 - /* get number of channels */ 361 - val = readl(tpm->base + PWM_IMX_TPM_PARAM); 362 - tpm->chip.npwm = FIELD_GET(PWM_IMX_TPM_PARAM_CHAN, val); 370 + chip->ops = &imx_tpm_pwm_ops; 363 371 364 372 mutex_init(&tpm->lock); 365 373 366 - ret = devm_pwmchip_add(&pdev->dev, &tpm->chip); 374 + ret = devm_pwmchip_add(&pdev->dev, chip); 367 375 if (ret) 368 376 return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n"); 369 377
+11 -9
drivers/pwm/pwm-imx1.c
··· 28 28 struct clk *clk_ipg; 29 29 struct clk *clk_per; 30 30 void __iomem *mmio_base; 31 - struct pwm_chip chip; 32 31 }; 33 32 34 - #define to_pwm_imx1_chip(chip) container_of(chip, struct pwm_imx1_chip, chip) 33 + static inline struct pwm_imx1_chip *to_pwm_imx1_chip(struct pwm_chip *chip) 34 + { 35 + return pwmchip_get_drvdata(chip); 36 + } 35 37 36 38 static int pwm_imx1_clk_prepare_enable(struct pwm_chip *chip) 37 39 { ··· 158 156 159 157 static int pwm_imx1_probe(struct platform_device *pdev) 160 158 { 159 + struct pwm_chip *chip; 161 160 struct pwm_imx1_chip *imx; 162 161 163 - imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL); 164 - if (!imx) 165 - return -ENOMEM; 162 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*imx)); 163 + if (IS_ERR(chip)) 164 + return PTR_ERR(chip); 165 + imx = to_pwm_imx1_chip(chip); 166 166 167 167 imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 168 168 if (IS_ERR(imx->clk_ipg)) ··· 176 172 return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_per), 177 173 "failed to get peripheral clock\n"); 178 174 179 - imx->chip.ops = &pwm_imx1_ops; 180 - imx->chip.dev = &pdev->dev; 181 - imx->chip.npwm = 1; 175 + chip->ops = &pwm_imx1_ops; 182 176 183 177 imx->mmio_base = devm_platform_ioremap_resource(pdev, 0); 184 178 if (IS_ERR(imx->mmio_base)) 185 179 return PTR_ERR(imx->mmio_base); 186 180 187 - return devm_pwmchip_add(&pdev->dev, &imx->chip); 181 + return devm_pwmchip_add(&pdev->dev, chip); 188 182 } 189 183 190 184 static struct platform_driver pwm_imx1_driver = {
+17 -18
drivers/pwm/pwm-imx27.c
··· 83 83 struct clk *clk_ipg; 84 84 struct clk *clk_per; 85 85 void __iomem *mmio_base; 86 - struct pwm_chip chip; 87 86 88 87 /* 89 88 * The driver cannot read the current duty cycle from the hardware if ··· 92 93 unsigned int duty_cycle; 93 94 }; 94 95 95 - #define to_pwm_imx27_chip(chip) container_of(chip, struct pwm_imx27_chip, chip) 96 + static inline struct pwm_imx27_chip *to_pwm_imx27_chip(struct pwm_chip *chip) 97 + { 98 + return pwmchip_get_drvdata(chip); 99 + } 96 100 97 101 static int pwm_imx27_clk_prepare_enable(struct pwm_imx27_chip *imx) 98 102 { ··· 147 145 state->polarity = PWM_POLARITY_INVERSED; 148 146 break; 149 147 default: 150 - dev_warn(chip->dev, "can't set polarity, output disconnected"); 148 + dev_warn(pwmchip_parent(chip), "can't set polarity, output disconnected"); 151 149 } 152 150 153 151 prescaler = MX3_PWMCR_PRESCALER_GET(val); ··· 179 177 static void pwm_imx27_sw_reset(struct pwm_chip *chip) 180 178 { 181 179 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip); 182 - struct device *dev = chip->dev; 180 + struct device *dev = pwmchip_parent(chip); 183 181 int wait_count = 0; 184 182 u32 cr; 185 183 ··· 198 196 struct pwm_device *pwm) 199 197 { 200 198 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip); 201 - struct device *dev = chip->dev; 199 + struct device *dev = pwmchip_parent(chip); 202 200 unsigned int period_ms; 203 201 int fifoav; 204 202 u32 sr; ··· 206 204 sr = readl(imx->mmio_base + MX3_PWMSR); 207 205 fifoav = FIELD_GET(MX3_PWMSR_FIFOAV, sr); 208 206 if (fifoav == MX3_PWMSR_FIFOAV_4WORDS) { 209 - period_ms = DIV_ROUND_UP_ULL(pwm_get_period(pwm), 210 - NSEC_PER_MSEC); 207 + period_ms = DIV_ROUND_UP_ULL(pwm->state.period, 208 + NSEC_PER_MSEC); 211 209 msleep(period_ms); 212 210 213 211 sr = readl(imx->mmio_base + MX3_PWMSR); ··· 221 219 { 222 220 unsigned long period_cycles, duty_cycles, prescale; 223 221 struct pwm_imx27_chip *imx = to_pwm_imx27_chip(chip); 224 - struct pwm_state cstate; 225 222 unsigned long long c; 226 223 unsigned long long clkrate; 227 224 int ret; 228 225 u32 cr; 229 - 230 - pwm_get_state(pwm, &cstate); 231 226 232 227 clkrate = clk_get_rate(imx->clk_per); 233 228 c = clkrate * state->period; ··· 253 254 * Wait for a free FIFO slot if the PWM is already enabled, and flush 254 255 * the FIFO if the PWM was disabled and is about to be enabled. 255 256 */ 256 - if (cstate.enabled) { 257 + if (pwm->state.enabled) { 257 258 pwm_imx27_wait_fifo_slot(chip, pwm); 258 259 } else { 259 260 ret = pwm_imx27_clk_prepare_enable(imx); ··· 305 306 306 307 static int pwm_imx27_probe(struct platform_device *pdev) 307 308 { 309 + struct pwm_chip *chip; 308 310 struct pwm_imx27_chip *imx; 309 311 int ret; 310 312 u32 pwmcr; 311 313 312 - imx = devm_kzalloc(&pdev->dev, sizeof(*imx), GFP_KERNEL); 313 - if (imx == NULL) 314 - return -ENOMEM; 314 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*imx)); 315 + if (IS_ERR(chip)) 316 + return PTR_ERR(chip); 317 + imx = to_pwm_imx27_chip(chip); 315 318 316 319 imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 317 320 if (IS_ERR(imx->clk_ipg)) ··· 325 324 return dev_err_probe(&pdev->dev, PTR_ERR(imx->clk_per), 326 325 "failed to get peripheral clock\n"); 327 326 328 - imx->chip.ops = &pwm_imx27_ops; 329 - imx->chip.dev = &pdev->dev; 330 - imx->chip.npwm = 1; 327 + chip->ops = &pwm_imx27_ops; 331 328 332 329 imx->mmio_base = devm_platform_ioremap_resource(pdev, 0); 333 330 if (IS_ERR(imx->mmio_base)) ··· 340 341 if (!(pwmcr & MX3_PWMCR_EN)) 341 342 pwm_imx27_clk_disable_unprepare(imx); 342 343 343 - return devm_pwmchip_add(&pdev->dev, &imx->chip); 344 + return devm_pwmchip_add(&pdev->dev, chip); 344 345 } 345 346 346 347 static struct platform_driver imx_pwm_driver = {
+8 -9
drivers/pwm/pwm-intel-lgm.c
··· 42 42 #define LGM_PWM_PERIOD_2WIRE_NS (40 * NSEC_PER_MSEC) 43 43 44 44 struct lgm_pwm_chip { 45 - struct pwm_chip chip; 46 45 struct regmap *regmap; 47 46 u32 period; 48 47 }; 49 48 50 49 static inline struct lgm_pwm_chip *to_lgm_pwm_chip(struct pwm_chip *chip) 51 50 { 52 - return container_of(chip, struct lgm_pwm_chip, chip); 51 + return pwmchip_get_drvdata(chip); 53 52 } 54 53 55 54 static int lgm_pwm_enable(struct pwm_chip *chip, bool enable) ··· 167 168 { 168 169 struct device *dev = &pdev->dev; 169 170 struct reset_control *rst; 171 + struct pwm_chip *chip; 170 172 struct lgm_pwm_chip *pc; 171 173 void __iomem *io_base; 172 174 struct clk *clk; 173 175 int ret; 174 176 175 - pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL); 176 - if (!pc) 177 - return -ENOMEM; 177 + chip = devm_pwmchip_alloc(dev, 1, sizeof(*pc)); 178 + if (IS_ERR(chip)) 179 + return PTR_ERR(chip); 180 + pc = to_lgm_pwm_chip(chip); 178 181 179 182 io_base = devm_platform_ioremap_resource(pdev, 0); 180 183 if (IS_ERR(io_base)) ··· 204 203 if (ret) 205 204 return dev_err_probe(dev, ret, "cannot deassert reset control\n"); 206 205 207 - pc->chip.dev = dev; 208 - pc->chip.ops = &lgm_pwm_ops; 209 - pc->chip.npwm = 1; 206 + chip->ops = &lgm_pwm_ops; 210 207 211 208 lgm_pwm_init(pc); 212 209 213 - ret = devm_pwmchip_add(dev, &pc->chip); 210 + ret = devm_pwmchip_add(dev, chip); 214 211 if (ret < 0) 215 212 return dev_err_probe(dev, ret, "failed to add PWM chip\n"); 216 213
+18 -12
drivers/pwm/pwm-iqs620a.c
··· 34 34 35 35 struct iqs620_pwm_private { 36 36 struct iqs62x_core *iqs62x; 37 - struct pwm_chip chip; 37 + struct device *dev; 38 38 struct notifier_block notifier; 39 39 struct mutex lock; 40 40 unsigned int duty_scale; 41 41 }; 42 + 43 + static inline struct iqs620_pwm_private *iqs620_pwm_from_chip(struct pwm_chip *chip) 44 + { 45 + return pwmchip_get_drvdata(chip); 46 + } 42 47 43 48 static int iqs620_pwm_init(struct iqs620_pwm_private *iqs620_pwm, 44 49 unsigned int duty_scale) ··· 78 73 if (state->period < IQS620_PWM_PERIOD_NS) 79 74 return -EINVAL; 80 75 81 - iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); 76 + iqs620_pwm = iqs620_pwm_from_chip(chip); 82 77 83 78 /* 84 79 * The duty cycle generated by the device is calculated as follows: ··· 114 109 { 115 110 struct iqs620_pwm_private *iqs620_pwm; 116 111 117 - iqs620_pwm = container_of(chip, struct iqs620_pwm_private, chip); 112 + iqs620_pwm = iqs620_pwm_from_chip(chip); 118 113 119 114 mutex_lock(&iqs620_pwm->lock); 120 115 ··· 160 155 mutex_unlock(&iqs620_pwm->lock); 161 156 162 157 if (ret) { 163 - dev_err(iqs620_pwm->chip.dev, 158 + dev_err(iqs620_pwm->dev, 164 159 "Failed to re-initialize device: %d\n", ret); 165 160 return NOTIFY_BAD; 166 161 } ··· 181 176 ret = blocking_notifier_chain_unregister(&iqs620_pwm->iqs62x->nh, 182 177 &iqs620_pwm->notifier); 183 178 if (ret) 184 - dev_err(iqs620_pwm->chip.dev, 179 + dev_err(iqs620_pwm->dev, 185 180 "Failed to unregister notifier: %d\n", ret); 186 181 } 187 182 188 183 static int iqs620_pwm_probe(struct platform_device *pdev) 189 184 { 190 185 struct iqs62x_core *iqs62x = dev_get_drvdata(pdev->dev.parent); 186 + struct pwm_chip *chip; 191 187 struct iqs620_pwm_private *iqs620_pwm; 192 188 unsigned int val; 193 189 int ret; 194 190 195 - iqs620_pwm = devm_kzalloc(&pdev->dev, sizeof(*iqs620_pwm), GFP_KERNEL); 196 - if (!iqs620_pwm) 197 - return -ENOMEM; 191 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*iqs620_pwm)); 192 + if (IS_ERR(chip)) 193 + return PTR_ERR(chip); 198 194 195 + iqs620_pwm = iqs620_pwm_from_chip(chip); 196 + iqs620_pwm->dev = &pdev->dev; 199 197 iqs620_pwm->iqs62x = iqs62x; 200 198 201 199 ret = regmap_read(iqs62x->regmap, IQS620_PWR_SETTINGS, &val); ··· 213 205 iqs620_pwm->duty_scale = val + 1; 214 206 } 215 207 216 - iqs620_pwm->chip.dev = &pdev->dev; 217 - iqs620_pwm->chip.ops = &iqs620_pwm_ops; 218 - iqs620_pwm->chip.npwm = 1; 208 + chip->ops = &iqs620_pwm_ops; 219 209 220 210 mutex_init(&iqs620_pwm->lock); 221 211 ··· 231 225 if (ret) 232 226 return ret; 233 227 234 - ret = devm_pwmchip_add(&pdev->dev, &iqs620_pwm->chip); 228 + ret = devm_pwmchip_add(&pdev->dev, chip); 235 229 if (ret) 236 230 dev_err(&pdev->dev, "Failed to add device: %d\n", ret); 237 231
+17 -19
drivers/pwm/pwm-jz4740.c
··· 25 25 }; 26 26 27 27 struct jz4740_pwm_chip { 28 - struct pwm_chip chip; 29 28 struct regmap *map; 30 29 struct clk *clk[]; 31 30 }; 32 31 33 32 static inline struct jz4740_pwm_chip *to_jz4740(struct pwm_chip *chip) 34 33 { 35 - return container_of(chip, struct jz4740_pwm_chip, chip); 34 + return pwmchip_get_drvdata(chip); 36 35 } 37 36 38 - static bool jz4740_pwm_can_use_chn(struct jz4740_pwm_chip *jz, 39 - unsigned int channel) 37 + static bool jz4740_pwm_can_use_chn(struct pwm_chip *chip, unsigned int channel) 40 38 { 41 39 /* Enable all TCU channels for PWM use by default except channels 0/1 */ 42 - u32 pwm_channels_mask = GENMASK(jz->chip.npwm - 1, 2); 40 + u32 pwm_channels_mask = GENMASK(chip->npwm - 1, 2); 43 41 44 - device_property_read_u32(jz->chip.dev->parent, 42 + device_property_read_u32(pwmchip_parent(chip)->parent, 45 43 "ingenic,pwm-channels-mask", 46 44 &pwm_channels_mask); 47 45 ··· 53 55 char name[16]; 54 56 int err; 55 57 56 - if (!jz4740_pwm_can_use_chn(jz, pwm->hwpwm)) 58 + if (!jz4740_pwm_can_use_chn(chip, pwm->hwpwm)) 57 59 return -EBUSY; 58 60 59 61 snprintf(name, sizeof(name), "timer%u", pwm->hwpwm); 60 62 61 - clk = clk_get(chip->dev, name); 63 + clk = clk_get(pwmchip_parent(chip), name); 62 64 if (IS_ERR(clk)) { 63 - dev_err(chip->dev, "error %pe: Failed to get clock\n", clk); 65 + dev_err(pwmchip_parent(chip), 66 + "error %pe: Failed to get clock\n", clk); 64 67 return PTR_ERR(clk); 65 68 } 66 69 ··· 149 150 */ 150 151 rate = clk_round_rate(clk, tmp); 151 152 if (rate < 0) { 152 - dev_err(chip->dev, "Unable to round rate: %ld\n", rate); 153 + dev_err(pwmchip_parent(chip), "Unable to round rate: %ld\n", rate); 153 154 return rate; 154 155 } 155 156 ··· 170 171 171 172 err = clk_set_rate(clk, rate); 172 173 if (err) { 173 - dev_err(chip->dev, "Unable to set rate: %d\n", err); 174 + dev_err(pwmchip_parent(chip), "Unable to set rate: %d\n", err); 174 175 return err; 175 176 } 176 177 ··· 223 224 static int jz4740_pwm_probe(struct platform_device *pdev) 224 225 { 225 226 struct device *dev = &pdev->dev; 227 + struct pwm_chip *chip; 226 228 struct jz4740_pwm_chip *jz; 227 229 const struct soc_info *info; 228 230 ··· 231 231 if (!info) 232 232 return -EINVAL; 233 233 234 - jz = devm_kzalloc(dev, struct_size(jz, clk, info->num_pwms), 235 - GFP_KERNEL); 236 - if (!jz) 237 - return -ENOMEM; 234 + chip = devm_pwmchip_alloc(dev, info->num_pwms, struct_size(jz, clk, info->num_pwms)); 235 + if (IS_ERR(chip)) 236 + return PTR_ERR(chip); 237 + jz = to_jz4740(chip); 238 238 239 239 jz->map = device_node_to_regmap(dev->parent->of_node); 240 240 if (IS_ERR(jz->map)) { ··· 242 242 return PTR_ERR(jz->map); 243 243 } 244 244 245 - jz->chip.dev = dev; 246 - jz->chip.ops = &jz4740_pwm_ops; 247 - jz->chip.npwm = info->num_pwms; 245 + chip->ops = &jz4740_pwm_ops; 248 246 249 - return devm_pwmchip_add(dev, &jz->chip); 247 + return devm_pwmchip_add(dev, chip); 250 248 } 251 249 252 250 static const struct soc_info jz4740_soc_info = {
+8 -9
drivers/pwm/pwm-keembay.c
··· 36 36 #define KMB_PWM_HIGHLOW_OFFSET(ch) (0x20 + 4 * (ch)) 37 37 38 38 struct keembay_pwm { 39 - struct pwm_chip chip; 40 39 struct device *dev; 41 40 struct clk *clk; 42 41 void __iomem *base; ··· 43 44 44 45 static inline struct keembay_pwm *to_keembay_pwm_dev(struct pwm_chip *chip) 45 46 { 46 - return container_of(chip, struct keembay_pwm, chip); 47 + return pwmchip_get_drvdata(chip); 47 48 } 48 49 49 50 static void keembay_clk_unprepare(void *data) ··· 184 185 static int keembay_pwm_probe(struct platform_device *pdev) 185 186 { 186 187 struct device *dev = &pdev->dev; 188 + struct pwm_chip *chip; 187 189 struct keembay_pwm *priv; 188 190 int ret; 189 191 190 - priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 191 - if (!priv) 192 - return -ENOMEM; 192 + chip = devm_pwmchip_alloc(dev, KMB_TOTAL_PWM_CHANNELS, sizeof(*priv)); 193 + if (IS_ERR(chip)) 194 + return PTR_ERR(chip); 195 + priv = to_keembay_pwm_dev(chip); 193 196 194 197 priv->clk = devm_clk_get(dev, NULL); 195 198 if (IS_ERR(priv->clk)) ··· 205 204 if (ret) 206 205 return ret; 207 206 208 - priv->chip.dev = dev; 209 - priv->chip.ops = &keembay_pwm_ops; 210 - priv->chip.npwm = KMB_TOTAL_PWM_CHANNELS; 207 + chip->ops = &keembay_pwm_ops; 211 208 212 - ret = devm_pwmchip_add(dev, &priv->chip); 209 + ret = devm_pwmchip_add(dev, chip); 213 210 if (ret) 214 211 return dev_err_probe(dev, ret, "Failed to add PWM chip\n"); 215 212
+8 -9
drivers/pwm/pwm-lp3943.c
··· 20 20 #define LP3943_MAX_PERIOD 1600000 21 21 22 22 struct lp3943_pwm { 23 - struct pwm_chip chip; 24 23 struct lp3943 *lp3943; 25 24 struct lp3943_platform_data *pdata; 26 25 struct lp3943_pwm_map pwm_map[LP3943_NUM_PWMS]; ··· 27 28 28 29 static inline struct lp3943_pwm *to_lp3943_pwm(struct pwm_chip *chip) 29 30 { 30 - return container_of(chip, struct lp3943_pwm, chip); 31 + return pwmchip_get_drvdata(chip); 31 32 } 32 33 33 34 static struct lp3943_pwm_map * ··· 272 273 static int lp3943_pwm_probe(struct platform_device *pdev) 273 274 { 274 275 struct lp3943 *lp3943 = dev_get_drvdata(pdev->dev.parent); 276 + struct pwm_chip *chip; 275 277 struct lp3943_pwm *lp3943_pwm; 276 278 int ret; 277 279 278 - lp3943_pwm = devm_kzalloc(&pdev->dev, sizeof(*lp3943_pwm), GFP_KERNEL); 279 - if (!lp3943_pwm) 280 - return -ENOMEM; 280 + chip = devm_pwmchip_alloc(&pdev->dev, LP3943_NUM_PWMS, sizeof(*lp3943_pwm)); 281 + if (IS_ERR(chip)) 282 + return PTR_ERR(chip); 283 + lp3943_pwm = to_lp3943_pwm(chip); 281 284 282 285 lp3943_pwm->pdata = lp3943->pdata; 283 286 if (!lp3943_pwm->pdata) { ··· 293 292 } 294 293 295 294 lp3943_pwm->lp3943 = lp3943; 296 - lp3943_pwm->chip.dev = &pdev->dev; 297 - lp3943_pwm->chip.ops = &lp3943_pwm_ops; 298 - lp3943_pwm->chip.npwm = LP3943_NUM_PWMS; 295 + chip->ops = &lp3943_pwm_ops; 299 296 300 - return devm_pwmchip_add(&pdev->dev, &lp3943_pwm->chip); 297 + return devm_pwmchip_add(&pdev->dev, chip); 301 298 } 302 299 303 300 #ifdef CONFIG_OF
+15 -19
drivers/pwm/pwm-lpc18xx-sct.c
··· 92 92 }; 93 93 94 94 struct lpc18xx_pwm_chip { 95 - struct device *dev; 96 - struct pwm_chip chip; 97 95 void __iomem *base; 98 96 struct clk *pwm_clk; 99 97 unsigned long clk_rate; ··· 108 110 static inline struct lpc18xx_pwm_chip * 109 111 to_lpc18xx_pwm_chip(struct pwm_chip *chip) 110 112 { 111 - return container_of(chip, struct lpc18xx_pwm_chip, chip); 113 + return pwmchip_get_drvdata(chip); 112 114 } 113 115 114 116 static inline void lpc18xx_pwm_writel(struct lpc18xx_pwm_chip *lpc18xx_pwm, ··· 196 198 197 199 if (period_ns < lpc18xx_pwm->min_period_ns || 198 200 period_ns > lpc18xx_pwm->max_period_ns) { 199 - dev_err(chip->dev, "period %d not in range\n", period_ns); 201 + dev_err(pwmchip_parent(chip), "period %d not in range\n", period_ns); 200 202 return -ERANGE; 201 203 } 202 204 ··· 212 214 */ 213 215 if (requested_events > 2 && lpc18xx_pwm->period_ns != period_ns && 214 216 lpc18xx_pwm->period_ns) { 215 - dev_err(chip->dev, "conflicting period requested for PWM %u\n", 217 + dev_err(pwmchip_parent(chip), "conflicting period requested for PWM %u\n", 216 218 pwm->hwpwm); 217 219 mutex_unlock(&lpc18xx_pwm->period_lock); 218 220 return -EBUSY; ··· 287 289 LPC18XX_PWM_EVENT_MAX); 288 290 289 291 if (event >= LPC18XX_PWM_EVENT_MAX) { 290 - dev_err(lpc18xx_pwm->dev, 292 + dev_err(pwmchip_parent(chip), 291 293 "maximum number of simultaneous channels reached\n"); 292 294 return -EBUSY; 293 295 } ··· 347 349 348 350 static int lpc18xx_pwm_probe(struct platform_device *pdev) 349 351 { 352 + struct pwm_chip *chip; 350 353 struct lpc18xx_pwm_chip *lpc18xx_pwm; 351 354 int ret; 352 355 u64 val; 353 356 354 - lpc18xx_pwm = devm_kzalloc(&pdev->dev, sizeof(*lpc18xx_pwm), 355 - GFP_KERNEL); 356 - if (!lpc18xx_pwm) 357 - return -ENOMEM; 358 - 359 - lpc18xx_pwm->dev = &pdev->dev; 357 + chip = devm_pwmchip_alloc(&pdev->dev, LPC18XX_NUM_PWMS, sizeof(*lpc18xx_pwm)); 358 + if (IS_ERR(chip)) 359 + return PTR_ERR(chip); 360 + lpc18xx_pwm = to_lpc18xx_pwm_chip(chip); 360 361 361 362 lpc18xx_pwm->base = devm_platform_ioremap_resource(pdev, 0); 362 363 if (IS_ERR(lpc18xx_pwm->base)) ··· 386 389 lpc18xx_pwm->min_period_ns = DIV_ROUND_UP(NSEC_PER_SEC, 387 390 lpc18xx_pwm->clk_rate); 388 391 389 - lpc18xx_pwm->chip.dev = &pdev->dev; 390 - lpc18xx_pwm->chip.ops = &lpc18xx_pwm_ops; 391 - lpc18xx_pwm->chip.npwm = LPC18XX_NUM_PWMS; 392 + chip->ops = &lpc18xx_pwm_ops; 392 393 393 394 /* SCT counter must be in unify (32 bit) mode */ 394 395 lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CONFIG, ··· 418 423 val |= LPC18XX_PWM_PRE(0); 419 424 lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CTRL, val); 420 425 421 - ret = pwmchip_add(&lpc18xx_pwm->chip); 426 + ret = pwmchip_add(chip); 422 427 if (ret < 0) 423 428 return dev_err_probe(&pdev->dev, ret, "pwmchip_add failed\n"); 424 429 425 - platform_set_drvdata(pdev, lpc18xx_pwm); 430 + platform_set_drvdata(pdev, chip); 426 431 427 432 return 0; 428 433 } 429 434 430 435 static void lpc18xx_pwm_remove(struct platform_device *pdev) 431 436 { 432 - struct lpc18xx_pwm_chip *lpc18xx_pwm = platform_get_drvdata(pdev); 437 + struct pwm_chip *chip = platform_get_drvdata(pdev); 438 + struct lpc18xx_pwm_chip *lpc18xx_pwm = to_lpc18xx_pwm_chip(chip); 433 439 u32 val; 434 440 435 - pwmchip_remove(&lpc18xx_pwm->chip); 441 + pwmchip_remove(chip); 436 442 437 443 val = lpc18xx_pwm_readl(lpc18xx_pwm, LPC18XX_PWM_CTRL); 438 444 lpc18xx_pwm_writel(lpc18xx_pwm, LPC18XX_PWM_CTRL,
+11 -10
drivers/pwm/pwm-lpc32xx.c
··· 15 15 #include <linux/slab.h> 16 16 17 17 struct lpc32xx_pwm_chip { 18 - struct pwm_chip chip; 19 18 struct clk *clk; 20 19 void __iomem *base; 21 20 }; ··· 22 23 #define PWM_ENABLE BIT(31) 23 24 #define PWM_PIN_LEVEL BIT(30) 24 25 25 - #define to_lpc32xx_pwm_chip(_chip) \ 26 - container_of(_chip, struct lpc32xx_pwm_chip, chip) 26 + static inline struct lpc32xx_pwm_chip *to_lpc32xx_pwm_chip(struct pwm_chip *chip) 27 + { 28 + return pwmchip_get_drvdata(chip); 29 + } 27 30 28 31 static int lpc32xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 29 32 int duty_ns, int period_ns) ··· 120 119 121 120 static int lpc32xx_pwm_probe(struct platform_device *pdev) 122 121 { 122 + struct pwm_chip *chip; 123 123 struct lpc32xx_pwm_chip *lpc32xx; 124 124 int ret; 125 125 u32 val; 126 126 127 - lpc32xx = devm_kzalloc(&pdev->dev, sizeof(*lpc32xx), GFP_KERNEL); 128 - if (!lpc32xx) 129 - return -ENOMEM; 127 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*lpc32xx)); 128 + if (IS_ERR(chip)) 129 + return PTR_ERR(chip); 130 + lpc32xx = to_lpc32xx_pwm_chip(chip); 130 131 131 132 lpc32xx->base = devm_platform_ioremap_resource(pdev, 0); 132 133 if (IS_ERR(lpc32xx->base)) ··· 138 135 if (IS_ERR(lpc32xx->clk)) 139 136 return PTR_ERR(lpc32xx->clk); 140 137 141 - lpc32xx->chip.dev = &pdev->dev; 142 - lpc32xx->chip.ops = &lpc32xx_pwm_ops; 143 - lpc32xx->chip.npwm = 1; 138 + chip->ops = &lpc32xx_pwm_ops; 144 139 145 140 /* If PWM is disabled, configure the output to the default value */ 146 141 val = readl(lpc32xx->base); 147 142 val &= ~PWM_PIN_LEVEL; 148 143 writel(val, lpc32xx->base); 149 144 150 - ret = devm_pwmchip_add(&pdev->dev, &lpc32xx->chip); 145 + ret = devm_pwmchip_add(&pdev->dev, chip); 151 146 if (ret < 0) { 152 147 dev_err(&pdev->dev, "failed to add PWM chip, error %d\n", ret); 153 148 return ret;
+4 -6
drivers/pwm/pwm-lpss-pci.c
··· 18 18 const struct pci_device_id *id) 19 19 { 20 20 const struct pwm_lpss_boardinfo *info; 21 - struct pwm_lpss_chip *lpwm; 21 + struct pwm_chip *chip; 22 22 int err; 23 23 24 24 err = pcim_enable_device(pdev); ··· 30 30 return err; 31 31 32 32 info = (struct pwm_lpss_boardinfo *)id->driver_data; 33 - lpwm = devm_pwm_lpss_probe(&pdev->dev, pcim_iomap_table(pdev)[0], info); 34 - if (IS_ERR(lpwm)) 35 - return PTR_ERR(lpwm); 36 - 37 - pci_set_drvdata(pdev, lpwm); 33 + chip = devm_pwm_lpss_probe(&pdev->dev, pcim_iomap_table(pdev)[0], info); 34 + if (IS_ERR(chip)) 35 + return PTR_ERR(chip); 38 36 39 37 pm_runtime_put(&pdev->dev); 40 38 pm_runtime_allow(&pdev->dev);
+4 -6
drivers/pwm/pwm-lpss-platform.c
··· 20 20 static int pwm_lpss_probe_platform(struct platform_device *pdev) 21 21 { 22 22 const struct pwm_lpss_boardinfo *info; 23 - struct pwm_lpss_chip *lpwm; 23 + struct pwm_chip *chip; 24 24 void __iomem *base; 25 25 26 26 info = device_get_match_data(&pdev->dev); ··· 31 31 if (IS_ERR(base)) 32 32 return PTR_ERR(base); 33 33 34 - lpwm = devm_pwm_lpss_probe(&pdev->dev, base, info); 35 - if (IS_ERR(lpwm)) 36 - return PTR_ERR(lpwm); 37 - 38 - platform_set_drvdata(pdev, lpwm); 34 + chip = devm_pwm_lpss_probe(&pdev->dev, base, info); 35 + if (IS_ERR(chip)) 36 + return PTR_ERR(chip); 39 37 40 38 /* 41 39 * On Cherry Trail devices the GFX0._PS0 AML checks if the controller
+19 -19
drivers/pwm/pwm-lpss.c
··· 68 68 69 69 static inline struct pwm_lpss_chip *to_lpwm(struct pwm_chip *chip) 70 70 { 71 - return container_of(chip, struct pwm_lpss_chip, chip); 71 + return pwmchip_get_drvdata(chip); 72 72 } 73 73 74 74 static inline u32 pwm_lpss_read(const struct pwm_device *pwm) ··· 106 106 */ 107 107 err = readl_poll_timeout(addr, val, !(val & PWM_SW_UPDATE), 40, ms); 108 108 if (err) 109 - dev_err(pwm->chip->dev, "PWM_SW_UPDATE was not cleared\n"); 109 + dev_err(pwmchip_parent(pwm->chip), "PWM_SW_UPDATE was not cleared\n"); 110 110 111 111 return err; 112 112 } ··· 114 114 static inline int pwm_lpss_is_updating(struct pwm_device *pwm) 115 115 { 116 116 if (pwm_lpss_read(pwm) & PWM_SW_UPDATE) { 117 - dev_err(pwm->chip->dev, "PWM_SW_UPDATE is still set, skipping update\n"); 117 + dev_err(pwmchip_parent(pwm->chip), "PWM_SW_UPDATE is still set, skipping update\n"); 118 118 return -EBUSY; 119 119 } 120 120 ··· 190 190 191 191 if (state->enabled) { 192 192 if (!pwm_is_enabled(pwm)) { 193 - pm_runtime_get_sync(chip->dev); 193 + pm_runtime_get_sync(pwmchip_parent(chip)); 194 194 ret = pwm_lpss_prepare_enable(lpwm, pwm, state); 195 195 if (ret) 196 - pm_runtime_put(chip->dev); 196 + pm_runtime_put(pwmchip_parent(chip)); 197 197 } else { 198 198 ret = pwm_lpss_prepare_enable(lpwm, pwm, state); 199 199 } 200 200 } else if (pwm_is_enabled(pwm)) { 201 201 pwm_lpss_write(pwm, pwm_lpss_read(pwm) & ~PWM_ENABLE); 202 - pm_runtime_put(chip->dev); 202 + pm_runtime_put(pwmchip_parent(chip)); 203 203 } 204 204 205 205 return ret; ··· 213 213 unsigned long long base_unit, freq, on_time_div; 214 214 u32 ctrl; 215 215 216 - pm_runtime_get_sync(chip->dev); 216 + pm_runtime_get_sync(pwmchip_parent(chip)); 217 217 218 218 base_unit_range = BIT(lpwm->info->base_unit_bits); 219 219 ··· 235 235 state->polarity = PWM_POLARITY_NORMAL; 236 236 state->enabled = !!(ctrl & PWM_ENABLE); 237 237 238 - pm_runtime_put(chip->dev); 238 + pm_runtime_put(pwmchip_parent(chip)); 239 239 240 240 return 0; 241 241 } ··· 245 245 .get_state = pwm_lpss_get_state, 246 246 }; 247 247 248 - struct pwm_lpss_chip *devm_pwm_lpss_probe(struct device *dev, void __iomem *base, 249 - const struct pwm_lpss_boardinfo *info) 248 + struct pwm_chip *devm_pwm_lpss_probe(struct device *dev, void __iomem *base, 249 + const struct pwm_lpss_boardinfo *info) 250 250 { 251 251 struct pwm_lpss_chip *lpwm; 252 + struct pwm_chip *chip; 252 253 unsigned long c; 253 254 int i, ret; 254 255 u32 ctrl; ··· 257 256 if (WARN_ON(info->npwm > LPSS_MAX_PWMS)) 258 257 return ERR_PTR(-ENODEV); 259 258 260 - lpwm = devm_kzalloc(dev, sizeof(*lpwm), GFP_KERNEL); 261 - if (!lpwm) 262 - return ERR_PTR(-ENOMEM); 259 + chip = devm_pwmchip_alloc(dev, info->npwm, sizeof(*lpwm)); 260 + if (IS_ERR(chip)) 261 + return chip; 262 + lpwm = to_lpwm(chip); 263 263 264 264 lpwm->regs = base; 265 265 lpwm->info = info; ··· 269 267 if (!c) 270 268 return ERR_PTR(-EINVAL); 271 269 272 - lpwm->chip.dev = dev; 273 - lpwm->chip.ops = &pwm_lpss_ops; 274 - lpwm->chip.npwm = info->npwm; 270 + chip->ops = &pwm_lpss_ops; 275 271 276 - ret = devm_pwmchip_add(dev, &lpwm->chip); 272 + ret = devm_pwmchip_add(dev, chip); 277 273 if (ret) { 278 274 dev_err(dev, "failed to add PWM chip: %d\n", ret); 279 275 return ERR_PTR(ret); 280 276 } 281 277 282 278 for (i = 0; i < lpwm->info->npwm; i++) { 283 - ctrl = pwm_lpss_read(&lpwm->chip.pwms[i]); 279 + ctrl = pwm_lpss_read(&chip->pwms[i]); 284 280 if (ctrl & PWM_ENABLE) 285 281 pm_runtime_get(dev); 286 282 } 287 283 288 - return lpwm; 284 + return chip; 289 285 } 290 286 EXPORT_SYMBOL_GPL(devm_pwm_lpss_probe); 291 287
-1
drivers/pwm/pwm-lpss.h
··· 18 18 #define LPSS_MAX_PWMS 4 19 19 20 20 struct pwm_lpss_chip { 21 - struct pwm_chip chip; 22 21 void __iomem *regs; 23 22 const struct pwm_lpss_boardinfo *info; 24 23 };
+23 -15
drivers/pwm/pwm-mediatek.c
··· 42 42 43 43 /** 44 44 * struct pwm_mediatek_chip - struct representing PWM chip 45 - * @chip: linux PWM chip representation 46 45 * @regs: base address of PWM chip 47 46 * @clk_top: the top clock generator 48 47 * @clk_main: the clock used by PWM core 49 48 * @clk_pwms: the clock used by each PWM channel 50 - * @clk_freq: the fix clock frequency of legacy MIPS SoC 51 49 * @soc: pointer to chip's platform data 52 50 */ 53 51 struct pwm_mediatek_chip { 54 - struct pwm_chip chip; 55 52 void __iomem *regs; 56 53 struct clk *clk_top; 57 54 struct clk *clk_main; ··· 67 70 static inline struct pwm_mediatek_chip * 68 71 to_pwm_mediatek_chip(struct pwm_chip *chip) 69 72 { 70 - return container_of(chip, struct pwm_mediatek_chip, chip); 73 + return pwmchip_get_drvdata(chip); 71 74 } 72 75 73 76 static int pwm_mediatek_clk_enable(struct pwm_chip *chip, ··· 147 150 148 151 if (clkdiv > PWM_CLK_DIV_MAX) { 149 152 pwm_mediatek_clk_disable(chip, pwm); 150 - dev_err(chip->dev, "period of %d ns not supported\n", period_ns); 153 + dev_err(pwmchip_parent(chip), "period of %d ns not supported\n", period_ns); 151 154 return -EINVAL; 152 155 } 153 156 ··· 230 233 231 234 static int pwm_mediatek_probe(struct platform_device *pdev) 232 235 { 236 + struct pwm_chip *chip; 233 237 struct pwm_mediatek_chip *pc; 238 + const struct pwm_mediatek_of_data *soc; 234 239 unsigned int i; 235 240 int ret; 236 241 237 - pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); 238 - if (!pc) 239 - return -ENOMEM; 242 + soc = of_device_get_match_data(&pdev->dev); 240 243 241 - pc->soc = of_device_get_match_data(&pdev->dev); 244 + chip = devm_pwmchip_alloc(&pdev->dev, soc->num_pwms, sizeof(*pc)); 245 + if (IS_ERR(chip)) 246 + return PTR_ERR(chip); 247 + pc = to_pwm_mediatek_chip(chip); 248 + 249 + pc->soc = soc; 242 250 243 251 pc->regs = devm_platform_ioremap_resource(pdev, 0); 244 252 if (IS_ERR(pc->regs)) 245 253 return PTR_ERR(pc->regs); 246 254 247 - pc->clk_pwms = devm_kmalloc_array(&pdev->dev, pc->soc->num_pwms, 255 + pc->clk_pwms = devm_kmalloc_array(&pdev->dev, soc->num_pwms, 248 256 sizeof(*pc->clk_pwms), GFP_KERNEL); 249 257 if (!pc->clk_pwms) 250 258 return -ENOMEM; ··· 264 262 return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk_main), 265 263 "Failed to get main clock\n"); 266 264 267 - for (i = 0; i < pc->soc->num_pwms; i++) { 265 + for (i = 0; i < soc->num_pwms; i++) { 268 266 char name[8]; 269 267 270 268 snprintf(name, sizeof(name), "pwm%d", i + 1); ··· 275 273 "Failed to get %s clock\n", name); 276 274 } 277 275 278 - pc->chip.dev = &pdev->dev; 279 - pc->chip.ops = &pwm_mediatek_ops; 280 - pc->chip.npwm = pc->soc->num_pwms; 276 + chip->ops = &pwm_mediatek_ops; 281 277 282 - ret = devm_pwmchip_add(&pdev->dev, &pc->chip); 278 + ret = devm_pwmchip_add(&pdev->dev, chip); 283 279 if (ret < 0) 284 280 return dev_err_probe(&pdev->dev, ret, "pwmchip_add() failed\n"); 285 281 ··· 340 340 .reg_offset = mtk_pwm_reg_offset_v1, 341 341 }; 342 342 343 + static const struct pwm_mediatek_of_data mt7988_pwm_data = { 344 + .num_pwms = 8, 345 + .pwm45_fixup = false, 346 + .has_ck_26m_sel = false, 347 + .reg_offset = mtk_pwm_reg_offset_v2, 348 + }; 349 + 343 350 static const struct pwm_mediatek_of_data mt8183_pwm_data = { 344 351 .num_pwms = 4, 345 352 .pwm45_fixup = false, ··· 377 370 { .compatible = "mediatek,mt7629-pwm", .data = &mt7629_pwm_data }, 378 371 { .compatible = "mediatek,mt7981-pwm", .data = &mt7981_pwm_data }, 379 372 { .compatible = "mediatek,mt7986-pwm", .data = &mt7986_pwm_data }, 373 + { .compatible = "mediatek,mt7988-pwm", .data = &mt7988_pwm_data }, 380 374 { .compatible = "mediatek,mt8183-pwm", .data = &mt8183_pwm_data }, 381 375 { .compatible = "mediatek,mt8365-pwm", .data = &mt8365_pwm_data }, 382 376 { .compatible = "mediatek,mt8516-pwm", .data = &mt8516_pwm_data },
+41 -69
drivers/pwm/pwm-meson.c
··· 60 60 #define MISC_A_EN BIT(0) 61 61 62 62 #define MESON_NUM_PWMS 2 63 - #define MESON_MAX_MUX_PARENTS 4 63 + #define MESON_NUM_MUX_PARENTS 4 64 64 65 65 static struct meson_pwm_channel_data { 66 66 u8 reg_offset; ··· 97 97 }; 98 98 99 99 struct meson_pwm_data { 100 - const char * const *parent_names; 101 - unsigned int num_parents; 100 + const char *const parent_names[MESON_NUM_MUX_PARENTS]; 102 101 }; 103 102 104 103 struct meson_pwm { 105 - struct pwm_chip chip; 106 104 const struct meson_pwm_data *data; 107 105 struct meson_pwm_channel channels[MESON_NUM_PWMS]; 108 106 void __iomem *base; ··· 113 115 114 116 static inline struct meson_pwm *to_meson_pwm(struct pwm_chip *chip) 115 117 { 116 - return container_of(chip, struct meson_pwm, chip); 118 + return pwmchip_get_drvdata(chip); 117 119 } 118 120 119 121 static int meson_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) 120 122 { 121 123 struct meson_pwm *meson = to_meson_pwm(chip); 122 124 struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm]; 123 - struct device *dev = chip->dev; 125 + struct device *dev = pwmchip_parent(chip); 124 126 int err; 125 127 126 128 err = clk_prepare_enable(channel->clk); ··· 141 143 clk_disable_unprepare(channel->clk); 142 144 } 143 145 144 - static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm, 146 + static int meson_pwm_calc(struct pwm_chip *chip, struct pwm_device *pwm, 145 147 const struct pwm_state *state) 146 148 { 149 + struct meson_pwm *meson = to_meson_pwm(chip); 147 150 struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm]; 148 151 unsigned int cnt, duty_cnt; 149 152 unsigned long fin_freq; ··· 168 169 169 170 fin_freq = clk_round_rate(channel->clk, freq); 170 171 if (fin_freq == 0) { 171 - dev_err(meson->chip.dev, "invalid source clock frequency\n"); 172 + dev_err(pwmchip_parent(chip), "invalid source clock frequency\n"); 172 173 return -EINVAL; 173 174 } 174 175 175 - dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq); 176 + dev_dbg(pwmchip_parent(chip), "fin_freq: %lu Hz\n", fin_freq); 176 177 177 178 cnt = div_u64(fin_freq * period, NSEC_PER_SEC); 178 179 if (cnt > 0xffff) { 179 - dev_err(meson->chip.dev, "unable to get period cnt\n"); 180 + dev_err(pwmchip_parent(chip), "unable to get period cnt\n"); 180 181 return -EINVAL; 181 182 } 182 183 183 - dev_dbg(meson->chip.dev, "period=%llu cnt=%u\n", period, cnt); 184 + dev_dbg(pwmchip_parent(chip), "period=%llu cnt=%u\n", period, cnt); 184 185 185 186 if (duty == period) { 186 187 channel->hi = cnt; ··· 191 192 } else { 192 193 duty_cnt = div_u64(fin_freq * duty, NSEC_PER_SEC); 193 194 194 - dev_dbg(meson->chip.dev, "duty=%llu duty_cnt=%u\n", duty, duty_cnt); 195 + dev_dbg(pwmchip_parent(chip), "duty=%llu duty_cnt=%u\n", duty, duty_cnt); 195 196 196 197 channel->hi = duty_cnt; 197 198 channel->lo = cnt - duty_cnt; ··· 202 203 return 0; 203 204 } 204 205 205 - static void meson_pwm_enable(struct meson_pwm *meson, struct pwm_device *pwm) 206 + static void meson_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) 206 207 { 208 + struct meson_pwm *meson = to_meson_pwm(chip); 207 209 struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm]; 208 210 struct meson_pwm_channel_data *channel_data; 209 211 unsigned long flags; ··· 215 215 216 216 err = clk_set_rate(channel->clk, channel->rate); 217 217 if (err) 218 - dev_err(meson->chip.dev, "setting clock rate failed\n"); 218 + dev_err(pwmchip_parent(chip), "setting clock rate failed\n"); 219 219 220 220 spin_lock_irqsave(&meson->lock, flags); 221 221 ··· 230 230 spin_unlock_irqrestore(&meson->lock, flags); 231 231 } 232 232 233 - static void meson_pwm_disable(struct meson_pwm *meson, struct pwm_device *pwm) 233 + static void meson_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) 234 234 { 235 + struct meson_pwm *meson = to_meson_pwm(chip); 235 236 unsigned long flags; 236 237 u32 value; 237 238 ··· 270 269 channel->hi = ~0; 271 270 channel->lo = 0; 272 271 273 - meson_pwm_enable(meson, pwm); 272 + meson_pwm_enable(chip, pwm); 274 273 } else { 275 - meson_pwm_disable(meson, pwm); 274 + meson_pwm_disable(chip, pwm); 276 275 } 277 276 } else { 278 - err = meson_pwm_calc(meson, pwm, state); 277 + err = meson_pwm_calc(chip, pwm, state); 279 278 if (err < 0) 280 279 return err; 281 280 282 - meson_pwm_enable(meson, pwm); 281 + meson_pwm_enable(chip, pwm); 283 282 } 284 283 285 284 return 0; ··· 338 337 .get_state = meson_pwm_get_state, 339 338 }; 340 339 341 - static const char * const pwm_meson8b_parent_names[] = { 342 - "xtal", NULL, "fclk_div4", "fclk_div3" 343 - }; 344 - 345 340 static const struct meson_pwm_data pwm_meson8b_data = { 346 - .parent_names = pwm_meson8b_parent_names, 347 - .num_parents = ARRAY_SIZE(pwm_meson8b_parent_names), 341 + .parent_names = { "xtal", NULL, "fclk_div4", "fclk_div3" }, 348 342 }; 349 343 350 344 /* 351 345 * Only the 2 first inputs of the GXBB AO PWMs are valid 352 346 * The last 2 are grounded 353 347 */ 354 - static const char * const pwm_gxbb_ao_parent_names[] = { 355 - "xtal", "clk81" 356 - }; 357 - 358 348 static const struct meson_pwm_data pwm_gxbb_ao_data = { 359 - .parent_names = pwm_gxbb_ao_parent_names, 360 - .num_parents = ARRAY_SIZE(pwm_gxbb_ao_parent_names), 361 - }; 362 - 363 - static const char * const pwm_axg_ee_parent_names[] = { 364 - "xtal", "fclk_div5", "fclk_div4", "fclk_div3" 349 + .parent_names = { "xtal", "clk81", NULL, NULL }, 365 350 }; 366 351 367 352 static const struct meson_pwm_data pwm_axg_ee_data = { 368 - .parent_names = pwm_axg_ee_parent_names, 369 - .num_parents = ARRAY_SIZE(pwm_axg_ee_parent_names), 370 - }; 371 - 372 - static const char * const pwm_axg_ao_parent_names[] = { 373 - "xtal", "axg_ao_clk81", "fclk_div4", "fclk_div5" 353 + .parent_names = { "xtal", "fclk_div5", "fclk_div4", "fclk_div3" }, 374 354 }; 375 355 376 356 static const struct meson_pwm_data pwm_axg_ao_data = { 377 - .parent_names = pwm_axg_ao_parent_names, 378 - .num_parents = ARRAY_SIZE(pwm_axg_ao_parent_names), 379 - }; 380 - 381 - static const char * const pwm_g12a_ao_ab_parent_names[] = { 382 - "xtal", "g12a_ao_clk81", "fclk_div4", "fclk_div5" 357 + .parent_names = { "xtal", "axg_ao_clk81", "fclk_div4", "fclk_div5" }, 383 358 }; 384 359 385 360 static const struct meson_pwm_data pwm_g12a_ao_ab_data = { 386 - .parent_names = pwm_g12a_ao_ab_parent_names, 387 - .num_parents = ARRAY_SIZE(pwm_g12a_ao_ab_parent_names), 388 - }; 389 - 390 - static const char * const pwm_g12a_ao_cd_parent_names[] = { 391 - "xtal", "g12a_ao_clk81", 361 + .parent_names = { "xtal", "g12a_ao_clk81", "fclk_div4", "fclk_div5" }, 392 362 }; 393 363 394 364 static const struct meson_pwm_data pwm_g12a_ao_cd_data = { 395 - .parent_names = pwm_g12a_ao_cd_parent_names, 396 - .num_parents = ARRAY_SIZE(pwm_g12a_ao_cd_parent_names), 365 + .parent_names = { "xtal", "g12a_ao_clk81", NULL, NULL }, 397 366 }; 398 367 399 368 static const struct of_device_id meson_pwm_matches[] = { ··· 403 432 }; 404 433 MODULE_DEVICE_TABLE(of, meson_pwm_matches); 405 434 406 - static int meson_pwm_init_channels(struct meson_pwm *meson) 435 + static int meson_pwm_init_channels(struct pwm_chip *chip) 407 436 { 408 - struct clk_parent_data mux_parent_data[MESON_MAX_MUX_PARENTS] = {}; 409 - struct device *dev = meson->chip.dev; 437 + struct meson_pwm *meson = to_meson_pwm(chip); 438 + struct clk_parent_data mux_parent_data[MESON_NUM_MUX_PARENTS] = {}; 439 + struct device *dev = pwmchip_parent(chip); 410 440 unsigned int i; 411 441 char name[255]; 412 442 int err; 413 443 414 - for (i = 0; i < meson->data->num_parents; i++) { 444 + for (i = 0; i < MESON_NUM_MUX_PARENTS; i++) { 415 445 mux_parent_data[i].index = -1; 416 446 mux_parent_data[i].name = meson->data->parent_names[i]; 417 447 } 418 448 419 - for (i = 0; i < meson->chip.npwm; i++) { 449 + for (i = 0; i < chip->npwm; i++) { 420 450 struct meson_pwm_channel *channel = &meson->channels[i]; 421 451 struct clk_parent_data div_parent = {}, gate_parent = {}; 422 452 struct clk_init_data init = {}; ··· 428 456 init.ops = &clk_mux_ops; 429 457 init.flags = 0; 430 458 init.parent_data = mux_parent_data; 431 - init.num_parents = meson->data->num_parents; 459 + init.num_parents = MESON_NUM_MUX_PARENTS; 432 460 433 461 channel->mux.reg = meson->base + REG_MISC_AB; 434 462 channel->mux.shift = ··· 497 525 498 526 static int meson_pwm_probe(struct platform_device *pdev) 499 527 { 528 + struct pwm_chip *chip; 500 529 struct meson_pwm *meson; 501 530 int err; 502 531 503 - meson = devm_kzalloc(&pdev->dev, sizeof(*meson), GFP_KERNEL); 504 - if (!meson) 505 - return -ENOMEM; 532 + chip = devm_pwmchip_alloc(&pdev->dev, MESON_NUM_PWMS, sizeof(*meson)); 533 + if (IS_ERR(chip)) 534 + return PTR_ERR(chip); 535 + meson = to_meson_pwm(chip); 506 536 507 537 meson->base = devm_platform_ioremap_resource(pdev, 0); 508 538 if (IS_ERR(meson->base)) 509 539 return PTR_ERR(meson->base); 510 540 511 541 spin_lock_init(&meson->lock); 512 - meson->chip.dev = &pdev->dev; 513 - meson->chip.ops = &meson_pwm_ops; 514 - meson->chip.npwm = MESON_NUM_PWMS; 542 + chip->ops = &meson_pwm_ops; 515 543 516 544 meson->data = of_device_get_match_data(&pdev->dev); 517 545 518 - err = meson_pwm_init_channels(meson); 546 + err = meson_pwm_init_channels(chip); 519 547 if (err < 0) 520 548 return err; 521 549 522 - err = devm_pwmchip_add(&pdev->dev, &meson->chip); 550 + err = devm_pwmchip_add(&pdev->dev, chip); 523 551 if (err < 0) 524 552 return dev_err_probe(&pdev->dev, err, 525 553 "failed to register PWM chip\n");
+8 -9
drivers/pwm/pwm-microchip-core.c
··· 54 54 #define MCHPCOREPWM_TIMEOUT_MS 100u 55 55 56 56 struct mchp_core_pwm_chip { 57 - struct pwm_chip chip; 58 57 struct clk *clk; 59 58 void __iomem *base; 60 59 struct mutex lock; /* protects the shared period */ ··· 64 65 65 66 static inline struct mchp_core_pwm_chip *to_mchp_core_pwm(struct pwm_chip *chip) 66 67 { 67 - return container_of(chip, struct mchp_core_pwm_chip, chip); 68 + return pwmchip_get_drvdata(chip); 68 69 } 69 70 70 71 static void mchp_core_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm, ··· 446 447 447 448 static int mchp_core_pwm_probe(struct platform_device *pdev) 448 449 { 450 + struct pwm_chip *chip; 449 451 struct mchp_core_pwm_chip *mchp_core_pwm; 450 452 struct resource *regs; 451 453 int ret; 452 454 453 - mchp_core_pwm = devm_kzalloc(&pdev->dev, sizeof(*mchp_core_pwm), GFP_KERNEL); 454 - if (!mchp_core_pwm) 455 - return -ENOMEM; 455 + chip = devm_pwmchip_alloc(&pdev->dev, 16, sizeof(*mchp_core_pwm)); 456 + if (IS_ERR(chip)) 457 + return PTR_ERR(chip); 458 + mchp_core_pwm = to_mchp_core_pwm(chip); 456 459 457 460 mchp_core_pwm->base = devm_platform_get_and_ioremap_resource(pdev, 0, &regs); 458 461 if (IS_ERR(mchp_core_pwm->base)) ··· 471 470 472 471 mutex_init(&mchp_core_pwm->lock); 473 472 474 - mchp_core_pwm->chip.dev = &pdev->dev; 475 - mchp_core_pwm->chip.ops = &mchp_core_pwm_ops; 476 - mchp_core_pwm->chip.npwm = 16; 473 + chip->ops = &mchp_core_pwm_ops; 477 474 478 475 mchp_core_pwm->channel_enabled = readb_relaxed(mchp_core_pwm->base + MCHPCOREPWM_EN(0)); 479 476 mchp_core_pwm->channel_enabled |= ··· 484 485 writel_relaxed(1U, mchp_core_pwm->base + MCHPCOREPWM_SYNC_UPD); 485 486 mchp_core_pwm->update_timestamp = ktime_get(); 486 487 487 - ret = devm_pwmchip_add(&pdev->dev, &mchp_core_pwm->chip); 488 + ret = devm_pwmchip_add(&pdev->dev, chip); 488 489 if (ret) 489 490 return dev_err_probe(&pdev->dev, ret, "Failed to add pwmchip\n"); 490 491
+12 -13
drivers/pwm/pwm-mtk-disp.c
··· 42 42 }; 43 43 44 44 struct mtk_disp_pwm { 45 - struct pwm_chip chip; 46 45 const struct mtk_pwm_data *data; 47 46 struct clk *clk_main; 48 47 struct clk *clk_mm; ··· 51 52 52 53 static inline struct mtk_disp_pwm *to_mtk_disp_pwm(struct pwm_chip *chip) 53 54 { 54 - return container_of(chip, struct mtk_disp_pwm, chip); 55 + return pwmchip_get_drvdata(chip); 55 56 } 56 57 57 58 static void mtk_disp_pwm_update_bits(struct mtk_disp_pwm *mdp, u32 offset, ··· 90 91 if (!mdp->enabled) { 91 92 err = clk_prepare_enable(mdp->clk_main); 92 93 if (err < 0) { 93 - dev_err(chip->dev, "Can't enable mdp->clk_main: %pe\n", 94 + dev_err(pwmchip_parent(chip), "Can't enable mdp->clk_main: %pe\n", 94 95 ERR_PTR(err)); 95 96 return err; 96 97 } 97 98 98 99 err = clk_prepare_enable(mdp->clk_mm); 99 100 if (err < 0) { 100 - dev_err(chip->dev, "Can't enable mdp->clk_mm: %pe\n", 101 + dev_err(pwmchip_parent(chip), "Can't enable mdp->clk_mm: %pe\n", 101 102 ERR_PTR(err)); 102 103 clk_disable_unprepare(mdp->clk_main); 103 104 return err; ··· 180 181 181 182 err = clk_prepare_enable(mdp->clk_main); 182 183 if (err < 0) { 183 - dev_err(chip->dev, "Can't enable mdp->clk_main: %pe\n", ERR_PTR(err)); 184 + dev_err(pwmchip_parent(chip), "Can't enable mdp->clk_main: %pe\n", ERR_PTR(err)); 184 185 return err; 185 186 } 186 187 187 188 err = clk_prepare_enable(mdp->clk_mm); 188 189 if (err < 0) { 189 - dev_err(chip->dev, "Can't enable mdp->clk_mm: %pe\n", ERR_PTR(err)); 190 + dev_err(pwmchip_parent(chip), "Can't enable mdp->clk_mm: %pe\n", ERR_PTR(err)); 190 191 clk_disable_unprepare(mdp->clk_main); 191 192 return err; 192 193 } ··· 230 231 231 232 static int mtk_disp_pwm_probe(struct platform_device *pdev) 232 233 { 234 + struct pwm_chip *chip; 233 235 struct mtk_disp_pwm *mdp; 234 236 int ret; 235 237 236 - mdp = devm_kzalloc(&pdev->dev, sizeof(*mdp), GFP_KERNEL); 237 - if (!mdp) 238 - return -ENOMEM; 238 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*mdp)); 239 + if (IS_ERR(chip)) 240 + return PTR_ERR(chip); 241 + mdp = to_mtk_disp_pwm(chip); 239 242 240 243 mdp->data = of_device_get_match_data(&pdev->dev); 241 244 ··· 255 254 return dev_err_probe(&pdev->dev, PTR_ERR(mdp->clk_mm), 256 255 "Failed to get mm clock\n"); 257 256 258 - mdp->chip.dev = &pdev->dev; 259 - mdp->chip.ops = &mtk_disp_pwm_ops; 260 - mdp->chip.npwm = 1; 257 + chip->ops = &mtk_disp_pwm_ops; 261 258 262 - ret = devm_pwmchip_add(&pdev->dev, &mdp->chip); 259 + ret = devm_pwmchip_add(&pdev->dev, chip); 263 260 if (ret < 0) 264 261 return dev_err_probe(&pdev->dev, ret, "pwmchip_add() failed\n"); 265 262
+18 -14
drivers/pwm/pwm-mxs.c
··· 37 37 }; 38 38 39 39 struct mxs_pwm_chip { 40 - struct pwm_chip chip; 41 40 struct clk *clk; 42 41 void __iomem *base; 43 42 }; 44 43 45 - #define to_mxs_pwm_chip(_chip) container_of(_chip, struct mxs_pwm_chip, chip) 44 + static inline struct mxs_pwm_chip *to_mxs_pwm_chip(struct pwm_chip *chip) 45 + { 46 + return pwmchip_get_drvdata(chip); 47 + } 46 48 47 49 static int mxs_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 48 50 const struct pwm_state *state) ··· 122 120 static int mxs_pwm_probe(struct platform_device *pdev) 123 121 { 124 122 struct device_node *np = pdev->dev.of_node; 123 + struct pwm_chip *chip; 125 124 struct mxs_pwm_chip *mxs; 125 + u32 npwm; 126 126 int ret; 127 127 128 - mxs = devm_kzalloc(&pdev->dev, sizeof(*mxs), GFP_KERNEL); 129 - if (!mxs) 130 - return -ENOMEM; 128 + ret = of_property_read_u32(np, "fsl,pwm-number", &npwm); 129 + if (ret < 0) { 130 + dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret); 131 + return ret; 132 + } 133 + 134 + chip = devm_pwmchip_alloc(&pdev->dev, npwm, sizeof(*mxs)); 135 + if (IS_ERR(chip)) 136 + return PTR_ERR(chip); 137 + mxs = to_mxs_pwm_chip(chip); 131 138 132 139 mxs->base = devm_platform_ioremap_resource(pdev, 0); 133 140 if (IS_ERR(mxs->base)) ··· 146 135 if (IS_ERR(mxs->clk)) 147 136 return PTR_ERR(mxs->clk); 148 137 149 - mxs->chip.dev = &pdev->dev; 150 - mxs->chip.ops = &mxs_pwm_ops; 151 - 152 - ret = of_property_read_u32(np, "fsl,pwm-number", &mxs->chip.npwm); 153 - if (ret < 0) { 154 - dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret); 155 - return ret; 156 - } 138 + chip->ops = &mxs_pwm_ops; 157 139 158 140 /* FIXME: Only do this if the PWM isn't already running */ 159 141 ret = stmp_reset_block(mxs->base); 160 142 if (ret) 161 143 return dev_err_probe(&pdev->dev, ret, "failed to reset PWM\n"); 162 144 163 - ret = devm_pwmchip_add(&pdev->dev, &mxs->chip); 145 + ret = devm_pwmchip_add(&pdev->dev, chip); 164 146 if (ret < 0) { 165 147 dev_err(&pdev->dev, "failed to add pwm chip %d\n", ret); 166 148 return ret;
+5 -9
drivers/pwm/pwm-ntxec.c
··· 25 25 26 26 struct ntxec_pwm { 27 27 struct ntxec *ec; 28 - struct pwm_chip chip; 29 28 }; 30 29 31 30 static struct ntxec_pwm *ntxec_pwm_from_chip(struct pwm_chip *chip) 32 31 { 33 - return container_of(chip, struct ntxec_pwm, chip); 32 + return pwmchip_get_drvdata(chip); 34 33 } 35 34 36 35 #define NTXEC_REG_AUTO_OFF_HI 0xa1 ··· 140 141 141 142 device_set_of_node_from_dev(&pdev->dev, pdev->dev.parent); 142 143 143 - priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 144 - if (!priv) 145 - return -ENOMEM; 144 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*priv)); 145 + if (IS_ERR(chip)) 146 + return PTR_ERR(chip); 147 + priv = ntxec_pwm_from_chip(chip); 146 148 147 149 priv->ec = ec; 148 - 149 - chip = &priv->chip; 150 - chip->dev = &pdev->dev; 151 150 chip->ops = &ntxec_pwm_ops; 152 - chip->npwm = 1; 153 151 154 152 return devm_pwmchip_add(&pdev->dev, chip); 155 153 }
+23 -24
drivers/pwm/pwm-omap-dmtimer.c
··· 53 53 /** 54 54 * struct pwm_omap_dmtimer_chip - Structure representing a pwm chip 55 55 * corresponding to omap dmtimer. 56 - * @chip: PWM chip structure representing PWM controller 57 56 * @dm_timer: Pointer to omap dm timer. 58 57 * @pdata: Pointer to omap dm timer ops. 59 58 * @dm_timer_pdev: Pointer to omap dm timer platform device 60 59 */ 61 60 struct pwm_omap_dmtimer_chip { 62 - struct pwm_chip chip; 63 61 /* Mutex to protect pwm apply state */ 64 62 struct omap_dm_timer *dm_timer; 65 63 const struct omap_dm_timer_ops *pdata; ··· 67 69 static inline struct pwm_omap_dmtimer_chip * 68 70 to_pwm_omap_dmtimer_chip(struct pwm_chip *chip) 69 71 { 70 - return container_of(chip, struct pwm_omap_dmtimer_chip, chip); 72 + return pwmchip_get_drvdata(chip); 71 73 } 72 74 73 75 /** ··· 153 155 unsigned long clk_rate; 154 156 struct clk *fclk; 155 157 156 - dev_dbg(chip->dev, "requested duty cycle: %d ns, period: %d ns\n", 158 + dev_dbg(pwmchip_parent(chip), "requested duty cycle: %d ns, period: %d ns\n", 157 159 duty_ns, period_ns); 158 160 159 161 if (duty_ns == pwm_get_duty_cycle(pwm) && ··· 162 164 163 165 fclk = omap->pdata->get_fclk(omap->dm_timer); 164 166 if (!fclk) { 165 - dev_err(chip->dev, "invalid pmtimer fclk\n"); 167 + dev_err(pwmchip_parent(chip), "invalid pmtimer fclk\n"); 166 168 return -EINVAL; 167 169 } 168 170 169 171 clk_rate = clk_get_rate(fclk); 170 172 if (!clk_rate) { 171 - dev_err(chip->dev, "invalid pmtimer fclk rate\n"); 173 + dev_err(pwmchip_parent(chip), "invalid pmtimer fclk rate\n"); 172 174 return -EINVAL; 173 175 } 174 176 175 - dev_dbg(chip->dev, "clk rate: %luHz\n", clk_rate); 177 + dev_dbg(pwmchip_parent(chip), "clk rate: %luHz\n", clk_rate); 176 178 177 179 /* 178 180 * Calculate the appropriate load and match values based on the ··· 194 196 duty_cycles = pwm_omap_dmtimer_get_clock_cycles(clk_rate, duty_ns); 195 197 196 198 if (period_cycles < 2) { 197 - dev_info(chip->dev, 199 + dev_info(pwmchip_parent(chip), 198 200 "period %d ns too short for clock rate %lu Hz\n", 199 201 period_ns, clk_rate); 200 202 return -EINVAL; 201 203 } 202 204 203 205 if (duty_cycles < 1) { 204 - dev_dbg(chip->dev, 206 + dev_dbg(pwmchip_parent(chip), 205 207 "duty cycle %d ns is too short for clock rate %lu Hz\n", 206 208 duty_ns, clk_rate); 207 - dev_dbg(chip->dev, "using minimum of 1 clock cycle\n"); 209 + dev_dbg(pwmchip_parent(chip), "using minimum of 1 clock cycle\n"); 208 210 duty_cycles = 1; 209 211 } else if (duty_cycles >= period_cycles) { 210 - dev_dbg(chip->dev, 212 + dev_dbg(pwmchip_parent(chip), 211 213 "duty cycle %d ns is too long for period %d ns at clock rate %lu Hz\n", 212 214 duty_ns, period_ns, clk_rate); 213 - dev_dbg(chip->dev, "using maximum of 1 clock cycle less than period\n"); 215 + dev_dbg(pwmchip_parent(chip), "using maximum of 1 clock cycle less than period\n"); 214 216 duty_cycles = period_cycles - 1; 215 217 } 216 218 217 - dev_dbg(chip->dev, "effective duty cycle: %lld ns, period: %lld ns\n", 219 + dev_dbg(pwmchip_parent(chip), "effective duty cycle: %lld ns, period: %lld ns\n", 218 220 DIV_ROUND_CLOSEST_ULL((u64)NSEC_PER_SEC * duty_cycles, 219 221 clk_rate), 220 222 DIV_ROUND_CLOSEST_ULL((u64)NSEC_PER_SEC * period_cycles, ··· 226 228 omap->pdata->set_load(omap->dm_timer, load_value); 227 229 omap->pdata->set_match(omap->dm_timer, true, match_value); 228 230 229 - dev_dbg(chip->dev, "load value: %#08x (%d), match value: %#08x (%d)\n", 231 + dev_dbg(pwmchip_parent(chip), "load value: %#08x (%d), match value: %#08x (%d)\n", 230 232 load_value, load_value, match_value, match_value); 231 233 232 234 return 0; ··· 309 311 struct dmtimer_platform_data *timer_pdata; 310 312 const struct omap_dm_timer_ops *pdata; 311 313 struct platform_device *timer_pdev; 314 + struct pwm_chip *chip; 312 315 struct pwm_omap_dmtimer_chip *omap; 313 316 struct omap_dm_timer *dm_timer; 314 317 struct device_node *timer; ··· 367 368 goto err_request_timer; 368 369 } 369 370 370 - omap = devm_kzalloc(&pdev->dev, sizeof(*omap), GFP_KERNEL); 371 - if (!omap) { 372 - ret = -ENOMEM; 371 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*omap)); 372 + if (IS_ERR(chip)) { 373 + ret = PTR_ERR(chip); 373 374 goto err_alloc_omap; 374 375 } 376 + omap = to_pwm_omap_dmtimer_chip(chip); 375 377 376 378 omap->pdata = pdata; 377 379 omap->dm_timer = dm_timer; ··· 392 392 if (!of_property_read_u32(pdev->dev.of_node, "ti,clock-source", &v)) 393 393 omap->pdata->set_source(omap->dm_timer, v); 394 394 395 - omap->chip.dev = &pdev->dev; 396 - omap->chip.ops = &pwm_omap_dmtimer_ops; 397 - omap->chip.npwm = 1; 395 + chip->ops = &pwm_omap_dmtimer_ops; 398 396 399 - ret = pwmchip_add(&omap->chip); 397 + ret = pwmchip_add(chip); 400 398 if (ret < 0) { 401 399 dev_err(&pdev->dev, "failed to register PWM\n"); 402 400 goto err_pwmchip_add; ··· 402 404 403 405 of_node_put(timer); 404 406 405 - platform_set_drvdata(pdev, omap); 407 + platform_set_drvdata(pdev, chip); 406 408 407 409 return 0; 408 410 ··· 430 432 431 433 static void pwm_omap_dmtimer_remove(struct platform_device *pdev) 432 434 { 433 - struct pwm_omap_dmtimer_chip *omap = platform_get_drvdata(pdev); 435 + struct pwm_chip *chip = platform_get_drvdata(pdev); 436 + struct pwm_omap_dmtimer_chip *omap = to_pwm_omap_dmtimer_chip(chip); 434 437 435 - pwmchip_remove(&omap->chip); 438 + pwmchip_remove(chip); 436 439 437 440 if (pm_runtime_active(&omap->dm_timer_pdev->dev)) 438 441 omap->pdata->stop(omap->dm_timer);
+82 -79
drivers/pwm/pwm-pca9685.c
··· 76 76 #define REG_OFF_L(C) ((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_OFF_L : LED_N_OFF_L((C))) 77 77 78 78 struct pca9685 { 79 - struct pwm_chip chip; 80 79 struct regmap *regmap; 81 80 struct mutex lock; 82 81 DECLARE_BITMAP(pwms_enabled, PCA9685_MAXCHAN + 1); ··· 87 88 88 89 static inline struct pca9685 *to_pca(struct pwm_chip *chip) 89 90 { 90 - return container_of(chip, struct pca9685, chip); 91 + return pwmchip_get_drvdata(chip); 91 92 } 92 93 93 94 /* This function is supposed to be called with the lock mutex held */ ··· 106 107 return test_bit(channel, pca->pwms_enabled); 107 108 } 108 109 109 - static int pca9685_read_reg(struct pca9685 *pca, unsigned int reg, unsigned int *val) 110 + static int pca9685_read_reg(struct pwm_chip *chip, unsigned int reg, unsigned int *val) 110 111 { 111 - struct device *dev = pca->chip.dev; 112 + struct pca9685 *pca = to_pca(chip); 113 + struct device *dev = pwmchip_parent(chip); 112 114 int err; 113 115 114 116 err = regmap_read(pca->regmap, reg, val); ··· 119 119 return err; 120 120 } 121 121 122 - static int pca9685_write_reg(struct pca9685 *pca, unsigned int reg, unsigned int val) 122 + static int pca9685_write_reg(struct pwm_chip *chip, unsigned int reg, unsigned int val) 123 123 { 124 - struct device *dev = pca->chip.dev; 124 + struct pca9685 *pca = to_pca(chip); 125 + struct device *dev = pwmchip_parent(chip); 125 126 int err; 126 127 127 128 err = regmap_write(pca->regmap, reg, val); ··· 133 132 } 134 133 135 134 /* Helper function to set the duty cycle ratio to duty/4096 (e.g. duty=2048 -> 50%) */ 136 - static void pca9685_pwm_set_duty(struct pca9685 *pca, int channel, unsigned int duty) 135 + static void pca9685_pwm_set_duty(struct pwm_chip *chip, int channel, unsigned int duty) 137 136 { 138 - struct pwm_device *pwm = &pca->chip.pwms[channel]; 137 + struct pwm_device *pwm = &chip->pwms[channel]; 139 138 unsigned int on, off; 140 139 141 140 if (duty == 0) { 142 141 /* Set the full OFF bit, which has the highest precedence */ 143 - pca9685_write_reg(pca, REG_OFF_H(channel), LED_FULL); 142 + pca9685_write_reg(chip, REG_OFF_H(channel), LED_FULL); 144 143 return; 145 144 } else if (duty >= PCA9685_COUNTER_RANGE) { 146 145 /* Set the full ON bit and clear the full OFF bit */ 147 - pca9685_write_reg(pca, REG_ON_H(channel), LED_FULL); 148 - pca9685_write_reg(pca, REG_OFF_H(channel), 0); 146 + pca9685_write_reg(chip, REG_ON_H(channel), LED_FULL); 147 + pca9685_write_reg(chip, REG_OFF_H(channel), 0); 149 148 return; 150 149 } 151 150 ··· 165 164 off = (on + duty) % PCA9685_COUNTER_RANGE; 166 165 167 166 /* Set ON time (clears full ON bit) */ 168 - pca9685_write_reg(pca, REG_ON_L(channel), on & 0xff); 169 - pca9685_write_reg(pca, REG_ON_H(channel), (on >> 8) & 0xf); 167 + pca9685_write_reg(chip, REG_ON_L(channel), on & 0xff); 168 + pca9685_write_reg(chip, REG_ON_H(channel), (on >> 8) & 0xf); 170 169 /* Set OFF time (clears full OFF bit) */ 171 - pca9685_write_reg(pca, REG_OFF_L(channel), off & 0xff); 172 - pca9685_write_reg(pca, REG_OFF_H(channel), (off >> 8) & 0xf); 170 + pca9685_write_reg(chip, REG_OFF_L(channel), off & 0xff); 171 + pca9685_write_reg(chip, REG_OFF_H(channel), (off >> 8) & 0xf); 173 172 } 174 173 175 - static unsigned int pca9685_pwm_get_duty(struct pca9685 *pca, int channel) 174 + static unsigned int pca9685_pwm_get_duty(struct pwm_chip *chip, int channel) 176 175 { 177 - struct pwm_device *pwm = &pca->chip.pwms[channel]; 176 + struct pwm_device *pwm = &chip->pwms[channel]; 178 177 unsigned int off = 0, on = 0, val = 0; 179 178 180 179 if (WARN_ON(channel >= PCA9685_MAXCHAN)) { ··· 182 181 return 0; 183 182 } 184 183 185 - pca9685_read_reg(pca, LED_N_OFF_H(channel), &off); 184 + pca9685_read_reg(chip, LED_N_OFF_H(channel), &off); 186 185 if (off & LED_FULL) { 187 186 /* Full OFF bit is set */ 188 187 return 0; 189 188 } 190 189 191 - pca9685_read_reg(pca, LED_N_ON_H(channel), &on); 190 + pca9685_read_reg(chip, LED_N_ON_H(channel), &on); 192 191 if (on & LED_FULL) { 193 192 /* Full ON bit is set */ 194 193 return PCA9685_COUNTER_RANGE; 195 194 } 196 195 197 - pca9685_read_reg(pca, LED_N_OFF_L(channel), &val); 196 + pca9685_read_reg(chip, LED_N_OFF_L(channel), &val); 198 197 off = ((off & 0xf) << 8) | (val & 0xff); 199 198 if (!pwm->state.usage_power) 200 199 return off; 201 200 202 201 /* Read ON register to calculate duty cycle of staggered output */ 203 - if (pca9685_read_reg(pca, LED_N_ON_L(channel), &val)) { 202 + if (pca9685_read_reg(chip, LED_N_ON_L(channel), &val)) { 204 203 /* Reset val to 0 in case reading LED_N_ON_L failed */ 205 204 val = 0; 206 205 } ··· 248 247 249 248 static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset) 250 249 { 251 - struct pca9685 *pca = gpiochip_get_data(gpio); 250 + struct pwm_chip *chip = gpiochip_get_data(gpio); 251 + struct pca9685 *pca = to_pca(chip); 252 252 253 253 if (pca9685_pwm_test_and_set_inuse(pca, offset)) 254 254 return -EBUSY; 255 - pm_runtime_get_sync(pca->chip.dev); 255 + pm_runtime_get_sync(pwmchip_parent(chip)); 256 256 return 0; 257 257 } 258 258 259 259 static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset) 260 260 { 261 - struct pca9685 *pca = gpiochip_get_data(gpio); 261 + struct pwm_chip *chip = gpiochip_get_data(gpio); 262 262 263 - return pca9685_pwm_get_duty(pca, offset) != 0; 263 + return pca9685_pwm_get_duty(chip, offset) != 0; 264 264 } 265 265 266 266 static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset, 267 267 int value) 268 268 { 269 - struct pca9685 *pca = gpiochip_get_data(gpio); 269 + struct pwm_chip *chip = gpiochip_get_data(gpio); 270 270 271 - pca9685_pwm_set_duty(pca, offset, value ? PCA9685_COUNTER_RANGE : 0); 271 + pca9685_pwm_set_duty(chip, offset, value ? PCA9685_COUNTER_RANGE : 0); 272 272 } 273 273 274 274 static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset) 275 275 { 276 - struct pca9685 *pca = gpiochip_get_data(gpio); 276 + struct pwm_chip *chip = gpiochip_get_data(gpio); 277 + struct pca9685 *pca = to_pca(chip); 277 278 278 - pca9685_pwm_set_duty(pca, offset, 0); 279 - pm_runtime_put(pca->chip.dev); 279 + pca9685_pwm_set_duty(chip, offset, 0); 280 + pm_runtime_put(pwmchip_parent(chip)); 280 281 pca9685_pwm_clear_inuse(pca, offset); 281 282 } 282 283 ··· 309 306 * expose a GPIO chip here which can exclusively take over the underlying 310 307 * PWM channel. 311 308 */ 312 - static int pca9685_pwm_gpio_probe(struct pca9685 *pca) 309 + static int pca9685_pwm_gpio_probe(struct pwm_chip *chip) 313 310 { 314 - struct device *dev = pca->chip.dev; 311 + struct pca9685 *pca = to_pca(chip); 312 + struct device *dev = pwmchip_parent(chip); 315 313 316 314 pca->gpio.label = dev_name(dev); 317 315 pca->gpio.parent = dev; ··· 327 323 pca->gpio.ngpio = PCA9685_MAXCHAN; 328 324 pca->gpio.can_sleep = true; 329 325 330 - return devm_gpiochip_add_data(dev, &pca->gpio, pca); 326 + return devm_gpiochip_add_data(dev, &pca->gpio, chip); 331 327 } 332 328 #else 333 329 static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, ··· 341 337 { 342 338 } 343 339 344 - static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca) 340 + static inline int pca9685_pwm_gpio_probe(struct pwm_chip *chip) 345 341 { 346 342 return 0; 347 343 } 348 344 #endif 349 345 350 - static void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable) 346 + static void pca9685_set_sleep_mode(struct pwm_chip *chip, bool enable) 351 347 { 352 - struct device *dev = pca->chip.dev; 348 + struct device *dev = pwmchip_parent(chip); 349 + struct pca9685 *pca = to_pca(chip); 353 350 int err = regmap_update_bits(pca->regmap, PCA9685_MODE1, 354 351 MODE1_SLEEP, enable ? MODE1_SLEEP : 0); 355 352 if (err) { ··· 378 373 prescale = DIV_ROUND_CLOSEST_ULL(PCA9685_OSC_CLOCK_MHZ * state->period, 379 374 PCA9685_COUNTER_RANGE * 1000) - 1; 380 375 if (prescale < PCA9685_PRESCALE_MIN || prescale > PCA9685_PRESCALE_MAX) { 381 - dev_err(chip->dev, "pwm not changed: period out of bounds!\n"); 376 + dev_err(pwmchip_parent(chip), "pwm not changed: period out of bounds!\n"); 382 377 return -EINVAL; 383 378 } 384 379 385 380 if (!state->enabled) { 386 - pca9685_pwm_set_duty(pca, pwm->hwpwm, 0); 381 + pca9685_pwm_set_duty(chip, pwm->hwpwm, 0); 387 382 return 0; 388 383 } 389 384 390 - pca9685_read_reg(pca, PCA9685_PRESCALE, &val); 385 + pca9685_read_reg(chip, PCA9685_PRESCALE, &val); 391 386 if (prescale != val) { 392 387 if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) { 393 - dev_err(chip->dev, 388 + dev_err(pwmchip_parent(chip), 394 389 "pwm not changed: periods of enabled pwms must match!\n"); 395 390 return -EBUSY; 396 391 } ··· 402 397 * state is guaranteed active here. 403 398 */ 404 399 /* Put chip into sleep mode */ 405 - pca9685_set_sleep_mode(pca, true); 400 + pca9685_set_sleep_mode(chip, true); 406 401 407 402 /* Change the chip-wide output frequency */ 408 - pca9685_write_reg(pca, PCA9685_PRESCALE, prescale); 403 + pca9685_write_reg(chip, PCA9685_PRESCALE, prescale); 409 404 410 405 /* Wake the chip up */ 411 - pca9685_set_sleep_mode(pca, false); 406 + pca9685_set_sleep_mode(chip, false); 412 407 } 413 408 414 409 duty = PCA9685_COUNTER_RANGE * state->duty_cycle; 415 410 duty = DIV_ROUND_UP_ULL(duty, state->period); 416 - pca9685_pwm_set_duty(pca, pwm->hwpwm, duty); 411 + pca9685_pwm_set_duty(chip, pwm->hwpwm, duty); 417 412 return 0; 418 413 } 419 414 ··· 439 434 static int pca9685_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, 440 435 struct pwm_state *state) 441 436 { 442 - struct pca9685 *pca = to_pca(chip); 443 437 unsigned long long duty; 444 438 unsigned int val = 0; 445 439 446 440 /* Calculate (chip-wide) period from prescale value */ 447 - pca9685_read_reg(pca, PCA9685_PRESCALE, &val); 441 + pca9685_read_reg(chip, PCA9685_PRESCALE, &val); 448 442 /* 449 443 * PCA9685_OSC_CLOCK_MHZ is 25, i.e. an integer divider of 1000. 450 444 * The following calculation is therefore only a multiplication ··· 466 462 } 467 463 468 464 state->enabled = true; 469 - duty = pca9685_pwm_get_duty(pca, pwm->hwpwm); 465 + duty = pca9685_pwm_get_duty(chip, pwm->hwpwm); 470 466 state->duty_cycle = DIV_ROUND_DOWN_ULL(duty * state->period, PCA9685_COUNTER_RANGE); 471 467 472 468 return 0; ··· 486 482 mutex_unlock(&pca->lock); 487 483 } 488 484 489 - pm_runtime_get_sync(chip->dev); 485 + pm_runtime_get_sync(pwmchip_parent(chip)); 490 486 491 487 return 0; 492 488 } ··· 496 492 struct pca9685 *pca = to_pca(chip); 497 493 498 494 mutex_lock(&pca->lock); 499 - pca9685_pwm_set_duty(pca, pwm->hwpwm, 0); 495 + pca9685_pwm_set_duty(chip, pwm->hwpwm, 0); 500 496 clear_bit(pwm->hwpwm, pca->pwms_enabled); 501 497 mutex_unlock(&pca->lock); 502 498 503 - pm_runtime_put(chip->dev); 499 + pm_runtime_put(pwmchip_parent(chip)); 504 500 pca9685_pwm_clear_inuse(pca, pwm->hwpwm); 505 501 } 506 502 ··· 520 516 521 517 static int pca9685_pwm_probe(struct i2c_client *client) 522 518 { 519 + struct pwm_chip *chip; 523 520 struct pca9685 *pca; 524 521 unsigned int reg; 525 522 int ret; 526 523 527 - pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL); 528 - if (!pca) 529 - return -ENOMEM; 524 + /* Add an extra channel for ALL_LED */ 525 + chip = devm_pwmchip_alloc(&client->dev, PCA9685_MAXCHAN + 1, sizeof(*pca)); 526 + if (IS_ERR(chip)) 527 + return PTR_ERR(chip); 528 + pca = to_pca(chip); 530 529 531 530 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config); 532 531 if (IS_ERR(pca->regmap)) { ··· 539 532 return ret; 540 533 } 541 534 542 - i2c_set_clientdata(client, pca); 535 + i2c_set_clientdata(client, chip); 543 536 544 537 mutex_init(&pca->lock); 545 538 546 - ret = pca9685_read_reg(pca, PCA9685_MODE2, &reg); 539 + ret = pca9685_read_reg(chip, PCA9685_MODE2, &reg); 547 540 if (ret) 548 541 return ret; 549 542 ··· 557 550 else 558 551 reg |= MODE2_OUTDRV; 559 552 560 - ret = pca9685_write_reg(pca, PCA9685_MODE2, reg); 553 + ret = pca9685_write_reg(chip, PCA9685_MODE2, reg); 561 554 if (ret) 562 555 return ret; 563 556 564 557 /* Disable all LED ALLCALL and SUBx addresses to avoid bus collisions */ 565 - pca9685_read_reg(pca, PCA9685_MODE1, &reg); 558 + pca9685_read_reg(chip, PCA9685_MODE1, &reg); 566 559 reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3); 567 - pca9685_write_reg(pca, PCA9685_MODE1, reg); 560 + pca9685_write_reg(chip, PCA9685_MODE1, reg); 568 561 569 562 /* Reset OFF/ON registers to POR default */ 570 - pca9685_write_reg(pca, PCA9685_ALL_LED_OFF_L, 0); 571 - pca9685_write_reg(pca, PCA9685_ALL_LED_OFF_H, LED_FULL); 572 - pca9685_write_reg(pca, PCA9685_ALL_LED_ON_L, 0); 573 - pca9685_write_reg(pca, PCA9685_ALL_LED_ON_H, LED_FULL); 563 + pca9685_write_reg(chip, PCA9685_ALL_LED_OFF_L, 0); 564 + pca9685_write_reg(chip, PCA9685_ALL_LED_OFF_H, LED_FULL); 565 + pca9685_write_reg(chip, PCA9685_ALL_LED_ON_L, 0); 566 + pca9685_write_reg(chip, PCA9685_ALL_LED_ON_H, LED_FULL); 574 567 575 - pca->chip.ops = &pca9685_pwm_ops; 576 - /* Add an extra channel for ALL_LED */ 577 - pca->chip.npwm = PCA9685_MAXCHAN + 1; 568 + chip->ops = &pca9685_pwm_ops; 578 569 579 - pca->chip.dev = &client->dev; 580 - 581 - ret = pwmchip_add(&pca->chip); 570 + ret = pwmchip_add(chip); 582 571 if (ret < 0) 583 572 return ret; 584 573 585 - ret = pca9685_pwm_gpio_probe(pca); 574 + ret = pca9685_pwm_gpio_probe(chip); 586 575 if (ret < 0) { 587 - pwmchip_remove(&pca->chip); 576 + pwmchip_remove(chip); 588 577 return ret; 589 578 } 590 579 ··· 591 588 * Although the chip comes out of power-up in the sleep state, 592 589 * we force it to sleep in case it was woken up before 593 590 */ 594 - pca9685_set_sleep_mode(pca, true); 591 + pca9685_set_sleep_mode(chip, true); 595 592 pm_runtime_set_suspended(&client->dev); 596 593 } else { 597 594 /* Wake the chip up if runtime PM is disabled */ 598 - pca9685_set_sleep_mode(pca, false); 595 + pca9685_set_sleep_mode(chip, false); 599 596 } 600 597 601 598 return 0; ··· 603 600 604 601 static void pca9685_pwm_remove(struct i2c_client *client) 605 602 { 606 - struct pca9685 *pca = i2c_get_clientdata(client); 603 + struct pwm_chip *chip = i2c_get_clientdata(client); 607 604 608 - pwmchip_remove(&pca->chip); 605 + pwmchip_remove(chip); 609 606 610 607 if (!pm_runtime_enabled(&client->dev)) { 611 608 /* Put chip in sleep state if runtime PM is disabled */ 612 - pca9685_set_sleep_mode(pca, true); 609 + pca9685_set_sleep_mode(chip, true); 613 610 } 614 611 615 612 pm_runtime_disable(&client->dev); ··· 618 615 static int __maybe_unused pca9685_pwm_runtime_suspend(struct device *dev) 619 616 { 620 617 struct i2c_client *client = to_i2c_client(dev); 621 - struct pca9685 *pca = i2c_get_clientdata(client); 618 + struct pwm_chip *chip = i2c_get_clientdata(client); 622 619 623 - pca9685_set_sleep_mode(pca, true); 620 + pca9685_set_sleep_mode(chip, true); 624 621 return 0; 625 622 } 626 623 627 624 static int __maybe_unused pca9685_pwm_runtime_resume(struct device *dev) 628 625 { 629 626 struct i2c_client *client = to_i2c_client(dev); 630 - struct pca9685 *pca = i2c_get_clientdata(client); 627 + struct pwm_chip *chip = i2c_get_clientdata(client); 631 628 632 - pca9685_set_sleep_mode(pca, false); 629 + pca9685_set_sleep_mode(chip, false); 633 630 return 0; 634 631 } 635 632
+12 -13
drivers/pwm/pwm-pxa.c
··· 49 49 #define PWMDCR_FD (1 << 10) 50 50 51 51 struct pxa_pwm_chip { 52 - struct pwm_chip chip; 53 52 struct device *dev; 54 53 55 54 struct clk *clk; ··· 57 58 58 59 static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip) 59 60 { 60 - return container_of(chip, struct pxa_pwm_chip, chip); 61 + return pwmchip_get_drvdata(chip); 61 62 } 62 63 63 64 /* ··· 158 159 static int pwm_probe(struct platform_device *pdev) 159 160 { 160 161 const struct platform_device_id *id = platform_get_device_id(pdev); 162 + struct pwm_chip *chip; 161 163 struct pxa_pwm_chip *pc; 162 164 int ret = 0; 163 165 ··· 168 168 if (id == NULL) 169 169 return -EINVAL; 170 170 171 - pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); 172 - if (pc == NULL) 173 - return -ENOMEM; 171 + chip = devm_pwmchip_alloc(&pdev->dev, 172 + (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1, 173 + sizeof(*pc)); 174 + if (IS_ERR(chip)) 175 + return PTR_ERR(chip); 176 + pc = to_pxa_pwm_chip(chip); 174 177 175 178 pc->clk = devm_clk_get(&pdev->dev, NULL); 176 179 if (IS_ERR(pc->clk)) 177 180 return PTR_ERR(pc->clk); 178 181 179 - pc->chip.dev = &pdev->dev; 180 - pc->chip.ops = &pxa_pwm_ops; 181 - pc->chip.npwm = (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1; 182 + chip->ops = &pxa_pwm_ops; 182 183 183 - if (IS_ENABLED(CONFIG_OF)) { 184 - pc->chip.of_xlate = of_pwm_single_xlate; 185 - pc->chip.of_pwm_n_cells = 1; 186 - } 184 + if (IS_ENABLED(CONFIG_OF)) 185 + chip->of_xlate = of_pwm_single_xlate; 187 186 188 187 pc->mmio_base = devm_platform_ioremap_resource(pdev, 0); 189 188 if (IS_ERR(pc->mmio_base)) 190 189 return PTR_ERR(pc->mmio_base); 191 190 192 - ret = devm_pwmchip_add(&pdev->dev, &pc->chip); 191 + ret = devm_pwmchip_add(&pdev->dev, chip); 193 192 if (ret < 0) { 194 193 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); 195 194 return ret;
+10 -10
drivers/pwm/pwm-raspberrypi-poe.c
··· 27 27 28 28 struct raspberrypi_pwm { 29 29 struct rpi_firmware *firmware; 30 - struct pwm_chip chip; 31 30 unsigned int duty_cycle; 32 31 }; 33 32 ··· 39 40 static inline 40 41 struct raspberrypi_pwm *raspberrypi_pwm_from_chip(struct pwm_chip *chip) 41 42 { 42 - return container_of(chip, struct raspberrypi_pwm, chip); 43 + return pwmchip_get_drvdata(chip); 43 44 } 44 45 45 46 static int raspberrypi_pwm_set_property(struct rpi_firmware *firmware, ··· 121 122 ret = raspberrypi_pwm_set_property(rpipwm->firmware, RPI_PWM_CUR_DUTY_REG, 122 123 duty_cycle); 123 124 if (ret) { 124 - dev_err(chip->dev, "Failed to set duty cycle: %pe\n", 125 + dev_err(pwmchip_parent(chip), "Failed to set duty cycle: %pe\n", 125 126 ERR_PTR(ret)); 126 127 return ret; 127 128 } ··· 141 142 struct device_node *firmware_node; 142 143 struct device *dev = &pdev->dev; 143 144 struct rpi_firmware *firmware; 145 + struct pwm_chip *chip; 144 146 struct raspberrypi_pwm *rpipwm; 145 147 int ret; 146 148 ··· 157 157 return dev_err_probe(dev, -EPROBE_DEFER, 158 158 "Failed to get firmware handle\n"); 159 159 160 - rpipwm = devm_kzalloc(&pdev->dev, sizeof(*rpipwm), GFP_KERNEL); 161 - if (!rpipwm) 162 - return -ENOMEM; 160 + chip = devm_pwmchip_alloc(&pdev->dev, RASPBERRYPI_FIRMWARE_PWM_NUM, 161 + sizeof(*rpipwm)); 162 + if (IS_ERR(chip)) 163 + return PTR_ERR(chip); 164 + rpipwm = raspberrypi_pwm_from_chip(chip); 163 165 164 166 rpipwm->firmware = firmware; 165 - rpipwm->chip.dev = dev; 166 - rpipwm->chip.ops = &raspberrypi_pwm_ops; 167 - rpipwm->chip.npwm = RASPBERRYPI_FIRMWARE_PWM_NUM; 167 + chip->ops = &raspberrypi_pwm_ops; 168 168 169 169 ret = raspberrypi_pwm_get_property(rpipwm->firmware, RPI_PWM_CUR_DUTY_REG, 170 170 &rpipwm->duty_cycle); ··· 173 173 return ret; 174 174 } 175 175 176 - return devm_pwmchip_add(dev, &rpipwm->chip); 176 + return devm_pwmchip_add(dev, chip); 177 177 } 178 178 179 179 static const struct of_device_id raspberrypi_pwm_of_match[] = {
+13 -14
drivers/pwm/pwm-rcar.c
··· 38 38 #define RCAR_PWMCNT_PH0_SHIFT 0 39 39 40 40 struct rcar_pwm_chip { 41 - struct pwm_chip chip; 42 41 void __iomem *base; 43 42 struct clk *clk; 44 43 }; 45 44 46 45 static inline struct rcar_pwm_chip *to_rcar_pwm_chip(struct pwm_chip *chip) 47 46 { 48 - return container_of(chip, struct rcar_pwm_chip, chip); 47 + return pwmchip_get_drvdata(chip); 49 48 } 50 49 51 50 static void rcar_pwm_write(struct rcar_pwm_chip *rp, u32 data, ··· 131 132 132 133 static int rcar_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) 133 134 { 134 - return pm_runtime_get_sync(chip->dev); 135 + return pm_runtime_get_sync(pwmchip_parent(chip)); 135 136 } 136 137 137 138 static void rcar_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) 138 139 { 139 - pm_runtime_put(chip->dev); 140 + pm_runtime_put(pwmchip_parent(chip)); 140 141 } 141 142 142 143 static int rcar_pwm_enable(struct rcar_pwm_chip *rp) ··· 201 202 202 203 static int rcar_pwm_probe(struct platform_device *pdev) 203 204 { 205 + struct pwm_chip *chip; 204 206 struct rcar_pwm_chip *rcar_pwm; 205 207 int ret; 206 208 207 - rcar_pwm = devm_kzalloc(&pdev->dev, sizeof(*rcar_pwm), GFP_KERNEL); 208 - if (rcar_pwm == NULL) 209 - return -ENOMEM; 209 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*rcar_pwm)); 210 + if (IS_ERR(chip)) 211 + return PTR_ERR(chip); 212 + rcar_pwm = to_rcar_pwm_chip(chip); 210 213 211 214 rcar_pwm->base = devm_platform_ioremap_resource(pdev, 0); 212 215 if (IS_ERR(rcar_pwm->base)) ··· 220 219 return PTR_ERR(rcar_pwm->clk); 221 220 } 222 221 223 - platform_set_drvdata(pdev, rcar_pwm); 222 + chip->ops = &rcar_pwm_ops; 224 223 225 - rcar_pwm->chip.dev = &pdev->dev; 226 - rcar_pwm->chip.ops = &rcar_pwm_ops; 227 - rcar_pwm->chip.npwm = 1; 224 + platform_set_drvdata(pdev, chip); 228 225 229 226 pm_runtime_enable(&pdev->dev); 230 227 231 - ret = pwmchip_add(&rcar_pwm->chip); 228 + ret = pwmchip_add(chip); 232 229 if (ret < 0) { 233 230 dev_err(&pdev->dev, "failed to register PWM chip: %d\n", ret); 234 231 pm_runtime_disable(&pdev->dev); ··· 238 239 239 240 static void rcar_pwm_remove(struct platform_device *pdev) 240 241 { 241 - struct rcar_pwm_chip *rcar_pwm = platform_get_drvdata(pdev); 242 + struct pwm_chip *chip = platform_get_drvdata(pdev); 242 243 243 - pwmchip_remove(&rcar_pwm->chip); 244 + pwmchip_remove(chip); 244 245 245 246 pm_runtime_disable(&pdev->dev); 246 247 }
+11 -9
drivers/pwm/pwm-renesas-tpu.c
··· 79 79 80 80 struct tpu_device { 81 81 struct platform_device *pdev; 82 - struct pwm_chip chip; 83 82 spinlock_t lock; 84 83 85 84 void __iomem *base; ··· 86 87 struct tpu_pwm_device tpd[TPU_CHANNEL_MAX]; 87 88 }; 88 89 89 - #define to_tpu_device(c) container_of(c, struct tpu_device, chip) 90 + static inline struct tpu_device *to_tpu_device(struct pwm_chip *chip) 91 + { 92 + return pwmchip_get_drvdata(chip); 93 + } 90 94 91 95 static void tpu_pwm_write(struct tpu_pwm_device *tpd, int reg_nr, u16 value) 92 96 { ··· 440 438 441 439 static int tpu_probe(struct platform_device *pdev) 442 440 { 441 + struct pwm_chip *chip; 443 442 struct tpu_device *tpu; 444 443 int ret; 445 444 446 - tpu = devm_kzalloc(&pdev->dev, sizeof(*tpu), GFP_KERNEL); 447 - if (tpu == NULL) 448 - return -ENOMEM; 445 + chip = devm_pwmchip_alloc(&pdev->dev, TPU_CHANNEL_MAX, sizeof(*tpu)); 446 + if (IS_ERR(chip)) 447 + return PTR_ERR(chip); 448 + tpu = to_tpu_device(chip); 449 449 450 450 spin_lock_init(&tpu->lock); 451 451 tpu->pdev = pdev; ··· 464 460 /* Initialize and register the device. */ 465 461 platform_set_drvdata(pdev, tpu); 466 462 467 - tpu->chip.dev = &pdev->dev; 468 - tpu->chip.ops = &tpu_pwm_ops; 469 - tpu->chip.npwm = TPU_CHANNEL_MAX; 463 + chip->ops = &tpu_pwm_ops; 470 464 471 465 ret = devm_pm_runtime_enable(&pdev->dev); 472 466 if (ret < 0) 473 467 return dev_err_probe(&pdev->dev, ret, "Failed to enable runtime PM\n"); 474 468 475 - ret = devm_pwmchip_add(&pdev->dev, &tpu->chip); 469 + ret = devm_pwmchip_add(&pdev->dev, chip); 476 470 if (ret < 0) 477 471 return dev_err_probe(&pdev->dev, ret, "Failed to register PWM chip\n"); 478 472
+12 -12
drivers/pwm/pwm-rockchip.c
··· 30 30 #define PWM_LP_DISABLE (0 << 8) 31 31 32 32 struct rockchip_pwm_chip { 33 - struct pwm_chip chip; 34 33 struct clk *clk; 35 34 struct clk *pclk; 36 35 const struct rockchip_pwm_data *data; ··· 53 54 54 55 static inline struct rockchip_pwm_chip *to_rockchip_pwm_chip(struct pwm_chip *chip) 55 56 { 56 - return container_of(chip, struct rockchip_pwm_chip, chip); 57 + return pwmchip_get_drvdata(chip); 57 58 } 58 59 59 60 static int rockchip_pwm_get_state(struct pwm_chip *chip, ··· 295 296 296 297 static int rockchip_pwm_probe(struct platform_device *pdev) 297 298 { 299 + struct pwm_chip *chip; 298 300 struct rockchip_pwm_chip *pc; 299 301 u32 enable_conf, ctrl; 300 302 bool enabled; 301 303 int ret, count; 302 304 303 - pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); 304 - if (!pc) 305 - return -ENOMEM; 305 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*pc)); 306 + if (IS_ERR(chip)) 307 + return PTR_ERR(chip); 308 + pc = to_rockchip_pwm_chip(chip); 306 309 307 310 pc->base = devm_platform_ioremap_resource(pdev, 0); 308 311 if (IS_ERR(pc->base)) ··· 338 337 goto err_clk; 339 338 } 340 339 341 - platform_set_drvdata(pdev, pc); 340 + platform_set_drvdata(pdev, chip); 342 341 343 342 pc->data = device_get_match_data(&pdev->dev); 344 - pc->chip.dev = &pdev->dev; 345 - pc->chip.ops = &rockchip_pwm_ops; 346 - pc->chip.npwm = 1; 343 + chip->ops = &rockchip_pwm_ops; 347 344 348 345 enable_conf = pc->data->enable_conf; 349 346 ctrl = readl_relaxed(pc->base + pc->data->regs.ctrl); 350 347 enabled = (ctrl & enable_conf) == enable_conf; 351 348 352 - ret = pwmchip_add(&pc->chip); 349 + ret = pwmchip_add(chip); 353 350 if (ret < 0) { 354 351 dev_err_probe(&pdev->dev, ret, "pwmchip_add() failed\n"); 355 352 goto err_pclk; ··· 371 372 372 373 static void rockchip_pwm_remove(struct platform_device *pdev) 373 374 { 374 - struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev); 375 + struct pwm_chip *chip = platform_get_drvdata(pdev); 376 + struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip); 375 377 376 - pwmchip_remove(&pc->chip); 378 + pwmchip_remove(chip); 377 379 378 380 clk_unprepare(pc->pclk); 379 381 clk_unprepare(pc->clk);
+31 -29
drivers/pwm/pwm-rz-mtu3.c
··· 61 61 /** 62 62 * struct rz_mtu3_pwm_chip - MTU3 pwm private data 63 63 * 64 - * @chip: MTU3 pwm chip data 65 64 * @clk: MTU3 module clock 66 65 * @lock: Lock to prevent concurrent access for usage count 67 66 * @rate: MTU3 clock rate ··· 71 72 */ 72 73 73 74 struct rz_mtu3_pwm_chip { 74 - struct pwm_chip chip; 75 75 struct clk *clk; 76 76 struct mutex lock; 77 77 unsigned long rate; ··· 90 92 91 93 static inline struct rz_mtu3_pwm_chip *to_rz_mtu3_pwm_chip(struct pwm_chip *chip) 92 94 { 93 - return container_of(chip, struct rz_mtu3_pwm_chip, chip); 95 + return pwmchip_get_drvdata(chip); 94 96 } 95 97 96 98 static void rz_mtu3_pwm_read_tgr_registers(struct rz_mtu3_pwm_channel *priv, ··· 209 211 mutex_unlock(&rz_mtu3_pwm->lock); 210 212 } 211 213 212 - static int rz_mtu3_pwm_enable(struct rz_mtu3_pwm_chip *rz_mtu3_pwm, 213 - struct pwm_device *pwm) 214 + static int rz_mtu3_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) 214 215 { 216 + struct rz_mtu3_pwm_chip *rz_mtu3_pwm = to_rz_mtu3_pwm_chip(chip); 215 217 struct rz_mtu3_pwm_channel *priv; 216 218 u32 ch; 217 219 u8 val; 218 220 int rc; 219 221 220 - rc = pm_runtime_resume_and_get(rz_mtu3_pwm->chip.dev); 222 + rc = pm_runtime_resume_and_get(pwmchip_parent(chip)); 221 223 if (rc) 222 224 return rc; 223 225 ··· 241 243 return 0; 242 244 } 243 245 244 - static void rz_mtu3_pwm_disable(struct rz_mtu3_pwm_chip *rz_mtu3_pwm, 245 - struct pwm_device *pwm) 246 + static void rz_mtu3_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) 246 247 { 248 + struct rz_mtu3_pwm_chip *rz_mtu3_pwm = to_rz_mtu3_pwm_chip(chip); 247 249 struct rz_mtu3_pwm_channel *priv; 248 250 u32 ch; 249 251 ··· 263 265 264 266 mutex_unlock(&rz_mtu3_pwm->lock); 265 267 266 - pm_runtime_put_sync(rz_mtu3_pwm->chip.dev); 268 + pm_runtime_put_sync(pwmchip_parent(chip)); 267 269 } 268 270 269 271 static int rz_mtu3_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, ··· 272 274 struct rz_mtu3_pwm_chip *rz_mtu3_pwm = to_rz_mtu3_pwm_chip(chip); 273 275 int rc; 274 276 275 - rc = pm_runtime_resume_and_get(chip->dev); 277 + rc = pm_runtime_resume_and_get(pwmchip_parent(chip)); 276 278 if (rc) 277 279 return rc; 278 280 ··· 305 307 } 306 308 307 309 state->polarity = PWM_POLARITY_NORMAL; 308 - pm_runtime_put(chip->dev); 310 + pm_runtime_put(pwmchip_parent(chip)); 309 311 310 312 return 0; 311 313 } ··· 360 362 if (!pwm->state.enabled) { 361 363 int rc; 362 364 363 - rc = pm_runtime_resume_and_get(chip->dev); 365 + rc = pm_runtime_resume_and_get(pwmchip_parent(chip)); 364 366 if (rc) 365 367 return rc; 366 368 } ··· 397 399 398 400 /* If the PWM is not enabled, turn the clock off again to save power. */ 399 401 if (!pwm->state.enabled) 400 - pm_runtime_put(chip->dev); 402 + pm_runtime_put(pwmchip_parent(chip)); 401 403 402 404 return 0; 403 405 } ··· 414 416 415 417 if (!state->enabled) { 416 418 if (enabled) 417 - rz_mtu3_pwm_disable(rz_mtu3_pwm, pwm); 419 + rz_mtu3_pwm_disable(chip, pwm); 418 420 419 421 return 0; 420 422 } ··· 426 428 return ret; 427 429 428 430 if (!enabled) 429 - ret = rz_mtu3_pwm_enable(rz_mtu3_pwm, pwm); 431 + ret = rz_mtu3_pwm_enable(chip, pwm); 430 432 431 433 return ret; 432 434 } ··· 440 442 441 443 static int rz_mtu3_pwm_pm_runtime_suspend(struct device *dev) 442 444 { 443 - struct rz_mtu3_pwm_chip *rz_mtu3_pwm = dev_get_drvdata(dev); 445 + struct pwm_chip *chip = dev_get_drvdata(dev); 446 + struct rz_mtu3_pwm_chip *rz_mtu3_pwm = to_rz_mtu3_pwm_chip(chip); 444 447 445 448 clk_disable_unprepare(rz_mtu3_pwm->clk); 446 449 ··· 450 451 451 452 static int rz_mtu3_pwm_pm_runtime_resume(struct device *dev) 452 453 { 453 - struct rz_mtu3_pwm_chip *rz_mtu3_pwm = dev_get_drvdata(dev); 454 + struct pwm_chip *chip = dev_get_drvdata(dev); 455 + struct rz_mtu3_pwm_chip *rz_mtu3_pwm = to_rz_mtu3_pwm_chip(chip); 454 456 455 457 return clk_prepare_enable(rz_mtu3_pwm->clk); 456 458 } ··· 462 462 463 463 static void rz_mtu3_pwm_pm_disable(void *data) 464 464 { 465 - struct rz_mtu3_pwm_chip *rz_mtu3_pwm = data; 465 + struct pwm_chip *chip = data; 466 + struct rz_mtu3_pwm_chip *rz_mtu3_pwm = to_rz_mtu3_pwm_chip(chip); 466 467 467 468 clk_rate_exclusive_put(rz_mtu3_pwm->clk); 468 - pm_runtime_disable(rz_mtu3_pwm->chip.dev); 469 - pm_runtime_set_suspended(rz_mtu3_pwm->chip.dev); 469 + pm_runtime_disable(pwmchip_parent(chip)); 470 + pm_runtime_set_suspended(pwmchip_parent(chip)); 470 471 } 471 472 472 473 static int rz_mtu3_pwm_probe(struct platform_device *pdev) 473 474 { 474 475 struct rz_mtu3 *parent_ddata = dev_get_drvdata(pdev->dev.parent); 475 476 struct rz_mtu3_pwm_chip *rz_mtu3_pwm; 477 + struct pwm_chip *chip; 476 478 struct device *dev = &pdev->dev; 477 479 unsigned int i, j = 0; 478 480 int ret; 479 481 480 - rz_mtu3_pwm = devm_kzalloc(&pdev->dev, sizeof(*rz_mtu3_pwm), GFP_KERNEL); 481 - if (!rz_mtu3_pwm) 482 - return -ENOMEM; 482 + chip = devm_pwmchip_alloc(&pdev->dev, RZ_MTU3_MAX_PWM_CHANNELS, 483 + sizeof(*rz_mtu3_pwm)); 484 + if (IS_ERR(chip)) 485 + return PTR_ERR(chip); 486 + rz_mtu3_pwm = to_rz_mtu3_pwm_chip(chip); 483 487 484 488 rz_mtu3_pwm->clk = parent_ddata->clk; 485 489 ··· 498 494 } 499 495 500 496 mutex_init(&rz_mtu3_pwm->lock); 501 - platform_set_drvdata(pdev, rz_mtu3_pwm); 497 + platform_set_drvdata(pdev, chip); 502 498 ret = clk_prepare_enable(rz_mtu3_pwm->clk); 503 499 if (ret) 504 500 return dev_err_probe(dev, ret, "Clock enable failed\n"); ··· 518 514 519 515 pm_runtime_set_active(&pdev->dev); 520 516 pm_runtime_enable(&pdev->dev); 521 - rz_mtu3_pwm->chip.dev = &pdev->dev; 522 517 ret = devm_add_action_or_reset(&pdev->dev, rz_mtu3_pwm_pm_disable, 523 - rz_mtu3_pwm); 518 + chip); 524 519 if (ret < 0) 525 520 return ret; 526 521 527 - rz_mtu3_pwm->chip.ops = &rz_mtu3_pwm_ops; 528 - rz_mtu3_pwm->chip.npwm = RZ_MTU3_MAX_PWM_CHANNELS; 529 - ret = devm_pwmchip_add(&pdev->dev, &rz_mtu3_pwm->chip); 522 + chip->ops = &rz_mtu3_pwm_ops; 523 + ret = devm_pwmchip_add(&pdev->dev, chip); 530 524 if (ret) 531 525 return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n"); 532 526
+36 -58
drivers/pwm/pwm-samsung.c
··· 69 69 70 70 /** 71 71 * struct samsung_pwm_chip - private data of PWM chip 72 - * @chip: generic PWM chip 73 72 * @variant: local copy of hardware variant data 74 73 * @inverter_mask: inverter status for all channels - one bit per channel 75 74 * @disabled_mask: disabled status for all channels - one bit per channel ··· 79 80 * @channel: per channel driver data 80 81 */ 81 82 struct samsung_pwm_chip { 82 - struct pwm_chip chip; 83 83 struct samsung_pwm_variant variant; 84 84 u8 inverter_mask; 85 85 u8 disabled_mask; ··· 108 110 static inline 109 111 struct samsung_pwm_chip *to_samsung_pwm_chip(struct pwm_chip *chip) 110 112 { 111 - return container_of(chip, struct samsung_pwm_chip, chip); 113 + return pwmchip_get_drvdata(chip); 112 114 } 113 115 114 116 static inline unsigned int to_tcon_channel(unsigned int channel) ··· 179 181 return rate / (reg + 1); 180 182 } 181 183 182 - static unsigned long pwm_samsung_calc_tin(struct samsung_pwm_chip *our_chip, 184 + static unsigned long pwm_samsung_calc_tin(struct pwm_chip *chip, 183 185 unsigned int chan, unsigned long freq) 184 186 { 187 + struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip); 185 188 struct samsung_pwm_variant *variant = &our_chip->variant; 186 189 unsigned long rate; 187 190 struct clk *clk; ··· 196 197 return rate; 197 198 } 198 199 199 - dev_warn(our_chip->chip.dev, 200 + dev_warn(pwmchip_parent(chip), 200 201 "tclk of PWM %d is inoperational, using tdiv\n", chan); 201 202 } 202 203 203 204 rate = pwm_samsung_get_tin_rate(our_chip, chan); 204 - dev_dbg(our_chip->chip.dev, "tin parent at %lu\n", rate); 205 + dev_dbg(pwmchip_parent(chip), "tin parent at %lu\n", rate); 205 206 206 207 /* 207 208 * Compare minimum PWM frequency that can be achieved with possible ··· 231 232 struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip); 232 233 233 234 if (!(our_chip->variant.output_mask & BIT(pwm->hwpwm))) { 234 - dev_warn(chip->dev, 235 + dev_warn(pwmchip_parent(chip), 235 236 "tried to request PWM channel %d without output\n", 236 237 pwm->hwpwm); 237 238 return -EINVAL; ··· 325 326 326 327 period = NSEC_PER_SEC / period_ns; 327 328 328 - dev_dbg(our_chip->chip.dev, "duty_ns=%d, period_ns=%d (%u)\n", 329 + dev_dbg(pwmchip_parent(chip), "duty_ns=%d, period_ns=%d (%u)\n", 329 330 duty_ns, period_ns, period); 330 331 331 - tin_rate = pwm_samsung_calc_tin(our_chip, pwm->hwpwm, period); 332 + tin_rate = pwm_samsung_calc_tin(chip, pwm->hwpwm, period); 332 333 333 - dev_dbg(our_chip->chip.dev, "tin_rate=%lu\n", tin_rate); 334 + dev_dbg(pwmchip_parent(chip), "tin_rate=%lu\n", tin_rate); 334 335 335 336 tin_ns = NSEC_PER_SEC / tin_rate; 336 337 tcnt = period_ns / tin_ns; ··· 354 355 /* -1UL will give 100% duty. */ 355 356 --tcmp; 356 357 357 - dev_dbg(our_chip->chip.dev, 358 - "tin_ns=%u, tcmp=%u/%u\n", tin_ns, tcmp, tcnt); 358 + dev_dbg(pwmchip_parent(chip), "tin_ns=%u, tcmp=%u/%u\n", tin_ns, tcmp, tcnt); 359 359 360 360 /* Update PWM registers. */ 361 361 writel(tcnt, our_chip->base + REG_TCNTB(pwm->hwpwm)); ··· 366 368 * shortly afer this update (before it autoreloaded the new values). 367 369 */ 368 370 if (oldtcmp == (u32) -1) { 369 - dev_dbg(our_chip->chip.dev, "Forcing manual update"); 371 + dev_dbg(pwmchip_parent(chip), "Forcing manual update"); 370 372 pwm_samsung_manual_update(our_chip, pwm); 371 373 } 372 374 ··· 505 507 }; 506 508 MODULE_DEVICE_TABLE(of, samsung_pwm_matches); 507 509 508 - static int pwm_samsung_parse_dt(struct samsung_pwm_chip *our_chip) 510 + static int pwm_samsung_parse_dt(struct pwm_chip *chip) 509 511 { 510 - struct device_node *np = our_chip->chip.dev->of_node; 512 + struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip); 513 + struct device_node *np = pwmchip_parent(chip)->of_node; 511 514 const struct of_device_id *match; 512 515 struct property *prop; 513 516 const __be32 *cur; ··· 522 523 523 524 of_property_for_each_u32(np, "samsung,pwm-outputs", prop, cur, val) { 524 525 if (val >= SAMSUNG_PWM_NUM) { 525 - dev_err(our_chip->chip.dev, 526 + dev_err(pwmchip_parent(chip), 526 527 "%s: invalid channel index in samsung,pwm-outputs property\n", 527 528 __func__); 528 529 continue; ··· 533 534 return 0; 534 535 } 535 536 #else 536 - static int pwm_samsung_parse_dt(struct samsung_pwm_chip *our_chip) 537 + static int pwm_samsung_parse_dt(struct pwm_chip *chip) 537 538 { 538 539 return -ENODEV; 539 540 } ··· 543 544 { 544 545 struct device *dev = &pdev->dev; 545 546 struct samsung_pwm_chip *our_chip; 547 + struct pwm_chip *chip; 546 548 unsigned int chan; 547 549 int ret; 548 550 549 - our_chip = devm_kzalloc(&pdev->dev, sizeof(*our_chip), GFP_KERNEL); 550 - if (our_chip == NULL) 551 - return -ENOMEM; 551 + chip = devm_pwmchip_alloc(&pdev->dev, SAMSUNG_PWM_NUM, sizeof(*our_chip)); 552 + if (IS_ERR(chip)) 553 + return PTR_ERR(chip); 554 + our_chip = to_samsung_pwm_chip(chip); 552 555 553 - our_chip->chip.dev = &pdev->dev; 554 - our_chip->chip.ops = &pwm_samsung_ops; 555 - our_chip->chip.npwm = SAMSUNG_PWM_NUM; 556 + chip->ops = &pwm_samsung_ops; 556 557 our_chip->inverter_mask = BIT(SAMSUNG_PWM_NUM) - 1; 557 558 558 559 if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) { 559 - ret = pwm_samsung_parse_dt(our_chip); 560 + ret = pwm_samsung_parse_dt(chip); 560 561 if (ret) 561 562 return ret; 562 563 } else { 563 - if (!pdev->dev.platform_data) { 564 - dev_err(&pdev->dev, "no platform data specified\n"); 565 - return -EINVAL; 566 - } 564 + if (!pdev->dev.platform_data) 565 + return dev_err_probe(&pdev->dev, -EINVAL, 566 + "no platform data specified\n"); 567 567 568 568 memcpy(&our_chip->variant, pdev->dev.platform_data, 569 569 sizeof(our_chip->variant)); ··· 572 574 if (IS_ERR(our_chip->base)) 573 575 return PTR_ERR(our_chip->base); 574 576 575 - our_chip->base_clk = devm_clk_get(&pdev->dev, "timers"); 576 - if (IS_ERR(our_chip->base_clk)) { 577 - dev_err(dev, "failed to get timer base clk\n"); 578 - return PTR_ERR(our_chip->base_clk); 579 - } 580 - 581 - ret = clk_prepare_enable(our_chip->base_clk); 582 - if (ret < 0) { 583 - dev_err(dev, "failed to enable base clock\n"); 584 - return ret; 585 - } 577 + our_chip->base_clk = devm_clk_get_enabled(&pdev->dev, "timers"); 578 + if (IS_ERR(our_chip->base_clk)) 579 + return dev_err_probe(dev, PTR_ERR(our_chip->base_clk), 580 + "failed to get timer base clk\n"); 586 581 587 582 for (chan = 0; chan < SAMSUNG_PWM_NUM; ++chan) 588 583 if (our_chip->variant.output_mask & BIT(chan)) ··· 585 594 our_chip->tclk0 = devm_clk_get(&pdev->dev, "pwm-tclk0"); 586 595 our_chip->tclk1 = devm_clk_get(&pdev->dev, "pwm-tclk1"); 587 596 588 - platform_set_drvdata(pdev, our_chip); 597 + platform_set_drvdata(pdev, chip); 589 598 590 - ret = pwmchip_add(&our_chip->chip); 591 - if (ret < 0) { 592 - dev_err(dev, "failed to register PWM chip\n"); 593 - clk_disable_unprepare(our_chip->base_clk); 594 - return ret; 595 - } 599 + ret = devm_pwmchip_add(&pdev->dev, chip); 600 + if (ret < 0) 601 + return dev_err_probe(dev, ret, "failed to register PWM chip\n"); 596 602 597 603 dev_dbg(dev, "base_clk at %lu, tclk0 at %lu, tclk1 at %lu\n", 598 604 clk_get_rate(our_chip->base_clk), ··· 599 611 return 0; 600 612 } 601 613 602 - static void pwm_samsung_remove(struct platform_device *pdev) 603 - { 604 - struct samsung_pwm_chip *our_chip = platform_get_drvdata(pdev); 605 - 606 - pwmchip_remove(&our_chip->chip); 607 - 608 - clk_disable_unprepare(our_chip->base_clk); 609 - } 610 - 611 614 static int pwm_samsung_resume(struct device *dev) 612 615 { 613 - struct samsung_pwm_chip *our_chip = dev_get_drvdata(dev); 614 - struct pwm_chip *chip = &our_chip->chip; 616 + struct pwm_chip *chip = dev_get_drvdata(dev); 617 + struct samsung_pwm_chip *our_chip = to_samsung_pwm_chip(chip); 615 618 unsigned int i; 616 619 617 620 for (i = 0; i < SAMSUNG_PWM_NUM; i++) { ··· 641 662 .of_match_table = of_match_ptr(samsung_pwm_matches), 642 663 }, 643 664 .probe = pwm_samsung_probe, 644 - .remove_new = pwm_samsung_remove, 645 665 }; 646 666 module_platform_driver(pwm_samsung_driver); 647 667
+15 -15
drivers/pwm/pwm-sifive.c
··· 41 41 #define PWM_SIFIVE_DEFAULT_PERIOD 10000000 42 42 43 43 struct pwm_sifive_ddata { 44 - struct pwm_chip chip; 44 + struct device *parent; 45 45 struct mutex lock; /* lock to protect user_count and approx_period */ 46 46 struct notifier_block notifier; 47 47 struct clk *clk; ··· 54 54 static inline 55 55 struct pwm_sifive_ddata *pwm_sifive_chip_to_ddata(struct pwm_chip *chip) 56 56 { 57 - return container_of(chip, struct pwm_sifive_ddata, chip); 57 + return pwmchip_get_drvdata(chip); 58 58 } 59 59 60 60 static int pwm_sifive_request(struct pwm_chip *chip, struct pwm_device *pwm) ··· 102 102 /* As scale <= 15 the shift operation cannot overflow. */ 103 103 num = (unsigned long long)NSEC_PER_SEC << (PWM_SIFIVE_CMPWIDTH + scale); 104 104 ddata->real_period = div64_ul(num, rate); 105 - dev_dbg(ddata->chip.dev, 105 + dev_dbg(ddata->parent, 106 106 "New real_period = %u ns\n", ddata->real_period); 107 107 } 108 108 ··· 185 185 if (!enabled) { 186 186 ret = clk_enable(ddata->clk); 187 187 if (ret) { 188 - dev_err(ddata->chip.dev, "Enable clk failed\n"); 188 + dev_err(pwmchip_parent(chip), "Enable clk failed\n"); 189 189 return ret; 190 190 } 191 191 } ··· 230 230 u32 val; 231 231 unsigned int enabled_pwms = 0, enabled_clks = 1; 232 232 233 - ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL); 234 - if (!ddata) 235 - return -ENOMEM; 233 + chip = devm_pwmchip_alloc(dev, 4, sizeof(*ddata)); 234 + if (IS_ERR(chip)) 235 + return PTR_ERR(chip); 236 236 237 + ddata = pwm_sifive_chip_to_ddata(chip); 238 + ddata->parent = dev; 237 239 mutex_init(&ddata->lock); 238 - chip = &ddata->chip; 239 - chip->dev = dev; 240 240 chip->ops = &pwm_sifive_ops; 241 - chip->npwm = 4; 242 241 243 242 ddata->regs = devm_platform_ioremap_resource(pdev, 0); 244 243 if (IS_ERR(ddata->regs)) ··· 295 296 goto unregister_clk; 296 297 } 297 298 298 - platform_set_drvdata(pdev, ddata); 299 + platform_set_drvdata(pdev, chip); 299 300 dev_dbg(dev, "SiFive PWM chip registered %d PWMs\n", chip->npwm); 300 301 301 302 return 0; ··· 313 314 314 315 static void pwm_sifive_remove(struct platform_device *dev) 315 316 { 316 - struct pwm_sifive_ddata *ddata = platform_get_drvdata(dev); 317 + struct pwm_chip *chip = platform_get_drvdata(dev); 318 + struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip); 317 319 struct pwm_device *pwm; 318 320 int ch; 319 321 320 - pwmchip_remove(&ddata->chip); 322 + pwmchip_remove(chip); 321 323 clk_notifier_unregister(ddata->clk, &ddata->notifier); 322 324 323 - for (ch = 0; ch < ddata->chip.npwm; ch++) { 324 - pwm = &ddata->chip.pwms[ch]; 325 + for (ch = 0; ch < chip->npwm; ch++) { 326 + pwm = &chip->pwms[ch]; 325 327 if (pwm->state.enabled) 326 328 clk_disable(ddata->clk); 327 329 }
+5 -8
drivers/pwm/pwm-sl28cpld.c
··· 81 81 regmap_write((priv)->regmap, (priv)->offset + (reg), (val)) 82 82 83 83 struct sl28cpld_pwm { 84 - struct pwm_chip chip; 85 84 struct regmap *regmap; 86 85 u32 offset; 87 86 }; 88 87 89 88 static inline struct sl28cpld_pwm *sl28cpld_pwm_from_chip(struct pwm_chip *chip) 90 89 { 91 - return container_of(chip, struct sl28cpld_pwm, chip); 90 + return pwmchip_get_drvdata(chip); 92 91 } 93 92 94 93 static int sl28cpld_pwm_get_state(struct pwm_chip *chip, ··· 212 213 return -ENODEV; 213 214 } 214 215 215 - priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 216 - if (!priv) 217 - return -ENOMEM; 216 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*priv)); 217 + if (IS_ERR(chip)) 218 + return PTR_ERR(chip); 219 + priv = sl28cpld_pwm_from_chip(chip); 218 220 219 221 priv->regmap = dev_get_regmap(pdev->dev.parent, NULL); 220 222 if (!priv->regmap) { ··· 231 231 } 232 232 233 233 /* Initialize the pwm_chip structure */ 234 - chip = &priv->chip; 235 - chip->dev = &pdev->dev; 236 234 chip->ops = &sl28cpld_pwm_ops; 237 - chip->npwm = 1; 238 235 239 236 ret = devm_pwmchip_add(&pdev->dev, chip); 240 237 if (ret) {
+8 -10
drivers/pwm/pwm-spear.c
··· 48 48 * 49 49 * @mmio_base: base address of pwm chip 50 50 * @clk: pointer to clk structure of pwm chip 51 - * @chip: linux pwm chip representation 52 51 */ 53 52 struct spear_pwm_chip { 54 53 void __iomem *mmio_base; 55 54 struct clk *clk; 56 - struct pwm_chip chip; 57 55 }; 58 56 59 57 static inline struct spear_pwm_chip *to_spear_pwm_chip(struct pwm_chip *chip) 60 58 { 61 - return container_of(chip, struct spear_pwm_chip, chip); 59 + return pwmchip_get_drvdata(chip); 62 60 } 63 61 64 62 static inline u32 spear_pwm_readl(struct spear_pwm_chip *chip, unsigned int num, ··· 192 194 static int spear_pwm_probe(struct platform_device *pdev) 193 195 { 194 196 struct device_node *np = pdev->dev.of_node; 197 + struct pwm_chip *chip; 195 198 struct spear_pwm_chip *pc; 196 199 int ret; 197 200 u32 val; 198 201 199 - pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); 200 - if (!pc) 201 - return -ENOMEM; 202 + chip = devm_pwmchip_alloc(&pdev->dev, NUM_PWM, sizeof(*pc)); 203 + if (IS_ERR(chip)) 204 + return PTR_ERR(chip); 205 + pc = to_spear_pwm_chip(chip); 202 206 203 207 pc->mmio_base = devm_platform_ioremap_resource(pdev, 0); 204 208 if (IS_ERR(pc->mmio_base)) ··· 211 211 return dev_err_probe(&pdev->dev, PTR_ERR(pc->clk), 212 212 "Failed to get clock\n"); 213 213 214 - pc->chip.dev = &pdev->dev; 215 - pc->chip.ops = &spear_pwm_ops; 216 - pc->chip.npwm = NUM_PWM; 214 + chip->ops = &spear_pwm_ops; 217 215 218 216 if (of_device_is_compatible(np, "st,spear1340-pwm")) { 219 217 ret = clk_enable(pc->clk); ··· 230 232 clk_disable(pc->clk); 231 233 } 232 234 233 - ret = devm_pwmchip_add(&pdev->dev, &pc->chip); 235 + ret = devm_pwmchip_add(&pdev->dev, chip); 234 236 if (ret < 0) 235 237 return dev_err_probe(&pdev->dev, ret, "pwmchip_add() failed\n"); 236 238
+27 -31
drivers/pwm/pwm-sprd.c
··· 34 34 35 35 struct sprd_pwm_chip { 36 36 void __iomem *base; 37 - struct device *dev; 38 - struct pwm_chip chip; 39 - int num_pwms; 40 37 struct sprd_pwm_chn chn[SPRD_PWM_CHN_NUM]; 41 38 }; 42 39 43 40 static inline struct sprd_pwm_chip* sprd_pwm_from_chip(struct pwm_chip *chip) 44 41 { 45 - return container_of(chip, struct sprd_pwm_chip, chip); 42 + return pwmchip_get_drvdata(chip); 46 43 } 47 44 48 45 /* ··· 83 86 */ 84 87 ret = clk_bulk_prepare_enable(SPRD_PWM_CHN_CLKS_NUM, chn->clks); 85 88 if (ret) { 86 - dev_err(spc->dev, "failed to enable pwm%u clocks\n", 89 + dev_err(pwmchip_parent(chip), "failed to enable pwm%u clocks\n", 87 90 pwm->hwpwm); 88 91 return ret; 89 92 } ··· 180 183 ret = clk_bulk_prepare_enable(SPRD_PWM_CHN_CLKS_NUM, 181 184 chn->clks); 182 185 if (ret) { 183 - dev_err(spc->dev, 186 + dev_err(pwmchip_parent(chip), 184 187 "failed to enable pwm%u clocks\n", 185 188 pwm->hwpwm); 186 189 return ret; ··· 212 215 .get_state = sprd_pwm_get_state, 213 216 }; 214 217 215 - static int sprd_pwm_clk_init(struct sprd_pwm_chip *spc) 218 + static int sprd_pwm_clk_init(struct device *dev, 219 + struct sprd_pwm_chn chn[SPRD_PWM_CHN_NUM]) 216 220 { 217 221 struct clk *clk_pwm; 218 222 int ret, i; 219 223 220 224 for (i = 0; i < SPRD_PWM_CHN_NUM; i++) { 221 - struct sprd_pwm_chn *chn = &spc->chn[i]; 222 225 int j; 223 226 224 227 for (j = 0; j < SPRD_PWM_CHN_CLKS_NUM; ++j) 225 - chn->clks[j].id = 228 + chn[i].clks[j].id = 226 229 sprd_pwm_clks[i * SPRD_PWM_CHN_CLKS_NUM + j]; 227 230 228 - ret = devm_clk_bulk_get(spc->dev, SPRD_PWM_CHN_CLKS_NUM, 229 - chn->clks); 231 + ret = devm_clk_bulk_get(dev, SPRD_PWM_CHN_CLKS_NUM, 232 + chn[i].clks); 230 233 if (ret) { 231 234 if (ret == -ENOENT) 232 235 break; 233 236 234 - return dev_err_probe(spc->dev, ret, 237 + return dev_err_probe(dev, ret, 235 238 "failed to get channel clocks\n"); 236 239 } 237 240 238 - clk_pwm = chn->clks[SPRD_PWM_CHN_OUTPUT_CLK].clk; 239 - chn->clk_rate = clk_get_rate(clk_pwm); 241 + clk_pwm = chn[i].clks[SPRD_PWM_CHN_OUTPUT_CLK].clk; 242 + chn[i].clk_rate = clk_get_rate(clk_pwm); 240 243 } 241 244 242 245 if (!i) 243 - return dev_err_probe(spc->dev, -ENODEV, "no available PWM channels\n"); 246 + return dev_err_probe(dev, -ENODEV, "no available PWM channels\n"); 244 247 245 - spc->num_pwms = i; 246 - 247 - return 0; 248 + return i; 248 249 } 249 250 250 251 static int sprd_pwm_probe(struct platform_device *pdev) 251 252 { 253 + struct pwm_chip *chip; 252 254 struct sprd_pwm_chip *spc; 253 - int ret; 255 + struct sprd_pwm_chn chn[SPRD_PWM_CHN_NUM]; 256 + int ret, npwm; 254 257 255 - spc = devm_kzalloc(&pdev->dev, sizeof(*spc), GFP_KERNEL); 256 - if (!spc) 257 - return -ENOMEM; 258 + npwm = sprd_pwm_clk_init(&pdev->dev, chn); 259 + if (npwm < 0) 260 + return npwm; 261 + 262 + chip = devm_pwmchip_alloc(&pdev->dev, npwm, sizeof(*spc)); 263 + if (IS_ERR(chip)) 264 + return PTR_ERR(chip); 265 + spc = sprd_pwm_from_chip(chip); 258 266 259 267 spc->base = devm_platform_ioremap_resource(pdev, 0); 260 268 if (IS_ERR(spc->base)) 261 269 return PTR_ERR(spc->base); 262 270 263 - spc->dev = &pdev->dev; 271 + memcpy(spc->chn, chn, sizeof(chn)); 264 272 265 - ret = sprd_pwm_clk_init(spc); 266 - if (ret) 267 - return ret; 273 + chip->ops = &sprd_pwm_ops; 268 274 269 - spc->chip.dev = &pdev->dev; 270 - spc->chip.ops = &sprd_pwm_ops; 271 - spc->chip.npwm = spc->num_pwms; 272 - 273 - ret = devm_pwmchip_add(&pdev->dev, &spc->chip); 275 + ret = devm_pwmchip_add(&pdev->dev, chip); 274 276 if (ret) 275 277 dev_err(&pdev->dev, "failed to add PWM chip\n"); 276 278
+40 -30
drivers/pwm/pwm-sti.c
··· 94 94 struct regmap_field *pwm_cpt_en; 95 95 struct regmap_field *pwm_cpt_int_en; 96 96 struct regmap_field *pwm_cpt_int_stat; 97 - struct pwm_chip chip; 98 97 struct pwm_device *cur; 99 98 unsigned long configured; 100 99 unsigned int en_count; ··· 113 114 114 115 static inline struct sti_pwm_chip *to_sti_pwmchip(struct pwm_chip *chip) 115 116 { 116 - return container_of(chip, struct sti_pwm_chip, chip); 117 + return pwmchip_get_drvdata(chip); 117 118 } 118 119 119 120 /* ··· 394 395 static int sti_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 395 396 const struct pwm_state *state) 396 397 { 398 + struct sti_pwm_chip *pc = to_sti_pwmchip(chip); 399 + struct sti_pwm_compat_data *cdata = pc->cdata; 400 + struct device *dev = pc->dev; 397 401 int err; 402 + 403 + if (pwm->hwpwm >= cdata->pwm_num_devs) { 404 + dev_err(dev, "device %u is not valid for pwm mode\n", 405 + pwm->hwpwm); 406 + return -EINVAL; 407 + } 398 408 399 409 if (state->polarity != PWM_POLARITY_NORMAL) 400 410 return -EINVAL; ··· 506 498 { 507 499 struct device *dev = pc->dev; 508 500 const struct reg_field *reg_fields; 509 - struct device_node *np = dev->of_node; 510 501 struct sti_pwm_compat_data *cdata = pc->cdata; 511 - u32 num_devs; 512 - int ret; 513 - 514 - ret = of_property_read_u32(np, "st,pwm-num-chan", &num_devs); 515 - if (!ret) 516 - cdata->pwm_num_devs = num_devs; 517 - 518 - ret = of_property_read_u32(np, "st,capture-num-chan", &num_devs); 519 - if (!ret) 520 - cdata->cpt_num_devs = num_devs; 521 - 522 - if (!cdata->pwm_num_devs && !cdata->cpt_num_devs) { 523 - dev_err(dev, "No channels configured\n"); 524 - return -EINVAL; 525 - } 526 502 527 503 reg_fields = cdata->reg_fields; 528 504 ··· 552 560 static int sti_pwm_probe(struct platform_device *pdev) 553 561 { 554 562 struct device *dev = &pdev->dev; 563 + struct device_node *np = dev->of_node; 564 + u32 num_devs; 565 + unsigned int pwm_num_devs = 0; 566 + unsigned int cpt_num_devs = 0; 555 567 struct sti_pwm_compat_data *cdata; 568 + struct pwm_chip *chip; 556 569 struct sti_pwm_chip *pc; 557 570 unsigned int i; 558 571 int irq, ret; 559 572 560 - pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL); 561 - if (!pc) 562 - return -ENOMEM; 573 + ret = of_property_read_u32(np, "st,pwm-num-chan", &num_devs); 574 + if (!ret) 575 + pwm_num_devs = num_devs; 576 + 577 + ret = of_property_read_u32(np, "st,capture-num-chan", &num_devs); 578 + if (!ret) 579 + cpt_num_devs = num_devs; 580 + 581 + if (!pwm_num_devs && !cpt_num_devs) { 582 + dev_err(dev, "No channels configured\n"); 583 + return -EINVAL; 584 + } 585 + 586 + chip = devm_pwmchip_alloc(dev, max(pwm_num_devs, cpt_num_devs), sizeof(*pc)); 587 + if (IS_ERR(chip)) 588 + return PTR_ERR(chip); 589 + pc = to_sti_pwmchip(chip); 563 590 564 591 cdata = devm_kzalloc(dev, sizeof(*cdata), GFP_KERNEL); 565 592 if (!cdata) ··· 611 600 cdata->reg_fields = sti_pwm_regfields; 612 601 cdata->max_prescale = 0xff; 613 602 cdata->max_pwm_cnt = 255; 614 - cdata->pwm_num_devs = 0; 615 - cdata->cpt_num_devs = 0; 603 + cdata->pwm_num_devs = pwm_num_devs; 604 + cdata->cpt_num_devs = cpt_num_devs; 616 605 617 606 pc->cdata = cdata; 618 607 pc->dev = dev; ··· 655 644 return -ENOMEM; 656 645 } 657 646 658 - pc->chip.dev = dev; 659 - pc->chip.ops = &sti_pwm_ops; 660 - pc->chip.npwm = pc->cdata->pwm_num_devs; 647 + chip->ops = &sti_pwm_ops; 661 648 662 649 for (i = 0; i < cdata->cpt_num_devs; i++) { 663 650 struct sti_cpt_ddata *ddata = &cdata->ddata[i]; ··· 664 655 mutex_init(&ddata->lock); 665 656 } 666 657 667 - ret = pwmchip_add(&pc->chip); 658 + ret = pwmchip_add(chip); 668 659 if (ret < 0) { 669 660 clk_unprepare(pc->pwm_clk); 670 661 clk_unprepare(pc->cpt_clk); 671 662 return ret; 672 663 } 673 664 674 - platform_set_drvdata(pdev, pc); 665 + platform_set_drvdata(pdev, chip); 675 666 676 667 return 0; 677 668 } 678 669 679 670 static void sti_pwm_remove(struct platform_device *pdev) 680 671 { 681 - struct sti_pwm_chip *pc = platform_get_drvdata(pdev); 672 + struct pwm_chip *chip = platform_get_drvdata(pdev); 673 + struct sti_pwm_chip *pc = to_sti_pwmchip(chip); 682 674 683 - pwmchip_remove(&pc->chip); 675 + pwmchip_remove(chip); 684 676 685 677 clk_unprepare(pc->pwm_clk); 686 678 clk_unprepare(pc->cpt_clk);
+15 -16
drivers/pwm/pwm-stm32-lp.c
··· 18 18 #include <linux/pwm.h> 19 19 20 20 struct stm32_pwm_lp { 21 - struct pwm_chip chip; 22 21 struct clk *clk; 23 22 struct regmap *regmap; 24 23 }; 25 24 26 25 static inline struct stm32_pwm_lp *to_stm32_pwm_lp(struct pwm_chip *chip) 27 26 { 28 - return container_of(chip, struct stm32_pwm_lp, chip); 27 + return pwmchip_get_drvdata(chip); 29 28 } 30 29 31 30 /* STM32 Low-Power Timer is preceded by a configurable power-of-2 prescaler */ ··· 60 61 do_div(div, NSEC_PER_SEC); 61 62 if (!div) { 62 63 /* Clock is too slow to achieve requested period. */ 63 - dev_dbg(priv->chip.dev, "Can't reach %llu ns\n", state->period); 64 + dev_dbg(pwmchip_parent(chip), "Can't reach %llu ns\n", state->period); 64 65 return -EINVAL; 65 66 } 66 67 ··· 68 69 while (div > STM32_LPTIM_MAX_ARR) { 69 70 presc++; 70 71 if ((1 << presc) > STM32_LPTIM_MAX_PRESCALER) { 71 - dev_err(priv->chip.dev, "max prescaler exceeded\n"); 72 + dev_err(pwmchip_parent(chip), "max prescaler exceeded\n"); 72 73 return -EINVAL; 73 74 } 74 75 div = prd >> presc; ··· 129 130 (val & STM32_LPTIM_CMPOK_ARROK) == STM32_LPTIM_CMPOK_ARROK, 130 131 100, 1000); 131 132 if (ret) { 132 - dev_err(priv->chip.dev, "ARR/CMP registers write issue\n"); 133 + dev_err(pwmchip_parent(chip), "ARR/CMP registers write issue\n"); 133 134 goto err; 134 135 } 135 136 ret = regmap_write(priv->regmap, STM32_LPTIM_ICR, ··· 196 197 { 197 198 struct stm32_lptimer *ddata = dev_get_drvdata(pdev->dev.parent); 198 199 struct stm32_pwm_lp *priv; 200 + struct pwm_chip *chip; 199 201 int ret; 200 202 201 - priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 202 - if (!priv) 203 - return -ENOMEM; 203 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*priv)); 204 + if (IS_ERR(chip)) 205 + return PTR_ERR(chip); 206 + priv = to_stm32_pwm_lp(chip); 204 207 205 208 priv->regmap = ddata->regmap; 206 209 priv->clk = ddata->clk; 207 - priv->chip.dev = &pdev->dev; 208 - priv->chip.ops = &stm32_pwm_lp_ops; 209 - priv->chip.npwm = 1; 210 + chip->ops = &stm32_pwm_lp_ops; 210 211 211 - ret = devm_pwmchip_add(&pdev->dev, &priv->chip); 212 + ret = devm_pwmchip_add(&pdev->dev, chip); 212 213 if (ret < 0) 213 214 return ret; 214 215 215 - platform_set_drvdata(pdev, priv); 216 + platform_set_drvdata(pdev, chip); 216 217 217 218 return 0; 218 219 } 219 220 220 221 static int stm32_pwm_lp_suspend(struct device *dev) 221 222 { 222 - struct stm32_pwm_lp *priv = dev_get_drvdata(dev); 223 + struct pwm_chip *chip = dev_get_drvdata(dev); 223 224 struct pwm_state state; 224 225 225 - pwm_get_state(&priv->chip.pwms[0], &state); 226 + pwm_get_state(&chip->pwms[0], &state); 226 227 if (state.enabled) { 227 228 dev_err(dev, "The consumer didn't stop us (%s)\n", 228 - priv->chip.pwms[0].label); 229 + chip->pwms[0].label); 229 230 return -EBUSY; 230 231 } 231 232
+30 -26
drivers/pwm/pwm-stm32.c
··· 27 27 }; 28 28 29 29 struct stm32_pwm { 30 - struct pwm_chip chip; 31 30 struct mutex lock; /* protect pwm config/enable */ 32 31 struct clk *clk; 33 32 struct regmap *regmap; ··· 39 40 40 41 static inline struct stm32_pwm *to_stm32_pwm_dev(struct pwm_chip *chip) 41 42 { 42 - return container_of(chip, struct stm32_pwm, chip); 43 + return pwmchip_get_drvdata(chip); 43 44 } 44 45 45 46 static u32 active_channels(struct stm32_pwm *dev) ··· 89 90 * - Period = t2 - t0 90 91 * - Duty cycle = t1 - t0 91 92 */ 92 - static int stm32_pwm_raw_capture(struct stm32_pwm *priv, struct pwm_device *pwm, 93 + static int stm32_pwm_raw_capture(struct pwm_chip *chip, struct pwm_device *pwm, 93 94 unsigned long tmo_ms, u32 *raw_prd, 94 95 u32 *raw_dty) 95 96 { 96 - struct device *parent = priv->chip.dev->parent; 97 + struct stm32_pwm *priv = to_stm32_pwm_dev(chip); 98 + struct device *parent = pwmchip_parent(chip)->parent; 97 99 enum stm32_timers_dmas dma_id; 98 100 u32 ccen, ccr; 99 101 int ret; ··· 170 170 171 171 ret = clk_enable(priv->clk); 172 172 if (ret) { 173 - dev_err(priv->chip.dev, "failed to enable counter clock\n"); 173 + dev_err(pwmchip_parent(chip), "failed to enable counter clock\n"); 174 174 goto unlock; 175 175 } 176 176 ··· 208 208 TIM_CCER_CC12P : TIM_CCER_CC34P, pwm->hwpwm < 2 ? 209 209 TIM_CCER_CC2P : TIM_CCER_CC4P); 210 210 211 - ret = stm32_pwm_raw_capture(priv, pwm, tmo_ms, &raw_prd, &raw_dty); 211 + ret = stm32_pwm_raw_capture(chip, pwm, tmo_ms, &raw_prd, &raw_dty); 212 212 if (ret) 213 213 goto stop; 214 214 ··· 229 229 /* 2nd measure with new scale */ 230 230 psc /= scale; 231 231 regmap_write(priv->regmap, TIM_PSC, psc); 232 - ret = stm32_pwm_raw_capture(priv, pwm, tmo_ms, &raw_prd, 232 + ret = stm32_pwm_raw_capture(chip, pwm, tmo_ms, &raw_prd, 233 233 &raw_dty); 234 234 if (ret) 235 235 goto stop; ··· 257 257 FIELD_PREP(TIM_CCMR_IC1PSC, icpsc) | 258 258 FIELD_PREP(TIM_CCMR_IC2PSC, icpsc)); 259 259 260 - ret = stm32_pwm_raw_capture(priv, pwm, tmo_ms, &raw_prd, &raw_dty); 260 + ret = stm32_pwm_raw_capture(chip, pwm, tmo_ms, &raw_prd, &raw_dty); 261 261 if (ret) 262 262 goto stop; 263 263 ··· 605 605 priv->have_complementary_output = (ccer != 0); 606 606 } 607 607 608 - static unsigned int stm32_pwm_detect_channels(struct stm32_pwm *priv, 608 + static unsigned int stm32_pwm_detect_channels(struct regmap *regmap, 609 609 unsigned int *num_enabled) 610 610 { 611 611 u32 ccer, ccer_backup; ··· 614 614 * If channels enable bits don't exist writing 1 will have no 615 615 * effect so we can detect and count them. 616 616 */ 617 - regmap_read(priv->regmap, TIM_CCER, &ccer_backup); 618 - regmap_set_bits(priv->regmap, TIM_CCER, TIM_CCER_CCXE); 619 - regmap_read(priv->regmap, TIM_CCER, &ccer); 620 - regmap_write(priv->regmap, TIM_CCER, ccer_backup); 617 + regmap_read(regmap, TIM_CCER, &ccer_backup); 618 + regmap_set_bits(regmap, TIM_CCER, TIM_CCER_CCXE); 619 + regmap_read(regmap, TIM_CCER, &ccer); 620 + regmap_write(regmap, TIM_CCER, ccer_backup); 621 621 622 622 *num_enabled = hweight32(ccer_backup & TIM_CCER_CCXE); 623 623 ··· 629 629 struct device *dev = &pdev->dev; 630 630 struct device_node *np = dev->of_node; 631 631 struct stm32_timers *ddata = dev_get_drvdata(pdev->dev.parent); 632 + struct pwm_chip *chip; 632 633 struct stm32_pwm *priv; 633 - unsigned int num_enabled; 634 + unsigned int npwm, num_enabled; 634 635 unsigned int i; 635 636 int ret; 636 637 637 - priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 638 - if (!priv) 639 - return -ENOMEM; 638 + npwm = stm32_pwm_detect_channels(ddata->regmap, &num_enabled); 639 + 640 + chip = devm_pwmchip_alloc(dev, npwm, sizeof(*priv)); 641 + if (IS_ERR(chip)) 642 + return PTR_ERR(chip); 643 + priv = to_stm32_pwm_dev(chip); 640 644 641 645 mutex_init(&priv->lock); 642 646 priv->regmap = ddata->regmap; ··· 656 652 657 653 stm32_pwm_detect_complementary(priv); 658 654 659 - priv->chip.dev = dev; 660 - priv->chip.ops = &stm32pwm_ops; 661 - priv->chip.npwm = stm32_pwm_detect_channels(priv, &num_enabled); 655 + chip->ops = &stm32pwm_ops; 662 656 663 657 /* Initialize clock refcount to number of enabled PWM channels. */ 664 658 for (i = 0; i < num_enabled; i++) 665 659 clk_enable(priv->clk); 666 660 667 - ret = devm_pwmchip_add(dev, &priv->chip); 661 + ret = devm_pwmchip_add(dev, chip); 668 662 if (ret < 0) 669 663 return ret; 670 664 671 - platform_set_drvdata(pdev, priv); 665 + platform_set_drvdata(pdev, chip); 672 666 673 667 return 0; 674 668 } 675 669 676 670 static int stm32_pwm_suspend(struct device *dev) 677 671 { 678 - struct stm32_pwm *priv = dev_get_drvdata(dev); 672 + struct pwm_chip *chip = dev_get_drvdata(dev); 673 + struct stm32_pwm *priv = to_stm32_pwm_dev(chip); 679 674 unsigned int i; 680 675 u32 ccer, mask; 681 676 682 677 /* Look for active channels */ 683 678 ccer = active_channels(priv); 684 679 685 - for (i = 0; i < priv->chip.npwm; i++) { 680 + for (i = 0; i < chip->npwm; i++) { 686 681 mask = TIM_CCER_CC1E << (i * 4); 687 682 if (ccer & mask) { 688 683 dev_err(dev, "PWM %u still in use by consumer %s\n", 689 - i, priv->chip.pwms[i].label); 684 + i, chip->pwms[i].label); 690 685 return -EBUSY; 691 686 } 692 687 } ··· 695 692 696 693 static int stm32_pwm_resume(struct device *dev) 697 694 { 698 - struct stm32_pwm *priv = dev_get_drvdata(dev); 695 + struct pwm_chip *chip = dev_get_drvdata(dev); 696 + struct stm32_pwm *priv = to_stm32_pwm_dev(chip); 699 697 int ret; 700 698 701 699 ret = pinctrl_pm_select_default_state(dev);
+30 -28
drivers/pwm/pwm-stmpe.c
··· 27 27 28 28 struct stmpe_pwm { 29 29 struct stmpe *stmpe; 30 - struct pwm_chip chip; 31 30 u8 last_duty; 32 31 }; 33 32 34 33 static inline struct stmpe_pwm *to_stmpe_pwm(struct pwm_chip *chip) 35 34 { 36 - return container_of(chip, struct stmpe_pwm, chip); 35 + return pwmchip_get_drvdata(chip); 37 36 } 38 37 39 38 static int stmpe_24xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) ··· 43 44 44 45 ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS); 45 46 if (ret < 0) { 46 - dev_dbg(chip->dev, "error reading PWM#%u control\n", 47 + dev_dbg(pwmchip_parent(chip), "error reading PWM#%u control\n", 47 48 pwm->hwpwm); 48 49 return ret; 49 50 } ··· 52 53 53 54 ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value); 54 55 if (ret) { 55 - dev_dbg(chip->dev, "error writing PWM#%u control\n", 56 + dev_dbg(pwmchip_parent(chip), "error writing PWM#%u control\n", 56 57 pwm->hwpwm); 57 58 return ret; 58 59 } ··· 69 70 70 71 ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS); 71 72 if (ret < 0) { 72 - dev_dbg(chip->dev, "error reading PWM#%u control\n", 73 + dev_dbg(pwmchip_parent(chip), "error reading PWM#%u control\n", 73 74 pwm->hwpwm); 74 75 return ret; 75 76 } ··· 78 79 79 80 ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value); 80 81 if (ret) 81 - dev_dbg(chip->dev, "error writing PWM#%u control\n", 82 + dev_dbg(pwmchip_parent(chip), "error writing PWM#%u control\n", 82 83 pwm->hwpwm); 83 84 return ret; 84 85 } ··· 124 125 ret = stmpe_set_altfunc(stmpe_pwm->stmpe, BIT(pin), 125 126 STMPE_BLOCK_PWM); 126 127 if (ret) { 127 - dev_err(chip->dev, "unable to connect PWM#%u to pin\n", 128 + dev_err(pwmchip_parent(chip), "unable to connect PWM#%u to pin\n", 128 129 pwm->hwpwm); 129 130 return ret; 130 131 } ··· 149 150 return -ENODEV; 150 151 } 151 152 152 - dev_dbg(chip->dev, "PWM#%u: config duty %d ns, period %d ns\n", 153 + dev_dbg(pwmchip_parent(chip), "PWM#%u: config duty %d ns, period %d ns\n", 153 154 pwm->hwpwm, duty_ns, period_ns); 154 155 155 156 if (duty_ns == 0) { ··· 215 216 program[1] = BRANCH; 216 217 } 217 218 218 - dev_dbg(chip->dev, 219 + dev_dbg(pwmchip_parent(chip), 219 220 "PWM#%u: value = %02x, last_duty = %02x, program=%04x,%04x,%04x\n", 220 221 pwm->hwpwm, value, last, program[0], program[1], 221 222 program[2]); ··· 232 233 233 234 ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value); 234 235 if (ret) { 235 - dev_dbg(chip->dev, "error writing register %02x: %d\n", 236 + dev_dbg(pwmchip_parent(chip), "error writing register %02x: %d\n", 236 237 offset, ret); 237 238 return ret; 238 239 } ··· 241 242 242 243 ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value); 243 244 if (ret) { 244 - dev_dbg(chip->dev, "error writing register %02x: %d\n", 245 + dev_dbg(pwmchip_parent(chip), "error writing register %02x: %d\n", 245 246 offset, ret); 246 247 return ret; 247 248 } ··· 254 255 /* Sleep for 200ms so we're sure it will take effect */ 255 256 msleep(200); 256 257 257 - dev_dbg(chip->dev, "programmed PWM#%u, %u bytes\n", pwm->hwpwm, i); 258 + dev_dbg(pwmchip_parent(chip), "programmed PWM#%u, %u bytes\n", pwm->hwpwm, i); 258 259 259 260 return 0; 260 261 } ··· 291 292 static int __init stmpe_pwm_probe(struct platform_device *pdev) 292 293 { 293 294 struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent); 295 + struct pwm_chip *chip; 294 296 struct stmpe_pwm *stmpe_pwm; 295 297 int ret; 296 298 297 - stmpe_pwm = devm_kzalloc(&pdev->dev, sizeof(*stmpe_pwm), GFP_KERNEL); 298 - if (!stmpe_pwm) 299 - return -ENOMEM; 299 + switch (stmpe->partnum) { 300 + case STMPE2401: 301 + case STMPE2403: 302 + break; 303 + case STMPE1601: 304 + return dev_err_probe(&pdev->dev, -ENODEV, 305 + "STMPE1601 not yet supported\n"); 306 + default: 307 + return dev_err_probe(&pdev->dev, -ENODEV, 308 + "Unknown STMPE PWM\n"); 309 + } 310 + 311 + chip = devm_pwmchip_alloc(&pdev->dev, 3, sizeof(*stmpe_pwm)); 312 + if (IS_ERR(chip)) 313 + return PTR_ERR(chip); 314 + stmpe_pwm = to_stmpe_pwm(chip); 300 315 301 316 stmpe_pwm->stmpe = stmpe; 302 - stmpe_pwm->chip.dev = &pdev->dev; 303 317 304 - if (stmpe->partnum == STMPE2401 || stmpe->partnum == STMPE2403) { 305 - stmpe_pwm->chip.ops = &stmpe_24xx_pwm_ops; 306 - stmpe_pwm->chip.npwm = 3; 307 - } else { 308 - if (stmpe->partnum == STMPE1601) 309 - dev_err(&pdev->dev, "STMPE1601 not yet supported\n"); 310 - else 311 - dev_err(&pdev->dev, "Unknown STMPE PWM\n"); 312 - 313 - return -ENODEV; 314 - } 318 + chip->ops = &stmpe_24xx_pwm_ops; 315 319 316 320 ret = stmpe_enable(stmpe, STMPE_BLOCK_PWM); 317 321 if (ret) 318 322 return ret; 319 323 320 - ret = pwmchip_add(&stmpe_pwm->chip); 324 + ret = pwmchip_add(chip); 321 325 if (ret) { 322 326 stmpe_disable(stmpe, STMPE_BLOCK_PWM); 323 327 return ret;
+51 -49
drivers/pwm/pwm-sun4i.c
··· 81 81 }; 82 82 83 83 struct sun4i_pwm_chip { 84 - struct pwm_chip chip; 85 84 struct clk *bus_clk; 86 85 struct clk *clk; 87 86 struct reset_control *rst; ··· 91 92 92 93 static inline struct sun4i_pwm_chip *to_sun4i_pwm_chip(struct pwm_chip *chip) 93 94 { 94 - return container_of(chip, struct sun4i_pwm_chip, chip); 95 + return pwmchip_get_drvdata(chip); 95 96 } 96 97 97 - static inline u32 sun4i_pwm_readl(struct sun4i_pwm_chip *chip, 98 + static inline u32 sun4i_pwm_readl(struct sun4i_pwm_chip *sun4ichip, 98 99 unsigned long offset) 99 100 { 100 - return readl(chip->base + offset); 101 + return readl(sun4ichip->base + offset); 101 102 } 102 103 103 - static inline void sun4i_pwm_writel(struct sun4i_pwm_chip *chip, 104 + static inline void sun4i_pwm_writel(struct sun4i_pwm_chip *sun4ichip, 104 105 u32 val, unsigned long offset) 105 106 { 106 - writel(val, chip->base + offset); 107 + writel(val, sun4ichip->base + offset); 107 108 } 108 109 109 110 static int sun4i_pwm_get_state(struct pwm_chip *chip, 110 111 struct pwm_device *pwm, 111 112 struct pwm_state *state) 112 113 { 113 - struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip); 114 + struct sun4i_pwm_chip *sun4ichip = to_sun4i_pwm_chip(chip); 114 115 u64 clk_rate, tmp; 115 116 u32 val; 116 117 unsigned int prescaler; 117 118 118 - clk_rate = clk_get_rate(sun4i_pwm->clk); 119 + clk_rate = clk_get_rate(sun4ichip->clk); 119 120 if (!clk_rate) 120 121 return -EINVAL; 121 122 122 - val = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG); 123 + val = sun4i_pwm_readl(sun4ichip, PWM_CTRL_REG); 123 124 124 125 /* 125 126 * PWM chapter in H6 manual has a diagram which explains that if bypass ··· 127 128 * proved that also enable bit is ignored in this case. 128 129 */ 129 130 if ((val & BIT_CH(PWM_BYPASS, pwm->hwpwm)) && 130 - sun4i_pwm->data->has_direct_mod_clk_output) { 131 + sun4ichip->data->has_direct_mod_clk_output) { 131 132 state->period = DIV_ROUND_UP_ULL(NSEC_PER_SEC, clk_rate); 132 133 state->duty_cycle = DIV_ROUND_UP_ULL(state->period, 2); 133 134 state->polarity = PWM_POLARITY_NORMAL; ··· 136 137 } 137 138 138 139 if ((PWM_REG_PRESCAL(val, pwm->hwpwm) == PWM_PRESCAL_MASK) && 139 - sun4i_pwm->data->has_prescaler_bypass) 140 + sun4ichip->data->has_prescaler_bypass) 140 141 prescaler = 1; 141 142 else 142 143 prescaler = prescaler_table[PWM_REG_PRESCAL(val, pwm->hwpwm)]; ··· 155 156 else 156 157 state->enabled = false; 157 158 158 - val = sun4i_pwm_readl(sun4i_pwm, PWM_CH_PRD(pwm->hwpwm)); 159 + val = sun4i_pwm_readl(sun4ichip, PWM_CH_PRD(pwm->hwpwm)); 159 160 160 161 tmp = (u64)prescaler * NSEC_PER_SEC * PWM_REG_DTY(val); 161 162 state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate); ··· 166 167 return 0; 167 168 } 168 169 169 - static int sun4i_pwm_calculate(struct sun4i_pwm_chip *sun4i_pwm, 170 + static int sun4i_pwm_calculate(struct sun4i_pwm_chip *sun4ichip, 170 171 const struct pwm_state *state, 171 172 u32 *dty, u32 *prd, unsigned int *prsclr, 172 173 bool *bypass) ··· 174 175 u64 clk_rate, div = 0; 175 176 unsigned int prescaler = 0; 176 177 177 - clk_rate = clk_get_rate(sun4i_pwm->clk); 178 + clk_rate = clk_get_rate(sun4ichip->clk); 178 179 179 - *bypass = sun4i_pwm->data->has_direct_mod_clk_output && 180 + *bypass = sun4ichip->data->has_direct_mod_clk_output && 180 181 state->enabled && 181 182 (state->period * clk_rate >= NSEC_PER_SEC) && 182 183 (state->period * clk_rate < 2 * NSEC_PER_SEC) && ··· 186 187 if (*bypass) 187 188 return 0; 188 189 189 - if (sun4i_pwm->data->has_prescaler_bypass) { 190 + if (sun4ichip->data->has_prescaler_bypass) { 190 191 /* First, test without any prescaler when available */ 191 192 prescaler = PWM_PRESCAL_MASK; 192 193 /* ··· 232 233 static int sun4i_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 233 234 const struct pwm_state *state) 234 235 { 235 - struct sun4i_pwm_chip *sun4i_pwm = to_sun4i_pwm_chip(chip); 236 + struct sun4i_pwm_chip *sun4ichip = to_sun4i_pwm_chip(chip); 236 237 struct pwm_state cstate; 237 238 u32 ctrl, duty = 0, period = 0, val; 238 239 int ret; ··· 242 243 pwm_get_state(pwm, &cstate); 243 244 244 245 if (!cstate.enabled) { 245 - ret = clk_prepare_enable(sun4i_pwm->clk); 246 + ret = clk_prepare_enable(sun4ichip->clk); 246 247 if (ret) { 247 - dev_err(chip->dev, "failed to enable PWM clock\n"); 248 + dev_err(pwmchip_parent(chip), "failed to enable PWM clock\n"); 248 249 return ret; 249 250 } 250 251 } 251 252 252 - ret = sun4i_pwm_calculate(sun4i_pwm, state, &duty, &period, &prescaler, 253 + ret = sun4i_pwm_calculate(sun4ichip, state, &duty, &period, &prescaler, 253 254 &bypass); 254 255 if (ret) { 255 - dev_err(chip->dev, "period exceeds the maximum value\n"); 256 + dev_err(pwmchip_parent(chip), "period exceeds the maximum value\n"); 256 257 if (!cstate.enabled) 257 - clk_disable_unprepare(sun4i_pwm->clk); 258 + clk_disable_unprepare(sun4ichip->clk); 258 259 return ret; 259 260 } 260 261 261 - spin_lock(&sun4i_pwm->ctrl_lock); 262 - ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG); 262 + spin_lock(&sun4ichip->ctrl_lock); 263 + ctrl = sun4i_pwm_readl(sun4ichip, PWM_CTRL_REG); 263 264 264 - if (sun4i_pwm->data->has_direct_mod_clk_output) { 265 + if (sun4ichip->data->has_direct_mod_clk_output) { 265 266 if (bypass) { 266 267 ctrl |= BIT_CH(PWM_BYPASS, pwm->hwpwm); 267 268 /* We can skip other parameter */ 268 - sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG); 269 - spin_unlock(&sun4i_pwm->ctrl_lock); 269 + sun4i_pwm_writel(sun4ichip, ctrl, PWM_CTRL_REG); 270 + spin_unlock(&sun4ichip->ctrl_lock); 270 271 return 0; 271 272 } 272 273 ··· 276 277 if (PWM_REG_PRESCAL(ctrl, pwm->hwpwm) != prescaler) { 277 278 /* Prescaler changed, the clock has to be gated */ 278 279 ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm); 279 - sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG); 280 + sun4i_pwm_writel(sun4ichip, ctrl, PWM_CTRL_REG); 280 281 281 282 ctrl &= ~BIT_CH(PWM_PRESCAL_MASK, pwm->hwpwm); 282 283 ctrl |= BIT_CH(prescaler, pwm->hwpwm); 283 284 } 284 285 285 286 val = (duty & PWM_DTY_MASK) | PWM_PRD(period); 286 - sun4i_pwm_writel(sun4i_pwm, val, PWM_CH_PRD(pwm->hwpwm)); 287 + sun4i_pwm_writel(sun4ichip, val, PWM_CH_PRD(pwm->hwpwm)); 287 288 288 289 if (state->polarity != PWM_POLARITY_NORMAL) 289 290 ctrl &= ~BIT_CH(PWM_ACT_STATE, pwm->hwpwm); ··· 295 296 if (state->enabled) 296 297 ctrl |= BIT_CH(PWM_EN, pwm->hwpwm); 297 298 298 - sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG); 299 + sun4i_pwm_writel(sun4ichip, ctrl, PWM_CTRL_REG); 299 300 300 - spin_unlock(&sun4i_pwm->ctrl_lock); 301 + spin_unlock(&sun4ichip->ctrl_lock); 301 302 302 303 if (state->enabled) 303 304 return 0; ··· 309 310 else 310 311 usleep_range(delay_us, delay_us * 2); 311 312 312 - spin_lock(&sun4i_pwm->ctrl_lock); 313 - ctrl = sun4i_pwm_readl(sun4i_pwm, PWM_CTRL_REG); 313 + spin_lock(&sun4ichip->ctrl_lock); 314 + ctrl = sun4i_pwm_readl(sun4ichip, PWM_CTRL_REG); 314 315 ctrl &= ~BIT_CH(PWM_CLK_GATING, pwm->hwpwm); 315 316 ctrl &= ~BIT_CH(PWM_EN, pwm->hwpwm); 316 - sun4i_pwm_writel(sun4i_pwm, ctrl, PWM_CTRL_REG); 317 - spin_unlock(&sun4i_pwm->ctrl_lock); 317 + sun4i_pwm_writel(sun4ichip, ctrl, PWM_CTRL_REG); 318 + spin_unlock(&sun4ichip->ctrl_lock); 318 319 319 - clk_disable_unprepare(sun4i_pwm->clk); 320 + clk_disable_unprepare(sun4ichip->clk); 320 321 321 322 return 0; 322 323 } ··· 383 384 384 385 static int sun4i_pwm_probe(struct platform_device *pdev) 385 386 { 387 + struct pwm_chip *chip; 388 + const struct sun4i_pwm_data *data; 386 389 struct sun4i_pwm_chip *sun4ichip; 387 390 int ret; 388 391 389 - sun4ichip = devm_kzalloc(&pdev->dev, sizeof(*sun4ichip), GFP_KERNEL); 390 - if (!sun4ichip) 391 - return -ENOMEM; 392 - 393 - sun4ichip->data = of_device_get_match_data(&pdev->dev); 394 - if (!sun4ichip->data) 392 + data = of_device_get_match_data(&pdev->dev); 393 + if (!data) 395 394 return -ENODEV; 396 395 396 + chip = devm_pwmchip_alloc(&pdev->dev, data->npwm, sizeof(*sun4ichip)); 397 + if (IS_ERR(chip)) 398 + return PTR_ERR(chip); 399 + sun4ichip = to_sun4i_pwm_chip(chip); 400 + 401 + sun4ichip->data = data; 397 402 sun4ichip->base = devm_platform_ioremap_resource(pdev, 0); 398 403 if (IS_ERR(sun4ichip->base)) 399 404 return PTR_ERR(sun4ichip->base); ··· 454 451 goto err_bus; 455 452 } 456 453 457 - sun4ichip->chip.dev = &pdev->dev; 458 - sun4ichip->chip.ops = &sun4i_pwm_ops; 459 - sun4ichip->chip.npwm = sun4ichip->data->npwm; 454 + chip->ops = &sun4i_pwm_ops; 460 455 461 456 spin_lock_init(&sun4ichip->ctrl_lock); 462 457 463 - ret = pwmchip_add(&sun4ichip->chip); 458 + ret = pwmchip_add(chip); 464 459 if (ret < 0) { 465 460 dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret); 466 461 goto err_pwm_add; 467 462 } 468 463 469 - platform_set_drvdata(pdev, sun4ichip); 464 + platform_set_drvdata(pdev, chip); 470 465 471 466 return 0; 472 467 ··· 478 477 479 478 static void sun4i_pwm_remove(struct platform_device *pdev) 480 479 { 481 - struct sun4i_pwm_chip *sun4ichip = platform_get_drvdata(pdev); 480 + struct pwm_chip *chip = platform_get_drvdata(pdev); 481 + struct sun4i_pwm_chip *sun4ichip = to_sun4i_pwm_chip(chip); 482 482 483 - pwmchip_remove(&sun4ichip->chip); 483 + pwmchip_remove(chip); 484 484 485 485 clk_disable_unprepare(sun4ichip->bus_clk); 486 486 reset_control_assert(sun4ichip->rst);
+8 -9
drivers/pwm/pwm-sunplus.c
··· 43 43 #define SP7021_PWM_NUM 4 44 44 45 45 struct sunplus_pwm { 46 - struct pwm_chip chip; 47 46 void __iomem *base; 48 47 struct clk *clk; 49 48 }; 50 49 51 50 static inline struct sunplus_pwm *to_sunplus_pwm(struct pwm_chip *chip) 52 51 { 53 - return container_of(chip, struct sunplus_pwm, chip); 52 + return pwmchip_get_drvdata(chip); 54 53 } 55 54 56 55 static int sunplus_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, ··· 174 175 static int sunplus_pwm_probe(struct platform_device *pdev) 175 176 { 176 177 struct device *dev = &pdev->dev; 178 + struct pwm_chip *chip; 177 179 struct sunplus_pwm *priv; 178 180 int ret; 179 181 180 - priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 181 - if (!priv) 182 - return -ENOMEM; 182 + chip = devm_pwmchip_alloc(dev, SP7021_PWM_NUM, sizeof(*priv)); 183 + if (IS_ERR(chip)) 184 + return PTR_ERR(chip); 185 + priv = to_sunplus_pwm(chip); 183 186 184 187 priv->base = devm_platform_ioremap_resource(pdev, 0); 185 188 if (IS_ERR(priv->base)) ··· 204 203 return ret; 205 204 } 206 205 207 - priv->chip.dev = dev; 208 - priv->chip.ops = &sunplus_pwm_ops; 209 - priv->chip.npwm = SP7021_PWM_NUM; 206 + chip->ops = &sunplus_pwm_ops; 210 207 211 - ret = devm_pwmchip_add(dev, &priv->chip); 208 + ret = devm_pwmchip_add(dev, chip); 212 209 if (ret < 0) 213 210 return dev_err_probe(dev, ret, "Cannot register sunplus PWM\n"); 214 211
+26 -24
drivers/pwm/pwm-tegra.c
··· 65 65 }; 66 66 67 67 struct tegra_pwm_chip { 68 - struct pwm_chip chip; 69 - struct device *dev; 70 - 71 68 struct clk *clk; 72 69 struct reset_control*rst; 73 70 ··· 78 81 79 82 static inline struct tegra_pwm_chip *to_tegra_pwm_chip(struct pwm_chip *chip) 80 83 { 81 - return container_of(chip, struct tegra_pwm_chip, chip); 84 + return pwmchip_get_drvdata(chip); 82 85 } 83 86 84 87 static inline u32 pwm_readl(struct tegra_pwm_chip *pc, unsigned int offset) ··· 155 158 */ 156 159 required_clk_rate *= 2; 157 160 158 - err = dev_pm_opp_set_rate(pc->dev, required_clk_rate); 161 + err = dev_pm_opp_set_rate(pwmchip_parent(chip), required_clk_rate); 159 162 if (err < 0) 160 163 return -EINVAL; 161 164 ··· 191 194 * before writing the register. Otherwise, keep it enabled. 192 195 */ 193 196 if (!pwm_is_enabled(pwm)) { 194 - err = pm_runtime_resume_and_get(pc->dev); 197 + err = pm_runtime_resume_and_get(pwmchip_parent(chip)); 195 198 if (err) 196 199 return err; 197 200 } else ··· 203 206 * If the PWM is not enabled, turn the clock off again to save power. 204 207 */ 205 208 if (!pwm_is_enabled(pwm)) 206 - pm_runtime_put(pc->dev); 209 + pm_runtime_put(pwmchip_parent(chip)); 207 210 208 211 return 0; 209 212 } ··· 214 217 int rc = 0; 215 218 u32 val; 216 219 217 - rc = pm_runtime_resume_and_get(pc->dev); 220 + rc = pm_runtime_resume_and_get(pwmchip_parent(chip)); 218 221 if (rc) 219 222 return rc; 220 223 ··· 234 237 val &= ~PWM_ENABLE; 235 238 pwm_writel(pc, pwm->hwpwm, val); 236 239 237 - pm_runtime_put_sync(pc->dev); 240 + pm_runtime_put_sync(pwmchip_parent(chip)); 238 241 } 239 242 240 243 static int tegra_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, ··· 269 272 270 273 static int tegra_pwm_probe(struct platform_device *pdev) 271 274 { 275 + struct pwm_chip *chip; 272 276 struct tegra_pwm_chip *pc; 277 + const struct tegra_pwm_soc *soc; 273 278 int ret; 274 279 275 - pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); 276 - if (!pc) 277 - return -ENOMEM; 280 + soc = of_device_get_match_data(&pdev->dev); 278 281 279 - pc->soc = of_device_get_match_data(&pdev->dev); 280 - pc->dev = &pdev->dev; 282 + chip = devm_pwmchip_alloc(&pdev->dev, soc->num_channels, sizeof(*pc)); 283 + if (IS_ERR(chip)) 284 + return PTR_ERR(chip); 285 + pc = to_tegra_pwm_chip(chip); 286 + 287 + pc->soc = soc; 281 288 282 289 pc->regs = devm_platform_ioremap_resource(pdev, 0); 283 290 if (IS_ERR(pc->regs)) 284 291 return PTR_ERR(pc->regs); 285 292 286 - platform_set_drvdata(pdev, pc); 293 + platform_set_drvdata(pdev, chip); 287 294 288 295 pc->clk = devm_clk_get(&pdev->dev, NULL); 289 296 if (IS_ERR(pc->clk)) ··· 303 302 return ret; 304 303 305 304 /* Set maximum frequency of the IP */ 306 - ret = dev_pm_opp_set_rate(pc->dev, pc->soc->max_frequency); 305 + ret = dev_pm_opp_set_rate(&pdev->dev, pc->soc->max_frequency); 307 306 if (ret < 0) { 308 307 dev_err(&pdev->dev, "Failed to set max frequency: %d\n", ret); 309 308 goto put_pm; ··· 329 328 330 329 reset_control_deassert(pc->rst); 331 330 332 - pc->chip.dev = &pdev->dev; 333 - pc->chip.ops = &tegra_pwm_ops; 334 - pc->chip.npwm = pc->soc->num_channels; 331 + chip->ops = &tegra_pwm_ops; 335 332 336 - ret = pwmchip_add(&pc->chip); 333 + ret = pwmchip_add(chip); 337 334 if (ret < 0) { 338 335 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); 339 336 reset_control_assert(pc->rst); ··· 349 350 350 351 static void tegra_pwm_remove(struct platform_device *pdev) 351 352 { 352 - struct tegra_pwm_chip *pc = platform_get_drvdata(pdev); 353 + struct pwm_chip *chip = platform_get_drvdata(pdev); 354 + struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip); 353 355 354 - pwmchip_remove(&pc->chip); 356 + pwmchip_remove(chip); 355 357 356 358 reset_control_assert(pc->rst); 357 359 ··· 361 361 362 362 static int __maybe_unused tegra_pwm_runtime_suspend(struct device *dev) 363 363 { 364 - struct tegra_pwm_chip *pc = dev_get_drvdata(dev); 364 + struct pwm_chip *chip = dev_get_drvdata(dev); 365 + struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip); 365 366 int err; 366 367 367 368 clk_disable_unprepare(pc->clk); ··· 378 377 379 378 static int __maybe_unused tegra_pwm_runtime_resume(struct device *dev) 380 379 { 381 - struct tegra_pwm_chip *pc = dev_get_drvdata(dev); 380 + struct pwm_chip *chip = dev_get_drvdata(dev); 381 + struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip); 382 382 int err; 383 383 384 384 err = pinctrl_pm_select_default_state(dev);
+29 -26
drivers/pwm/pwm-tiecap.c
··· 32 32 }; 33 33 34 34 struct ecap_pwm_chip { 35 - struct pwm_chip chip; 36 35 unsigned int clk_rate; 37 36 void __iomem *mmio_base; 38 37 struct ecap_context ctx; ··· 39 40 40 41 static inline struct ecap_pwm_chip *to_ecap_pwm_chip(struct pwm_chip *chip) 41 42 { 42 - return container_of(chip, struct ecap_pwm_chip, chip); 43 + return pwmchip_get_drvdata(chip); 43 44 } 44 45 45 46 /* ··· 69 70 duty_cycles = (u32)c; 70 71 } 71 72 72 - pm_runtime_get_sync(pc->chip.dev); 73 + pm_runtime_get_sync(pwmchip_parent(chip)); 73 74 74 75 value = readw(pc->mmio_base + ECCTL2); 75 76 ··· 99 100 writew(value, pc->mmio_base + ECCTL2); 100 101 } 101 102 102 - pm_runtime_put_sync(pc->chip.dev); 103 + pm_runtime_put_sync(pwmchip_parent(chip)); 103 104 104 105 return 0; 105 106 } ··· 110 111 struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip); 111 112 u16 value; 112 113 113 - pm_runtime_get_sync(pc->chip.dev); 114 + pm_runtime_get_sync(pwmchip_parent(chip)); 114 115 115 116 value = readw(pc->mmio_base + ECCTL2); 116 117 ··· 123 124 124 125 writew(value, pc->mmio_base + ECCTL2); 125 126 126 - pm_runtime_put_sync(pc->chip.dev); 127 + pm_runtime_put_sync(pwmchip_parent(chip)); 127 128 128 129 return 0; 129 130 } ··· 134 135 u16 value; 135 136 136 137 /* Leave clock enabled on enabling PWM */ 137 - pm_runtime_get_sync(pc->chip.dev); 138 + pm_runtime_get_sync(pwmchip_parent(chip)); 138 139 139 140 /* 140 141 * Enable 'Free run Time stamp counter mode' to start counter ··· 161 162 writew(value, pc->mmio_base + ECCTL2); 162 163 163 164 /* Disable clock on PWM disable */ 164 - pm_runtime_put_sync(pc->chip.dev); 165 + pm_runtime_put_sync(pwmchip_parent(chip)); 165 166 } 166 167 167 168 static int ecap_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, ··· 217 218 { 218 219 struct device_node *np = pdev->dev.of_node; 219 220 struct ecap_pwm_chip *pc; 221 + struct pwm_chip *chip; 220 222 struct clk *clk; 221 223 int ret; 222 224 223 - pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); 224 - if (!pc) 225 - return -ENOMEM; 225 + chip = devm_pwmchip_alloc(&pdev->dev, 1, sizeof(*pc)); 226 + if (IS_ERR(chip)) 227 + return PTR_ERR(chip); 228 + pc = to_ecap_pwm_chip(chip); 226 229 227 230 clk = devm_clk_get(&pdev->dev, "fck"); 228 231 if (IS_ERR(clk)) { ··· 245 244 return -EINVAL; 246 245 } 247 246 248 - pc->chip.dev = &pdev->dev; 249 - pc->chip.ops = &ecap_pwm_ops; 250 - pc->chip.npwm = 1; 247 + chip->ops = &ecap_pwm_ops; 251 248 252 249 pc->mmio_base = devm_platform_ioremap_resource(pdev, 0); 253 250 if (IS_ERR(pc->mmio_base)) 254 251 return PTR_ERR(pc->mmio_base); 255 252 256 - ret = devm_pwmchip_add(&pdev->dev, &pc->chip); 253 + ret = devm_pwmchip_add(&pdev->dev, chip); 257 254 if (ret < 0) { 258 255 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); 259 256 return ret; 260 257 } 261 258 262 - platform_set_drvdata(pdev, pc); 259 + platform_set_drvdata(pdev, chip); 263 260 pm_runtime_enable(&pdev->dev); 264 261 265 262 return 0; ··· 268 269 pm_runtime_disable(&pdev->dev); 269 270 } 270 271 271 - static void ecap_pwm_save_context(struct ecap_pwm_chip *pc) 272 + static void ecap_pwm_save_context(struct pwm_chip *chip) 272 273 { 273 - pm_runtime_get_sync(pc->chip.dev); 274 + struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip); 275 + 276 + pm_runtime_get_sync(pwmchip_parent(chip)); 274 277 pc->ctx.ecctl2 = readw(pc->mmio_base + ECCTL2); 275 278 pc->ctx.cap4 = readl(pc->mmio_base + CAP4); 276 279 pc->ctx.cap3 = readl(pc->mmio_base + CAP3); 277 - pm_runtime_put_sync(pc->chip.dev); 280 + pm_runtime_put_sync(pwmchip_parent(chip)); 278 281 } 279 282 280 - static void ecap_pwm_restore_context(struct ecap_pwm_chip *pc) 283 + static void ecap_pwm_restore_context(struct pwm_chip *chip) 281 284 { 285 + struct ecap_pwm_chip *pc = to_ecap_pwm_chip(chip); 286 + 282 287 writel(pc->ctx.cap3, pc->mmio_base + CAP3); 283 288 writel(pc->ctx.cap4, pc->mmio_base + CAP4); 284 289 writew(pc->ctx.ecctl2, pc->mmio_base + ECCTL2); ··· 290 287 291 288 static int ecap_pwm_suspend(struct device *dev) 292 289 { 293 - struct ecap_pwm_chip *pc = dev_get_drvdata(dev); 294 - struct pwm_device *pwm = pc->chip.pwms; 290 + struct pwm_chip *chip = dev_get_drvdata(dev); 291 + struct pwm_device *pwm = chip->pwms; 295 292 296 - ecap_pwm_save_context(pc); 293 + ecap_pwm_save_context(chip); 297 294 298 295 /* Disable explicitly if PWM is running */ 299 296 if (pwm_is_enabled(pwm)) ··· 304 301 305 302 static int ecap_pwm_resume(struct device *dev) 306 303 { 307 - struct ecap_pwm_chip *pc = dev_get_drvdata(dev); 308 - struct pwm_device *pwm = pc->chip.pwms; 304 + struct pwm_chip *chip = dev_get_drvdata(dev); 305 + struct pwm_device *pwm = chip->pwms; 309 306 310 307 /* Enable explicitly if PWM was running */ 311 308 if (pwm_is_enabled(pwm)) 312 309 pm_runtime_get_sync(dev); 313 310 314 - ecap_pwm_restore_context(pc); 311 + ecap_pwm_restore_context(chip); 315 312 return 0; 316 313 } 317 314
+38 -34
drivers/pwm/pwm-tiehrpwm.c
··· 105 105 }; 106 106 107 107 struct ehrpwm_pwm_chip { 108 - struct pwm_chip chip; 109 108 unsigned long clk_rate; 110 109 void __iomem *mmio_base; 111 110 unsigned long period_cycles[NUM_PWM_CHANNEL]; ··· 115 116 116 117 static inline struct ehrpwm_pwm_chip *to_ehrpwm_pwm_chip(struct pwm_chip *chip) 117 118 { 118 - return container_of(chip, struct ehrpwm_pwm_chip, chip); 119 + return pwmchip_get_drvdata(chip); 119 120 } 120 121 121 122 static inline u16 ehrpwm_read(void __iomem *base, unsigned int offset) ··· 255 256 if (i == pwm->hwpwm) 256 257 continue; 257 258 258 - dev_err(chip->dev, 259 + dev_err(pwmchip_parent(chip), 259 260 "period value conflicts with channel %u\n", 260 261 i); 261 262 return -EINVAL; ··· 267 268 /* Configure clock prescaler to support Low frequency PWM wave */ 268 269 if (set_prescale_div(period_cycles/PERIOD_MAX, &ps_divval, 269 270 &tb_divval)) { 270 - dev_err(chip->dev, "Unsupported values\n"); 271 + dev_err(pwmchip_parent(chip), "Unsupported values\n"); 271 272 return -EINVAL; 272 273 } 273 274 274 - pm_runtime_get_sync(chip->dev); 275 + pm_runtime_get_sync(pwmchip_parent(chip)); 275 276 276 277 /* Update clock prescaler values */ 277 278 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_CLKDIV_MASK, tb_divval); ··· 298 299 299 300 ehrpwm_write(pc->mmio_base, cmp_reg, duty_cycles); 300 301 301 - pm_runtime_put_sync(chip->dev); 302 + pm_runtime_put_sync(pwmchip_parent(chip)); 302 303 303 304 return 0; 304 305 } ··· 322 323 int ret; 323 324 324 325 /* Leave clock enabled on enabling PWM */ 325 - pm_runtime_get_sync(chip->dev); 326 + pm_runtime_get_sync(pwmchip_parent(chip)); 326 327 327 328 /* Disabling Action Qualifier on PWM output */ 328 329 if (pwm->hwpwm) { ··· 345 346 /* Enable TBCLK */ 346 347 ret = clk_enable(pc->tbclk); 347 348 if (ret) { 348 - dev_err(chip->dev, "Failed to enable TBCLK for %s: %d\n", 349 - dev_name(pc->chip.dev), ret); 349 + dev_err(pwmchip_parent(chip), "Failed to enable TBCLK for %s: %d\n", 350 + dev_name(pwmchip_parent(chip)), ret); 350 351 return ret; 351 352 } 352 353 ··· 384 385 clk_disable(pc->tbclk); 385 386 386 387 /* Disable clock on PWM disable */ 387 - pm_runtime_put_sync(chip->dev); 388 + pm_runtime_put_sync(pwmchip_parent(chip)); 388 389 } 389 390 390 391 static void ehrpwm_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) ··· 392 393 struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip); 393 394 394 395 if (pwm_is_enabled(pwm)) { 395 - dev_warn(chip->dev, "Removing PWM device without disabling\n"); 396 - pm_runtime_put_sync(chip->dev); 396 + dev_warn(pwmchip_parent(chip), "Removing PWM device without disabling\n"); 397 + pm_runtime_put_sync(pwmchip_parent(chip)); 397 398 } 398 399 399 400 /* set period value to zero on free */ ··· 449 450 { 450 451 struct device_node *np = pdev->dev.of_node; 451 452 struct ehrpwm_pwm_chip *pc; 453 + struct pwm_chip *chip; 452 454 struct clk *clk; 453 455 int ret; 454 456 455 - pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); 456 - if (!pc) 457 - return -ENOMEM; 457 + chip = devm_pwmchip_alloc(&pdev->dev, NUM_PWM_CHANNEL, sizeof(*pc)); 458 + if (IS_ERR(chip)) 459 + return PTR_ERR(chip); 460 + pc = to_ehrpwm_pwm_chip(chip); 458 461 459 462 clk = devm_clk_get(&pdev->dev, "fck"); 460 463 if (IS_ERR(clk)) { ··· 475 474 return -EINVAL; 476 475 } 477 476 478 - pc->chip.dev = &pdev->dev; 479 - pc->chip.ops = &ehrpwm_pwm_ops; 480 - pc->chip.npwm = NUM_PWM_CHANNEL; 477 + chip->ops = &ehrpwm_pwm_ops; 481 478 482 479 pc->mmio_base = devm_platform_ioremap_resource(pdev, 0); 483 480 if (IS_ERR(pc->mmio_base)) ··· 492 493 return ret; 493 494 } 494 495 495 - ret = pwmchip_add(&pc->chip); 496 + ret = pwmchip_add(chip); 496 497 if (ret < 0) { 497 498 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); 498 499 goto err_clk_unprepare; 499 500 } 500 501 501 - platform_set_drvdata(pdev, pc); 502 + platform_set_drvdata(pdev, chip); 502 503 pm_runtime_enable(&pdev->dev); 503 504 504 505 return 0; ··· 511 512 512 513 static void ehrpwm_pwm_remove(struct platform_device *pdev) 513 514 { 514 - struct ehrpwm_pwm_chip *pc = platform_get_drvdata(pdev); 515 + struct pwm_chip *chip = platform_get_drvdata(pdev); 516 + struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip); 515 517 516 - pwmchip_remove(&pc->chip); 518 + pwmchip_remove(chip); 517 519 518 520 clk_unprepare(pc->tbclk); 519 521 520 522 pm_runtime_disable(&pdev->dev); 521 523 } 522 524 523 - static void ehrpwm_pwm_save_context(struct ehrpwm_pwm_chip *pc) 525 + static void ehrpwm_pwm_save_context(struct pwm_chip *chip) 524 526 { 525 - pm_runtime_get_sync(pc->chip.dev); 527 + struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip); 528 + 529 + pm_runtime_get_sync(pwmchip_parent(chip)); 526 530 527 531 pc->ctx.tbctl = ehrpwm_read(pc->mmio_base, TBCTL); 528 532 pc->ctx.tbprd = ehrpwm_read(pc->mmio_base, TBPRD); ··· 536 534 pc->ctx.aqsfrc = ehrpwm_read(pc->mmio_base, AQSFRC); 537 535 pc->ctx.aqcsfrc = ehrpwm_read(pc->mmio_base, AQCSFRC); 538 536 539 - pm_runtime_put_sync(pc->chip.dev); 537 + pm_runtime_put_sync(pwmchip_parent(chip)); 540 538 } 541 539 542 - static void ehrpwm_pwm_restore_context(struct ehrpwm_pwm_chip *pc) 540 + static void ehrpwm_pwm_restore_context(struct pwm_chip *chip) 543 541 { 542 + struct ehrpwm_pwm_chip *pc = to_ehrpwm_pwm_chip(chip); 543 + 544 544 ehrpwm_write(pc->mmio_base, TBPRD, pc->ctx.tbprd); 545 545 ehrpwm_write(pc->mmio_base, CMPA, pc->ctx.cmpa); 546 546 ehrpwm_write(pc->mmio_base, CMPB, pc->ctx.cmpb); ··· 555 551 556 552 static int ehrpwm_pwm_suspend(struct device *dev) 557 553 { 558 - struct ehrpwm_pwm_chip *pc = dev_get_drvdata(dev); 554 + struct pwm_chip *chip = dev_get_drvdata(dev); 559 555 unsigned int i; 560 556 561 - ehrpwm_pwm_save_context(pc); 557 + ehrpwm_pwm_save_context(chip); 562 558 563 - for (i = 0; i < pc->chip.npwm; i++) { 564 - struct pwm_device *pwm = &pc->chip.pwms[i]; 559 + for (i = 0; i < chip->npwm; i++) { 560 + struct pwm_device *pwm = &chip->pwms[i]; 565 561 566 562 if (!pwm_is_enabled(pwm)) 567 563 continue; ··· 575 571 576 572 static int ehrpwm_pwm_resume(struct device *dev) 577 573 { 578 - struct ehrpwm_pwm_chip *pc = dev_get_drvdata(dev); 574 + struct pwm_chip *chip = dev_get_drvdata(dev); 579 575 unsigned int i; 580 576 581 - for (i = 0; i < pc->chip.npwm; i++) { 582 - struct pwm_device *pwm = &pc->chip.pwms[i]; 577 + for (i = 0; i < chip->npwm; i++) { 578 + struct pwm_device *pwm = &chip->pwms[i]; 583 579 584 580 if (!pwm_is_enabled(pwm)) 585 581 continue; ··· 588 584 pm_runtime_get_sync(dev); 589 585 } 590 586 591 - ehrpwm_pwm_restore_context(pc); 587 + ehrpwm_pwm_restore_context(chip); 592 588 593 589 return 0; 594 590 }
+29 -26
drivers/pwm/pwm-twl-led.c
··· 62 62 #define TWL6040_LED_MODE_MASK 0x03 63 63 64 64 struct twl_pwmled_chip { 65 - struct pwm_chip chip; 66 65 struct mutex mutex; 67 66 }; 68 67 69 68 static inline struct twl_pwmled_chip *to_twl(struct pwm_chip *chip) 70 69 { 71 - return container_of(chip, struct twl_pwmled_chip, chip); 70 + return pwmchip_get_drvdata(chip); 72 71 } 73 72 74 73 static int twl4030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm, ··· 99 100 100 101 ret = twl_i2c_write(TWL4030_MODULE_LED, pwm_config, base, 2); 101 102 if (ret < 0) 102 - dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label); 103 + dev_err(pwmchip_parent(chip), "%s: Failed to configure PWM\n", pwm->label); 103 104 104 105 return ret; 105 106 } ··· 113 114 mutex_lock(&twl->mutex); 114 115 ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG); 115 116 if (ret < 0) { 116 - dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label); 117 + dev_err(pwmchip_parent(chip), "%s: Failed to read LEDEN\n", pwm->label); 117 118 goto out; 118 119 } 119 120 ··· 121 122 122 123 ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG); 123 124 if (ret < 0) 124 - dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label); 125 + dev_err(pwmchip_parent(chip), "%s: Failed to enable PWM\n", pwm->label); 125 126 126 127 out: 127 128 mutex_unlock(&twl->mutex); ··· 138 139 mutex_lock(&twl->mutex); 139 140 ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG); 140 141 if (ret < 0) { 141 - dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label); 142 + dev_err(pwmchip_parent(chip), "%s: Failed to read LEDEN\n", pwm->label); 142 143 goto out; 143 144 } 144 145 ··· 146 147 147 148 ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG); 148 149 if (ret < 0) 149 - dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label); 150 + dev_err(pwmchip_parent(chip), "%s: Failed to disable PWM\n", pwm->label); 150 151 151 152 out: 152 153 mutex_unlock(&twl->mutex); ··· 202 203 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, on_time, 203 204 TWL6030_LED_PWM_CTRL1); 204 205 if (ret < 0) 205 - dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label); 206 + dev_err(pwmchip_parent(chip), "%s: Failed to configure PWM\n", pwm->label); 206 207 207 208 return ret; 208 209 } ··· 216 217 mutex_lock(&twl->mutex); 217 218 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2); 218 219 if (ret < 0) { 219 - dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n", 220 + dev_err(pwmchip_parent(chip), "%s: Failed to read PWM_CTRL2\n", 220 221 pwm->label); 221 222 goto out; 222 223 } ··· 226 227 227 228 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2); 228 229 if (ret < 0) 229 - dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label); 230 + dev_err(pwmchip_parent(chip), "%s: Failed to enable PWM\n", pwm->label); 230 231 231 232 out: 232 233 mutex_unlock(&twl->mutex); ··· 243 244 mutex_lock(&twl->mutex); 244 245 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2); 245 246 if (ret < 0) { 246 - dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n", 247 + dev_err(pwmchip_parent(chip), "%s: Failed to read PWM_CTRL2\n", 247 248 pwm->label); 248 249 goto out; 249 250 } ··· 253 254 254 255 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2); 255 256 if (ret < 0) 256 - dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label); 257 + dev_err(pwmchip_parent(chip), "%s: Failed to disable PWM\n", pwm->label); 257 258 258 259 out: 259 260 mutex_unlock(&twl->mutex); ··· 294 295 mutex_lock(&twl->mutex); 295 296 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2); 296 297 if (ret < 0) { 297 - dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n", 298 + dev_err(pwmchip_parent(chip), "%s: Failed to read PWM_CTRL2\n", 298 299 pwm->label); 299 300 goto out; 300 301 } ··· 304 305 305 306 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2); 306 307 if (ret < 0) 307 - dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label); 308 + dev_err(pwmchip_parent(chip), "%s: Failed to request PWM\n", pwm->label); 308 309 309 310 out: 310 311 mutex_unlock(&twl->mutex); ··· 320 321 mutex_lock(&twl->mutex); 321 322 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2); 322 323 if (ret < 0) { 323 - dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n", 324 + dev_err(pwmchip_parent(chip), "%s: Failed to read PWM_CTRL2\n", 324 325 pwm->label); 325 326 goto out; 326 327 } ··· 330 331 331 332 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2); 332 333 if (ret < 0) 333 - dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label); 334 + dev_err(pwmchip_parent(chip), "%s: Failed to free PWM\n", pwm->label); 334 335 335 336 out: 336 337 mutex_unlock(&twl->mutex); ··· 344 345 345 346 static int twl_pwmled_probe(struct platform_device *pdev) 346 347 { 348 + struct pwm_chip *chip; 347 349 struct twl_pwmled_chip *twl; 348 - 349 - twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL); 350 - if (!twl) 351 - return -ENOMEM; 350 + unsigned int npwm; 351 + const struct pwm_ops *ops; 352 352 353 353 if (twl_class_is_4030()) { 354 - twl->chip.ops = &twl4030_pwmled_ops; 355 - twl->chip.npwm = 2; 354 + ops = &twl4030_pwmled_ops; 355 + npwm = 2; 356 356 } else { 357 - twl->chip.ops = &twl6030_pwmled_ops; 358 - twl->chip.npwm = 1; 357 + ops = &twl6030_pwmled_ops; 358 + npwm = 1; 359 359 } 360 360 361 - twl->chip.dev = &pdev->dev; 361 + chip = devm_pwmchip_alloc(&pdev->dev, npwm, sizeof(*twl)); 362 + if (IS_ERR(chip)) 363 + return PTR_ERR(chip); 364 + twl = to_twl(chip); 365 + 366 + chip->ops = ops; 362 367 363 368 mutex_init(&twl->mutex); 364 369 365 - return devm_pwmchip_add(&pdev->dev, &twl->chip); 370 + return devm_pwmchip_add(&pdev->dev, chip); 366 371 } 367 372 368 373 #ifdef CONFIG_OF
+24 -26
drivers/pwm/pwm-twl.c
··· 46 46 #define TWL6030_PWM_TOGGLE(pwm, x) ((x) << (pwm * 3)) 47 47 48 48 struct twl_pwm_chip { 49 - struct pwm_chip chip; 50 49 struct mutex mutex; 51 50 u8 twl6030_toggle3; 52 51 u8 twl4030_pwm_mux; ··· 53 54 54 55 static inline struct twl_pwm_chip *to_twl(struct pwm_chip *chip) 55 56 { 56 - return container_of(chip, struct twl_pwm_chip, chip); 57 + return pwmchip_get_drvdata(chip); 57 58 } 58 59 59 60 static int twl_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, ··· 85 86 86 87 ret = twl_i2c_write(TWL_MODULE_PWM, pwm_config, base, 2); 87 88 if (ret < 0) 88 - dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label); 89 + dev_err(pwmchip_parent(chip), "%s: Failed to configure PWM\n", pwm->label); 89 90 90 91 return ret; 91 92 } ··· 99 100 mutex_lock(&twl->mutex); 100 101 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_GPBR1_REG); 101 102 if (ret < 0) { 102 - dev_err(chip->dev, "%s: Failed to read GPBR1\n", pwm->label); 103 + dev_err(pwmchip_parent(chip), "%s: Failed to read GPBR1\n", pwm->label); 103 104 goto out; 104 105 } 105 106 ··· 107 108 108 109 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG); 109 110 if (ret < 0) 110 - dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label); 111 + dev_err(pwmchip_parent(chip), "%s: Failed to enable PWM\n", pwm->label); 111 112 112 113 val |= TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMX_ENABLE); 113 114 114 115 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG); 115 116 if (ret < 0) 116 - dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label); 117 + dev_err(pwmchip_parent(chip), "%s: Failed to enable PWM\n", pwm->label); 117 118 118 119 out: 119 120 mutex_unlock(&twl->mutex); ··· 129 130 mutex_lock(&twl->mutex); 130 131 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_GPBR1_REG); 131 132 if (ret < 0) { 132 - dev_err(chip->dev, "%s: Failed to read GPBR1\n", pwm->label); 133 + dev_err(pwmchip_parent(chip), "%s: Failed to read GPBR1\n", pwm->label); 133 134 goto out; 134 135 } 135 136 ··· 137 138 138 139 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG); 139 140 if (ret < 0) 140 - dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label); 141 + dev_err(pwmchip_parent(chip), "%s: Failed to disable PWM\n", pwm->label); 141 142 142 143 val &= ~TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMXCLK_ENABLE); 143 144 144 145 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG); 145 146 if (ret < 0) 146 - dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label); 147 + dev_err(pwmchip_parent(chip), "%s: Failed to disable PWM\n", pwm->label); 147 148 148 149 out: 149 150 mutex_unlock(&twl->mutex); ··· 166 167 mutex_lock(&twl->mutex); 167 168 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_PMBR1_REG); 168 169 if (ret < 0) { 169 - dev_err(chip->dev, "%s: Failed to read PMBR1\n", pwm->label); 170 + dev_err(pwmchip_parent(chip), "%s: Failed to read PMBR1\n", pwm->label); 170 171 goto out; 171 172 } 172 173 ··· 180 181 181 182 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_PMBR1_REG); 182 183 if (ret < 0) 183 - dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label); 184 + dev_err(pwmchip_parent(chip), "%s: Failed to request PWM\n", pwm->label); 184 185 185 186 out: 186 187 mutex_unlock(&twl->mutex); ··· 201 202 mutex_lock(&twl->mutex); 202 203 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_PMBR1_REG); 203 204 if (ret < 0) { 204 - dev_err(chip->dev, "%s: Failed to read PMBR1\n", pwm->label); 205 + dev_err(pwmchip_parent(chip), "%s: Failed to read PMBR1\n", pwm->label); 205 206 goto out; 206 207 } 207 208 ··· 211 212 212 213 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_PMBR1_REG); 213 214 if (ret < 0) 214 - dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label); 215 + dev_err(pwmchip_parent(chip), "%s: Failed to free PWM\n", pwm->label); 215 216 216 217 out: 217 218 mutex_unlock(&twl->mutex); ··· 230 231 231 232 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG); 232 233 if (ret < 0) { 233 - dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label); 234 + dev_err(pwmchip_parent(chip), "%s: Failed to enable PWM\n", pwm->label); 234 235 goto out; 235 236 } 236 237 ··· 253 254 254 255 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG); 255 256 if (ret < 0) { 256 - dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label); 257 + dev_err(pwmchip_parent(chip), "%s: Failed to disable PWM\n", pwm->label); 257 258 goto out; 258 259 } 259 260 ··· 261 262 262 263 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG); 263 264 if (ret < 0) { 264 - dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label); 265 + dev_err(pwmchip_parent(chip), "%s: Failed to disable PWM\n", pwm->label); 265 266 goto out; 266 267 } 267 268 ··· 269 270 270 271 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG); 271 272 if (ret < 0) { 272 - dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label); 273 + dev_err(pwmchip_parent(chip), "%s: Failed to disable PWM\n", pwm->label); 273 274 goto out; 274 275 } 275 276 ··· 340 341 341 342 static int twl_pwm_probe(struct platform_device *pdev) 342 343 { 344 + struct pwm_chip *chip; 343 345 struct twl_pwm_chip *twl; 344 346 345 - twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL); 346 - if (!twl) 347 - return -ENOMEM; 347 + chip = devm_pwmchip_alloc(&pdev->dev, 2, sizeof(*twl)); 348 + if (IS_ERR(chip)) 349 + return PTR_ERR(chip); 350 + twl = to_twl(chip); 348 351 349 352 if (twl_class_is_4030()) 350 - twl->chip.ops = &twl4030_pwm_ops; 353 + chip->ops = &twl4030_pwm_ops; 351 354 else 352 - twl->chip.ops = &twl6030_pwm_ops; 353 - 354 - twl->chip.dev = &pdev->dev; 355 - twl->chip.npwm = 2; 355 + chip->ops = &twl6030_pwm_ops; 356 356 357 357 mutex_init(&twl->mutex); 358 358 359 - return devm_pwmchip_add(&pdev->dev, &twl->chip); 359 + return devm_pwmchip_add(&pdev->dev, chip); 360 360 } 361 361 362 362 #ifdef CONFIG_OF
+8 -9
drivers/pwm/pwm-visconti.c
··· 34 34 #define PIPGM_PWMC_POLARITY_MASK GENMASK(5, 5) 35 35 36 36 struct visconti_pwm_chip { 37 - struct pwm_chip chip; 38 37 void __iomem *base; 39 38 }; 40 39 41 40 static inline struct visconti_pwm_chip *visconti_pwm_from_chip(struct pwm_chip *chip) 42 41 { 43 - return container_of(chip, struct visconti_pwm_chip, chip); 42 + return pwmchip_get_drvdata(chip); 44 43 } 45 44 46 45 static int visconti_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, ··· 133 134 static int visconti_pwm_probe(struct platform_device *pdev) 134 135 { 135 136 struct device *dev = &pdev->dev; 137 + struct pwm_chip *chip; 136 138 struct visconti_pwm_chip *priv; 137 139 int ret; 138 140 139 - priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 140 - if (!priv) 141 - return -ENOMEM; 141 + chip = devm_pwmchip_alloc(dev, 4, sizeof(*priv)); 142 + if (IS_ERR(chip)) 143 + return PTR_ERR(chip); 144 + priv = visconti_pwm_from_chip(chip); 142 145 143 146 priv->base = devm_platform_ioremap_resource(pdev, 0); 144 147 if (IS_ERR(priv->base)) 145 148 return PTR_ERR(priv->base); 146 149 147 - priv->chip.dev = dev; 148 - priv->chip.ops = &visconti_pwm_ops; 149 - priv->chip.npwm = 4; 150 + chip->ops = &visconti_pwm_ops; 150 151 151 - ret = devm_pwmchip_add(&pdev->dev, &priv->chip); 152 + ret = devm_pwmchip_add(&pdev->dev, chip); 152 153 if (ret < 0) 153 154 return dev_err_probe(&pdev->dev, ret, "Cannot register visconti PWM\n"); 154 155
+23 -20
drivers/pwm/pwm-vt8500.c
··· 45 45 #define STATUS_ALL_UPDATE 0x0F 46 46 47 47 struct vt8500_chip { 48 - struct pwm_chip chip; 49 48 void __iomem *base; 50 49 struct clk *clk; 51 50 }; 52 51 53 - #define to_vt8500_chip(chip) container_of(chip, struct vt8500_chip, chip) 52 + static inline struct vt8500_chip *to_vt8500_chip(struct pwm_chip *chip) 53 + { 54 + return pwmchip_get_drvdata(chip); 55 + } 54 56 55 57 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t) 56 - static inline void vt8500_pwm_busy_wait(struct vt8500_chip *vt8500, int nr, u8 bitmask) 58 + static inline void vt8500_pwm_busy_wait(struct pwm_chip *chip, int nr, u8 bitmask) 57 59 { 60 + struct vt8500_chip *vt8500 = to_vt8500_chip(chip); 58 61 int loops = msecs_to_loops(10); 59 62 u32 mask = bitmask << (nr << 8); 60 63 ··· 65 62 cpu_relax(); 66 63 67 64 if (unlikely(!loops)) 68 - dev_warn(vt8500->chip.dev, "Waiting for status bits 0x%x to clear timed out\n", 65 + dev_warn(pwmchip_parent(chip), "Waiting for status bits 0x%x to clear timed out\n", 69 66 mask); 70 67 } 71 68 ··· 80 77 81 78 err = clk_enable(vt8500->clk); 82 79 if (err < 0) { 83 - dev_err(chip->dev, "failed to enable clock\n"); 80 + dev_err(pwmchip_parent(chip), "failed to enable clock\n"); 84 81 return err; 85 82 } 86 83 ··· 106 103 dc = div64_u64(c, period_ns); 107 104 108 105 writel(prescale, vt8500->base + REG_SCALAR(pwm->hwpwm)); 109 - vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_SCALAR_UPDATE); 106 + vt8500_pwm_busy_wait(chip, pwm->hwpwm, STATUS_SCALAR_UPDATE); 110 107 111 108 writel(pv, vt8500->base + REG_PERIOD(pwm->hwpwm)); 112 - vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_PERIOD_UPDATE); 109 + vt8500_pwm_busy_wait(chip, pwm->hwpwm, STATUS_PERIOD_UPDATE); 113 110 114 111 writel(dc, vt8500->base + REG_DUTY(pwm->hwpwm)); 115 - vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_DUTY_UPDATE); 112 + vt8500_pwm_busy_wait(chip, pwm->hwpwm, STATUS_DUTY_UPDATE); 116 113 117 114 val = readl(vt8500->base + REG_CTRL(pwm->hwpwm)); 118 115 val |= CTRL_AUTOLOAD; 119 116 writel(val, vt8500->base + REG_CTRL(pwm->hwpwm)); 120 - vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE); 117 + vt8500_pwm_busy_wait(chip, pwm->hwpwm, STATUS_CTRL_UPDATE); 121 118 122 119 clk_disable(vt8500->clk); 123 120 return 0; ··· 131 128 132 129 err = clk_enable(vt8500->clk); 133 130 if (err < 0) { 134 - dev_err(chip->dev, "failed to enable clock\n"); 131 + dev_err(pwmchip_parent(chip), "failed to enable clock\n"); 135 132 return err; 136 133 } 137 134 138 135 val = readl(vt8500->base + REG_CTRL(pwm->hwpwm)); 139 136 val |= CTRL_ENABLE; 140 137 writel(val, vt8500->base + REG_CTRL(pwm->hwpwm)); 141 - vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE); 138 + vt8500_pwm_busy_wait(chip, pwm->hwpwm, STATUS_CTRL_UPDATE); 142 139 143 140 return 0; 144 141 } ··· 151 148 val = readl(vt8500->base + REG_CTRL(pwm->hwpwm)); 152 149 val &= ~CTRL_ENABLE; 153 150 writel(val, vt8500->base + REG_CTRL(pwm->hwpwm)); 154 - vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE); 151 + vt8500_pwm_busy_wait(chip, pwm->hwpwm, STATUS_CTRL_UPDATE); 155 152 156 153 clk_disable(vt8500->clk); 157 154 } ··· 171 168 val &= ~CTRL_INVERT; 172 169 173 170 writel(val, vt8500->base + REG_CTRL(pwm->hwpwm)); 174 - vt8500_pwm_busy_wait(vt8500, pwm->hwpwm, STATUS_CTRL_UPDATE); 171 + vt8500_pwm_busy_wait(chip, pwm->hwpwm, STATUS_CTRL_UPDATE); 175 172 176 173 return 0; 177 174 } ··· 234 231 235 232 static int vt8500_pwm_probe(struct platform_device *pdev) 236 233 { 234 + struct pwm_chip *chip; 237 235 struct vt8500_chip *vt8500; 238 236 struct device_node *np = pdev->dev.of_node; 239 237 int ret; ··· 242 238 if (!np) 243 239 return dev_err_probe(&pdev->dev, -EINVAL, "invalid devicetree node\n"); 244 240 245 - vt8500 = devm_kzalloc(&pdev->dev, sizeof(*vt8500), GFP_KERNEL); 246 - if (vt8500 == NULL) 247 - return -ENOMEM; 241 + chip = devm_pwmchip_alloc(&pdev->dev, VT8500_NR_PWMS, sizeof(*vt8500)); 242 + if (IS_ERR(chip)) 243 + return PTR_ERR(chip); 244 + vt8500 = to_vt8500_chip(chip); 248 245 249 - vt8500->chip.dev = &pdev->dev; 250 - vt8500->chip.ops = &vt8500_pwm_ops; 251 - vt8500->chip.npwm = VT8500_NR_PWMS; 246 + chip->ops = &vt8500_pwm_ops; 252 247 253 248 vt8500->clk = devm_clk_get_prepared(&pdev->dev, NULL); 254 249 if (IS_ERR(vt8500->clk)) ··· 257 254 if (IS_ERR(vt8500->base)) 258 255 return PTR_ERR(vt8500->base); 259 256 260 - ret = devm_pwmchip_add(&pdev->dev, &vt8500->chip); 257 + ret = devm_pwmchip_add(&pdev->dev, chip); 261 258 if (ret < 0) 262 259 return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n"); 263 260
+14 -20
drivers/pwm/pwm-xilinx.c
··· 80 80 #define TCSR_PWM_CLEAR (TCSR_MDT | TCSR_LOAD) 81 81 #define TCSR_PWM_MASK (TCSR_PWM_SET | TCSR_PWM_CLEAR) 82 82 83 - struct xilinx_pwm_device { 84 - struct pwm_chip chip; 85 - struct xilinx_timer_priv priv; 86 - }; 87 - 88 83 static inline struct xilinx_timer_priv 89 84 *xilinx_pwm_chip_to_priv(struct pwm_chip *chip) 90 85 { 91 - return &container_of(chip, struct xilinx_pwm_device, chip)->priv; 86 + return pwmchip_get_drvdata(chip); 92 87 } 93 88 94 89 static bool xilinx_timer_pwm_enabled(u32 tcsr0, u32 tcsr1) ··· 209 214 struct device *dev = &pdev->dev; 210 215 struct device_node *np = dev->of_node; 211 216 struct xilinx_timer_priv *priv; 212 - struct xilinx_pwm_device *xilinx_pwm; 217 + struct pwm_chip *chip; 213 218 u32 pwm_cells, one_timer, width; 214 219 void __iomem *regs; 215 220 ··· 220 225 if (ret) 221 226 return dev_err_probe(dev, ret, "could not read #pwm-cells\n"); 222 227 223 - xilinx_pwm = devm_kzalloc(dev, sizeof(*xilinx_pwm), GFP_KERNEL); 224 - if (!xilinx_pwm) 225 - return -ENOMEM; 226 - platform_set_drvdata(pdev, xilinx_pwm); 227 - priv = &xilinx_pwm->priv; 228 + chip = devm_pwmchip_alloc(dev, 1, sizeof(*priv)); 229 + if (IS_ERR(chip)) 230 + return PTR_ERR(chip); 231 + priv = xilinx_pwm_chip_to_priv(chip); 232 + platform_set_drvdata(pdev, chip); 228 233 229 234 regs = devm_platform_ioremap_resource(pdev, 0); 230 235 if (IS_ERR(regs)) ··· 273 278 return dev_err_probe(dev, ret, "Clock enable failed\n"); 274 279 clk_rate_exclusive_get(priv->clk); 275 280 276 - xilinx_pwm->chip.dev = dev; 277 - xilinx_pwm->chip.ops = &xilinx_pwm_ops; 278 - xilinx_pwm->chip.npwm = 1; 279 - ret = pwmchip_add(&xilinx_pwm->chip); 281 + chip->ops = &xilinx_pwm_ops; 282 + ret = pwmchip_add(chip); 280 283 if (ret) { 281 284 clk_rate_exclusive_put(priv->clk); 282 285 clk_disable_unprepare(priv->clk); ··· 286 293 287 294 static void xilinx_pwm_remove(struct platform_device *pdev) 288 295 { 289 - struct xilinx_pwm_device *xilinx_pwm = platform_get_drvdata(pdev); 296 + struct pwm_chip *chip = platform_get_drvdata(pdev); 297 + struct xilinx_timer_priv *priv = xilinx_pwm_chip_to_priv(chip); 290 298 291 - pwmchip_remove(&xilinx_pwm->chip); 292 - clk_rate_exclusive_put(xilinx_pwm->priv.clk); 293 - clk_disable_unprepare(xilinx_pwm->priv.clk); 299 + pwmchip_remove(chip); 300 + clk_rate_exclusive_put(priv->clk); 301 + clk_disable_unprepare(priv->clk); 294 302 } 295 303 296 304 static const struct of_device_id xilinx_pwm_of_match[] = {
+2 -2
drivers/pwm/sysfs.c
··· 509 509 * If device_create() fails the pwm_chip is still usable by 510 510 * the kernel it's just not exported. 511 511 */ 512 - parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip, 512 + parent = device_create(&pwm_class, pwmchip_parent(chip), MKDEV(0, 0), chip, 513 513 "pwmchip%d", chip->id); 514 514 if (IS_ERR(parent)) { 515 - dev_warn(chip->dev, 515 + dev_warn(pwmchip_parent(chip), 516 516 "device_create failed for pwm_chip sysfs export\n"); 517 517 } 518 518 }
+56 -77
drivers/staging/greybus/pwm.c
··· 16 16 17 17 struct gb_pwm_chip { 18 18 struct gb_connection *connection; 19 - u8 pwm_max; /* max pwm number */ 20 - 21 19 struct pwm_chip chip; 22 20 }; 23 21 ··· 24 26 return container_of(chip, struct gb_pwm_chip, chip); 25 27 } 26 28 27 - static int gb_pwm_count_operation(struct gb_pwm_chip *pwmc) 29 + static int gb_pwm_get_npwm(struct gb_connection *connection) 28 30 { 29 31 struct gb_pwm_count_response response; 30 32 int ret; 31 33 32 - ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_PWM_COUNT, 34 + ret = gb_operation_sync(connection, GB_PWM_TYPE_PWM_COUNT, 33 35 NULL, 0, &response, sizeof(response)); 34 36 if (ret) 35 37 return ret; 36 - pwmc->pwm_max = response.count; 37 - return 0; 38 + 39 + /* 40 + * The request returns the highest allowed PWM id parameter. So add one 41 + * to get the number of PWMs. 42 + */ 43 + return response.count + 1; 38 44 } 39 45 40 - static int gb_pwm_activate_operation(struct gb_pwm_chip *pwmc, 41 - u8 which) 46 + static int gb_pwm_activate_operation(struct pwm_chip *chip, u8 which) 42 47 { 48 + struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); 43 49 struct gb_pwm_activate_request request; 44 50 struct gbphy_device *gbphy_dev; 45 51 int ret; 46 52 47 - if (which > pwmc->pwm_max) 48 - return -EINVAL; 49 - 50 53 request.which = which; 51 54 52 - gbphy_dev = to_gbphy_dev(pwmc->chip.dev); 55 + gbphy_dev = to_gbphy_dev(pwmchip_parent(chip)); 53 56 ret = gbphy_runtime_get_sync(gbphy_dev); 54 57 if (ret) 55 58 return ret; ··· 63 64 return ret; 64 65 } 65 66 66 - static int gb_pwm_deactivate_operation(struct gb_pwm_chip *pwmc, 67 - u8 which) 67 + static int gb_pwm_deactivate_operation(struct pwm_chip *chip, u8 which) 68 68 { 69 + struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); 69 70 struct gb_pwm_deactivate_request request; 70 71 struct gbphy_device *gbphy_dev; 71 72 int ret; 72 73 73 - if (which > pwmc->pwm_max) 74 - return -EINVAL; 75 - 76 74 request.which = which; 77 75 78 - gbphy_dev = to_gbphy_dev(pwmc->chip.dev); 76 + gbphy_dev = to_gbphy_dev(pwmchip_parent(chip)); 79 77 ret = gbphy_runtime_get_sync(gbphy_dev); 80 78 if (ret) 81 79 return ret; ··· 85 89 return ret; 86 90 } 87 91 88 - static int gb_pwm_config_operation(struct gb_pwm_chip *pwmc, 92 + static int gb_pwm_config_operation(struct pwm_chip *chip, 89 93 u8 which, u32 duty, u32 period) 90 94 { 95 + struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); 91 96 struct gb_pwm_config_request request; 92 97 struct gbphy_device *gbphy_dev; 93 98 int ret; 94 - 95 - if (which > pwmc->pwm_max) 96 - return -EINVAL; 97 99 98 100 request.which = which; 99 101 request.duty = cpu_to_le32(duty); 100 102 request.period = cpu_to_le32(period); 101 103 102 - gbphy_dev = to_gbphy_dev(pwmc->chip.dev); 104 + gbphy_dev = to_gbphy_dev(pwmchip_parent(chip)); 103 105 ret = gbphy_runtime_get_sync(gbphy_dev); 104 106 if (ret) 105 107 return ret; ··· 110 116 return ret; 111 117 } 112 118 113 - static int gb_pwm_set_polarity_operation(struct gb_pwm_chip *pwmc, 119 + static int gb_pwm_set_polarity_operation(struct pwm_chip *chip, 114 120 u8 which, u8 polarity) 115 121 { 122 + struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); 116 123 struct gb_pwm_polarity_request request; 117 124 struct gbphy_device *gbphy_dev; 118 125 int ret; 119 126 120 - if (which > pwmc->pwm_max) 121 - return -EINVAL; 122 - 123 127 request.which = which; 124 128 request.polarity = polarity; 125 129 126 - gbphy_dev = to_gbphy_dev(pwmc->chip.dev); 130 + gbphy_dev = to_gbphy_dev(pwmchip_parent(chip)); 127 131 ret = gbphy_runtime_get_sync(gbphy_dev); 128 132 if (ret) 129 133 return ret; ··· 134 142 return ret; 135 143 } 136 144 137 - static int gb_pwm_enable_operation(struct gb_pwm_chip *pwmc, 138 - u8 which) 145 + static int gb_pwm_enable_operation(struct pwm_chip *chip, u8 which) 139 146 { 147 + struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); 140 148 struct gb_pwm_enable_request request; 141 149 struct gbphy_device *gbphy_dev; 142 150 int ret; 143 151 144 - if (which > pwmc->pwm_max) 145 - return -EINVAL; 146 - 147 152 request.which = which; 148 153 149 - gbphy_dev = to_gbphy_dev(pwmc->chip.dev); 154 + gbphy_dev = to_gbphy_dev(pwmchip_parent(chip)); 150 155 ret = gbphy_runtime_get_sync(gbphy_dev); 151 156 if (ret) 152 157 return ret; ··· 156 167 return ret; 157 168 } 158 169 159 - static int gb_pwm_disable_operation(struct gb_pwm_chip *pwmc, 160 - u8 which) 170 + static int gb_pwm_disable_operation(struct pwm_chip *chip, u8 which) 161 171 { 172 + struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); 162 173 struct gb_pwm_disable_request request; 163 174 struct gbphy_device *gbphy_dev; 164 175 int ret; 165 - 166 - if (which > pwmc->pwm_max) 167 - return -EINVAL; 168 176 169 177 request.which = which; 170 178 171 179 ret = gb_operation_sync(pwmc->connection, GB_PWM_TYPE_DISABLE, 172 180 &request, sizeof(request), NULL, 0); 173 181 174 - gbphy_dev = to_gbphy_dev(pwmc->chip.dev); 182 + gbphy_dev = to_gbphy_dev(pwmchip_parent(chip)); 175 183 gbphy_runtime_put_autosuspend(gbphy_dev); 176 184 177 185 return ret; ··· 176 190 177 191 static int gb_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) 178 192 { 179 - struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); 180 - 181 - return gb_pwm_activate_operation(pwmc, pwm->hwpwm); 193 + return gb_pwm_activate_operation(chip, pwm->hwpwm); 182 194 }; 183 195 184 196 static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) 185 197 { 186 - struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); 187 - 188 198 if (pwm_is_enabled(pwm)) 189 - dev_warn(chip->dev, "freeing PWM device without disabling\n"); 199 + dev_warn(pwmchip_parent(chip), "freeing PWM device without disabling\n"); 190 200 191 - gb_pwm_deactivate_operation(pwmc, pwm->hwpwm); 201 + gb_pwm_deactivate_operation(chip, pwm->hwpwm); 192 202 } 193 203 194 204 static int gb_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, ··· 194 212 bool enabled = pwm->state.enabled; 195 213 u64 period = state->period; 196 214 u64 duty_cycle = state->duty_cycle; 197 - struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); 198 215 199 216 /* Set polarity */ 200 217 if (state->polarity != pwm->state.polarity) { 201 218 if (enabled) { 202 - gb_pwm_disable_operation(pwmc, pwm->hwpwm); 219 + gb_pwm_disable_operation(chip, pwm->hwpwm); 203 220 enabled = false; 204 221 } 205 - err = gb_pwm_set_polarity_operation(pwmc, pwm->hwpwm, state->polarity); 222 + err = gb_pwm_set_polarity_operation(chip, pwm->hwpwm, state->polarity); 206 223 if (err) 207 224 return err; 208 225 } 209 226 210 227 if (!state->enabled) { 211 228 if (enabled) 212 - gb_pwm_disable_operation(pwmc, pwm->hwpwm); 229 + gb_pwm_disable_operation(chip, pwm->hwpwm); 213 230 return 0; 214 231 } 215 232 ··· 224 243 if (duty_cycle > period) 225 244 duty_cycle = period; 226 245 227 - err = gb_pwm_config_operation(pwmc, pwm->hwpwm, duty_cycle, period); 246 + err = gb_pwm_config_operation(chip, pwm->hwpwm, duty_cycle, period); 228 247 if (err) 229 248 return err; 230 249 231 250 /* enable/disable */ 232 251 if (!enabled) 233 - return gb_pwm_enable_operation(pwmc, pwm->hwpwm); 252 + return gb_pwm_enable_operation(chip, pwm->hwpwm); 234 253 235 254 return 0; 236 255 } ··· 247 266 struct gb_connection *connection; 248 267 struct gb_pwm_chip *pwmc; 249 268 struct pwm_chip *chip; 250 - int ret; 251 - 252 - pwmc = kzalloc(sizeof(*pwmc), GFP_KERNEL); 253 - if (!pwmc) 254 - return -ENOMEM; 269 + int ret, npwm; 255 270 256 271 connection = gb_connection_create(gbphy_dev->bundle, 257 272 le16_to_cpu(gbphy_dev->cport_desc->id), 258 273 NULL); 259 - if (IS_ERR(connection)) { 260 - ret = PTR_ERR(connection); 261 - goto exit_pwmc_free; 262 - } 263 - 264 - pwmc->connection = connection; 265 - gb_connection_set_data(connection, pwmc); 266 - gb_gbphy_set_data(gbphy_dev, pwmc); 274 + if (IS_ERR(connection)) 275 + return PTR_ERR(connection); 267 276 268 277 ret = gb_connection_enable(connection); 269 278 if (ret) 270 279 goto exit_connection_destroy; 271 280 272 281 /* Query number of pwms present */ 273 - ret = gb_pwm_count_operation(pwmc); 274 - if (ret) 282 + ret = gb_pwm_get_npwm(connection); 283 + if (ret < 0) 275 284 goto exit_connection_disable; 285 + npwm = ret; 276 286 277 - chip = &pwmc->chip; 287 + chip = pwmchip_alloc(&gbphy_dev->dev, npwm, sizeof(*pwmc)); 288 + if (IS_ERR(chip)) { 289 + ret = PTR_ERR(chip); 290 + goto exit_connection_disable; 291 + } 292 + gb_gbphy_set_data(gbphy_dev, chip); 278 293 279 - chip->dev = &gbphy_dev->dev; 294 + pwmc = pwm_chip_to_gb_pwm_chip(chip); 295 + pwmc->connection = connection; 296 + 280 297 chip->ops = &gb_pwm_ops; 281 - chip->npwm = pwmc->pwm_max + 1; 282 298 283 299 ret = pwmchip_add(chip); 284 300 if (ret) { 285 301 dev_err(&gbphy_dev->dev, 286 302 "failed to register PWM: %d\n", ret); 287 - goto exit_connection_disable; 303 + goto exit_pwmchip_put; 288 304 } 289 305 290 306 gbphy_runtime_put_autosuspend(gbphy_dev); 291 307 return 0; 292 308 309 + exit_pwmchip_put: 310 + pwmchip_put(chip); 293 311 exit_connection_disable: 294 312 gb_connection_disable(connection); 295 313 exit_connection_destroy: 296 314 gb_connection_destroy(connection); 297 - exit_pwmc_free: 298 - kfree(pwmc); 299 315 return ret; 300 316 } 301 317 302 318 static void gb_pwm_remove(struct gbphy_device *gbphy_dev) 303 319 { 304 - struct gb_pwm_chip *pwmc = gb_gbphy_get_data(gbphy_dev); 320 + struct pwm_chip *chip = gb_gbphy_get_data(gbphy_dev); 321 + struct gb_pwm_chip *pwmc = pwm_chip_to_gb_pwm_chip(chip); 305 322 struct gb_connection *connection = pwmc->connection; 306 323 int ret; 307 324 ··· 307 328 if (ret) 308 329 gbphy_runtime_get_noresume(gbphy_dev); 309 330 310 - pwmchip_remove(&pwmc->chip); 331 + pwmchip_remove(chip); 332 + pwmchip_put(chip); 311 333 gb_connection_disable(connection); 312 334 gb_connection_destroy(connection); 313 - kfree(pwmc); 314 335 } 315 336 316 337 static const struct gbphy_device_id gb_pwm_id_table[] = {
+2 -2
include/linux/platform_data/x86/pwm-lpss.h
··· 27 27 bool other_devices_aml_touches_pwm_regs; 28 28 }; 29 29 30 - struct pwm_lpss_chip *devm_pwm_lpss_probe(struct device *dev, void __iomem *base, 31 - const struct pwm_lpss_boardinfo *info); 30 + struct pwm_chip *devm_pwm_lpss_probe(struct device *dev, void __iomem *base, 31 + const struct pwm_lpss_boardinfo *info); 32 32 33 33 #endif /* __PLATFORM_DATA_X86_PWM_LPSS_H */
+47 -2
include/linux/pwm.h
··· 271 271 * @id: unique number of this PWM chip 272 272 * @npwm: number of PWMs controlled by this chip 273 273 * @of_xlate: request a PWM device given a device tree PWM specifier 274 - * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier 275 274 * @atomic: can the driver's ->apply() be called in atomic context 275 + * @driver_data: Private pointer for driver specific info 276 276 * @pwms: array of PWM devices allocated by the framework 277 277 */ 278 278 struct pwm_chip { ··· 284 284 285 285 struct pwm_device * (*of_xlate)(struct pwm_chip *chip, 286 286 const struct of_phandle_args *args); 287 - unsigned int of_pwm_n_cells; 288 287 bool atomic; 289 288 290 289 /* only used internally by the PWM framework */ 290 + void *driver_data; 291 291 struct pwm_device *pwms; 292 292 }; 293 + 294 + static inline struct device *pwmchip_parent(const struct pwm_chip *chip) 295 + { 296 + return chip->dev; 297 + } 298 + 299 + static inline void *pwmchip_get_drvdata(struct pwm_chip *chip) 300 + { 301 + /* 302 + * After pwm_chip got a dedicated struct device, this can be replaced by 303 + * dev_get_drvdata(&chip->dev); 304 + */ 305 + return chip->driver_data; 306 + } 307 + 308 + static inline void pwmchip_set_drvdata(struct pwm_chip *chip, void *data) 309 + { 310 + /* 311 + * After pwm_chip got a dedicated struct device, this can be replaced by 312 + * dev_set_drvdata(&chip->dev, data); 313 + */ 314 + chip->driver_data = data; 315 + } 293 316 294 317 #if IS_ENABLED(CONFIG_PWM) 295 318 /* PWM user APIs */ ··· 403 380 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result, 404 381 unsigned long timeout); 405 382 383 + void pwmchip_put(struct pwm_chip *chip); 384 + struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv); 385 + struct pwm_chip *devm_pwmchip_alloc(struct device *parent, unsigned int npwm, size_t sizeof_priv); 386 + 406 387 int __pwmchip_add(struct pwm_chip *chip, struct module *owner); 407 388 #define pwmchip_add(chip) __pwmchip_add(chip, THIS_MODULE) 408 389 void pwmchip_remove(struct pwm_chip *chip); ··· 477 450 unsigned long timeout) 478 451 { 479 452 return -EINVAL; 453 + } 454 + 455 + static inline void pwmchip_put(struct pwm_chip *chip) 456 + { 457 + } 458 + 459 + static inline struct pwm_chip *pwmchip_alloc(struct device *parent, 460 + unsigned int npwm, 461 + size_t sizeof_priv) 462 + { 463 + return ERR_PTR(-EINVAL); 464 + } 465 + 466 + static inline struct pwm_chip *devm_pwmchip_alloc(struct device *parent, 467 + unsigned int npwm, 468 + size_t sizeof_priv) 469 + { 470 + return pwmchip_alloc(parent, npwm, sizeof_priv); 480 471 } 481 472 482 473 static inline int pwmchip_add(struct pwm_chip *chip)