summaryrefslogtreecommitdiffstats
path: root/drivers/regulator/rt5120-regulator.c
blob: a388ac70865fd84ac04ada50f92618914781ccb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// SPDX-License-Identifier: GPL-2.0+

#include <linux/bits.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/regulator/of_regulator.h>

#define RT5120_REG_PGSTAT	0x03
#define RT5120_REG_CH1VID	0x06
#define RT5120_REG_CH1SLPVID	0x07
#define RT5120_REG_ENABLE	0x08
#define RT5120_REG_MODECTL	0x09
#define RT5120_REG_UVOVPROT	0x0A
#define RT5120_REG_SLPCTL	0x0C
#define RT5120_REG_INTSTAT	0x1E
#define RT5120_REG_DISCHG	0x1F

#define RT5120_OUTPG_MASK(rid)	BIT(rid + 1)
#define RT5120_OUTUV_MASK(rid)	BIT(rid + 9)
#define RT5120_OUTOV_MASK(rid)	BIT(rid + 16)
#define RT5120_CH1VID_MASK	GENMASK(6, 0)
#define RT5120_RIDEN_MASK(rid)	BIT(rid + 1)
#define RT5120_RADEN_MASK(rid)	BIT(rid)
#define RT5120_FPWM_MASK(rid)	BIT(rid + 1)
#define RT5120_UVHICCUP_MASK	BIT(1)
#define RT5120_OVHICCUP_MASK	BIT(0)
#define RT5120_HOTDIE_MASK	BIT(1)

#define RT5120_BUCK1_MINUV	600000
#define RT5120_BUCK1_MAXUV	1393750
#define RT5120_BUCK1_STEPUV	6250
#define RT5120_BUCK1_NUM_VOLT	0x80

#define RT5120_AUTO_MODE	0
#define RT5120_FPWM_MODE	1

enum {
	RT5120_REGULATOR_BUCK1 = 0,
	RT5120_REGULATOR_BUCK2,
	RT5120_REGULATOR_BUCK3,
	RT5120_REGULATOR_BUCK4,
	RT5120_REGULATOR_LDO,
	RT5120_REGULATOR_EXTEN,
	RT5120_MAX_REGULATOR
};

struct rt5120_priv {
	struct device *dev;
	struct regmap *regmap;
	struct regulator_desc rdesc[RT5120_MAX_REGULATOR];
};

static int rt5120_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
{
	struct regmap *regmap = rdev_get_regmap(rdev);
	int rid = rdev_get_id(rdev);
	unsigned int mask = RT5120_FPWM_MASK(rid), val;

	switch (mode) {
	case REGULATOR_MODE_NORMAL:
		val = 0;
		break;
	case REGULATOR_MODE_FAST:
		val = RT5120_FPWM_MASK(rid);
		break;
	default:
		return -EINVAL;
	}

	return regmap_update_bits(regmap, RT5120_REG_MODECTL, mask, val);
}

static unsigned int rt5120_buck_get_mode(struct regulator_dev *rdev)
{
	struct regmap *regmap = rdev_get_regmap(rdev);
	int ret, rid = rdev_get_id(rdev);
	unsigned int val;

	ret = regmap_read(regmap, RT5120_REG_MODECTL, &val);
	if (ret)
		return REGULATOR_MODE_INVALID;

	if (val & RT5120_FPWM_MASK(rid))
		return REGULATOR_MODE_FAST;

	return REGULATOR_MODE_NORMAL;
}

