1 // SPDX-License-Identifier: GPL-2.0
3 * R-Mobile TPU PWM driver
5 * Copyright (C) 2012 Renesas Solutions Corp.
11 #include <linux/init.h>
12 #include <linux/ioport.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/pwm.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
22 #define TPU_CHANNEL_MAX 4
24 #define TPU_TSTR 0x00 /* Timer start register (shared) */
26 #define TPU_TCRn 0x00 /* Timer control register */
27 #define TPU_TCR_CCLR_NONE (0 << 5)
28 #define TPU_TCR_CCLR_TGRA (1 << 5)
29 #define TPU_TCR_CCLR_TGRB (2 << 5)
30 #define TPU_TCR_CCLR_TGRC (5 << 5)
31 #define TPU_TCR_CCLR_TGRD (6 << 5)
32 #define TPU_TCR_CKEG_RISING (0 << 3)
33 #define TPU_TCR_CKEG_FALLING (1 << 3)
34 #define TPU_TCR_CKEG_BOTH (2 << 3)
35 #define TPU_TMDRn 0x04 /* Timer mode register */
36 #define TPU_TMDR_BFWT (1 << 6)
37 #define TPU_TMDR_BFB (1 << 5)
38 #define TPU_TMDR_BFA (1 << 4)
39 #define TPU_TMDR_MD_NORMAL (0 << 0)
40 #define TPU_TMDR_MD_PWM (2 << 0)
41 #define TPU_TIORn 0x08 /* Timer I/O control register */
42 #define TPU_TIOR_IOA_0 (0 << 0)
43 #define TPU_TIOR_IOA_0_CLR (1 << 0)
44 #define TPU_TIOR_IOA_0_SET (2 << 0)
45 #define TPU_TIOR_IOA_0_TOGGLE (3 << 0)
46 #define TPU_TIOR_IOA_1 (4 << 0)
47 #define TPU_TIOR_IOA_1_CLR (5 << 0)
48 #define TPU_TIOR_IOA_1_SET (6 << 0)
49 #define TPU_TIOR_IOA_1_TOGGLE (7 << 0)
50 #define TPU_TIERn 0x0c /* Timer interrupt enable register */
51 #define TPU_TSRn 0x10 /* Timer status register */
52 #define TPU_TCNTn 0x14 /* Timer counter */
53 #define TPU_TGRAn 0x18 /* Timer general register A */
54 #define TPU_TGRBn 0x1c /* Timer general register B */
55 #define TPU_TGRCn 0x20 /* Timer general register C */
56 #define TPU_TGRDn 0x24 /* Timer general register D */
58 #define TPU_CHANNEL_OFFSET 0x10
59 #define TPU_CHANNEL_SIZE 0x40
62 TPU_PIN_INACTIVE
, /* Pin is driven inactive */
63 TPU_PIN_PWM
, /* Pin is driven by PWM */
64 TPU_PIN_ACTIVE
, /* Pin is driven active */
69 struct tpu_pwm_device
{
70 bool timer_on
; /* Whether the timer is running */
72 struct tpu_device
*tpu
;
73 unsigned int channel
; /* Channel number in the TPU */
75 enum pwm_polarity polarity
;
76 unsigned int prescaler
;
82 struct platform_device
*pdev
;
90 #define to_tpu_device(c) container_of(c, struct tpu_device, chip)
92 static void tpu_pwm_write(struct tpu_pwm_device
*pwm
, int reg_nr
, u16 value
)
94 void __iomem
*base
= pwm
->tpu
->base
+ TPU_CHANNEL_OFFSET
95 + pwm
->channel
* TPU_CHANNEL_SIZE
;
97 iowrite16(value
, base
+ reg_nr
);
100 static void tpu_pwm_set_pin(struct tpu_pwm_device
*pwm
,
101 enum tpu_pin_state state
)
103 static const char * const states
[] = { "inactive", "PWM", "active" };
105 dev_dbg(&pwm
->tpu
->pdev
->dev
, "%u: configuring pin as %s\n",
106 pwm
->channel
, states
[state
]);
109 case TPU_PIN_INACTIVE
:
110 tpu_pwm_write(pwm
, TPU_TIORn
,
111 pwm
->polarity
== PWM_POLARITY_INVERSED
?
112 TPU_TIOR_IOA_1
: TPU_TIOR_IOA_0
);
115 tpu_pwm_write(pwm
, TPU_TIORn
,
116 pwm
->polarity
== PWM_POLARITY_INVERSED
?
117 TPU_TIOR_IOA_0_SET
: TPU_TIOR_IOA_1_CLR
);
120 tpu_pwm_write(pwm
, TPU_TIORn
,
121 pwm
->polarity
== PWM_POLARITY_INVERSED
?
122 TPU_TIOR_IOA_0
: TPU_TIOR_IOA_1
);
127 static void tpu_pwm_start_stop(struct tpu_pwm_device
*pwm
, int start
)
132 spin_lock_irqsave(&pwm
->tpu
->lock
, flags
);
133 value
= ioread16(pwm
->tpu
->base
+ TPU_TSTR
);
136 value
|= 1 << pwm
->channel
;
138 value
&= ~(1 << pwm
->channel
);
140 iowrite16(value
, pwm
->tpu
->base
+ TPU_TSTR
);
141 spin_unlock_irqrestore(&pwm
->tpu
->lock
, flags
);
144 static int tpu_pwm_timer_start(struct tpu_pwm_device
*pwm
)
148 if (!pwm
->timer_on
) {
149 /* Wake up device and enable clock. */
150 pm_runtime_get_sync(&pwm
->tpu
->pdev
->dev
);
151 ret
= clk_prepare_enable(pwm
->tpu
->clk
);
153 dev_err(&pwm
->tpu
->pdev
->dev
, "cannot enable clock\n");
156 pwm
->timer_on
= true;
160 * Make sure the channel is stopped, as we need to reconfigure it
161 * completely. First drive the pin to the inactive state to avoid
164 tpu_pwm_set_pin(pwm
, TPU_PIN_INACTIVE
);
165 tpu_pwm_start_stop(pwm
, false);
168 * - Clear TCNT on TGRB match
169 * - Count on rising edge
171 * - Output 0 until TGRA, output 1 until TGRB (active low polarity)
172 * - Output 1 until TGRA, output 0 until TGRB (active high polarity
175 tpu_pwm_write(pwm
, TPU_TCRn
, TPU_TCR_CCLR_TGRB
| TPU_TCR_CKEG_RISING
|
177 tpu_pwm_write(pwm
, TPU_TMDRn
, TPU_TMDR_MD_PWM
);
178 tpu_pwm_set_pin(pwm
, TPU_PIN_PWM
);
179 tpu_pwm_write(pwm
, TPU_TGRAn
, pwm
->duty
);
180 tpu_pwm_write(pwm
, TPU_TGRBn
, pwm
->period
);
182 dev_dbg(&pwm
->tpu
->pdev
->dev
, "%u: TGRA 0x%04x TGRB 0x%04x\n",
183 pwm
->channel
, pwm
->duty
, pwm
->period
);
185 /* Start the channel. */
186 tpu_pwm_start_stop(pwm
, true);
191 static void tpu_pwm_timer_stop(struct tpu_pwm_device
*pwm
)
196 /* Disable channel. */
197 tpu_pwm_start_stop(pwm
, false);
199 /* Stop clock and mark device as idle. */
200 clk_disable_unprepare(pwm
->tpu
->clk
);
201 pm_runtime_put(&pwm
->tpu
->pdev
->dev
);
203 pwm
->timer_on
= false;
206 /* -----------------------------------------------------------------------------
210 static int tpu_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*_pwm
)
212 struct tpu_device
*tpu
= to_tpu_device(chip
);
213 struct tpu_pwm_device
*pwm
;
215 if (_pwm
->hwpwm
>= TPU_CHANNEL_MAX
)
218 pwm
= kzalloc(sizeof(*pwm
), GFP_KERNEL
);
223 pwm
->channel
= _pwm
->hwpwm
;
224 pwm
->polarity
= PWM_POLARITY_NORMAL
;
229 pwm
->timer_on
= false;
231 pwm_set_chip_data(_pwm
, pwm
);
236 static void tpu_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*_pwm
)
238 struct tpu_pwm_device
*pwm
= pwm_get_chip_data(_pwm
);
240 tpu_pwm_timer_stop(pwm
);
244 static int tpu_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*_pwm
,
245 int duty_ns
, int period_ns
)
247 static const unsigned int prescalers
[] = { 1, 4, 16, 64 };
248 struct tpu_pwm_device
*pwm
= pwm_get_chip_data(_pwm
);
249 struct tpu_device
*tpu
= to_tpu_device(chip
);
250 unsigned int prescaler
;
251 bool duty_only
= false;
258 * Pick a prescaler to avoid overflowing the counter.
259 * TODO: Pick the highest acceptable prescaler.
261 clk_rate
= clk_get_rate(tpu
->clk
);
263 for (prescaler
= 0; prescaler
< ARRAY_SIZE(prescalers
); ++prescaler
) {
264 period
= clk_rate
/ prescalers
[prescaler
]
265 / (NSEC_PER_SEC
/ period_ns
);
266 if (period
<= 0xffff)
270 if (prescaler
== ARRAY_SIZE(prescalers
) || period
== 0) {
271 dev_err(&tpu
->pdev
->dev
, "clock rate mismatch\n");
276 duty
= clk_rate
/ prescalers
[prescaler
]
277 / (NSEC_PER_SEC
/ duty_ns
);
284 dev_dbg(&tpu
->pdev
->dev
,
285 "rate %u, prescaler %u, period %u, duty %u\n",
286 clk_rate
, prescalers
[prescaler
], period
, duty
);
288 if (pwm
->prescaler
== prescaler
&& pwm
->period
== period
)
291 pwm
->prescaler
= prescaler
;
292 pwm
->period
= period
;
295 /* If the channel is disabled we're done. */
296 if (!pwm_is_enabled(_pwm
))
299 if (duty_only
&& pwm
->timer_on
) {
301 * If only the duty cycle changed and the timer is already
302 * running, there's no need to reconfigure it completely, Just
303 * modify the duty cycle.
305 tpu_pwm_write(pwm
, TPU_TGRAn
, pwm
->duty
);
306 dev_dbg(&tpu
->pdev
->dev
, "%u: TGRA 0x%04x\n", pwm
->channel
,
309 /* Otherwise perform a full reconfiguration. */
310 ret
= tpu_pwm_timer_start(pwm
);
315 if (duty
== 0 || duty
== period
) {
317 * To avoid running the timer when not strictly required, handle
318 * 0% and 100% duty cycles as fixed levels and stop the timer.
320 tpu_pwm_set_pin(pwm
, duty
? TPU_PIN_ACTIVE
: TPU_PIN_INACTIVE
);
321 tpu_pwm_timer_stop(pwm
);
327 static int tpu_pwm_set_polarity(struct pwm_chip
*chip
, struct pwm_device
*_pwm
,
328 enum pwm_polarity polarity
)
330 struct tpu_pwm_device
*pwm
= pwm_get_chip_data(_pwm
);
332 pwm
->polarity
= polarity
;
337 static int tpu_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*_pwm
)
339 struct tpu_pwm_device
*pwm
= pwm_get_chip_data(_pwm
);
342 ret
= tpu_pwm_timer_start(pwm
);
347 * To avoid running the timer when not strictly required, handle 0% and
348 * 100% duty cycles as fixed levels and stop the timer.
350 if (pwm
->duty
== 0 || pwm
->duty
== pwm
->period
) {
351 tpu_pwm_set_pin(pwm
, pwm
->duty
?
352 TPU_PIN_ACTIVE
: TPU_PIN_INACTIVE
);
353 tpu_pwm_timer_stop(pwm
);
359 static void tpu_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*_pwm
)
361 struct tpu_pwm_device
*pwm
= pwm_get_chip_data(_pwm
);
363 /* The timer must be running to modify the pin output configuration. */
364 tpu_pwm_timer_start(pwm
);
365 tpu_pwm_set_pin(pwm
, TPU_PIN_INACTIVE
);
366 tpu_pwm_timer_stop(pwm
);
369 static const struct pwm_ops tpu_pwm_ops
= {
370 .request
= tpu_pwm_request
,
371 .free
= tpu_pwm_free
,
372 .config
= tpu_pwm_config
,
373 .set_polarity
= tpu_pwm_set_polarity
,
374 .enable
= tpu_pwm_enable
,
375 .disable
= tpu_pwm_disable
,
376 .owner
= THIS_MODULE
,
379 /* -----------------------------------------------------------------------------
383 static int tpu_probe(struct platform_device
*pdev
)
385 struct tpu_device
*tpu
;
388 tpu
= devm_kzalloc(&pdev
->dev
, sizeof(*tpu
), GFP_KERNEL
);
392 spin_lock_init(&tpu
->lock
);
395 /* Map memory, get clock and pin control. */
396 tpu
->base
= devm_platform_ioremap_resource(pdev
, 0);
397 if (IS_ERR(tpu
->base
))
398 return PTR_ERR(tpu
->base
);
400 tpu
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
401 if (IS_ERR(tpu
->clk
)) {
402 dev_err(&pdev
->dev
, "cannot get clock\n");
403 return PTR_ERR(tpu
->clk
);
406 /* Initialize and register the device. */
407 platform_set_drvdata(pdev
, tpu
);
409 tpu
->chip
.dev
= &pdev
->dev
;
410 tpu
->chip
.ops
= &tpu_pwm_ops
;
411 tpu
->chip
.of_xlate
= of_pwm_xlate_with_flags
;
412 tpu
->chip
.of_pwm_n_cells
= 3;
414 tpu
->chip
.npwm
= TPU_CHANNEL_MAX
;
416 pm_runtime_enable(&pdev
->dev
);
418 ret
= pwmchip_add(&tpu
->chip
);
420 dev_err(&pdev
->dev
, "failed to register PWM chip\n");
421 pm_runtime_disable(&pdev
->dev
);
428 static int tpu_remove(struct platform_device
*pdev
)
430 struct tpu_device
*tpu
= platform_get_drvdata(pdev
);
433 ret
= pwmchip_remove(&tpu
->chip
);
435 pm_runtime_disable(&pdev
->dev
);
441 static const struct of_device_id tpu_of_table
[] = {
442 { .compatible
= "renesas,tpu-r8a73a4", },
443 { .compatible
= "renesas,tpu-r8a7740", },
444 { .compatible
= "renesas,tpu-r8a7790", },
445 { .compatible
= "renesas,tpu", },
449 MODULE_DEVICE_TABLE(of
, tpu_of_table
);
452 static struct platform_driver tpu_driver
= {
454 .remove
= tpu_remove
,
456 .name
= "renesas-tpu-pwm",
457 .of_match_table
= of_match_ptr(tpu_of_table
),
461 module_platform_driver(tpu_driver
);
463 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
464 MODULE_DESCRIPTION("Renesas TPU PWM Driver");
465 MODULE_LICENSE("GPL v2");
466 MODULE_ALIAS("platform:renesas-tpu-pwm");