Commit | Line | Data |
---|---|---|
90550d19 MN |
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 | ||
7 | #include <linux/err.h> | |
4e36dd33 | 8 | #include <linux/module.h> |
90550d19 | 9 | #include <linux/platform_device.h> |
379749c4 | 10 | #include <linux/pm.h> |
7c34d7c2 | 11 | #include <linux/reboot.h> |
379749c4 | 12 | #include <linux/signal.h> |
7c34d7c2 | 13 | #include <linux/power_supply.h> |
90550d19 | 14 | #include <linux/mfd/abx500.h> |
ee66e653 LW |
15 | #include <linux/mfd/abx500/ab8500.h> |
16 | #include <linux/mfd/abx500/ab8500-sysctrl.h> | |
90550d19 | 17 | |
75932094 LJ |
18 | /* 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 | ||
90550d19 MN |
24 | static struct device *sysctrl_dev; |
25 | ||
3d2088a1 | 26 | static void ab8500_power_off(void) |
379749c4 LJ |
27 | { |
28 | sigset_t old; | |
29 | sigset_t all; | |
63b4fd75 | 30 | static const char * const pss[] = {"ab8500_ac", "pm2301", "ab8500_usb"}; |
7c34d7c2 | 31 | int i; |
0903940d JA |
32 | bool charger_present = false; |
33 | union power_supply_propval val; | |
34 | struct power_supply *psy; | |
35 | int ret; | |
7c34d7c2 | 36 | |
2377e52f MD |
37 | if (sysctrl_dev == NULL) { |
38 | pr_err("%s: sysctrl not initialized\n", __func__); | |
39 | return; | |
40 | } | |
41 | ||
7c34d7c2 JA |
42 | /* |
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++) { | |
7c34d7c2 JA |
48 | psy = power_supply_get_by_name(pss[i]); |
49 | if (!psy) | |
50 | continue; | |
0903940d | 51 | |
091e73a2 KK |
52 | ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE, |
53 | &val); | |
6ff9d25b | 54 | power_supply_put(psy); |
7c34d7c2 JA |
55 | |
56 | if (!ret && val.intval) { | |
0903940d JA |
57 | 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) { | |
091e73a2 KK |
68 | ret = power_supply_get_property(psy, |
69 | POWER_SUPPLY_PROP_TECHNOLOGY, &val); | |
0903940d | 70 | if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) { |
63b4fd75 LJ |
71 | pr_info("Charger '%s' is connected with known battery", |
72 | pss[i]); | |
73 | pr_info(" - Rebooting.\n"); | |
5a4bac6e | 74 | machine_restart("charging"); |
7c34d7c2 | 75 | } |
6ff9d25b | 76 | power_supply_put(psy); |
7c34d7c2 | 77 | } |
379749c4 | 78 | |
0903940d | 79 | shutdown: |
379749c4 LJ |
80 | 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 | ||
90550d19 MN |
90 | static inline bool valid_bank(u8 bank) |
91 | { | |
92 | return ((bank == AB8500_SYS_CTRL1_BLOCK) || | |
93 | (bank == AB8500_SYS_CTRL2_BLOCK)); | |
94 | } | |
95 | ||
96 | int ab8500_sysctrl_read(u16 reg, u8 *value) | |
97 | { | |
98 | u8 bank; | |
99 | ||
100 | if (sysctrl_dev == NULL) | |
2377e52f | 101 | return -EINVAL; |
90550d19 MN |
102 | |
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 | } | |
c73db9f7 | 110 | EXPORT_SYMBOL(ab8500_sysctrl_read); |
90550d19 MN |
111 | |
112 | int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value) | |
113 | { | |
114 | u8 bank; | |
115 | ||
116 | if (sysctrl_dev == NULL) | |
2377e52f | 117 | return -EINVAL; |
90550d19 MN |
118 | |
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 | } | |
c73db9f7 | 126 | EXPORT_SYMBOL(ab8500_sysctrl_write); |
90550d19 | 127 | |
f791be49 | 128 | static int ab8500_sysctrl_probe(struct platform_device *pdev) |
90550d19 | 129 | { |
9f7af61a FB |
130 | sysctrl_dev = &pdev->dev; |
131 | ||
0b8ebdb1 | 132 | if (!pm_power_off) |
379749c4 | 133 | pm_power_off = ab8500_power_off; |
1abf063f | 134 | |
90550d19 MN |
135 | return 0; |
136 | } | |
137 | ||
4740f73f | 138 | static int ab8500_sysctrl_remove(struct platform_device *pdev) |
90550d19 MN |
139 | { |
140 | sysctrl_dev = NULL; | |
0b8ebdb1 FB |
141 | |
142 | if (pm_power_off == ab8500_power_off) | |
143 | pm_power_off = NULL; | |
144 | ||
90550d19 MN |
145 | return 0; |
146 | } | |
147 | ||
148 | static struct platform_driver ab8500_sysctrl_driver = { | |
149 | .driver = { | |
150 | .name = "ab8500-sysctrl", | |
90550d19 MN |
151 | }, |
152 | .probe = ab8500_sysctrl_probe, | |
84449216 | 153 | .remove = ab8500_sysctrl_remove, |
90550d19 MN |
154 | }; |
155 | ||
156 | static int __init ab8500_sysctrl_init(void) | |
157 | { | |
158 | return platform_driver_register(&ab8500_sysctrl_driver); | |
159 | } | |
caa62d64 | 160 | arch_initcall(ab8500_sysctrl_init); |
90550d19 MN |
161 | |
162 | MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com"); | |
163 | MODULE_DESCRIPTION("AB8500 system control driver"); | |
164 | MODULE_LICENSE("GPL v2"); |