blob: e3f1417f2ad050f295d20743ce134fb0de4822c6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass97f6c522016-01-30 16:37:46 -07002/*
3 * Copyright 2016 Google Inc.
Simon Glass97f6c522016-01-30 16:37:46 -07004 */
5
Simon Glass97f6c522016-01-30 16:37:46 -07006#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Simon Glass97f6c522016-01-30 16:37:46 -07008#include <pwm.h>
9#include <asm/io.h>
10#include <asm/arch/clock.h>
11#include <asm/arch/pwm.h>
12
Simon Glass97f6c522016-01-30 16:37:46 -070013struct tegra_pwm_priv {
14 struct pwm_ctlr *regs;
15};
16
17static int tegra_pwm_set_config(struct udevice *dev, uint channel,
18 uint period_ns, uint duty_ns)
19{
20 struct tegra_pwm_priv *priv = dev_get_priv(dev);
21 struct pwm_ctlr *regs = priv->regs;
Svyatoslav Ryhelc226fc72023-02-14 19:35:28 +020022 const u32 pwm_max_freq = dev_get_driver_data(dev);
Simon Glass97f6c522016-01-30 16:37:46 -070023 uint pulse_width;
24 u32 reg;
25
26 if (channel >= 4)
27 return -EINVAL;
28 debug("%s: Configure '%s' channel %u\n", __func__, dev->name, channel);
Svyatoslav Ryhelc226fc72023-02-14 19:35:28 +020029
30 clock_start_periph_pll(PERIPH_ID_PWM, CLOCK_ID_PERIPH, pwm_max_freq);
Simon Glass97f6c522016-01-30 16:37:46 -070031
32 pulse_width = duty_ns * 255 / period_ns;
33
34 reg = pulse_width << PWM_WIDTH_SHIFT;
35 reg |= 1 << PWM_DIVIDER_SHIFT;
Svyatoslav Ryhelc226fc72023-02-14 19:35:28 +020036 reg |= PWM_ENABLE_MASK;
Simon Glass97f6c522016-01-30 16:37:46 -070037 writel(reg, &regs[channel].control);
38 debug("%s: pulse_width=%u\n", __func__, pulse_width);
39
40 return 0;
41}
42
43static int tegra_pwm_set_enable(struct udevice *dev, uint channel, bool enable)
44{
45 struct tegra_pwm_priv *priv = dev_get_priv(dev);
46 struct pwm_ctlr *regs = priv->regs;
47
48 if (channel >= 4)
49 return -EINVAL;
50 debug("%s: Enable '%s' channel %u\n", __func__, dev->name, channel);
51 clrsetbits_le32(&regs[channel].control, PWM_ENABLE_MASK,
52 enable ? PWM_ENABLE_MASK : 0);
53
54 return 0;
55}
56
Simon Glassaad29ae2020-12-03 16:55:21 -070057static int tegra_pwm_of_to_plat(struct udevice *dev)
Simon Glass97f6c522016-01-30 16:37:46 -070058{
59 struct tegra_pwm_priv *priv = dev_get_priv(dev);
60
Johan Jonker8d5d8e02023-03-13 01:32:04 +010061 priv->regs = dev_read_addr_ptr(dev);
Simon Glass97f6c522016-01-30 16:37:46 -070062
63 return 0;
64}
65
66static const struct pwm_ops tegra_pwm_ops = {
67 .set_config = tegra_pwm_set_config,
68 .set_enable = tegra_pwm_set_enable,
69};
70
71static const struct udevice_id tegra_pwm_ids[] = {
Svyatoslav Ryhelc226fc72023-02-14 19:35:28 +020072 { .compatible = "nvidia,tegra20-pwm", .data = 48 * 1000000 },
73 { .compatible = "nvidia,tegra114-pwm", .data = 408 * 1000000 },
Simon Glass97f6c522016-01-30 16:37:46 -070074 { }
75};
76
77U_BOOT_DRIVER(tegra_pwm) = {
78 .name = "tegra_pwm",
79 .id = UCLASS_PWM,
80 .of_match = tegra_pwm_ids,
81 .ops = &tegra_pwm_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -070082 .of_to_plat = tegra_pwm_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -070083 .priv_auto = sizeof(struct tegra_pwm_priv),
Simon Glass97f6c522016-01-30 16:37:46 -070084};