2 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 #include <asm/arch/gpio.h>
32 #define MAX77693_PMIC_PREFIX "max77693-pmic:"
34 #define MAX77693_PMIC_ID1 0x20
35 #define MAX77693_PMIC_ID2 0x21
37 /* MAX77693_PMIC_ID1 */
38 #define MAX77693_PMIC_ID 0x93
40 /* MAX77693_PMIC_ID2 */
41 #define MAX77693_PMIC_REV_PASS1 0x0
42 #define MAX77693_PMIC_REV_PASS2 0x1
43 #define MAX77693_PMIC_REV_PASS3 0x2
46 * PMIC(CHARGER) REGISTER
49 #define MAX77693_CHG_PREFIX "max77693-chg:"
51 #define MAX77693_CHG_BASE 0xB0
52 #define MAX77693_CHG_INT_OK 0xB2
53 #define MAX77693_CHG_CNFG_00 0xB7
54 #define MAX77693_CHG_CNFG_02 0xB9
55 #define MAX77693_CHG_CNFG_06 0xBD
56 #define MAX77693_SAFEOUT 0xC6
58 /* MAX77693_CHG_INT_OK */
59 #define MAX77693_CHG_DETBAT (0x1 << 7)
61 /* MAX77693_CHG_CNFG_00 */
62 #define MAX77693_CHG_MODE_ON 0x05
64 /* MAX77693_CHG_CNFG_02 */
65 #define MAX77693_CHG_CC 0x3F
67 /* MAX77693_CHG_CNFG_06 */
68 #define MAX77693_CHG_LOCK (0x0 << 2)
69 #define MAX77693_CHG_UNLOCK (0x3 << 2)
75 #define MAX77693_MUIC_PREFIX "max77693-muic:"
77 #define MAX77693_MUIC_STATUS1 0x04
78 #define MAX77693_MUIC_STATUS2 0x05
79 #define MAX77693_MUIC_CONTROL1 0x0C
81 /* MAX77693_MUIC_STATUS1 */
82 #define MAX77693_MUIC_ADC_MASK 0x1F
84 /* MAX77693_MUIC_STATUS2 */
85 #define MAX77693_MUIC_CHG_NO 0x00
86 #define MAX77693_MUIC_CHG_USB 0x01
87 #define MAX77693_MUIC_CHG_USB_D 0x02
88 #define MAX77693_MUIC_CHG_TA 0x03
89 #define MAX77693_MUIC_CHG_TA_500 0x04
90 #define MAX77693_MUIC_CHG_TA_1A 0x05
91 #define MAX77693_MUIC_CHG_MASK 0x07
93 /* MAX77693_MUIC_CONTROL1 */
94 #define MAX77693_MUIC_CTRL1_DN1DP2 ((0x1 << 3) | 0x1)
95 #define MAX77693_MUIC_CTRL1_UT1UR2 ((0x3 << 3) | 0x3)
96 #define MAX77693_MUIC_CTRL1_ADN1ADP2 ((0x4 << 3) | 0x4)
97 #define MAX77693_MUIC_CTRL1_AUT1AUR2 ((0x5 << 3) | 0x5)
98 #define MAX77693_MUIC_CTRL1_MASK 0xC0
100 /* MUIC INFO - passing to kernel driver by pmic_info field of cmdline */
101 #define MUIC_PATH_USB 0
102 #define MUIC_PATH_UART 1
104 #define MUIC_PATH_CP 0
105 #define MUIC_PATH_AP 1
114 static unsigned int pmic_bus = -1;
115 static unsigned int muic_bus = -1;
117 static unsigned int revision;
119 static int usb_path = MUIC_PATH_AP;
121 static void set_muic_path_info(void)
127 path = getenv("uartpath");
128 if (path && !strncmp(path, "cp", 2))
129 val |= MUIC_PATH_CP << MUIC_PATH_UART;
131 val |= MUIC_PATH_AP << MUIC_PATH_UART;
133 path = getenv("usbpath");
134 if (path && !strncmp(path, "cp", 2))
135 val |= MUIC_PATH_CP << MUIC_PATH_USB;
137 val |= MUIC_PATH_AP << MUIC_PATH_USB;
139 sprintf(buf, "pmic_info=%d", val);
140 setenv("muicpathinfo", buf);
143 void max77693_pmic_bus_init(int bus_num)
148 int max77693_pmic_probe(void)
150 unsigned char addr = MAX77693_PMIC_ADDR;
152 i2c_set_bus_num(pmic_bus);
154 if (i2c_probe(addr)) {
155 puts("Can't found max77693 pmic\n");
162 int max77693_init(void)
164 unsigned char addr = MAX77693_PMIC_ADDR;
165 unsigned char val[2];
167 if (max77693_pmic_probe())
171 i2c_read(addr, MAX77693_PMIC_ID1, 1, val, 2);
173 debug(MAX77693_PMIC_PREFIX "\tID:0x%x, REV:0x%x, VER:0x%x\n",
174 val[0], val[1] & 0x7, val[1] & 0xF8);
176 revision = val[1] & 0x3;
179 set_muic_path_info();
183 static int max77693_charger_en(int set_current)
185 unsigned char addr = MAX77693_PMIC_ADDR;
186 unsigned char val[2];
188 if (max77693_pmic_probe())
191 /* unlock write capability */
192 val[0] = MAX77693_CHG_UNLOCK;
193 i2c_write(addr, MAX77693_CHG_CNFG_06, 1, val, 1);
196 puts(MAX77693_CHG_PREFIX "\tdisabled\n");
197 i2c_read(addr, MAX77693_CHG_CNFG_00, 1, val, 1);
199 i2c_write(addr, MAX77693_CHG_CNFG_00, 1, val, 1);
203 /* set charging current */
204 i2c_read(addr, MAX77693_CHG_CNFG_02, 1, val, 1);
205 val[0] &= ~MAX77693_CHG_CC;
206 val[0] |= set_current * 10 / 333; /* 0.1A/3 steps */
207 i2c_write(addr, MAX77693_CHG_CNFG_02, 1, val, 1);
209 /* enable charging */
210 val[0] = MAX77693_CHG_MODE_ON;
211 i2c_write(addr, MAX77693_CHG_CNFG_00, 1, val, 1);
213 /* check charging current */
214 i2c_read(addr, MAX77693_CHG_CNFG_02, 1, val, 1);
216 printf(MAX77693_CHG_PREFIX "\tenabled @ %d mA\n", val[0] * 333 / 10);
221 int max77693_charger_detbat(void)
223 unsigned char addr = MAX77693_PMIC_ADDR;
224 unsigned char val[2];
226 if (max77693_pmic_probe())
229 i2c_read(addr, MAX77693_CHG_INT_OK, 1, val, 1);
231 if (val[0] & MAX77693_CHG_DETBAT)
237 int max77693_charger_status(void)
239 unsigned char addr = MAX77693_PMIC_ADDR;
240 unsigned char base = MAX77693_CHG_BASE;
241 unsigned char val[0x10];
243 if (max77693_pmic_probe())
246 i2c_read(addr, base, 1, val, 0x10);
249 printf("INT (%02Xh) = 0x%02x\n", base + 0x00, val[0x0]);
250 printf("INT_MSK(%02Xh) = 0x%02x\n", base + 0x01, val[0x1]);
251 printf("INT_OK (%02Xh) = 0x%02x\n", base + 0x02, val[0x2]);
252 printf("DTLS00 (%02Xh) = 0x%02x\n", base + 0x03, val[0x3]);
253 printf("DTLS01 (%02Xh) = 0x%02x\n", base + 0x04, val[0x4]);
254 printf("DTLS02 (%02Xh) = 0x%02x\n", base + 0x05, val[0x5]);
255 printf("CNFG00 (%02Xh) = 0x%02x\n", base + 0x07, val[0x7]);
256 printf("CNFG01 (%02Xh) = 0x%02x\n", base + 0x08, val[0x8]);
257 printf("CNFG02 (%02Xh) = 0x%02x\n", base + 0x09, val[0x9]);
258 printf("CNFG03 (%02Xh) = 0x%02x\n", base + 0x0a, val[0xa]);
259 printf("CNFG04 (%02Xh) = 0x%02x\n", base + 0x0b, val[0xb]);
260 printf("CNFG06 (%02Xh) = 0x%02x\n", base + 0x0d, val[0xd]);
265 void max77693_muic_bus_init(int bus_num)
270 int max77693_muic_probe(void)
272 unsigned char addr = MAX77693_MUIC_ADDR;
274 i2c_set_bus_num(muic_bus);
276 if (i2c_probe(addr)) {
277 puts("Can't found max77693 muic\n");
284 int max77693_muic_check(void)
286 unsigned char addr = MAX77693_MUIC_ADDR;
287 unsigned char val[2];
288 unsigned char charge_type, charger;
290 /* if probe failed, return cable none */
291 if (max77693_muic_probe())
294 i2c_read(addr, MAX77693_MUIC_STATUS2, 1, val, 1);
296 charge_type = val[0] & MAX77693_MUIC_CHG_MASK;
298 switch (charge_type) {
299 case MAX77693_MUIC_CHG_NO:
300 charger = CHARGER_NO;
302 case MAX77693_MUIC_CHG_USB:
303 case MAX77693_MUIC_CHG_USB_D:
304 charger = CHARGER_USB;
306 case MAX77693_MUIC_CHG_TA:
307 case MAX77693_MUIC_CHG_TA_1A:
308 charger = CHARGER_TA;
310 case MAX77693_MUIC_CHG_TA_500:
311 charger = CHARGER_TA_500;
314 charger = CHARGER_UNKNOWN;
321 void __board_muic_gpio_control(int sw, int path) { }
322 void board_muic_gpio_control(int sw, int path)
323 __attribute__((weak, alias("__board_muic_gpio_control")));
325 static int max77693_muic_switch(enum muic_path path)
327 unsigned char addr = MAX77693_MUIC_ADDR;
330 if (max77693_muic_probe())
333 i2c_read(addr, MAX77693_MUIC_CONTROL1, 1, &val, 1);
335 val &= MAX77693_MUIC_CTRL1_MASK;
337 if (revision >= MAX77693_PMIC_REV_PASS2)
339 case MUIC_PATH_USB_AP:
340 val |= MAX77693_MUIC_CTRL1_DN1DP2;
342 case MUIC_PATH_USB_CP:
343 val |= MAX77693_MUIC_CTRL1_ADN1ADP2;
345 case MUIC_PATH_UART_AP:
346 val |= MAX77693_MUIC_CTRL1_UT1UR2;
348 case MUIC_PATH_UART_CP:
349 val |= MAX77693_MUIC_CTRL1_AUT1AUR2;
354 case MUIC_PATH_USB_AP:
355 val |= MAX77693_MUIC_CTRL1_DN1DP2;
357 case MUIC_PATH_USB_CP:
358 val |= MAX77693_MUIC_CTRL1_UT1UR2;
359 board_muic_gpio_control(MUIC_PATH_USB, MUIC_PATH_CP);
361 case MUIC_PATH_UART_AP:
362 val |= MAX77693_MUIC_CTRL1_UT1UR2;
363 board_muic_gpio_control(MUIC_PATH_UART, MUIC_PATH_AP);
364 board_muic_gpio_control(MUIC_PATH_USB, MUIC_PATH_AP);
366 case MUIC_PATH_UART_CP:
367 val |= MAX77693_MUIC_CTRL1_UT1UR2;
368 board_muic_gpio_control(MUIC_PATH_UART, MUIC_PATH_CP);
369 board_muic_gpio_control(MUIC_PATH_USB, MUIC_PATH_AP);
373 i2c_write(addr, MAX77693_MUIC_CONTROL1, 1, &val, 1);
378 /* To detect the attached accessory type on anyway jig box */
379 int max77693_muic_get_adc(void)
381 unsigned char addr = MAX77693_MUIC_ADDR;
382 unsigned char val[2];
384 if (max77693_muic_probe())
387 i2c_read(addr, MAX77693_MUIC_STATUS1, 1, val, 1);
389 return (val[0] & MAX77693_MUIC_ADC_MASK);
392 static int max77693_status(void)
394 unsigned char addr = MAX77693_PMIC_ADDR;
395 unsigned char val[2];
397 if (max77693_pmic_probe())
400 i2c_read(addr, MAX77693_SAFEOUT, 1, val, 1);
401 printf("SAFEOUT1 %s\n", (val[0] & (1 << 6)) ? "on" : "off");
402 printf("SAFEOUT2 %s\n", (val[0] & (1 << 7)) ? "on" : "off");
407 static int max77693_safeout_control(int safeout, int on)
409 unsigned char addr = MAX77693_PMIC_ADDR;
410 unsigned char val[2];
411 unsigned int reg, set;
413 if (safeout < 1 || safeout > 2)
415 reg = MAX77693_SAFEOUT;
416 set = 1 << (5 + safeout);
418 if (max77693_pmic_probe())
421 i2c_read(addr, reg, 1, val, 1);
427 i2c_write(addr, reg, 1, val, 1);
428 i2c_read(addr, reg, 1, val, 1);
433 static int do_max77693(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
440 return cmd_usage(cmdtp);
442 if (!strncmp(argv[1], "status", 6))
443 return max77693_status();
445 if (!strncmp(argv[1], "safeout", 7)) {
447 return cmd_usage(cmdtp);
449 safeout = simple_strtoul(argv[2], NULL, 10);
451 if (!strncmp(argv[3], "on", 2))
452 ret = max77693_safeout_control(safeout, 1);
453 else if (!strncmp(argv[3], "off", 3))
454 ret = max77693_safeout_control(safeout, 0);
456 return cmd_usage(cmdtp);
459 printf("%s %s %s\n", argv[1], argv[2], argv[3]);
465 if (!strncmp(argv[1], "usb", 4)) {
466 if (!strncmp(argv[2], "cp", 2))
467 path = MUIC_PATH_USB_CP;
468 else if (!strncmp(argv[2], "ap", 2))
469 path = MUIC_PATH_USB_AP;
471 return cmd_usage(cmdtp);
473 max77693_muic_switch(path);
475 setenv("usbpath", argv[2]);
476 set_muic_path_info();
480 if (!strncmp(argv[1], "uart", 4)) {
481 if (!strncmp(argv[2], "cp", 2))
482 path = MUIC_PATH_UART_CP;
483 else if (!strncmp(argv[2], "ap", 2))
484 path = MUIC_PATH_UART_AP;
486 return cmd_usage(cmdtp);
488 max77693_muic_switch(path);
490 setenv("uartpath", argv[2]);
491 set_muic_path_info();
495 if (!strncmp(argv[1], "charger", 1)) {
496 if (!strncmp(argv[2], "start", 5)) {
499 return cmd_usage(cmdtp);
500 current = simple_strtoul(argv[3], NULL, 10);
501 max77693_charger_en(current);
502 } else if (!strncmp(argv[2], "stop", 4)) {
503 max77693_charger_en(0);
504 } else if (!strncmp(argv[2], "status", 6)) {
505 max77693_fg_show_battery();
507 return cmd_usage(cmdtp);
513 return cmd_usage(cmdtp);
517 max77693, 4, 1, do_max77693,
518 "PMIC/MUIC control for MAX77693",
520 "max77693 safeout num on/off - Turn on/off the SAFEOUT\n"
521 "max77693 charger start/stop/status current\n"
522 "max77693 uart ap/cp - Switching UART path between AP/CP\n"
523 "max77693 usb ap/cp - Switching USB path between AP/CP\n"