Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 1 | /* |
Joonyoung Shim | 2c7e6f5 | 2010-09-10 18:36:39 +0200 | [diff] [blame] | 2 | * max8998.c - mfd core driver for the Maxim 8998 |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2009-2010 Samsung Electronics |
| 5 | * Kyungmin Park <kyungmin.park@samsung.com> |
| 6 | * Marek Szyprowski <m.szyprowski@samsung.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/moduleparam.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/i2c.h> |
MyungJoo Ham | cdd137c | 2010-12-23 17:53:36 +0900 | [diff] [blame^] | 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/pm_runtime.h> |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 30 | #include <linux/mutex.h> |
| 31 | #include <linux/mfd/core.h> |
| 32 | #include <linux/mfd/max8998.h> |
| 33 | #include <linux/mfd/max8998-private.h> |
| 34 | |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 35 | #define RTC_I2C_ADDR (0x0c >> 1) |
| 36 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 37 | static struct mfd_cell max8998_devs[] = { |
| 38 | { |
| 39 | .name = "max8998-pmic", |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 40 | }, { |
| 41 | .name = "max8998-rtc", |
| 42 | }, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 45 | int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 46 | { |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 47 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 48 | int ret; |
| 49 | |
| 50 | mutex_lock(&max8998->iolock); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 51 | ret = i2c_smbus_read_byte_data(i2c, reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 52 | mutex_unlock(&max8998->iolock); |
| 53 | if (ret < 0) |
| 54 | return ret; |
| 55 | |
| 56 | ret &= 0xff; |
| 57 | *dest = ret; |
| 58 | return 0; |
| 59 | } |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 60 | EXPORT_SYMBOL(max8998_read_reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 61 | |
Joonyoung Shim | 2c7e6f5 | 2010-09-10 18:36:39 +0200 | [diff] [blame] | 62 | int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf) |
| 63 | { |
| 64 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
| 65 | int ret; |
| 66 | |
| 67 | mutex_lock(&max8998->iolock); |
| 68 | ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf); |
| 69 | mutex_unlock(&max8998->iolock); |
| 70 | if (ret < 0) |
| 71 | return ret; |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | EXPORT_SYMBOL(max8998_bulk_read); |
| 76 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 77 | int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 78 | { |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 79 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 80 | int ret; |
| 81 | |
| 82 | mutex_lock(&max8998->iolock); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 83 | ret = i2c_smbus_write_byte_data(i2c, reg, value); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 84 | mutex_unlock(&max8998->iolock); |
| 85 | return ret; |
| 86 | } |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 87 | EXPORT_SYMBOL(max8998_write_reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 88 | |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 89 | int max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf) |
| 90 | { |
| 91 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
| 92 | int ret; |
| 93 | |
| 94 | mutex_lock(&max8998->iolock); |
| 95 | ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf); |
| 96 | mutex_unlock(&max8998->iolock); |
| 97 | if (ret < 0) |
| 98 | return ret; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | EXPORT_SYMBOL(max8998_bulk_write); |
| 103 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 104 | int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 105 | { |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 106 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 107 | int ret; |
| 108 | |
| 109 | mutex_lock(&max8998->iolock); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 110 | ret = i2c_smbus_read_byte_data(i2c, reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 111 | if (ret >= 0) { |
| 112 | u8 old_val = ret & 0xff; |
| 113 | u8 new_val = (val & mask) | (old_val & (~mask)); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 114 | ret = i2c_smbus_write_byte_data(i2c, reg, new_val); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 115 | } |
| 116 | mutex_unlock(&max8998->iolock); |
| 117 | return ret; |
| 118 | } |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 119 | EXPORT_SYMBOL(max8998_update_reg); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 120 | |
| 121 | static int max8998_i2c_probe(struct i2c_client *i2c, |
| 122 | const struct i2c_device_id *id) |
| 123 | { |
Joonyoung Shim | 2c7e6f5 | 2010-09-10 18:36:39 +0200 | [diff] [blame] | 124 | struct max8998_platform_data *pdata = i2c->dev.platform_data; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 125 | struct max8998_dev *max8998; |
| 126 | int ret = 0; |
| 127 | |
| 128 | max8998 = kzalloc(sizeof(struct max8998_dev), GFP_KERNEL); |
Axel Lin | 8f1f151 | 2010-08-09 14:48:16 +0800 | [diff] [blame] | 129 | if (max8998 == NULL) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 130 | return -ENOMEM; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 131 | |
| 132 | i2c_set_clientdata(i2c, max8998); |
| 133 | max8998->dev = &i2c->dev; |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 134 | max8998->i2c = i2c; |
Joonyoung Shim | 2c7e6f5 | 2010-09-10 18:36:39 +0200 | [diff] [blame] | 135 | max8998->irq = i2c->irq; |
Lukasz Majewski | 509bd47 | 2010-09-27 14:32:24 +0200 | [diff] [blame] | 136 | max8998->type = id->driver_data; |
Joonyoung Shim | 2c7e6f5 | 2010-09-10 18:36:39 +0200 | [diff] [blame] | 137 | if (pdata) { |
| 138 | max8998->ono = pdata->ono; |
| 139 | max8998->irq_base = pdata->irq_base; |
MyungJoo Ham | cdd137c | 2010-12-23 17:53:36 +0900 | [diff] [blame^] | 140 | max8998->wakeup = pdata->wakeup; |
Joonyoung Shim | 2c7e6f5 | 2010-09-10 18:36:39 +0200 | [diff] [blame] | 141 | } |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 142 | mutex_init(&max8998->iolock); |
| 143 | |
Joonyoung Shim | 9b16c0a | 2010-08-06 11:28:08 +0900 | [diff] [blame] | 144 | max8998->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR); |
| 145 | i2c_set_clientdata(max8998->rtc, max8998); |
| 146 | |
Joonyoung Shim | 2c7e6f5 | 2010-09-10 18:36:39 +0200 | [diff] [blame] | 147 | max8998_irq_init(max8998); |
| 148 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 149 | ret = mfd_add_devices(max8998->dev, -1, |
| 150 | max8998_devs, ARRAY_SIZE(max8998_devs), |
| 151 | NULL, 0); |
MyungJoo Ham | cdd137c | 2010-12-23 17:53:36 +0900 | [diff] [blame^] | 152 | pm_runtime_set_active(max8998->dev); |
| 153 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 154 | if (ret < 0) |
| 155 | goto err; |
| 156 | |
| 157 | return ret; |
| 158 | |
| 159 | err: |
| 160 | mfd_remove_devices(max8998->dev); |
Axel Lin | 7484552 | 2010-10-22 08:31:49 +0800 | [diff] [blame] | 161 | max8998_irq_exit(max8998); |
| 162 | i2c_unregister_device(max8998->rtc); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 163 | kfree(max8998); |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | static int max8998_i2c_remove(struct i2c_client *i2c) |
| 168 | { |
| 169 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
| 170 | |
| 171 | mfd_remove_devices(max8998->dev); |
Axel Lin | 7484552 | 2010-10-22 08:31:49 +0800 | [diff] [blame] | 172 | max8998_irq_exit(max8998); |
| 173 | i2c_unregister_device(max8998->rtc); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 174 | kfree(max8998); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static const struct i2c_device_id max8998_i2c_id[] = { |
Lukasz Majewski | 509bd47 | 2010-09-27 14:32:24 +0200 | [diff] [blame] | 180 | { "max8998", TYPE_MAX8998 }, |
| 181 | { "lp3974", TYPE_LP3974}, |
Kyungmin Park | f8539dd | 2010-08-23 13:46:49 +0900 | [diff] [blame] | 182 | { } |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 183 | }; |
| 184 | MODULE_DEVICE_TABLE(i2c, max8998_i2c_id); |
| 185 | |
MyungJoo Ham | cdd137c | 2010-12-23 17:53:36 +0900 | [diff] [blame^] | 186 | static int max8998_suspend(struct device *dev) |
| 187 | { |
| 188 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
| 189 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
| 190 | |
| 191 | if (max8998->wakeup) |
| 192 | set_irq_wake(max8998->irq, 1); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int max8998_resume(struct device *dev) |
| 197 | { |
| 198 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
| 199 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
| 200 | |
| 201 | if (max8998->wakeup) |
| 202 | set_irq_wake(max8998->irq, 0); |
| 203 | /* |
| 204 | * In LP3974, if IRQ registers are not "read & clear" |
| 205 | * when it's set during sleep, the interrupt becomes |
| 206 | * disabled. |
| 207 | */ |
| 208 | return max8998_irq_resume(i2c_get_clientdata(i2c)); |
| 209 | } |
| 210 | |
| 211 | struct max8998_reg_dump { |
| 212 | u8 addr; |
| 213 | u8 val; |
| 214 | }; |
| 215 | #define SAVE_ITEM(x) { .addr = (x), .val = 0x0, } |
| 216 | struct max8998_reg_dump max8998_dump[] = { |
| 217 | SAVE_ITEM(MAX8998_REG_IRQM1), |
| 218 | SAVE_ITEM(MAX8998_REG_IRQM2), |
| 219 | SAVE_ITEM(MAX8998_REG_IRQM3), |
| 220 | SAVE_ITEM(MAX8998_REG_IRQM4), |
| 221 | SAVE_ITEM(MAX8998_REG_STATUSM1), |
| 222 | SAVE_ITEM(MAX8998_REG_STATUSM2), |
| 223 | SAVE_ITEM(MAX8998_REG_CHGR1), |
| 224 | SAVE_ITEM(MAX8998_REG_CHGR2), |
| 225 | SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1), |
| 226 | SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1), |
| 227 | SAVE_ITEM(MAX8998_REG_BUCK_ACTIVE_DISCHARGE3), |
| 228 | SAVE_ITEM(MAX8998_REG_ONOFF1), |
| 229 | SAVE_ITEM(MAX8998_REG_ONOFF2), |
| 230 | SAVE_ITEM(MAX8998_REG_ONOFF3), |
| 231 | SAVE_ITEM(MAX8998_REG_ONOFF4), |
| 232 | SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE1), |
| 233 | SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE2), |
| 234 | SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE3), |
| 235 | SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE4), |
| 236 | SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE1), |
| 237 | SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE2), |
| 238 | SAVE_ITEM(MAX8998_REG_LDO2_LDO3), |
| 239 | SAVE_ITEM(MAX8998_REG_LDO4), |
| 240 | SAVE_ITEM(MAX8998_REG_LDO5), |
| 241 | SAVE_ITEM(MAX8998_REG_LDO6), |
| 242 | SAVE_ITEM(MAX8998_REG_LDO7), |
| 243 | SAVE_ITEM(MAX8998_REG_LDO8_LDO9), |
| 244 | SAVE_ITEM(MAX8998_REG_LDO10_LDO11), |
| 245 | SAVE_ITEM(MAX8998_REG_LDO12), |
| 246 | SAVE_ITEM(MAX8998_REG_LDO13), |
| 247 | SAVE_ITEM(MAX8998_REG_LDO14), |
| 248 | SAVE_ITEM(MAX8998_REG_LDO15), |
| 249 | SAVE_ITEM(MAX8998_REG_LDO16), |
| 250 | SAVE_ITEM(MAX8998_REG_LDO17), |
| 251 | SAVE_ITEM(MAX8998_REG_BKCHR), |
| 252 | SAVE_ITEM(MAX8998_REG_LBCNFG1), |
| 253 | SAVE_ITEM(MAX8998_REG_LBCNFG2), |
| 254 | }; |
| 255 | /* Save registers before hibernation */ |
| 256 | static int max8998_freeze(struct device *dev) |
| 257 | { |
| 258 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
| 259 | int i; |
| 260 | |
| 261 | for (i = 0; i < ARRAY_SIZE(max8998_dump); i++) |
| 262 | max8998_read_reg(i2c, max8998_dump[i].addr, |
| 263 | &max8998_dump[i].val); |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /* Restore registers after hibernation */ |
| 269 | static int max8998_restore(struct device *dev) |
| 270 | { |
| 271 | struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); |
| 272 | int i; |
| 273 | |
| 274 | for (i = 0; i < ARRAY_SIZE(max8998_dump); i++) |
| 275 | max8998_write_reg(i2c, max8998_dump[i].addr, |
| 276 | max8998_dump[i].val); |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | const struct dev_pm_ops max8998_pm = { |
| 282 | .suspend = max8998_suspend, |
| 283 | .resume = max8998_resume, |
| 284 | .freeze = max8998_freeze, |
| 285 | .restore = max8998_restore, |
| 286 | }; |
| 287 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 288 | static struct i2c_driver max8998_i2c_driver = { |
| 289 | .driver = { |
| 290 | .name = "max8998", |
| 291 | .owner = THIS_MODULE, |
MyungJoo Ham | cdd137c | 2010-12-23 17:53:36 +0900 | [diff] [blame^] | 292 | .pm = &max8998_pm, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 293 | }, |
| 294 | .probe = max8998_i2c_probe, |
| 295 | .remove = max8998_i2c_remove, |
| 296 | .id_table = max8998_i2c_id, |
| 297 | }; |
| 298 | |
| 299 | static int __init max8998_i2c_init(void) |
| 300 | { |
| 301 | return i2c_add_driver(&max8998_i2c_driver); |
| 302 | } |
| 303 | /* init early so consumer devices can complete system boot */ |
| 304 | subsys_initcall(max8998_i2c_init); |
| 305 | |
| 306 | static void __exit max8998_i2c_exit(void) |
| 307 | { |
| 308 | i2c_del_driver(&max8998_i2c_driver); |
| 309 | } |
| 310 | module_exit(max8998_i2c_exit); |
| 311 | |
| 312 | MODULE_DESCRIPTION("MAXIM 8998 multi-function core driver"); |
| 313 | MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>"); |
| 314 | MODULE_LICENSE("GPL"); |