History

Merge tag 'pwm/for-4.8-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 improve some aspects of the atomic API as well as make use of this new API in the regulator framework to allow properly dealing with critical regulators controlled by a PWM. Aside from that there's a bunch of updates and cleanups for existing drivers, as well as the addition of new drivers for the Broadcom iProc, STMPE and ChromeOS EC controllers" * tag 'pwm/for-4.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry.reding/linux-pwm: (44 commits) regulator: pwm: Document pwm-dutycycle-unit and pwm-dutycycle-range regulator: pwm: Support extra continuous mode cases pwm: Add ChromeOS EC PWM driver dt-bindings: pwm: Add binding for ChromeOS EC PWM mfd: cros_ec: Add EC_PWM function definitions mfd: cros_ec: Add cros_ec_cmd_xfer_status() helper pwm: atmel: Use of_device_get_match_data() pwm: atmel: Fix checkpatch warnings pwm: atmel: Fix disabling of PWM channels dt-bindings: pwm: Add R-Car H3 device tree bindings pwm: rcar: Use ARCH_RENESAS pwm: tegra: Add support for Tegra186 dt-bindings: pwm: tegra: Add compatible string for Tegra186 pwm: tegra: Avoid overflow when calculating duty cycle pwm: tegra: Allow 100 % duty cycle pwm: tegra: Add support for reset control pwm: tegra: Rename mmio_base to regs pwm: tegra: Remove useless padding pwm: tegra: Drop NUM_PWM macro pwm: lpc32xx: Set PWM_PIN_LEVEL bit to default value ...

Linus Torvalds committed 10 years ago

Commit a157b3a2 parents 32199ec 53de7c229 files · +1561 −283Browse files

Unified · Split

29 files
  1. Documentation/ABI/testing/sysfs-class-pwm +9 −0
  2. Documentation/devicetree/bindings/pwm/brcm,iproc-pwm.txt +21 −0
  3. Documentation/devicetree/bindings/pwm/google,cros-ec-pwm.txt +23 −0
  4. Documentation/devicetree/bindings/pwm/nvidia,tegra20-pwm.txt +8 −4
  5. Documentation/devicetree/bindings/pwm/renesas,pwm-rcar.txt +1 −0
  6. Documentation/devicetree/bindings/pwm/st,stmpe-pwm.txt +18 −0
  7. Documentation/devicetree/bindings/regulator/pwm-regulator.txt +19 −0
  8. drivers/platform/chrome/cros_ec_proto.c +17 −0
  9. drivers/pwm/Kconfig +25 −1
  10. drivers/pwm/Makefile +3 −0
  11. drivers/pwm/core.c +27 −0
  12. drivers/pwm/pwm-atmel.c +17 −13
  13. drivers/pwm/pwm-bcm-iproc.c +277 −0
  14. drivers/pwm/pwm-cros-ec.c +260 −0
  15. drivers/pwm/pwm-lpc32xx.c +7 −0
  16. drivers/pwm/pwm-lpss-pci.c +1 −0
  17. drivers/pwm/pwm-lpss.c +14 −12
  18. drivers/pwm/pwm-rockchip.c +132 −46
  19. drivers/pwm/pwm-stmpe.c +319 −0
  20. drivers/pwm/pwm-tegra.c +52 −17
  21. drivers/pwm/pwm-tiecap.c +9 −28
  22. drivers/pwm/pwm-tiehrpwm.c +9 −29
  23. drivers/pwm/pwm-tipwmss.c +0 −49
  24. drivers/pwm/pwm-tipwmss.h +0 −39
  25. drivers/pwm/sysfs.c +17 −0
  26. drivers/regulator/pwm-regulator.c +118 −44
  27. include/linux/mfd/cros_ec.h +15 −0
  28. include/linux/mfd/cros_ec_commands.h +31 −0
  29. include/linux/pwm.h +112 −1
