1 /* 2 * (C) Copyright 2020 Rockchip Electronics Co., Ltd 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <dm.h> 9 #include <i2c.h> 10 #include <asm/gpio.h> 11 #include "rk_ebc.h" 12 13 #define msleep(a) udelay((a) * 1000) 14 15 struct tps65185_priv_data { 16 struct udevice *dev; 17 struct gpio_desc pwr_up_gpio; 18 struct gpio_desc pwr_en_gpio; 19 struct gpio_desc vcom_gpio; 20 struct gpio_desc wake_up_gpio; 21 u8 rev_id; 22 u8 vadj; 23 u8 vcom1; 24 u8 vcom2; 25 u8 upseq0; 26 u8 upseq1; 27 u8 dwnseq0; 28 u8 dwnseq1; 29 u8 shadow_en; 30 }; 31 32 #define REG_TMST_VALUE 0x00 33 #define REG_ENABLE 0x01 34 #define REG_VADJ 0x02 35 #define REG_VCOM1_ADJUST 0x03 36 #define REG_VCOM2_ADJUST 0x04 37 #define REG_INT_ENABLE1 0x05 38 #define REG_INT_ENABLE2 0x06 39 #define REG_INT_STATUS1 0x07 40 #define REG_INT_STATUS2 0x08 41 #define REG_UPSEQ0 0x09 42 #define REG_UPSEQ1 0x0a 43 #define REG_DWNSEQ0 0x0b 44 #define REG_DWNSEQ1 0x0c 45 #define REG_TMST1 0x0d 46 #define REG_TMST2 0x0e 47 #define REG_PG_STATUS 0x0f 48 #define REG_REVID 0x10 49 #define mv_to_vcom1_reg(mv) (((mv) / 10) & 0xff) 50 #define mv_to_vcom2_reg(mv) ((((mv) / 10) & 0x100) >> 8) 51 52 /* 53 * After waking up from sleep, Papyrus 54 * waits for VN to be discharged and all 55 * voltage ref to startup before loading 56 * the default EEPROM settings. So accessing 57 * registers too early after WAKEUP could 58 * cause the register to be overridden by 59 * default values 60 */ 61 #define PAPYRUS_EEPROM_DELAY_MS 50 62 /* 63 * Papyrus WAKEUP pin must stay low for 64 * a minimum time 65 */ 66 #define PAPYRUS_SLEEP_MINIMUM_MS 110 67 /* 68 * Temp sensor might take a little time to 69 * settle even though the status bit in TMST1 70 * state conversion is done - if read too early 71 * 0C will be returned instead of the right temp 72 */ 73 #define PAPYRUS_TEMP_READ_TIME_MS 10 74 75 /* 76 * Powerup sequence takes at least 24 ms 77 * - no need to poll too frequently 78 */ 79 #define HW_GET_STATE_INTERVAL_MS 24 80 81 #define SEQ_VDD(index) (((index) % 4) << 6) 82 #define SEQ_VPOS(index) (((index) % 4) << 4) 83 #define SEQ_VEE(index) (((index) % 4) << 2) 84 #define SEQ_VNEG(index) (((index) % 4) << 0) 85 86 /* power up seq delay time */ 87 #define UDLY_3ms(index) (0x00 << (((index) % 4) * 2)) 88 #define UDLY_6ms(index) (0x01 << (((index) % 4) * 2)) 89 #define UDLY_9ms(index) (0x10 << (((index) % 4) * 2)) 90 #define UDLY_12ms(index) (0x11 << (((index) % 4) * 2)) 91 92 /* power down seq delay time */ 93 #define DDLY_6ms(index) (0x00 << (((index) % 4) * 2)) 94 #define DDLY_12ms(index) (0x01 << (((index) % 4) * 2)) 95 #define DDLY_24ms(index) (0x10 << (((index) % 4) * 2)) 96 #define DDLY_48ms(index) (0x11 << (((index) % 4) * 2)) 97 98 #define NUMBER_PMIC_REGS 10 99 // INT_ENABLE1 100 #define PAPYRUS_INT_ENABLE1_ACQC_EN 1 101 #define PAPYRUS_INT_ENABLE1_PRGC_EN 0 102 103 // INT_STATUS1 104 #define PAPYRUS_INT_STATUS1_ACQC 1 105 #define PAPYRUS_INT_STATUS1_PRGC 0 106 107 // VCOM2_ADJUST 108 #define PAPYRUS_VCOM2_ACQ 7 109 #define PAPYRUS_VCOM2_PROG 6 110 #define PAPYRUS_VCOM2_HIZ 5 111 #define V3P3_EN_MASK 0x20 112 113 #define PAPYRUS_V3P3OFF_DELAY_MS 20//100 114 115 static struct udevice *pmic_dev; 116 117 int tps65185_i2c_write(struct tps65185_priv_data *priv_data, u8 reg, u8 val) 118 { 119 int ret; 120 u8 buf[2]; 121 struct i2c_msg msg; 122 struct dm_i2c_chip *chip = dev_get_parent_platdata(priv_data->dev); 123 124 buf[0] = reg; 125 buf[1] = val; 126 msg.addr = chip->chip_addr; 127 msg.flags = 0; 128 msg.len = 2; 129 msg.buf = buf; 130 131 ret = dm_i2c_xfer(priv_data->dev, &msg, 1); 132 if (ret) { 133 printf("tps65185 i2c write failed: %d\n", ret); 134 return ret; 135 } 136 137 return 0; 138 } 139 140 int tps65185_i2c_read(struct tps65185_priv_data *priv_data, u8 reg, u8 *val) 141 { 142 int ret; 143 u8 data; 144 struct dm_i2c_chip *chip = dev_get_parent_platdata(priv_data->dev); 145 struct i2c_msg msg[] = { 146 { 147 .addr = chip->chip_addr, 148 .flags = 0, 149 .buf = (u8 *)®, 150 .len = 1, 151 }, { 152 .addr = chip->chip_addr, 153 .flags = I2C_M_RD, 154 .buf = (u8 *)&data, 155 .len = 1, 156 } 157 }; 158 159 ret = dm_i2c_xfer(priv_data->dev, msg, 2); 160 if (ret) { 161 printf("tps65185 i2c read failed: %d\n", ret); 162 return ret; 163 } 164 165 *val = data; 166 167 return 0; 168 } 169 170 void tps65185_dump_registers(void) 171 { 172 u8 i, reg = 0; 173 struct tps65185_priv_data *priv_data = dev_get_priv(pmic_dev); 174 175 for (i = 0; i <= REG_REVID; i++) { 176 tps65185_i2c_read(priv_data, i, ®); 177 printf("0x%x\t", reg); 178 } 179 printf("\n"); 180 } 181 182 static int tps65185_read_vcom_value(struct tps65185_priv_data *priv_data, 183 u32 *vcom_read) 184 { 185 int ret; 186 u8 vcom_reg; 187 188 dm_gpio_set_value(&priv_data->wake_up_gpio, 0); 189 msleep(10); 190 dm_gpio_set_value(&priv_data->wake_up_gpio, 1); 191 msleep(10); 192 ret = tps65185_i2c_read(priv_data, REG_VCOM1_ADJUST, &vcom_reg); 193 if (ret) { 194 printf("read vcom failed: %d\n", ret); 195 return ret; 196 } 197 *vcom_read = vcom_reg; 198 ret = tps65185_i2c_read(priv_data, REG_VCOM2_ADJUST, &vcom_reg); 199 if (ret) { 200 printf("read vcom failed: %d\n", ret); 201 return ret; 202 } 203 *vcom_read += (vcom_reg & 0x1) << 8; 204 205 printf("read vcom value: %d\n", *vcom_read); 206 207 return 0; 208 } 209 210 static int tps65185_set_vcom_value(struct udevice *dev, u32 set_value) 211 { 212 int ret = 0; 213 u32 vcom_readback = 0; 214 u8 vcom1_val, vcom2_val, int_stat = 0; 215 struct tps65185_priv_data *priv_data = dev_get_priv(dev); 216 217 ret = tps65185_read_vcom_value(priv_data, &vcom_readback); 218 if (ret < 0) { 219 printf("tps65185 read vcom value failed\n"); 220 } else { 221 if (vcom_readback == set_value / 10) { 222 printf("Same as pmic default value, just return.\n"); 223 return 0; 224 } 225 } 226 227 vcom1_val = mv_to_vcom1_reg(set_value); 228 vcom2_val = mv_to_vcom2_reg(set_value); 229 230 dm_gpio_set_value(&priv_data->wake_up_gpio, 1); 231 msleep(20); 232 // Set vcom voltage 233 tps65185_i2c_write(priv_data, REG_VCOM1_ADJUST, vcom1_val); 234 tps65185_i2c_write(priv_data, REG_VCOM2_ADJUST, vcom2_val); 235 // PROGRAMMING 236 tps65185_i2c_write(priv_data, REG_VCOM2_ADJUST, 237 vcom2_val | (1 << PAPYRUS_VCOM2_PROG)); 238 do { 239 msleep(20); 240 ret = tps65185_i2c_read(priv_data, REG_INT_STATUS1, &int_stat); 241 if (ret) { 242 printf("read status1 failed: %d\n", ret); 243 break; 244 } 245 } while (!(int_stat & (1 << PAPYRUS_INT_STATUS1_PRGC))); 246 247 // VERIFICATION 248 tps65185_read_vcom_value(priv_data, &vcom_readback); 249 250 if (vcom_readback != set_value / 10) { 251 printf("vcom set failed, expect: %d, readback: %d\n", 252 set_value, vcom_readback); 253 return -1; 254 } 255 256 return 0; 257 } 258 259 static bool tps65185_hw_power_ack(struct tps65185_priv_data *priv_data, int up) 260 { 261 u8 pg_status; 262 int st, ret, retries_left = 10; 263 264 do { 265 ret = tps65185_i2c_read(priv_data, REG_PG_STATUS, &pg_status); 266 if (ret) 267 printf("read REG_PG_STATUS failed: %d\n", ret); 268 269 pg_status &= 0xfa; 270 if (pg_status == 0xfa && up == 1) { 271 st = 1; 272 } else if (pg_status == 0x00 && up == 0) { 273 st = 0; 274 } else { 275 st = -1; /* not settled yet */ 276 msleep(HW_GET_STATE_INTERVAL_MS); 277 } 278 retries_left--; 279 } while ((st == -1) && retries_left); 280 281 if ((st == -1) && !retries_left) 282 printf("power %s settle error (PG = %02x)\n", 283 up ? "up" : "down", pg_status); 284 285 return (st == up); 286 } 287 288 static int tps65185_power_on(struct udevice *dev) 289 { 290 struct tps65185_priv_data *priv_data = dev_get_priv(dev); 291 292 tps65185_i2c_write(priv_data, REG_VADJ, priv_data->vadj); 293 tps65185_i2c_write(priv_data, REG_UPSEQ0, priv_data->upseq0); 294 tps65185_i2c_write(priv_data, REG_UPSEQ1, priv_data->upseq1); 295 tps65185_i2c_write(priv_data, REG_DWNSEQ0, priv_data->dwnseq0); 296 tps65185_i2c_write(priv_data, REG_DWNSEQ1, priv_data->dwnseq1); 297 298 priv_data->shadow_en |= V3P3_EN_MASK; 299 tps65185_i2c_write(priv_data, REG_ENABLE, priv_data->shadow_en); 300 msleep(PAPYRUS_V3P3OFF_DELAY_MS); 301 priv_data->shadow_en = (0x80 | 0x30 | 0x0F); 302 tps65185_i2c_write(priv_data, REG_ENABLE, priv_data->shadow_en); 303 304 tps65185_hw_power_ack(priv_data, 1); 305 return 0; 306 } 307 308 static int tps65185_power_down(struct udevice *dev) 309 { 310 struct tps65185_priv_data *priv_data = dev_get_priv(dev); 311 312 priv_data->shadow_en = (0x40 | 0x20 | 0x0F); 313 tps65185_i2c_write(priv_data, REG_ENABLE, priv_data->shadow_en); 314 msleep(PAPYRUS_V3P3OFF_DELAY_MS); 315 priv_data->shadow_en &= ~V3P3_EN_MASK; 316 tps65185_i2c_write(priv_data, REG_ENABLE, priv_data->shadow_en); 317 318 tps65185_hw_power_ack(priv_data, 0); 319 320 return 0; 321 } 322 323 static int tps65185_temp_get(struct udevice *dev, u32 *temp) 324 { 325 int ret; 326 u8 read_val = 0; 327 struct tps65185_priv_data *priv_data = dev_get_priv(dev); 328 329 tps65185_i2c_write(priv_data, REG_TMST1, 0x80); 330 tps65185_i2c_write(priv_data, REG_TMST1, 0x80); 331 do { 332 int retry_time = 100; 333 334 ret = tps65185_i2c_read(priv_data, REG_TMST1, &read_val); 335 if (ret < 0) { 336 printf("read REG_TMST1 failed: %d\n", ret); 337 return ret; 338 } 339 if (retry_time-- == 0) { 340 printf("read REG_TMST1 retry 100 times\n"); 341 break; 342 } 343 debug("read_val = 0x%x\n", read_val); 344 } while (((read_val & 0x20) == 0 || (read_val & 0x80))); 345 346 mdelay(PAPYRUS_TEMP_READ_TIME_MS); 347 ret = tps65185_i2c_read(priv_data, REG_TMST_VALUE, &read_val); 348 if (ret) { 349 printf("read REG_TMST_VALUE failed: %d\n", ret); 350 return ret; 351 } 352 *temp = (u32)read_val; 353 354 return 0; 355 } 356 357 static int tps65185_hw_init(struct udevice *dev) 358 { 359 int ret; 360 u8 rev_id = 0; 361 struct tps65185_priv_data *priv_data = dev_get_priv(dev); 362 363 dm_gpio_set_value(&priv_data->wake_up_gpio, 0); 364 mdelay(PAPYRUS_SLEEP_MINIMUM_MS); 365 dm_gpio_set_value(&priv_data->wake_up_gpio, 1); 366 dm_gpio_set_value(&priv_data->pwr_up_gpio, 0); 367 dm_gpio_set_value(&priv_data->vcom_gpio, 1); 368 mdelay(PAPYRUS_EEPROM_DELAY_MS); 369 ret = tps65185_i2c_read(priv_data, REG_REVID, &rev_id); 370 if (ret) { 371 printf("read revid failed: %d\n", ret); 372 return ret; 373 } 374 375 if (rev_id > 0) 376 printf("detected device with ID=%02x (TPS6518%dr%dp%d)\n", 377 rev_id, rev_id & 0xF, (rev_id & 0xC0) >> 6, 378 (rev_id & 0x30) >> 4); 379 380 tps65185_i2c_write(priv_data, REG_ENABLE, priv_data->shadow_en); 381 priv_data->rev_id = rev_id; 382 printf("rev_id=%x\n", rev_id); 383 return 0; 384 } 385 386 static void tps65185_init_arg(struct tps65185_priv_data *priv_data) 387 { 388 priv_data->vadj = 0x03; 389 390 priv_data->upseq0 = SEQ_VEE(0) | SEQ_VNEG(1) 391 | SEQ_VPOS(2) | SEQ_VDD(3); 392 priv_data->upseq1 = UDLY_3ms(0) | UDLY_3ms(1) 393 | UDLY_3ms(2) | UDLY_3ms(3); 394 395 priv_data->dwnseq0 = SEQ_VDD(0) | SEQ_VPOS(1) 396 | SEQ_VNEG(2) | SEQ_VEE(3); 397 priv_data->dwnseq1 = DDLY_6ms(0) | DDLY_6ms(1) 398 | DDLY_6ms(2) | DDLY_6ms(3); 399 400 priv_data->vcom1 = mv_to_vcom1_reg(1560); 401 priv_data->vcom2 = mv_to_vcom2_reg(1560); 402 priv_data->shadow_en = 0; 403 } 404 405 static int tps65185_probe(struct udevice *dev) 406 { 407 int ret; 408 struct tps65185_priv_data *tps65185_priv = dev_get_priv(dev); 409 410 tps65185_priv->dev = dev; 411 pmic_dev = dev; 412 tps65185_init_arg(tps65185_priv); 413 414 ret = gpio_request_by_name(dev, "wakeup-gpios", 0, 415 &tps65185_priv->wake_up_gpio, GPIOD_IS_OUT); 416 if (ret) { 417 printf("Cannot get wakeup_pin GPIO: %d\n", ret); 418 return ret; 419 } 420 ret = gpio_request_by_name(dev, "powerup-gpios", 0, 421 &tps65185_priv->pwr_up_gpio, GPIOD_IS_OUT); 422 if (ret) { 423 printf("Cannot get pwr_up_gpio GPIO: %d\n", ret); 424 return ret; 425 } 426 ret = gpio_request_by_name(dev, "vcomctl-gpios", 0, 427 &tps65185_priv->vcom_gpio, GPIOD_IS_OUT); 428 if (ret) { 429 printf("Cannot get vcom_gpio GPIO: %d\n", ret); 430 return ret; 431 } 432 ret = gpio_request_by_name(dev, "poweren-gpios", 0, 433 &tps65185_priv->pwr_en_gpio, GPIOD_IS_OUT); 434 if (!ret) 435 dm_gpio_set_value(&tps65185_priv->pwr_en_gpio, 1); 436 else 437 printf("Cannot get pwren_pin GPIO: %d\n", ret); 438 439 ret = tps65185_hw_init(dev); 440 if (ret) { 441 printf("Cannot init hardware for tps65185: %d\n", ret); 442 return ret; 443 } 444 445 return 0; 446 } 447 448 const struct rk_ebc_pwr_ops tps65185_funcs = { 449 .power_on = tps65185_power_on, 450 .power_down = tps65185_power_down, 451 .temp_get = tps65185_temp_get, 452 .vcom_set = tps65185_set_vcom_value, 453 }; 454 455 static const struct udevice_id ebc_power_of_match[] = { 456 { .compatible = "ti,tps65185" }, 457 {} 458 }; 459 460 U_BOOT_DRIVER(tps65185_ebc_pwr) = { 461 .name = "tps65185_ebc_pwr", 462 .id = UCLASS_I2C_GENERIC, 463 .of_match = ebc_power_of_match, 464 .probe = tps65185_probe, 465 .ops = &tps65185_funcs, 466 .bind = dm_scan_fdt_dev, 467 .priv_auto_alloc_size = sizeof(struct tps65185_priv_data), 468 }; 469 470