ab8500-sysctrl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. static inline bool valid_bank(u8 bank)
  75. {
  76. return ((bank == AB8500_SYS_CTRL1_BLOCK) ||
  77. (bank == AB8500_SYS_CTRL2_BLOCK));
  78. }
  79. int ab8500_sysctrl_read(u16 reg, u8 *value)
  80. {
  81. u8 bank;
  82. if (sysctrl_dev == NULL)
  83. return -EINVAL;
  84. bank = (reg >> 8);
  85. if (!valid_bank(bank))
  86. return -EINVAL;
  87. return abx500_get_register_interruptible(sysctrl_dev, bank,
  88. (u8)(reg & 0xFF), value);
  89. }
  90. EXPORT_SYMBOL(ab8500_sysctrl_read);
  91. int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
  92. {
  93. u8 bank;
  94. if (sysctrl_dev == NULL)
  95. return -EINVAL;
  96. bank = (reg >> 8);
  97. if (!valid_bank(bank))
  98. return -EINVAL;
  99. return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
  100. (u8)(reg & 0xFF), mask, value);
  101. }
  102. EXPORT_SYMBOL(ab8500_sysctrl_write);
  103. static int ab8500_sysctrl_probe(struct platform_device *pdev)
  104. {
  105. struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
  106. struct ab8500_platform_data *plat;
  107. struct ab8500_sysctrl_platform_data *pdata;
  108. plat = dev_get_platdata(pdev->dev.parent);
  109. if (!plat)
  110. return -EINVAL;
  111. sysctrl_dev = &pdev->dev;
  112. if (!pm_power_off)
  113. pm_power_off = ab8500_power_off;
  114. pdata = plat->sysctrl;
  115. if (pdata) {
  116. int last, ret, i, j;
  117. if (is_ab8505(ab8500))
  118. last = AB8500_SYSCLKREQ4RFCLKBUF;
  119. else
  120. last = AB8500_SYSCLKREQ8RFCLKBUF;
  121. for (i = AB8500_SYSCLKREQ1RFCLKBUF; i <= last; i++) {
  122. j = i - AB8500_SYSCLKREQ1RFCLKBUF;
  123. ret = ab8500_sysctrl_write(i, 0xff,
  124. pdata->initial_req_buf_config[j]);
  125. dev_dbg(&pdev->dev,
  126. "Setting SysClkReq%dRfClkBuf 0x%X\n",
  127. j + 1,
  128. pdata->initial_req_buf_config[j]);
  129. if (ret < 0) {
  130. dev_err(&pdev->dev,
  131. "unable to set sysClkReq%dRfClkBuf: "
  132. "%d\n", j + 1, ret);
  133. }
  134. }
  135. }
  136. return 0;
  137. }
  138. static int ab8500_sysctrl_remove(struct platform_device *pdev)
  139. {
  140. sysctrl_dev = NULL;
  141. if (pm_power_off == ab8500_power_off)
  142. pm_power_off = NULL;
  143. return 0;
  144. }
  145. static struct platform_driver ab8500_sysctrl_driver = {
  146. .driver = {
  147. .name = "ab8500-sysctrl",
  148. },
  149. .probe = ab8500_sysctrl_probe,
  150. .remove = ab8500_sysctrl_remove,
  151. };
  152. static int __init ab8500_sysctrl_init(void)
  153. {
  154. return platform_driver_register(&ab8500_sysctrl_driver);
  155. }
  156. arch_initcall(ab8500_sysctrl_init);
  157. MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com");
  158. MODULE_DESCRIPTION("AB8500 system control driver");
  159. MODULE_LICENSE("GPL v2");