aboutsummaryrefslogtreecommitdiff
path: root/drivers/regulator/hi6421-regulator.c
blob: 222ecd1d2a402ac830685413511b805c77145d7b (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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
 * Device driver for regulators in Hi6421 IC
 *
 * Copyright (c) 2013 Linaro Ltd.
 * Copyright (c) 2011 Hisilicon.
 *
 * Guodong Xu <guodong.xu@linaro.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <linux/slab.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_address.h>
#include <linux/regmap.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/regulator/of_regulator.h>
#include <linux/mfd/hi6421-pmic.h>
#include <linux/delay.h>
#include <linux/time.h>

struct hi6421_regulator_register_info {
	u32 ctrl_reg;
	u32 enable_mask;
	u32 eco_mode_mask;
	u32 vset_reg;
	u32 vset_mask;
};

struct hi6421_regulator {
	const char *name;
	struct hi6421_regulator_register_info register_info;
	struct timeval last_off_time;
	u32 off_on_delay;
	u32 eco_uA;
	struct regulator_desc rdesc;
	int (*dt_parse)(struct hi6421_regulator *, struct platform_device *);
};

static inline struct hi6421_pmic *rdev_to_pmic(struct regulator_dev *dev)
{
	/* regulator_dev parent to->
	 * hi6421 regulator platform device_dev parent to->
	 * hi6421 pmic platform device_dev
	 */
	return dev_get_drvdata(rdev_get_dev(dev)->parent->parent);
}

/* helper function to ensure when it returns it is at least 'delay_us'
 * microseconds after 'since'.
 */
static void ensured_time_after(struct timeval since, u32 delay_us)
{
	struct timeval now;
	u64 elapsed_ns64, delay_ns64;
	u32 actual_us32;

	delay_ns64 = delay_us * NSEC_PER_USEC;
	do_gettimeofday(&now);
	elapsed_ns64 = timeval_to_ns(&now) - timeval_to_ns(&since);
	if (delay_ns64 > elapsed_ns64) {
		actual_us32 = ((u32)(delay_ns64 - elapsed_ns64) /
							NSEC_PER_USEC);
		if (actual_us32 >= 1000) {
			mdelay(actual_us32 / 1000);
			udelay(actual_us32 % 1000);
		} else if (actual_us32 > 0) {
			udelay(actual_us32);
		}
	}
	return;
}

static int hi6421_regulator_is_enabled(struct regulator_dev *dev)
{
	u32 reg_val;
	struct hi6421_regulator *sreg = rdev_get_drvdata(dev);
	struct hi6421_pmic *pmic = rdev_to_pmic(dev);

	reg_val = hi6421_pmic_read(pmic, sreg->register_info.ctrl_reg);

	return ((reg_val & sreg->register_info.enable_mask) != 0);
}

static int hi6421_regulator_enable(struct regulator_dev *dev)
{
	struct hi6421_regulator *sreg = rdev_get_drvdata(dev);
	struct hi6421_pmic *pmic = rdev_to_pmic(dev);

	/* keep a distance of off_on_delay from last time disabled */
	ensured_time_after(sreg->last_off_time, sreg->off_on_delay);

	/* cannot enable more than one regulator at one time */
	mutex_lock(&pmic->enable_mutex);
	ensured_time_after(pmic->last_enabled, HI6421_REGS_ENA_PROTECT_TIME);

	/* set enable register */
	hi6421_pmic_rmw(pmic, sreg->register_info.ctrl_reg,
				sreg->register_info.enable_mask,
				sreg->register_info.enable_mask);

	do_gettimeofday(&pmic->last_enabled);
	mutex_unlock(&pmic->enable_mutex);

	return 0;
}

static int hi6421_regulator_disable(struct regulator_dev *dev)
{
	struct hi6421_regulator *sreg = rdev_get_drvdata(dev);
	struct hi6421_pmic *pmic = rdev_to_pmic(dev);

	/* set enable register to 0 */
	hi6421_pmic_rmw(pmic, sreg->register_info.ctrl_reg,
				sreg->register_info.enable_mask, 0);

	do_gettimeofday(&sreg->last_off_time);

	return 0;
}

static int hi6421_regulator_get_voltage(struct regulator_dev *dev)
{
	struct hi6421_regulator *sreg = rdev_get_drvdata(dev);
	struct hi6421_pmic *pmic = rdev_to_pmic(dev);
	u32 reg_val, selector;

	/* get voltage selector */
	reg_val = hi6421_pmic_read(pmic, sreg->register_info.vset_reg);
	selector = (reg_val & sreg->register_info.vset_mask) >>
				(ffs(sreg->register_info.vset_mask) - 1);

	return sreg->rdesc.ops->list_voltage(dev, selector);
}

static int hi6421_regulator_ldo_set_voltage(struct regulator_dev *dev,
				int min_uV, int max_uV, unsigned *selector)
{
	struct hi6421_regulator *sreg = rdev_get_drvdata(dev);
	struct hi6421_pmic *pmic = rdev_to_pmic(dev);
	u32 vsel;
	int ret = 0;

	for (vsel = 0; vsel < sreg->rdesc.n_voltages; vsel++) {
		int uV = sreg->rdesc.volt_table[vsel];
		/* Break at the first in-range value */
		if (min_uV <= uV && uV <= max_uV)
			break;
	}

	/* unlikely to happen. sanity test done by regulator core */
	if (unlikely(vsel == sreg->rdesc.n_voltages))
		return -EINVAL;

	*selector = vsel;
	/* set voltage selector */
	hi6421_pmic_rmw(pmic, sreg->register_info.vset_reg,
		sreg->register_info.vset_mask,
		vsel << (ffs(sreg->register_info.vset_mask) - 1));

	return ret;
}

static int hi6421_regulator_buck012_set_voltage(struct regulator_dev *dev,
				int min_uV, int max_uV, unsigned *selector)
{
	struct hi6421_regulator *sreg = rdev_get_drvdata(dev);
	struct hi6421_pmic *pmic = rdev_to_pmic(dev);
	u32 vsel;
	int ret = 0;

	vsel = DIV_ROUND_UP((max_uV - sreg->rdesc.min_uV),
				sreg->rdesc.uV_step);

	*selector = vsel;
	/* set voltage selector */
	hi6421_pmic_rmw(pmic, sreg->register_info.vset_reg,
		sreg->register_info.vset_mask,
		vsel << (ffs(sreg->register_info.vset_mask) - 1));

	return ret;
}

static unsigned int hi6421_regulator_get_mode(struct regulator_dev *dev)
{
	struct hi6421_regulator *sreg = rdev_get_drvdata(dev);
	struct hi6421_pmic *pmic = rdev_to_pmic(dev);
	u32 reg_val;

	reg_val = hi6421_pmic_read(pmic, sreg->register_info.ctrl_reg);
	if (reg_val & sreg->register_info.eco_mode_mask)
		return REGULATOR_MODE_IDLE;
	else
		return REGULATOR_MODE_NORMAL;
}

static int hi6421_regulator_set_mode(struct regulator_dev *dev,
						unsigned int mode)
{
	struct hi6421_regulator *sreg = rdev_get_drvdata(dev);
	struct hi6421_pmic *pmic = rdev_to_pmic(dev);
	u32 eco_mode;

	switch (mode) {
	case REGULATOR_MODE_NORMAL:
		eco_mode = HI6421_ECO_MODE_DISABLE;
		break;
	case REGULATOR_MODE_IDLE:
		eco_mode = HI6421_ECO_MODE_ENABLE;
		break;
	default:
		return -EINVAL;
	}

	/* set mode */
	hi6421_pmic_rmw(pmic, sreg->register_info.ctrl_reg,
		sreg->register_info.eco_mode_mask,
		eco_mode << (ffs(sreg->register_info.eco_mode_mask) - 1));

	return 0;
}


unsigned int hi6421_regulator_get_optimum_mode(struct regulator_dev *dev,
			int input_uV, int output_uV, int load_uA)
{
	struct hi6421_regulator *sreg = rdev_get_drvdata(dev);

	if ((load_uA == 0) || (load_uA > sreg->eco_uA))
		return REGULATOR_MODE_NORMAL;
	else
		return REGULATOR_MODE_IDLE;
}

static int hi6421_dt_parse_common(struct hi6421_regulator *sreg,
					struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
	struct regulator_desc *rdesc = &sreg->rdesc;
	unsigned int register_info[3];
	int ret = 0;

	/* parse .register_info.ctrl_reg */
	ret = of_property_read_u32_array(np, "hisilicon,hi6421-ctrl",
						register_info, 3);
	if (ret) {
		dev_err(dev, "no hisilicon,hi6421-ctrl property set\n");
		goto dt_parse_common_end;
	}
	sreg->register_info.ctrl_reg = register_info[0];
	sreg->register_info.enable_mask = register_info[1];
	sreg->register_info.eco_mode_mask = register_info[2];

	/* parse .register_info.vset_reg */
	ret = of_property_read_u32_array(np, "hisilicon,hi6421-vset",
						register_info, 2);
	if (ret) {
		dev_err(dev, "no hisilicon,hi6421-vset property set\n");
		goto dt_parse_common_end;
	}
	sreg->register_info.vset_reg = register_info[0];
	sreg->register_info.vset_mask = register_info[1];

	/* parse .off-on-delay */
	ret = of_property_read_u32(np, "hisilicon,hi6421-off-on-delay-us",
						&sreg->off_on_delay);
	if (ret) {
		dev_err(dev, "no hisilicon,hi6421-off-on-delay-us property set\n");
		goto dt_parse_common_end;
	}

	/* parse .enable_time */
	ret = of_property_read_u32(np, "hisilicon,hi6421-enable-time-us",
				   &rdesc->enable_time);
	if (ret) {
		dev_err(dev, "no hisilicon,hi6421-enable-time-us property set\n");
		goto dt_parse_common_end;
	}

	/* parse .eco_uA */
	ret = of_property_read_u32(np, "hisilicon,hi6421-eco-microamp",
				   &sreg->eco_uA);
	if (ret) {
		sreg->eco_uA = 0;
		ret = 0;
	}

dt_parse_common_end:
	return ret;
}

static int hi6421_dt_parse_ldo(struct hi6421_regulator *sreg,
				struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
	struct regulator_desc *rdesc = &sreg->rdesc;
	unsigned int *v_table;
	int ret = 0;

	/* parse .n_voltages, and .volt_table */
	ret = of_property_read_u32(np, "hisilicon,hi6421-n-voltages",
				   &rdesc->n_voltages);
	if (ret) {
		dev_err(dev, "no hisilicon,hi6421-n-voltages property set\n");
		goto dt_parse_ldo_end;
	}

	/* alloc space for .volt_table */
	v_table = devm_kzalloc(dev, sizeof(unsigned int) * rdesc->n_voltages,
								GFP_KERNEL);
	if (unlikely(!v_table)) {
		ret = -ENOMEM;
		dev_err(dev, "no memory for .volt_table\n");
		goto dt_parse_ldo_end;
	}

	ret = of_property_read_u32_array(np, "hisilicon,hi6421-vset-table",
						v_table, rdesc->n_voltages);
	if (ret) {
		dev_err(dev, "no hisilicon,hi6421-vset-table property set\n");
		goto dt_parse_ldo_end;
	}
	rdesc->volt_table = v_table;

	/* parse hi6421 regulator's dt common part */
	ret = hi6421_dt_parse_common(sreg, pdev);
	if (ret) {
		dev_err(dev, "failure in hi6421_dt_parse_common\n");
		goto dt_parse_ldo_end;
	}

dt_parse_ldo_end:
	return ret;
}

static int hi6421_dt_parse_buck012(struct hi6421_regulator *sreg,
					struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
	struct regulator_desc *rdesc = &sreg->rdesc;
	int ret = 0;

	/* parse .n_voltages, and .uV_step */
	ret = of_property_read_u32(np, "hisilicon,hi6421-n-voltages",
				   &rdesc->n_voltages);
	if (ret) {
		dev_err(dev, "no hisilicon,hi6421-n-voltages property set\n");
		goto dt_parse_buck012_end;
	}
	ret = of_property_read_u32(np, "hisilicon,hi6421-uv-step",
				   &rdesc->uV_step);
	if (ret) {
		dev_err(dev, "no hisilicon,hi6421-uv-step property set\n");
		goto dt_parse_buck012_end;
	}

	/* parse hi6421 regulator's dt common part */
	ret = hi6421_dt_parse_common(sreg, pdev);
	if (ret) {
		dev_err(dev, "failure in hi6421_dt_parse_common\n");
		goto dt_parse_buck012_end;
	}

dt_parse_buck012_end:
	return ret;
}

static struct regulator_ops hi6421_ldo_rops = {
	.is_enabled = hi6421_regulator_is_enabled,
	.enable = hi6421_regulator_enable,
	.disable = hi6421_regulator_disable,
	.list_voltage = regulator_list_voltage_table,
	.get_voltage = hi6421_regulator_get_voltage,
	.set_voltage = hi6421_regulator_ldo_set_voltage,
	.get_mode = hi6421_regulator_get_mode,
	.set_mode = hi6421_regulator_set_mode,
	.get_optimum_mode = hi6421_regulator_get_optimum_mode,
};

static struct regulator_ops hi6421_buck012_rops = {
	.is_enabled = hi6421_regulator_is_enabled,
	.enable = hi6421_regulator_enable,
	.disable = hi6421_regulator_disable,
	.list_voltage = regulator_list_voltage_linear,
	.get_voltage = hi6421_regulator_get_voltage,
	.set_voltage = hi6421_regulator_buck012_set_voltage,
	.get_mode = hi6421_regulator_get_mode,
	.set_mode = hi6421_regulator_set_mode,
	.get_optimum_mode = hi6421_regulator_get_optimum_mode,
};

static struct regulator_ops hi6421_buck345_rops = {
	.is_enabled = hi6421_regulator_is_enabled,
	.enable = hi6421_regulator_enable,
	.disable = hi6421_regulator_disable,
	.list_voltage = regulator_list_voltage_table,
	.get_voltage = hi6421_regulator_get_voltage,
	.set_voltage = hi6421_regulator_ldo_set_voltage,
	.get_mode = hi6421_regulator_get_mode,
	.set_mode = hi6421_regulator_set_mode,
	.get_optimum_mode = hi6421_regulator_get_optimum_mode,
};

static const struct hi6421_regulator hi6421_regulator_ldo = {
	.rdesc = {
	.ops = &hi6421_ldo_rops,
		.type = REGULATOR_VOLTAGE,
		.owner = THIS_MODULE,
		},
	.dt_parse = hi6421_dt_parse_ldo,
};

static const struct hi6421_regulator hi6421_regulator_buck012 = {
	.rdesc = {
		.ops = &hi6421_buck012_rops,
		.type = REGULATOR_VOLTAGE,
		.owner = THIS_MODULE,
		},
	.dt_parse = hi6421_dt_parse_buck012,
};

static const struct hi6421_regulator hi6421_regulator_buck345 = {
	.rdesc = {
		.ops = &hi6421_buck345_rops,
		.type = REGULATOR_VOLTAGE,
		.owner = THIS_MODULE,
		},
	.dt_parse = hi6421_dt_parse_ldo,
};

static struct of_device_id of_hi6421_regulator_match_tbl[] = {
	{
		.compatible = "hisilicon,hi6421-ldo",
		.data = &hi6421_regulator_ldo,
	},
	{
		.compatible = "hisilicon,hi6421-buck012",
		.data = &hi6421_regulator_buck012,
	},
	{
		.compatible = "hisilicon,hi6421-buck345",
		.data = &hi6421_regulator_buck345,
	},
	{ /* end */ }
};

static int hi6421_regulator_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
	struct regulator_desc *rdesc;
	struct regulator_dev *rdev;
	struct hi6421_regulator *sreg = NULL;
	struct regulator_init_data *initdata;
	struct regulation_constraints *c;
	struct regulator_config config = { };
	const struct of_device_id *match;
	const struct hi6421_regulator *template = NULL;
	int ret = 0;

	/* to check which type of regulator this is */
	match = of_match_device(of_hi6421_regulator_match_tbl, &pdev->dev);
	if (match)
		template = match->data;
	else
		return -EINVAL;

	initdata = of_get_regulator_init_data(dev, np);

	/* hi6421 regulator supports two modes */
	c = &initdata->constraints;
	c->valid_modes_mask = REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE;
	c->valid_ops_mask |= (REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_DRMS);
	c->input_uV = c->min_uV;

	sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
	if (sreg == NULL)
		return -ENOMEM;
	memcpy(sreg, template, sizeof(*sreg));

	sreg->name = initdata->constraints.name;
	rdesc = &sreg->rdesc;
	rdesc->name = sreg->name;
	rdesc->min_uV = initdata->constraints.min_uV;
	if (of_get_property(np, "ldo-supply", NULL)) {
		rdesc->supply_name = "ldo";
	}

	/* to parse device tree data for regulator specific */
	ret = sreg->dt_parse(sreg, pdev);
	if (ret) {
		dev_err(dev, "device tree parameter parse error!\n");
		goto hi6421_probe_end;
	}

	config.dev = &pdev->dev;
	config.init_data = initdata;
	config.driver_data = sreg;
	config.of_node = pdev->dev.of_node;

	/* register regulator */
	rdev = regulator_register(rdesc, &config);
	if (IS_ERR(rdev)) {
		dev_err(dev, "failed to register %s\n",
			rdesc->name);
		ret = PTR_ERR(rdev);
		goto hi6421_probe_end;
	}

	platform_set_drvdata(pdev, rdev);

hi6421_probe_end:
	return ret;
}

static int hi6421_regulator_remove(struct platform_device *pdev)
{
	struct regulator_dev *rdev = platform_get_drvdata(pdev);

	regulator_unregister(rdev);

	return 0;
}

static struct platform_driver hi6421_regulator_driver = {
	.driver = {
		.name	= "hi6421_regulator",
		.owner  = THIS_MODULE,
		.of_match_table = of_hi6421_regulator_match_tbl,
	},
	.probe	= hi6421_regulator_probe,
	.remove	= hi6421_regulator_remove,
};
module_platform_driver(hi6421_regulator_driver);

MODULE_AUTHOR("Guodong Xu <guodong.xu@linaro.org>");
MODULE_DESCRIPTION("Hi6421 regulator driver");
MODULE_LICENSE("GPL v2");