blob: 6ac63a05893c8a57374bcf01c5ea7c4f82574c72 [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;
Rajkumar Kasirajanf04a9d82012-05-30 16:32:37 +053024 static char *pss[] = {"ab8500_ac", "pm2301", "ab8500_usb"};
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020025 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
Marcus Danielsson2377e522012-06-18 15:00:40 +020031 if (sysctrl_dev == NULL) {
32 pr_err("%s: sysctrl not initialized\n", __func__);
33 return;
34 }
35
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020036 /*
37 * If we have a charger connected and we're powering off,
38 * reboot into charge-only mode.
39 */
40
41 for (i = 0; i < ARRAY_SIZE(pss); i++) {
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020042 psy = power_supply_get_by_name(pss[i]);
43 if (!psy)
44 continue;
Jonas Aaberg09039402011-08-17 15:58:52 +020045
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020046 ret = psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &val);
47
48 if (!ret && val.intval) {
Jonas Aaberg09039402011-08-17 15:58:52 +020049 charger_present = true;
50 break;
51 }
52 }
53
54 if (!charger_present)
55 goto shutdown;
56
57 /* Check if battery is known */
58 psy = power_supply_get_by_name("ab8500_btemp");
59 if (psy) {
60 ret = psy->get_property(psy, POWER_SUPPLY_PROP_TECHNOLOGY,
61 &val);
62 if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) {
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020063 printk(KERN_INFO
Jonas Aaberg09039402011-08-17 15:58:52 +020064 "Charger \"%s\" is connected with known battery."
65 " Rebooting.\n",
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020066 pss[i]);
Jonas Aaberg5a4bac62011-08-18 10:14:38 +020067 machine_restart("charging");
Jonas Aaberg7c34d7c2011-08-17 13:20:21 +020068 }
69 }
Lee Jones379749c2013-01-14 13:26:15 +000070
Jonas Aaberg09039402011-08-17 15:58:52 +020071shutdown:
Lee Jones379749c2013-01-14 13:26:15 +000072 sigfillset(&all);
73
74 if (!sigprocmask(SIG_BLOCK, &all, &old)) {
75 (void)ab8500_sysctrl_set(AB8500_STW4500CTRL1,
76 AB8500_STW4500CTRL1_SWOFF |
77 AB8500_STW4500CTRL1_SWRESET4500N);
78 (void)sigprocmask(SIG_SETMASK, &old, NULL);
79 }
80}
81
Mattias Nilsson90550d12011-02-14 11:17:12 +010082static inline bool valid_bank(u8 bank)
83{
84 return ((bank == AB8500_SYS_CTRL1_BLOCK) ||
85 (bank == AB8500_SYS_CTRL2_BLOCK));
86}
87
88int ab8500_sysctrl_read(u16 reg, u8 *value)
89{
90 u8 bank;
91
92 if (sysctrl_dev == NULL)
Marcus Danielsson2377e522012-06-18 15:00:40 +020093 return -EINVAL;
Mattias Nilsson90550d12011-02-14 11:17:12 +010094
95 bank = (reg >> 8);
96 if (!valid_bank(bank))
97 return -EINVAL;
98
99 return abx500_get_register_interruptible(sysctrl_dev, bank,
100 (u8)(reg & 0xFF), value);
101}
Jonas Aabergc73db9f2011-11-11 07:52:10 +0100102EXPORT_SYMBOL(ab8500_sysctrl_read);
Mattias Nilsson90550d12011-02-14 11:17:12 +0100103
104int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
105{
106 u8 bank;
107
108 if (sysctrl_dev == NULL)
Marcus Danielsson2377e522012-06-18 15:00:40 +0200109 return -EINVAL;
Mattias Nilsson90550d12011-02-14 11:17:12 +0100110
111 bank = (reg >> 8);
112 if (!valid_bank(bank))
113 return -EINVAL;
114
115 return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
116 (u8)(reg & 0xFF), mask, value);
117}
Jonas Aabergc73db9f2011-11-11 07:52:10 +0100118EXPORT_SYMBOL(ab8500_sysctrl_write);
Mattias Nilsson90550d12011-02-14 11:17:12 +0100119
Bill Pembertonf791be42012-11-19 13:23:04 -0500120static int ab8500_sysctrl_probe(struct platform_device *pdev)
Mattias Nilsson90550d12011-02-14 11:17:12 +0100121{
Lee Jones379749c2013-01-14 13:26:15 +0000122 struct ab8500_platform_data *plat;
Kennet Wallden1abf0632011-09-27 09:23:56 +0200123 struct ab8500_sysctrl_platform_data *pdata;
Marcus Danielsson2377e522012-06-18 15:00:40 +0200124 int ret, i, j;
Lee Jones379749c2013-01-14 13:26:15 +0000125
Lee Jones379749c2013-01-14 13:26:15 +0000126 plat = dev_get_platdata(pdev->dev.parent);
Marcus Danielsson2377e522012-06-18 15:00:40 +0200127
128 if (!(plat && plat->sysctrl))
129 return -EINVAL;
130
Lee Jones379749c2013-01-14 13:26:15 +0000131 if (plat->pm_power_off)
132 pm_power_off = ab8500_power_off;
Kennet Wallden1abf0632011-09-27 09:23:56 +0200133
134 pdata = plat->sysctrl;
135
Kennet Wallden1abf0632011-09-27 09:23:56 +0200136
Marcus Danielsson2377e522012-06-18 15:00:40 +0200137 for (i = AB8500_SYSCLKREQ1RFCLKBUF;
138 i <= AB8500_SYSCLKREQ8RFCLKBUF; i++) {
139 j = i - AB8500_SYSCLKREQ1RFCLKBUF;
140 ret = ab8500_sysctrl_write(i, 0xff,
141 pdata->initial_req_buf_config[j]);
142 dev_dbg(&pdev->dev,
Kennet Wallden1abf0632011-09-27 09:23:56 +0200143 "Setting SysClkReq%dRfClkBuf 0x%X\n",
144 j + 1,
145 pdata->initial_req_buf_config[j]);
Marcus Danielsson2377e522012-06-18 15:00:40 +0200146 if (ret < 0) {
147 dev_err(&pdev->dev,
148 "unable to set sysClkReq%dRfClkBuf: "
149 "%d\n", j + 1, ret);
Kennet Wallden1abf0632011-09-27 09:23:56 +0200150 }
151 }
152
Mattias Nilsson90550d12011-02-14 11:17:12 +0100153 return 0;
154}
155
Bill Pemberton4740f732012-11-19 13:26:01 -0500156static int ab8500_sysctrl_remove(struct platform_device *pdev)
Mattias Nilsson90550d12011-02-14 11:17:12 +0100157{
158 sysctrl_dev = NULL;
159 return 0;
160}
161
162static struct platform_driver ab8500_sysctrl_driver = {
163 .driver = {
164 .name = "ab8500-sysctrl",
165 .owner = THIS_MODULE,
Mattias Nilsson90550d12011-02-14 11:17:12 +0100166 },
167 .probe = ab8500_sysctrl_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500168 .remove = ab8500_sysctrl_remove,
Mattias Nilsson90550d12011-02-14 11:17:12 +0100169};
170
171static int __init ab8500_sysctrl_init(void)
172{
173 return platform_driver_register(&ab8500_sysctrl_driver);
174}
175subsys_initcall(ab8500_sysctrl_init);
176
177MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com");
178MODULE_DESCRIPTION("AB8500 system control driver");
179MODULE_LICENSE("GPL v2");