fg_max77693.c 2.7 KB

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