ab8500-sysctrl.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /*
  24. * If we have a charger connected and we're powering off,
  25. * reboot into charge-only mode.
  26. */
  27. for (i = 0; i < ARRAY_SIZE(pss); i++) {
  28. union power_supply_propval val;
  29. struct power_supply *psy;
  30. int ret;
  31. psy = power_supply_get_by_name(pss[i]);
  32. if (!psy)
  33. continue;
  34. ret = psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &val);
  35. if (!ret && val.intval) {
  36. printk(KERN_INFO
  37. "Charger \"%s\" is connected. Rebooting.\n",
  38. pss[i]);
  39. machine_restart(NULL);
  40. }
  41. }
  42. sigfillset(&all);
  43. if (!sigprocmask(SIG_BLOCK, &all, &old)) {
  44. (void)ab8500_sysctrl_set(AB8500_STW4500CTRL1,
  45. AB8500_STW4500CTRL1_SWOFF |
  46. AB8500_STW4500CTRL1_SWRESET4500N);
  47. (void)sigprocmask(SIG_SETMASK, &old, NULL);
  48. }
  49. }
  50. static inline bool valid_bank(u8 bank)
  51. {
  52. return ((bank == AB8500_SYS_CTRL1_BLOCK) ||
  53. (bank == AB8500_SYS_CTRL2_BLOCK));
  54. }
  55. int ab8500_sysctrl_read(u16 reg, u8 *value)
  56. {
  57. u8 bank;
  58. if (sysctrl_dev == NULL)
  59. return -EAGAIN;
  60. bank = (reg >> 8);
  61. if (!valid_bank(bank))
  62. return -EINVAL;
  63. return abx500_get_register_interruptible(sysctrl_dev, bank,
  64. (u8)(reg & 0xFF), value);
  65. }
  66. int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
  67. {
  68. u8 bank;
  69. if (sysctrl_dev == NULL)
  70. return -EAGAIN;
  71. bank = (reg >> 8);
  72. if (!valid_bank(bank))
  73. return -EINVAL;
  74. return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
  75. (u8)(reg & 0xFF), mask, value);
  76. }
  77. static int ab8500_sysctrl_probe(struct platform_device *pdev)
  78. {
  79. struct ab8500_platform_data *plat;
  80. sysctrl_dev = &pdev->dev;
  81. plat = dev_get_platdata(pdev->dev.parent);
  82. if (plat->pm_power_off)
  83. pm_power_off = ab8500_power_off;
  84. return 0;
  85. }
  86. static int ab8500_sysctrl_remove(struct platform_device *pdev)
  87. {
  88. sysctrl_dev = NULL;
  89. return 0;
  90. }
  91. static struct platform_driver ab8500_sysctrl_driver = {
  92. .driver = {
  93. .name = "ab8500-sysctrl",
  94. .owner = THIS_MODULE,
  95. },
  96. .probe = ab8500_sysctrl_probe,
  97. .remove = ab8500_sysctrl_remove,
  98. };
  99. static int __init ab8500_sysctrl_init(void)
  100. {
  101. return platform_driver_register(&ab8500_sysctrl_driver);
  102. }
  103. subsys_initcall(ab8500_sysctrl_init);
  104. MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com");
  105. MODULE_DESCRIPTION("AB8500 system control driver");
  106. MODULE_LICENSE("GPL v2");