blob: 7336e6d8a001311aca74f7d8118209ceb67905e8 [file] [log] [blame]
Thomas Gleixner3c910ec2019-06-01 10:09:00 +02001// SPDX-License-Identifier: GPL-2.0-only
Beniamino Galvani9bb9e292014-08-30 14:50:23 +02002/*
3 * MFD core driver for Ricoh RN5T618 PMIC
4 *
5 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
Stefan Agnera99ab502016-06-28 22:53:19 -07006 * Copyright (C) 2016 Toradex AG
Beniamino Galvani9bb9e292014-08-30 14:50:23 +02007 */
8
Stefan Agnera370f602016-06-28 22:53:22 -07009#include <linux/delay.h>
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020010#include <linux/i2c.h>
Andreas Kemnade0c816042020-03-20 09:11:00 +010011#include <linux/interrupt.h>
12#include <linux/irq.h>
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020013#include <linux/mfd/core.h>
14#include <linux/mfd/rn5t618.h>
15#include <linux/module.h>
Rob Herring0db434f2023-10-17 15:36:02 -050016#include <linux/of.h>
Andreas Kemnadebc616762020-03-20 09:11:02 +010017#include <linux/platform_device.h>
Stefan Agnera370f602016-06-28 22:53:22 -070018#include <linux/reboot.h>
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020019#include <linux/regmap.h>
20
21static const struct mfd_cell rn5t618_cells[] = {
22 { .name = "rn5t618-regulator" },
23 { .name = "rn5t618-wdt" },
24};
25
Andreas Kemnadebc616762020-03-20 09:11:02 +010026static const struct mfd_cell rc5t619_cells[] = {
27 { .name = "rn5t618-adc" },
Andreas Kemnaded1264a02020-08-15 18:56:10 +020028 { .name = "rn5t618-power" },
Andreas Kemnadebc616762020-03-20 09:11:02 +010029 { .name = "rn5t618-regulator" },
30 { .name = "rc5t619-rtc" },
31 { .name = "rn5t618-wdt" },
32};
33
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020034static 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:
Andreas Kemnade2f3dc252020-01-17 22:59:22 +010040 case RN5T618_ADCCNT3:
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020041 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:
Andreas Kemnade11027ce2020-03-20 09:11:01 +010046 case RN5T618_RTC_CTRL1 ... RN5T618_RTC_CTRL2:
47 case RN5T618_RTC_SECONDS ... RN5T618_RTC_YEAR:
Andreas Kemnade45026472021-03-14 12:02:36 +010048 case RN5T618_CHGCTL1:
49 case RN5T618_REGISET1 ... RN5T618_REGISET2:
Andreas Kemnade594f1932020-07-17 23:00:02 +020050 case RN5T618_CHGSTATE:
51 case RN5T618_CHGCTRL_IRR ... RN5T618_CHGERR_MONI:
Andreas Kemnade45026472021-03-14 12:02:36 +010052 case RN5T618_GCHGDET:
Andreas Kemnade594f1932020-07-17 23:00:02 +020053 case RN5T618_CONTROL ... RN5T618_CC_AVEREG0:
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020054 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
Andreas Kemnade0c816042020-03-20 09:11:00 +010068static 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,
Aidan MacDonald80ff2d32022-11-12 15:18:26 +000083 .unmask_base = RN5T618_INTEN,
Andreas Kemnade0c816042020-03-20 09:11:00 +010084};
85
Andreas Kemnadecf84dc02020-07-21 09:34:02 +020086static struct i2c_client *rn5t618_pm_power_off;
Stefan Agnera370f602016-06-28 22:53:22 -070087static struct notifier_block rn5t618_restart_handler;
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020088
Andreas Kemnade0c816042020-03-20 09:11:00 +010089static int rn5t618_irq_init(struct rn5t618 *rn5t618)
90{
91 const struct regmap_irq_chip *irq_chip = NULL;
92 int ret;
93
94 if (!rn5t618->irq)
95 return 0;
96
97 switch (rn5t618->variant) {
98 case RC5T619:
99 irq_chip = &rc5t619_irq_chip;
100 break;
101 default:
102 dev_err(rn5t618->dev, "Currently no IRQ support for variant %d\n",
103 (int)rn5t618->variant);
104 return -ENOENT;
105 }
106
107 ret = devm_regmap_add_irq_chip(rn5t618->dev, rn5t618->regmap,
108 rn5t618->irq,
Andreas Kemnadea1649a52021-05-15 22:55:18 +0200109 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
Andreas Kemnade0c816042020-03-20 09:11:00 +0100110 0, irq_chip, &rn5t618->irq_data);
111 if (ret)
112 dev_err(rn5t618->dev, "Failed to register IRQ chip\n");
113
114 return ret;
115}
116
Stefan Agnera370f602016-06-28 22:53:22 -0700117static void rn5t618_trigger_poweroff_sequence(bool repower)
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200118{
Andreas Kemnadecf84dc02020-07-21 09:34:02 +0200119 int ret;
120
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200121 /* disable automatic repower-on */
Andreas Kemnadecf84dc02020-07-21 09:34:02 +0200122 ret = i2c_smbus_read_byte_data(rn5t618_pm_power_off, RN5T618_REPCNT);
123 if (ret < 0)
124 goto err;
125
126 ret &= ~RN5T618_REPCNT_REPWRON;
127 if (repower)
128 ret |= RN5T618_REPCNT_REPWRON;
129
130 ret = i2c_smbus_write_byte_data(rn5t618_pm_power_off,
131 RN5T618_REPCNT, (u8)ret);
132 if (ret < 0)
133 goto err;
134
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200135 /* start power-off sequence */
Andreas Kemnadecf84dc02020-07-21 09:34:02 +0200136 ret = i2c_smbus_read_byte_data(rn5t618_pm_power_off, RN5T618_SLPCNT);
137 if (ret < 0)
138 goto err;
139
140 ret |= RN5T618_SLPCNT_SWPWROFF;
141
142 ret = i2c_smbus_write_byte_data(rn5t618_pm_power_off,
143 RN5T618_SLPCNT, (u8)ret);
144 if (ret < 0)
145 goto err;
146
147 return;
148
149err:
150 dev_alert(&rn5t618_pm_power_off->dev, "Failed to shutdown (err = %d)\n", ret);
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200151}
152
Stefan Agnera370f602016-06-28 22:53:22 -0700153static void rn5t618_power_off(void)
154{
155 rn5t618_trigger_poweroff_sequence(false);
156}
157
158static int rn5t618_restart(struct notifier_block *this,
159 unsigned long mode, void *cmd)
160{
161 rn5t618_trigger_poweroff_sequence(true);
162
163 /*
164 * Re-power factor detection on PMIC side is not instant. 1ms
165 * proved to be enough time until reset takes effect.
166 */
167 mdelay(1);
168
169 return NOTIFY_DONE;
170}
171
Stefan Agnera99ab502016-06-28 22:53:19 -0700172static const struct of_device_id rn5t618_of_match[] = {
173 { .compatible = "ricoh,rn5t567", .data = (void *)RN5T567 },
174 { .compatible = "ricoh,rn5t618", .data = (void *)RN5T618 },
Pierre-Hugues Hussonc5e589a2016-11-05 17:19:24 +0100175 { .compatible = "ricoh,rc5t619", .data = (void *)RC5T619 },
Stefan Agnera99ab502016-06-28 22:53:19 -0700176 { }
177};
178MODULE_DEVICE_TABLE(of, rn5t618_of_match);
179
Andreas Kemnade78586582020-03-20 09:11:05 +0100180static int rn5t618_i2c_probe(struct i2c_client *i2c)
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200181{
182 struct rn5t618 *priv;
183 int ret;
184
185 priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
186 if (!priv)
187 return -ENOMEM;
188
189 i2c_set_clientdata(i2c, priv);
Rob Herring0db434f2023-10-17 15:36:02 -0500190 priv->variant = (long)i2c_get_match_data(i2c);
Andreas Kemnade0c816042020-03-20 09:11:00 +0100191 priv->irq = i2c->irq;
192 priv->dev = &i2c->dev;
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200193
194 priv->regmap = devm_regmap_init_i2c(i2c, &rn5t618_regmap_config);
195 if (IS_ERR(priv->regmap)) {
196 ret = PTR_ERR(priv->regmap);
197 dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
198 return ret;
199 }
200
Andreas Kemnadebc616762020-03-20 09:11:02 +0100201 if (priv->variant == RC5T619)
202 ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_NONE,
203 rc5t619_cells,
204 ARRAY_SIZE(rc5t619_cells),
205 NULL, 0, NULL);
206 else
207 ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_NONE,
208 rn5t618_cells,
209 ARRAY_SIZE(rn5t618_cells),
210 NULL, 0, NULL);
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200211 if (ret) {
212 dev_err(&i2c->dev, "failed to add sub-devices: %d\n", ret);
213 return ret;
214 }
215
Andreas Kemnadecf84dc02020-07-21 09:34:02 +0200216 rn5t618_pm_power_off = i2c;
Stefan Agnerfd65ae42016-06-28 22:53:21 -0700217 if (of_device_is_system_power_controller(i2c->dev.of_node)) {
Stefan Agnera370f602016-06-28 22:53:22 -0700218 if (!pm_power_off)
Stefan Agnerfd65ae42016-06-28 22:53:21 -0700219 pm_power_off = rn5t618_power_off;
Stefan Agnera370f602016-06-28 22:53:22 -0700220 else
Stefan Agnerfd65ae42016-06-28 22:53:21 -0700221 dev_warn(&i2c->dev, "Poweroff callback already assigned\n");
Stefan Agnera370f602016-06-28 22:53:22 -0700222 }
223
224 rn5t618_restart_handler.notifier_call = rn5t618_restart;
225 rn5t618_restart_handler.priority = 192;
226
227 ret = register_restart_handler(&rn5t618_restart_handler);
228 if (ret) {
229 dev_err(&i2c->dev, "cannot register restart handler, %d\n", ret);
230 return ret;
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200231 }
232
Andreas Kemnade0c816042020-03-20 09:11:00 +0100233 return rn5t618_irq_init(priv);
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200234}
235
Uwe Kleine-Königed5c2f52022-08-15 10:02:30 +0200236static void rn5t618_i2c_remove(struct i2c_client *i2c)
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200237{
Andreas Kemnadecf84dc02020-07-21 09:34:02 +0200238 if (i2c == rn5t618_pm_power_off) {
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200239 rn5t618_pm_power_off = NULL;
240 pm_power_off = NULL;
241 }
242
Stefan Agnerecca7902017-05-15 23:24:11 -0700243 unregister_restart_handler(&rn5t618_restart_handler);
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200244}
245
Andreas Kemnade0c816042020-03-20 09:11:00 +0100246static int __maybe_unused rn5t618_i2c_suspend(struct device *dev)
247{
248 struct rn5t618 *priv = dev_get_drvdata(dev);
249
250 if (priv->irq)
251 disable_irq(priv->irq);
252
253 return 0;
254}
255
256static int __maybe_unused rn5t618_i2c_resume(struct device *dev)
257{
258 struct rn5t618 *priv = dev_get_drvdata(dev);
259
260 if (priv->irq)
261 enable_irq(priv->irq);
262
263 return 0;
264}
265
Andreas Kemnade0c816042020-03-20 09:11:00 +0100266static SIMPLE_DEV_PM_OPS(rn5t618_i2c_dev_pm_ops,
267 rn5t618_i2c_suspend,
268 rn5t618_i2c_resume);
269
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200270static struct i2c_driver rn5t618_i2c_driver = {
271 .driver = {
272 .name = "rn5t618",
Zhu Wang59cf3812023-08-08 21:00:23 +0800273 .of_match_table = rn5t618_of_match,
Andreas Kemnade0c816042020-03-20 09:11:00 +0100274 .pm = &rn5t618_i2c_dev_pm_ops,
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200275 },
Uwe Kleine-König9816d852023-05-15 20:27:52 +0200276 .probe = rn5t618_i2c_probe,
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200277 .remove = rn5t618_i2c_remove,
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200278};
279
280module_i2c_driver(rn5t618_i2c_driver);
281
282MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
Stefan Agnera99ab502016-06-28 22:53:19 -0700283MODULE_DESCRIPTION("Ricoh RN5T567/618 MFD driver");
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200284MODULE_LICENSE("GPL v2");