wm831x_backup.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. };
  23. static int wm831x_backup_read_voltage(struct wm831x *wm831x,
  24. enum wm831x_auxadc src,
  25. union power_supply_propval *val)
  26. {
  27. int ret;
  28. ret = wm831x_auxadc_read_uv(wm831x, src);
  29. if (ret >= 0)
  30. val->intval = ret;
  31. return ret;
  32. }
  33. /*********************************************************************
  34. * Backup supply properties
  35. *********************************************************************/
  36. static void wm831x_config_backup(struct wm831x *wm831x)
  37. {
  38. struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
  39. struct wm831x_backup_pdata *pdata;
  40. int ret, reg;
  41. if (!wm831x_pdata || !wm831x_pdata->backup) {
  42. dev_warn(wm831x->dev,
  43. "No backup battery charger configuration\n");
  44. return;
  45. }
  46. pdata = wm831x_pdata->backup;
  47. reg = 0;
  48. if (pdata->charger_enable)
  49. reg |= WM831X_BKUP_CHG_ENA | WM831X_BKUP_BATT_DET_ENA;
  50. if (pdata->no_constant_voltage)
  51. reg |= WM831X_BKUP_CHG_MODE;
  52. switch (pdata->vlim) {
  53. case 2500:
  54. break;
  55. case 3100:
  56. reg |= WM831X_BKUP_CHG_VLIM;
  57. break;
  58. default:
  59. dev_err(wm831x->dev, "Invalid backup voltage limit %dmV\n",
  60. pdata->vlim);
  61. }
  62. switch (pdata->ilim) {
  63. case 100:
  64. break;
  65. case 200:
  66. reg |= 1;
  67. break;
  68. case 300:
  69. reg |= 2;
  70. break;
  71. case 400:
  72. reg |= 3;
  73. break;
  74. default:
  75. dev_err(wm831x->dev, "Invalid backup current limit %duA\n",
  76. pdata->ilim);
  77. }
  78. ret = wm831x_reg_unlock(wm831x);
  79. if (ret != 0) {
  80. dev_err(wm831x->dev, "Failed to unlock registers: %d\n", ret);
  81. return;
  82. }
  83. ret = wm831x_set_bits(wm831x, WM831X_BACKUP_CHARGER_CONTROL,
  84. WM831X_BKUP_CHG_ENA_MASK |
  85. WM831X_BKUP_CHG_MODE_MASK |
  86. WM831X_BKUP_BATT_DET_ENA_MASK |
  87. WM831X_BKUP_CHG_VLIM_MASK |
  88. WM831X_BKUP_CHG_ILIM_MASK,
  89. reg);
  90. if (ret != 0)
  91. dev_err(wm831x->dev,
  92. "Failed to set backup charger config: %d\n", ret);
  93. wm831x_reg_lock(wm831x);
  94. }
  95. static int wm831x_backup_get_prop(struct power_supply *psy,
  96. enum power_supply_property psp,
  97. union power_supply_propval *val)
  98. {
  99. struct wm831x_backup *devdata = dev_get_drvdata(psy->dev->parent);
  100. struct wm831x *wm831x = devdata->wm831x;
  101. int ret = 0;
  102. ret = wm831x_reg_read(wm831x, WM831X_BACKUP_CHARGER_CONTROL);
  103. if (ret < 0)
  104. return ret;
  105. switch (psp) {
  106. case POWER_SUPPLY_PROP_STATUS:
  107. if (ret & WM831X_BKUP_CHG_STS)
  108. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  109. else
  110. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  111. break;
  112. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  113. ret = wm831x_backup_read_voltage(wm831x, WM831X_AUX_BKUP_BATT,
  114. val);
  115. break;
  116. case POWER_SUPPLY_PROP_PRESENT:
  117. if (ret & WM831X_BKUP_CHG_STS)
  118. val->intval = 1;
  119. else
  120. val->intval = 0;
  121. break;
  122. default:
  123. ret = -EINVAL;
  124. break;
  125. }
  126. return ret;
  127. }
  128. static enum power_supply_property wm831x_backup_props[] = {
  129. POWER_SUPPLY_PROP_STATUS,
  130. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  131. POWER_SUPPLY_PROP_PRESENT,
  132. };
  133. /*********************************************************************
  134. * Initialisation
  135. *********************************************************************/
  136. static __devinit int wm831x_backup_probe(struct platform_device *pdev)
  137. {
  138. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  139. struct wm831x_backup *devdata;
  140. struct power_supply *backup;
  141. int ret;
  142. devdata = kzalloc(sizeof(struct wm831x_backup), GFP_KERNEL);
  143. if (devdata == NULL)
  144. return -ENOMEM;
  145. devdata->wm831x = wm831x;
  146. platform_set_drvdata(pdev, devdata);
  147. backup = &devdata->backup;
  148. /* We ignore configuration failures since we can still read
  149. * back the status without enabling the charger (which may
  150. * already be enabled anyway).
  151. */
  152. wm831x_config_backup(wm831x);
  153. backup->name = "wm831x-backup";
  154. backup->type = POWER_SUPPLY_TYPE_BATTERY;
  155. backup->properties = wm831x_backup_props;
  156. backup->num_properties = ARRAY_SIZE(wm831x_backup_props);
  157. backup->get_property = wm831x_backup_get_prop;
  158. ret = power_supply_register(&pdev->dev, backup);
  159. if (ret)
  160. goto err_kmalloc;
  161. return ret;
  162. err_kmalloc:
  163. kfree(devdata);
  164. return ret;
  165. }
  166. static __devexit int wm831x_backup_remove(struct platform_device *pdev)
  167. {
  168. struct wm831x_backup *devdata = platform_get_drvdata(pdev);
  169. power_supply_unregister(&devdata->backup);
  170. kfree(devdata);
  171. return 0;
  172. }
  173. static struct platform_driver wm831x_backup_driver = {
  174. .probe = wm831x_backup_probe,
  175. .remove = __devexit_p(wm831x_backup_remove),
  176. .driver = {
  177. .name = "wm831x-backup",
  178. },
  179. };
  180. static int __init wm831x_backup_init(void)
  181. {
  182. return platform_driver_register(&wm831x_backup_driver);
  183. }
  184. module_init(wm831x_backup_init);
  185. static void __exit wm831x_backup_exit(void)
  186. {
  187. platform_driver_unregister(&wm831x_backup_driver);
  188. }
  189. module_exit(wm831x_backup_exit);
  190. MODULE_DESCRIPTION("Backup battery charger driver for WM831x PMICs");
  191. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  192. MODULE_LICENSE("GPL");
  193. MODULE_ALIAS("platform:wm831x-backup");