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
77 Enable/disable the PWM signal.77 Enable/disable the PWM signal.
78 0 is disabled78 0 is disabled
79 1 is enabled79 1 is enabled
80
81What: /sys/class/pwm/pwmchipN/pwmX/capture
82Date: June 2016
83KernelVersion: 4.8
84Contact: Lee Jones <lee.jones@linaro.org>
85Description:
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 @@
1Broadcom iProc PWM controller device tree bindings
2
3This controller has 4 channels.
4
5Required 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
12Refer to clocks/clock-bindings.txt for generic clock consumer properties.
13
14Example:
15
16pwm: 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
3Google's ChromeOS EC PWM is a simple PWM attached to the Embedded Controller
4(EC) and controlled via a host-command interface.
5
6An EC PWM node should be only found as a sub-node of the EC node (see
7Documentation/devicetree/bindings/mfd/cros-ec.txt).
8
9Required properties:
10- compatible: Must contain "google,cros-ec-pwm"
11- #pwm-cells: Should be 1. The cell specifies the PWM index.
12
13Example:
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 @@
1Tegra SoC PWFM controller1Tegra SoC PWFM controller
2 2
3Required properties:3Required properties:
4- compatible: For Tegra20, must contain "nvidia,tegra20-pwm". For Tegra30,4- compatible: Must be:
5 must contain "nvidia,tegra30-pwm". Otherwise, must contain5 - "nvidia,tegra20-pwm": for Tegra20
6 "nvidia,<chip>-pwm", plus one of the above, where <chip> is tegra114,6 - "nvidia,tegra30-pwm", "nvidia,tegra20-pwm": for Tegra30
7 tegra124, tegra132, or tegra210.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
8- reg: physical base address and length of the controller's registers12- reg: physical base address and length of the controller's registers
9- #pwm-cells: should be 2. See pwm.txt in this directory for a description of13- #pwm-cells: should be 2. See pwm.txt in this directory for a description of
10 the cells format.14 the cells format.
Documentation/devicetree/bindings/pwm/renesas,pwm-rcar.txt+1−0
@@ -7,6 +7,7 @@ Required Properties: · 6 unmodified lines
7 - "renesas,pwm-r8a7790": for R-Car H27 - "renesas,pwm-r8a7790": for R-Car H2
8 - "renesas,pwm-r8a7791": for R-Car M2-W8 - "renesas,pwm-r8a7791": for R-Car M2-W
9 - "renesas,pwm-r8a7794": for R-Car E29 - "renesas,pwm-r8a7794": for R-Car E2
10 - "renesas,pwm-r8a7795": for R-Car H3
10- reg: base address and length of the registers block for the PWM.11- reg: base address and length of the registers block for the PWM.
11- #pwm-cells: should be 2. See pwm.txt in this directory for a description of12- #pwm-cells: should be 2. See pwm.txt in this directory for a description of
12 the cells format.13 the cells format.
Documentation/devicetree/bindings/pwm/st,stmpe-pwm.txt+18−0
@@ -0,0 +1,18 @@
1== ST STMPE PWM controller ==
2
3This is a PWM block embedded in the ST Microelectronics STMPE
4(ST Multi-Purpose Expander) chips. The PWM is registered as a
5subdevices of the STMPE MFD device.
6
7Required 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
13Example:
14
15pwm0: 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
34 First cell is voltage in microvolts (uV)34 First cell is voltage in microvolts (uV)
35 Second cell is duty-cycle in percent (%)35 Second cell is duty-cycle in percent (%)
36 36
37Optional 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
37NB: To be clear, if voltage-table is provided, then the device will be used49NB: To be clear, if voltage-table is provided, then the device will be used
38in Voltage Table Mode. If no voltage-table is provided, then the device will50in Voltage Table Mode. If no voltage-table is provided, then the device will
39be used in Continuous Voltage Mode.51be used in Continuous Voltage Mode.
@@ -53,6 +65,13 @@ Continuous Voltage With Enable GPIO Example: · 13 unmodified lines
53 regulator-min-microvolt = <1016000>;65 regulator-min-microvolt = <1016000>;
54 regulator-max-microvolt = <1114000>;66 regulator-max-microvolt = <1114000>;
55 regulator-name = "vdd_logic";67 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>; /* */
56 };75 };
57 76
58Voltage Table Example:77Voltage 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
380 return ret;380 return ret;
381}381}
382EXPORT_SYMBOL(cros_ec_cmd_xfer);382EXPORT_SYMBOL(cros_ec_cmd_xfer);
383
384int 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}
399EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
drivers/pwm/Kconfig+25−1
@@ -74,6 +74,16 @@ config PWM_ATMEL_TCB · 73 unmodified lines
74 To compile this driver as a module, choose M here: the module74 To compile this driver as a module, choose M here: the module
75 will be called pwm-atmel-tcb.75 will be called pwm-atmel-tcb.
76 76
77config 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
77config PWM_BCM_KONA87config PWM_BCM_KONA
78 tristate "Kona PWM support"88 tristate "Kona PWM support"
79 depends on ARCH_BCM_MOBILE89 depends on ARCH_BCM_MOBILE
@@ -137,6 +147,13 @@ config PWM_CRC · 57 unmodified lines
137 Generic PWM framework driver for Crystalcove (CRC) PMIC based PWM147 Generic PWM framework driver for Crystalcove (CRC) PMIC based PWM
138 control.148 control.
139 149
150config 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
140config PWM_EP93XX157config PWM_EP93XX
141 tristate "Cirrus Logic EP93xx PWM support"158 tristate "Cirrus Logic EP93xx PWM support"
142 depends on ARCH_EP93XX159 depends on ARCH_EP93XX
@@ -305,7 +322,7 @@ config PWM_PXA · 162 unmodified lines
305 322
306config PWM_RCAR323config PWM_RCAR
307 tristate "Renesas R-Car PWM support"324 tristate "Renesas R-Car PWM support"
308 depends on ARCH_RCAR_GEN1 || ARCH_RCAR_GEN2 || COMPILE_TEST325 depends on ARCH_RENESAS || COMPILE_TEST
309 depends on HAS_IOMEM326 depends on HAS_IOMEM
310 help327 help
311 This driver exposes the PWM Timer controller found in Renesas328 This driver exposes the PWM Timer controller found in Renesas
@@ -362,6 +379,13 @@ config PWM_STI · 50 unmodified lines
362 To compile this driver as a module, choose M here: the module379 To compile this driver as a module, choose M here: the module
363 will be called pwm-sti.380 will be called pwm-sti.
364 381
382config 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
365config PWM_SUN4I389config PWM_SUN4I
366 tristate "Allwinner PWM support"390 tristate "Allwinner PWM support"
367 depends on ARCH_SUNXI || COMPILE_TEST391 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
4obj-$(CONFIG_PWM_ATMEL) += pwm-atmel.o4obj-$(CONFIG_PWM_ATMEL) += pwm-atmel.o
5obj-$(CONFIG_PWM_ATMEL_HLCDC_PWM) += pwm-atmel-hlcdc.o5obj-$(CONFIG_PWM_ATMEL_HLCDC_PWM) += pwm-atmel-hlcdc.o
6obj-$(CONFIG_PWM_ATMEL_TCB) += pwm-atmel-tcb.o6obj-$(CONFIG_PWM_ATMEL_TCB) += pwm-atmel-tcb.o
7obj-$(CONFIG_PWM_BCM_IPROC) += pwm-bcm-iproc.o
7obj-$(CONFIG_PWM_BCM_KONA) += pwm-bcm-kona.o8obj-$(CONFIG_PWM_BCM_KONA) += pwm-bcm-kona.o
8obj-$(CONFIG_PWM_BCM2835) += pwm-bcm2835.o9obj-$(CONFIG_PWM_BCM2835) += pwm-bcm2835.o
9obj-$(CONFIG_PWM_BERLIN) += pwm-berlin.o10obj-$(CONFIG_PWM_BERLIN) += pwm-berlin.o
@@ -11,6 +12,7 @@ obj-$(CONFIG_PWM_BFIN) += pwm-bfin.o · 1 unmodified line
11obj-$(CONFIG_PWM_BRCMSTB) += pwm-brcmstb.o12obj-$(CONFIG_PWM_BRCMSTB) += pwm-brcmstb.o
12obj-$(CONFIG_PWM_CLPS711X) += pwm-clps711x.o13obj-$(CONFIG_PWM_CLPS711X) += pwm-clps711x.o
13obj-$(CONFIG_PWM_CRC) += pwm-crc.o14obj-$(CONFIG_PWM_CRC) += pwm-crc.o
15obj-$(CONFIG_PWM_CROS_EC) += pwm-cros-ec.o
14obj-$(CONFIG_PWM_EP93XX) += pwm-ep93xx.o16obj-$(CONFIG_PWM_EP93XX) += pwm-ep93xx.o
15obj-$(CONFIG_PWM_FSL_FTM) += pwm-fsl-ftm.o17obj-$(CONFIG_PWM_FSL_FTM) += pwm-fsl-ftm.o
16obj-$(CONFIG_PWM_IMG) += pwm-img.o18obj-$(CONFIG_PWM_IMG) += pwm-img.o
@@ -34,6 +36,7 @@ obj-$(CONFIG_PWM_ROCKCHIP) += pwm-rockchip.o · 17 unmodified lines
34obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o36obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o
35obj-$(CONFIG_PWM_SPEAR) += pwm-spear.o37obj-$(CONFIG_PWM_SPEAR) += pwm-spear.o
36obj-$(CONFIG_PWM_STI) += pwm-sti.o38obj-$(CONFIG_PWM_STI) += pwm-sti.o
39obj-$(CONFIG_PWM_STMPE) += pwm-stmpe.o
37obj-$(CONFIG_PWM_SUN4I) += pwm-sun4i.o40obj-$(CONFIG_PWM_SUN4I) += pwm-sun4i.o
38obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o41obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o
39obj-$(CONFIG_PWM_TIECAP) += pwm-tiecap.o42obj-$(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
525}525}
526EXPORT_SYMBOL_GPL(pwm_apply_state);526EXPORT_SYMBOL_GPL(pwm_apply_state);
527 527
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 */
536int 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}
553EXPORT_SYMBOL_GPL(pwm_capture);
554
528/**555/**
529 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments556 * pwm_adjust_config() - adjust the current PWM config to the PWM arguments
530 * @pwm: PWM device557 * @pwm: PWM device
drivers/pwm/pwm-atmel.c+17−13
@@ -64,7 +64,8 @@ struct atmel_pwm_chip { · 63 unmodified lines
64 void __iomem *base;64 void __iomem *base;
65 65
66 unsigned int updated_pwms;66 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;
68 69
69 void (*config)(struct pwm_chip *chip, struct pwm_device *pwm,70 void (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
70 unsigned long dty, unsigned long prd);71 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
271 mutex_unlock(&atmel_pwm->isr_lock);272 mutex_unlock(&atmel_pwm->isr_lock);
272 atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);273 atmel_pwm_writel(atmel_pwm, PWM_DIS, 1 << pwm->hwpwm);
273 274
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
274 clk_disable(atmel_pwm->clk);285 clk_disable(atmel_pwm->clk);
275}286}
276 287
@@ -324,21 +335,14 @@ MODULE_DEVICE_TABLE(of, atmel_pwm_dt_ids); · 47 unmodified lines
324static inline const struct atmel_pwm_data *335static inline const struct atmel_pwm_data *
325atmel_pwm_get_driver_data(struct platform_device *pdev)336atmel_pwm_get_driver_data(struct platform_device *pdev)
326{337{
327 if (pdev->dev.of_node) {338 const struct platform_device_id *id;
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;
333 339
334 return match->data;340 if (pdev->dev.of_node)
335 } else {341 return of_device_get_match_data(&pdev->dev);
336 const struct platform_device_id *id;
337 342
338 id = platform_get_device_id(pdev);343 id = platform_get_device_id(pdev);
339 344
340 return (struct atmel_pwm_data *)id->driver_data;345 return (struct atmel_pwm_data *)id->driver_data;
341 }
342}346}
343 347
344static int atmel_pwm_probe(struct platform_device *pdev)348static 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
46struct iproc_pwmc {
47 struct pwm_chip chip;
48 void __iomem *base;
49 struct clk *clk;
50};
51
52static inline struct iproc_pwmc *to_iproc_pwmc(struct pwm_chip *chip)
53{
54 return container_of(chip, struct iproc_pwmc, chip);
55}
56
57static 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
69static 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
81static 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
117static 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
187static const struct pwm_ops iproc_pwm_ops = {
188 .apply = iproc_pwmc_apply,
189 .get_state = iproc_pwmc_get_state,
190};
191
192static 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
250static 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
259static const struct of_device_id bcm_iproc_pwmc_dt[] = {
260 { .compatible = "brcm,iproc-pwm" },
261 { },
262};
263MODULE_DEVICE_TABLE(of, bcm_iproc_pwmc_dt);
264
265static 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};
273module_platform_driver(iproc_pwmc_driver);
274
275MODULE_AUTHOR("Yendapally Reddy Dhananjaya Reddy <yendapally.reddy@broadcom.com>");
276MODULE_DESCRIPTION("Broadcom iProc PWM driver");
277MODULE_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 */
25struct cros_ec_pwm_device {
26 struct device *dev;
27 struct cros_ec_device *ec;
28 struct pwm_chip chip;
29};
30
31static 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
36static 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
59static 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
93static 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
98static 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
117static 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
136static struct pwm_device *
137cros_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
154static 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
160static 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
188static 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
232static 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
241static const struct of_device_id cros_ec_pwm_of_match[] = {
242 { .compatible = "google,cros-ec-pwm" },
243 {},
244};
245MODULE_DEVICE_TABLE(of, cros_ec_pwm_of_match);
246#endif
247
248static 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};
256module_platform_driver(cros_ec_pwm_driver);
257
258MODULE_ALIAS("platform:cros-ec-pwm");
259MODULE_DESCRIPTION("ChromeOS EC PWM driver");
260MODULE_LICENSE("GPL v2");
drivers/pwm/pwm-lpc32xx.c+7−0
@@ -25,6 +25,7 @@ struct lpc32xx_pwm_chip { · 24 unmodified lines
25};25};
26 26
27#define PWM_ENABLE BIT(31)27#define PWM_ENABLE BIT(31)
28#define PWM_PIN_LEVEL BIT(30)
28 29
29#define to_lpc32xx_pwm_chip(_chip) \30#define to_lpc32xx_pwm_chip(_chip) \
30 container_of(_chip, struct lpc32xx_pwm_chip, chip)31 container_of(_chip, struct lpc32xx_pwm_chip, chip)
@@ -103,6 +104,7 @@ static int lpc32xx_pwm_probe(struct platform_device *pdev) · 72 unmodified lines
103 struct lpc32xx_pwm_chip *lpc32xx;104 struct lpc32xx_pwm_chip *lpc32xx;
104 struct resource *res;105 struct resource *res;
105 int ret;106 int ret;
107 u32 val;
106 108
107 lpc32xx = devm_kzalloc(&pdev->dev, sizeof(*lpc32xx), GFP_KERNEL);109 lpc32xx = devm_kzalloc(&pdev->dev, sizeof(*lpc32xx), GFP_KERNEL);
108 if (!lpc32xx)110 if (!lpc32xx)
@@ -128,6 +130,11 @@ static int lpc32xx_pwm_probe(struct platform_device *pdev) · 19 unmodified lines
128 return ret;130 return ret;
129 }131 }
130 132
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
131 platform_set_drvdata(pdev, lpc32xx);138 platform_set_drvdata(pdev, lpc32xx);
132 139
133 return 0;140 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
76 { PCI_VDEVICE(INTEL, 0x0ac8), (unsigned long)&pwm_lpss_bxt_info},76 { PCI_VDEVICE(INTEL, 0x0ac8), (unsigned long)&pwm_lpss_bxt_info},
77 { PCI_VDEVICE(INTEL, 0x0f08), (unsigned long)&pwm_lpss_byt_info},77 { PCI_VDEVICE(INTEL, 0x0f08), (unsigned long)&pwm_lpss_byt_info},
78 { PCI_VDEVICE(INTEL, 0x0f09), (unsigned long)&pwm_lpss_byt_info},78 { PCI_VDEVICE(INTEL, 0x0f09), (unsigned long)&pwm_lpss_byt_info},
79 { PCI_VDEVICE(INTEL, 0x11a5), (unsigned long)&pwm_lpss_bxt_info},
79 { PCI_VDEVICE(INTEL, 0x1ac8), (unsigned long)&pwm_lpss_bxt_info},80 { PCI_VDEVICE(INTEL, 0x1ac8), (unsigned long)&pwm_lpss_bxt_info},
80 { PCI_VDEVICE(INTEL, 0x2288), (unsigned long)&pwm_lpss_bsw_info},81 { PCI_VDEVICE(INTEL, 0x2288), (unsigned long)&pwm_lpss_bsw_info},
81 { PCI_VDEVICE(INTEL, 0x2289), (unsigned long)&pwm_lpss_bsw_info},82 { PCI_VDEVICE(INTEL, 0x2289), (unsigned long)&pwm_lpss_bsw_info},
drivers/pwm/pwm-lpss.c+14−12
@@ -27,7 +27,6 @@ · 26 unmodified lines
27#define PWM_SW_UPDATE BIT(30)27#define PWM_SW_UPDATE BIT(30)
28#define PWM_BASE_UNIT_SHIFT 828#define PWM_BASE_UNIT_SHIFT 8
29#define PWM_ON_TIME_DIV_MASK 0x000000ff29#define PWM_ON_TIME_DIV_MASK 0x000000ff
30#define PWM_DIVISION_CORRECTION 0x2
31 30
32/* Size of each PWM register space if multiple */31/* Size of each PWM register space if multiple */
33#define PWM_SIZE 0x40032#define PWM_SIZE 0x400
@@ -92,8 +91,8 @@ static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm, · 58 unmodified lines
92 int duty_ns, int period_ns)91 int duty_ns, int period_ns)
93{92{
94 struct pwm_lpss_chip *lpwm = to_lpwm(chip);93 struct pwm_lpss_chip *lpwm = to_lpwm(chip);
95 u8 on_time_div;94 unsigned long long on_time_div;
96 unsigned long c, base_unit_range;95 unsigned long c = lpwm->info->clk_rate, base_unit_range;
97 unsigned long long base_unit, freq = NSEC_PER_SEC;96 unsigned long long base_unit, freq = NSEC_PER_SEC;
98 u32 ctrl;97 u32 ctrl;
99 98
@@ -101,21 +100,18 @@ static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm, · 1 unmodified line
101 100
102 /*101 /*
103 * The equation is:102 * The equation is:
104 * base_unit = ((freq / c) * base_unit_range) + correction103 * base_unit = round(base_unit_range * freq / c)
105 */104 */
106 base_unit_range = BIT(lpwm->info->base_unit_bits);105 base_unit_range = BIT(lpwm->info->base_unit_bits);
107 base_unit = freq * base_unit_range;106 freq *= base_unit_range;
108 107
109 c = lpwm->info->clk_rate;108 base_unit = DIV_ROUND_CLOSEST_ULL(freq, c);
110 if (!c)
111 return -EINVAL;
112
113 do_div(base_unit, c);
114 base_unit += PWM_DIVISION_CORRECTION;
115 109
116 if (duty_ns <= 0)110 if (duty_ns <= 0)
117 duty_ns = 1;111 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;
119 115
120 pm_runtime_get_sync(chip->dev);116 pm_runtime_get_sync(chip->dev);
121 117
@@ -169,6 +165,7 @@ struct pwm_lpss_chip *pwm_lpss_probe(struct device *dev, struct resource *r, · 47 unmodified lines
169 const struct pwm_lpss_boardinfo *info)165 const struct pwm_lpss_boardinfo *info)
170{166{
171 struct pwm_lpss_chip *lpwm;167 struct pwm_lpss_chip *lpwm;
168 unsigned long c;
172 int ret;169 int ret;
173 170
174 lpwm = devm_kzalloc(dev, sizeof(*lpwm), GFP_KERNEL);171 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
180 return ERR_CAST(lpwm->regs);177 return ERR_CAST(lpwm->regs);
181 178
182 lpwm->info = info;179 lpwm->info = info;
180
181 c = lpwm->info->clk_rate;
182 if (!c)
183 return ERR_PTR(-EINVAL);
184
183 lpwm->chip.dev = dev;185 lpwm->chip.dev = dev;
184 lpwm->chip.ops = &pwm_lpss_ops;186 lpwm->chip.ops = &pwm_lpss_ops;
185 lpwm->chip.base = -1;187 lpwm->chip.base = -1;
drivers/pwm/pwm-rockchip.c+132−46
@@ -47,10 +47,14 @@ struct rockchip_pwm_regs { · 46 unmodified lines
47struct rockchip_pwm_data {47struct rockchip_pwm_data {
48 struct rockchip_pwm_regs regs;48 struct rockchip_pwm_regs regs;
49 unsigned int prescaler;49 unsigned int prescaler;
50 bool supports_polarity;
50 const struct pwm_ops *ops;51 const struct pwm_ops *ops;
51 52
52 void (*set_enable)(struct pwm_chip *chip,53 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);
54};58};
55 59
56static inline struct rockchip_pwm_chip *to_rockchip_pwm_chip(struct pwm_chip *c)60static 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
59}63}
60 64
61static void rockchip_pwm_set_enable_v1(struct pwm_chip *chip,65static 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)
63{68{
64 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);69 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
65 u32 enable_conf = PWM_CTRL_OUTPUT_EN | PWM_CTRL_TIMER_EN;70 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
75 writel_relaxed(val, pc->base + pc->data->regs.ctrl);80 writel_relaxed(val, pc->base + pc->data->regs.ctrl);
76}81}
77 82
83static 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
78static void rockchip_pwm_set_enable_v2(struct pwm_chip *chip,96static 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)
80{99{
81 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);100 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
82 u32 enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |101 u32 enable_conf = PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_ENABLE |
83 PWM_CONTINUOUS;102 PWM_CONTINUOUS;
84 u32 val;103 u32 val;
85 104
86 if (pwm_get_polarity(pwm) == PWM_POLARITY_INVERSED)105 if (polarity == PWM_POLARITY_INVERSED)
87 enable_conf |= PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSITIVE;106 enable_conf |= PWM_DUTY_NEGATIVE | PWM_INACTIVE_POSITIVE;
88 else107 else
89 enable_conf |= PWM_DUTY_POSITIVE | PWM_INACTIVE_NEGATIVE;108 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
98 writel_relaxed(val, pc->base + pc->data->regs.ctrl);117 writel_relaxed(val, pc->base + pc->data->regs.ctrl);
99}118}
100 119
120static 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
139static 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
101static int rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,167static int rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
102 int duty_ns, int period_ns)168 int duty_ns, int period_ns)
103{169{
104 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);170 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
105 unsigned long period, duty;171 unsigned long period, duty;
106 u64 clk_rate, div;172 u64 clk_rate, div;
107 int ret;
108 173
109 clk_rate = clk_get_rate(pc->clk);174 clk_rate = clk_get_rate(pc->clk);
110 175
@@ -114,74 +179,72 @@ static int rockchip_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, · 3 unmodified lines
114 * default prescaler value for all practical clock rate values.179 * default prescaler value for all practical clock rate values.
115 */180 */
116 div = clk_rate * period_ns;181 div = clk_rate * period_ns;
117 do_div(div, pc->data->prescaler * NSEC_PER_SEC);182 period = DIV_ROUND_CLOSEST_ULL(div,
118 period = div;183 pc->data->prescaler * NSEC_PER_SEC);
119 184
120 div = clk_rate * duty_ns;185 div = clk_rate * duty_ns;
121 do_div(div, pc->data->prescaler * NSEC_PER_SEC);186 duty = DIV_ROUND_CLOSEST_ULL(div, pc->data->prescaler * NSEC_PER_SEC);
122 duty = div;
123
124 ret = clk_enable(pc->clk);
125 if (ret)
126 return ret;
127 187
128 writel(period, pc->base + pc->data->regs.period);188 writel(period, pc->base + pc->data->regs.period);
129 writel(duty, pc->base + pc->data->regs.duty);189 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
137static 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 */
146 190
147 return 0;191 return 0;
148}192}
149 193
150static int rockchip_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)194static int rockchip_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
195 struct pwm_state *state)
151{196{
152 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);197 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);
198 struct pwm_state curstate;
199 bool enabled;
153 int ret;200 int ret;
154 201
202 pwm_get_state(pwm, &curstate);
203 enabled = curstate.enabled;
204
155 ret = clk_enable(pc->clk);205 ret = clk_enable(pc->clk);
156 if (ret)206 if (ret)
157 return ret;207 return ret;
158 208
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 }
160 213
161 return 0;214 ret = rockchip_pwm_config(chip, pwm, state->duty_cycle, state->period);
162}215 if (ret) {
216 if (enabled != curstate.enabled)
217 pc->data->set_enable(chip, pwm, !enabled,
218 state->polarity);
163 219
164static void rockchip_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)220 goto out;
165{221 }
166 struct rockchip_pwm_chip *pc = to_rockchip_pwm_chip(chip);222
223 if (state->enabled != enabled)
224 pc->data->set_enable(chip, pwm, state->enabled,
225 state->polarity);
167 226
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);
169 232
233out:
170 clk_disable(pc->clk);234 clk_disable(pc->clk);
235
236 return ret;
171}237}
172 238
173static const struct pwm_ops rockchip_pwm_ops_v1 = {239static const struct pwm_ops rockchip_pwm_ops_v1 = {
174 .config = rockchip_pwm_config,240 .get_state = rockchip_pwm_get_state,
175 .enable = rockchip_pwm_enable,241 .apply = rockchip_pwm_apply,
176 .disable = rockchip_pwm_disable,
177 .owner = THIS_MODULE,242 .owner = THIS_MODULE,
178};243};
179 244
180static const struct pwm_ops rockchip_pwm_ops_v2 = {245static const struct pwm_ops rockchip_pwm_ops_v2 = {
181 .config = rockchip_pwm_config,246 .get_state = rockchip_pwm_get_state,
182 .set_polarity = rockchip_pwm_set_polarity,247 .apply = rockchip_pwm_apply,
183 .enable = rockchip_pwm_enable,
184 .disable = rockchip_pwm_disable,
185 .owner = THIS_MODULE,248 .owner = THIS_MODULE,
186};249};
187 250
@@ -195,6 +258,7 @@ static const struct rockchip_pwm_data pwm_data_v1 = { · 7 unmodified lines
195 .prescaler = 2,258 .prescaler = 2,
196 .ops = &rockchip_pwm_ops_v1,259 .ops = &rockchip_pwm_ops_v1,
197 .set_enable = rockchip_pwm_set_enable_v1,260 .set_enable = rockchip_pwm_set_enable_v1,
261 .get_state = rockchip_pwm_get_state_v1,
198};262};
199 263
200static const struct rockchip_pwm_data pwm_data_v2 = {264static const struct rockchip_pwm_data pwm_data_v2 = {
@@ -205,8 +269,10 @@ static const struct rockchip_pwm_data pwm_data_v2 = { · 4 unmodified lines
205 .ctrl = 0x0c,269 .ctrl = 0x0c,
206 },270 },
207 .prescaler = 1,271 .prescaler = 1,
272 .supports_polarity = true,
208 .ops = &rockchip_pwm_ops_v2,273 .ops = &rockchip_pwm_ops_v2,
209 .set_enable = rockchip_pwm_set_enable_v2,274 .set_enable = rockchip_pwm_set_enable_v2,
275 .get_state = rockchip_pwm_get_state_v2,
210};276};
211 277
212static const struct rockchip_pwm_data pwm_data_vop = {278static const struct rockchip_pwm_data pwm_data_vop = {
@@ -217,8 +283,10 @@ static const struct rockchip_pwm_data pwm_data_vop = { · 4 unmodified lines
217 .ctrl = 0x00,283 .ctrl = 0x00,
218 },284 },
219 .prescaler = 1,285 .prescaler = 1,
286 .supports_polarity = true,
220 .ops = &rockchip_pwm_ops_v2,287 .ops = &rockchip_pwm_ops_v2,
221 .set_enable = rockchip_pwm_set_enable_v2,288 .set_enable = rockchip_pwm_set_enable_v2,
289 .get_state = rockchip_pwm_get_state_v2,
222};290};
223 291
224static const struct of_device_id rockchip_pwm_dt_ids[] = {292static 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
253 if (IS_ERR(pc->clk))321 if (IS_ERR(pc->clk))
254 return PTR_ERR(pc->clk);322 return PTR_ERR(pc->clk);
255 323
256 ret = clk_prepare(pc->clk);324 ret = clk_prepare_enable(pc->clk);
257 if (ret)325 if (ret)
258 return ret;326 return ret;
259 327
@@ -265,7 +333,7 @@ static int rockchip_pwm_probe(struct platform_device *pdev) · 5 unmodified lines
265 pc->chip.base = -1;333 pc->chip.base = -1;
266 pc->chip.npwm = 1;334 pc->chip.npwm = 1;
267 335
268 if (pc->data->ops->set_polarity) {336 if (pc->data->supports_polarity) {
269 pc->chip.of_xlate = of_pwm_xlate_with_flags;337 pc->chip.of_xlate = of_pwm_xlate_with_flags;
270 pc->chip.of_pwm_n_cells = 3;338 pc->chip.of_pwm_n_cells = 3;
271 }339 }
@@ -276,6 +344,10 @@ static int rockchip_pwm_probe(struct platform_device *pdev) · 4 unmodified lines
276 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);344 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
277 }345 }
278 346
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
279 return ret;351 return ret;
280}352}
281 353
@@ -283,6 +355,20 @@ static int rockchip_pwm_remove(struct platform_device *pdev) · 1 unmodified line
283{355{
284 struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev);356 struct rockchip_pwm_chip *pc = platform_get_drvdata(pdev);
285 357
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
286 clk_unprepare(pc->clk);372 clk_unprepare(pc->clk);
287 373
288 return pwmchip_remove(&pc->chip);374 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
32struct stmpe_pwm {
33 struct stmpe *stmpe;
34 struct pwm_chip chip;
35 u8 last_duty;
36};
37
38static inline struct stmpe_pwm *to_stmpe_pwm(struct pwm_chip *chip)
39{
40 return container_of(chip, struct stmpe_pwm, chip);
41}
42
43static 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
68static 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
103static 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
266static 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
273static 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
314static struct platform_driver stmpe_pwm_driver = {
315 .driver = {
316 .name = "stmpe-pwm",
317 },
318};
319builtin_platform_driver_probe(stmpe_pwm_driver, stmpe_pwm_probe);
drivers/pwm/pwm-tegra.c+52−17
@@ -26,9 +26,11 @@ · 25 unmodified lines
26#include <linux/io.h>26#include <linux/io.h>
27#include <linux/module.h>27#include <linux/module.h>
28#include <linux/of.h>28#include <linux/of.h>
29#include <linux/of_device.h>
29#include <linux/pwm.h>30#include <linux/pwm.h>
30#include <linux/platform_device.h>31#include <linux/platform_device.h>
31#include <linux/slab.h>32#include <linux/slab.h>
33#include <linux/reset.h>
32 34
33#define PWM_ENABLE (1 << 31)35#define PWM_ENABLE (1 << 31)
34#define PWM_DUTY_WIDTH 836#define PWM_DUTY_WIDTH 8
@@ -36,15 +38,20 @@ · 1 unmodified line
36#define PWM_SCALE_WIDTH 1338#define PWM_SCALE_WIDTH 13
37#define PWM_SCALE_SHIFT 039#define PWM_SCALE_SHIFT 0
38 40
39#define NUM_PWM 441struct tegra_pwm_soc {
42 unsigned int num_channels;
43};
40 44
41struct tegra_pwm_chip {45struct tegra_pwm_chip {
42 struct pwm_chip chip;46 struct pwm_chip chip;
43 struct device *dev;47 struct device *dev;
48
49 struct clk *clk;
50 struct reset_control*rst;
44 51
45 struct clk *clk;52 void __iomem *regs;
46 53
47 void __iomem *mmio_base;54 const struct tegra_pwm_soc *soc;
48};55};
49 56
50static inline struct tegra_pwm_chip *to_tegra_pwm_chip(struct pwm_chip *chip)57static 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
54 61
55static inline u32 pwm_readl(struct tegra_pwm_chip *chip, unsigned int num)62static inline u32 pwm_readl(struct tegra_pwm_chip *chip, unsigned int num)
56{63{
57 return readl(chip->mmio_base + (num << 4));64 return readl(chip->regs + (num << 4));
58}65}
59 66
60static inline void pwm_writel(struct tegra_pwm_chip *chip, unsigned int num,67static inline void pwm_writel(struct tegra_pwm_chip *chip, unsigned int num,
61 unsigned long val)68 unsigned long val)
62{69{
63 writel(val, chip->mmio_base + (num << 4));70 writel(val, chip->regs + (num << 4));
64}71}
65 72
66static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,73static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
67 int duty_ns, int period_ns)74 int duty_ns, int period_ns)
68{75{
69 struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);76 struct tegra_pwm_chip *pc = to_tegra_pwm_chip(chip);
70 unsigned long long c;77 unsigned long long c = duty_ns;
71 unsigned long rate, hz;78 unsigned long rate, hz;
72 u32 val = 0;79 u32 val = 0;
73 int err;80 int err;
@@ -77,7 +84,8 @@ static int tegra_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, · 3 unmodified lines
77 * per (1 << PWM_DUTY_WIDTH) cycles and make sure to round to the84 * per (1 << PWM_DUTY_WIDTH) cycles and make sure to round to the
78 * nearest integer during division.85 * nearest integer during division.
79 */86 */
80 c = duty_ns * ((1 << PWM_DUTY_WIDTH) - 1) + period_ns / 2;87 c *= (1 << PWM_DUTY_WIDTH);
88 c += period_ns / 2;
81 do_div(c, period_ns);89 do_div(c, period_ns);
82 90
83 val = (u32)c << PWM_DUTY_SHIFT;91 val = (u32)c << PWM_DUTY_SHIFT;
@@ -176,12 +184,13 @@ static int tegra_pwm_probe(struct platform_device *pdev) · 92 unmodified lines
176 if (!pwm)184 if (!pwm)
177 return -ENOMEM;185 return -ENOMEM;
178 186
187 pwm->soc = of_device_get_match_data(&pdev->dev);
179 pwm->dev = &pdev->dev;188 pwm->dev = &pdev->dev;
180 189
181 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);190 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
182 pwm->mmio_base = devm_ioremap_resource(&pdev->dev, r);191 pwm->regs = devm_ioremap_resource(&pdev->dev, r);
183 if (IS_ERR(pwm->mmio_base))192 if (IS_ERR(pwm->regs))
184 return PTR_ERR(pwm->mmio_base);193 return PTR_ERR(pwm->regs);
185 194
186 platform_set_drvdata(pdev, pwm);195 platform_set_drvdata(pdev, pwm);
187 196
@@ -189,14 +198,24 @@ static int tegra_pwm_probe(struct platform_device *pdev) · 1 unmodified line
189 if (IS_ERR(pwm->clk))198 if (IS_ERR(pwm->clk))
190 return PTR_ERR(pwm->clk);199 return PTR_ERR(pwm->clk);
191 200
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
192 pwm->chip.dev = &pdev->dev;210 pwm->chip.dev = &pdev->dev;
193 pwm->chip.ops = &tegra_pwm_ops;211 pwm->chip.ops = &tegra_pwm_ops;
194 pwm->chip.base = -1;212 pwm->chip.base = -1;
195 pwm->chip.npwm = NUM_PWM;213 pwm->chip.npwm = pwm->soc->num_channels;
196 214
197 ret = pwmchip_add(&pwm->chip);215 ret = pwmchip_add(&pwm->chip);
198 if (ret < 0) {216 if (ret < 0) {
199 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);217 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
218 reset_control_assert(pwm->rst);
200 return ret;219 return ret;
201 }220 }
202 221
@@ -206,12 +225,17 @@ static int tegra_pwm_probe(struct platform_device *pdev) · 3 unmodified lines
206static int tegra_pwm_remove(struct platform_device *pdev)225static int tegra_pwm_remove(struct platform_device *pdev)
207{226{
208 struct tegra_pwm_chip *pc = platform_get_drvdata(pdev);227 struct tegra_pwm_chip *pc = platform_get_drvdata(pdev);
209 int i;228 unsigned int i;
229 int err;
210 230
211 if (WARN_ON(!pc))231 if (WARN_ON(!pc))
212 return -ENODEV;232 return -ENODEV;
213 233
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++) {
215 struct pwm_device *pwm = &pc->chip.pwms[i];239 struct pwm_device *pwm = &pc->chip.pwms[i];
216 240
217 if (!pwm_is_enabled(pwm))241 if (!pwm_is_enabled(pwm))
@@ -223,12 +247,23 @@ static int tegra_pwm_remove(struct platform_device *pdev) · 5 unmodified lines
223 clk_disable_unprepare(pc->clk);247 clk_disable_unprepare(pc->clk);
224 }248 }
225 249
250 reset_control_assert(pc->rst);
251 clk_disable_unprepare(pc->clk);
252
226 return pwmchip_remove(&pc->chip);253 return pwmchip_remove(&pc->chip);
227}254}
228 255
256static const struct tegra_pwm_soc tegra20_pwm_soc = {
257 .num_channels = 4,
258};
259
260static const struct tegra_pwm_soc tegra186_pwm_soc = {
261 .num_channels = 1,
262};
263
229static const struct of_device_id tegra_pwm_of_match[] = {264static const struct of_device_id tegra_pwm_of_match[] = {
230 { .compatible = "nvidia,tegra20-pwm" },265 { .compatible = "nvidia,tegra20-pwm", .data = &tegra20_pwm_soc },
231 { .compatible = "nvidia,tegra30-pwm" },266 { .compatible = "nvidia,tegra186-pwm", .data = &tegra186_pwm_soc },
232 { }267 { }
233};268};
234 269
drivers/pwm/pwm-tiecap.c+9−28
@@ -27,8 +27,6 @@ · 26 unmodified lines
27#include <linux/pwm.h>27#include <linux/pwm.h>
28#include <linux/of_device.h>28#include <linux/of_device.h>
29 29
30#include "pwm-tipwmss.h"
31
32/* ECAP registers and bits definitions */30/* ECAP registers and bits definitions */
33#define CAP1 0x0831#define CAP1 0x08
34#define CAP2 0x0C32#define CAP2 0x0C
@@ -195,6 +193,7 @@ static const struct pwm_ops ecap_pwm_ops = { · 160 unmodified lines
195};193};
196 194
197static const struct of_device_id ecap_of_match[] = {195static const struct of_device_id ecap_of_match[] = {
196 { .compatible = "ti,am3352-ecap" },
198 { .compatible = "ti,am33xx-ecap" },197 { .compatible = "ti,am33xx-ecap" },
199 {},198 {},
200};199};
@@ -202,17 +201,24 @@ MODULE_DEVICE_TABLE(of, ecap_of_match); · 1 unmodified line
202 201
203static int ecap_pwm_probe(struct platform_device *pdev)202static int ecap_pwm_probe(struct platform_device *pdev)
204{203{
204 struct device_node *np = pdev->dev.of_node;
205 int ret;205 int ret;
206 struct resource *r;206 struct resource *r;
207 struct clk *clk;207 struct clk *clk;
208 struct ecap_pwm_chip *pc;208 struct ecap_pwm_chip *pc;
209 u16 status;
210 209
211 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);210 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
212 if (!pc)211 if (!pc)
213 return -ENOMEM;212 return -ENOMEM;
214 213
215 clk = devm_clk_get(&pdev->dev, "fck");214 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
216 if (IS_ERR(clk)) {222 if (IS_ERR(clk)) {
217 dev_err(&pdev->dev, "failed to get clock\n");223 dev_err(&pdev->dev, "failed to get clock\n");
218 return PTR_ERR(clk);224 return PTR_ERR(clk);
@@ -243,40 +249,15 @@ static int ecap_pwm_probe(struct platform_device *pdev) · 24 unmodified lines
243 }249 }
244 250
245 pm_runtime_enable(&pdev->dev);251 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);
257 252
258 platform_set_drvdata(pdev, pc);253 platform_set_drvdata(pdev, pc);
259 return 0;254 return 0;
260
261pwmss_clk_failure:
262 pm_runtime_put_sync(&pdev->dev);
263 pm_runtime_disable(&pdev->dev);
264 pwmchip_remove(&pc->chip);
265 return ret;
266}255}
267 256
268static int ecap_pwm_remove(struct platform_device *pdev)257static int ecap_pwm_remove(struct platform_device *pdev)
269{258{
270 struct ecap_pwm_chip *pc = platform_get_drvdata(pdev);259 struct ecap_pwm_chip *pc = platform_get_drvdata(pdev);
271 260
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
280 pm_runtime_disable(&pdev->dev);261 pm_runtime_disable(&pdev->dev);
281 return pwmchip_remove(&pc->chip);262 return pwmchip_remove(&pc->chip);
282}263}
drivers/pwm/pwm-tiehrpwm.c+9−29
@@ -27,8 +27,6 @@ · 26 unmodified lines
27#include <linux/pm_runtime.h>27#include <linux/pm_runtime.h>
28#include <linux/of_device.h>28#include <linux/of_device.h>
29 29
30#include "pwm-tipwmss.h"
31
32/* EHRPWM registers and bits definitions */30/* EHRPWM registers and bits definitions */
33 31
34/* Time base module registers */32/* Time base module registers */
@@ -426,6 +424,7 @@ static const struct pwm_ops ehrpwm_pwm_ops = { · 391 unmodified lines
426};424};
427 425
428static const struct of_device_id ehrpwm_of_match[] = {426static const struct of_device_id ehrpwm_of_match[] = {
427 { .compatible = "ti,am3352-ehrpwm" },
429 { .compatible = "ti,am33xx-ehrpwm" },428 { .compatible = "ti,am33xx-ehrpwm" },
430 {},429 {},
431};430};
@@ -433,17 +432,24 @@ MODULE_DEVICE_TABLE(of, ehrpwm_of_match); · 1 unmodified line
433 432
434static int ehrpwm_pwm_probe(struct platform_device *pdev)433static int ehrpwm_pwm_probe(struct platform_device *pdev)
435{434{
435 struct device_node *np = pdev->dev.of_node;
436 int ret;436 int ret;
437 struct resource *r;437 struct resource *r;
438 struct clk *clk;438 struct clk *clk;
439 struct ehrpwm_pwm_chip *pc;439 struct ehrpwm_pwm_chip *pc;
440 u16 status;
441 440
442 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);441 pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
443 if (!pc)442 if (!pc)
444 return -ENOMEM;443 return -ENOMEM;
445 444
446 clk = devm_clk_get(&pdev->dev, "fck");445 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
447 if (IS_ERR(clk)) {453 if (IS_ERR(clk)) {
448 dev_err(&pdev->dev, "failed to get clock\n");454 dev_err(&pdev->dev, "failed to get clock\n");
449 return PTR_ERR(clk);455 return PTR_ERR(clk);
@@ -487,27 +493,9 @@ static int ehrpwm_pwm_probe(struct platform_device *pdev) · 37 unmodified lines
487 }493 }
488 494
489 pm_runtime_enable(&pdev->dev);495 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);
501 496
502 platform_set_drvdata(pdev, pc);497 platform_set_drvdata(pdev, pc);
503 return 0;498 return 0;
504
505pwmss_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;
511}499}
512 500
513static int ehrpwm_pwm_remove(struct platform_device *pdev)501static int ehrpwm_pwm_remove(struct platform_device *pdev)
@@ -516,14 +504,6 @@ static int ehrpwm_pwm_remove(struct platform_device *pdev) · 2 unmodified lines
516 504
517 clk_unprepare(pc->tbclk);505 clk_unprepare(pc->tbclk);
518 506
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
527 pm_runtime_put_sync(&pdev->dev);507 pm_runtime_put_sync(&pdev->dev);
528 pm_runtime_disable(&pdev->dev);508 pm_runtime_disable(&pdev->dev);
529 return pwmchip_remove(&pc->chip);509 return pwmchip_remove(&pc->chip);
drivers/pwm/pwm-tipwmss.c+0−49
@@ -22,32 +22,6 @@ · 21 unmodified lines
22#include <linux/pm_runtime.h>22#include <linux/pm_runtime.h>
23#include <linux/of_device.h>23#include <linux/of_device.h>
24 24
25#include "pwm-tipwmss.h"
26
27#define PWMSS_CLKCONFIG 0x8 /* Clock gating reg */
28#define PWMSS_CLKSTATUS 0xc /* Clock gating status reg */
29
30struct pwmss_info {
31 void __iomem *mmio_base;
32 struct mutex pwmss_lock;
33 u16 pwmss_clkconfig;
34};
35
36u16 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}
49EXPORT_SYMBOL(pwmss_submodule_state_change);
50
51static const struct of_device_id pwmss_of_match[] = {25static const struct of_device_id pwmss_of_match[] = {
52 { .compatible = "ti,am33xx-pwmss" },26 { .compatible = "ti,am33xx-pwmss" },
53 {},27 {},
@@ -57,24 +31,10 @@ MODULE_DEVICE_TABLE(of, pwmss_of_match); · 3 unmodified lines
57static int pwmss_probe(struct platform_device *pdev)31static int pwmss_probe(struct platform_device *pdev)
58{32{
59 int ret;33 int ret;
60 struct resource *r;
61 struct pwmss_info *info;
62 struct device_node *node = pdev->dev.of_node;34 struct device_node *node = pdev->dev.of_node;
63 35
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
75 pm_runtime_enable(&pdev->dev);36 pm_runtime_enable(&pdev->dev);
76 pm_runtime_get_sync(&pdev->dev);37 pm_runtime_get_sync(&pdev->dev);
77 platform_set_drvdata(pdev, info);
78 38
79 /* Populate all the child nodes here... */39 /* Populate all the child nodes here... */
80 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);40 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
@@ -86,30 +46,21 @@ static int pwmss_probe(struct platform_device *pdev) · 5 unmodified lines
86 46
87static int pwmss_remove(struct platform_device *pdev)47static int pwmss_remove(struct platform_device *pdev)
88{48{
89 struct pwmss_info *info = platform_get_drvdata(pdev);
90
91 pm_runtime_put_sync(&pdev->dev);49 pm_runtime_put_sync(&pdev->dev);
92 pm_runtime_disable(&pdev->dev);50 pm_runtime_disable(&pdev->dev);
93 mutex_destroy(&info->pwmss_lock);
94 return 0;51 return 0;
95}52}
96 53
97#ifdef CONFIG_PM_SLEEP54#ifdef CONFIG_PM_SLEEP
98static int pwmss_suspend(struct device *dev)55static int pwmss_suspend(struct device *dev)
99{56{
100 struct pwmss_info *info = dev_get_drvdata(dev);
101
102 info->pwmss_clkconfig = readw(info->mmio_base + PWMSS_CLKCONFIG);
103 pm_runtime_put_sync(dev);57 pm_runtime_put_sync(dev);
104 return 0;58 return 0;
105}59}
106 60
107static int pwmss_resume(struct device *dev)61static int pwmss_resume(struct device *dev)
108{62{
109 struct pwmss_info *info = dev_get_drvdata(dev);
110
111 pm_runtime_get_sync(dev);63 pm_runtime_get_sync(dev);
112 writew(info->pwmss_clkconfig, info->mmio_base + PWMSS_CLKCONFIG);
113 return 0;64 return 0;
114}65}
115#endif66#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
31extern u16 pwmss_submodule_state_change(struct device *dev, int set);
32#else
33static 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
208 return ret ? : size;208 return ret ? : size;
209}209}
210 210
211static 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
211static DEVICE_ATTR_RW(period);226static DEVICE_ATTR_RW(period);
212static DEVICE_ATTR_RW(duty_cycle);227static DEVICE_ATTR_RW(duty_cycle);
213static DEVICE_ATTR_RW(enable);228static DEVICE_ATTR_RW(enable);
214static DEVICE_ATTR_RW(polarity);229static DEVICE_ATTR_RW(polarity);
230static DEVICE_ATTR_RO(capture);
215 231
216static struct attribute *pwm_attrs[] = {232static struct attribute *pwm_attrs[] = {
217 &dev_attr_period.attr,233 &dev_attr_period.attr,
218 &dev_attr_duty_cycle.attr,234 &dev_attr_duty_cycle.attr,
219 &dev_attr_enable.attr,235 &dev_attr_enable.attr,
220 &dev_attr_polarity.attr,236 &dev_attr_polarity.attr,
237 &dev_attr_capture.attr,
221 NULL238 NULL
222};239};
223ATTRIBUTE_GROUPS(pwm);240ATTRIBUTE_GROUPS(pwm);
drivers/regulator/pwm-regulator.c+118−44
@@ -22,6 +22,12 @@ · 21 unmodified lines
22#include <linux/pwm.h>22#include <linux/pwm.h>
23#include <linux/gpio/consumer.h>23#include <linux/gpio/consumer.h>
24 24
25struct pwm_continuous_reg_data {
26 unsigned int min_uV_dutycycle;
27 unsigned int max_uV_dutycycle;
28 unsigned int dutycycle_unit;
29};
30
25struct pwm_regulator_data {31struct pwm_regulator_data {
26 /* Shared */32 /* Shared */
27 struct pwm_device *pwm;33 struct pwm_device *pwm;
@@ -29,6 +35,9 @@ struct pwm_regulator_data { · 1 unmodified line
29 /* Voltage table */35 /* Voltage table */
30 struct pwm_voltages *duty_cycle_table;36 struct pwm_voltages *duty_cycle_table;
31 37
38 /* Continuous mode info */
39 struct pwm_continuous_reg_data continuous;
40
32 /* regulator descriptor */41 /* regulator descriptor */
33 struct regulator_desc desc;42 struct regulator_desc desc;
34 43
@@ -37,9 +46,6 @@ struct pwm_regulator_data { · 2 unmodified lines
37 46
38 int state;47 int state;
39 48
40 /* Continuous voltage */
41 int volt_uV;
42
43 /* Enable GPIO */49 /* Enable GPIO */
44 struct gpio_desc *enb_gpio;50 struct gpio_desc *enb_gpio;
45};51};
@@ -52,10 +58,31 @@ struct pwm_voltages { · 6 unmodified lines
52/**58/**
53 * Voltage table call-backs59 * Voltage table call-backs
54 */60 */
61static 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
55static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)79static int pwm_regulator_get_voltage_sel(struct regulator_dev *rdev)
56{80{
57 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);81 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
58 82
83 if (drvdata->state < 0)
84 pwm_regulator_init_state(rdev);
85
59 return drvdata->state;86 return drvdata->state;
60}87}
61 88
@@ -63,16 +90,14 @@ static int pwm_regulator_set_voltage_sel(struct regulator_dev *rdev, · 1 unmodified line
63 unsigned selector)90 unsigned selector)
64{91{
65 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);92 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);
66 struct pwm_args pargs;93 struct pwm_state pstate;
67 int dutycycle;
68 int ret;94 int ret;
69 95
70 pwm_get_args(drvdata->pwm, &pargs);96 pwm_init_state(drvdata->pwm, &pstate);
71 97 pwm_set_relative_duty_cycle(&pstate,
72 dutycycle = (pargs.period *98 drvdata->duty_cycle_table[selector].dutycycle, 100);
73 drvdata->duty_cycle_table[selector].dutycycle) / 100;
74 99
75 ret = pwm_config(drvdata->pwm, dutycycle, pargs.period);100 ret = pwm_apply_state(drvdata->pwm, &pstate);
76 if (ret) {101 if (ret) {
77 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);102 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
78 return ret;103 return ret;
@@ -129,57 +154,90 @@ static int pwm_regulator_is_enabled(struct regulator_dev *dev) · 50 unmodified lines
129static int pwm_regulator_get_voltage(struct regulator_dev *rdev)154static int pwm_regulator_get_voltage(struct regulator_dev *rdev)
130{155{
131 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);156 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 }
132 183
133 return drvdata->volt_uV;184 voltage = DIV_ROUND_CLOSEST_ULL((u64)voltage * diff_uV, diff_duty);
185
186 return voltage + min_uV;
134}187}
135 188
136static int pwm_regulator_set_voltage(struct regulator_dev *rdev,189static int pwm_regulator_set_voltage(struct regulator_dev *rdev,
137 int min_uV, int max_uV,190 int req_min_uV, int req_max_uV,
138 unsigned *selector)191 unsigned int *selector)
139{192{
140 struct pwm_regulator_data *drvdata = rdev_get_drvdata(rdev);193 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;
141 unsigned int ramp_delay = rdev->constraints->ramp_delay;197 unsigned int ramp_delay = rdev->constraints->ramp_delay;
142 struct pwm_args pargs;198 int min_uV = rdev->constraints->min_uV;
143 unsigned int req_diff = min_uV - rdev->constraints->min_uV;199 int max_uV = rdev->constraints->max_uV;
144 unsigned int diff;200 int diff_uV = max_uV - min_uV;
145 unsigned int duty_pulse;201 struct pwm_state pstate;
146 u64 req_period;
147 u32 rem;
148 int old_uV = pwm_regulator_get_voltage(rdev);202 int old_uV = pwm_regulator_get_voltage(rdev);
203 unsigned int diff_duty;
204 unsigned int dutycycle;
149 int ret;205 int ret;
150 206
151 pwm_get_args(drvdata->pwm, &pargs);207 pwm_init_state(drvdata->pwm, &pstate);
152 diff = rdev->constraints->max_uV - rdev->constraints->min_uV;
153 208
154 /* First try to find out if we get the iduty cycle time which is209 /*
155 * factor of PWM period time. If (request_diff_to_min * pwm_period)210 * The dutycycle for min_uV might be greater than the one for max_uV.
156 * is perfect divided by voltage_range_diff then it is possible to211 * This is happening when the user needs an inversed polarity, but the
157 * get duty cycle time which is factor of PWM period. This will help212 * PWM device does not support inversing it in hardware.
158 * to get output voltage nearer to requested value as there is no
159 * calculation loss.
160 */213 */
161 req_period = req_diff * pargs.period;214 if (max_uV_duty < min_uV_duty)
162 div_u64_rem(req_period, diff, &rem);215 diff_duty = min_uV_duty - max_uV_duty;
163 if (!rem) {216 else
164 do_div(req_period, diff);217 diff_duty = max_uV_duty - min_uV_duty;
165 duty_pulse = (unsigned int)req_period;218
166 } else {219 dutycycle = DIV_ROUND_CLOSEST_ULL((u64)(req_min_uV - min_uV) *
167 duty_pulse = (pargs.period / 100) * ((req_diff * 100) / diff);220 diff_duty,
168 }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);
169 229
170 ret = pwm_config(drvdata->pwm, duty_pulse, pargs.period);230 ret = pwm_apply_state(drvdata->pwm, &pstate);
171 if (ret) {231 if (ret) {
172 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);232 dev_err(&rdev->dev, "Failed to configure PWM: %d\n", ret);
173 return ret;233 return ret;
174 }234 }
175 235
176 drvdata->volt_uV = min_uV;
177
178 if ((ramp_delay == 0) || !pwm_regulator_is_enabled(rdev))236 if ((ramp_delay == 0) || !pwm_regulator_is_enabled(rdev))
179 return 0;237 return 0;
180 238
181 /* Ramp delay is in uV/uS. Adjust to uS and delay */239 /* 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);
183 usleep_range(ramp_delay, ramp_delay + DIV_ROUND_UP(ramp_delay, 10));241 usleep_range(ramp_delay, ramp_delay + DIV_ROUND_UP(ramp_delay, 10));
184 242
185 return 0;243 return 0;
@@ -239,6 +297,7 @@ static int pwm_regulator_init_table(struct platform_device *pdev, · 53 unmodified lines
239 return ret;297 return ret;
240 }298 }
241 299
300 drvdata->state = -EINVAL;
242 drvdata->duty_cycle_table = duty_cycle_table;301 drvdata->duty_cycle_table = duty_cycle_table;
243 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,302 memcpy(&drvdata->ops, &pwm_regulator_voltage_table_ops,
244 sizeof(drvdata->ops));303 sizeof(drvdata->ops));
@@ -251,11 +310,28 @@ static int pwm_regulator_init_table(struct platform_device *pdev, · 6 unmodified lines
251static int pwm_regulator_init_continuous(struct platform_device *pdev,310static int pwm_regulator_init_continuous(struct platform_device *pdev,
252 struct pwm_regulator_data *drvdata)311 struct pwm_regulator_data *drvdata)
253{312{
313 u32 dutycycle_range[2] = { 0, 100 };
314 u32 dutycycle_unit = 100;
315
254 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,316 memcpy(&drvdata->ops, &pwm_regulator_voltage_continuous_ops,
255 sizeof(drvdata->ops));317 sizeof(drvdata->ops));
256 drvdata->desc.ops = &drvdata->ops;318 drvdata->desc.ops = &drvdata->ops;
257 drvdata->desc.continuous_voltage_range = true;319 drvdata->desc.continuous_voltage_range = true;
258 320
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
259 return 0;335 return 0;
260}336}
261 337
@@ -316,11 +392,9 @@ static int pwm_regulator_probe(struct platform_device *pdev) · 54 unmodified lines
316 return ret;392 return ret;
317 }393 }
318 394
319 /*395 ret = pwm_adjust_config(drvdata->pwm);
320 * FIXME: pwm_apply_args() should be removed when switching to the396 if (ret)
321 * atomic PWM API.397 return ret;
322 */
323 pwm_apply_args(drvdata->pwm);
324 398
325 regulator = devm_regulator_register(&pdev->dev,399 regulator = devm_regulator_register(&pdev->dev,
326 &drvdata->desc, &config);400 &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
225int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,225int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
226 struct cros_ec_command *msg);226 struct cros_ec_command *msg);
227 227
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 */
240int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
241 struct cros_ec_command *msg);
242
228/**243/**
229 * cros_ec_remove - Remove a ChromeOS EC244 * cros_ec_remove - Remove a ChromeOS EC
230 *245 *
include/linux/mfd/cros_ec_commands.h+31−0
@@ -949,6 +949,37 @@ struct ec_params_pwm_set_fan_duty { · 948 unmodified lines
949 uint32_t percent;949 uint32_t percent;
950} __packed;950} __packed;
951 951
952#define EC_CMD_PWM_SET_DUTY 0x25
953/* 16 bit duty cycle, 0xffff = 100% */
954#define EC_PWM_MAX_DUTY 0xffff
955
956enum 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
966struct 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
974struct 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
979struct ec_response_pwm_get_duty {
980 uint16_t duty; /* Duty cycle, EC_PWM_MAX_DUTY = 100% */
981} __packed;
982
952/*****************************************************************************/983/*****************************************************************************/
953/*984/*
954 * Lightbar commands. This looks worse than it is. Since we only use one HOST985 * 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
5#include <linux/mutex.h>5#include <linux/mutex.h>
6#include <linux/of.h>6#include <linux/of.h>
7 7
8struct pwm_capture;
8struct seq_file;9struct seq_file;
10
9struct pwm_chip;11struct pwm_chip;
10 12
11/**13/**
@@ -147,12 +149,101 @@ static inline void pwm_get_args(const struct pwm_device *pwm, · 135 unmodified lines
147 *args = pwm->args;149 *args = pwm->args;
148}150}
149 151
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 */
169static 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 */
198static inline unsigned int
199pwm_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 */
226static inline int
227pwm_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
150/**240/**
151 * struct pwm_ops - PWM controller operations241 * struct pwm_ops - PWM controller operations
152 * @request: optional hook for requesting a PWM242 * @request: optional hook for requesting a PWM
153 * @free: optional hook for freeing a PWM243 * @free: optional hook for freeing a PWM
154 * @config: configure duty cycles and period length for this PWM244 * @config: configure duty cycles and period length for this PWM
155 * @set_polarity: configure the polarity of this PWM245 * @set_polarity: configure the polarity of this PWM
246 * @capture: capture and report PWM signal
156 * @enable: enable PWM output toggling247 * @enable: enable PWM output toggling
157 * @disable: disable PWM output toggling248 * @disable: disable PWM output toggling
158 * @apply: atomically apply a new PWM config. The state argument249 * @apply: atomically apply a new PWM config. The state argument
@@ -172,6 +263,8 @@ struct pwm_ops { · 13 unmodified lines
172 int duty_ns, int period_ns);263 int duty_ns, int period_ns);
173 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,264 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
174 enum pwm_polarity polarity);265 enum pwm_polarity polarity);
266 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
267 struct pwm_capture *result, unsigned long timeout);
175 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);268 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
176 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);269 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
177 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,270 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
@@ -212,6 +305,16 @@ struct pwm_chip { · 34 unmodified lines
212 bool can_sleep;305 bool can_sleep;
213};306};
214 307
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 */
313struct pwm_capture {
314 unsigned int period;
315 unsigned int duty_cycle;
316};
317
215#if IS_ENABLED(CONFIG_PWM)318#if IS_ENABLED(CONFIG_PWM)
216/* PWM user APIs */319/* PWM user APIs */
217struct pwm_device *pwm_request(int pwm_id, const char *label);320struct 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
323 pwm_apply_state(pwm, &state);426 pwm_apply_state(pwm, &state);
324}427}
325 428
326
327/* PWM provider APIs */429/* PWM provider APIs */
430int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
431 unsigned long timeout);
328int pwm_set_chip_data(struct pwm_device *pwm, void *data);432int pwm_set_chip_data(struct pwm_device *pwm, void *data);
329void *pwm_get_chip_data(struct pwm_device *pwm);433void *pwm_get_chip_data(struct pwm_device *pwm);
330 434
@@ -376,6 +480,13 @@ static inline int pwm_config(struct pwm_device *pwm, int duty_ns, · 45 unmodified lines
376 return -EINVAL;480 return -EINVAL;
377}481}
378 482
483static inline int pwm_capture(struct pwm_device *pwm,
484 struct pwm_capture *result,
485 unsigned long timeout)
486{
487 return -EINVAL;
488}
489
379static inline int pwm_set_polarity(struct pwm_device *pwm,490static inline int pwm_set_polarity(struct pwm_device *pwm,
380 enum pwm_polarity polarity)491 enum pwm_polarity polarity)
381{492{

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