Ć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> |
| 30 | #include <linux/types.h> |
| 31 | #include <pmic.h> |
| 32 | |
| 33 | static struct pmic pmic; |
| 34 | |
| 35 | int check_reg(u32 reg) |
| 36 | { |
| 37 | if (reg >= pmic.number_of_regs) { |
| 38 | printf("<reg num> = %d is invalid. Should be less than %d\n", |
| 39 | reg, pmic.number_of_regs); |
| 40 | return -1; |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | int pmic_set_output(struct pmic *p, u32 reg, int out, int on) |
| 46 | { |
| 47 | u32 val; |
| 48 | |
| 49 | if (pmic_reg_read(p, reg, &val)) |
| 50 | return -1; |
| 51 | |
| 52 | if (on) |
| 53 | val |= out; |
| 54 | else |
| 55 | val &= ~out; |
| 56 | |
| 57 | if (pmic_reg_write(p, reg, val)) |
| 58 | return -1; |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static void pmic_show_info(struct pmic *p) |
| 64 | { |
| 65 | printf("PMIC: %s\n", p->name); |
| 66 | } |
| 67 | |
| 68 | static void pmic_dump(struct pmic *p) |
| 69 | { |
| 70 | int i, ret; |
| 71 | u32 val; |
| 72 | |
| 73 | pmic_show_info(p); |
| 74 | for (i = 0; i < p->number_of_regs; i++) { |
| 75 | ret = pmic_reg_read(p, i, &val); |
| 76 | if (ret) |
| 77 | puts("PMIC: Registers dump failed\n"); |
| 78 | |
| 79 | if (!(i % 8)) |
| 80 | printf("\n0x%02x: ", i); |
| 81 | |
| 82 | printf("%08x ", val); |
| 83 | } |
| 84 | puts("\n"); |
| 85 | } |
| 86 | |
| 87 | struct pmic *get_pmic(void) |
| 88 | { |
| 89 | return &pmic; |
| 90 | } |
| 91 | |
| 92 | int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 93 | { |
| 94 | u32 ret, reg, val; |
| 95 | char *cmd; |
| 96 | |
| 97 | struct pmic *p = &pmic; |
| 98 | |
| 99 | /* at least two arguments please */ |
| 100 | if (argc < 2) |
| 101 | return cmd_usage(cmdtp); |
| 102 | |
| 103 | cmd = argv[1]; |
| 104 | if (strcmp(cmd, "dump") == 0) { |
| 105 | pmic_dump(p); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | if (strcmp(cmd, "read") == 0) { |
| 110 | if (argc < 3) |
| 111 | return cmd_usage(cmdtp); |
| 112 | |
| 113 | reg = simple_strtoul(argv[2], NULL, 16); |
| 114 | |
| 115 | ret = pmic_reg_read(p, reg, &val); |
| 116 | |
| 117 | if (ret) |
| 118 | puts("PMIC: Register read failed\n"); |
| 119 | |
| 120 | printf("\n0x%02x: 0x%08x\n", reg, val); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | if (strcmp(cmd, "write") == 0) { |
| 126 | if (argc < 4) |
| 127 | return cmd_usage(cmdtp); |
| 128 | |
| 129 | reg = simple_strtoul(argv[2], NULL, 16); |
| 130 | val = simple_strtoul(argv[3], NULL, 16); |
| 131 | |
| 132 | pmic_reg_write(p, reg, val); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /* No subcommand found */ |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | U_BOOT_CMD( |
| 142 | pmic, CONFIG_SYS_MAXARGS, 1, do_pmic, |
| 143 | "PMIC", |
| 144 | "dump - dump PMIC registers\n" |
| 145 | "pmic read <reg> - read register\n" |
| 146 | "pmic write <reg> <value> - write register" |
| 147 | ); |