93 lines
1.5 KiB
C
93 lines
1.5 KiB
C
/*
|
|
* da9063.c
|
|
*
|
|
* Dialog DA9063 PMIC
|
|
*
|
|
* Copyright (C) 2018 NetModule AG - http://www.netmodule.com/
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
*/
|
|
|
|
#include <common.h>
|
|
#include <errno.h>
|
|
#include <i2c.h>
|
|
|
|
#include "da9063.h"
|
|
|
|
|
|
static int da9063_i2c_bus = 0;
|
|
|
|
|
|
void da9063_init(int i2c_bus)
|
|
{
|
|
da9063_i2c_bus = i2c_bus;
|
|
}
|
|
|
|
int da9063_get_reg(int reg, u8* val)
|
|
{
|
|
int ret;
|
|
int old_bus;
|
|
u8 temp;
|
|
|
|
/* TODO: Check whether switching is required */
|
|
old_bus = i2c_get_bus_num();
|
|
i2c_set_bus_num(da9063_i2c_bus);
|
|
|
|
/* TODO: Use CONFIG_PMIC_I2C_ADDR+1 if reg > 0xFF */
|
|
*val = 0;
|
|
ret = i2c_read(CONFIG_PMIC_I2C_ADDR, reg, 1, &temp, 1);
|
|
if (ret == 0)
|
|
*val = temp;
|
|
|
|
i2c_set_bus_num(old_bus);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int da9063_set_reg(int reg, u8 val)
|
|
{
|
|
int ret;
|
|
int old_bus;
|
|
|
|
/* TODO: Check whether switching is required */
|
|
old_bus = i2c_get_bus_num();
|
|
i2c_set_bus_num(da9063_i2c_bus);
|
|
|
|
/* TODO: Use CONFIG_PMIC_I2C_ADDR+1 if reg > 0xFF */
|
|
ret = i2c_write(CONFIG_PMIC_I2C_ADDR, reg, 1, &val, 1);
|
|
if (ret != 0)
|
|
puts("da9063 write error\n");
|
|
|
|
i2c_set_bus_num(old_bus);
|
|
|
|
return ret;
|
|
}
|
|
|
|
void da9063_set_gpio(unsigned bit, int state)
|
|
{
|
|
int pmic_reg;
|
|
int ret;
|
|
u8 bitmask;
|
|
u8 reg = 0x00;
|
|
|
|
if (bit <= 7) {
|
|
pmic_reg = PMIC_REG_GPIO_MODE0_7;
|
|
bitmask = 1U << (bit-0);
|
|
}
|
|
else {
|
|
pmic_reg = PMIC_REG_GPIO_MODE8_15;
|
|
bitmask = 1U << (bit-8);
|
|
}
|
|
|
|
/* printf("da9063_set_gpio %d 0x%04x\n", pmic_reg, bitmask); */
|
|
ret = da9063_get_reg(pmic_reg, ®);
|
|
|
|
if (ret == 0) {
|
|
if (state) reg |= bitmask;
|
|
else reg &= ~bitmask;
|
|
|
|
(void)da9063_set_reg(pmic_reg, reg);
|
|
}
|
|
}
|
|
|