blob: 2510cd7689e7dc36ae9d97a792259546981abeae [file] [log] [blame]
huang linbfdd7322014-09-25 16:33:38 +08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2014 Rockchip Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
huang linbfdd7322014-09-25 16:33:38 +080018 */
19
huang linbfdd7322014-09-25 16:33:38 +080020#include <arch/io.h>
huang linbfdd7322014-09-25 16:33:38 +080021#include <assert.h>
Julius Werner7a453eb2014-10-20 13:14:55 -070022#include <console/console.h>
huang linbfdd7322014-09-25 16:33:38 +080023#include <delay.h>
Julius Werner7a453eb2014-10-20 13:14:55 -070024#include <soc/addressmap.h>
25#include <soc/grf.h>
26#include <soc/soc.h>
27#include <soc/pwm.h>
28#include <soc/clock.h>
29#include <stdlib.h>
30#include <timer.h>
huang linbfdd7322014-09-25 16:33:38 +080031
32struct pwm_ctl {
33 u32 pwm_cnt;
34 u32 pwm_period_hpr;
35 u32 pwm_duty_lpr;
36 u32 pwm_ctrl;
37};
38
39struct rk3288_pwm_regs {
40 struct pwm_ctl pwm[4];
41 u32 intsts;
42 u32 int_en;
43};
44check_member(rk3288_pwm_regs, int_en, 0x44);
45
46#define RK_PWM_DISABLE (0 << 0)
47#define RK_PWM_ENABLE (1 << 0)
48
49
50#define PWM_ONE_SHOT (0 << 1)
51#define PWM_CONTINUOUS (1 << 1)
52#define RK_PWM_CAPTURE (1 << 2)
53
54#define PWM_DUTY_POSTIVE (1 << 3)
55#define PWM_DUTY_NEGATIVE (0 << 3)
56
57#define PWM_INACTIVE_POSTIVE (1 << 4)
58#define PWM_INACTIVE_NEGATIVE (0 << 4)
59
60#define PWM_OUTPUT_LEFT (0 << 5)
61#define PWM_OUTPUT_CENTER (1 << 5)
62
63#define PWM_LP_ENABLE (1 << 8)
64#define PWM_LP_DISABLE (0 << 8)
65
66#define PWM_SEL_SCALE_CLK (1 << 9)
67#define PWM_SEL_SRC_CLK (0 << 9)
68
69struct rk3288_pwm_regs *rk3288_pwm = (void *)RK_PWM0123_BASE;
70
71void pwm_init(u32 id, u32 period_ns, u32 duty_ns)
72{
73 unsigned long period, duty;
74
75 /*use rk pwm*/
Julius Werner2f37bd62015-02-19 14:51:15 -080076 write32(&rk3288_grf->soc_con2, RK_SETBITS(1 << 0));
huang linbfdd7322014-09-25 16:33:38 +080077
Julius Werner94184762015-02-19 20:19:23 -080078 write32(&rk3288_pwm->pwm[id].pwm_ctrl, PWM_SEL_SRC_CLK |
79 PWM_OUTPUT_LEFT | PWM_LP_DISABLE | PWM_CONTINUOUS |
80 PWM_DUTY_POSTIVE | PWM_INACTIVE_POSTIVE | RK_PWM_DISABLE);
huang linbfdd7322014-09-25 16:33:38 +080081
82 period = (PD_BUS_PCLK_HZ / 1000) * period_ns / USECS_PER_SEC;
83 duty = (PD_BUS_PCLK_HZ / 1000) * duty_ns / USECS_PER_SEC;
84
Julius Werner2f37bd62015-02-19 14:51:15 -080085 write32(&rk3288_pwm->pwm[id].pwm_period_hpr, period);
86 write32(&rk3288_pwm->pwm[id].pwm_duty_lpr, duty);
huang linbfdd7322014-09-25 16:33:38 +080087 setbits_le32(&rk3288_pwm->pwm[id].pwm_ctrl, RK_PWM_ENABLE);
88}