blob: b655c5cd06f28d748a56b9c91367529d0a61cca5 [file] [log] [blame]
Simon Glass1564f342012-10-17 13:24:49 +00001/*
2 * Tegra2 pulse width frequency modulator definitions
3 *
4 * Copyright (c) 2011 The Chromium OS Authors.
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <fdtdec.h>
26#include <asm/io.h>
27#include <asm/arch/clock.h>
28#include <asm/arch/pwm.h>
29
30struct pwm_info {
31 struct pwm_ctlr *pwm; /* Registers for our pwm controller */
32 int pwm_node; /* PWM device tree node */
33} local;
34
35void pwm_enable(unsigned channel, int rate, int pulse_width, int freq_divider)
36{
37 u32 reg;
38
39 assert(channel < PWM_NUM_CHANNELS);
40
41 /* TODO: Can we use clock_adjust_periph_pll_div() here? */
42 clock_start_periph_pll(PERIPH_ID_PWM, CLOCK_ID_SFROM32KHZ, rate);
43
44 reg = PWM_ENABLE_MASK;
45 reg |= pulse_width << PWM_WIDTH_SHIFT;
46 reg |= freq_divider << PWM_DIVIDER_SHIFT;
47 writel(reg, &local.pwm[channel].control);
48 debug("%s: channel=%d, rate=%d\n", __func__, channel, rate);
49}
50
51int pwm_request(const void *blob, int node, const char *prop_name)
52{
53 int pwm_node;
54 u32 data[3];
55
56 if (fdtdec_get_int_array(blob, node, prop_name, data,
57 ARRAY_SIZE(data))) {
58 debug("%s: Cannot decode PWM property '%s'\n", __func__,
59 prop_name);
60 return -1;
61 }
62
63 pwm_node = fdt_node_offset_by_phandle(blob, data[0]);
64 if (pwm_node != local.pwm_node) {
65 debug("%s: PWM property '%s' phandle %d not recognised"
66 "- expecting %d\n", __func__, prop_name, data[0],
67 local.pwm_node);
68 return -1;
69 }
70 if (data[1] >= PWM_NUM_CHANNELS) {
71 debug("%s: PWM property '%s': invalid channel %u\n", __func__,
72 prop_name, data[1]);
73 return -1;
74 }
75
76 /*
77 * TODO: We could maintain a list of requests, but it might not be
78 * worth it for U-Boot.
79 */
80 return data[1];
81}
82
83int pwm_init(const void *blob)
84{
85 local.pwm_node = fdtdec_next_compatible(blob, 0,
86 COMPAT_NVIDIA_TEGRA20_PWM);
87 if (local.pwm_node < 0) {
88 debug("%s: Cannot find device tree node\n", __func__);
89 return -1;
90 }
91
92 local.pwm = (struct pwm_ctlr *)fdtdec_get_addr(blob, local.pwm_node,
93 "reg");
94 if (local.pwm == (struct pwm_ctlr *)FDT_ADDR_T_NONE) {
95 debug("%s: Cannot find pwm reg address\n", __func__);
96 return -1;
97 }
98 debug("Tegra PWM at %p, node %d\n", local.pwm, local.pwm_node);
99
100 return 0;
101}