xref: /openbmc/linux/drivers/mfd/rn5t618.c (revision fd65ae4c)
1 /*
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/i2c.h>
16 #include <linux/mfd/core.h>
17 #include <linux/mfd/rn5t618.h>
18 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/regmap.h>
21 
22 static const struct mfd_cell rn5t618_cells[] = {
23 	{ .name = "rn5t618-regulator" },
24 	{ .name = "rn5t618-wdt" },
25 };
26 
27 static bool rn5t618_volatile_reg(struct device *dev, unsigned int reg)
28 {
29 	switch (reg) {
30 	case RN5T618_WATCHDOGCNT:
31 	case RN5T618_DCIRQ:
32 	case RN5T618_ILIMDATAH ... RN5T618_AIN0DATAL:
33 	case RN5T618_IR_ADC1 ... RN5T618_IR_ADC3:
34 	case RN5T618_IR_GPR:
35 	case RN5T618_IR_GPF:
36 	case RN5T618_MON_IOIN:
37 	case RN5T618_INTMON:
38 		return true;
39 	default:
40 		return false;
41 	}
42 }
43 
44 static const struct regmap_config rn5t618_regmap_config = {
45 	.reg_bits	= 8,
46 	.val_bits	= 8,
47 	.volatile_reg	= rn5t618_volatile_reg,
48 	.max_register	= RN5T618_MAX_REG,
49 	.cache_type	= REGCACHE_RBTREE,
50 };
51 
52 static struct rn5t618 *rn5t618_pm_power_off;
53 
54 static void rn5t618_power_off(void)
55 {
56 	/* disable automatic repower-on */
57 	regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_REPCNT,
58 			   RN5T618_REPCNT_REPWRON, 0);
59 	/* start power-off sequence */
60 	regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_SLPCNT,
61 			   RN5T618_SLPCNT_SWPWROFF, RN5T618_SLPCNT_SWPWROFF);
62 }
63 
64 static const struct of_device_id rn5t618_of_match[] = {
65 	{ .compatible = "ricoh,rn5t567", .data = (void *)RN5T567 },
66 	{ .compatible = "ricoh,rn5t618", .data = (void *)RN5T618 },
67 	{ }
68 };
69 MODULE_DEVICE_TABLE(of, rn5t618_of_match);
70 
71 static int rn5t618_i2c_probe(struct i2c_client *i2c,
72 			     const struct i2c_device_id *id)
73 {
74 	const struct of_device_id *of_id;
75 	struct rn5t618 *priv;
76 	int ret;
77 
78 	of_id = of_match_device(rn5t618_of_match, &i2c->dev);
79 	if (!of_id) {
80 		dev_err(&i2c->dev, "Failed to find matching DT ID\n");
81 		return -EINVAL;
82 	}
83 
84 	priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
85 	if (!priv)
86 		return -ENOMEM;
87 
88 	i2c_set_clientdata(i2c, priv);
89 	priv->variant = (long)of_id->data;
90 
91 	priv->regmap = devm_regmap_init_i2c(i2c, &rn5t618_regmap_config);
92 	if (IS_ERR(priv->regmap)) {
93 		ret = PTR_ERR(priv->regmap);
94 		dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
95 		return ret;
96 	}
97 
98 	ret = devm_mfd_add_devices(&i2c->dev, -1, rn5t618_cells,
99 				   ARRAY_SIZE(rn5t618_cells), NULL, 0, NULL);
100 	if (ret) {
101 		dev_err(&i2c->dev, "failed to add sub-devices: %d\n", ret);
102 		return ret;
103 	}
104 
105 	if (of_device_is_system_power_controller(i2c->dev.of_node)) {
106 		if (!pm_power_off) {
107 			rn5t618_pm_power_off = priv;
108 			pm_power_off = rn5t618_power_off;
109 		} else {
110 			dev_warn(&i2c->dev, "Poweroff callback already assigned\n");
111 		}
112 	}
113 
114 	return 0;
115 }
116 
117 static int rn5t618_i2c_remove(struct i2c_client *i2c)
118 {
119 	struct rn5t618 *priv = i2c_get_clientdata(i2c);
120 
121 	if (priv == rn5t618_pm_power_off) {
122 		rn5t618_pm_power_off = NULL;
123 		pm_power_off = NULL;
124 	}
125 
126 	return 0;
127 }
128 
129 static const struct i2c_device_id rn5t618_i2c_id[] = {
130 	{ }
131 };
132 MODULE_DEVICE_TABLE(i2c, rn5t618_i2c_id);
133 
134 static struct i2c_driver rn5t618_i2c_driver = {
135 	.driver = {
136 		.name = "rn5t618",
137 		.of_match_table = of_match_ptr(rn5t618_of_match),
138 	},
139 	.probe = rn5t618_i2c_probe,
140 	.remove = rn5t618_i2c_remove,
141 	.id_table = rn5t618_i2c_id,
142 };
143 
144 module_i2c_driver(rn5t618_i2c_driver);
145 
146 MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
147 MODULE_DESCRIPTION("Ricoh RN5T567/618 MFD driver");
148 MODULE_LICENSE("GPL v2");
149