1 /* 2 * sec-core.c 3 * 4 * Copyright (c) 2012 Samsung Electronics Co., Ltd 5 * http://www.samsung.com 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the 9 * Free Software Foundation; either version 2 of the License, or (at your 10 * option) any later version. 11 * 12 */ 13 14 #include <linux/module.h> 15 #include <linux/moduleparam.h> 16 #include <linux/init.h> 17 #include <linux/err.h> 18 #include <linux/slab.h> 19 #include <linux/i2c.h> 20 #include <linux/of.h> 21 #include <linux/of_irq.h> 22 #include <linux/interrupt.h> 23 #include <linux/pm_runtime.h> 24 #include <linux/mutex.h> 25 #include <linux/mfd/core.h> 26 #include <linux/mfd/samsung/core.h> 27 #include <linux/mfd/samsung/irq.h> 28 #include <linux/mfd/samsung/s2mpa01.h> 29 #include <linux/mfd/samsung/s2mps11.h> 30 #include <linux/mfd/samsung/s2mps13.h> 31 #include <linux/mfd/samsung/s2mps14.h> 32 #include <linux/mfd/samsung/s2mpu02.h> 33 #include <linux/mfd/samsung/s5m8763.h> 34 #include <linux/mfd/samsung/s5m8767.h> 35 #include <linux/regmap.h> 36 37 static const struct mfd_cell s5m8751_devs[] = { 38 { 39 .name = "s5m8751-pmic", 40 }, { 41 .name = "s5m-charger", 42 }, { 43 .name = "s5m8751-codec", 44 }, 45 }; 46 47 static const struct mfd_cell s5m8763_devs[] = { 48 { 49 .name = "s5m8763-pmic", 50 }, { 51 .name = "s5m-rtc", 52 }, { 53 .name = "s5m-charger", 54 }, 55 }; 56 57 static const struct mfd_cell s5m8767_devs[] = { 58 { 59 .name = "s5m8767-pmic", 60 }, { 61 .name = "s5m-rtc", 62 }, { 63 .name = "s5m8767-clk", 64 .of_compatible = "samsung,s5m8767-clk", 65 } 66 }; 67 68 static const struct mfd_cell s2mps11_devs[] = { 69 { 70 .name = "s2mps11-pmic", 71 }, { 72 .name = "s2mps14-rtc", 73 }, { 74 .name = "s2mps11-clk", 75 .of_compatible = "samsung,s2mps11-clk", 76 } 77 }; 78 79 static const struct mfd_cell s2mps13_devs[] = { 80 { .name = "s2mps13-pmic", }, 81 { .name = "s2mps13-rtc", }, 82 { 83 .name = "s2mps13-clk", 84 .of_compatible = "samsung,s2mps13-clk", 85 }, 86 }; 87 88 static const struct mfd_cell s2mps14_devs[] = { 89 { 90 .name = "s2mps14-pmic", 91 }, { 92 .name = "s2mps14-rtc", 93 }, { 94 .name = "s2mps14-clk", 95 .of_compatible = "samsung,s2mps14-clk", 96 } 97 }; 98 99 static const struct mfd_cell s2mpa01_devs[] = { 100 { 101 .name = "s2mpa01-pmic", 102 }, 103 }; 104 105 static const struct mfd_cell s2mpu02_devs[] = { 106 { .name = "s2mpu02-pmic", }, 107 { .name = "s2mpu02-rtc", }, 108 { 109 .name = "s2mpu02-clk", 110 .of_compatible = "samsung,s2mpu02-clk", 111 } 112 }; 113 114 #ifdef CONFIG_OF 115 static const struct of_device_id sec_dt_match[] = { 116 { .compatible = "samsung,s5m8767-pmic", 117 .data = (void *)S5M8767X, 118 }, { 119 .compatible = "samsung,s2mps11-pmic", 120 .data = (void *)S2MPS11X, 121 }, { 122 .compatible = "samsung,s2mps13-pmic", 123 .data = (void *)S2MPS13X, 124 }, { 125 .compatible = "samsung,s2mps14-pmic", 126 .data = (void *)S2MPS14X, 127 }, { 128 .compatible = "samsung,s2mpa01-pmic", 129 .data = (void *)S2MPA01, 130 }, { 131 .compatible = "samsung,s2mpu02-pmic", 132 .data = (void *)S2MPU02, 133 }, { 134 /* Sentinel */ 135 }, 136 }; 137 #endif 138 139 static bool s2mpa01_volatile(struct device *dev, unsigned int reg) 140 { 141 switch (reg) { 142 case S2MPA01_REG_INT1M: 143 case S2MPA01_REG_INT2M: 144 case S2MPA01_REG_INT3M: 145 return false; 146 default: 147 return true; 148 } 149 } 150 151 static bool s2mps11_volatile(struct device *dev, unsigned int reg) 152 { 153 switch (reg) { 154 case S2MPS11_REG_INT1M: 155 case S2MPS11_REG_INT2M: 156 case S2MPS11_REG_INT3M: 157 return false; 158 default: 159 return true; 160 } 161 } 162 163 static bool s2mpu02_volatile(struct device *dev, unsigned int reg) 164 { 165 switch (reg) { 166 case S2MPU02_REG_INT1M: 167 case S2MPU02_REG_INT2M: 168 case S2MPU02_REG_INT3M: 169 return false; 170 default: 171 return true; 172 } 173 } 174 175 static bool s5m8763_volatile(struct device *dev, unsigned int reg) 176 { 177 switch (reg) { 178 case S5M8763_REG_IRQM1: 179 case S5M8763_REG_IRQM2: 180 case S5M8763_REG_IRQM3: 181 case S5M8763_REG_IRQM4: 182 return false; 183 default: 184 return true; 185 } 186 } 187 188 static const struct regmap_config sec_regmap_config = { 189 .reg_bits = 8, 190 .val_bits = 8, 191 }; 192 193 static const struct regmap_config s2mpa01_regmap_config = { 194 .reg_bits = 8, 195 .val_bits = 8, 196 197 .max_register = S2MPA01_REG_LDO_OVCB4, 198 .volatile_reg = s2mpa01_volatile, 199 .cache_type = REGCACHE_FLAT, 200 }; 201 202 static const struct regmap_config s2mps11_regmap_config = { 203 .reg_bits = 8, 204 .val_bits = 8, 205 206 .max_register = S2MPS11_REG_L38CTRL, 207 .volatile_reg = s2mps11_volatile, 208 .cache_type = REGCACHE_FLAT, 209 }; 210 211 static const struct regmap_config s2mps13_regmap_config = { 212 .reg_bits = 8, 213 .val_bits = 8, 214 215 .max_register = S2MPS13_REG_LDODSCH5, 216 .volatile_reg = s2mps11_volatile, 217 .cache_type = REGCACHE_FLAT, 218 }; 219 220 static const struct regmap_config s2mps14_regmap_config = { 221 .reg_bits = 8, 222 .val_bits = 8, 223 224 .max_register = S2MPS14_REG_LDODSCH3, 225 .volatile_reg = s2mps11_volatile, 226 .cache_type = REGCACHE_FLAT, 227 }; 228 229 static const struct regmap_config s2mpu02_regmap_config = { 230 .reg_bits = 8, 231 .val_bits = 8, 232 233 .max_register = S2MPU02_REG_DVSDATA, 234 .volatile_reg = s2mpu02_volatile, 235 .cache_type = REGCACHE_FLAT, 236 }; 237 238 static const struct regmap_config s5m8763_regmap_config = { 239 .reg_bits = 8, 240 .val_bits = 8, 241 242 .max_register = S5M8763_REG_LBCNFG2, 243 .volatile_reg = s5m8763_volatile, 244 .cache_type = REGCACHE_FLAT, 245 }; 246 247 static const struct regmap_config s5m8767_regmap_config = { 248 .reg_bits = 8, 249 .val_bits = 8, 250 251 .max_register = S5M8767_REG_LDO28CTRL, 252 .volatile_reg = s2mps11_volatile, 253 .cache_type = REGCACHE_FLAT, 254 }; 255 256 static void sec_pmic_dump_rev(struct sec_pmic_dev *sec_pmic) 257 { 258 unsigned int val; 259 260 /* For each device type, the REG_ID is always the first register */ 261 if (!regmap_read(sec_pmic->regmap_pmic, S2MPS11_REG_ID, &val)) 262 dev_dbg(sec_pmic->dev, "Revision: 0x%x\n", val); 263 } 264 265 static void sec_pmic_configure(struct sec_pmic_dev *sec_pmic) 266 { 267 int err; 268 269 if (sec_pmic->device_type != S2MPS13X) 270 return; 271 272 if (sec_pmic->pdata->disable_wrstbi) { 273 /* 274 * If WRSTBI pin is pulled down this feature must be disabled 275 * because each Suspend to RAM will trigger buck voltage reset 276 * to default values. 277 */ 278 err = regmap_update_bits(sec_pmic->regmap_pmic, 279 S2MPS13_REG_WRSTBI, 280 S2MPS13_REG_WRSTBI_MASK, 0x0); 281 if (err) 282 dev_warn(sec_pmic->dev, 283 "Cannot initialize WRSTBI config: %d\n", 284 err); 285 } 286 } 287 288 #ifdef CONFIG_OF 289 /* 290 * Only the common platform data elements for s5m8767 are parsed here from the 291 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and 292 * others have to parse their own platform data elements from device tree. 293 * 294 * The s5m8767 platform data structure is instantiated here and the drivers for 295 * the sub-modules need not instantiate another instance while parsing their 296 * platform data. 297 */ 298 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata( 299 struct device *dev) 300 { 301 struct sec_platform_data *pd; 302 303 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL); 304 if (!pd) 305 return ERR_PTR(-ENOMEM); 306 307 /* 308 * ToDo: the 'wakeup' member in the platform data is more of a linux 309 * specfic information. Hence, there is no binding for that yet and 310 * not parsed here. 311 */ 312 313 pd->manual_poweroff = of_property_read_bool(dev->of_node, 314 "samsung,s2mps11-acokb-ground"); 315 pd->disable_wrstbi = of_property_read_bool(dev->of_node, 316 "samsung,s2mps11-wrstbi-ground"); 317 return pd; 318 } 319 #else 320 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata( 321 struct device *dev) 322 { 323 return NULL; 324 } 325 #endif 326 327 static inline unsigned long sec_i2c_get_driver_data(struct i2c_client *i2c, 328 const struct i2c_device_id *id) 329 { 330 #ifdef CONFIG_OF 331 if (i2c->dev.of_node) { 332 const struct of_device_id *match; 333 334 match = of_match_node(sec_dt_match, i2c->dev.of_node); 335 return (unsigned long)match->data; 336 } 337 #endif 338 return id->driver_data; 339 } 340 341 static int sec_pmic_probe(struct i2c_client *i2c, 342 const struct i2c_device_id *id) 343 { 344 struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev); 345 const struct regmap_config *regmap; 346 const struct mfd_cell *sec_devs; 347 struct sec_pmic_dev *sec_pmic; 348 unsigned long device_type; 349 int ret, num_sec_devs; 350 351 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev), 352 GFP_KERNEL); 353 if (sec_pmic == NULL) 354 return -ENOMEM; 355 356 i2c_set_clientdata(i2c, sec_pmic); 357 sec_pmic->dev = &i2c->dev; 358 sec_pmic->i2c = i2c; 359 sec_pmic->irq = i2c->irq; 360 device_type = sec_i2c_get_driver_data(i2c, id); 361 362 if (sec_pmic->dev->of_node) { 363 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev); 364 if (IS_ERR(pdata)) { 365 ret = PTR_ERR(pdata); 366 return ret; 367 } 368 pdata->device_type = device_type; 369 } 370 if (pdata) { 371 sec_pmic->device_type = pdata->device_type; 372 sec_pmic->irq_base = pdata->irq_base; 373 sec_pmic->wakeup = pdata->wakeup; 374 sec_pmic->pdata = pdata; 375 } 376 377 switch (sec_pmic->device_type) { 378 case S2MPA01: 379 regmap = &s2mpa01_regmap_config; 380 break; 381 case S2MPS11X: 382 regmap = &s2mps11_regmap_config; 383 break; 384 case S2MPS13X: 385 regmap = &s2mps13_regmap_config; 386 break; 387 case S2MPS14X: 388 regmap = &s2mps14_regmap_config; 389 break; 390 case S5M8763X: 391 regmap = &s5m8763_regmap_config; 392 break; 393 case S5M8767X: 394 regmap = &s5m8767_regmap_config; 395 break; 396 case S2MPU02: 397 regmap = &s2mpu02_regmap_config; 398 break; 399 default: 400 regmap = &sec_regmap_config; 401 break; 402 } 403 404 sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap); 405 if (IS_ERR(sec_pmic->regmap_pmic)) { 406 ret = PTR_ERR(sec_pmic->regmap_pmic); 407 dev_err(&i2c->dev, "Failed to allocate register map: %d\n", 408 ret); 409 return ret; 410 } 411 412 if (pdata && pdata->cfg_pmic_irq) 413 pdata->cfg_pmic_irq(); 414 415 sec_irq_init(sec_pmic); 416 417 pm_runtime_set_active(sec_pmic->dev); 418 419 switch (sec_pmic->device_type) { 420 case S5M8751X: 421 sec_devs = s5m8751_devs; 422 num_sec_devs = ARRAY_SIZE(s5m8751_devs); 423 break; 424 case S5M8763X: 425 sec_devs = s5m8763_devs; 426 num_sec_devs = ARRAY_SIZE(s5m8763_devs); 427 break; 428 case S5M8767X: 429 sec_devs = s5m8767_devs; 430 num_sec_devs = ARRAY_SIZE(s5m8767_devs); 431 break; 432 case S2MPA01: 433 sec_devs = s2mpa01_devs; 434 num_sec_devs = ARRAY_SIZE(s2mpa01_devs); 435 break; 436 case S2MPS11X: 437 sec_devs = s2mps11_devs; 438 num_sec_devs = ARRAY_SIZE(s2mps11_devs); 439 break; 440 case S2MPS13X: 441 sec_devs = s2mps13_devs; 442 num_sec_devs = ARRAY_SIZE(s2mps13_devs); 443 break; 444 case S2MPS14X: 445 sec_devs = s2mps14_devs; 446 num_sec_devs = ARRAY_SIZE(s2mps14_devs); 447 break; 448 case S2MPU02: 449 sec_devs = s2mpu02_devs; 450 num_sec_devs = ARRAY_SIZE(s2mpu02_devs); 451 break; 452 default: 453 /* If this happens the probe function is problem */ 454 BUG(); 455 } 456 ret = mfd_add_devices(sec_pmic->dev, -1, sec_devs, num_sec_devs, NULL, 457 0, NULL); 458 if (ret) 459 goto err_mfd; 460 461 device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup); 462 sec_pmic_configure(sec_pmic); 463 sec_pmic_dump_rev(sec_pmic); 464 465 return ret; 466 467 err_mfd: 468 sec_irq_exit(sec_pmic); 469 return ret; 470 } 471 472 static int sec_pmic_remove(struct i2c_client *i2c) 473 { 474 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c); 475 476 mfd_remove_devices(sec_pmic->dev); 477 sec_irq_exit(sec_pmic); 478 return 0; 479 } 480 481 static void sec_pmic_shutdown(struct i2c_client *i2c) 482 { 483 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c); 484 unsigned int reg, mask; 485 486 if (!sec_pmic->pdata->manual_poweroff) 487 return; 488 489 switch (sec_pmic->device_type) { 490 case S2MPS11X: 491 reg = S2MPS11_REG_CTRL1; 492 mask = S2MPS11_CTRL1_PWRHOLD_MASK; 493 break; 494 default: 495 /* 496 * Currently only one board with S2MPS11 needs this, so just 497 * ignore the rest. 498 */ 499 dev_warn(sec_pmic->dev, 500 "Unsupported device %lu for manual power off\n", 501 sec_pmic->device_type); 502 return; 503 } 504 505 regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, 0); 506 } 507 508 #ifdef CONFIG_PM_SLEEP 509 static int sec_pmic_suspend(struct device *dev) 510 { 511 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); 512 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c); 513 514 if (device_may_wakeup(dev)) 515 enable_irq_wake(sec_pmic->irq); 516 /* 517 * PMIC IRQ must be disabled during suspend for RTC alarm 518 * to work properly. 519 * When device is woken up from suspend, an 520 * interrupt occurs before resuming I2C bus controller. 521 * The interrupt is handled by regmap_irq_thread which tries 522 * to read RTC registers. This read fails (I2C is still 523 * suspended) and RTC Alarm interrupt is disabled. 524 */ 525 disable_irq(sec_pmic->irq); 526 527 return 0; 528 } 529 530 static int sec_pmic_resume(struct device *dev) 531 { 532 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); 533 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c); 534 535 if (device_may_wakeup(dev)) 536 disable_irq_wake(sec_pmic->irq); 537 enable_irq(sec_pmic->irq); 538 539 return 0; 540 } 541 #endif /* CONFIG_PM_SLEEP */ 542 543 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume); 544 545 static const struct i2c_device_id sec_pmic_id[] = { 546 { "sec_pmic", 0 }, 547 { } 548 }; 549 MODULE_DEVICE_TABLE(i2c, sec_pmic_id); 550 551 static struct i2c_driver sec_pmic_driver = { 552 .driver = { 553 .name = "sec_pmic", 554 .pm = &sec_pmic_pm_ops, 555 .of_match_table = of_match_ptr(sec_dt_match), 556 }, 557 .probe = sec_pmic_probe, 558 .remove = sec_pmic_remove, 559 .shutdown = sec_pmic_shutdown, 560 .id_table = sec_pmic_id, 561 }; 562 563 static int __init sec_pmic_init(void) 564 { 565 return i2c_add_driver(&sec_pmic_driver); 566 } 567 568 subsys_initcall(sec_pmic_init); 569 570 static void __exit sec_pmic_exit(void) 571 { 572 i2c_del_driver(&sec_pmic_driver); 573 } 574 module_exit(sec_pmic_exit); 575 576 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>"); 577 MODULE_DESCRIPTION("Core support for the S5M MFD"); 578 MODULE_LICENSE("GPL"); 579