Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 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> |
Paul Gortmaker | 4e36dd3 | 2011-07-03 15:13:27 -0400 | [diff] [blame] | 8 | #include <linux/module.h> |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 9 | #include <linux/platform_device.h> |
Lee Jones | 379749c | 2013-01-14 13:26:15 +0000 | [diff] [blame] | 10 | #include <linux/pm.h> |
Jonas Aaberg | 7c34d7c | 2011-08-17 13:20:21 +0200 | [diff] [blame] | 11 | #include <linux/reboot.h> |
Lee Jones | 379749c | 2013-01-14 13:26:15 +0000 | [diff] [blame] | 12 | #include <linux/signal.h> |
Jonas Aaberg | 7c34d7c | 2011-08-17 13:20:21 +0200 | [diff] [blame] | 13 | #include <linux/power_supply.h> |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 14 | #include <linux/mfd/abx500.h> |
Linus Walleij | ee66e65 | 2011-12-02 14:16:33 +0100 | [diff] [blame] | 15 | #include <linux/mfd/abx500/ab8500.h> |
| 16 | #include <linux/mfd/abx500/ab8500-sysctrl.h> |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 17 | |
Lee Jones | 7593209 | 2013-02-12 15:11:19 +0000 | [diff] [blame] | 18 | /* RtcCtrl bits */ |
| 19 | #define AB8500_ALARM_MIN_LOW 0x08 |
| 20 | #define AB8500_ALARM_MIN_MID 0x09 |
| 21 | #define RTC_CTRL 0x0B |
| 22 | #define RTC_ALARM_ENABLE 0x4 |
| 23 | |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 24 | static struct device *sysctrl_dev; |
| 25 | |
Fabio Baltieri | 3d2088a | 2013-04-26 14:17:15 +0200 | [diff] [blame] | 26 | static void ab8500_power_off(void) |
Lee Jones | 379749c | 2013-01-14 13:26:15 +0000 | [diff] [blame] | 27 | { |
| 28 | sigset_t old; |
| 29 | sigset_t all; |
Rajkumar Kasirajan | f04a9d8 | 2012-05-30 16:32:37 +0530 | [diff] [blame] | 30 | static char *pss[] = {"ab8500_ac", "pm2301", "ab8500_usb"}; |
Jonas Aaberg | 7c34d7c | 2011-08-17 13:20:21 +0200 | [diff] [blame] | 31 | int i; |
Jonas Aaberg | 0903940 | 2011-08-17 15:58:52 +0200 | [diff] [blame] | 32 | bool charger_present = false; |
| 33 | union power_supply_propval val; |
| 34 | struct power_supply *psy; |
| 35 | int ret; |
Jonas Aaberg | 7c34d7c | 2011-08-17 13:20:21 +0200 | [diff] [blame] | 36 | |
Marcus Danielsson | 2377e52 | 2012-06-18 15:00:40 +0200 | [diff] [blame] | 37 | if (sysctrl_dev == NULL) { |
| 38 | pr_err("%s: sysctrl not initialized\n", __func__); |
| 39 | return; |
| 40 | } |
| 41 | |
Jonas Aaberg | 7c34d7c | 2011-08-17 13:20:21 +0200 | [diff] [blame] | 42 | /* |
| 43 | * If we have a charger connected and we're powering off, |
| 44 | * reboot into charge-only mode. |
| 45 | */ |
| 46 | |
| 47 | for (i = 0; i < ARRAY_SIZE(pss); i++) { |
Jonas Aaberg | 7c34d7c | 2011-08-17 13:20:21 +0200 | [diff] [blame] | 48 | psy = power_supply_get_by_name(pss[i]); |
| 49 | if (!psy) |
| 50 | continue; |
Jonas Aaberg | 0903940 | 2011-08-17 15:58:52 +0200 | [diff] [blame] | 51 | |
Jonas Aaberg | 7c34d7c | 2011-08-17 13:20:21 +0200 | [diff] [blame] | 52 | ret = psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &val); |
| 53 | |
| 54 | if (!ret && val.intval) { |
Jonas Aaberg | 0903940 | 2011-08-17 15:58:52 +0200 | [diff] [blame] | 55 | charger_present = true; |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (!charger_present) |
| 61 | goto shutdown; |
| 62 | |
| 63 | /* Check if battery is known */ |
| 64 | psy = power_supply_get_by_name("ab8500_btemp"); |
| 65 | if (psy) { |
| 66 | ret = psy->get_property(psy, POWER_SUPPLY_PROP_TECHNOLOGY, |
| 67 | &val); |
| 68 | if (!ret && val.intval != POWER_SUPPLY_TECHNOLOGY_UNKNOWN) { |
Jonas Aaberg | 7c34d7c | 2011-08-17 13:20:21 +0200 | [diff] [blame] | 69 | printk(KERN_INFO |
Jonas Aaberg | 0903940 | 2011-08-17 15:58:52 +0200 | [diff] [blame] | 70 | "Charger \"%s\" is connected with known battery." |
| 71 | " Rebooting.\n", |
Jonas Aaberg | 7c34d7c | 2011-08-17 13:20:21 +0200 | [diff] [blame] | 72 | pss[i]); |
Jonas Aaberg | 5a4bac6 | 2011-08-18 10:14:38 +0200 | [diff] [blame] | 73 | machine_restart("charging"); |
Jonas Aaberg | 7c34d7c | 2011-08-17 13:20:21 +0200 | [diff] [blame] | 74 | } |
| 75 | } |
Lee Jones | 379749c | 2013-01-14 13:26:15 +0000 | [diff] [blame] | 76 | |
Jonas Aaberg | 0903940 | 2011-08-17 15:58:52 +0200 | [diff] [blame] | 77 | shutdown: |
Lee Jones | 379749c | 2013-01-14 13:26:15 +0000 | [diff] [blame] | 78 | sigfillset(&all); |
| 79 | |
| 80 | if (!sigprocmask(SIG_BLOCK, &all, &old)) { |
| 81 | (void)ab8500_sysctrl_set(AB8500_STW4500CTRL1, |
| 82 | AB8500_STW4500CTRL1_SWOFF | |
| 83 | AB8500_STW4500CTRL1_SWRESET4500N); |
| 84 | (void)sigprocmask(SIG_SETMASK, &old, NULL); |
| 85 | } |
| 86 | } |
| 87 | |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 88 | static inline bool valid_bank(u8 bank) |
| 89 | { |
| 90 | return ((bank == AB8500_SYS_CTRL1_BLOCK) || |
| 91 | (bank == AB8500_SYS_CTRL2_BLOCK)); |
| 92 | } |
| 93 | |
| 94 | int ab8500_sysctrl_read(u16 reg, u8 *value) |
| 95 | { |
| 96 | u8 bank; |
| 97 | |
| 98 | if (sysctrl_dev == NULL) |
Marcus Danielsson | 2377e52 | 2012-06-18 15:00:40 +0200 | [diff] [blame] | 99 | return -EINVAL; |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 100 | |
| 101 | bank = (reg >> 8); |
| 102 | if (!valid_bank(bank)) |
| 103 | return -EINVAL; |
| 104 | |
| 105 | return abx500_get_register_interruptible(sysctrl_dev, bank, |
| 106 | (u8)(reg & 0xFF), value); |
| 107 | } |
Jonas Aaberg | c73db9f | 2011-11-11 07:52:10 +0100 | [diff] [blame] | 108 | EXPORT_SYMBOL(ab8500_sysctrl_read); |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 109 | |
| 110 | int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value) |
| 111 | { |
| 112 | u8 bank; |
| 113 | |
| 114 | if (sysctrl_dev == NULL) |
Marcus Danielsson | 2377e52 | 2012-06-18 15:00:40 +0200 | [diff] [blame] | 115 | return -EINVAL; |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 116 | |
| 117 | bank = (reg >> 8); |
| 118 | if (!valid_bank(bank)) |
| 119 | return -EINVAL; |
| 120 | |
| 121 | return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank, |
| 122 | (u8)(reg & 0xFF), mask, value); |
| 123 | } |
Jonas Aaberg | c73db9f | 2011-11-11 07:52:10 +0100 | [diff] [blame] | 124 | EXPORT_SYMBOL(ab8500_sysctrl_write); |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 125 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 126 | static int ab8500_sysctrl_probe(struct platform_device *pdev) |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 127 | { |
Rabin Vincent | c553552 | 2012-08-22 13:08:11 +0200 | [diff] [blame] | 128 | struct ab8500 *ab8500 = dev_get_drvdata(pdev->dev.parent); |
Lee Jones | 379749c | 2013-01-14 13:26:15 +0000 | [diff] [blame] | 129 | struct ab8500_platform_data *plat; |
Kennet Wallden | 1abf063 | 2011-09-27 09:23:56 +0200 | [diff] [blame] | 130 | struct ab8500_sysctrl_platform_data *pdata; |
Lee Jones | 379749c | 2013-01-14 13:26:15 +0000 | [diff] [blame] | 131 | |
Lee Jones | 379749c | 2013-01-14 13:26:15 +0000 | [diff] [blame] | 132 | plat = dev_get_platdata(pdev->dev.parent); |
Marcus Danielsson | 2377e52 | 2012-06-18 15:00:40 +0200 | [diff] [blame] | 133 | |
Fabio Baltieri | 5649d8f | 2013-04-26 14:17:18 +0200 | [diff] [blame] | 134 | if (!plat) |
Marcus Danielsson | 2377e52 | 2012-06-18 15:00:40 +0200 | [diff] [blame] | 135 | return -EINVAL; |
| 136 | |
Fabio Baltieri | 9f7af61 | 2013-04-26 14:17:17 +0200 | [diff] [blame] | 137 | sysctrl_dev = &pdev->dev; |
| 138 | |
Fabio Baltieri | 0b8ebdb | 2013-05-09 13:08:08 +0200 | [diff] [blame] | 139 | if (!pm_power_off) |
Lee Jones | 379749c | 2013-01-14 13:26:15 +0000 | [diff] [blame] | 140 | pm_power_off = ab8500_power_off; |
Kennet Wallden | 1abf063 | 2011-09-27 09:23:56 +0200 | [diff] [blame] | 141 | |
| 142 | pdata = plat->sysctrl; |
Rabin Vincent | c553552 | 2012-08-22 13:08:11 +0200 | [diff] [blame] | 143 | if (pdata) { |
| 144 | int last, ret, i, j; |
Kennet Wallden | 1abf063 | 2011-09-27 09:23:56 +0200 | [diff] [blame] | 145 | |
Rabin Vincent | c553552 | 2012-08-22 13:08:11 +0200 | [diff] [blame] | 146 | if (is_ab8505(ab8500)) |
| 147 | last = AB8500_SYSCLKREQ4RFCLKBUF; |
| 148 | else |
| 149 | last = AB8500_SYSCLKREQ8RFCLKBUF; |
| 150 | |
| 151 | for (i = AB8500_SYSCLKREQ1RFCLKBUF; i <= last; i++) { |
| 152 | j = i - AB8500_SYSCLKREQ1RFCLKBUF; |
| 153 | ret = ab8500_sysctrl_write(i, 0xff, |
| 154 | pdata->initial_req_buf_config[j]); |
| 155 | dev_dbg(&pdev->dev, |
| 156 | "Setting SysClkReq%dRfClkBuf 0x%X\n", |
| 157 | j + 1, |
| 158 | pdata->initial_req_buf_config[j]); |
| 159 | if (ret < 0) { |
| 160 | dev_err(&pdev->dev, |
| 161 | "unable to set sysClkReq%dRfClkBuf: " |
| 162 | "%d\n", j + 1, ret); |
| 163 | } |
Kennet Wallden | 1abf063 | 2011-09-27 09:23:56 +0200 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 170 | static int ab8500_sysctrl_remove(struct platform_device *pdev) |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 171 | { |
| 172 | sysctrl_dev = NULL; |
Fabio Baltieri | 0b8ebdb | 2013-05-09 13:08:08 +0200 | [diff] [blame] | 173 | |
| 174 | if (pm_power_off == ab8500_power_off) |
| 175 | pm_power_off = NULL; |
| 176 | |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static struct platform_driver ab8500_sysctrl_driver = { |
| 181 | .driver = { |
| 182 | .name = "ab8500-sysctrl", |
| 183 | .owner = THIS_MODULE, |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 184 | }, |
| 185 | .probe = ab8500_sysctrl_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 186 | .remove = ab8500_sysctrl_remove, |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | static int __init ab8500_sysctrl_init(void) |
| 190 | { |
| 191 | return platform_driver_register(&ab8500_sysctrl_driver); |
| 192 | } |
Ulf Hansson | caa62d6 | 2013-04-03 01:06:27 +0200 | [diff] [blame] | 193 | arch_initcall(ab8500_sysctrl_init); |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 194 | |
| 195 | MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com"); |
| 196 | MODULE_DESCRIPTION("AB8500 system control driver"); |
| 197 | MODULE_LICENSE("GPL v2"); |