blob: 1ad810acc266cb41cfbc5071c633c464d6418865 [file] [log] [blame]
Rajeshwari Shindee9e8be82012-08-24 00:39:23 +00001/*
2 * Copyright (C) 2012 Samsung Electronics
3 * Rajeshwari Shinde <rajeshwari.s@samsung.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Rajeshwari Shindee9e8be82012-08-24 00:39:23 +00006 */
7
8#include <common.h>
Rajeshwari Shinde817cc272013-01-08 21:03:40 +00009#include <fdtdec.h>
10#include <i2c.h>
Rajeshwari Shindefe7c3622012-12-10 01:55:47 +000011#include <power/pmic.h>
12#include <power/max77686_pmic.h>
13#include <errno.h>
Rajeshwari Shindee9e8be82012-08-24 00:39:23 +000014
Rajeshwari Shinde817cc272013-01-08 21:03:40 +000015DECLARE_GLOBAL_DATA_PTR;
16
Piotr Wilczek64a3d4d2013-06-25 09:59:47 +020017static const char max77686_buck_addr[] = {
18 0xff, 0x10, 0x12, 0x1c, 0x26, 0x30, 0x32, 0x34, 0x36, 0x38
19};
20
21static unsigned int max77686_ldo_volt2hex(int ldo, ulong uV)
22{
23 unsigned int hex = 0;
24
25 switch (ldo) {
26 case 1:
27 case 2:
28 case 6:
29 case 7:
30 case 8:
31 case 15:
32 hex = (uV - 800000) / 25000;
33 break;
34 default:
35 hex = (uV - 800000) / 50000;
36 }
37
38 if (hex >= 0 && hex <= MAX77686_LDO_VOLT_MAX_HEX)
39 return hex;
40
41 debug("%s: %ld is wrong voltage value for LDO%d\n", __func__, uV, ldo);
42 return 0;
43}
44
Suriyan Ramasamicab8ef92014-10-29 09:22:41 -070045static int max77686_buck_volt2hex(int buck, ulong uV)
46{
47 int hex = 0;
48
49 if (buck < 5 || buck > 9) {
50 debug("%s: buck %d is not supported\n", __func__, buck);
51 return -EINVAL;
52 }
53
54 hex = (uV - 750000) / 50000;
55
56 if (hex >= 0 && hex <= MAX77686_BUCK_VOLT_MAX_HEX)
57 return hex;
58
59 debug("%s: %ld is wrong voltage value for BUCK%d\n",
60 __func__, uV, buck);
61 return -EINVAL;
62}
63
Piotr Wilczek64a3d4d2013-06-25 09:59:47 +020064int max77686_set_ldo_voltage(struct pmic *p, int ldo, ulong uV)
65{
66 unsigned int val, ret, hex, adr;
67
Suriyan Ramasamicab8ef92014-10-29 09:22:41 -070068 if (ldo < 1 || ldo > 26) {
Piotr Wilczek64a3d4d2013-06-25 09:59:47 +020069 printf("%s: %d is wrong ldo number\n", __func__, ldo);
70 return -1;
71 }
72
73 adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1;
74 hex = max77686_ldo_volt2hex(ldo, uV);
75
76 if (!hex)
77 return -1;
78
79 ret = pmic_reg_read(p, adr, &val);
80 if (ret)
81 return ret;
82
83 val &= ~MAX77686_LDO_VOLT_MASK;
84 val |= hex;
85 ret |= pmic_reg_write(p, adr, val);
86
87 return ret;
88}
89
Suriyan Ramasamicab8ef92014-10-29 09:22:41 -070090int max77686_set_buck_voltage(struct pmic *p, int buck, ulong uV)
91{
92 unsigned int val, adr;
93 int hex, ret;
94
95 if (buck < 5 || buck > 9) {
96 printf("%s: %d is an unsupported bucket number\n",
97 __func__, buck);
98 return -EINVAL;
99 }
100
101 adr = max77686_buck_addr[buck] + 1;
102 hex = max77686_buck_volt2hex(buck, uV);
103
104 if (hex < 0)
105 return hex;
106
107 ret = pmic_reg_read(p, adr, &val);
108 if (ret)
109 return ret;
110
111 val &= ~MAX77686_BUCK_VOLT_MASK;
112 ret |= pmic_reg_write(p, adr, val | hex);
113
114 return ret;
115}
116
Piotr Wilczek64a3d4d2013-06-25 09:59:47 +0200117int max77686_set_ldo_mode(struct pmic *p, int ldo, char opmode)
118{
119 unsigned int val, ret, adr, mode;
120
Suriyan Ramasamicab8ef92014-10-29 09:22:41 -0700121 if (ldo < 1 || 26 < ldo) {
Piotr Wilczek64a3d4d2013-06-25 09:59:47 +0200122 printf("%s: %d is wrong ldo number\n", __func__, ldo);
123 return -1;
124 }
125
126 adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1;
127
128 /* mode */
129 switch (opmode) {
130 case OPMODE_OFF:
131 mode = MAX77686_LDO_MODE_OFF;
132 break;
133 case OPMODE_STANDBY:
134 switch (ldo) {
135 case 2:
136 case 6:
137 case 7:
138 case 8:
139 case 10:
140 case 11:
141 case 12:
142 case 14:
143 case 15:
144 case 16:
145 mode = MAX77686_LDO_MODE_STANDBY;
146 break;
147 default:
148 mode = 0xff;
149 }
150 break;
151 case OPMODE_LPM:
152 mode = MAX77686_LDO_MODE_LPM;
153 break;
154 case OPMODE_ON:
155 mode = MAX77686_LDO_MODE_ON;
156 break;
157 default:
158 mode = 0xff;
159 }
160
161 if (mode == 0xff) {
162 printf("%s: %d is not supported on LDO%d\n",
163 __func__, opmode, ldo);
164 return -1;
165 }
166
167 ret = pmic_reg_read(p, adr, &val);
168 if (ret)
169 return ret;
170
171 val &= ~MAX77686_LDO_MODE_MASK;
172 val |= mode;
173 ret |= pmic_reg_write(p, adr, val);
174
175 return ret;
176}
177
178int max77686_set_buck_mode(struct pmic *p, int buck, char opmode)
179{
180 unsigned int val, ret, mask, adr, size, mode, mode_shift;
181
182 size = ARRAY_SIZE(max77686_buck_addr);
183 if (buck >= size) {
184 printf("%s: %d is wrong buck number\n", __func__, buck);
185 return -1;
186 }
187
188 adr = max77686_buck_addr[buck];
189
190 /* mask */
191 switch (buck) {
192 case 2:
193 case 3:
194 case 4:
195 mode_shift = MAX77686_BUCK_MODE_SHIFT_2;
196 break;
197 default:
198 mode_shift = MAX77686_BUCK_MODE_SHIFT_1;
199 }
200
201 mask = MAX77686_BUCK_MODE_MASK << mode_shift;
202
203 /* mode */
204 switch (opmode) {
205 case OPMODE_OFF:
Suriyan Ramasamicab8ef92014-10-29 09:22:41 -0700206 mode = MAX77686_BUCK_MODE_OFF << mode_shift;
Piotr Wilczek64a3d4d2013-06-25 09:59:47 +0200207 break;
208 case OPMODE_STANDBY:
209 switch (buck) {
210 case 1:
211 case 2:
212 case 3:
213 case 4:
214 mode = MAX77686_BUCK_MODE_STANDBY << mode_shift;
215 break;
216 default:
217 mode = 0xff;
218 }
219 break;
220 case OPMODE_LPM:
221 switch (buck) {
222 case 2:
223 case 3:
224 case 4:
225 mode = MAX77686_BUCK_MODE_LPM << mode_shift;
226 break;
227 default:
228 mode = 0xff;
229 }
230 break;
231 case OPMODE_ON:
232 mode = MAX77686_BUCK_MODE_ON << mode_shift;
233 break;
234 default:
235 mode = 0xff;
236 }
237
238 if (mode == 0xff) {
239 printf("%s: %d is not supported on BUCK%d\n",
240 __func__, opmode, buck);
241 return -1;
242 }
243
244 ret = pmic_reg_read(p, adr, &val);
245 if (ret)
246 return ret;
247
248 val &= ~mask;
249 val |= mode;
250 ret |= pmic_reg_write(p, adr, val);
251
252 return ret;
253}
254
Rajeshwari Shindefe7c3622012-12-10 01:55:47 +0000255int pmic_init(unsigned char bus)
Rajeshwari Shindee9e8be82012-08-24 00:39:23 +0000256{
Rajeshwari Shindee9e8be82012-08-24 00:39:23 +0000257 static const char name[] = "MAX77686_PMIC";
Rajeshwari Shindefe7c3622012-12-10 01:55:47 +0000258 struct pmic *p = pmic_alloc();
Jeroen Hofstee746b92b2014-06-18 22:13:52 +0200259#ifdef CONFIG_OF_CONTROL
260 const void *blob = gd->fdt_blob;
261 int node, parent, tmp;
262#endif
Rajeshwari Shindefe7c3622012-12-10 01:55:47 +0000263
264 if (!p) {
265 printf("%s: POWER allocation error!\n", __func__);
266 return -ENOMEM;
267 }
Rajeshwari Shindee9e8be82012-08-24 00:39:23 +0000268
Rajeshwari Shinde817cc272013-01-08 21:03:40 +0000269#ifdef CONFIG_OF_CONTROL
Rajeshwari Shinde817cc272013-01-08 21:03:40 +0000270 node = fdtdec_next_compatible(blob, 0, COMPAT_MAXIM_MAX77686_PMIC);
271 if (node < 0) {
272 debug("PMIC: No node for PMIC Chip in device tree\n");
273 debug("node = %d\n", node);
274 return -1;
275 }
276
277 parent = fdt_parent_offset(blob, node);
278 if (parent < 0) {
279 debug("%s: Cannot find node parent\n", __func__);
280 return -1;
281 }
282
Jeroen Hofstee746b92b2014-06-18 22:13:52 +0200283 /* tmp since p->bus is unsigned */
284 tmp = i2c_get_bus_num_fdt(parent);
285 if (tmp < 0) {
Rajeshwari Shinde817cc272013-01-08 21:03:40 +0000286 debug("%s: Cannot find I2C bus\n", __func__);
287 return -1;
288 }
Jeroen Hofstee746b92b2014-06-18 22:13:52 +0200289 p->bus = tmp;
Rajeshwari Shinde817cc272013-01-08 21:03:40 +0000290 p->hw.i2c.addr = fdtdec_get_int(blob, node, "reg", 9);
291#else
292 p->bus = bus;
293 p->hw.i2c.addr = MAX77686_I2C_ADDR;
294#endif
295
Rajeshwari Shindee9e8be82012-08-24 00:39:23 +0000296 p->name = name;
297 p->interface = PMIC_I2C;
Przemyslaw Marczak58ea76f2015-04-20 20:07:46 +0200298 p->number_of_regs = MAX77686_NUM_OF_REGS;
Rajeshwari Shindee9e8be82012-08-24 00:39:23 +0000299 p->hw.i2c.tx_num = 1;
Rajeshwari Shinde817cc272013-01-08 21:03:40 +0000300
301 puts("Board PMIC init\n");
Rajeshwari Shindee9e8be82012-08-24 00:39:23 +0000302
303 return 0;
304}