ab8500-sysctrl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
  45. &val);
  46. power_supply_put(psy);
  47. if (!ret && val.intval) {
  48. charger_present = true;
  49. break;
  50. }
  51. }
  52. if (!charger_present)
  53. goto shutdown;
  54. /* Check if battery is known */
  55. psy = power_supply_get_by_name("ab8500_btemp");
  56. if (psy) {
  57. ret = power_supply_get_property(psy,
  58. POWER_SUPPLY_PROP_TECHNOLOGY, &val);
  59. if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) {
  60. printk(KERN_INFO
  61. "Charger \"%s\" is connected with known battery."
  62. " Rebooting.\n",
  63. pss[i]);
  64. machine_restart("charging");
  65. }
  66. power_supply_put(psy);
  67. }
  68. shutdown:
  69. sigfillset(&all);
  70. if (!sigprocmask(SIG_BLOCK, &all, &old)) {
  71. (void)ab8500_sysctrl_set(AB8500_STW4500CTRL1,
  72. AB8500_STW4500CTRL1_SWOFF |
  73. AB8500_STW4500CTRL1_SWRESET4500N);
  74. (void)sigprocmask(SIG_SETMASK, &old, NULL);
  75. }
  76. }
  77. static inline bool valid_bank(u8 bank)
  78. {
  79. return ((bank == AB8500_SYS_CTRL1_BLOCK) ||
  80. (bank == AB8500_SYS_CTRL2_BLOCK));
  81. }
  82. int ab8500_sysctrl_read(u16 reg, u8 *value)
  83. {
  84. u8 bank;
  85. if (sysctrl_dev == NULL)
  86. return -EINVAL;
  87. bank = (reg >> 8);
  88. if (!valid_bank(bank))
  89. return -EINVAL;
  90. return abx500_get_register_interruptible(sysctrl_dev, bank,
  91. (u8)(reg & 0xFF), value);
  92. }
  93. EXPORT_SYMBOL(ab8500_sysctrl_read);
  94. int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
  95. {
  96. u8 bank;
  97. if (sysctrl_dev == NULL)
  98. return -EINVAL;
  99. bank = (reg >> 8);
  100. if (!valid_bank(bank))
  101. return -EINVAL;
  102. return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
  103. (u8)(reg & 0xFF), mask, value);
  104. }
  105. EXPORT_SYMBOL(ab8500_sysctrl_write);
  106. static int ab8500_sysctrl_probe(struct platform_device *pdev)
  107. {
  108. struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
  109. struct ab8500_platform_data *plat;
  110. struct ab8500_sysctrl_platform_data *pdata;
  111. plat = dev_get_platdata(pdev->dev.parent);
  112. if (!plat)
  113. return -EINVAL;
  114. sysctrl_dev = &pdev->dev;
  115. if (!pm_power_off)
  116. pm_power_off = ab8500_power_off;
  117. pdata = plat->sysctrl;
  118. if (pdata) {
  119. int last, ret, i, j;
  120. if (is_ab8505(ab8500))
  121. last = AB8500_SYSCLKREQ4RFCLKBUF;
  122. else
  123. last = AB8500_SYSCLKREQ8RFCLKBUF;
  124. for (i = AB8500_SYSCLKREQ1RFCLKBUF; i <= last; i++) {
  125. j = i - AB8500_SYSCLKREQ1RFCLKBUF;
  126. ret = ab8500_sysctrl_write(i, 0xff,
  127. pdata->initial_req_buf_config[j]);
  128. dev_dbg(&pdev->dev,
  129. "Setting SysClkReq%dRfClkBuf 0x%X\n",
  130. j + 1,
  131. pdata->initial_req_buf_config[j]);
  132. if (ret < 0) {
  133. dev_err(&pdev->dev,
  134. "unable to set sysClkReq%dRfClkBuf: "
  135. "%d\n", j + 1, ret);
  136. }
  137. }
  138. }
  139. return 0;
  140. }
  141. static int ab8500_sysctrl_remove(struct platform_device *pdev)
  142. {
  143. sysctrl_dev = NULL;
  144. if (pm_power_off == ab8500_power_off)
  145. pm_power_off = NULL;
  146. return 0;
  147. }
  148. static struct platform_driver ab8500_sysctrl_driver = {
  149. .driver = {
  150. .name = "ab8500-sysctrl",
  151. },
  152. .probe = ab8500_sysctrl_probe,
  153. .remove = ab8500_sysctrl_remove,
  154. };
  155. static int __init ab8500_sysctrl_init(void)
  156. {
  157. return platform_driver_register(&ab8500_sysctrl_driver);
  158. }
  159. arch_initcall(ab8500_sysctrl_init);
  160. MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com");
  161. MODULE_DESCRIPTION("AB8500 system control driver");
  162. MODULE_LICENSE("GPL v2");