Merge tag 'acpi-6.9-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-block.git] / drivers / pwm / pwm-pxa.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
75540c1a 2/*
45b301d2 3 * drivers/pwm/pwm-pxa.c
75540c1a 4 *
5 * simple driver for PWM (Pulse Width Modulator) controller
6 *
75540c1a 7 * 2008-02-13 initial version
b07ab663 8 * eric miao <eric.miao@marvell.com>
8ba2725f
DB
9 *
10 * Links to reference manuals for some of the supported PWM chips can be found
173cb655 11 * in Documentation/arch/arm/marvell.rst.
8ba2725f
DB
12 *
13 * Limitations:
14 * - When PWM is stopped, the current PWM period stops abruptly at the next
15 * input clock (PWMCR_SD is set) and the output is driven to inactive.
75540c1a 16 */
17
0a41b0c5 18#include <linux/mod_devicetable.h>
75540c1a 19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/platform_device.h>
5a0e3ad6 22#include <linux/slab.h>
75540c1a 23#include <linux/err.h>
24#include <linux/clk.h>
25#include <linux/io.h>
26#include <linux/pwm.h>
87e51b76 27#include <linux/of.h>
75540c1a 28
29#include <asm/div64.h>
75540c1a 30
3d2a98cd
EM
31#define HAS_SECONDARY_PWM 0x10
32
33static const struct platform_device_id pwm_id_table[] = {
34 /* PWM has_secondary_pwm? */
35 { "pxa25x-pwm", 0 },
22976a5d
AL
36 { "pxa27x-pwm", HAS_SECONDARY_PWM },
37 { "pxa168-pwm", 0 },
38 { "pxa910-pwm", 0 },
3d2a98cd
EM
39 { },
40};
41MODULE_DEVICE_TABLE(platform, pwm_id_table);
42
75540c1a 43/* PWM registers and bits definitions */
44#define PWMCR (0x00)
45#define PWMDCR (0x04)
46#define PWMPCR (0x08)
47
48#define PWMCR_SD (1 << 6)
49#define PWMDCR_FD (1 << 10)
50
17b2b478 51struct pxa_pwm_chip {
17b2b478 52 struct device *dev;
75540c1a 53
75540c1a 54 struct clk *clk;
55 void __iomem *mmio_base;
75540c1a 56};
57
17b2b478
TR
58static inline struct pxa_pwm_chip *to_pxa_pwm_chip(struct pwm_chip *chip)
59{
8aebd722 60 return pwmchip_get_drvdata(chip);
17b2b478
TR
61}
62
75540c1a 63/*
64 * period_ns = 10^9 * (PRESCALE + 1) * (PV + 1) / PWM_CLK_RATE
65 * duty_ns = 10^9 * (PRESCALE + 1) * DC / PWM_CLK_RATE
66 */
17b2b478 67static int pxa_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
657e54e5 68 u64 duty_ns, u64 period_ns)
75540c1a 69{
17b2b478 70 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
75540c1a 71 unsigned long long c;
72 unsigned long period_cycles, prescale, pv, dc;
17b2b478 73 unsigned long offset;
75540c1a 74
17b2b478
TR
75 offset = pwm->hwpwm ? 0x10 : 0;
76
77 c = clk_get_rate(pc->clk);
75540c1a 78 c = c * period_ns;
79 do_div(c, 1000000000);
80 period_cycles = c;
81
71a35d75 82 if (period_cycles < 1)
75540c1a 83 period_cycles = 1;
84 prescale = (period_cycles - 1) / 1024;
85 pv = period_cycles / (prescale + 1) - 1;
86
87 if (prescale > 63)
88 return -EINVAL;
89
90 if (duty_ns == period_ns)
91 dc = PWMDCR_FD;
92 else
657e54e5 93 dc = mul_u64_u64_div_u64(pv + 1, duty_ns, period_ns);
75540c1a 94
092c2ef4 95 writel(prescale | PWMCR_SD, pc->mmio_base + offset + PWMCR);
17b2b478
TR
96 writel(dc, pc->mmio_base + offset + PWMDCR);
97 writel(pv, pc->mmio_base + offset + PWMPCR);
75540c1a 98
99 return 0;
100}
75540c1a 101
657e54e5
UKK
102static int pxa_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
103 const struct pwm_state *state)
104{
f956b838 105 struct pxa_pwm_chip *pc = to_pxa_pwm_chip(chip);
152f2d1d 106 u64 duty_cycle;
657e54e5
UKK
107 int err;
108
109 if (state->polarity != PWM_POLARITY_NORMAL)
110 return -EINVAL;
111
152f2d1d
DB
112 err = clk_prepare_enable(pc->clk);
113 if (err)
114 return err;
657e54e5 115
152f2d1d 116 duty_cycle = state->enabled ? state->duty_cycle : 0;
657e54e5 117
152f2d1d
DB
118 err = pxa_pwm_config(chip, pwm, duty_cycle, state->period);
119 if (err) {
120 clk_disable_unprepare(pc->clk);
657e54e5 121 return err;
152f2d1d
DB
122 }
123
124 if (state->enabled && !pwm->state.enabled)
125 return 0;
126
127 clk_disable_unprepare(pc->clk);
657e54e5 128
152f2d1d
DB
129 if (!state->enabled && pwm->state.enabled)
130 clk_disable_unprepare(pc->clk);
657e54e5
UKK
131
132 return 0;
133}
134
b2ec9efc 135static const struct pwm_ops pxa_pwm_ops = {
657e54e5 136 .apply = pxa_pwm_apply,
17b2b478 137};
75540c1a 138
b52fa7bc
MD
139#ifdef CONFIG_OF
140/*
fdec4f72 141 * Device tree users must create one device instance for each PWM channel.
b52fa7bc
MD
142 * Hence we dispense with the HAS_SECONDARY_PWM and "tell" the original driver
143 * code that this is a single channel pxa25x-pwm. Currently all devices are
144 * supported identically.
145 */
2ae69a46 146static const struct of_device_id pwm_of_match[] = {
b52fa7bc
MD
147 { .compatible = "marvell,pxa250-pwm", .data = &pwm_id_table[0]},
148 { .compatible = "marvell,pxa270-pwm", .data = &pwm_id_table[0]},
149 { .compatible = "marvell,pxa168-pwm", .data = &pwm_id_table[0]},
150 { .compatible = "marvell,pxa910-pwm", .data = &pwm_id_table[0]},
151 { }
152};
153MODULE_DEVICE_TABLE(of, pwm_of_match);
f409cd38
TR
154#else
155#define pwm_of_match NULL
b52fa7bc
MD
156#endif
157
3e9fe83d 158static int pwm_probe(struct platform_device *pdev)
75540c1a 159{
b3282ab1 160 const struct platform_device_id *id = platform_get_device_id(pdev);
8aebd722 161 struct pwm_chip *chip;
b63d60b2 162 struct pxa_pwm_chip *pc;
75540c1a 163 int ret = 0;
164
b52fa7bc 165 if (IS_ENABLED(CONFIG_OF) && id == NULL)
63808bbb 166 id = of_device_get_match_data(&pdev->dev);
b52fa7bc
MD
167
168 if (id == NULL)
169 return -EINVAL;
170
8aebd722
UKK
171 chip = devm_pwmchip_alloc(&pdev->dev,
172 (id->driver_data & HAS_SECONDARY_PWM) ? 2 : 1,
173 sizeof(*pc));
174 if (IS_ERR(chip))
175 return PTR_ERR(chip);
176 pc = to_pxa_pwm_chip(chip);
75540c1a 177
b63d60b2
UKK
178 pc->clk = devm_clk_get(&pdev->dev, NULL);
179 if (IS_ERR(pc->clk))
180 return PTR_ERR(pc->clk);
45b301d2 181
8aebd722 182 chip->ops = &pxa_pwm_ops;
75540c1a 183
4e77431c 184 if (IS_ENABLED(CONFIG_OF))
8aebd722 185 chip->of_xlate = of_pwm_single_xlate;
b52fa7bc 186
b63d60b2
UKK
187 pc->mmio_base = devm_platform_ioremap_resource(pdev, 0);
188 if (IS_ERR(pc->mmio_base))
189 return PTR_ERR(pc->mmio_base);
75540c1a 190
8aebd722 191 ret = devm_pwmchip_add(&pdev->dev, chip);
17b2b478
TR
192 if (ret < 0) {
193 dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
194 return ret;
3d2a98cd
EM
195 }
196
3d2a98cd 197 return 0;
75540c1a 198}
199
3d2a98cd 200static struct platform_driver pwm_driver = {
75540c1a 201 .driver = {
202 .name = "pxa25x-pwm",
f409cd38 203 .of_match_table = pwm_of_match,
75540c1a 204 },
3d2a98cd 205 .probe = pwm_probe,
3d2a98cd 206 .id_table = pwm_id_table,
75540c1a 207};
208
1e185c7a 209module_platform_driver(pwm_driver);
b5f0228a
GL
210
211MODULE_LICENSE("GPL v2");