[U-Boot] [PATCH 00/16] pmic: Redesign PMIC framework to support multiple instances of devices

PMIC framework has been redesigned to support multiple instances of power related devices (e.g. fuel gauge, PMICs, chargers, micro USB IC).
Due to that, code at other architectures and boards have been adjusted properly.
New power_board_init() method at ./lib/board.c file has been introduced. It is meant to be an architecture dependent function to support advanced power management. Since PMIC framework uses lists internally to link different devices, its initialization must be done just after malloc initialization.
Please consider commits from this patch set as the example of advanced power management for a particular HW (Trats board in this case).
In the new approach PMICs are selected with their names (e.g. 'pmic dump MAX8997_PMIC') Presented patch set is a first step to change 'pmic' command to more generic (i.e. power) to provide control for multiple devices.
Dependencies: 1. gpio:fix: Proper handling of GPIO subsystem parts at Samsung devices http://patchwork.ozlabs.org/patch/181768/ 2. i2c:soft:multi: Support for multiple soft I2C buses at Samsung boards http://patchwork.ozlabs.org/patch/181789/ 3. i2c:soft:multi: Enable soft I2C multibus at Trats development board http://patchwork.ozlabs.org/patch/181788/
Test HW: - Exynos4210 Trats development board
Lukasz Majewski (16): pmic:i2c: Handle PMIC I2C transmission comprising of two bytes pmic:i2c: Add I2C byte order to PMIC framework pmic:max8997: Switch the MAX8997 PMIC to be used with multibus I2C pmic: Extend PMIC framework to support multiple instances of PMIC devices pmic: Introduce power_board_init() method at ./lib/board.c file pmic: Enable power_board_init() support at TRATS pmic:chrg: Common information about charger and battery (power_chrg.h) pmic:muic: Support for MUIC built into MAX8997 device pmic:fuel-gauge: Support for MAX17042 fuel-gauge pmic:max8997: Function for calculating LDO internal register value arm:trats:pmic: Default PMIC(MAX8997) initialization for Samsung's TRATS board arm:trats:pmic: Enable MUIC (MAX8997) at Samsung's TRATS board arm:trats:pmic: Enable fuel-gauge (MAX17042) at Samsung's TRATS board pmic:max8997: Support for MAX8997 internal charger control arm:trats:pmic: Support for charging battery at Samsung's TRATS board arm:trats:pmic: Power consumption reduction state for Samsung's TRATS board
arch/arm/lib/board.c | 4 + board/davedenx/qong/qong.c | 6 +- board/freescale/mx31pdk/mx31pdk.c | 6 +- board/freescale/mx35pdk/mx35pdk.c | 8 +- board/freescale/mx51evk/mx51evk.c | 6 +- board/freescale/mx53evk/mx53evk.c | 6 +- board/freescale/mx53loco/mx53loco.c | 10 +- board/hale/tt01/tt01.c | 6 +- board/samsung/goni/goni.c | 9 +- board/samsung/trats/trats.c | 259 ++++++++++++++++++++++++++++- board/samsung/universal_c210/universal.c | 10 +- board/ttcontrol/vision2/vision2.c | 6 +- drivers/misc/Makefile | 2 + drivers/misc/fg_max17042.c | 235 +++++++++++++++++++++++++++ drivers/misc/muic_max8997.c | 80 +++++++++ drivers/misc/pmic_core.c | 108 +++++++++---- drivers/misc/pmic_dialog.c | 8 +- drivers/misc/pmic_fsl.c | 8 +- drivers/misc/pmic_i2c.c | 40 ++++- drivers/misc/pmic_max8997.c | 80 +++++++++- drivers/misc/pmic_max8998.c | 10 +- drivers/misc/pmic_spi.c | 4 +- drivers/rtc/mc13xxx-rtc.c | 6 +- include/common.h | 3 + include/configs/trats.h | 7 + include/power/fg_battery_cell_params.h | 88 ++++++++++ include/power/max17042_fg.h | 74 +++++++++ include/power/max8997_muic.h | 61 +++++++ include/{ => power}/max8997_pmic.h | 27 +++- include/{ => power}/max8998_pmic.h | 0 include/{ => power}/pmic.h | 20 ++- include/power/power_chrg.h | 56 +++++++ 32 files changed, 1143 insertions(+), 110 deletions(-) create mode 100644 drivers/misc/fg_max17042.c create mode 100644 drivers/misc/muic_max8997.c create mode 100644 include/power/fg_battery_cell_params.h create mode 100644 include/power/max17042_fg.h create mode 100644 include/power/max8997_muic.h rename include/{ => power}/max8997_pmic.h (87%) rename include/{ => power}/max8998_pmic.h (100%) rename include/{ => power}/pmic.h (80%) create mode 100644 include/power/power_chrg.h

