1 // SPDX-License-Identifier: GPL-2.0
3 * ROHM BD9571MWV-M and BD9574MWF-M regulator driver
5 * Copyright (C) 2017 Marek Vasut <marek.vasut+renesas@gmail.com>
7 * Based on the TPS65086 driver
9 * NOTE: VD09 is missing
12 #include <linux/mfd/rohm-generic.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/driver.h>
18 #include <linux/mfd/bd9571mwv.h>
20 struct bd9571mwv_reg
{
21 struct regmap
*regmap
;
23 /* DDR Backup Power */
24 u8 bkup_mode_cnt_keepon
; /* from "rohm,ddr-backup-power" */
25 u8 bkup_mode_cnt_saved
;
26 bool bkup_mode_enabled
;
28 /* Power switch type */
33 enum bd9571mwv_regulators
{ VD09
, VD18
, VD25
, VD33
, DVFS
};
35 #define BD9571MWV_REG(_name, _of, _id, _ops, _vr, _vm, _nv, _min, _step, _lmin)\
38 .of_match = of_match_ptr(_of), \
39 .regulators_node = "regulators", \
43 .type = REGULATOR_VOLTAGE, \
44 .owner = THIS_MODULE, \
49 .linear_min_sel = _lmin, \
52 static int bd9571mwv_avs_get_moni_state(struct regulator_dev
*rdev
)
57 ret
= regmap_read(rdev
->regmap
, BD9571MWV_AVS_SET_MONI
, &val
);
61 return val
& BD9571MWV_AVS_SET_MONI_MASK
;
64 static int bd9571mwv_avs_set_voltage_sel_regmap(struct regulator_dev
*rdev
,
69 ret
= bd9571mwv_avs_get_moni_state(rdev
);
73 return regmap_write_bits(rdev
->regmap
, BD9571MWV_AVS_VD09_VID(ret
),
74 rdev
->desc
->vsel_mask
, sel
);
77 static int bd9571mwv_avs_get_voltage_sel_regmap(struct regulator_dev
*rdev
)
82 ret
= bd9571mwv_avs_get_moni_state(rdev
);
86 ret
= regmap_read(rdev
->regmap
, BD9571MWV_AVS_VD09_VID(ret
), &val
);
90 val
&= rdev
->desc
->vsel_mask
;
91 val
>>= ffs(rdev
->desc
->vsel_mask
) - 1;
96 static int bd9571mwv_reg_set_voltage_sel_regmap(struct regulator_dev
*rdev
,
99 return regmap_write_bits(rdev
->regmap
, BD9571MWV_DVFS_SETVID
,
100 rdev
->desc
->vsel_mask
, sel
);
103 /* Operations permitted on AVS voltage regulator */
104 static const struct regulator_ops avs_ops
= {
105 .set_voltage_sel
= bd9571mwv_avs_set_voltage_sel_regmap
,
106 .map_voltage
= regulator_map_voltage_linear
,
107 .get_voltage_sel
= bd9571mwv_avs_get_voltage_sel_regmap
,
108 .list_voltage
= regulator_list_voltage_linear
,
111 /* Operations permitted on voltage regulators */
112 static const struct regulator_ops reg_ops
= {
113 .set_voltage_sel
= bd9571mwv_reg_set_voltage_sel_regmap
,
114 .map_voltage
= regulator_map_voltage_linear
,
115 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
116 .list_voltage
= regulator_list_voltage_linear
,
119 /* Operations permitted on voltage monitors */
120 static const struct regulator_ops vid_ops
= {
121 .map_voltage
= regulator_map_voltage_linear
,
122 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
123 .list_voltage
= regulator_list_voltage_linear
,
126 static const struct regulator_desc regulators
[] = {
127 BD9571MWV_REG("VD09", "vd09", VD09
, avs_ops
, 0, 0x7f,
128 0x6f, 600000, 10000, 0x3c),
129 BD9571MWV_REG("VD18", "vd18", VD18
, vid_ops
, BD9571MWV_VD18_VID
, 0xf,
130 16, 1625000, 25000, 0),
131 BD9571MWV_REG("VD25", "vd25", VD25
, vid_ops
, BD9571MWV_VD25_VID
, 0xf,
132 16, 2150000, 50000, 0),
133 BD9571MWV_REG("VD33", "vd33", VD33
, vid_ops
, BD9571MWV_VD33_VID
, 0xf,
134 11, 2800000, 100000, 0),
135 BD9571MWV_REG("DVFS", "dvfs", DVFS
, reg_ops
,
136 BD9571MWV_DVFS_MONIVDAC
, 0x7f,
137 0x6f, 600000, 10000, 0x3c),
140 #ifdef CONFIG_PM_SLEEP
141 static int bd9571mwv_bkup_mode_read(struct bd9571mwv_reg
*bdreg
,
146 ret
= regmap_read(bdreg
->regmap
, BD9571MWV_BKUP_MODE_CNT
, mode
);
148 dev_err(regmap_get_device(bdreg
->regmap
),
149 "failed to read backup mode (%d)\n", ret
);
156 static int bd9571mwv_bkup_mode_write(struct bd9571mwv_reg
*bdreg
,
161 ret
= regmap_write(bdreg
->regmap
, BD9571MWV_BKUP_MODE_CNT
, mode
);
163 dev_err(regmap_get_device(bdreg
->regmap
),
164 "failed to configure backup mode 0x%x (%d)\n",
172 static ssize_t
backup_mode_show(struct device
*dev
,
173 struct device_attribute
*attr
, char *buf
)
175 struct bd9571mwv_reg
*bdreg
= dev_get_drvdata(dev
);
177 return sysfs_emit(buf
, "%s\n", bdreg
->bkup_mode_enabled
? "on" : "off");
180 static ssize_t
backup_mode_store(struct device
*dev
,
181 struct device_attribute
*attr
,
182 const char *buf
, size_t count
)
184 struct bd9571mwv_reg
*bdreg
= dev_get_drvdata(dev
);
191 ret
= kstrtobool(buf
, &bdreg
->bkup_mode_enabled
);
195 if (!bdreg
->rstbmode_level
)
199 * Configure DDR Backup Mode, to change the role of the accessory power
200 * switch from a power switch to a wake-up switch, or vice versa
202 ret
= bd9571mwv_bkup_mode_read(bdreg
, &mode
);
206 mode
&= ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK
;
207 if (bdreg
->bkup_mode_enabled
)
208 mode
|= bdreg
->bkup_mode_cnt_keepon
;
210 ret
= bd9571mwv_bkup_mode_write(bdreg
, mode
);
217 static DEVICE_ATTR_RW(backup_mode
);
219 static int bd9571mwv_suspend(struct device
*dev
)
221 struct bd9571mwv_reg
*bdreg
= dev_get_drvdata(dev
);
225 if (!bdreg
->bkup_mode_enabled
)
228 /* Save DDR Backup Mode */
229 ret
= bd9571mwv_bkup_mode_read(bdreg
, &mode
);
233 bdreg
->bkup_mode_cnt_saved
= mode
;
235 if (!bdreg
->rstbmode_pulse
)
238 /* Enable DDR Backup Mode */
239 mode
&= ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK
;
240 mode
|= bdreg
->bkup_mode_cnt_keepon
;
242 if (mode
!= bdreg
->bkup_mode_cnt_saved
)
243 return bd9571mwv_bkup_mode_write(bdreg
, mode
);
248 static int bd9571mwv_resume(struct device
*dev
)
250 struct bd9571mwv_reg
*bdreg
= dev_get_drvdata(dev
);
252 if (!bdreg
->bkup_mode_enabled
)
255 /* Restore DDR Backup Mode */
256 return bd9571mwv_bkup_mode_write(bdreg
, bdreg
->bkup_mode_cnt_saved
);
259 static const struct dev_pm_ops bd9571mwv_pm
= {
260 SET_SYSTEM_SLEEP_PM_OPS(bd9571mwv_suspend
, bd9571mwv_resume
)
263 static void bd9571mwv_regulator_remove(struct platform_device
*pdev
)
265 device_remove_file(&pdev
->dev
, &dev_attr_backup_mode
);
267 #define DEV_PM_OPS &bd9571mwv_pm
269 #define DEV_PM_OPS NULL
270 #define bd9571mwv_regulator_remove NULL
271 #endif /* CONFIG_PM_SLEEP */
273 static int bd9571mwv_regulator_probe(struct platform_device
*pdev
)
275 struct regulator_config config
= { };
276 struct bd9571mwv_reg
*bdreg
;
277 struct regulator_dev
*rdev
;
280 enum rohm_chip_type chip
= platform_get_device_id(pdev
)->driver_data
;
282 bdreg
= devm_kzalloc(&pdev
->dev
, sizeof(*bdreg
), GFP_KERNEL
);
286 bdreg
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
288 platform_set_drvdata(pdev
, bdreg
);
290 config
.dev
= &pdev
->dev
;
291 config
.dev
->of_node
= pdev
->dev
.parent
->of_node
;
292 config
.driver_data
= bdreg
;
293 config
.regmap
= bdreg
->regmap
;
295 for (i
= 0; i
< ARRAY_SIZE(regulators
); i
++) {
296 /* BD9574MWF supports DVFS only */
297 if (chip
== ROHM_CHIP_TYPE_BD9574
&& regulators
[i
].id
!= DVFS
)
299 rdev
= devm_regulator_register(&pdev
->dev
, ®ulators
[i
],
302 dev_err(&pdev
->dev
, "failed to register %s regulator\n",
304 return PTR_ERR(rdev
);
309 of_property_read_u32(config
.dev
->of_node
, "rohm,ddr-backup-power", &val
);
310 if (val
& ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK
) {
311 dev_err(&pdev
->dev
, "invalid %s mode %u\n",
312 "rohm,ddr-backup-power", val
);
315 bdreg
->bkup_mode_cnt_keepon
= val
;
317 bdreg
->rstbmode_level
= of_property_read_bool(config
.dev
->of_node
,
318 "rohm,rstbmode-level");
319 bdreg
->rstbmode_pulse
= of_property_read_bool(config
.dev
->of_node
,
320 "rohm,rstbmode-pulse");
321 if (bdreg
->rstbmode_level
&& bdreg
->rstbmode_pulse
) {
322 dev_err(&pdev
->dev
, "only one rohm,rstbmode-* may be specified");
326 #ifdef CONFIG_PM_SLEEP
327 if (bdreg
->bkup_mode_cnt_keepon
) {
331 * Backup mode is enabled by default in pulse mode, but needs
332 * explicit user setup in level mode.
334 bdreg
->bkup_mode_enabled
= bdreg
->rstbmode_pulse
;
336 ret
= device_create_file(&pdev
->dev
, &dev_attr_backup_mode
);
340 #endif /* CONFIG_PM_SLEEP */
345 static const struct platform_device_id bd9571mwv_regulator_id_table
[] = {
346 { "bd9571mwv-regulator", ROHM_CHIP_TYPE_BD9571
},
347 { "bd9574mwf-regulator", ROHM_CHIP_TYPE_BD9574
},
350 MODULE_DEVICE_TABLE(platform
, bd9571mwv_regulator_id_table
);
352 static struct platform_driver bd9571mwv_regulator_driver
= {
354 .name
= "bd9571mwv-regulator",
355 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
358 .probe
= bd9571mwv_regulator_probe
,
359 .remove
= bd9571mwv_regulator_remove
,
360 .id_table
= bd9571mwv_regulator_id_table
,
362 module_platform_driver(bd9571mwv_regulator_driver
);
364 MODULE_AUTHOR("Marek Vasut <marek.vasut+renesas@gmail.com>");
365 MODULE_DESCRIPTION("BD9571MWV Regulator driver");
366 MODULE_LICENSE("GPL v2");