]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/spi/imx_spi_pmic.c
Unified codebase for TX28, TX48, TX51, TX53
[karo-tx-uboot.git] / drivers / spi / imx_spi_pmic.c
1 /*
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
29 #include <imx_spi.h>
30
31 static u32 pmic_tx, pmic_rx;
32
33 /*!
34  * To read/write to a PMIC register. For write, it does another read for the
35  * actual register value.
36  *
37  * @param       reg                     register number inside the PMIC
38  * @param       val                     data to be written to the register; don't care for read
39  * @param       write           0 for read; 1 for write
40  *
41  * @return                              the actual data in the PMIC register
42  */
43 u32 pmic_reg(struct spi_slave *slave, u32 reg, u32 val, u32 write)
44 {
45         if (!slave)
46                 return 0;
47
48         if (reg > 63 || write > 1) {
49                 printf("<reg num> = %d is invalide. Should be less then 63\n",
50                         reg);
51                 return 0;
52         }
53         pmic_tx = (write << 31) | (reg << 25) | (val & 0x00FFFFFF);
54         debug("reg=0x%x, val=0x%08x\n", reg, pmic_tx);
55
56         if (spi_xfer(slave, 4 << 3, (u8 *)&pmic_tx, (u8 *)&pmic_rx,
57                         SPI_XFER_BEGIN | SPI_XFER_END)) {
58                 return -1;
59         }
60
61         if (write) {
62                 pmic_tx &= ~(1 << 31);
63                 if (spi_xfer(slave, 4 << 3, (u8 *)&pmic_tx, (u8 *)&pmic_rx,
64                         SPI_XFER_BEGIN | SPI_XFER_END)) {
65                         return -1;
66                 }
67         }
68
69         return pmic_rx;
70 }
71
72 void show_pmic_info(struct spi_slave *slave)
73 {
74         volatile u32 rev_id;
75
76         if (!slave)
77                 return;
78
79         rev_id = pmic_reg(slave, 7, 0, 0);
80         debug("PMIC ID: 0x%08x [Rev: ", rev_id);
81         switch (rev_id & 0x1F) {
82         case 0x1:
83                 printf("1.0");
84                 break;
85         case 0x9:
86                 printf("1.1");
87                 break;
88         case 0xA:
89                 printf("1.2");
90                 break;
91         case 0x10:
92                 printf("2.0");
93                 break;
94         case 0x11:
95                 printf("2.1");
96                 break;
97         case 0x18:
98                 printf("3.0");
99                 break;
100         case 0x19:
101                 printf("3.1");
102                 break;
103         case 0x1A:
104                 printf("3.2");
105                 break;
106         case 0x2:
107                 printf("3.2A");
108                 break;
109         case 0x1B:
110                 printf("3.3");
111                 break;
112         case 0x1D:
113                 printf("3.5");
114                 break;
115         default:
116                 printf("unknown");
117                 break;
118         }
119         printf("]\n");
120 }
121
122 struct spi_slave *spi_pmic_probe(void)
123 {
124         return spi_setup_slave(0, CONFIG_IMX_SPI_PMIC_CS, 2500000, 0);
125 }
126
127 void spi_pmic_free(struct spi_slave *slave)
128 {
129         if (slave)
130                 spi_free_slave(slave);
131 }