wm831x_backup.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Backup battery driver for Wolfson Microelectronics wm831x PMICs
  3. *
  4. * Copyright 2009 Wolfson Microelectronics PLC.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/err.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/power_supply.h>
  14. #include <linux/slab.h>
  15. #include <linux/mfd/wm831x/core.h>
  16. #include <linux/mfd/wm831x/auxadc.h>
  17. #include <linux/mfd/wm831x/pmu.h>
  18. #include <linux/mfd/wm831x/pdata.h>
  19. struct wm831x_backup {
  20. struct wm831x *wm831x;
  21. struct power_supply *backup;
  22. struct power_supply_desc backup_desc;
  23. char name[20];
  24. };
  25. static int wm831x_backup_read_voltage(struct wm831x *wm831x,
  26. enum wm831x_auxadc src,
  27. union power_supply_propval *val)
  28. {
  29. int ret;
  30. ret = wm831x_auxadc_read_uv(wm831x, src);
  31. if (ret >= 0)
  32. val->intval = ret;
  33. return ret;
  34. }
  35. /*********************************************************************
  36. * Backup supply properties
  37. *********************************************************************/
  38. static void wm831x_config_backup(struct wm831x *wm831x)
  39. {
  40. struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
  41. struct wm831x_backup_pdata *pdata;
  42. int ret, reg;
  43. if (!wm831x_pdata || !wm831x_pdata->backup) {
  44. dev_warn(wm831x->dev,
  45. "No backup battery charger configuration\n");
  46. return;
  47. }
  48. pdata = wm831x_pdata->backup;
  49. reg = 0;
  50. if (pdata->charger_enable)
  51. reg |= WM831X_BKUP_CHG_ENA | WM831X_BKUP_BATT_DET_ENA;
  52. if (pdata->no_constant_voltage)
  53. reg |= WM831X_BKUP_CHG_MODE;
  54. switch (pdata->vlim) {
  55. case 2500:
  56. break;
  57. case 3100:
  58. reg |= WM831X_BKUP_CHG_VLIM;
  59. break;
  60. default:
  61. dev_err(wm831x->dev, "Invalid backup voltage limit %dmV\n",
  62. pdata->vlim);
  63. }
  64. switch (pdata->ilim) {
  65. case 100:
  66. break;
  67. case 200:
  68. reg |= 1;
  69. break;
  70. case 300:
  71. reg |= 2;
  72. break;
  73. case 400:
  74. reg |= 3;
  75. break;
  76. default:
  77. dev_err(wm831x->dev, "Invalid backup current limit %duA\n",
  78. pdata->ilim);
  79. }
  80. ret = wm831x_reg_unlock(wm831x);
  81. if (ret != 0) {
  82. dev_err(wm831x->dev, "Failed to unlock registers: %d\n", ret);
  83. return;
  84. }
  85. ret = wm831x_set_bits(wm831x, WM831X_BACKUP_CHARGER_CONTROL,
  86. WM831X_BKUP_CHG_ENA_MASK |
  87. WM831X_BKUP_CHG_MODE_MASK |
  88. WM831X_BKUP_BATT_DET_ENA_MASK |
  89. WM831X_BKUP_CHG_VLIM_MASK |
  90. WM831X_BKUP_CHG_ILIM_MASK,
  91. reg);
  92. if (ret != 0)
  93. dev_err(wm831x->dev,
  94. "Failed to set backup charger config: %d\n", ret);
  95. wm831x_reg_lock(wm831x);
  96. }
  97. static int wm831x_backup_get_prop(struct power_supply *psy,
  98. enum power_supply_property psp,
  99. union power_supply_propval *val)
  100. {
  101. struct wm831x_backup *devdata = dev_get_drvdata(psy->dev.parent);
  102. struct wm831x *wm831x = devdata->wm831x;
  103. int ret = 0;
  104. ret = wm831x_reg_read(wm831x, WM831X_BACKUP_CHARGER_CONTROL);
  105. if (ret < 0)
  106. return ret;
  107. switch (psp) {
  108. case POWER_SUPPLY_PROP_STATUS:
  109. if (ret & WM831X_BKUP_CHG_STS)
  110. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  111. else
  112. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  113. break;
  114. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  115. ret = wm831x_backup_read_voltage(wm831x, WM831X_AUX_BKUP_BATT,
  116. val);
  117. break;
  118. case POWER_SUPPLY_PROP_PRESENT:
  119. if (ret & WM831X_BKUP_CHG_STS)
  120. val->intval = 1;
  121. else
  122. val->intval = 0;
  123. break;
  124. default:
  125. ret = -EINVAL;
  126. break;
  127. }
  128. return ret;
  129. }
  130. static enum power_supply_property wm831x_backup_props[] = {
  131. POWER_SUPPLY_PROP_STATUS,
  132. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  133. POWER_SUPPLY_PROP_PRESENT,
  134. };
  135. /*********************************************************************
  136. * Initialisation
  137. *********************************************************************/
  138. static int wm831x_backup_probe(struct platform_device *pdev)
  139. {
  140. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  141. struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
  142. struct wm831x_backup *devdata;
  143. devdata = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_backup),
  144. GFP_KERNEL);
  145. if (devdata == NULL)
  146. return -ENOMEM;
  147. devdata->wm831x = wm831x;
  148. platform_set_drvdata(pdev, devdata);
  149. /* We ignore configuration failures since we can still read
  150. * back the status without enabling the charger (which may
  151. * already be enabled anyway).
  152. */
  153. wm831x_config_backup(wm831x);
  154. if (wm831x_pdata && wm831x_pdata->wm831x_num)
  155. snprintf(devdata->name, sizeof(devdata->name),
  156. "wm831x-backup.%d", wm831x_pdata->wm831x_num);
  157. else
  158. snprintf(devdata->name, sizeof(devdata->name),
  159. "wm831x-backup");
  160. devdata->backup_desc.name = devdata->name;
  161. devdata->backup_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  162. devdata->backup_desc.properties = wm831x_backup_props;
  163. devdata->backup_desc.num_properties = ARRAY_SIZE(wm831x_backup_props);
  164. devdata->backup_desc.get_property = wm831x_backup_get_prop;
  165. devdata->backup = power_supply_register(&pdev->dev,
  166. &devdata->backup_desc, NULL);
  167. return PTR_ERR_OR_ZERO(devdata->backup);
  168. }
  169. static int wm831x_backup_remove(struct platform_device *pdev)
  170. {
  171. struct wm831x_backup *devdata = platform_get_drvdata(pdev);
  172. power_supply_unregister(devdata->backup);
  173. return 0;
  174. }
  175. static struct platform_driver wm831x_backup_driver = {
  176. .probe = wm831x_backup_probe,
  177. .remove = wm831x_backup_remove,
  178. .driver = {
  179. .name = "wm831x-backup",
  180. },
  181. };
  182. module_platform_driver(wm831x_backup_driver);
  183. MODULE_DESCRIPTION("Backup battery charger driver for WM831x PMICs");
  184. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  185. MODULE_LICENSE("GPL");
  186. MODULE_ALIAS("platform:wm831x-backup");