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/mfd/core.h> 12 #include <linux/mfd/rn5t618.h> 13 #include <linux/module.h> 14 #include <linux/of_device.h> 15 #include <linux/reboot.h> 16 #include <linux/regmap.h> 17 18 static const struct mfd_cell rn5t618_cells[] = { 19 { .name = "rn5t618-regulator" }, 20 { .name = "rn5t618-wdt" }, 21 }; 22 23 static bool rn5t618_volatile_reg(struct device *dev, unsigned int reg) 24 { 25 switch (reg) { 26 case RN5T618_WATCHDOGCNT: 27 case RN5T618_DCIRQ: 28 case RN5T618_ILIMDATAH ... RN5T618_AIN0DATAL: 29 case RN5T618_ADCCNT3: 30 case RN5T618_IR_ADC1 ... RN5T618_IR_ADC3: 31 case RN5T618_IR_GPR: 32 case RN5T618_IR_GPF: 33 case RN5T618_MON_IOIN: 34 case RN5T618_INTMON: 35 return true; 36 default: 37 return false; 38 } 39 } 40 41 static const struct regmap_config rn5t618_regmap_config = { 42 .reg_bits = 8, 43 .val_bits = 8, 44 .volatile_reg = rn5t618_volatile_reg, 45 .max_register = RN5T618_MAX_REG, 46 .cache_type = REGCACHE_RBTREE, 47 }; 48 49 static struct rn5t618 *rn5t618_pm_power_off; 50 static struct notifier_block rn5t618_restart_handler; 51 52 static void rn5t618_trigger_poweroff_sequence(bool repower) 53 { 54 /* disable automatic repower-on */ 55 regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_REPCNT, 56 RN5T618_REPCNT_REPWRON, 57 repower ? RN5T618_REPCNT_REPWRON : 0); 58 /* start power-off sequence */ 59 regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_SLPCNT, 60 RN5T618_SLPCNT_SWPWROFF, RN5T618_SLPCNT_SWPWROFF); 61 } 62 63 static void rn5t618_power_off(void) 64 { 65 rn5t618_trigger_poweroff_sequence(false); 66 } 67 68 static int rn5t618_restart(struct notifier_block *this, 69 unsigned long mode, void *cmd) 70 { 71 rn5t618_trigger_poweroff_sequence(true); 72 73 /* 74 * Re-power factor detection on PMIC side is not instant. 1ms 75 * proved to be enough time until reset takes effect. 76 */ 77 mdelay(1); 78 79 return NOTIFY_DONE; 80 } 81 82 static const struct of_device_id rn5t618_of_match[] = { 83 { .compatible = "ricoh,rn5t567", .data = (void *)RN5T567 }, 84 { .compatible = "ricoh,rn5t618", .data = (void *)RN5T618 }, 85 { .compatible = "ricoh,rc5t619", .data = (void *)RC5T619 }, 86 { } 87 }; 88 MODULE_DEVICE_TABLE(of, rn5t618_of_match); 89 90 static int rn5t618_i2c_probe(struct i2c_client *i2c, 91 const struct i2c_device_id *id) 92 { 93 const struct of_device_id *of_id; 94 struct rn5t618 *priv; 95 int ret; 96 97 of_id = of_match_device(rn5t618_of_match, &i2c->dev); 98 if (!of_id) { 99 dev_err(&i2c->dev, "Failed to find matching DT ID\n"); 100 return -EINVAL; 101 } 102 103 priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL); 104 if (!priv) 105 return -ENOMEM; 106 107 i2c_set_clientdata(i2c, priv); 108 priv->variant = (long)of_id->data; 109 110 priv->regmap = devm_regmap_init_i2c(i2c, &rn5t618_regmap_config); 111 if (IS_ERR(priv->regmap)) { 112 ret = PTR_ERR(priv->regmap); 113 dev_err(&i2c->dev, "regmap init failed: %d\n", ret); 114 return ret; 115 } 116 117 ret = devm_mfd_add_devices(&i2c->dev, -1, rn5t618_cells, 118 ARRAY_SIZE(rn5t618_cells), NULL, 0, NULL); 119 if (ret) { 120 dev_err(&i2c->dev, "failed to add sub-devices: %d\n", ret); 121 return ret; 122 } 123 124 rn5t618_pm_power_off = priv; 125 if (of_device_is_system_power_controller(i2c->dev.of_node)) { 126 if (!pm_power_off) 127 pm_power_off = rn5t618_power_off; 128 else 129 dev_warn(&i2c->dev, "Poweroff callback already assigned\n"); 130 } 131 132 rn5t618_restart_handler.notifier_call = rn5t618_restart; 133 rn5t618_restart_handler.priority = 192; 134 135 ret = register_restart_handler(&rn5t618_restart_handler); 136 if (ret) { 137 dev_err(&i2c->dev, "cannot register restart handler, %d\n", ret); 138 return ret; 139 } 140 141 return 0; 142 } 143 144 static int rn5t618_i2c_remove(struct i2c_client *i2c) 145 { 146 struct rn5t618 *priv = i2c_get_clientdata(i2c); 147 148 if (priv == rn5t618_pm_power_off) { 149 rn5t618_pm_power_off = NULL; 150 pm_power_off = NULL; 151 } 152 153 unregister_restart_handler(&rn5t618_restart_handler); 154 155 return 0; 156 } 157 158 static const struct i2c_device_id rn5t618_i2c_id[] = { 159 { } 160 }; 161 MODULE_DEVICE_TABLE(i2c, rn5t618_i2c_id); 162 163 static struct i2c_driver rn5t618_i2c_driver = { 164 .driver = { 165 .name = "rn5t618", 166 .of_match_table = of_match_ptr(rn5t618_of_match), 167 }, 168 .probe = rn5t618_i2c_probe, 169 .remove = rn5t618_i2c_remove, 170 .id_table = rn5t618_i2c_id, 171 }; 172 173 module_i2c_driver(rn5t618_i2c_driver); 174 175 MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>"); 176 MODULE_DESCRIPTION("Ricoh RN5T567/618 MFD driver"); 177 MODULE_LICENSE("GPL v2"); 178