Simon Glass | e1ae0d1 | 2012-10-17 13:24:49 +0000 | [diff] [blame] | 1 | /* |
Simon Glass | 1c82c2f | 2015-04-14 21:03:22 -0600 | [diff] [blame] | 2 | * Tegra pulse width frequency modulator definitions |
Simon Glass | e1ae0d1 | 2012-10-17 13:24:49 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | e1ae0d1 | 2012-10-17 13:24:49 +0000 | [diff] [blame] | 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e1ae0d1 | 2012-10-17 13:24:49 +0000 | [diff] [blame] | 7 | */ |
| 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 | |
| 15 | struct pwm_info { |
| 16 | struct pwm_ctlr *pwm; /* Registers for our pwm controller */ |
| 17 | int pwm_node; /* PWM device tree node */ |
| 18 | } local; |
| 19 | |
| 20 | void 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 Glass | db04378 | 2015-04-14 21:03:23 -0600 | [diff] [blame] | 27 | if (rate) { |
| 28 | clock_start_periph_pll(PERIPH_ID_PWM, CLOCK_ID_SFROM32KHZ, |
| 29 | rate); |
| 30 | } |
Simon Glass | e1ae0d1 | 2012-10-17 13:24:49 +0000 | [diff] [blame] | 31 | |
| 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 | |
| 39 | int 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 | |
| 71 | int 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 | } |