Documentation/ABI/testing/sysfs-class-pwm+9−0
@@ -77,3 +77,12 @@ Description: · 76 unmodified lines
7777 Enable/disable the PWM signal.
7878 0 is disabled
7979 1 is enabled
80+
81+What: /sys/class/pwm/pwmchipN/pwmX/capture
82+Date: June 2016
83+KernelVersion: 4.8
84+Contact: Lee Jones <lee.jones@linaro.org>
85+Description:
86+ Capture information about a PWM signal. The output format is a
87+ pair unsigned integers (period and duty cycle), separated by a
88+ single space.
Documentation/devicetree/bindings/pwm/brcm,iproc-pwm.txt+21−0
@@ -0,0 +1,21 @@
1+Broadcom iProc PWM controller device tree bindings
2+
3+This controller has 4 channels.
4+
5+Required Properties :
6+- compatible: must be "brcm,iproc-pwm"
7+- reg: physical base address and length of the controller's registers
8+- clocks: phandle + clock specifier pair for the external clock
9+- #pwm-cells: Should be 3. See pwm.txt in this directory for a
10+ description of the cells format.
11+
12+Refer to clocks/clock-bindings.txt for generic clock consumer properties.
13+
14+Example:
15+
16+pwm: pwm@18031000 {
17+ compatible = "brcm,iproc-pwm";
18+ reg = <0x18031000 0x28>;
19+ clocks = <&osc>;
20+ #pwm-cells = <3>;
21+};
Documentation/devicetree/bindings/pwm/google,cros-ec-pwm.txt+23−0
@@ -0,0 +1,23 @@
1+* PWM controlled by ChromeOS EC
2+
3+Google's ChromeOS EC PWM is a simple PWM attached to the Embedded Controller
4+(EC) and controlled via a host-command interface.
5+
6+An EC PWM node should be only found as a sub-node of the EC node (see
7+Documentation/devicetree/bindings/mfd/cros-ec.txt).
8+
9+Required properties:
10+- compatible: Must contain "google,cros-ec-pwm"
11+- #pwm-cells: Should be 1. The cell specifies the PWM index.
12+
13+Example:
14+ cros-ec@0 {
15+ compatible = "google,cros-ec-spi";
16+
17+ ...
18+
19+ cros_ec_pwm: ec-pwm {
20+ compatible = "google,cros-ec-pwm";
21+ #pwm-cells = <1>;
22+ };
23+ };
Documentation/devicetree/bindings/pwm/nvidia,tegra20-pwm.txt+8−4
@@ -1,10 +1,14 @@
11 Tegra SoC PWFM controller
22
33 Required properties:
4−- compatible: For Tegra20, must contain "nvidia,tegra20-pwm". For Tegra30,
5− must contain "nvidia,tegra30-pwm". Otherwise, must contain
6− "nvidia,<chip>-pwm", plus one of the above, where <chip> is tegra114,
7− tegra124, tegra132, or tegra210.
4+- compatible: Must be:
5+ - "nvidia,tegra20-pwm": for Tegra20
6+ - "nvidia,tegra30-pwm", "nvidia,tegra20-pwm": for Tegra30
7+ - "nvidia,tegra114-pwm", "nvidia,tegra20-pwm": for Tegra114
8+ - "nvidia,tegra124-pwm", "nvidia,tegra20-pwm": for Tegra124
9+ - "nvidia,tegra132-pwm", "nvidia,tegra20-pwm": for Tegra132
10+ - "nvidia,tegra210-pwm", "nvidia,tegra20-pwm": for Tegra210
11+ - "nvidia,tegra186-pwm": for Tegra186
812 - reg: physical base address and length of the controller's registers
913 - #pwm-cells: should be 2. See pwm.txt in this directory for a description of
1014 the cells format.
Documentation/devicetree/bindings/pwm/renesas,pwm-rcar.txt+1−0
@@ -7,6 +7,7 @@ Required Properties: · 6 unmodified lines
77 - "renesas,pwm-r8a7790": for R-Car H2
88 - "renesas,pwm-r8a7791": for R-Car M2-W
99 - "renesas,pwm-r8a7794": for R-Car E2
10+ - "renesas,pwm-r8a7795": for R-Car H3
1011 - reg: base address and length of the registers block for the PWM.
1112 - #pwm-cells: should be 2. See pwm.txt in this directory for a description of
1213 the cells format.
Documentation/devicetree/bindings/pwm/st,stmpe-pwm.txt+18−0
@@ -0,0 +1,18 @@
1+== ST STMPE PWM controller ==
2+
3+This is a PWM block embedded in the ST Microelectronics STMPE
4+(ST Multi-Purpose Expander) chips. The PWM is registered as a
5+subdevices of the STMPE MFD device.
6+
7+Required properties:
8+- compatible: should be:
9+ - "st,stmpe-pwm"
10+- #pwm-cells: should be 2. See pwm.txt in this directory for a description of
11+ the cells format.
12+
13+Example:
14+
15+pwm0: pwm {
16+ compatible = "st,stmpe-pwm";
17+ #pwm-cells = <2>;
18+};
Documentation/devicetree/bindings/regulator/pwm-regulator.txt+19−0
@@ -34,6 +34,18 @@ Only required for Voltage Table Mode: · 33 unmodified lines
3434 First cell is voltage in microvolts (uV)
3535 Second cell is duty-cycle in percent (%)
3636
37+Optional properties for Continuous mode:
38+- pwm-dutycycle-unit: Integer value encoding the duty cycle unit. If not
39+ defined, <100> is assumed, meaning that
40+ pwm-dutycycle-range contains values expressed in
41+ percent.
42+
43+- pwm-dutycycle-range: Should contain 2 entries. The first entry is encoding
44+ the dutycycle for regulator-min-microvolt and the
45+ second one the dutycycle for regulator-max-microvolt.
46+ Duty cycle values are expressed in pwm-dutycycle-unit.
47+ If not defined, <0 100> is assumed.
48+
3749 NB: To be clear, if voltage-table is provided, then the device will be used
3850 in Voltage Table Mode. If no voltage-table is provided, then the device will
3951 be used in Continuous Voltage Mode.
@@ -53,6 +65,13 @@ Continuous Voltage With Enable GPIO Example: · 13 unmodified lines
5365 regulator-min-microvolt = <1016000>;
5466 regulator-max-microvolt = <1114000>;
5567 regulator-name = "vdd_logic";
68+ /* unit == per-mille */
69+ pwm-dutycycle-unit = <1000>;
70+ /*
71+ * Inverted PWM logic, and the duty cycle range is limited
72+ * to 30%-70%.
73+ */
74+ pwm-dutycycle-range <700 300>; /* */
5675 };
5776
5877 Voltage Table Example:
drivers/platform/chrome/cros_ec_proto.c+17−0
@@ -380,3 +380,20 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, · 379 unmodified lines
380380 return ret;
381381 }
382382 EXPORT_SYMBOL(cros_ec_cmd_xfer);
383+
384+int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
385+ struct cros_ec_command *msg)
386+{
387+ int ret;
388+
389+ ret = cros_ec_cmd_xfer(ec_dev, msg);
390+ if (ret < 0) {
391+ dev_err(ec_dev->dev, "Command xfer error (err:%d)\n", ret);
392+ } else if (msg->result != EC_RES_SUCCESS) {
393+ dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
394+ return -EPROTO;
395+ }
396+
397+ return ret;
398+}
399+EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
drivers/pwm/Kconfig+25−1
@@ -74,6 +74,16 @@ config PWM_ATMEL_TCB · 73 unmodified lines
7474 To compile this driver as a module, choose M here: the module
7575 will be called pwm-atmel-tcb.
7676
77+config PWM_BCM_IPROC
78+ tristate "iProc PWM support"
79+ depends on ARCH_BCM_IPROC
80+ help
81+ Generic PWM framework driver for Broadcom iProc PWM block. This
82+ block is used in Broadcom iProc SoC's.
83+
84+ To compile this driver as a module, choose M here: the module
85+ will be called pwm-bcm-iproc.
86+
7787 config PWM_BCM_KONA
7888 tristate "Kona PWM support"
7989 depends on ARCH_BCM_MOBILE
@@ -137,6 +147,13 @@ config PWM_CRC · 57 unmodified lines
137147 Generic PWM framework driver for Crystalcove (CRC) PMIC based PWM
138148 control.
139149
150+config PWM_CROS_EC
151+ tristate "ChromeOS EC PWM driver"
152+ depends on MFD_CROS_EC
153+ help
154+ PWM driver for exposing a PWM attached to the ChromeOS Embedded
155+ Controller.
156+
140157 config PWM_EP93XX
141158 tristate "Cirrus Logic EP93xx PWM support"
142159 depends on ARCH_EP93XX
@@ -305,7 +322,7 @@ config PWM_PXA · 162 unmodified lines
305322
306323 config PWM_RCAR
307324 tristate "Renesas R-Car PWM support"
308− depends on ARCH_RCAR_GEN1 || ARCH_RCAR_GEN2 || COMPILE_TEST
325+ depends on ARCH_RENESAS || COMPILE_TEST
309326 depends on HAS_IOMEM
310327 help
311328 This driver exposes the PWM Timer controller found in Renesas
@@ -362,6 +379,13 @@ config PWM_STI · 50 unmodified lines
362379 To compile this driver as a module, choose M here: the module
363380 will be called pwm-sti.
364381
382+config PWM_STMPE
383+ bool "STMPE expander PWM export"
384+ depends on MFD_STMPE
385+ help
386+ This enables support for the PWMs found in the STMPE I/O
387+ expanders.
388+
365389 config PWM_SUN4I
366390 tristate "Allwinner PWM support"
367391 depends on ARCH_SUNXI || COMPILE_TEST
drivers/pwm/Makefile+3−0
@@ -4,6 +4,7 @@ obj-$(CONFIG_PWM_AB8500) += pwm-ab8500.o · 3 unmodified lines
44 obj-$(CONFIG_PWM_ATMEL) += pwm-atmel.o
55 obj-$(CONFIG_PWM_ATMEL_HLCDC_PWM) += pwm-atmel-hlcdc.o
66 obj-$(CONFIG_PWM_ATMEL_TCB) += pwm-atmel-tcb.o
7+obj-$(CONFIG_PWM_BCM_IPROC) += pwm-bcm-iproc.o
78 obj-$(CONFIG_PWM_BCM_KONA) += pwm-bcm-kona.o
89 obj-$(CONFIG_PWM_BCM2835) += pwm-bcm2835.o
910 obj-$(CONFIG_PWM_BERLIN) += pwm-berlin.o
@@ -11,6 +12,7 @@ obj-$(CONFIG_PWM_BFIN) += pwm-bfin.o · 1 unmodified line
1112 obj-$(CONFIG_PWM_BRCMSTB) += pwm-brcmstb.o
1213 obj-$(CONFIG_PWM_CLPS711X) += pwm-clps711x.o
1314 obj-$(CONFIG_PWM_CRC) += pwm-crc.o
15+obj-$(CONFIG_PWM_CROS_EC) += pwm-cros-ec.o
1416 obj-$(CONFIG_PWM_EP93XX) += pwm-ep93xx.o
1517 obj-$(CONFIG_PWM_FSL_FTM) += pwm-fsl-ftm.o
1618 obj-$(CONFIG_PWM_IMG) += pwm-img.o
@@ -34,6 +36,7 @@ obj-$(CONFIG_PWM_ROCKCHIP) += pwm-rockchip.o · 17 unmodified lines
3436 obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o
3537 obj-$(CONFIG_PWM_SPEAR) += pwm-spear.o
3638 obj-$(CONFIG_PWM_STI) += pwm-sti.o
39+obj-$(CONFIG_PWM_STMPE) += pwm-stmpe.o
3740 obj-$(CONFIG_PWM_SUN4I) += pwm-sun4i.o
3841 obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o
3942 obj-$(CONFIG_PWM_TIECAP) += pwm-tiecap.o
drivers/pwm/core.c+27−0
@@ -525,6 +525,33 @@ int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state) · 524 unmodified lines
525525 }
526526 EXPORT_SYMBOL_GPL(pwm_apply_state);
527527
528+/**
529+ * pwm_capture() - capture and report a PWM signal
530+ * @pwm: PWM device
531+ * @result: structure to fill with capture result
532+ * @timeout: time to wait, in milliseconds, before giving up on capture
533+ *
534+ * Returns: 0 on success or a negative error code on failure.
535+ */
536+int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
537+ unsigned long timeout)
538+{
539+ int err;
540+
541+ if (!pwm || !pwm->chip->ops)
542+ return -EINVAL;
543+
544+ if (!pwm->chip->ops->capture)
545+ return -ENOSYS;
546+
547+ mutex_lock(&pwm_lock);
548+ err = pwm->chip->ops->capture(pwm->chip, pwm, result, timeout);
549+ mutex_unlock(&pwm_lock);
550+
551+ return err;
552+}
553+EXPORT_SYMBOL_GPL(pwm_capture);
554+
528555 /**
529556 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
530557 * @pwm: PWM device
drivers/pwm/pwm-atmel.c+17−13
@@ -64,7 +64,8 @@ struct atmel_pwm_chip { · 63 unmodified lines
6464 void __iomem *base;
6565
6666 unsigned int updated_pwms;
67− struct mutex isr_lock; /* ISR is cleared when read, ensure only one thread does that */
67+ /* ISR is cleared when read, ensure only one thread does that */
68+ struct mutex isr_lock;
6869
6970 void (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
7071 unsigned long dty, unsigned long prd);
@@ -271,6 +272,16 @@ static void atmel_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) · 200 unmodified lines
271272 mutex_unlock(&atmel_pwm->isr_lock);
272273 atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);
273274
275+ /*
276+ * Wait for the PWM channel disable operation to be effective before
277+ * stopping the clock.
278+ */
279+ timeout = jiffies + 2 * HZ;
280+
281+ while ((atmel_pwm_readl(atmel_pwm, PWM_SR) & (1 << pwm->hwpwm)) &&
282+ time_before(jiffies, timeout))
283+ usleep_range(10, 100);
284+
274285 clk_disable(atmel_pwm->clk);
275286 }
276287
@@ -324,21 +335,14 @@ MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids); · 47 unmodified lines
324335 static inline const struct atmel_pwm_data *
325336 atmel_pwm_get_driver_data(struct platform_device *pdev)
326337 {
327− if (pdev->dev.of_node) {
328− const struct of_device_id *match;
329−
330− match = of_match_device(atmel_pwm_dt_ids, &pdev->dev);
331− if (!match)
332− return NULL;
338+ const struct platform_device_id *id;
333339
334− return match->data;
335− } else {
336− const struct platform_device_id *id;
340+ if (pdev->dev.of_node)
341+ return of_device_get_match_data(&pdev->dev);
337342
338− id = platform_get_device_id(pdev);
343+ id = platform_get_device_id(pdev);
339344
340− return (struct atmel_pwm_data *)id->driver_data;
341− }
345+ return (struct atmel_pwm_data *)id->driver_data;
342346 }
343347
344348 static int atmel_pwm_probe(struct platform_device *pdev)
drivers/pwm/pwm-bcm-iproc.c+277−0
@@ -0,0 +1,277 @@
1+/*
2+ * Copyright (C) 2016 Broadcom
3+ *
4+ * This program is free software; you can redistribute it and/or
5+ * modify it under the terms of the GNU General Public License as
6+ * published by the Free Software Foundation version 2.
7+ *
8+ * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9+ * kind, whether express or implied; without even the implied warranty
10+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ */
13+
14+#include <linux/clk.h>
15+#include <linux/delay.h>
16+#include <linux/err.h>
17+#include <linux/io.h>
18+#include <linux/math64.h>
19+#include <linux/module.h>
20+#include <linux/of.h>
21+#include <linux/platform_device.h>
22+#include <linux/pwm.h>
23+
24+#define IPROC_PWM_CTRL_OFFSET 0x00
25+#define IPROC_PWM_CTRL_TYPE_SHIFT(ch) (15 + (ch))
26+#define IPROC_PWM_CTRL_POLARITY_SHIFT(ch) (8 + (ch))
27+#define IPROC_PWM_CTRL_EN_SHIFT(ch) (ch)
28+
29+#define IPROC_PWM_PERIOD_OFFSET(ch) (0x04 + ((ch) << 3))
30+#define IPROC_PWM_PERIOD_MIN 0x02
31+#define IPROC_PWM_PERIOD_MAX 0xffff
32+
33+#define IPROC_PWM_DUTY_CYCLE_OFFSET(ch) (0x08 + ((ch) << 3))
34+#define IPROC_PWM_DUTY_CYCLE_MIN 0x00
35+#define IPROC_PWM_DUTY_CYCLE_MAX 0xffff
36+
37+#define IPROC_PWM_PRESCALE_OFFSET 0x24
38+#define IPROC_PWM_PRESCALE_BITS 0x06
39+#define IPROC_PWM_PRESCALE_SHIFT(ch) ((3 - (ch)) * \
40+ IPROC_PWM_PRESCALE_BITS)
41+#define IPROC_PWM_PRESCALE_MASK(ch) (IPROC_PWM_PRESCALE_MAX << \
42+ IPROC_PWM_PRESCALE_SHIFT(ch))
43+#define IPROC_PWM_PRESCALE_MIN 0x00
44+#define IPROC_PWM_PRESCALE_MAX 0x3f
45+
46+struct iproc_pwmc {
47+ struct pwm_chip chip;
48+ void __iomem *base;
49+ struct clk *clk;
50+};
51+
52+static inline struct iproc_pwmc *to_iproc_pwmc(struct pwm_chip *chip)
53+{
54+ return container_of(chip, struct iproc_pwmc, chip);
55+}
56+
57+static void iproc_pwmc_enable(struct iproc_pwmc *ip, unsigned int channel)
58+{
59+ u32 value;
60+
61+ value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
62+ value |= 1 << IPROC_PWM_CTRL_EN_SHIFT(channel);
63+ writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
64+
65+ /* must be a 400 ns delay between clearing and setting enable bit */
66+ ndelay(400);
67+}
68+
69+static void iproc_pwmc_disable(struct iproc_pwmc *ip, unsigned int channel)
70+{
71+ u32 value;
72+
73+ value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
74+ value &= ~(1 << IPROC_PWM_CTRL_EN_SHIFT(channel));
75+ writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
76+
77+ /* must be a 400 ns delay between clearing and setting enable bit */
78+ ndelay(400);
79+}
80+
81+static void iproc_pwmc_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
82+ struct pwm_state *state)
83+{
84+ struct iproc_pwmc *ip = to_iproc_pwmc(chip);
85+ u64 tmp, multi, rate;
86+ u32 value, prescale;
87+
88+ rate = clk_get_rate(ip->clk);
89+
90+ value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
91+
92+ if (value & BIT(IPROC_PWM_CTRL_EN_SHIFT(pwm->hwpwm)))
93+ state->enabled = true;
94+ else
95+ state->enabled = false;
96+
97+ if (value & BIT(IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm)))
98+ state->polarity = PWM_POLARITY_NORMAL;
99+ else
100+ state->polarity = PWM_POLARITY_INVERSED;
101+
102+ value = readl(ip->base + IPROC_PWM_PRESCALE_OFFSET);
103+ prescale = value >> IPROC_PWM_PRESCALE_SHIFT(pwm->hwpwm);
104+ prescale &= IPROC_PWM_PRESCALE_MAX;
105+
106+ multi = NSEC_PER_SEC * (prescale + 1);
107+
108+ value = readl(ip->base + IPROC_PWM_PERIOD_OFFSET(pwm->hwpwm));
109+ tmp = (value & IPROC_PWM_PERIOD_MAX) * multi;
110+ state->period = div64_u64(tmp, rate);
111+
112+ value = readl(ip->base + IPROC_PWM_DUTY_CYCLE_OFFSET(pwm->hwpwm));
113+ tmp = (value & IPROC_PWM_PERIOD_MAX) * multi;
114+ state->duty_cycle = div64_u64(tmp, rate);
115+}
116+
117+static int iproc_pwmc_apply(struct pwm_chip *chip, struct pwm_device *pwm,
118+ struct pwm_state *state)
119+{
120+ unsigned long prescale = IPROC_PWM_PRESCALE_MIN;
121+ struct iproc_pwmc *ip = to_iproc_pwmc(chip);
122+ u32 value, period, duty;
123+ u64 rate;
124+
125+ rate = clk_get_rate(ip->clk);
126+
127+ /*
128+ * Find period count, duty count and prescale to suit duty_cycle and
129+ * period. This is done according to formulas described below:
130+ *
131+ * period_ns = 10^9 * (PRESCALE + 1) * PC / PWM_CLK_RATE
132+ * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
133+ *
134+ * PC = (PWM_CLK_RATE * period_ns) / (10^9 * (PRESCALE + 1))
135+ * DC = (PWM_CLK_RATE * duty_ns) / (10^9 * (PRESCALE + 1))
136+ */
137+ while (1) {
138+ u64 value, div;
139+
140+ div = NSEC_PER_SEC * (prescale + 1);
141+ value = rate * state->period;
142+ period = div64_u64(value, div);
143+ value = rate * state->duty_cycle;
144+ duty = div64_u64(value, div);
145+
146+ if (period < IPROC_PWM_PERIOD_MIN ||
147+ duty < IPROC_PWM_DUTY_CYCLE_MIN)
148+ return -EINVAL;
149+
150+ if (period <= IPROC_PWM_PERIOD_MAX &&
151+ duty <= IPROC_PWM_DUTY_CYCLE_MAX)
152+ break;
153+
154+ /* Otherwise, increase prescale and recalculate counts */
155+ if (++prescale > IPROC_PWM_PRESCALE_MAX)
156+ return -EINVAL;
157+ }
158+
159+ iproc_pwmc_disable(ip, pwm->hwpwm);
160+
161+ /* Set prescale */
162+ value = readl(ip->base + IPROC_PWM_PRESCALE_OFFSET);
163+ value &= ~IPROC_PWM_PRESCALE_MASK(pwm->hwpwm);
164+ value |= prescale << IPROC_PWM_PRESCALE_SHIFT(pwm->hwpwm);
165+ writel(value, ip->base + IPROC_PWM_PRESCALE_OFFSET);
166+
167+ /* set period and duty cycle */
168+ writel(period, ip->base + IPROC_PWM_PERIOD_OFFSET(pwm->hwpwm));
169+ writel(duty, ip->base + IPROC_PWM_DUTY_CYCLE_OFFSET(pwm->hwpwm));
170+
171+ /* set polarity */
172+ value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
173+
174+ if (state->polarity == PWM_POLARITY_NORMAL)
175+ value |= 1 << IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm);
176+ else
177+ value &= ~(1 << IPROC_PWM_CTRL_POLARITY_SHIFT(pwm->hwpwm));
178+
179+ writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
180+
181+ if (state->enabled)
182+ iproc_pwmc_enable(ip, pwm->hwpwm);
183+
184+ return 0;
185+}
186+
187+static const struct pwm_ops iproc_pwm_ops = {
188+ .apply = iproc_pwmc_apply,
189+ .get_state = iproc_pwmc_get_state,
190+};
191+
192+static int iproc_pwmc_probe(struct platform_device *pdev)
193+{
194+ struct iproc_pwmc *ip;
195+ struct resource *res;
196+ unsigned int i;
197+ u32 value;
198+ int ret;
199+
200+ ip = devm_kzalloc(&pdev->dev, sizeof(*ip), GFP_KERNEL);
201+ if (!ip)
202+ return -ENOMEM;
203+
204+ platform_set_drvdata(pdev, ip);
205+
206+ ip->chip.dev = &pdev->dev;
207+ ip->chip.ops = &iproc_pwm_ops;
208+ ip->chip.base = -1;
209+ ip->chip.npwm = 4;
210+ ip->chip.of_xlate = of_pwm_xlate_with_flags;
211+ ip->chip.of_pwm_n_cells = 3;
212+
213+ res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
214+ ip->base = devm_ioremap_resource(&pdev->dev, res);
215+ if (IS_ERR(ip->base))
216+ return PTR_ERR(ip->base);
217+
218+ ip->clk = devm_clk_get(&pdev->dev, NULL);
219+ if (IS_ERR(ip->clk)) {
220+ dev_err(&pdev->dev, "failed to get clock: %ld\n",
221+ PTR_ERR(ip->clk));
222+ return PTR_ERR(ip->clk);
223+ }
224+
225+ ret = clk_prepare_enable(ip->clk);
226+ if (ret < 0) {
227+ dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
228+ return ret;
229+ }
230+
231+ /* Set full drive and normal polarity for all channels */
232+ value = readl(ip->base + IPROC_PWM_CTRL_OFFSET);
233+
234+ for (i = 0; i < ip->chip.npwm; i++) {
235+ value &= ~(1 << IPROC_PWM_CTRL_TYPE_SHIFT(i));
236+ value |= 1 << IPROC_PWM_CTRL_POLARITY_SHIFT(i);
237+ }
238+
239+ writel(value, ip->base + IPROC_PWM_CTRL_OFFSET);
240+
241+ ret = pwmchip_add(&ip->chip);
242+ if (ret < 0) {
243+ dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
244+ clk_disable_unprepare(ip->clk);
245+ }
246+
247+ return ret;
248+}
249+
250+static int iproc_pwmc_remove(struct platform_device *pdev)
251+{
252+ struct iproc_pwmc *ip = platform_get_drvdata(pdev);
253+
254+ clk_disable_unprepare(ip->clk);
255+
256+ return pwmchip_remove(&ip->chip);
257+}
258+
259+static const struct of_device_id bcm_iproc_pwmc_dt[] = {
260+ { .compatible = "brcm,iproc-pwm" },
261+ { },
262+};
263+MODULE_DEVICE_TABLE(of, bcm_iproc_pwmc_dt);
264+
265+static struct platform_driver iproc_pwmc_driver = {
266+ .driver = {
267+ .name = "bcm-iproc-pwm",
268+ .of_match_table = bcm_iproc_pwmc_dt,
269+ },
270+ .probe = iproc_pwmc_probe,
271+ .remove = iproc_pwmc_remove,
272+};
273+module_platform_driver(iproc_pwmc_driver);
274+
275+MODULE_AUTHOR("Yendapally Reddy Dhananjaya Reddy <yendapally.reddy@broadcom.com>");
276+MODULE_DESCRIPTION("Broadcom iProc PWM driver");
277+MODULE_LICENSE("GPL v2");
drivers/pwm/pwm-cros-ec.c+260−0
@@ -0,0 +1,260 @@
1+/*
2+ * Copyright (C) 2016 Google, Inc
3+ *
4+ * This program is free software; you can redistribute it and/or modify it
5+ * under the terms of the GNU General Public License version 2, as published by
6+ * the Free Software Foundation.
7+ *
8+ * Expose a PWM controlled by the ChromeOS EC to the host processor.
9+ */
10+
11+#include <linux/module.h>
12+#include <linux/mfd/cros_ec.h>
13+#include <linux/mfd/cros_ec_commands.h>
14+#include <linux/platform_device.h>
15+#include <linux/pwm.h>
16+#include <linux/slab.h>
17+
18+/**
19+ * struct cros_ec_pwm_device - Driver data for EC PWM
20+ *
21+ * @dev: Device node
22+ * @ec: Pointer to EC device
23+ * @chip: PWM controller chip
24+ */
25+struct cros_ec_pwm_device {
26+ struct device *dev;
27+ struct cros_ec_device *ec;
28+ struct pwm_chip chip;
29+};
30+
31+static inline struct cros_ec_pwm_device *pwm_to_cros_ec_pwm(struct pwm_chip *c)
32+{
33+ return container_of(c, struct cros_ec_pwm_device, chip);
34+}
35+
36+static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
37+{
38+ struct {
39+ struct cros_ec_command msg;
40+ struct ec_params_pwm_set_duty params;
41+ } buf;
42+ struct ec_params_pwm_set_duty *params = &buf.params;
43+ struct cros_ec_command *msg = &buf.msg;
44+
45+ memset(&buf, 0, sizeof(buf));
46+
47+ msg->version = 0;
48+ msg->command = EC_CMD_PWM_SET_DUTY;
49+ msg->insize = 0;
50+ msg->outsize = sizeof(*params);
51+
52+ params->duty = duty;
53+ params->pwm_type = EC_PWM_TYPE_GENERIC;
54+ params->index = index;
55+
56+ return cros_ec_cmd_xfer_status(ec, msg);
57+}
58+
59+static int __cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index,
60+ u32 *result)
61+{
62+ struct {
63+ struct cros_ec_command msg;
64+ union {
65+ struct ec_params_pwm_get_duty params;
66+ struct ec_response_pwm_get_duty resp;
67+ };
68+ } buf;
69+ struct ec_params_pwm_get_duty *params = &buf.params;
70+ struct ec_response_pwm_get_duty *resp = &buf.resp;
71+ struct cros_ec_command *msg = &buf.msg;
72+ int ret;
73+
74+ memset(&buf, 0, sizeof(buf));
75+
76+ msg->version = 0;
77+ msg->command = EC_CMD_PWM_GET_DUTY;
78+ msg->insize = sizeof(*params);
79+ msg->outsize = sizeof(*resp);
80+
81+ params->pwm_type = EC_PWM_TYPE_GENERIC;
82+ params->index = index;
83+
84+ ret = cros_ec_cmd_xfer_status(ec, msg);
85+ if (result)
86+ *result = msg->result;
87+ if (ret < 0)
88+ return ret;
89+
90+ return resp->duty;
91+}
92+
93+static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index)
94+{
95+ return __cros_ec_pwm_get_duty(ec, index, NULL);
96+}
97+
98+static int cros_ec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
99+ struct pwm_state *state)
100+{
101+ struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip);
102+ int duty_cycle;
103+
104+ /* The EC won't let us change the period */
105+ if (state->period != EC_PWM_MAX_DUTY)
106+ return -EINVAL;
107+
108+ /*
109+ * EC doesn't separate the concept of duty cycle and enabled, but
110+ * kernel does. Translate.
111+ */
112+ duty_cycle = state->enabled ? state->duty_cycle : 0;
113+
114+ return cros_ec_pwm_set_duty(ec_pwm->ec, pwm->hwpwm, duty_cycle);
115+}
116+
117+static void cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
118+ struct pwm_state *state)
119+{
120+ struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip);
121+ int ret;
122+
123+ ret = cros_ec_pwm_get_duty(ec_pwm->ec, pwm->hwpwm);
124+ if (ret < 0) {
125+ dev_err(chip->dev, "error getting initial duty: %d\n", ret);
126+ return;
127+ }
128+
129+ state->enabled = (ret > 0);
130+ state->period = EC_PWM_MAX_DUTY;
131+
132+ /* Note that "disabled" and "duty cycle == 0" are treated the same */
133+ state->duty_cycle = ret;
134+}
135+
136+static struct pwm_device *
137+cros_ec_pwm_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
138+{
139+ struct pwm_device *pwm;
140+
141+ if (args->args[0] >= pc->npwm)
142+ return ERR_PTR(-EINVAL);
143+
144+ pwm = pwm_request_from_chip(pc, args->args[0], NULL);
145+ if (IS_ERR(pwm))
146+ return pwm;
147+
148+ /* The EC won't let us change the period */
149+ pwm->args.period = EC_PWM_MAX_DUTY;
150+
151+ return pwm;
152+}
153+
154+static const struct pwm_ops cros_ec_pwm_ops = {
155+ .get_state = cros_ec_pwm_get_state,
156+ .apply = cros_ec_pwm_apply,
157+ .owner = THIS_MODULE,
158+};
159+
160+static int cros_ec_num_pwms(struct cros_ec_device *ec)
161+{
162+ int i, ret;
163+
164+ /* The index field is only 8 bits */
165+ for (i = 0; i <= U8_MAX; i++) {
166+ u32 result = 0;
167+
168+ ret = __cros_ec_pwm_get_duty(ec, i, &result);
169+ /* We want to parse EC protocol errors */
170+ if (ret < 0 && !(ret == -EPROTO && result))
171+ return ret;
172+
173+ /*
174+ * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM
175+ * responses; everything else is treated as an error.
176+ */
177+ if (result == EC_RES_INVALID_COMMAND)
178+ return -ENODEV;
179+ else if (result == EC_RES_INVALID_PARAM)
180+ return i;
181+ else if (result)
182+ return -EPROTO;
183+ }
184+
185+ return U8_MAX;
186+}
187+
188+static int cros_ec_pwm_probe(struct platform_device *pdev)
189+{
190+ struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
191+ struct device *dev = &pdev->dev;
192+ struct cros_ec_pwm_device *ec_pwm;
193+ struct pwm_chip *chip;
194+ int ret;
195+
196+ if (!ec) {
197+ dev_err(dev, "no parent EC device\n");
198+ return -EINVAL;
199+ }
200+
201+ ec_pwm = devm_kzalloc(dev, sizeof(*ec_pwm), GFP_KERNEL);
202+ if (!ec_pwm)
203+ return -ENOMEM;
204+ chip = &ec_pwm->chip;
205+ ec_pwm->ec = ec;
206+
207+ /* PWM chip */
208+ chip->dev = dev;
209+ chip->ops = &cros_ec_pwm_ops;
210+ chip->of_xlate = cros_ec_pwm_xlate;
211+ chip->of_pwm_n_cells = 1;
212+ chip->base = -1;
213+ ret = cros_ec_num_pwms(ec);
214+ if (ret < 0) {
215+ dev_err(dev, "Couldn't find PWMs: %d\n", ret);
216+ return ret;
217+ }
218+ chip->npwm = ret;
219+ dev_dbg(dev, "Probed %u PWMs\n", chip->npwm);
220+
221+ ret = pwmchip_add(chip);
222+ if (ret < 0) {
223+ dev_err(dev, "cannot register PWM: %d\n", ret);
224+ return ret;
225+ }
226+
227+ platform_set_drvdata(pdev, ec_pwm);
228+
229+ return ret;
230+}
231+
232+static int cros_ec_pwm_remove(struct platform_device *dev)
233+{
234+ struct cros_ec_pwm_device *ec_pwm = platform_get_drvdata(dev);
235+ struct pwm_chip *chip = &ec_pwm->chip;
236+
237+ return pwmchip_remove(chip);
238+}
239+
240+#ifdef CONFIG_OF
241+static const struct of_device_id cros_ec_pwm_of_match[] = {
242+ { .compatible = "google,cros-ec-pwm" },
243+ {},
244+};
245+MODULE_DEVICE_TABLE(of, cros_ec_pwm_of_match);
246+#endif
247+
248+static struct platform_driver cros_ec_pwm_driver = {
249+ .probe = cros_ec_pwm_probe,
250+ .remove = cros_ec_pwm_remove,
251+ .driver = {
252+ .name = "cros-ec-pwm",
253+ .of_match_table = of_match_ptr(cros_ec_pwm_of_match),
254+ },
255+};
256+module_platform_driver(cros_ec_pwm_driver);
257+
258+MODULE_ALIAS("platform:cros-ec-pwm");
259+MODULE_DESCRIPTION("ChromeOS EC PWM driver");
260+MODULE_LICENSE("GPL v2");
drivers/pwm/pwm-lpc32xx.c+7−0
@@ -25,6 +25,7 @@ struct lpc32xx_pwm_chip { · 24 unmodified lines
2525 };
2626
2727 #define PWM_ENABLE BIT(31)
28+#define PWM_PIN_LEVEL BIT(30)
2829
2930 #define to_lpc32xx_pwm_chip(_chip) \
3031 container_of(_chip, struct lpc32xx_pwm_chip, chip)
@@ -103,6 +104,7 @@ static int lpc32xx_pwm_probe(struct platform_device *pdev) · 72 unmodified lines
103104 struct lpc32xx_pwm_chip *lpc32xx;
104105 struct resource *res;
105106 int ret;
107+ u32 val;
106108
107109 lpc32xx = devm_kzalloc(&pdev->dev, sizeof(*lpc32xx), GFP_KERNEL);
108110 if (!lpc32xx)
@@ -128,6 +130,11 @@ static int lpc32xx_pwm_probe(struct platform_device *pdev) · 19 unmodified lines
128130 return ret;
129131 }
130132
133+ /* When PWM is disable, configure the output to the default value */
134+ val = readl(lpc32xx->base + (lpc32xx->chip.pwms[0].hwpwm << 2));
135+ val &= ~PWM_PIN_LEVEL;
136+ writel(val, lpc32xx->base + (lpc32xx->chip.pwms[0].hwpwm << 2));
137+
131138 platform_set_drvdata(pdev, lpc32xx);
132139
133140 return 0;
drivers/pwm/pwm-lpss-pci.c+1−0
@@ -76,6 +76,7 @@ static const struct pci_device_id pwm_lpss_pci_ids[] = { · 75 unmodified lines
7676 { PCI_VDEVICE(INTEL, 0x0ac8), (unsigned long)&pwm_lpss_bxt_info},
7777 { PCI_VDEVICE(INTEL, 0x0f08), (unsigned long)&pwm_lpss_byt_info},
7878 { PCI_VDEVICE(INTEL, 0x0f09), (unsigned long)&pwm_lpss_byt_info},
79+ { PCI_VDEVICE(INTEL, 0x11a5), (unsigned long)&pwm_lpss_bxt_info},
7980 { PCI_VDEVICE(INTEL, 0x1ac8), (unsigned long)&pwm_lpss_bxt_info},
8081 { PCI_VDEVICE(INTEL, 0x2288), (unsigned long)&pwm_lpss_bsw_info},
8182 { PCI_VDEVICE(INTEL, 0x2289), (unsigned long)&pwm_lpss_bsw_info},
drivers/pwm/pwm-lpss.c+14−12
@@ -27,7 +27,6 @@ · 26 unmodified lines
2727 #define PWM_SW_UPDATE BIT(30)
2828 #define PWM_BASE_UNIT_SHIFT 8
2929 #define PWM_ON_TIME_DIV_MASK 0x000000ff
30−#define PWM_DIVISION_CORRECTION 0x2
3130
3231 /* Size of each PWM register space if multiple */
3332 #define PWM_SIZE 0x400
@@ -92,8 +91,8 @@ static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm, · 58 unmodified lines
9291 int duty_ns, int period_ns)
9392 {
9493 struct pwm_lpss_chip *lpwm = to_lpwm(chip);
95− u8 on_time_div;
96− unsigned long c, base_unit_range;
94+ unsigned long long on_time_div;
95+ unsigned long c = lpwm->info->clk_rate, base_unit_range;
9796 unsigned long long base_unit, freq = NSEC_PER_SEC;
9897 u32 ctrl;
9998
@@ -101,21 +100,18 @@ static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm, · 1 unmodified line
101100
102101 /*
103102 * The equation is:
104− * base_unit = ((freq / c) * base_unit_range) + correction
103+ * base_unit = round(base_unit_range * freq / c)
105104 */
106105 base_unit_range = BIT(lpwm->info->base_unit_bits);
107− base_unit = freq * base_unit_range;
106+ freq *= base_unit_range;
108107
109− c = lpwm->info->clk_rate;
110− if (!c)
111− return -EINVAL;
112−
113− do_div(base_unit, c);
114− base_unit += PWM_DIVISION_CORRECTION;
108+ base_unit = DIV_ROUND_CLOSEST_ULL(freq, c);
115109
116110 if (duty_ns <= 0)
117111 duty_ns = 1;
118− on_time_div = 255 - (255 * duty_ns / period_ns);
112+ on_time_div = 255ULL * duty_ns;
113+ do_div(on_time_div, period_ns);
114+ on_time_div = 255ULL - on_time_div;
119115
120116 pm_runtime_get_sync(chip->dev);
121117
@@ -169,6 +165,7 @@ struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r, · 47 unmodified lines
169165 const struct pwm_lpss_boardinfo *info)
170166 {
171167 struct pwm_lpss_chip *lpwm;
168+ unsigned long c;
172169 int ret;
173170
174171 lpwm = devm_kzalloc(dev, sizeof(*lpwm), GFP_KERNEL);
@@ -180,6 +177,11 @@ struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r, · 5 unmodified lines
180177 return ERR_CAST(lpwm->regs);
181178
182179 lpwm->info = info;
180+
181+ c = lpwm->info->clk_rate;
182+ if (!c)
183+ return ERR_PTR(-EINVAL);
184+
183185 lpwm->chip.dev = dev;
184186 lpwm->chip.ops = &pwm_lpss_ops;
185187 lpwm->chip.base = -1;
drivers/pwm/pwm-rockchip.c+132−46
@@ -47,10 +47,14 @@ struct rockchip_pwm_regs { · 46 unmodified lines
4747 struct rockchip_pwm_data {
4848 struct rockchip_pwm_regs regs;
4949 unsigned int prescaler;
50+ bool supports_polarity;
5051 const struct pwm_ops *ops;
5152
5253 void (*set_enable)(struct pwm_chip *chip,
53− struct pwm_device *pwm, bool enable);
54+ struct pwm_device *pwm, bool enable,
55+ enum pwm_polarity polarity);
56+ void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
57+ struct pwm_state *state);
5458 };
5559
5660 static inline struct rockchip_pwm_chip *to_rockchip_pwm_chip(struct pwm_chip *c)
@@ -59,7 +63,8 @@ static inline struct rockchip_pwm_chip *to_rockchip_pwm_chip(struct pwm_chip *c) · 2 unmodified lines
5963 }
6064
6165 static void rockchip_pwm_set_enable_v1(struct pwm_chip *chip,
62− struct pwm_device *pwm, bool enable)
66+ struct pwm_device *pwm, bool enable,
67+ enum pwm_polarity polarity)
6368 {
6469 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
6570 u32 enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN;
@@ -75,15 +80,29 @@ static void rockchip_pwm_set_enable_v1(struct pwm_chip *chip, · 9 unmodified lines
7580 writel_relaxed(val, pc->base + pc->data->regs.ctrl);
7681 }
7782
83+static void rockchip_pwm_get_state_v1(struct pwm_chip *chip,
84+ struct pwm_device *pwm,
85+ struct pwm_state *state)
86+{
87+ struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
88+ u32 enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN;
89+ u32 val;
90+
91+ val = readl_relaxed(pc->base + pc->data->regs.ctrl);
92+ if ((val & enable_conf) == enable_conf)
93+ state->enabled = true;
94+}
95+
7896 static void rockchip_pwm_set_enable_v2(struct pwm_chip *chip,
79− struct pwm_device *pwm, bool enable)
97+ struct pwm_device *pwm, bool enable,
98+ enum pwm_polarity polarity)
8099 {
81100 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
82101 u32 enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
83102 PWM_CONTINUOUS;
84103 u32 val;
85104
86− if (pwm_get_polarity(pwm) == PWM_POLARITY_INVERSED)
105+ if (polarity == PWM_POLARITY_INVERSED)
87106 enable_conf |= PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSITIVE;
88107 else
89108 enable_conf |= PWM_DUTY_POSITIVE | PWM_INACTIVE_NEGATIVE;
@@ -98,13 +117,59 @@ static void rockchip_pwm_set_enable_v2(struct pwm_chip *chip, · 8 unmodified lines
98117 writel_relaxed(val, pc->base + pc->data->regs.ctrl);
99118 }
100119
120+static void rockchip_pwm_get_state_v2(struct pwm_chip *chip,
121+ struct pwm_device *pwm,
122+ struct pwm_state *state)
123+{
124+ struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
125+ u32 enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
126+ PWM_CONTINUOUS;
127+ u32 val;
128+
129+ val = readl_relaxed(pc->base + pc->data->regs.ctrl);
130+ if ((val & enable_conf) != enable_conf)
131+ return;
132+
133+ state->enabled = true;
134+
135+ if (!(val & PWM_DUTY_POSITIVE))
136+ state->polarity = PWM_POLARITY_INVERSED;
137+}
138+
139+static void rockchip_pwm_get_state(struct pwm_chip *chip,
140+ struct pwm_device *pwm,
141+ struct pwm_state *state)
142+{
143+ struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
144+ unsigned long clk_rate;
145+ u64 tmp;
146+ int ret;
147+
148+ ret = clk_enable(pc->clk);
149+ if (ret)
150+ return;
151+
152+ clk_rate = clk_get_rate(pc->clk);
153+
154+ tmp = readl_relaxed(pc->base + pc->data->regs.period);
155+ tmp *= pc->data->prescaler * NSEC_PER_SEC;
156+ state->period = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
157+
158+ tmp = readl_relaxed(pc->base + pc->data->regs.duty);
159+ tmp *= pc->data->prescaler * NSEC_PER_SEC;
160+ state->duty_cycle = DIV_ROUND_CLOSEST_ULL(tmp, clk_rate);
161+
162+ pc->data->get_state(chip, pwm, state);
163+
164+ clk_disable(pc->clk);
165+}
166+
101167 static int rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
102168 int duty_ns, int period_ns)
103169 {
104170 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
105171 unsigned long period, duty;
106172 u64 clk_rate, div;
107− int ret;
108173
109174 clk_rate = clk_get_rate(pc->clk);
110175
@@ -114,74 +179,72 @@ static int rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, · 3 unmodified lines
114179 * default prescaler value for all practical clock rate values.
115180 */
116181 div = clk_rate * period_ns;
117− do_div(div, pc->data->prescaler * NSEC_PER_SEC);
118− period = div;
182+ period = DIV_ROUND_CLOSEST_ULL(div,
183+ pc->data->prescaler * NSEC_PER_SEC);
119184
120185 div = clk_rate * duty_ns;
121− do_div(div, pc->data->prescaler * NSEC_PER_SEC);
122− duty = div;
123−
124− ret = clk_enable(pc->clk);
125− if (ret)
126− return ret;
186+ duty = DIV_ROUND_CLOSEST_ULL(div, pc->data->prescaler * NSEC_PER_SEC);
127187
128188 writel(period, pc->base + pc->data->regs.period);
129189 writel(duty, pc->base + pc->data->regs.duty);
130− writel(0, pc->base + pc->data->regs.cntr);
131−
132− clk_disable(pc->clk);
133−
134− return 0;
135−}
136−
137−static int rockchip_pwm_set_polarity(struct pwm_chip *chip,
138− struct pwm_device *pwm,
139− enum pwm_polarity polarity)
140−{
141− /*
142− * No action needed here because pwm->polarity will be set by the core
143− * and the core will only change polarity when the PWM is not enabled.
144− * We'll handle things in set_enable().
145− */
146190
147191 return 0;
148192 }
149193
150−static int rockchip_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
194+static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
195+ struct pwm_state *state)
151196 {
152197 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
198+ struct pwm_state curstate;
199+ bool enabled;
153200 int ret;
154201
202+ pwm_get_state(pwm, &curstate);
203+ enabled = curstate.enabled;
204+
155205 ret = clk_enable(pc->clk);
156206 if (ret)
157207 return ret;
158208
159− pc->data->set_enable(chip, pwm, true);
209+ if (state->polarity != curstate.polarity && enabled) {
210+ pc->data->set_enable(chip, pwm, false, state->polarity);
211+ enabled = false;
212+ }
160213
161− return 0;
162−}
214+ ret = rockchip_pwm_config(chip, pwm, state->duty_cycle, state->period);
215+ if (ret) {
216+ if (enabled != curstate.enabled)
217+ pc->data->set_enable(chip, pwm, !enabled,
218+ state->polarity);
163219
164−static void rockchip_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
165−{
166− struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
220+ goto out;
221+ }
222+
223+ if (state->enabled != enabled)
224+ pc->data->set_enable(chip, pwm, state->enabled,
225+ state->polarity);
167226
168− pc->data->set_enable(chip, pwm, false);
227+ /*
228+ * Update the state with the real hardware, which can differ a bit
229+ * because of period/duty_cycle approximation.
230+ */
231+ rockchip_pwm_get_state(chip, pwm, state);
169232
233+out:
170234 clk_disable(pc->clk);
235+
236+ return ret;
171237 }
172238
173239 static const struct pwm_ops rockchip_pwm_ops_v1 = {
174− .config = rockchip_pwm_config,
175− .enable = rockchip_pwm_enable,
176− .disable = rockchip_pwm_disable,
240+ .get_state = rockchip_pwm_get_state,
241+ .apply = rockchip_pwm_apply,
177242 .owner = THIS_MODULE,
178243 };
179244
180245 static const struct pwm_ops rockchip_pwm_ops_v2 = {
181− .config = rockchip_pwm_config,
182− .set_polarity = rockchip_pwm_set_polarity,
183− .enable = rockchip_pwm_enable,
184− .disable = rockchip_pwm_disable,
246+ .get_state = rockchip_pwm_get_state,
247+ .apply = rockchip_pwm_apply,
185248 .owner = THIS_MODULE,
186249 };
187250
@@ -195,6 +258,7 @@ static const struct rockchip_pwm_data pwm_data_v1 = { · 7 unmodified lines
195258 .prescaler = 2,
196259 .ops = &rockchip_pwm_ops_v1,
197260 .set_enable = rockchip_pwm_set_enable_v1,
261+ .get_state = rockchip_pwm_get_state_v1,
198262 };
199263
200264 static const struct rockchip_pwm_data pwm_data_v2 = {
@@ -205,8 +269,10 @@ static const struct rockchip_pwm_data pwm_data_v2 = { · 4 unmodified lines
205269 .ctrl = 0x0c,
206270 },
207271 .prescaler = 1,
272+ .supports_polarity = true,
208273 .ops = &rockchip_pwm_ops_v2,
209274 .set_enable = rockchip_pwm_set_enable_v2,
275+ .get_state = rockchip_pwm_get_state_v2,
210276 };
211277
212278 static const struct rockchip_pwm_data pwm_data_vop = {
@@ -217,8 +283,10 @@ static const struct rockchip_pwm_data pwm_data_vop = { · 4 unmodified lines
217283 .ctrl = 0x00,
218284 },
219285 .prescaler = 1,
286+ .supports_polarity = true,
220287 .ops = &rockchip_pwm_ops_v2,
221288 .set_enable = rockchip_pwm_set_enable_v2,
289+ .get_state = rockchip_pwm_get_state_v2,
222290 };
223291
224292 static const struct of_device_id rockchip_pwm_dt_ids[] = {
@@ -253,7 +321,7 @@ static int rockchip_pwm_probe(struct platform_device *pdev) · 28 unmodified lines
253321 if (IS_ERR(pc->clk))
254322 return PTR_ERR(pc->clk);
255323
256− ret = clk_prepare(pc->clk);
324+ ret = clk_prepare_enable(pc->clk);
257325 if (ret)
258326 return ret;
259327
@@ -265,7 +333,7 @@ static int rockchip_pwm_probe(struct platform_device *pdev) · 5 unmodified lines
265333 pc->chip.base = -1;
266334 pc->chip.npwm = 1;
267335
268− if (pc->data->ops->set_polarity) {
336+ if (pc->data->supports_polarity) {
269337 pc->chip.of_xlate = of_pwm_xlate_with_flags;
270338 pc->chip.of_pwm_n_cells = 3;
271339 }
@@ -276,6 +344,10 @@ static int rockchip_pwm_probe(struct platform_device *pdev) · 4 unmodified lines
276344 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
277345 }
278346
347+ /* Keep the PWM clk enabled if the PWM appears to be up and running. */
348+ if (!pwm_is_enabled(pc->chip.pwms))
349+ clk_disable(pc->clk);
350+
279351 return ret;
280352 }
281353
@@ -283,6 +355,20 @@ static int rockchip_pwm_remove(struct platform_device *pdev) · 1 unmodified line
283355 {
284356 struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev);
285357
358+ /*
359+ * Disable the PWM clk before unpreparing it if the PWM device is still
360+ * running. This should only happen when the last PWM user left it
361+ * enabled, or when nobody requested a PWM that was previously enabled
362+ * by the bootloader.
363+ *
364+ * FIXME: Maybe the core should disable all PWM devices in
365+ * pwmchip_remove(). In this case we'd only have to call
366+ * clk_unprepare() after pwmchip_remove().
367+ *
368+ */
369+ if (pwm_is_enabled(pc->chip.pwms))
370+ clk_disable(pc->clk);
371+
286372 clk_unprepare(pc->clk);
287373
288374 return pwmchip_remove(&pc->chip);
drivers/pwm/pwm-stmpe.c+319−0
@@ -0,0 +1,319 @@
1+/*
2+ * Copyright (C) 2016 Linaro Ltd.
3+ *
4+ * Author: Linus Walleij <linus.walleij@linaro.org>
5+ *
6+ * This program is free software; you can redistribute it and/or modify
7+ * it under the terms of the GNU General Public License version 2, as
8+ * published by the Free Software Foundation.
9+ *
10+ */
11+
12+#include <linux/bitops.h>
13+#include <linux/delay.h>
14+#include <linux/err.h>
15+#include <linux/mfd/stmpe.h>
16+#include <linux/module.h>
17+#include <linux/of.h>
18+#include <linux/platform_device.h>
19+#include <linux/pwm.h>
20+#include <linux/slab.h>
21+
22+#define STMPE24XX_PWMCS 0x30
23+#define PWMCS_EN_PWM0 BIT(0)
24+#define PWMCS_EN_PWM1 BIT(1)
25+#define PWMCS_EN_PWM2 BIT(2)
26+#define STMPE24XX_PWMIC0 0x38
27+#define STMPE24XX_PWMIC1 0x39
28+#define STMPE24XX_PWMIC2 0x3a
29+
30+#define STMPE_PWM_24XX_PINBASE 21
31+
32+struct stmpe_pwm {
33+ struct stmpe *stmpe;
34+ struct pwm_chip chip;
35+ u8 last_duty;
36+};
37+
38+static inline struct stmpe_pwm *to_stmpe_pwm(struct pwm_chip *chip)
39+{
40+ return container_of(chip, struct stmpe_pwm, chip);
41+}
42+
43+static int stmpe_24xx_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
44+{
45+ struct stmpe_pwm *stmpe_pwm = to_stmpe_pwm(chip);
46+ u8 value;
47+ int ret;
48+
49+ ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS);
50+ if (ret < 0) {
51+ dev_err(chip->dev, "error reading PWM#%u control\n",
52+ pwm->hwpwm);
53+ return ret;
54+ }
55+
56+ value = ret | BIT(pwm->hwpwm);
57+
58+ ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value);
59+ if (ret) {
60+ dev_err(chip->dev, "error writing PWM#%u control\n",
61+ pwm->hwpwm);
62+ return ret;
63+ }
64+
65+ return 0;
66+}
67+
68+static void stmpe_24xx_pwm_disable(struct pwm_chip *chip,
69+ struct pwm_device *pwm)
70+{
71+ struct stmpe_pwm *stmpe_pwm = to_stmpe_pwm(chip);
72+ u8 value;
73+ int ret;
74+
75+ ret = stmpe_reg_read(stmpe_pwm->stmpe, STMPE24XX_PWMCS);
76+ if (ret < 0) {
77+ dev_err(chip->dev, "error reading PWM#%u control\n",
78+ pwm->hwpwm);
79+ return;
80+ }
81+
82+ value = ret & ~BIT(pwm->hwpwm);
83+
84+ ret = stmpe_reg_write(stmpe_pwm->stmpe, STMPE24XX_PWMCS, value);
85+ if (ret) {
86+ dev_err(chip->dev, "error writing PWM#%u control\n",
87+ pwm->hwpwm);
88+ return;
89+ }
90+}
91+
92+/* STMPE 24xx PWM instructions */
93+#define SMAX 0x007f
94+#define SMIN 0x00ff
95+#define GTS 0x0000
96+#define LOAD BIT(14) /* Only available on 2403 */
97+#define RAMPUP 0x0000
98+#define RAMPDOWN BIT(7)
99+#define PRESCALE_512 BIT(14)
100+#define STEPTIME_1 BIT(8)
101+#define BRANCH (BIT(15) | BIT(13))
102+
103+static int stmpe_24xx_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
104+ int duty_ns, int period_ns)
105+{
106+ struct stmpe_pwm *stmpe_pwm = to_stmpe_pwm(chip);
107+ unsigned int i, pin;
108+ u16 program[3] = {
109+ SMAX,
110+ GTS,
111+ GTS,
112+ };
113+ u8 offset;
114+ int ret;
115+
116+ /* Make sure we are disabled */
117+ if (pwm_is_enabled(pwm)) {
118+ stmpe_24xx_pwm_disable(chip, pwm);
119+ } else {
120+ /* Connect the PWM to the pin */
121+ pin = pwm->hwpwm;
122+
123+ /* On STMPE2401 and 2403 pins 21,22,23 are used */
124+ if (stmpe_pwm->stmpe->partnum == STMPE2401 ||
125+ stmpe_pwm->stmpe->partnum == STMPE2403)
126+ pin += STMPE_PWM_24XX_PINBASE;
127+
128+ ret = stmpe_set_altfunc(stmpe_pwm->stmpe, BIT(pin),
129+ STMPE_BLOCK_PWM);
130+ if (ret) {
131+ dev_err(chip->dev, "unable to connect PWM#%u to pin\n",
132+ pwm->hwpwm);
133+ return ret;
134+ }
135+ }
136+
137+ /* STMPE24XX */
138+ switch (pwm->hwpwm) {
139+ case 0:
140+ offset = STMPE24XX_PWMIC0;
141+ break;
142+
143+ case 1:
144+ offset = STMPE24XX_PWMIC1;
145+ break;
146+
147+ case 2:
148+ offset = STMPE24XX_PWMIC1;
149+ break;
150+
151+ default:
152+ /* Should not happen as npwm is 3 */
153+ return -ENODEV;
154+ }
155+
156+ dev_dbg(chip->dev, "PWM#%u: config duty %d ns, period %d ns\n",
157+ pwm->hwpwm, duty_ns, period_ns);
158+
159+ if (duty_ns == 0) {
160+ if (stmpe_pwm->stmpe->partnum == STMPE2401)
161+ program[0] = SMAX; /* off all the time */
162+
163+ if (stmpe_pwm->stmpe->partnum == STMPE2403)
164+ program[0] = LOAD | 0xff; /* LOAD 0xff */
165+
166+ stmpe_pwm->last_duty = 0x00;
167+ } else if (duty_ns == period_ns) {
168+ if (stmpe_pwm->stmpe->partnum == STMPE2401)
169+ program[0] = SMIN; /* on all the time */
170+
171+ if (stmpe_pwm->stmpe->partnum == STMPE2403)
172+ program[0] = LOAD | 0x00; /* LOAD 0x00 */
173+
174+ stmpe_pwm->last_duty = 0xff;
175+ } else {
176+ u8 value, last = stmpe_pwm->last_duty;
177+ unsigned long duty;
178+
179+ /*
180+ * Counter goes from 0x00 to 0xff repeatedly at 32768 Hz,
181+ * (means a period of 30517 ns) then this is compared to the
182+ * counter from the ramp, if this is >= PWM counter the output
183+ * is high. With LOAD we can define how much of the cycle it
184+ * is on.
185+ *
186+ * Prescale = 0 -> 2 kHz -> T = 1/f = 488281.25 ns
187+ */
188+
189+ /* Scale to 0..0xff */
190+ duty = duty_ns * 256;
191+ duty = DIV_ROUND_CLOSEST(duty, period_ns);
192+ value = duty;
193+
194+ if (value == last) {
195+ /* Run the old program */
196+ if (pwm_is_enabled(pwm))
197+ stmpe_24xx_pwm_enable(chip, pwm);
198+
199+ return 0;
200+ } else if (stmpe_pwm->stmpe->partnum == STMPE2403) {
201+ /* STMPE2403 can simply set the right PWM value */
202+ program[0] = LOAD | value;
203+ program[1] = 0x0000;
204+ } else if (stmpe_pwm->stmpe->partnum == STMPE2401) {
205+ /* STMPE2401 need a complex program */
206+ u16 incdec = 0x0000;
207+
208+ if (last < value)
209+ /* Count up */
210+ incdec = RAMPUP | (value - last);
211+ else
212+ /* Count down */
213+ incdec = RAMPDOWN | (last - value);
214+
215+ /* Step to desired value, smoothly */
216+ program[0] = PRESCALE_512 | STEPTIME_1 | incdec;
217+
218+ /* Loop eternally to 0x00 */
219+ program[1] = BRANCH;
220+ }
221+
222+ dev_dbg(chip->dev,
223+ "PWM#%u: value = %02x, last_duty = %02x, program=%04x,%04x,%04x\n",
224+ pwm->hwpwm, value, last, program[0], program[1],
225+ program[2]);
226+ stmpe_pwm->last_duty = value;
227+ }
228+
229+ /*
230+ * We can write programs of up to 64 16-bit words into this channel.
231+ */
232+ for (i = 0; i < ARRAY_SIZE(program); i++) {
233+ u8 value;
234+
235+ value = (program[i] >> 8) & 0xff;
236+
237+ ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value);
238+ if (ret) {
239+ dev_err(chip->dev, "error writing register %02x: %d\n",
240+ offset, ret);
241+ return ret;
242+ }
243+
244+ value = program[i] & 0xff;
245+
246+ ret = stmpe_reg_write(stmpe_pwm->stmpe, offset, value);
247+ if (ret) {
248+ dev_err(chip->dev, "error writing register %02x: %d\n",
249+ offset, ret);
250+ return ret;
251+ }
252+ }
253+
254+ /* If we were enabled, re-enable this PWM */
255+ if (pwm_is_enabled(pwm))
256+ stmpe_24xx_pwm_enable(chip, pwm);
257+
258+ /* Sleep for 200ms so we're sure it will take effect */
259+ msleep(200);
260+
261+ dev_dbg(chip->dev, "programmed PWM#%u, %u bytes\n", pwm->hwpwm, i);
262+
263+ return 0;
264+}
265+
266+static const struct pwm_ops stmpe_24xx_pwm_ops = {
267+ .config = stmpe_24xx_pwm_config,
268+ .enable = stmpe_24xx_pwm_enable,
269+ .disable = stmpe_24xx_pwm_disable,
270+ .owner = THIS_MODULE,
271+};
272+
273+static int __init stmpe_pwm_probe(struct platform_device *pdev)
274+{
275+ struct stmpe *stmpe = dev_get_drvdata(pdev->dev.parent);
276+ struct stmpe_pwm *pwm;
277+ int ret;
278+
279+ pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
280+ if (!pwm)
281+ return -ENOMEM;
282+
283+ pwm->stmpe = stmpe;
284+ pwm->chip.dev = &pdev->dev;
285+ pwm->chip.base = -1;
286+
287+ if (stmpe->partnum == STMPE2401 || stmpe->partnum == STMPE2403) {
288+ pwm->chip.ops = &stmpe_24xx_pwm_ops;
289+ pwm->chip.npwm = 3;
290+ } else {
291+ if (stmpe->partnum == STMPE1601)
292+ dev_err(&pdev->dev, "STMPE1601 not yet supported\n");
293+ else
294+ dev_err(&pdev->dev, "Unknown STMPE PWM\n");
295+
296+ return -ENODEV;
297+ }
298+
299+ ret = stmpe_enable(stmpe, STMPE_BLOCK_PWM);
300+ if (ret)
301+ return ret;
302+
303+ ret = pwmchip_add(&pwm->chip);
304+ if (ret) {
305+ stmpe_disable(stmpe, STMPE_BLOCK_PWM);
306+ return ret;
307+ }
308+
309+ platform_set_drvdata(pdev, pwm);
310+
311+ return 0;
312+}
313+
314+static struct platform_driver stmpe_pwm_driver = {
315+ .driver = {
316+ .name = "stmpe-pwm",
317+ },
318+};
319+builtin_platform_driver_probe(stmpe_pwm_driver, stmpe_pwm_probe);
drivers/pwm/pwm-tegra.c+52−17
@@ -26,9 +26,11 @@ · 25 unmodified lines
2626 #include <linux/io.h>
2727 #include <linux/module.h>
2828 #include <linux/of.h>
29+#include <linux/of_device.h>
2930 #include <linux/pwm.h>
3031 #include <linux/platform_device.h>
3132 #include <linux/slab.h>
33+#include <linux/reset.h>
3234
3335 #define PWM_ENABLE (1 << 31)
3436 #define PWM_DUTY_WIDTH 8
@@ -36,15 +38,20 @@ · 1 unmodified line
3638 #define PWM_SCALE_WIDTH 13
3739 #define PWM_SCALE_SHIFT 0
3840
39−#define NUM_PWM 4
41+struct tegra_pwm_soc {
42+ unsigned int num_channels;
43+};
4044
4145 struct tegra_pwm_chip {
42− struct pwm_chip chip;
43− struct device *dev;
46+ struct pwm_chip chip;
47+ struct device *dev;
48+
49+ struct clk *clk;
50+ struct reset_control*rst;
4451
45− struct clk *clk;
52+ void __iomem *regs;
4653
47− void __iomem *mmio_base;
54+ const struct tegra_pwm_soc *soc;
4855 };
4956
5057 static inline struct tegra_pwm_chip *to_tegra_pwm_chip(struct pwm_chip *chip)
@@ -54,20 +61,20 @@ static inline struct tegra_pwm_chip *to_tegra_pwm_chip(struct pwm_chip *chip) · 3 unmodified lines
5461
5562 static inline u32 pwm_readl(struct tegra_pwm_chip *chip, unsigned int num)
5663 {
57− return readl(chip->mmio_base + (num << 4));
64+ return readl(chip->regs + (num << 4));
5865 }
5966
6067 static inline void pwm_writel(struct tegra_pwm_chip *chip, unsigned int num,
6168 unsigned long val)
6269 {
63− writel(val, chip->mmio_base + (num << 4));
70+ writel(val, chip->regs + (num << 4));
6471 }
6572
6673 static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
6774 int duty_ns, int period_ns)
6875 {
6976 struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
70− unsigned long long c;
77+ unsigned long long c = duty_ns;
7178 unsigned long rate, hz;
7279 u32 val = 0;
7380 int err;
@@ -77,7 +84,8 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, · 3 unmodified lines
7784 * per (1 << PWM_DUTY_WIDTH) cycles and make sure to round to the
7885 * nearest integer during division.
7986 */
80− c = duty_ns * ((1 << PWM_DUTY_WIDTH) - 1) + period_ns / 2;
87+ c *= (1 << PWM_DUTY_WIDTH);
88+ c += period_ns / 2;
8189 do_div(c, period_ns);
8290
8391 val = (u32)c << PWM_DUTY_SHIFT;
@@ -176,12 +184,13 @@ static int tegra_pwm_probe(struct platform_device *pdev) · 92 unmodified lines
176184 if (!pwm)
177185 return -ENOMEM;
178186
187+ pwm->soc = of_device_get_match_data(&pdev->dev);
179188 pwm->dev = &pdev->dev;
180189
181190 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
182− pwm->mmio_base = devm_ioremap_resource(&pdev->dev, r);
183− if (IS_ERR(pwm->mmio_base))
184− return PTR_ERR(pwm->mmio_base);
191+ pwm->regs = devm_ioremap_resource(&pdev->dev, r);
192+ if (IS_ERR(pwm->regs))
193+ return PTR_ERR(pwm->regs);
185194
186195 platform_set_drvdata(pdev, pwm);
187196
@@ -189,14 +198,24 @@ static int tegra_pwm_probe(struct platform_device *pdev) · 1 unmodified line
189198 if (IS_ERR(pwm->clk))
190199 return PTR_ERR(pwm->clk);
191200
201+ pwm->rst = devm_reset_control_get(&pdev->dev, "pwm");
202+ if (IS_ERR(pwm->rst)) {
203+ ret = PTR_ERR(pwm->rst);
204+ dev_err(&pdev->dev, "Reset control is not found: %d\n", ret);
205+ return ret;
206+ }
207+
208+ reset_control_deassert(pwm->rst);
209+
192210 pwm->chip.dev = &pdev->dev;
193211 pwm->chip.ops = &tegra_pwm_ops;
194212 pwm->chip.base = -1;
195− pwm->chip.npwm = NUM_PWM;
213+ pwm->chip.npwm = pwm->soc->num_channels;
196214
197215 ret = pwmchip_add(&pwm->chip);
198216 if (ret < 0) {
199217 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
218+ reset_control_assert(pwm->rst);
200219 return ret;
201220 }
202221
@@ -206,12 +225,17 @@ static int tegra_pwm_probe(struct platform_device *pdev) · 3 unmodified lines
206225 static int tegra_pwm_remove(struct platform_device *pdev)
207226 {
208227 struct tegra_pwm_chip *pc = platform_get_drvdata(pdev);
209− int i;
228+ unsigned int i;
229+ int err;
210230
211231 if (WARN_ON(!pc))
212232 return -ENODEV;
213233
214− for (i = 0; i < NUM_PWM; i++) {
234+ err = clk_prepare_enable(pc->clk);
235+ if (err < 0)
236+ return err;
237+
238+ for (i = 0; i < pc->chip.npwm; i++) {
215239 struct pwm_device *pwm = &pc->chip.pwms[i];
216240
217241 if (!pwm_is_enabled(pwm))
@@ -223,12 +247,23 @@ static int tegra_pwm_remove(struct platform_device *pdev) · 5 unmodified lines
223247 clk_disable_unprepare(pc->clk);
224248 }
225249
250+ reset_control_assert(pc->rst);
251+ clk_disable_unprepare(pc->clk);
252+
226253 return pwmchip_remove(&pc->chip);
227254 }
228255
256+static const struct tegra_pwm_soc tegra20_pwm_soc = {
257+ .num_channels = 4,
258+};
259+
260+static const struct tegra_pwm_soc tegra186_pwm_soc = {
261+ .num_channels = 1,
262+};
263+
229264 static const struct of_device_id tegra_pwm_of_match[] = {
230− { .compatible = "nvidia,tegra20-pwm" },
231− { .compatible = "nvidia,tegra30-pwm" },
265+ { .compatible = "nvidia,tegra20-pwm", .data = &tegra20_pwm_soc },
266+ { .compatible = "nvidia,tegra186-pwm", .data = &tegra186_pwm_soc },
232267 { }
233268 };
234269
drivers/pwm/pwm-tiecap.c+9−28
@@ -27,8 +27,6 @@ · 26 unmodified lines
2727 #include <linux/pwm.h>
2828 #include <linux/of_device.h>
2929
30−#include "pwm-tipwmss.h"
31−
3230 /* ECAP registers and bits definitions */
3331 #define CAP1 0x08
3432 #define CAP2 0x0C
@@ -195,6 +193,7 @@ static const struct pwm_ops ecap_pwm_ops = { · 160 unmodified lines
195193 };
196194
197195 static const struct of_device_id ecap_of_match[] = {
196+ { .compatible = "ti,am3352-ecap" },
198197 { .compatible = "ti,am33xx-ecap" },
199198 {},
200199 };
@@ -202,17 +201,24 @@ MODULE_DEVICE_TABLE(of, ecap_of_match); · 1 unmodified line
202201
203202 static int ecap_pwm_probe(struct platform_device *pdev)
204203 {
204+ struct device_node *np = pdev->dev.of_node;
205205 int ret;
206206 struct resource *r;
207207 struct clk *clk;
208208 struct ecap_pwm_chip *pc;
209− u16 status;
210209
211210 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
212211 if (!pc)
213212 return -ENOMEM;
214213
215214 clk = devm_clk_get(&pdev->dev, "fck");
215+ if (IS_ERR(clk)) {
216+ if (of_device_is_compatible(np, "ti,am33xx-ecap")) {
217+ dev_warn(&pdev->dev, "Binding is obsolete.\n");
218+ clk = devm_clk_get(pdev->dev.parent, "fck");
219+ }
220+ }
221+
216222 if (IS_ERR(clk)) {
217223 dev_err(&pdev->dev, "failed to get clock\n");
218224 return PTR_ERR(clk);
@@ -243,40 +249,15 @@ static int ecap_pwm_probe(struct platform_device *pdev) · 24 unmodified lines
243249 }
244250
245251 pm_runtime_enable(&pdev->dev);
246− pm_runtime_get_sync(&pdev->dev);
247−
248− status = pwmss_submodule_state_change(pdev->dev.parent,
249− PWMSS_ECAPCLK_EN);
250− if (!(status & PWMSS_ECAPCLK_EN_ACK)) {
251− dev_err(&pdev->dev, "PWMSS config space clock enable failed\n");
252− ret = -EINVAL;
253− goto pwmss_clk_failure;
254− }
255−
256− pm_runtime_put_sync(&pdev->dev);
257252
258253 platform_set_drvdata(pdev, pc);
259254 return 0;
260−
261−pwmss_clk_failure:
262− pm_runtime_put_sync(&pdev->dev);
263− pm_runtime_disable(&pdev->dev);
264− pwmchip_remove(&pc->chip);
265− return ret;
266255 }
267256
268257 static int ecap_pwm_remove(struct platform_device *pdev)
269258 {
270259 struct ecap_pwm_chip *pc = platform_get_drvdata(pdev);
271260
272− pm_runtime_get_sync(&pdev->dev);
273− /*
274− * Due to hardware misbehaviour, acknowledge of the stop_req
275− * is missing. Hence checking of the status bit skipped.
276− */
277− pwmss_submodule_state_change(pdev->dev.parent, PWMSS_ECAPCLK_STOP_REQ);
278− pm_runtime_put_sync(&pdev->dev);
279−
280261 pm_runtime_disable(&pdev->dev);
281262 return pwmchip_remove(&pc->chip);
282263 }
drivers/pwm/pwm-tiehrpwm.c+9−29
@@ -27,8 +27,6 @@ · 26 unmodified lines
2727 #include <linux/pm_runtime.h>
2828 #include <linux/of_device.h>
2929
30−#include "pwm-tipwmss.h"
31−
3230 /* EHRPWM registers and bits definitions */
3331
3432 /* Time base module registers */
@@ -426,6 +424,7 @@ static const struct pwm_ops ehrpwm_pwm_ops = { · 391 unmodified lines
426424 };
427425
428426 static const struct of_device_id ehrpwm_of_match[] = {
427+ { .compatible = "ti,am3352-ehrpwm" },
429428 { .compatible = "ti,am33xx-ehrpwm" },
430429 {},
431430 };
@@ -433,17 +432,24 @@ MODULE_DEVICE_TABLE(of, ehrpwm_of_match); · 1 unmodified line
433432
434433 static int ehrpwm_pwm_probe(struct platform_device *pdev)
435434 {
435+ struct device_node *np = pdev->dev.of_node;
436436 int ret;
437437 struct resource *r;
438438 struct clk *clk;
439439 struct ehrpwm_pwm_chip *pc;
440− u16 status;
441440
442441 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
443442 if (!pc)
444443 return -ENOMEM;
445444
446445 clk = devm_clk_get(&pdev->dev, "fck");
446+ if (IS_ERR(clk)) {
447+ if (of_device_is_compatible(np, "ti,am33xx-ecap")) {
448+ dev_warn(&pdev->dev, "Binding is obsolete.\n");
449+ clk = devm_clk_get(pdev->dev.parent, "fck");
450+ }
451+ }
452+
447453 if (IS_ERR(clk)) {
448454 dev_err(&pdev->dev, "failed to get clock\n");
449455 return PTR_ERR(clk);
@@ -487,27 +493,9 @@ static int ehrpwm_pwm_probe(struct platform_device *pdev) · 37 unmodified lines
487493 }
488494
489495 pm_runtime_enable(&pdev->dev);
490− pm_runtime_get_sync(&pdev->dev);
491−
492− status = pwmss_submodule_state_change(pdev->dev.parent,
493− PWMSS_EPWMCLK_EN);
494− if (!(status & PWMSS_EPWMCLK_EN_ACK)) {
495− dev_err(&pdev->dev, "PWMSS config space clock enable failed\n");
496− ret = -EINVAL;
497− goto pwmss_clk_failure;
498− }
499−
500− pm_runtime_put_sync(&pdev->dev);
501496
502497 platform_set_drvdata(pdev, pc);
503498 return 0;
504−
505−pwmss_clk_failure:
506− pm_runtime_put_sync(&pdev->dev);
507− pm_runtime_disable(&pdev->dev);
508− pwmchip_remove(&pc->chip);
509− clk_unprepare(pc->tbclk);
510− return ret;
511499 }
512500
513501 static int ehrpwm_pwm_remove(struct platform_device *pdev)
@@ -516,14 +504,6 @@ static int ehrpwm_pwm_remove(struct platform_device *pdev) · 2 unmodified lines
516504
517505 clk_unprepare(pc->tbclk);
518506
519− pm_runtime_get_sync(&pdev->dev);
520− /*
521− * Due to hardware misbehaviour, acknowledge of the stop_req
522− * is missing. Hence checking of the status bit skipped.
523− */
524− pwmss_submodule_state_change(pdev->dev.parent, PWMSS_EPWMCLK_STOP_REQ);
525− pm_runtime_put_sync(&pdev->dev);
526−
527507 pm_runtime_put_sync(&pdev->dev);
528508 pm_runtime_disable(&pdev->dev);
529509 return pwmchip_remove(&pc->chip);
drivers/pwm/pwm-tipwmss.c+0−49
@@ -22,32 +22,6 @@ · 21 unmodified lines
2222 #include <linux/pm_runtime.h>
2323 #include <linux/of_device.h>
2424
25−#include "pwm-tipwmss.h"
26−
27−#define PWMSS_CLKCONFIG 0x8 /* Clock gating reg */
28−#define PWMSS_CLKSTATUS 0xc /* Clock gating status reg */
29−
30−struct pwmss_info {
31− void __iomem *mmio_base;
32− struct mutex pwmss_lock;
33− u16 pwmss_clkconfig;
34−};
35−
36−u16 pwmss_submodule_state_change(struct device *dev, int set)
37−{
38− struct pwmss_info *info = dev_get_drvdata(dev);
39− u16 val;
40−
41− mutex_lock(&info->pwmss_lock);
42− val = readw(info->mmio_base + PWMSS_CLKCONFIG);
43− val |= set;
44− writew(val , info->mmio_base + PWMSS_CLKCONFIG);
45− mutex_unlock(&info->pwmss_lock);
46−
47− return readw(info->mmio_base + PWMSS_CLKSTATUS);
48−}
49−EXPORT_SYMBOL(pwmss_submodule_state_change);
50−
5125 static const struct of_device_id pwmss_of_match[] = {
5226 { .compatible = "ti,am33xx-pwmss" },
5327 {},
@@ -57,24 +31,10 @@ MODULE_DEVICE_TABLE(of, pwmss_of_match); · 3 unmodified lines
5731 static int pwmss_probe(struct platform_device *pdev)
5832 {
5933 int ret;
60− struct resource *r;
61− struct pwmss_info *info;
6234 struct device_node *node = pdev->dev.of_node;
6335
64− info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
65− if (!info)
66− return -ENOMEM;
67−
68− mutex_init(&info->pwmss_lock);
69−
70− r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
71− info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
72− if (IS_ERR(info->mmio_base))
73− return PTR_ERR(info->mmio_base);
74−
7536 pm_runtime_enable(&pdev->dev);
7637 pm_runtime_get_sync(&pdev->dev);
77− platform_set_drvdata(pdev, info);
7838
7939 /* Populate all the child nodes here... */
8040 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
@@ -86,30 +46,21 @@ static int pwmss_probe(struct platform_device *pdev) · 5 unmodified lines
8646
8747 static int pwmss_remove(struct platform_device *pdev)
8848 {
89− struct pwmss_info *info = platform_get_drvdata(pdev);
90−
9149 pm_runtime_put_sync(&pdev->dev);
9250 pm_runtime_disable(&pdev->dev);
93− mutex_destroy(&info->pwmss_lock);
9451 return 0;
9552 }
9653
9754 #ifdef CONFIG_PM_SLEEP
9855 static int pwmss_suspend(struct device *dev)
9956 {
100− struct pwmss_info *info = dev_get_drvdata(dev);
101−
102− info->pwmss_clkconfig = readw(info->mmio_base + PWMSS_CLKCONFIG);
10357 pm_runtime_put_sync(dev);
10458 return 0;
10559 }
10660
10761 static int pwmss_resume(struct device *dev)
10862 {
109− struct pwmss_info *info = dev_get_drvdata(dev);
110−
11163 pm_runtime_get_sync(dev);
112− writew(info->pwmss_clkconfig, info->mmio_base + PWMSS_CLKCONFIG);
11364 return 0;
11465 }
11566 #endif
drivers/pwm/pwm-tipwmss.h+0−39
@@ -1,39 +0,0 @@
1−/*
2− * TI PWM Subsystem driver
3− *
4− * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
5− *
6− * This program is free software; you can redistribute it and/or modify
7− * it under the terms of the GNU General Public License as published by
8− * the Free Software Foundation; either version 2 of the License, or
9− * (at your option) any later version.
10− *
11− * This program is distributed in the hope that it will be useful,
12− * but WITHOUT ANY WARRANTY; without even the implied warranty of
13− * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14− * GNU General Public License for more details.
15− *
16− */
17−
18−#ifndef __TIPWMSS_H
19−#define __TIPWMSS_H
20−
21−/* PWM substem clock gating */
22−#define PWMSS_ECAPCLK_EN BIT(0)
23−#define PWMSS_ECAPCLK_STOP_REQ BIT(1)
24−#define PWMSS_EPWMCLK_EN BIT(8)
25−#define PWMSS_EPWMCLK_STOP_REQ BIT(9)
26−
27−#define PWMSS_ECAPCLK_EN_ACK BIT(0)
28−#define PWMSS_EPWMCLK_EN_ACK BIT(8)
29−
30−#ifdef CONFIG_PWM_TIPWMSS
31−extern u16 pwmss_submodule_state_change(struct device *dev, int set);
32−#else
33−static inline u16 pwmss_submodule_state_change(struct device *dev, int set)
34−{
35− /* return success status value */
36− return 0xFFFF;
37−}
38−#endif
39−#endif /* __TIPWMSS_H */
drivers/pwm/sysfs.c+17−0
@@ -208,16 +208,33 @@ static ssize_t polarity_store(struct device *child, · 207 unmodified lines
208208 return ret ? : size;
209209 }
210210
211+static ssize_t capture_show(struct device *child,
212+ struct device_attribute *attr,
213+ char *buf)
214+{
215+ struct pwm_device *pwm = child_to_pwm_device(child);
216+ struct pwm_capture result;
217+ int ret;
218+
219+ ret = pwm_capture(pwm, &result, jiffies_to_msecs(HZ));
220+ if (ret)
221+ return ret;
222+
223+ return sprintf(buf, "%u %u\n", result.period, result.duty_cycle);
224+}
225+
211226 static DEVICE_ATTR_RW(period);
212227 static DEVICE_ATTR_RW(duty_cycle);
213228 static DEVICE_ATTR_RW(enable);
214229 static DEVICE_ATTR_RW(polarity);
230+static DEVICE_ATTR_RO(capture);
215231
216232 static struct attribute *pwm_attrs[] = {
217233 &dev_attr_period.attr,
218234 &dev_attr_duty_cycle.attr,
219235 &dev_attr_enable.attr,
220236 &dev_attr_polarity.attr,
237+ &dev_attr_capture.attr,
221238 NULL
222239 };
223240 ATTRIBUTE_GROUPS(pwm);
drivers/regulator/pwm-regulator.c+118−44
@@ -22,6 +22,12 @@ · 21 unmodified lines
2222 #include <linux/pwm.h>
2323 #include <linux/gpio/consumer.h>
2424
25+struct pwm_continuous_reg_data {
26+ unsigned int min_uV_dutycycle;
27+ unsigned int max_uV_dutycycle;
28+ unsigned int dutycycle_unit;
29+};
30+
2531 struct pwm_regulator_data {
2632 /* Shared */
2733 struct pwm_device *pwm;
@@ -29,6 +35,9 @@ struct pwm_regulator_data { · 1 unmodified line
2935 /* Voltage table */
3036 struct pwm_voltages *duty_cycle_table;
3137
38+ /* Continuous mode info */
39+ struct pwm_continuous_reg_data continuous;
40+
3241 /* regulator descriptor */
3342 struct regulator_desc desc;
3443
@@ -37,9 +46,6 @@ struct pwm_regulator_data { · 2 unmodified lines
3746
3847 int state;
3948
40− /* Continuous voltage */
41− int volt_uV;
42−
4349 /* Enable GPIO */
4450 struct gpio_desc *enb_gpio;
4551 };
@@ -52,10 +58,31 @@ struct pwm_voltages { · 6 unmodified lines
5258 /**
5359 * Voltage table call-backs
5460 */
61+static void pwm_regulator_init_state(struct regulator_dev *rdev)
62+{
63+ struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
64+ struct pwm_state pwm_state;
65+ unsigned int dutycycle;
66+ int i;
67+
68+ pwm_get_state(drvdata->pwm, &pwm_state);
69+ dutycycle = pwm_get_relative_duty_cycle(&pwm_state, 100);
70+
71+ for (i = 0; i < rdev->desc->n_voltages; i++) {
72+ if (dutycycle == drvdata->duty_cycle_table[i].dutycycle) {
73+ drvdata->state = i;
74+ return;
75+ }
76+ }
77+}
78+
5579 static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
5680 {
5781 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
5882
83+ if (drvdata->state < 0)
84+ pwm_regulator_init_state(rdev);
85+
5986 return drvdata->state;
6087 }
6188
@@ -63,16 +90,14 @@ static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev, · 1 unmodified line
6390 unsigned selector)
6491 {
6592 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
66− struct pwm_args pargs;
67− int dutycycle;
93+ struct pwm_state pstate;
6894 int ret;
6995
70− pwm_get_args(drvdata->pwm, &pargs);
71−
72− dutycycle = (pargs.period *
73− drvdata->duty_cycle_table[selector].dutycycle) / 100;
96+ pwm_init_state(drvdata->pwm, &pstate);
97+ pwm_set_relative_duty_cycle(&pstate,
98+ drvdata->duty_cycle_table[selector].dutycycle, 100);
7499
75− ret = pwm_config(drvdata->pwm, dutycycle, pargs.period);
100+ ret = pwm_apply_state(drvdata->pwm, &pstate);
76101 if (ret) {
77102 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
78103 return ret;
@@ -129,57 +154,90 @@ static int pwm_regulator_is_enabled(struct regulator_dev *dev) · 50 unmodified lines
129154 static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
130155 {
131156 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
157+ unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
158+ unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
159+ unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
160+ int min_uV = rdev->constraints->min_uV;
161+ int max_uV = rdev->constraints->max_uV;
162+ int diff_uV = max_uV - min_uV;
163+ struct pwm_state pstate;
164+ unsigned int diff_duty;
165+ unsigned int voltage;
166+
167+ pwm_get_state(drvdata->pwm, &pstate);
168+
169+ voltage = pwm_get_relative_duty_cycle(&pstate, duty_unit);
170+
171+ /*
172+ * The dutycycle for min_uV might be greater than the one for max_uV.
173+ * This is happening when the user needs an inversed polarity, but the
174+ * PWM device does not support inversing it in hardware.
175+ */
176+ if (max_uV_duty < min_uV_duty) {
177+ voltage = min_uV_duty - voltage;
178+ diff_duty = min_uV_duty - max_uV_duty;
179+ } else {
180+ voltage = voltage - min_uV_duty;
181+ diff_duty = max_uV_duty - min_uV_duty;
182+ }
132183
133− return drvdata->volt_uV;
184+ voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty);
185+
186+ return voltage + min_uV;
134187 }
135188
136189 static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
137− int min_uV, int max_uV,
138− unsigned *selector)
190+ int req_min_uV, int req_max_uV,
191+ unsigned int *selector)
139192 {
140193 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
194+ unsigned int min_uV_duty = drvdata->continuous.min_uV_dutycycle;
195+ unsigned int max_uV_duty = drvdata->continuous.max_uV_dutycycle;
196+ unsigned int duty_unit = drvdata->continuous.dutycycle_unit;
141197 unsigned int ramp_delay = rdev->constraints->ramp_delay;
142− struct pwm_args pargs;
143− unsigned int req_diff = min_uV - rdev->constraints->min_uV;
144− unsigned int diff;
145− unsigned int duty_pulse;
146− u64 req_period;
147− u32 rem;
198+ int min_uV = rdev->constraints->min_uV;
199+ int max_uV = rdev->constraints->max_uV;
200+ int diff_uV = max_uV - min_uV;
201+ struct pwm_state pstate;
148202 int old_uV = pwm_regulator_get_voltage(rdev);
203+ unsigned int diff_duty;
204+ unsigned int dutycycle;
149205 int ret;
150206
151− pwm_get_args(drvdata->pwm, &pargs);
152− diff = rdev->constraints->max_uV - rdev->constraints->min_uV;
207+ pwm_init_state(drvdata->pwm, &pstate);
153208
154− /* First try to find out if we get the iduty cycle time which is
155− * factor of PWM period time. If (request_diff_to_min * pwm_period)
156− * is perfect divided by voltage_range_diff then it is possible to
157− * get duty cycle time which is factor of PWM period. This will help
158− * to get output voltage nearer to requested value as there is no
159− * calculation loss.
209+ /*
210+ * The dutycycle for min_uV might be greater than the one for max_uV.
211+ * This is happening when the user needs an inversed polarity, but the
212+ * PWM device does not support inversing it in hardware.
160213 */
161− req_period = req_diff * pargs.period;
162− div_u64_rem(req_period, diff, &rem);
163− if (!rem) {
164− do_div(req_period, diff);
165− duty_pulse = (unsigned int)req_period;
166− } else {
167− duty_pulse = (pargs.period / 100) * ((req_diff * 100) / diff);
168− }
214+ if (max_uV_duty < min_uV_duty)
215+ diff_duty = min_uV_duty - max_uV_duty;
216+ else
217+ diff_duty = max_uV_duty - min_uV_duty;
218+
219+ dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) *
220+ diff_duty,
221+ diff_uV);
222+
223+ if (max_uV_duty < min_uV_duty)
224+ dutycycle = min_uV_duty - dutycycle;
225+ else
226+ dutycycle = min_uV_duty + dutycycle;
227+
228+ pwm_set_relative_duty_cycle(&pstate, dutycycle, duty_unit);
169229
170− ret = pwm_config(drvdata->pwm, duty_pulse, pargs.period);
230+ ret = pwm_apply_state(drvdata->pwm, &pstate);
171231 if (ret) {
172232 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
173233 return ret;
174234 }
175235
176− drvdata->volt_uV = min_uV;
177−
178236 if ((ramp_delay == 0) || !pwm_regulator_is_enabled(rdev))
179237 return 0;
180238
181239 /* Ramp delay is in uV/uS. Adjust to uS and delay */
182− ramp_delay = DIV_ROUND_UP(abs(min_uV - old_uV), ramp_delay);
240+ ramp_delay = DIV_ROUND_UP(abs(req_min_uV - old_uV), ramp_delay);
183241 usleep_range(ramp_delay, ramp_delay + DIV_ROUND_UP(ramp_delay, 10));
184242
185243 return 0;
@@ -239,6 +297,7 @@ static int pwm_regulator_init_table(struct platform_device *pdev, · 53 unmodified lines
239297 return ret;
240298 }
241299
300+ drvdata->state = -EINVAL;
242301 drvdata->duty_cycle_table = duty_cycle_table;
243302 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
244303 sizeof(drvdata->ops));
@@ -251,11 +310,28 @@ static int pwm_regulator_init_table(struct platform_device *pdev, · 6 unmodified lines
251310 static int pwm_regulator_init_continuous(struct platform_device *pdev,
252311 struct pwm_regulator_data *drvdata)
253312 {
313+ u32 dutycycle_range[2] = { 0, 100 };
314+ u32 dutycycle_unit = 100;
315+
254316 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
255317 sizeof(drvdata->ops));
256318 drvdata->desc.ops = &drvdata->ops;
257319 drvdata->desc.continuous_voltage_range = true;
258320
321+ of_property_read_u32_array(pdev->dev.of_node,
322+ "pwm-dutycycle-range",
323+ dutycycle_range, 2);
324+ of_property_read_u32(pdev->dev.of_node, "pwm-dutycycle-unit",
325+ &dutycycle_unit);
326+
327+ if (dutycycle_range[0] > dutycycle_unit ||
328+ dutycycle_range[1] > dutycycle_unit)
329+ return -EINVAL;
330+
331+ drvdata->continuous.dutycycle_unit = dutycycle_unit;
332+ drvdata->continuous.min_uV_dutycycle = dutycycle_range[0];
333+ drvdata->continuous.max_uV_dutycycle = dutycycle_range[1];
334+
259335 return 0;
260336 }
261337
@@ -316,11 +392,9 @@ static int pwm_regulator_probe(struct platform_device *pdev) · 54 unmodified lines
316392 return ret;
317393 }
318394
319− /*
320− * FIXME: pwm_apply_args() should be removed when switching to the
321− * atomic PWM API.
322− */
323− pwm_apply_args(drvdata->pwm);
395+ ret = pwm_adjust_config(drvdata->pwm);
396+ if (ret)
397+ return ret;
324398
325399 regulator = devm_regulator_register(&pdev->dev,
326400 &drvdata->desc, &config);
include/linux/mfd/cros_ec.h+15−0
@@ -225,6 +225,21 @@ int cros_ec_check_result(struct cros_ec_device *ec_dev, · 224 unmodified lines
225225 int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
226226 struct cros_ec_command *msg);
227227
228+/**
229+ * cros_ec_cmd_xfer_status - Send a command to the ChromeOS EC
230+ *
231+ * This function is identical to cros_ec_cmd_xfer, except it returns success
232+ * status only if both the command was transmitted successfully and the EC
233+ * replied with success status. It's not necessary to check msg->result when
234+ * using this function.
235+ *
236+ * @ec_dev: EC device
237+ * @msg: Message to write
238+ * @return: Num. of bytes transferred on success, <0 on failure
239+ */
240+int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
241+ struct cros_ec_command *msg);
242+
228243 /**
229244 * cros_ec_remove - Remove a ChromeOS EC
230245 *
include/linux/mfd/cros_ec_commands.h+31−0
@@ -949,6 +949,37 @@ struct ec_params_pwm_set_fan_duty { · 948 unmodified lines
949949 uint32_t percent;
950950 } __packed;
951951
952+#define EC_CMD_PWM_SET_DUTY 0x25
953+/* 16 bit duty cycle, 0xffff = 100% */
954+#define EC_PWM_MAX_DUTY 0xffff
955+
956+enum ec_pwm_type {
957+ /* All types, indexed by board-specific enum pwm_channel */
958+ EC_PWM_TYPE_GENERIC = 0,
959+ /* Keyboard backlight */
960+ EC_PWM_TYPE_KB_LIGHT,
961+ /* Display backlight */
962+ EC_PWM_TYPE_DISPLAY_LIGHT,
963+ EC_PWM_TYPE_COUNT,
964+};
965+
966+struct ec_params_pwm_set_duty {
967+ uint16_t duty; /* Duty cycle, EC_PWM_MAX_DUTY = 100% */
968+ uint8_t pwm_type; /* ec_pwm_type */
969+ uint8_t index; /* Type-specific index, or 0 if unique */
970+} __packed;
971+
972+#define EC_CMD_PWM_GET_DUTY 0x26
973+
974+struct ec_params_pwm_get_duty {
975+ uint8_t pwm_type; /* ec_pwm_type */
976+ uint8_t index; /* Type-specific index, or 0 if unique */
977+} __packed;
978+
979+struct ec_response_pwm_get_duty {
980+ uint16_t duty; /* Duty cycle, EC_PWM_MAX_DUTY = 100% */
981+} __packed;
982+
952983 /*****************************************************************************/
953984 /*
954985 * Lightbar commands. This looks worse than it is. Since we only use one HOST
include/linux/pwm.h+112−1
@@ -5,7 +5,9 @@ · 4 unmodified lines
55 #include <linux/mutex.h>
66 #include <linux/of.h>
77
8+struct pwm_capture;
89 struct seq_file;
10+
911 struct pwm_chip;
1012
1113 /**
@@ -147,12 +149,101 @@ static inline void pwm_get_args(const struct pwm_device *pwm, · 135 unmodified lines
147149 *args = pwm->args;
148150 }
149151
152+/**
153+ * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
154+ * @pwm: PWM device
155+ * @state: state to fill with the prepared PWM state
156+ *
157+ * This functions prepares a state that can later be tweaked and applied
158+ * to the PWM device with pwm_apply_state(). This is a convenient function
159+ * that first retrieves the current PWM state and the replaces the period
160+ * and polarity fields with the reference values defined in pwm->args.
161+ * Once the function returns, you can adjust the ->enabled and ->duty_cycle
162+ * fields according to your needs before calling pwm_apply_state().
163+ *
164+ * ->duty_cycle is initially set to zero to avoid cases where the current
165+ * ->duty_cycle value exceed the pwm_args->period one, which would trigger
166+ * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
167+ * first.
168+ */
169+static inline void pwm_init_state(const struct pwm_device *pwm,
170+ struct pwm_state *state)
171+{
172+ struct pwm_args args;
173+
174+ /* First get the current state. */
175+ pwm_get_state(pwm, state);
176+
177+ /* Then fill it with the reference config */
178+ pwm_get_args(pwm, &args);
179+
180+ state->period = args.period;
181+ state->polarity = args.polarity;
182+ state->duty_cycle = 0;
183+}
184+
185+/**
186+ * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
187+ * @state: PWM state to extract the duty cycle from
188+ * @scale: target scale of the relative duty cycle
189+ *
190+ * This functions converts the absolute duty cycle stored in @state (expressed
191+ * in nanosecond) into a value relative to the period.
192+ *
193+ * For example if you want to get the duty_cycle expressed in percent, call:
194+ *
195+ * pwm_get_state(pwm, &state);
196+ * duty = pwm_get_relative_duty_cycle(&state, 100);
197+ */
198+static inline unsigned int
199+pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
200+{
201+ if (!state->period)
202+ return 0;
203+
204+ return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
205+ state->period);
206+}
207+
208+/**
209+ * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
210+ * @state: PWM state to fill
211+ * @duty_cycle: relative duty cycle value
212+ * @scale: scale in which @duty_cycle is expressed
213+ *
214+ * This functions converts a relative into an absolute duty cycle (expressed
215+ * in nanoseconds), and puts the result in state->duty_cycle.
216+ *
217+ * For example if you want to configure a 50% duty cycle, call:
218+ *
219+ * pwm_init_state(pwm, &state);
220+ * pwm_set_relative_duty_cycle(&state, 50, 100);
221+ * pwm_apply_state(pwm, &state);
222+ *
223+ * This functions returns -EINVAL if @duty_cycle and/or @scale are
224+ * inconsistent (@scale == 0 or @duty_cycle > @scale).
225+ */
226+static inline int
227+pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
228+ unsigned int scale)
229+{
230+ if (!scale || duty_cycle > scale)
231+ return -EINVAL;
232+
233+ state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
234+ state->period,
235+ scale);
236+
237+ return 0;
238+}
239+
150240 /**
151241 * struct pwm_ops - PWM controller operations
152242 * @request: optional hook for requesting a PWM
153243 * @free: optional hook for freeing a PWM
154244 * @config: configure duty cycles and period length for this PWM
155245 * @set_polarity: configure the polarity of this PWM
246+ * @capture: capture and report PWM signal
156247 * @enable: enable PWM output toggling
157248 * @disable: disable PWM output toggling
158249 * @apply: atomically apply a new PWM config. The state argument
@@ -172,6 +263,8 @@ struct pwm_ops { · 13 unmodified lines
172263 int duty_ns, int period_ns);
173264 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
174265 enum pwm_polarity polarity);
266+ int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
267+ struct pwm_capture *result, unsigned long timeout);
175268 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
176269 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
177270 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
@@ -212,6 +305,16 @@ struct pwm_chip { · 34 unmodified lines
212305 bool can_sleep;
213306 };
214307
308+/**
309+ * struct pwm_capture - PWM capture data
310+ * @period: period of the PWM signal (in nanoseconds)
311+ * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
312+ */
313+struct pwm_capture {
314+ unsigned int period;
315+ unsigned int duty_cycle;
316+};
317+
215318 #if IS_ENABLED(CONFIG_PWM)
216319 /* PWM user APIs */
217320 struct pwm_device *pwm_request(int pwm_id, const char *label);
@@ -323,8 +426,9 @@ static inline void pwm_disable(struct pwm_device *pwm) · 105 unmodified lines
323426 pwm_apply_state(pwm, &state);
324427 }
325428
326−
327429 /* PWM provider APIs */
430+int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
431+ unsigned long timeout);
328432 int pwm_set_chip_data(struct pwm_device *pwm, void *data);
329433 void *pwm_get_chip_data(struct pwm_device *pwm);
330434
@@ -376,6 +480,13 @@ static inline int pwm_config(struct pwm_device *pwm, int duty_ns, · 45 unmodified lines
376480 return -EINVAL;
377481 }
378482
483+static inline int pwm_capture(struct pwm_device *pwm,
484+ struct pwm_capture *result,
485+ unsigned long timeout)
486+{
487+ return -EINVAL;
488+}
489+
379490 static inline int pwm_set_polarity(struct pwm_device *pwm,
380491 enum pwm_polarity polarity)
381492 {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t