blob: 207cc497958a855c0abe012bbee749dfac650f93 [file] [log] [blame]
Mattias Nilsson90550d12011-02-14 11:17:12 +01001/*
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
7#include <linux/err.h>
Paul Gortmaker4e36dd32011-07-03 15:13:27 -04008#include <linux/module.h>
Mattias Nilsson90550d12011-02-14 11:17:12 +01009#include <linux/platform_device.h>
Lee Jones379749c2013-01-14 13:26:15 +000010#include <linux/pm.h>
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020011#include <linux/reboot.h>
Lee Jones379749c2013-01-14 13:26:15 +000012#include <linux/signal.h>
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020013#include <linux/power_supply.h>
Mattias Nilsson90550d12011-02-14 11:17:12 +010014#include <linux/mfd/abx500.h>
Linus Walleijee66e652011-12-02 14:16:33 +010015#include <linux/mfd/abx500/ab8500.h>
16#include <linux/mfd/abx500/ab8500-sysctrl.h>
Mattias Nilsson90550d12011-02-14 11:17:12 +010017
Lee Jones75932092013-02-12 15:11:19 +000018/* RtcCtrl bits */
19#define AB8500_ALARM_MIN_LOW 0x08
20#define AB8500_ALARM_MIN_MID 0x09
21#define RTC_CTRL 0x0B
22#define RTC_ALARM_ENABLE 0x4
23
Mattias Nilsson90550d12011-02-14 11:17:12 +010024static struct device *sysctrl_dev;
25
Fabio Baltieri3d2088a2013-04-26 14:17:15 +020026static void ab8500_power_off(void)
Lee Jones379749c2013-01-14 13:26:15 +000027{
28 sigset_t old;
29 sigset_t all;
Lee Jones63b4fd72015-10-28 11:11:05 +000030 static const char * const pss[] = {"ab8500_ac", "pm2301", "ab8500_usb"};
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020031 int i;
Jonas Aaberg09039402011-08-17 15:58:52 +020032 bool charger_present = false;
33 union power_supply_propval val;
34 struct power_supply *psy;
35 int ret;
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020036
Marcus Danielsson2377e522012-06-18 15:00:40 +020037 if (sysctrl_dev == NULL) {
38 pr_err("%s: sysctrl not initialized\n", __func__);
39 return;
40 }
41
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020042 /*
43 * If we have a charger connected and we're powering off,
44 * reboot into charge-only mode.
45 */
46
47 for (i = 0; i < ARRAY_SIZE(pss); i++) {
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020048 psy = power_supply_get_by_name(pss[i]);
49 if (!psy)
50 continue;
Jonas Aaberg09039402011-08-17 15:58:52 +020051
Krzysztof Kozlowski091e73a2015-03-12 08:44:07 +010052 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
53 &val);
Krzysztof Kozlowski6ff9d252015-03-12 08:44:19 +010054 power_supply_put(psy);
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020055
56 if (!ret && val.intval) {
Jonas Aaberg09039402011-08-17 15:58:52 +020057 charger_present = true;
58 break;
59 }
60 }
61
62 if (!charger_present)
63 goto shutdown;
64
65 /* Check if battery is known */
66 psy = power_supply_get_by_name("ab8500_btemp");
67 if (psy) {
Krzysztof Kozlowski091e73a2015-03-12 08:44:07 +010068 ret = power_supply_get_property(psy,
69 POWER_SUPPLY_PROP_TECHNOLOGY, &val);
Jonas Aaberg09039402011-08-17 15:58:52 +020070 if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) {
Lee Jones63b4fd72015-10-28 11:11:05 +000071 pr_info("Charger '%s' is connected with known battery",
72 pss[i]);
73 pr_info(" - Rebooting.\n");
Jonas Aaberg5a4bac62011-08-18 10:14:38 +020074 machine_restart("charging");
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020075 }
Krzysztof Kozlowski6ff9d252015-03-12 08:44:19 +010076 power_supply_put(psy);
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020077 }
Lee Jones379749c2013-01-14 13:26:15 +000078
Jonas Aaberg09039402011-08-17 15:58:52 +020079shutdown:
Lee Jones379749c2013-01-14 13:26:15 +000080 sigfillset(&all);
81
82 if (!sigprocmask(SIG_BLOCK, &all, &old)) {
83 (void)ab8500_sysctrl_set(AB8500_STW4500CTRL1,
84 AB8500_STW4500CTRL1_SWOFF |
85 AB8500_STW4500CTRL1_SWRESET4500N);
86 (void)sigprocmask(SIG_SETMASK, &old, NULL);
87 }
88}
89
Mattias Nilsson90550d12011-02-14 11:17:12 +010090static inline bool valid_bank(u8 bank)
91{
92 return ((bank == AB8500_SYS_CTRL1_BLOCK) ||
93 (bank == AB8500_SYS_CTRL2_BLOCK));
94}
95
96int ab8500_sysctrl_read(u16 reg, u8 *value)
97{
98 u8 bank;
99
100 if (sysctrl_dev == NULL)
Marcus Danielsson2377e522012-06-18 15:00:40 +0200101 return -EINVAL;
Mattias Nilsson90550d12011-02-14 11:17:12 +0100102
103 bank = (reg >> 8);
104 if (!valid_bank(bank))
105 return -EINVAL;
106
107 return abx500_get_register_interruptible(sysctrl_dev, bank,
108 (u8)(reg & 0xFF), value);
109}
Jonas Aabergc73db9f2011-11-11 07:52:10 +0100110EXPORT_SYMBOL(ab8500_sysctrl_read);
Mattias Nilsson90550d12011-02-14 11:17:12 +0100111
112int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
113{
114 u8 bank;
115
116 if (sysctrl_dev == NULL)
Marcus Danielsson2377e522012-06-18 15:00:40 +0200117 return -EINVAL;
Mattias Nilsson90550d12011-02-14 11:17:12 +0100118
119 bank = (reg >> 8);
120 if (!valid_bank(bank))
121 return -EINVAL;
122
123 return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
124 (u8)(reg & 0xFF), mask, value);
125}
Jonas Aabergc73db9f2011-11-11 07:52:10 +0100126EXPORT_SYMBOL(ab8500_sysctrl_write);
Mattias Nilsson90550d12011-02-14 11:17:12 +0100127
Bill Pembertonf791be42012-11-19 13:23:04 -0500128static int ab8500_sysctrl_probe(struct platform_device *pdev)
Mattias Nilsson90550d12011-02-14 11:17:12 +0100129{
Fabio Baltieri9f7af612013-04-26 14:17:17 +0200130 sysctrl_dev = &pdev->dev;
131
Fabio Baltieri0b8ebdb2013-05-09 13:08:08 +0200132 if (!pm_power_off)
Lee Jones379749c2013-01-14 13:26:15 +0000133 pm_power_off = ab8500_power_off;
Kennet Wallden1abf0632011-09-27 09:23:56 +0200134
Mattias Nilsson90550d12011-02-14 11:17:12 +0100135 return 0;
136}
137
Bill Pemberton4740f732012-11-19 13:26:01 -0500138static int ab8500_sysctrl_remove(struct platform_device *pdev)
Mattias Nilsson90550d12011-02-14 11:17:12 +0100139{
140 sysctrl_dev = NULL;
Fabio Baltieri0b8ebdb2013-05-09 13:08:08 +0200141
142 if (pm_power_off == ab8500_power_off)
143 pm_power_off = NULL;
144
Mattias Nilsson90550d12011-02-14 11:17:12 +0100145 return 0;
146}
147
148static struct platform_driver ab8500_sysctrl_driver = {
149 .driver = {
150 .name = "ab8500-sysctrl",
Mattias Nilsson90550d12011-02-14 11:17:12 +0100151 },
152 .probe = ab8500_sysctrl_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500153 .remove = ab8500_sysctrl_remove,
Mattias Nilsson90550d12011-02-14 11:17:12 +0100154};
155
156static int __init ab8500_sysctrl_init(void)
157{
158 return platform_driver_register(&ab8500_sysctrl_driver);
159}
Ulf Hanssoncaa62d62013-04-03 01:06:27 +0200160arch_initcall(ab8500_sysctrl_init);
Mattias Nilsson90550d12011-02-14 11:17:12 +0100161
162MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com");
163MODULE_DESCRIPTION("AB8500 system control driver");
164MODULE_LICENSE("GPL v2");