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