blob: de19a551352f50d7f1bef6ad9395843382ca8482 [file] [log] [blame]
Julius Werner7a757c92014-09-10 19:37:15 -07001/*
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
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <assert.h>
21#include <console/console.h>
Julius Werner7a453eb2014-10-20 13:14:55 -070022#include <delay.h>
Julius Werner7a757c92014-09-10 19:37:15 -070023#include <device/i2c.h>
Julius Werner7a453eb2014-10-20 13:14:55 -070024#include <soc/rk808.h>
Julius Werner7a757c92014-09-10 19:37:15 -070025#include <stdint.h>
26#include <stdlib.h>
Julius Werner7a757c92014-09-10 19:37:15 -070027
David Hendricks4d244212015-01-12 13:13:30 -080028#if CONFIG_PMIC_BUS < 0
29#error "PMIC_BUS must be set in mainboard's Kconfig."
30#endif
Julius Werner7a757c92014-09-10 19:37:15 -070031
David Hendricks4d244212015-01-12 13:13:30 -080032#define RK808_ADDR 0x1b
Julius Werner7a757c92014-09-10 19:37:15 -070033
David Hendricks4d244212015-01-12 13:13:30 -080034#define DCDC_EN 0x23
35#define LDO_EN 0x24
36#define BUCK1SEL 0x2f
37#define BUCK4SEL 0x38
38#define LDO_ONSEL(i) (0x39 + 2 * i)
39#define LDO_SLPSEL(i) (0x3a + 2 * i)
40
41static int rk808_read(uint8_t reg, uint8_t *value)
42{
43 return i2c_readb(CONFIG_PMIC_BUS, RK808_ADDR, reg, value);
44}
45
46static int rk808_write(uint8_t reg, uint8_t value)
47{
48 return i2c_writeb(CONFIG_PMIC_BUS, RK808_ADDR, reg, value);
49}
50
51static void rk808_clrsetbits(uint8_t reg, uint8_t clr, uint8_t set)
Julius Werner7a757c92014-09-10 19:37:15 -070052{
53 uint8_t value;
54
David Hendricks4d244212015-01-12 13:13:30 -080055 if (rk808_read(reg, &value) || rk808_write(reg, (value & ~clr) | set))
Julius Werner7a757c92014-09-10 19:37:15 -070056 printk(BIOS_ERR, "ERROR: Cannot set Rk808[%#x]!\n", reg);
57}
58
David Hendricks4d244212015-01-12 13:13:30 -080059void rk808_configure_switch(int sw, int enabled)
Julius Werner8f3883d2014-09-26 21:01:08 -070060{
61 assert(sw == 1 || sw == 2);
David Hendricks4d244212015-01-12 13:13:30 -080062 rk808_clrsetbits(DCDC_EN, 1 << (sw + 4), !!enabled << (sw + 4));
Julius Werner8f3883d2014-09-26 21:01:08 -070063}
64
David Hendricks4d244212015-01-12 13:13:30 -080065void rk808_configure_ldo(int ldo, int millivolts)
Julius Werner7a757c92014-09-10 19:37:15 -070066{
67 uint8_t vsel;
68
Julius Wernerdbfa9d52014-12-05 17:29:42 -080069 if (!millivolts) {
David Hendricks4d244212015-01-12 13:13:30 -080070 rk808_clrsetbits(LDO_EN, 1 << (ldo - 1), 0);
Julius Wernerdbfa9d52014-12-05 17:29:42 -080071 return;
72 }
73
Julius Werner7a757c92014-09-10 19:37:15 -070074 switch (ldo) {
75 case 1:
76 case 2:
77 case 4:
78 case 5:
79 case 8:
huang lin08884e32014-10-10 20:28:47 -070080 vsel = div_round_up(millivolts, 100) - 18;
Julius Werneref639b22014-11-06 14:33:12 -080081 assert(vsel <= 0x10);
Julius Werner7a757c92014-09-10 19:37:15 -070082 break;
83 case 3:
84 case 6:
85 case 7:
huang lin08884e32014-10-10 20:28:47 -070086 vsel = div_round_up(millivolts, 100) - 8;
Julius Werneref639b22014-11-06 14:33:12 -080087 assert(vsel <= 0x11);
Julius Werner7a757c92014-09-10 19:37:15 -070088 break;
89 default:
90 die("Unknown LDO index!");
91 }
Julius Werner7a757c92014-09-10 19:37:15 -070092
David Hendricks4d244212015-01-12 13:13:30 -080093 rk808_clrsetbits(LDO_ONSEL(ldo), 0x1f, vsel);
94 rk808_clrsetbits(LDO_EN, 0, 1 << (ldo - 1));
Julius Werner7a757c92014-09-10 19:37:15 -070095}
huang lin08884e32014-10-10 20:28:47 -070096
David Hendricks4d244212015-01-12 13:13:30 -080097void rk808_configure_buck(int buck, int millivolts)
huang lin08884e32014-10-10 20:28:47 -070098{
99 uint8_t vsel;
100 uint8_t buck_reg;
101
102 switch (buck) {
103 case 1:
104 case 2:
105 /*base on 725mv, use 25mv step */
106 vsel = (div_round_up(millivolts, 25) - 29) * 2 + 1;
107 assert(vsel <= 0x3f);
108 buck_reg = BUCK1SEL + 4 * (buck - 1);
109 break;
110 case 4:
111 vsel = div_round_up(millivolts, 100) - 18;
112 assert(vsel <= 0xf);
113 buck_reg = BUCK4SEL;
114 break;
115 default:
116 die("fault buck index!");
117 }
David Hendricks4d244212015-01-12 13:13:30 -0800118 rk808_clrsetbits(buck_reg, 0x3f, vsel);
119 rk808_clrsetbits(DCDC_EN, 0, 1 << (buck - 1));
huang lin08884e32014-10-10 20:28:47 -0700120}