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 * SPDX-License-Identifier: GPL-2.0+
15 #include <linux/types.h>
16 #include <linux/list.h>
17 #include <power/pmic.h>
19 static LIST_HEAD(pmic_list);
21 int check_reg(struct pmic *p, u32 reg)
23 if (reg >= p->number_of_regs) {
24 printf("<reg num> = %d is invalid. Should be less than %d\n",
25 reg, p->number_of_regs);
32 int pmic_set_output(struct pmic *p, u32 reg, int out, int on)
36 if (pmic_reg_read(p, reg, &val))
44 if (pmic_reg_write(p, reg, val))
50 static void pmic_show_info(struct pmic *p)
52 printf("PMIC: %s\n", p->name);
55 static int pmic_dump(struct pmic *p)
61 puts("Wrong PMIC name!\n");
66 for (i = 0; i < p->number_of_regs; i++) {
67 ret = pmic_reg_read(p, i, &val);
69 puts("PMIC: Registers dump failed\n");
72 printf("\n0x%02x: ", i);
80 struct pmic *pmic_alloc(void)
84 p = calloc(sizeof(*p), 1);
86 printf("%s: No available memory for allocation!\n", __func__);
90 list_add_tail(&p->list, &pmic_list);
92 debug("%s: new pmic struct: 0x%p\n", __func__, p);
97 struct pmic *pmic_get(const char *s)
101 list_for_each_entry(p, &pmic_list, list) {
102 if (strcmp(p->name, s) == 0) {
103 debug("%s: pmic %s -> 0x%p\n", __func__, p->name, p);
111 const char *power_get_interface(int interface)
113 const char *power_interface[] = {"I2C", "SPI", "|+|-|"};
114 return power_interface[interface];
117 static void pmic_list_names(void)
121 puts("PMIC devices:\n");
122 list_for_each_entry(p, &pmic_list, list) {
123 printf("name: %s bus: %s_%d\n", p->name,
124 power_get_interface(p->interface), p->bus);
128 int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
134 /* at least two arguments please */
136 return CMD_RET_USAGE;
138 if (strcmp(argv[1], "list") == 0) {
140 return CMD_RET_SUCCESS;
146 debug("%s: name: %s cmd: %s\n", __func__, name, cmd);
149 return CMD_RET_FAILURE;
151 if (strcmp(cmd, "dump") == 0) {
153 return CMD_RET_FAILURE;
154 return CMD_RET_SUCCESS;
157 if (strcmp(cmd, "read") == 0) {
159 return CMD_RET_USAGE;
161 reg = simple_strtoul(argv[3], NULL, 16);
162 ret = pmic_reg_read(p, reg, &val);
165 puts("PMIC: Register read failed\n");
167 printf("\n0x%02x: 0x%08x\n", reg, val);
169 return CMD_RET_SUCCESS;
172 if (strcmp(cmd, "write") == 0) {
174 return CMD_RET_USAGE;
176 reg = simple_strtoul(argv[3], NULL, 16);
177 val = simple_strtoul(argv[4], NULL, 16);
178 pmic_reg_write(p, reg, val);
180 return CMD_RET_SUCCESS;
183 if (strcmp(cmd, "bat") == 0) {
185 return CMD_RET_USAGE;
187 if (strcmp(argv[3], "state") == 0)
188 p->fg->fg_battery_check(p->pbat->fg, p);
190 if (strcmp(argv[3], "charge") == 0) {
192 printf("BAT: %s charging (ctrl+c to break)\n",
194 if (p->low_power_mode)
196 if (p->pbat->battery_charge)
197 p->pbat->battery_charge(p);
201 return CMD_RET_SUCCESS;
204 /* No subcommand found */
205 return CMD_RET_SUCCESS;
209 pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
211 "list - list available PMICs\n"
212 "pmic name dump - dump named PMIC registers\n"
213 "pmic name read <reg> - read register\n"
214 "pmic name write <reg> <value> - write register\n"
215 "pmic name bat state - write register\n"
216 "pmic name bat charge - write register\n"