]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/pmic/arm/mx25_3stack/v2_0/src/mc34704.c
Initial revision
[karo-tx-redboot.git] / packages / devs / pmic / arm / mx25_3stack / v2_0 / src / mc34704.c
1 //==========================================================================
2 //
3 //      mc34704.c
4 //
5 //      PMIC support on i.MX25 3stack platforms
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41
42 #include <redboot.h>
43 #include <stdlib.h>
44 #include <pkgconf/hal.h>
45 #include <pkgconf/devs_pmic_arm_imx25_3stack.h>
46 #include <cyg/hal/hal_arch.h>
47 #include <cyg/hal/hal_cache.h>
48 #include <cyg/hal/hal_io.h>
49 #include <cyg/hal/fsl_board.h>
50 #include <cyg/io/mxc_i2c.h>
51 #include <cyg/io/mc34704.h>
52
53 #define MC34704_REG_MAX 0x59
54
55 static void mxc_pmic_init(void)
56 {
57         if (CYGHWR_DEVS_PMIC_I2C_PORT >= i2c_num) 
58                 return;
59
60         i2c_init(i2c_base_addr[CYGHWR_DEVS_PMIC_I2C_PORT], 40000); 
61         
62         diag_printf("Turning on PMIC regulators: 1,2,3,4,5\n");
63
64         pmic_reg(0x02, 0x09, 1);
65 }
66
67 RedBoot_init(mxc_pmic_init, RedBoot_INIT_PRIO(100));
68
69 static void do_pmic(int argc, char *argv[]);
70 RedBoot_cmd("pmic",
71             "Read/Write internal PMIC register",
72             "<reg num> [value to be written]",
73             do_pmic);
74
75 static void do_pmic(int argc,char *argv[])
76 {
77         unsigned int reg, temp, val = 0, write = 0;
78
79         if (argc == 1) {
80                 diag_printf("\tRead:  pmic <reg num>\n");
81                 diag_printf("\tWrite: pmic <reg num> <value to be written>\n");
82                 return;
83         }
84
85         if (!parse_num(*(&argv[1]), (unsigned long *)&reg, &argv[1], ":")) {
86                 diag_printf("Error: Invalid parameter\n");
87                 return;
88         }
89
90         if (argc == 3) {
91                 if (!parse_num(*(&argv[2]), (unsigned long *)&val, &argv[2], ":")) {
92                         diag_printf("Error: Invalid parameter\n");
93                         return;
94                 }
95                 write = 1;
96         }
97
98         temp = pmic_reg(reg, val, write);
99
100         diag_printf("\tval: 0x%08x\n\n", temp);
101 }
102
103 static unsigned int pmic_reg_i2c(unsigned int reg, unsigned int val, unsigned int write)
104 {
105         struct mxc_i2c_request rq;
106         rq.dev_addr = CYGHWR_DEVS_PMIC_I2C_ADDR;
107         rq.reg_addr = reg;
108         rq.reg_addr_sz = 1;
109         rq.buffer = (unsigned char *)&val;
110         rq.buffer_sz = 1;
111         write =  write ? I2C_WRITE : I2C_READ;
112         if (i2c_xfer(CYGHWR_DEVS_PMIC_I2C_PORT, &rq, write) != 0) {
113                 diag_printf("Error in I2C transaction\n\n");
114                 return 0;
115         }
116         return val;     
117 }
118
119 /*!
120  * To read/write to a PMIC register. For write, it does another read for the
121  * actual register value.
122  *
123  * @param   reg         register number inside the PMIC
124  * @param   val         data to be written to the register; don't care for read
125  * @param   write       0 for read; 1 for write
126  *
127  * @return              the actual data in the PMIC register
128  */
129 unsigned int pmic_reg(unsigned int reg, unsigned int val, unsigned int write)
130 {
131         if (reg > MC34704_REG_MAX) {
132                 diag_printf("<reg num> = %d is invalid. Should be less than %d\n",
133                         reg, MC34704_REG_MAX);
134                 return 0;
135         }
136         return pmic_reg_i2c(reg, val, write);
137 }