Googler | 25e92cf | 2023-12-13 10:05:01 +0000 | [diff] [blame^] | 1 | // SPDX-License-Identifier: (GPL-2.0+ OR MIT) |
| 2 | /* |
| 3 | * Copyright (c) 2019 Amlogic, Inc. All rights reserved. |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <malloc.h> |
| 8 | #include <asm/arch/gpio.h> |
| 9 | #include <fdtdec.h> |
| 10 | #include <i2c.h> |
| 11 | #include <dm.h> |
| 12 | #include <amlogic/media/vout/lcd/aml_lcd.h> |
| 13 | #include <amlogic/media/vout/lcd/lcd_i2c_dev.h> |
| 14 | #include "lcd_common.h" |
| 15 | |
| 16 | #define LCDI2C_PR(fmt, args...) printf("lcd_i2c: "fmt"", ## args) |
| 17 | #define LCDI2C_ERR(fmt, args...) printf("lcd_i2c: error: "fmt"", ## args) |
| 18 | |
| 19 | struct lcd_i2c_match_s { |
| 20 | unsigned char bus_id; |
| 21 | char *bus_str; |
| 22 | }; |
| 23 | |
| 24 | static struct lcd_i2c_match_s lcd_i2c_match_table[] = { |
| 25 | {LCD_EXT_I2C_BUS_0, "i2c_0/a"}, |
| 26 | {LCD_EXT_I2C_BUS_1, "i2c_1/b"}, |
| 27 | {LCD_EXT_I2C_BUS_2, "i2c_2/c"}, |
| 28 | {LCD_EXT_I2C_BUS_3, "i2c_3/d"}, |
| 29 | {LCD_EXT_I2C_BUS_4, "i2c_4/ao"}, |
| 30 | {LCD_EXT_I2C_BUS_MAX, "i2c_invalid"}, |
| 31 | }; |
| 32 | |
| 33 | void aml_lcd_i2c_bus_print(unsigned char i2c_bus) |
| 34 | { |
| 35 | int i, temp = ARRAY_SIZE(lcd_i2c_match_table) - 1; |
| 36 | |
| 37 | for (i = 0; i < ARRAY_SIZE(lcd_i2c_match_table); i++) { |
| 38 | if (lcd_i2c_match_table[i].bus_id == i2c_bus) { |
| 39 | temp = i; |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | LCDI2C_PR("i2c_bus = %s(%d)\n", lcd_i2c_match_table[temp].bus_str, temp); |
| 45 | } |
| 46 | |
| 47 | int aml_lcd_i2c_write(unsigned char i2c_bus, unsigned int i2c_addr, |
| 48 | unsigned char *buff, unsigned int len) |
| 49 | { |
| 50 | unsigned char aml_i2c_bus; |
| 51 | struct udevice *i2c_dev; |
| 52 | int i, ret = 0; |
| 53 | unsigned char data = 0; |
| 54 | |
| 55 | aml_i2c_bus = i2c_bus; |
| 56 | ret = i2c_get_chip_for_busnum(aml_i2c_bus, i2c_addr, 1, &i2c_dev); |
| 57 | if (ret) { |
| 58 | LCDI2C_ERR("no sys aml_i2c_bus %d find\n", aml_i2c_bus); |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | if (lcd_debug_print_flag & LCD_DBG_PR_NORMAL) { |
| 63 | printf("%s:", __func__); |
| 64 | for (i = 0; i < len; i++) |
| 65 | printf(" 0x%02x", buff[i]); |
| 66 | printf(" [addr 0x%02x]\n", i2c_addr); |
| 67 | } |
| 68 | |
| 69 | if (len < 1) { |
| 70 | LCDI2C_ERR("invailed len %d\n", len); |
| 71 | return -1; |
| 72 | } |
| 73 | if (len == 1) |
| 74 | ret = dm_i2c_write(i2c_dev, buff[0], &data, len); |
| 75 | else if (len > 1) |
| 76 | ret = dm_i2c_write(i2c_dev, buff[0], &buff[1], len - 1); |
| 77 | |
| 78 | if (ret) { |
| 79 | LCDI2C_ERR("i2c write failed [addr 0x%02x]\n", i2c_addr); |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | int aml_lcd_i2c_read(unsigned char i2c_bus, unsigned int i2c_addr, |
| 87 | unsigned char *buff, unsigned int len) |
| 88 | { |
| 89 | unsigned char aml_i2c_bus; |
| 90 | struct udevice *i2c_dev; |
| 91 | int ret = 0, i; |
| 92 | |
| 93 | aml_i2c_bus = i2c_bus; |
| 94 | ret = i2c_get_chip_for_busnum(aml_i2c_bus, i2c_addr, 1, &i2c_dev); |
| 95 | if (ret) { |
| 96 | LCDI2C_ERR("no sys aml_i2c_bus %d find\n", aml_i2c_bus); |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | #if 0 |
| 101 | ret = i2c_write(i2c_dev, buff[0], &buff[1], 1); |
| 102 | if (ret) { |
| 103 | LCDI2C_ERR("i2c write failed [addr 0x%02x]\n", i2c_addr); |
| 104 | return ret; |
| 105 | } |
| 106 | #endif |
| 107 | if (len < 1) { |
| 108 | LCDI2C_ERR("invalied len %d\n", len); |
| 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | ret = dm_i2c_read(i2c_dev, buff[0], buff, len); |
| 113 | if (ret) { |
| 114 | LCDI2C_ERR("i2c read failed [addr 0x%02x]\n", i2c_addr); |
| 115 | return ret; |
| 116 | } |
| 117 | if (lcd_debug_print_flag & LCD_DBG_PR_NORMAL) { |
| 118 | printf("%s:", __func__); |
| 119 | for (i = 0; i < len; i++) |
| 120 | printf(" 0x%02x", buff[i]); |
| 121 | printf(" [addr 0x%02x]\n", i2c_addr); |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |