karo: tx6: rework PMIC code to allow for different configs for same chip
[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 "../common/karo.h"
22 #include "pmic.h"
23
24 #define RN5T618_NOETIMSET       0x11
25 #define RN5T618_LDORTC1_SLOT    0x2a
26 #define RN5T618_DC1CTL          0x2c
27 #define RN5T618_DC1CTL2         0x2d
28 #define RN5T618_DC2CTL          0x2e
29 #define RN5T618_DC2CTL2         0x2f
30 #define RN5T618_DC3CTL          0x30
31 #define RN5T618_DC3CTL2         0x31
32 #define RN5T618_DC1DAC          0x36 /* CORE */
33 #define RN5T618_DC2DAC          0x37 /* SOC */
34 #define RN5T618_DC3DAC          0x38 /* DDR */
35 #define RN5T618_DC1DAC_SLP      0x3b
36 #define RN5T618_DC2DAC_SLP      0x3c
37 #define RN5T618_DC3DAC_SLP      0x3d
38 #define RN5T618_LDOEN1          0x44
39 #define RN5T618_LDOEN2          0x45
40 #define RN5T618_LDODIS          0x46
41 #define RN5T618_LDO3DAC         0x4e /* IO */
42 #define RN5T618_LDORTCDAC       0x56 /* VBACKUP */
43
44 #define VDD_RTC_VAL             mV_to_regval_rtc(3000)
45 #define VDD_HIGH_VAL            mV_to_regval3(3000)
46 #define VDD_HIGH_VAL_LP         mV_to_regval3(3000)
47 #define VDD_CORE_VAL            mV_to_regval(1425)              /* DCDC1 */
48 #define VDD_CORE_VAL_LP         mV_to_regval(900)
49 #define VDD_SOC_VAL             mV_to_regval(1425)              /* DCDC2 */
50 #define VDD_SOC_VAL_LP          mV_to_regval(900)
51 #define VDD_DDR_VAL             mV_to_regval(1500)              /* DCDC3 */
52 #define VDD_DDR_VAL_LP          mV_to_regval(1500)
53
54 /* calculate voltages in 10mV */
55 #define v2r(v,n,m)              DIV_ROUND(((((v) < (n)) ? (n) : (v)) - (n)), (m))
56 #define r2v(r,n,m)              (((r) * (m) + (n)) / 10)
57
58 /* DCDC1-3 */
59 #define mV_to_regval(mV)        v2r((mV) * 10, 6000, 125)
60 #define regval_to_mV(r)         r2v(r, 6000, 125)
61
62 /* LDO1-2 */
63 #define mV_to_regval2(mV)       v2r((mV) * 10, 9000, 250)
64 #define regval2_to_mV(r)        r2v(r, 9000, 250)
65
66 /* LDO3 */
67 #define mV_to_regval3(mV)       v2r((mV) * 10, 6000, 250)
68 #define regval3_to_mV(r)        r2v(r, 6000, 250)
69
70 /* LDORTC */
71 #define mV_to_regval_rtc(mV)    v2r((mV) * 10, 17000, 250)
72 #define regval_rtc_to_mV(r)     r2v(r, 17000, 250)
73
74 static struct pmic_regs 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 pmic_regs *r,
91                         size_t count)
92 {
93         int ret;
94         int i;
95
96         for (i = 0; i < count; i++, r++) {
97 #ifdef DEBUG
98                 unsigned char value;
99
100                 ret = i2c_read(slave_addr, r->addr, 1, &value, 1);
101                 if (ret) {
102                         printf("%s: failed to read PMIC register %02x: %d\n",
103                                 __func__, r->addr, ret);
104                         return ret;
105                 }
106                 if ((value & ~r->mask) != r->val) {
107                         printf("Changing PMIC reg %02x from %02x to %02x\n",
108                                 r->addr, value, r->val);
109                 }
110 #endif
111                 ret = i2c_write(slave_addr, r->addr, 1, &r->val, 1);
112                 if (ret) {
113                         printf("%s: failed to write PMIC register %02x: %d\n",
114                                 __func__, r->addr, ret);
115                         return ret;
116                 }
117 #ifdef DEBUG
118                 ret = i2c_read(slave_addr, r->addr, 1, &value, 1);
119                 if (ret) {
120                         printf("%s: failed to read PMIC register %02x: %d\n",
121                                 __func__, r->addr, ret);
122                         return ret;
123                 }
124                 if (value != r->val) {
125                         printf("Failed to set PMIC reg %02x to %02x; actual value: %02x\n",
126                                 r->addr, r->val, value);
127                 }
128 #endif
129         }
130         return 0;
131 }
132
133 int rn5t618_pmic_setup(uchar slave_addr, struct pmic_regs *regs,
134                 size_t count)
135 {
136         int ret;
137         unsigned char value;
138
139         ret = i2c_read(slave_addr, 0x11, 1, &value, 1);
140         if (ret) {
141                 printf("%s: i2c_read error: %d\n", __func__, ret);
142                 return ret;
143         }
144
145         ret = rn5t618_setup_regs(slave_addr, rn5t618_regs,
146                                 ARRAY_SIZE(rn5t618_regs));
147         if (ret)
148                 return ret;
149
150         ret = i2c_read(slave_addr, RN5T618_DC1DAC, 1, &value, 1);
151         if (ret == 0) {
152                 printf("VDDCORE set to %umV\n", regval_to_mV(value));
153         } else {
154                 printf("Failed to read VDDCORE register setting\n");
155         }
156
157         ret = i2c_read(slave_addr, RN5T618_DC2DAC, 1, &value, 1);
158         if (ret == 0) {
159                 printf("VDDSOC  set to %umV\n", regval_to_mV(value));
160         } else {
161                 printf("Failed to read VDDSOC register setting\n");
162         }
163
164         return ret;
165 }