18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci// 38c2ecf20Sopenharmony_ci// max77686.c - mfd core driver for the Maxim 77686/802 48c2ecf20Sopenharmony_ci// 58c2ecf20Sopenharmony_ci// Copyright (C) 2012 Samsung Electronics 68c2ecf20Sopenharmony_ci// Chiwoong Byun <woong.byun@samsung.com> 78c2ecf20Sopenharmony_ci// Jonghwa Lee <jonghwa3.lee@samsung.com> 88c2ecf20Sopenharmony_ci// 98c2ecf20Sopenharmony_ci//This driver is based on max8997.c 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/export.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci#include <linux/i2c.h> 148c2ecf20Sopenharmony_ci#include <linux/irq.h> 158c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 168c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 178c2ecf20Sopenharmony_ci#include <linux/module.h> 188c2ecf20Sopenharmony_ci#include <linux/mfd/core.h> 198c2ecf20Sopenharmony_ci#include <linux/mfd/max77686.h> 208c2ecf20Sopenharmony_ci#include <linux/mfd/max77686-private.h> 218c2ecf20Sopenharmony_ci#include <linux/err.h> 228c2ecf20Sopenharmony_ci#include <linux/of.h> 238c2ecf20Sopenharmony_ci#include <linux/of_device.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistatic const struct mfd_cell max77686_devs[] = { 268c2ecf20Sopenharmony_ci { .name = "max77686-pmic", }, 278c2ecf20Sopenharmony_ci { .name = "max77686-rtc", }, 288c2ecf20Sopenharmony_ci { .name = "max77686-clk", }, 298c2ecf20Sopenharmony_ci}; 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic const struct mfd_cell max77802_devs[] = { 328c2ecf20Sopenharmony_ci { .name = "max77802-pmic", }, 338c2ecf20Sopenharmony_ci { .name = "max77802-clk", }, 348c2ecf20Sopenharmony_ci { .name = "max77802-rtc", }, 358c2ecf20Sopenharmony_ci}; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistatic bool max77802_pmic_is_accessible_reg(struct device *dev, 388c2ecf20Sopenharmony_ci unsigned int reg) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci return reg < MAX77802_REG_PMIC_END; 418c2ecf20Sopenharmony_ci} 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic bool max77802_rtc_is_accessible_reg(struct device *dev, 448c2ecf20Sopenharmony_ci unsigned int reg) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci return (reg >= MAX77802_RTC_INT && reg < MAX77802_RTC_END); 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic bool max77802_is_accessible_reg(struct device *dev, unsigned int reg) 508c2ecf20Sopenharmony_ci{ 518c2ecf20Sopenharmony_ci return (max77802_pmic_is_accessible_reg(dev, reg) || 528c2ecf20Sopenharmony_ci max77802_rtc_is_accessible_reg(dev, reg)); 538c2ecf20Sopenharmony_ci} 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cistatic bool max77802_pmic_is_precious_reg(struct device *dev, unsigned int reg) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci return (reg == MAX77802_REG_INTSRC || reg == MAX77802_REG_INT1 || 588c2ecf20Sopenharmony_ci reg == MAX77802_REG_INT2); 598c2ecf20Sopenharmony_ci} 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_cistatic bool max77802_rtc_is_precious_reg(struct device *dev, unsigned int reg) 628c2ecf20Sopenharmony_ci{ 638c2ecf20Sopenharmony_ci return (reg == MAX77802_RTC_INT || 648c2ecf20Sopenharmony_ci reg == MAX77802_RTC_UPDATE0 || 658c2ecf20Sopenharmony_ci reg == MAX77802_RTC_UPDATE1); 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic bool max77802_is_precious_reg(struct device *dev, unsigned int reg) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci return (max77802_pmic_is_precious_reg(dev, reg) || 718c2ecf20Sopenharmony_ci max77802_rtc_is_precious_reg(dev, reg)); 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistatic bool max77802_pmic_is_volatile_reg(struct device *dev, unsigned int reg) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci return (max77802_is_precious_reg(dev, reg) || 778c2ecf20Sopenharmony_ci reg == MAX77802_REG_STATUS1 || reg == MAX77802_REG_STATUS2 || 788c2ecf20Sopenharmony_ci reg == MAX77802_REG_PWRON); 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic bool max77802_rtc_is_volatile_reg(struct device *dev, unsigned int reg) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci return (max77802_rtc_is_precious_reg(dev, reg) || 848c2ecf20Sopenharmony_ci reg == MAX77802_RTC_SEC || 858c2ecf20Sopenharmony_ci reg == MAX77802_RTC_MIN || 868c2ecf20Sopenharmony_ci reg == MAX77802_RTC_HOUR || 878c2ecf20Sopenharmony_ci reg == MAX77802_RTC_WEEKDAY || 888c2ecf20Sopenharmony_ci reg == MAX77802_RTC_MONTH || 898c2ecf20Sopenharmony_ci reg == MAX77802_RTC_YEAR || 908c2ecf20Sopenharmony_ci reg == MAX77802_RTC_DATE); 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic bool max77802_is_volatile_reg(struct device *dev, unsigned int reg) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci return (max77802_pmic_is_volatile_reg(dev, reg) || 968c2ecf20Sopenharmony_ci max77802_rtc_is_volatile_reg(dev, reg)); 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_cistatic const struct regmap_config max77686_regmap_config = { 1008c2ecf20Sopenharmony_ci .reg_bits = 8, 1018c2ecf20Sopenharmony_ci .val_bits = 8, 1028c2ecf20Sopenharmony_ci}; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic const struct regmap_config max77802_regmap_config = { 1058c2ecf20Sopenharmony_ci .reg_bits = 8, 1068c2ecf20Sopenharmony_ci .val_bits = 8, 1078c2ecf20Sopenharmony_ci .writeable_reg = max77802_is_accessible_reg, 1088c2ecf20Sopenharmony_ci .readable_reg = max77802_is_accessible_reg, 1098c2ecf20Sopenharmony_ci .precious_reg = max77802_is_precious_reg, 1108c2ecf20Sopenharmony_ci .volatile_reg = max77802_is_volatile_reg, 1118c2ecf20Sopenharmony_ci .name = "max77802-pmic", 1128c2ecf20Sopenharmony_ci .cache_type = REGCACHE_RBTREE, 1138c2ecf20Sopenharmony_ci}; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_cistatic const struct regmap_irq max77686_irqs[] = { 1168c2ecf20Sopenharmony_ci /* INT1 interrupts */ 1178c2ecf20Sopenharmony_ci { .reg_offset = 0, .mask = MAX77686_INT1_PWRONF_MSK, }, 1188c2ecf20Sopenharmony_ci { .reg_offset = 0, .mask = MAX77686_INT1_PWRONR_MSK, }, 1198c2ecf20Sopenharmony_ci { .reg_offset = 0, .mask = MAX77686_INT1_JIGONBF_MSK, }, 1208c2ecf20Sopenharmony_ci { .reg_offset = 0, .mask = MAX77686_INT1_JIGONBR_MSK, }, 1218c2ecf20Sopenharmony_ci { .reg_offset = 0, .mask = MAX77686_INT1_ACOKBF_MSK, }, 1228c2ecf20Sopenharmony_ci { .reg_offset = 0, .mask = MAX77686_INT1_ACOKBR_MSK, }, 1238c2ecf20Sopenharmony_ci { .reg_offset = 0, .mask = MAX77686_INT1_ONKEY1S_MSK, }, 1248c2ecf20Sopenharmony_ci { .reg_offset = 0, .mask = MAX77686_INT1_MRSTB_MSK, }, 1258c2ecf20Sopenharmony_ci /* INT2 interrupts */ 1268c2ecf20Sopenharmony_ci { .reg_offset = 1, .mask = MAX77686_INT2_140C_MSK, }, 1278c2ecf20Sopenharmony_ci { .reg_offset = 1, .mask = MAX77686_INT2_120C_MSK, }, 1288c2ecf20Sopenharmony_ci}; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic const struct regmap_irq_chip max77686_irq_chip = { 1318c2ecf20Sopenharmony_ci .name = "max77686-pmic", 1328c2ecf20Sopenharmony_ci .status_base = MAX77686_REG_INT1, 1338c2ecf20Sopenharmony_ci .mask_base = MAX77686_REG_INT1MSK, 1348c2ecf20Sopenharmony_ci .num_regs = 2, 1358c2ecf20Sopenharmony_ci .irqs = max77686_irqs, 1368c2ecf20Sopenharmony_ci .num_irqs = ARRAY_SIZE(max77686_irqs), 1378c2ecf20Sopenharmony_ci}; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic const struct regmap_irq_chip max77802_irq_chip = { 1408c2ecf20Sopenharmony_ci .name = "max77802-pmic", 1418c2ecf20Sopenharmony_ci .status_base = MAX77802_REG_INT1, 1428c2ecf20Sopenharmony_ci .mask_base = MAX77802_REG_INT1MSK, 1438c2ecf20Sopenharmony_ci .num_regs = 2, 1448c2ecf20Sopenharmony_ci .irqs = max77686_irqs, /* same masks as 77686 */ 1458c2ecf20Sopenharmony_ci .num_irqs = ARRAY_SIZE(max77686_irqs), 1468c2ecf20Sopenharmony_ci}; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_cistatic const struct of_device_id max77686_pmic_dt_match[] = { 1498c2ecf20Sopenharmony_ci { 1508c2ecf20Sopenharmony_ci .compatible = "maxim,max77686", 1518c2ecf20Sopenharmony_ci .data = (void *)TYPE_MAX77686, 1528c2ecf20Sopenharmony_ci }, 1538c2ecf20Sopenharmony_ci { 1548c2ecf20Sopenharmony_ci .compatible = "maxim,max77802", 1558c2ecf20Sopenharmony_ci .data = (void *)TYPE_MAX77802, 1568c2ecf20Sopenharmony_ci }, 1578c2ecf20Sopenharmony_ci { }, 1588c2ecf20Sopenharmony_ci}; 1598c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, max77686_pmic_dt_match); 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cistatic int max77686_i2c_probe(struct i2c_client *i2c) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci struct max77686_dev *max77686 = NULL; 1648c2ecf20Sopenharmony_ci unsigned int data; 1658c2ecf20Sopenharmony_ci int ret = 0; 1668c2ecf20Sopenharmony_ci const struct regmap_config *config; 1678c2ecf20Sopenharmony_ci const struct regmap_irq_chip *irq_chip; 1688c2ecf20Sopenharmony_ci const struct mfd_cell *cells; 1698c2ecf20Sopenharmony_ci int n_devs; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci max77686 = devm_kzalloc(&i2c->dev, 1728c2ecf20Sopenharmony_ci sizeof(struct max77686_dev), GFP_KERNEL); 1738c2ecf20Sopenharmony_ci if (!max77686) 1748c2ecf20Sopenharmony_ci return -ENOMEM; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci i2c_set_clientdata(i2c, max77686); 1778c2ecf20Sopenharmony_ci max77686->type = (unsigned long)of_device_get_match_data(&i2c->dev); 1788c2ecf20Sopenharmony_ci max77686->dev = &i2c->dev; 1798c2ecf20Sopenharmony_ci max77686->i2c = i2c; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci max77686->irq = i2c->irq; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci if (max77686->type == TYPE_MAX77686) { 1848c2ecf20Sopenharmony_ci config = &max77686_regmap_config; 1858c2ecf20Sopenharmony_ci irq_chip = &max77686_irq_chip; 1868c2ecf20Sopenharmony_ci cells = max77686_devs; 1878c2ecf20Sopenharmony_ci n_devs = ARRAY_SIZE(max77686_devs); 1888c2ecf20Sopenharmony_ci } else { 1898c2ecf20Sopenharmony_ci config = &max77802_regmap_config; 1908c2ecf20Sopenharmony_ci irq_chip = &max77802_irq_chip; 1918c2ecf20Sopenharmony_ci cells = max77802_devs; 1928c2ecf20Sopenharmony_ci n_devs = ARRAY_SIZE(max77802_devs); 1938c2ecf20Sopenharmony_ci } 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci max77686->regmap = devm_regmap_init_i2c(i2c, config); 1968c2ecf20Sopenharmony_ci if (IS_ERR(max77686->regmap)) { 1978c2ecf20Sopenharmony_ci ret = PTR_ERR(max77686->regmap); 1988c2ecf20Sopenharmony_ci dev_err(max77686->dev, "Failed to allocate register map: %d\n", 1998c2ecf20Sopenharmony_ci ret); 2008c2ecf20Sopenharmony_ci return ret; 2018c2ecf20Sopenharmony_ci } 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci ret = regmap_read(max77686->regmap, MAX77686_REG_DEVICE_ID, &data); 2048c2ecf20Sopenharmony_ci if (ret < 0) { 2058c2ecf20Sopenharmony_ci dev_err(max77686->dev, 2068c2ecf20Sopenharmony_ci "device not found on this channel (this is not an error)\n"); 2078c2ecf20Sopenharmony_ci return -ENODEV; 2088c2ecf20Sopenharmony_ci } 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci ret = devm_regmap_add_irq_chip(&i2c->dev, max77686->regmap, 2118c2ecf20Sopenharmony_ci max77686->irq, 2128c2ecf20Sopenharmony_ci IRQF_TRIGGER_FALLING | IRQF_ONESHOT | 2138c2ecf20Sopenharmony_ci IRQF_SHARED, 0, irq_chip, 2148c2ecf20Sopenharmony_ci &max77686->irq_data); 2158c2ecf20Sopenharmony_ci if (ret < 0) { 2168c2ecf20Sopenharmony_ci dev_err(&i2c->dev, "failed to add PMIC irq chip: %d\n", ret); 2178c2ecf20Sopenharmony_ci return ret; 2188c2ecf20Sopenharmony_ci } 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci ret = devm_mfd_add_devices(max77686->dev, -1, cells, n_devs, NULL, 2218c2ecf20Sopenharmony_ci 0, NULL); 2228c2ecf20Sopenharmony_ci if (ret < 0) { 2238c2ecf20Sopenharmony_ci dev_err(&i2c->dev, "failed to add MFD devices: %d\n", ret); 2248c2ecf20Sopenharmony_ci return ret; 2258c2ecf20Sopenharmony_ci } 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci return 0; 2288c2ecf20Sopenharmony_ci} 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 2318c2ecf20Sopenharmony_cistatic int max77686_suspend(struct device *dev) 2328c2ecf20Sopenharmony_ci{ 2338c2ecf20Sopenharmony_ci struct i2c_client *i2c = to_i2c_client(dev); 2348c2ecf20Sopenharmony_ci struct max77686_dev *max77686 = i2c_get_clientdata(i2c); 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci if (device_may_wakeup(dev)) 2378c2ecf20Sopenharmony_ci enable_irq_wake(max77686->irq); 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci /* 2408c2ecf20Sopenharmony_ci * IRQ must be disabled during suspend because if it happens 2418c2ecf20Sopenharmony_ci * while suspended it will be handled before resuming I2C. 2428c2ecf20Sopenharmony_ci * 2438c2ecf20Sopenharmony_ci * When device is woken up from suspend (e.g. by RTC wake alarm), 2448c2ecf20Sopenharmony_ci * an interrupt occurs before resuming I2C bus controller. 2458c2ecf20Sopenharmony_ci * Interrupt handler tries to read registers but this read 2468c2ecf20Sopenharmony_ci * will fail because I2C is still suspended. 2478c2ecf20Sopenharmony_ci */ 2488c2ecf20Sopenharmony_ci disable_irq(max77686->irq); 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci return 0; 2518c2ecf20Sopenharmony_ci} 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_cistatic int max77686_resume(struct device *dev) 2548c2ecf20Sopenharmony_ci{ 2558c2ecf20Sopenharmony_ci struct i2c_client *i2c = to_i2c_client(dev); 2568c2ecf20Sopenharmony_ci struct max77686_dev *max77686 = i2c_get_clientdata(i2c); 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci if (device_may_wakeup(dev)) 2598c2ecf20Sopenharmony_ci disable_irq_wake(max77686->irq); 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci enable_irq(max77686->irq); 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci return 0; 2648c2ecf20Sopenharmony_ci} 2658c2ecf20Sopenharmony_ci#endif /* CONFIG_PM_SLEEP */ 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(max77686_pm, max77686_suspend, max77686_resume); 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cistatic struct i2c_driver max77686_i2c_driver = { 2708c2ecf20Sopenharmony_ci .driver = { 2718c2ecf20Sopenharmony_ci .name = "max77686", 2728c2ecf20Sopenharmony_ci .pm = &max77686_pm, 2738c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(max77686_pmic_dt_match), 2748c2ecf20Sopenharmony_ci }, 2758c2ecf20Sopenharmony_ci .probe_new = max77686_i2c_probe, 2768c2ecf20Sopenharmony_ci}; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_cimodule_i2c_driver(max77686_i2c_driver); 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MAXIM 77686/802 multi-function core driver"); 2818c2ecf20Sopenharmony_ciMODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>"); 2828c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 283