blob: 3aaff6043e184735c60ea166e502da18ec73cb20 [file] [log] [blame]
Mattias Nilsson90550d12011-02-14 11:17:12 +01001/*
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>
Paul Gortmaker4e36dd32011-07-03 15:13:27 -04008#include <linux/module.h>
Mattias Nilsson90550d12011-02-14 11:17:12 +01009#include <linux/platform_device.h>
Lee Jones379749c2013-01-14 13:26:15 +000010#include <linux/pm.h>
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020011#include <linux/reboot.h>
Lee Jones379749c2013-01-14 13:26:15 +000012#include <linux/signal.h>
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020013#include <linux/power_supply.h>
Mattias Nilsson90550d12011-02-14 11:17:12 +010014#include <linux/mfd/abx500.h>
Linus Walleijee66e652011-12-02 14:16:33 +010015#include <linux/mfd/abx500/ab8500.h>
16#include <linux/mfd/abx500/ab8500-sysctrl.h>
Mattias Nilsson90550d12011-02-14 11:17:12 +010017
18static struct device *sysctrl_dev;
19
Lee Jones379749c2013-01-14 13:26:15 +000020void ab8500_power_off(void)
21{
22 sigset_t old;
23 sigset_t all;
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020024 static char *pss[] = {"ab8500_ac", "ab8500_usb"};
25 int i;
Jonas Aaberg09039402011-08-17 15:58:52 +020026 bool charger_present = false;
27 union power_supply_propval val;
28 struct power_supply *psy;
29 int ret;
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020030
31 /*
32 * If we have a charger connected and we're powering off,
33 * reboot into charge-only mode.
34 */
35
36 for (i = 0; i < ARRAY_SIZE(pss); i++) {
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020037 psy = power_supply_get_by_name(pss[i]);
38 if (!psy)
39 continue;
Jonas Aaberg09039402011-08-17 15:58:52 +020040
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020041 ret = psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &val);
42
43 if (!ret && val.intval) {
Jonas Aaberg09039402011-08-17 15:58:52 +020044 charger_present = true;
45 break;
46 }
47 }
48
49 if (!charger_present)
50 goto shutdown;
51
52 /* Check if battery is known */
53 psy = power_supply_get_by_name("ab8500_btemp");
54 if (psy) {
55 ret = psy->get_property(psy, POWER_SUPPLY_PROP_TECHNOLOGY,
56 &val);
57 if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) {
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020058 printk(KERN_INFO
Jonas Aaberg09039402011-08-17 15:58:52 +020059 "Charger \"%s\" is connected with known battery."
60 " Rebooting.\n",
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020061 pss[i]);
Jonas Aaberg5a4bac62011-08-18 10:14:38 +020062 machine_restart("charging");
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020063 }
64 }
Lee Jones379749c2013-01-14 13:26:15 +000065
Jonas Aaberg09039402011-08-17 15:58:52 +020066shutdown:
Lee Jones379749c2013-01-14 13:26:15 +000067 sigfillset(&all);
68
69 if (!sigprocmask(SIG_BLOCK, &all, &old)) {
70 (void)ab8500_sysctrl_set(AB8500_STW4500CTRL1,
71 AB8500_STW4500CTRL1_SWOFF |
72 AB8500_STW4500CTRL1_SWRESET4500N);
73 (void)sigprocmask(SIG_SETMASK, &old, NULL);
74 }
75}
76
Mattias Nilsson90550d12011-02-14 11:17:12 +010077static inline bool valid_bank(u8 bank)
78{
79 return ((bank == AB8500_SYS_CTRL1_BLOCK) ||
80 (bank == AB8500_SYS_CTRL2_BLOCK));
81}
82
83int ab8500_sysctrl_read(u16 reg, u8 *value)
84{
85 u8 bank;
86
87 if (sysctrl_dev == NULL)
88 return -EAGAIN;
89
90 bank = (reg >> 8);
91 if (!valid_bank(bank))
92 return -EINVAL;
93
94 return abx500_get_register_interruptible(sysctrl_dev, bank,
95 (u8)(reg & 0xFF), value);
96}
97
98int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
99{
100 u8 bank;
101
102 if (sysctrl_dev == NULL)
103 return -EAGAIN;
104
105 bank = (reg >> 8);
106 if (!valid_bank(bank))
107 return -EINVAL;
108
109 return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
110 (u8)(reg & 0xFF), mask, value);
111}
112
Bill Pembertonf791be42012-11-19 13:23:04 -0500113static int ab8500_sysctrl_probe(struct platform_device *pdev)
Mattias Nilsson90550d12011-02-14 11:17:12 +0100114{
Lee Jones379749c2013-01-14 13:26:15 +0000115 struct ab8500_platform_data *plat;
116
Mattias Nilsson90550d12011-02-14 11:17:12 +0100117 sysctrl_dev = &pdev->dev;
Lee Jones379749c2013-01-14 13:26:15 +0000118 plat = dev_get_platdata(pdev->dev.parent);
119 if (plat->pm_power_off)
120 pm_power_off = ab8500_power_off;
Mattias Nilsson90550d12011-02-14 11:17:12 +0100121 return 0;
122}
123
Bill Pemberton4740f732012-11-19 13:26:01 -0500124static int ab8500_sysctrl_remove(struct platform_device *pdev)
Mattias Nilsson90550d12011-02-14 11:17:12 +0100125{
126 sysctrl_dev = NULL;
127 return 0;
128}
129
130static struct platform_driver ab8500_sysctrl_driver = {
131 .driver = {
132 .name = "ab8500-sysctrl",
133 .owner = THIS_MODULE,
Mattias Nilsson90550d12011-02-14 11:17:12 +0100134 },
135 .probe = ab8500_sysctrl_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500136 .remove = ab8500_sysctrl_remove,
Mattias Nilsson90550d12011-02-14 11:17:12 +0100137};
138
139static int __init ab8500_sysctrl_init(void)
140{
141 return platform_driver_register(&ab8500_sysctrl_driver);
142}
143subsys_initcall(ab8500_sysctrl_init);
144
145MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com");
146MODULE_DESCRIPTION("AB8500 system control driver");
147MODULE_LICENSE("GPL v2");