2011-10-06 02:37:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Samsung Electronics
|
|
|
|
* Lukasz Majewski <l.majewski@samsung.com>
|
|
|
|
*
|
|
|
|
* (C) Copyright 2010
|
|
|
|
* Stefano Babic, DENX Software Engineering, sbabic@denx.de
|
|
|
|
*
|
|
|
|
* (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2011-10-06 02:37:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <linux/types.h>
|
2012-11-13 03:21:55 +00:00
|
|
|
#include <power/pmic.h>
|
2011-10-06 02:37:34 +00:00
|
|
|
#include <i2c.h>
|
2014-11-26 07:00:58 +00:00
|
|
|
#include <linux/compiler.h>
|
2011-10-06 02:37:34 +00:00
|
|
|
|
|
|
|
int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
|
|
|
|
{
|
|
|
|
unsigned char buf[4] = { 0 };
|
|
|
|
|
2012-11-13 03:21:55 +00:00
|
|
|
if (check_reg(p, reg))
|
2016-12-15 11:49:50 +00:00
|
|
|
return -EINVAL;
|
2011-10-06 02:37:34 +00:00
|
|
|
|
2014-05-20 12:01:38 +00:00
|
|
|
I2C_SET_BUS(p->bus);
|
|
|
|
|
2011-10-06 02:37:34 +00:00
|
|
|
switch (pmic_i2c_tx_num) {
|
|
|
|
case 3:
|
2012-11-13 03:21:53 +00:00
|
|
|
if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
|
|
|
|
buf[2] = (cpu_to_le32(val) >> 16) & 0xff;
|
|
|
|
buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
|
|
|
|
buf[0] = cpu_to_le32(val) & 0xff;
|
|
|
|
} else {
|
|
|
|
buf[0] = (cpu_to_le32(val) >> 16) & 0xff;
|
|
|
|
buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
|
|
|
|
buf[2] = cpu_to_le32(val) & 0xff;
|
|
|
|
}
|
2011-10-06 02:37:34 +00:00
|
|
|
break;
|
2012-11-13 03:21:52 +00:00
|
|
|
case 2:
|
2012-11-13 03:21:53 +00:00
|
|
|
if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
|
|
|
|
buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
|
|
|
|
buf[0] = cpu_to_le32(val) & 0xff;
|
|
|
|
} else {
|
|
|
|
buf[0] = (cpu_to_le32(val) >> 8) & 0xff;
|
|
|
|
buf[1] = cpu_to_le32(val) & 0xff;
|
|
|
|
}
|
2012-11-13 03:21:52 +00:00
|
|
|
break;
|
2011-10-06 02:37:34 +00:00
|
|
|
case 1:
|
2012-11-13 03:21:53 +00:00
|
|
|
buf[0] = cpu_to_le32(val) & 0xff;
|
2011-10-06 02:37:34 +00:00
|
|
|
break;
|
2012-03-16 11:32:09 +00:00
|
|
|
default:
|
|
|
|
printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
|
2016-12-15 11:49:50 +00:00
|
|
|
return -EINVAL;
|
2011-10-06 02:37:34 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 11:49:50 +00:00
|
|
|
return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
|
2011-10-06 02:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
|
|
|
|
{
|
|
|
|
unsigned char buf[4] = { 0 };
|
|
|
|
u32 ret_val = 0;
|
2016-12-15 11:49:50 +00:00
|
|
|
int ret;
|
2011-10-06 02:37:34 +00:00
|
|
|
|
2012-11-13 03:21:55 +00:00
|
|
|
if (check_reg(p, reg))
|
2016-12-15 11:49:50 +00:00
|
|
|
return -EINVAL;
|
2011-10-06 02:37:34 +00:00
|
|
|
|
2014-05-20 12:01:38 +00:00
|
|
|
I2C_SET_BUS(p->bus);
|
|
|
|
|
2016-12-15 11:49:50 +00:00
|
|
|
ret = i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2011-10-06 02:37:34 +00:00
|
|
|
|
|
|
|
switch (pmic_i2c_tx_num) {
|
|
|
|
case 3:
|
2012-11-13 03:21:53 +00:00
|
|
|
if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
|
|
|
|
ret_val = le32_to_cpu(buf[2] << 16
|
|
|
|
| buf[1] << 8 | buf[0]);
|
|
|
|
else
|
|
|
|
ret_val = le32_to_cpu(buf[0] << 16 |
|
|
|
|
buf[1] << 8 | buf[2]);
|
2011-10-06 02:37:34 +00:00
|
|
|
break;
|
2012-11-13 03:21:52 +00:00
|
|
|
case 2:
|
2012-11-13 03:21:53 +00:00
|
|
|
if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
|
|
|
|
ret_val = le32_to_cpu(buf[1] << 8 | buf[0]);
|
|
|
|
else
|
|
|
|
ret_val = le32_to_cpu(buf[0] << 8 | buf[1]);
|
2012-11-13 03:21:52 +00:00
|
|
|
break;
|
2011-10-06 02:37:34 +00:00
|
|
|
case 1:
|
2012-11-13 03:21:53 +00:00
|
|
|
ret_val = le32_to_cpu(buf[0]);
|
2011-10-06 02:37:34 +00:00
|
|
|
break;
|
2012-03-16 11:32:09 +00:00
|
|
|
default:
|
|
|
|
printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
|
2016-12-15 11:49:50 +00:00
|
|
|
return -EINVAL;
|
2011-10-06 02:37:34 +00:00
|
|
|
}
|
|
|
|
memcpy(val, &ret_val, sizeof(ret_val));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pmic_probe(struct pmic *p)
|
|
|
|
{
|
2013-08-16 13:35:26 +00:00
|
|
|
i2c_set_bus_num(p->bus);
|
2012-11-13 03:21:52 +00:00
|
|
|
debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
|
2011-10-06 02:37:34 +00:00
|
|
|
if (i2c_probe(pmic_i2c_addr)) {
|
|
|
|
printf("Can't find PMIC:%s\n", p->name);
|
2016-12-15 11:49:50 +00:00
|
|
|
return -ENODEV;
|
2011-10-06 02:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|