xref: /openbmc/linux/drivers/mfd/ab8500-sysctrl.c (revision 5a4bac6e6331feb0edd9522f3c7bbb9a01571566)
190550d19SMattias Nilsson /*
290550d19SMattias Nilsson  * Copyright (C) ST-Ericsson SA 2010
390550d19SMattias Nilsson  * Author: Mattias Nilsson <mattias.i.nilsson@stericsson.com> for ST Ericsson.
490550d19SMattias Nilsson  * License terms: GNU General Public License (GPL) version 2
590550d19SMattias Nilsson  */
690550d19SMattias Nilsson 
790550d19SMattias Nilsson #include <linux/err.h>
84e36dd33SPaul Gortmaker #include <linux/module.h>
990550d19SMattias Nilsson #include <linux/platform_device.h>
10379749c4SLee Jones #include <linux/pm.h>
117c34d7c2SJonas Aaberg #include <linux/reboot.h>
12379749c4SLee Jones #include <linux/signal.h>
137c34d7c2SJonas Aaberg #include <linux/power_supply.h>
1490550d19SMattias Nilsson #include <linux/mfd/abx500.h>
15ee66e653SLinus Walleij #include <linux/mfd/abx500/ab8500.h>
16ee66e653SLinus Walleij #include <linux/mfd/abx500/ab8500-sysctrl.h>
1790550d19SMattias Nilsson 
1890550d19SMattias Nilsson static struct device *sysctrl_dev;
1990550d19SMattias Nilsson 
20379749c4SLee Jones void ab8500_power_off(void)
21379749c4SLee Jones {
22379749c4SLee Jones 	sigset_t old;
23379749c4SLee Jones 	sigset_t all;
247c34d7c2SJonas Aaberg 	static char *pss[] = {"ab8500_ac", "ab8500_usb"};
257c34d7c2SJonas Aaberg 	int i;
260903940dSJonas Aaberg 	bool charger_present = false;
270903940dSJonas Aaberg 	union power_supply_propval val;
280903940dSJonas Aaberg 	struct power_supply *psy;
290903940dSJonas Aaberg 	int ret;
307c34d7c2SJonas Aaberg 
317c34d7c2SJonas Aaberg 	/*
327c34d7c2SJonas Aaberg 	 * If we have a charger connected and we're powering off,
337c34d7c2SJonas Aaberg 	 * reboot into charge-only mode.
347c34d7c2SJonas Aaberg 	 */
357c34d7c2SJonas Aaberg 
367c34d7c2SJonas Aaberg 	for (i = 0; i < ARRAY_SIZE(pss); i++) {
377c34d7c2SJonas Aaberg 		psy = power_supply_get_by_name(pss[i]);
387c34d7c2SJonas Aaberg 		if (!psy)
397c34d7c2SJonas Aaberg 			continue;
400903940dSJonas Aaberg 
417c34d7c2SJonas Aaberg 		ret = psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &val);
427c34d7c2SJonas Aaberg 
437c34d7c2SJonas Aaberg 		if (!ret && val.intval) {
440903940dSJonas Aaberg 			charger_present = true;
450903940dSJonas Aaberg 			break;
460903940dSJonas Aaberg 		}
470903940dSJonas Aaberg 	}
480903940dSJonas Aaberg 
490903940dSJonas Aaberg 	if (!charger_present)
500903940dSJonas Aaberg 		goto shutdown;
510903940dSJonas Aaberg 
520903940dSJonas Aaberg 	/* Check if battery is known */
530903940dSJonas Aaberg 	psy = power_supply_get_by_name("ab8500_btemp");
540903940dSJonas Aaberg 	if (psy) {
550903940dSJonas Aaberg 		ret = psy->get_property(psy, POWER_SUPPLY_PROP_TECHNOLOGY,
560903940dSJonas Aaberg 					&val);
570903940dSJonas Aaberg 		if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) {
587c34d7c2SJonas Aaberg 			printk(KERN_INFO
590903940dSJonas Aaberg 			       "Charger \"%s\" is connected with known battery."
600903940dSJonas Aaberg 			       " Rebooting.\n",
617c34d7c2SJonas Aaberg 			       pss[i]);
62*5a4bac6eSJonas Aaberg 			machine_restart("charging");
637c34d7c2SJonas Aaberg 		}
647c34d7c2SJonas Aaberg 	}
65379749c4SLee Jones 
660903940dSJonas Aaberg shutdown:
67379749c4SLee Jones 	sigfillset(&all);
68379749c4SLee Jones 
69379749c4SLee Jones 	if (!sigprocmask(SIG_BLOCK, &all, &old)) {
70379749c4SLee Jones 		(void)ab8500_sysctrl_set(AB8500_STW4500CTRL1,
71379749c4SLee Jones 					 AB8500_STW4500CTRL1_SWOFF |
72379749c4SLee Jones 					 AB8500_STW4500CTRL1_SWRESET4500N);
73379749c4SLee Jones 		(void)sigprocmask(SIG_SETMASK, &old, NULL);
74379749c4SLee Jones 	}
75379749c4SLee Jones }
76379749c4SLee Jones 
7790550d19SMattias Nilsson static inline bool valid_bank(u8 bank)
7890550d19SMattias Nilsson {
7990550d19SMattias Nilsson 	return ((bank == AB8500_SYS_CTRL1_BLOCK) ||
8090550d19SMattias Nilsson 		(bank == AB8500_SYS_CTRL2_BLOCK));
8190550d19SMattias Nilsson }
8290550d19SMattias Nilsson 
8390550d19SMattias Nilsson int ab8500_sysctrl_read(u16 reg, u8 *value)
8490550d19SMattias Nilsson {
8590550d19SMattias Nilsson 	u8 bank;
8690550d19SMattias Nilsson 
8790550d19SMattias Nilsson 	if (sysctrl_dev == NULL)
8890550d19SMattias Nilsson 		return -EAGAIN;
8990550d19SMattias Nilsson 
9090550d19SMattias Nilsson 	bank = (reg >> 8);
9190550d19SMattias Nilsson 	if (!valid_bank(bank))
9290550d19SMattias Nilsson 		return -EINVAL;
9390550d19SMattias Nilsson 
9490550d19SMattias Nilsson 	return abx500_get_register_interruptible(sysctrl_dev, bank,
9590550d19SMattias Nilsson 		(u8)(reg & 0xFF), value);
9690550d19SMattias Nilsson }
9790550d19SMattias Nilsson 
9890550d19SMattias Nilsson int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
9990550d19SMattias Nilsson {
10090550d19SMattias Nilsson 	u8 bank;
10190550d19SMattias Nilsson 
10290550d19SMattias Nilsson 	if (sysctrl_dev == NULL)
10390550d19SMattias Nilsson 		return -EAGAIN;
10490550d19SMattias Nilsson 
10590550d19SMattias Nilsson 	bank = (reg >> 8);
10690550d19SMattias Nilsson 	if (!valid_bank(bank))
10790550d19SMattias Nilsson 		return -EINVAL;
10890550d19SMattias Nilsson 
10990550d19SMattias Nilsson 	return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
11090550d19SMattias Nilsson 		(u8)(reg & 0xFF), mask, value);
11190550d19SMattias Nilsson }
11290550d19SMattias Nilsson 
113f791be49SBill Pemberton static int ab8500_sysctrl_probe(struct platform_device *pdev)
11490550d19SMattias Nilsson {
115379749c4SLee Jones 	struct ab8500_platform_data *plat;
116379749c4SLee Jones 
11790550d19SMattias Nilsson 	sysctrl_dev = &pdev->dev;
118379749c4SLee Jones 	plat = dev_get_platdata(pdev->dev.parent);
119379749c4SLee Jones 	if (plat->pm_power_off)
120379749c4SLee Jones 		pm_power_off = ab8500_power_off;
12190550d19SMattias Nilsson 	return 0;
12290550d19SMattias Nilsson }
12390550d19SMattias Nilsson 
1244740f73fSBill Pemberton static int ab8500_sysctrl_remove(struct platform_device *pdev)
12590550d19SMattias Nilsson {
12690550d19SMattias Nilsson 	sysctrl_dev = NULL;
12790550d19SMattias Nilsson 	return 0;
12890550d19SMattias Nilsson }
12990550d19SMattias Nilsson 
13090550d19SMattias Nilsson static struct platform_driver ab8500_sysctrl_driver = {
13190550d19SMattias Nilsson 	.driver = {
13290550d19SMattias Nilsson 		.name = "ab8500-sysctrl",
13390550d19SMattias Nilsson 		.owner = THIS_MODULE,
13490550d19SMattias Nilsson 	},
13590550d19SMattias Nilsson 	.probe = ab8500_sysctrl_probe,
13684449216SBill Pemberton 	.remove = ab8500_sysctrl_remove,
13790550d19SMattias Nilsson };
13890550d19SMattias Nilsson 
13990550d19SMattias Nilsson static int __init ab8500_sysctrl_init(void)
14090550d19SMattias Nilsson {
14190550d19SMattias Nilsson 	return platform_driver_register(&ab8500_sysctrl_driver);
14290550d19SMattias Nilsson }
14390550d19SMattias Nilsson subsys_initcall(ab8500_sysctrl_init);
14490550d19SMattias Nilsson 
14590550d19SMattias Nilsson MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com");
14690550d19SMattias Nilsson MODULE_DESCRIPTION("AB8500 system control driver");
14790550d19SMattias Nilsson MODULE_LICENSE("GPL v2");
148