Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[linux-2.6-microblaze.git] / drivers / regulator / bd9571mwv-regulator.c
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         bool bkup_mode_enabled;
34
35         /* Power switch type */
36         bool rstbmode_level;
37         bool rstbmode_pulse;
38 };
39
40 enum 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
59 static int bd9571mwv_avs_get_moni_state(struct regulator_dev *rdev)
60 {
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
71 static int bd9571mwv_avs_set_voltage_sel_regmap(struct regulator_dev *rdev,
72                                                 unsigned int sel)
73 {
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
84 static int bd9571mwv_avs_get_voltage_sel_regmap(struct regulator_dev *rdev)
85 {
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
103 static int bd9571mwv_reg_set_voltage_sel_regmap(struct regulator_dev *rdev,
104                                                 unsigned int sel)
105 {
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 */
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,
116 };
117
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,
124 };
125
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,
131 };
132
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),
145 };
146
147 #ifdef CONFIG_PM_SLEEP
148 static 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
161 static 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
175 static 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
183 static 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         unsigned int mode;
189         int ret;
190
191         if (!count)
192                 return 0;
193
194         ret = kstrtobool(buf, &bdreg->bkup_mode_enabled);
195         if (ret)
196                 return ret;
197
198         if (!bdreg->rstbmode_level)
199                 return count;
200
201         /*
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
204          */
205         ret = bd9571mwv_bkup_mode_read(bdreg->bd, &mode);
206         if (ret)
207                 return ret;
208
209         mode &= ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK;
210         if (bdreg->bkup_mode_enabled)
211                 mode |= bdreg->bkup_mode_cnt_keepon;
212
213         ret = bd9571mwv_bkup_mode_write(bdreg->bd, mode);
214         if (ret)
215                 return ret;
216
217         return count;
218 }
219
220 static DEVICE_ATTR_RW(backup_mode);
221
222 static int bd9571mwv_suspend(struct device *dev)
223 {
224         struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
225         unsigned int mode;
226         int ret;
227
228         if (!bdreg->bkup_mode_enabled)
229                 return 0;
230
231         /* Save DDR Backup Mode */
232         ret = bd9571mwv_bkup_mode_read(bdreg->bd, &mode);
233         if (ret)
234                 return ret;
235
236         bdreg->bkup_mode_cnt_saved = mode;
237
238         if (!bdreg->rstbmode_pulse)
239                 return 0;
240
241         /* Enable DDR Backup Mode */
242         mode &= ~BD9571MWV_BKUP_MODE_CNT_KEEPON_MASK;
243         mode |= bdreg->bkup_mode_cnt_keepon;
244
245         if (mode != bdreg->bkup_mode_cnt_saved)
246                 return bd9571mwv_bkup_mode_write(bdreg->bd, mode);
247
248         return 0;
249 }
250
251 static int bd9571mwv_resume(struct device *dev)
252 {
253         struct bd9571mwv_reg *bdreg = dev_get_drvdata(dev);
254
255         if (!bdreg->bkup_mode_enabled)
256                 return 0;
257
258         /* Restore DDR Backup Mode */
259         return bd9571mwv_bkup_mode_write(bdreg->bd, bdreg->bkup_mode_cnt_saved);
260 }
261
262 static const struct dev_pm_ops bd9571mwv_pm  = {
263         SET_SYSTEM_SLEEP_PM_OPS(bd9571mwv_suspend, bd9571mwv_resume)
264 };
265
266 static int bd9571mwv_regulator_remove(struct platform_device *pdev)
267 {
268         device_remove_file(&pdev->dev, &dev_attr_backup_mode);
269         return 0;
270 }
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 */
276
277 static int bd9571mwv_regulator_probe(struct platform_device *pdev)
278 {
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;
285
286         bdreg = devm_kzalloc(&pdev->dev, sizeof(*bdreg), GFP_KERNEL);
287         if (!bdreg)
288                 return -ENOMEM;
289
290         bdreg->bd = bd;
291
292         platform_set_drvdata(pdev, bdreg);
293
294         config.dev = &pdev->dev;
295         config.dev->of_node = bd->dev->of_node;
296         config.driver_data = bd;
297         config.regmap = bd->regmap;
298
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);
306                 }
307         }
308
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;
315         }
316         bdreg->bkup_mode_cnt_keepon = val;
317
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;
325         }
326
327 #ifdef CONFIG_PM_SLEEP
328         if (bdreg->bkup_mode_cnt_keepon) {
329                 int ret;
330
331                 /*
332                  * Backup mode is enabled by default in pulse mode, but needs
333                  * explicit user setup in level mode.
334                  */
335                 bdreg->bkup_mode_enabled = bdreg->rstbmode_pulse;
336
337                 ret = device_create_file(&pdev->dev, &dev_attr_backup_mode);
338                 if (ret)
339                         return ret;
340         }
341 #endif /* CONFIG_PM_SLEEP */
342
343         return 0;
344 }
345
346 static const struct platform_device_id bd9571mwv_regulator_id_table[] = {
347         { "bd9571mwv-regulator", },
348         { /* sentinel */ }
349 };
350 MODULE_DEVICE_TABLE(platform, bd9571mwv_regulator_id_table);
351
352 static struct platform_driver bd9571mwv_regulator_driver = {
353         .driver = {
354                 .name = "bd9571mwv-regulator",
355                 .pm = DEV_PM_OPS,
356         },
357         .probe = bd9571mwv_regulator_probe,
358         .remove = bd9571mwv_regulator_remove,
359         .id_table = bd9571mwv_regulator_id_table,
360 };
361 module_platform_driver(bd9571mwv_regulator_driver);
362
363 MODULE_AUTHOR("Marek Vasut <marek.vasut+renesas@gmail.com>");
364 MODULE_DESCRIPTION("BD9571MWV Regulator driver");
365 MODULE_LICENSE("GPL v2");