]>
Commit | Line | Data |
---|---|---|
90550d19 | 1 | /* |
dac94efa PG |
2 | * AB8500 system control driver |
3 | * | |
90550d19 MN |
4 | * Copyright (C) ST-Ericsson SA 2010 |
5 | * Author: Mattias Nilsson <[email protected]> for ST Ericsson. | |
6 | * License terms: GNU General Public License (GPL) version 2 | |
7 | */ | |
8 | ||
9 | #include <linux/err.h> | |
dac94efa PG |
10 | #include <linux/init.h> |
11 | #include <linux/export.h> | |
90550d19 | 12 | #include <linux/platform_device.h> |
379749c4 | 13 | #include <linux/pm.h> |
7c34d7c2 | 14 | #include <linux/reboot.h> |
379749c4 | 15 | #include <linux/signal.h> |
7c34d7c2 | 16 | #include <linux/power_supply.h> |
90550d19 | 17 | #include <linux/mfd/abx500.h> |
ee66e653 LW |
18 | #include <linux/mfd/abx500/ab8500.h> |
19 | #include <linux/mfd/abx500/ab8500-sysctrl.h> | |
90550d19 | 20 | |
75932094 LJ |
21 | /* RtcCtrl bits */ |
22 | #define AB8500_ALARM_MIN_LOW 0x08 | |
23 | #define AB8500_ALARM_MIN_MID 0x09 | |
24 | #define RTC_CTRL 0x0B | |
25 | #define RTC_ALARM_ENABLE 0x4 | |
26 | ||
90550d19 MN |
27 | static struct device *sysctrl_dev; |
28 | ||
3d2088a1 | 29 | static void ab8500_power_off(void) |
379749c4 LJ |
30 | { |
31 | sigset_t old; | |
32 | sigset_t all; | |
63b4fd75 | 33 | static const char * const pss[] = {"ab8500_ac", "pm2301", "ab8500_usb"}; |
7c34d7c2 | 34 | int i; |
0903940d JA |
35 | bool charger_present = false; |
36 | union power_supply_propval val; | |
37 | struct power_supply *psy; | |
38 | int ret; | |
7c34d7c2 | 39 | |
2377e52f MD |
40 | if (sysctrl_dev == NULL) { |
41 | pr_err("%s: sysctrl not initialized\n", __func__); | |
42 | return; | |
43 | } | |
44 | ||
7c34d7c2 JA |
45 | /* |
46 | * If we have a charger connected and we're powering off, | |
47 | * reboot into charge-only mode. | |
48 | */ | |
49 | ||
50 | for (i = 0; i < ARRAY_SIZE(pss); i++) { | |
7c34d7c2 JA |
51 | psy = power_supply_get_by_name(pss[i]); |
52 | if (!psy) | |
53 | continue; | |
0903940d | 54 | |
091e73a2 KK |
55 | ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE, |
56 | &val); | |
6ff9d25b | 57 | power_supply_put(psy); |
7c34d7c2 JA |
58 | |
59 | if (!ret && val.intval) { | |
0903940d JA |
60 | charger_present = true; |
61 | break; | |
62 | } | |
63 | } | |
64 | ||
65 | if (!charger_present) | |
66 | goto shutdown; | |
67 | ||
68 | /* Check if battery is known */ | |
69 | psy = power_supply_get_by_name("ab8500_btemp"); | |
70 | if (psy) { | |
091e73a2 KK |
71 | ret = power_supply_get_property(psy, |
72 | POWER_SUPPLY_PROP_TECHNOLOGY, &val); | |
0903940d | 73 | if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) { |
63b4fd75 LJ |
74 | pr_info("Charger '%s' is connected with known battery", |
75 | pss[i]); | |
76 | pr_info(" - Rebooting.\n"); | |
5a4bac6e | 77 | machine_restart("charging"); |
7c34d7c2 | 78 | } |
6ff9d25b | 79 | power_supply_put(psy); |
7c34d7c2 | 80 | } |
379749c4 | 81 | |
0903940d | 82 | shutdown: |
379749c4 LJ |
83 | sigfillset(&all); |
84 | ||
85 | if (!sigprocmask(SIG_BLOCK, &all, &old)) { | |
86 | (void)ab8500_sysctrl_set(AB8500_STW4500CTRL1, | |
87 | AB8500_STW4500CTRL1_SWOFF | | |
88 | AB8500_STW4500CTRL1_SWRESET4500N); | |
89 | (void)sigprocmask(SIG_SETMASK, &old, NULL); | |
90 | } | |
91 | } | |
92 | ||
90550d19 MN |
93 | static inline bool valid_bank(u8 bank) |
94 | { | |
95 | return ((bank == AB8500_SYS_CTRL1_BLOCK) || | |
96 | (bank == AB8500_SYS_CTRL2_BLOCK)); | |
97 | } | |
98 | ||
99 | int ab8500_sysctrl_read(u16 reg, u8 *value) | |
100 | { | |
101 | u8 bank; | |
102 | ||
103 | if (sysctrl_dev == NULL) | |
7e9c40c6 | 104 | return -EPROBE_DEFER; |
90550d19 MN |
105 | |
106 | bank = (reg >> 8); | |
107 | if (!valid_bank(bank)) | |
108 | return -EINVAL; | |
109 | ||
110 | return abx500_get_register_interruptible(sysctrl_dev, bank, | |
111 | (u8)(reg & 0xFF), value); | |
112 | } | |
c73db9f7 | 113 | EXPORT_SYMBOL(ab8500_sysctrl_read); |
90550d19 MN |
114 | |
115 | int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value) | |
116 | { | |
117 | u8 bank; | |
118 | ||
119 | if (sysctrl_dev == NULL) | |
7e9c40c6 | 120 | return -EPROBE_DEFER; |
90550d19 MN |
121 | |
122 | bank = (reg >> 8); | |
7e9c40c6 LW |
123 | if (!valid_bank(bank)) { |
124 | pr_err("invalid bank\n"); | |
90550d19 | 125 | return -EINVAL; |
7e9c40c6 | 126 | } |
90550d19 MN |
127 | |
128 | return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank, | |
129 | (u8)(reg & 0xFF), mask, value); | |
130 | } | |
c73db9f7 | 131 | EXPORT_SYMBOL(ab8500_sysctrl_write); |
90550d19 | 132 | |
f791be49 | 133 | static int ab8500_sysctrl_probe(struct platform_device *pdev) |
90550d19 | 134 | { |
9f7af61a FB |
135 | sysctrl_dev = &pdev->dev; |
136 | ||
0b8ebdb1 | 137 | if (!pm_power_off) |
379749c4 | 138 | pm_power_off = ab8500_power_off; |
1abf063f | 139 | |
90550d19 MN |
140 | return 0; |
141 | } | |
142 | ||
4740f73f | 143 | static int ab8500_sysctrl_remove(struct platform_device *pdev) |
90550d19 MN |
144 | { |
145 | sysctrl_dev = NULL; | |
0b8ebdb1 FB |
146 | |
147 | if (pm_power_off == ab8500_power_off) | |
148 | pm_power_off = NULL; | |
149 | ||
90550d19 MN |
150 | return 0; |
151 | } | |
152 | ||
7e9c40c6 LW |
153 | static const struct of_device_id ab8500_sysctrl_match[] = { |
154 | { .compatible = "stericsson,ab8500-sysctrl", }, | |
155 | {} | |
156 | }; | |
157 | ||
90550d19 MN |
158 | static struct platform_driver ab8500_sysctrl_driver = { |
159 | .driver = { | |
160 | .name = "ab8500-sysctrl", | |
7e9c40c6 | 161 | .of_match_table = ab8500_sysctrl_match, |
90550d19 MN |
162 | }, |
163 | .probe = ab8500_sysctrl_probe, | |
84449216 | 164 | .remove = ab8500_sysctrl_remove, |
90550d19 MN |
165 | }; |
166 | ||
167 | static int __init ab8500_sysctrl_init(void) | |
168 | { | |
169 | return platform_driver_register(&ab8500_sysctrl_driver); | |
170 | } | |
caa62d64 | 171 | arch_initcall(ab8500_sysctrl_init); |