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