ab8500-sysctrl.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. static struct device *sysctrl_dev;
  17. void ab8500_power_off(void)
  18. {
  19. sigset_t old;
  20. sigset_t all;
  21. static char *pss[] = {"ab8500_ac", "ab8500_usb"};
  22. int i;
  23. bool charger_present = false;
  24. union power_supply_propval val;
  25. struct power_supply *psy;
  26. int ret;
  27. /*
  28. * If we have a charger connected and we're powering off,
  29. * reboot into charge-only mode.
  30. */
  31. for (i = 0; i < ARRAY_SIZE(pss); i++) {
  32. psy = power_supply_get_by_name(pss[i]);
  33. if (!psy)
  34. continue;
  35. ret = psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &val);
  36. if (!ret && val.intval) {
  37. charger_present = true;
  38. break;
  39. }
  40. }
  41. if (!charger_present)
  42. goto shutdown;
  43. /* Check if battery is known */
  44. psy = power_supply_get_by_name("ab8500_btemp");
  45. if (psy) {
  46. ret = psy->get_property(psy, POWER_SUPPLY_PROP_TECHNOLOGY,
  47. &val);
  48. if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) {
  49. printk(KERN_INFO
  50. "Charger \"%s\" is connected with known battery."
  51. " Rebooting.\n",
  52. pss[i]);
  53. machine_restart("charging");
  54. }
  55. }
  56. shutdown:
  57. sigfillset(&all);
  58. if (!sigprocmask(SIG_BLOCK, &all, &old)) {
  59. (void)ab8500_sysctrl_set(AB8500_STW4500CTRL1,
  60. AB8500_STW4500CTRL1_SWOFF |
  61. AB8500_STW4500CTRL1_SWRESET4500N);
  62. (void)sigprocmask(SIG_SETMASK, &old, NULL);
  63. }
  64. }
  65. static inline bool valid_bank(u8 bank)
  66. {
  67. return ((bank == AB8500_SYS_CTRL1_BLOCK) ||
  68. (bank == AB8500_SYS_CTRL2_BLOCK));
  69. }
  70. int ab8500_sysctrl_read(u16 reg, u8 *value)
  71. {
  72. u8 bank;
  73. if (sysctrl_dev == NULL)
  74. return -EAGAIN;
  75. bank = (reg >> 8);
  76. if (!valid_bank(bank))
  77. return -EINVAL;
  78. return abx500_get_register_interruptible(sysctrl_dev, bank,
  79. (u8)(reg & 0xFF), value);
  80. }
  81. int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
  82. {
  83. u8 bank;
  84. if (sysctrl_dev == NULL)
  85. return -EAGAIN;
  86. bank = (reg >> 8);
  87. if (!valid_bank(bank))
  88. return -EINVAL;
  89. return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
  90. (u8)(reg & 0xFF), mask, value);
  91. }
  92. static int ab8500_sysctrl_probe(struct platform_device *pdev)
  93. {
  94. struct ab8500_platform_data *plat;
  95. struct ab8500_sysctrl_platform_data *pdata;
  96. sysctrl_dev = &pdev->dev;
  97. plat = dev_get_platdata(pdev->dev.parent);
  98. if (plat->pm_power_off)
  99. pm_power_off = ab8500_power_off;
  100. pdata = plat->sysctrl;
  101. if (pdata) {
  102. int ret, i, j;
  103. for (i = AB8500_SYSCLKREQ1RFCLKBUF;
  104. i <= AB8500_SYSCLKREQ8RFCLKBUF; i++) {
  105. j = i - AB8500_SYSCLKREQ1RFCLKBUF;
  106. ret = ab8500_sysctrl_write(i, 0xff,
  107. pdata->initial_req_buf_config[j]);
  108. dev_dbg(&pdev->dev,
  109. "Setting SysClkReq%dRfClkBuf 0x%X\n",
  110. j + 1,
  111. pdata->initial_req_buf_config[j]);
  112. if (ret < 0) {
  113. dev_err(&pdev->dev,
  114. "unable to set sysClkReq%dRfClkBuf: "
  115. "%d\n", j + 1, ret);
  116. }
  117. }
  118. }
  119. return 0;
  120. }
  121. static int ab8500_sysctrl_remove(struct platform_device *pdev)
  122. {
  123. sysctrl_dev = NULL;
  124. return 0;
  125. }
  126. static struct platform_driver ab8500_sysctrl_driver = {
  127. .driver = {
  128. .name = "ab8500-sysctrl",
  129. .owner = THIS_MODULE,
  130. },
  131. .probe = ab8500_sysctrl_probe,
  132. .remove = ab8500_sysctrl_remove,
  133. };
  134. static int __init ab8500_sysctrl_init(void)
  135. {
  136. return platform_driver_register(&ab8500_sysctrl_driver);
  137. }
  138. subsys_initcall(ab8500_sysctrl_init);
  139. MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com");
  140. MODULE_DESCRIPTION("AB8500 system control driver");
  141. MODULE_LICENSE("GPL v2");