Linux 5.1.15
[linux/fpc-iii.git] / drivers / mfd / rn5t618.c
blobf4037d42a60fa7bcc63f034e01779d0557597110
1 /*
2 * MFD core driver for Ricoh RN5T618 PMIC
4 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
5 * Copyright (C) 2016 Toradex AG
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.
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/>.
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>
24 static const struct mfd_cell rn5t618_cells[] = {
25 { .name = "rn5t618-regulator" },
26 { .name = "rn5t618-wdt" },
29 static bool rn5t618_volatile_reg(struct device *dev, unsigned int reg)
31 switch (reg) {
32 case RN5T618_WATCHDOGCNT:
33 case RN5T618_DCIRQ:
34 case RN5T618_ILIMDATAH ... RN5T618_AIN0DATAL:
35 case RN5T618_IR_ADC1 ... RN5T618_IR_ADC3:
36 case RN5T618_IR_GPR:
37 case RN5T618_IR_GPF:
38 case RN5T618_MON_IOIN:
39 case RN5T618_INTMON:
40 return true;
41 default:
42 return false;
46 static const struct regmap_config rn5t618_regmap_config = {
47 .reg_bits = 8,
48 .val_bits = 8,
49 .volatile_reg = rn5t618_volatile_reg,
50 .max_register = RN5T618_MAX_REG,
51 .cache_type = REGCACHE_RBTREE,
54 static struct rn5t618 *rn5t618_pm_power_off;
55 static struct notifier_block rn5t618_restart_handler;
57 static void rn5t618_trigger_poweroff_sequence(bool repower)
59 /* disable automatic repower-on */
60 regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_REPCNT,
61 RN5T618_REPCNT_REPWRON,
62 repower ? RN5T618_REPCNT_REPWRON : 0);
63 /* start power-off sequence */
64 regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_SLPCNT,
65 RN5T618_SLPCNT_SWPWROFF, RN5T618_SLPCNT_SWPWROFF);
68 static void rn5t618_power_off(void)
70 rn5t618_trigger_poweroff_sequence(false);
73 static int rn5t618_restart(struct notifier_block *this,
74 unsigned long mode, void *cmd)
76 rn5t618_trigger_poweroff_sequence(true);
79 * Re-power factor detection on PMIC side is not instant. 1ms
80 * proved to be enough time until reset takes effect.
82 mdelay(1);
84 return NOTIFY_DONE;
87 static const struct of_device_id rn5t618_of_match[] = {
88 { .compatible = "ricoh,rn5t567", .data = (void *)RN5T567 },
89 { .compatible = "ricoh,rn5t618", .data = (void *)RN5T618 },
90 { .compatible = "ricoh,rc5t619", .data = (void *)RC5T619 },
91 { }
93 MODULE_DEVICE_TABLE(of, rn5t618_of_match);
95 static int rn5t618_i2c_probe(struct i2c_client *i2c,
96 const struct i2c_device_id *id)
98 const struct of_device_id *of_id;
99 struct rn5t618 *priv;
100 int ret;
102 of_id = of_match_device(rn5t618_of_match, &i2c->dev);
103 if (!of_id) {
104 dev_err(&i2c->dev, "Failed to find matching DT ID\n");
105 return -EINVAL;
108 priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
109 if (!priv)
110 return -ENOMEM;
112 i2c_set_clientdata(i2c, priv);
113 priv->variant = (long)of_id->data;
115 priv->regmap = devm_regmap_init_i2c(i2c, &rn5t618_regmap_config);
116 if (IS_ERR(priv->regmap)) {
117 ret = PTR_ERR(priv->regmap);
118 dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
119 return ret;
122 ret = devm_mfd_add_devices(&i2c->dev, -1, rn5t618_cells,
123 ARRAY_SIZE(rn5t618_cells), NULL, 0, NULL);
124 if (ret) {
125 dev_err(&i2c->dev, "failed to add sub-devices: %d\n", ret);
126 return ret;
129 rn5t618_pm_power_off = priv;
130 if (of_device_is_system_power_controller(i2c->dev.of_node)) {
131 if (!pm_power_off)
132 pm_power_off = rn5t618_power_off;
133 else
134 dev_warn(&i2c->dev, "Poweroff callback already assigned\n");
137 rn5t618_restart_handler.notifier_call = rn5t618_restart;
138 rn5t618_restart_handler.priority = 192;
140 ret = register_restart_handler(&rn5t618_restart_handler);
141 if (ret) {
142 dev_err(&i2c->dev, "cannot register restart handler, %d\n", ret);
143 return ret;
146 return 0;
149 static int rn5t618_i2c_remove(struct i2c_client *i2c)
151 struct rn5t618 *priv = i2c_get_clientdata(i2c);
153 if (priv == rn5t618_pm_power_off) {
154 rn5t618_pm_power_off = NULL;
155 pm_power_off = NULL;
158 unregister_restart_handler(&rn5t618_restart_handler);
160 return 0;
163 static const struct i2c_device_id rn5t618_i2c_id[] = {
166 MODULE_DEVICE_TABLE(i2c, rn5t618_i2c_id);
168 static struct i2c_driver rn5t618_i2c_driver = {
169 .driver = {
170 .name = "rn5t618",
171 .of_match_table = of_match_ptr(rn5t618_of_match),
173 .probe = rn5t618_i2c_probe,
174 .remove = rn5t618_i2c_remove,
175 .id_table = rn5t618_i2c_id,
178 module_i2c_driver(rn5t618_i2c_driver);
180 MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
181 MODULE_DESCRIPTION("Ricoh RN5T567/618 MFD driver");
182 MODULE_LICENSE("GPL v2");