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