2 * Copyright (C) 2011 Samsung Electronics
3 * Lukasz Majewski <l.majewski@samsung.com>
6 * Stefano Babic, DENX Software Engineering, sbabic@denx.de
8 * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
10 * See file CREDITS for list of people who contributed to this
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #include <linux/types.h>
32 #include <linux/list.h>
33 #include <power/pmic.h>
35 static LIST_HEAD(pmic_list);
37 int check_reg(struct pmic *p, u32 reg)
39 if (reg >= p->number_of_regs) {
40 printf("<reg num> = %d is invalid. Should be less than %d\n",
41 reg, p->number_of_regs);
48 int pmic_set_output(struct pmic *p, u32 reg, int out, int on)
52 if (pmic_reg_read(p, reg, &val))
60 if (pmic_reg_write(p, reg, val))
66 static void pmic_show_info(struct pmic *p)
68 printf("PMIC: %s\n", p->name);
71 static int pmic_dump(struct pmic *p)
77 puts("Wrong PMIC name!\n");
82 for (i = 0; i < p->number_of_regs; i++) {
83 ret = pmic_reg_read(p, i, &val);
85 puts("PMIC: Registers dump failed\n");
88 printf("\n0x%02x: ", i);
96 struct pmic *pmic_alloc(void)
100 p = calloc(sizeof(*p), 1);
102 printf("%s: No available memory for allocation!\n", __func__);
106 list_add_tail(&p->list, &pmic_list);
108 debug("%s: new pmic struct: 0x%p\n", __func__, p);
113 struct pmic *pmic_get(const char *s)
117 list_for_each_entry(p, &pmic_list, list) {
118 if (strcmp(p->name, s) == 0) {
119 debug("%s: pmic %s -> 0x%p\n", __func__, p->name, p);
127 const char *power_get_interface(int interface)
129 const char *power_interface[] = {"I2C", "SPI", "|+|-|"};
130 return power_interface[interface];
133 static void pmic_list_names(void)
137 puts("PMIC devices:\n");
138 list_for_each_entry(p, &pmic_list, list) {
139 printf("name: %s bus: %s_%d\n", p->name,
140 power_get_interface(p->interface), p->bus);
144 int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
150 /* at least two arguments please */
152 return CMD_RET_USAGE;
154 if (strcmp(argv[1], "list") == 0) {
156 return CMD_RET_SUCCESS;
162 debug("%s: name: %s cmd: %s\n", __func__, name, cmd);
165 return CMD_RET_FAILURE;
167 if (strcmp(cmd, "dump") == 0) {
169 return CMD_RET_FAILURE;
170 return CMD_RET_SUCCESS;
173 if (strcmp(cmd, "read") == 0) {
175 return CMD_RET_USAGE;
177 reg = simple_strtoul(argv[3], NULL, 16);
178 ret = pmic_reg_read(p, reg, &val);
181 puts("PMIC: Register read failed\n");
183 printf("\n0x%02x: 0x%08x\n", reg, val);
185 return CMD_RET_SUCCESS;
188 if (strcmp(cmd, "write") == 0) {
190 return CMD_RET_USAGE;
192 reg = simple_strtoul(argv[3], NULL, 16);
193 val = simple_strtoul(argv[4], NULL, 16);
194 pmic_reg_write(p, reg, val);
196 return CMD_RET_SUCCESS;
199 if (strcmp(cmd, "bat") == 0) {
201 return CMD_RET_USAGE;
203 if (strcmp(argv[3], "state") == 0)
204 p->fg->fg_battery_check(p->pbat->fg, p);
206 if (strcmp(argv[3], "charge") == 0) {
208 printf("PRINT BAT charge %s\n", p->name);
209 if (p->low_power_mode)
211 if (p->pbat->battery_charge)
212 p->pbat->battery_charge(p);
216 return CMD_RET_SUCCESS;
219 /* No subcommand found */
220 return CMD_RET_SUCCESS;
224 pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
226 "list - list available PMICs\n"
227 "pmic name dump - dump named PMIC registers\n"
228 "pmic name read <reg> - read register\n"
229 "pmic name write <reg> <value> - write register\n"
230 "pmic name bat state - write register\n"
231 "pmic name bat charge - write register\n"