blob: dca0a1d57e7d9ff81348261799b3f1266e208463 [file] [log] [blame]
Stefano Babic28bb6d32010-04-04 23:08:03 +02001/*
2 * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
3 *
4 * See file CREDITS for list of people who contributed to this
5 * project.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23#include <config.h>
24#include <common.h>
25#include <spi.h>
26#include <asm/errno.h>
27#include <linux/types.h>
28#include <fsl_pmic.h>
29
30static struct spi_slave *slave;
31
32struct spi_slave *pmic_spi_probe(void)
33{
34 return spi_setup_slave(CONFIG_FSL_PMIC_BUS,
35 CONFIG_FSL_PMIC_CS,
36 CONFIG_FSL_PMIC_CLK,
37 CONFIG_FSL_PMIC_MODE);
38}
39
40void pmic_spi_free(struct spi_slave *slave)
41{
42 if (slave)
43 spi_free_slave(slave);
44}
45
46u32 pmic_reg(u32 reg, u32 val, u32 write)
47{
48 u32 pmic_tx, pmic_rx;
49
50 if (!slave) {
51 slave = pmic_spi_probe();
52
53 if (!slave)
54 return -1;
55 }
56
57 if (reg > 63 || write > 1) {
58 printf("<reg num> = %d is invalid. Should be less then 63\n",
59 reg);
60 return -1;
61 }
62
63 if (spi_claim_bus(slave))
64 return -1;
65
66 pmic_tx = (write << 31) | (reg << 25) | (val & 0x00FFFFFF);
67
68 if (spi_xfer(slave, 4 << 3, &pmic_tx, &pmic_rx,
69 SPI_XFER_BEGIN | SPI_XFER_END)) {
70 spi_release_bus(slave);
71 return -1;
72 }
73
74 if (write) {
75 pmic_tx &= ~(1 << 31);
76 if (spi_xfer(slave, 4 << 3, &pmic_tx, &pmic_rx,
77 SPI_XFER_BEGIN | SPI_XFER_END)) {
78 spi_release_bus(slave);
79 return -1;
80 }
81 }
82
83 spi_release_bus(slave);
84 return pmic_rx;
85}
86
87void pmic_reg_write(u32 reg, u32 value)
88{
89 pmic_reg(reg, value, 1);
90}
91
92u32 pmic_reg_read(u32 reg)
93{
94 return pmic_reg(reg, 0, 0);
95}
96
97void pmic_show_pmic_info(void)
98{
99 u32 rev_id;
100
101 rev_id = pmic_reg_read(REG_IDENTIFICATION);
102 printf("PMIC ID: 0x%08x [Rev: ", rev_id);
103 switch (rev_id & 0x1F) {
104 case 0x1:
105 puts("1.0");
106 break;
107 case 0x9:
108 puts("1.1");
109 break;
110 case 0xA:
111 puts("1.2");
112 break;
113 case 0x10:
114 puts("2.0");
115 break;
116 case 0x11:
117 puts("2.1");
118 break;
119 case 0x18:
120 puts("3.0");
121 break;
122 case 0x19:
123 puts("3.1");
124 break;
125 case 0x1A:
126 puts("3.2");
127 break;
128 case 0x2:
129 puts("3.2A");
130 break;
131 case 0x1B:
132 puts("3.3");
133 break;
134 case 0x1D:
135 puts("3.5");
136 break;
137 default:
138 puts("unknown");
139 break;
140 }
141 puts("]\n");
142}
143
144static void pmic_dump(int numregs)
145{
146 u32 val;
147 int i;
148
149 pmic_show_pmic_info();
150 for (i = 0; i < numregs; i++) {
151 val = pmic_reg_read(i);
152 if (!(i % 8))
153 printf ("\n0x%02x: ", i);
154 printf("%08x ", val);
155 }
156 puts("\n");
157}
158
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200159int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stefano Babic28bb6d32010-04-04 23:08:03 +0200160{
161 char *cmd;
162 int nregs;
163 u32 val;
164
165 /* at least two arguments please */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200166 if (argc < 2)
167 return cmd_usage(cmdtp);
Stefano Babic28bb6d32010-04-04 23:08:03 +0200168
169 cmd = argv[1];
170 if (strcmp(cmd, "dump") == 0) {
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200171 if (argc < 3)
172 return cmd_usage(cmdtp);
173
Stefano Babic28bb6d32010-04-04 23:08:03 +0200174 nregs = simple_strtoul(argv[2], NULL, 16);
175 pmic_dump(nregs);
176 return 0;
177 }
178 if (strcmp(cmd, "write") == 0) {
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200179 if (argc < 4)
180 return cmd_usage(cmdtp);
181
Stefano Babic28bb6d32010-04-04 23:08:03 +0200182 nregs = simple_strtoul(argv[2], NULL, 16);
183 val = simple_strtoul(argv[3], NULL, 16);
184 pmic_reg_write(nregs, val);
185 return 0;
186 }
187 /* No subcommand found */
188 return 1;
189}
190
191U_BOOT_CMD(
192 pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
193 "Freescale PMIC (Atlas)",
194 "dump [numregs] dump registers\n"
195 "pmic write <reg> <value> - write register"
196);