static int rt5120_regulator_get_error_flags(struct regulator_dev *rdev,
					    unsigned int *flags)
{
	struct regmap *regmap = rdev_get_regmap(rdev);
	unsigned int stat, hd_stat, cur_flags = 0;
	int rid = rdev_get_id(rdev), ret;

	/*
	 * reg 0x03/0x04/0x05 to indicate PG/UV/OV
	 * use block read to descrease I/O xfer time
	 */
	ret = regmap_raw_read(regmap, RT5120_REG_PGSTAT, &stat, 3);
	if (ret)
		return ret;

	ret = regmap_read(regmap, RT5120_REG_INTSTAT, &hd_stat);
	if (ret)
		return ret;

	if (!(stat & RT5120_OUTPG_MASK(rid))) {
		if (stat & RT5120_OUTUV_MASK(rid))
			cur_flags |= REGULATOR_ERROR_UNDER_VOLTAGE;

		if (stat & RT5120_OUTOV_MASK(rid))
			cur_flags |= REGULATOR_ERROR_REGULATION_OUT;
	}

	if (hd_stat & RT5120_HOTDIE_MASK)
		cur_flags |= REGULATOR_ERROR_OVER_TEMP;

	*flags = cur_flags;
	return 0;
}

static int rt5120_buck1_set_suspend_voltage(struct regulator_dev *rdev, int uV)
{
	struct regmap *regmap = rdev_get_regmap(rdev);
	int sel;

	if (uV < RT5120_BUCK1_MINUV || uV > RT5120_BUCK1_MAXUV)
		return -EINVAL;

	sel = (uV - RT5120_BUCK1_MINUV) / RT5120_BUCK1_STEPUV;
	return regmap_write(regmap, RT5120_REG_CH1SLPVID, sel);
}

static int rt5120_regulator_set_suspend_enable(struct regulator_dev *rdev)
{
	struct regmap *regmap = rdev_get_regmap(rdev);
	int rid = rdev_get_id(rdev);
	unsigned int mask = RT5120_RIDEN_MASK(rid);

	return regmap_update_bits(regmap, RT5120_REG_SLPCTL, mask, mask);
}

static int rt5120_regulator_set_suspend_disable(struct regulator_dev *rdev)
{
	struct regmap *regmap = rdev_get_regmap(rdev);
	int rid = rdev_get_id(rdev);
	unsigned int mask = RT5120_RIDEN_MASK(rid);

	return regmap_update_bits(regmap, RT5120_REG_SLPCTL, mask, 0);
}

static const struct regulator_ops rt5120_buck1_ops = {
	.enable = regulator_enable_regmap,
	.disable = regulator_disable_regmap,
	.is_enabled = regulator_is_enabled_regmap,
	.list_voltage = regulator_list_voltage_linear,
	.set_voltage_sel = regulator_set_voltage_sel_regmap,
	.get_voltage_sel = regulator_get_voltage_sel_regmap,
	.set_active_discharge = regulator_set_active_discharge_regmap,
	.set_mode = rt5120_buck_set_mode,
	.get_mode = rt5120_buck_get_mode,
	.get_error_flags = rt5120_regulator_get_error_flags,
	.set_suspend_voltage = rt5120_buck1_set_suspend_voltage,
	.set_suspend_enable = rt5120_regulator_set_suspend_enable,
	.set_suspend_disable = rt5120_regulator_set_suspend_disable,
};

static const struct regulator_ops rt5120_buck234_ops = {
	.enable = regulator_enable_regmap,
	.disable = regulator_disable_regmap,
	.is_enabled = regulator_is_enabled_regmap,
	.set_active_discharge = regulator_set_active_discharge_regmap,
	.set_mode = rt5120_buck_set_mode,
	.get_mode = rt5120_buck_get_mode,
	.get_error_flags = rt5120_regulator_get_error_flags,
	.set_suspend_enable = rt5120_regulator_set_suspend_enable,
	.set_suspend_disable = rt5120_regulator_set_suspend_disable,
};

static const struct regulator_ops rt5120_ldo_ops = {
	.enable = regulator_enable_regmap,
	.disable = regulator_disable_regmap,
	.is_enabled = regulator_is_enabled_regmap,
	.set_active_discharge = regulator_set_active_discharge_regmap,
	.get_error_flags = rt5120_regulator_get_error_flags,
	.set_suspend_enable = rt5120_regulator_set_suspend_enable,
	.set_suspend_disable = rt5120_regulator_set_suspend_disable,
};

