| 1 | /*************************************************************************** |
|---|
| 2 | * (c)2007-2010 Broadcom Corporation |
|---|
| 3 | * |
|---|
| 4 | * This program is the proprietary software of Broadcom Corporation and/or its licensors, |
|---|
| 5 | * and may only be used, duplicated, modified or distributed pursuant to the terms and |
|---|
| 6 | * conditions of a separate, written license agreement executed between you and Broadcom |
|---|
| 7 | * (an "Authorized License"). Except as set forth in an Authorized License, Broadcom grants |
|---|
| 8 | * no license (express or implied), right to use, or waiver of any kind with respect to the |
|---|
| 9 | * Software, and Broadcom expressly reserves all rights in and to the Software and all |
|---|
| 10 | * intellectual property rights therein. IF YOU HAVE NO AUTHORIZED LICENSE, THEN YOU |
|---|
| 11 | * HAVE NO RIGHT TO USE THIS SOFTWARE IN ANY WAY, AND SHOULD IMMEDIATELY |
|---|
| 12 | * NOTIFY BROADCOM AND DISCONTINUE ALL USE OF THE SOFTWARE. |
|---|
| 13 | * |
|---|
| 14 | * Except as expressly set forth in the Authorized License, |
|---|
| 15 | * |
|---|
| 16 | * 1. This program, including its structure, sequence and organization, constitutes the valuable trade |
|---|
| 17 | * secrets of Broadcom, and you shall use all reasonable efforts to protect the confidentiality thereof, |
|---|
| 18 | * and to use this information only in connection with your use of Broadcom integrated circuit products. |
|---|
| 19 | * |
|---|
| 20 | * 2. TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" |
|---|
| 21 | * AND WITH ALL FAULTS AND BROADCOM MAKES NO PROMISES, REPRESENTATIONS OR |
|---|
| 22 | * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO |
|---|
| 23 | * THE SOFTWARE. BROADCOM SPECIFICALLY DISCLAIMS ANY AND ALL IMPLIED WARRANTIES |
|---|
| 24 | * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, |
|---|
| 25 | * LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION |
|---|
| 26 | * OR CORRESPONDENCE TO DESCRIPTION. YOU ASSUME THE ENTIRE RISK ARISING OUT OF |
|---|
| 27 | * USE OR PERFORMANCE OF THE SOFTWARE. |
|---|
| 28 | * |
|---|
| 29 | * 3. TO THE MAXIMUM EXTENT PERMITTED BY LAW, IN NO EVENT SHALL BROADCOM OR ITS |
|---|
| 30 | * LICENSORS BE LIABLE FOR (i) CONSEQUENTIAL, INCIDENTAL, SPECIAL, INDIRECT, OR |
|---|
| 31 | * EXEMPLARY DAMAGES WHATSOEVER ARISING OUT OF OR IN ANY WAY RELATING TO YOUR |
|---|
| 32 | * USE OF OR INABILITY TO USE THE SOFTWARE EVEN IF BROADCOM HAS BEEN ADVISED OF |
|---|
| 33 | * THE POSSIBILITY OF SUCH DAMAGES; OR (ii) ANY AMOUNT IN EXCESS OF THE AMOUNT |
|---|
| 34 | * ACTUALLY PAID FOR THE SOFTWARE ITSELF OR U.S. $1, WHICHEVER IS GREATER. THESE |
|---|
| 35 | * LIMITATIONS SHALL APPLY NOTWITHSTANDING ANY FAILURE OF ESSENTIAL PURPOSE OF |
|---|
| 36 | * ANY LIMITED REMEDY. |
|---|
| 37 | * |
|---|
| 38 | * $brcm_Workfile: nexus_led.c $ |
|---|
| 39 | * $brcm_Revision: 10 $ |
|---|
| 40 | * $brcm_Date: 5/11/10 5:04p $ |
|---|
| 41 | * |
|---|
| 42 | * Module Description: |
|---|
| 43 | * |
|---|
| 44 | * Revision History: |
|---|
| 45 | * |
|---|
| 46 | * $brcm_Log: /nexus/modules/led/7405/src/nexus_led.c $ |
|---|
| 47 | * |
|---|
| 48 | * 10 5/11/10 5:04p jtna |
|---|
| 49 | * SW7125-307: Coverity - check for null pointer arguments before |
|---|
| 50 | * dereferencing them |
|---|
| 51 | * |
|---|
| 52 | * 9 11/18/09 2:07p gmohile |
|---|
| 53 | * SW7408-1 : Use NEXUS_HAS_SPI for spi frontend |
|---|
| 54 | * |
|---|
| 55 | * 8 10/14/09 2:32p jtna |
|---|
| 56 | * SW7405-3181: add NEXUS_Led_WriteSegments |
|---|
| 57 | * |
|---|
| 58 | * 7 7/23/09 12:00p erickson |
|---|
| 59 | * PR57047: add NEXUS_Led_SetDot |
|---|
| 60 | * |
|---|
| 61 | * 6 4/9/09 11:40a jrubio |
|---|
| 62 | * PR52188: add support for 2nd Receiver |
|---|
| 63 | * |
|---|
| 64 | * 5 1/26/09 1:54p erickson |
|---|
| 65 | * PR51468: global variable naming convention |
|---|
| 66 | * |
|---|
| 67 | * 4 8/18/08 10:28a katrep |
|---|
| 68 | * PR45674: Compiler warnings in DEBUG=n builds |
|---|
| 69 | * |
|---|
| 70 | * 3 4/11/08 9:53a erickson |
|---|
| 71 | * PR41246: convert BDBG_OBJECT_UNSET to BDBG_OBJECT_DESTROY if freeing |
|---|
| 72 | * memory |
|---|
| 73 | * |
|---|
| 74 | * 2 3/31/08 12:32p erickson |
|---|
| 75 | * PR41073: check result of malloc and fail graciously |
|---|
| 76 | * |
|---|
| 77 | * 1 1/18/08 2:21p jgarrett |
|---|
| 78 | * PR 38808: Merging to main branch |
|---|
| 79 | * |
|---|
| 80 | * Nexus_Devel/2 11/21/07 11:12a erickson |
|---|
| 81 | * PR37423: update |
|---|
| 82 | * |
|---|
| 83 | * Nexus_Devel/1 11/20/07 4:53p erickson |
|---|
| 84 | * PR37423: adding ir_input and led |
|---|
| 85 | * |
|---|
| 86 | **************************************************************************/ |
|---|
| 87 | #include "nexus_led_module.h" |
|---|
| 88 | #include "bled.h" |
|---|
| 89 | #include "priv/nexus_core.h" |
|---|
| 90 | |
|---|
| 91 | BDBG_MODULE(nexus_led); |
|---|
| 92 | |
|---|
| 93 | NEXUS_ModuleHandle g_NEXUS_ledModule; |
|---|
| 94 | struct { |
|---|
| 95 | NEXUS_LedModuleSettings settings; |
|---|
| 96 | } g_NEXUS_led; |
|---|
| 97 | |
|---|
| 98 | /**************************************** |
|---|
| 99 | * Module functions |
|---|
| 100 | ***************/ |
|---|
| 101 | void NEXUS_LedModule_GetDefaultSettings(NEXUS_LedModuleSettings *pSettings) |
|---|
| 102 | { |
|---|
| 103 | BKNI_Memset(pSettings, 0, sizeof(*pSettings)); |
|---|
| 104 | } |
|---|
| 105 | NEXUS_ModuleHandle NEXUS_LedModule_Init(const NEXUS_LedModuleSettings *pSettings) |
|---|
| 106 | { |
|---|
| 107 | BDBG_ASSERT(!g_NEXUS_ledModule); |
|---|
| 108 | g_NEXUS_ledModule = NEXUS_Module_Create("led", NULL); |
|---|
| 109 | if (pSettings) { |
|---|
| 110 | g_NEXUS_led.settings = *pSettings; |
|---|
| 111 | } |
|---|
| 112 | else { |
|---|
| 113 | NEXUS_LedModule_GetDefaultSettings(&g_NEXUS_led.settings); |
|---|
| 114 | } |
|---|
| 115 | return g_NEXUS_ledModule; |
|---|
| 116 | } |
|---|
| 117 | void NEXUS_LedModule_Uninit() |
|---|
| 118 | { |
|---|
| 119 | NEXUS_Module_Destroy(g_NEXUS_ledModule); |
|---|
| 120 | g_NEXUS_ledModule = NULL; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /**************************************** |
|---|
| 124 | * API functions |
|---|
| 125 | ***************/ |
|---|
| 126 | |
|---|
| 127 | BDBG_OBJECT_ID(NEXUS_Led); |
|---|
| 128 | |
|---|
| 129 | #define NEXUS_NUM_LED_DIGITS 4 |
|---|
| 130 | |
|---|
| 131 | struct NEXUS_Led { |
|---|
| 132 | BDBG_OBJECT(NEXUS_Led) |
|---|
| 133 | BLED_Handle led; |
|---|
| 134 | NEXUS_LedSettings settings; |
|---|
| 135 | uint8_t ledState; |
|---|
| 136 | bool dots[NEXUS_NUM_LED_DIGITS]; |
|---|
| 137 | uint8_t chars[NEXUS_NUM_LED_DIGITS]; |
|---|
| 138 | }; |
|---|
| 139 | |
|---|
| 140 | void NEXUS_Led_GetDefaultSettings(NEXUS_LedSettings *pSettings) |
|---|
| 141 | { |
|---|
| 142 | BKNI_Memset(pSettings, 0, sizeof(*pSettings)); |
|---|
| 143 | pSettings->brightness = 100; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | NEXUS_LedHandle NEXUS_Led_Open(unsigned index, const NEXUS_LedSettings *pSettings) |
|---|
| 147 | { |
|---|
| 148 | BERR_Code rc; |
|---|
| 149 | NEXUS_LedHandle led; |
|---|
| 150 | BLED_Settings ledSettings; |
|---|
| 151 | NEXUS_LedSettings defaultSettings; |
|---|
| 152 | |
|---|
| 153 | if (!pSettings) { |
|---|
| 154 | NEXUS_Led_GetDefaultSettings(&defaultSettings); |
|---|
| 155 | pSettings = &defaultSettings; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | /* only one LED controller */ |
|---|
| 159 | if (index > 0) { |
|---|
| 160 | BDBG_ERR(("invalid led[%d]", index)); |
|---|
| 161 | return NULL; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | led = BKNI_Malloc(sizeof(*led)); |
|---|
| 165 | if (!led) { |
|---|
| 166 | rc=BERR_TRACE(NEXUS_OUT_OF_SYSTEM_MEMORY); |
|---|
| 167 | return NULL; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | BKNI_Memset(led, 0, sizeof(*led)); |
|---|
| 171 | BDBG_OBJECT_SET(led, NEXUS_Led); |
|---|
| 172 | |
|---|
| 173 | /* Check for SPI LED, return if this is the case */ |
|---|
| 174 | #if NEXUS_HAS_SPI |
|---|
| 175 | if (g_NEXUS_led.settings.spi != NULL ) |
|---|
| 176 | { |
|---|
| 177 | return led; |
|---|
| 178 | } |
|---|
| 179 | #endif |
|---|
| 180 | |
|---|
| 181 | BLED_GetDefaultSettings(&ledSettings, g_pCoreHandles->chp); |
|---|
| 182 | ledSettings.percentBrightness = pSettings->brightness; |
|---|
| 183 | rc = BLED_Open(&led->led, g_pCoreHandles->chp, g_pCoreHandles->reg, &ledSettings); |
|---|
| 184 | if (rc) {rc=BERR_TRACE(rc); goto error;} |
|---|
| 185 | |
|---|
| 186 | return led; |
|---|
| 187 | error: |
|---|
| 188 | NEXUS_Led_Close(led); |
|---|
| 189 | return NULL; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | void NEXUS_Led_Close(NEXUS_LedHandle led) |
|---|
| 193 | { |
|---|
| 194 | BDBG_OBJECT_ASSERT(led, NEXUS_Led); |
|---|
| 195 | |
|---|
| 196 | if ((led->led) |
|---|
| 197 | #if NEXUS_HAS_SPI |
|---|
| 198 | && (g_NEXUS_led.settings.spi == NULL) |
|---|
| 199 | #endif |
|---|
| 200 | ) { |
|---|
| 201 | BLED_Close(led->led); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | BDBG_OBJECT_DESTROY(led, NEXUS_Led); |
|---|
| 205 | BKNI_Free(led); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | void NEXUS_Led_GetSettings(NEXUS_LedHandle led, NEXUS_LedSettings *pSettings) |
|---|
| 209 | { |
|---|
| 210 | BDBG_OBJECT_ASSERT(led, NEXUS_Led); |
|---|
| 211 | *pSettings = led->settings; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | NEXUS_Error NEXUS_Led_SetSettings(NEXUS_LedHandle led, const NEXUS_LedSettings *pSettings) |
|---|
| 215 | { |
|---|
| 216 | BERR_Code rc; |
|---|
| 217 | |
|---|
| 218 | BDBG_OBJECT_ASSERT(led, NEXUS_Led); |
|---|
| 219 | |
|---|
| 220 | #if NEXUS_HAS_SPI |
|---|
| 221 | if (g_NEXUS_led.settings.spi == NULL) |
|---|
| 222 | #endif |
|---|
| 223 | { |
|---|
| 224 | rc = BLED_AdjustBrightness(led->led, pSettings->brightness); |
|---|
| 225 | if (rc) return BERR_TRACE(rc); |
|---|
| 226 | } |
|---|
| 227 | /* TODO: implement scrolling option with timer */ |
|---|
| 228 | |
|---|
| 229 | led->settings = *pSettings; |
|---|
| 230 | return 0; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | static NEXUS_Error NEXUS_Led_P_WriteString(NEXUS_LedHandle led) |
|---|
| 234 | { |
|---|
| 235 | unsigned i; |
|---|
| 236 | |
|---|
| 237 | for (i=0;i<NEXUS_NUM_LED_DIGITS;i++) { |
|---|
| 238 | uint8_t code; |
|---|
| 239 | BERR_Code rc; |
|---|
| 240 | |
|---|
| 241 | switch(led->chars[i]) { |
|---|
| 242 | case 'A': case 'a': code = LED_A; break; |
|---|
| 243 | case 'B': case 'b': code = LED_B; break; |
|---|
| 244 | case 'C': case 'c': code = LED_C; break; |
|---|
| 245 | case 'D': case 'd': code = LED_D; break; |
|---|
| 246 | case 'E': case 'e': code = LED_E; break; |
|---|
| 247 | case 'F': case 'f': code = LED_F; break; |
|---|
| 248 | case 'G': case 'g': code = LED_G; break; |
|---|
| 249 | case 'H': case 'h': code = LED_H; break; |
|---|
| 250 | case 'I': case 'i': code = LED_I; break; |
|---|
| 251 | case 'J': case 'j': code = LED_J; break; |
|---|
| 252 | case 'K': case 'k': code = LED_K; break; |
|---|
| 253 | case 'L': case 'l': code = LED_L; break; |
|---|
| 254 | case 'M': case 'm': code = LED_M; break; |
|---|
| 255 | case 'N': case 'n': code = LED_N; break; |
|---|
| 256 | case 'O': case 'o': code = LED_O; break; |
|---|
| 257 | case 'P': case 'p': code = LED_P; break; |
|---|
| 258 | case 'Q': case 'q': code = LED_C; break; |
|---|
| 259 | case 'R': case 'r': code = LED_R; break; |
|---|
| 260 | case 'S': case 's': code = LED_S; break; |
|---|
| 261 | case 'T': case 't': code = LED_T; break; |
|---|
| 262 | case 'U': case 'u': code = LED_U; break; |
|---|
| 263 | case 'V': case 'v': code = LED_V; break; |
|---|
| 264 | case 'W': case 'w': code = LED_W; break; |
|---|
| 265 | case 'X': case 'x': code = LED_X; break; |
|---|
| 266 | case 'Y': case 'y': code = LED_Y; break; |
|---|
| 267 | case 'Z': case 'z': code = LED_Z; break; |
|---|
| 268 | case '-': code = LED_DASH; break; |
|---|
| 269 | case '0': code = LED_NUM0; break; |
|---|
| 270 | case '1': code = LED_NUM1; break; |
|---|
| 271 | case '2': code = LED_NUM2; break; |
|---|
| 272 | case '3': code = LED_NUM3; break; |
|---|
| 273 | case '4': code = LED_NUM4; break; |
|---|
| 274 | case '5': code = LED_NUM5; break; |
|---|
| 275 | case '6': code = LED_NUM6; break; |
|---|
| 276 | case '7': code = LED_NUM7; break; |
|---|
| 277 | case '8': code = LED_NUM8; break; |
|---|
| 278 | case '9': code = LED_NUM9; break; |
|---|
| 279 | default: code = 0xFF; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | if (led->dots[i]) { |
|---|
| 283 | code &= ~0x80; /* 0 = on */ |
|---|
| 284 | } |
|---|
| 285 | else { |
|---|
| 286 | code |= 0x80; /* 1 = off */ |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | BDBG_MSG(("write %d: %c%s, code=%x", i+1, led->chars[i], led->dots[i]?" (dot)":"", code)); |
|---|
| 290 | rc = BLED_Write(led->led, i+1, code); |
|---|
| 291 | if (rc) { return BERR_TRACE(rc); } |
|---|
| 292 | } |
|---|
| 293 | return 0; |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | NEXUS_Error NEXUS_Led_WriteString(NEXUS_LedHandle led, const char *pString, unsigned length) |
|---|
| 297 | { |
|---|
| 298 | unsigned i; |
|---|
| 299 | |
|---|
| 300 | BDBG_OBJECT_ASSERT(led, NEXUS_Led); |
|---|
| 301 | |
|---|
| 302 | /* SPI LED does not support this function */ |
|---|
| 303 | #if NEXUS_HAS_SPI |
|---|
| 304 | if (g_NEXUS_led.settings.spi != NULL) { |
|---|
| 305 | return BERR_TRACE(NEXUS_NOT_SUPPORTED); |
|---|
| 306 | } |
|---|
| 307 | #endif |
|---|
| 308 | |
|---|
| 309 | if (!length && pString) { |
|---|
| 310 | while (pString[length]) { |
|---|
| 311 | length++; |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | if (length > 128) { |
|---|
| 315 | BDBG_WRN(("NEXUS_Led_WriteString pString length exceeds kernel mode proxy attr fixed_length comment.")); |
|---|
| 316 | } |
|---|
| 317 | if (length > NEXUS_NUM_LED_DIGITS) { |
|---|
| 318 | length = NEXUS_NUM_LED_DIGITS; |
|---|
| 319 | } |
|---|
| 320 | /* write the whole chars[] array */ |
|---|
| 321 | for (i=0;i<NEXUS_NUM_LED_DIGITS;i++) { |
|---|
| 322 | if (i < length) { |
|---|
| 323 | led->chars[i] = pString[i]; |
|---|
| 324 | } |
|---|
| 325 | else { |
|---|
| 326 | led->chars[i] = 0; |
|---|
| 327 | } |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | return NEXUS_Led_P_WriteString(led); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | NEXUS_Error NEXUS_Led_WriteSegments(NEXUS_LedHandle led, NEXUS_LedDigit digit, uint8_t value) |
|---|
| 334 | { |
|---|
| 335 | BERR_Code rc; |
|---|
| 336 | |
|---|
| 337 | BDBG_OBJECT_ASSERT(led, NEXUS_Led); |
|---|
| 338 | |
|---|
| 339 | /* SPI LED does not support this function */ |
|---|
| 340 | #if NEXUS_HAS_SPI |
|---|
| 341 | if (g_NEXUS_led.settings.spi != NULL) { |
|---|
| 342 | return BERR_TRACE(NEXUS_NOT_SUPPORTED); |
|---|
| 343 | } |
|---|
| 344 | #endif |
|---|
| 345 | |
|---|
| 346 | if (digit >= NEXUS_LedDigit_eMax) { |
|---|
| 347 | return NEXUS_INVALID_PARAMETER; |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | rc = BLED_Write(led->led, digit, ~value); |
|---|
| 351 | if (rc) { return BERR_TRACE(rc); } |
|---|
| 352 | |
|---|
| 353 | return NEXUS_SUCCESS; |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | NEXUS_Error NEXUS_Led_SetDot( NEXUS_LedHandle led, unsigned dotIndex, bool enabled ) |
|---|
| 358 | { |
|---|
| 359 | BDBG_OBJECT_ASSERT(led, NEXUS_Led); |
|---|
| 360 | |
|---|
| 361 | /* SPI LED does not support this function */ |
|---|
| 362 | #if NEXUS_HAS_SPI |
|---|
| 363 | if (g_NEXUS_led.settings.spi != NULL) { |
|---|
| 364 | return BERR_TRACE(NEXUS_NOT_SUPPORTED); |
|---|
| 365 | } |
|---|
| 366 | #endif |
|---|
| 367 | |
|---|
| 368 | if (dotIndex >= NEXUS_NUM_LED_DIGITS) { |
|---|
| 369 | return BERR_TRACE(NEXUS_NOT_SUPPORTED); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | led->dots[dotIndex] = enabled; |
|---|
| 373 | return NEXUS_Led_P_WriteString(led); |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | NEXUS_Error NEXUS_Led_SetLedState(NEXUS_LedHandle led, unsigned ledNumber, bool active) |
|---|
| 377 | { |
|---|
| 378 | BDBG_OBJECT_ASSERT(led, NEXUS_Led); |
|---|
| 379 | |
|---|
| 380 | #if NEXUS_HAS_SPI |
|---|
| 381 | if (g_NEXUS_led.settings.spi != NULL) |
|---|
| 382 | { /* Spi Code Base */ |
|---|
| 383 | { |
|---|
| 384 | NEXUS_Error rc; |
|---|
| 385 | uint8_t chipAddr; |
|---|
| 386 | uint8_t data[2]; |
|---|
| 387 | |
|---|
| 388 | if (active) |
|---|
| 389 | led->ledState |= (1 << ledNumber); |
|---|
| 390 | else |
|---|
| 391 | led->ledState &= ~(1 << ledNumber); |
|---|
| 392 | |
|---|
| 393 | /* use chip address 0 */ |
|---|
| 394 | chipAddr = 0; |
|---|
| 395 | data[0] = (chipAddr << 1) | 0x01; |
|---|
| 396 | data[1] = led->ledState; |
|---|
| 397 | |
|---|
| 398 | rc = NEXUS_Spi_Write(g_NEXUS_led.settings.spi, data, 2); |
|---|
| 399 | if (rc) return BERR_TRACE(rc); |
|---|
| 400 | |
|---|
| 401 | return 0; |
|---|
| 402 | } |
|---|
| 403 | } |
|---|
| 404 | #endif |
|---|
| 405 | |
|---|
| 406 | return BLED_SetDiscreteLED(led->led, active, ledNumber); |
|---|
| 407 | } |
|---|