Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Samsung Electronics |
| 3 | * Lukasz Majewski <l.majewski@samsung.com> |
| 4 | * |
| 5 | * (C) Copyright 2010 |
| 6 | * Stefano Babic, DENX Software Engineering, sbabic@denx.de |
| 7 | * |
| 8 | * (C) Copyright 2008-2009 Freescale Semiconductor, Inc. |
| 9 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 14 | #include <malloc.h> |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 15 | #include <linux/types.h> |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 16 | #include <linux/list.h> |
| 17 | #include <power/pmic.h> |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 18 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 19 | static LIST_HEAD(pmic_list); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 20 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 21 | int check_reg(struct pmic *p, u32 reg) |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 22 | { |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 23 | if (reg >= p->number_of_regs) { |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 24 | printf("<reg num> = %d is invalid. Should be less than %d\n", |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 25 | reg, p->number_of_regs); |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 26 | return -EINVAL; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 27 | } |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 28 | |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | int pmic_set_output(struct pmic *p, u32 reg, int out, int on) |
| 33 | { |
| 34 | u32 val; |
| 35 | |
| 36 | if (pmic_reg_read(p, reg, &val)) |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 37 | return -ENOTSUPP; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 38 | |
| 39 | if (on) |
| 40 | val |= out; |
| 41 | else |
| 42 | val &= ~out; |
| 43 | |
| 44 | if (pmic_reg_write(p, reg, val)) |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 45 | return -ENOTSUPP; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static void pmic_show_info(struct pmic *p) |
| 51 | { |
| 52 | printf("PMIC: %s\n", p->name); |
| 53 | } |
| 54 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 55 | static int pmic_dump(struct pmic *p) |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 56 | { |
| 57 | int i, ret; |
| 58 | u32 val; |
| 59 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 60 | if (!p) { |
| 61 | puts("Wrong PMIC name!\n"); |
Jaehoon Chung | 10e80f3 | 2016-12-15 20:49:50 +0900 | [diff] [blame] | 62 | return -ENODEV; |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 65 | pmic_show_info(p); |
| 66 | for (i = 0; i < p->number_of_regs; i++) { |
| 67 | ret = pmic_reg_read(p, i, &val); |
| 68 | if (ret) |
| 69 | puts("PMIC: Registers dump failed\n"); |
| 70 | |
| 71 | if (!(i % 8)) |
| 72 | printf("\n0x%02x: ", i); |
| 73 | |
| 74 | printf("%08x ", val); |
| 75 | } |
| 76 | puts("\n"); |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | struct pmic *pmic_alloc(void) |
| 81 | { |
| 82 | struct pmic *p; |
| 83 | |
| 84 | p = calloc(sizeof(*p), 1); |
| 85 | if (!p) { |
| 86 | printf("%s: No available memory for allocation!\n", __func__); |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | list_add_tail(&p->list, &pmic_list); |
| 91 | |
| 92 | debug("%s: new pmic struct: 0x%p\n", __func__, p); |
| 93 | |
| 94 | return p; |
| 95 | } |
| 96 | |
| 97 | struct pmic *pmic_get(const char *s) |
| 98 | { |
| 99 | struct pmic *p; |
| 100 | |
| 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); |
| 104 | return p; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return NULL; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Łukasz Majewski | 6328caf | 2012-11-13 03:22:12 +0000 | [diff] [blame] | 111 | const char *power_get_interface(int interface) |
| 112 | { |
| 113 | const char *power_interface[] = {"I2C", "SPI", "|+|-|"}; |
| 114 | return power_interface[interface]; |
| 115 | } |
| 116 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 117 | static void pmic_list_names(void) |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 118 | { |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 119 | struct pmic *p; |
| 120 | |
| 121 | puts("PMIC devices:\n"); |
| 122 | list_for_each_entry(p, &pmic_list, list) { |
Łukasz Majewski | 6328caf | 2012-11-13 03:22:12 +0000 | [diff] [blame] | 123 | printf("name: %s bus: %s_%d\n", p->name, |
| 124 | power_get_interface(p->interface), p->bus); |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 125 | } |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 129 | { |
| 130 | u32 ret, reg, val; |
Łukasz Majewski | 6328caf | 2012-11-13 03:22:12 +0000 | [diff] [blame] | 131 | char *cmd, *name; |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 132 | struct pmic *p; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 133 | |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 134 | /* at least two arguments please */ |
| 135 | if (argc < 2) |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 136 | return CMD_RET_USAGE; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 137 | |
Łukasz Majewski | 6328caf | 2012-11-13 03:22:12 +0000 | [diff] [blame] | 138 | if (strcmp(argv[1], "list") == 0) { |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 139 | pmic_list_names(); |
| 140 | return CMD_RET_SUCCESS; |
| 141 | } |
| 142 | |
Łukasz Majewski | 2571923 | 2014-02-24 13:33:08 +0100 | [diff] [blame] | 143 | if (argc < 3) |
| 144 | return CMD_RET_USAGE; |
| 145 | |
Łukasz Majewski | 6328caf | 2012-11-13 03:22:12 +0000 | [diff] [blame] | 146 | name = argv[1]; |
| 147 | cmd = argv[2]; |
| 148 | |
| 149 | debug("%s: name: %s cmd: %s\n", __func__, name, cmd); |
| 150 | p = pmic_get(name); |
| 151 | if (!p) |
| 152 | return CMD_RET_FAILURE; |
| 153 | |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 154 | if (strcmp(cmd, "dump") == 0) { |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 155 | if (pmic_dump(p)) |
| 156 | return CMD_RET_FAILURE; |
| 157 | return CMD_RET_SUCCESS; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | if (strcmp(cmd, "read") == 0) { |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 161 | if (argc < 4) |
| 162 | return CMD_RET_USAGE; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 163 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 164 | reg = simple_strtoul(argv[3], NULL, 16); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 165 | ret = pmic_reg_read(p, reg, &val); |
| 166 | |
| 167 | if (ret) |
| 168 | puts("PMIC: Register read failed\n"); |
| 169 | |
| 170 | printf("\n0x%02x: 0x%08x\n", reg, val); |
| 171 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 172 | return CMD_RET_SUCCESS; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | if (strcmp(cmd, "write") == 0) { |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 176 | if (argc < 5) |
| 177 | return CMD_RET_USAGE; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 178 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 179 | reg = simple_strtoul(argv[3], NULL, 16); |
| 180 | val = simple_strtoul(argv[4], NULL, 16); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 181 | pmic_reg_write(p, reg, val); |
| 182 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 183 | return CMD_RET_SUCCESS; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Łukasz Majewski | 6328caf | 2012-11-13 03:22:12 +0000 | [diff] [blame] | 186 | if (strcmp(cmd, "bat") == 0) { |
| 187 | if (argc < 4) |
| 188 | return CMD_RET_USAGE; |
| 189 | |
Piotr Wilczek | dfb100d | 2013-09-26 14:43:35 +0200 | [diff] [blame] | 190 | if (!p->pbat) { |
| 191 | printf("%s is not a battery\n", p->name); |
| 192 | return CMD_RET_FAILURE; |
| 193 | } |
| 194 | |
Łukasz Majewski | 6328caf | 2012-11-13 03:22:12 +0000 | [diff] [blame] | 195 | if (strcmp(argv[3], "state") == 0) |
| 196 | p->fg->fg_battery_check(p->pbat->fg, p); |
| 197 | |
| 198 | if (strcmp(argv[3], "charge") == 0) { |
Piotr Wilczek | dfb100d | 2013-09-26 14:43:35 +0200 | [diff] [blame] | 199 | printf("BAT: %s charging (ctrl+c to break)\n", |
| 200 | p->name); |
| 201 | if (p->low_power_mode) |
| 202 | p->low_power_mode(); |
| 203 | if (p->pbat->battery_charge) |
| 204 | p->pbat->battery_charge(p); |
Łukasz Majewski | 6328caf | 2012-11-13 03:22:12 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | return CMD_RET_SUCCESS; |
| 208 | } |
| 209 | |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 210 | /* No subcommand found */ |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 211 | return CMD_RET_SUCCESS; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | U_BOOT_CMD( |
| 215 | pmic, CONFIG_SYS_MAXARGS, 1, do_pmic, |
| 216 | "PMIC", |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 217 | "list - list available PMICs\n" |
Łukasz Majewski | 6328caf | 2012-11-13 03:22:12 +0000 | [diff] [blame] | 218 | "pmic name dump - dump named PMIC registers\n" |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame] | 219 | "pmic name read <reg> - read register\n" |
Łukasz Majewski | 6328caf | 2012-11-13 03:22:12 +0000 | [diff] [blame] | 220 | "pmic name write <reg> <value> - write register\n" |
| 221 | "pmic name bat state - write register\n" |
| 222 | "pmic name bat charge - write register\n" |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 223 | ); |