blob: 73a70bcc82ba4c84451d7fd8641a4ac74ca2b6a0 [file] [log] [blame]
/*
* drivers/amlogic/media/vout/lcd/bl_extern/bl_extern_i2c_dev.c
*
* Copyright (C) 2015 Amlogic, Inc. All rights reserved.
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <common.h>
#include <malloc.h>
#include <asm/arch/gpio.h>
#include <fdtdec.h>
#include <i2c.h>
#include <dm.h>
#include <amlogic/media/vout/lcd/aml_lcd.h>
#include <amlogic/media/vout/lcd/bl_extern.h>
#include "bl_extern.h"
#include "../lcd_common.h"
struct aml_bl_extern_i2c_match_s {
unsigned char bus_id;
unsigned char bus_sys;
char *bus_str;
};
static struct aml_bl_extern_i2c_match_s bl_extern_i2c_match_table[] = {
{BL_EXTERN_I2C_BUS_0, LCD_EXT_I2C_BUS_0, "i2c_0/a"},
{BL_EXTERN_I2C_BUS_1, LCD_EXT_I2C_BUS_1, "i2c_1/b"},
{BL_EXTERN_I2C_BUS_2, LCD_EXT_I2C_BUS_2, "i2c_2/c"},
{BL_EXTERN_I2C_BUS_3, LCD_EXT_I2C_BUS_3, "i2c_3/d"},
{BL_EXTERN_I2C_BUS_4, LCD_EXT_I2C_BUS_4, "i2c_4/ao"},
{BL_EXTERN_I2C_BUS_MAX, LCD_EXT_I2C_BUS_MAX, "i2c_invalid"},
};
void bl_extern_i2c_bus_print(unsigned char i2c_bus)
{
int i, temp = ARRAY_SIZE(bl_extern_i2c_match_table) - 1;
for (i = 0; i < ARRAY_SIZE(bl_extern_i2c_match_table); i++) {
if (bl_extern_i2c_match_table[i].bus_id == i2c_bus) {
temp = i;
break;
}
}
BLEXT("i2c_bus = %s(%d)\n",
bl_extern_i2c_match_table[temp].bus_str, temp);
}
static unsigned char bl_extern_i2c_bus_get_sys(unsigned char i2c_bus)
{
int i, ret = LCD_EXT_I2C_BUS_MAX;
for (i = 0; i < ARRAY_SIZE(bl_extern_i2c_match_table); i++) {
if (bl_extern_i2c_match_table[i].bus_id == i2c_bus) {
ret = bl_extern_i2c_match_table[i].bus_sys;
break;
}
}
if (lcd_debug_print_flag)
BLEXT("%s: %d->%d\n", __func__, i2c_bus, ret);
return ret;
}
int bl_extern_i2c_write(unsigned char i2c_bus, unsigned i2c_addr,
unsigned char *buff, unsigned len)
{
unsigned char sys_i2c_bus;
struct udevice *i2c_dev;
int i, ret = 0;
sys_i2c_bus = bl_extern_i2c_bus_get_sys(i2c_bus);
ret = i2c_get_chip_for_busnum(sys_i2c_bus, i2c_addr, 1, &i2c_dev);
if (ret) {
BLEXTERR("no sys_i2c_bus %d find\n", sys_i2c_bus);
return ret;
}
if (lcd_debug_print_flag) {
printf("%s:", __func__);
for (i = 0; i < len; i++)
printf(" 0x%02x", buff[i]);
printf(" [addr 0x%02x]\n", i2c_addr);
}
ret = dm_i2c_write(i2c_dev, i2c_addr, buff, len);
if (ret) {
BLEXTERR("i2c write failed [addr 0x%02x]\n", i2c_addr);
return ret;
}
return 0;
}
int bl_extern_i2c_read(unsigned char i2c_bus, unsigned i2c_addr,
unsigned char *buff, unsigned int len)
{
unsigned char sys_i2c_bus;
struct udevice *i2c_dev;
int ret = 0;
sys_i2c_bus = bl_extern_i2c_bus_get_sys(i2c_bus);
ret = i2c_get_chip_for_busnum(sys_i2c_bus, i2c_addr, 1, &i2c_dev);
if (ret) {
BLEXTERR("no sys_i2c_bus %d find\n", sys_i2c_bus);
return ret;
}
ret = dm_i2c_write(i2c_dev, i2c_addr, buff, 1);
if (ret) {
BLEXTERR("i2c write failed [addr 0x%02x]\n", i2c_addr);
return ret;
}
ret = dm_i2c_read(i2c_dev, i2c_addr, buff, len);
if (ret) {
BLEXTERR("i2c read failed [addr 0x%02x]\n", i2c_addr);
return ret;
}
return 0;
}