1 /*******************************************************************************
2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Except as contained in this notice, the name of Maxim Integrated
23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
24 * Products, Inc. Branding Policy.
25 *
26 * The mere transfer of this software does not imply any licenses
27 * of trade secrets, proprietary technology, copyrights, patents,
28 * trademarks, maskwork rights, or any other form of intellectual
29 * property whatsoever. Maxim Integrated Products, Inc. retains all
30 * ownership rights.
31 *
32 * $Date: 2016-03-11 10:46:37 -0700 (Fri, 11 Mar 2016) $
33 * $Revision: 21839 $
34 *
35 ******************************************************************************/
36
37 /***** Includes *****/
38 #include <stddef.h>
39 #include "mxc_config.h"
40 #include "mxc_sys.h"
41 #include "max14690n.h"
42 #include "board.h"
43 #include "i2cm.h"
44 #include "lp.h"
45
46 /***** Definitions *****/
47 #define MAX14690_I2C_ADDR (0x50 >> 1)
48 #define MAX14690_ADDR_ID 0x00
49
50 #define MAX14690_ADDR_LDO2 0x14
51 #define MAX14690_LDO_EN 0x02
52
53 /***** Function Prototypes *****/
54 static void VBUS_Interrupt(void *unused);
55
56
57 /******************************************************************************/
MAX14690N_Init(float ldo2v,ldo_enable_mode_t ldo2en,float ldo3v,ldo_enable_mode_t ldo3en)58 int MAX14690N_Init(float ldo2v, ldo_enable_mode_t ldo2en, float ldo3v, ldo_enable_mode_t ldo3en)
59 {
60 uint8_t addr;
61 uint8_t data[2];
62
63 /* Setup the I2CM Peripheral to talk to the MAX14690 */
64 sys_cfg_i2cm_t cfg;
65 cfg.clk_scale = CLKMAN_SCALE_DIV_1;
66 cfg.io_cfg = max14690_io_cfg;
67 I2CM_Init(MAX14690_I2CM, &cfg, I2CM_SPEED_100KHZ);
68
69 /* Attempt to read the ID from the device */
70 addr = MAX14690_ADDR_ID;
71 if (I2CM_Read(MAX14690_I2CM, MAX14690_I2C_ADDR, &addr, 1, data, 2) != 2) {
72 return E_COMM_ERR;
73 }
74
75 /* Configure the initial state of LDO2 */
76 if ((0.8 <= ldo2v)&&(ldo2v <= 3.6)){
77 data[0] = MAX14690_REG_LDO2_VSET;
78 data[1] = (10 * ldo2v) - 8;
79 if (I2CM_Write(MAX14690_I2CM, MAX14690_I2C_ADDR, NULL, 0, data, 2) != 2) {
80 return -1;
81 }
82 }
83 data[0] = MAX14690_REG_LDO2_CFG;
84 data[1] = ldo2en;
85 if (I2CM_Write(MAX14690_I2CM, MAX14690_I2C_ADDR, NULL, 0, data, 2) != 2) {
86 return -1;
87 }
88
89 /* Configure the initial state of LDO3 */
90 if ((0.8 <= ldo3v)&&(ldo3v <= 3.6)){
91 data[0] = MAX14690_REG_LDO3_VSET;
92 data[1] = (10 * ldo3v) - 8;
93 if (I2CM_Write(MAX14690_I2CM, MAX14690_I2C_ADDR, NULL, 0, data, 2) != 2) {
94 return -1;
95 }
96 } else {
97 return -1;
98 }
99 data[0] = MAX14690_REG_LDO3_CFG;
100 data[1] = ldo3en;
101 if (I2CM_Write(MAX14690_I2CM, MAX14690_I2C_ADDR, NULL, 0, data, 2) != 2) {
102 return -1;
103 }
104
105 VBUS_Interrupt(NULL);
106
107 /* Configure GPIO for interrupt pin from PMIC */
108 if (GPIO_Config(&max14690_int) != E_NO_ERROR) {
109 return E_UNKNOWN;
110 }
111
112 /* Configure and enable interrupt */
113 GPIO_RegisterCallback(&max14690_int, VBUS_Interrupt, NULL);
114 GPIO_IntConfig(&max14690_int, GPIO_INT_FALLING_EDGE);
115 GPIO_IntEnable(&max14690_int);
116 NVIC_EnableIRQ(MXC_GPIO_GET_IRQ(max14690_int.port));
117
118 /* Configure interrupt wakeup */
119 if (LP_ConfigGPIOWakeUpDetect(&max14690_int, 0, LP_WEAK_PULL_UP) != E_NO_ERROR) {
120 return E_UNKNOWN;
121 }
122
123 /* Enable the VBUS interrupt */
124 data[0] = 0x07; /* IntMaskA */
125 data[1] = 0x08; /* UsbOk */
126 if (I2CM_Write(MAX14690_I2CM, MAX14690_I2C_ADDR, NULL, 0, data, 2) != 2) {
127 return -1;
128 }
129
130 return E_NO_ERROR;
131 }
132
133 /******************************************************************************/
VBUS_Interrupt(void * unused)134 static void VBUS_Interrupt(void *unused)
135 {
136 uint8_t addr = 0x02; /* StatusA */
137 uint8_t data[5];
138
139 if (I2CM_Read(MAX14690_I2CM, MAX14690_I2C_ADDR, &addr, 1, data, sizeof(data)) == sizeof(data)) {
140 if (data[1] & 0x08) { /* UsbOk */
141 /* VBUS is present. Enable LDO2 */
142 // MAX14690_EnableLDO2(1);
143 } else {
144 /* VBUS is not present. Disable LDO2 */
145 // MAX14690_EnableLDO2(0);
146 }
147 }
148 }
149
150 /******************************************************************************/
MAX14690_LDO2setMode(ldo_enable_mode_t mode)151 int MAX14690_LDO2setMode(ldo_enable_mode_t mode)
152 {
153 int retval;
154 uint8_t data[2] = {MAX14690_REG_LDO2_CFG, mode};
155
156 retval = I2CM_Write(MAX14690_I2CM, MAX14690_I2C_ADDR, NULL, 0, data, 2);
157 if(retval != 2) {
158 return retval;
159 }
160
161 return E_NO_ERROR;
162 }
163
164 /******************************************************************************/
MAX14690_LDO2setV(float voltage)165 int MAX14690_LDO2setV(float voltage)
166 {
167 int retval;
168 uint8_t data[2] = {MAX14690_REG_LDO2_VSET, 0};
169
170 if ((0.8 <= voltage)&&(voltage <= 3.6)){
171 data[1] = (10 * voltage) - 8;
172 } else {
173 return -1;
174 }
175
176 retval = I2CM_Write(MAX14690_I2CM, MAX14690_I2C_ADDR, NULL, 0, data, 2);
177 if(retval != 2) {
178 return retval;
179 }
180
181 return E_NO_ERROR;
182 }
183
184 /******************************************************************************/
MAX14690_LDO3setMode(ldo_enable_mode_t mode)185 int MAX14690_LDO3setMode(ldo_enable_mode_t mode)
186 {
187 int retval;
188 uint8_t data[2] = {MAX14690_REG_LDO3_CFG, mode};
189
190 retval = I2CM_Write(MAX14690_I2CM, MAX14690_I2C_ADDR, NULL, 0, data, 2);
191 if(retval != 2) {
192 return retval;
193 }
194
195 return E_NO_ERROR;
196 }
197
198 /******************************************************************************/
MAX14690_LDO3setV(float voltage)199 int MAX14690_LDO3setV(float voltage)
200 {
201 int retval;
202 uint8_t data[2] = {MAX14690_REG_LDO3_VSET, 0};
203
204 if ((0.8 <= voltage)&&(voltage <= 3.6)){
205 data[1] = (10 * voltage) - 8;
206 } else {
207 return -1;
208 }
209
210 retval = I2CM_Write(MAX14690_I2CM, MAX14690_I2C_ADDR, NULL, 0, data, 2);
211 if(retval != 2) {
212 return retval;
213 }
214
215 return E_NO_ERROR;
216 }
217
218 /******************************************************************************/
MAX14690_MuxSet(max14690_mux_ch_t ch,max14690_mux_div_t div)219 int MAX14690_MuxSet(max14690_mux_ch_t ch, max14690_mux_div_t div)
220 {
221 int retval;
222 uint8_t data[2] = {MAX14690_REG_MON_CFG, 0};
223
224 data[1] = (div << 4) + ch;
225
226 retval = I2CM_Write(MAX14690_I2CM, MAX14690_I2C_ADDR, NULL, 0, data, 2);
227 if(retval != 2) {
228 return retval;
229 }
230
231 return E_NO_ERROR;
232 }
233