blob: 23255a59b5588f3eb2d9e8716087cf26457ffe32 [file] [log] [blame]
Stefano Babic6a0034d2010-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>
Stefano Babic6a0034d2010-04-04 23:08:03 +020025#include <asm/errno.h>
26#include <linux/types.h>
27#include <fsl_pmic.h>
28
Liu Hui-R64343e50ced82011-01-03 22:27:40 +000029static int check_param(u32 reg, u32 write)
30{
31 if (reg > 63 || write > 1) {
32 printf("<reg num> = %d is invalid. Should be less then 63\n",
33 reg);
34 return -1;
35 }
36
37 return 0;
38}
39
40#ifdef CONFIG_FSL_PMIC_I2C
41#include <i2c.h>
42
43u32 pmic_reg(u32 reg, u32 val, u32 write)
44{
45 unsigned char buf[4] = { 0 };
46 u32 ret_val = 0;
47
48 if (check_param(reg, write))
49 return -1;
50
51 if (write) {
52 buf[0] = (val >> 16) & 0xff;
53 buf[1] = (val >> 8) & 0xff;
54 buf[2] = (val) & 0xff;
55 if (i2c_write(CONFIG_SYS_FSL_PMIC_I2C_ADDR, reg, 1, buf, 3))
56 return -1;
57 } else {
58 if (i2c_read(CONFIG_SYS_FSL_PMIC_I2C_ADDR, reg, 1, buf, 3))
59 return -1;
60 ret_val = buf[0] << 16 | buf[1] << 8 | buf[2];
61 }
62
63 return ret_val;
64}
65#else /* SPI interface */
66#include <spi.h>
Stefano Babic6a0034d2010-04-04 23:08:03 +020067static struct spi_slave *slave;
68
69struct spi_slave *pmic_spi_probe(void)
70{
71 return spi_setup_slave(CONFIG_FSL_PMIC_BUS,
72 CONFIG_FSL_PMIC_CS,
73 CONFIG_FSL_PMIC_CLK,
74 CONFIG_FSL_PMIC_MODE);
75}
76
77void pmic_spi_free(struct spi_slave *slave)
78{
79 if (slave)
80 spi_free_slave(slave);
81}
82
83u32 pmic_reg(u32 reg, u32 val, u32 write)
84{
85 u32 pmic_tx, pmic_rx;
Stefano Babic125f82a2010-08-20 12:05:03 +020086 u32 tmp;
Stefano Babic6a0034d2010-04-04 23:08:03 +020087
88 if (!slave) {
89 slave = pmic_spi_probe();
90
91 if (!slave)
92 return -1;
93 }
94
Liu Hui-R64343e50ced82011-01-03 22:27:40 +000095 if (check_param(reg, write))
Stefano Babic6a0034d2010-04-04 23:08:03 +020096 return -1;
Stefano Babic6a0034d2010-04-04 23:08:03 +020097
98 if (spi_claim_bus(slave))
99 return -1;
100
101 pmic_tx = (write << 31) | (reg << 25) | (val & 0x00FFFFFF);
102
Stefano Babic125f82a2010-08-20 12:05:03 +0200103 tmp = cpu_to_be32(pmic_tx);
104
105 if (spi_xfer(slave, 4 << 3, &tmp, &pmic_rx,
Stefano Babic6a0034d2010-04-04 23:08:03 +0200106 SPI_XFER_BEGIN | SPI_XFER_END)) {
107 spi_release_bus(slave);
108 return -1;
109 }
110
111 if (write) {
112 pmic_tx &= ~(1 << 31);
Stefano Babic125f82a2010-08-20 12:05:03 +0200113 tmp = cpu_to_be32(pmic_tx);
114 if (spi_xfer(slave, 4 << 3, &tmp, &pmic_rx,
Stefano Babic6a0034d2010-04-04 23:08:03 +0200115 SPI_XFER_BEGIN | SPI_XFER_END)) {
116 spi_release_bus(slave);
117 return -1;
118 }
119 }
120
121 spi_release_bus(slave);
Stefano Babic125f82a2010-08-20 12:05:03 +0200122 return cpu_to_be32(pmic_rx);
Stefano Babic6a0034d2010-04-04 23:08:03 +0200123}
Liu Hui-R64343e50ced82011-01-03 22:27:40 +0000124#endif
Stefano Babic6a0034d2010-04-04 23:08:03 +0200125
126void pmic_reg_write(u32 reg, u32 value)
127{
128 pmic_reg(reg, value, 1);
129}
130
131u32 pmic_reg_read(u32 reg)
132{
133 return pmic_reg(reg, 0, 0);
134}
135
136void pmic_show_pmic_info(void)
137{
138 u32 rev_id;
139
140 rev_id = pmic_reg_read(REG_IDENTIFICATION);
141 printf("PMIC ID: 0x%08x [Rev: ", rev_id);
142 switch (rev_id & 0x1F) {
143 case 0x1:
144 puts("1.0");
145 break;
146 case 0x9:
147 puts("1.1");
148 break;
149 case 0xA:
150 puts("1.2");
151 break;
152 case 0x10:
153 puts("2.0");
154 break;
155 case 0x11:
156 puts("2.1");
157 break;
158 case 0x18:
159 puts("3.0");
160 break;
161 case 0x19:
162 puts("3.1");
163 break;
164 case 0x1A:
165 puts("3.2");
166 break;
167 case 0x2:
168 puts("3.2A");
169 break;
170 case 0x1B:
171 puts("3.3");
172 break;
173 case 0x1D:
174 puts("3.5");
175 break;
176 default:
177 puts("unknown");
178 break;
179 }
180 puts("]\n");
181}
182
183static void pmic_dump(int numregs)
184{
185 u32 val;
186 int i;
187
188 pmic_show_pmic_info();
189 for (i = 0; i < numregs; i++) {
190 val = pmic_reg_read(i);
191 if (!(i % 8))
192 printf ("\n0x%02x: ", i);
193 printf("%08x ", val);
194 }
195 puts("\n");
196}
197
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200198int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Stefano Babic6a0034d2010-04-04 23:08:03 +0200199{
200 char *cmd;
201 int nregs;
202 u32 val;
203
204 /* at least two arguments please */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200205 if (argc < 2)
206 return cmd_usage(cmdtp);
Stefano Babic6a0034d2010-04-04 23:08:03 +0200207
208 cmd = argv[1];
209 if (strcmp(cmd, "dump") == 0) {
Wolfgang Denk3b683112010-07-17 01:06:04 +0200210 if (argc < 3)
211 return cmd_usage(cmdtp);
212
Stefano Babic6a0034d2010-04-04 23:08:03 +0200213 nregs = simple_strtoul(argv[2], NULL, 16);
214 pmic_dump(nregs);
215 return 0;
216 }
217 if (strcmp(cmd, "write") == 0) {
Wolfgang Denk3b683112010-07-17 01:06:04 +0200218 if (argc < 4)
219 return cmd_usage(cmdtp);
220
Stefano Babic6a0034d2010-04-04 23:08:03 +0200221 nregs = simple_strtoul(argv[2], NULL, 16);
222 val = simple_strtoul(argv[3], NULL, 16);
223 pmic_reg_write(nregs, val);
224 return 0;
225 }
226 /* No subcommand found */
227 return 1;
228}
229
230U_BOOT_CMD(
231 pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
232 "Freescale PMIC (Atlas)",
Jason Liu6d346a72011-09-21 19:30:53 +0000233 "dump [numregs] - dump registers\n"
Stefano Babic6a0034d2010-04-04 23:08:03 +0200234 "pmic write <reg> <value> - write register"
235);