Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Julius Werner | 8f3883d | 2014-09-26 21:01:08 -0700 | [diff] [blame^] | 29 | #define DCDC_EN 0x23 |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 30 | #define LDO_EN 0x24 |
| 31 | #define LDO_ONSEL(i) (0x39 + 2 * i) |
| 32 | #define LDO_SLPSEL(i) (0x3a + 2 * i) |
| 33 | |
| 34 | static void rk808_clrsetbits(uint8_t bus, uint8_t reg, uint8_t clr, uint8_t set) |
| 35 | { |
| 36 | uint8_t value; |
| 37 | |
| 38 | if (i2c_readb(bus, RK808_ADDR, reg, &value) || |
| 39 | i2c_writeb(bus, RK808_ADDR, reg, (value & ~clr) | set)) |
| 40 | printk(BIOS_ERR, "ERROR: Cannot set Rk808[%#x]!\n", reg); |
| 41 | } |
| 42 | |
Julius Werner | 8f3883d | 2014-09-26 21:01:08 -0700 | [diff] [blame^] | 43 | void rk808_configure_switch(uint8_t bus, int sw, int enabled) |
| 44 | { |
| 45 | assert(sw == 1 || sw == 2); |
| 46 | rk808_clrsetbits(bus, DCDC_EN, 1 << (sw + 4), !!enabled << (sw + 4)); |
| 47 | } |
| 48 | |
Julius Werner | 7a757c9 | 2014-09-10 19:37:15 -0700 | [diff] [blame] | 49 | void rk808_configure_ldo(uint8_t bus, int ldo, int millivolts) |
| 50 | { |
| 51 | uint8_t vsel; |
| 52 | |
| 53 | switch (ldo) { |
| 54 | case 1: |
| 55 | case 2: |
| 56 | case 4: |
| 57 | case 5: |
| 58 | case 8: |
| 59 | vsel = millivolts / 100 - 18; |
| 60 | break; |
| 61 | case 3: |
| 62 | case 6: |
| 63 | case 7: |
| 64 | vsel = millivolts / 100 - 8; |
| 65 | break; |
| 66 | default: |
| 67 | die("Unknown LDO index!"); |
| 68 | } |
| 69 | assert(vsel <= 0x10); |
| 70 | |
| 71 | rk808_clrsetbits(bus, LDO_ONSEL(ldo), 0x1f, vsel); |
| 72 | rk808_clrsetbits(bus, LDO_EN, 0, 1 << (ldo - 1)); |
| 73 | } |