blob: 87034706060c306704a17d7c9f7b543f7237776c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass7429b962016-01-30 16:37:46 -07002/*
3 * Copyright 2016 Google Inc.
Simon Glass7429b962016-01-30 16:37:46 -07004 */
5
6#include <common.h>
7#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Simon Glass7429b962016-01-30 16:37:46 -07009#include <pwm.h>
10#include <asm/io.h>
11#include <asm/arch/clock.h>
12#include <asm/arch/pwm.h>
13
Simon Glass7429b962016-01-30 16:37:46 -070014struct tegra_pwm_priv {
15 struct pwm_ctlr *regs;
16};
17
18static int tegra_pwm_set_config(struct udevice *dev, uint channel,
19 uint period_ns, uint duty_ns)
20{
21 struct tegra_pwm_priv *priv = dev_get_priv(dev);
22 struct pwm_ctlr *regs = priv->regs;
Svyatoslav Ryhel23d24df2023-02-14 19:35:28 +020023 const u32 pwm_max_freq = dev_get_driver_data(dev);
Simon Glass7429b962016-01-30 16:37:46 -070024 uint pulse_width;
25 u32 reg;
26
27 if (channel >= 4)
28 return -EINVAL;
29 debug("%s: Configure '%s' channel %u\n", __func__, dev->name, channel);
Svyatoslav Ryhel23d24df2023-02-14 19:35:28 +020030
31 clock_start_periph_pll(PERIPH_ID_PWM, CLOCK_ID_PERIPH, pwm_max_freq);
Simon Glass7429b962016-01-30 16:37:46 -070032
33 pulse_width = duty_ns * 255 / period_ns;
34
35 reg = pulse_width << PWM_WIDTH_SHIFT;
36 reg |= 1 << PWM_DIVIDER_SHIFT;
Svyatoslav Ryhel23d24df2023-02-14 19:35:28 +020037 reg |= PWM_ENABLE_MASK;
Simon Glass7429b962016-01-30 16:37:46 -070038 writel(reg, &regs[channel].control);
39 debug("%s: pulse_width=%u\n", __func__, pulse_width);
40
41 return 0;
42}
43
44static int tegra_pwm_set_enable(struct udevice *dev, uint channel, bool enable)
45{
46 struct tegra_pwm_priv *priv = dev_get_priv(dev);
47 struct pwm_ctlr *regs = priv->regs;
48
49 if (channel >= 4)
50 return -EINVAL;
51 debug("%s: Enable '%s' channel %u\n", __func__, dev->name, channel);
52 clrsetbits_le32(&regs[channel].control, PWM_ENABLE_MASK,
53 enable ? PWM_ENABLE_MASK : 0);
54
55 return 0;
56}
57
Simon Glassd1998a92020-12-03 16:55:21 -070058static int tegra_pwm_of_to_plat(struct udevice *dev)
Simon Glass7429b962016-01-30 16:37:46 -070059{
60 struct tegra_pwm_priv *priv = dev_get_priv(dev);
61
Johan Jonkera12a73b2023-03-13 01:32:04 +010062 priv->regs = dev_read_addr_ptr(dev);
Simon Glass7429b962016-01-30 16:37:46 -070063
64 return 0;
65}
66
67static const struct pwm_ops tegra_pwm_ops = {
68 .set_config = tegra_pwm_set_config,
69 .set_enable = tegra_pwm_set_enable,
70};
71
72static const struct udevice_id tegra_pwm_ids[] = {
Svyatoslav Ryhel23d24df2023-02-14 19:35:28 +020073 { .compatible = "nvidia,tegra20-pwm", .data = 48 * 1000000 },
74 { .compatible = "nvidia,tegra114-pwm", .data = 408 * 1000000 },
Simon Glass7429b962016-01-30 16:37:46 -070075 { }
76};
77
78U_BOOT_DRIVER(tegra_pwm) = {
79 .name = "tegra_pwm",
80 .id = UCLASS_PWM,
81 .of_match = tegra_pwm_ids,
82 .ops = &tegra_pwm_ops,
Simon Glassd1998a92020-12-03 16:55:21 -070083 .of_to_plat = tegra_pwm_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -070084 .priv_auto = sizeof(struct tegra_pwm_priv),
Simon Glass7429b962016-01-30 16:37:46 -070085};