lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <common.h> |
| 2 | #include <command.h> |
| 3 | #include <net.h> |
| 4 | #include <jffs2/load_kernel.h> |
| 5 | #include <common.h> |
| 6 | #include <asm/arch/hardware.h> |
| 7 | #include <i2c.h> |
| 8 | #include <asm/arch/uart.h> |
| 9 | #include <asm/arch/lsp_crpm.h> |
| 10 | #include "errno.h" |
| 11 | |
| 12 | //#if CONFIG_HARDWARE_TEST |
| 13 | #if 0 |
| 14 | |
| 15 | #define TPS65912_I2C_SLAVE_ADDR0 (0x2D) |
| 16 | |
| 17 | #define TPS65912_REG_ADDR_GPIO3 0x43 |
| 18 | #define TPS65912_REG_ADDR_GPIO4 0x44 |
| 19 | #define TPS65912_REG_ADDR_GPIO5 0x45 |
| 20 | #define GPIO_ODEN_LSH 5 |
| 21 | #define GPIO_CFG_LSH 2 |
| 22 | #define GPIO_LED_ONOFF_LSH 0 |
| 23 | #define GPIO_BIT_SET 1 |
| 24 | |
| 25 | #if 1 |
| 26 | #define TPS65912_BITFVAL(var, lsh) ( (var) << (lsh) ) |
| 27 | #define TPS65912_BITFMASK(wid, lsh) ( ((1U << (wid)) - 1) << (lsh) ) |
| 28 | #define TPS65912_BITFEXT(var, wid, lsh) ((var & TPS65912_BITFMASK(wid, lsh)) >> (lsh)) |
| 29 | |
| 30 | #define PMIC_ALL_BITS 0xFF |
| 31 | #endif |
| 32 | |
| 33 | int zDrvPmic_SetRegister(uchar slv_addr, uchar reg_addr, uchar data, uchar mask) |
| 34 | { |
| 35 | int ret = 0; |
| 36 | uchar content =0; |
| 37 | |
| 38 | // If it is not updating the whole register (8 bits), we need to read |
| 39 | // the register first. Then we set the bits and write it back. |
| 40 | if ((mask & PMIC_ALL_BITS) ^ PMIC_ALL_BITS) /* 0xFF=0b 1111 1111, mask xx11xxx , */ |
| 41 | { |
| 42 | ret = i2c_read(0, slv_addr, reg_addr, 8, &content, 1); |
| 43 | if (ret != 0) |
| 44 | { |
| 45 | return ret; |
| 46 | } |
| 47 | content &= ~mask; |
| 48 | content |= data & mask; |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | content = data; |
| 53 | } |
| 54 | |
| 55 | ret = i2c_write(0, slv_addr, reg_addr, 8, &content, 1); |
| 56 | if (ret != 0) |
| 57 | { |
| 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | int tcled_init(void) |
| 66 | { |
| 67 | int ret = 0; |
| 68 | uchar slv_addr=0, reg_addr=0, reg_val=0, mask=0; |
| 69 | |
| 70 | #if 1 /* set led as GPIO */ |
| 71 | /* GPIO_3 */ |
| 72 | /* ODEN - 1 */ |
| 73 | slv_addr = TPS65912_I2C_SLAVE_ADDR0; |
| 74 | reg_addr = TPS65912_REG_ADDR_GPIO3; |
| 75 | |
| 76 | reg_val = TPS65912_BITFVAL(1, GPIO_ODEN_LSH ); /* µÚÒ»¸ö²ÎÊý val = 1;¸ÃλÖÃ1 */ |
| 77 | mask = TPS65912_BITFMASK(GPIO_BIT_SET, GPIO_ODEN_LSH); |
| 78 | ret = zDrvPmic_SetRegister(slv_addr, reg_addr, reg_val, mask); |
| 79 | /* CFG - 1 */ |
| 80 | reg_val = TPS65912_BITFVAL(1, GPIO_CFG_LSH); |
| 81 | mask = TPS65912_BITFMASK(GPIO_BIT_SET, GPIO_CFG_LSH); |
| 82 | ret = zDrvPmic_SetRegister(slv_addr, reg_addr, reg_val, mask); |
| 83 | |
| 84 | |
| 85 | /* GPIO_4 */ |
| 86 | /* ODEN - 1 */ |
| 87 | slv_addr = TPS65912_I2C_SLAVE_ADDR0; |
| 88 | reg_addr = TPS65912_REG_ADDR_GPIO4; |
| 89 | |
| 90 | reg_val = TPS65912_BITFVAL(1, GPIO_ODEN_LSH ); |
| 91 | mask = TPS65912_BITFMASK(GPIO_BIT_SET, GPIO_ODEN_LSH); |
| 92 | ret = zDrvPmic_SetRegister(slv_addr, reg_addr, reg_val, mask); |
| 93 | /* CFG - 1 */ |
| 94 | reg_val = TPS65912_BITFVAL(1, GPIO_CFG_LSH ); |
| 95 | mask = TPS65912_BITFMASK(GPIO_BIT_SET, GPIO_CFG_LSH); |
| 96 | ret = zDrvPmic_SetRegister(slv_addr, reg_addr, reg_val, mask); |
| 97 | |
| 98 | |
| 99 | /* GPIO_5 */ |
| 100 | /* ODEN - 1 */ |
| 101 | slv_addr = TPS65912_I2C_SLAVE_ADDR0; |
| 102 | reg_addr = TPS65912_REG_ADDR_GPIO5; |
| 103 | |
| 104 | reg_val = TPS65912_BITFVAL(1, GPIO_ODEN_LSH ); |
| 105 | mask = TPS65912_BITFMASK(GPIO_BIT_SET, GPIO_ODEN_LSH); |
| 106 | ret = zDrvPmic_SetRegister(slv_addr, reg_addr, reg_val, mask); |
| 107 | /* CFG - 1 */ |
| 108 | reg_val = TPS65912_BITFVAL(1, GPIO_CFG_LSH); |
| 109 | mask = TPS65912_BITFMASK(GPIO_BIT_SET, GPIO_CFG_LSH); |
| 110 | ret = zDrvPmic_SetRegister(slv_addr, reg_addr, reg_val, mask); |
| 111 | #endif |
| 112 | |
| 113 | /* set the leds initial status */ |
| 114 | /* CLOSE LEDA - RED */ |
| 115 | slv_addr = TPS65912_I2C_SLAVE_ADDR0; |
| 116 | reg_addr = TPS65912_REG_ADDR_GPIO3; |
| 117 | reg_val = TPS65912_BITFVAL(1, GPIO_LED_ONOFF_LSH); |
| 118 | mask = TPS65912_BITFMASK(GPIO_BIT_SET, GPIO_LED_ONOFF_LSH); |
| 119 | ret = zDrvPmic_SetRegister(slv_addr, reg_addr, reg_val, mask); |
| 120 | |
| 121 | /* CLOSE LEDB - GREEN */ |
| 122 | slv_addr = TPS65912_I2C_SLAVE_ADDR0; |
| 123 | reg_addr = TPS65912_REG_ADDR_GPIO4; |
| 124 | reg_val = TPS65912_BITFVAL(1, GPIO_LED_ONOFF_LSH); |
| 125 | mask = TPS65912_BITFMASK(GPIO_BIT_SET, GPIO_LED_ONOFF_LSH); |
| 126 | ret = zDrvPmic_SetRegister(slv_addr, reg_addr, reg_val, mask); |
| 127 | |
| 128 | /* CLOSE LEDC - BLUE */ |
| 129 | slv_addr = TPS65912_I2C_SLAVE_ADDR0; |
| 130 | reg_addr = TPS65912_REG_ADDR_GPIO5; |
| 131 | reg_val = TPS65912_BITFVAL(1, GPIO_LED_ONOFF_LSH); |
| 132 | mask = TPS65912_BITFMASK(GPIO_BIT_SET, GPIO_LED_ONOFF_LSH); |
| 133 | ret = zDrvPmic_SetRegister(slv_addr, reg_addr, reg_val, mask); |
| 134 | |
| 135 | return ret; |
| 136 | |
| 137 | } |
| 138 | |
| 139 | int tcled_on(int led_red, int led_green, int led_blue) |
| 140 | { |
| 141 | int ret = 0; |
| 142 | uchar slv_addr=0, reg_addr=0, reg_val=0, mask=0; |
| 143 | uchar statusA = 0; |
| 144 | uchar statusB = 0; |
| 145 | uchar statusC = 0; |
| 146 | |
| 147 | slv_addr = TPS65912_I2C_SLAVE_ADDR0; |
| 148 | |
| 149 | statusA = led_red ? 0 : 1; /* led_red=1,±íʾ¿ª£¬ÔòGPIOÉèÖÃΪ0 */ |
| 150 | statusB = led_green ? 0 : 1; |
| 151 | statusC = led_blue ? 0 : 1; |
| 152 | |
| 153 | /* LEDA µÄ²Ù×÷ */ |
| 154 | slv_addr = TPS65912_I2C_SLAVE_ADDR0; |
| 155 | reg_addr = TPS65912_REG_ADDR_GPIO3; |
| 156 | reg_val = TPS65912_BITFVAL(statusA, 0); |
| 157 | mask = TPS65912_BITFMASK(1, 0); |
| 158 | ret = zDrvPmic_SetRegister(slv_addr, reg_addr, reg_val, mask); |
| 159 | |
| 160 | /* LEDB µÄ²Ù×÷ */ |
| 161 | slv_addr = TPS65912_I2C_SLAVE_ADDR0; |
| 162 | reg_addr = TPS65912_REG_ADDR_GPIO4; |
| 163 | reg_val = TPS65912_BITFVAL(statusB, 0); /* statusB=0:¿ª£¬ =1,¹Ø */ |
| 164 | mask = TPS65912_BITFMASK(1, 0); |
| 165 | ret = zDrvPmic_SetRegister(slv_addr, reg_addr, reg_val, mask); |
| 166 | |
| 167 | /* LEDC µÄ²Ù×÷ */ |
| 168 | slv_addr = TPS65912_I2C_SLAVE_ADDR0; |
| 169 | reg_addr = TPS65912_REG_ADDR_GPIO5; |
| 170 | reg_val = TPS65912_BITFVAL(statusB, 0); /* statusB=0:¿ª£¬ =1,¹Ø */ |
| 171 | mask = TPS65912_BITFMASK(1, 0); |
| 172 | ret = zDrvPmic_SetRegister(slv_addr, reg_addr, reg_val, mask); |
| 173 | } |
| 174 | |
| 175 | void leds_on(void) |
| 176 | { |
| 177 | tcled_on(1, 0, 0); |
| 178 | tcled_on(0, 1, 0); |
| 179 | tcled_on(1, 0, 1); |
| 180 | udelay(1000000); |
| 181 | tcled_on(1, 1, 0); |
| 182 | tcled_on(1, 0, 1); |
| 183 | tcled_on(0, 1, 1); |
| 184 | udelay(1000000); |
| 185 | tcled_on(1, 1, 1); |
| 186 | tcled_on(0, 0, 0); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | int do_test_tcled(void) |
| 191 | { |
| 192 | tcled_init(); |
| 193 | |
| 194 | uint8_t i = 10; |
| 195 | while( i-- ) |
| 196 | { |
| 197 | leds_on(); |
| 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | U_BOOT_CMD( |
| 203 | test_tcled, CONFIG_SYS_MAXARGS, 0, do_test_tcled, |
| 204 | "test_TCled: test_tcled ", |
| 205 | "" |
| 206 | ); |
| 207 | |
| 208 | #endif |
| 209 | |
| 210 | |