fg_max77693.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2013 Samsung Electronics
  4. * Piotr Wilczek <p.wilczek@samsung.com>
  5. */
  6. #include <common.h>
  7. #include <power/pmic.h>
  8. #include <power/max77693_fg.h>
  9. #include <i2c.h>
  10. #include <power/power_chrg.h>
  11. #include <power/battery.h>
  12. #include <power/fg_battery_cell_params.h>
  13. #include <errno.h>
  14. static int max77693_get_vcell(u32 *vcell)
  15. {
  16. u16 value;
  17. u8 ret;
  18. ret = i2c_read(MAX77693_FUEL_I2C_ADDR, MAX77693_VCELL, 1,
  19. (u8 *)&value, 2);
  20. if (ret)
  21. return ret;
  22. *vcell = (u32)(value >> 3);
  23. *vcell = *vcell * 625;
  24. return 0;
  25. }
  26. static int max77693_get_soc(u32 *soc)
  27. {
  28. u16 value;
  29. u8 ret;
  30. ret = i2c_read(MAX77693_FUEL_I2C_ADDR, MAX77693_VFSOC, 1,
  31. (u8 *)&value, 2);
  32. if (ret)
  33. return ret;
  34. *soc = (u32)(value >> 8);
  35. return 0;
  36. }
  37. static int power_update_battery(struct pmic *p, struct pmic *bat)
  38. {
  39. struct power_battery *pb = bat->pbat;
  40. int ret;
  41. if (pmic_probe(p)) {
  42. puts("Can't find max77693 fuel gauge\n");
  43. return -ENODEV;
  44. }
  45. ret = max77693_get_soc(&pb->bat->state_of_chrg);
  46. if (ret)
  47. return ret;
  48. max77693_get_vcell(&pb->bat->voltage_uV);
  49. return 0;
  50. }
  51. static int power_check_battery(struct pmic *p, struct pmic *bat)
  52. {
  53. struct power_battery *pb = bat->pbat;
  54. unsigned int val;
  55. int ret = 0;
  56. if (pmic_probe(p)) {
  57. puts("Can't find max77693 fuel gauge\n");
  58. return -ENODEV;
  59. }
  60. ret = pmic_reg_read(p, MAX77693_STATUS, &val);
  61. if (ret)
  62. return ret;
  63. debug("fg status: 0x%x\n", val);
  64. ret = pmic_reg_read(p, MAX77693_VERSION, &pb->bat->version);
  65. if (ret)
  66. return ret;
  67. ret = power_update_battery(p, bat);
  68. if (ret)
  69. return ret;
  70. debug("fg ver: 0x%x\n", pb->bat->version);
  71. printf("BAT: state_of_charge(SOC):%d%%\n",
  72. pb->bat->state_of_chrg);
  73. printf(" voltage: %d.%6.6d [V] (expected to be %d [mAh])\n",
  74. pb->bat->voltage_uV / 1000000,
  75. pb->bat->voltage_uV % 1000000,
  76. pb->bat->capacity);
  77. if (pb->bat->voltage_uV > 3850000)
  78. pb->bat->state = EXT_SOURCE;
  79. else if (pb->bat->voltage_uV < 3600000 || pb->bat->state_of_chrg < 5)
  80. pb->bat->state = CHARGE;
  81. else
  82. pb->bat->state = NORMAL;
  83. return 0;
  84. }
  85. static struct power_fg power_fg_ops = {
  86. .fg_battery_check = power_check_battery,
  87. .fg_battery_update = power_update_battery,
  88. };
  89. int power_fg_init(unsigned char bus)
  90. {
  91. static const char name[] = "MAX77693_FG";
  92. struct pmic *p = pmic_alloc();
  93. if (!p) {
  94. printf("%s: POWER allocation error!\n", __func__);
  95. return -ENOMEM;
  96. }
  97. debug("Board Fuel Gauge init\n");
  98. p->name = name;
  99. p->interface = PMIC_I2C;
  100. p->number_of_regs = FG_NUM_OF_REGS;
  101. p->hw.i2c.addr = MAX77693_FUEL_I2C_ADDR;
  102. p->hw.i2c.tx_num = 2;
  103. p->sensor_byte_order = PMIC_SENSOR_BYTE_ORDER_BIG;
  104. p->bus = bus;
  105. p->fg = &power_fg_ops;
  106. return 0;
  107. }