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