Ł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 | * |
| 10 | * See file CREDITS for list of people who contributed to this |
| 11 | * project. |
| 12 | * |
| 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. |
| 17 | * |
| 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. |
| 22 | * |
| 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, |
| 26 | * MA 02111-1307 USA |
| 27 | */ |
| 28 | |
| 29 | #include <common.h> |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 30 | #include <malloc.h> |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 31 | #include <linux/types.h> |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 32 | #include <linux/list.h> |
| 33 | #include <power/pmic.h> |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 34 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 35 | static LIST_HEAD(pmic_list); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 36 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 37 | int check_reg(struct pmic *p, u32 reg) |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 38 | { |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 39 | if (reg >= p->number_of_regs) { |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 40 | printf("<reg num> = %d is invalid. Should be less than %d\n", |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 41 | reg, p->number_of_regs); |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 42 | return -1; |
| 43 | } |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 44 | |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | int pmic_set_output(struct pmic *p, u32 reg, int out, int on) |
| 49 | { |
| 50 | u32 val; |
| 51 | |
| 52 | if (pmic_reg_read(p, reg, &val)) |
| 53 | return -1; |
| 54 | |
| 55 | if (on) |
| 56 | val |= out; |
| 57 | else |
| 58 | val &= ~out; |
| 59 | |
| 60 | if (pmic_reg_write(p, reg, val)) |
| 61 | return -1; |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static void pmic_show_info(struct pmic *p) |
| 67 | { |
| 68 | printf("PMIC: %s\n", p->name); |
| 69 | } |
| 70 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 71 | static int pmic_dump(struct pmic *p) |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 72 | { |
| 73 | int i, ret; |
| 74 | u32 val; |
| 75 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 76 | if (!p) { |
| 77 | puts("Wrong PMIC name!\n"); |
| 78 | return -1; |
| 79 | } |
| 80 | |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 81 | pmic_show_info(p); |
| 82 | for (i = 0; i < p->number_of_regs; i++) { |
| 83 | ret = pmic_reg_read(p, i, &val); |
| 84 | if (ret) |
| 85 | puts("PMIC: Registers dump failed\n"); |
| 86 | |
| 87 | if (!(i % 8)) |
| 88 | printf("\n0x%02x: ", i); |
| 89 | |
| 90 | printf("%08x ", val); |
| 91 | } |
| 92 | puts("\n"); |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | struct pmic *pmic_alloc(void) |
| 97 | { |
| 98 | struct pmic *p; |
| 99 | |
| 100 | p = calloc(sizeof(*p), 1); |
| 101 | if (!p) { |
| 102 | printf("%s: No available memory for allocation!\n", __func__); |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | list_add_tail(&p->list, &pmic_list); |
| 107 | |
| 108 | debug("%s: new pmic struct: 0x%p\n", __func__, p); |
| 109 | |
| 110 | return p; |
| 111 | } |
| 112 | |
| 113 | struct pmic *pmic_get(const char *s) |
| 114 | { |
| 115 | struct pmic *p; |
| 116 | |
| 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); |
| 120 | return p; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return NULL; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 127 | static void pmic_list_names(void) |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 128 | { |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 129 | struct pmic *p; |
| 130 | |
| 131 | puts("PMIC devices:\n"); |
| 132 | list_for_each_entry(p, &pmic_list, list) { |
| 133 | printf("name: %s\n", p->name); |
| 134 | } |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 138 | { |
| 139 | u32 ret, reg, val; |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 140 | struct pmic *p; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 141 | char *cmd; |
| 142 | |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 143 | /* at least two arguments please */ |
| 144 | if (argc < 2) |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 145 | return CMD_RET_USAGE; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 146 | |
| 147 | cmd = argv[1]; |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 148 | |
| 149 | if (strcmp(cmd, "list") == 0) { |
| 150 | pmic_list_names(); |
| 151 | return CMD_RET_SUCCESS; |
| 152 | } |
| 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 | p = pmic_get(argv[2]); |
| 156 | if (!p) |
| 157 | return CMD_RET_FAILURE; |
| 158 | if (pmic_dump(p)) |
| 159 | return CMD_RET_FAILURE; |
| 160 | return CMD_RET_SUCCESS; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | if (strcmp(cmd, "read") == 0) { |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 164 | if (argc < 4) |
| 165 | return CMD_RET_USAGE; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 166 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 167 | reg = simple_strtoul(argv[3], NULL, 16); |
| 168 | p = pmic_get(argv[2]); |
| 169 | if (!p) |
| 170 | return CMD_RET_FAILURE; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 171 | |
| 172 | ret = pmic_reg_read(p, reg, &val); |
| 173 | |
| 174 | if (ret) |
| 175 | puts("PMIC: Register read failed\n"); |
| 176 | |
| 177 | printf("\n0x%02x: 0x%08x\n", reg, val); |
| 178 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 179 | return CMD_RET_SUCCESS; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | if (strcmp(cmd, "write") == 0) { |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 183 | if (argc < 5) |
| 184 | return CMD_RET_USAGE; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 185 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 186 | reg = simple_strtoul(argv[3], NULL, 16); |
| 187 | val = simple_strtoul(argv[4], NULL, 16); |
| 188 | p = pmic_get(argv[2]); |
| 189 | if (!p) |
| 190 | return CMD_RET_FAILURE; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 191 | pmic_reg_write(p, reg, val); |
| 192 | |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 193 | return CMD_RET_SUCCESS; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* No subcommand found */ |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 197 | return CMD_RET_SUCCESS; |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | U_BOOT_CMD( |
| 201 | pmic, CONFIG_SYS_MAXARGS, 1, do_pmic, |
| 202 | "PMIC", |
Łukasz Majewski | 1c6dba1 | 2012-11-13 03:21:55 +0000 | [diff] [blame^] | 203 | "list - list available PMICs\n" |
| 204 | "pmic dump name - dump named PMIC registers\n" |
| 205 | "pmic name read <reg> - read register\n" |
| 206 | "pmic name write <reg> <value> - write register" |
Łukasz Majewski | 16db062 | 2011-10-06 02:37:34 +0000 | [diff] [blame] | 207 | ); |