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