1 // SPDX-License-Identifier: GPL-2.0+
3 // max8998.c - mfd core driver for the Maxim 8998
5 // Copyright (C) 2009-2010 Samsung Electronics
6 // Kyungmin Park <kyungmin.park@samsung.com>
7 // Marek Szyprowski <m.szyprowski@samsung.com>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
15 #include <linux/of_device.h>
16 #include <linux/of_irq.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/mutex.h>
19 #include <linux/mfd/core.h>
20 #include <linux/mfd/max8998.h>
21 #include <linux/mfd/max8998-private.h>
23 #define RTC_I2C_ADDR (0x0c >> 1)
25 static const struct mfd_cell max8998_devs[] = {
27 .name = "max8998-pmic",
29 .name = "max8998-rtc",
31 .name = "max8998-battery",
35 static const struct mfd_cell lp3974_devs[] = {
37 .name = "lp3974-pmic",
43 int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
45 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
48 mutex_lock(&max8998->iolock);
49 ret = i2c_smbus_read_byte_data(i2c, reg);
50 mutex_unlock(&max8998->iolock);
58 EXPORT_SYMBOL(max8998_read_reg);
60 int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
62 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
65 mutex_lock(&max8998->iolock);
66 ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
67 mutex_unlock(&max8998->iolock);
73 EXPORT_SYMBOL(max8998_bulk_read);
75 int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
77 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
80 mutex_lock(&max8998->iolock);
81 ret = i2c_smbus_write_byte_data(i2c, reg, value);
82 mutex_unlock(&max8998->iolock);
85 EXPORT_SYMBOL(max8998_write_reg);
87 int max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
89 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
92 mutex_lock(&max8998->iolock);
93 ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
94 mutex_unlock(&max8998->iolock);
100 EXPORT_SYMBOL(max8998_bulk_write);
102 int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
104 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
107 mutex_lock(&max8998->iolock);
108 ret = i2c_smbus_read_byte_data(i2c, reg);
110 u8 old_val = ret & 0xff;
111 u8 new_val = (val & mask) | (old_val & (~mask));
112 ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
114 mutex_unlock(&max8998->iolock);
117 EXPORT_SYMBOL(max8998_update_reg);
120 static const struct of_device_id max8998_dt_match[] = {
121 { .compatible = "maxim,max8998", .data = (void *)TYPE_MAX8998 },
122 { .compatible = "national,lp3974", .data = (void *)TYPE_LP3974 },
123 { .compatible = "ti,lp3974", .data = (void *)TYPE_LP3974 },
129 * Only the common platform data elements for max8998 are parsed here from the
130 * device tree. Other sub-modules of max8998 such as pmic, rtc and others have
131 * to parse their own platform data elements from device tree.
133 * The max8998 platform data structure is instantiated here and the drivers for
134 * the sub-modules need not instantiate another instance while parsing their
137 static struct max8998_platform_data *max8998_i2c_parse_dt_pdata(
140 struct max8998_platform_data *pd;
142 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
144 return ERR_PTR(-ENOMEM);
146 pd->ono = irq_of_parse_and_map(dev->of_node, 1);
149 * ToDo: the 'wakeup' member in the platform data is more of a linux
150 * specfic information. Hence, there is no binding for that yet and
156 static inline unsigned long max8998_i2c_get_driver_data(struct i2c_client *i2c,
157 const struct i2c_device_id *id)
159 if (i2c->dev.of_node)
160 return (unsigned long)of_device_get_match_data(&i2c->dev);
162 return id->driver_data;
165 static int max8998_i2c_probe(struct i2c_client *i2c,
166 const struct i2c_device_id *id)
168 struct max8998_platform_data *pdata = dev_get_platdata(&i2c->dev);
169 struct max8998_dev *max8998;
172 max8998 = devm_kzalloc(&i2c->dev, sizeof(struct max8998_dev),
177 if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node) {
178 pdata = max8998_i2c_parse_dt_pdata(&i2c->dev);
180 return PTR_ERR(pdata);
183 i2c_set_clientdata(i2c, max8998);
184 max8998->dev = &i2c->dev;
186 max8998->irq = i2c->irq;
187 max8998->type = max8998_i2c_get_driver_data(i2c, id);
188 max8998->pdata = pdata;
190 max8998->ono = pdata->ono;
191 max8998->irq_base = pdata->irq_base;
192 max8998->wakeup = pdata->wakeup;
194 mutex_init(&max8998->iolock);
196 max8998->rtc = i2c_new_dummy_device(i2c->adapter, RTC_I2C_ADDR);
197 if (IS_ERR(max8998->rtc)) {
198 dev_err(&i2c->dev, "Failed to allocate I2C device for RTC\n");
199 return PTR_ERR(max8998->rtc);
201 i2c_set_clientdata(max8998->rtc, max8998);
203 max8998_irq_init(max8998);
205 pm_runtime_set_active(max8998->dev);
207 switch (max8998->type) {
209 ret = mfd_add_devices(max8998->dev, -1,
210 lp3974_devs, ARRAY_SIZE(lp3974_devs),
214 ret = mfd_add_devices(max8998->dev, -1,
215 max8998_devs, ARRAY_SIZE(max8998_devs),
225 device_init_wakeup(max8998->dev, max8998->wakeup);
230 mfd_remove_devices(max8998->dev);
231 max8998_irq_exit(max8998);
232 i2c_unregister_device(max8998->rtc);
236 static const struct i2c_device_id max8998_i2c_id[] = {
237 { "max8998", TYPE_MAX8998 },
238 { "lp3974", TYPE_LP3974},
242 static int max8998_suspend(struct device *dev)
244 struct i2c_client *i2c = to_i2c_client(dev);
245 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
247 if (device_may_wakeup(dev))
248 irq_set_irq_wake(max8998->irq, 1);
252 static int max8998_resume(struct device *dev)
254 struct i2c_client *i2c = to_i2c_client(dev);
255 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
257 if (device_may_wakeup(dev))
258 irq_set_irq_wake(max8998->irq, 0);
260 * In LP3974, if IRQ registers are not "read & clear"
261 * when it's set during sleep, the interrupt becomes
264 return max8998_irq_resume(i2c_get_clientdata(i2c));
267 struct max8998_reg_dump {
271 #define SAVE_ITEM(x) { .addr = (x), .val = 0x0, }
272 static struct max8998_reg_dump max8998_dump[] = {
273 SAVE_ITEM(MAX8998_REG_IRQM1),
274 SAVE_ITEM(MAX8998_REG_IRQM2),
275 SAVE_ITEM(MAX8998_REG_IRQM3),
276 SAVE_ITEM(MAX8998_REG_IRQM4),
277 SAVE_ITEM(MAX8998_REG_STATUSM1),
278 SAVE_ITEM(MAX8998_REG_STATUSM2),
279 SAVE_ITEM(MAX8998_REG_CHGR1),
280 SAVE_ITEM(MAX8998_REG_CHGR2),
281 SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
282 SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
283 SAVE_ITEM(MAX8998_REG_BUCK_ACTIVE_DISCHARGE3),
284 SAVE_ITEM(MAX8998_REG_ONOFF1),
285 SAVE_ITEM(MAX8998_REG_ONOFF2),
286 SAVE_ITEM(MAX8998_REG_ONOFF3),
287 SAVE_ITEM(MAX8998_REG_ONOFF4),
288 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE1),
289 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE2),
290 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE3),
291 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE4),
292 SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE1),
293 SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE2),
294 SAVE_ITEM(MAX8998_REG_LDO2_LDO3),
295 SAVE_ITEM(MAX8998_REG_LDO4),
296 SAVE_ITEM(MAX8998_REG_LDO5),
297 SAVE_ITEM(MAX8998_REG_LDO6),
298 SAVE_ITEM(MAX8998_REG_LDO7),
299 SAVE_ITEM(MAX8998_REG_LDO8_LDO9),
300 SAVE_ITEM(MAX8998_REG_LDO10_LDO11),
301 SAVE_ITEM(MAX8998_REG_LDO12),
302 SAVE_ITEM(MAX8998_REG_LDO13),
303 SAVE_ITEM(MAX8998_REG_LDO14),
304 SAVE_ITEM(MAX8998_REG_LDO15),
305 SAVE_ITEM(MAX8998_REG_LDO16),
306 SAVE_ITEM(MAX8998_REG_LDO17),
307 SAVE_ITEM(MAX8998_REG_BKCHR),
308 SAVE_ITEM(MAX8998_REG_LBCNFG1),
309 SAVE_ITEM(MAX8998_REG_LBCNFG2),
311 /* Save registers before hibernation */
312 static int max8998_freeze(struct device *dev)
314 struct i2c_client *i2c = to_i2c_client(dev);
317 for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
318 max8998_read_reg(i2c, max8998_dump[i].addr,
319 &max8998_dump[i].val);
324 /* Restore registers after hibernation */
325 static int max8998_restore(struct device *dev)
327 struct i2c_client *i2c = to_i2c_client(dev);
330 for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
331 max8998_write_reg(i2c, max8998_dump[i].addr,
332 max8998_dump[i].val);
337 static const struct dev_pm_ops max8998_pm = {
338 .suspend = max8998_suspend,
339 .resume = max8998_resume,
340 .freeze = max8998_freeze,
341 .restore = max8998_restore,
344 static struct i2c_driver max8998_i2c_driver = {
348 .suppress_bind_attrs = true,
349 .of_match_table = of_match_ptr(max8998_dt_match),
351 .probe = max8998_i2c_probe,
352 .id_table = max8998_i2c_id,
355 static int __init max8998_i2c_init(void)
357 return i2c_add_driver(&max8998_i2c_driver);
359 /* init early so consumer devices can complete system boot */
360 subsys_initcall(max8998_i2c_init);