ab8500-sysctrl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * AB8500 system control driver
  3. *
  4. * Copyright (C) ST-Ericsson SA 2010
  5. * Author: Mattias Nilsson <mattias.i.nilsson@stericsson.com> for ST Ericsson.
  6. * License terms: GNU General Public License (GPL) version 2
  7. */
  8. #include <linux/err.h>
  9. #include <linux/init.h>
  10. #include <linux/export.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm.h>
  13. #include <linux/reboot.h>
  14. #include <linux/signal.h>
  15. #include <linux/power_supply.h>
  16. #include <linux/mfd/abx500.h>
  17. #include <linux/mfd/abx500/ab8500.h>
  18. #include <linux/mfd/abx500/ab8500-sysctrl.h>
  19. /* RtcCtrl bits */
  20. #define AB8500_ALARM_MIN_LOW 0x08
  21. #define AB8500_ALARM_MIN_MID 0x09
  22. #define RTC_CTRL 0x0B
  23. #define RTC_ALARM_ENABLE 0x4
  24. static struct device *sysctrl_dev;
  25. static void ab8500_power_off(void)
  26. {
  27. sigset_t old;
  28. sigset_t all;
  29. static const char * const pss[] = {"ab8500_ac", "pm2301", "ab8500_usb"};
  30. int i;
  31. bool charger_present = false;
  32. union power_supply_propval val;
  33. struct power_supply *psy;
  34. int ret;
  35. if (sysctrl_dev == NULL) {
  36. pr_err("%s: sysctrl not initialized\n", __func__);
  37. return;
  38. }
  39. /*
  40. * If we have a charger connected and we're powering off,
  41. * reboot into charge-only mode.
  42. */
  43. for (i = 0; i < ARRAY_SIZE(pss); i++) {
  44. psy = power_supply_get_by_name(pss[i]);
  45. if (!psy)
  46. continue;
  47. ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
  48. &val);
  49. power_supply_put(psy);
  50. if (!ret && val.intval) {
  51. charger_present = true;
  52. break;
  53. }
  54. }
  55. if (!charger_present)
  56. goto shutdown;
  57. /* Check if battery is known */
  58. psy = power_supply_get_by_name("ab8500_btemp");
  59. if (psy) {
  60. ret = power_supply_get_property(psy,
  61. POWER_SUPPLY_PROP_TECHNOLOGY, &val);
  62. if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) {
  63. pr_info("Charger '%s' is connected with known battery",
  64. pss[i]);
  65. pr_info(" - Rebooting.\n");
  66. machine_restart("charging");
  67. }
  68. power_supply_put(psy);
  69. }
  70. shutdown:
  71. sigfillset(&all);
  72. if (!sigprocmask(SIG_BLOCK, &all, &old)) {
  73. (void)ab8500_sysctrl_set(AB8500_STW4500CTRL1,
  74. AB8500_STW4500CTRL1_SWOFF |
  75. AB8500_STW4500CTRL1_SWRESET4500N);
  76. (void)sigprocmask(SIG_SETMASK, &old, NULL);
  77. }
  78. }
  79. static inline bool valid_bank(u8 bank)
  80. {
  81. return ((bank == AB8500_SYS_CTRL1_BLOCK) ||
  82. (bank == AB8500_SYS_CTRL2_BLOCK));
  83. }
  84. int ab8500_sysctrl_read(u16 reg, u8 *value)
  85. {
  86. u8 bank;
  87. if (sysctrl_dev == NULL)
  88. return -EPROBE_DEFER;
  89. bank = (reg >> 8);
  90. if (!valid_bank(bank))
  91. return -EINVAL;
  92. return abx500_get_register_interruptible(sysctrl_dev, bank,
  93. (u8)(reg & 0xFF), value);
  94. }
  95. EXPORT_SYMBOL(ab8500_sysctrl_read);
  96. int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
  97. {
  98. u8 bank;
  99. if (sysctrl_dev == NULL)
  100. return -EPROBE_DEFER;
  101. bank = (reg >> 8);
  102. if (!valid_bank(bank)) {
  103. pr_err("invalid bank\n");
  104. return -EINVAL;
  105. }
  106. return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
  107. (u8)(reg & 0xFF), mask, value);
  108. }
  109. EXPORT_SYMBOL(ab8500_sysctrl_write);
  110. static int ab8500_sysctrl_probe(struct platform_device *pdev)
  111. {
  112. sysctrl_dev = &pdev->dev;
  113. if (!pm_power_off)
  114. pm_power_off = ab8500_power_off;
  115. return 0;
  116. }
  117. static int ab8500_sysctrl_remove(struct platform_device *pdev)
  118. {
  119. sysctrl_dev = NULL;
  120. if (pm_power_off == ab8500_power_off)
  121. pm_power_off = NULL;
  122. return 0;
  123. }
  124. static const struct of_device_id ab8500_sysctrl_match[] = {
  125. { .compatible = "stericsson,ab8500-sysctrl", },
  126. {}
  127. };
  128. static struct platform_driver ab8500_sysctrl_driver = {
  129. .driver = {
  130. .name = "ab8500-sysctrl",
  131. .of_match_table = ab8500_sysctrl_match,
  132. },
  133. .probe = ab8500_sysctrl_probe,
  134. .remove = ab8500_sysctrl_remove,
  135. };
  136. static int __init ab8500_sysctrl_init(void)
  137. {
  138. return platform_driver_register(&ab8500_sysctrl_driver);
  139. }
  140. arch_initcall(ab8500_sysctrl_init);