blob: 0d629ca04186a6fbf26b7c84ddd659397d1c889b [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>
22#include <device/i2c.h>
23#include <stdint.h>
24#include <stdlib.h>
25#include "rk808.h"
26
27#define RK808_ADDR 0x1b
28
29#define LDO_EN 0x24
30#define LDO_ONSEL(i) (0x39 + 2 * i)
31#define LDO_SLPSEL(i) (0x3a + 2 * i)
32
33static void rk808_clrsetbits(uint8_t bus, uint8_t reg, uint8_t clr, uint8_t set)
34{
35 uint8_t value;
36
37 if (i2c_readb(bus, RK808_ADDR, reg, &value) ||
38 i2c_writeb(bus, RK808_ADDR, reg, (value & ~clr) | set))
39 printk(BIOS_ERR, "ERROR: Cannot set Rk808[%#x]!\n", reg);
40}
41
42void rk808_configure_ldo(uint8_t bus, int ldo, int millivolts)
43{
44 uint8_t vsel;
45
46 switch (ldo) {
47 case 1:
48 case 2:
49 case 4:
50 case 5:
51 case 8:
52 vsel = millivolts / 100 - 18;
53 break;
54 case 3:
55 case 6:
56 case 7:
57 vsel = millivolts / 100 - 8;
58 break;
59 default:
60 die("Unknown LDO index!");
61 }
62 assert(vsel <= 0x10);
63
64 rk808_clrsetbits(bus, LDO_ONSEL(ldo), 0x1f, vsel);
65 rk808_clrsetbits(bus, LDO_EN, 0, 1 << (ldo - 1));
66}