1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // max8998.c - mfd core driver for the Maxim 8998 4 // 5 // Copyright (C) 2009-2010 Samsung Electronics 6 // Kyungmin Park <kyungmin.park@samsung.com> 7 // Marek Szyprowski <m.szyprowski@samsung.com> 8 9 #include <linux/err.h> 10 #include <linux/init.h> 11 #include <linux/slab.h> 12 #include <linux/i2c.h> 13 #include <linux/interrupt.h> 14 #include <linux/of.h> 15 #include <linux/of_irq.h> 16 #include <linux/pm_runtime.h> 17 #include <linux/mutex.h> 18 #include <linux/mfd/core.h> 19 #include <linux/mfd/max8998.h> 20 #include <linux/mfd/max8998-private.h> 21 22 #define RTC_I2C_ADDR (0x0c >> 1) 23 24 static const struct mfd_cell max8998_devs[] = { 25 { 26 .name = "max8998-pmic", 27 }, { 28 .name = "max8998-rtc", 29 }, { 30 .name = "max8998-battery", 31 }, 32 }; 33 34 static const struct mfd_cell lp3974_devs[] = { 35 { 36 .name = "lp3974-pmic", 37 }, { 38 .name = "lp3974-rtc", 39 }, 40 }; 41 42 int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest) 43 { 44 struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 45 int ret; 46 47 mutex_lock(&max8998->iolock); 48 ret = i2c_smbus_read_byte_data(i2c, reg); 49 mutex_unlock(&max8998->iolock); 50 if (ret < 0) 51 return ret; 52 53 ret &= 0xff; 54 *dest = ret; 55 return 0; 56 } 57 EXPORT_SYMBOL(max8998_read_reg); 58 59 int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf) 60 { 61 struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 62 int ret; 63 64 mutex_lock(&max8998->iolock); 65 ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf); 66 mutex_unlock(&max8998->iolock); 67 if (ret < 0) 68 return ret; 69 70 return 0; 71 } 72 EXPORT_SYMBOL(max8998_bulk_read); 73 74 int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value) 75 { 76 struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 77 int ret; 78 79 mutex_lock(&max8998->iolock); 80 ret = i2c_smbus_write_byte_data(i2c, reg, value); 81 mutex_unlock(&max8998->iolock); 82 return ret; 83 } 84 EXPORT_SYMBOL(max8998_write_reg); 85 86 int max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf) 87 { 88 struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 89 int ret; 90 91 mutex_lock(&max8998->iolock); 92 ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf); 93 mutex_unlock(&max8998->iolock); 94 if (ret < 0) 95 return ret; 96 97 return 0; 98 } 99 EXPORT_SYMBOL(max8998_bulk_write); 100 101 int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask) 102 { 103 struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 104 int ret; 105 106 mutex_lock(&max8998->iolock); 107 ret = i2c_smbus_read_byte_data(i2c, reg); 108 if (ret >= 0) { 109 u8 old_val = ret & 0xff; 110 u8 new_val = (val & mask) | (old_val & (~mask)); 111 ret = i2c_smbus_write_byte_data(i2c, reg, new_val); 112 } 113 mutex_unlock(&max8998->iolock); 114 return ret; 115 } 116 EXPORT_SYMBOL(max8998_update_reg); 117 118 #ifdef CONFIG_OF 119 static const struct of_device_id max8998_dt_match[] = { 120 { .compatible = "maxim,max8998", .data = (void *)TYPE_MAX8998 }, 121 { .compatible = "national,lp3974", .data = (void *)TYPE_LP3974 }, 122 { .compatible = "ti,lp3974", .data = (void *)TYPE_LP3974 }, 123 {}, 124 }; 125 #endif 126 127 /* 128 * Only the common platform data elements for max8998 are parsed here from the 129 * device tree. Other sub-modules of max8998 such as pmic, rtc and others have 130 * to parse their own platform data elements from device tree. 131 * 132 * The max8998 platform data structure is instantiated here and the drivers for 133 * the sub-modules need not instantiate another instance while parsing their 134 * platform data. 135 */ 136 static struct max8998_platform_data *max8998_i2c_parse_dt_pdata( 137 struct device *dev) 138 { 139 struct max8998_platform_data *pd; 140 141 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL); 142 if (!pd) 143 return ERR_PTR(-ENOMEM); 144 145 pd->ono = irq_of_parse_and_map(dev->of_node, 1); 146 147 /* 148 * ToDo: the 'wakeup' member in the platform data is more of a linux 149 * specfic information. Hence, there is no binding for that yet and 150 * not parsed here. 151 */ 152 return pd; 153 } 154 155 static inline unsigned long max8998_i2c_get_driver_data(struct i2c_client *i2c, 156 const struct i2c_device_id *id) 157 { 158 if (i2c->dev.of_node) 159 return (unsigned long)of_device_get_match_data(&i2c->dev); 160 161 return id->driver_data; 162 } 163 164 static int max8998_i2c_probe(struct i2c_client *i2c) 165 { 166 const struct i2c_device_id *id = i2c_client_get_device_id(i2c); 167 struct max8998_platform_data *pdata = dev_get_platdata(&i2c->dev); 168 struct max8998_dev *max8998; 169 int ret = 0; 170 171 max8998 = devm_kzalloc(&i2c->dev, sizeof(struct max8998_dev), 172 GFP_KERNEL); 173 if (max8998 == NULL) 174 return -ENOMEM; 175 176 if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node) { 177 pdata = max8998_i2c_parse_dt_pdata(&i2c->dev); 178 if (IS_ERR(pdata)) 179 return PTR_ERR(pdata); 180 } 181 182 i2c_set_clientdata(i2c, max8998); 183 max8998->dev = &i2c->dev; 184 max8998->i2c = i2c; 185 max8998->irq = i2c->irq; 186 max8998->type = max8998_i2c_get_driver_data(i2c, id); 187 max8998->pdata = pdata; 188 if (pdata) { 189 max8998->ono = pdata->ono; 190 max8998->irq_base = pdata->irq_base; 191 max8998->wakeup = pdata->wakeup; 192 } 193 mutex_init(&max8998->iolock); 194 195 max8998->rtc = i2c_new_dummy_device(i2c->adapter, RTC_I2C_ADDR); 196 if (IS_ERR(max8998->rtc)) { 197 dev_err(&i2c->dev, "Failed to allocate I2C device for RTC\n"); 198 return PTR_ERR(max8998->rtc); 199 } 200 i2c_set_clientdata(max8998->rtc, max8998); 201 202 max8998_irq_init(max8998); 203 204 pm_runtime_set_active(max8998->dev); 205 206 switch (max8998->type) { 207 case TYPE_LP3974: 208 ret = mfd_add_devices(max8998->dev, -1, 209 lp3974_devs, ARRAY_SIZE(lp3974_devs), 210 NULL, 0, NULL); 211 break; 212 case TYPE_MAX8998: 213 ret = mfd_add_devices(max8998->dev, -1, 214 max8998_devs, ARRAY_SIZE(max8998_devs), 215 NULL, 0, NULL); 216 break; 217 default: 218 ret = -EINVAL; 219 } 220 221 if (ret < 0) 222 goto err; 223 224 device_init_wakeup(max8998->dev, max8998->wakeup); 225 226 return ret; 227 228 err: 229 mfd_remove_devices(max8998->dev); 230 max8998_irq_exit(max8998); 231 i2c_unregister_device(max8998->rtc); 232 return ret; 233 } 234 235 static const struct i2c_device_id max8998_i2c_id[] = { 236 { "max8998", TYPE_MAX8998 }, 237 { "lp3974", TYPE_LP3974}, 238 { } 239 }; 240 241 static int max8998_suspend(struct device *dev) 242 { 243 struct i2c_client *i2c = to_i2c_client(dev); 244 struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 245 246 if (device_may_wakeup(dev)) 247 irq_set_irq_wake(max8998->irq, 1); 248 return 0; 249 } 250 251 static int max8998_resume(struct device *dev) 252 { 253 struct i2c_client *i2c = to_i2c_client(dev); 254 struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 255 256 if (device_may_wakeup(dev)) 257 irq_set_irq_wake(max8998->irq, 0); 258 /* 259 * In LP3974, if IRQ registers are not "read & clear" 260 * when it's set during sleep, the interrupt becomes 261 * disabled. 262 */ 263 return max8998_irq_resume(i2c_get_clientdata(i2c)); 264 } 265 266 struct max8998_reg_dump { 267 u8 addr; 268 u8 val; 269 }; 270 #define SAVE_ITEM(x) { .addr = (x), .val = 0x0, } 271 static struct max8998_reg_dump max8998_dump[] = { 272 SAVE_ITEM(MAX8998_REG_IRQM1), 273 SAVE_ITEM(MAX8998_REG_IRQM2), 274 SAVE_ITEM(MAX8998_REG_IRQM3), 275 SAVE_ITEM(MAX8998_REG_IRQM4), 276 SAVE_ITEM(MAX8998_REG_STATUSM1), 277 SAVE_ITEM(MAX8998_REG_STATUSM2), 278 SAVE_ITEM(MAX8998_REG_CHGR1), 279 SAVE_ITEM(MAX8998_REG_CHGR2), 280 SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1), 281 SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1), 282 SAVE_ITEM(MAX8998_REG_BUCK_ACTIVE_DISCHARGE3), 283 SAVE_ITEM(MAX8998_REG_ONOFF1), 284 SAVE_ITEM(MAX8998_REG_ONOFF2), 285 SAVE_ITEM(MAX8998_REG_ONOFF3), 286 SAVE_ITEM(MAX8998_REG_ONOFF4), 287 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE1), 288 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE2), 289 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE3), 290 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE4), 291 SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE1), 292 SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE2), 293 SAVE_ITEM(MAX8998_REG_LDO2_LDO3), 294 SAVE_ITEM(MAX8998_REG_LDO4), 295 SAVE_ITEM(MAX8998_REG_LDO5), 296 SAVE_ITEM(MAX8998_REG_LDO6), 297 SAVE_ITEM(MAX8998_REG_LDO7), 298 SAVE_ITEM(MAX8998_REG_LDO8_LDO9), 299 SAVE_ITEM(MAX8998_REG_LDO10_LDO11), 300 SAVE_ITEM(MAX8998_REG_LDO12), 301 SAVE_ITEM(MAX8998_REG_LDO13), 302 SAVE_ITEM(MAX8998_REG_LDO14), 303 SAVE_ITEM(MAX8998_REG_LDO15), 304 SAVE_ITEM(MAX8998_REG_LDO16), 305 SAVE_ITEM(MAX8998_REG_LDO17), 306 SAVE_ITEM(MAX8998_REG_BKCHR), 307 SAVE_ITEM(MAX8998_REG_LBCNFG1), 308 SAVE_ITEM(MAX8998_REG_LBCNFG2), 309 }; 310 /* Save registers before hibernation */ 311 static int max8998_freeze(struct device *dev) 312 { 313 struct i2c_client *i2c = to_i2c_client(dev); 314 int i; 315 316 for (i = 0; i < ARRAY_SIZE(max8998_dump); i++) 317 max8998_read_reg(i2c, max8998_dump[i].addr, 318 &max8998_dump[i].val); 319 320 return 0; 321 } 322 323 /* Restore registers after hibernation */ 324 static int max8998_restore(struct device *dev) 325 { 326 struct i2c_client *i2c = to_i2c_client(dev); 327 int i; 328 329 for (i = 0; i < ARRAY_SIZE(max8998_dump); i++) 330 max8998_write_reg(i2c, max8998_dump[i].addr, 331 max8998_dump[i].val); 332 333 return 0; 334 } 335 336 static const struct dev_pm_ops max8998_pm = { 337 .suspend = max8998_suspend, 338 .resume = max8998_resume, 339 .freeze = max8998_freeze, 340 .restore = max8998_restore, 341 }; 342 343 static struct i2c_driver max8998_i2c_driver = { 344 .driver = { 345 .name = "max8998", 346 .pm = &max8998_pm, 347 .suppress_bind_attrs = true, 348 .of_match_table = of_match_ptr(max8998_dt_match), 349 }, 350 .probe = max8998_i2c_probe, 351 .id_table = max8998_i2c_id, 352 }; 353 354 static int __init max8998_i2c_init(void) 355 { 356 return i2c_add_driver(&max8998_i2c_driver); 357 } 358 /* init early so consumer devices can complete system boot */ 359 subsys_initcall(max8998_i2c_init); 360