rn5t618.c (7136B)
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * MFD core driver for Ricoh RN5T618 PMIC 4 * 5 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com> 6 * Copyright (C) 2016 Toradex AG 7 */ 8 9#include <linux/delay.h> 10#include <linux/i2c.h> 11#include <linux/interrupt.h> 12#include <linux/irq.h> 13#include <linux/mfd/core.h> 14#include <linux/mfd/rn5t618.h> 15#include <linux/module.h> 16#include <linux/of_device.h> 17#include <linux/platform_device.h> 18#include <linux/reboot.h> 19#include <linux/regmap.h> 20 21static const struct mfd_cell rn5t618_cells[] = { 22 { .name = "rn5t618-regulator" }, 23 { .name = "rn5t618-wdt" }, 24}; 25 26static const struct mfd_cell rc5t619_cells[] = { 27 { .name = "rn5t618-adc" }, 28 { .name = "rn5t618-power" }, 29 { .name = "rn5t618-regulator" }, 30 { .name = "rc5t619-rtc" }, 31 { .name = "rn5t618-wdt" }, 32}; 33 34static bool rn5t618_volatile_reg(struct device *dev, unsigned int reg) 35{ 36 switch (reg) { 37 case RN5T618_WATCHDOGCNT: 38 case RN5T618_DCIRQ: 39 case RN5T618_ILIMDATAH ... RN5T618_AIN0DATAL: 40 case RN5T618_ADCCNT3: 41 case RN5T618_IR_ADC1 ... RN5T618_IR_ADC3: 42 case RN5T618_IR_GPR: 43 case RN5T618_IR_GPF: 44 case RN5T618_MON_IOIN: 45 case RN5T618_INTMON: 46 case RN5T618_RTC_CTRL1 ... RN5T618_RTC_CTRL2: 47 case RN5T618_RTC_SECONDS ... RN5T618_RTC_YEAR: 48 case RN5T618_CHGCTL1: 49 case RN5T618_REGISET1 ... RN5T618_REGISET2: 50 case RN5T618_CHGSTATE: 51 case RN5T618_CHGCTRL_IRR ... RN5T618_CHGERR_MONI: 52 case RN5T618_GCHGDET: 53 case RN5T618_CONTROL ... RN5T618_CC_AVEREG0: 54 return true; 55 default: 56 return false; 57 } 58} 59 60static const struct regmap_config rn5t618_regmap_config = { 61 .reg_bits = 8, 62 .val_bits = 8, 63 .volatile_reg = rn5t618_volatile_reg, 64 .max_register = RN5T618_MAX_REG, 65 .cache_type = REGCACHE_RBTREE, 66}; 67 68static const struct regmap_irq rc5t619_irqs[] = { 69 REGMAP_IRQ_REG(RN5T618_IRQ_SYS, 0, BIT(0)), 70 REGMAP_IRQ_REG(RN5T618_IRQ_DCDC, 0, BIT(1)), 71 REGMAP_IRQ_REG(RN5T618_IRQ_RTC, 0, BIT(2)), 72 REGMAP_IRQ_REG(RN5T618_IRQ_ADC, 0, BIT(3)), 73 REGMAP_IRQ_REG(RN5T618_IRQ_GPIO, 0, BIT(4)), 74 REGMAP_IRQ_REG(RN5T618_IRQ_CHG, 0, BIT(6)), 75}; 76 77static const struct regmap_irq_chip rc5t619_irq_chip = { 78 .name = "rc5t619", 79 .irqs = rc5t619_irqs, 80 .num_irqs = ARRAY_SIZE(rc5t619_irqs), 81 .num_regs = 1, 82 .status_base = RN5T618_INTMON, 83 .mask_base = RN5T618_INTEN, 84 .mask_invert = true, 85}; 86 87static struct i2c_client *rn5t618_pm_power_off; 88static struct notifier_block rn5t618_restart_handler; 89 90static int rn5t618_irq_init(struct rn5t618 *rn5t618) 91{ 92 const struct regmap_irq_chip *irq_chip = NULL; 93 int ret; 94 95 if (!rn5t618->irq) 96 return 0; 97 98 switch (rn5t618->variant) { 99 case RC5T619: 100 irq_chip = &rc5t619_irq_chip; 101 break; 102 default: 103 dev_err(rn5t618->dev, "Currently no IRQ support for variant %d\n", 104 (int)rn5t618->variant); 105 return -ENOENT; 106 } 107 108 ret = devm_regmap_add_irq_chip(rn5t618->dev, rn5t618->regmap, 109 rn5t618->irq, 110 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 111 0, irq_chip, &rn5t618->irq_data); 112 if (ret) 113 dev_err(rn5t618->dev, "Failed to register IRQ chip\n"); 114 115 return ret; 116} 117 118static void rn5t618_trigger_poweroff_sequence(bool repower) 119{ 120 int ret; 121 122 /* disable automatic repower-on */ 123 ret = i2c_smbus_read_byte_data(rn5t618_pm_power_off, RN5T618_REPCNT); 124 if (ret < 0) 125 goto err; 126 127 ret &= ~RN5T618_REPCNT_REPWRON; 128 if (repower) 129 ret |= RN5T618_REPCNT_REPWRON; 130 131 ret = i2c_smbus_write_byte_data(rn5t618_pm_power_off, 132 RN5T618_REPCNT, (u8)ret); 133 if (ret < 0) 134 goto err; 135 136 /* start power-off sequence */ 137 ret = i2c_smbus_read_byte_data(rn5t618_pm_power_off, RN5T618_SLPCNT); 138 if (ret < 0) 139 goto err; 140 141 ret |= RN5T618_SLPCNT_SWPWROFF; 142 143 ret = i2c_smbus_write_byte_data(rn5t618_pm_power_off, 144 RN5T618_SLPCNT, (u8)ret); 145 if (ret < 0) 146 goto err; 147 148 return; 149 150err: 151 dev_alert(&rn5t618_pm_power_off->dev, "Failed to shutdown (err = %d)\n", ret); 152} 153 154static void rn5t618_power_off(void) 155{ 156 rn5t618_trigger_poweroff_sequence(false); 157} 158 159static int rn5t618_restart(struct notifier_block *this, 160 unsigned long mode, void *cmd) 161{ 162 rn5t618_trigger_poweroff_sequence(true); 163 164 /* 165 * Re-power factor detection on PMIC side is not instant. 1ms 166 * proved to be enough time until reset takes effect. 167 */ 168 mdelay(1); 169 170 return NOTIFY_DONE; 171} 172 173static const struct of_device_id rn5t618_of_match[] = { 174 { .compatible = "ricoh,rn5t567", .data = (void *)RN5T567 }, 175 { .compatible = "ricoh,rn5t618", .data = (void *)RN5T618 }, 176 { .compatible = "ricoh,rc5t619", .data = (void *)RC5T619 }, 177 { } 178}; 179MODULE_DEVICE_TABLE(of, rn5t618_of_match); 180 181static int rn5t618_i2c_probe(struct i2c_client *i2c) 182{ 183 const struct of_device_id *of_id; 184 struct rn5t618 *priv; 185 int ret; 186 187 of_id = of_match_device(rn5t618_of_match, &i2c->dev); 188 if (!of_id) { 189 dev_err(&i2c->dev, "Failed to find matching DT ID\n"); 190 return -EINVAL; 191 } 192 193 priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL); 194 if (!priv) 195 return -ENOMEM; 196 197 i2c_set_clientdata(i2c, priv); 198 priv->variant = (long)of_id->data; 199 priv->irq = i2c->irq; 200 priv->dev = &i2c->dev; 201 202 priv->regmap = devm_regmap_init_i2c(i2c, &rn5t618_regmap_config); 203 if (IS_ERR(priv->regmap)) { 204 ret = PTR_ERR(priv->regmap); 205 dev_err(&i2c->dev, "regmap init failed: %d\n", ret); 206 return ret; 207 } 208 209 if (priv->variant == RC5T619) 210 ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_NONE, 211 rc5t619_cells, 212 ARRAY_SIZE(rc5t619_cells), 213 NULL, 0, NULL); 214 else 215 ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_NONE, 216 rn5t618_cells, 217 ARRAY_SIZE(rn5t618_cells), 218 NULL, 0, NULL); 219 if (ret) { 220 dev_err(&i2c->dev, "failed to add sub-devices: %d\n", ret); 221 return ret; 222 } 223 224 rn5t618_pm_power_off = i2c; 225 if (of_device_is_system_power_controller(i2c->dev.of_node)) { 226 if (!pm_power_off) 227 pm_power_off = rn5t618_power_off; 228 else 229 dev_warn(&i2c->dev, "Poweroff callback already assigned\n"); 230 } 231 232 rn5t618_restart_handler.notifier_call = rn5t618_restart; 233 rn5t618_restart_handler.priority = 192; 234 235 ret = register_restart_handler(&rn5t618_restart_handler); 236 if (ret) { 237 dev_err(&i2c->dev, "cannot register restart handler, %d\n", ret); 238 return ret; 239 } 240 241 return rn5t618_irq_init(priv); 242} 243 244static int rn5t618_i2c_remove(struct i2c_client *i2c) 245{ 246 if (i2c == rn5t618_pm_power_off) { 247 rn5t618_pm_power_off = NULL; 248 pm_power_off = NULL; 249 } 250 251 unregister_restart_handler(&rn5t618_restart_handler); 252 253 return 0; 254} 255 256static int __maybe_unused rn5t618_i2c_suspend(struct device *dev) 257{ 258 struct rn5t618 *priv = dev_get_drvdata(dev); 259 260 if (priv->irq) 261 disable_irq(priv->irq); 262 263 return 0; 264} 265 266static int __maybe_unused rn5t618_i2c_resume(struct device *dev) 267{ 268 struct rn5t618 *priv = dev_get_drvdata(dev); 269 270 if (priv->irq) 271 enable_irq(priv->irq); 272 273 return 0; 274} 275 276static SIMPLE_DEV_PM_OPS(rn5t618_i2c_dev_pm_ops, 277 rn5t618_i2c_suspend, 278 rn5t618_i2c_resume); 279 280static struct i2c_driver rn5t618_i2c_driver = { 281 .driver = { 282 .name = "rn5t618", 283 .of_match_table = of_match_ptr(rn5t618_of_match), 284 .pm = &rn5t618_i2c_dev_pm_ops, 285 }, 286 .probe_new = rn5t618_i2c_probe, 287 .remove = rn5t618_i2c_remove, 288}; 289 290module_i2c_driver(rn5t618_i2c_driver); 291 292MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>"); 293MODULE_DESCRIPTION("Ricoh RN5T567/618 MFD driver"); 294MODULE_LICENSE("GPL v2");