xref: /openbmc/linux/drivers/regulator/bd9571mwv-regulator.c (revision ba61bb17496d1664bf7c5c2fd650d5fd78bd0a92)
1 /*
2  * ROHM BD9571MWV-M regulator driver
3  *
4  * Copyright (C) 2017 Marek Vasut <marek.vasut+renesas@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11  * kind, whether expressed or implied; without even the implied warranty
12  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.
14  *
15  * Based on the TPS65086 driver
16  *
17  * NOTE: VD09 is missing
18  */
19 
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/platform_device.h>
23 #include <linux/regulator/driver.h>
24 
25 #include <linux/mfd/bd9571mwv.h>
26 
27 struct bd9571mwv_reg {
28 	struct bd9571mwv *bd;
29 
30 	/* DDR Backup Power */
31 	u8 bkup_mode_cnt_keepon;	/* from "rohm,ddr-backup-power" */
32 	u8 bkup_mode_cnt_saved;
33 
34 	/* Power switch type */
35 	bool rstbmode_level;
36 	bool rstbmode_pulse;
37 };
38 
39 enum bd9571mwv_regulators { VD09, VD18, VD25, VD33, DVFS };
40 
41 #define BD9571MWV_REG(_name, _of, _id, _ops, _vr, _vm, _nv, _min, _step, _lmin)\
42 	{							\
43 		.name			= _name,		\
44 		.of_match		= of_match_ptr(_of),	\
45 		.regulators_node	= "regulators",		\
46 		.id			= _id,			\
47 		.ops			= &_ops,		\
48 		.n_voltages		= _nv,			\
49 		.type			= REGULATOR_VOLTAGE,	\
50 		.owner			= THIS_MODULE,		\
51 		.vsel_reg		= _vr,			\
52 		.vsel_mask		= _vm,			\
53 		.min_uV			= _min,			\
54 		.uV_step		= _step,		\
55 		.linear_min_sel		= _lmin,		\
56 	}
57 
58 static int bd9571mwv_avs_get_moni_state(struct regulator_dev *rdev)
59 {
60 	unsigned int val;
61 	int ret;
62 
63 	ret = regmap_read(rdev->regmap, BD9571MWV_AVS_SET_MONI, &val);
64 	if (ret != 0)
65 		return ret;
66 
67 	return val & BD9571MWV_AVS_SET_MONI_MASK;
68 }
69 
70 static int bd9571mwv_avs_set_voltage_sel_regmap(struct regulator_dev *rdev,
71 						unsigned int sel)
72 {
73 	int ret;
74 
75 	ret = bd9571mwv_avs_get_moni_state(rdev);
76 	if (ret < 0)
77 		return ret;
78 
79 	return regmap_write_bits(rdev->regmap, BD9571MWV_AVS_VD09_VID(ret),
80 				 rdev->desc->vsel_mask, sel);
81 }
82 
83 static int bd9571mwv_avs_get_voltage_sel_regmap(struct regulator_dev *rdev)
84 {
85 	unsigned int val;
86 	int ret;
87 
88 	ret = bd9571mwv_avs_get_moni_state(rdev);
89 	if (ret < 0)
90 		return ret;
91 
92 	ret = regmap_read(rdev->regmap, BD9571MWV_AVS_VD09_VID(ret), &val);
93 	if (ret != 0)
94 		return ret;
95 
96 	val &= rdev->desc->vsel_mask;
97 	val >>= ffs(rdev->desc->vsel_mask) - 1;
98 
99 	return val;
100 }
101 
102 static int bd9571mwv_reg_set_voltage_sel_regmap(struct regulator_dev *rdev,
103 						unsigned int sel)
104 {
105 	return regmap_write_bits(rdev->regmap, BD9571MWV_DVFS_SETVID,
106 				 rdev->desc->vsel_mask, sel);
107 }
108 
109 /* Operations permitted on AVS voltage regulator */
110 static struct regulator_ops avs_ops = {
111 	.set_voltage_sel	= bd9571mwv_avs_set_voltage_sel_regmap,
112 	.map_voltage		= regulator_map_voltage_linear,
113 	.get_voltage_sel	= bd9571mwv_avs_get_voltage_sel_regmap,
114 	.list_voltage		= regulator_list_voltage_linear,
115 };
116 
117 /* Operations permitted on voltage regulators */
118 static struct regulator_ops reg_ops = {
119 	.set_voltage_sel	= bd9571mwv_reg_set_voltage_sel_regmap,
120 	.map_voltage		= regulator_map_voltage_linear,
121 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
122 	.list_voltage		= regulator_list_voltage_linear,
123 };
124 
125 /* Operations permitted on voltage monitors */
126 static struct regulator_ops vid_ops = {
127 	.map_voltage		= regulator_map_voltage_linear,
128 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
129 	.list_voltage		= regulator_list_voltage_linear,
130 };
131 
132 static struct regulator_desc regulators[] = {
133 	BD9571MWV_REG("VD09", "vd09", VD09, avs_ops, 0, 0x7f,
134 		      0x80, 600000, 10000, 0x3c),
135 	BD9571MWV_REG("VD18", "vd18", VD18, vid_ops, BD9571MWV_VD18_VID, 0xf,
136 		      16, 1625000, 25000, 0),
137 	BD9571MWV_REG("VD25", "vd25", VD25, vid_ops, BD9571MWV_VD25_VID, 0xf,
138 		      16, 2150000, 50000, 0),
139 	BD9571MWV_REG("VD33", "vd33", VD33, vid_ops, BD9571MWV_VD33_VID, 0xf,
140 		      11, 2800000, 100000, 0),
141 	BD9571MWV_REG("DVFS", "dvfs", DVFS, reg_ops,
142 		      BD9571MWV_DVFS_MONIVDAC, 0x7f,
143 		      0x80, 600000, 10000, 0x3c),
144 };
145 
146 #ifdef CONFIG_PM_SLEEP
147 static int bd9571mwv_bkup_mode_read(struct bd9571mwv *bd, unsigned int *mode)
148 {
149 	int ret;
150 
151 	ret = regmap_read(bd->regmap, BD9571MWV_BKUP_MODE_CNT, mode);
152 	if (ret) {
153 		dev_err(bd->dev, "failed to read backup mode (%d)\n", ret);
154 		return ret;
155 	}
156 
157 	return 0;
158 }
159 
160 static int bd9571mwv_bkup_mode_write(struct bd9571mwv *bd, unsigned int mode)
161 {
162 	int ret;
163 
164 	ret = regmap_write(bd->regmap, BD9571MWV_BKUP_MODE_CNT, mode);
165 	if (ret) {
166 		dev_err(bd->dev, "failed to configure backup mode 0x%x (%d)\n",
167 			mode, ret);
168 		return ret;
169 	}
170 
171 	return 0;
172 }
173 
174 static int bd9571mwv_suspend(struct device *dev)
175 {
176 	struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
177 	unsigned int mode;
178 	int ret;
179 
180 	if (!device_may_wakeup(dev))
181 		return 0;
182 
183 	/* Save DDR Backup Mode */
184 	ret = bd9571mwv_bkup_mode_read(bdreg->bd, &mode);
185 	if (ret)
186 		return ret;
187 
188 	bdreg->bkup_mode_cnt_saved = mode;
189 
190 	if (!bdreg->rstbmode_pulse)
191 		return 0;
192 
193 	/* Enable DDR Backup Mode */
194 	mode &= ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK;
195 	mode |= bdreg->bkup_mode_cnt_keepon;
196 
197 	if (mode != bdreg->bkup_mode_cnt_saved)
198 		return bd9571mwv_bkup_mode_write(bdreg->bd, mode);
199 
200 	return 0;
201 }
202 
203 static int bd9571mwv_resume(struct device *dev)
204 {
205 	struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
206 
207 	if (!device_may_wakeup(dev))
208 		return 0;
209 
210 	/* Restore DDR Backup Mode */
211 	return bd9571mwv_bkup_mode_write(bdreg->bd, bdreg->bkup_mode_cnt_saved);
212 }
213 
214 static const struct dev_pm_ops bd9571mwv_pm  = {
215 	SET_SYSTEM_SLEEP_PM_OPS(bd9571mwv_suspend, bd9571mwv_resume)
216 };
217 
218 #define DEV_PM_OPS	&bd9571mwv_pm
219 #else
220 #define DEV_PM_OPS	NULL
221 #endif /* CONFIG_PM_SLEEP */
222 
223 static int bd9571mwv_regulator_probe(struct platform_device *pdev)
224 {
225 	struct bd9571mwv *bd = dev_get_drvdata(pdev->dev.parent);
226 	struct regulator_config config = { };
227 	struct bd9571mwv_reg *bdreg;
228 	struct regulator_dev *rdev;
229 	unsigned int val;
230 	int i;
231 
232 	bdreg = devm_kzalloc(&pdev->dev, sizeof(*bdreg), GFP_KERNEL);
233 	if (!bdreg)
234 		return -ENOMEM;
235 
236 	bdreg->bd = bd;
237 
238 	platform_set_drvdata(pdev, bdreg);
239 
240 	config.dev = &pdev->dev;
241 	config.dev->of_node = bd->dev->of_node;
242 	config.driver_data = bd;
243 	config.regmap = bd->regmap;
244 
245 	for (i = 0; i < ARRAY_SIZE(regulators); i++) {
246 		rdev = devm_regulator_register(&pdev->dev, &regulators[i],
247 					       &config);
248 		if (IS_ERR(rdev)) {
249 			dev_err(bd->dev, "failed to register %s regulator\n",
250 				pdev->name);
251 			return PTR_ERR(rdev);
252 		}
253 	}
254 
255 	val = 0;
256 	of_property_read_u32(bd->dev->of_node, "rohm,ddr-backup-power", &val);
257 	if (val & ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK) {
258 		dev_err(bd->dev, "invalid %s mode %u\n",
259 			"rohm,ddr-backup-power", val);
260 		return -EINVAL;
261 	}
262 	bdreg->bkup_mode_cnt_keepon = val;
263 
264 	bdreg->rstbmode_level = of_property_read_bool(bd->dev->of_node,
265 						      "rohm,rstbmode-level");
266 	bdreg->rstbmode_pulse = of_property_read_bool(bd->dev->of_node,
267 						      "rohm,rstbmode-pulse");
268 	if (bdreg->rstbmode_level && bdreg->rstbmode_pulse) {
269 		dev_err(bd->dev, "only one rohm,rstbmode-* may be specified");
270 		return -EINVAL;
271 	}
272 
273 	if (bdreg->bkup_mode_cnt_keepon) {
274 		device_set_wakeup_capable(&pdev->dev, true);
275 		/*
276 		 * Wakeup is enabled by default in pulse mode, but needs
277 		 * explicit user setup in level mode.
278 		 */
279 		device_set_wakeup_enable(&pdev->dev, bdreg->rstbmode_pulse);
280 	}
281 
282 	return 0;
283 }
284 
285 static const struct platform_device_id bd9571mwv_regulator_id_table[] = {
286 	{ "bd9571mwv-regulator", },
287 	{ /* sentinel */ }
288 };
289 MODULE_DEVICE_TABLE(platform, bd9571mwv_regulator_id_table);
290 
291 static struct platform_driver bd9571mwv_regulator_driver = {
292 	.driver = {
293 		.name = "bd9571mwv-regulator",
294 		.pm = DEV_PM_OPS,
295 	},
296 	.probe = bd9571mwv_regulator_probe,
297 	.id_table = bd9571mwv_regulator_id_table,
298 };
299 module_platform_driver(bd9571mwv_regulator_driver);
300 
301 MODULE_AUTHOR("Marek Vasut <marek.vasut+renesas@gmail.com>");
302 MODULE_DESCRIPTION("BD9571MWV Regulator driver");
303 MODULE_LICENSE("GPL v2");
304