static const struct regulator_ops rt5120_exten_ops = {
	.enable = regulator_enable_regmap,
	.disable = regulator_disable_regmap,
	.is_enabled = regulator_is_enabled_regmap,
	.set_suspend_enable = rt5120_regulator_set_suspend_enable,
	.set_suspend_disable = rt5120_regulator_set_suspend_disable,
};

static unsigned int rt5120_buck_of_map_mode(unsigned int mode)
{
	switch (mode) {
	case RT5120_AUTO_MODE:
		return REGULATOR_MODE_NORMAL;
	case RT5120_FPWM_MODE:
		return REGULATOR_MODE_FAST;
	default:
		return REGULATOR_MODE_INVALID;
	}
}

static void rt5120_fillin_regulator_desc(struct regulator_desc *desc, int rid)
{
	static const char * const name[] = {
		"buck1", "buck2", "buck3", "buck4", "ldo", "exten" };
	static const char * const sname[] = {
		"vin1", "vin2", "vin3", "vin4", "vinldo", NULL };

	/* Common regulator property */
	desc->name = name[rid];
	desc->supply_name = sname[rid];
	desc->owner = THIS_MODULE;
	desc->type = REGULATOR_VOLTAGE;
	desc->id = rid;
	desc->enable_reg = RT5120_REG_ENABLE;
	desc->enable_mask = RT5120_RIDEN_MASK(rid);
	desc->active_discharge_reg = RT5120_REG_DISCHG;
	desc->active_discharge_mask = RT5120_RADEN_MASK(rid);
	desc->active_discharge_on = RT5120_RADEN_MASK(rid);
	/* Config n_voltages to 1 for all*/
	desc->n_voltages = 1;

	/* Only buck support mode change */
	if (rid >= RT5120_REGULATOR_BUCK1 && rid <= RT5120_REGULATOR_BUCK4)
		desc->of_map_mode = rt5120_buck_of_map_mode;

	/* RID specific property init */
	switch (rid) {
	case RT5120_REGULATOR_BUCK1:
		/* Only buck1 support voltage change by I2C */
		desc->n_voltages = RT5120_BUCK1_NUM_VOLT;
		desc->min_uV = RT5120_BUCK1_MINUV;
		desc->uV_step = RT5120_BUCK1_STEPUV;
		desc->vsel_reg = RT5120_REG_CH1VID,
		desc->vsel_mask = RT5120_CH1VID_MASK,
		desc->ops = &rt5120_buck1_ops;
		break;
	case RT5120_REGULATOR_BUCK2 ... RT5120_REGULATOR_BUCK4:
		desc->ops = &rt5120_buck234_ops;
		break;
	case RT5120_REGULATOR_LDO:
		desc->ops = &rt5120_ldo_ops;
		break;
	default:
		desc->ops = &rt5120_exten_ops;
	}
}

static int rt5120_of_parse_cb(struct rt5120_priv *priv, int rid,
			      struct of_regulator_match *match)
{
	struct regulator_desc *desc = priv->rdesc + rid;
	struct regulator_init_data *init_data = match->init_data;

	if (!init_data || rid == RT5120_REGULATOR_BUCK1)
		return 0;

	if (init_data->constraints.min_uV != init_data->constraints.max_uV) {
		dev_err(priv->dev, "Variable voltage for fixed regulator\n");
		return -EINVAL;
	}

	desc->fixed_uV = init_data->constraints.min_uV;
	return 0;
}

static struct of_regulator_match rt5120_regu_match[RT5120_MAX_REGULATOR] = {
	[RT5120_REGULATOR_BUCK1] = { .name = "buck1", },
	[RT5120_REGULATOR_BUCK2] = { .name = "buck2", },
	[RT5120_REGULATOR_BUCK3] = { .name = "buck3", },
	[RT5120_REGULATOR_BUCK4] = { .name = "buck4", },
	[RT5120_REGULATOR_LDO] = { .name = "ldo", },
	[RT5120_REGULATOR_EXTEN] = { .name = "exten", }
};

