2 * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
4 * See file CREDITS for list of people who contributed to this
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.
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.
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,
25 #include <asm/errno.h>
26 #include <linux/types.h>
29 static int check_param(u32 reg, u32 write)
31 if (reg > 63 || write > 1) {
32 printf("<reg num> = %d is invalid. Should be less then 63\n",
40 #ifdef CONFIG_FSL_PMIC_I2C
43 u32 pmic_reg(u32 reg, u32 val, u32 write)
45 unsigned char buf[4] = { 0 };
48 if (check_param(reg, 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))
58 if (i2c_read(CONFIG_SYS_FSL_PMIC_I2C_ADDR, reg, 1, buf, 3))
60 ret_val = buf[0] << 16 | buf[1] << 8 | buf[2];
65 #else /* SPI interface */
67 static struct spi_slave *slave;
69 struct spi_slave *pmic_spi_probe(void)
71 return spi_setup_slave(CONFIG_FSL_PMIC_BUS,
74 CONFIG_FSL_PMIC_MODE);
77 void pmic_spi_free(struct spi_slave *slave)
80 spi_free_slave(slave);
83 u32 pmic_reg(u32 reg, u32 val, u32 write)
89 slave = pmic_spi_probe();
95 if (check_param(reg, write))
98 if (spi_claim_bus(slave))
101 pmic_tx = (write << 31) | (reg << 25) | (val & 0x00FFFFFF);
103 tmp = cpu_to_be32(pmic_tx);
105 if (spi_xfer(slave, 4 << 3, &tmp, &pmic_rx,
106 SPI_XFER_BEGIN | SPI_XFER_END)) {
107 spi_release_bus(slave);
112 pmic_tx &= ~(1 << 31);
113 tmp = cpu_to_be32(pmic_tx);
114 if (spi_xfer(slave, 4 << 3, &tmp, &pmic_rx,
115 SPI_XFER_BEGIN | SPI_XFER_END)) {
116 spi_release_bus(slave);
121 spi_release_bus(slave);
122 return cpu_to_be32(pmic_rx);
126 void pmic_reg_write(u32 reg, u32 value)
128 pmic_reg(reg, value, 1);
131 u32 pmic_reg_read(u32 reg)
133 return pmic_reg(reg, 0, 0);
136 void pmic_show_pmic_info(void)
140 rev_id = pmic_reg_read(REG_IDENTIFICATION);
141 printf("PMIC ID: 0x%08x [Rev: ", rev_id);
142 switch (rev_id & 0x1F) {
183 static void pmic_dump(int numregs)
188 pmic_show_pmic_info();
189 for (i = 0; i < numregs; i++) {
190 val = pmic_reg_read(i);
192 printf ("\n0x%02x: ", i);
193 printf("%08x ", val);
198 int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
204 /* at least two arguments please */
206 return cmd_usage(cmdtp);
209 if (strcmp(cmd, "dump") == 0) {
211 return cmd_usage(cmdtp);
213 nregs = simple_strtoul(argv[2], NULL, 16);
217 if (strcmp(cmd, "write") == 0) {
219 return cmd_usage(cmdtp);
221 nregs = simple_strtoul(argv[2], NULL, 16);
222 val = simple_strtoul(argv[3], NULL, 16);
223 pmic_reg_write(nregs, val);
226 /* No subcommand found */
231 pmic, CONFIG_SYS_MAXARGS, 1, do_pmic,
232 "Freescale PMIC (Atlas)",
233 "dump [numregs] dump registers\n"
234 "pmic write <reg> <value> - write register"