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/s2mps14.h> 31 #include <linux/mfd/samsung/s5m8763.h> 32 #include <linux/mfd/samsung/s5m8767.h> 33 #include <linux/regmap.h> 34 35 static const struct mfd_cell s5m8751_devs[] = { 36 { 37 .name = "s5m8751-pmic", 38 }, { 39 .name = "s5m-charger", 40 }, { 41 .name = "s5m8751-codec", 42 }, 43 }; 44 45 static const struct mfd_cell s5m8763_devs[] = { 46 { 47 .name = "s5m8763-pmic", 48 }, { 49 .name = "s5m-rtc", 50 }, { 51 .name = "s5m-charger", 52 }, 53 }; 54 55 static const struct mfd_cell s5m8767_devs[] = { 56 { 57 .name = "s5m8767-pmic", 58 }, { 59 .name = "s5m-rtc", 60 }, { 61 .name = "s5m8767-clk", 62 .of_compatible = "samsung,s5m8767-clk", 63 } 64 }; 65 66 static const struct mfd_cell s2mps11_devs[] = { 67 { 68 .name = "s2mps11-pmic", 69 }, { 70 .name = "s2mps11-clk", 71 .of_compatible = "samsung,s2mps11-clk", 72 } 73 }; 74 75 static const struct mfd_cell s2mps14_devs[] = { 76 { 77 .name = "s2mps14-pmic", 78 }, { 79 .name = "s2mps14-rtc", 80 }, { 81 .name = "s2mps14-clk", 82 .of_compatible = "samsung,s2mps14-clk", 83 } 84 }; 85 86 static const struct mfd_cell s2mpa01_devs[] = { 87 { 88 .name = "s2mpa01-pmic", 89 }, 90 }; 91 92 #ifdef CONFIG_OF 93 static struct of_device_id sec_dt_match[] = { 94 { .compatible = "samsung,s5m8767-pmic", 95 .data = (void *)S5M8767X, 96 }, { 97 .compatible = "samsung,s2mps11-pmic", 98 .data = (void *)S2MPS11X, 99 }, { 100 .compatible = "samsung,s2mps14-pmic", 101 .data = (void *)S2MPS14X, 102 }, { 103 .compatible = "samsung,s2mpa01-pmic", 104 .data = (void *)S2MPA01, 105 }, { 106 /* Sentinel */ 107 }, 108 }; 109 #endif 110 111 static bool s2mpa01_volatile(struct device *dev, unsigned int reg) 112 { 113 switch (reg) { 114 case S2MPA01_REG_INT1M: 115 case S2MPA01_REG_INT2M: 116 case S2MPA01_REG_INT3M: 117 return false; 118 default: 119 return true; 120 } 121 } 122 123 static bool s2mps11_volatile(struct device *dev, unsigned int reg) 124 { 125 switch (reg) { 126 case S2MPS11_REG_INT1M: 127 case S2MPS11_REG_INT2M: 128 case S2MPS11_REG_INT3M: 129 return false; 130 default: 131 return true; 132 } 133 } 134 135 static bool s5m8763_volatile(struct device *dev, unsigned int reg) 136 { 137 switch (reg) { 138 case S5M8763_REG_IRQM1: 139 case S5M8763_REG_IRQM2: 140 case S5M8763_REG_IRQM3: 141 case S5M8763_REG_IRQM4: 142 return false; 143 default: 144 return true; 145 } 146 } 147 148 static const struct regmap_config sec_regmap_config = { 149 .reg_bits = 8, 150 .val_bits = 8, 151 }; 152 153 static const struct regmap_config s2mpa01_regmap_config = { 154 .reg_bits = 8, 155 .val_bits = 8, 156 157 .max_register = S2MPA01_REG_LDO_OVCB4, 158 .volatile_reg = s2mpa01_volatile, 159 .cache_type = REGCACHE_FLAT, 160 }; 161 162 static const struct regmap_config s2mps11_regmap_config = { 163 .reg_bits = 8, 164 .val_bits = 8, 165 166 .max_register = S2MPS11_REG_L38CTRL, 167 .volatile_reg = s2mps11_volatile, 168 .cache_type = REGCACHE_FLAT, 169 }; 170 171 static const struct regmap_config s2mps14_regmap_config = { 172 .reg_bits = 8, 173 .val_bits = 8, 174 175 .max_register = S2MPS14_REG_LDODSCH3, 176 .volatile_reg = s2mps11_volatile, 177 .cache_type = REGCACHE_FLAT, 178 }; 179 180 static const struct regmap_config s5m8763_regmap_config = { 181 .reg_bits = 8, 182 .val_bits = 8, 183 184 .max_register = S5M8763_REG_LBCNFG2, 185 .volatile_reg = s5m8763_volatile, 186 .cache_type = REGCACHE_FLAT, 187 }; 188 189 static const struct regmap_config s5m8767_regmap_config = { 190 .reg_bits = 8, 191 .val_bits = 8, 192 193 .max_register = S5M8767_REG_LDO28CTRL, 194 .volatile_reg = s2mps11_volatile, 195 .cache_type = REGCACHE_FLAT, 196 }; 197 198 #ifdef CONFIG_OF 199 /* 200 * Only the common platform data elements for s5m8767 are parsed here from the 201 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and 202 * others have to parse their own platform data elements from device tree. 203 * 204 * The s5m8767 platform data structure is instantiated here and the drivers for 205 * the sub-modules need not instantiate another instance while parsing their 206 * platform data. 207 */ 208 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata( 209 struct device *dev) 210 { 211 struct sec_platform_data *pd; 212 213 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL); 214 if (!pd) { 215 dev_err(dev, "could not allocate memory for pdata\n"); 216 return ERR_PTR(-ENOMEM); 217 } 218 219 /* 220 * ToDo: the 'wakeup' member in the platform data is more of a linux 221 * specfic information. Hence, there is no binding for that yet and 222 * not parsed here. 223 */ 224 225 return pd; 226 } 227 #else 228 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata( 229 struct device *dev) 230 { 231 return NULL; 232 } 233 #endif 234 235 static inline unsigned long sec_i2c_get_driver_data(struct i2c_client *i2c, 236 const struct i2c_device_id *id) 237 { 238 #ifdef CONFIG_OF 239 if (i2c->dev.of_node) { 240 const struct of_device_id *match; 241 match = of_match_node(sec_dt_match, i2c->dev.of_node); 242 return (unsigned long)match->data; 243 } 244 #endif 245 return id->driver_data; 246 } 247 248 static int sec_pmic_probe(struct i2c_client *i2c, 249 const struct i2c_device_id *id) 250 { 251 struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev); 252 const struct regmap_config *regmap; 253 struct sec_pmic_dev *sec_pmic; 254 int ret; 255 256 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev), 257 GFP_KERNEL); 258 if (sec_pmic == NULL) 259 return -ENOMEM; 260 261 i2c_set_clientdata(i2c, sec_pmic); 262 sec_pmic->dev = &i2c->dev; 263 sec_pmic->i2c = i2c; 264 sec_pmic->irq = i2c->irq; 265 sec_pmic->type = sec_i2c_get_driver_data(i2c, id); 266 267 if (sec_pmic->dev->of_node) { 268 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev); 269 if (IS_ERR(pdata)) { 270 ret = PTR_ERR(pdata); 271 return ret; 272 } 273 pdata->device_type = sec_pmic->type; 274 } 275 if (pdata) { 276 sec_pmic->device_type = pdata->device_type; 277 sec_pmic->ono = pdata->ono; 278 sec_pmic->irq_base = pdata->irq_base; 279 sec_pmic->wakeup = pdata->wakeup; 280 sec_pmic->pdata = pdata; 281 } 282 283 switch (sec_pmic->device_type) { 284 case S2MPA01: 285 regmap = &s2mpa01_regmap_config; 286 break; 287 case S2MPS11X: 288 regmap = &s2mps11_regmap_config; 289 break; 290 case S2MPS14X: 291 regmap = &s2mps14_regmap_config; 292 break; 293 case S5M8763X: 294 regmap = &s5m8763_regmap_config; 295 break; 296 case S5M8767X: 297 regmap = &s5m8767_regmap_config; 298 break; 299 default: 300 regmap = &sec_regmap_config; 301 break; 302 } 303 304 sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap); 305 if (IS_ERR(sec_pmic->regmap_pmic)) { 306 ret = PTR_ERR(sec_pmic->regmap_pmic); 307 dev_err(&i2c->dev, "Failed to allocate register map: %d\n", 308 ret); 309 return ret; 310 } 311 312 if (pdata && pdata->cfg_pmic_irq) 313 pdata->cfg_pmic_irq(); 314 315 sec_irq_init(sec_pmic); 316 317 pm_runtime_set_active(sec_pmic->dev); 318 319 switch (sec_pmic->device_type) { 320 case S5M8751X: 321 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs, 322 ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL); 323 break; 324 case S5M8763X: 325 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs, 326 ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL); 327 break; 328 case S5M8767X: 329 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs, 330 ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL); 331 break; 332 case S2MPA01: 333 ret = mfd_add_devices(sec_pmic->dev, -1, s2mpa01_devs, 334 ARRAY_SIZE(s2mpa01_devs), NULL, 0, NULL); 335 break; 336 case S2MPS11X: 337 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs, 338 ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL); 339 break; 340 case S2MPS14X: 341 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps14_devs, 342 ARRAY_SIZE(s2mps14_devs), NULL, 0, NULL); 343 break; 344 default: 345 /* If this happens the probe function is problem */ 346 BUG(); 347 } 348 349 if (ret) 350 goto err_mfd; 351 352 device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup); 353 354 return ret; 355 356 err_mfd: 357 sec_irq_exit(sec_pmic); 358 return ret; 359 } 360 361 static int sec_pmic_remove(struct i2c_client *i2c) 362 { 363 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c); 364 365 mfd_remove_devices(sec_pmic->dev); 366 sec_irq_exit(sec_pmic); 367 return 0; 368 } 369 370 #ifdef CONFIG_PM_SLEEP 371 static int sec_pmic_suspend(struct device *dev) 372 { 373 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); 374 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c); 375 376 if (device_may_wakeup(dev)) { 377 enable_irq_wake(sec_pmic->irq); 378 /* 379 * PMIC IRQ must be disabled during suspend for RTC alarm 380 * to work properly. 381 * When device is woken up from suspend by RTC Alarm, an 382 * interrupt occurs before resuming I2C bus controller. 383 * The interrupt is handled by regmap_irq_thread which tries 384 * to read RTC registers. This read fails (I2C is still 385 * suspended) and RTC Alarm interrupt is disabled. 386 */ 387 disable_irq(sec_pmic->irq); 388 } 389 390 return 0; 391 } 392 393 static int sec_pmic_resume(struct device *dev) 394 { 395 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev); 396 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c); 397 398 if (device_may_wakeup(dev)) { 399 disable_irq_wake(sec_pmic->irq); 400 enable_irq(sec_pmic->irq); 401 } 402 403 return 0; 404 } 405 #endif /* CONFIG_PM_SLEEP */ 406 407 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume); 408 409 static const struct i2c_device_id sec_pmic_id[] = { 410 { "sec_pmic", 0 }, 411 { } 412 }; 413 MODULE_DEVICE_TABLE(i2c, sec_pmic_id); 414 415 static struct i2c_driver sec_pmic_driver = { 416 .driver = { 417 .name = "sec_pmic", 418 .owner = THIS_MODULE, 419 .pm = &sec_pmic_pm_ops, 420 .of_match_table = of_match_ptr(sec_dt_match), 421 }, 422 .probe = sec_pmic_probe, 423 .remove = sec_pmic_remove, 424 .id_table = sec_pmic_id, 425 }; 426 427 static int __init sec_pmic_init(void) 428 { 429 return i2c_add_driver(&sec_pmic_driver); 430 } 431 432 subsys_initcall(sec_pmic_init); 433 434 static void __exit sec_pmic_exit(void) 435 { 436 i2c_del_driver(&sec_pmic_driver); 437 } 438 module_exit(sec_pmic_exit); 439 440 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>"); 441 MODULE_DESCRIPTION("Core support for the S5M MFD"); 442 MODULE_LICENSE("GPL"); 443