static int rt5120_parse_regulator_dt_data(struct rt5120_priv *priv)
{
	struct device *dev = priv->dev->parent;
	struct device_node *reg_node;
	int i, ret;

	for (i = 0; i < RT5120_MAX_REGULATOR; i++) {
		rt5120_fillin_regulator_desc(priv->rdesc + i, i);

		rt5120_regu_match[i].desc = priv->rdesc + i;
	}

	reg_node = of_get_child_by_name(dev->of_node, "regulators");
	if (!reg_node) {
		dev_err(priv->dev, "Couldn't find 'regulators' node\n");
		return -ENODEV;
	}

	ret = of_regulator_match(priv->dev, reg_node, rt5120_regu_match,
				 ARRAY_SIZE(rt5120_regu_match));

	of_node_put(reg_node);

	if (ret < 0) {
		dev_err(priv->dev,
			"Error parsing regulator init data (%d)\n", ret);
		return ret;
	}

	for (i = 0; i < RT5120_MAX_REGULATOR; i++) {
		ret = rt5120_of_parse_cb(priv, i, rt5120_regu_match + i);
		if (ret) {
			dev_err(priv->dev, "Failed in [%d] of_passe_cb\n", i);
			return ret;
		}
	}

	return 0;
}

static int rt5120_device_property_init(struct rt5120_priv *priv)
{
	struct device *dev = priv->dev->parent;
	struct device_node *np = dev->of_node;
	bool prot_enable;
	unsigned int prot_enable_val = 0;

	/* Assign UV/OV HW protection behavior */
	prot_enable = of_property_read_bool(np,
					    "richtek,enable-undervolt-hiccup");
	if (prot_enable)
		prot_enable_val |= RT5120_UVHICCUP_MASK;

	prot_enable = of_property_read_bool(np,
					    "richtek,enable-overvolt-hiccup");
	if (prot_enable)
		prot_enable_val |= RT5120_OVHICCUP_MASK;

	return regmap_update_bits(priv->regmap, RT5120_REG_UVOVPROT,
				  RT5120_UVHICCUP_MASK | RT5120_OVHICCUP_MASK,
				  prot_enable_val);
}

static int rt5120_regulator_probe(struct platform_device *pdev)
{
	struct rt5120_priv *priv;
	struct regulator_dev *rdev;
	struct regulator_config config = {};
	int i, ret;

	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->dev = &pdev->dev;

	priv->regmap = dev_get_regmap(pdev->dev.parent, NULL);
	if (!priv->regmap) {
		dev_err(&pdev->dev, "Failed to init regmap\n");
		return -ENODEV;
	}

	ret = rt5120_device_property_init(priv);
	if (ret) {
		dev_err(&pdev->dev, "Failed to do property init\n");
		return ret;
	}

	ret = rt5120_parse_regulator_dt_data(priv);
	if (ret) {
		dev_err(&pdev->dev, "Failed to parse dt data\n");
		return ret;
	}

	config.dev = &pdev->dev;
	config.regmap = priv->regmap;

	for (i = 0; i < RT5120_MAX_REGULATOR; i++) {
		config.of_node = rt5120_regu_match[i].of_node;
		config.init_data = rt5120_regu_match[i].init_data;

		rdev = devm_regulator_register(&pdev->dev, priv->rdesc + i,
					       &config);
		if (IS_ERR(rdev)) {
			dev_err(&pdev->dev,
				"Failed to register regulator [%d]\n", i);
			return PTR_ERR(rdev);
		}
	}

	return 0;
}

static const struct platform_device_id rt5120_regulator_dev_table[] = {
	{ "rt5120-regulator", 0 },
	{}
};
MODULE_DEVICE_TABLE(platform, rt5120_regulator_dev_table);

static struct platform_driver rt5120_regulator_driver = {
	.driver = {
		.name = "rt5120-regulator",
		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
	},
	.id_table = rt5120_regulator_dev_table,
	.probe = rt5120_regulator_probe,
};
module_platform_driver(rt5120_regulator_driver);

MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
MODULE_DESCRIPTION("Richtek RT5120 regulator driver");
MODULE_LICENSE("GPL v2");