blob: 1da36a6590c84ba47a2a88ff68b57a5288428549 [file] [log] [blame]
Marek Vasute85c5a12017-04-24 17:21:30 +02001/*
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
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +020027struct 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;
Geert Uytterhoeven02b3a072018-07-16 17:30:50 +020033 bool bkup_mode_enabled;
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +020034
35 /* Power switch type */
36 bool rstbmode_level;
37 bool rstbmode_pulse;
38};
39
Marek Vasute85c5a12017-04-24 17:21:30 +020040enum bd9571mwv_regulators { VD09, VD18, VD25, VD33, DVFS };
41
42#define BD9571MWV_REG(_name, _of, _id, _ops, _vr, _vm, _nv, _min, _step, _lmin)\
43 { \
44 .name = _name, \
45 .of_match = of_match_ptr(_of), \
46 .regulators_node = "regulators", \
47 .id = _id, \
48 .ops = &_ops, \
49 .n_voltages = _nv, \
50 .type = REGULATOR_VOLTAGE, \
51 .owner = THIS_MODULE, \
52 .vsel_reg = _vr, \
53 .vsel_mask = _vm, \
54 .min_uV = _min, \
55 .uV_step = _step, \
56 .linear_min_sel = _lmin, \
57 }
58
Axel Lincb42b642017-05-28 14:02:17 +080059static int bd9571mwv_avs_get_moni_state(struct regulator_dev *rdev)
Marek Vasute85c5a12017-04-24 17:21:30 +020060{
61 unsigned int val;
62 int ret;
63
64 ret = regmap_read(rdev->regmap, BD9571MWV_AVS_SET_MONI, &val);
65 if (ret != 0)
66 return ret;
67
68 return val & BD9571MWV_AVS_SET_MONI_MASK;
69}
70
Axel Lincb42b642017-05-28 14:02:17 +080071static int bd9571mwv_avs_set_voltage_sel_regmap(struct regulator_dev *rdev,
72 unsigned int sel)
Marek Vasute85c5a12017-04-24 17:21:30 +020073{
74 int ret;
75
76 ret = bd9571mwv_avs_get_moni_state(rdev);
77 if (ret < 0)
78 return ret;
79
80 return regmap_write_bits(rdev->regmap, BD9571MWV_AVS_VD09_VID(ret),
81 rdev->desc->vsel_mask, sel);
82}
83
Axel Lincb42b642017-05-28 14:02:17 +080084static int bd9571mwv_avs_get_voltage_sel_regmap(struct regulator_dev *rdev)
Marek Vasute85c5a12017-04-24 17:21:30 +020085{
86 unsigned int val;
87 int ret;
88
89 ret = bd9571mwv_avs_get_moni_state(rdev);
90 if (ret < 0)
91 return ret;
92
93 ret = regmap_read(rdev->regmap, BD9571MWV_AVS_VD09_VID(ret), &val);
94 if (ret != 0)
95 return ret;
96
97 val &= rdev->desc->vsel_mask;
98 val >>= ffs(rdev->desc->vsel_mask) - 1;
99
100 return val;
101}
102
Axel Lincb42b642017-05-28 14:02:17 +0800103static int bd9571mwv_reg_set_voltage_sel_regmap(struct regulator_dev *rdev,
104 unsigned int sel)
Marek Vasute85c5a12017-04-24 17:21:30 +0200105{
106 return regmap_write_bits(rdev->regmap, BD9571MWV_DVFS_SETVID,
107 rdev->desc->vsel_mask, sel);
108}
109
110/* Operations permitted on AVS voltage regulator */
111static struct regulator_ops avs_ops = {
112 .set_voltage_sel = bd9571mwv_avs_set_voltage_sel_regmap,
113 .map_voltage = regulator_map_voltage_linear,
114 .get_voltage_sel = bd9571mwv_avs_get_voltage_sel_regmap,
115 .list_voltage = regulator_list_voltage_linear,
116};
117
118/* Operations permitted on voltage regulators */
119static struct regulator_ops reg_ops = {
120 .set_voltage_sel = bd9571mwv_reg_set_voltage_sel_regmap,
121 .map_voltage = regulator_map_voltage_linear,
122 .get_voltage_sel = regulator_get_voltage_sel_regmap,
123 .list_voltage = regulator_list_voltage_linear,
124};
125
126/* Operations permitted on voltage monitors */
127static struct regulator_ops vid_ops = {
128 .map_voltage = regulator_map_voltage_linear,
129 .get_voltage_sel = regulator_get_voltage_sel_regmap,
130 .list_voltage = regulator_list_voltage_linear,
131};
132
133static struct regulator_desc regulators[] = {
134 BD9571MWV_REG("VD09", "vd09", VD09, avs_ops, 0, 0x7f,
135 0x80, 600000, 10000, 0x3c),
136 BD9571MWV_REG("VD18", "vd18", VD18, vid_ops, BD9571MWV_VD18_VID, 0xf,
137 16, 1625000, 25000, 0),
138 BD9571MWV_REG("VD25", "vd25", VD25, vid_ops, BD9571MWV_VD25_VID, 0xf,
139 16, 2150000, 50000, 0),
140 BD9571MWV_REG("VD33", "vd33", VD33, vid_ops, BD9571MWV_VD33_VID, 0xf,
141 11, 2800000, 100000, 0),
142 BD9571MWV_REG("DVFS", "dvfs", DVFS, reg_ops,
143 BD9571MWV_DVFS_MONIVDAC, 0x7f,
144 0x80, 600000, 10000, 0x3c),
145};
146
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200147#ifdef CONFIG_PM_SLEEP
148static int bd9571mwv_bkup_mode_read(struct bd9571mwv *bd, unsigned int *mode)
149{
150 int ret;
151
152 ret = regmap_read(bd->regmap, BD9571MWV_BKUP_MODE_CNT, mode);
153 if (ret) {
154 dev_err(bd->dev, "failed to read backup mode (%d)\n", ret);
155 return ret;
156 }
157
158 return 0;
159}
160
161static int bd9571mwv_bkup_mode_write(struct bd9571mwv *bd, unsigned int mode)
162{
163 int ret;
164
165 ret = regmap_write(bd->regmap, BD9571MWV_BKUP_MODE_CNT, mode);
166 if (ret) {
167 dev_err(bd->dev, "failed to configure backup mode 0x%x (%d)\n",
168 mode, ret);
169 return ret;
170 }
171
172 return 0;
173}
174
Geert Uytterhoeven02b3a072018-07-16 17:30:50 +0200175static ssize_t backup_mode_show(struct device *dev,
176 struct device_attribute *attr, char *buf)
177{
178 struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
179
180 return sprintf(buf, "%s\n", bdreg->bkup_mode_enabled ? "on" : "off");
181}
182
183static ssize_t backup_mode_store(struct device *dev,
184 struct device_attribute *attr,
185 const char *buf, size_t count)
186{
187 struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
188 int ret;
189
190 if (!count)
191 return 0;
192
193 ret = kstrtobool(buf, &bdreg->bkup_mode_enabled);
194 if (ret)
195 return ret;
196
197 return count;
198}
199
200DEVICE_ATTR_RW(backup_mode);
201
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200202static int bd9571mwv_suspend(struct device *dev)
203{
204 struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
205 unsigned int mode;
206 int ret;
207
Geert Uytterhoeven02b3a072018-07-16 17:30:50 +0200208 if (!bdreg->bkup_mode_enabled)
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200209 return 0;
210
211 /* Save DDR Backup Mode */
212 ret = bd9571mwv_bkup_mode_read(bdreg->bd, &mode);
213 if (ret)
214 return ret;
215
216 bdreg->bkup_mode_cnt_saved = mode;
217
218 if (!bdreg->rstbmode_pulse)
219 return 0;
220
221 /* Enable DDR Backup Mode */
222 mode &= ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK;
223 mode |= bdreg->bkup_mode_cnt_keepon;
224
225 if (mode != bdreg->bkup_mode_cnt_saved)
226 return bd9571mwv_bkup_mode_write(bdreg->bd, mode);
227
228 return 0;
229}
230
231static int bd9571mwv_resume(struct device *dev)
232{
233 struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
234
Geert Uytterhoeven02b3a072018-07-16 17:30:50 +0200235 if (!bdreg->bkup_mode_enabled)
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200236 return 0;
237
238 /* Restore DDR Backup Mode */
239 return bd9571mwv_bkup_mode_write(bdreg->bd, bdreg->bkup_mode_cnt_saved);
240}
241
242static const struct dev_pm_ops bd9571mwv_pm = {
243 SET_SYSTEM_SLEEP_PM_OPS(bd9571mwv_suspend, bd9571mwv_resume)
244};
245
Geert Uytterhoeven02b3a072018-07-16 17:30:50 +0200246static int bd9571mwv_regulator_remove(struct platform_device *pdev)
247{
248 device_remove_file(&pdev->dev, &dev_attr_backup_mode);
249 return 0;
250}
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200251#define DEV_PM_OPS &bd9571mwv_pm
252#else
253#define DEV_PM_OPS NULL
Geert Uytterhoeven02b3a072018-07-16 17:30:50 +0200254#define bd9571mwv_regulator_remove NULL
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200255#endif /* CONFIG_PM_SLEEP */
256
Marek Vasute85c5a12017-04-24 17:21:30 +0200257static int bd9571mwv_regulator_probe(struct platform_device *pdev)
258{
259 struct bd9571mwv *bd = dev_get_drvdata(pdev->dev.parent);
260 struct regulator_config config = { };
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200261 struct bd9571mwv_reg *bdreg;
Marek Vasute85c5a12017-04-24 17:21:30 +0200262 struct regulator_dev *rdev;
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200263 unsigned int val;
Marek Vasute85c5a12017-04-24 17:21:30 +0200264 int i;
265
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200266 bdreg = devm_kzalloc(&pdev->dev, sizeof(*bdreg), GFP_KERNEL);
267 if (!bdreg)
268 return -ENOMEM;
269
270 bdreg->bd = bd;
271
272 platform_set_drvdata(pdev, bdreg);
Marek Vasute85c5a12017-04-24 17:21:30 +0200273
274 config.dev = &pdev->dev;
275 config.dev->of_node = bd->dev->of_node;
276 config.driver_data = bd;
277 config.regmap = bd->regmap;
278
279 for (i = 0; i < ARRAY_SIZE(regulators); i++) {
280 rdev = devm_regulator_register(&pdev->dev, &regulators[i],
281 &config);
282 if (IS_ERR(rdev)) {
283 dev_err(bd->dev, "failed to register %s regulator\n",
284 pdev->name);
285 return PTR_ERR(rdev);
286 }
287 }
288
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200289 val = 0;
290 of_property_read_u32(bd->dev->of_node, "rohm,ddr-backup-power", &val);
291 if (val & ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK) {
292 dev_err(bd->dev, "invalid %s mode %u\n",
293 "rohm,ddr-backup-power", val);
294 return -EINVAL;
295 }
296 bdreg->bkup_mode_cnt_keepon = val;
297
298 bdreg->rstbmode_level = of_property_read_bool(bd->dev->of_node,
299 "rohm,rstbmode-level");
300 bdreg->rstbmode_pulse = of_property_read_bool(bd->dev->of_node,
301 "rohm,rstbmode-pulse");
302 if (bdreg->rstbmode_level && bdreg->rstbmode_pulse) {
303 dev_err(bd->dev, "only one rohm,rstbmode-* may be specified");
304 return -EINVAL;
305 }
306
Geert Uytterhoeven02b3a072018-07-16 17:30:50 +0200307#ifdef CONFIG_PM_SLEEP
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200308 if (bdreg->bkup_mode_cnt_keepon) {
Geert Uytterhoeven02b3a072018-07-16 17:30:50 +0200309 int ret;
310
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200311 /*
Geert Uytterhoeven02b3a072018-07-16 17:30:50 +0200312 * Backup mode is enabled by default in pulse mode, but needs
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200313 * explicit user setup in level mode.
314 */
Geert Uytterhoeven02b3a072018-07-16 17:30:50 +0200315 bdreg->bkup_mode_enabled = bdreg->rstbmode_pulse;
316
317 ret = device_create_file(&pdev->dev, &dev_attr_backup_mode);
318 if (ret)
319 return ret;
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200320 }
Geert Uytterhoeven02b3a072018-07-16 17:30:50 +0200321#endif /* CONFIG_PM_SLEEP */
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200322
Marek Vasute85c5a12017-04-24 17:21:30 +0200323 return 0;
324}
325
326static const struct platform_device_id bd9571mwv_regulator_id_table[] = {
327 { "bd9571mwv-regulator", },
328 { /* sentinel */ }
329};
330MODULE_DEVICE_TABLE(platform, bd9571mwv_regulator_id_table);
331
332static struct platform_driver bd9571mwv_regulator_driver = {
333 .driver = {
334 .name = "bd9571mwv-regulator",
Geert Uytterhoeven6eb0bfa2018-04-18 15:18:04 +0200335 .pm = DEV_PM_OPS,
Marek Vasute85c5a12017-04-24 17:21:30 +0200336 },
337 .probe = bd9571mwv_regulator_probe,
Geert Uytterhoeven02b3a072018-07-16 17:30:50 +0200338 .remove = bd9571mwv_regulator_remove,
Marek Vasute85c5a12017-04-24 17:21:30 +0200339 .id_table = bd9571mwv_regulator_id_table,
340};
341module_platform_driver(bd9571mwv_regulator_driver);
342
343MODULE_AUTHOR("Marek Vasut <marek.vasut+renesas@gmail.com>");
344MODULE_DESCRIPTION("BD9571MWV Regulator driver");
345MODULE_LICENSE("GPL v2");