This patch adds support for proper handling of a PMIC I2C transmission comprising of two bytes.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com --- drivers/misc/pmic_i2c.c | 9 ++++++++- 1 files changed, 8 insertions(+), 1 deletions(-)
diff --git a/drivers/misc/pmic_i2c.c b/drivers/misc/pmic_i2c.c index 95a3365..e74c372 100644 --- a/drivers/misc/pmic_i2c.c +++ b/drivers/misc/pmic_i2c.c @@ -44,6 +44,10 @@ int pmic_reg_write(struct pmic *p, u32 reg, u32 val) buf[1] = (val >> 8) & 0xff; buf[2] = val & 0xff; break; + case 2: + buf[0] = (val >> 8) & 0xff; + buf[1] = val & 0xff; + break; case 1: buf[0] = val & 0xff; break; @@ -73,6 +77,9 @@ int pmic_reg_read(struct pmic *p, u32 reg, u32 *val) case 3: ret_val = buf[0] << 16 | buf[1] << 8 | buf[2]; break; + case 2: + ret_val = buf[0] << 8 | buf[1]; + break; case 1: ret_val = buf[0]; break; @@ -88,7 +95,7 @@ int pmic_reg_read(struct pmic *p, u32 reg, u32 *val) int pmic_probe(struct pmic *p) { I2C_SET_BUS(p->bus); - debug("PMIC:%s probed!\n", p->name); + debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name); if (i2c_probe(pmic_i2c_addr)) { printf("Can't find PMIC:%s\n", p->name); return -1;

Since the pmic_reg_read is the u32 value, the order in which bytes are placed to form u32 value is important.
This commit adds the reverse (which is default) and normal byte order.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com --- drivers/misc/pmic_i2c.c | 31 ++++++++++++++++++++++++------- include/pmic.h | 3 +++ 2 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/drivers/misc/pmic_i2c.c b/drivers/misc/pmic_i2c.c index e74c372..1aa5b7b 100644 --- a/drivers/misc/pmic_i2c.c +++ b/drivers/misc/pmic_i2c.c @@ -40,13 +40,24 @@ int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
switch (pmic_i2c_tx_num) { case 3: - buf[0] = (val >> 16) & 0xff; - buf[1] = (val >> 8) & 0xff; - buf[2] = val & 0xff; + if (pmic_i2c_byte_order == PMIC_BYTE_ORDER_NORMAL) { + buf[2] = (val >> 16) & 0xff; + buf[1] = (val >> 8) & 0xff; + buf[0] = val & 0xff; + } else { + buf[0] = (val >> 16) & 0xff; + buf[1] = (val >> 8) & 0xff; + buf[2] = val & 0xff; + } break; case 2: - buf[0] = (val >> 8) & 0xff; - buf[1] = val & 0xff; + if (pmic_i2c_byte_order == PMIC_BYTE_ORDER_NORMAL) { + buf[1] = (val >> 8) & 0xff; + buf[0] = val & 0xff; + } else { + buf[0] = (val >> 8) & 0xff; + buf[1] = val & 0xff; + } break; case 1: buf[0] = val & 0xff; @@ -75,10 +86,16 @@ int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
switch (pmic_i2c_tx_num) { case 3: - ret_val = buf[0] << 16 | buf[1] << 8 | buf[2]; + if (pmic_i2c_byte_order == PMIC_BYTE_ORDER_NORMAL) + ret_val = buf[2] << 16 | buf[1] << 8 | buf[0]; + else + ret_val = buf[0] << 16 | buf[1] << 8 | buf[2]; break; case 2: - ret_val = buf[0] << 8 | buf[1]; + if (pmic_i2c_byte_order == PMIC_BYTE_ORDER_NORMAL) + ret_val = buf[1] << 8 | buf[0]; + else + ret_val = buf[0] << 8 | buf[1]; break; case 1: ret_val = buf[0]; diff --git a/include/pmic.h b/include/pmic.h index 6a05b40..2166f73 100644 --- a/include/pmic.h +++ b/include/pmic.h @@ -27,11 +27,13 @@ enum { PMIC_I2C, PMIC_SPI, }; enum { I2C_PMIC, I2C_NUM, }; enum { PMIC_READ, PMIC_WRITE, }; +enum { PMIC_BYTE_ORDER_REVERSED, PMIC_BYTE_ORDER_NORMAL, };
struct p_i2c { unsigned char addr; unsigned char *buf; unsigned char tx_num; + unsigned char byte_order; };
struct p_spi { @@ -65,6 +67,7 @@ int pmic_set_output(struct pmic *p, u32 reg, int ldo, int on);
#define pmic_i2c_addr (p->hw.i2c.addr) #define pmic_i2c_tx_num (p->hw.i2c.tx_num) +#define pmic_i2c_byte_order (p->hw.i2c.byte_order)
#define pmic_spi_bitlen (p->hw.spi.bitlen) #define pmic_spi_flags (p->hw.spi.flags)

On 14/09/2012 17:40, Lukasz Majewski wrote:
Since the pmic_reg_read is the u32 value, the order in which bytes are placed to form u32 value is important.
This commit adds the reverse (which is default) and normal byte order.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com
Hi,
drivers/misc/pmic_i2c.c | 31 ++++++++++++++++++++++++------- include/pmic.h | 3 +++ 2 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/drivers/misc/pmic_i2c.c b/drivers/misc/pmic_i2c.c index e74c372..1aa5b7b 100644 --- a/drivers/misc/pmic_i2c.c +++ b/drivers/misc/pmic_i2c.c @@ -40,13 +40,24 @@ int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
switch (pmic_i2c_tx_num) { case 3:
buf[0] = (val >> 16) & 0xff;
buf[1] = (val >> 8) & 0xff;
buf[2] = val & 0xff;
if (pmic_i2c_byte_order == PMIC_BYTE_ORDER_NORMAL) {
buf[2] = (val >> 16) & 0xff;
buf[1] = (val >> 8) & 0xff;
buf[0] = val & 0xff;
} else {
buf[0] = (val >> 16) & 0xff;
buf[1] = (val >> 8) & 0xff;
buf[2] = val & 0xff;
break; case 2:}
buf[0] = (val >> 8) & 0xff;
buf[1] = val & 0xff;
if (pmic_i2c_byte_order == PMIC_BYTE_ORDER_NORMAL) {
buf[1] = (val >> 8) & 0xff;
buf[0] = val & 0xff;
} else {
buf[0] = (val >> 8) & 0xff;
buf[1] = val & 0xff;
break; case 1: buf[0] = val & 0xff;}
@@ -75,10 +86,16 @@ int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
switch (pmic_i2c_tx_num) { case 3:
ret_val = buf[0] << 16 | buf[1] << 8 | buf[2];
if (pmic_i2c_byte_order == PMIC_BYTE_ORDER_NORMAL)
ret_val = buf[2] << 16 | buf[1] << 8 | buf[0];
else
break; case 2:ret_val = buf[0] << 16 | buf[1] << 8 | buf[2];
ret_val = buf[0] << 8 | buf[1];
if (pmic_i2c_byte_order == PMIC_BYTE_ORDER_NORMAL)
ret_val = buf[1] << 8 | buf[0];
else
break; case 1: ret_val = buf[0];ret_val = buf[0] << 8 | buf[1];
diff --git a/include/pmic.h b/include/pmic.h index 6a05b40..2166f73 100644 --- a/include/pmic.h +++ b/include/pmic.h @@ -27,11 +27,13 @@ enum { PMIC_I2C, PMIC_SPI, }; enum { I2C_PMIC, I2C_NUM, }; enum { PMIC_READ, PMIC_WRITE, }; +enum { PMIC_BYTE_ORDER_REVERSED, PMIC_BYTE_ORDER_NORMAL, };
struct p_i2c { unsigned char addr; unsigned char *buf; unsigned char tx_num;
- unsigned char byte_order;
I can imagine we could have the same issue with SPI and not only with I2C. The byte order is not strictly related to the interface type, and I think this should add to the "struct pmic" instead of the "struct p_i2c".
Regards, Stefano Babic

Hi Stefano,
On 14/09/2012 17:40, Lukasz Majewski wrote:
Since the pmic_reg_read is the u32 value, the order in which bytes are placed to form u32 value is important.
This commit adds the reverse (which is default) and normal byte order.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com
Hi,
drivers/misc/pmic_i2c.c | 31 ++++++++++++++++++++++++------- include/pmic.h | 3 +++ 2 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/drivers/misc/pmic_i2c.c b/drivers/misc/pmic_i2c.c index e74c372..1aa5b7b 100644 --- a/drivers/misc/pmic_i2c.c +++ b/drivers/misc/pmic_i2c.c @@ -40,13 +40,24 @@ int pmic_reg_write(struct pmic *p, u32 reg, u32 val) switch (pmic_i2c_tx_num) { case 3:
buf[0] = (val >> 16) & 0xff;
buf[1] = (val >> 8) & 0xff;
buf[2] = val & 0xff;
if (pmic_i2c_byte_order == PMIC_BYTE_ORDER_NORMAL)
{
buf[2] = (val >> 16) & 0xff;
buf[1] = (val >> 8) & 0xff;
buf[0] = val & 0xff;
} else {
buf[0] = (val >> 16) & 0xff;
buf[1] = (val >> 8) & 0xff;
buf[2] = val & 0xff;
break; case 2:}
buf[0] = (val >> 8) & 0xff;
buf[1] = val & 0xff;
if (pmic_i2c_byte_order == PMIC_BYTE_ORDER_NORMAL)
{
buf[1] = (val >> 8) & 0xff;
buf[0] = val & 0xff;
} else {
buf[0] = (val >> 8) & 0xff;
buf[1] = val & 0xff;
break; case 1: buf[0] = val & 0xff;}
@@ -75,10 +86,16 @@ int pmic_reg_read(struct pmic *p, u32 reg, u32 *val) switch (pmic_i2c_tx_num) { case 3:
ret_val = buf[0] << 16 | buf[1] << 8 | buf[2];
if (pmic_i2c_byte_order == PMIC_BYTE_ORDER_NORMAL)
ret_val = buf[2] << 16 | buf[1] << 8 |
buf[0];
else
ret_val = buf[0] << 16 | buf[1] << 8 |
buf[2]; break; case 2:
ret_val = buf[0] << 8 | buf[1];
if (pmic_i2c_byte_order == PMIC_BYTE_ORDER_NORMAL)
ret_val = buf[1] << 8 | buf[0];
else
break; case 1: ret_val = buf[0];ret_val = buf[0] << 8 | buf[1];
diff --git a/include/pmic.h b/include/pmic.h index 6a05b40..2166f73 100644 --- a/include/pmic.h +++ b/include/pmic.h @@ -27,11 +27,13 @@ enum { PMIC_I2C, PMIC_SPI, }; enum { I2C_PMIC, I2C_NUM, }; enum { PMIC_READ, PMIC_WRITE, }; +enum { PMIC_BYTE_ORDER_REVERSED, PMIC_BYTE_ORDER_NORMAL, };
struct p_i2c { unsigned char addr; unsigned char *buf; unsigned char tx_num;
- unsigned char byte_order;
I can imagine we could have the same issue with SPI and not only with I2C. The byte order is not strictly related to the interface type, and I think this should add to the "struct pmic" instead of the "struct p_i2c".
Thanks for reply.
Good point. I will move this to struct pmic.
Best regards, Lukasz Majewski

PMIC MAX8997 is now ready to work with single and multibus soft I2C implementation.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com --- drivers/misc/pmic_max8997.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
diff --git a/drivers/misc/pmic_max8997.c b/drivers/misc/pmic_max8997.c index 62dbc05..4943f66 100644 --- a/drivers/misc/pmic_max8997.c +++ b/drivers/misc/pmic_max8997.c @@ -24,6 +24,7 @@ #include <common.h> #include <pmic.h> #include <max8997_pmic.h> +#include <i2c.h>
int pmic_init(void) { @@ -37,7 +38,7 @@ int pmic_init(void) p->number_of_regs = PMIC_NUM_OF_REGS; p->hw.i2c.addr = MAX8997_I2C_ADDR; p->hw.i2c.tx_num = 1; - p->bus = I2C_PMIC; + p->bus = I2C_0;
return 0; }

On 14/09/2012 17:40, Lukasz Majewski wrote:
PMIC MAX8997 is now ready to work with single and multibus soft I2C implementation.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com
drivers/misc/pmic_max8997.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
diff --git a/drivers/misc/pmic_max8997.c b/drivers/misc/pmic_max8997.c index 62dbc05..4943f66 100644 --- a/drivers/misc/pmic_max8997.c +++ b/drivers/misc/pmic_max8997.c @@ -24,6 +24,7 @@ #include <common.h> #include <pmic.h> #include <max8997_pmic.h> +#include <i2c.h>
int pmic_init(void) { @@ -37,7 +38,7 @@ int pmic_init(void) p->number_of_regs = PMIC_NUM_OF_REGS; p->hw.i2c.addr = MAX8997_I2C_ADDR; p->hw.i2c.tx_num = 1;
- p->bus = I2C_PMIC;
- p->bus = I2C_0;
I do not see so useful to add an enum for each instance of the I2C bus. And we have to add it if the number of i2c busses grows. IMHO it is better to use directly the constant, so later in another patch pmic_init(5) instead of pmic(I2C_5).
Regards, Stefano Babic

Hi Stefano,
On 14/09/2012 17:40, Lukasz Majewski wrote:
PMIC MAX8997 is now ready to work with single and multibus soft I2C implementation.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com
drivers/misc/pmic_max8997.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
diff --git a/drivers/misc/pmic_max8997.c b/drivers/misc/pmic_max8997.c index 62dbc05..4943f66 100644 --- a/drivers/misc/pmic_max8997.c +++ b/drivers/misc/pmic_max8997.c @@ -24,6 +24,7 @@ #include <common.h> #include <pmic.h> #include <max8997_pmic.h> +#include <i2c.h>
int pmic_init(void) { @@ -37,7 +38,7 @@ int pmic_init(void) p->number_of_regs = PMIC_NUM_OF_REGS; p->hw.i2c.addr = MAX8997_I2C_ADDR; p->hw.i2c.tx_num = 1;
- p->bus = I2C_PMIC;
- p->bus = I2C_0;
I do not see so useful to add an enum for each instance of the I2C bus. And we have to add it if the number of i2c busses grows. IMHO it is better to use directly the constant, so later in another patch pmic_init(5) instead of pmic(I2C_5).
This problem has been already discussed with Heiko:
http://patchwork.ozlabs.org/patch/181789/
I think, that we will have to have an enum of available I2C_x busses at <i2c.h> header file.
In the case of Trats and PMIC framework, the I2C_0 is done on purpose to keep the trats board working (since the PMIC is in reality connected to I2C_5). This numbering (I2C_0) needs to be there until prerequisite patches aren't accepted (the multibus I2C support on trats board)
Regards, Lukasz Majewski

The PMIC framework has been extended to support multiple instances of the variety of devices responsible for power management. This change allows supporting of e.g. fuel gauge, charger, MUIC (Micro USB Interface Circuit). Power related includes have been moved to ./include/power directory. This is a first of a series of patches - in the future "pmic" will be replaced with "power".
Two important issues: 1. The PMIC needs to be initialized just after malloc is configured 2. It uses list to hold information about available PMIC devices
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Stefano Babic sbabic@denx.de --- board/davedenx/qong/qong.c | 6 +- board/freescale/mx31pdk/mx31pdk.c | 6 +- board/freescale/mx35pdk/mx35pdk.c | 8 +- board/freescale/mx51evk/mx51evk.c | 6 +- board/freescale/mx53evk/mx53evk.c | 6 +- board/freescale/mx53loco/mx53loco.c | 10 ++-- board/hale/tt01/tt01.c | 6 +- board/samsung/goni/goni.c | 9 ++- board/samsung/trats/trats.c | 12 ++-- board/samsung/universal_c210/universal.c | 10 ++-- board/ttcontrol/vision2/vision2.c | 6 +- drivers/misc/pmic_core.c | 108 ++++++++++++++++++++++-------- drivers/misc/pmic_dialog.c | 8 +- drivers/misc/pmic_fsl.c | 8 +- drivers/misc/pmic_i2c.c | 6 +- drivers/misc/pmic_max8997.c | 10 ++-- drivers/misc/pmic_max8998.c | 10 ++-- drivers/misc/pmic_spi.c | 4 +- drivers/rtc/mc13xxx-rtc.c | 6 +- include/{ => power}/max8997_pmic.h | 17 +++++- include/{ => power}/max8998_pmic.h | 0 include/{ => power}/pmic.h | 17 +++-- 22 files changed, 177 insertions(+), 102 deletions(-) rename include/{ => power}/max8997_pmic.h (92%) rename include/{ => power}/max8998_pmic.h (100%) rename include/{ => power}/pmic.h (86%)
diff --git a/board/davedenx/qong/qong.c b/board/davedenx/qong/qong.c index c41f11d..e8c23f8 100644 --- a/board/davedenx/qong/qong.c +++ b/board/davedenx/qong/qong.c @@ -28,7 +28,7 @@ #include <asm/arch/sys_proto.h> #include <asm/io.h> #include <nand.h> -#include <pmic.h> +#include <power/pmic.h> #include <fsl_pmic.h> #include <asm/gpio.h> #include "qong_fpga.h" @@ -173,8 +173,8 @@ int board_late_init(void) u32 val; struct pmic *p;
- pmic_init(); - p = get_pmic(); + pmic_init(I2C_PMIC); + p = pmic_get("FSL_PMIC");
/* Enable RTC battery */ pmic_reg_read(p, REG_POWER_CTL0, &val); diff --git a/board/freescale/mx31pdk/mx31pdk.c b/board/freescale/mx31pdk/mx31pdk.c index 9f8bc53..24c0a1e 100644 --- a/board/freescale/mx31pdk/mx31pdk.c +++ b/board/freescale/mx31pdk/mx31pdk.c @@ -30,7 +30,7 @@ #include <asm/arch/imx-regs.h> #include <asm/arch/sys_proto.h> #include <watchdog.h> -#include <pmic.h> +#include <power/pmic.h> #include <fsl_pmic.h>
DECLARE_GLOBAL_DATA_PTR; @@ -84,8 +84,8 @@ int board_late_init(void) u32 val; struct pmic *p;
- pmic_init(); - p = get_pmic(); + pmic_init(I2C_PMIC); + p = pmic_get("FSL_PMIC");
/* Enable RTC battery */ pmic_reg_read(p, REG_POWER_CTL0, &val); diff --git a/board/freescale/mx35pdk/mx35pdk.c b/board/freescale/mx35pdk/mx35pdk.c index 787c923..46dd315 100644 --- a/board/freescale/mx35pdk/mx35pdk.c +++ b/board/freescale/mx35pdk/mx35pdk.c @@ -30,7 +30,7 @@ #include <asm/arch/mx35_pins.h> #include <asm/arch/iomux.h> #include <i2c.h> -#include <pmic.h> +#include <power/pmic.h> #include <fsl_pmic.h> #include <mc9sdz60.h> #include <mc13892.h> @@ -204,7 +204,7 @@ int board_init(void) static inline int pmic_detect(void) { unsigned int id; - struct pmic *p = get_pmic(); + struct pmic *p = pmic_get("FSL_PMIC");
pmic_reg_read(p, REG_IDENTIFICATION, &id);
@@ -229,9 +229,9 @@ int board_late_init(void) u32 pmic_val; struct pmic *p;
- pmic_init(); + pmic_init(I2C_PMIC); if (pmic_detect()) { - p = get_pmic(); + p = pmic_get("FSL_PMIC"); mxc_request_iomux(MX35_PIN_WATCHDOG_RST, MUX_CONFIG_SION | MUX_CONFIG_ALT1);
diff --git a/board/freescale/mx51evk/mx51evk.c b/board/freescale/mx51evk/mx51evk.c index 7a0682a..4496ce9 100644 --- a/board/freescale/mx51evk/mx51evk.c +++ b/board/freescale/mx51evk/mx51evk.c @@ -32,7 +32,7 @@ #include <i2c.h> #include <mmc.h> #include <fsl_esdhc.h> -#include <pmic.h> +#include <power/pmic.h> #include <fsl_pmic.h> #include <mc13892.h> #include <usb/ehci-fsl.h> @@ -244,8 +244,8 @@ static void power_init(void) struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)MXC_CCM_BASE; struct pmic *p;
- pmic_init(); - p = get_pmic(); + pmic_init(I2C_PMIC); + p = pmic_get("FSL_PMIC");
/* Write needed to Power Gate 2 register */ pmic_reg_read(p, REG_POWER_MISC, &val); diff --git a/board/freescale/mx53evk/mx53evk.c b/board/freescale/mx53evk/mx53evk.c index b11a94c..e2d8b61 100644 --- a/board/freescale/mx53evk/mx53evk.c +++ b/board/freescale/mx53evk/mx53evk.c @@ -33,7 +33,7 @@ #include <i2c.h> #include <mmc.h> #include <fsl_esdhc.h> -#include <pmic.h> +#include <power/pmic.h> #include <fsl_pmic.h> #include <asm/gpio.h> #include <mc13892.h> @@ -123,8 +123,8 @@ void power_init(void) unsigned int val; struct pmic *p;
- pmic_init(); - p = get_pmic(); + pmic_init(I2C_PMIC); + p = pmic_get("FSL_PMIC");
/* Set VDDA to 1.25V */ pmic_reg_read(p, REG_SW_2, &val); diff --git a/board/freescale/mx53loco/mx53loco.c b/board/freescale/mx53loco/mx53loco.c index 8f82125..d69769f 100644 --- a/board/freescale/mx53loco/mx53loco.c +++ b/board/freescale/mx53loco/mx53loco.c @@ -36,7 +36,7 @@ #include <mmc.h> #include <fsl_esdhc.h> #include <asm/gpio.h> -#include <pmic.h> +#include <power/pmic.h> #include <dialog_pmic.h> #include <fsl_pmic.h> #include <linux/fb.h> @@ -343,8 +343,8 @@ static int power_init(void) struct pmic *p;
if (!i2c_probe(CONFIG_SYS_DIALOG_PMIC_I2C_ADDR)) { - pmic_dialog_init(); - p = get_pmic(); + pmic_dialog_init(I2C_PMIC); + p = pmic_get("DIALOG_PMIC");
/* Set VDDA to 1.25V */ val = DA9052_BUCKCORE_BCOREEN | DA_BUCKCORE_VBCORE_1_250V; @@ -360,8 +360,8 @@ static int power_init(void) }
if (!i2c_probe(CONFIG_SYS_FSL_PMIC_I2C_ADDR)) { - pmic_init(); - p = get_pmic(); + pmic_init(I2C_PMIC); + p = pmic_get("DIALOG_PMIC");
/* Set VDDGP to 1.25V for 1GHz on SW1 */ pmic_reg_read(p, REG_SW_0, &val); diff --git a/board/hale/tt01/tt01.c b/board/hale/tt01/tt01.c index 02e75ed..6e885c5 100644 --- a/board/hale/tt01/tt01.c +++ b/board/hale/tt01/tt01.c @@ -25,7 +25,7 @@ #include <common.h> #include <netdev.h> #include <command.h> -#include <pmic.h> +#include <power/pmic.h> #include <fsl_pmic.h> #include <mc13783.h> #include <asm/arch/clock.h> @@ -201,8 +201,8 @@ int board_mmc_init(bd_t *bis) * pmic_init() here. board_late_init() is too late for * the MMC driver. */ - pmic_init(); - p = get_pmic(); + pmic_init(I2C_PMIC); + p = pmic_get("FSL_PMIC");
/* configure pins for SDHC1 only */ mx31_gpio_mux(IOMUX_MODE(MUX_CTL_SD1_CLK, MUX_CTL_FUNC)); diff --git a/board/samsung/goni/goni.c b/board/samsung/goni/goni.c index e8fb1ea..94731e4a 100644 --- a/board/samsung/goni/goni.c +++ b/board/samsung/goni/goni.c @@ -25,10 +25,10 @@ #include <common.h> #include <asm/arch/gpio.h> #include <asm/arch/mmc.h> -#include <pmic.h> +#include <power/pmic.h> #include <usb/s3c_udc.h> #include <asm/arch/cpu.h> -#include <max8998_pmic.h> +#include <power/max8998_pmic.h> DECLARE_GLOBAL_DATA_PTR;
static struct s5pc110_gpio *s5pc110_gpio; @@ -42,8 +42,9 @@ int board_init(void) gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x100;
#if defined(CONFIG_PMIC) - pmic_init(); + pmic_init(I2C_5); #endif + return 0; }
@@ -108,7 +109,7 @@ static int s5pc1xx_phy_control(int on) { int ret; static int status; - struct pmic *p = get_pmic(); + struct pmic *p = pmic_get("MAX8998_PMIC");
if (pmic_probe(p)) return -1; diff --git a/board/samsung/trats/trats.c b/board/samsung/trats/trats.c index e11a892..80ec4ad 100644 --- a/board/samsung/trats/trats.c +++ b/board/samsung/trats/trats.c @@ -34,9 +34,9 @@ #include <asm/arch/mipi_dsim.h> #include <asm/arch/watchdog.h> #include <asm/arch/power.h> -#include <pmic.h> +#include <power/pmic.h> #include <usb/s3c_udc.h> -#include <max8997_pmic.h> +#include <power/max8997_pmic.h> #include <libtizen.h>
#include "setup.h" @@ -69,7 +69,7 @@ int board_init(void) printf("HW Revision:\t0x%x\n", board_rev);
#if defined(CONFIG_PMIC) - pmic_init(); + pmic_init(I2C_5); #endif
return 0; @@ -238,7 +238,7 @@ static int s5pc210_phy_control(int on) { int ret = 0; u32 val = 0; - struct pmic *p = get_pmic(); + struct pmic *p = pmic_get("MAX8997_PMIC");
if (pmic_probe(p)) return -1; @@ -413,7 +413,7 @@ static void lcd_reset(void) static int lcd_power(void) { int ret = 0; - struct pmic *p = get_pmic(); + struct pmic *p = pmic_get("MAX8997_PMIC");
if (pmic_probe(p)) return 0; @@ -473,7 +473,7 @@ static struct mipi_dsim_lcd_device mipi_lcd_device = { static int mipi_power(void) { int ret = 0; - struct pmic *p = get_pmic(); + struct pmic *p = pmic_get("MAX8997_PMIC");
if (pmic_probe(p)) return 0; diff --git a/board/samsung/universal_c210/universal.c b/board/samsung/universal_c210/universal.c index 90fff5c..80a7346 100644 --- a/board/samsung/universal_c210/universal.c +++ b/board/samsung/universal_c210/universal.c @@ -27,10 +27,10 @@ #include <asm/arch/adc.h> #include <asm/arch/gpio.h> #include <asm/arch/mmc.h> -#include <pmic.h> +#include <power/pmic.h> #include <usb/s3c_udc.h> #include <asm/arch/cpu.h> -#include <max8998_pmic.h> +#include <power/max8998_pmic.h>
DECLARE_GLOBAL_DATA_PTR;
@@ -59,7 +59,7 @@ int board_init(void) gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x100;
#if defined(CONFIG_PMIC) - pmic_init(); + pmic_init(I2C_5); #endif
check_hw_revision(); @@ -112,7 +112,7 @@ static unsigned short get_adc_value(int channel) static int adc_power_control(int on) { int ret; - struct pmic *p = get_pmic(); + struct pmic *p = pmic_get("MAX8998_PMIC");
if (pmic_probe(p)) return -1; @@ -280,7 +280,7 @@ int board_mmc_init(bd_t *bis) static int s5pc210_phy_control(int on) { int ret = 0; - struct pmic *p = get_pmic(); + struct pmic *p = pmic_get("MAX8998_PMIC");
if (pmic_probe(p)) return -1; diff --git a/board/ttcontrol/vision2/vision2.c b/board/ttcontrol/vision2/vision2.c index f28eab0..3cfa7dc 100644 --- a/board/ttcontrol/vision2/vision2.c +++ b/board/ttcontrol/vision2/vision2.c @@ -33,7 +33,7 @@ #include <asm/arch/sys_proto.h> #include <i2c.h> #include <mmc.h> -#include <pmic.h> +#include <power/pmic.h> #include <fsl_esdhc.h> #include <fsl_pmic.h> #include <mc13892.h> @@ -306,8 +306,8 @@ static void power_init_mx51(void) unsigned int val; struct pmic *p;
- pmic_init(); - p = get_pmic(); + pmic_init(I2C_PMIC); + p = pmic_get("FSL_PMIC");
/* Write needed to Power Gate 2 register */ pmic_reg_read(p, REG_POWER_MISC, &val); diff --git a/drivers/misc/pmic_core.c b/drivers/misc/pmic_core.c index 5d62a56..2908f66 100644 --- a/drivers/misc/pmic_core.c +++ b/drivers/misc/pmic_core.c @@ -27,18 +27,21 @@ */
#include <common.h> +#include <malloc.h> #include <linux/types.h> -#include <pmic.h> +#include <linux/list.h> +#include <power/pmic.h>
-static struct pmic pmic; +static LIST_HEAD(pmic_list);
-int check_reg(u32 reg) +int check_reg(struct pmic *p, u32 reg) { - if (reg >= pmic.number_of_regs) { + if (reg >= p->number_of_regs) { printf("<reg num> = %d is invalid. Should be less than %d\n", - reg, pmic.number_of_regs); + reg, p->number_of_regs); return -1; } + return 0; }
@@ -65,11 +68,16 @@ static void pmic_show_info(struct pmic *p) printf("PMIC: %s\n", p->name); }
-static void pmic_dump(struct pmic *p) +static int pmic_dump(struct pmic *p) { int i, ret; u32 val;
+ if (!p) { + puts("Wrong PMIC name!\n"); + return -1; + } + pmic_show_info(p); for (i = 0; i < p->number_of_regs; i++) { ret = pmic_reg_read(p, i, &val); @@ -82,36 +90,79 @@ static void pmic_dump(struct pmic *p) printf("%08x ", val); } puts("\n"); + return 0; }
-struct pmic *get_pmic(void) +struct pmic *pmic_alloc(void) { - return &pmic; + struct pmic *p; + + p = calloc(sizeof(*p), 1); + if (!p) { + printf("%s: No available memory for allocation!\n", __func__); + return NULL; + } + + list_add_tail(&p->list, &pmic_list); + + debug("%s: new pmic struct: 0x%p\n", __func__, p); + + return p; +} + +struct pmic *pmic_get(const char *s) +{ + struct pmic *p; + + list_for_each_entry(p, &pmic_list, list) { + if (strcmp(p->name, s) == 0) { + debug("%s: pmic %s -> 0x%p\n", __func__, p->name, p); + return p; + } + } + + return NULL; +} + +static void pmic_list_names(void) +{ + struct pmic *p; + + puts("PMIC devices:\n"); + list_for_each_entry(p, &pmic_list, list) { + printf("name: %s\n", p->name); + } }
int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { u32 ret, reg, val; + struct pmic *p; char *cmd;
- struct pmic *p = &pmic; - /* at least two arguments please */ if (argc < 2) - return cmd_usage(cmdtp); + return CMD_RET_USAGE;
cmd = argv[1]; + + if (strcmp(cmd, "list") == 0) { + pmic_list_names(); + return CMD_RET_SUCCESS; + } + if (strcmp(cmd, "dump") == 0) { - pmic_dump(p); - return 0; + if (pmic_dump(pmic_get(argv[2]))) + return CMD_RET_FAILURE; + return CMD_RET_SUCCESS; }
if (strcmp(cmd, "read") == 0) { - if (argc < 3) - return cmd_usage(cmdtp); - - reg = simple_strtoul(argv[2], NULL, 16); + if (argc < 4) + return CMD_RET_USAGE;
+ reg = simple_strtoul(argv[3], NULL, 16); + p = pmic_get(argv[2]); ret = pmic_reg_read(p, reg, &val);
if (ret) @@ -119,29 +170,30 @@ int do_pmic(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
printf("\n0x%02x: 0x%08x\n", reg, val);
- return 0; + return CMD_RET_SUCCESS; }
if (strcmp(cmd, "write") == 0) { - if (argc < 4) - return cmd_usage(cmdtp); - - reg = simple_strtoul(argv[2], NULL, 16); - val = simple_strtoul(argv[3], NULL, 16); + if (argc < 5) + return CMD_RET_USAGE;
+ reg = simple_strtoul(argv[3], NULL, 16); + val = simple_strtoul(argv[4], NULL, 16); + p = pmic_get(argv[2]); pmic_reg_write(p, reg, val);
- return 0; + return CMD_RET_SUCCESS; }
/* No subcommand found */ - return 1; + return CMD_RET_SUCCESS; }
U_BOOT_CMD( pmic, CONFIG_SYS_MAXARGS, 1, do_pmic, "PMIC", - "dump - dump PMIC registers\n" - "pmic read <reg> - read register\n" - "pmic write <reg> <value> - write register" + "list - list available PMICs\n" + "pmic dump name - dump named PMIC registers\n" + "pmic name read <reg> - read register\n" + "pmic name write <reg> <value> - write register" ); diff --git a/drivers/misc/pmic_dialog.c b/drivers/misc/pmic_dialog.c index e97af1d..d972211 100644 --- a/drivers/misc/pmic_dialog.c +++ b/drivers/misc/pmic_dialog.c @@ -17,12 +17,12 @@ */
#include <common.h> -#include <pmic.h> +#include <power/pmic.h> #include <dialog_pmic.h>
-int pmic_dialog_init(void) +int pmic_dialog_init(unsigned char bus) { - struct pmic *p = get_pmic(); + struct pmic *p = pmic_alloc(); static const char name[] = "DIALOG_PMIC";
p->name = name; @@ -31,7 +31,7 @@ int pmic_dialog_init(void) p->interface = PMIC_I2C; p->hw.i2c.addr = CONFIG_SYS_DIALOG_PMIC_I2C_ADDR; p->hw.i2c.tx_num = 1; - p->bus = I2C_PMIC; + p->bus = bus;
return 0; } diff --git a/drivers/misc/pmic_fsl.c b/drivers/misc/pmic_fsl.c index 0ff75ed..868c3c4 100644 --- a/drivers/misc/pmic_fsl.c +++ b/drivers/misc/pmic_fsl.c @@ -23,7 +23,7 @@
#include <common.h> #include <spi.h> -#include <pmic.h> +#include <power/pmic.h> #include <fsl_pmic.h>
#if defined(CONFIG_PMIC_SPI) @@ -33,9 +33,9 @@ static u32 pmic_spi_prepare_tx(u32 reg, u32 *val, u32 write) } #endif
-int pmic_init(void) +int pmic_init(unsigned char bus) { - struct pmic *p = get_pmic(); + struct pmic *p = pmic_alloc(); static const char name[] = "FSL_PMIC";
p->name = name; @@ -54,7 +54,7 @@ int pmic_init(void) p->interface = PMIC_I2C; p->hw.i2c.addr = CONFIG_SYS_FSL_PMIC_I2C_ADDR; p->hw.i2c.tx_num = 3; - p->bus = I2C_PMIC; + p->bus = bus; #else #error "You must select CONFIG_PMIC_SPI or CONFIG_PMIC_I2C" #endif diff --git a/drivers/misc/pmic_i2c.c b/drivers/misc/pmic_i2c.c index 1aa5b7b..6452ea9 100644 --- a/drivers/misc/pmic_i2c.c +++ b/drivers/misc/pmic_i2c.c @@ -28,14 +28,14 @@
#include <common.h> #include <linux/types.h> -#include <pmic.h> +#include <power/pmic.h> #include <i2c.h>
int pmic_reg_write(struct pmic *p, u32 reg, u32 val) { unsigned char buf[4] = { 0 };
- if (check_reg(reg)) + if (check_reg(p, reg)) return -1;
switch (pmic_i2c_tx_num) { @@ -78,7 +78,7 @@ int pmic_reg_read(struct pmic *p, u32 reg, u32 *val) unsigned char buf[4] = { 0 }; u32 ret_val = 0;
- if (check_reg(reg)) + if (check_reg(p, reg)) return -1;
if (i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num)) diff --git a/drivers/misc/pmic_max8997.c b/drivers/misc/pmic_max8997.c index 4943f66..4e8283a 100644 --- a/drivers/misc/pmic_max8997.c +++ b/drivers/misc/pmic_max8997.c @@ -22,13 +22,13 @@ */
#include <common.h> -#include <pmic.h> -#include <max8997_pmic.h> +#include <power/pmic.h> +#include <power/max8997_pmic.h> #include <i2c.h>
-int pmic_init(void) +int pmic_init(unsigned char bus) { - struct pmic *p = get_pmic(); + struct pmic *p = pmic_alloc(); static const char name[] = "MAX8997_PMIC";
puts("Board PMIC init\n"); @@ -38,7 +38,7 @@ int pmic_init(void) p->number_of_regs = PMIC_NUM_OF_REGS; p->hw.i2c.addr = MAX8997_I2C_ADDR; p->hw.i2c.tx_num = 1; - p->bus = I2C_0; + p->bus = bus;
return 0; } diff --git a/drivers/misc/pmic_max8998.c b/drivers/misc/pmic_max8998.c index cc69fd7..3cad60d 100644 --- a/drivers/misc/pmic_max8998.c +++ b/drivers/misc/pmic_max8998.c @@ -22,12 +22,12 @@ */
#include <common.h> -#include <pmic.h> -#include <max8998_pmic.h> +#include <power/pmic.h> +#include <power/max8998_pmic.h>
-int pmic_init(void) +int pmic_init(unsigned char bus) { - struct pmic *p = get_pmic(); + struct pmic *p = pmic_alloc(); static const char name[] = "MAX8998_PMIC";
puts("Board PMIC init\n"); @@ -37,7 +37,7 @@ int pmic_init(void) p->number_of_regs = PMIC_NUM_OF_REGS; p->hw.i2c.addr = MAX8998_I2C_ADDR; p->hw.i2c.tx_num = 1; - p->bus = I2C_PMIC; + p->bus = bus;
return 0; } diff --git a/drivers/misc/pmic_spi.c b/drivers/misc/pmic_spi.c index 5a0dd22..27488ea 100644 --- a/drivers/misc/pmic_spi.c +++ b/drivers/misc/pmic_spi.c @@ -28,7 +28,7 @@
#include <common.h> #include <linux/types.h> -#include <pmic.h> +#include <power/pmic.h> #include <spi.h>
static struct spi_slave *slave; @@ -59,7 +59,7 @@ static u32 pmic_reg(struct pmic *p, u32 reg, u32 *val, u32 write) return -1; }
- if (check_reg(reg)) + if (check_reg(p, reg)) return -1;
if (spi_claim_bus(slave)) diff --git a/drivers/rtc/mc13xxx-rtc.c b/drivers/rtc/mc13xxx-rtc.c index 70ea8a1..1a10588 100644 --- a/drivers/rtc/mc13xxx-rtc.c +++ b/drivers/rtc/mc13xxx-rtc.c @@ -23,14 +23,14 @@ #include <common.h> #include <rtc.h> #include <spi.h> -#include <pmic.h> +#include <power/pmic.h> #include <fsl_pmic.h>
int rtc_get(struct rtc_time *rtc) { u32 day1, day2, time; int tim, i = 0; - struct pmic *p = get_pmic(); + struct pmic *p = pmic_get("FSL_PMIC"); int ret;
do { @@ -61,7 +61,7 @@ int rtc_get(struct rtc_time *rtc) int rtc_set(struct rtc_time *rtc) { u32 time, day; - struct pmic *p = get_pmic(); + struct pmic *p = pmic_get("FSL_PMIC");
time = mktime(rtc->tm_year, rtc->tm_mon, rtc->tm_mday, rtc->tm_hour, rtc->tm_min, rtc->tm_sec); diff --git a/include/max8997_pmic.h b/include/power/max8997_pmic.h similarity index 92% rename from include/max8997_pmic.h rename to include/power/max8997_pmic.h index 17ae24e..af48a49 100644 --- a/include/max8997_pmic.h +++ b/include/power/max8997_pmic.h @@ -111,7 +111,7 @@ enum { MAX8997_REG_MBCCTRL6 = 0x55, MAX8997_REG_OTPCGHCVS = 0x56,
- MAX8997_REG_SAFEOUTCTRL = 0x5a, + MAX8997_REG_SAFEOUTCTRL = 0x5a,
MAX8997_REG_LBCNFG1 = 0x5e, MAX8997_REG_LBCNFG2 = 0x5f, @@ -171,9 +171,22 @@ enum { PMIC_NUM_OF_REGS = 0x9b, };
+#define ACTDISSAFEO1 (1 << 4) +#define ACTDISSAFEO2 (1 << 5) #define ENSAFEOUT1 (1 << 6) #define ENSAFEOUT2 (1 << 7)
+/* Charger */ +enum {CHARGER_ENABLE, CHARGER_DISABLE}; +#define DETBAT (1 << 2) +#define MBCICHFCSET (1 << 4) +#define MBCHOSTEN (1 << 6) +#define VCHGR_FC (1 << 7) + +#define CHARGER_MIN_CURRENT 200 +#define CHARGER_MAX_CURRENT 950 +#define CHARGER_CURRENT_RESOLUTION 50 + #define MAX8997_I2C_ADDR (0xCC >> 1) #define MAX8997_RTC_ADDR (0x0C >> 1) #define MAX8997_MUIC_ADDR (0x4A >> 1) @@ -187,4 +200,6 @@ enum { EN_LDO = (0x3 << 6), };
+unsigned char max8997_reg_ldo(int uV); + #endif /* __MAX8997_PMIC_H_ */ diff --git a/include/max8998_pmic.h b/include/power/max8998_pmic.h similarity index 100% rename from include/max8998_pmic.h rename to include/power/max8998_pmic.h diff --git a/include/pmic.h b/include/power/pmic.h similarity index 86% rename from include/pmic.h rename to include/power/pmic.h index 2166f73..8f88944 100644 --- a/include/pmic.h +++ b/include/power/pmic.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 Samsung Electronics + * Copyright (C) 2011-2012 Samsung Electronics * Lukasz Majewski l.majewski@samsung.com * * See file CREDITS for list of people who contributed to this @@ -24,6 +24,10 @@ #ifndef __CORE_PMIC_H_ #define __CORE_PMIC_H_
+#include <common.h> +#include <linux/list.h> +#include <i2c.h> + enum { PMIC_I2C, PMIC_SPI, }; enum { I2C_PMIC, I2C_NUM, }; enum { PMIC_READ, PMIC_WRITE, }; @@ -49,17 +53,20 @@ struct pmic { const char *name; unsigned char bus; unsigned char interface; - unsigned char number_of_regs; + unsigned int number_of_regs; union hw { struct p_i2c i2c; struct p_spi spi; } hw; + + struct list_head list; };
-int pmic_init(void); +int pmic_init(unsigned char bus); int pmic_dialog_init(void); -int check_reg(u32 reg); -struct pmic *get_pmic(void); +int check_reg(struct pmic *p, u32 reg); +struct pmic *pmic_alloc(void); +struct pmic *pmic_get(const char *s); int pmic_probe(struct pmic *p); int pmic_reg_read(struct pmic *p, u32 reg, u32 *val); int pmic_reg_write(struct pmic *p, u32 reg, u32 val);

It is necessary to introduce a new system wide function- power_board_init()
It turns out, that power initialization must be done as early as possible. In the case of PMIC framework redesign, which aims to support multiple instances of PMIC devices the initialization shall be performed just after malloc configuration.
The arch_early_init_r() could be used instead, but it doesn't reflect that the "power" subsystem is going to be initialized.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com --- arch/arm/lib/board.c | 4 ++++ include/common.h | 3 +++ 2 files changed, 7 insertions(+), 0 deletions(-)
diff --git a/arch/arm/lib/board.c b/arch/arm/lib/board.c index f1951e8..38d92fe 100644 --- a/arch/arm/lib/board.c +++ b/arch/arm/lib/board.c @@ -510,6 +510,10 @@ void board_init_r(gd_t *id, ulong dest_addr) arch_early_init_r(); #endif
+#ifdef CONFIG_POWER_INIT + power_board_init(); +#endif + #if !defined(CONFIG_SYS_NO_FLASH) puts("Flash: ");
diff --git a/include/common.h b/include/common.h index 55025c0..62c8e9a 100644 --- a/include/common.h +++ b/include/common.h @@ -487,6 +487,9 @@ int board_late_init (void); int board_postclk_init (void); /* after clocks/timebase, before env/serial */ int board_early_init_r (void); void board_poweroff (void); +#ifdef CONFIG_POWER_INIT +int power_board_init(void); +#endif
#if defined(CONFIG_SYS_DRAM_TEST) int testdram(void);

Enable support for power_board_init() method at TRATS board.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com --- board/samsung/trats/trats.c | 15 +++++++++++---- include/configs/trats.h | 1 + 2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/board/samsung/trats/trats.c b/board/samsung/trats/trats.c index 80ec4ad..0285668 100644 --- a/board/samsung/trats/trats.c +++ b/board/samsung/trats/trats.c @@ -68,10 +68,6 @@ int board_init(void) check_hw_revision(); printf("HW Revision:\t0x%x\n", board_rev);
-#if defined(CONFIG_PMIC) - pmic_init(I2C_5); -#endif - return 0; }
@@ -90,6 +86,17 @@ void i2c_init_board(void) s5p_gpio_direction_output(&gpio2->y4, 1, 1); }
+#ifdef CONFIG_POWER_INIT +int power_board_init(void) +{ + +#ifdef CONFIG_PMIC + pmic_init(I2C_5); +#endif + return 0; +} +#endif + int dram_init(void) { gd->ram_size = get_ram_size((long *)PHYS_SDRAM_1, PHYS_SDRAM_1_SIZE) + diff --git a/include/configs/trats.h b/include/configs/trats.h index e64a47e..b2324f7 100644 --- a/include/configs/trats.h +++ b/include/configs/trats.h @@ -255,6 +255,7 @@ #define CONFIG_SOFT_I2C_GPIO_SDA get_multi_sda_pin() #define I2C_INIT multi_i2c_init()
+#define CONFIG_POWER_INIT #define CONFIG_PMIC #define CONFIG_PMIC_I2C #define CONFIG_PMIC_MAX8997

New power_chrg.h file has been added to "bind" together common information about charging battery available in the system.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Stefano Babic sbabic@denx.de --- include/power/power_chrg.h | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 53 insertions(+), 0 deletions(-) create mode 100644 include/power/power_chrg.h
diff --git a/include/power/power_chrg.h b/include/power/power_chrg.h new file mode 100644 index 0000000..fb3c3a6 --- /dev/null +++ b/include/power/power_chrg.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2012 Samsung Electronics + * Lukasz Majewski l.majewski@samsung.com + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#ifndef __POWER_CHARGER_H_ +#define __POWER_CHARGER_H_ + +/* Type of available chargers */ +enum { + CHARGER_NO = 0, + CHARGER_TA, + CHARGER_USB, + CHARGER_TA_500, + CHARGER_UNKNOWN, +}; + +enum { + UNKNOWN, + EXT_SOURCE, + CHARGE, + NORMAL, +}; + +struct battery { + unsigned int version; + unsigned int state_of_chrg; + unsigned int time_to_empty; + unsigned int capacity; + unsigned int voltage_uV; + + unsigned int state; +}; + +#endif /* __POWER_CHARGER_H_ */

Support for MUIC (Micro USB Integrated Circuit) built into the MAX8997 power management device.
The MUIC device will work with redesigned PMIC framework.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Stefano Babic sbabic@denx.de --- drivers/misc/Makefile | 1 + drivers/misc/muic_max8997.c | 80 ++++++++++++++++++++++++++++++++++++++++++ include/power/max8997_muic.h | 61 ++++++++++++++++++++++++++++++++ include/power/power_chrg.h | 1 + 4 files changed, 143 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/muic_max8997.c create mode 100644 include/power/max8997_muic.h
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index 271463c..f08a800 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -41,6 +41,7 @@ COBJS-$(CONFIG_PMIC_I2C) += pmic_i2c.o COBJS-$(CONFIG_PMIC_SPI) += pmic_spi.o COBJS-$(CONFIG_PMIC_MAX8998) += pmic_max8998.o COBJS-$(CONFIG_PMIC_MAX8997) += pmic_max8997.o +COBJS-$(CONFIG_POWER_MUIC_MAX8997) += muic_max8997.o
COBJS := $(COBJS-y) SRCS := $(COBJS:.o=.c) diff --git a/drivers/misc/muic_max8997.c b/drivers/misc/muic_max8997.c new file mode 100644 index 0000000..d332c09 --- /dev/null +++ b/drivers/misc/muic_max8997.c @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2012 Samsung Electronics + * Lukasz Majewski l.majewski@samsung.com + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#include <common.h> +#include <power/pmic.h> +#include <power/power_chrg.h> +#include <power/max8997_muic.h> +#include <i2c.h> + +int power_muic_init(unsigned int bus) +{ + struct pmic *p = pmic_alloc(); + static const char name[] = "MAX8997_MUIC"; + + puts("Board Micro USB Interface Controller init\n"); + + p->name = name; + p->interface = PMIC_I2C; + p->number_of_regs = MUIC_NUM_OF_REGS; + p->hw.i2c.addr = MAX8997_MUIC_I2C_ADDR; + p->hw.i2c.tx_num = 1; + p->bus = bus; + + return 0; +} + +int power_chrg_get_type(void) +{ + unsigned int val; + unsigned char charge_type, charger; + struct pmic *p = pmic_get("MAX8997_MUIC"); + + if (pmic_probe(p)) + return CHARGER_NO; + + pmic_reg_read(p, MAX8997_MUIC_STATUS2, &val); + charge_type = val & MAX8997_MUIC_CHG_MASK; + + switch (charge_type) { + case MAX8997_MUIC_CHG_NO: + charger = CHARGER_NO; + break; + case MAX8997_MUIC_CHG_USB: + case MAX8997_MUIC_CHG_USB_D: + charger = CHARGER_USB; + break; + case MAX8997_MUIC_CHG_TA: + case MAX8997_MUIC_CHG_TA_1A: + charger = CHARGER_TA; + break; + case MAX8997_MUIC_CHG_TA_500: + charger = CHARGER_TA_500; + break; + default: + charger = CHARGER_UNKNOWN; + break; + } + + return charger; +} diff --git a/include/power/max8997_muic.h b/include/power/max8997_muic.h new file mode 100644 index 0000000..0149c12 --- /dev/null +++ b/include/power/max8997_muic.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2012 Samsung Electronics + * Lukasz Majewski l.majewski@samsung.com + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#ifndef __MAX8997_MUIC_H_ +#define __MAX8997_MUIC_H_ + +#include <power/power_chrg.h> + +/* MAX8997_MUIC_STATUS2 */ +#define MAX8997_MUIC_CHG_NO 0x00 +#define MAX8997_MUIC_CHG_USB 0x01 +#define MAX8997_MUIC_CHG_USB_D 0x02 +#define MAX8997_MUIC_CHG_TA 0x03 +#define MAX8997_MUIC_CHG_TA_500 0x04 +#define MAX8997_MUIC_CHG_TA_1A 0x05 +#define MAX8997_MUIC_CHG_MASK 0x07 + +/* MAX 8997 MUIC registers */ +enum { + MAX8997_MUIC_ID = 0x00, + MAX8997_MUIC_INT1 = 0x01, + MAX8997_MUIC_INT2 = 0x02, + MAX8997_MUIC_INT3 = 0x03, + MAX8997_MUIC_STATUS1 = 0x04, + MAX8997_MUIC_STATUS2 = 0x05, + MAX8997_MUIC_STATUS3 = 0x06, + MAX8997_MUIC_INTMASK1 = 0x07, + MAX8997_MUIC_INTMASK2 = 0x08, + MAX8997_MUIC_INTMASK3 = 0x09, + MAX8997_MUIC_CDETCTRL = 0x0A, + MAX8997_MUIC_CONTROL1 = 0x0C, + MAX8997_MUIC_CONTROL2 = 0x0D, + MAX8997_MUIC_CONTROL3 = 0x0E, + + MUIC_NUM_OF_REGS = 0x0F, +}; + +#define MAX8997_MUIC_I2C_ADDR (0x4A >> 1) + +int power_muic_init(unsigned int bus); +#endif /* __MAX8997_MUIC_H_ */ diff --git a/include/power/power_chrg.h b/include/power/power_chrg.h index fb3c3a6..ecbad46 100644 --- a/include/power/power_chrg.h +++ b/include/power/power_chrg.h @@ -50,4 +50,5 @@ struct battery { unsigned int state; };
+int power_chrg_get_type(void); #endif /* __POWER_CHARGER_H_ */

On 14/09/2012 17:40, Lukasz Majewski wrote:
Support for MUIC (Micro USB Integrated Circuit) built into the MAX8997 power management device.
The MUIC device will work with redesigned PMIC framework.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Stefano Babic sbabic@denx.de
Hi Lukasz,
drivers/misc/Makefile | 1 + drivers/misc/muic_max8997.c | 80 ++++++++++++++++++++++++++++++++++++++++++ include/power/max8997_muic.h | 61 ++++++++++++++++++++++++++++++++ include/power/power_chrg.h | 1 + 4 files changed, 143 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/muic_max8997.c create mode 100644 include/power/max8997_muic.h
I see now with this patch, but we have a mix between drivers/misc and drivers/power. I think this was done because the power directory was introduced later, but really all pmics under drivers/misc should be moved into drivers/power.
Maybe belong this one also to drivers/power ?
You added several "add-ons" for MAX8997. Maybe it is clearer if everything goes into a separate directory, such as power/max8997/
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index 271463c..f08a800 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -41,6 +41,7 @@ COBJS-$(CONFIG_PMIC_I2C) += pmic_i2c.o COBJS-$(CONFIG_PMIC_SPI) += pmic_spi.o COBJS-$(CONFIG_PMIC_MAX8998) += pmic_max8998.o COBJS-$(CONFIG_PMIC_MAX8997) += pmic_max8997.o +COBJS-$(CONFIG_POWER_MUIC_MAX8997) += muic_max8997.o
COBJS := $(COBJS-y) SRCS := $(COBJS:.o=.c) diff --git a/drivers/misc/muic_max8997.c b/drivers/misc/muic_max8997.c new file mode 100644 index 0000000..d332c09 --- /dev/null +++ b/drivers/misc/muic_max8997.c @@ -0,0 +1,80 @@ +/*
- Copyright (C) 2012 Samsung Electronics
- Lukasz Majewski l.majewski@samsung.com
- See file CREDITS for list of people who contributed to this
- project.
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation; either version 2 of
- the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- MA 02111-1307 USA
- */
+#include <common.h> +#include <power/pmic.h> +#include <power/power_chrg.h> +#include <power/max8997_muic.h> +#include <i2c.h>
+int power_muic_init(unsigned int bus)
You add a different function to initialize it, but this is a version opf pmic_init(). Why cannot we consider this one as all other pmics, avoiding to introduce new and undocumented functions ?
If I do not misunderstand, you use the different functions to select the pmic. In other words, calling power_update_battery() in the trats code selects automatically the fulel-gauge, because this code implements this function. But this is against the goal of your patches, that is to add multi instances of the pmics (and maybe a second instance of fuel_gauge..).
I think this should be solved moving this function to the general API and calling it via pointer. For example, something like:
p = pmic_get("MAX_8997); p->read(...); p = pmic_get("MAX17042_FG"); p->power_update_battery(..);
What do you think ?
Best regards, Stefano Babic

Hi Stefano,
On 14/09/2012 17:40, Lukasz Majewski wrote:
Support for MUIC (Micro USB Integrated Circuit) built into the MAX8997 power management device.
The MUIC device will work with redesigned PMIC framework.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Stefano Babic sbabic@denx.de
Hi Lukasz,
drivers/misc/Makefile | 1 + drivers/misc/muic_max8997.c | 80 ++++++++++++++++++++++++++++++++++++++++++ include/power/max8997_muic.h | 61 ++++++++++++++++++++++++++++++++ include/power/power_chrg.h | 1 + 4 files changed, 143 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/muic_max8997.c create mode 100644 include/power/max8997_muic.h
I see now with this patch, but we have a mix between drivers/misc and drivers/power. I think this was done because the power directory was introduced later, but really all pmics under drivers/misc should be moved into drivers/power.
Yes, I agree about that. PMIC (or in the future POWER) related code shall be moved to drivers/power/{subdir_per_PMIC_device}
Please also keep in mind, that I plan to rename the "pmic" command/framework to "power" to better reflect its purpose.
Maybe belong this one also to drivers/power ?
You added several "add-ons" for MAX8997. Maybe it is clearer if everything goes into a separate directory, such as power/max8997/
Very good idea. I've already proposed a little "cleanup" with putting all power related includes to ./include/power/ directory.
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index 271463c..f08a800 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -41,6 +41,7 @@ COBJS-$(CONFIG_PMIC_I2C) += pmic_i2c.o COBJS-$(CONFIG_PMIC_SPI) += pmic_spi.o COBJS-$(CONFIG_PMIC_MAX8998) += pmic_max8998.o COBJS-$(CONFIG_PMIC_MAX8997) += pmic_max8997.o +COBJS-$(CONFIG_POWER_MUIC_MAX8997) += muic_max8997.o
COBJS := $(COBJS-y) SRCS := $(COBJS:.o=.c) diff --git a/drivers/misc/muic_max8997.c b/drivers/misc/muic_max8997.c new file mode 100644 index 0000000..d332c09 --- /dev/null +++ b/drivers/misc/muic_max8997.c @@ -0,0 +1,80 @@ +/*
- Copyright (C) 2012 Samsung Electronics
- Lukasz Majewski l.majewski@samsung.com
- See file CREDITS for list of people who contributed to this
- project.
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation; either version 2 of
- the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public
License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- MA 02111-1307 USA
- */
+#include <common.h> +#include <power/pmic.h> +#include <power/power_chrg.h> +#include <power/max8997_muic.h> +#include <i2c.h>
+int power_muic_init(unsigned int bus)
You add a different function to initialize it, but this is a version opf pmic_init(). Why cannot we consider this one as all other pmics, avoiding to introduce new and undocumented functions ?
If I do not misunderstand, you use the different functions to select the pmic. In other words, calling power_update_battery() in the trats code selects automatically the fulel-gauge, because this code implements this function. But this is against the goal of your patches, that is to add multi instances of the pmics (and maybe a second instance of fuel_gauge..).
I think this should be solved moving this function to the general API and calling it via pointer. For example, something like:
p = pmic_get("MAX_8997); p->read(...);
p = pmic_get("MAX17042_FG"); p->power_update_battery(..);
What do you think ?
I will think about that and give you an answer.
Best regards, Stefano Babic
Regards, Lukasz

Hi Stefano,
On 14/09/2012 17:40, Lukasz Majewski wrote:
Support for MUIC (Micro USB Integrated Circuit) built into the MAX8997 power management device.
The MUIC device will work with redesigned PMIC framework.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Stefano Babic sbabic@denx.de
Hi Lukasz,
drivers/misc/Makefile | 1 + drivers/misc/muic_max8997.c | 80 ++++++++++++++++++++++++++++++++++++++++++ include/power/max8997_muic.h | 61 ++++++++++++++++++++++++++++++++ include/power/power_chrg.h | 1 + 4 files changed, 143 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/muic_max8997.c create mode 100644 include/power/max8997_muic.h
I see now with this patch, but we have a mix between drivers/misc and drivers/power. I think this was done because the power directory was introduced later, but really all pmics under drivers/misc should be moved into drivers/power.
Maybe belong this one also to drivers/power ?
You added several "add-ons" for MAX8997. Maybe it is clearer if everything goes into a separate directory, such as power/max8997/
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index 271463c..f08a800 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -41,6 +41,7 @@ COBJS-$(CONFIG_PMIC_I2C) += pmic_i2c.o COBJS-$(CONFIG_PMIC_SPI) += pmic_spi.o COBJS-$(CONFIG_PMIC_MAX8998) += pmic_max8998.o COBJS-$(CONFIG_PMIC_MAX8997) += pmic_max8997.o +COBJS-$(CONFIG_POWER_MUIC_MAX8997) += muic_max8997.o
COBJS := $(COBJS-y) SRCS := $(COBJS:.o=.c) diff --git a/drivers/misc/muic_max8997.c b/drivers/misc/muic_max8997.c new file mode 100644 index 0000000..d332c09 --- /dev/null +++ b/drivers/misc/muic_max8997.c @@ -0,0 +1,80 @@ +/*
- Copyright (C) 2012 Samsung Electronics
- Lukasz Majewski l.majewski@samsung.com
- See file CREDITS for list of people who contributed to this
- project.
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation; either version 2 of
- the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public
License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- MA 02111-1307 USA
- */
+#include <common.h> +#include <power/pmic.h> +#include <power/power_chrg.h> +#include <power/max8997_muic.h> +#include <i2c.h>
+int power_muic_init(unsigned int bus)
You add a different function to initialize it, but this is a version opf pmic_init(). Why cannot we consider this one as all other pmics, avoiding to introduce new and undocumented functions ?
If I do not misunderstand, you use the different functions to select the pmic. In other words, calling power_update_battery() in the trats code selects automatically the fulel-gauge, because this code implements this function. But this is against the goal of your patches, that is to add multi instances of the pmics (and maybe a second instance of fuel_gauge..).
I think this should be solved moving this function to the general API and calling it via pointer. For example, something like:
p = pmic_get("MAX_8997); p->read(...);
p = pmic_get("MAX17042_FG"); p->power_update_battery(..);
What do you think ?
I think that struct pmic shall be extended by another field:
struct pmic { struct power_ops *p_ops; };
struct bat_ops { int (*check_battery) (struct pmic *p); int (*update_battery) (struct pmic *p); int (*init_battery) (struct pmic *p);
[other battery related callbacks] };
Each instance of struct bat_ops is tied with power related device.
I will post v2 of the framework very soon.

Support for MAX17042 fuel-gauge (FG), which is built into the MAX8997 power management device. Special file - fg_battery_cell_params.h with cells characteristics added.
The FG device will work with redesigned PMIC framework.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Stefano Babic sbabic@denx.de --- drivers/misc/Makefile | 1 + drivers/misc/fg_max17042.c | 235 ++++++++++++++++++++++++++++++++ include/power/fg_battery_cell_params.h | 88 ++++++++++++ include/power/max17042_fg.h | 74 ++++++++++ include/power/power_chrg.h | 2 + 5 files changed, 400 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/fg_max17042.c create mode 100644 include/power/fg_battery_cell_params.h create mode 100644 include/power/max17042_fg.h
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index f08a800..f3b302b 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -41,6 +41,7 @@ COBJS-$(CONFIG_PMIC_I2C) += pmic_i2c.o COBJS-$(CONFIG_PMIC_SPI) += pmic_spi.o COBJS-$(CONFIG_PMIC_MAX8998) += pmic_max8998.o COBJS-$(CONFIG_PMIC_MAX8997) += pmic_max8997.o +COBJS-$(CONFIG_POWER_FG_MAX17042) += fg_max17042.o COBJS-$(CONFIG_POWER_MUIC_MAX8997) += muic_max8997.o
COBJS := $(COBJS-y) diff --git a/drivers/misc/fg_max17042.c b/drivers/misc/fg_max17042.c new file mode 100644 index 0000000..27285e8 --- /dev/null +++ b/drivers/misc/fg_max17042.c @@ -0,0 +1,235 @@ +/* + * Copyright (C) 2012 Samsung Electronics + * Lukasz Majewski l.majewski@samsung.com + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#include <common.h> +#include <power/pmic.h> +#include <power/max17042_fg.h> +#include <i2c.h> +#include <power/max8997_pmic.h> +#include <power/power_chrg.h> +#include <power/fg_battery_cell_params.h> + +int power_fg_init(unsigned char bus) +{ + struct pmic *p = pmic_alloc(); + static const char name[] = "MAX17042_FG"; + + debug("Board Fuel Gauge init\n"); + + p->name = name; + p->interface = PMIC_I2C; + p->number_of_regs = FG_NUM_OF_REGS; + p->hw.i2c.addr = MAX17042_I2C_ADDR; + p->hw.i2c.tx_num = 2; + p->hw.i2c.byte_order = PMIC_BYTE_ORDER_NORMAL; + p->bus = bus; + + return 0; +} + +static int fg_write_regs(struct pmic *p, u8 addr, u16 *data, int num) +{ + int ret = 0; + int i; + + for (i = 0; i < num; i++, addr++) + ret |= pmic_reg_write(p, addr, *(data + i)); + + return ret; +} + +static int fg_read_regs(struct pmic *p, u8 addr, u16 *data, int num) +{ + int ret = 0; + int i; + + for (i = 0; i < num; i++, addr++) + ret |= pmic_reg_read(p, addr, (u32 *) (data + i)); + + return ret; +} + +static int fg_write_and_verify(struct pmic *p, u8 addr, u16 data) +{ + unsigned int val = data; + int ret = 0; + + ret |= pmic_reg_write(p, addr, val); + ret |= pmic_reg_read(p, addr, &val); + + if (ret) + return ret; + + if (((u16) val) == data) + return 0; + + return -1; +} + +static void por_fuelgauge_init(struct pmic *p) +{ + u16 r_data0[16], r_data1[16], r_data2[16]; + u32 rewrite_count = 5, i = 0; + unsigned int val; + int ret = 0; + + /* Delay 500 ms */ + mdelay(500); + /* Initilize Configuration */ + pmic_reg_write(p, MAX17042_CONFIG, 0x2310); + +rewrite_model: + /* Unlock Model Access */ + pmic_reg_write(p, MAX17042_MLOCKReg1, MODEL_UNLOCK1); + pmic_reg_write(p, MAX17042_MLOCKReg2, MODEL_UNLOCK2); + + /* Write/Read/Verify the Custom Model */ + ret |= fg_write_regs(p, MAX17042_MODEL1, cell_character0, + ARRAY_SIZE(cell_character0)); + ret |= fg_write_regs(p, MAX17042_MODEL2, cell_character1, + ARRAY_SIZE(cell_character1)); + ret |= fg_write_regs(p, MAX17042_MODEL3, cell_character2, + ARRAY_SIZE(cell_character2)); + + if (ret) { + printf("%s: Cell parameters write failed!\n", __func__); + return; + } + + ret |= fg_read_regs(p, MAX17042_MODEL1, r_data0, ARRAY_SIZE(r_data0)); + ret |= fg_read_regs(p, MAX17042_MODEL2, r_data1, ARRAY_SIZE(r_data1)); + ret |= fg_read_regs(p, MAX17042_MODEL3, r_data2, ARRAY_SIZE(r_data2)); + + if (ret) + printf("%s: Cell parameters read failed!\n", __func__); + + for (i = 0; i < 16; i++) { + if ((cell_character0[i] != r_data0[i]) + || (cell_character1[i] != r_data1[i]) + || (cell_character2[i] != r_data2[i])) + goto rewrite_model; + } + + /* Lock model access */ + pmic_reg_write(p, MAX17042_MLOCKReg1, MODEL_LOCK1); + pmic_reg_write(p, MAX17042_MLOCKReg2, MODEL_LOCK2); + + /* Verify the model access is locked */ + ret |= fg_read_regs(p, MAX17042_MODEL1, r_data0, ARRAY_SIZE(r_data0)); + ret |= fg_read_regs(p, MAX17042_MODEL2, r_data1, ARRAY_SIZE(r_data1)); + ret |= fg_read_regs(p, MAX17042_MODEL3, r_data2, ARRAY_SIZE(r_data2)); + + if (ret) { + printf("%s: Cell parameters read failed!\n", __func__); + return; + } + + for (i = 0; i < ARRAY_SIZE(r_data0); i++) { + /* Check if model locked */ + if (r_data0[i] || r_data1[i] || r_data2[i]) { + /* Rewrite model data - prevent from endless loop */ + if (rewrite_count--) { + puts("FG - Lock model access failed!\n"); + goto rewrite_model; + } + } + } + + /* Write Custom Parameters */ + fg_write_and_verify(p, MAX17042_RCOMP0, RCOMP0); + fg_write_and_verify(p, MAX17042_TEMPCO, TempCo); + + /* Delay at least 350mS */ + mdelay(350); + + /* Initialization Complete */ + pmic_reg_read(p, MAX17042_STATUS, &val); + /* Write and Verify Status with POR bit Cleared */ + fg_write_and_verify(p, MAX17042_STATUS, val & ~MAX17042_POR); + + /* Delay at least 350 ms */ + mdelay(350); +} + +int power_update_battery(struct battery *bat) +{ + struct pmic *p = pmic_get("MAX17042_FG"); + unsigned int val; + int ret = 0; + + if (pmic_probe(p)) { + puts("Can't find max17042 fuel gauge\n"); + return -1; + } + + ret |= pmic_reg_read(p, MAX17042_VFSOC, &val); + bat->state_of_chrg = (val >> 8); + + pmic_reg_read(p, MAX17042_VCELL, &val); + debug("vfsoc: 0x%x\n", val); + bat->voltage_uV = ((val & 0xFFUL) >> 3) + ((val & 0xFF00) >> 3); + bat->voltage_uV = (bat->voltage_uV * 625); + + pmic_reg_read(p, 0x05, &val); + bat->capacity = val >> 2; + + return ret; +} + +int power_check_battery(struct battery *bat) +{ + struct pmic *p = pmic_get("MAX17042_FG"); + unsigned int val; + int ret = 0; + + if (pmic_probe(p)) { + puts("Can't find max17042 fuel gauge\n"); + return -1; + } + + ret |= pmic_reg_read(p, MAX17042_STATUS, &val); + debug("fg status: 0x%x\n", val); + + if (val == MAX17042_POR) + por_fuelgauge_init(p); + + ret |= pmic_reg_read(p, MAX17042_VERSION, &val); + bat->version = val; + + power_update_battery(bat); + printf("fg ver: 0x%x state_of_charge(SOC):%d%%\n", + bat->version, bat->state_of_chrg); + + printf("voltage:\t%d.%6.6d [V] (expected to be %d [mAh])\n", + bat->voltage_uV / 1000000, bat->voltage_uV % 1000000, + bat->capacity); + + if (bat->voltage_uV > 3850000) + bat->state = EXT_SOURCE; + else if (bat->voltage_uV < 3600000 || bat->state_of_chrg < 5) + bat->state = CHARGE; + else + bat->state = NORMAL; + + return ret; +} diff --git a/include/power/fg_battery_cell_params.h b/include/power/fg_battery_cell_params.h new file mode 100644 index 0000000..fd068a9 --- /dev/null +++ b/include/power/fg_battery_cell_params.h @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2012 Samsung Electronics + * Lukasz Majewski l.majewski@samsung.com + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#ifndef __FG_BATTERY_CELL_PARAMS_H_ +#define __FG_BATTERY_CELL_PARAMS_H_ + +#if defined(CONFIG_POWER_FG_MAX17042) && defined(CONFIG_TRATS) + +/* Cell characteristics - Exynos4 TRATS development board */ + +u16 cell_character0[16] /* Shall be written to addr 0x80h */ += { 0xA2A0, + 0xB6E0, + 0xB850, + 0xBAD0, + 0xBB20, + 0xBB70, + 0xBBC0, + 0xBC20, + 0xBC80, + 0xBCE0, + 0xBD80, + 0xBE20, + 0xC090, + 0xC420, + 0xC910, + 0xD070 +}; + +u16 cell_character1[16] /* Shall be written to addr 0x90h */ += { 0x0090, + 0x1A50, + 0x02F0, + 0x2060, + 0x2060, + 0x2E60, + 0x26A0, + 0x2DB0, + 0x2DB0, + 0x1870, + 0x2A20, + 0x16F0, + 0x08F0, + 0x0D40, + 0x08C0, + 0x08C0 +}; + +u16 cell_character2[16] /* Shall be written to addr 0xA0h */ += { 0x0100, + 0x0100, + 0x0100, + 0x0100, + 0x0100, + 0x0100, + 0x0100, + 0x0100, + 0x0100, + 0x0100, + 0x0100, + 0x0100, + 0x0100, + 0x0100, + 0x0100, + 0x0100 +}; +#endif +#endif /* __FG_BATTERY_CELL_PARAMS_H_ */ diff --git a/include/power/max17042_fg.h b/include/power/max17042_fg.h new file mode 100644 index 0000000..1103a48 --- /dev/null +++ b/include/power/max17042_fg.h @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2012 Samsung Electronics + * Lukasz Majewski l.majewski@samsung.com + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#ifndef __MAX17042_FG_H_ +#define __MAX17042_FG_H_ + +/* MAX 17042 registers */ +enum { + MAX17042_STATUS = 0x00, + MAX17042_SOCREP = 0x06, + MAX17042_VCELL = 0x09, + MAX17042_CURRENT = 0x0A, + MAX17042_AVG_CURRENT = 0x0B, + MAX17042_SOCMIX = 0x0D, + MAX17042_SOCAV = 0x0E, + MAX17042_DESIGN_CAP = 0x18, + MAX17042_AVG_VCELL = 0x19, + MAX17042_CONFIG = 0x1D, + MAX17042_VERSION = 0x21, + MAX17042_LEARNCFG = 0x28, + MAX17042_FILTERCFG = 0x29, + MAX17042_RELAXCFG = 0x2A, + MAX17042_MISCCFG = 0x2B, + MAX17042_CGAIN = 0x2E, + MAX17042_COFF = 0x2F, + MAX17042_RCOMP0 = 0x38, + MAX17042_TEMPCO = 0x39, + MAX17042_FSTAT = 0x3D, + MAX17042_MLOCKReg1 = 0x62, + MAX17042_MLOCKReg2 = 0x63, + MAX17042_MODEL1 = 0x80, + MAX17042_MODEL2 = 0x90, + MAX17042_MODEL3 = 0xA0, + MAX17042_VFOCV = 0xFB, + MAX17042_VFSOC = 0xFF, + + FG_NUM_OF_REGS = 0x100, +}; + +#define RCOMP0 0x0060 +#define TempCo 0x1015 + + +#define MAX17042_POR (1 << 1) + +#define MODEL_UNLOCK1 0x0059 +#define MODEL_UNLOCK2 0x00c4 +#define MODEL_LOCK1 0x0000 +#define MODEL_LOCK2 0x0000 + +#define MAX17042_I2C_ADDR (0x6C >> 1) + +int power_fg_init(unsigned char bus); +#endif /* __MAX17042_FG_H_ */ diff --git a/include/power/power_chrg.h b/include/power/power_chrg.h index ecbad46..c8ae650 100644 --- a/include/power/power_chrg.h +++ b/include/power/power_chrg.h @@ -50,5 +50,7 @@ struct battery { unsigned int state; };
+int power_update_battery(struct battery *bat); +int power_check_battery(struct battery *bat); int power_chrg_get_type(void); #endif /* __POWER_CHARGER_H_ */

On Fri, Sep 14, 2012 at 05:40:08PM +0200, Lukasz Majewski wrote:
Support for MAX17042 fuel-gauge (FG), which is built into the MAX8997 power management device. Special file - fg_battery_cell_params.h with cells characteristics added.
[snip]
+#ifndef __FG_BATTERY_CELL_PARAMS_H_ +#define __FG_BATTERY_CELL_PARAMS_H_
+#if defined(CONFIG_POWER_FG_MAX17042) && defined(CONFIG_TRATS)
+/* Cell characteristics - Exynos4 TRATS development board */
+u16 cell_character0[16] /* Shall be written to addr 0x80h */ += { 0xA2A0,
This (and the rest of the file) should be: /* Shall be written to addr 0x80h */ u16 cell_character0[16] = { 0xA2A0, ...
Thanks.
Also: [snip]
- MAX17042_AVG_CURRENT = 0x0B,
- MAX17042_SOCMIX = 0x0D,
- MAX17042_SOCAV = 0x0E,
I assume this is just odd formatting in the mail and once applied it's aligned. If not, please fix (and check the rest of the series). Thanks!

Hi Tom ,
On Fri, Sep 14, 2012 at 05:40:08PM +0200, Lukasz Majewski wrote:
Support for MAX17042 fuel-gauge (FG), which is built into the MAX8997 power management device. Special file - fg_battery_cell_params.h with cells characteristics added.
[snip]
+#ifndef __FG_BATTERY_CELL_PARAMS_H_ +#define __FG_BATTERY_CELL_PARAMS_H_
+#if defined(CONFIG_POWER_FG_MAX17042) && defined(CONFIG_TRATS)
+/* Cell characteristics - Exynos4 TRATS development board */
+u16 cell_character0[16] /* Shall be written to addr 0x80h */ += { 0xA2A0,
This (and the rest of the file) should be: /* Shall be written to addr 0x80h */ u16 cell_character0[16] = { 0xA2A0, ...
Ok, I will fix this.
Thanks.
Also: [snip]
- MAX17042_AVG_CURRENT = 0x0B,
- MAX17042_SOCMIX = 0x0D,
- MAX17042_SOCAV = 0x0E,
I assume this is just odd formatting in the mail and once applied it's aligned. If not, please fix (and check the rest of the series). Thanks!
I will double check that. Nether checkpatch nor emac's whitespace-cleanup weren't complaining.
Regards, Lukasz

Function for calculating LDO internal register value from passed micro Volt.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Stefano Babic sbabic@denx.de --- drivers/misc/pmic_max8997.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)
diff --git a/drivers/misc/pmic_max8997.c b/drivers/misc/pmic_max8997.c index 4e8283a..a4158e2 100644 --- a/drivers/misc/pmic_max8997.c +++ b/drivers/misc/pmic_max8997.c @@ -42,3 +42,20 @@ int pmic_init(unsigned char bus)
return 0; } + +unsigned char max8997_reg_ldo(int uV) +{ + unsigned char ret; + if (uV <= 800000) + return 0; + if (uV >= 3950000) + return 0x3f; + ret = (uV - 800000) / 50000; + if (ret > 0x3f) { + printf("MAX8997 LDO SETTING ERROR (%duV) -> %u\n", uV, ret); + ret = 0x3f; + } + + return ret; +} +

On Fri, Sep 14, 2012 at 05:40:09PM +0200, Lukasz Majewski wrote:
Function for calculating LDO internal register value from passed micro Volt.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Stefano Babic sbabic@denx.de
drivers/misc/pmic_max8997.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)
diff --git a/drivers/misc/pmic_max8997.c b/drivers/misc/pmic_max8997.c index 4e8283a..a4158e2 100644 --- a/drivers/misc/pmic_max8997.c +++ b/drivers/misc/pmic_max8997.c @@ -42,3 +42,20 @@ int pmic_init(unsigned char bus)
return 0; }
+unsigned char max8997_reg_ldo(int uV) +{
- unsigned char ret;
- if (uV <= 800000)
return 0;
- if (uV >= 3950000)
return 0x3f;
- ret = (uV - 800000) / 50000;
- if (ret > 0x3f) {
printf("MAX8997 LDO SETTING ERROR (%duV) -> %u\n", uV, ret);
ret = 0x3f;
- }
Can we get a comment to point at the doc that says what these values are? Maybe even a #define for the return values.

On Fri, 14 Sep 2012 10:24:49 -0700 Tom Rini trini@ti.com wrote:
On Fri, Sep 14, 2012 at 05:40:09PM +0200, Lukasz Majewski wrote:
Function for calculating LDO internal register value from passed micro Volt.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Stefano Babic sbabic@denx.de
drivers/misc/pmic_max8997.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)
diff --git a/drivers/misc/pmic_max8997.c b/drivers/misc/pmic_max8997.c index 4e8283a..a4158e2 100644 --- a/drivers/misc/pmic_max8997.c +++ b/drivers/misc/pmic_max8997.c @@ -42,3 +42,20 @@ int pmic_init(unsigned char bus)
return 0; }
+unsigned char max8997_reg_ldo(int uV) +{
- unsigned char ret;
- if (uV <= 800000)
return 0;
- if (uV >= 3950000)
return 0x3f;
- ret = (uV - 800000) / 50000;
- if (ret > 0x3f) {
printf("MAX8997 LDO SETTING ERROR (%duV) -> %u\n",
uV, ret);
ret = 0x3f;
- }
Can we get a comment to point at the doc that says what these values are? Maybe even a #define for the return values.
Yes, explanation will be provided.
Regards, Lukasz Majewski

Default PMIC (MAX8997) initialization for Samsung's TRATS development board.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Minkyu Kang mk7.kang@samsung.com --- board/samsung/trats/trats.c | 150 ++++++++++++++++++++++++++++++++++++++++++ include/power/max8997_pmic.h | 9 +++ 2 files changed, 159 insertions(+), 0 deletions(-)
diff --git a/board/samsung/trats/trats.c b/board/samsung/trats/trats.c index 0285668..bb83b94 100644 --- a/board/samsung/trats/trats.c +++ b/board/samsung/trats/trats.c @@ -86,6 +86,155 @@ void i2c_init_board(void) s5p_gpio_direction_output(&gpio2->y4, 1, 1); }
+static int pmic_init_max8997(void) +{ + struct pmic *p = pmic_get("MAX8997_PMIC"); + int i = 0, ret = 0; + u32 val; + + if (pmic_probe(p)) + return -1; + + /* BUCK1 VARM: 1.2V */ + val = (1200000 - 650000) / 25000; + ret |= pmic_reg_write(p, MAX8997_REG_BUCK1DVS1, val); + val = ENBUCK | ACTIVE_DISCHARGE; /* DVS OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_BUCK1CTRL, val); + + /* BUCK2 VINT: 1.1V */ + val = (1100000 - 650000) / 25000; + ret |= pmic_reg_write(p, MAX8997_REG_BUCK2DVS1, val); + val = ENBUCK | ACTIVE_DISCHARGE; /* DVS OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_BUCK2CTRL, val); + + + /* BUCK3 G3D: 1.1V - OFF */ + ret |= pmic_reg_read(p, MAX8997_REG_BUCK3CTRL, &val); + val &= ~ENBUCK; + ret |= pmic_reg_write(p, MAX8997_REG_BUCK3CTRL, val); + + val = (1100000 - 750000) / 50000; + ret |= pmic_reg_write(p, MAX8997_REG_BUCK3DVS, val); + + /* BUCK4 CAMISP: 1.2V - OFF */ + ret |= pmic_reg_read(p, MAX8997_REG_BUCK4CTRL, &val); + val &= ~ENBUCK; + ret |= pmic_reg_write(p, MAX8997_REG_BUCK4CTRL, val); + + val = (1200000 - 650000) / 25000; + ret |= pmic_reg_write(p, MAX8997_REG_BUCK4DVS, val); + + /* BUCK5 VMEM: 1.2V */ + val = (1200000 - 650000) / 25000; + for (i = 0; i < 8; i++) + ret |= pmic_reg_write(p, MAX8997_REG_BUCK5DVS1 + i, val); + + val = ENBUCK | ACTIVE_DISCHARGE; /* DVS OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_BUCK5CTRL, val); + + /* BUCK6 CAM AF: 2.8V */ + /* No Voltage Setting Register */ + /* GNSLCT 3.0X */ + val = GNSLCT; + ret |= pmic_reg_write(p, MAX8997_REG_BUCK6CTRL, val); + + /* BUCK7 VCC_SUB: 2.0V */ + val = (2000000 - 750000) / 50000; + ret |= pmic_reg_write(p, MAX8997_REG_BUCK7DVS, val); + + /* LDO1 VADC: 3.3V */ + val = max8997_reg_ldo(3300000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO1CTRL, val); + + /* LDO1 Disable active discharging */ + ret |= pmic_reg_read(p, MAX8997_REG_LDO1CONFIG, &val); + val &= ~LDO_ADE; + ret |= pmic_reg_write(p, MAX8997_REG_LDO1CONFIG, val); + + /* LDO2 VALIVE: 1.1V */ + val = max8997_reg_ldo(1100000) | EN_LDO; + ret |= pmic_reg_write(p, MAX8997_REG_LDO2CTRL, val); + + /* LDO3 VUSB/MIPI: 1.1V */ + val = max8997_reg_ldo(1100000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO3CTRL, val); + + /* LDO4 VMIPI: 1.8V */ + val = max8997_reg_ldo(1800000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO4CTRL, val); + + /* LDO5 VHSIC: 1.2V */ + val = max8997_reg_ldo(1200000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO5CTRL, val); + + /* LDO6 VCC_1.8V_PDA: 1.8V */ + val = max8997_reg_ldo(1800000) | EN_LDO; + ret |= pmic_reg_write(p, MAX8997_REG_LDO6CTRL, val); + + /* LDO7 CAM_ISP: 1.8V */ + val = max8997_reg_ldo(1800000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO7CTRL, val); + + /* LDO8 VDAC/VUSB: 3.3V */ + val = max8997_reg_ldo(3300000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO8CTRL, val); + + /* LDO9 VCC_2.8V_PDA: 2.8V */ + val = max8997_reg_ldo(2800000) | EN_LDO; + ret |= pmic_reg_write(p, MAX8997_REG_LDO9CTRL, val); + + /* LDO10 VPLL: 1.1V */ + val = max8997_reg_ldo(1100000) | EN_LDO; + ret |= pmic_reg_write(p, MAX8997_REG_LDO10CTRL, val); + + /* LDO11 TOUCH: 2.8V */ + val = max8997_reg_ldo(2800000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO11CTRL, val); + + /* LDO12 VTCAM: 1.8V */ + val = max8997_reg_ldo(1800000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO12CTRL, val); + + /* LDO13 VCC_3.0_LCD: 3.0V */ + val = max8997_reg_ldo(3000000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO13CTRL, val); + + /* LDO14 MOTOR: 3.0V */ + val = max8997_reg_ldo(3000000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO14CTRL, val); + + /* LDO15 LED_A: 2.8V */ + val = max8997_reg_ldo(2800000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO15CTRL, val); + + /* LDO16 CAM_SENSOR: 1.8V */ + val = max8997_reg_ldo(1800000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO16CTRL, val); + + /* LDO17 VTF: 2.8V */ + val = max8997_reg_ldo(2800000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO17CTRL, val); + + /* LDO18 TOUCH_LED 3.3V */ + val = max8997_reg_ldo(3300000) | DIS_LDO; /* OFF */ + ret |= pmic_reg_write(p, MAX8997_REG_LDO18CTRL, val); + + /* LDO21 VDDQ: 1.2V */ + val = max8997_reg_ldo(1200000) | EN_LDO; + ret |= pmic_reg_write(p, MAX8997_REG_LDO21CTRL, val); + + /* SAFEOUT for both 1 and 2: 4.9V, Active discharge, Enable */ + val = (SAFEOUT_4_90V << 0) | (SAFEOUT_4_90V << 2) | + ACTDISSAFEO1 | ACTDISSAFEO2 | ENSAFEOUT1 | ENSAFEOUT2; + ret |= pmic_reg_write(p, MAX8997_REG_SAFEOUTCTRL, val); + + if (ret) { + puts("MAX8997 PMIC setting error!\n"); + return -1; + } + return 0; +} + #ifdef CONFIG_POWER_INIT int power_board_init(void) { @@ -93,6 +242,7 @@ int power_board_init(void) #ifdef CONFIG_PMIC pmic_init(I2C_5); #endif + pmic_init_max8997(); return 0; } #endif diff --git a/include/power/max8997_pmic.h b/include/power/max8997_pmic.h index af48a49..b7edee4 100644 --- a/include/power/max8997_pmic.h +++ b/include/power/max8997_pmic.h @@ -176,6 +176,15 @@ enum { #define ENSAFEOUT1 (1 << 6) #define ENSAFEOUT2 (1 << 7)
+#define ENBUCK (1 << 0) +#define ACTIVE_DISCHARGE (1 << 3) +#define GNSLCT (1 << 2) +#define LDO_ADE (1 << 1) +#define SAFEOUT_4_85V 0x00 +#define SAFEOUT_4_90V 0x01 +#define SAFEOUT_4_95V 0x02 +#define SAFEOUT_3_30V 0x03 + /* Charger */ enum {CHARGER_ENABLE, CHARGER_DISABLE}; #define DETBAT (1 << 2)

MUIC IC built into the MAX8997 device is enabled at TRATS.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Minkyu Kang mk7.kang@samsung.com --- board/samsung/trats/trats.c | 2 ++ include/configs/trats.h | 2 ++ 2 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/board/samsung/trats/trats.c b/board/samsung/trats/trats.c index bb83b94..c729fb8 100644 --- a/board/samsung/trats/trats.c +++ b/board/samsung/trats/trats.c @@ -38,6 +38,7 @@ #include <usb/s3c_udc.h> #include <power/max8997_pmic.h> #include <libtizen.h> +#include <power/max8997_muic.h>
#include "setup.h"
@@ -243,6 +244,7 @@ int power_board_init(void) pmic_init(I2C_5); #endif pmic_init_max8997(); + power_muic_init(I2C_5); return 0; } #endif diff --git a/include/configs/trats.h b/include/configs/trats.h index b2324f7..ad79416 100644 --- a/include/configs/trats.h +++ b/include/configs/trats.h @@ -260,6 +260,8 @@ #define CONFIG_PMIC_I2C #define CONFIG_PMIC_MAX8997
+#define CONFIG_POWER_MUIC +#define CONFIG_POWER_MUIC_MAX8997 #define CONFIG_USB_GADGET #define CONFIG_USB_GADGET_S3C_UDC_OTG #define CONFIG_USB_GADGET_DUALSPEED

FG IC built into the MAX8997 device (compliant to MAX17042) is enabled at TRATS.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Minkyu Kang mk7.kang@samsung.com --- board/samsung/trats/trats.c | 2 ++ include/configs/trats.h | 4 ++++ 2 files changed, 6 insertions(+), 0 deletions(-)
diff --git a/board/samsung/trats/trats.c b/board/samsung/trats/trats.c index c729fb8..6462b30 100644 --- a/board/samsung/trats/trats.c +++ b/board/samsung/trats/trats.c @@ -39,6 +39,7 @@ #include <power/max8997_pmic.h> #include <libtizen.h> #include <power/max8997_muic.h> +#include <power/max17042_fg.h>
#include "setup.h"
@@ -244,6 +245,7 @@ int power_board_init(void) pmic_init(I2C_5); #endif pmic_init_max8997(); + power_fg_init(I2C_9); power_muic_init(I2C_5); return 0; } diff --git a/include/configs/trats.h b/include/configs/trats.h index ad79416..297bff6 100644 --- a/include/configs/trats.h +++ b/include/configs/trats.h @@ -260,8 +260,12 @@ #define CONFIG_PMIC_I2C #define CONFIG_PMIC_MAX8997
+#define CONFIG_POWER_FG +#define CONFIG_POWER_FG_MAX17042 #define CONFIG_POWER_MUIC #define CONFIG_POWER_MUIC_MAX8997 + + #define CONFIG_USB_GADGET #define CONFIG_USB_GADGET_S3C_UDC_OTG #define CONFIG_USB_GADGET_DUALSPEED

On Fri, Sep 14, 2012 at 05:40:12PM +0200, Lukasz Majewski wrote:
FG IC built into the MAX8997 device (compliant to MAX17042) is enabled at TRATS.
[snip]
+#define CONFIG_POWER_FG +#define CONFIG_POWER_FG_MAX17042 #define CONFIG_POWER_MUIC #define CONFIG_POWER_MUIC_MAX8997
#define CONFIG_USB_GADGET
Double space? :)

Hi Tom,
On Fri, Sep 14, 2012 at 05:40:12PM +0200, Lukasz Majewski wrote:
FG IC built into the MAX8997 device (compliant to MAX17042) is enabled at TRATS.
[snip]
+#define CONFIG_POWER_FG +#define CONFIG_POWER_FG_MAX17042 #define CONFIG_POWER_MUIC #define CONFIG_POWER_MUIC_MAX8997
#define CONFIG_USB_GADGET
Double space? :)
Will be fixed :-)
Regards, Lukasz

Support for MAX8997 built-in charger.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Stefano Babic sbabic@denx.de --- drivers/misc/pmic_max8997.c | 52 ++++++++++++++++++++++++++++++++++++++++++ include/power/max8997_pmic.h | 3 +- 2 files changed, 54 insertions(+), 1 deletions(-)
diff --git a/drivers/misc/pmic_max8997.c b/drivers/misc/pmic_max8997.c index a4158e2..5fd2ae8 100644 --- a/drivers/misc/pmic_max8997.c +++ b/drivers/misc/pmic_max8997.c @@ -59,3 +59,55 @@ unsigned char max8997_reg_ldo(int uV) return ret; }
+int pmic_charger_state(int state, int current) +{ + struct pmic *p = pmic_get("MAX8997_PMIC"); + unsigned char fc; + u32 val = 0; + + if (pmic_probe(p)) + return -1; + + if (state == CHARGER_DISABLE) { + puts("Disable the charger.\n"); + pmic_reg_read(p, MAX8997_REG_MBCCTRL2, &val); + val &= ~(MBCHOSTEN | VCHGR_FC); + pmic_reg_write(p, MAX8997_REG_MBCCTRL2, val); + + return -1; + } + + if (current < CHARGER_MIN_CURRENT || current > CHARGER_MAX_CURRENT) { + printf("%s: Wrong charge current: %d [mA]\n", + __func__, current); + return -1; + } + + fc = (current - CHARGER_MIN_CURRENT) / CHARGER_CURRENT_RESOLUTION; + fc = fc & 0xf; /* up to 950 mA */ + + printf("Enable the charger @ %d [mA]\n", fc * CHARGER_CURRENT_RESOLUTION + + CHARGER_MIN_CURRENT); + + val = fc | MBCICHFCSET; + pmic_reg_write(p, MAX8997_REG_MBCCTRL4, val); + + pmic_reg_read(p, MAX8997_REG_MBCCTRL2, &val); + val = MBCHOSTEN | VCHGR_FC; /* enable charger & fast charge */ + pmic_reg_write(p, MAX8997_REG_MBCCTRL2, val); + + return 0; +} + +int pmic_charger_bat_present(void) +{ + struct pmic *p = pmic_get("MAX8997_PMIC"); + u32 val; + + if (pmic_probe(p)) + return -1; + + pmic_reg_read(p, MAX8997_REG_STATUS4, &val); + + return !(val & DETBAT); +} diff --git a/include/power/max8997_pmic.h b/include/power/max8997_pmic.h index b7edee4..b3baada 100644 --- a/include/power/max8997_pmic.h +++ b/include/power/max8997_pmic.h @@ -209,6 +209,7 @@ enum { EN_LDO = (0x3 << 6), };
+int pmic_charger_state(int state, int current); unsigned char max8997_reg_ldo(int uV); - +int pmic_charger_bat_present(void); #endif /* __MAX8997_PMIC_H_ */

The battery connected to Samsung's Trats development board is now charged when voltage drops below threshold.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Minkyu Kang mk7.kang@samsung.com --- board/samsung/trats/trats.c | 37 +++++++++++++++++++++++++++++++++++++ 1 files changed, 37 insertions(+), 0 deletions(-)
diff --git a/board/samsung/trats/trats.c b/board/samsung/trats/trats.c index 6462b30..21050c8 100644 --- a/board/samsung/trats/trats.c +++ b/board/samsung/trats/trats.c @@ -39,6 +39,7 @@ #include <power/max8997_pmic.h> #include <libtizen.h> #include <power/max8997_muic.h> +#include <power/power_chrg.h> #include <power/max17042_fg.h>
#include "setup.h" @@ -240,6 +241,8 @@ static int pmic_init_max8997(void) #ifdef CONFIG_POWER_INIT int power_board_init(void) { + int chrg, k; + struct battery bat;
#ifdef CONFIG_PMIC pmic_init(I2C_5); @@ -247,6 +250,40 @@ int power_board_init(void) pmic_init_max8997(); power_fg_init(I2C_9); power_muic_init(I2C_5); + + chrg = power_chrg_get_type(); + debug("CHARGER TYPE: %d\n", chrg); + + if (!pmic_charger_bat_present()) { + puts("No battery detected\n"); + return -1; + } + + power_check_battery(&bat); + + if (bat.state == CHARGE && chrg == CHARGER_USB) { + puts("CHARGER ENABLE\n"); + if (pmic_charger_state(CHARGER_ENABLE, 450)) + return -1; + + for (k = 0; pmic_charger_bat_present() && + power_chrg_get_type() && + bat.state_of_chrg < 5; k++) { + udelay(10000000); + puts("."); + power_update_battery(&bat); + + if (k == 100) { + printf(" %d [V]\n", bat.voltage_uV); + k = 0; + } + + } + + puts("CHARGER DISABLE\n"); + pmic_charger_state(CHARGER_DISABLE, 0); + } + return 0; } #endif

On Fri, Sep 14, 2012 at 05:40:14PM +0200, Lukasz Majewski wrote:
The battery connected to Samsung's Trats development board is now charged when voltage drops below threshold.
[snip]
@@ -247,6 +250,40 @@ int power_board_init(void) pmic_init_max8997(); power_fg_init(I2C_9); power_muic_init(I2C_5);
- chrg = power_chrg_get_type();
- debug("CHARGER TYPE: %d\n", chrg);
- if (!pmic_charger_bat_present()) {
puts("No battery detected\n");
return -1;
- }
- power_check_battery(&bat);
- if (bat.state == CHARGE && chrg == CHARGER_USB) {
puts("CHARGER ENABLE\n");
if (pmic_charger_state(CHARGER_ENABLE, 450))
return -1;
for (k = 0; pmic_charger_bat_present() &&
power_chrg_get_type() &&
bat.state_of_chrg < 5; k++) {
udelay(10000000);
puts(".");
power_update_battery(&bat);
if (k == 100) {
printf(" %d [V]\n", bat.voltage_uV);
k = 0;
}
}
puts("CHARGER DISABLE\n");
pmic_charger_state(CHARGER_DISABLE, 0);
- }
If I read this right we'll pause in the middle of start up to charge the battery for possibly a long time right? And this could be a while loop even, yes? If so to the first one, this really should be under some sort of CONFIG option. I know the value of showing proof of concept type examples in development boards but that should still be an opt-in thing I would think.

Hi Tom ,
If I read this right we'll pause in the middle of start up to charge the battery for possibly a long time right? And this could be a while loop even, yes? If so to the first one, this really should be under some sort of CONFIG option.
This is one of the options. Other option is to extend the pmic/power command to:
pmic charger on/off to recharge battery when needed.
I think, that above is a better solution.
I know the value of showing proof of concept type examples in development boards but that should still be an opt-in thing I would think.
Yes, indeed this shall be regarded as a proof-of-concept to show that battery is automatically charged when needed.
Instead, I think, that it would be a good idea to write a warning on the u-boot starting (when PMIC driver is initialized):
e.g. WARNING: Battery needs charging (voltage: 3.5V capacity: 8%)
And then user can on its own decide if he/she will charge the battery or not.
Regards, Lukasz Majewski

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 09/17/12 13:55, Lukasz Majewski wrote:
Hi Tom ,
If I read this right we'll pause in the middle of start up to charge the battery for possibly a long time right? And this could be a while loop even, yes? If so to the first one, this really should be under some sort of CONFIG option.
This is one of the options. Other option is to extend the pmic/power command to:
pmic charger on/off to recharge battery when needed.
I think, that above is a better solution.
I know the value of showing proof of concept type examples in development boards but that should still be an opt-in thing I would think.
Yes, indeed this shall be regarded as a proof-of-concept to show that battery is automatically charged when needed.
Instead, I think, that it would be a good idea to write a warning on the u-boot starting (when PMIC driver is initialized):
e.g. WARNING: Battery needs charging (voltage: 3.5V capacity: 8%)
And then user can on its own decide if he/she will charge the battery or not.
Yes, sounds good, thanks!
- -- Tom

When charging battery is necessary, the development board needs to be turned into low power mode for better efficiency.
Signed-off-by: Lukasz Majewski l.majewski@samsung.com Signed-off-by: Kyungmin Park kyungmin.park@samsung.com Cc: Minkyu Kang mk7.kang@samsung.com --- board/samsung/trats/trats.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 43 insertions(+), 0 deletions(-)
diff --git a/board/samsung/trats/trats.c b/board/samsung/trats/trats.c index 21050c8..f213d72 100644 --- a/board/samsung/trats/trats.c +++ b/board/samsung/trats/trats.c @@ -89,6 +89,48 @@ void i2c_init_board(void) s5p_gpio_direction_output(&gpio2->y4, 1, 1); }
+static void trats_low_power_mode(void) +{ + struct exynos4_clock *clk = + (struct exynos4_clock *)samsung_get_base_clock(); + struct exynos4_power *pwr = + (struct exynos4_power *)samsung_get_base_power(); + + /* Power down CORE1 */ + writel(0x0, &pwr->arm_core1_configuration); + + /* Change the APLL frequency */ + writel(0xa0c80604, &clk->apll_con0); + /* Change CPU0 clock divider */ + writel(0x00000100, &clk->div_cpu0); + /* CLK_DIV_STAT_CPU0 - wait until clock gets stable */ + while (readl(&clk->div_stat_cpu0) & 0x1111111) + ; + + /* Change clock divider ratio for DMC */ + writel(0x13113117, &clk->div_dmc0); + while (readl(&clk->div_stat_dmc0) & 0x11111111) + ; + + /* Turn off unnecessary power domains */ + writel(0x0, &pwr->xxti_configuration); /* XXTI */ + writel(0x0, &pwr->cam_configuration); /* CAM */ + writel(0x0, &pwr->tv_configuration); /* TV */ + writel(0x0, &pwr->mfc_configuration); /* MFC */ + writel(0x0, &pwr->g3d_configuration); /* G3D */ + writel(0x0, &pwr->gps_configuration); /* GPS */ + writel(0x0, &pwr->gps_alive_configuration); /* GPS_ALIVE */ + + /* Turn off unnecessary clocks */ + writel(0x0, &clk->gate_ip_cam); /* CAM */ + writel(0x0, &clk->gate_ip_tv); /* TV */ + writel(0x0, &clk->gate_ip_mfc); /* MFC */ + writel(0x0, &clk->gate_ip_g3d); /* G3D */ + writel(0x0, &clk->gate_ip_image); /* IMAGE */ + writel(0x0, &clk->gate_ip_gps); /* GPS */ +} + + static int pmic_init_max8997(void) { struct pmic *p = pmic_get("MAX8997_PMIC"); @@ -262,6 +304,7 @@ int power_board_init(void) power_check_battery(&bat);
if (bat.state == CHARGE && chrg == CHARGER_USB) { + trats_low_power_mode(); puts("CHARGER ENABLE\n"); if (pmic_charger_state(CHARGER_ENABLE, 450)) return -1;

On Fri, Sep 14, 2012 at 05:39:59PM +0200, Lukasz Majewski wrote:
PMIC framework has been redesigned to support multiple instances of power related devices (e.g. fuel gauge, PMICs, chargers, micro USB IC).
Due to that, code at other architectures and boards have been adjusted properly.
New power_board_init() method at ./lib/board.c file has been introduced. It is meant to be an architecture dependent function to support advanced power management. Since PMIC framework uses lists internally to link different devices, its initialization must be done just after malloc initialization.
The framework itself looks OK, but I'd appreciate others reviewing as well.

Hi Tom,
On Fri, Sep 14, 2012 at 05:39:59PM +0200, Lukasz Majewski wrote:
PMIC framework has been redesigned to support multiple instances of power related devices (e.g. fuel gauge, PMICs, chargers, micro USB IC).
Due to that, code at other architectures and boards have been adjusted properly.
New power_board_init() method at ./lib/board.c file has been introduced. It is meant to be an architecture dependent function to support advanced power management. Since PMIC framework uses lists internally to link different devices, its initialization must be done just after malloc initialization.
The framework itself looks OK, but I'd appreciate others reviewing as well.
That was my intention to start a discussion. I'm welcome for other opinions.
Regards, Lukasz
participants (4)
-
Lukasz Majewski
-
Lukasz Majewski
-
Stefano Babic
-
Tom Rini