blob: ee94080e1cbb704a6106b97d63fffe74e64a5daa [file] [log] [blame]
Beniamino Galvani9bb9e292014-08-30 14:50:23 +02001/*
2 * MFD core driver for Ricoh RN5T618 PMIC
3 *
4 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
Stefan Agnera99ab502016-06-28 22:53:19 -07005 * Copyright (C) 2016 Toradex AG
Beniamino Galvani9bb9e292014-08-30 14:50:23 +02006 *
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
Stefan Agnera370f602016-06-28 22:53:22 -070015#include <linux/delay.h>
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020016#include <linux/i2c.h>
17#include <linux/mfd/core.h>
18#include <linux/mfd/rn5t618.h>
19#include <linux/module.h>
Stefan Agnera99ab502016-06-28 22:53:19 -070020#include <linux/of_device.h>
Stefan Agnera370f602016-06-28 22:53:22 -070021#include <linux/reboot.h>
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020022#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_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;
43 }
44}
45
46static 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,
52};
53
54static struct rn5t618 *rn5t618_pm_power_off;
Stefan Agnera370f602016-06-28 22:53:22 -070055static struct notifier_block rn5t618_restart_handler;
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020056
Stefan Agnera370f602016-06-28 22:53:22 -070057static void rn5t618_trigger_poweroff_sequence(bool repower)
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020058{
59 /* disable automatic repower-on */
60 regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_REPCNT,
Stefan Agnera370f602016-06-28 22:53:22 -070061 RN5T618_REPCNT_REPWRON,
62 repower ? RN5T618_REPCNT_REPWRON : 0);
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020063 /* start power-off sequence */
64 regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_SLPCNT,
65 RN5T618_SLPCNT_SWPWROFF, RN5T618_SLPCNT_SWPWROFF);
66}
67
Stefan Agnera370f602016-06-28 22:53:22 -070068static void rn5t618_power_off(void)
69{
70 rn5t618_trigger_poweroff_sequence(false);
71}
72
73static int rn5t618_restart(struct notifier_block *this,
74 unsigned long mode, void *cmd)
75{
76 rn5t618_trigger_poweroff_sequence(true);
77
78 /*
79 * Re-power factor detection on PMIC side is not instant. 1ms
80 * proved to be enough time until reset takes effect.
81 */
82 mdelay(1);
83
84 return NOTIFY_DONE;
85}
86
Stefan Agnera99ab502016-06-28 22:53:19 -070087static const struct of_device_id rn5t618_of_match[] = {
88 { .compatible = "ricoh,rn5t567", .data = (void *)RN5T567 },
89 { .compatible = "ricoh,rn5t618", .data = (void *)RN5T618 },
90 { }
91};
92MODULE_DEVICE_TABLE(of, rn5t618_of_match);
93
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020094static int rn5t618_i2c_probe(struct i2c_client *i2c,
95 const struct i2c_device_id *id)
96{
Stefan Agnera99ab502016-06-28 22:53:19 -070097 const struct of_device_id *of_id;
Beniamino Galvani9bb9e292014-08-30 14:50:23 +020098 struct rn5t618 *priv;
99 int ret;
100
Stefan Agnera99ab502016-06-28 22:53:19 -0700101 of_id = of_match_device(rn5t618_of_match, &i2c->dev);
102 if (!of_id) {
103 dev_err(&i2c->dev, "Failed to find matching DT ID\n");
104 return -EINVAL;
105 }
106
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200107 priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
108 if (!priv)
109 return -ENOMEM;
110
111 i2c_set_clientdata(i2c, priv);
Stefan Agnera99ab502016-06-28 22:53:19 -0700112 priv->variant = (long)of_id->data;
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200113
114 priv->regmap = devm_regmap_init_i2c(i2c, &rn5t618_regmap_config);
115 if (IS_ERR(priv->regmap)) {
116 ret = PTR_ERR(priv->regmap);
117 dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
118 return ret;
119 }
120
Laxman Dewanganf41206c2016-04-08 00:13:07 +0530121 ret = devm_mfd_add_devices(&i2c->dev, -1, rn5t618_cells,
122 ARRAY_SIZE(rn5t618_cells), NULL, 0, NULL);
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200123 if (ret) {
124 dev_err(&i2c->dev, "failed to add sub-devices: %d\n", ret);
125 return ret;
126 }
127
Stefan Agnera370f602016-06-28 22:53:22 -0700128 rn5t618_pm_power_off = priv;
Stefan Agnerfd65ae42016-06-28 22:53:21 -0700129 if (of_device_is_system_power_controller(i2c->dev.of_node)) {
Stefan Agnera370f602016-06-28 22:53:22 -0700130 if (!pm_power_off)
Stefan Agnerfd65ae42016-06-28 22:53:21 -0700131 pm_power_off = rn5t618_power_off;
Stefan Agnera370f602016-06-28 22:53:22 -0700132 else
Stefan Agnerfd65ae42016-06-28 22:53:21 -0700133 dev_warn(&i2c->dev, "Poweroff callback already assigned\n");
Stefan Agnera370f602016-06-28 22:53:22 -0700134 }
135
136 rn5t618_restart_handler.notifier_call = rn5t618_restart;
137 rn5t618_restart_handler.priority = 192;
138
139 ret = register_restart_handler(&rn5t618_restart_handler);
140 if (ret) {
141 dev_err(&i2c->dev, "cannot register restart handler, %d\n", ret);
142 return ret;
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200143 }
144
145 return 0;
146}
147
148static int rn5t618_i2c_remove(struct i2c_client *i2c)
149{
150 struct rn5t618 *priv = i2c_get_clientdata(i2c);
151
152 if (priv == rn5t618_pm_power_off) {
153 rn5t618_pm_power_off = NULL;
154 pm_power_off = NULL;
155 }
156
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200157 return 0;
158}
159
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200160static const struct i2c_device_id rn5t618_i2c_id[] = {
161 { }
162};
163MODULE_DEVICE_TABLE(i2c, rn5t618_i2c_id);
164
165static struct i2c_driver rn5t618_i2c_driver = {
166 .driver = {
167 .name = "rn5t618",
168 .of_match_table = of_match_ptr(rn5t618_of_match),
169 },
170 .probe = rn5t618_i2c_probe,
171 .remove = rn5t618_i2c_remove,
172 .id_table = rn5t618_i2c_id,
173};
174
175module_i2c_driver(rn5t618_i2c_driver);
176
177MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
Stefan Agnera99ab502016-06-28 22:53:19 -0700178MODULE_DESCRIPTION("Ricoh RN5T567/618 MFD driver");
Beniamino Galvani9bb9e292014-08-30 14:50:23 +0200179MODULE_LICENSE("GPL v2");