Merge branch 'for-john' of git://git.kernel.org/pub/scm/linux/kernel/git/jberg/mac80211
[deliverable/linux.git] / drivers / mfd / ab8500-sysctrl.c
CommitLineData
90550d19
MN
1/*
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>
4e36dd33 8#include <linux/module.h>
90550d19 9#include <linux/platform_device.h>
379749c4 10#include <linux/pm.h>
7c34d7c2 11#include <linux/reboot.h>
379749c4 12#include <linux/signal.h>
7c34d7c2 13#include <linux/power_supply.h>
90550d19 14#include <linux/mfd/abx500.h>
ee66e653
LW
15#include <linux/mfd/abx500/ab8500.h>
16#include <linux/mfd/abx500/ab8500-sysctrl.h>
90550d19
MN
17
18static struct device *sysctrl_dev;
19
379749c4
LJ
20void ab8500_power_off(void)
21{
22 sigset_t old;
23 sigset_t all;
7c34d7c2
JA
24 static char *pss[] = {"ab8500_ac", "ab8500_usb"};
25 int i;
0903940d
JA
26 bool charger_present = false;
27 union power_supply_propval val;
28 struct power_supply *psy;
29 int ret;
7c34d7c2
JA
30
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++) {
7c34d7c2
JA
37 psy = power_supply_get_by_name(pss[i]);
38 if (!psy)
39 continue;
0903940d 40
7c34d7c2
JA
41 ret = psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &val);
42
43 if (!ret && val.intval) {
0903940d
JA
44 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) {
7c34d7c2 58 printk(KERN_INFO
0903940d
JA
59 "Charger \"%s\" is connected with known battery."
60 " Rebooting.\n",
7c34d7c2 61 pss[i]);
5a4bac6e 62 machine_restart("charging");
7c34d7c2
JA
63 }
64 }
379749c4 65
0903940d 66shutdown:
379749c4
LJ
67 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
90550d19
MN
77static 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}
c73db9f7 97EXPORT_SYMBOL(ab8500_sysctrl_read);
90550d19
MN
98
99int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value)
100{
101 u8 bank;
102
103 if (sysctrl_dev == NULL)
104 return -EAGAIN;
105
106 bank = (reg >> 8);
107 if (!valid_bank(bank))
108 return -EINVAL;
109
110 return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank,
111 (u8)(reg & 0xFF), mask, value);
112}
c73db9f7 113EXPORT_SYMBOL(ab8500_sysctrl_write);
90550d19 114
f791be49 115static int ab8500_sysctrl_probe(struct platform_device *pdev)
90550d19 116{
379749c4 117 struct ab8500_platform_data *plat;
1abf063f 118 struct ab8500_sysctrl_platform_data *pdata;
379749c4 119
90550d19 120 sysctrl_dev = &pdev->dev;
379749c4
LJ
121 plat = dev_get_platdata(pdev->dev.parent);
122 if (plat->pm_power_off)
123 pm_power_off = ab8500_power_off;
1abf063f
KW
124
125 pdata = plat->sysctrl;
126
127 if (pdata) {
128 int ret, i, j;
129
130 for (i = AB8500_SYSCLKREQ1RFCLKBUF;
131 i <= AB8500_SYSCLKREQ8RFCLKBUF; i++) {
132 j = i - AB8500_SYSCLKREQ1RFCLKBUF;
133 ret = ab8500_sysctrl_write(i, 0xff,
134 pdata->initial_req_buf_config[j]);
135 dev_dbg(&pdev->dev,
136 "Setting SysClkReq%dRfClkBuf 0x%X\n",
137 j + 1,
138 pdata->initial_req_buf_config[j]);
139 if (ret < 0) {
140 dev_err(&pdev->dev,
141 "unable to set sysClkReq%dRfClkBuf: "
142 "%d\n", j + 1, ret);
143 }
144 }
145 }
146
90550d19
MN
147 return 0;
148}
149
4740f73f 150static int ab8500_sysctrl_remove(struct platform_device *pdev)
90550d19
MN
151{
152 sysctrl_dev = NULL;
153 return 0;
154}
155
156static struct platform_driver ab8500_sysctrl_driver = {
157 .driver = {
158 .name = "ab8500-sysctrl",
159 .owner = THIS_MODULE,
160 },
161 .probe = ab8500_sysctrl_probe,
84449216 162 .remove = ab8500_sysctrl_remove,
90550d19
MN
163};
164
165static int __init ab8500_sysctrl_init(void)
166{
167 return platform_driver_register(&ab8500_sysctrl_driver);
168}
169subsys_initcall(ab8500_sysctrl_init);
170
171MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com");
172MODULE_DESCRIPTION("AB8500 system control driver");
173MODULE_LICENSE("GPL v2");
This page took 0.144818 seconds and 5 git commands to generate.