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