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