]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/tx6/rn5t567.c
karo: tx6: add support for TX6 HW Rev. 3
[karo-tx-uboot.git] / board / karo / tx6 / rn5t567.c
1 /*
2  * Copyright (C) 2014 Lothar Waßmann <LW@KARO-electronics.de>
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 #include <common.h>
19 #include <i2c.h>
20
21 #include "pmic.h"
22
23 #define RN5T567_NOETIMSET       0x11
24 #define RN5T567_LDORTC1_SLOT    0x2a
25 #define RN5T567_DC1CTL          0x2c
26 #define RN5T567_DC1CTL2         0x2d
27 #define RN5T567_DC2CTL          0x2e
28 #define RN5T567_DC2CTL2         0x2f
29 #define RN5T567_DC3CTL          0x30
30 #define RN5T567_DC3CTL2         0x31
31 #define RN5T567_DC1DAC          0x36 /* CORE */
32 #define RN5T567_DC2DAC          0x37 /* SOC */
33 #define RN5T567_DC3DAC          0x38 /* DDR */
34 #define RN5T567_DC1DAC_SLP      0x3b
35 #define RN5T567_DC2DAC_SLP      0x3c
36 #define RN5T567_DC3DAC_SLP      0x3d
37 #define RN5T567_LDOEN1          0x44
38 #define RN5T567_LDODIS          0x46
39 #define RN5T567_LDOEN2          0x48
40 #define RN5T567_LDO3DAC         0x4e /* IO */
41 #define RN5T567_LDORTC1DAC      0x56 /* VBACKUP */
42
43 #define NOETIMSET_DIS_OFF_NOE_TIM       (1 << 3)
44
45 #define VDD_RTC_VAL             mV_to_regval_rtc(3000 * 10)
46 #define VDD_HIGH_VAL            mV_to_regval3(3000 * 10)
47 #define VDD_HIGH_VAL_LP         mV_to_regval3(3000 * 10)
48 #define VDD_CORE_VAL            mV_to_regval(1425 * 10)
49 #define VDD_CORE_VAL_LP         mV_to_regval(900 * 10)
50 #define VDD_SOC_VAL             mV_to_regval(1425 * 10)
51 #define VDD_SOC_VAL_LP          mV_to_regval(900 * 10)
52 #define VDD_DDR_VAL             mV_to_regval(1500 * 10)
53 #define VDD_DDR_VAL_LP          mV_to_regval(1500 * 10)
54
55 /* calculate voltages in 10mV */
56 /* DCDC1-3 */
57 #define mV_to_regval(mV)        DIV_ROUND(((((mV) < 6000) ? 6000 : (mV)) - 6000), 125)
58 #define regval_to_mV(v)         (((v) * 125 + 6000))
59
60 /* LDO1-2 */
61 #define mV_to_regval2(mV)       DIV_ROUND(((((mV) < 9000) ? 9000 : (mV)) - 9000), 250)
62 #define regval2_to_mV(v)        (((v) * 250 + 9000))
63
64 /* LDO3 */
65 #define mV_to_regval3(mV)       DIV_ROUND(((((mV) < 6000) ? 6000 : (mV)) - 6000), 250)
66 #define regval3_to_mV(v)        (((v) * 250 + 6000))
67
68 /* LDORTC */
69 #define mV_to_regval_rtc(mV)    DIV_ROUND(((((mV) < 17000) ? 17000 : (mV)) - 17000), 250)
70 #define regval_rtc_to_mV(v)     (((v) * 250 + 17000))
71
72 static struct rn5t567_regs {
73         u8 addr;
74         u8 val;
75         u8 mask;
76 } rn5t567_regs[] = {
77         { RN5T567_NOETIMSET, NOETIMSET_DIS_OFF_NOE_TIM | 0x5, },
78 #if 0
79         { RN5T567_DC1DAC, VDD_CORE_VAL, },
80         { RN5T567_DC2DAC, VDD_SOC_VAL, },
81         { RN5T567_DC3DAC, VDD_DDR_VAL, },
82         { RN5T567_DC1DAC_SLP, VDD_CORE_VAL_LP, },
83         { RN5T567_DC2DAC_SLP, VDD_SOC_VAL_LP, },
84         { RN5T567_DC3DAC_SLP, VDD_DDR_VAL_LP, },
85         { RN5T567_LDOEN1, 0x01f, ~0x1f, },
86         { RN5T567_LDOEN2, 0x10, ~0x30, },
87         { RN5T567_LDODIS, 0x00, },
88         { RN5T567_LDO3DAC, VDD_HIGH_VAL, },
89         { RN5T567_LDORTCDAC, VDD_RTC_VAL, },
90         { RN5T567_LDORTC1_SLOT, 0x0f, ~0x3f, },
91 #endif
92 };
93
94 static struct rn5t567_regs debug_regs[] __maybe_unused = {
95         { 0x00,  4, },
96         { 0x09,  4, },
97         { 0x10, 16, },
98         { 0x25, 26, },
99         { 0x44,  3, },
100         { 0x4c,  5, },
101         { 0x56,  1, },
102         { 0x58,  5, },
103         { 0x97,  2, },
104         { 0xb0,  1, },
105         { 0xbc,  1, },
106 };
107
108 static int rn5t567_setup_regs(struct rn5t567_regs *r, size_t count)
109 {
110         int ret;
111         int i;
112
113         for (i = 0; i < count; i++, r++) {
114 #ifdef DEBUG
115                 unsigned char value;
116
117                 ret = i2c_read(CONFIG_SYS_I2C_SLAVE, r->addr, 1, &value, 1);
118                 if ((value & ~r->mask) != r->val) {
119                         printf("Changing PMIC reg %02x from %02x to %02x\n",
120                                 r->addr, value, r->val);
121                 }
122                 if (ret) {
123                         printf("%s: failed to read PMIC register %02x: %d\n",
124                                 __func__, r->addr, ret);
125                         return ret;
126                 }
127 #endif
128 //              value = (value & ~r->mask) | r->val;
129                 ret = i2c_write(CONFIG_SYS_I2C_SLAVE,
130                                 r->addr, 1, &r->val, 1);
131                 if (ret) {
132                         printf("%s: failed to write PMIC register %02x: %d\n",
133                                 __func__, r->addr, ret);
134                         return ret;
135                 }
136 #ifdef DEBUG
137                 ret = i2c_read(CONFIG_SYS_I2C_SLAVE, r->addr, 1, &value, 1);
138                 printf("PMIC reg %02x is %02x\n", r->addr, value);
139 #endif
140         }
141 #if 0
142         for (i = 0; i < ARRAY_SIZE(debug_regs); i++) {
143                 int j;
144
145                 r = &debug_regs[i];
146                 for (j = r->addr; j < r->addr + r->val; j++) {
147                         unsigned char value;
148
149                         ret = i2c_read(CONFIG_SYS_I2C_SLAVE, j, 1, &value, 1);
150                         printf("PMIC reg %02x = %02x\n",
151                                 j, value);
152                 }
153         }
154 #endif
155         debug("%s() complete\n", __func__);
156         return 0;
157 }
158
159 int setup_pmic_voltages(void)
160 {
161         int ret;
162         unsigned char value;
163
164         ret = i2c_probe(CONFIG_SYS_I2C_SLAVE);
165         if (ret != 0) {
166                 printf("Failed to initialize I2C\n");
167                 return ret;
168         }
169
170         ret = i2c_read(CONFIG_SYS_I2C_SLAVE, 0x11, 1, &value, 1);
171         if (ret) {
172                 printf("%s: i2c_read error: %d\n", __func__, ret);
173                 return ret;
174         }
175
176         ret = rn5t567_setup_regs(rn5t567_regs, ARRAY_SIZE(rn5t567_regs));
177         if (ret)
178                 return ret;
179
180         printf("VDDCORE set to %umV\n",
181                 DIV_ROUND(regval_to_mV(VDD_CORE_VAL), 10));
182         printf("VDDSOC  set to %umV\n",
183                 DIV_ROUND(regval_to_mV(VDD_SOC_VAL), 10));
184
185         return ret;
186 }