source: svn/trunk/newcon3bcm2_21bu/magnum/portinginterface/led/7552/bled.c

Last change on this file was 2, checked in by jglee, 11 years ago

first commit

  • Property svn:executable set to *
File size: 6.1 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2003-2011, Broadcom Corporation
3 *     All Rights Reserved
4 *     Confidential Property of Broadcom Corporation
5 *
6 *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
7 *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
8 *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9 *
10 * $brcm_Workfile: bled.c $
11 * $brcm_Revision: Hydra_Software_Devel/6 $
12 * $brcm_Date: 1/14/11 7:57p $
13 *
14 * Module Description:
15 *
16 * Revision History:
17 *
18 * $brcm_Log: /magnum/portinginterface/led/7422/bled.c $
19 *
20 * Hydra_Software_Devel/6   1/14/11 7:57p brianlee
21 * SW7422-197: Make sure to zero out upper bits of LDK_CONTROL during
22 * initialization.
23 *
24 * Hydra_Software_Devel/5   6/5/06 4:28p erickson
25 * PR21941: cleaned up minor warnings
26 *
27 * Hydra_Software_Devel/4   5/27/04 5:15p brianlee
28 * PR11238: Fixed code to work with 7038 B0.
29 *
30 * Hydra_Software_Devel/3   11/4/03 6:59p brianlee
31 * Get rid of enter/leave macros.
32 *
33 * Hydra_Software_Devel/2   9/30/03 11:21a brianlee
34 * Fixed a warning.
35 *
36 * Hydra_Software_Devel/1   9/23/03 10:17a brianlee
37 * Initial version.  This module includes stubs for now since 7038 A0 does
38 * not contain LED module.
39 *
40 ***************************************************************************/
41#include "bstd.h"
42#include "bled.h"
43#include "bchp_ldk.h"
44
45
46BDBG_MODULE(bled);
47
48#define DEV_MAGIC_ID                    ((BERR_LED_ID<<16) | 0xFACE)
49
50#define BLED_CHK_RETCODE( rc, func )            \
51do {                                                                            \
52        if( (rc = BERR_TRACE(func)) != BERR_SUCCESS ) \
53        {                                                                               \
54                goto done;                                                      \
55        }                                                                               \
56} while(0)
57
58/* Default values */
59#define LED_DEFAULT_PRESCALE_HI         0x00
60#define LED_DEFAULT_PRESCALE_LO         0x55
61#define LED_DEFAULT_DUTYCYCLE_OFF       0x01
62#define LED_DEFAULT_DUTYCYCLE_ON        0xAA
63#define LED_DEFAULT_DEBOUNCE            0x40
64
65/*******************************************************************************
66*
67*       Private Module Handles
68*
69*******************************************************************************/
70
71typedef struct BLED_P_Handle
72{
73        uint32_t                magicId;                                        /* Used to check if structure is corrupt */
74        BCHP_Handle     hChip;
75        BREG_Handle             hRegister;
76} BLED_P_Handle;
77
78/*******************************************************************************
79*
80*       Default Module Settings
81*
82*******************************************************************************/
83static const BLED_Settings defLedSettings = 
84{
85        100                                                                     /* percent brightness */
86};
87
88/*******************************************************************************
89*
90*       Public Module Functions
91*
92*******************************************************************************/
93BERR_Code BLED_Open(
94        BLED_Handle *pLed,                                      /* [output] Returns handle */
95        BCHP_Handle hChip,                                      /* Chip handle */
96        BREG_Handle hRegister,                          /* Register handle */
97        const BLED_Settings *pDefSettings       /* Default settings */
98        )
99{
100        BERR_Code               retCode = BERR_SUCCESS;
101        BLED_Handle     hDev;
102        uint32_t                lval;
103
104        /* Sanity check on the handles we've been given. */
105        BDBG_ASSERT( hChip );
106        BDBG_ASSERT( hRegister );
107
108        /* Alloc memory from the system heap */
109        hDev = (BLED_Handle) BKNI_Malloc( sizeof( BLED_P_Handle ) );
110        if( hDev == NULL )
111        {
112                *pLed = NULL;
113                retCode = BERR_TRACE(BERR_OUT_OF_SYSTEM_MEMORY);
114                BDBG_ERR(("BLED_Open: BKNI_malloc() failed\n"));
115                goto done;
116        }
117
118        hDev->magicId   = DEV_MAGIC_ID;
119        hDev->hChip             = hChip;
120        hDev->hRegister = hRegister;
121        *pLed = hDev;
122
123        /* Reset LED/Keypad core */
124        lval = BREG_Read32 (hDev->hRegister, BCHP_LDK_CONTROL);
125        lval |= BCHP_LDK_CONTROL_swr_MASK;
126        lval &= 0x0f;                                           /* only keep lower 4 bits */
127        BREG_Write32 (hDev->hRegister, BCHP_LDK_CONTROL, lval);
128       
129        lval &= ~BCHP_LDK_CONTROL_swr_MASK;
130        lval |=  BCHP_LDK_CONTROL_ver_MASK;
131        BREG_Write32 (hDev->hRegister, BCHP_LDK_CONTROL, lval);
132
133        /* Set up LED */
134        BREG_Write32 (hDev->hRegister, BCHP_LDK_PRESCHI, LED_DEFAULT_PRESCALE_HI);
135        BREG_Write32 (hDev->hRegister, BCHP_LDK_PRESCLO, LED_DEFAULT_PRESCALE_LO);
136
137        BLED_AdjustBrightness (hDev, pDefSettings->percentBrightness);
138
139done:
140        return( retCode );
141}
142
143BERR_Code BLED_Close(
144        BLED_Handle hDev                                        /* Device handle */
145        )
146{
147        BERR_Code retCode = BERR_SUCCESS;
148
149
150        BDBG_ASSERT( hDev );
151        BDBG_ASSERT( hDev->magicId == DEV_MAGIC_ID );
152
153        BKNI_Free( (void *) hDev );
154
155        return( retCode );
156}
157
158BERR_Code BLED_GetDefaultSettings(
159        BLED_Settings *pDefSettings,            /* [output] Returns default setting */
160        BCHP_Handle hChip                                       /* Chip handle */
161)
162{
163        BERR_Code retCode = BERR_SUCCESS;
164
165        BSTD_UNUSED(hChip);
166
167        *pDefSettings = defLedSettings;
168
169        return( retCode );
170}
171
172BERR_Code BLED_Write ( 
173        BLED_Handle             hLed,                   /* Device handle */
174        uint8_t                         digit,                  /* digit to write to */
175        uint8_t                         value                   /* value to write */
176)
177{
178        uint32_t        offset;
179       
180        switch (digit)
181        {
182                case 1:
183                        offset = BCHP_LDK_DIGIT1;
184                        break;
185
186                case 2:
187                        offset = BCHP_LDK_DIGIT2;
188                        break;
189                       
190                case 3:
191                        offset = BCHP_LDK_DIGIT3;
192                        break;
193
194                case 4:
195                        offset = BCHP_LDK_DIGIT4;
196                        break;
197                       
198                default:
199                        return BERR_INVALID_PARAMETER;
200        }
201       
202        BREG_Write32 (hLed->hRegister, offset, (uint32_t)value);
203
204        return BERR_SUCCESS;
205}
206
207BERR_Code BLED_AdjustBrightness ( 
208        BLED_Handle             hLed,                           /* Device handle */
209        uint8_t                         percentBrightness       /* percent of brightness */
210)
211{
212        uint8_t                         ucDutyCycleOn;
213        uint8_t                         ucDutyCycleOff;
214        uint32_t                        dutyCycleClks;
215        uint32_t                        valueOn;
216
217        dutyCycleClks   = LED_DEFAULT_DUTYCYCLE_ON + LED_DEFAULT_DUTYCYCLE_OFF;
218
219        valueOn                 = dutyCycleClks * percentBrightness / 100;
220        ucDutyCycleOn   = (uint8_t)valueOn;
221        ucDutyCycleOff  = dutyCycleClks - ucDutyCycleOn;
222
223        BREG_Write32 (hLed->hRegister, BCHP_LDK_DUTYOFF, (uint32_t)ucDutyCycleOff);
224        BREG_Write32 (hLed->hRegister, BCHP_LDK_DUTYON,  (uint32_t)ucDutyCycleOn);
225
226        return BERR_SUCCESS;
227}
228
229BERR_Code BLED_SetDiscreteLED ( 
230        BLED_Handle             hLed,                   /* Device handle */
231        bool                            on,                             /* turn on or off */
232        uint8_t                         ledStatusBit    /* bit to turn on or off */
233)
234{
235        uint32_t                        lval;
236
237        if (ledStatusBit > 7)
238                return BERR_INVALID_PARAMETER;
239               
240        lval = BREG_Read32 (hLed->hRegister, BCHP_LDK_STATUS);
241        if (on)
242                lval &= ~(1 << ledStatusBit);
243        else
244                lval |= (1 << ledStatusBit);
245
246        BREG_Write32 (hLed->hRegister, BCHP_LDK_STATUS, lval);
247
248        return BERR_SUCCESS;
249}
Note: See TracBrowser for help on using the repository browser.