1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas RZ/G2L MTU3a PWM Timer driver
5 * Copyright (C) 2023 Renesas Electronics Corporation
7 * Hardware manual for this IP can be found here
8 * https://www.renesas.com/eu/en/document/mah/rzg2l-group-rzg2lc-group-users-manual-hardware-0?language=en
11 * - When PWM is disabled, the output is driven to Hi-Z.
12 * - While the hardware supports both polarities, the driver (for now)
13 * only handles normal polarity.
14 * - HW uses one counter and two match components to configure duty_cycle
16 * - Multi-Function Timer Pulse Unit (a.k.a MTU) has 7 HW channels for PWM
17 * operations. (The channels are MTU{0..4, 6, 7}.)
18 * - MTU{1, 2} channels have a single IO, whereas all other HW channels have
20 * - Each IO is modelled as an independent PWM channel.
21 * - rz_mtu3_channel_io_map table is used to map the PWM channel to the
22 * corresponding HW channel as there are difference in number of IOs
23 * between HW channels.
26 #include <linux/bitfield.h>
27 #include <linux/clk.h>
28 #include <linux/limits.h>
29 #include <linux/mfd/rz-mtu3.h>
30 #include <linux/module.h>
31 #include <linux/platform_device.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/pwm.h>
34 #include <linux/time.h>
36 #define RZ_MTU3_MAX_PWM_CHANNELS 12
37 #define RZ_MTU3_MAX_HW_CHANNELS 7
40 * struct rz_mtu3_channel_io_map - MTU3 pwm channel map
42 * @base_pwm_number: First PWM of a channel
43 * @num_channel_ios: number of IOs on the HW channel.
45 struct rz_mtu3_channel_io_map {
51 * struct rz_mtu3_pwm_channel - MTU3 pwm channel data
53 * @mtu: MTU3 channel data
54 * @map: MTU3 pwm channel map
56 struct rz_mtu3_pwm_channel {
57 struct rz_mtu3_channel *mtu;
58 const struct rz_mtu3_channel_io_map *map;
62 * struct rz_mtu3_pwm_chip - MTU3 pwm private data
64 * @chip: MTU3 pwm chip data
65 * @clk: MTU3 module clock
66 * @lock: Lock to prevent concurrent access for usage count
67 * @rate: MTU3 clock rate
68 * @user_count: MTU3 usage count
69 * @enable_count: MTU3 enable count
70 * @prescale: MTU3 prescale
71 * @channel_data: MTU3 pwm channel data
74 struct rz_mtu3_pwm_chip {
79 u32 user_count[RZ_MTU3_MAX_HW_CHANNELS];
80 u32 enable_count[RZ_MTU3_MAX_HW_CHANNELS];
81 u8 prescale[RZ_MTU3_MAX_HW_CHANNELS];
82 struct rz_mtu3_pwm_channel channel_data[RZ_MTU3_MAX_HW_CHANNELS];
86 * The MTU channels are {0..4, 6, 7} and the number of IO on MTU1
87 * and MTU2 channel is 1 compared to 2 on others.
89 static const struct rz_mtu3_channel_io_map channel_map[] = {
90 { 0, 2 }, { 2, 1 }, { 3, 1 }, { 4, 2 }, { 6, 2 }, { 8, 2 }, { 10, 2 }
93 static inline struct rz_mtu3_pwm_chip *to_rz_mtu3_pwm_chip(struct pwm_chip *chip)
95 return container_of(chip, struct rz_mtu3_pwm_chip, chip);
98 static void rz_mtu3_pwm_read_tgr_registers(struct rz_mtu3_pwm_channel *priv,
99 u16 reg_pv_offset, u16 *pv_val,
100 u16 reg_dc_offset, u16 *dc_val)
102 *pv_val = rz_mtu3_16bit_ch_read(priv->mtu, reg_pv_offset);
103 *dc_val = rz_mtu3_16bit_ch_read(priv->mtu, reg_dc_offset);
106 static void rz_mtu3_pwm_write_tgr_registers(struct rz_mtu3_pwm_channel *priv,
107 u16 reg_pv_offset, u16 pv_val,
108 u16 reg_dc_offset, u16 dc_val)
110 rz_mtu3_16bit_ch_write(priv->mtu, reg_pv_offset, pv_val);
111 rz_mtu3_16bit_ch_write(priv->mtu, reg_dc_offset, dc_val);
114 static u8 rz_mtu3_pwm_calculate_prescale(struct rz_mtu3_pwm_chip *rz_mtu3,
117 u32 prescaled_period_cycles;
121 * Supported prescale values are 1, 4, 16 and 64.
122 * TODO: Support prescale values 2, 8, 32, 256 and 1024.
124 prescaled_period_cycles = period_cycles >> 16;
125 if (prescaled_period_cycles >= 16)
128 prescale = (fls(prescaled_period_cycles) + 1) / 2;
133 static struct rz_mtu3_pwm_channel *
134 rz_mtu3_get_channel(struct rz_mtu3_pwm_chip *rz_mtu3_pwm, u32 hwpwm)
136 struct rz_mtu3_pwm_channel *priv = rz_mtu3_pwm->channel_data;
139 for (ch = 0; ch < RZ_MTU3_MAX_HW_CHANNELS; ch++, priv++) {
140 if (priv->map->base_pwm_number + priv->map->num_channel_ios > hwpwm)
147 static bool rz_mtu3_pwm_is_ch_enabled(struct rz_mtu3_pwm_chip *rz_mtu3_pwm,
150 struct rz_mtu3_pwm_channel *priv;
154 priv = rz_mtu3_get_channel(rz_mtu3_pwm, hwpwm);
155 is_channel_en = rz_mtu3_is_enabled(priv->mtu);
159 if (priv->map->base_pwm_number == hwpwm)
160 val = rz_mtu3_8bit_ch_read(priv->mtu, RZ_MTU3_TIORH);
162 val = rz_mtu3_8bit_ch_read(priv->mtu, RZ_MTU3_TIORL);
164 return val & RZ_MTU3_TIOR_IOA;
167 static int rz_mtu3_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
169 struct rz_mtu3_pwm_chip *rz_mtu3_pwm = to_rz_mtu3_pwm_chip(chip);
170 struct rz_mtu3_pwm_channel *priv;
171 bool is_mtu3_channel_available;
174 priv = rz_mtu3_get_channel(rz_mtu3_pwm, pwm->hwpwm);
175 ch = priv - rz_mtu3_pwm->channel_data;
177 mutex_lock(&rz_mtu3_pwm->lock);
179 * Each channel must be requested only once, so if the channel
180 * serves two PWMs and the other is already requested, skip over
181 * rz_mtu3_request_channel()
183 if (!rz_mtu3_pwm->user_count[ch]) {
184 is_mtu3_channel_available = rz_mtu3_request_channel(priv->mtu);
185 if (!is_mtu3_channel_available) {
186 mutex_unlock(&rz_mtu3_pwm->lock);
191 rz_mtu3_pwm->user_count[ch]++;
192 mutex_unlock(&rz_mtu3_pwm->lock);
197 static void rz_mtu3_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
199 struct rz_mtu3_pwm_chip *rz_mtu3_pwm = to_rz_mtu3_pwm_chip(chip);
200 struct rz_mtu3_pwm_channel *priv;
203 priv = rz_mtu3_get_channel(rz_mtu3_pwm, pwm->hwpwm);
204 ch = priv - rz_mtu3_pwm->channel_data;
206 mutex_lock(&rz_mtu3_pwm->lock);
207 rz_mtu3_pwm->user_count[ch]--;
208 if (!rz_mtu3_pwm->user_count[ch])
209 rz_mtu3_release_channel(priv->mtu);
211 mutex_unlock(&rz_mtu3_pwm->lock);
214 static int rz_mtu3_pwm_enable(struct rz_mtu3_pwm_chip *rz_mtu3_pwm,
215 struct pwm_device *pwm)
217 struct rz_mtu3_pwm_channel *priv;
222 rc = pm_runtime_resume_and_get(rz_mtu3_pwm->chip.dev);
226 priv = rz_mtu3_get_channel(rz_mtu3_pwm, pwm->hwpwm);
227 ch = priv - rz_mtu3_pwm->channel_data;
228 val = RZ_MTU3_TIOR_OC_IOB_TOGGLE | RZ_MTU3_TIOR_OC_IOA_H_COMP_MATCH;
230 rz_mtu3_8bit_ch_write(priv->mtu, RZ_MTU3_TMDR1, RZ_MTU3_TMDR1_MD_PWMMODE1);
231 if (priv->map->base_pwm_number == pwm->hwpwm)
232 rz_mtu3_8bit_ch_write(priv->mtu, RZ_MTU3_TIORH, val);
234 rz_mtu3_8bit_ch_write(priv->mtu, RZ_MTU3_TIORL, val);
236 mutex_lock(&rz_mtu3_pwm->lock);
237 if (!rz_mtu3_pwm->enable_count[ch])
238 rz_mtu3_enable(priv->mtu);
240 rz_mtu3_pwm->enable_count[ch]++;
241 mutex_unlock(&rz_mtu3_pwm->lock);
246 static void rz_mtu3_pwm_disable(struct rz_mtu3_pwm_chip *rz_mtu3_pwm,
247 struct pwm_device *pwm)
249 struct rz_mtu3_pwm_channel *priv;
252 priv = rz_mtu3_get_channel(rz_mtu3_pwm, pwm->hwpwm);
253 ch = priv - rz_mtu3_pwm->channel_data;
255 /* Disable output pins of MTU3 channel */
256 if (priv->map->base_pwm_number == pwm->hwpwm)
257 rz_mtu3_8bit_ch_write(priv->mtu, RZ_MTU3_TIORH, RZ_MTU3_TIOR_OC_RETAIN);
259 rz_mtu3_8bit_ch_write(priv->mtu, RZ_MTU3_TIORL, RZ_MTU3_TIOR_OC_RETAIN);
261 mutex_lock(&rz_mtu3_pwm->lock);
262 rz_mtu3_pwm->enable_count[ch]--;
263 if (!rz_mtu3_pwm->enable_count[ch])
264 rz_mtu3_disable(priv->mtu);
266 mutex_unlock(&rz_mtu3_pwm->lock);
268 pm_runtime_put_sync(rz_mtu3_pwm->chip.dev);
271 static int rz_mtu3_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
272 struct pwm_state *state)
274 struct rz_mtu3_pwm_chip *rz_mtu3_pwm = to_rz_mtu3_pwm_chip(chip);
277 rc = pm_runtime_resume_and_get(chip->dev);
281 state->enabled = rz_mtu3_pwm_is_ch_enabled(rz_mtu3_pwm, pwm->hwpwm);
282 if (state->enabled) {
283 struct rz_mtu3_pwm_channel *priv;
288 priv = rz_mtu3_get_channel(rz_mtu3_pwm, pwm->hwpwm);
289 if (priv->map->base_pwm_number == pwm->hwpwm)
290 rz_mtu3_pwm_read_tgr_registers(priv, RZ_MTU3_TGRA, &pv,
293 rz_mtu3_pwm_read_tgr_registers(priv, RZ_MTU3_TGRC, &pv,
296 val = rz_mtu3_8bit_ch_read(priv->mtu, RZ_MTU3_TCR);
297 prescale = FIELD_GET(RZ_MTU3_TCR_TPCS, val);
299 /* With prescale <= 7 and pv <= 0xffff this doesn't overflow. */
300 tmp = NSEC_PER_SEC * (u64)pv << (2 * prescale);
301 state->period = DIV_ROUND_UP_ULL(tmp, rz_mtu3_pwm->rate);
302 tmp = NSEC_PER_SEC * (u64)dc << (2 * prescale);
303 state->duty_cycle = DIV_ROUND_UP_ULL(tmp, rz_mtu3_pwm->rate);
305 if (state->duty_cycle > state->period)
306 state->duty_cycle = state->period;
309 state->polarity = PWM_POLARITY_NORMAL;
310 pm_runtime_put(chip->dev);
315 static u16 rz_mtu3_pwm_calculate_pv_or_dc(u64 period_or_duty_cycle, u8 prescale)
317 return min(period_or_duty_cycle >> (2 * prescale), (u64)U16_MAX);
320 static int rz_mtu3_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
321 const struct pwm_state *state)
323 struct rz_mtu3_pwm_chip *rz_mtu3_pwm = to_rz_mtu3_pwm_chip(chip);
324 struct rz_mtu3_pwm_channel *priv;
332 priv = rz_mtu3_get_channel(rz_mtu3_pwm, pwm->hwpwm);
333 ch = priv - rz_mtu3_pwm->channel_data;
335 period_cycles = mul_u64_u32_div(state->period, rz_mtu3_pwm->rate,
337 prescale = rz_mtu3_pwm_calculate_prescale(rz_mtu3_pwm, period_cycles);
340 * Prescalar is shared by multiple channels, so prescale can
341 * NOT be modified when there are multiple channels in use with
342 * different settings. Modify prescalar if other PWM is off or handle
343 * it, if current prescale value is less than the one we want to set.
345 if (rz_mtu3_pwm->enable_count[ch] > 1) {
346 if (rz_mtu3_pwm->prescale[ch] > prescale)
349 prescale = rz_mtu3_pwm->prescale[ch];
352 pv = rz_mtu3_pwm_calculate_pv_or_dc(period_cycles, prescale);
354 duty_cycles = mul_u64_u32_div(state->duty_cycle, rz_mtu3_pwm->rate,
356 dc = rz_mtu3_pwm_calculate_pv_or_dc(duty_cycles, prescale);
359 * If the PWM channel is disabled, make sure to turn on the clock
360 * before writing the register.
362 if (!pwm->state.enabled) {
365 rc = pm_runtime_resume_and_get(chip->dev);
370 val = RZ_MTU3_TCR_CKEG_RISING | prescale;
372 /* Counter must be stopped while updating TCR register */
373 if (rz_mtu3_pwm->prescale[ch] != prescale && rz_mtu3_pwm->enable_count[ch])
374 rz_mtu3_disable(priv->mtu);
376 if (priv->map->base_pwm_number == pwm->hwpwm) {
377 rz_mtu3_8bit_ch_write(priv->mtu, RZ_MTU3_TCR,
378 RZ_MTU3_TCR_CCLR_TGRA | val);
379 rz_mtu3_pwm_write_tgr_registers(priv, RZ_MTU3_TGRA, pv,
382 rz_mtu3_8bit_ch_write(priv->mtu, RZ_MTU3_TCR,
383 RZ_MTU3_TCR_CCLR_TGRC | val);
384 rz_mtu3_pwm_write_tgr_registers(priv, RZ_MTU3_TGRC, pv,
388 if (rz_mtu3_pwm->prescale[ch] != prescale) {
390 * Prescalar is shared by multiple channels, we cache the
391 * prescalar value from first enabled channel and use the same
392 * value for both channels.
394 rz_mtu3_pwm->prescale[ch] = prescale;
396 if (rz_mtu3_pwm->enable_count[ch])
397 rz_mtu3_enable(priv->mtu);
400 /* If the PWM is not enabled, turn the clock off again to save power. */
401 if (!pwm->state.enabled)
402 pm_runtime_put(chip->dev);
407 static int rz_mtu3_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
408 const struct pwm_state *state)
410 struct rz_mtu3_pwm_chip *rz_mtu3_pwm = to_rz_mtu3_pwm_chip(chip);
411 bool enabled = pwm->state.enabled;
414 if (state->polarity != PWM_POLARITY_NORMAL)
417 if (!state->enabled) {
419 rz_mtu3_pwm_disable(rz_mtu3_pwm, pwm);
424 mutex_lock(&rz_mtu3_pwm->lock);
425 ret = rz_mtu3_pwm_config(chip, pwm, state);
426 mutex_unlock(&rz_mtu3_pwm->lock);
431 ret = rz_mtu3_pwm_enable(rz_mtu3_pwm, pwm);
436 static const struct pwm_ops rz_mtu3_pwm_ops = {
437 .request = rz_mtu3_pwm_request,
438 .free = rz_mtu3_pwm_free,
439 .get_state = rz_mtu3_pwm_get_state,
440 .apply = rz_mtu3_pwm_apply,
441 .owner = THIS_MODULE,
444 static int rz_mtu3_pwm_pm_runtime_suspend(struct device *dev)
446 struct rz_mtu3_pwm_chip *rz_mtu3_pwm = dev_get_drvdata(dev);
448 clk_disable_unprepare(rz_mtu3_pwm->clk);
453 static int rz_mtu3_pwm_pm_runtime_resume(struct device *dev)
455 struct rz_mtu3_pwm_chip *rz_mtu3_pwm = dev_get_drvdata(dev);
457 return clk_prepare_enable(rz_mtu3_pwm->clk);
460 static DEFINE_RUNTIME_DEV_PM_OPS(rz_mtu3_pwm_pm_ops,
461 rz_mtu3_pwm_pm_runtime_suspend,
462 rz_mtu3_pwm_pm_runtime_resume, NULL);
464 static void rz_mtu3_pwm_pm_disable(void *data)
466 struct rz_mtu3_pwm_chip *rz_mtu3_pwm = data;
468 clk_rate_exclusive_put(rz_mtu3_pwm->clk);
469 pm_runtime_disable(rz_mtu3_pwm->chip.dev);
470 pm_runtime_set_suspended(rz_mtu3_pwm->chip.dev);
473 static int rz_mtu3_pwm_probe(struct platform_device *pdev)
475 struct rz_mtu3 *parent_ddata = dev_get_drvdata(pdev->dev.parent);
476 struct rz_mtu3_pwm_chip *rz_mtu3_pwm;
477 struct device *dev = &pdev->dev;
478 unsigned int i, j = 0;
481 rz_mtu3_pwm = devm_kzalloc(&pdev->dev, sizeof(*rz_mtu3_pwm), GFP_KERNEL);
485 rz_mtu3_pwm->clk = parent_ddata->clk;
487 for (i = 0; i < RZ_MTU_NUM_CHANNELS; i++) {
488 if (i == RZ_MTU3_CHAN_5 || i == RZ_MTU3_CHAN_8)
491 rz_mtu3_pwm->channel_data[j].mtu = &parent_ddata->channels[i];
492 rz_mtu3_pwm->channel_data[j].mtu->dev = dev;
493 rz_mtu3_pwm->channel_data[j].map = &channel_map[j];
497 mutex_init(&rz_mtu3_pwm->lock);
498 platform_set_drvdata(pdev, rz_mtu3_pwm);
499 ret = clk_prepare_enable(rz_mtu3_pwm->clk);
501 return dev_err_probe(dev, ret, "Clock enable failed\n");
503 clk_rate_exclusive_get(rz_mtu3_pwm->clk);
505 rz_mtu3_pwm->rate = clk_get_rate(rz_mtu3_pwm->clk);
507 * Refuse clk rates > 1 GHz to prevent overflow later for computing
508 * period and duty cycle.
510 if (rz_mtu3_pwm->rate > NSEC_PER_SEC) {
512 clk_rate_exclusive_put(rz_mtu3_pwm->clk);
516 pm_runtime_set_active(&pdev->dev);
517 pm_runtime_enable(&pdev->dev);
518 rz_mtu3_pwm->chip.dev = &pdev->dev;
519 ret = devm_add_action_or_reset(&pdev->dev, rz_mtu3_pwm_pm_disable,
524 rz_mtu3_pwm->chip.ops = &rz_mtu3_pwm_ops;
525 rz_mtu3_pwm->chip.npwm = RZ_MTU3_MAX_PWM_CHANNELS;
526 ret = devm_pwmchip_add(&pdev->dev, &rz_mtu3_pwm->chip);
528 return dev_err_probe(&pdev->dev, ret, "failed to add PWM chip\n");
530 pm_runtime_idle(&pdev->dev);
535 clk_disable_unprepare(rz_mtu3_pwm->clk);
539 static struct platform_driver rz_mtu3_pwm_driver = {
541 .name = "pwm-rz-mtu3",
542 .pm = pm_ptr(&rz_mtu3_pwm_pm_ops),
544 .probe = rz_mtu3_pwm_probe,
546 module_platform_driver(rz_mtu3_pwm_driver);
548 MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
549 MODULE_ALIAS("platform:pwm-rz-mtu3");
550 MODULE_DESCRIPTION("Renesas RZ/G2L MTU3a PWM Timer Driver");
551 MODULE_LICENSE("GPL");