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