blob: 1c38fc1784938b41e67e7b4989f87ced19e40188 [file] [log] [blame]
Simon Glasse1ae0d12012-10-17 13:24:49 +00001/*
Simon Glass1c82c2f2015-04-14 21:03:22 -06002 * Tegra pulse width frequency modulator definitions
Simon Glasse1ae0d12012-10-17 13:24:49 +00003 *
4 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glasse1ae0d12012-10-17 13:24:49 +00005 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Simon Glasse1ae0d12012-10-17 13:24:49 +00007 */
8
9#include <common.h>
10#include <fdtdec.h>
11#include <asm/io.h>
12#include <asm/arch/clock.h>
13#include <asm/arch/pwm.h>
14
15struct pwm_info {
16 struct pwm_ctlr *pwm; /* Registers for our pwm controller */
17 int pwm_node; /* PWM device tree node */
18} local;
19
20void pwm_enable(unsigned channel, int rate, int pulse_width, int freq_divider)
21{
22 u32 reg;
23
24 assert(channel < PWM_NUM_CHANNELS);
25
26 /* TODO: Can we use clock_adjust_periph_pll_div() here? */
Simon Glassdb043782015-04-14 21:03:23 -060027 if (rate) {
28 clock_start_periph_pll(PERIPH_ID_PWM, CLOCK_ID_SFROM32KHZ,
29 rate);
30 }
Simon Glasse1ae0d12012-10-17 13:24:49 +000031
32 reg = PWM_ENABLE_MASK;
33 reg |= pulse_width << PWM_WIDTH_SHIFT;
34 reg |= freq_divider << PWM_DIVIDER_SHIFT;
35 writel(reg, &local.pwm[channel].control);
36 debug("%s: channel=%d, rate=%d\n", __func__, channel, rate);
37}
38
39int pwm_request(const void *blob, int node, const char *prop_name)
40{
41 int pwm_node;
42 u32 data[3];
43
44 if (fdtdec_get_int_array(blob, node, prop_name, data,
45 ARRAY_SIZE(data))) {
46 debug("%s: Cannot decode PWM property '%s'\n", __func__,
47 prop_name);
48 return -1;
49 }
50
51 pwm_node = fdt_node_offset_by_phandle(blob, data[0]);
52 if (pwm_node != local.pwm_node) {
53 debug("%s: PWM property '%s' phandle %d not recognised"
54 "- expecting %d\n", __func__, prop_name, data[0],
55 local.pwm_node);
56 return -1;
57 }
58 if (data[1] >= PWM_NUM_CHANNELS) {
59 debug("%s: PWM property '%s': invalid channel %u\n", __func__,
60 prop_name, data[1]);
61 return -1;
62 }
63
64 /*
65 * TODO: We could maintain a list of requests, but it might not be
66 * worth it for U-Boot.
67 */
68 return data[1];
69}
70
71int pwm_init(const void *blob)
72{
73 local.pwm_node = fdtdec_next_compatible(blob, 0,
74 COMPAT_NVIDIA_TEGRA20_PWM);
75 if (local.pwm_node < 0) {
76 debug("%s: Cannot find device tree node\n", __func__);
77 return -1;
78 }
79
80 local.pwm = (struct pwm_ctlr *)fdtdec_get_addr(blob, local.pwm_node,
81 "reg");
82 if (local.pwm == (struct pwm_ctlr *)FDT_ADDR_T_NONE) {
83 debug("%s: Cannot find pwm reg address\n", __func__);
84 return -1;
85 }
86 debug("Tegra PWM at %p, node %d\n", local.pwm, local.pwm_node);
87
88 return 0;
89}