aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pwm
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/pwm')
-rw-r--r--drivers/pwm/Kconfig23
-rw-r--r--drivers/pwm/Makefile3
-rw-r--r--drivers/pwm/core.c29
-rw-r--r--drivers/pwm/pwm-atmel-tcb.c5
-rw-r--r--drivers/pwm/pwm-bfin.c1
-rw-r--r--drivers/pwm/pwm-imx.c1
-rw-r--r--drivers/pwm/pwm-lpc32xx.c1
-rw-r--r--drivers/pwm/pwm-mxs.c7
-rw-r--r--drivers/pwm/pwm-pca9685.c300
-rw-r--r--drivers/pwm/pwm-puv3.c1
-rw-r--r--drivers/pwm/pwm-renesas-tpu.c474
-rw-r--r--drivers/pwm/pwm-spear.c1
-rw-r--r--drivers/pwm/pwm-tegra.c1
-rw-r--r--drivers/pwm/pwm-tiehrpwm.c13
-rw-r--r--drivers/pwm/sysfs.c352
15 files changed, 1198 insertions, 14 deletions
diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 115b64453493..75840b5cea6d 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -28,6 +28,10 @@ menuconfig PWM
28 28
29if PWM 29if PWM
30 30
31config PWM_SYSFS
32 bool
33 default y if SYSFS
34
31config PWM_AB8500 35config PWM_AB8500
32 tristate "AB8500 PWM support" 36 tristate "AB8500 PWM support"
33 depends on AB8500_CORE && ARCH_U8500 37 depends on AB8500_CORE && ARCH_U8500
@@ -97,6 +101,15 @@ config PWM_MXS
97 To compile this driver as a module, choose M here: the module 101 To compile this driver as a module, choose M here: the module
98 will be called pwm-mxs. 102 will be called pwm-mxs.
99 103
104config PWM_PCA9685
105 tristate "NXP PCA9685 PWM driver"
106 depends on OF && REGMAP_I2C
107 help
108 Generic PWM framework driver for NXP PCA9685 LED controller.
109
110 To compile this driver as a module, choose M here: the module
111 will be called pwm-pca9685.
112
100config PWM_PUV3 113config PWM_PUV3
101 tristate "PKUnity NetBook-0916 PWM support" 114 tristate "PKUnity NetBook-0916 PWM support"
102 depends on ARCH_PUV3 115 depends on ARCH_PUV3
@@ -115,6 +128,16 @@ config PWM_PXA
115 To compile this driver as a module, choose M here: the module 128 To compile this driver as a module, choose M here: the module
116 will be called pwm-pxa. 129 will be called pwm-pxa.
117 130
131config PWM_RENESAS_TPU
132 tristate "Renesas TPU PWM support"
133 depends on ARCH_SHMOBILE
134 help
135 This driver exposes the Timer Pulse Unit (TPU) PWM controller found
136 in Renesas chips through the PWM API.
137
138 To compile this driver as a module, choose M here: the module
139 will be called pwm-renesas-tpu.
140
118config PWM_SAMSUNG 141config PWM_SAMSUNG
119 tristate "Samsung PWM support" 142 tristate "Samsung PWM support"
120 depends on PLAT_SAMSUNG 143 depends on PLAT_SAMSUNG
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index 94ba21e24bd6..77a8c185c5b2 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -1,4 +1,5 @@
1obj-$(CONFIG_PWM) += core.o 1obj-$(CONFIG_PWM) += core.o
2obj-$(CONFIG_PWM_SYSFS) += sysfs.o
2obj-$(CONFIG_PWM_AB8500) += pwm-ab8500.o 3obj-$(CONFIG_PWM_AB8500) += pwm-ab8500.o
3obj-$(CONFIG_PWM_ATMEL_TCB) += pwm-atmel-tcb.o 4obj-$(CONFIG_PWM_ATMEL_TCB) += pwm-atmel-tcb.o
4obj-$(CONFIG_PWM_BFIN) += pwm-bfin.o 5obj-$(CONFIG_PWM_BFIN) += pwm-bfin.o
@@ -6,8 +7,10 @@ obj-$(CONFIG_PWM_IMX) += pwm-imx.o
6obj-$(CONFIG_PWM_JZ4740) += pwm-jz4740.o 7obj-$(CONFIG_PWM_JZ4740) += pwm-jz4740.o
7obj-$(CONFIG_PWM_LPC32XX) += pwm-lpc32xx.o 8obj-$(CONFIG_PWM_LPC32XX) += pwm-lpc32xx.o
8obj-$(CONFIG_PWM_MXS) += pwm-mxs.o 9obj-$(CONFIG_PWM_MXS) += pwm-mxs.o
10obj-$(CONFIG_PWM_PCA9685) += pwm-pca9685.o
9obj-$(CONFIG_PWM_PUV3) += pwm-puv3.o 11obj-$(CONFIG_PWM_PUV3) += pwm-puv3.o
10obj-$(CONFIG_PWM_PXA) += pwm-pxa.o 12obj-$(CONFIG_PWM_PXA) += pwm-pxa.o
13obj-$(CONFIG_PWM_RENESAS_TPU) += pwm-renesas-tpu.o
11obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o 14obj-$(CONFIG_PWM_SAMSUNG) += pwm-samsung.o
12obj-$(CONFIG_PWM_SPEAR) += pwm-spear.o 15obj-$(CONFIG_PWM_SPEAR) += pwm-spear.o
13obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o 16obj-$(CONFIG_PWM_TEGRA) += pwm-tegra.o
diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index 32221cb0cbe7..dfbfbc521768 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -274,6 +274,8 @@ int pwmchip_add(struct pwm_chip *chip)
274 if (IS_ENABLED(CONFIG_OF)) 274 if (IS_ENABLED(CONFIG_OF))
275 of_pwmchip_add(chip); 275 of_pwmchip_add(chip);
276 276
277 pwmchip_sysfs_export(chip);
278
277out: 279out:
278 mutex_unlock(&pwm_lock); 280 mutex_unlock(&pwm_lock);
279 return ret; 281 return ret;
@@ -310,6 +312,8 @@ int pwmchip_remove(struct pwm_chip *chip)
310 312
311 free_pwms(chip); 313 free_pwms(chip);
312 314
315 pwmchip_sysfs_unexport(chip);
316
313out: 317out:
314 mutex_unlock(&pwm_lock); 318 mutex_unlock(&pwm_lock);
315 return ret; 319 return ret;
@@ -402,10 +406,19 @@ EXPORT_SYMBOL_GPL(pwm_free);
402 */ 406 */
403int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns) 407int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
404{ 408{
409 int err;
410
405 if (!pwm || duty_ns < 0 || period_ns <= 0 || duty_ns > period_ns) 411 if (!pwm || duty_ns < 0 || period_ns <= 0 || duty_ns > period_ns)
406 return -EINVAL; 412 return -EINVAL;
407 413
408 return pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns); 414 err = pwm->chip->ops->config(pwm->chip, pwm, duty_ns, period_ns);
415 if (err)
416 return err;
417
418 pwm->duty_cycle = duty_ns;
419 pwm->period = period_ns;
420
421 return 0;
409} 422}
410EXPORT_SYMBOL_GPL(pwm_config); 423EXPORT_SYMBOL_GPL(pwm_config);
411 424
@@ -418,6 +431,8 @@ EXPORT_SYMBOL_GPL(pwm_config);
418 */ 431 */
419int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity) 432int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity)
420{ 433{
434 int err;
435
421 if (!pwm || !pwm->chip->ops) 436 if (!pwm || !pwm->chip->ops)
422 return -EINVAL; 437 return -EINVAL;
423 438
@@ -427,7 +442,13 @@ int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity)
427 if (test_bit(PWMF_ENABLED, &pwm->flags)) 442 if (test_bit(PWMF_ENABLED, &pwm->flags))
428 return -EBUSY; 443 return -EBUSY;
429 444
430 return pwm->chip->ops->set_polarity(pwm->chip, pwm, polarity); 445 err = pwm->chip->ops->set_polarity(pwm->chip, pwm, polarity);
446 if (err)
447 return err;
448
449 pwm->polarity = polarity;
450
451 return 0;
431} 452}
432EXPORT_SYMBOL_GPL(pwm_set_polarity); 453EXPORT_SYMBOL_GPL(pwm_set_polarity);
433 454
@@ -694,7 +715,7 @@ struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)
694{ 715{
695 struct pwm_device **ptr, *pwm; 716 struct pwm_device **ptr, *pwm;
696 717
697 ptr = devres_alloc(devm_pwm_release, sizeof(**ptr), GFP_KERNEL); 718 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
698 if (!ptr) 719 if (!ptr)
699 return ERR_PTR(-ENOMEM); 720 return ERR_PTR(-ENOMEM);
700 721
@@ -724,7 +745,7 @@ struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
724{ 745{
725 struct pwm_device **ptr, *pwm; 746 struct pwm_device **ptr, *pwm;
726 747
727 ptr = devres_alloc(devm_pwm_release, sizeof(**ptr), GFP_KERNEL); 748 ptr = devres_alloc(devm_pwm_release, sizeof(*ptr), GFP_KERNEL);
728 if (!ptr) 749 if (!ptr)
729 return ERR_PTR(-ENOMEM); 750 return ERR_PTR(-ENOMEM);
730 751
diff --git a/drivers/pwm/pwm-atmel-tcb.c b/drivers/pwm/pwm-atmel-tcb.c
index 0a7b6582edb1..ba6ce01035e4 100644
--- a/drivers/pwm/pwm-atmel-tcb.c
+++ b/drivers/pwm/pwm-atmel-tcb.c
@@ -76,7 +76,7 @@ static int atmel_tcb_pwm_request(struct pwm_chip *chip,
76 if (!tcbpwm) 76 if (!tcbpwm)
77 return -ENOMEM; 77 return -ENOMEM;
78 78
79 ret = clk_enable(tc->clk[group]); 79 ret = clk_prepare_enable(tc->clk[group]);
80 if (ret) { 80 if (ret) {
81 devm_kfree(chip->dev, tcbpwm); 81 devm_kfree(chip->dev, tcbpwm);
82 return ret; 82 return ret;
@@ -124,7 +124,7 @@ static void atmel_tcb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
124 struct atmel_tcb_pwm_device *tcbpwm = pwm_get_chip_data(pwm); 124 struct atmel_tcb_pwm_device *tcbpwm = pwm_get_chip_data(pwm);
125 struct atmel_tc *tc = tcbpwmc->tc; 125 struct atmel_tc *tc = tcbpwmc->tc;
126 126
127 clk_disable(tc->clk[pwm->hwpwm / 2]); 127 clk_disable_unprepare(tc->clk[pwm->hwpwm / 2]);
128 tcbpwmc->pwms[pwm->hwpwm] = NULL; 128 tcbpwmc->pwms[pwm->hwpwm] = NULL;
129 devm_kfree(chip->dev, tcbpwm); 129 devm_kfree(chip->dev, tcbpwm);
130} 130}
@@ -434,6 +434,7 @@ MODULE_DEVICE_TABLE(of, atmel_tcb_pwm_dt_ids);
434static struct platform_driver atmel_tcb_pwm_driver = { 434static struct platform_driver atmel_tcb_pwm_driver = {
435 .driver = { 435 .driver = {
436 .name = "atmel-tcb-pwm", 436 .name = "atmel-tcb-pwm",
437 .owner = THIS_MODULE,
437 .of_match_table = atmel_tcb_pwm_dt_ids, 438 .of_match_table = atmel_tcb_pwm_dt_ids,
438 }, 439 },
439 .probe = atmel_tcb_pwm_probe, 440 .probe = atmel_tcb_pwm_probe,
diff --git a/drivers/pwm/pwm-bfin.c b/drivers/pwm/pwm-bfin.c
index 7631ef194de7..9985d830e554 100644
--- a/drivers/pwm/pwm-bfin.c
+++ b/drivers/pwm/pwm-bfin.c
@@ -149,6 +149,7 @@ static int bfin_pwm_remove(struct platform_device *pdev)
149static struct platform_driver bfin_pwm_driver = { 149static struct platform_driver bfin_pwm_driver = {
150 .driver = { 150 .driver = {
151 .name = "bfin-pwm", 151 .name = "bfin-pwm",
152 .owner = THIS_MODULE,
152 }, 153 },
153 .probe = bfin_pwm_probe, 154 .probe = bfin_pwm_probe,
154 .remove = bfin_pwm_remove, 155 .remove = bfin_pwm_remove,
diff --git a/drivers/pwm/pwm-imx.c b/drivers/pwm/pwm-imx.c
index c938bae18812..2b7c4f88b461 100644
--- a/drivers/pwm/pwm-imx.c
+++ b/drivers/pwm/pwm-imx.c
@@ -295,6 +295,7 @@ static int imx_pwm_remove(struct platform_device *pdev)
295static struct platform_driver imx_pwm_driver = { 295static struct platform_driver imx_pwm_driver = {
296 .driver = { 296 .driver = {
297 .name = "imx-pwm", 297 .name = "imx-pwm",
298 .owner = THIS_MODULE,
298 .of_match_table = of_match_ptr(imx_pwm_dt_ids), 299 .of_match_table = of_match_ptr(imx_pwm_dt_ids),
299 }, 300 },
300 .probe = imx_pwm_probe, 301 .probe = imx_pwm_probe,
diff --git a/drivers/pwm/pwm-lpc32xx.c b/drivers/pwm/pwm-lpc32xx.c
index 8272883c0d05..efb6c7bf8750 100644
--- a/drivers/pwm/pwm-lpc32xx.c
+++ b/drivers/pwm/pwm-lpc32xx.c
@@ -171,6 +171,7 @@ MODULE_DEVICE_TABLE(of, lpc32xx_pwm_dt_ids);
171static struct platform_driver lpc32xx_pwm_driver = { 171static struct platform_driver lpc32xx_pwm_driver = {
172 .driver = { 172 .driver = {
173 .name = "lpc32xx-pwm", 173 .name = "lpc32xx-pwm",
174 .owner = THIS_MODULE,
174 .of_match_table = of_match_ptr(lpc32xx_pwm_dt_ids), 175 .of_match_table = of_match_ptr(lpc32xx_pwm_dt_ids),
175 }, 176 },
176 .probe = lpc32xx_pwm_probe, 177 .probe = lpc32xx_pwm_probe,
diff --git a/drivers/pwm/pwm-mxs.c b/drivers/pwm/pwm-mxs.c
index 3febdddf71f9..2c77b81da7c4 100644
--- a/drivers/pwm/pwm-mxs.c
+++ b/drivers/pwm/pwm-mxs.c
@@ -16,7 +16,6 @@
16#include <linux/module.h> 16#include <linux/module.h>
17#include <linux/of.h> 17#include <linux/of.h>
18#include <linux/of_address.h> 18#include <linux/of_address.h>
19#include <linux/pinctrl/consumer.h>
20#include <linux/platform_device.h> 19#include <linux/platform_device.h>
21#include <linux/pwm.h> 20#include <linux/pwm.h>
22#include <linux/slab.h> 21#include <linux/slab.h>
@@ -130,7 +129,6 @@ static int mxs_pwm_probe(struct platform_device *pdev)
130 struct device_node *np = pdev->dev.of_node; 129 struct device_node *np = pdev->dev.of_node;
131 struct mxs_pwm_chip *mxs; 130 struct mxs_pwm_chip *mxs;
132 struct resource *res; 131 struct resource *res;
133 struct pinctrl *pinctrl;
134 int ret; 132 int ret;
135 133
136 mxs = devm_kzalloc(&pdev->dev, sizeof(*mxs), GFP_KERNEL); 134 mxs = devm_kzalloc(&pdev->dev, sizeof(*mxs), GFP_KERNEL);
@@ -142,10 +140,6 @@ static int mxs_pwm_probe(struct platform_device *pdev)
142 if (IS_ERR(mxs->base)) 140 if (IS_ERR(mxs->base))
143 return PTR_ERR(mxs->base); 141 return PTR_ERR(mxs->base);
144 142
145 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
146 if (IS_ERR(pinctrl))
147 return PTR_ERR(pinctrl);
148
149 mxs->clk = devm_clk_get(&pdev->dev, NULL); 143 mxs->clk = devm_clk_get(&pdev->dev, NULL);
150 if (IS_ERR(mxs->clk)) 144 if (IS_ERR(mxs->clk))
151 return PTR_ERR(mxs->clk); 145 return PTR_ERR(mxs->clk);
@@ -188,6 +182,7 @@ MODULE_DEVICE_TABLE(of, mxs_pwm_dt_ids);
188static struct platform_driver mxs_pwm_driver = { 182static struct platform_driver mxs_pwm_driver = {
189 .driver = { 183 .driver = {
190 .name = "mxs-pwm", 184 .name = "mxs-pwm",
185 .owner = THIS_MODULE,
191 .of_match_table = of_match_ptr(mxs_pwm_dt_ids), 186 .of_match_table = of_match_ptr(mxs_pwm_dt_ids),
192 }, 187 },
193 .probe = mxs_pwm_probe, 188 .probe = mxs_pwm_probe,
diff --git a/drivers/pwm/pwm-pca9685.c b/drivers/pwm/pwm-pca9685.c
new file mode 100644
index 000000000000..3fb775ded0df
--- /dev/null
+++ b/drivers/pwm/pwm-pca9685.c
@@ -0,0 +1,300 @@
1/*
2 * Driver for PCA9685 16-channel 12-bit PWM LED controller
3 *
4 * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
5 *
6 * based on the pwm-twl-led.c driver
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <linux/i2c.h>
22#include <linux/module.h>
23#include <linux/platform_device.h>
24#include <linux/pwm.h>
25#include <linux/regmap.h>
26#include <linux/slab.h>
27
28#define PCA9685_MODE1 0x00
29#define PCA9685_MODE2 0x01
30#define PCA9685_SUBADDR1 0x02
31#define PCA9685_SUBADDR2 0x03
32#define PCA9685_SUBADDR3 0x04
33#define PCA9685_ALLCALLADDR 0x05
34#define PCA9685_LEDX_ON_L 0x06
35#define PCA9685_LEDX_ON_H 0x07
36#define PCA9685_LEDX_OFF_L 0x08
37#define PCA9685_LEDX_OFF_H 0x09
38
39#define PCA9685_ALL_LED_ON_L 0xFA
40#define PCA9685_ALL_LED_ON_H 0xFB
41#define PCA9685_ALL_LED_OFF_L 0xFC
42#define PCA9685_ALL_LED_OFF_H 0xFD
43#define PCA9685_PRESCALE 0xFE
44
45#define PCA9685_NUMREGS 0xFF
46#define PCA9685_MAXCHAN 0x10
47
48#define LED_FULL (1 << 4)
49#define MODE1_SLEEP (1 << 4)
50#define MODE2_INVRT (1 << 4)
51#define MODE2_OUTDRV (1 << 2)
52
53#define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
54#define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
55#define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
56#define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
57
58struct pca9685 {
59 struct pwm_chip chip;
60 struct regmap *regmap;
61 int active_cnt;
62};
63
64static inline struct pca9685 *to_pca(struct pwm_chip *chip)
65{
66 return container_of(chip, struct pca9685, chip);
67}
68
69static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
70 int duty_ns, int period_ns)
71{
72 struct pca9685 *pca = to_pca(chip);
73 unsigned long long duty;
74 unsigned int reg;
75
76 if (duty_ns < 1) {
77 if (pwm->hwpwm >= PCA9685_MAXCHAN)
78 reg = PCA9685_ALL_LED_OFF_H;
79 else
80 reg = LED_N_OFF_H(pwm->hwpwm);
81
82 regmap_write(pca->regmap, reg, LED_FULL);
83
84 return 0;
85 }
86
87 if (duty_ns == period_ns) {
88 if (pwm->hwpwm >= PCA9685_MAXCHAN)
89 reg = PCA9685_ALL_LED_ON_H;
90 else
91 reg = LED_N_ON_H(pwm->hwpwm);
92
93 regmap_write(pca->regmap, reg, LED_FULL);
94
95 return 0;
96 }
97
98 duty = 4096 * (unsigned long long)duty_ns;
99 duty = DIV_ROUND_UP_ULL(duty, period_ns);
100
101 if (pwm->hwpwm >= PCA9685_MAXCHAN)
102 reg = PCA9685_ALL_LED_OFF_L;
103 else
104 reg = LED_N_OFF_L(pwm->hwpwm);
105
106 regmap_write(pca->regmap, reg, (int)duty & 0xff);
107
108 if (pwm->hwpwm >= PCA9685_MAXCHAN)
109 reg = PCA9685_ALL_LED_OFF_H;
110 else
111 reg = LED_N_OFF_H(pwm->hwpwm);
112
113 regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
114
115 return 0;
116}
117
118static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
119{
120 struct pca9685 *pca = to_pca(chip);
121 unsigned int reg;
122
123 /*
124 * The PWM subsystem does not support a pre-delay.
125 * So, set the ON-timeout to 0
126 */
127 if (pwm->hwpwm >= PCA9685_MAXCHAN)
128 reg = PCA9685_ALL_LED_ON_L;
129 else
130 reg = LED_N_ON_L(pwm->hwpwm);
131
132 regmap_write(pca->regmap, reg, 0);
133
134 if (pwm->hwpwm >= PCA9685_MAXCHAN)
135 reg = PCA9685_ALL_LED_ON_H;
136 else
137 reg = LED_N_ON_H(pwm->hwpwm);
138
139 regmap_write(pca->regmap, reg, 0);
140
141 /*
142 * Clear the full-off bit.
143 * It has precedence over the others and must be off.
144 */
145 if (pwm->hwpwm >= PCA9685_MAXCHAN)
146 reg = PCA9685_ALL_LED_OFF_H;
147 else
148 reg = LED_N_OFF_H(pwm->hwpwm);
149
150 regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
151
152 return 0;
153}
154
155static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
156{
157 struct pca9685 *pca = to_pca(chip);
158 unsigned int reg;
159
160 if (pwm->hwpwm >= PCA9685_MAXCHAN)
161 reg = PCA9685_ALL_LED_OFF_H;
162 else
163 reg = LED_N_OFF_H(pwm->hwpwm);
164
165 regmap_write(pca->regmap, reg, LED_FULL);
166
167 /* Clear the LED_OFF counter. */
168 if (pwm->hwpwm >= PCA9685_MAXCHAN)
169 reg = PCA9685_ALL_LED_OFF_L;
170 else
171 reg = LED_N_OFF_L(pwm->hwpwm);
172
173 regmap_write(pca->regmap, reg, 0x0);
174}
175
176static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
177{
178 struct pca9685 *pca = to_pca(chip);
179
180 if (pca->active_cnt++ == 0)
181 return regmap_update_bits(pca->regmap, PCA9685_MODE1,
182 MODE1_SLEEP, 0x0);
183
184 return 0;
185}
186
187static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
188{
189 struct pca9685 *pca = to_pca(chip);
190
191 if (--pca->active_cnt == 0)
192 regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
193 MODE1_SLEEP);
194}
195
196static const struct pwm_ops pca9685_pwm_ops = {
197 .enable = pca9685_pwm_enable,
198 .disable = pca9685_pwm_disable,
199 .config = pca9685_pwm_config,
200 .request = pca9685_pwm_request,
201 .free = pca9685_pwm_free,
202 .owner = THIS_MODULE,
203};
204
205static struct regmap_config pca9685_regmap_i2c_config = {
206 .reg_bits = 8,
207 .val_bits = 8,
208 .max_register = PCA9685_NUMREGS,
209 .cache_type = REGCACHE_NONE,
210};
211
212static int pca9685_pwm_probe(struct i2c_client *client,
213 const struct i2c_device_id *id)
214{
215 struct device_node *np = client->dev.of_node;
216 struct pca9685 *pca;
217 int ret;
218 int mode2;
219
220 pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
221 if (!pca)
222 return -ENOMEM;
223
224 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
225 if (IS_ERR(pca->regmap)) {
226 ret = PTR_ERR(pca->regmap);
227 dev_err(&client->dev, "Failed to initialize register map: %d\n",
228 ret);
229 return ret;
230 }
231
232 i2c_set_clientdata(client, pca);
233
234 regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
235
236 if (of_property_read_bool(np, "invert"))
237 mode2 |= MODE2_INVRT;
238 else
239 mode2 &= ~MODE2_INVRT;
240
241 if (of_property_read_bool(np, "open-drain"))
242 mode2 &= ~MODE2_OUTDRV;
243 else
244 mode2 |= MODE2_OUTDRV;
245
246 regmap_write(pca->regmap, PCA9685_MODE2, mode2);
247
248 /* clear all "full off" bits */
249 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
250 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
251
252 pca->chip.ops = &pca9685_pwm_ops;
253 /* add an extra channel for ALL_LED */
254 pca->chip.npwm = PCA9685_MAXCHAN + 1;
255
256 pca->chip.dev = &client->dev;
257 pca->chip.base = -1;
258 pca->chip.can_sleep = true;
259
260 return pwmchip_add(&pca->chip);
261}
262
263static int pca9685_pwm_remove(struct i2c_client *client)
264{
265 struct pca9685 *pca = i2c_get_clientdata(client);
266
267 regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
268 MODE1_SLEEP);
269
270 return pwmchip_remove(&pca->chip);
271}
272
273static const struct i2c_device_id pca9685_id[] = {
274 { "pca9685", 0 },
275 { /* sentinel */ },
276};
277MODULE_DEVICE_TABLE(i2c, pca9685_id);
278
279static const struct of_device_id pca9685_dt_ids[] = {
280 { .compatible = "nxp,pca9685-pwm", },
281 { /* sentinel */ }
282};
283MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
284
285static struct i2c_driver pca9685_i2c_driver = {
286 .driver = {
287 .name = "pca9685-pwm",
288 .owner = THIS_MODULE,
289 .of_match_table = pca9685_dt_ids,
290 },
291 .probe = pca9685_pwm_probe,
292 .remove = pca9685_pwm_remove,
293 .id_table = pca9685_id,
294};
295
296module_i2c_driver(pca9685_i2c_driver);
297
298MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
299MODULE_DESCRIPTION("PWM driver for PCA9685");
300MODULE_LICENSE("GPL");
diff --git a/drivers/pwm/pwm-puv3.c b/drivers/pwm/pwm-puv3.c
index ed6007b27585..a9a28083f245 100644
--- a/drivers/pwm/pwm-puv3.c
+++ b/drivers/pwm/pwm-puv3.c
@@ -146,6 +146,7 @@ static int pwm_remove(struct platform_device *pdev)
146static struct platform_driver puv3_pwm_driver = { 146static struct platform_driver puv3_pwm_driver = {
147 .driver = { 147 .driver = {
148 .name = "PKUnity-v3-PWM", 148 .name = "PKUnity-v3-PWM",
149 .owner = THIS_MODULE,
149 }, 150 },
150 .probe = pwm_probe, 151 .probe = pwm_probe,
151 .remove = pwm_remove, 152 .remove = pwm_remove,
diff --git a/drivers/pwm/pwm-renesas-tpu.c b/drivers/pwm/pwm-renesas-tpu.c
new file mode 100644
index 000000000000..2600892782c1
--- /dev/null
+++ b/drivers/pwm/pwm-renesas-tpu.c
@@ -0,0 +1,474 @@
1/*
2 * R-Mobile TPU PWM driver
3 *
4 * Copyright (C) 2012 Renesas Solutions Corp.
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
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/clk.h>
17#include <linux/err.h>
18#include <linux/io.h>
19#include <linux/init.h>
20#include <linux/ioport.h>
21#include <linux/module.h>
22#include <linux/mutex.h>
23#include <linux/platform_data/pwm-renesas-tpu.h>
24#include <linux/platform_device.h>
25#include <linux/pm_runtime.h>
26#include <linux/pwm.h>
27#include <linux/slab.h>
28#include <linux/spinlock.h>
29
30#define TPU_TSTR 0x00 /* Timer start register (shared) */
31
32#define TPU_TCRn 0x00 /* Timer control register */
33#define TPU_TCR_CCLR_NONE (0 << 5)
34#define TPU_TCR_CCLR_TGRA (1 << 5)
35#define TPU_TCR_CCLR_TGRB (2 << 5)
36#define TPU_TCR_CCLR_TGRC (5 << 5)
37#define TPU_TCR_CCLR_TGRD (6 << 5)
38#define TPU_TCR_CKEG_RISING (0 << 3)
39#define TPU_TCR_CKEG_FALLING (1 << 3)
40#define TPU_TCR_CKEG_BOTH (2 << 3)
41#define TPU_TMDRn 0x04 /* Timer mode register */
42#define TPU_TMDR_BFWT (1 << 6)
43#define TPU_TMDR_BFB (1 << 5)
44#define TPU_TMDR_BFA (1 << 4)
45#define TPU_TMDR_MD_NORMAL (0 << 0)
46#define TPU_TMDR_MD_PWM (2 << 0)
47#define TPU_TIORn 0x08 /* Timer I/O control register */
48#define TPU_TIOR_IOA_0 (0 << 0)
49#define TPU_TIOR_IOA_0_CLR (1 << 0)
50#define TPU_TIOR_IOA_0_SET (2 << 0)
51#define TPU_TIOR_IOA_0_TOGGLE (3 << 0)
52#define TPU_TIOR_IOA_1 (4 << 0)
53#define TPU_TIOR_IOA_1_CLR (5 << 0)
54#define TPU_TIOR_IOA_1_SET (6 << 0)
55#define TPU_TIOR_IOA_1_TOGGLE (7 << 0)
56#define TPU_TIERn 0x0c /* Timer interrupt enable register */
57#define TPU_TSRn 0x10 /* Timer status register */
58#define TPU_TCNTn 0x14 /* Timer counter */
59#define TPU_TGRAn 0x18 /* Timer general register A */
60#define TPU_TGRBn 0x1c /* Timer general register B */
61#define TPU_TGRCn 0x20 /* Timer general register C */
62#define TPU_TGRDn 0x24 /* Timer general register D */
63
64#define TPU_CHANNEL_OFFSET 0x10
65#define TPU_CHANNEL_SIZE 0x40
66
67enum tpu_pin_state {
68 TPU_PIN_INACTIVE, /* Pin is driven inactive */
69 TPU_PIN_PWM, /* Pin is driven by PWM */
70 TPU_PIN_ACTIVE, /* Pin is driven active */
71};
72
73struct tpu_device;
74
75struct tpu_pwm_device {
76 bool timer_on; /* Whether the timer is running */
77
78 struct tpu_device *tpu;
79 unsigned int channel; /* Channel number in the TPU */
80
81 enum pwm_polarity polarity;
82 unsigned int prescaler;
83 u16 period;
84 u16 duty;
85};
86
87struct tpu_device {
88 struct platform_device *pdev;
89 struct tpu_pwm_platform_data *pdata;
90 struct pwm_chip chip;
91 spinlock_t lock;
92
93 void __iomem *base;
94 struct clk *clk;
95};
96
97#define to_tpu_device(c) container_of(c, struct tpu_device, chip)
98
99static void tpu_pwm_write(struct tpu_pwm_device *pwm, int reg_nr, u16 value)
100{
101 void __iomem *base = pwm->tpu->base + TPU_CHANNEL_OFFSET
102 + pwm->channel * TPU_CHANNEL_SIZE;
103
104 iowrite16(value, base + reg_nr);
105}
106
107static void tpu_pwm_set_pin(struct tpu_pwm_device *pwm,
108 enum tpu_pin_state state)
109{
110 static const char * const states[] = { "inactive", "PWM", "active" };
111
112 dev_dbg(&pwm->tpu->pdev->dev, "%u: configuring pin as %s\n",
113 pwm->channel, states[state]);
114
115 switch (state) {
116 case TPU_PIN_INACTIVE:
117 tpu_pwm_write(pwm, TPU_TIORn,
118 pwm->polarity == PWM_POLARITY_INVERSED ?
119 TPU_TIOR_IOA_1 : TPU_TIOR_IOA_0);
120 break;
121 case TPU_PIN_PWM:
122 tpu_pwm_write(pwm, TPU_TIORn,
123 pwm->polarity == PWM_POLARITY_INVERSED ?
124 TPU_TIOR_IOA_0_SET : TPU_TIOR_IOA_1_CLR);
125 break;
126 case TPU_PIN_ACTIVE:
127 tpu_pwm_write(pwm, TPU_TIORn,
128 pwm->polarity == PWM_POLARITY_INVERSED ?
129 TPU_TIOR_IOA_0 : TPU_TIOR_IOA_1);
130 break;
131 }
132}
133
134static void tpu_pwm_start_stop(struct tpu_pwm_device *pwm, int start)
135{
136 unsigned long flags;
137 u16 value;
138
139 spin_lock_irqsave(&pwm->tpu->lock, flags);
140 value = ioread16(pwm->tpu->base + TPU_TSTR);
141
142 if (start)
143 value |= 1 << pwm->channel;
144 else
145 value &= ~(1 << pwm->channel);
146
147 iowrite16(value, pwm->tpu->base + TPU_TSTR);
148 spin_unlock_irqrestore(&pwm->tpu->lock, flags);
149}
150
151static int tpu_pwm_timer_start(struct tpu_pwm_device *pwm)
152{
153 int ret;
154
155 if (!pwm->timer_on) {
156 /* Wake up device and enable clock. */
157 pm_runtime_get_sync(&pwm->tpu->pdev->dev);
158 ret = clk_prepare_enable(pwm->tpu->clk);
159 if (ret) {
160 dev_err(&pwm->tpu->pdev->dev, "cannot enable clock\n");
161 return ret;
162 }
163 pwm->timer_on = true;
164 }
165
166 /*
167 * Make sure the channel is stopped, as we need to reconfigure it
168 * completely. First drive the pin to the inactive state to avoid
169 * glitches.
170 */
171 tpu_pwm_set_pin(pwm, TPU_PIN_INACTIVE);
172 tpu_pwm_start_stop(pwm, false);
173
174 /*
175 * - Clear TCNT on TGRB match
176 * - Count on rising edge
177 * - Set prescaler
178 * - Output 0 until TGRA, output 1 until TGRB (active low polarity)
179 * - Output 1 until TGRA, output 0 until TGRB (active high polarity
180 * - PWM mode
181 */
182 tpu_pwm_write(pwm, TPU_TCRn, TPU_TCR_CCLR_TGRB | TPU_TCR_CKEG_RISING |
183 pwm->prescaler);
184 tpu_pwm_write(pwm, TPU_TMDRn, TPU_TMDR_MD_PWM);
185 tpu_pwm_set_pin(pwm, TPU_PIN_PWM);
186 tpu_pwm_write(pwm, TPU_TGRAn, pwm->duty);
187 tpu_pwm_write(pwm, TPU_TGRBn, pwm->period);
188
189 dev_dbg(&pwm->tpu->pdev->dev, "%u: TGRA 0x%04x TGRB 0x%04x\n",
190 pwm->channel, pwm->duty, pwm->period);
191
192 /* Start the channel. */
193 tpu_pwm_start_stop(pwm, true);
194
195 return 0;
196}
197
198static void tpu_pwm_timer_stop(struct tpu_pwm_device *pwm)
199{
200 if (!pwm->timer_on)
201 return;
202
203 /* Disable channel. */
204 tpu_pwm_start_stop(pwm, false);
205
206 /* Stop clock and mark device as idle. */
207 clk_disable_unprepare(pwm->tpu->clk);
208 pm_runtime_put(&pwm->tpu->pdev->dev);
209
210 pwm->timer_on = false;
211}
212
213/* -----------------------------------------------------------------------------
214 * PWM API
215 */
216
217static int tpu_pwm_request(struct pwm_chip *chip, struct pwm_device *_pwm)
218{
219 struct tpu_device *tpu = to_tpu_device(chip);
220 struct tpu_pwm_device *pwm;
221
222 if (_pwm->hwpwm >= TPU_CHANNEL_MAX)
223 return -EINVAL;
224
225 pwm = kzalloc(sizeof(*pwm), GFP_KERNEL);
226 if (pwm == NULL)
227 return -ENOMEM;
228
229 pwm->tpu = tpu;
230 pwm->channel = _pwm->hwpwm;
231 pwm->polarity = tpu->pdata ? tpu->pdata->channels[pwm->channel].polarity
232 : PWM_POLARITY_NORMAL;
233 pwm->prescaler = 0;
234 pwm->period = 0;
235 pwm->duty = 0;
236
237 pwm->timer_on = false;
238
239 pwm_set_chip_data(_pwm, pwm);
240
241 return 0;
242}
243
244static void tpu_pwm_free(struct pwm_chip *chip, struct pwm_device *_pwm)
245{
246 struct tpu_pwm_device *pwm = pwm_get_chip_data(_pwm);
247
248 tpu_pwm_timer_stop(pwm);
249 kfree(pwm);
250}
251
252static int tpu_pwm_config(struct pwm_chip *chip, struct pwm_device *_pwm,
253 int duty_ns, int period_ns)
254{
255 static const unsigned int prescalers[] = { 1, 4, 16, 64 };
256 struct tpu_pwm_device *pwm = pwm_get_chip_data(_pwm);
257 struct tpu_device *tpu = to_tpu_device(chip);
258 unsigned int prescaler;
259 bool duty_only = false;
260 u32 clk_rate;
261 u32 period;
262 u32 duty;
263 int ret;
264
265 /*
266 * Pick a prescaler to avoid overflowing the counter.
267 * TODO: Pick the highest acceptable prescaler.
268 */
269 clk_rate = clk_get_rate(tpu->clk);
270
271 for (prescaler = 0; prescaler < ARRAY_SIZE(prescalers); ++prescaler) {
272 period = clk_rate / prescalers[prescaler]
273 / (NSEC_PER_SEC / period_ns);
274 if (period <= 0xffff)
275 break;
276 }
277
278 if (prescaler == ARRAY_SIZE(prescalers) || period == 0) {
279 dev_err(&tpu->pdev->dev, "clock rate mismatch\n");
280 return -ENOTSUPP;
281 }
282
283 if (duty_ns) {
284 duty = clk_rate / prescalers[prescaler]
285 / (NSEC_PER_SEC / duty_ns);
286 if (duty > period)
287 return -EINVAL;
288 } else {
289 duty = 0;
290 }
291
292 dev_dbg(&tpu->pdev->dev,
293 "rate %u, prescaler %u, period %u, duty %u\n",
294 clk_rate, prescalers[prescaler], period, duty);
295
296 if (pwm->prescaler == prescaler && pwm->period == period)
297 duty_only = true;
298
299 pwm->prescaler = prescaler;
300 pwm->period = period;
301 pwm->duty = duty;
302
303 /* If the channel is disabled we're done. */
304 if (!test_bit(PWMF_ENABLED, &_pwm->flags))
305 return 0;
306
307 if (duty_only && pwm->timer_on) {
308 /*
309 * If only the duty cycle changed and the timer is already
310 * running, there's no need to reconfigure it completely, Just
311 * modify the duty cycle.
312 */
313 tpu_pwm_write(pwm, TPU_TGRAn, pwm->duty);
314 dev_dbg(&tpu->pdev->dev, "%u: TGRA 0x%04x\n", pwm->channel,
315 pwm->duty);
316 } else {
317 /* Otherwise perform a full reconfiguration. */
318 ret = tpu_pwm_timer_start(pwm);
319 if (ret < 0)
320 return ret;
321 }
322
323 if (duty == 0 || duty == period) {
324 /*
325 * To avoid running the timer when not strictly required, handle
326 * 0% and 100% duty cycles as fixed levels and stop the timer.
327 */
328 tpu_pwm_set_pin(pwm, duty ? TPU_PIN_ACTIVE : TPU_PIN_INACTIVE);
329 tpu_pwm_timer_stop(pwm);
330 }
331
332 return 0;
333}
334
335static int tpu_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *_pwm,
336 enum pwm_polarity polarity)
337{
338 struct tpu_pwm_device *pwm = pwm_get_chip_data(_pwm);
339
340 pwm->polarity = polarity;
341
342 return 0;
343}
344
345static int tpu_pwm_enable(struct pwm_chip *chip, struct pwm_device *_pwm)
346{
347 struct tpu_pwm_device *pwm = pwm_get_chip_data(_pwm);
348 int ret;
349
350 ret = tpu_pwm_timer_start(pwm);
351 if (ret < 0)
352 return ret;
353
354 /*
355 * To avoid running the timer when not strictly required, handle 0% and
356 * 100% duty cycles as fixed levels and stop the timer.
357 */
358 if (pwm->duty == 0 || pwm->duty == pwm->period) {
359 tpu_pwm_set_pin(pwm, pwm->duty ?
360 TPU_PIN_ACTIVE : TPU_PIN_INACTIVE);
361 tpu_pwm_timer_stop(pwm);
362 }
363
364 return 0;
365}
366
367static void tpu_pwm_disable(struct pwm_chip *chip, struct pwm_device *_pwm)
368{
369 struct tpu_pwm_device *pwm = pwm_get_chip_data(_pwm);
370
371 /* The timer must be running to modify the pin output configuration. */
372 tpu_pwm_timer_start(pwm);
373 tpu_pwm_set_pin(pwm, TPU_PIN_INACTIVE);
374 tpu_pwm_timer_stop(pwm);
375}
376
377static const struct pwm_ops tpu_pwm_ops = {
378 .request = tpu_pwm_request,
379 .free = tpu_pwm_free,
380 .config = tpu_pwm_config,
381 .set_polarity = tpu_pwm_set_polarity,
382 .enable = tpu_pwm_enable,
383 .disable = tpu_pwm_disable,
384 .owner = THIS_MODULE,
385};
386
387/* -----------------------------------------------------------------------------
388 * Probe and remove
389 */
390
391static int tpu_probe(struct platform_device *pdev)
392{
393 struct tpu_device *tpu;
394 struct resource *res;
395 int ret;
396
397 tpu = devm_kzalloc(&pdev->dev, sizeof(*tpu), GFP_KERNEL);
398 if (tpu == NULL) {
399 dev_err(&pdev->dev, "failed to allocate driver data\n");
400 return -ENOMEM;
401 }
402
403 tpu->pdata = pdev->dev.platform_data;
404
405 /* Map memory, get clock and pin control. */
406 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
407 if (!res) {
408 dev_err(&pdev->dev, "failed to get I/O memory\n");
409 return -ENXIO;
410 }
411
412 tpu->base = devm_ioremap_resource(&pdev->dev, res);
413 if (IS_ERR(tpu->base))
414 return PTR_ERR(tpu->base);
415
416 tpu->clk = devm_clk_get(&pdev->dev, NULL);
417 if (IS_ERR(tpu->clk)) {
418 dev_err(&pdev->dev, "cannot get clock\n");
419 return PTR_ERR(tpu->clk);
420 }
421
422 /* Initialize and register the device. */
423 platform_set_drvdata(pdev, tpu);
424
425 spin_lock_init(&tpu->lock);
426 tpu->pdev = pdev;
427
428 tpu->chip.dev = &pdev->dev;
429 tpu->chip.ops = &tpu_pwm_ops;
430 tpu->chip.base = -1;
431 tpu->chip.npwm = TPU_CHANNEL_MAX;
432
433 ret = pwmchip_add(&tpu->chip);
434 if (ret < 0) {
435 dev_err(&pdev->dev, "failed to register PWM chip\n");
436 return ret;
437 }
438
439 dev_info(&pdev->dev, "TPU PWM %d registered\n", tpu->pdev->id);
440
441 pm_runtime_enable(&pdev->dev);
442
443 return 0;
444}
445
446static int tpu_remove(struct platform_device *pdev)
447{
448 struct tpu_device *tpu = platform_get_drvdata(pdev);
449 int ret;
450
451 ret = pwmchip_remove(&tpu->chip);
452 if (ret)
453 return ret;
454
455 pm_runtime_disable(&pdev->dev);
456
457 return 0;
458}
459
460static struct platform_driver tpu_driver = {
461 .probe = tpu_probe,
462 .remove = tpu_remove,
463 .driver = {
464 .name = "renesas-tpu-pwm",
465 .owner = THIS_MODULE,
466 }
467};
468
469module_platform_driver(tpu_driver);
470
471MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
472MODULE_DESCRIPTION("Renesas TPU PWM Driver");
473MODULE_LICENSE("GPL v2");
474MODULE_ALIAS("platform:renesas-tpu-pwm");
diff --git a/drivers/pwm/pwm-spear.c b/drivers/pwm/pwm-spear.c
index 6d99e2cbdc73..a54d21401431 100644
--- a/drivers/pwm/pwm-spear.c
+++ b/drivers/pwm/pwm-spear.c
@@ -259,6 +259,7 @@ MODULE_DEVICE_TABLE(of, spear_pwm_of_match);
259static struct platform_driver spear_pwm_driver = { 259static struct platform_driver spear_pwm_driver = {
260 .driver = { 260 .driver = {
261 .name = "spear-pwm", 261 .name = "spear-pwm",
262 .owner = THIS_MODULE,
262 .of_match_table = spear_pwm_of_match, 263 .of_match_table = spear_pwm_of_match,
263 }, 264 },
264 .probe = spear_pwm_probe, 265 .probe = spear_pwm_probe,
diff --git a/drivers/pwm/pwm-tegra.c b/drivers/pwm/pwm-tegra.c
index a5402933001f..74298c561c4e 100644
--- a/drivers/pwm/pwm-tegra.c
+++ b/drivers/pwm/pwm-tegra.c
@@ -239,6 +239,7 @@ MODULE_DEVICE_TABLE(of, tegra_pwm_of_match);
239static struct platform_driver tegra_pwm_driver = { 239static struct platform_driver tegra_pwm_driver = {
240 .driver = { 240 .driver = {
241 .name = "tegra-pwm", 241 .name = "tegra-pwm",
242 .owner = THIS_MODULE,
242 .of_match_table = tegra_pwm_of_match, 243 .of_match_table = tegra_pwm_of_match,
243 }, 244 },
244 .probe = tegra_pwm_probe, 245 .probe = tegra_pwm_probe,
diff --git a/drivers/pwm/pwm-tiehrpwm.c b/drivers/pwm/pwm-tiehrpwm.c
index 48a485c2e422..aa4c5586f53b 100644
--- a/drivers/pwm/pwm-tiehrpwm.c
+++ b/drivers/pwm/pwm-tiehrpwm.c
@@ -359,7 +359,7 @@ static int ehrpwm_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
359 configure_polarity(pc, pwm->hwpwm); 359 configure_polarity(pc, pwm->hwpwm);
360 360
361 /* Enable TBCLK before enabling PWM device */ 361 /* Enable TBCLK before enabling PWM device */
362 ret = clk_prepare_enable(pc->tbclk); 362 ret = clk_enable(pc->tbclk);
363 if (ret) { 363 if (ret) {
364 pr_err("Failed to enable TBCLK for %s\n", 364 pr_err("Failed to enable TBCLK for %s\n",
365 dev_name(pc->chip.dev)); 365 dev_name(pc->chip.dev));
@@ -395,7 +395,7 @@ static void ehrpwm_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
395 ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val); 395 ehrpwm_modify(pc->mmio_base, AQCSFRC, aqcsfrc_mask, aqcsfrc_val);
396 396
397 /* Disabling TBCLK on PWM disable */ 397 /* Disabling TBCLK on PWM disable */
398 clk_disable_unprepare(pc->tbclk); 398 clk_disable(pc->tbclk);
399 399
400 /* Stop Time base counter */ 400 /* Stop Time base counter */
401 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_RUN_MASK, TBCTL_STOP_NEXT); 401 ehrpwm_modify(pc->mmio_base, TBCTL, TBCTL_RUN_MASK, TBCTL_STOP_NEXT);
@@ -482,6 +482,12 @@ static int ehrpwm_pwm_probe(struct platform_device *pdev)
482 return PTR_ERR(pc->tbclk); 482 return PTR_ERR(pc->tbclk);
483 } 483 }
484 484
485 ret = clk_prepare(pc->tbclk);
486 if (ret < 0) {
487 dev_err(&pdev->dev, "clk_prepare() failed: %d\n", ret);
488 return ret;
489 }
490
485 ret = pwmchip_add(&pc->chip); 491 ret = pwmchip_add(&pc->chip);
486 if (ret < 0) { 492 if (ret < 0) {
487 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret); 493 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
@@ -508,6 +514,7 @@ pwmss_clk_failure:
508 pm_runtime_put_sync(&pdev->dev); 514 pm_runtime_put_sync(&pdev->dev);
509 pm_runtime_disable(&pdev->dev); 515 pm_runtime_disable(&pdev->dev);
510 pwmchip_remove(&pc->chip); 516 pwmchip_remove(&pc->chip);
517 clk_unprepare(pc->tbclk);
511 return ret; 518 return ret;
512} 519}
513 520
@@ -515,6 +522,8 @@ static int ehrpwm_pwm_remove(struct platform_device *pdev)
515{ 522{
516 struct ehrpwm_pwm_chip *pc = platform_get_drvdata(pdev); 523 struct ehrpwm_pwm_chip *pc = platform_get_drvdata(pdev);
517 524
525 clk_unprepare(pc->tbclk);
526
518 pm_runtime_get_sync(&pdev->dev); 527 pm_runtime_get_sync(&pdev->dev);
519 /* 528 /*
520 * Due to hardware misbehaviour, acknowledge of the stop_req 529 * Due to hardware misbehaviour, acknowledge of the stop_req
diff --git a/drivers/pwm/sysfs.c b/drivers/pwm/sysfs.c
new file mode 100644
index 000000000000..8ca5de316d3b
--- /dev/null
+++ b/drivers/pwm/sysfs.c
@@ -0,0 +1,352 @@
1/*
2 * A simple sysfs interface for the generic PWM framework
3 *
4 * Copyright (C) 2013 H Hartley Sweeten <hsweeten@visionengravers.com>
5 *
6 * Based on previous work by Lars Poeschel <poeschel@lemonage.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19#include <linux/device.h>
20#include <linux/mutex.h>
21#include <linux/err.h>
22#include <linux/slab.h>
23#include <linux/kdev_t.h>
24#include <linux/pwm.h>
25
26struct pwm_export {
27 struct device child;
28 struct pwm_device *pwm;
29};
30
31static struct pwm_export *child_to_pwm_export(struct device *child)
32{
33 return container_of(child, struct pwm_export, child);
34}
35
36static struct pwm_device *child_to_pwm_device(struct device *child)
37{
38 struct pwm_export *export = child_to_pwm_export(child);
39
40 return export->pwm;
41}
42
43static ssize_t pwm_period_show(struct device *child,
44 struct device_attribute *attr,
45 char *buf)
46{
47 const struct pwm_device *pwm = child_to_pwm_device(child);
48
49 return sprintf(buf, "%u\n", pwm->period);
50}
51
52static ssize_t pwm_period_store(struct device *child,
53 struct device_attribute *attr,
54 const char *buf, size_t size)
55{
56 struct pwm_device *pwm = child_to_pwm_device(child);
57 unsigned int val;
58 int ret;
59
60 ret = kstrtouint(buf, 0, &val);
61 if (ret)
62 return ret;
63
64 ret = pwm_config(pwm, pwm->duty_cycle, val);
65
66 return ret ? : size;
67}
68
69static ssize_t pwm_duty_cycle_show(struct device *child,
70 struct device_attribute *attr,
71 char *buf)
72{
73 const struct pwm_device *pwm = child_to_pwm_device(child);
74
75 return sprintf(buf, "%u\n", pwm->duty_cycle);
76}
77
78static ssize_t pwm_duty_cycle_store(struct device *child,
79 struct device_attribute *attr,
80 const char *buf, size_t size)
81{
82 struct pwm_device *pwm = child_to_pwm_device(child);
83 unsigned int val;
84 int ret;
85
86 ret = kstrtouint(buf, 0, &val);
87 if (ret)
88 return ret;
89
90 ret = pwm_config(pwm, val, pwm->period);
91
92 return ret ? : size;
93}
94
95static ssize_t pwm_enable_show(struct device *child,
96 struct device_attribute *attr,
97 char *buf)
98{
99 const struct pwm_device *pwm = child_to_pwm_device(child);
100 int enabled = test_bit(PWMF_ENABLED, &pwm->flags);
101
102 return sprintf(buf, "%d\n", enabled);
103}
104
105static ssize_t pwm_enable_store(struct device *child,
106 struct device_attribute *attr,
107 const char *buf, size_t size)
108{
109 struct pwm_device *pwm = child_to_pwm_device(child);
110 int val, ret;
111
112 ret = kstrtoint(buf, 0, &val);
113 if (ret)
114 return ret;
115
116 switch (val) {
117 case 0:
118 pwm_disable(pwm);
119 break;
120 case 1:
121 ret = pwm_enable(pwm);
122 break;
123 default:
124 ret = -EINVAL;
125 break;
126 }
127
128 return ret ? : size;
129}
130
131static ssize_t pwm_polarity_show(struct device *child,
132 struct device_attribute *attr,
133 char *buf)
134{
135 const struct pwm_device *pwm = child_to_pwm_device(child);
136
137 return sprintf(buf, "%s\n", pwm->polarity ? "inversed" : "normal");
138}
139
140static ssize_t pwm_polarity_store(struct device *child,
141 struct device_attribute *attr,
142 const char *buf, size_t size)
143{
144 struct pwm_device *pwm = child_to_pwm_device(child);
145 enum pwm_polarity polarity;
146 int ret;
147
148 if (sysfs_streq(buf, "normal"))
149 polarity = PWM_POLARITY_NORMAL;
150 else if (sysfs_streq(buf, "inversed"))
151 polarity = PWM_POLARITY_INVERSED;
152 else
153 return -EINVAL;
154
155 ret = pwm_set_polarity(pwm, polarity);
156
157 return ret ? : size;
158}
159
160static DEVICE_ATTR(period, 0644, pwm_period_show, pwm_period_store);
161static DEVICE_ATTR(duty_cycle, 0644, pwm_duty_cycle_show, pwm_duty_cycle_store);
162static DEVICE_ATTR(enable, 0644, pwm_enable_show, pwm_enable_store);
163static DEVICE_ATTR(polarity, 0644, pwm_polarity_show, pwm_polarity_store);
164
165static struct attribute *pwm_attrs[] = {
166 &dev_attr_period.attr,
167 &dev_attr_duty_cycle.attr,
168 &dev_attr_enable.attr,
169 &dev_attr_polarity.attr,
170 NULL
171};
172
173static const struct attribute_group pwm_attr_group = {
174 .attrs = pwm_attrs,
175};
176
177static const struct attribute_group *pwm_attr_groups[] = {
178 &pwm_attr_group,
179 NULL,
180};
181
182static void pwm_export_release(struct device *child)
183{
184 struct pwm_export *export = child_to_pwm_export(child);
185
186 kfree(export);
187}
188
189static int pwm_export_child(struct device *parent, struct pwm_device *pwm)
190{
191 struct pwm_export *export;
192 int ret;
193
194 if (test_and_set_bit(PWMF_EXPORTED, &pwm->flags))
195 return -EBUSY;
196
197 export = kzalloc(sizeof(*export), GFP_KERNEL);
198 if (!export) {
199 clear_bit(PWMF_EXPORTED, &pwm->flags);
200 return -ENOMEM;
201 }
202
203 export->pwm = pwm;
204
205 export->child.release = pwm_export_release;
206 export->child.parent = parent;
207 export->child.devt = MKDEV(0, 0);
208 export->child.groups = pwm_attr_groups;
209 dev_set_name(&export->child, "pwm%u", pwm->hwpwm);
210
211 ret = device_register(&export->child);
212 if (ret) {
213 clear_bit(PWMF_EXPORTED, &pwm->flags);
214 kfree(export);
215 return ret;
216 }
217
218 return 0;
219}
220
221static int pwm_unexport_match(struct device *child, void *data)
222{
223 return child_to_pwm_device(child) == data;
224}
225
226static int pwm_unexport_child(struct device *parent, struct pwm_device *pwm)
227{
228 struct device *child;
229
230 if (!test_and_clear_bit(PWMF_EXPORTED, &pwm->flags))
231 return -ENODEV;
232
233 child = device_find_child(parent, pwm, pwm_unexport_match);
234 if (!child)
235 return -ENODEV;
236
237 /* for device_find_child() */
238 put_device(child);
239 device_unregister(child);
240 pwm_put(pwm);
241
242 return 0;
243}
244
245static ssize_t pwm_export_store(struct device *parent,
246 struct device_attribute *attr,
247 const char *buf, size_t len)
248{
249 struct pwm_chip *chip = dev_get_drvdata(parent);
250 struct pwm_device *pwm;
251 unsigned int hwpwm;
252 int ret;
253
254 ret = kstrtouint(buf, 0, &hwpwm);
255 if (ret < 0)
256 return ret;
257
258 if (hwpwm >= chip->npwm)
259 return -ENODEV;
260
261 pwm = pwm_request_from_chip(chip, hwpwm, "sysfs");
262 if (IS_ERR(pwm))
263 return PTR_ERR(pwm);
264
265 ret = pwm_export_child(parent, pwm);
266 if (ret < 0)
267 pwm_put(pwm);
268
269 return ret ? : len;
270}
271
272static ssize_t pwm_unexport_store(struct device *parent,
273 struct device_attribute *attr,
274 const char *buf, size_t len)
275{
276 struct pwm_chip *chip = dev_get_drvdata(parent);
277 unsigned int hwpwm;
278 int ret;
279
280 ret = kstrtouint(buf, 0, &hwpwm);
281 if (ret < 0)
282 return ret;
283
284 if (hwpwm >= chip->npwm)
285 return -ENODEV;
286
287 ret = pwm_unexport_child(parent, &chip->pwms[hwpwm]);
288
289 return ret ? : len;
290}
291
292static ssize_t pwm_npwm_show(struct device *parent,
293 struct device_attribute *attr,
294 char *buf)
295{
296 const struct pwm_chip *chip = dev_get_drvdata(parent);
297
298 return sprintf(buf, "%u\n", chip->npwm);
299}
300
301static struct device_attribute pwm_chip_attrs[] = {
302 __ATTR(export, 0200, NULL, pwm_export_store),
303 __ATTR(unexport, 0200, NULL, pwm_unexport_store),
304 __ATTR(npwm, 0444, pwm_npwm_show, NULL),
305 __ATTR_NULL,
306};
307
308static struct class pwm_class = {
309 .name = "pwm",
310 .owner = THIS_MODULE,
311 .dev_attrs = pwm_chip_attrs,
312};
313
314static int pwmchip_sysfs_match(struct device *parent, const void *data)
315{
316 return dev_get_drvdata(parent) == data;
317}
318
319void pwmchip_sysfs_export(struct pwm_chip *chip)
320{
321 struct device *parent;
322
323 /*
324 * If device_create() fails the pwm_chip is still usable by
325 * the kernel its just not exported.
326 */
327 parent = device_create(&pwm_class, chip->dev, MKDEV(0, 0), chip,
328 "pwmchip%d", chip->base);
329 if (IS_ERR(parent)) {
330 dev_warn(chip->dev,
331 "device_create failed for pwm_chip sysfs export\n");
332 }
333}
334
335void pwmchip_sysfs_unexport(struct pwm_chip *chip)
336{
337 struct device *parent;
338
339 parent = class_find_device(&pwm_class, NULL, chip,
340 pwmchip_sysfs_match);
341 if (parent) {
342 /* for class_find_device() */
343 put_device(parent);
344 device_unregister(parent);
345 }
346}
347
348static int __init pwm_sysfs_init(void)
349{
350 return class_register(&pwm_class);
351}
352subsys_initcall(pwm_sysfs_init);