fg_max77693.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <log.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 -ENODEV;
  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. return 0;
  51. }
  52. static int power_check_battery(struct pmic *p, struct pmic *bat)
  53. {
  54. struct power_battery *pb = bat->pbat;
  55. unsigned int val;
  56. int ret = 0;
  57. if (pmic_probe(p)) {
  58. puts("Can't find max77693 fuel gauge\n");
  59. return -ENODEV;
  60. }
  61. ret = pmic_reg_read(p, MAX77693_STATUS, &val);
  62. if (ret)
  63. return ret;
  64. debug("fg status: 0x%x\n", val);
  65. ret = pmic_reg_read(p, MAX77693_VERSION, &pb->bat->version);
  66. if (ret)
  67. return ret;
  68. ret = power_update_battery(p, bat);
  69. if (ret)
  70. return ret;
  71. debug("fg ver: 0x%x\n", pb->bat->version);
  72. printf("BAT: state_of_charge(SOC):%d%%\n",
  73. pb->bat->state_of_chrg);
  74. printf(" voltage: %d.%6.6d [V] (expected to be %d [mAh])\n",
  75. pb->bat->voltage_uV / 1000000,
  76. pb->bat->voltage_uV % 1000000,
  77. pb->bat->capacity);
  78. if (pb->bat->voltage_uV > 3850000)
  79. pb->bat->state = EXT_SOURCE;
  80. else if (pb->bat->voltage_uV < 3600000 || pb->bat->state_of_chrg < 5)
  81. pb->bat->state = CHARGE;
  82. else
  83. pb->bat->state = NORMAL;
  84. return 0;
  85. }
  86. static struct power_fg power_fg_ops = {
  87. .fg_battery_check = power_check_battery,
  88. .fg_battery_update = power_update_battery,
  89. };
  90. int power_fg_init(unsigned char bus)
  91. {
  92. static const char name[] = "MAX77693_FG";
  93. struct pmic *p = pmic_alloc();
  94. if (!p) {
  95. printf("%s: POWER allocation error!\n", __func__);
  96. return -ENOMEM;
  97. }
  98. debug("Board Fuel Gauge init\n");
  99. p->name = name;
  100. p->interface = PMIC_I2C;
  101. p->number_of_regs = FG_NUM_OF_REGS;
  102. p->hw.i2c.addr = MAX77693_FUEL_I2C_ADDR;
  103. p->hw.i2c.tx_num = 2;
  104. p->sensor_byte_order = PMIC_SENSOR_BYTE_ORDER_BIG;
  105. p->bus = bus;
  106. p->fg = &power_fg_ops;
  107. return 0;
  108. }