]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/tx6/rn5t618.c
karo: tx6: autodetect HW rev 1 and 3
[karo-tx-uboot.git] / board / karo / tx6 / rn5t618.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 RN5T618_NOETIMSET       0x11
24 #define RN5T618_LDORTC1_SLOT    0x2a
25 #define RN5T618_DC1CTL          0x2c
26 #define RN5T618_DC1CTL2         0x2d
27 #define RN5T618_DC2CTL          0x2e
28 #define RN5T618_DC2CTL2         0x2f
29 #define RN5T618_DC3CTL          0x30
30 #define RN5T618_DC3CTL2         0x31
31 #define RN5T618_DC1DAC          0x36 /* CORE */
32 #define RN5T618_DC2DAC          0x37 /* SOC */
33 #define RN5T618_DC3DAC          0x38 /* DDR */
34 #define RN5T618_DC1DAC_SLP      0x3b
35 #define RN5T618_DC2DAC_SLP      0x3c
36 #define RN5T618_DC3DAC_SLP      0x3d
37 #define RN5T618_LDOEN1          0x44
38 #define RN5T618_LDODIS          0x46
39 #define RN5T618_LDOEN2          0x48
40 #define RN5T618_LDO3DAC         0x4e /* IO */
41 #define RN5T618_LDORTCDAC       0x56 /* VBACKUP */
42
43 #define VDD_RTC_VAL             mV_to_regval_rtc(3000 * 10)
44 #define VDD_HIGH_VAL            mV_to_regval3(3000 * 10)
45 #define VDD_HIGH_VAL_LP         mV_to_regval3(3000 * 10)
46 #define VDD_CORE_VAL            mV_to_regval(1425 * 10)
47 #define VDD_CORE_VAL_LP         mV_to_regval(900 * 10)
48 #define VDD_SOC_VAL             mV_to_regval(1425 * 10)
49 #define VDD_SOC_VAL_LP          mV_to_regval(900 * 10)
50 #define VDD_DDR_VAL             mV_to_regval(1500 * 10)
51 #define VDD_DDR_VAL_LP          mV_to_regval(1500 * 10)
52
53 /* calculate voltages in 10mV */
54 /* DCDC1-3 */
55 #define mV_to_regval(mV)        DIV_ROUND(((((mV) < 6000) ? 6000 : (mV)) - 6000), 125)
56 #define regval_to_mV(v)         (((v) * 125 + 6000))
57
58 /* LDO1-2 */
59 #define mV_to_regval2(mV)       DIV_ROUND(((((mV) < 9000) ? 9000 : (mV)) - 9000), 250)
60 #define regval2_to_mV(v)        (((v) * 250 + 9000))
61
62 /* LDO3 */
63 #define mV_to_regval3(mV)       DIV_ROUND(((((mV) < 6000) ? 6000 : (mV)) - 6000), 250)
64 #define regval3_to_mV(v)        (((v) * 250 + 6000))
65
66 /* LDORTC */
67 #define mV_to_regval_rtc(mV)    DIV_ROUND(((((mV) < 17000) ? 17000 : (mV)) - 17000), 250)
68 #define regval_rtc_to_mV(v)     (((v) * 250 + 17000))
69
70 static struct rn5t618_regs {
71         u8 addr;
72         u8 val;
73         u8 mask;
74 } rn5t618_regs[] = {
75         { RN5T618_NOETIMSET, 0, },
76         { RN5T618_DC1DAC, VDD_CORE_VAL, },
77         { RN5T618_DC2DAC, VDD_SOC_VAL, },
78         { RN5T618_DC3DAC, VDD_DDR_VAL, },
79         { RN5T618_DC1DAC_SLP, VDD_CORE_VAL_LP, },
80         { RN5T618_DC2DAC_SLP, VDD_SOC_VAL_LP, },
81         { RN5T618_DC3DAC_SLP, VDD_DDR_VAL_LP, },
82         { RN5T618_LDOEN1, 0x01f, ~0x1f, },
83         { RN5T618_LDOEN2, 0x10, ~0x30, },
84         { RN5T618_LDODIS, 0x00, },
85         { RN5T618_LDO3DAC, VDD_HIGH_VAL, },
86         { RN5T618_LDORTCDAC, VDD_RTC_VAL, },
87         { RN5T618_LDORTC1_SLOT, 0x0f, ~0x3f, },
88 };
89
90 static int rn5t618_setup_regs(uchar slave_addr, struct rn5t618_regs *r,
91                         size_t count)
92 {
93         int ret;
94         int i;
95
96         for (i = 0; i < count; i++, r++) {
97                 unsigned char value;
98
99                 ret = i2c_read(slave_addr, r->addr, 1, &value, 1);
100                 debug("PMIC reg %02x = %02x\n", r->addr, value);
101         }
102         for (i = 0; i < count; i++, r++) {
103 #ifdef DEBUG
104                 unsigned char value;
105
106                 ret = i2c_read(slave_addr, r->addr, 1, &value, 1);
107                 if ((value & ~r->mask) != r->val) {
108                         printf("Changing PMIC reg %02x from %02x to %02x\n",
109                                 r->addr, value, r->val);
110                 }
111                 if (ret) {
112                         printf("%s: failed to read PMIC register %02x: %d\n",
113                                 __func__, r->addr, ret);
114                         return ret;
115                 }
116 #endif
117                 ret = i2c_write(slave_addr, r->addr, 1, &r->val, 1);
118                 if (ret) {
119                         printf("%s: failed to write PMIC register %02x: %d\n",
120                                 __func__, r->addr, ret);
121                         return ret;
122                 }
123 #ifdef DEBUG
124                 ret = i2c_read(slave_addr, r->addr, 1, &value, 1);
125                 printf("PMIC reg %02x is %02x\n", r->addr, value);
126 #endif
127         }
128         return 0;
129 }
130
131 int rn5t618_pmic_setup(uchar slave_addr)
132 {
133         int ret;
134         unsigned char value;
135
136         ret = i2c_read(slave_addr, 0x11, 1, &value, 1);
137         if (ret) {
138                 printf("%s: i2c_read error: %d\n", __func__, ret);
139                 return ret;
140         }
141
142         ret = rn5t618_setup_regs(slave_addr, rn5t618_regs,
143                                 ARRAY_SIZE(rn5t618_regs));
144         if (ret)
145                 return ret;
146
147         printf("VDDCORE set to %umV\n",
148                 DIV_ROUND(regval_to_mV(VDD_CORE_VAL), 10));
149         printf("VDDSOC  set to %umV\n",
150                 DIV_ROUND(regval_to_mV(VDD_SOC_VAL), 10));
151
152         return ret;
153 }