18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci// 38c2ecf20Sopenharmony_ci// max8998.c - mfd core driver for the Maxim 8998 48c2ecf20Sopenharmony_ci// 58c2ecf20Sopenharmony_ci// Copyright (C) 2009-2010 Samsung Electronics 68c2ecf20Sopenharmony_ci// Kyungmin Park <kyungmin.park@samsung.com> 78c2ecf20Sopenharmony_ci// Marek Szyprowski <m.szyprowski@samsung.com> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/err.h> 108c2ecf20Sopenharmony_ci#include <linux/init.h> 118c2ecf20Sopenharmony_ci#include <linux/slab.h> 128c2ecf20Sopenharmony_ci#include <linux/i2c.h> 138c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 148c2ecf20Sopenharmony_ci#include <linux/of.h> 158c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 168c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 178c2ecf20Sopenharmony_ci#include <linux/mutex.h> 188c2ecf20Sopenharmony_ci#include <linux/mfd/core.h> 198c2ecf20Sopenharmony_ci#include <linux/mfd/max8998.h> 208c2ecf20Sopenharmony_ci#include <linux/mfd/max8998-private.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define RTC_I2C_ADDR (0x0c >> 1) 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistatic const struct mfd_cell max8998_devs[] = { 258c2ecf20Sopenharmony_ci { 268c2ecf20Sopenharmony_ci .name = "max8998-pmic", 278c2ecf20Sopenharmony_ci }, { 288c2ecf20Sopenharmony_ci .name = "max8998-rtc", 298c2ecf20Sopenharmony_ci }, { 308c2ecf20Sopenharmony_ci .name = "max8998-battery", 318c2ecf20Sopenharmony_ci }, 328c2ecf20Sopenharmony_ci}; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic const struct mfd_cell lp3974_devs[] = { 358c2ecf20Sopenharmony_ci { 368c2ecf20Sopenharmony_ci .name = "lp3974-pmic", 378c2ecf20Sopenharmony_ci }, { 388c2ecf20Sopenharmony_ci .name = "lp3974-rtc", 398c2ecf20Sopenharmony_ci }, 408c2ecf20Sopenharmony_ci}; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ciint max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 458c2ecf20Sopenharmony_ci int ret; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci mutex_lock(&max8998->iolock); 488c2ecf20Sopenharmony_ci ret = i2c_smbus_read_byte_data(i2c, reg); 498c2ecf20Sopenharmony_ci mutex_unlock(&max8998->iolock); 508c2ecf20Sopenharmony_ci if (ret < 0) 518c2ecf20Sopenharmony_ci return ret; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci ret &= 0xff; 548c2ecf20Sopenharmony_ci *dest = ret; 558c2ecf20Sopenharmony_ci return 0; 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ciEXPORT_SYMBOL(max8998_read_reg); 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ciint max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf) 608c2ecf20Sopenharmony_ci{ 618c2ecf20Sopenharmony_ci struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 628c2ecf20Sopenharmony_ci int ret; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci mutex_lock(&max8998->iolock); 658c2ecf20Sopenharmony_ci ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf); 668c2ecf20Sopenharmony_ci mutex_unlock(&max8998->iolock); 678c2ecf20Sopenharmony_ci if (ret < 0) 688c2ecf20Sopenharmony_ci return ret; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci return 0; 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_ciEXPORT_SYMBOL(max8998_bulk_read); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ciint max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 778c2ecf20Sopenharmony_ci int ret; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci mutex_lock(&max8998->iolock); 808c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(i2c, reg, value); 818c2ecf20Sopenharmony_ci mutex_unlock(&max8998->iolock); 828c2ecf20Sopenharmony_ci return ret; 838c2ecf20Sopenharmony_ci} 848c2ecf20Sopenharmony_ciEXPORT_SYMBOL(max8998_write_reg); 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ciint max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 898c2ecf20Sopenharmony_ci int ret; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci mutex_lock(&max8998->iolock); 928c2ecf20Sopenharmony_ci ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf); 938c2ecf20Sopenharmony_ci mutex_unlock(&max8998->iolock); 948c2ecf20Sopenharmony_ci if (ret < 0) 958c2ecf20Sopenharmony_ci return ret; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci return 0; 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ciEXPORT_SYMBOL(max8998_bulk_write); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ciint max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 1048c2ecf20Sopenharmony_ci int ret; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci mutex_lock(&max8998->iolock); 1078c2ecf20Sopenharmony_ci ret = i2c_smbus_read_byte_data(i2c, reg); 1088c2ecf20Sopenharmony_ci if (ret >= 0) { 1098c2ecf20Sopenharmony_ci u8 old_val = ret & 0xff; 1108c2ecf20Sopenharmony_ci u8 new_val = (val & mask) | (old_val & (~mask)); 1118c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(i2c, reg, new_val); 1128c2ecf20Sopenharmony_ci } 1138c2ecf20Sopenharmony_ci mutex_unlock(&max8998->iolock); 1148c2ecf20Sopenharmony_ci return ret; 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ciEXPORT_SYMBOL(max8998_update_reg); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci#ifdef CONFIG_OF 1198c2ecf20Sopenharmony_cistatic const struct of_device_id max8998_dt_match[] = { 1208c2ecf20Sopenharmony_ci { .compatible = "maxim,max8998", .data = (void *)TYPE_MAX8998 }, 1218c2ecf20Sopenharmony_ci { .compatible = "national,lp3974", .data = (void *)TYPE_LP3974 }, 1228c2ecf20Sopenharmony_ci { .compatible = "ti,lp3974", .data = (void *)TYPE_LP3974 }, 1238c2ecf20Sopenharmony_ci {}, 1248c2ecf20Sopenharmony_ci}; 1258c2ecf20Sopenharmony_ci#endif 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci/* 1288c2ecf20Sopenharmony_ci * Only the common platform data elements for max8998 are parsed here from the 1298c2ecf20Sopenharmony_ci * device tree. Other sub-modules of max8998 such as pmic, rtc and others have 1308c2ecf20Sopenharmony_ci * to parse their own platform data elements from device tree. 1318c2ecf20Sopenharmony_ci * 1328c2ecf20Sopenharmony_ci * The max8998 platform data structure is instantiated here and the drivers for 1338c2ecf20Sopenharmony_ci * the sub-modules need not instantiate another instance while parsing their 1348c2ecf20Sopenharmony_ci * platform data. 1358c2ecf20Sopenharmony_ci */ 1368c2ecf20Sopenharmony_cistatic struct max8998_platform_data *max8998_i2c_parse_dt_pdata( 1378c2ecf20Sopenharmony_ci struct device *dev) 1388c2ecf20Sopenharmony_ci{ 1398c2ecf20Sopenharmony_ci struct max8998_platform_data *pd; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL); 1428c2ecf20Sopenharmony_ci if (!pd) 1438c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci pd->ono = irq_of_parse_and_map(dev->of_node, 1); 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci /* 1488c2ecf20Sopenharmony_ci * ToDo: the 'wakeup' member in the platform data is more of a linux 1498c2ecf20Sopenharmony_ci * specfic information. Hence, there is no binding for that yet and 1508c2ecf20Sopenharmony_ci * not parsed here. 1518c2ecf20Sopenharmony_ci */ 1528c2ecf20Sopenharmony_ci return pd; 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_cistatic inline unsigned long max8998_i2c_get_driver_data(struct i2c_client *i2c, 1568c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 1578c2ecf20Sopenharmony_ci{ 1588c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node) { 1598c2ecf20Sopenharmony_ci const struct of_device_id *match; 1608c2ecf20Sopenharmony_ci match = of_match_node(max8998_dt_match, i2c->dev.of_node); 1618c2ecf20Sopenharmony_ci return (unsigned long)match->data; 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci return id->driver_data; 1658c2ecf20Sopenharmony_ci} 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_cistatic int max8998_i2c_probe(struct i2c_client *i2c, 1688c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 1698c2ecf20Sopenharmony_ci{ 1708c2ecf20Sopenharmony_ci struct max8998_platform_data *pdata = dev_get_platdata(&i2c->dev); 1718c2ecf20Sopenharmony_ci struct max8998_dev *max8998; 1728c2ecf20Sopenharmony_ci int ret = 0; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci max8998 = devm_kzalloc(&i2c->dev, sizeof(struct max8998_dev), 1758c2ecf20Sopenharmony_ci GFP_KERNEL); 1768c2ecf20Sopenharmony_ci if (max8998 == NULL) 1778c2ecf20Sopenharmony_ci return -ENOMEM; 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node) { 1808c2ecf20Sopenharmony_ci pdata = max8998_i2c_parse_dt_pdata(&i2c->dev); 1818c2ecf20Sopenharmony_ci if (IS_ERR(pdata)) 1828c2ecf20Sopenharmony_ci return PTR_ERR(pdata); 1838c2ecf20Sopenharmony_ci } 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci i2c_set_clientdata(i2c, max8998); 1868c2ecf20Sopenharmony_ci max8998->dev = &i2c->dev; 1878c2ecf20Sopenharmony_ci max8998->i2c = i2c; 1888c2ecf20Sopenharmony_ci max8998->irq = i2c->irq; 1898c2ecf20Sopenharmony_ci max8998->type = max8998_i2c_get_driver_data(i2c, id); 1908c2ecf20Sopenharmony_ci max8998->pdata = pdata; 1918c2ecf20Sopenharmony_ci if (pdata) { 1928c2ecf20Sopenharmony_ci max8998->ono = pdata->ono; 1938c2ecf20Sopenharmony_ci max8998->irq_base = pdata->irq_base; 1948c2ecf20Sopenharmony_ci max8998->wakeup = pdata->wakeup; 1958c2ecf20Sopenharmony_ci } 1968c2ecf20Sopenharmony_ci mutex_init(&max8998->iolock); 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci max8998->rtc = i2c_new_dummy_device(i2c->adapter, RTC_I2C_ADDR); 1998c2ecf20Sopenharmony_ci if (IS_ERR(max8998->rtc)) { 2008c2ecf20Sopenharmony_ci dev_err(&i2c->dev, "Failed to allocate I2C device for RTC\n"); 2018c2ecf20Sopenharmony_ci return PTR_ERR(max8998->rtc); 2028c2ecf20Sopenharmony_ci } 2038c2ecf20Sopenharmony_ci i2c_set_clientdata(max8998->rtc, max8998); 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci max8998_irq_init(max8998); 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci pm_runtime_set_active(max8998->dev); 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci switch (max8998->type) { 2108c2ecf20Sopenharmony_ci case TYPE_LP3974: 2118c2ecf20Sopenharmony_ci ret = mfd_add_devices(max8998->dev, -1, 2128c2ecf20Sopenharmony_ci lp3974_devs, ARRAY_SIZE(lp3974_devs), 2138c2ecf20Sopenharmony_ci NULL, 0, NULL); 2148c2ecf20Sopenharmony_ci break; 2158c2ecf20Sopenharmony_ci case TYPE_MAX8998: 2168c2ecf20Sopenharmony_ci ret = mfd_add_devices(max8998->dev, -1, 2178c2ecf20Sopenharmony_ci max8998_devs, ARRAY_SIZE(max8998_devs), 2188c2ecf20Sopenharmony_ci NULL, 0, NULL); 2198c2ecf20Sopenharmony_ci break; 2208c2ecf20Sopenharmony_ci default: 2218c2ecf20Sopenharmony_ci ret = -EINVAL; 2228c2ecf20Sopenharmony_ci } 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci if (ret < 0) 2258c2ecf20Sopenharmony_ci goto err; 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci device_init_wakeup(max8998->dev, max8998->wakeup); 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci return ret; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_cierr: 2328c2ecf20Sopenharmony_ci mfd_remove_devices(max8998->dev); 2338c2ecf20Sopenharmony_ci max8998_irq_exit(max8998); 2348c2ecf20Sopenharmony_ci i2c_unregister_device(max8998->rtc); 2358c2ecf20Sopenharmony_ci return ret; 2368c2ecf20Sopenharmony_ci} 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_cistatic const struct i2c_device_id max8998_i2c_id[] = { 2398c2ecf20Sopenharmony_ci { "max8998", TYPE_MAX8998 }, 2408c2ecf20Sopenharmony_ci { "lp3974", TYPE_LP3974}, 2418c2ecf20Sopenharmony_ci { } 2428c2ecf20Sopenharmony_ci}; 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_cistatic int max8998_suspend(struct device *dev) 2458c2ecf20Sopenharmony_ci{ 2468c2ecf20Sopenharmony_ci struct i2c_client *i2c = to_i2c_client(dev); 2478c2ecf20Sopenharmony_ci struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci if (device_may_wakeup(dev)) 2508c2ecf20Sopenharmony_ci irq_set_irq_wake(max8998->irq, 1); 2518c2ecf20Sopenharmony_ci return 0; 2528c2ecf20Sopenharmony_ci} 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_cistatic int max8998_resume(struct device *dev) 2558c2ecf20Sopenharmony_ci{ 2568c2ecf20Sopenharmony_ci struct i2c_client *i2c = to_i2c_client(dev); 2578c2ecf20Sopenharmony_ci struct max8998_dev *max8998 = i2c_get_clientdata(i2c); 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci if (device_may_wakeup(dev)) 2608c2ecf20Sopenharmony_ci irq_set_irq_wake(max8998->irq, 0); 2618c2ecf20Sopenharmony_ci /* 2628c2ecf20Sopenharmony_ci * In LP3974, if IRQ registers are not "read & clear" 2638c2ecf20Sopenharmony_ci * when it's set during sleep, the interrupt becomes 2648c2ecf20Sopenharmony_ci * disabled. 2658c2ecf20Sopenharmony_ci */ 2668c2ecf20Sopenharmony_ci return max8998_irq_resume(i2c_get_clientdata(i2c)); 2678c2ecf20Sopenharmony_ci} 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cistruct max8998_reg_dump { 2708c2ecf20Sopenharmony_ci u8 addr; 2718c2ecf20Sopenharmony_ci u8 val; 2728c2ecf20Sopenharmony_ci}; 2738c2ecf20Sopenharmony_ci#define SAVE_ITEM(x) { .addr = (x), .val = 0x0, } 2748c2ecf20Sopenharmony_cistatic struct max8998_reg_dump max8998_dump[] = { 2758c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_IRQM1), 2768c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_IRQM2), 2778c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_IRQM3), 2788c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_IRQM4), 2798c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_STATUSM1), 2808c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_STATUSM2), 2818c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_CHGR1), 2828c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_CHGR2), 2838c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1), 2848c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1), 2858c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_BUCK_ACTIVE_DISCHARGE3), 2868c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_ONOFF1), 2878c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_ONOFF2), 2888c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_ONOFF3), 2898c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_ONOFF4), 2908c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE1), 2918c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE2), 2928c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE3), 2938c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE4), 2948c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE1), 2958c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE2), 2968c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO2_LDO3), 2978c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO4), 2988c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO5), 2998c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO6), 3008c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO7), 3018c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO8_LDO9), 3028c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO10_LDO11), 3038c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO12), 3048c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO13), 3058c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO14), 3068c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO15), 3078c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO16), 3088c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LDO17), 3098c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_BKCHR), 3108c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LBCNFG1), 3118c2ecf20Sopenharmony_ci SAVE_ITEM(MAX8998_REG_LBCNFG2), 3128c2ecf20Sopenharmony_ci}; 3138c2ecf20Sopenharmony_ci/* Save registers before hibernation */ 3148c2ecf20Sopenharmony_cistatic int max8998_freeze(struct device *dev) 3158c2ecf20Sopenharmony_ci{ 3168c2ecf20Sopenharmony_ci struct i2c_client *i2c = to_i2c_client(dev); 3178c2ecf20Sopenharmony_ci int i; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(max8998_dump); i++) 3208c2ecf20Sopenharmony_ci max8998_read_reg(i2c, max8998_dump[i].addr, 3218c2ecf20Sopenharmony_ci &max8998_dump[i].val); 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci return 0; 3248c2ecf20Sopenharmony_ci} 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci/* Restore registers after hibernation */ 3278c2ecf20Sopenharmony_cistatic int max8998_restore(struct device *dev) 3288c2ecf20Sopenharmony_ci{ 3298c2ecf20Sopenharmony_ci struct i2c_client *i2c = to_i2c_client(dev); 3308c2ecf20Sopenharmony_ci int i; 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(max8998_dump); i++) 3338c2ecf20Sopenharmony_ci max8998_write_reg(i2c, max8998_dump[i].addr, 3348c2ecf20Sopenharmony_ci max8998_dump[i].val); 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci return 0; 3378c2ecf20Sopenharmony_ci} 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_cistatic const struct dev_pm_ops max8998_pm = { 3408c2ecf20Sopenharmony_ci .suspend = max8998_suspend, 3418c2ecf20Sopenharmony_ci .resume = max8998_resume, 3428c2ecf20Sopenharmony_ci .freeze = max8998_freeze, 3438c2ecf20Sopenharmony_ci .restore = max8998_restore, 3448c2ecf20Sopenharmony_ci}; 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_cistatic struct i2c_driver max8998_i2c_driver = { 3478c2ecf20Sopenharmony_ci .driver = { 3488c2ecf20Sopenharmony_ci .name = "max8998", 3498c2ecf20Sopenharmony_ci .pm = &max8998_pm, 3508c2ecf20Sopenharmony_ci .suppress_bind_attrs = true, 3518c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(max8998_dt_match), 3528c2ecf20Sopenharmony_ci }, 3538c2ecf20Sopenharmony_ci .probe = max8998_i2c_probe, 3548c2ecf20Sopenharmony_ci .id_table = max8998_i2c_id, 3558c2ecf20Sopenharmony_ci}; 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_cistatic int __init max8998_i2c_init(void) 3588c2ecf20Sopenharmony_ci{ 3598c2ecf20Sopenharmony_ci return i2c_add_driver(&max8998_i2c_driver); 3608c2ecf20Sopenharmony_ci} 3618c2ecf20Sopenharmony_ci/* init early so consumer devices can complete system boot */ 3628c2ecf20Sopenharmony_cisubsys_initcall(max8998_i2c_init); 363