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

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

Pull pwm updates from Thierry Reding:
"This set of changes contains a new driver for SiFive SoCs as well as
enhancements to the core (device links are used to track dependencies
between PWM providers and consumers, support for PWM controllers via
ACPI, sysfs will now suspend/resume PWMs that it has claimed) and
various existing drivers"

* tag 'pwm/for-5.3-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry.reding/linux-pwm: (37 commits)
pwm: fsl-ftm: Make sure to unlock mutex on failure
pwm: fsl-ftm: Use write protection for prescaler & polarity
pwm: fsl-ftm: More relaxed permissions for updating period
pwm: atmel-hlcdc: Add compatible for SAM9X60 HLCDC's PWM
pwm: bcm2835: Improve precision of PWM
leds: pwm: Support ACPI via firmware-node framework
pwm: Add support referencing PWMs from ACPI
pwm: rcar: Remove suspend/resume support
pwm: sysfs: Add suspend/resume support
pwm: Add power management descriptions
pwm: meson: Add documentation to the driver
pwm: meson: Add support PWM_POLARITY_INVERSED when disabling
pwm: meson: Don't cache struct pwm_state internally
pwm: meson: Read the full hardware state in meson_pwm_get_state()
pwm: meson: Simplify the calculation of the pre-divider and count
pwm: meson: Move pwm_set_chip_data() to meson_pwm_request()
pwm: meson: Add the per-channel register offsets and bits in a struct
pwm: meson: Add the meson_pwm_channel data to struct meson_pwm
pwm: meson: Pass struct pwm_device to meson_pwm_calc()
pwm: meson: Don't duplicate the polarity internally
...

