1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <clocksource/timer-xilinx.h>
21#include <linux/clk.h>
22#include <linux/clk-provider.h>
23#include <linux/device.h>
24#include <linux/module.h>
25#include <linux/of.h>
26#include <linux/platform_device.h>
27#include <linux/pwm.h>
28#include <linux/regmap.h>
29
30
31
32
33
34u32 xilinx_timer_tlr_cycles(struct xilinx_timer_priv *priv, u32 tcsr,
35 u64 cycles)
36{
37 WARN_ON(cycles < 2 || cycles - 2 > priv->max);
38
39 if (tcsr & TCSR_UDT)
40 return cycles - 2;
41 return priv->max - cycles + 2;
42}
43
44unsigned int xilinx_timer_get_period(struct xilinx_timer_priv *priv,
45 u32 tlr, u32 tcsr)
46{
47 u64 cycles;
48
49 if (tcsr & TCSR_UDT)
50 cycles = tlr + 2;
51 else
52 cycles = (u64)priv->max - tlr + 2;
53
54
55 return DIV64_U64_ROUND_UP(cycles * NSEC_PER_SEC,
56 clk_get_rate(priv->clk));
57}
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79#define TCSR_PWM_SET (TCSR_GENT | TCSR_ARHT | TCSR_ENT | TCSR_PWMA)
80#define TCSR_PWM_CLEAR (TCSR_MDT | TCSR_LOAD)
81#define TCSR_PWM_MASK (TCSR_PWM_SET | TCSR_PWM_CLEAR)
82
83struct xilinx_pwm_device {
84 struct pwm_chip chip;
85 struct xilinx_timer_priv priv;
86};
87
88static inline struct xilinx_timer_priv
89*xilinx_pwm_chip_to_priv(struct pwm_chip *chip)
90{
91 return &container_of(chip, struct xilinx_pwm_device, chip)->priv;
92}
93
94static bool xilinx_timer_pwm_enabled(u32 tcsr0, u32 tcsr1)
95{
96 return ((TCSR_PWM_MASK | TCSR_CASC) & tcsr0) == TCSR_PWM_SET &&
97 (TCSR_PWM_MASK & tcsr1) == TCSR_PWM_SET;
98}
99
100static int xilinx_pwm_apply(struct pwm_chip *chip, struct pwm_device *unused,
101 const struct pwm_state *state)
102{
103 struct xilinx_timer_priv *priv = xilinx_pwm_chip_to_priv(chip);
104 u32 tlr0, tlr1, tcsr0, tcsr1;
105 u64 period_cycles, duty_cycles;
106 unsigned long rate;
107
108 if (state->polarity != PWM_POLARITY_NORMAL)
109 return -EINVAL;
110
111
112
113
114
115
116
117
118 rate = clk_get_rate(priv->clk);
119
120 period_cycles = min_t(u64, state->period, U32_MAX * NSEC_PER_SEC);
121 period_cycles = mul_u64_u32_div(period_cycles, rate, NSEC_PER_SEC);
122 period_cycles = min_t(u64, period_cycles, priv->max + 2);
123 if (period_cycles < 2)
124 return -ERANGE;
125
126
127 duty_cycles = min_t(u64, state->duty_cycle, U32_MAX * NSEC_PER_SEC);
128 duty_cycles = mul_u64_u32_div(duty_cycles, rate, NSEC_PER_SEC);
129 duty_cycles = min_t(u64, duty_cycles, priv->max + 2);
130
131
132
133
134
135 if (duty_cycles >= period_cycles)
136 duty_cycles = period_cycles - 1;
137
138
139 if (duty_cycles < 2)
140 duty_cycles = period_cycles;
141
142 regmap_read(priv->map, TCSR0, &tcsr0);
143 regmap_read(priv->map, TCSR1, &tcsr1);
144 tlr0 = xilinx_timer_tlr_cycles(priv, tcsr0, period_cycles);
145 tlr1 = xilinx_timer_tlr_cycles(priv, tcsr1, duty_cycles);
146 regmap_write(priv->map, TLR0, tlr0);
147 regmap_write(priv->map, TLR1, tlr1);
148
149 if (state->enabled) {
150
151
152
153
154 if (!xilinx_timer_pwm_enabled(tcsr0, tcsr1)) {
155
156 regmap_write(priv->map, TCSR0, tcsr0 | TCSR_LOAD);
157 regmap_write(priv->map, TCSR1, tcsr1 | TCSR_LOAD);
158
159 tcsr0 = (TCSR_PWM_SET & ~TCSR_ENT) | (tcsr0 & TCSR_UDT);
160 tcsr1 = TCSR_PWM_SET | TCSR_ENALL | (tcsr1 & TCSR_UDT);
161 regmap_write(priv->map, TCSR0, tcsr0);
162 regmap_write(priv->map, TCSR1, tcsr1);
163 }
164 } else {
165 regmap_write(priv->map, TCSR0, 0);
166 regmap_write(priv->map, TCSR1, 0);
167 }
168
169 return 0;
170}
171
172static void xilinx_pwm_get_state(struct pwm_chip *chip,
173 struct pwm_device *unused,
174 struct pwm_state *state)
175{
176 struct xilinx_timer_priv *priv = xilinx_pwm_chip_to_priv(chip);
177 u32 tlr0, tlr1, tcsr0, tcsr1;
178
179 regmap_read(priv->map, TLR0, &tlr0);
180 regmap_read(priv->map, TLR1, &tlr1);
181 regmap_read(priv->map, TCSR0, &tcsr0);
182 regmap_read(priv->map, TCSR1, &tcsr1);
183 state->period = xilinx_timer_get_period(priv, tlr0, tcsr0);
184 state->duty_cycle = xilinx_timer_get_period(priv, tlr1, tcsr1);
185 state->enabled = xilinx_timer_pwm_enabled(tcsr0, tcsr1);
186 state->polarity = PWM_POLARITY_NORMAL;
187
188
189
190
191
192 if (state->period == state->duty_cycle)
193 state->duty_cycle = 0;
194}
195
196static const struct pwm_ops xilinx_pwm_ops = {
197 .apply = xilinx_pwm_apply,
198 .get_state = xilinx_pwm_get_state,
199 .owner = THIS_MODULE,
200};
201
202static const struct regmap_config xilinx_pwm_regmap_config = {
203 .reg_bits = 32,
204 .reg_stride = 4,
205 .val_bits = 32,
206 .val_format_endian = REGMAP_ENDIAN_LITTLE,
207 .max_register = TCR1,
208};
209
210static int xilinx_pwm_probe(struct platform_device *pdev)
211{
212 int ret;
213 struct device *dev = &pdev->dev;
214 struct device_node *np = dev->of_node;
215 struct xilinx_timer_priv *priv;
216 struct xilinx_pwm_device *xilinx_pwm;
217 u32 pwm_cells, one_timer, width;
218 void __iomem *regs;
219
220
221 ret = of_property_read_u32(np, "#pwm-cells", &pwm_cells);
222 if (ret == -EINVAL)
223 return -ENODEV;
224 if (ret)
225 return dev_err_probe(dev, ret, "could not read #pwm-cells\n");
226
227 xilinx_pwm = devm_kzalloc(dev, sizeof(*xilinx_pwm), GFP_KERNEL);
228 if (!xilinx_pwm)
229 return -ENOMEM;
230 platform_set_drvdata(pdev, xilinx_pwm);
231 priv = &xilinx_pwm->priv;
232
233 regs = devm_platform_ioremap_resource(pdev, 0);
234 if (IS_ERR(regs))
235 return PTR_ERR(regs);
236
237 priv->map = devm_regmap_init_mmio(dev, regs,
238 &xilinx_pwm_regmap_config);
239 if (IS_ERR(priv->map))
240 return dev_err_probe(dev, PTR_ERR(priv->map),
241 "Could not create regmap\n");
242
243 ret = of_property_read_u32(np, "xlnx,one-timer-only", &one_timer);
244 if (ret)
245 return dev_err_probe(dev, ret,
246 "Could not read xlnx,one-timer-only\n");
247
248 if (one_timer)
249 return dev_err_probe(dev, -EINVAL,
250 "Two timers required for PWM mode\n");
251
252 ret = of_property_read_u32(np, "xlnx,count-width", &width);
253 if (ret == -EINVAL)
254 width = 32;
255 else if (ret)
256 return dev_err_probe(dev, ret,
257 "Could not read xlnx,count-width\n");
258
259 if (width != 8 && width != 16 && width != 32)
260 return dev_err_probe(dev, -EINVAL,
261 "Invalid counter width %d\n", width);
262 priv->max = BIT_ULL(width) - 1;
263
264
265
266
267
268
269
270 priv->clk = devm_clk_get(dev, "s_axi_aclk");
271 if (IS_ERR(priv->clk))
272 return dev_err_probe(dev, PTR_ERR(priv->clk),
273 "Could not get clock\n");
274
275 ret = clk_prepare_enable(priv->clk);
276 if (ret)
277 return dev_err_probe(dev, ret, "Clock enable failed\n");
278 clk_rate_exclusive_get(priv->clk);
279
280 xilinx_pwm->chip.dev = dev;
281 xilinx_pwm->chip.ops = &xilinx_pwm_ops;
282 xilinx_pwm->chip.npwm = 1;
283 ret = pwmchip_add(&xilinx_pwm->chip);
284 if (ret) {
285 clk_rate_exclusive_put(priv->clk);
286 clk_disable_unprepare(priv->clk);
287 return dev_err_probe(dev, ret, "Could not register PWM chip\n");
288 }
289
290 return 0;
291}
292
293static int xilinx_pwm_remove(struct platform_device *pdev)
294{
295 struct xilinx_pwm_device *xilinx_pwm = platform_get_drvdata(pdev);
296
297 pwmchip_remove(&xilinx_pwm->chip);
298 clk_rate_exclusive_put(xilinx_pwm->priv.clk);
299 clk_disable_unprepare(xilinx_pwm->priv.clk);
300 return 0;
301}
302
303static const struct of_device_id xilinx_pwm_of_match[] = {
304 { .compatible = "xlnx,xps-timer-1.00.a", },
305 {},
306};
307MODULE_DEVICE_TABLE(of, xilinx_pwm_of_match);
308
309static struct platform_driver xilinx_pwm_driver = {
310 .probe = xilinx_pwm_probe,
311 .remove = xilinx_pwm_remove,
312 .driver = {
313 .name = "xilinx-pwm",
314 .of_match_table = of_match_ptr(xilinx_pwm_of_match),
315 },
316};
317module_platform_driver(xilinx_pwm_driver);
318
319MODULE_ALIAS("platform:xilinx-pwm");
320MODULE_DESCRIPTION("PWM driver for Xilinx LogiCORE IP AXI Timer");
321MODULE_LICENSE("GPL");
322