1*af873fceSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
290550d19SMattias Nilsson /*
3dac94efaSPaul Gortmaker * AB8500 system control driver
4dac94efaSPaul Gortmaker *
590550d19SMattias Nilsson * Copyright (C) ST-Ericsson SA 2010
690550d19SMattias Nilsson * Author: Mattias Nilsson <mattias.i.nilsson@stericsson.com> for ST Ericsson.
790550d19SMattias Nilsson */
890550d19SMattias Nilsson
990550d19SMattias Nilsson #include <linux/err.h>
10dac94efaSPaul Gortmaker #include <linux/init.h>
11dac94efaSPaul Gortmaker #include <linux/export.h>
1290550d19SMattias Nilsson #include <linux/platform_device.h>
13379749c4SLee Jones #include <linux/pm.h>
147c34d7c2SJonas Aaberg #include <linux/reboot.h>
15379749c4SLee Jones #include <linux/signal.h>
167c34d7c2SJonas Aaberg #include <linux/power_supply.h>
1790550d19SMattias Nilsson #include <linux/mfd/abx500.h>
18ee66e653SLinus Walleij #include <linux/mfd/abx500/ab8500.h>
19ee66e653SLinus Walleij #include <linux/mfd/abx500/ab8500-sysctrl.h>
2090550d19SMattias Nilsson
2175932094SLee Jones /* RtcCtrl bits */
2275932094SLee Jones #define AB8500_ALARM_MIN_LOW 0x08
2375932094SLee Jones #define AB8500_ALARM_MIN_MID 0x09
2475932094SLee Jones #define RTC_CTRL 0x0B
2575932094SLee Jones #define RTC_ALARM_ENABLE 0x4
2675932094SLee Jones
2790550d19SMattias Nilsson static struct device *sysctrl_dev;
2890550d19SMattias Nilsson
ab8500_power_off(void)293d2088a1SFabio Baltieri static void ab8500_power_off(void)
30379749c4SLee Jones {
31379749c4SLee Jones sigset_t old;
32379749c4SLee Jones sigset_t all;
3363b4fd75SLee Jones static const char * const pss[] = {"ab8500_ac", "pm2301", "ab8500_usb"};
347c34d7c2SJonas Aaberg int i;
350903940dSJonas Aaberg bool charger_present = false;
360903940dSJonas Aaberg union power_supply_propval val;
370903940dSJonas Aaberg struct power_supply *psy;
380903940dSJonas Aaberg int ret;
397c34d7c2SJonas Aaberg
402377e52fSMarcus Danielsson if (sysctrl_dev == NULL) {
412377e52fSMarcus Danielsson pr_err("%s: sysctrl not initialized\n", __func__);
422377e52fSMarcus Danielsson return;
432377e52fSMarcus Danielsson }
442377e52fSMarcus Danielsson
457c34d7c2SJonas Aaberg /*
467c34d7c2SJonas Aaberg * If we have a charger connected and we're powering off,
477c34d7c2SJonas Aaberg * reboot into charge-only mode.
487c34d7c2SJonas Aaberg */
497c34d7c2SJonas Aaberg
507c34d7c2SJonas Aaberg for (i = 0; i < ARRAY_SIZE(pss); i++) {
517c34d7c2SJonas Aaberg psy = power_supply_get_by_name(pss[i]);
527c34d7c2SJonas Aaberg if (!psy)
537c34d7c2SJonas Aaberg continue;
540903940dSJonas Aaberg
55091e73a2SKrzysztof Kozlowski ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_ONLINE,
56091e73a2SKrzysztof Kozlowski &val);
576ff9d25bSKrzysztof Kozlowski power_supply_put(psy);
587c34d7c2SJonas Aaberg
597c34d7c2SJonas Aaberg if (!ret && val.intval) {
600903940dSJonas Aaberg charger_present = true;
610903940dSJonas Aaberg break;
620903940dSJonas Aaberg }
630903940dSJonas Aaberg }
640903940dSJonas Aaberg
650903940dSJonas Aaberg if (!charger_present)
660903940dSJonas Aaberg goto shutdown;
670903940dSJonas Aaberg
680903940dSJonas Aaberg /* Check if battery is known */
690903940dSJonas Aaberg psy = power_supply_get_by_name("ab8500_btemp");
700903940dSJonas Aaberg if (psy) {
71091e73a2SKrzysztof Kozlowski ret = power_supply_get_property(psy,
72091e73a2SKrzysztof Kozlowski POWER_SUPPLY_PROP_TECHNOLOGY, &val);
730903940dSJonas Aaberg if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) {
7463b4fd75SLee Jones pr_info("Charger '%s' is connected with known battery",
757c34d7c2SJonas Aaberg pss[i]);
7663b4fd75SLee Jones pr_info(" - Rebooting.\n");
775a4bac6eSJonas Aaberg machine_restart("charging");
787c34d7c2SJonas Aaberg }
796ff9d25bSKrzysztof Kozlowski power_supply_put(psy);
807c34d7c2SJonas Aaberg }
81379749c4SLee Jones
820903940dSJonas Aaberg shutdown:
83379749c4SLee Jones sigfillset(&all);
84379749c4SLee Jones
85379749c4SLee Jones if (!sigprocmask(SIG_BLOCK, &all, &old)) {
86379749c4SLee Jones (void)ab8500_sysctrl_set(AB8500_STW4500CTRL1,
87379749c4SLee Jones AB8500_STW4500CTRL1_SWOFF |
88379749c4SLee Jones AB8500_STW4500CTRL1_SWRESET4500N);
89379749c4SLee Jones (void)sigprocmask(SIG_SETMASK, &old, NULL);
90379749c4SLee Jones }
91379749c4SLee Jones }
92379749c4SLee Jones
valid_bank(u8 bank)9390550d19SMattias Nilsson static inline bool valid_bank(u8 bank)
9490550d19SMattias Nilsson {
9590550d19SMattias Nilsson return ((bank == AB8500_SYS_CTRL1_BLOCK) ||
9690550d19SMattias Nilsson (bank == AB8500_SYS_CTRL2_BLOCK));
9790550d19SMattias Nilsson }
9890550d19SMattias Nilsson
ab8500_sysctrl_read(u16 reg,u8 * value)9990550d19SMattias Nilsson int ab8500_sysctrl_read(u16 reg, u8 *value)
10090550d19SMattias Nilsson {
10190550d19SMattias Nilsson u8 bank;
10290550d19SMattias Nilsson
10390550d19SMattias Nilsson if (sysctrl_dev == NULL)
1047e9c40c6SLinus Walleij return -EPROBE_DEFER;
10590550d19SMattias Nilsson
10690550d19SMattias Nilsson bank = (reg >> 8);
10790550d19SMattias Nilsson if (!valid_bank(bank))
10890550d19SMattias Nilsson return -EINVAL;
10990550d19SMattias Nilsson
11090550d19SMattias Nilsson return abx500_get_register_interruptible(sysctrl_dev, bank,
11190550d19SMattias Nilsson (u8)(reg & 0xFF), value);
11290550d19SMattias Nilsson }
113c73db9f7SJonas Aaberg EXPORT_SYMBOL(ab8500_sysctrl_read);
11490550d19SMattias Nilsson
ab8500_sysctrl_write(u16 reg,u8 mask,u8 value)11590550d19SMattias Nilsson int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
11690550d19SMattias Nilsson {
11790550d19SMattias Nilsson u8 bank;
11890550d19SMattias Nilsson
11990550d19SMattias Nilsson if (sysctrl_dev == NULL)
1207e9c40c6SLinus Walleij return -EPROBE_DEFER;
12190550d19SMattias Nilsson
12290550d19SMattias Nilsson bank = (reg >> 8);
1237e9c40c6SLinus Walleij if (!valid_bank(bank)) {
1247e9c40c6SLinus Walleij pr_err("invalid bank\n");
12590550d19SMattias Nilsson return -EINVAL;
1267e9c40c6SLinus Walleij }
12790550d19SMattias Nilsson
12890550d19SMattias Nilsson return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
12990550d19SMattias Nilsson (u8)(reg & 0xFF), mask, value);
13090550d19SMattias Nilsson }
131c73db9f7SJonas Aaberg EXPORT_SYMBOL(ab8500_sysctrl_write);
13290550d19SMattias Nilsson
ab8500_sysctrl_probe(struct platform_device * pdev)133f791be49SBill Pemberton static int ab8500_sysctrl_probe(struct platform_device *pdev)
13490550d19SMattias Nilsson {
1359f7af61aSFabio Baltieri sysctrl_dev = &pdev->dev;
1369f7af61aSFabio Baltieri
1370b8ebdb1SFabio Baltieri if (!pm_power_off)
138379749c4SLee Jones pm_power_off = ab8500_power_off;
1391abf063fSKennet Wallden
14090550d19SMattias Nilsson return 0;
14190550d19SMattias Nilsson }
14290550d19SMattias Nilsson
ab8500_sysctrl_remove(struct platform_device * pdev)1434740f73fSBill Pemberton static int ab8500_sysctrl_remove(struct platform_device *pdev)
14490550d19SMattias Nilsson {
14590550d19SMattias Nilsson sysctrl_dev = NULL;
1460b8ebdb1SFabio Baltieri
1470b8ebdb1SFabio Baltieri if (pm_power_off == ab8500_power_off)
1480b8ebdb1SFabio Baltieri pm_power_off = NULL;
1490b8ebdb1SFabio Baltieri
15090550d19SMattias Nilsson return 0;
15190550d19SMattias Nilsson }
15290550d19SMattias Nilsson
1537e9c40c6SLinus Walleij static const struct of_device_id ab8500_sysctrl_match[] = {
1547e9c40c6SLinus Walleij { .compatible = "stericsson,ab8500-sysctrl", },
1557e9c40c6SLinus Walleij {}
1567e9c40c6SLinus Walleij };
1577e9c40c6SLinus Walleij
15890550d19SMattias Nilsson static struct platform_driver ab8500_sysctrl_driver = {
15990550d19SMattias Nilsson .driver = {
16090550d19SMattias Nilsson .name = "ab8500-sysctrl",
1617e9c40c6SLinus Walleij .of_match_table = ab8500_sysctrl_match,
16290550d19SMattias Nilsson },
16390550d19SMattias Nilsson .probe = ab8500_sysctrl_probe,
16484449216SBill Pemberton .remove = ab8500_sysctrl_remove,
16590550d19SMattias Nilsson };
16690550d19SMattias Nilsson
ab8500_sysctrl_init(void)16790550d19SMattias Nilsson static int __init ab8500_sysctrl_init(void)
16890550d19SMattias Nilsson {
16990550d19SMattias Nilsson return platform_driver_register(&ab8500_sysctrl_driver);
17090550d19SMattias Nilsson }
171caa62d64SUlf Hansson arch_initcall(ab8500_sysctrl_init);
172