rt5033_battery.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 = container_of(psy,
  63. struct rt5033_battery, psy);
  64. switch (psp) {
  65. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  66. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  67. case POWER_SUPPLY_PROP_VOLTAGE_OCV:
  68. val->intval = rt5033_battery_get_watt_prop(battery->client,
  69. psp);
  70. break;
  71. case POWER_SUPPLY_PROP_PRESENT:
  72. val->intval = rt5033_battery_get_present(battery->client);
  73. break;
  74. case POWER_SUPPLY_PROP_CAPACITY:
  75. val->intval = rt5033_battery_get_capacity(battery->client);
  76. break;
  77. default:
  78. return -EINVAL;
  79. }
  80. return 0;
  81. }
  82. static enum power_supply_property rt5033_battery_props[] = {
  83. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  84. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  85. POWER_SUPPLY_PROP_VOLTAGE_OCV,
  86. POWER_SUPPLY_PROP_PRESENT,
  87. POWER_SUPPLY_PROP_CAPACITY,
  88. };
  89. static struct regmap_config rt5033_battery_regmap_config = {
  90. .reg_bits = 8,
  91. .val_bits = 8,
  92. .max_register = RT5033_FUEL_REG_END,
  93. };
  94. static int rt5033_battery_probe(struct i2c_client *client,
  95. const struct i2c_device_id *id)
  96. {
  97. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  98. struct rt5033_battery *battery;
  99. u32 ret;
  100. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  101. return -EIO;
  102. battery = devm_kzalloc(&client->dev, sizeof(*battery), GFP_KERNEL);
  103. if (!battery)
  104. return -EINVAL;
  105. battery->client = client;
  106. battery->regmap = devm_regmap_init_i2c(client,
  107. &rt5033_battery_regmap_config);
  108. if (IS_ERR(battery->regmap)) {
  109. dev_err(&client->dev, "Failed to initialize regmap\n");
  110. return -EINVAL;
  111. }
  112. i2c_set_clientdata(client, battery);
  113. battery->psy.name = "rt5033-battery";
  114. battery->psy.type = POWER_SUPPLY_TYPE_BATTERY;
  115. battery->psy.get_property = rt5033_battery_get_property;
  116. battery->psy.properties = rt5033_battery_props;
  117. battery->psy.num_properties = ARRAY_SIZE(rt5033_battery_props);
  118. ret = power_supply_register(&client->dev, &battery->psy);
  119. if (ret) {
  120. dev_err(&client->dev, "Failed to register power supply\n");
  121. return ret;
  122. }
  123. return 0;
  124. }
  125. static int rt5033_battery_remove(struct i2c_client *client)
  126. {
  127. struct rt5033_battery *battery = i2c_get_clientdata(client);
  128. power_supply_unregister(&battery->psy);
  129. return 0;
  130. }
  131. static const struct i2c_device_id rt5033_battery_id[] = {
  132. { "rt5033-battery", },
  133. { }
  134. };
  135. MODULE_DEVICE_TABLE(platform, rt5033_battery_id);
  136. static struct i2c_driver rt5033_battery_driver = {
  137. .driver = {
  138. .name = "rt5033-battery",
  139. },
  140. .probe = rt5033_battery_probe,
  141. .remove = rt5033_battery_remove,
  142. .id_table = rt5033_battery_id,
  143. };
  144. module_i2c_driver(rt5033_battery_driver);
  145. MODULE_DESCRIPTION("Richtek RT5033 fuel gauge driver");
  146. MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
  147. MODULE_LICENSE("GPL");