Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * AB8500 system control driver
4 *
5 * Copyright (C) ST-Ericsson SA 2010
6 * Author: Mattias Nilsson <mattias.i.nilsson@stericsson.com> for ST Ericsson.
7 */
8
9#include <linux/err.h>
10#include <linux/init.h>
11#include <linux/export.h>
12#include <linux/platform_device.h>
13#include <linux/pm.h>
14#include <linux/reboot.h>
15#include <linux/signal.h>
16#include <linux/power_supply.h>
17#include <linux/mfd/abx500.h>
18#include <linux/mfd/abx500/ab8500.h>
19#include <linux/mfd/abx500/ab8500-sysctrl.h>
20
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
27static struct device *sysctrl_dev;
28
29static void ab8500_power_off(void)
30{
31 sigset_t old;
32 sigset_t all;
33 static const char * const pss[] = {"ab8500_ac", "ab8500_usb"};
34 int i;
35 bool charger_present = false;
36 union power_supply_propval val;
37 struct power_supply *psy;
38 int ret;
39
40 if (sysctrl_dev == NULL) {
41 pr_err("%s: sysctrl not initialized\n", __func__);
42 return;
43 }
44
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++) {
51 psy = power_supply_get_by_name(pss[i]);
52 if (!psy)
53 continue;
54
55 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
56 &val);
57 power_supply_put(psy);
58
59 if (!ret && val.intval) {
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) {
71 ret = power_supply_get_property(psy,
72 POWER_SUPPLY_PROP_TECHNOLOGY, &val);
73 if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) {
74 pr_info("Charger '%s' is connected with known battery",
75 pss[i]);
76 pr_info(" - Rebooting.\n");
77 machine_restart("charging");
78 }
79 power_supply_put(psy);
80 }
81
82shutdown:
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
93static inline bool valid_bank(u8 bank)
94{
95 return ((bank == AB8500_SYS_CTRL1_BLOCK) ||
96 (bank == AB8500_SYS_CTRL2_BLOCK));
97}
98
99int ab8500_sysctrl_read(u16 reg, u8 *value)
100{
101 u8 bank;
102
103 if (sysctrl_dev == NULL)
104 return -EPROBE_DEFER;
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}
113EXPORT_SYMBOL(ab8500_sysctrl_read);
114
115int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
116{
117 u8 bank;
118
119 if (sysctrl_dev == NULL)
120 return -EPROBE_DEFER;
121
122 bank = (reg >> 8);
123 if (!valid_bank(bank)) {
124 pr_err("invalid bank\n");
125 return -EINVAL;
126 }
127
128 return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
129 (u8)(reg & 0xFF), mask, value);
130}
131EXPORT_SYMBOL(ab8500_sysctrl_write);
132
133static int ab8500_sysctrl_probe(struct platform_device *pdev)
134{
135 sysctrl_dev = &pdev->dev;
136
137 if (!pm_power_off)
138 pm_power_off = ab8500_power_off;
139
140 return 0;
141}
142
143static void ab8500_sysctrl_remove(struct platform_device *pdev)
144{
145 sysctrl_dev = NULL;
146
147 if (pm_power_off == ab8500_power_off)
148 pm_power_off = NULL;
149}
150
151static const struct of_device_id ab8500_sysctrl_match[] = {
152 { .compatible = "stericsson,ab8500-sysctrl", },
153 {}
154};
155
156static struct platform_driver ab8500_sysctrl_driver = {
157 .driver = {
158 .name = "ab8500-sysctrl",
159 .of_match_table = ab8500_sysctrl_match,
160 },
161 .probe = ab8500_sysctrl_probe,
162 .remove_new = ab8500_sysctrl_remove,
163};
164
165static int __init ab8500_sysctrl_init(void)
166{
167 return platform_driver_register(&ab8500_sysctrl_driver);
168}
169arch_initcall(ab8500_sysctrl_init);
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>
8#include <linux/module.h>
9#include <linux/platform_device.h>
10#include <linux/pm.h>
11#include <linux/reboot.h>
12#include <linux/signal.h>
13#include <linux/power_supply.h>
14#include <linux/mfd/abx500.h>
15#include <linux/mfd/abx500/ab8500.h>
16#include <linux/mfd/abx500/ab8500-sysctrl.h>
17
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
24static struct device *sysctrl_dev;
25
26static void ab8500_power_off(void)
27{
28 sigset_t old;
29 sigset_t all;
30 static const char * const pss[] = {"ab8500_ac", "pm2301", "ab8500_usb"};
31 int i;
32 bool charger_present = false;
33 union power_supply_propval val;
34 struct power_supply *psy;
35 int ret;
36
37 if (sysctrl_dev == NULL) {
38 pr_err("%s: sysctrl not initialized\n", __func__);
39 return;
40 }
41
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++) {
48 psy = power_supply_get_by_name(pss[i]);
49 if (!psy)
50 continue;
51
52 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
53 &val);
54 power_supply_put(psy);
55
56 if (!ret && val.intval) {
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) {
68 ret = power_supply_get_property(psy,
69 POWER_SUPPLY_PROP_TECHNOLOGY, &val);
70 if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) {
71 pr_info("Charger '%s' is connected with known battery",
72 pss[i]);
73 pr_info(" - Rebooting.\n");
74 machine_restart("charging");
75 }
76 power_supply_put(psy);
77 }
78
79shutdown:
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
90static 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)
101 return -EINVAL;
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}
110EXPORT_SYMBOL(ab8500_sysctrl_read);
111
112int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
113{
114 u8 bank;
115
116 if (sysctrl_dev == NULL)
117 return -EINVAL;
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}
126EXPORT_SYMBOL(ab8500_sysctrl_write);
127
128static int ab8500_sysctrl_probe(struct platform_device *pdev)
129{
130 struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent);
131 struct ab8500_platform_data *plat;
132 struct ab8500_sysctrl_platform_data *pdata;
133
134 plat = dev_get_platdata(pdev->dev.parent);
135
136 if (!plat)
137 return -EINVAL;
138
139 sysctrl_dev = &pdev->dev;
140
141 if (!pm_power_off)
142 pm_power_off = ab8500_power_off;
143
144 pdata = plat->sysctrl;
145 if (pdata) {
146 int last, ret, i, j;
147
148 if (is_ab8505(ab8500))
149 last = AB8500_SYSCLKREQ4RFCLKBUF;
150 else
151 last = AB8500_SYSCLKREQ8RFCLKBUF;
152
153 for (i = AB8500_SYSCLKREQ1RFCLKBUF; i <= last; i++) {
154 j = i - AB8500_SYSCLKREQ1RFCLKBUF;
155 ret = ab8500_sysctrl_write(i, 0xff,
156 pdata->initial_req_buf_config[j]);
157 dev_dbg(&pdev->dev,
158 "Setting SysClkReq%dRfClkBuf 0x%X\n",
159 j + 1,
160 pdata->initial_req_buf_config[j]);
161 if (ret < 0) {
162 dev_err(&pdev->dev,
163 "Can't set sysClkReq%dRfClkBuf: %d\n",
164 j + 1, ret);
165 }
166 }
167 }
168
169 return 0;
170}
171
172static int ab8500_sysctrl_remove(struct platform_device *pdev)
173{
174 sysctrl_dev = NULL;
175
176 if (pm_power_off == ab8500_power_off)
177 pm_power_off = NULL;
178
179 return 0;
180}
181
182static struct platform_driver ab8500_sysctrl_driver = {
183 .driver = {
184 .name = "ab8500-sysctrl",
185 },
186 .probe = ab8500_sysctrl_probe,
187 .remove = ab8500_sysctrl_remove,
188};
189
190static int __init ab8500_sysctrl_init(void)
191{
192 return platform_driver_register(&ab8500_sysctrl_driver);
193}
194arch_initcall(ab8500_sysctrl_init);
195
196MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com");
197MODULE_DESCRIPTION("AB8500 system control driver");
198MODULE_LICENSE("GPL v2");