ALSA: hda/ca0132 - Call pci_iounmap() instead of iounmap()
[linux/fpc-iii.git] / drivers / regulator / bd9571mwv-regulator.c
blob274c5ed7cd737ecd3e4eae78458b1879af44747a
1 /*
2 * ROHM BD9571MWV-M regulator driver
4 * Copyright (C) 2017 Marek Vasut <marek.vasut+renesas@gmail.com>
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.
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.
15 * Based on the TPS65086 driver
17 * NOTE: VD09 is missing
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/platform_device.h>
23 #include <linux/regulator/driver.h>
25 #include <linux/mfd/bd9571mwv.h>
27 struct bd9571mwv_reg {
28 struct bd9571mwv *bd;
30 /* DDR Backup Power */
31 u8 bkup_mode_cnt_keepon; /* from "rohm,ddr-backup-power" */
32 u8 bkup_mode_cnt_saved;
33 bool bkup_mode_enabled;
35 /* Power switch type */
36 bool rstbmode_level;
37 bool rstbmode_pulse;
40 enum bd9571mwv_regulators { VD09, VD18, VD25, VD33, DVFS };
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, \
59 static int bd9571mwv_avs_get_moni_state(struct regulator_dev *rdev)
61 unsigned int val;
62 int ret;
64 ret = regmap_read(rdev->regmap, BD9571MWV_AVS_SET_MONI, &val);
65 if (ret != 0)
66 return ret;
68 return val & BD9571MWV_AVS_SET_MONI_MASK;
71 static int bd9571mwv_avs_set_voltage_sel_regmap(struct regulator_dev *rdev,
72 unsigned int sel)
74 int ret;
76 ret = bd9571mwv_avs_get_moni_state(rdev);
77 if (ret < 0)
78 return ret;
80 return regmap_write_bits(rdev->regmap, BD9571MWV_AVS_VD09_VID(ret),
81 rdev->desc->vsel_mask, sel);
84 static int bd9571mwv_avs_get_voltage_sel_regmap(struct regulator_dev *rdev)
86 unsigned int val;
87 int ret;
89 ret = bd9571mwv_avs_get_moni_state(rdev);
90 if (ret < 0)
91 return ret;
93 ret = regmap_read(rdev->regmap, BD9571MWV_AVS_VD09_VID(ret), &val);
94 if (ret != 0)
95 return ret;
97 val &= rdev->desc->vsel_mask;
98 val >>= ffs(rdev->desc->vsel_mask) - 1;
100 return val;
103 static int bd9571mwv_reg_set_voltage_sel_regmap(struct regulator_dev *rdev,
104 unsigned int sel)
106 return regmap_write_bits(rdev->regmap, BD9571MWV_DVFS_SETVID,
107 rdev->desc->vsel_mask, sel);
110 /* Operations permitted on AVS voltage regulator */
111 static 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,
118 /* Operations permitted on voltage regulators */
119 static 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,
126 /* Operations permitted on voltage monitors */
127 static 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,
133 static 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),
147 #ifdef CONFIG_PM_SLEEP
148 static int bd9571mwv_bkup_mode_read(struct bd9571mwv *bd, unsigned int *mode)
150 int ret;
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;
158 return 0;
161 static int bd9571mwv_bkup_mode_write(struct bd9571mwv *bd, unsigned int mode)
163 int ret;
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;
172 return 0;
175 static ssize_t backup_mode_show(struct device *dev,
176 struct device_attribute *attr, char *buf)
178 struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
180 return sprintf(buf, "%s\n", bdreg->bkup_mode_enabled ? "on" : "off");
183 static ssize_t backup_mode_store(struct device *dev,
184 struct device_attribute *attr,
185 const char *buf, size_t count)
187 struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
188 unsigned int mode;
189 int ret;
191 if (!count)
192 return 0;
194 ret = kstrtobool(buf, &bdreg->bkup_mode_enabled);
195 if (ret)
196 return ret;
198 if (!bdreg->rstbmode_level)
199 return count;
202 * Configure DDR Backup Mode, to change the role of the accessory power
203 * switch from a power switch to a wake-up switch, or vice versa
205 ret = bd9571mwv_bkup_mode_read(bdreg->bd, &mode);
206 if (ret)
207 return ret;
209 mode &= ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK;
210 if (bdreg->bkup_mode_enabled)
211 mode |= bdreg->bkup_mode_cnt_keepon;
213 ret = bd9571mwv_bkup_mode_write(bdreg->bd, mode);
214 if (ret)
215 return ret;
217 return count;
220 static DEVICE_ATTR_RW(backup_mode);
222 static int bd9571mwv_suspend(struct device *dev)
224 struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
225 unsigned int mode;
226 int ret;
228 if (!bdreg->bkup_mode_enabled)
229 return 0;
231 /* Save DDR Backup Mode */
232 ret = bd9571mwv_bkup_mode_read(bdreg->bd, &mode);
233 if (ret)
234 return ret;
236 bdreg->bkup_mode_cnt_saved = mode;
238 if (!bdreg->rstbmode_pulse)
239 return 0;
241 /* Enable DDR Backup Mode */
242 mode &= ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK;
243 mode |= bdreg->bkup_mode_cnt_keepon;
245 if (mode != bdreg->bkup_mode_cnt_saved)
246 return bd9571mwv_bkup_mode_write(bdreg->bd, mode);
248 return 0;
251 static int bd9571mwv_resume(struct device *dev)
253 struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
255 if (!bdreg->bkup_mode_enabled)
256 return 0;
258 /* Restore DDR Backup Mode */
259 return bd9571mwv_bkup_mode_write(bdreg->bd, bdreg->bkup_mode_cnt_saved);
262 static const struct dev_pm_ops bd9571mwv_pm = {
263 SET_SYSTEM_SLEEP_PM_OPS(bd9571mwv_suspend, bd9571mwv_resume)
266 static int bd9571mwv_regulator_remove(struct platform_device *pdev)
268 device_remove_file(&pdev->dev, &dev_attr_backup_mode);
269 return 0;
271 #define DEV_PM_OPS &bd9571mwv_pm
272 #else
273 #define DEV_PM_OPS NULL
274 #define bd9571mwv_regulator_remove NULL
275 #endif /* CONFIG_PM_SLEEP */
277 static int bd9571mwv_regulator_probe(struct platform_device *pdev)
279 struct bd9571mwv *bd = dev_get_drvdata(pdev->dev.parent);
280 struct regulator_config config = { };
281 struct bd9571mwv_reg *bdreg;
282 struct regulator_dev *rdev;
283 unsigned int val;
284 int i;
286 bdreg = devm_kzalloc(&pdev->dev, sizeof(*bdreg), GFP_KERNEL);
287 if (!bdreg)
288 return -ENOMEM;
290 bdreg->bd = bd;
292 platform_set_drvdata(pdev, bdreg);
294 config.dev = &pdev->dev;
295 config.dev->of_node = bd->dev->of_node;
296 config.driver_data = bd;
297 config.regmap = bd->regmap;
299 for (i = 0; i < ARRAY_SIZE(regulators); i++) {
300 rdev = devm_regulator_register(&pdev->dev, &regulators[i],
301 &config);
302 if (IS_ERR(rdev)) {
303 dev_err(bd->dev, "failed to register %s regulator\n",
304 pdev->name);
305 return PTR_ERR(rdev);
309 val = 0;
310 of_property_read_u32(bd->dev->of_node, "rohm,ddr-backup-power", &val);
311 if (val & ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK) {
312 dev_err(bd->dev, "invalid %s mode %u\n",
313 "rohm,ddr-backup-power", val);
314 return -EINVAL;
316 bdreg->bkup_mode_cnt_keepon = val;
318 bdreg->rstbmode_level = of_property_read_bool(bd->dev->of_node,
319 "rohm,rstbmode-level");
320 bdreg->rstbmode_pulse = of_property_read_bool(bd->dev->of_node,
321 "rohm,rstbmode-pulse");
322 if (bdreg->rstbmode_level && bdreg->rstbmode_pulse) {
323 dev_err(bd->dev, "only one rohm,rstbmode-* may be specified");
324 return -EINVAL;
327 #ifdef CONFIG_PM_SLEEP
328 if (bdreg->bkup_mode_cnt_keepon) {
329 int ret;
332 * Backup mode is enabled by default in pulse mode, but needs
333 * explicit user setup in level mode.
335 bdreg->bkup_mode_enabled = bdreg->rstbmode_pulse;
337 ret = device_create_file(&pdev->dev, &dev_attr_backup_mode);
338 if (ret)
339 return ret;
341 #endif /* CONFIG_PM_SLEEP */
343 return 0;
346 static const struct platform_device_id bd9571mwv_regulator_id_table[] = {
347 { "bd9571mwv-regulator", },
348 { /* sentinel */ }
350 MODULE_DEVICE_TABLE(platform, bd9571mwv_regulator_id_table);
352 static struct platform_driver bd9571mwv_regulator_driver = {
353 .driver = {
354 .name = "bd9571mwv-regulator",
355 .pm = DEV_PM_OPS,
357 .probe = bd9571mwv_regulator_probe,
358 .remove = bd9571mwv_regulator_remove,
359 .id_table = bd9571mwv_regulator_id_table,
361 module_platform_driver(bd9571mwv_regulator_driver);
363 MODULE_AUTHOR("Marek Vasut <marek.vasut+renesas@gmail.com>");
364 MODULE_DESCRIPTION("BD9571MWV Regulator driver");
365 MODULE_LICENSE("GPL v2");