ab8500-sysctrl.c 3.8 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. 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. EXPORT_SYMBOL(ab8500_sysctrl_read);
  82. int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
  83. {
  84. u8 bank;
  85. if (sysctrl_dev == NULL)
  86. return -EAGAIN;
  87. bank = (reg >> 8);
  88. if (!valid_bank(bank))
  89. return -EINVAL;
  90. return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
  91. (u8)(reg & 0xFF), mask, value);
  92. }
  93. EXPORT_SYMBOL(ab8500_sysctrl_write);
  94. static int ab8500_sysctrl_probe(struct platform_device *pdev)
  95. {
  96. struct ab8500_platform_data *plat;
  97. struct ab8500_sysctrl_platform_data *pdata;
  98. sysctrl_dev = &pdev->dev;
  99. plat = dev_get_platdata(pdev->dev.parent);
  100. if (plat->pm_power_off)
  101. pm_power_off = ab8500_power_off;
  102. pdata = plat->sysctrl;
  103. if (pdata) {
  104. int ret, i, j;
  105. for (i = AB8500_SYSCLKREQ1RFCLKBUF;
  106. i <= AB8500_SYSCLKREQ8RFCLKBUF; i++) {
  107. j = i - AB8500_SYSCLKREQ1RFCLKBUF;
  108. ret = ab8500_sysctrl_write(i, 0xff,
  109. pdata->initial_req_buf_config[j]);
  110. dev_dbg(&pdev->dev,
  111. "Setting SysClkReq%dRfClkBuf 0x%X\n",
  112. j + 1,
  113. pdata->initial_req_buf_config[j]);
  114. if (ret < 0) {
  115. dev_err(&pdev->dev,
  116. "unable to set sysClkReq%dRfClkBuf: "
  117. "%d\n", j + 1, ret);
  118. }
  119. }
  120. }
  121. return 0;
  122. }
  123. static int ab8500_sysctrl_remove(struct platform_device *pdev)
  124. {
  125. sysctrl_dev = NULL;
  126. return 0;
  127. }
  128. static struct platform_driver ab8500_sysctrl_driver = {
  129. .driver = {
  130. .name = "ab8500-sysctrl",
  131. .owner = THIS_MODULE,
  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. subsys_initcall(ab8500_sysctrl_init);
  141. MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com");
  142. MODULE_DESCRIPTION("AB8500 system control driver");
  143. MODULE_LICENSE("GPL v2");