]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/tx6/rn5t567.c
913dd2c274743fe2a5801f44205064ba67eb5f05
[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 "../common/karo.h"
22 #include "pmic.h"
23
24 #define RN5T567_NOETIMSET       0x11
25 #define RN5T567_LDORTC1_SLOT    0x2a
26 #define RN5T567_DC1CTL          0x2c
27 #define RN5T567_DC1CTL2         0x2d
28 #define RN5T567_DC2CTL          0x2e
29 #define RN5T567_DC2CTL2         0x2f
30 #define RN5T567_DC3CTL          0x30
31 #define RN5T567_DC3CTL2         0x31
32 #define RN5T567_DC1DAC          0x36 /* CORE */
33 #define RN5T567_DC2DAC          0x37 /* SOC */
34 #define RN5T567_DC3DAC          0x38 /* DDR */
35 #define RN5T567_DC1DAC_SLP      0x3b
36 #define RN5T567_DC2DAC_SLP      0x3c
37 #define RN5T567_DC3DAC_SLP      0x3d
38 #define RN5T567_LDOEN1          0x44
39 #define RN5T567_LDODIS          0x46
40 #define RN5T567_LDOEN2          0x48
41 #define RN5T567_LDO3DAC         0x4e /* IO */
42 #define RN5T567_LDORTC1DAC      0x56 /* VBACKUP */
43
44 #define NOETIMSET_DIS_OFF_NOE_TIM       (1 << 3)
45
46 #define VDD_RTC_VAL             mV_to_regval_rtc(3000)
47 #define VDD_HIGH_VAL            mV_to_regval3(3000)
48 #define VDD_HIGH_VAL_LP         mV_to_regval3(3000)
49 #define VDD_CORE_VAL            mV_to_regval(1350)              /* DCDC1 */
50 #define VDD_CORE_VAL_LP         mV_to_regval(900)
51 #define VDD_SOC_VAL             mV_to_regval(1350)              /* DCDC2 */
52 #define VDD_SOC_VAL_LP          mV_to_regval(900)
53 #define VDD_DDR_VAL             mV_to_regval(1350)              /* DCDC3 */
54 #define VDD_DDR_VAL_LP          mV_to_regval(1350)
55
56 /* calculate voltages in 10mV */
57 #define v2r(v,n,m)              DIV_ROUND(((((v) < (n)) ? (n) : (v)) - (n)), (m))
58 #define r2v(r,n,m)              (((r) * (m) + (n)) / 10)
59
60 /* DCDC1-3 */
61 #define mV_to_regval(mV)        v2r((mV) * 10, 6000, 125)
62 #define regval_to_mV(r)         r2v(r, 6000, 125)
63
64 /* LDO1-2 */
65 #define mV_to_regval2(mV)       v2r((mV) * 10, 9000, 250)
66 #define regval2_to_mV(r)        r2v(r, 9000, 250)
67
68 /* LDO3 */
69 #define mV_to_regval3(mV)       v2r((mV) * 10, 6000, 250)
70 #define regval3_to_mV(r)        r2v(r, 6000, 250)
71
72 /* LDORTC */
73 #define mV_to_regval_rtc(mV)    v2r((mV) * 10, 17000, 250)
74 #define regval_rtc_to_mV(r)     r2v(r, 17000, 250)
75
76 static struct rn5t567_regs {
77         u8 addr;
78         u8 val;
79         u8 mask;
80 } rn5t567_regs[] = {
81         { RN5T567_NOETIMSET, NOETIMSET_DIS_OFF_NOE_TIM | 0x5, },
82         { RN5T567_DC1DAC, VDD_CORE_VAL, },
83         { RN5T567_DC2DAC, VDD_SOC_VAL, },
84         { RN5T567_DC3DAC, VDD_DDR_VAL, },
85         { RN5T567_DC1DAC_SLP, VDD_CORE_VAL_LP, },
86         { RN5T567_DC2DAC_SLP, VDD_SOC_VAL_LP, },
87         { RN5T567_DC3DAC_SLP, VDD_DDR_VAL_LP, },
88         { RN5T567_LDOEN1, 0x01f, ~0x1f, },
89         { RN5T567_LDOEN2, 0x10, ~0x30, },
90         { RN5T567_LDODIS, 0x00, },
91         { RN5T567_LDO3DAC, VDD_HIGH_VAL, },
92         { RN5T567_LDORTC1DAC, VDD_RTC_VAL, },
93         { RN5T567_LDORTC1_SLOT, 0x0f, ~0x3f, },
94 };
95
96 static int rn5t567_setup_regs(uchar slave_addr, struct rn5t567_regs *r,
97                         size_t count)
98 {
99         int ret;
100         int i;
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         }
124         return 0;
125 }
126
127 int rn5t567_pmic_setup(uchar slave_addr)
128 {
129         int ret;
130         unsigned char value;
131
132         ret = i2c_read(slave_addr, 0x11, 1, &value, 1);
133         if (ret) {
134                 printf("%s: i2c_read error: %d\n", __func__, ret);
135                 return ret;
136         }
137
138         ret = rn5t567_setup_regs(slave_addr, rn5t567_regs,
139                                 ARRAY_SIZE(rn5t567_regs));
140         if (ret)
141                 return ret;
142
143         ret = i2c_read(slave_addr, RN5T567_DC1DAC, 1, &value, 1);
144         if (ret == 0) {
145                 printf("VDDCORE set to %umV\n", regval_to_mV(value));
146         } else {
147                 printf("Failed to read VDDCORE register setting\n");
148         }
149
150         ret = i2c_read(slave_addr, RN5T567_DC2DAC, 1, &value, 1);
151         if (ret == 0) {
152                 printf("VDDSOC  set to %umV\n", regval_to_mV(value));
153         } else {
154                 printf("Failed to read VDDSOC register setting\n");
155         }
156
157         return ret;
158 }