ab8500-sysctrl.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 const char * const 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. pr_info("Charger '%s' is connected with known battery",
  61. pss[i]);
  62. pr_info(" - Rebooting.\n");
  63. machine_restart("charging");
  64. }
  65. power_supply_put(psy);
  66. }
  67. shutdown:
  68. sigfillset(&all);
  69. if (!sigprocmask(SIG_BLOCK, &all, &old)) {
  70. (void)ab8500_sysctrl_set(AB8500_STW4500CTRL1,
  71. AB8500_STW4500CTRL1_SWOFF |
  72. AB8500_STW4500CTRL1_SWRESET4500N);
  73. (void)sigprocmask(SIG_SETMASK, &old, NULL);
  74. }
  75. }
  76. static inline bool valid_bank(u8 bank)
  77. {
  78. return ((bank == AB8500_SYS_CTRL1_BLOCK) ||
  79. (bank == AB8500_SYS_CTRL2_BLOCK));
  80. }
  81. int ab8500_sysctrl_read(u16 reg, u8 *value)
  82. {
  83. u8 bank;
  84. if (sysctrl_dev == NULL)
  85. return -EPROBE_DEFER;
  86. bank = (reg >> 8);
  87. if (!valid_bank(bank))
  88. return -EINVAL;
  89. return abx500_get_register_interruptible(sysctrl_dev, bank,
  90. (u8)(reg & 0xFF), value);
  91. }
  92. EXPORT_SYMBOL(ab8500_sysctrl_read);
  93. int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
  94. {
  95. u8 bank;
  96. if (sysctrl_dev == NULL)
  97. return -EPROBE_DEFER;
  98. bank = (reg >> 8);
  99. if (!valid_bank(bank)) {
  100. pr_err("invalid bank\n");
  101. return -EINVAL;
  102. }
  103. return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
  104. (u8)(reg & 0xFF), mask, value);
  105. }
  106. EXPORT_SYMBOL(ab8500_sysctrl_write);
  107. static int ab8500_sysctrl_probe(struct platform_device *pdev)
  108. {
  109. sysctrl_dev = &pdev->dev;
  110. if (!pm_power_off)
  111. pm_power_off = ab8500_power_off;
  112. return 0;
  113. }
  114. static int ab8500_sysctrl_remove(struct platform_device *pdev)
  115. {
  116. sysctrl_dev = NULL;
  117. if (pm_power_off == ab8500_power_off)
  118. pm_power_off = NULL;
  119. return 0;
  120. }
  121. static const struct of_device_id ab8500_sysctrl_match[] = {
  122. { .compatible = "stericsson,ab8500-sysctrl", },
  123. {}
  124. };
  125. static struct platform_driver ab8500_sysctrl_driver = {
  126. .driver = {
  127. .name = "ab8500-sysctrl",
  128. .of_match_table = ab8500_sysctrl_match,
  129. },
  130. .probe = ab8500_sysctrl_probe,
  131. .remove = ab8500_sysctrl_remove,
  132. };
  133. static int __init ab8500_sysctrl_init(void)
  134. {
  135. return platform_driver_register(&ab8500_sysctrl_driver);
  136. }
  137. arch_initcall(ab8500_sysctrl_init);
  138. MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com");
  139. MODULE_DESCRIPTION("AB8500 system control driver");
  140. MODULE_LICENSE("GPL v2");