162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2021 ROHM Semiconductors 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * ROHM BD9576MUF and BD9573MUF PMIC driver 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#include <linux/i2c.h> 962306a36Sopenharmony_ci#include <linux/interrupt.h> 1062306a36Sopenharmony_ci#include <linux/ioport.h> 1162306a36Sopenharmony_ci#include <linux/irq.h> 1262306a36Sopenharmony_ci#include <linux/mfd/core.h> 1362306a36Sopenharmony_ci#include <linux/mfd/rohm-bd957x.h> 1462306a36Sopenharmony_ci#include <linux/mfd/rohm-generic.h> 1562306a36Sopenharmony_ci#include <linux/module.h> 1662306a36Sopenharmony_ci#include <linux/of.h> 1762306a36Sopenharmony_ci#include <linux/regmap.h> 1862306a36Sopenharmony_ci#include <linux/types.h> 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_cienum { 2162306a36Sopenharmony_ci BD957X_REGULATOR_CELL, 2262306a36Sopenharmony_ci BD957X_WDT_CELL, 2362306a36Sopenharmony_ci}; 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci/* 2662306a36Sopenharmony_ci * Due to the BD9576MUF nasty IRQ behaviour we don't always populate IRQs. 2762306a36Sopenharmony_ci * These will be added to regulator resources only if IRQ information for the 2862306a36Sopenharmony_ci * PMIC is populated in device-tree. 2962306a36Sopenharmony_ci */ 3062306a36Sopenharmony_cistatic const struct resource bd9576_regulator_irqs[] = { 3162306a36Sopenharmony_ci DEFINE_RES_IRQ_NAMED(BD9576_INT_THERM, "bd9576-temp"), 3262306a36Sopenharmony_ci DEFINE_RES_IRQ_NAMED(BD9576_INT_OVD, "bd9576-ovd"), 3362306a36Sopenharmony_ci DEFINE_RES_IRQ_NAMED(BD9576_INT_UVD, "bd9576-uvd"), 3462306a36Sopenharmony_ci}; 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_cistatic struct mfd_cell bd9573_mfd_cells[] = { 3762306a36Sopenharmony_ci [BD957X_REGULATOR_CELL] = { .name = "bd9573-regulator", }, 3862306a36Sopenharmony_ci [BD957X_WDT_CELL] = { .name = "bd9576-wdt", }, 3962306a36Sopenharmony_ci}; 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_cistatic struct mfd_cell bd9576_mfd_cells[] = { 4262306a36Sopenharmony_ci [BD957X_REGULATOR_CELL] = { .name = "bd9576-regulator", }, 4362306a36Sopenharmony_ci [BD957X_WDT_CELL] = { .name = "bd9576-wdt", }, 4462306a36Sopenharmony_ci}; 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_cistatic const struct regmap_range volatile_ranges[] = { 4762306a36Sopenharmony_ci regmap_reg_range(BD957X_REG_SMRB_ASSERT, BD957X_REG_SMRB_ASSERT), 4862306a36Sopenharmony_ci regmap_reg_range(BD957X_REG_PMIC_INTERNAL_STAT, 4962306a36Sopenharmony_ci BD957X_REG_PMIC_INTERNAL_STAT), 5062306a36Sopenharmony_ci regmap_reg_range(BD957X_REG_INT_THERM_STAT, BD957X_REG_INT_THERM_STAT), 5162306a36Sopenharmony_ci regmap_reg_range(BD957X_REG_INT_OVP_STAT, BD957X_REG_INT_SYS_STAT), 5262306a36Sopenharmony_ci regmap_reg_range(BD957X_REG_INT_MAIN_STAT, BD957X_REG_INT_MAIN_STAT), 5362306a36Sopenharmony_ci}; 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_cistatic const struct regmap_access_table volatile_regs = { 5662306a36Sopenharmony_ci .yes_ranges = &volatile_ranges[0], 5762306a36Sopenharmony_ci .n_yes_ranges = ARRAY_SIZE(volatile_ranges), 5862306a36Sopenharmony_ci}; 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_cistatic struct regmap_config bd957x_regmap = { 6162306a36Sopenharmony_ci .reg_bits = 8, 6262306a36Sopenharmony_ci .val_bits = 8, 6362306a36Sopenharmony_ci .volatile_table = &volatile_regs, 6462306a36Sopenharmony_ci .max_register = BD957X_MAX_REGISTER, 6562306a36Sopenharmony_ci .cache_type = REGCACHE_RBTREE, 6662306a36Sopenharmony_ci}; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_cistatic struct regmap_irq bd9576_irqs[] = { 6962306a36Sopenharmony_ci REGMAP_IRQ_REG(BD9576_INT_THERM, 0, BD957X_MASK_INT_MAIN_THERM), 7062306a36Sopenharmony_ci REGMAP_IRQ_REG(BD9576_INT_OVP, 0, BD957X_MASK_INT_MAIN_OVP), 7162306a36Sopenharmony_ci REGMAP_IRQ_REG(BD9576_INT_SCP, 0, BD957X_MASK_INT_MAIN_SCP), 7262306a36Sopenharmony_ci REGMAP_IRQ_REG(BD9576_INT_OCP, 0, BD957X_MASK_INT_MAIN_OCP), 7362306a36Sopenharmony_ci REGMAP_IRQ_REG(BD9576_INT_OVD, 0, BD957X_MASK_INT_MAIN_OVD), 7462306a36Sopenharmony_ci REGMAP_IRQ_REG(BD9576_INT_UVD, 0, BD957X_MASK_INT_MAIN_UVD), 7562306a36Sopenharmony_ci REGMAP_IRQ_REG(BD9576_INT_UVP, 0, BD957X_MASK_INT_MAIN_UVP), 7662306a36Sopenharmony_ci REGMAP_IRQ_REG(BD9576_INT_SYS, 0, BD957X_MASK_INT_MAIN_SYS), 7762306a36Sopenharmony_ci}; 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_cistatic struct regmap_irq_chip bd9576_irq_chip = { 8062306a36Sopenharmony_ci .name = "bd9576_irq", 8162306a36Sopenharmony_ci .irqs = &bd9576_irqs[0], 8262306a36Sopenharmony_ci .num_irqs = ARRAY_SIZE(bd9576_irqs), 8362306a36Sopenharmony_ci .status_base = BD957X_REG_INT_MAIN_STAT, 8462306a36Sopenharmony_ci .mask_base = BD957X_REG_INT_MAIN_MASK, 8562306a36Sopenharmony_ci .ack_base = BD957X_REG_INT_MAIN_STAT, 8662306a36Sopenharmony_ci .init_ack_masked = true, 8762306a36Sopenharmony_ci .num_regs = 1, 8862306a36Sopenharmony_ci .irq_reg_stride = 1, 8962306a36Sopenharmony_ci}; 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_cistatic int bd957x_i2c_probe(struct i2c_client *i2c) 9262306a36Sopenharmony_ci{ 9362306a36Sopenharmony_ci int ret; 9462306a36Sopenharmony_ci struct regmap *regmap; 9562306a36Sopenharmony_ci struct mfd_cell *cells; 9662306a36Sopenharmony_ci int num_cells; 9762306a36Sopenharmony_ci unsigned long chip_type; 9862306a36Sopenharmony_ci struct irq_domain *domain; 9962306a36Sopenharmony_ci bool usable_irqs; 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ci chip_type = (unsigned long)of_device_get_match_data(&i2c->dev); 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci switch (chip_type) { 10462306a36Sopenharmony_ci case ROHM_CHIP_TYPE_BD9576: 10562306a36Sopenharmony_ci cells = bd9576_mfd_cells; 10662306a36Sopenharmony_ci num_cells = ARRAY_SIZE(bd9576_mfd_cells); 10762306a36Sopenharmony_ci usable_irqs = !!i2c->irq; 10862306a36Sopenharmony_ci break; 10962306a36Sopenharmony_ci case ROHM_CHIP_TYPE_BD9573: 11062306a36Sopenharmony_ci cells = bd9573_mfd_cells; 11162306a36Sopenharmony_ci num_cells = ARRAY_SIZE(bd9573_mfd_cells); 11262306a36Sopenharmony_ci /* 11362306a36Sopenharmony_ci * BD9573 only supports fatal IRQs which we can not handle 11462306a36Sopenharmony_ci * because SoC is going to lose the power. 11562306a36Sopenharmony_ci */ 11662306a36Sopenharmony_ci usable_irqs = false; 11762306a36Sopenharmony_ci break; 11862306a36Sopenharmony_ci default: 11962306a36Sopenharmony_ci dev_err(&i2c->dev, "Unknown device type"); 12062306a36Sopenharmony_ci return -EINVAL; 12162306a36Sopenharmony_ci } 12262306a36Sopenharmony_ci 12362306a36Sopenharmony_ci regmap = devm_regmap_init_i2c(i2c, &bd957x_regmap); 12462306a36Sopenharmony_ci if (IS_ERR(regmap)) 12562306a36Sopenharmony_ci return dev_err_probe(&i2c->dev, PTR_ERR(regmap), 12662306a36Sopenharmony_ci "Failed to initialize Regmap\n"); 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci /* 12962306a36Sopenharmony_ci * BD9576 behaves badly. It kepts IRQ line asserted for the whole 13062306a36Sopenharmony_ci * duration of detected HW condition (like over temperature). So we 13162306a36Sopenharmony_ci * don't require IRQ to be populated. 13262306a36Sopenharmony_ci * If IRQ information is not given, then we mask all IRQs and do not 13362306a36Sopenharmony_ci * provide IRQ resources to regulator driver - which then just omits 13462306a36Sopenharmony_ci * the notifiers. 13562306a36Sopenharmony_ci */ 13662306a36Sopenharmony_ci if (usable_irqs) { 13762306a36Sopenharmony_ci struct regmap_irq_chip_data *irq_data; 13862306a36Sopenharmony_ci struct mfd_cell *regulators; 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_ci regulators = &bd9576_mfd_cells[BD957X_REGULATOR_CELL]; 14162306a36Sopenharmony_ci regulators->resources = bd9576_regulator_irqs; 14262306a36Sopenharmony_ci regulators->num_resources = ARRAY_SIZE(bd9576_regulator_irqs); 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci ret = devm_regmap_add_irq_chip(&i2c->dev, regmap, i2c->irq, 14562306a36Sopenharmony_ci IRQF_ONESHOT, 0, 14662306a36Sopenharmony_ci &bd9576_irq_chip, &irq_data); 14762306a36Sopenharmony_ci if (ret) 14862306a36Sopenharmony_ci return dev_err_probe(&i2c->dev, ret, 14962306a36Sopenharmony_ci "Failed to add IRQ chip\n"); 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ci domain = regmap_irq_get_domain(irq_data); 15262306a36Sopenharmony_ci } else { 15362306a36Sopenharmony_ci ret = regmap_update_bits(regmap, BD957X_REG_INT_MAIN_MASK, 15462306a36Sopenharmony_ci BD957X_MASK_INT_ALL, 15562306a36Sopenharmony_ci BD957X_MASK_INT_ALL); 15662306a36Sopenharmony_ci if (ret) 15762306a36Sopenharmony_ci return ret; 15862306a36Sopenharmony_ci domain = NULL; 15962306a36Sopenharmony_ci } 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_AUTO, cells, 16262306a36Sopenharmony_ci num_cells, NULL, 0, domain); 16362306a36Sopenharmony_ci if (ret) 16462306a36Sopenharmony_ci dev_err_probe(&i2c->dev, ret, "Failed to create subdevices\n"); 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci return ret; 16762306a36Sopenharmony_ci} 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_cistatic const struct of_device_id bd957x_of_match[] = { 17062306a36Sopenharmony_ci { .compatible = "rohm,bd9576", .data = (void *)ROHM_CHIP_TYPE_BD9576, }, 17162306a36Sopenharmony_ci { .compatible = "rohm,bd9573", .data = (void *)ROHM_CHIP_TYPE_BD9573, }, 17262306a36Sopenharmony_ci { }, 17362306a36Sopenharmony_ci}; 17462306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, bd957x_of_match); 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_cistatic struct i2c_driver bd957x_drv = { 17762306a36Sopenharmony_ci .driver = { 17862306a36Sopenharmony_ci .name = "rohm-bd957x", 17962306a36Sopenharmony_ci .of_match_table = bd957x_of_match, 18062306a36Sopenharmony_ci }, 18162306a36Sopenharmony_ci .probe = bd957x_i2c_probe, 18262306a36Sopenharmony_ci}; 18362306a36Sopenharmony_cimodule_i2c_driver(bd957x_drv); 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ciMODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>"); 18662306a36Sopenharmony_ciMODULE_DESCRIPTION("ROHM BD9576MUF and BD9573MUF Power Management IC driver"); 18762306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 188