diff options
author | Randall Spangler <rspangler@chromium.org> | 2013-10-16 13:23:10 -0700 |
---|---|---|
committer | chrome-internal-fetch <chrome-internal-fetch@google.com> | 2013-10-23 20:07:25 +0000 |
commit | 8cf03ac0563294fbdeca2dc133d06f0b51c9a546 (patch) | |
tree | 6b07c493e7567a3221d8592b4337d2787d6bc531 /driver/led | |
parent | 2464d08e4d310a3f63208f22df4502c5250c4b58 (diff) | |
download | chrome-ec-8cf03ac0563294fbdeca2dc133d06f0b51c9a546.tar.gz |
Move source files to driver/ and power/ subdirs
The common/ subdir was getting cluttered. Move drivers for external
components to a new driver/ tree, and move what used to be called
chipset_*.c to a new power/ directory.
This does not move/rename header files or CONFIG options. That will
be done in subsequent steps, since moving and modifying .c files in
the same CL is harder to review.
BUG=chrome-os-partner:18343
BRANCH=none
TEST=build all boards; pass unit tests
Change-Id: I67a3003dc8564783a320335cf0e9620a21982d5e
Signed-off-by: Randall Spangler <rspangler@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/173601
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
Tested-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-by: Vic Yang <victoryang@chromium.org>
Diffstat (limited to 'driver/led')
-rw-r--r-- | driver/led/ds2413.c | 166 | ||||
-rw-r--r-- | driver/led/lp5562.c | 161 |
2 files changed, 327 insertions, 0 deletions
diff --git a/driver/led/ds2413.c b/driver/led/ds2413.c new file mode 100644 index 0000000000..9babd75992 --- /dev/null +++ b/driver/led/ds2413.c @@ -0,0 +1,166 @@ +/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* Power LED control for Chrome EC */ + +#include "charge_state.h" +#include "console.h" +#include "hooks.h" +#include "onewire.h" +#include "timer.h" +#include "util.h" + +#define ONEWIRE_RETRIES 10 + +enum led_color { + LED_OFF = 0, + LED_RED, + LED_YELLOW, + LED_GREEN, + LED_COLOR_COUNT /* Number of colors, not a color itself */ +}; + +static const uint8_t led_masks[LED_COLOR_COUNT] = {0xff, 0xfe, 0xfc, 0xfd}; +static const char * const color_names[LED_COLOR_COUNT] = { + "off", "red", "yellow", "green"}; + +/** + * Set the onewire LED GPIO controller outputs + * + * @param mask Mask of outputs to enable + * + * @return EC_SUCCESS, or non-zero if error. + */ +static int led_set_mask(int mask) +{ + int rv; + + /* Reset the 1-wire bus */ + rv = onewire_reset(); + if (rv) + return rv; + + /* Skip ROM, since only one device */ + onewire_write(0xcc); + + /* Write and turn on the LEDs */ + onewire_write(0x5a); + onewire_write(mask); + onewire_write(~mask); /* Repeat inverted */ + + rv = onewire_read(); /* Confirmation byte */ + if (rv != 0xaa) + return EC_ERROR_UNKNOWN; + + /* The next byte is a read-back of the chip status. Since we're only + * using lines as outputs, we can ignore it. */ + return EC_SUCCESS; +} + +static int led_set(enum led_color color) +{ + int rv = EC_SUCCESS; + int i; + + /* + * 1-wire communication can fail for timing reasons in the current + * system. We have a limited timing window to send/receive bits, but + * we can't disable interrupts for the rest of the system to guarantee + * we hit that window. Instead, simply retry the low-level command a + * few times. + */ + for (i = 0; i < ONEWIRE_RETRIES; i++) { + rv = led_set_mask(led_masks[color]); + if (rv == EC_SUCCESS) + break; + + /* + * Sleep for a bit between tries. This gives the 1-wire GPIO + * chip time to recover from the failed attempt, and allows + * lower-priority tasks a chance to run. + */ + usleep(100); + } + + return rv; +} + +/*****************************************************************************/ +/* Hooks */ + +static void onewire_led_tick(void) +{ + static enum led_color current_color = LED_COLOR_COUNT; + static int tick_count; + + enum led_color new_color = LED_OFF; + uint32_t chflags = charge_get_flags(); + + tick_count++; + + if (!(chflags & CHARGE_FLAG_EXTERNAL_POWER)) { + /* AC isn't present, so the power LED on the AC plug is off */ + current_color = LED_OFF; + return; + } + + /* Translate charge state to LED color */ + switch (charge_get_state()) { + case PWR_STATE_IDLE: + if (chflags & CHARGE_FLAG_FORCE_IDLE) + new_color = (tick_count & 1) ? LED_GREEN : LED_OFF; + else + new_color = LED_GREEN; + break; + case PWR_STATE_CHARGE: + new_color = LED_YELLOW; + break; + case PWR_STATE_CHARGE_NEAR_FULL: + new_color = LED_GREEN; + break; + case PWR_STATE_ERROR: + new_color = LED_RED; + break; + default: + /* Other states don't change LED color */ + break; + } + + /* + * The power adapter on link can partially unplug and lose its LED + * state. There's no way to detect this, so just assume it forgets its + * state every 10 seconds. + */ + if (!(tick_count % 10)) + current_color = LED_COLOR_COUNT; + + /* If current color is still correct, leave now */ + if (new_color == current_color) + return; + + /* Update LED */ + if (!led_set(new_color)) + current_color = new_color; +} +DECLARE_HOOK(HOOK_SECOND, onewire_led_tick, HOOK_PRIO_DEFAULT); + +/*****************************************************************************/ +/* Console commands */ + +static int command_powerled(int argc, char **argv) +{ + int i; + + /* Pick a color, any color... */ + for (i = 0; i < LED_COLOR_COUNT; i++) { + if (!strcasecmp(argv[1], color_names[i])) + return led_set(i); + } + return EC_ERROR_PARAM1; +} +DECLARE_CONSOLE_COMMAND(powerled, command_powerled, + "<off | red | yellow | green>", + "Set power LED color", + NULL); diff --git a/driver/led/lp5562.c b/driver/led/lp5562.c new file mode 100644 index 0000000000..fd92fae26f --- /dev/null +++ b/driver/led/lp5562.c @@ -0,0 +1,161 @@ +/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * TI LP5562 driver. + */ + +#include "console.h" +#include "i2c.h" +#include "lp5562.h" +#include "timer.h" +#include "uart.h" +#include "util.h" + +/* 8-bit I2C address */ +#define LP5562_I2C_ADDR (0x30 << 1) + +inline int lp5562_write(uint8_t reg, uint8_t val) +{ + return i2c_write8(I2C_PORT_HOST, LP5562_I2C_ADDR, reg, val); +} + +inline int lp5562_read(uint8_t reg, int *val) +{ + return i2c_read8(I2C_PORT_HOST, LP5562_I2C_ADDR, reg, val); +} + +int lp5562_set_color(uint32_t rgb) +{ + int ret = 0; + + ret |= lp5562_write(LP5562_REG_B_PWM, rgb & 0xff); + ret |= lp5562_write(LP5562_REG_G_PWM, (rgb >> 8) & 0xff); + ret |= lp5562_write(LP5562_REG_R_PWM, (rgb >> 16) & 0xff); + + return ret; +} + +int lp5562_set_engine(uint8_t r, uint8_t g, uint8_t b) +{ + return lp5562_write(LP5562_REG_LED_MAP, (r << 4) | (g << 2) | b); +} + +int lp5562_engine_load(int engine, const uint8_t *program, int size) +{ + int prog_addr = LP5562_REG_ENG_PROG(engine); + int i, ret, val; + int shift = 6 - engine * 2; + + ret = lp5562_read(LP5562_REG_OP_MODE, &val); + if (ret) + return ret; + val &= ~(0x3 << shift); + val |= 0x1 << shift; + ret = lp5562_write(LP5562_REG_OP_MODE, val); + if (ret) + return ret; + + for (i = 0; i < size; ++i) { + ret = lp5562_write(prog_addr + i, program[i]); + if (ret) + return ret; + } + + val &= ~(0x3 << shift); + val |= 0x2 << shift; + ret = lp5562_write(LP5562_REG_OP_MODE, val); + + return ret; +} + +int lp5562_engine_control(int eng1, int eng2, int eng3) +{ + int ret, val; + + ret = lp5562_read(LP5562_REG_ENABLE, &val); + if (ret) + return ret; + val &= 0xc0; + val |= (eng1 << 4) | (eng2 << 2) | eng3; + return lp5562_write(LP5562_REG_ENABLE, val); +} + +int lp5562_get_engine_state(int engine) +{ + int val; + + if (lp5562_read(LP5562_REG_ENABLE, &val)) + return 0xee; + return (val >> (6 - engine * 2)) & 0x3; +} + +int lp5562_poweron(void) +{ + int ret = 0; + + ret |= lp5562_write(LP5562_REG_ENABLE, 0x40); + udelay(500); /* start-up delay */ + + ret |= lp5562_write(LP5562_REG_CONFIG, 0x1); + ret |= lp5562_write(LP5562_REG_LED_MAP, 0x0); + + return ret; +} + +int lp5562_poweroff(void) +{ + return lp5562_write(LP5562_REG_ENABLE, 0x0); +} + +int lp5562_get_pc(int engine) +{ + int ret; + if (lp5562_read(LP5562_REG_ENG1_PC + engine - 1, &ret)) + return 0xee; + return ret; +} + +int lp5562_set_pc(int engine, int val) +{ + return lp5562_write(LP5562_REG_ENG1_PC + engine - 1, val); +} + +/*****************************************************************************/ +/* Console commands */ + +static int command_lp5562(int argc, char **argv) +{ + if (argc == 4) { + char *e; + uint8_t red, green, blue; + + red = strtoi(argv[1], &e, 0); + if (e && *e) + return EC_ERROR_PARAM1; + green = strtoi(argv[2], &e, 0); + if (e && *e) + return EC_ERROR_PARAM2; + blue = strtoi(argv[3], &e, 0); + if (e && *e) + return EC_ERROR_PARAM3; + + return lp5562_set_color((red << 16) | (green << 8) | blue); + } else if (argc == 2) { + int v; + + if (!parse_bool(argv[1], &v)) + return EC_ERROR_PARAM1; + + if (v) + return lp5562_poweron(); + else + return lp5562_poweroff(); + } + + return EC_ERROR_INVAL; +} +DECLARE_CONSOLE_COMMAND(lp5562, command_lp5562, + "on | off | <red> <green> <blue>", + "Set the color of the LED", + NULL); |