+1156 -484
+1 -4
Documentation/devicetree/bindings/pwm/ingenic,jz47xx-pwm.txt
··· 2 2 ============================= 3 3 4 4 Required properties: 5 - - compatible: One of: 6 - * "ingenic,jz4740-pwm" 7 - * "ingenic,jz4770-pwm" 8 - * "ingenic,jz4780-pwm" 5 + - compatible: Should be "ingenic,jz4740-pwm" 9 6 - #pwm-cells: Should be 3. See pwm.txt in this directory for a description 10 7 of the cells format. 11 8 - clocks : phandle to the external clock.
+33
Documentation/devicetree/bindings/pwm/pwm-sifive.txt
··· 1 + SiFive PWM controller 2 + 3 + Unlike most other PWM controllers, the SiFive PWM controller currently only 4 + supports one period for all channels in the PWM. All PWMs need to run at 5 + the same period. The period also has significant restrictions on the values 6 + it can achieve, which the driver rounds to the nearest achievable period. 7 + PWM RTL that corresponds to the IP block version numbers can be found 8 + here: 9 + 10 + https://github.com/sifive/sifive-blocks/tree/master/src/main/scala/devices/pwm 11 + 12 + Required properties: 13 + - compatible: Should be "sifive,<chip>-pwm" and "sifive,pwm<version>". 14 + Supported compatible strings are: "sifive,fu540-c000-pwm" for the SiFive 15 + PWM v0 as integrated onto the SiFive FU540 chip, and "sifive,pwm0" for the 16 + SiFive PWM v0 IP block with no chip integration tweaks. 17 + Please refer to sifive-blocks-ip-versioning.txt for details. 18 + - reg: physical base address and length of the controller's registers 19 + - clocks: Should contain a clock identifier for the PWM's parent clock. 20 + - #pwm-cells: Should be 3. See pwm.txt in this directory 21 + for a description of the cell format. 22 + - interrupts: one interrupt per PWM channel 23 + 24 + Examples: 25 + 26 + pwm: pwm@10020000 { 27 + compatible = "sifive,fu540-c000-pwm", "sifive,pwm0"; 28 + reg = <0x0 0x10020000 0x0 0x1000>; 29 + clocks = <&tlclk>; 30 + interrupt-parent = <&plic>; 31 + interrupts = <42 43 44 45>; 32 + #pwm-cells = <3>; 33 + };
+6 -3
Documentation/devicetree/bindings/pwm/pwm-stm32-lp.txt
··· 11 11 bindings defined in pwm.txt. 12 12 13 13 Optional properties: 14 - - pinctrl-names: Set to "default". 15 - - pinctrl-0: Phandle pointing to pin configuration node for PWM. 14 + - pinctrl-names: Set to "default". An additional "sleep" state can be 15 + defined to set pins in sleep state when in low power. 16 + - pinctrl-n: Phandle(s) pointing to pin configuration node for PWM, 17 + respectively for "default" and "sleep" states. 16 18 17 19 Example: 18 20 timer@40002400 { ··· 23 21 pwm { 24 22 compatible = "st,stm32-pwm-lp"; 25 23 #pwm-cells = <3>; 26 - pinctrl-names = "default"; 24 + pinctrl-names = "default", "sleep"; 27 25 pinctrl-0 = <&lppwm1_pins>; 26 + pinctrl-1 = <&lppwm1_sleep_pins>; 28 27 }; 29 28 };
+3
Documentation/devicetree/bindings/pwm/pwm-stm32.txt
··· 8 8 - pinctrl-names: Set to "default". 9 9 - pinctrl-0: List of phandles pointing to pin configuration nodes for PWM module. 10 10 For Pinctrl properties see ../pinctrl/pinctrl-bindings.txt 11 + - #pwm-cells: Should be set to 3. This PWM chip uses the default 3 cells 12 + bindings defined in pwm.txt. 11 13 12 14 Optional parameters: 13 15 - st,breakinput: One or two <index level filter> to describe break input configurations. ··· 30 28 31 29 pwm { 32 30 compatible = "st,stm32-pwm"; 31 + #pwm-cells = <3>; 33 32 pinctrl-0 = <&pwm1_pins>; 34 33 pinctrl-names = "default"; 35 34 st,breakinput = <0 1 5>;
+7
Documentation/pwm.txt
··· 65 65 be used to set the initial PWM config (usually done in the probe function 66 66 of the PWM user). PWM arguments are retrieved with pwm_get_args(). 67 67 68 + All consumers should really be reconfiguring the PWM upon resume as 69 + appropriate. This is the only way to ensure that everything is resumed in 70 + the proper order. 71 + 68 72 Using PWMs with the sysfs interface 69 73 ----------------------------------- 70 74 ··· 144 140 The implementation of ->get_state() (a method used to retrieve initial PWM 145 141 state) is also encouraged for the same reason: letting the PWM user know 146 142 about the current PWM state would allow him to avoid glitches. 143 + 144 + Drivers should not implement any power management. In other words, 145 + consumers should implement it as described in the "Using PWMs" section. 147 146 148 147 Locking 149 148 -------
+27 -18
drivers/leds/leds-pwm.c
··· 72 72 } 73 73 74 74 static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv, 75 - struct led_pwm *led, struct device_node *child) 75 + struct led_pwm *led, struct fwnode_handle *fwnode) 76 76 { 77 77 struct led_pwm_data *led_data = &priv->leds[priv->num_leds]; 78 78 struct pwm_args pargs; ··· 85 85 led_data->cdev.max_brightness = led->max_brightness; 86 86 led_data->cdev.flags = LED_CORE_SUSPENDRESUME; 87 87 88 - if (child) 89 - led_data->pwm = devm_of_pwm_get(dev, child, NULL); 88 + if (fwnode) 89 + led_data->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL); 90 90 else 91 91 led_data->pwm = devm_pwm_get(dev, led->name); 92 92 if (IS_ERR(led_data->pwm)) { ··· 111 111 if (!led_data->period && (led->pwm_period_ns > 0)) 112 112 led_data->period = led->pwm_period_ns; 113 113 114 - ret = devm_of_led_classdev_register(dev, child, &led_data->cdev); 114 + ret = devm_of_led_classdev_register(dev, to_of_node(fwnode), 115 + &led_data->cdev); 115 116 if (ret == 0) { 116 117 priv->num_leds++; 117 118 led_pwm_set(&led_data->cdev, led_data->cdev.brightness); ··· 124 123 return ret; 125 124 } 126 125 127 - static int led_pwm_create_of(struct device *dev, struct led_pwm_priv *priv) 126 + static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv) 128 127 { 129 - struct device_node *child; 128 + struct fwnode_handle *fwnode; 130 129 struct led_pwm led; 131 130 int ret = 0; 132 131 133 132 memset(&led, 0, sizeof(led)); 134 133 135 - for_each_child_of_node(dev->of_node, child) { 136 - led.name = of_get_property(child, "label", NULL) ? : 137 - child->name; 134 + device_for_each_child_node(dev, fwnode) { 135 + ret = fwnode_property_read_string(fwnode, "label", &led.name); 136 + if (ret && is_of_node(fwnode)) 137 + led.name = to_of_node(fwnode)->name; 138 138 139 - led.default_trigger = of_get_property(child, 140 - "linux,default-trigger", NULL); 141 - led.active_low = of_property_read_bool(child, "active-low"); 142 - of_property_read_u32(child, "max-brightness", 143 - &led.max_brightness); 139 + if (!led.name) { 140 + fwnode_handle_put(fwnode); 141 + return -EINVAL; 142 + } 144 143 145 - ret = led_pwm_add(dev, priv, &led, child); 144 + fwnode_property_read_string(fwnode, "linux,default-trigger", 145 + &led.default_trigger); 146 + 147 + led.active_low = fwnode_property_read_bool(fwnode, 148 + "active-low"); 149 + fwnode_property_read_u32(fwnode, "max-brightness", 150 + &led.max_brightness); 151 + 152 + ret = led_pwm_add(dev, priv, &led, fwnode); 146 153 if (ret) { 147 - of_node_put(child); 154 + fwnode_handle_put(fwnode); 148 155 break; 149 156 } 150 157 } ··· 170 161 if (pdata) 171 162 count = pdata->num_leds; 172 163 else 173 - count = of_get_child_count(pdev->dev.of_node); 164 + count = device_get_child_node_count(&pdev->dev); 174 165 175 166 if (!count) 176 167 return -EINVAL; ··· 188 179 break; 189 180 } 190 181 } else { 191 - ret = led_pwm_create_of(&pdev->dev, priv); 182 + ret = led_pwm_create_fwnode(&pdev->dev, priv); 192 183 } 193 184 194 185 if (ret)
+11
drivers/pwm/Kconfig
··· 401 401 To compile this driver as a module, choose M here: the module 402 402 will be called pwm-samsung. 403 403 404 + config PWM_SIFIVE 405 + tristate "SiFive PWM support" 406 + depends on OF 407 + depends on COMMON_CLK 408 + depends on RISCV || COMPILE_TEST 409 + help 410 + Generic PWM framework driver for SiFive SoCs. 411 + 412 + To compile this driver as a module, choose M here: the module 413 + will be called pwm-sifive. 414 + 404 415 config PWM_SPEAR 405 416 tristate "STMicroelectronics SPEAr PWM support" 406 417 depends on PLAT_SPEAR
+1
drivers/pwm/Makefile
··· 39 39 obj-$(CONFIG_PWM_RENESAS_TPU) += pwm-renesas-tpu.o 40 40 obj-$(CONFIG_PWM_ROCKCHIP) += pwm-rockchip.o 41 41 obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o 42 + obj-$(CONFIG_PWM_SIFIVE) += pwm-sifive.o 42 43 obj-$(CONFIG_PWM_SPEAR) += pwm-spear.o 43 44 obj-$(CONFIG_PWM_STI) += pwm-sti.o 44 45 obj-$(CONFIG_PWM_STM32) += pwm-stm32.o
+169 -3
drivers/pwm/core.c
··· 6 6 * Copyright (C) 2011-2012 Avionic Design GmbH 7 7 */ 8 8 9 + #include <linux/acpi.h> 9 10 #include <linux/module.h> 10 11 #include <linux/pwm.h> 11 12 #include <linux/radix-tree.h> ··· 627 626 return ERR_PTR(-EPROBE_DEFER); 628 627 } 629 628 629 + static struct device_link *pwm_device_link_add(struct device *dev, 630 + struct pwm_device *pwm) 631 + { 632 + struct device_link *dl; 633 + 634 + if (!dev) { 635 + /* 636 + * No device for the PWM consumer has been provided. It may 637 + * impact the PM sequence ordering: the PWM supplier may get 638 + * suspended before the consumer. 639 + */ 640 + dev_warn(pwm->chip->dev, 641 + "No consumer device specified to create a link to\n"); 642 + return NULL; 643 + } 644 + 645 + dl = device_link_add(dev, pwm->chip->dev, DL_FLAG_AUTOREMOVE_CONSUMER); 646 + if (!dl) { 647 + dev_err(dev, "failed to create device link to %s\n", 648 + dev_name(pwm->chip->dev)); 649 + return ERR_PTR(-EINVAL); 650 + } 651 + 652 + return dl; 653 + } 654 + 630 655 /** 631 656 * of_pwm_get() - request a PWM via the PWM framework 657 + * @dev: device for PWM consumer 632 658 * @np: device node to get the PWM from 633 659 * @con_id: consumer name 634 660 * ··· 673 645 * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 674 646 * error code on failure. 675 647 */ 676 - struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id) 648 + struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np, 649 + const char *con_id) 677 650 { 678 651 struct pwm_device *pwm = NULL; 679 652 struct of_phandle_args args; 653 + struct device_link *dl; 680 654 struct pwm_chip *pc; 681 655 int index = 0; 682 656 int err; ··· 709 679 if (IS_ERR(pwm)) 710 680 goto put; 711 681 682 + dl = pwm_device_link_add(dev, pwm); 683 + if (IS_ERR(dl)) { 684 + /* of_xlate ended up calling pwm_request_from_chip() */ 685 + pwm_free(pwm); 686 + pwm = ERR_CAST(dl); 687 + goto put; 688 + } 689 + 712 690 /* 713 691 * If a consumer name was not given, try to look it up from the 714 692 * "pwm-names" property if it exists. Otherwise use the name of ··· 737 699 return pwm; 738 700 } 739 701 EXPORT_SYMBOL_GPL(of_pwm_get); 702 + 703 + #if IS_ENABLED(CONFIG_ACPI) 704 + static struct pwm_chip *device_to_pwmchip(struct device *dev) 705 + { 706 + struct pwm_chip *chip; 707 + 708 + mutex_lock(&pwm_lock); 709 + 710 + list_for_each_entry(chip, &pwm_chips, list) { 711 + struct acpi_device *adev = ACPI_COMPANION(chip->dev); 712 + 713 + if ((chip->dev == dev) || (adev && &adev->dev == dev)) { 714 + mutex_unlock(&pwm_lock); 715 + return chip; 716 + } 717 + } 718 + 719 + mutex_unlock(&pwm_lock); 720 + 721 + return ERR_PTR(-EPROBE_DEFER); 722 + } 723 + #endif 724 + 725 + /** 726 + * acpi_pwm_get() - request a PWM via parsing "pwms" property in ACPI 727 + * @fwnode: firmware node to get the "pwm" property from 728 + * 729 + * Returns the PWM device parsed from the fwnode and index specified in the 730 + * "pwms" property or a negative error-code on failure. 731 + * Values parsed from the device tree are stored in the returned PWM device 732 + * object. 733 + * 734 + * This is analogous to of_pwm_get() except con_id is not yet supported. 735 + * ACPI entries must look like 736 + * Package () {"pwms", Package () 737 + * { <PWM device reference>, <PWM index>, <PWM period> [, <PWM flags>]}} 738 + * 739 + * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 740 + * error code on failure. 741 + */ 742 + static struct pwm_device *acpi_pwm_get(struct fwnode_handle *fwnode) 743 + { 744 + struct pwm_device *pwm = ERR_PTR(-ENODEV); 745 + #if IS_ENABLED(CONFIG_ACPI) 746 + struct fwnode_reference_args args; 747 + struct acpi_device *acpi; 748 + struct pwm_chip *chip; 749 + int ret; 750 + 751 + memset(&args, 0, sizeof(args)); 752 + 753 + ret = __acpi_node_get_property_reference(fwnode, "pwms", 0, 3, &args); 754 + if (ret < 0) 755 + return ERR_PTR(ret); 756 + 757 + acpi = to_acpi_device_node(args.fwnode); 758 + if (!acpi) 759 + return ERR_PTR(-EINVAL); 760 + 761 + if (args.nargs < 2) 762 + return ERR_PTR(-EPROTO); 763 + 764 + chip = device_to_pwmchip(&acpi->dev); 765 + if (IS_ERR(chip)) 766 + return ERR_CAST(chip); 767 + 768 + pwm = pwm_request_from_chip(chip, args.args[0], NULL); 769 + if (IS_ERR(pwm)) 770 + return pwm; 771 + 772 + pwm->args.period = args.args[1]; 773 + pwm->args.polarity = PWM_POLARITY_NORMAL; 774 + 775 + if (args.nargs > 2 && args.args[2] & PWM_POLARITY_INVERTED) 776 + pwm->args.polarity = PWM_POLARITY_INVERSED; 777 + #endif 778 + 779 + return pwm; 780 + } 740 781 741 782 /** 742 783 * pwm_add_table() - register PWM device consumers ··· 871 754 const char *dev_id = dev ? dev_name(dev) : NULL; 872 755 struct pwm_device *pwm; 873 756 struct pwm_chip *chip; 757 + struct device_link *dl; 874 758 unsigned int best = 0; 875 759 struct pwm_lookup *p, *chosen = NULL; 876 760 unsigned int match; ··· 879 761 880 762 /* look up via DT first */ 881 763 if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) 882 - return of_pwm_get(dev->of_node, con_id); 764 + return of_pwm_get(dev, dev->of_node, con_id); 765 + 766 + /* then lookup via ACPI */ 767 + if (dev && is_acpi_node(dev->fwnode)) 768 + return acpi_pwm_get(dev->fwnode); 883 769 884 770 /* 885 771 * We look up the provider in the static table typically provided by ··· 959 837 pwm = pwm_request_from_chip(chip, chosen->index, con_id ?: dev_id); 960 838 if (IS_ERR(pwm)) 961 839 return pwm; 840 + 841 + dl = pwm_device_link_add(dev, pwm); 842 + if (IS_ERR(dl)) { 843 + pwm_free(pwm); 844 + return ERR_CAST(dl); 845 + } 962 846 963 847 pwm->args.period = chosen->period; 964 848 pwm->args.polarity = chosen->polarity; ··· 1058 930 if (!ptr) 1059 931 return ERR_PTR(-ENOMEM); 1060 932 1061 - pwm = of_pwm_get(np, con_id); 933 + pwm = of_pwm_get(dev, np, con_id); 1062 934 if (!IS_ERR(pwm)) { 1063 935 *ptr = pwm; 1064 936 devres_add(dev, ptr); ··· 1069 941 return pwm; 1070 942 } 1071 943 EXPORT_SYMBOL_GPL(devm_of_pwm_get); 944 + 945 + /** 946 + * devm_fwnode_pwm_get() - request a resource managed PWM from firmware node 947 + * @dev: device for PWM consumer 948 + * @fwnode: firmware node to get the PWM from 949 + * @con_id: consumer name 950 + * 951 + * Returns the PWM device parsed from the firmware node. See of_pwm_get() and 952 + * acpi_pwm_get() for a detailed description. 953 + * 954 + * Returns: A pointer to the requested PWM device or an ERR_PTR()-encoded 955 + * error code on failure. 956 + */ 957 + struct pwm_device *devm_fwnode_pwm_get(struct device *dev, 958 + struct fwnode_handle *fwnode, 959 + const char *con_id) 960 + { 961 + struct pwm_device **ptr, *pwm = ERR_PTR(-ENODEV); 962 + 963 + ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL); 964 + if (!ptr) 965 + return ERR_PTR(-ENOMEM); 966 + 967 + if (is_of_node(fwnode)) 968 + pwm = of_pwm_get(dev, to_of_node(fwnode), con_id); 969 + else if (is_acpi_node(fwnode)) 970 + pwm = acpi_pwm_get(fwnode); 971 + 972 + if (!IS_ERR(pwm)) { 973 + *ptr = pwm; 974 + devres_add(dev, ptr); 975 + } else { 976 + devres_free(ptr); 977 + } 978 + 979 + return pwm; 980 + } 981 + EXPORT_SYMBOL_GPL(devm_fwnode_pwm_get); 1072 982 1073 983 static int devm_pwm_match(struct device *dev, void *res, void *data) 1074 984 {
+1
drivers/pwm/pwm-atmel-hlcdc.c
··· 235 235 .compatible = "atmel,sama5d4-hlcdc", 236 236 .data = &atmel_hlcdc_pwm_sama5d3_errata, 237 237 }, 238 + { .compatible = "microchip,sam9x60-hlcdc", }, 238 239 { /* sentinel */ }, 239 240 }; 240 241 MODULE_DEVICE_TABLE(of, atmel_hlcdc_dt_ids);
+5 -3
drivers/pwm/pwm-bcm2835.c
··· 70 70 return -EINVAL; 71 71 } 72 72 73 - scaler = NSEC_PER_SEC / rate; 73 + scaler = DIV_ROUND_CLOSEST(NSEC_PER_SEC, rate); 74 74 75 75 if (period_ns <= MIN_PERIOD) { 76 76 dev_err(pc->dev, "period %d not supported, minimum %d\n", ··· 78 78 return -EINVAL; 79 79 } 80 80 81 - writel(duty_ns / scaler, pc->base + DUTY(pwm->hwpwm)); 82 - writel(period_ns / scaler, pc->base + PERIOD(pwm->hwpwm)); 81 + writel(DIV_ROUND_CLOSEST(duty_ns, scaler), 82 + pc->base + DUTY(pwm->hwpwm)); 83 + writel(DIV_ROUND_CLOSEST(period_ns, scaler), 84 + pc->base + PERIOD(pwm->hwpwm)); 83 85 84 86 return 0; 85 87 }
+207 -180
drivers/pwm/pwm-fsl-ftm.c
··· 34 34 bool has_enable_bits; 35 35 }; 36 36 37 + struct fsl_pwm_periodcfg { 38 + enum fsl_pwm_clk clk_select; 39 + unsigned int clk_ps; 40 + unsigned int mod_period; 41 + }; 42 + 37 43 struct fsl_pwm_chip { 38 44 struct pwm_chip chip; 39 - 40 45 struct mutex lock; 41 - 42 - unsigned int cnt_select; 43 - unsigned int clk_ps; 44 - 45 46 struct regmap *regmap; 46 47 47 - int period_ns; 48 + /* This value is valid iff a pwm is running */ 49 + struct fsl_pwm_periodcfg period; 48 50 49 51 struct clk *ipg_clk; 50 52 struct clk *clk[FSL_PWM_CLK_MAX]; ··· 57 55 static inline struct fsl_pwm_chip *to_fsl_chip(struct pwm_chip *chip) 58 56 { 59 57 return container_of(chip, struct fsl_pwm_chip, chip); 58 + } 59 + 60 + static void ftm_clear_write_protection(struct fsl_pwm_chip *fpc) 61 + { 62 + u32 val; 63 + 64 + regmap_read(fpc->regmap, FTM_FMS, &val); 65 + if (val & FTM_FMS_WPEN) 66 + regmap_update_bits(fpc->regmap, FTM_MODE, FTM_MODE_WPDIS, 67 + FTM_MODE_WPDIS); 68 + } 69 + 70 + static void ftm_set_write_protection(struct fsl_pwm_chip *fpc) 71 + { 72 + regmap_update_bits(fpc->regmap, FTM_FMS, FTM_FMS_WPEN, FTM_FMS_WPEN); 73 + } 74 + 75 + static bool fsl_pwm_periodcfg_are_equal(const struct fsl_pwm_periodcfg *a, 76 + const struct fsl_pwm_periodcfg *b) 77 + { 78 + if (a->clk_select != b->clk_select) 79 + return false; 80 + if (a->clk_ps != b->clk_ps) 81 + return false; 82 + if (a->mod_period != b->mod_period) 83 + return false; 84 + return true; 60 85 } 61 86 62 87 static int fsl_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) ··· 116 87 clk_disable_unprepare(fpc->ipg_clk); 117 88 } 118 89 119 - static int fsl_pwm_calculate_default_ps(struct fsl_pwm_chip *fpc, 120 - enum fsl_pwm_clk index) 90 + static unsigned int fsl_pwm_ticks_to_ns(struct fsl_pwm_chip *fpc, 91 + unsigned int ticks) 121 92 { 122 - unsigned long sys_rate, cnt_rate; 123 - unsigned long long ratio; 93 + unsigned long rate; 94 + unsigned long long exval; 124 95 125 - sys_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_SYS]); 126 - if (!sys_rate) 127 - return -EINVAL; 128 - 129 - cnt_rate = clk_get_rate(fpc->clk[fpc->cnt_select]); 130 - if (!cnt_rate) 131 - return -EINVAL; 132 - 133 - switch (index) { 134 - case FSL_PWM_CLK_SYS: 135 - fpc->clk_ps = 1; 136 - break; 137 - case FSL_PWM_CLK_FIX: 138 - ratio = 2 * cnt_rate - 1; 139 - do_div(ratio, sys_rate); 140 - fpc->clk_ps = ratio; 141 - break; 142 - case FSL_PWM_CLK_EXT: 143 - ratio = 4 * cnt_rate - 1; 144 - do_div(ratio, sys_rate); 145 - fpc->clk_ps = ratio; 146 - break; 147 - default: 148 - return -EINVAL; 149 - } 150 - 151 - return 0; 96 + rate = clk_get_rate(fpc->clk[fpc->period.clk_select]); 97 + exval = ticks; 98 + exval *= 1000000000UL; 99 + do_div(exval, rate >> fpc->period.clk_ps); 100 + return exval; 152 101 } 153 102 154 - static unsigned long fsl_pwm_calculate_cycles(struct fsl_pwm_chip *fpc, 155 - unsigned long period_ns) 103 + static bool fsl_pwm_calculate_period_clk(struct fsl_pwm_chip *fpc, 104 + unsigned int period_ns, 105 + enum fsl_pwm_clk index, 106 + struct fsl_pwm_periodcfg *periodcfg 107 + ) 156 108 { 157 - unsigned long long c, c0; 109 + unsigned long long c; 110 + unsigned int ps; 158 111 159 - c = clk_get_rate(fpc->clk[fpc->cnt_select]); 112 + c = clk_get_rate(fpc->clk[index]); 160 113 c = c * period_ns; 161 114 do_div(c, 1000000000UL); 162 115 163 - do { 164 - c0 = c; 165 - do_div(c0, (1 << fpc->clk_ps)); 166 - if (c0 <= 0xFFFF) 167 - return (unsigned long)c0; 168 - } while (++fpc->clk_ps < 8); 116 + if (c == 0) 117 + return false; 169 118 170 - return 0; 171 - } 172 - 173 - static unsigned long fsl_pwm_calculate_period_cycles(struct fsl_pwm_chip *fpc, 174 - unsigned long period_ns, 175 - enum fsl_pwm_clk index) 176 - { 177 - int ret; 178 - 179 - ret = fsl_pwm_calculate_default_ps(fpc, index); 180 - if (ret) { 181 - dev_err(fpc->chip.dev, 182 - "failed to calculate default prescaler: %d\n", 183 - ret); 184 - return 0; 119 + for (ps = 0; ps < 8 ; ++ps, c >>= 1) { 120 + if (c <= 0x10000) { 121 + periodcfg->clk_select = index; 122 + periodcfg->clk_ps = ps; 123 + periodcfg->mod_period = c - 1; 124 + return true; 125 + } 185 126 } 186 - 187 - return fsl_pwm_calculate_cycles(fpc, period_ns); 127 + return false; 188 128 } 189 129 190 - static unsigned long fsl_pwm_calculate_period(struct fsl_pwm_chip *fpc, 191 - unsigned long period_ns) 130 + static bool fsl_pwm_calculate_period(struct fsl_pwm_chip *fpc, 131 + unsigned int period_ns, 132 + struct fsl_pwm_periodcfg *periodcfg) 192 133 { 193 134 enum fsl_pwm_clk m0, m1; 194 - unsigned long fix_rate, ext_rate, cycles; 135 + unsigned long fix_rate, ext_rate; 136 + bool ret; 195 137 196 - cycles = fsl_pwm_calculate_period_cycles(fpc, period_ns, 197 - FSL_PWM_CLK_SYS); 198 - if (cycles) { 199 - fpc->cnt_select = FSL_PWM_CLK_SYS; 200 - return cycles; 201 - } 138 + ret = fsl_pwm_calculate_period_clk(fpc, period_ns, FSL_PWM_CLK_SYS, 139 + periodcfg); 140 + if (ret) 141 + return true; 202 142 203 143 fix_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_FIX]); 204 144 ext_rate = clk_get_rate(fpc->clk[FSL_PWM_CLK_EXT]); ··· 180 182 m1 = FSL_PWM_CLK_FIX; 181 183 } 182 184 183 - cycles = fsl_pwm_calculate_period_cycles(fpc, period_ns, m0); 184 - if (cycles) { 185 - fpc->cnt_select = m0; 186 - return cycles; 187 - } 185 + ret = fsl_pwm_calculate_period_clk(fpc, period_ns, m0, periodcfg); 186 + if (ret) 187 + return true; 188 188 189 - fpc->cnt_select = m1; 190 - 191 - return fsl_pwm_calculate_period_cycles(fpc, period_ns, m1); 189 + return fsl_pwm_calculate_period_clk(fpc, period_ns, m1, periodcfg); 192 190 } 193 191 194 - static unsigned long fsl_pwm_calculate_duty(struct fsl_pwm_chip *fpc, 195 - unsigned long period_ns, 196 - unsigned long duty_ns) 192 + static unsigned int fsl_pwm_calculate_duty(struct fsl_pwm_chip *fpc, 193 + unsigned int duty_ns) 197 194 { 198 195 unsigned long long duty; 199 - u32 val; 200 196 201 - regmap_read(fpc->regmap, FTM_MOD, &val); 202 - duty = (unsigned long long)duty_ns * (val + 1); 197 + unsigned int period = fpc->period.mod_period + 1; 198 + unsigned int period_ns = fsl_pwm_ticks_to_ns(fpc, period); 199 + 200 + duty = (unsigned long long)duty_ns * period; 203 201 do_div(duty, period_ns); 204 202 205 - return (unsigned long)duty; 203 + return (unsigned int)duty; 206 204 } 207 205 208 - static int fsl_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 209 - int duty_ns, int period_ns) 206 + static bool fsl_pwm_is_any_pwm_enabled(struct fsl_pwm_chip *fpc, 207 + struct pwm_device *pwm) 210 208 { 211 - struct fsl_pwm_chip *fpc = to_fsl_chip(chip); 212 - u32 period, duty; 209 + u32 val; 213 210 214 - mutex_lock(&fpc->lock); 211 + regmap_read(fpc->regmap, FTM_OUTMASK, &val); 212 + if (~val & 0xFF) 213 + return true; 214 + else 215 + return false; 216 + } 215 217 218 + static bool fsl_pwm_is_other_pwm_enabled(struct fsl_pwm_chip *fpc, 219 + struct pwm_device *pwm) 220 + { 221 + u32 val; 222 + 223 + regmap_read(fpc->regmap, FTM_OUTMASK, &val); 224 + if (~(val | BIT(pwm->hwpwm)) & 0xFF) 225 + return true; 226 + else 227 + return false; 228 + } 229 + 230 + static int fsl_pwm_apply_config(struct fsl_pwm_chip *fpc, 231 + struct pwm_device *pwm, 232 + struct pwm_state *newstate) 233 + { 234 + unsigned int duty; 235 + u32 reg_polarity; 236 + 237 + struct fsl_pwm_periodcfg periodcfg; 238 + bool do_write_period = false; 239 + 240 + if (!fsl_pwm_calculate_period(fpc, newstate->period, &periodcfg)) { 241 + dev_err(fpc->chip.dev, "failed to calculate new period\n"); 242 + return -EINVAL; 243 + } 244 + 245 + if (!fsl_pwm_is_any_pwm_enabled(fpc, pwm)) 246 + do_write_period = true; 216 247 /* 217 248 * The Freescale FTM controller supports only a single period for 218 - * all PWM channels, therefore incompatible changes need to be 219 - * refused. 249 + * all PWM channels, therefore verify if the newly computed period 250 + * is different than the current period being used. In such case 251 + * we allow to change the period only if no other pwm is running. 220 252 */ 221 - if (fpc->period_ns && fpc->period_ns != period_ns) { 222 - dev_err(fpc->chip.dev, 223 - "conflicting period requested for PWM %u\n", 224 - pwm->hwpwm); 225 - mutex_unlock(&fpc->lock); 226 - return -EBUSY; 227 - } 228 - 229 - if (!fpc->period_ns && duty_ns) { 230 - period = fsl_pwm_calculate_period(fpc, period_ns); 231 - if (!period) { 232 - dev_err(fpc->chip.dev, "failed to calculate period\n"); 233 - mutex_unlock(&fpc->lock); 234 - return -EINVAL; 253 + else if (!fsl_pwm_periodcfg_are_equal(&fpc->period, &periodcfg)) { 254 + if (fsl_pwm_is_other_pwm_enabled(fpc, pwm)) { 255 + dev_err(fpc->chip.dev, 256 + "Cannot change period for PWM %u, disable other PWMs first\n", 257 + pwm->hwpwm); 258 + return -EBUSY; 235 259 } 260 + if (fpc->period.clk_select != periodcfg.clk_select) { 261 + int ret; 262 + enum fsl_pwm_clk oldclk = fpc->period.clk_select; 263 + enum fsl_pwm_clk newclk = periodcfg.clk_select; 236 264 237 - regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_PS_MASK, 238 - fpc->clk_ps); 239 - regmap_write(fpc->regmap, FTM_MOD, period - 1); 240 - 241 - fpc->period_ns = period_ns; 265 + ret = clk_prepare_enable(fpc->clk[newclk]); 266 + if (ret) 267 + return ret; 268 + clk_disable_unprepare(fpc->clk[oldclk]); 269 + } 270 + do_write_period = true; 242 271 } 243 272 244 - mutex_unlock(&fpc->lock); 273 + ftm_clear_write_protection(fpc); 245 274 246 - duty = fsl_pwm_calculate_duty(fpc, period_ns, duty_ns); 275 + if (do_write_period) { 276 + regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_CLK_MASK, 277 + FTM_SC_CLK(periodcfg.clk_select)); 278 + regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_PS_MASK, 279 + periodcfg.clk_ps); 280 + regmap_write(fpc->regmap, FTM_MOD, periodcfg.mod_period); 281 + 282 + fpc->period = periodcfg; 283 + } 284 + 285 + duty = fsl_pwm_calculate_duty(fpc, newstate->duty_cycle); 247 286 248 287 regmap_write(fpc->regmap, FTM_CSC(pwm->hwpwm), 249 288 FTM_CSC_MSB | FTM_CSC_ELSB); 250 289 regmap_write(fpc->regmap, FTM_CV(pwm->hwpwm), duty); 251 290 291 + reg_polarity = 0; 292 + if (newstate->polarity == PWM_POLARITY_INVERSED) 293 + reg_polarity = BIT(pwm->hwpwm); 294 + 295 + regmap_update_bits(fpc->regmap, FTM_POL, BIT(pwm->hwpwm), reg_polarity); 296 + 297 + newstate->period = fsl_pwm_ticks_to_ns(fpc, 298 + fpc->period.mod_period + 1); 299 + newstate->duty_cycle = fsl_pwm_ticks_to_ns(fpc, duty); 300 + 301 + ftm_set_write_protection(fpc); 302 + 252 303 return 0; 253 304 } 254 305 255 - static int fsl_pwm_set_polarity(struct pwm_chip *chip, 256 - struct pwm_device *pwm, 257 - enum pwm_polarity polarity) 306 + static int fsl_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 307 + struct pwm_state *newstate) 258 308 { 259 309 struct fsl_pwm_chip *fpc = to_fsl_chip(chip); 260 - u32 val; 310 + struct pwm_state *oldstate = &pwm->state; 311 + int ret = 0; 261 312 262 - regmap_read(fpc->regmap, FTM_POL, &val); 313 + /* 314 + * oldstate to newstate : action 315 + * 316 + * disabled to disabled : ignore 317 + * enabled to disabled : disable 318 + * enabled to enabled : update settings 319 + * disabled to enabled : update settings + enable 320 + */ 263 321 264 - if (polarity == PWM_POLARITY_INVERSED) 265 - val |= BIT(pwm->hwpwm); 266 - else 267 - val &= ~BIT(pwm->hwpwm); 322 + mutex_lock(&fpc->lock); 268 323 269 - regmap_write(fpc->regmap, FTM_POL, val); 324 + if (!newstate->enabled) { 325 + if (oldstate->enabled) { 326 + regmap_update_bits(fpc->regmap, FTM_OUTMASK, 327 + BIT(pwm->hwpwm), BIT(pwm->hwpwm)); 328 + clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]); 329 + clk_disable_unprepare(fpc->clk[fpc->period.clk_select]); 330 + } 270 331 271 - return 0; 272 - } 273 - 274 - static int fsl_counter_clock_enable(struct fsl_pwm_chip *fpc) 275 - { 276 - int ret; 277 - 278 - /* select counter clock source */ 279 - regmap_update_bits(fpc->regmap, FTM_SC, FTM_SC_CLK_MASK, 280 - FTM_SC_CLK(fpc->cnt_select)); 281 - 282 - ret = clk_prepare_enable(fpc->clk[fpc->cnt_select]); 283 - if (ret) 284 - return ret; 285 - 286 - ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]); 287 - if (ret) { 288 - clk_disable_unprepare(fpc->clk[fpc->cnt_select]); 289 - return ret; 332 + goto end_mutex; 290 333 } 291 334 292 - return 0; 293 - } 335 + ret = fsl_pwm_apply_config(fpc, pwm, newstate); 336 + if (ret) 337 + goto end_mutex; 294 338 295 - static int fsl_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm) 296 - { 297 - struct fsl_pwm_chip *fpc = to_fsl_chip(chip); 298 - int ret; 339 + /* check if need to enable */ 340 + if (!oldstate->enabled) { 341 + ret = clk_prepare_enable(fpc->clk[fpc->period.clk_select]); 342 + if (ret) 343 + goto end_mutex; 299 344 300 - mutex_lock(&fpc->lock); 301 - regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm), 0); 345 + ret = clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]); 346 + if (ret) { 347 + clk_disable_unprepare(fpc->clk[fpc->period.clk_select]); 348 + goto end_mutex; 349 + } 302 350 303 - ret = fsl_counter_clock_enable(fpc); 351 + regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm), 352 + 0); 353 + } 354 + 355 + end_mutex: 304 356 mutex_unlock(&fpc->lock); 305 - 306 357 return ret; 307 - } 308 - 309 - static void fsl_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) 310 - { 311 - struct fsl_pwm_chip *fpc = to_fsl_chip(chip); 312 - u32 val; 313 - 314 - mutex_lock(&fpc->lock); 315 - regmap_update_bits(fpc->regmap, FTM_OUTMASK, BIT(pwm->hwpwm), 316 - BIT(pwm->hwpwm)); 317 - 318 - clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]); 319 - clk_disable_unprepare(fpc->clk[fpc->cnt_select]); 320 - 321 - regmap_read(fpc->regmap, FTM_OUTMASK, &val); 322 - if ((val & 0xFF) == 0xFF) 323 - fpc->period_ns = 0; 324 - 325 - mutex_unlock(&fpc->lock); 326 358 } 327 359 328 360 static const struct pwm_ops fsl_pwm_ops = { 329 361 .request = fsl_pwm_request, 330 362 .free = fsl_pwm_free, 331 - .config = fsl_pwm_config, 332 - .set_polarity = fsl_pwm_set_polarity, 333 - .enable = fsl_pwm_enable, 334 - .disable = fsl_pwm_disable, 363 + .apply = fsl_pwm_apply, 335 364 .owner = THIS_MODULE, 336 365 }; 337 366 ··· 382 357 static bool fsl_pwm_volatile_reg(struct device *dev, unsigned int reg) 383 358 { 384 359 switch (reg) { 360 + case FTM_FMS: 361 + case FTM_MODE: 385 362 case FTM_CNT: 386 363 return true; 387 364 } ··· 501 474 continue; 502 475 503 476 clk_disable_unprepare(fpc->clk[FSL_PWM_CLK_CNTEN]); 504 - clk_disable_unprepare(fpc->clk[fpc->cnt_select]); 477 + clk_disable_unprepare(fpc->clk[fpc->period.clk_select]); 505 478 } 506 479 507 480 return 0; ··· 523 496 if (!pwm_is_enabled(pwm)) 524 497 continue; 525 498 526 - clk_prepare_enable(fpc->clk[fpc->cnt_select]); 499 + clk_prepare_enable(fpc->clk[fpc->period.clk_select]); 527 500 clk_prepare_enable(fpc->clk[FSL_PWM_CLK_CNTEN]); 528 501 } 529 502
+21 -28
drivers/pwm/pwm-jz4740.c
··· 63 63 { 64 64 uint32_t ctrl = jz4740_timer_get_ctrl(pwm->hwpwm); 65 65 66 - /* Disable PWM output. 66 + /* 67 + * Set duty > period. This trick allows the TCU channels in TCU2 mode to 68 + * properly return to their init level. 69 + */ 70 + jz4740_timer_set_duty(pwm->hwpwm, 0xffff); 71 + jz4740_timer_set_period(pwm->hwpwm, 0x0); 72 + 73 + /* 74 + * Disable PWM output. 67 75 * In TCU2 mode (channel 1/2 on JZ4750+), this must be done before the 68 76 * counter is stopped, while in TCU1 mode the order does not matter. 69 77 */ ··· 82 74 jz4740_timer_disable(pwm->hwpwm); 83 75 } 84 76 85 - static int jz4740_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 86 - int duty_ns, int period_ns) 77 + static int jz4740_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm, 78 + struct pwm_state *state) 87 79 { 88 80 struct jz4740_pwm_chip *jz4740 = to_jz4740(pwm->chip); 89 81 unsigned long long tmp; 90 82 unsigned long period, duty; 91 83 unsigned int prescaler = 0; 92 84 uint16_t ctrl; 93 - bool is_enabled; 94 85 95 - tmp = (unsigned long long)clk_get_rate(jz4740->clk) * period_ns; 86 + tmp = (unsigned long long)clk_get_rate(jz4740->clk) * state->period; 96 87 do_div(tmp, 1000000000); 97 88 period = tmp; 98 89 ··· 103 96 if (prescaler == 6) 104 97 return -EINVAL; 105 98 106 - tmp = (unsigned long long)period * duty_ns; 107 - do_div(tmp, period_ns); 99 + tmp = (unsigned long long)period * state->duty_cycle; 100 + do_div(tmp, state->period); 108 101 duty = period - tmp; 109 102 110 103 if (duty >= period) 111 104 duty = period - 1; 112 105 113 - is_enabled = jz4740_timer_is_enabled(pwm->hwpwm); 114 - if (is_enabled) 115 - jz4740_pwm_disable(chip, pwm); 106 + jz4740_pwm_disable(chip, pwm); 116 107 117 108 jz4740_timer_set_count(pwm->hwpwm, 0); 118 109 jz4740_timer_set_duty(pwm->hwpwm, duty); ··· 121 116 122 117 jz4740_timer_set_ctrl(pwm->hwpwm, ctrl); 123 118 124 - if (is_enabled) 125 - jz4740_pwm_enable(chip, pwm); 126 - 127 - return 0; 128 - } 129 - 130 - static int jz4740_pwm_set_polarity(struct pwm_chip *chip, 131 - struct pwm_device *pwm, enum pwm_polarity polarity) 132 - { 133 - uint32_t ctrl = jz4740_timer_get_ctrl(pwm->pwm); 134 - 135 - switch (polarity) { 119 + switch (state->polarity) { 136 120 case PWM_POLARITY_NORMAL: 137 121 ctrl &= ~JZ_TIMER_CTRL_PWM_ACTIVE_LOW; 138 122 break; ··· 131 137 } 132 138 133 139 jz4740_timer_set_ctrl(pwm->hwpwm, ctrl); 140 + 141 + if (state->enabled) 142 + jz4740_pwm_enable(chip, pwm); 143 + 134 144 return 0; 135 145 } 136 146 137 147 static const struct pwm_ops jz4740_pwm_ops = { 138 148 .request = jz4740_pwm_request, 139 149 .free = jz4740_pwm_free, 140 - .config = jz4740_pwm_config, 141 - .set_polarity = jz4740_pwm_set_polarity, 142 - .enable = jz4740_pwm_enable, 143 - .disable = jz4740_pwm_disable, 150 + .apply = jz4740_pwm_apply, 144 151 .owner = THIS_MODULE, 145 152 }; 146 153 ··· 179 184 #ifdef CONFIG_OF 180 185 static const struct of_device_id jz4740_pwm_dt_ids[] = { 181 186 { .compatible = "ingenic,jz4740-pwm", }, 182 - { .compatible = "ingenic,jz4770-pwm", }, 183 - { .compatible = "ingenic,jz4780-pwm", }, 184 187 {}, 185 188 }; 186 189 MODULE_DEVICE_TABLE(of, jz4740_pwm_dt_ids);
+182 -204
drivers/pwm/pwm-meson.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 1 2 /* 2 - * This file is provided under a dual BSD/GPLv2 license. When using or 3 - * redistributing this file, you may do so under either license. 3 + * PWM controller driver for Amlogic Meson SoCs. 4 4 * 5 - * GPL LICENSE SUMMARY 5 + * This PWM is only a set of Gates, Dividers and Counters: 6 + * PWM output is achieved by calculating a clock that permits calculating 7 + * two periods (low and high). The counter then has to be set to switch after 8 + * N cycles for the first half period. 9 + * The hardware has no "polarity" setting. This driver reverses the period 10 + * cycles (the low length is inverted with the high length) for 11 + * PWM_POLARITY_INVERSED. This means that .get_state cannot read the polarity 12 + * from the hardware. 13 + * Setting the duty cycle will disable and re-enable the PWM output. 14 + * Disabling the PWM stops the output immediately (without waiting for the 15 + * current period to complete first). 6 16 * 7 - * Copyright (c) 2016 BayLibre, SAS. 8 - * Author: Neil Armstrong <narmstrong@baylibre.com> 9 - * Copyright (C) 2014 Amlogic, Inc. 10 - * 11 - * This program is free software; you can redistribute it and/or modify 12 - * it under the terms of version 2 of the GNU General Public License as 13 - * published by the Free Software Foundation. 14 - * 15 - * This program is distributed in the hope that it will be useful, but 16 - * WITHOUT ANY WARRANTY; without even the implied warranty of 17 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 - * General Public License for more details. 19 - * 20 - * You should have received a copy of the GNU General Public License 21 - * along with this program; if not, see <http://www.gnu.org/licenses/>. 22 - * The full GNU General Public License is included in this distribution 23 - * in the file called COPYING. 24 - * 25 - * BSD LICENSE 17 + * The public S912 (GXM) datasheet contains some documentation for this PWM 18 + * controller starting on page 543: 19 + * https://dl.khadas.com/Hardware/VIM2/Datasheet/S912_Datasheet_V0.220170314publicversion-Wesion.pdf 20 + * An updated version of this IP block is found in S922X (G12B) SoCs. The 21 + * datasheet contains the description for this IP block revision starting at 22 + * page 1084: 23 + * https://dn.odroid.com/S922X/ODROID-N2/Datasheet/S922X_Public_Datasheet_V0.2.pdf 26 24 * 27 25 * Copyright (c) 2016 BayLibre, SAS. 28 26 * Author: Neil Armstrong <narmstrong@baylibre.com> 29 27 * Copyright (C) 2014 Amlogic, Inc. 30 - * 31 - * Redistribution and use in source and binary forms, with or without 32 - * modification, are permitted provided that the following conditions 33 - * are met: 34 - * 35 - * * Redistributions of source code must retain the above copyright 36 - * notice, this list of conditions and the following disclaimer. 37 - * * Redistributions in binary form must reproduce the above copyright 38 - * notice, this list of conditions and the following disclaimer in 39 - * the documentation and/or other materials provided with the 40 - * distribution. 41 - * * Neither the name of Intel Corporation nor the names of its 42 - * contributors may be used to endorse or promote products derived 43 - * from this software without specific prior written permission. 44 - * 45 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 46 - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 47 - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 48 - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 49 - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 50 - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 51 - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 52 - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 53 - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 54 - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 55 - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 28 */ 57 29 30 + #include <linux/bitfield.h> 31 + #include <linux/bits.h> 58 32 #include <linux/clk.h> 59 33 #include <linux/clk-provider.h> 60 34 #include <linux/err.h> 61 35 #include <linux/io.h> 62 36 #include <linux/kernel.h> 37 + #include <linux/math64.h> 63 38 #include <linux/module.h> 64 39 #include <linux/of.h> 65 40 #include <linux/of_device.h> ··· 45 70 46 71 #define REG_PWM_A 0x0 47 72 #define REG_PWM_B 0x4 48 - #define PWM_HIGH_SHIFT 16 73 + #define PWM_LOW_MASK GENMASK(15, 0) 74 + #define PWM_HIGH_MASK GENMASK(31, 16) 49 75 50 76 #define REG_MISC_AB 0x8 51 77 #define MISC_B_CLK_EN BIT(23) ··· 56 80 #define MISC_A_CLK_DIV_SHIFT 8 57 81 #define MISC_B_CLK_SEL_SHIFT 6 58 82 #define MISC_A_CLK_SEL_SHIFT 4 59 - #define MISC_CLK_SEL_WIDTH 2 83 + #define MISC_CLK_SEL_MASK 0x3 60 84 #define MISC_B_EN BIT(1) 61 85 #define MISC_A_EN BIT(0) 62 86 63 - static const unsigned int mux_reg_shifts[] = { 64 - MISC_A_CLK_SEL_SHIFT, 65 - MISC_B_CLK_SEL_SHIFT 87 + #define MESON_NUM_PWMS 2 88 + 89 + static struct meson_pwm_channel_data { 90 + u8 reg_offset; 91 + u8 clk_sel_shift; 92 + u8 clk_div_shift; 93 + u32 clk_en_mask; 94 + u32 pwm_en_mask; 95 + } meson_pwm_per_channel_data[MESON_NUM_PWMS] = { 96 + { 97 + .reg_offset = REG_PWM_A, 98 + .clk_sel_shift = MISC_A_CLK_SEL_SHIFT, 99 + .clk_div_shift = MISC_A_CLK_DIV_SHIFT, 100 + .clk_en_mask = MISC_A_CLK_EN, 101 + .pwm_en_mask = MISC_A_EN, 102 + }, 103 + { 104 + .reg_offset = REG_PWM_B, 105 + .clk_sel_shift = MISC_B_CLK_SEL_SHIFT, 106 + .clk_div_shift = MISC_B_CLK_DIV_SHIFT, 107 + .clk_en_mask = MISC_B_CLK_EN, 108 + .pwm_en_mask = MISC_B_EN, 109 + } 66 110 }; 67 111 68 112 struct meson_pwm_channel { 69 113 unsigned int hi; 70 114 unsigned int lo; 71 115 u8 pre_div; 72 - 73 - struct pwm_state state; 74 116 75 117 struct clk *clk_parent; 76 118 struct clk_mux mux; ··· 103 109 struct meson_pwm { 104 110 struct pwm_chip chip; 105 111 const struct meson_pwm_data *data; 112 + struct meson_pwm_channel channels[MESON_NUM_PWMS]; 106 113 void __iomem *base; 107 - u8 inverter_mask; 108 114 /* 109 115 * Protects register (write) access to the REG_MISC_AB register 110 116 * that is shared between the two PWMs. ··· 119 125 120 126 static int meson_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm) 121 127 { 122 - struct meson_pwm_channel *channel = pwm_get_chip_data(pwm); 128 + struct meson_pwm *meson = to_meson_pwm(chip); 129 + struct meson_pwm_channel *channel; 123 130 struct device *dev = chip->dev; 124 131 int err; 125 132 126 - if (!channel) 127 - return -ENODEV; 133 + channel = pwm_get_chip_data(pwm); 134 + if (channel) 135 + return 0; 136 + 137 + channel = &meson->channels[pwm->hwpwm]; 128 138 129 139 if (channel->clk_parent) { 130 140 err = clk_set_parent(channel->clk, channel->clk_parent); ··· 147 149 return err; 148 150 } 149 151 150 - chip->ops->get_state(chip, pwm, &channel->state); 151 - 152 - return 0; 152 + return pwm_set_chip_data(pwm, channel); 153 153 } 154 154 155 155 static void meson_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm) ··· 158 162 clk_disable_unprepare(channel->clk); 159 163 } 160 164 161 - static int meson_pwm_calc(struct meson_pwm *meson, 162 - struct meson_pwm_channel *channel, unsigned int id, 163 - unsigned int duty, unsigned int period) 165 + static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm, 166 + struct pwm_state *state) 164 167 { 165 - unsigned int pre_div, cnt, duty_cnt; 168 + struct meson_pwm_channel *channel = pwm_get_chip_data(pwm); 169 + unsigned int duty, period, pre_div, cnt, duty_cnt; 166 170 unsigned long fin_freq = -1; 167 - u64 fin_ps; 168 171 169 - if (~(meson->inverter_mask >> id) & 0x1) 172 + duty = state->duty_cycle; 173 + period = state->period; 174 + 175 + if (state->polarity == PWM_POLARITY_INVERSED) 170 176 duty = period - duty; 171 - 172 - if (period == channel->state.period && 173 - duty == channel->state.duty_cycle) 174 - return 0; 175 177 176 178 fin_freq = clk_get_rate(channel->clk); 177 179 if (fin_freq == 0) { ··· 178 184 } 179 185 180 186 dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq); 181 - fin_ps = (u64)NSEC_PER_SEC * 1000; 182 - do_div(fin_ps, fin_freq); 183 187 184 - /* Calc pre_div with the period */ 185 - for (pre_div = 0; pre_div <= MISC_CLK_DIV_MASK; pre_div++) { 186 - cnt = DIV_ROUND_CLOSEST_ULL((u64)period * 1000, 187 - fin_ps * (pre_div + 1)); 188 - dev_dbg(meson->chip.dev, "fin_ps=%llu pre_div=%u cnt=%u\n", 189 - fin_ps, pre_div, cnt); 190 - if (cnt <= 0xffff) 191 - break; 192 - } 193 - 188 + pre_div = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * 0xffffLL); 194 189 if (pre_div > MISC_CLK_DIV_MASK) { 195 190 dev_err(meson->chip.dev, "unable to get period pre_div\n"); 191 + return -EINVAL; 192 + } 193 + 194 + cnt = div64_u64(fin_freq * (u64)period, NSEC_PER_SEC * (pre_div + 1)); 195 + if (cnt > 0xffff) { 196 + dev_err(meson->chip.dev, "unable to get period cnt\n"); 196 197 return -EINVAL; 197 198 } 198 199 ··· 204 215 channel->lo = cnt; 205 216 } else { 206 217 /* Then check is we can have the duty with the same pre_div */ 207 - duty_cnt = DIV_ROUND_CLOSEST_ULL((u64)duty * 1000, 208 - fin_ps * (pre_div + 1)); 218 + duty_cnt = div64_u64(fin_freq * (u64)duty, 219 + NSEC_PER_SEC * (pre_div + 1)); 209 220 if (duty_cnt > 0xffff) { 210 221 dev_err(meson->chip.dev, "unable to get duty cycle\n"); 211 222 return -EINVAL; ··· 222 233 return 0; 223 234 } 224 235 225 - static void meson_pwm_enable(struct meson_pwm *meson, 226 - struct meson_pwm_channel *channel, 227 - unsigned int id) 236 + static void meson_pwm_enable(struct meson_pwm *meson, struct pwm_device *pwm) 228 237 { 229 - u32 value, clk_shift, clk_enable, enable; 230 - unsigned int offset; 238 + struct meson_pwm_channel *channel = pwm_get_chip_data(pwm); 239 + struct meson_pwm_channel_data *channel_data; 231 240 unsigned long flags; 241 + u32 value; 232 242 233 - switch (id) { 234 - case 0: 235 - clk_shift = MISC_A_CLK_DIV_SHIFT; 236 - clk_enable = MISC_A_CLK_EN; 237 - enable = MISC_A_EN; 238 - offset = REG_PWM_A; 239 - break; 240 - 241 - case 1: 242 - clk_shift = MISC_B_CLK_DIV_SHIFT; 243 - clk_enable = MISC_B_CLK_EN; 244 - enable = MISC_B_EN; 245 - offset = REG_PWM_B; 246 - break; 247 - 248 - default: 249 - return; 250 - } 243 + channel_data = &meson_pwm_per_channel_data[pwm->hwpwm]; 251 244 252 245 spin_lock_irqsave(&meson->lock, flags); 253 246 254 247 value = readl(meson->base + REG_MISC_AB); 255 - value &= ~(MISC_CLK_DIV_MASK << clk_shift); 256 - value |= channel->pre_div << clk_shift; 257 - value |= clk_enable; 248 + value &= ~(MISC_CLK_DIV_MASK << channel_data->clk_div_shift); 249 + value |= channel->pre_div << channel_data->clk_div_shift; 250 + value |= channel_data->clk_en_mask; 258 251 writel(value, meson->base + REG_MISC_AB); 259 252 260 - value = (channel->hi << PWM_HIGH_SHIFT) | channel->lo; 261 - writel(value, meson->base + offset); 253 + value = FIELD_PREP(PWM_HIGH_MASK, channel->hi) | 254 + FIELD_PREP(PWM_LOW_MASK, channel->lo); 255 + writel(value, meson->base + channel_data->reg_offset); 262 256 263 257 value = readl(meson->base + REG_MISC_AB); 264 - value |= enable; 258 + value |= channel_data->pwm_en_mask; 265 259 writel(value, meson->base + REG_MISC_AB); 266 260 267 261 spin_unlock_irqrestore(&meson->lock, flags); 268 262 } 269 263 270 - static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id) 264 + static void meson_pwm_disable(struct meson_pwm *meson, struct pwm_device *pwm) 271 265 { 272 - u32 value, enable; 273 266 unsigned long flags; 274 - 275 - switch (id) { 276 - case 0: 277 - enable = MISC_A_EN; 278 - break; 279 - 280 - case 1: 281 - enable = MISC_B_EN; 282 - break; 283 - 284 - default: 285 - return; 286 - } 267 + u32 value; 287 268 288 269 spin_lock_irqsave(&meson->lock, flags); 289 270 290 271 value = readl(meson->base + REG_MISC_AB); 291 - value &= ~enable; 272 + value &= ~meson_pwm_per_channel_data[pwm->hwpwm].pwm_en_mask; 292 273 writel(value, meson->base + REG_MISC_AB); 293 274 294 275 spin_unlock_irqrestore(&meson->lock, flags); ··· 275 316 return -EINVAL; 276 317 277 318 if (!state->enabled) { 278 - meson_pwm_disable(meson, pwm->hwpwm); 279 - channel->state.enabled = false; 319 + if (state->polarity == PWM_POLARITY_INVERSED) { 320 + /* 321 + * This IP block revision doesn't have an "always high" 322 + * setting which we can use for "inverted disabled". 323 + * Instead we achieve this using the same settings 324 + * that we use a pre_div of 0 (to get the shortest 325 + * possible duration for one "count") and 326 + * "period == duty_cycle". This results in a signal 327 + * which is LOW for one "count", while being HIGH for 328 + * the rest of the (so the signal is HIGH for slightly 329 + * less than 100% of the period, but this is the best 330 + * we can achieve). 331 + */ 332 + channel->pre_div = 0; 333 + channel->hi = ~0; 334 + channel->lo = 0; 280 335 281 - return 0; 282 - } 283 - 284 - if (state->period != channel->state.period || 285 - state->duty_cycle != channel->state.duty_cycle || 286 - state->polarity != channel->state.polarity) { 287 - if (state->polarity != channel->state.polarity) { 288 - if (state->polarity == PWM_POLARITY_NORMAL) 289 - meson->inverter_mask |= BIT(pwm->hwpwm); 290 - else 291 - meson->inverter_mask &= ~BIT(pwm->hwpwm); 336 + meson_pwm_enable(meson, pwm); 337 + } else { 338 + meson_pwm_disable(meson, pwm); 292 339 } 293 - 294 - err = meson_pwm_calc(meson, channel, pwm->hwpwm, 295 - state->duty_cycle, state->period); 340 + } else { 341 + err = meson_pwm_calc(meson, pwm, state); 296 342 if (err < 0) 297 343 return err; 298 344 299 - channel->state.polarity = state->polarity; 300 - channel->state.period = state->period; 301 - channel->state.duty_cycle = state->duty_cycle; 302 - } 303 - 304 - if (state->enabled && !channel->state.enabled) { 305 - meson_pwm_enable(meson, channel, pwm->hwpwm); 306 - channel->state.enabled = true; 345 + meson_pwm_enable(meson, pwm); 307 346 } 308 347 309 348 return 0; 349 + } 350 + 351 + static unsigned int meson_pwm_cnt_to_ns(struct pwm_chip *chip, 352 + struct pwm_device *pwm, u32 cnt) 353 + { 354 + struct meson_pwm *meson = to_meson_pwm(chip); 355 + struct meson_pwm_channel *channel; 356 + unsigned long fin_freq; 357 + u32 fin_ns; 358 + 359 + /* to_meson_pwm() can only be used after .get_state() is called */ 360 + channel = &meson->channels[pwm->hwpwm]; 361 + 362 + fin_freq = clk_get_rate(channel->clk); 363 + if (fin_freq == 0) 364 + return 0; 365 + 366 + fin_ns = div_u64(NSEC_PER_SEC, fin_freq); 367 + 368 + return cnt * fin_ns * (channel->pre_div + 1); 310 369 } 311 370 312 371 static void meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm, 313 372 struct pwm_state *state) 314 373 { 315 374 struct meson_pwm *meson = to_meson_pwm(chip); 316 - u32 value, mask; 375 + struct meson_pwm_channel_data *channel_data; 376 + struct meson_pwm_channel *channel; 377 + u32 value, tmp; 317 378 318 379 if (!state) 319 380 return; 320 381 321 - switch (pwm->hwpwm) { 322 - case 0: 323 - mask = MISC_A_EN; 324 - break; 325 - 326 - case 1: 327 - mask = MISC_B_EN; 328 - break; 329 - 330 - default: 331 - return; 332 - } 382 + channel = &meson->channels[pwm->hwpwm]; 383 + channel_data = &meson_pwm_per_channel_data[pwm->hwpwm]; 333 384 334 385 value = readl(meson->base + REG_MISC_AB); 335 - state->enabled = (value & mask) != 0; 386 + 387 + tmp = channel_data->pwm_en_mask | channel_data->clk_en_mask; 388 + state->enabled = (value & tmp) == tmp; 389 + 390 + tmp = value >> channel_data->clk_div_shift; 391 + channel->pre_div = FIELD_GET(MISC_CLK_DIV_MASK, tmp); 392 + 393 + value = readl(meson->base + channel_data->reg_offset); 394 + 395 + channel->lo = FIELD_GET(PWM_LOW_MASK, value); 396 + channel->hi = FIELD_GET(PWM_HIGH_MASK, value); 397 + 398 + if (channel->lo == 0) { 399 + state->period = meson_pwm_cnt_to_ns(chip, pwm, channel->hi); 400 + state->duty_cycle = state->period; 401 + } else if (channel->lo >= channel->hi) { 402 + state->period = meson_pwm_cnt_to_ns(chip, pwm, 403 + channel->lo + channel->hi); 404 + state->duty_cycle = meson_pwm_cnt_to_ns(chip, pwm, 405 + channel->hi); 406 + } else { 407 + state->period = 0; 408 + state->duty_cycle = 0; 409 + } 336 410 } 337 411 338 412 static const struct pwm_ops meson_pwm_ops = { ··· 425 433 .num_parents = ARRAY_SIZE(pwm_axg_ao_parent_names), 426 434 }; 427 435 436 + static const char * const pwm_g12a_ao_ab_parent_names[] = { 437 + "xtal", "aoclk81", "fclk_div4", "fclk_div5" 438 + }; 439 + 440 + static const struct meson_pwm_data pwm_g12a_ao_ab_data = { 441 + .parent_names = pwm_g12a_ao_ab_parent_names, 442 + .num_parents = ARRAY_SIZE(pwm_g12a_ao_ab_parent_names), 443 + }; 444 + 428 445 static const char * const pwm_g12a_ao_cd_parent_names[] = { 429 - "aoclk81", "xtal", 446 + "xtal", "aoclk81", 430 447 }; 431 448 432 449 static const struct meson_pwm_data pwm_g12a_ao_cd_data = { ··· 479 478 }, 480 479 { 481 480 .compatible = "amlogic,meson-g12a-ao-pwm-ab", 482 - .data = &pwm_axg_ao_data 481 + .data = &pwm_g12a_ao_ab_data 483 482 }, 484 483 { 485 484 .compatible = "amlogic,meson-g12a-ao-pwm-cd", ··· 489 488 }; 490 489 MODULE_DEVICE_TABLE(of, meson_pwm_matches); 491 490 492 - static int meson_pwm_init_channels(struct meson_pwm *meson, 493 - struct meson_pwm_channel *channels) 491 + static int meson_pwm_init_channels(struct meson_pwm *meson) 494 492 { 495 493 struct device *dev = meson->chip.dev; 496 494 struct clk_init_data init; ··· 498 498 int err; 499 499 500 500 for (i = 0; i < meson->chip.npwm; i++) { 501 - struct meson_pwm_channel *channel = &channels[i]; 501 + struct meson_pwm_channel *channel = &meson->channels[i]; 502 502 503 503 snprintf(name, sizeof(name), "%s#mux%u", dev_name(dev), i); 504 504 ··· 509 509 init.num_parents = meson->data->num_parents; 510 510 511 511 channel->mux.reg = meson->base + REG_MISC_AB; 512 - channel->mux.shift = mux_reg_shifts[i]; 513 - channel->mux.mask = BIT(MISC_CLK_SEL_WIDTH) - 1; 512 + channel->mux.shift = 513 + meson_pwm_per_channel_data[i].clk_sel_shift; 514 + channel->mux.mask = MISC_CLK_SEL_MASK; 514 515 channel->mux.flags = 0; 515 516 channel->mux.lock = &meson->lock; 516 517 channel->mux.table = NULL; ··· 526 525 527 526 snprintf(name, sizeof(name), "clkin%u", i); 528 527 529 - channel->clk_parent = devm_clk_get(dev, name); 530 - if (IS_ERR(channel->clk_parent)) { 531 - err = PTR_ERR(channel->clk_parent); 532 - if (err == -EPROBE_DEFER) 533 - return err; 534 - 535 - channel->clk_parent = NULL; 536 - } 528 + channel->clk_parent = devm_clk_get_optional(dev, name); 529 + if (IS_ERR(channel->clk_parent)) 530 + return PTR_ERR(channel->clk_parent); 537 531 } 538 532 539 533 return 0; 540 534 } 541 535 542 - static void meson_pwm_add_channels(struct meson_pwm *meson, 543 - struct meson_pwm_channel *channels) 544 - { 545 - unsigned int i; 546 - 547 - for (i = 0; i < meson->chip.npwm; i++) 548 - pwm_set_chip_data(&meson->chip.pwms[i], &channels[i]); 549 - } 550 - 551 536 static int meson_pwm_probe(struct platform_device *pdev) 552 537 { 553 - struct meson_pwm_channel *channels; 554 538 struct meson_pwm *meson; 555 539 struct resource *regs; 556 540 int err; ··· 553 567 meson->chip.dev = &pdev->dev; 554 568 meson->chip.ops = &meson_pwm_ops; 555 569 meson->chip.base = -1; 556 - meson->chip.npwm = 2; 570 + meson->chip.npwm = MESON_NUM_PWMS; 557 571 meson->chip.of_xlate = of_pwm_xlate_with_flags; 558 572 meson->chip.of_pwm_n_cells = 3; 559 573 560 574 meson->data = of_device_get_match_data(&pdev->dev); 561 - meson->inverter_mask = BIT(meson->chip.npwm) - 1; 562 575 563 - channels = devm_kcalloc(&pdev->dev, meson->chip.npwm, 564 - sizeof(*channels), GFP_KERNEL); 565 - if (!channels) 566 - return -ENOMEM; 567 - 568 - err = meson_pwm_init_channels(meson, channels); 576 + err = meson_pwm_init_channels(meson); 569 577 if (err < 0) 570 578 return err; 571 579 ··· 568 588 dev_err(&pdev->dev, "failed to register PWM chip: %d\n", err); 569 589 return err; 570 590 } 571 - 572 - meson_pwm_add_channels(meson, channels); 573 591 574 592 platform_set_drvdata(pdev, meson); 575 593
-39
drivers/pwm/pwm-rcar.c
··· 254 254 }; 255 255 MODULE_DEVICE_TABLE(of, rcar_pwm_of_table); 256 256 257 - #ifdef CONFIG_PM_SLEEP 258 - static struct pwm_device *rcar_pwm_dev_to_pwm_dev(struct device *dev) 259 - { 260 - struct rcar_pwm_chip *rcar_pwm = dev_get_drvdata(dev); 261 - struct pwm_chip *chip = &rcar_pwm->chip; 262 - 263 - return &chip->pwms[0]; 264 - } 265 - 266 - static int rcar_pwm_suspend(struct device *dev) 267 - { 268 - struct pwm_device *pwm = rcar_pwm_dev_to_pwm_dev(dev); 269 - 270 - if (!test_bit(PWMF_REQUESTED, &pwm->flags)) 271 - return 0; 272 - 273 - pm_runtime_put(dev); 274 - 275 - return 0; 276 - } 277 - 278 - static int rcar_pwm_resume(struct device *dev) 279 - { 280 - struct pwm_device *pwm = rcar_pwm_dev_to_pwm_dev(dev); 281 - struct pwm_state state; 282 - 283 - if (!test_bit(PWMF_REQUESTED, &pwm->flags)) 284 - return 0; 285 - 286 - pm_runtime_get_sync(dev); 287 - 288 - pwm_get_state(pwm, &state); 289 - 290 - return rcar_pwm_apply(pwm->chip, pwm, &state); 291 - } 292 - #endif /* CONFIG_PM_SLEEP */ 293 - static SIMPLE_DEV_PM_OPS(rcar_pwm_pm_ops, rcar_pwm_suspend, rcar_pwm_resume); 294 - 295 257 static struct platform_driver rcar_pwm_driver = { 296 258 .probe = rcar_pwm_probe, 297 259 .remove = rcar_pwm_remove, 298 260 .driver = { 299 261 .name = "pwm-rcar", 300 - .pm = &rcar_pwm_pm_ops, 301 262 .of_match_table = of_match_ptr(rcar_pwm_of_table), 302 263 } 303 264 };
+339
drivers/pwm/pwm-sifive.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Copyright (C) 2017-2018 SiFive 4 + * For SiFive's PWM IP block documentation please refer Chapter 14 of 5 + * Reference Manual : https://static.dev.sifive.com/FU540-C000-v1.0.pdf 6 + * 7 + * Limitations: 8 + * - When changing both duty cycle and period, we cannot prevent in 9 + * software that the output might produce a period with mixed 10 + * settings (new period length and old duty cycle). 11 + * - The hardware cannot generate a 100% duty cycle. 12 + * - The hardware generates only inverted output. 13 + */ 14 + #include <linux/clk.h> 15 + #include <linux/io.h> 16 + #include <linux/module.h> 17 + #include <linux/platform_device.h> 18 + #include <linux/pwm.h> 19 + #include <linux/slab.h> 20 + #include <linux/bitfield.h> 21 + 22 + /* Register offsets */ 23 + #define PWM_SIFIVE_PWMCFG 0x0 24 + #define PWM_SIFIVE_PWMCOUNT 0x8 25 + #define PWM_SIFIVE_PWMS 0x10 26 + #define PWM_SIFIVE_PWMCMP0 0x20 27 + 28 + /* PWMCFG fields */ 29 + #define PWM_SIFIVE_PWMCFG_SCALE GENMASK(3, 0) 30 + #define PWM_SIFIVE_PWMCFG_STICKY BIT(8) 31 + #define PWM_SIFIVE_PWMCFG_ZERO_CMP BIT(9) 32 + #define PWM_SIFIVE_PWMCFG_DEGLITCH BIT(10) 33 + #define PWM_SIFIVE_PWMCFG_EN_ALWAYS BIT(12) 34 + #define PWM_SIFIVE_PWMCFG_EN_ONCE BIT(13) 35 + #define PWM_SIFIVE_PWMCFG_CENTER BIT(16) 36 + #define PWM_SIFIVE_PWMCFG_GANG BIT(24) 37 + #define PWM_SIFIVE_PWMCFG_IP BIT(28) 38 + 39 + /* PWM_SIFIVE_SIZE_PWMCMP is used to calculate offset for pwmcmpX registers */ 40 + #define PWM_SIFIVE_SIZE_PWMCMP 4 41 + #define PWM_SIFIVE_CMPWIDTH 16 42 + #define PWM_SIFIVE_DEFAULT_PERIOD 10000000 43 + 44 + struct pwm_sifive_ddata { 45 + struct pwm_chip chip; 46 + struct mutex lock; /* lock to protect user_count */ 47 + struct notifier_block notifier; 48 + struct clk *clk; 49 + void __iomem *regs; 50 + unsigned int real_period; 51 + unsigned int approx_period; 52 + int user_count; 53 + }; 54 + 55 + static inline 56 + struct pwm_sifive_ddata *pwm_sifive_chip_to_ddata(struct pwm_chip *c) 57 + { 58 + return container_of(c, struct pwm_sifive_ddata, chip); 59 + } 60 + 61 + static int pwm_sifive_request(struct pwm_chip *chip, struct pwm_device *pwm) 62 + { 63 + struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip); 64 + 65 + mutex_lock(&ddata->lock); 66 + ddata->user_count++; 67 + mutex_unlock(&ddata->lock); 68 + 69 + return 0; 70 + } 71 + 72 + static void pwm_sifive_free(struct pwm_chip *chip, struct pwm_device *pwm) 73 + { 74 + struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip); 75 + 76 + mutex_lock(&ddata->lock); 77 + ddata->user_count--; 78 + mutex_unlock(&ddata->lock); 79 + } 80 + 81 + static void pwm_sifive_update_clock(struct pwm_sifive_ddata *ddata, 82 + unsigned long rate) 83 + { 84 + unsigned long long num; 85 + unsigned long scale_pow; 86 + int scale; 87 + u32 val; 88 + /* 89 + * The PWM unit is used with pwmzerocmp=0, so the only way to modify the 90 + * period length is using pwmscale which provides the number of bits the 91 + * counter is shifted before being feed to the comparators. A period 92 + * lasts (1 << (PWM_SIFIVE_CMPWIDTH + pwmscale)) clock ticks. 93 + * (1 << (PWM_SIFIVE_CMPWIDTH + scale)) * 10^9/rate = period 94 + */ 95 + scale_pow = div64_ul(ddata->approx_period * (u64)rate, NSEC_PER_SEC); 96 + scale = clamp(ilog2(scale_pow) - PWM_SIFIVE_CMPWIDTH, 0, 0xf); 97 + 98 + val = PWM_SIFIVE_PWMCFG_EN_ALWAYS | 99 + FIELD_PREP(PWM_SIFIVE_PWMCFG_SCALE, scale); 100 + writel(val, ddata->regs + PWM_SIFIVE_PWMCFG); 101 + 102 + /* As scale <= 15 the shift operation cannot overflow. */ 103 + num = (unsigned long long)NSEC_PER_SEC << (PWM_SIFIVE_CMPWIDTH + scale); 104 + ddata->real_period = div64_ul(num, rate); 105 + dev_dbg(ddata->chip.dev, 106 + "New real_period = %u ns\n", ddata->real_period); 107 + } 108 + 109 + static void pwm_sifive_get_state(struct pwm_chip *chip, struct pwm_device *pwm, 110 + struct pwm_state *state) 111 + { 112 + struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip); 113 + u32 duty, val; 114 + 115 + duty = readl(ddata->regs + PWM_SIFIVE_PWMCMP0 + 116 + pwm->hwpwm * PWM_SIFIVE_SIZE_PWMCMP); 117 + 118 + state->enabled = duty > 0; 119 + 120 + val = readl(ddata->regs + PWM_SIFIVE_PWMCFG); 121 + if (!(val & PWM_SIFIVE_PWMCFG_EN_ALWAYS)) 122 + state->enabled = false; 123 + 124 + state->period = ddata->real_period; 125 + state->duty_cycle = 126 + (u64)duty * ddata->real_period >> PWM_SIFIVE_CMPWIDTH; 127 + state->polarity = PWM_POLARITY_INVERSED; 128 + } 129 + 130 + static int pwm_sifive_enable(struct pwm_chip *chip, bool enable) 131 + { 132 + struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip); 133 + int ret; 134 + 135 + if (enable) { 136 + ret = clk_enable(ddata->clk); 137 + if (ret) { 138 + dev_err(ddata->chip.dev, "Enable clk failed\n"); 139 + return ret; 140 + } 141 + } 142 + 143 + if (!enable) 144 + clk_disable(ddata->clk); 145 + 146 + return 0; 147 + } 148 + 149 + static int pwm_sifive_apply(struct pwm_chip *chip, struct pwm_device *pwm, 150 + struct pwm_state *state) 151 + { 152 + struct pwm_sifive_ddata *ddata = pwm_sifive_chip_to_ddata(chip); 153 + struct pwm_state cur_state; 154 + unsigned int duty_cycle; 155 + unsigned long long num; 156 + bool enabled; 157 + int ret = 0; 158 + u32 frac; 159 + 160 + if (state->polarity != PWM_POLARITY_INVERSED) 161 + return -EINVAL; 162 + 163 + ret = clk_enable(ddata->clk); 164 + if (ret) { 165 + dev_err(ddata->chip.dev, "Enable clk failed\n"); 166 + return ret; 167 + } 168 + 169 + mutex_lock(&ddata->lock); 170 + cur_state = pwm->state; 171 + enabled = cur_state.enabled; 172 + 173 + duty_cycle = state->duty_cycle; 174 + if (!state->enabled) 175 + duty_cycle = 0; 176 + 177 + /* 178 + * The problem of output producing mixed setting as mentioned at top, 179 + * occurs here. To minimize the window for this problem, we are 180 + * calculating the register values first and then writing them 181 + * consecutively 182 + */ 183 + num = (u64)duty_cycle * (1U << PWM_SIFIVE_CMPWIDTH); 184 + frac = DIV_ROUND_CLOSEST_ULL(num, state->period); 185 + /* The hardware cannot generate a 100% duty cycle */ 186 + frac = min(frac, (1U << PWM_SIFIVE_CMPWIDTH) - 1); 187 + 188 + if (state->period != ddata->approx_period) { 189 + if (ddata->user_count != 1) { 190 + ret = -EBUSY; 191 + goto exit; 192 + } 193 + ddata->approx_period = state->period; 194 + pwm_sifive_update_clock(ddata, clk_get_rate(ddata->clk)); 195 + } 196 + 197 + writel(frac, ddata->regs + PWM_SIFIVE_PWMCMP0 + 198 + pwm->hwpwm * PWM_SIFIVE_SIZE_PWMCMP); 199 + 200 + if (state->enabled != enabled) 201 + pwm_sifive_enable(chip, state->enabled); 202 + 203 + exit: 204 + clk_disable(ddata->clk); 205 + mutex_unlock(&ddata->lock); 206 + return ret; 207 + } 208 + 209 + static const struct pwm_ops pwm_sifive_ops = { 210 + .request = pwm_sifive_request, 211 + .free = pwm_sifive_free, 212 + .get_state = pwm_sifive_get_state, 213 + .apply = pwm_sifive_apply, 214 + .owner = THIS_MODULE, 215 + }; 216 + 217 + static int pwm_sifive_clock_notifier(struct notifier_block *nb, 218 + unsigned long event, void *data) 219 + { 220 + struct clk_notifier_data *ndata = data; 221 + struct pwm_sifive_ddata *ddata = 222 + container_of(nb, struct pwm_sifive_ddata, notifier); 223 + 224 + if (event == POST_RATE_CHANGE) 225 + pwm_sifive_update_clock(ddata, ndata->new_rate); 226 + 227 + return NOTIFY_OK; 228 + } 229 + 230 + static int pwm_sifive_probe(struct platform_device *pdev) 231 + { 232 + struct device *dev = &pdev->dev; 233 + struct pwm_sifive_ddata *ddata; 234 + struct pwm_chip *chip; 235 + struct resource *res; 236 + int ret; 237 + 238 + ddata = devm_kzalloc(dev, sizeof(*ddata), GFP_KERNEL); 239 + if (!ddata) 240 + return -ENOMEM; 241 + 242 + mutex_init(&ddata->lock); 243 + chip = &ddata->chip; 244 + chip->dev = dev; 245 + chip->ops = &pwm_sifive_ops; 246 + chip->of_xlate = of_pwm_xlate_with_flags; 247 + chip->of_pwm_n_cells = 3; 248 + chip->base = -1; 249 + chip->npwm = 4; 250 + 251 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 252 + ddata->regs = devm_ioremap_resource(dev, res); 253 + if (IS_ERR(ddata->regs)) { 254 + dev_err(dev, "Unable to map IO resources\n"); 255 + return PTR_ERR(ddata->regs); 256 + } 257 + 258 + ddata->clk = devm_clk_get(dev, NULL); 259 + if (IS_ERR(ddata->clk)) { 260 + if (PTR_ERR(ddata->clk) != -EPROBE_DEFER) 261 + dev_err(dev, "Unable to find controller clock\n"); 262 + return PTR_ERR(ddata->clk); 263 + } 264 + 265 + ret = clk_prepare_enable(ddata->clk); 266 + if (ret) { 267 + dev_err(dev, "failed to enable clock for pwm: %d\n", ret); 268 + return ret; 269 + } 270 + 271 + /* Watch for changes to underlying clock frequency */ 272 + ddata->notifier.notifier_call = pwm_sifive_clock_notifier; 273 + ret = clk_notifier_register(ddata->clk, &ddata->notifier); 274 + if (ret) { 275 + dev_err(dev, "failed to register clock notifier: %d\n", ret); 276 + goto disable_clk; 277 + } 278 + 279 + ret = pwmchip_add(chip); 280 + if (ret < 0) { 281 + dev_err(dev, "cannot register PWM: %d\n", ret); 282 + goto unregister_clk; 283 + } 284 + 285 + platform_set_drvdata(pdev, ddata); 286 + dev_dbg(dev, "SiFive PWM chip registered %d PWMs\n", chip->npwm); 287 + 288 + return 0; 289 + 290 + unregister_clk: 291 + clk_notifier_unregister(ddata->clk, &ddata->notifier); 292 + disable_clk: 293 + clk_disable_unprepare(ddata->clk); 294 + 295 + return ret; 296 + } 297 + 298 + static int pwm_sifive_remove(struct platform_device *dev) 299 + { 300 + struct pwm_sifive_ddata *ddata = platform_get_drvdata(dev); 301 + bool is_enabled = false; 302 + struct pwm_device *pwm; 303 + int ret, ch; 304 + 305 + for (ch = 0; ch < ddata->chip.npwm; ch++) { 306 + pwm = &ddata->chip.pwms[ch]; 307 + if (pwm->state.enabled) { 308 + is_enabled = true; 309 + break; 310 + } 311 + } 312 + if (is_enabled) 313 + clk_disable(ddata->clk); 314 + 315 + clk_disable_unprepare(ddata->clk); 316 + ret = pwmchip_remove(&ddata->chip); 317 + clk_notifier_unregister(ddata->clk, &ddata->notifier); 318 + 319 + return ret; 320 + } 321 + 322 + static const struct of_device_id pwm_sifive_of_match[] = { 323 + { .compatible = "sifive,pwm0" }, 324 + {}, 325 + }; 326 + MODULE_DEVICE_TABLE(of, pwm_sifive_of_match); 327 + 328 + static struct platform_driver pwm_sifive_driver = { 329 + .probe = pwm_sifive_probe, 330 + .remove = pwm_sifive_remove, 331 + .driver = { 332 + .name = "pwm-sifive", 333 + .of_match_table = pwm_sifive_of_match, 334 + }, 335 + }; 336 + module_platform_driver(pwm_sifive_driver); 337 + 338 + MODULE_DESCRIPTION("SiFive PWM driver"); 339 + MODULE_LICENSE("GPL v2");
+25
drivers/pwm/pwm-stm32-lp.c
··· 13 13 #include <linux/mfd/stm32-lptimer.h> 14 14 #include <linux/module.h> 15 15 #include <linux/of.h> 16 + #include <linux/pinctrl/consumer.h> 16 17 #include <linux/platform_device.h> 17 18 #include <linux/pwm.h> 18 19 ··· 224 223 return pwmchip_remove(&priv->chip); 225 224 } 226 225 226 + static int __maybe_unused stm32_pwm_lp_suspend(struct device *dev) 227 + { 228 + struct stm32_pwm_lp *priv = dev_get_drvdata(dev); 229 + struct pwm_state state; 230 + 231 + pwm_get_state(&priv->chip.pwms[0], &state); 232 + if (state.enabled) { 233 + dev_err(dev, "The consumer didn't stop us (%s)\n", 234 + priv->chip.pwms[0].label); 235 + return -EBUSY; 236 + } 237 + 238 + return pinctrl_pm_select_sleep_state(dev); 239 + } 240 + 241 + static int __maybe_unused stm32_pwm_lp_resume(struct device *dev) 242 + { 243 + return pinctrl_pm_select_default_state(dev); 244 + } 245 + 246 + static SIMPLE_DEV_PM_OPS(stm32_pwm_lp_pm_ops, stm32_pwm_lp_suspend, 247 + stm32_pwm_lp_resume); 248 + 227 249 static const struct of_device_id stm32_pwm_lp_of_match[] = { 228 250 { .compatible = "st,stm32-pwm-lp", }, 229 251 {}, ··· 259 235 .driver = { 260 236 .name = "stm32-pwm-lp", 261 237 .of_match_table = of_match_ptr(stm32_pwm_lp_of_match), 238 + .pm = &stm32_pwm_lp_pm_ops, 262 239 }, 263 240 }; 264 241 module_platform_driver(stm32_pwm_lp_driver);
+2
drivers/pwm/pwm-stm32.c
··· 608 608 priv->regmap = ddata->regmap; 609 609 priv->clk = ddata->clk; 610 610 priv->max_arr = ddata->max_arr; 611 + priv->chip.of_xlate = of_pwm_xlate_with_flags; 612 + priv->chip.of_pwm_n_cells = 3; 611 613 612 614 if (!priv->regmap || !priv->clk) 613 615 return -EINVAL;
+102
drivers/pwm/sysfs.c
··· 18 18 struct device child; 19 19 struct pwm_device *pwm; 20 20 struct mutex lock; 21 + struct pwm_state suspend; 21 22 }; 22 23 23 24 static struct pwm_export *child_to_pwm_export(struct device *child) ··· 373 372 }; 374 373 ATTRIBUTE_GROUPS(pwm_chip); 375 374 375 + /* takes export->lock on success */ 376 + static struct pwm_export *pwm_class_get_state(struct device *parent, 377 + struct pwm_device *pwm, 378 + struct pwm_state *state) 379 + { 380 + struct device *child; 381 + struct pwm_export *export; 382 + 383 + if (!test_bit(PWMF_EXPORTED, &pwm->flags)) 384 + return NULL; 385 + 386 + child = device_find_child(parent, pwm, pwm_unexport_match); 387 + if (!child) 388 + return NULL; 389 + 390 + export = child_to_pwm_export(child); 391 + put_device(child); /* for device_find_child() */ 392 + 393 + mutex_lock(&export->lock); 394 + pwm_get_state(pwm, state); 395 + 396 + return export; 397 + } 398 + 399 + static int pwm_class_apply_state(struct pwm_export *export, 400 + struct pwm_device *pwm, 401 + struct pwm_state *state) 402 + { 403 + int ret = pwm_apply_state(pwm, state); 404 + 405 + /* release lock taken in pwm_class_get_state */ 406 + mutex_unlock(&export->lock); 407 + 408 + return ret; 409 + } 410 + 411 + static int pwm_class_resume_npwm(struct device *parent, unsigned int npwm) 412 + { 413 + struct pwm_chip *chip = dev_get_drvdata(parent); 414 + unsigned int i; 415 + int ret = 0; 416 + 417 + for (i = 0; i < npwm; i++) { 418 + struct pwm_device *pwm = &chip->pwms[i]; 419 + struct pwm_state state; 420 + struct pwm_export *export; 421 + 422 + export = pwm_class_get_state(parent, pwm, &state); 423 + if (!export) 424 + continue; 425 + 426 + state.enabled = export->suspend.enabled; 427 + ret = pwm_class_apply_state(export, pwm, &state); 428 + if (ret < 0) 429 + break; 430 + } 431 + 432 + return ret; 433 + } 434 + 435 + static int __maybe_unused pwm_class_suspend(struct device *parent) 436 + { 437 + struct pwm_chip *chip = dev_get_drvdata(parent); 438 + unsigned int i; 439 + int ret = 0; 440 + 441 + for (i = 0; i < chip->npwm; i++) { 442 + struct pwm_device *pwm = &chip->pwms[i]; 443 + struct pwm_state state; 444 + struct pwm_export *export; 445 + 446 + export = pwm_class_get_state(parent, pwm, &state); 447 + if (!export) 448 + continue; 449 + 450 + export->suspend = state; 451 + state.enabled = false; 452 + ret = pwm_class_apply_state(export, pwm, &state); 453 + if (ret < 0) { 454 + /* 455 + * roll back the PWM devices that were disabled by 456 + * this suspend function. 457 + */ 458 + pwm_class_resume_npwm(parent, i); 459 + break; 460 + } 461 + } 462 + 463 + return ret; 464 + } 465 + 466 + static int __maybe_unused pwm_class_resume(struct device *parent) 467 + { 468 + struct pwm_chip *chip = dev_get_drvdata(parent); 469 + 470 + return pwm_class_resume_npwm(parent, chip->npwm); 471 + } 472 + 473 + static SIMPLE_DEV_PM_OPS(pwm_class_pm_ops, pwm_class_suspend, pwm_class_resume); 474 + 376 475 static struct class pwm_class = { 377 476 .name = "pwm", 378 477 .owner = THIS_MODULE, 379 478 .dev_groups = pwm_chip_groups, 479 + .pm = &pwm_class_pm_ops, 380 480 }; 381 481 382 482 static int pwmchip_sysfs_match(struct device *parent, const void *data)
+14 -2
include/linux/pwm.h
··· 405 405 const struct of_phandle_args *args); 406 406 407 407 struct pwm_device *pwm_get(struct device *dev, const char *con_id); 408 - struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id); 408 + struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np, 409 + const char *con_id); 409 410 void pwm_put(struct pwm_device *pwm); 410 411 411 412 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id); 412 413 struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, 413 414 const char *con_id); 415 + struct pwm_device *devm_fwnode_pwm_get(struct device *dev, 416 + struct fwnode_handle *fwnode, 417 + const char *con_id); 414 418 void devm_pwm_put(struct device *dev, struct pwm_device *pwm); 415 419 #else 416 420 static inline struct pwm_device *pwm_request(int pwm_id, const char *label) ··· 497 493 return ERR_PTR(-ENODEV); 498 494 } 499 495 500 - static inline struct pwm_device *of_pwm_get(struct device_node *np, 496 + static inline struct pwm_device *of_pwm_get(struct device *dev, 497 + struct device_node *np, 501 498 const char *con_id) 502 499 { 503 500 return ERR_PTR(-ENODEV); ··· 517 512 static inline struct pwm_device *devm_of_pwm_get(struct device *dev, 518 513 struct device_node *np, 519 514 const char *con_id) 515 + { 516 + return ERR_PTR(-ENODEV); 517 + } 518 + 519 + static inline struct pwm_device * 520 + devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode, 521 + const char *con_id) 520 522 { 521 523 return ERR_PTR(-ENODEV); 522 524 }