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 | |
| 18 | static struct device *sysctrl_dev; |
| 19 | |
Lee Jones | 379749c | 2013-01-14 13:26:15 +0000 | [diff] [blame] | 20 | void ab8500_power_off(void) |
| 21 | { |
| 22 | sigset_t old; |
| 23 | sigset_t all; |
Jonas Aaberg | 7c34d7c | 2011-08-17 13:20:21 +0200 | [diff] [blame^] | 24 | static char *pss[] = {"ab8500_ac", "ab8500_usb"}; |
| 25 | int i; |
| 26 | |
| 27 | /* |
| 28 | * If we have a charger connected and we're powering off, |
| 29 | * reboot into charge-only mode. |
| 30 | */ |
| 31 | |
| 32 | for (i = 0; i < ARRAY_SIZE(pss); i++) { |
| 33 | union power_supply_propval val; |
| 34 | struct power_supply *psy; |
| 35 | int ret; |
| 36 | |
| 37 | psy = power_supply_get_by_name(pss[i]); |
| 38 | if (!psy) |
| 39 | continue; |
| 40 | ret = psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &val); |
| 41 | |
| 42 | if (!ret && val.intval) { |
| 43 | printk(KERN_INFO |
| 44 | "Charger \"%s\" is connected. Rebooting.\n", |
| 45 | pss[i]); |
| 46 | machine_restart(NULL); |
| 47 | } |
| 48 | } |
Lee Jones | 379749c | 2013-01-14 13:26:15 +0000 | [diff] [blame] | 49 | |
| 50 | sigfillset(&all); |
| 51 | |
| 52 | if (!sigprocmask(SIG_BLOCK, &all, &old)) { |
| 53 | (void)ab8500_sysctrl_set(AB8500_STW4500CTRL1, |
| 54 | AB8500_STW4500CTRL1_SWOFF | |
| 55 | AB8500_STW4500CTRL1_SWRESET4500N); |
| 56 | (void)sigprocmask(SIG_SETMASK, &old, NULL); |
| 57 | } |
| 58 | } |
| 59 | |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 60 | static inline bool valid_bank(u8 bank) |
| 61 | { |
| 62 | return ((bank == AB8500_SYS_CTRL1_BLOCK) || |
| 63 | (bank == AB8500_SYS_CTRL2_BLOCK)); |
| 64 | } |
| 65 | |
| 66 | int ab8500_sysctrl_read(u16 reg, u8 *value) |
| 67 | { |
| 68 | u8 bank; |
| 69 | |
| 70 | if (sysctrl_dev == NULL) |
| 71 | return -EAGAIN; |
| 72 | |
| 73 | bank = (reg >> 8); |
| 74 | if (!valid_bank(bank)) |
| 75 | return -EINVAL; |
| 76 | |
| 77 | return abx500_get_register_interruptible(sysctrl_dev, bank, |
| 78 | (u8)(reg & 0xFF), value); |
| 79 | } |
| 80 | |
| 81 | int ab8500_sysctrl_write(u16 reg, u8 mask, u8 value) |
| 82 | { |
| 83 | u8 bank; |
| 84 | |
| 85 | if (sysctrl_dev == NULL) |
| 86 | return -EAGAIN; |
| 87 | |
| 88 | bank = (reg >> 8); |
| 89 | if (!valid_bank(bank)) |
| 90 | return -EINVAL; |
| 91 | |
| 92 | return abx500_mask_and_set_register_interruptible(sysctrl_dev, bank, |
| 93 | (u8)(reg & 0xFF), mask, value); |
| 94 | } |
| 95 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 96 | static int ab8500_sysctrl_probe(struct platform_device *pdev) |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 97 | { |
Lee Jones | 379749c | 2013-01-14 13:26:15 +0000 | [diff] [blame] | 98 | struct ab8500_platform_data *plat; |
| 99 | |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 100 | sysctrl_dev = &pdev->dev; |
Lee Jones | 379749c | 2013-01-14 13:26:15 +0000 | [diff] [blame] | 101 | plat = dev_get_platdata(pdev->dev.parent); |
| 102 | if (plat->pm_power_off) |
| 103 | pm_power_off = ab8500_power_off; |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 107 | static int ab8500_sysctrl_remove(struct platform_device *pdev) |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 108 | { |
| 109 | sysctrl_dev = NULL; |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static struct platform_driver ab8500_sysctrl_driver = { |
| 114 | .driver = { |
| 115 | .name = "ab8500-sysctrl", |
| 116 | .owner = THIS_MODULE, |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 117 | }, |
| 118 | .probe = ab8500_sysctrl_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 119 | .remove = ab8500_sysctrl_remove, |
Mattias Nilsson | 90550d1 | 2011-02-14 11:17:12 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | static int __init ab8500_sysctrl_init(void) |
| 123 | { |
| 124 | return platform_driver_register(&ab8500_sysctrl_driver); |
| 125 | } |
| 126 | subsys_initcall(ab8500_sysctrl_init); |
| 127 | |
| 128 | MODULE_AUTHOR("Mattias Nilsson <mattias.i.nilsson@stericsson.com"); |
| 129 | MODULE_DESCRIPTION("AB8500 system control driver"); |
| 130 | MODULE_LICENSE("GPL v2"); |