ab8500-sysctrl.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. * Author: Mattias Nilsson <mattias.i.nilsson@stericsson.com> for ST Ericsson.
  4. * License terms: GNU General Public License (GPL) version 2
  5. */
  6. #include <linux/err.h>
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/pm.h>
  10. #include <linux/reboot.h>
  11. #include <linux/signal.h>
  12. #include <linux/power_supply.h>
  13. #include <linux/mfd/abx500.h>
  14. #include <linux/mfd/abx500/ab8500.h>
  15. #include <linux/mfd/abx500/ab8500-sysctrl.h>
  16. /* RtcCtrl bits */
  17. #define AB8500_ALARM_MIN_LOW 0x08
  18. #define AB8500_ALARM_MIN_MID 0x09
  19. #define RTC_CTRL 0x0B
  20. #define RTC_ALARM_ENABLE 0x4
  21. static struct device *sysctrl_dev;
  22. static void ab8500_power_off(void)
  23. {
  24. sigset_t old;
  25. sigset_t all;
  26. static char *pss[] = {"ab8500_ac", "pm2301", "ab8500_usb"};
  27. int i;
  28. bool charger_present = false;
  29. union power_supply_propval val;
  30. struct power_supply *psy;
  31. int ret;
  32. if (sysctrl_dev == NULL) {
  33. pr_err("%s: sysctrl not initialized\n", __func__);
  34. return;
  35. }
  36. /*
  37. * If we have a charger connected and we're powering off,
  38. * reboot into charge-only mode.
  39. */
  40. for (i = 0; i < ARRAY_SIZE(pss); i++) {
  41. psy = power_supply_get_by_name(pss[i]);
  42. if (!psy)
  43. continue;
  44. ret = psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &val);
  45. if (!ret && val.intval) {
  46. charger_present = true;
  47. break;
  48. }
  49. }
  50. if (!charger_present)
  51. goto shutdown;
  52. /* Check if battery is known */
  53. psy = power_supply_get_by_name("ab8500_btemp");
  54. if (psy) {
  55. ret = psy->get_property(psy, POWER_SUPPLY_PROP_TECHNOLOGY,
  56. &val);
  57. if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) {
  58. printk(KERN_INFO
  59. "Charger \"%s\" is connected with known battery."
  60. " Rebooting.\n",
  61. pss[i]);
  62. machine_restart("charging");
  63. }
  64. }
  65. shutdown:
  66. sigfillset(&all);
  67. if (!sigprocmask(SIG_BLOCK, &all, &old)) {
  68. (void)ab8500_sysctrl_set(AB8500_STW4500CTRL1,
  69. AB8500_STW4500CTRL1_SWOFF |
  70. AB8500_STW4500CTRL1_SWRESET4500N);
  71. (void)sigprocmask(SIG_SETMASK, &old, NULL);
  72. }
  73. }
  74. /*
  75. * Use the AB WD to reset the platform. It will perform a hard
  76. * reset instead of a soft reset. Write the reset reason to
  77. * the AB before reset, which can be read upon restart.
  78. */
  79. void ab8500_restart(char mode, const char *cmd)
  80. {
  81. struct ab8500_platform_data *plat;
  82. struct ab8500_sysctrl_platform_data *pdata;
  83. u16 reason = 0;
  84. u8 val;
  85. if (sysctrl_dev == NULL) {
  86. pr_err("%s: sysctrl not initialized\n", __func__);
  87. return;
  88. }
  89. plat = dev_get_platdata(sysctrl_dev->parent);
  90. pdata = plat->sysctrl;
  91. if (pdata && pdata->reboot_reason_code)
  92. reason = pdata->reboot_reason_code(cmd);
  93. else
  94. pr_warn("[%s] No reboot reason set. Default reason %d\n",
  95. __func__, reason);
  96. /*
  97. * Disable RTC alarm, just a precaution so that no alarm
  98. * is running when WD reset is executed.
  99. */
  100. abx500_get_register_interruptible(sysctrl_dev, AB8500_RTC,
  101. RTC_CTRL , &val);
  102. abx500_set_register_interruptible(sysctrl_dev, AB8500_RTC,
  103. RTC_CTRL , (val & ~RTC_ALARM_ENABLE));
  104. /*
  105. * Android is not using the RTC alarm registers during reboot
  106. * so we borrow them for writing the reason of reset
  107. */
  108. /* reason[8 LSB] */
  109. val = reason & 0xFF;
  110. abx500_set_register_interruptible(sysctrl_dev, AB8500_RTC,
  111. AB8500_ALARM_MIN_LOW , val);
  112. /* reason[8 MSB] */
  113. val = (reason>>8) & 0xFF;
  114. abx500_set_register_interruptible(sysctrl_dev, AB8500_RTC,
  115. AB8500_ALARM_MIN_MID , val);
  116. /* Setting WD timeout to 0 */
  117. ab8500_sysctrl_write(AB8500_MAINWDOGTIMER, 0xFF, 0x0);
  118. /* Setting the parameters to AB8500 WD*/
  119. ab8500_sysctrl_write(AB8500_MAINWDOGCTRL, 0xFF, (AB8500_ENABLE_WD |
  120. AB8500_WD_RESTART_ON_EXPIRE | AB8500_KICK_WD));
  121. }
  122. static inline bool valid_bank(u8 bank)
  123. {
  124. return ((bank == AB8500_SYS_CTRL1_BLOCK) ||
  125. (bank == AB8500_SYS_CTRL2_BLOCK));
  126. }
  127. int ab8500_sysctrl_read(u16 reg, u8 *value)
  128. {
  129. u8 bank;
  130. if (sysctrl_dev == NULL)
  131. return -EINVAL;
  132. bank = (reg >> 8);
  133. if (!valid_bank(bank))
  134. return -EINVAL;
  135. return abx500_get_register_interruptible(sysctrl_dev, bank,
  136. (u8)(reg & 0xFF), value);
  137. }
  138. EXPORT_SYMBOL(ab8500_sysctrl_read);
  139. int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
  140. {
  141. u8 bank;
  142. if (sysctrl_dev == NULL)
  143. return -EINVAL;
  144. bank = (reg >> 8);
  145. if (!valid_bank(bank))
  146. return -EINVAL;
  147. return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
  148. (u8)(reg & 0xFF), mask, value);
  149. }
  150. EXPORT_SYMBOL(ab8500_sysctrl_write);
  151. static int ab8500_sysctrl_probe(struct platform_device *pdev)
  152. {
  153. struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
  154. struct ab8500_platform_data *plat;
  155. struct ab8500_sysctrl_platform_data *pdata;
  156. plat = dev_get_platdata(pdev->dev.parent);
  157. if (!plat)
  158. return -EINVAL;
  159. sysctrl_dev = &pdev->dev;
  160. if (!pm_power_off)
  161. pm_power_off = ab8500_power_off;
  162. pdata = plat->sysctrl;
  163. if (pdata) {
  164. int last, ret, i, j;
  165. if (is_ab8505(ab8500))
  166. last = AB8500_SYSCLKREQ4RFCLKBUF;
  167. else
  168. last = AB8500_SYSCLKREQ8RFCLKBUF;
  169. for (i = AB8500_SYSCLKREQ1RFCLKBUF; i <= last; i++) {
  170. j = i - AB8500_SYSCLKREQ1RFCLKBUF;
  171. ret = ab8500_sysctrl_write(i, 0xff,
  172. pdata->initial_req_buf_config[j]);
  173. dev_dbg(&pdev->dev,
  174. "Setting SysClkReq%dRfClkBuf 0x%X\n",
  175. j + 1,
  176. pdata->initial_req_buf_config[j]);
  177. if (ret < 0) {
  178. dev_err(&pdev->dev,
  179. "unable to set sysClkReq%dRfClkBuf: "
  180. "%d\n", j + 1, ret);
  181. }
  182. }
  183. }
  184. return 0;
  185. }
  186. static int ab8500_sysctrl_remove(struct platform_device *pdev)
  187. {
  188. sysctrl_dev = NULL;
  189. if (pm_power_off == ab8500_power_off)
  190. pm_power_off = NULL;
  191. return 0;
  192. }
  193. static struct platform_driver ab8500_sysctrl_driver = {
  194. .driver = {
  195. .name = "ab8500-sysctrl",
  196. .owner = THIS_MODULE,
  197. },
  198. .probe = ab8500_sysctrl_probe,
  199. .remove = ab8500_sysctrl_remove,
  200. };
  201. static int __init ab8500_sysctrl_init(void)
  202. {
  203. return platform_driver_register(&ab8500_sysctrl_driver);
  204. }
  205. arch_initcall(ab8500_sysctrl_init);
  206. MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com");
  207. MODULE_DESCRIPTION("AB8500 system control driver");
  208. MODULE_LICENSE("GPL v2");