[processor-sdk/performance-audio-sr.git] / psdk_cust / pdk_k2g_1_0_1_2_eng / packages / ti / board / src / idkAM572x / device / pmic_device.c
1 /**
2 * \file pmic_device.c
3 *
4 * \brief This file contains PRCM functions used in SBL to enable clocks
5 * to the slave cores.
6 *
7 */
9 /*
10 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the
22 * distribution.
23 *
24 * Neither the name of Texas Instruments Incorporated nor the names of
25 * its contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 */
42 /* TI RTOS header files */
43 #include <stdint.h>
44 #include <stdio.h>
46 #include "pmic_device.h"
48 /* ========================================================================== */
49 /* Macros & Typedefs */
50 /* ========================================================================== */
52 /* Standard offset is 0.5v expressed in uv */
53 #define PALMAS_SMPS_BASE_VOLT_UV 500000
55 /* I2C instance to which PMIC is connected. */
56 #define PMIC_DEV_INSTANCE 1
58 /* ========================================================================== */
59 /* Global Variables */
60 /* ========================================================================== */
61 extern const I2C_Config I2C_config[];
63 I2C_Handle handle = NULL;
65 /* ========================================================================== */
66 /* Internal Functions */
67 /* ========================================================================== */
69 /**
70 * \brief This API initializes the I2C instance associated with the pmic
71 * device connected on the board.
72 *
73 * \param instance I2C instance.
74 */
75 void Board_PMICDevOpen(uint32_t instance);
77 /**
78 * \brief This API closes the communication interface to the on board PMIC
79 * device.
80 *
81 */
82 void Board_PMICDevClose();
84 /**
85 * \brief This API returns the offset value for given mV to be written to
86 * the pmic register.
87 *
88 * \param instance I2C instance.
89 */
90 uint32_t Board_GetPMICOffset(uint32_t voltOffset, pmic_data_t *pmic);
92 /**
93 * \brief This API sets the voltage value to the PMIC register.
94 *
95 * \param slaveAddr Slave address of the PMIC device.
96 * \param regAddr register address of the pmic
97 * \param value value to be written to the pmic register.
98 */
99 bool Board_SetPMICVoltage(uint32_t slaveAddr, uint8_t regAddr, uint8_t value);
101 pmic_data_t tps659039 = {
102 PALMAS_SMPS_BASE_VOLT_UV,
103 10000, /* 10 mV represented in uV */
104 /*
105 * Offset codes 1-6 all give the base voltage in Palmas
106 * Offset code 0 switches OFF the SMPS
107 */
108 6,
109 TPS659039_I2C_SLAVE_ADDR,
110 PMIC_DEV_INSTANCE,
111 Board_PMICDevOpen,
112 Board_SetPMICVoltage,
113 Board_GetPMICOffset,
114 Board_PMICDevClose
115 };
117 pmic_data_t* Board_GetPmicData()
118 {
119 return &tps659039;
120 }
122 void Board_PMICDevOpen(uint32_t instance)
123 {
124 I2C_Params i2cParams;
125 int i;
127 for (i=0; I2C_config[i].fxnTablePtr != NULL; i++)
128 {
129 ((I2C_HwAttrs *)I2C_config[i].hwAttrs)->enableIntr = false;
130 }
132 I2C_init();
134 I2C_Params_init(&i2cParams);
136 handle = I2C_open(instance, &i2cParams);
137 }
139 void Board_PMICDevClose()
140 {
141 I2C_close(handle);
142 }
144 uint32_t Board_GetPMICOffset(uint32_t voltOffset, pmic_data_t *pmic)
145 {
146 uint32_t offsetCode;
148 offsetCode = (26 + ((voltOffset - 700)/10)) & 0x7F;
150 return offsetCode;
151 }
153 bool Board_SetPMICVoltage(uint32_t slaveAddr, uint8_t regAddr, uint8_t value)
154 {
155 bool status = false;
156 I2C_Transaction i2cTransaction;
157 uint8_t txBuf[2] = {0x00, 0x00};
159 i2cTransaction.slaveAddress = slaveAddr;
160 i2cTransaction.writeBuf = (uint8_t *)&txBuf[0];
161 i2cTransaction.writeCount = 2;
162 i2cTransaction.readCount = 0;
164 /* Set the address and the value to be written. */
165 txBuf[0] = regAddr;
166 txBuf[1] = value;
168 status = I2C_transfer(handle, &i2cTransaction);
170 return status;
171 }