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