rt5033_battery.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Fuel gauge driver for Richtek RT5033
  3. *
  4. * Copyright (C) 2014 Samsung Electronics, Co., Ltd.
  5. * Author: Beomho Seo <beomho.seo@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published bythe Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/power_supply.h>
  14. #include <linux/mfd/rt5033-private.h>
  15. #include <linux/mfd/rt5033.h>
  16. static int rt5033_battery_get_capacity(struct i2c_client *client)
  17. {
  18. struct rt5033_battery *battery = i2c_get_clientdata(client);
  19. u32 msb;
  20. regmap_read(battery->regmap, RT5033_FUEL_REG_SOC_H, &msb);
  21. return msb;
  22. }
  23. static int rt5033_battery_get_present(struct i2c_client *client)
  24. {
  25. struct rt5033_battery *battery = i2c_get_clientdata(client);
  26. u32 val;
  27. regmap_read(battery->regmap, RT5033_FUEL_REG_CONFIG_L, &val);
  28. return (val & RT5033_FUEL_BAT_PRESENT) ? true : false;
  29. }
  30. static int rt5033_battery_get_watt_prop(struct i2c_client *client,
  31. enum power_supply_property psp)
  32. {
  33. struct rt5033_battery *battery = i2c_get_clientdata(client);
  34. unsigned int regh, regl;
  35. int ret;
  36. u32 msb, lsb;
  37. switch (psp) {
  38. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  39. regh = RT5033_FUEL_REG_VBAT_H;
  40. regl = RT5033_FUEL_REG_VBAT_L;
  41. break;
  42. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  43. regh = RT5033_FUEL_REG_AVG_VOLT_H;
  44. regl = RT5033_FUEL_REG_AVG_VOLT_L;
  45. break;
  46. case POWER_SUPPLY_PROP_VOLTAGE_OCV:
  47. regh = RT5033_FUEL_REG_OCV_H;
  48. regl = RT5033_FUEL_REG_OCV_L;
  49. break;
  50. default:
  51. return -EINVAL;
  52. }
  53. regmap_read(battery->regmap, regh, &msb);
  54. regmap_read(battery->regmap, regl, &lsb);
  55. ret = ((msb << 4) + (lsb >> 4)) * 1250 / 1000;
  56. return ret;
  57. }
  58. static int rt5033_battery_get_property(struct power_supply *psy,
  59. enum power_supply_property psp,
  60. union power_supply_propval *val)
  61. {
  62. struct rt5033_battery *battery = power_supply_get_drvdata(psy);
  63. switch (psp) {
  64. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  65. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  66. case POWER_SUPPLY_PROP_VOLTAGE_OCV:
  67. val->intval = rt5033_battery_get_watt_prop(battery->client,
  68. psp);
  69. break;
  70. case POWER_SUPPLY_PROP_PRESENT:
  71. val->intval = rt5033_battery_get_present(battery->client);
  72. break;
  73. case POWER_SUPPLY_PROP_CAPACITY:
  74. val->intval = rt5033_battery_get_capacity(battery->client);
  75. break;
  76. default:
  77. return -EINVAL;
  78. }
  79. return 0;
  80. }
  81. static enum power_supply_property rt5033_battery_props[] = {
  82. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  83. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  84. POWER_SUPPLY_PROP_VOLTAGE_OCV,
  85. POWER_SUPPLY_PROP_PRESENT,
  86. POWER_SUPPLY_PROP_CAPACITY,
  87. };
  88. static const struct regmap_config rt5033_battery_regmap_config = {
  89. .reg_bits = 8,
  90. .val_bits = 8,
  91. .max_register = RT5033_FUEL_REG_END,
  92. };
  93. static const struct power_supply_desc rt5033_battery_desc = {
  94. .name = "rt5033-battery",
  95. .type = POWER_SUPPLY_TYPE_BATTERY,
  96. .get_property = rt5033_battery_get_property,
  97. .properties = rt5033_battery_props,
  98. .num_properties = ARRAY_SIZE(rt5033_battery_props),
  99. };
  100. static int rt5033_battery_probe(struct i2c_client *client,
  101. const struct i2c_device_id *id)
  102. {
  103. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  104. struct power_supply_config psy_cfg = {};
  105. struct rt5033_battery *battery;
  106. u32 ret;
  107. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  108. return -EIO;
  109. battery = devm_kzalloc(&client->dev, sizeof(*battery), GFP_KERNEL);
  110. if (!battery)
  111. return -EINVAL;
  112. battery->client = client;
  113. battery->regmap = devm_regmap_init_i2c(client,
  114. &rt5033_battery_regmap_config);
  115. if (IS_ERR(battery->regmap)) {
  116. dev_err(&client->dev, "Failed to initialize regmap\n");
  117. return -EINVAL;
  118. }
  119. i2c_set_clientdata(client, battery);
  120. psy_cfg.drv_data = battery;
  121. battery->psy = power_supply_register(&client->dev,
  122. &rt5033_battery_desc, &psy_cfg);
  123. if (IS_ERR(battery->psy)) {
  124. dev_err(&client->dev, "Failed to register power supply\n");
  125. ret = PTR_ERR(battery->psy);
  126. return ret;
  127. }
  128. return 0;
  129. }
  130. static int rt5033_battery_remove(struct i2c_client *client)
  131. {
  132. struct rt5033_battery *battery = i2c_get_clientdata(client);
  133. power_supply_unregister(battery->psy);
  134. return 0;
  135. }
  136. static const struct i2c_device_id rt5033_battery_id[] = {
  137. { "rt5033-battery", },
  138. { }
  139. };
  140. MODULE_DEVICE_TABLE(i2c, rt5033_battery_id);
  141. static struct i2c_driver rt5033_battery_driver = {
  142. .driver = {
  143. .name = "rt5033-battery",
  144. },
  145. .probe = rt5033_battery_probe,
  146. .remove = rt5033_battery_remove,
  147. .id_table = rt5033_battery_id,
  148. };
  149. module_i2c_driver(rt5033_battery_driver);
  150. MODULE_DESCRIPTION("Richtek RT5033 fuel gauge driver");
  151. MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
  152. MODULE_LICENSE("GPL");