blob: eeeb62415f538daae637fb98b35a53fb11547f6d [file] [log] [blame]
Thomas Gleixneraf873fc2019-05-28 09:57:21 -07001// SPDX-License-Identifier: GPL-2.0-only
Mattias Nilsson90550d12011-02-14 11:17:12 +01002/*
Paul Gortmakerdac94ef2016-10-29 21:24:39 -04003 * AB8500 system control driver
4 *
Mattias Nilsson90550d12011-02-14 11:17:12 +01005 * Copyright (C) ST-Ericsson SA 2010
6 * Author: Mattias Nilsson <mattias.i.nilsson@stericsson.com> for ST Ericsson.
Mattias Nilsson90550d12011-02-14 11:17:12 +01007 */
8
9#include <linux/err.h>
Paul Gortmakerdac94ef2016-10-29 21:24:39 -040010#include <linux/init.h>
11#include <linux/export.h>
Mattias Nilsson90550d12011-02-14 11:17:12 +010012#include <linux/platform_device.h>
Lee Jones379749c2013-01-14 13:26:15 +000013#include <linux/pm.h>
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020014#include <linux/reboot.h>
Lee Jones379749c2013-01-14 13:26:15 +000015#include <linux/signal.h>
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020016#include <linux/power_supply.h>
Mattias Nilsson90550d12011-02-14 11:17:12 +010017#include <linux/mfd/abx500.h>
Linus Walleijee66e652011-12-02 14:16:33 +010018#include <linux/mfd/abx500/ab8500.h>
19#include <linux/mfd/abx500/ab8500-sysctrl.h>
Mattias Nilsson90550d12011-02-14 11:17:12 +010020
Lee Jones75932092013-02-12 15:11:19 +000021/* 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
Mattias Nilsson90550d12011-02-14 11:17:12 +010027static struct device *sysctrl_dev;
28
Fabio Baltieri3d2088a2013-04-26 14:17:15 +020029static void ab8500_power_off(void)
Lee Jones379749c2013-01-14 13:26:15 +000030{
31 sigset_t old;
32 sigset_t all;
Lee Jones63b4fd72015-10-28 11:11:05 +000033 static const char * const pss[] = {"ab8500_ac", "pm2301", "ab8500_usb"};
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020034 int i;
Jonas Aaberg09039402011-08-17 15:58:52 +020035 bool charger_present = false;
36 union power_supply_propval val;
37 struct power_supply *psy;
38 int ret;
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020039
Marcus Danielsson2377e522012-06-18 15:00:40 +020040 if (sysctrl_dev == NULL) {
41 pr_err("%s: sysctrl not initialized\n", __func__);
42 return;
43 }
44
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020045 /*
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++) {
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020051 psy = power_supply_get_by_name(pss[i]);
52 if (!psy)
53 continue;
Jonas Aaberg09039402011-08-17 15:58:52 +020054
Krzysztof Kozlowski091e73a2015-03-12 08:44:07 +010055 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
56 &val);
Krzysztof Kozlowski6ff9d252015-03-12 08:44:19 +010057 power_supply_put(psy);
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020058
59 if (!ret && val.intval) {
Jonas Aaberg09039402011-08-17 15:58:52 +020060 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) {
Krzysztof Kozlowski091e73a2015-03-12 08:44:07 +010071 ret = power_supply_get_property(psy,
72 POWER_SUPPLY_PROP_TECHNOLOGY, &val);
Jonas Aaberg09039402011-08-17 15:58:52 +020073 if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) {
Lee Jones63b4fd72015-10-28 11:11:05 +000074 pr_info("Charger '%s' is connected with known battery",
75 pss[i]);
76 pr_info(" - Rebooting.\n");
Jonas Aaberg5a4bac62011-08-18 10:14:38 +020077 machine_restart("charging");
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020078 }
Krzysztof Kozlowski6ff9d252015-03-12 08:44:19 +010079 power_supply_put(psy);
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020080 }
Lee Jones379749c2013-01-14 13:26:15 +000081
Jonas Aaberg09039402011-08-17 15:58:52 +020082shutdown:
Lee Jones379749c2013-01-14 13:26:15 +000083 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
Mattias Nilsson90550d12011-02-14 11:17:12 +010093static 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)
Linus Walleij7e9c40c2017-01-13 10:53:55 +0100104 return -EPROBE_DEFER;
Mattias Nilsson90550d12011-02-14 11:17:12 +0100105
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}
Jonas Aabergc73db9f2011-11-11 07:52:10 +0100113EXPORT_SYMBOL(ab8500_sysctrl_read);
Mattias Nilsson90550d12011-02-14 11:17:12 +0100114
115int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
116{
117 u8 bank;
118
119 if (sysctrl_dev == NULL)
Linus Walleij7e9c40c2017-01-13 10:53:55 +0100120 return -EPROBE_DEFER;
Mattias Nilsson90550d12011-02-14 11:17:12 +0100121
122 bank = (reg >> 8);
Linus Walleij7e9c40c2017-01-13 10:53:55 +0100123 if (!valid_bank(bank)) {
124 pr_err("invalid bank\n");
Mattias Nilsson90550d12011-02-14 11:17:12 +0100125 return -EINVAL;
Linus Walleij7e9c40c2017-01-13 10:53:55 +0100126 }
Mattias Nilsson90550d12011-02-14 11:17:12 +0100127
128 return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
129 (u8)(reg & 0xFF), mask, value);
130}
Jonas Aabergc73db9f2011-11-11 07:52:10 +0100131EXPORT_SYMBOL(ab8500_sysctrl_write);
Mattias Nilsson90550d12011-02-14 11:17:12 +0100132
Bill Pembertonf791be42012-11-19 13:23:04 -0500133static int ab8500_sysctrl_probe(struct platform_device *pdev)
Mattias Nilsson90550d12011-02-14 11:17:12 +0100134{
Fabio Baltieri9f7af612013-04-26 14:17:17 +0200135 sysctrl_dev = &pdev->dev;
136
Fabio Baltieri0b8ebdb2013-05-09 13:08:08 +0200137 if (!pm_power_off)
Lee Jones379749c2013-01-14 13:26:15 +0000138 pm_power_off = ab8500_power_off;
Kennet Wallden1abf0632011-09-27 09:23:56 +0200139
Mattias Nilsson90550d12011-02-14 11:17:12 +0100140 return 0;
141}
142
Bill Pemberton4740f732012-11-19 13:26:01 -0500143static int ab8500_sysctrl_remove(struct platform_device *pdev)
Mattias Nilsson90550d12011-02-14 11:17:12 +0100144{
145 sysctrl_dev = NULL;
Fabio Baltieri0b8ebdb2013-05-09 13:08:08 +0200146
147 if (pm_power_off == ab8500_power_off)
148 pm_power_off = NULL;
149
Mattias Nilsson90550d12011-02-14 11:17:12 +0100150 return 0;
151}
152
Linus Walleij7e9c40c2017-01-13 10:53:55 +0100153static const struct of_device_id ab8500_sysctrl_match[] = {
154 { .compatible = "stericsson,ab8500-sysctrl", },
155 {}
156};
157
Mattias Nilsson90550d12011-02-14 11:17:12 +0100158static struct platform_driver ab8500_sysctrl_driver = {
159 .driver = {
160 .name = "ab8500-sysctrl",
Linus Walleij7e9c40c2017-01-13 10:53:55 +0100161 .of_match_table = ab8500_sysctrl_match,
Mattias Nilsson90550d12011-02-14 11:17:12 +0100162 },
163 .probe = ab8500_sysctrl_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500164 .remove = ab8500_sysctrl_remove,
Mattias Nilsson90550d12011-02-14 11:17:12 +0100165};
166
167static int __init ab8500_sysctrl_init(void)
168{
169 return platform_driver_register(&ab8500_sysctrl_driver);
170}
Ulf Hanssoncaa62d62013-04-03 01:06:27 +0200171arch_initcall(ab8500_sysctrl_init);