summaryrefslogtreecommitdiff
path: root/drivers/regulator/xz3216.c
blob: 3bcaf898c768f4b5a357535b970ee0af145bd9ef (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
/*
 * Regulator driver for xz3216 DCDC chip for rk32xx
 *
 * Copyright (C) 2010, 2011 ROCKCHIP, Inc.

 * Based on xz3216.c that is work by zhangqing<zhangqing@rock-chips.com>
 *
 * 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.
 *
 */

#include <linux/bug.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/regulator/driver.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/mfd/core.h>

#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/regulator/of_regulator.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/regmap.h>

#if 0
#define DBG(x...)	pr_info(x)
#else
#define DBG(x...)
#endif

#define DBG_ERR(x...)	pr_err(x)

#define XZ3216_NUM_REGULATORS 1

#define XZ3216_BUCK1_SET_VOL_BASE 0x00
#define XZ3216_BUCK1_SLP_VOL_BASE 0x01
#define XZ3216_CONTR_REG1 0x02
#define XZ3216_ID1_REG 0x03
#define BUCK_VOL_MASK 0x3f
#define VOL_MIN_IDX 0x00
#define VOL_MAX_IDX 0x3

/* VSEL bit definitions */
#define VSEL_BUCK_EN	BIT(7)
#define VSEL_MODE	BIT(6)
#define VSEL_NSEL_MASK	0x3F

/* Control bit definitions */
#define CTL_OUTPUT_DISCHG	BIT(7)
#define CTL_SLEW_MASK		(0x7 << 4)
#define CTL_SLEW_SHIFT		4
#define CTL_RESET		BIT(2)

struct xz3216 {
	struct device *dev;
	struct i2c_client *i2c;
	int num_regulators;
	struct regulator_dev *rdev;
	struct regulator_init_data *regulator;
	struct regmap *regmap;
	/* Voltage setting register */
	unsigned int vol_reg;
	unsigned int sleep_reg;
	/* Voltage range and step(linear) */
	unsigned int vsel_min;
	unsigned int vsel_step;
	unsigned int sleep_vol_cache;
};

struct xz3216_regulator {
	struct device		*dev;
	struct regulator_desc	*desc;
	struct regulator_dev	*rdev;
};

struct xz3216_board {
	struct regulator_init_data *xz3216_init_data;
	struct device_node *of_node;
};

static unsigned int xz3216_dcdc_get_mode(struct regulator_dev *dev)
{
	struct xz3216 *xz3216 = rdev_get_drvdata(dev);
	unsigned int val;
	int ret = 0;

	ret = regmap_read(xz3216->regmap, xz3216->vol_reg, &val);
	if (ret < 0)
		return ret;
	if (val & VSEL_MODE)
		return REGULATOR_MODE_FAST;
	else
		return REGULATOR_MODE_NORMAL;
}

static int xz3216_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
{
	struct xz3216 *xz3216 = rdev_get_drvdata(dev);

	switch (mode) {
	case REGULATOR_MODE_FAST:
		regmap_update_bits(xz3216->regmap, xz3216->vol_reg,
				VSEL_MODE, VSEL_MODE);
	case REGULATOR_MODE_NORMAL:
		regmap_update_bits(xz3216->regmap, xz3216->vol_reg,
							VSEL_MODE, 0);
	default:
		DBG("error:dcdc_xz3216 only auto and pwm mode\n");
		return -EINVAL;
	}
}

static int xz3216_dcdc_suspend_enable(struct regulator_dev *dev)
{
	struct xz3216 *xz3216 = rdev_get_drvdata(dev);
	return regmap_update_bits(xz3216->regmap, XZ3216_BUCK1_SLP_VOL_BASE,
				  VSEL_BUCK_EN, VSEL_BUCK_EN);
}

static int xz3216_dcdc_suspend_disable(struct regulator_dev *dev)
{
	struct xz3216 *xz3216 = rdev_get_drvdata(dev);
	return regmap_update_bits(xz3216->regmap, XZ3216_BUCK1_SLP_VOL_BASE,
				  VSEL_BUCK_EN, 0);

}

static int xz3216_dcdc_set_sleep_voltage(struct regulator_dev *dev,
					 int uV)
{
	struct xz3216 *xz3216 = rdev_get_drvdata(dev);
	int ret;

	if (xz3216->sleep_vol_cache == uV)
		return 0;
	ret = regulator_map_voltage_linear(dev, uV, uV);
	if (ret < 0)
		return ret;
	ret = regmap_update_bits(xz3216->regmap, XZ3216_BUCK1_SLP_VOL_BASE,
					VSEL_NSEL_MASK, ret);
	if (ret < 0)
		return ret;
	xz3216->sleep_vol_cache = uV;
	return 0;

}

static int xz3216_dcdc_set_suspend_mode(struct regulator_dev *dev,
					 unsigned int mode)
{
	struct xz3216 *xz3216 = rdev_get_drvdata(dev);

	switch (mode) {
	case REGULATOR_MODE_FAST:
		regmap_update_bits(xz3216->regmap, xz3216->vol_reg,
							VSEL_MODE, VSEL_MODE);
	case REGULATOR_MODE_NORMAL:
		regmap_update_bits(xz3216->regmap, xz3216->vol_reg,
						    VSEL_MODE, 0);
	default:
		DBG_ERR("error:dcdc_xz3216 only auto and pwm mode\n");
		return -EINVAL;
	}
}

static const int slew_rates[] = {
	64000,
	32000,
	16000,
	 8000,
	 4000,
	 2000,
	 1000,
	  500,
};

static int xz3216_set_ramp(struct regulator_dev *rdev, int ramp)
{
	struct xz3216 *xz3216 = rdev_get_drvdata(rdev);
	int regval = -1, i;

	for (i = 0; i < ARRAY_SIZE(slew_rates); i++) {
		if (ramp <= slew_rates[i])
			regval = i;
		else
			break;
	}
	if (regval < 0) {
		dev_err(xz3216->dev, "unsupported ramp value %d\n", ramp);
		return -EINVAL;
	}

	return regmap_update_bits(xz3216->regmap, XZ3216_CONTR_REG1,
				  CTL_SLEW_MASK, regval << CTL_SLEW_SHIFT);
}

static struct regulator_ops xz3216_dcdc_ops = {
	.set_voltage_sel = regulator_set_voltage_sel_regmap,
	.get_voltage_sel = regulator_get_voltage_sel_regmap,
	.list_voltage = regulator_list_voltage_linear,
	.map_voltage = regulator_map_voltage_linear,
	.is_enabled = regulator_is_enabled_regmap,
	.enable = regulator_enable_regmap,
	.disable = regulator_disable_regmap,
	.get_mode = xz3216_dcdc_get_mode,
	.set_mode = xz3216_dcdc_set_mode,
	.set_suspend_voltage = xz3216_dcdc_set_sleep_voltage,
	.set_suspend_enable = xz3216_dcdc_suspend_enable,
	.set_suspend_disable = xz3216_dcdc_suspend_disable,
	.set_suspend_mode = xz3216_dcdc_set_suspend_mode,
	.set_ramp_delay = xz3216_set_ramp,
	.set_voltage_time_sel = regulator_set_voltage_time_sel,
};

static struct regulator_desc regulators[] = {
	{
		.name = "XZ_DCDC1",
		.supply_name = "vin",
		.id = 0,
		.ops = &xz3216_dcdc_ops,
		.n_voltages = 64,
		.type = REGULATOR_VOLTAGE,
		.enable_time = 400,
		.enable_reg = XZ3216_BUCK1_SET_VOL_BASE,
		.enable_mask = VSEL_BUCK_EN,
		.min_uV = 600000,
		.uV_step = 12500,
		.vsel_reg = XZ3216_BUCK1_SET_VOL_BASE,
		.vsel_mask = VSEL_NSEL_MASK,
		.owner = THIS_MODULE,
	},
};

static const struct regmap_config xz3216_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8,
};

#ifdef CONFIG_OF
static struct of_device_id xz3216_of_match[] = {
	{ .compatible = "xz3216"},
	{ },
};
MODULE_DEVICE_TABLE(of, xz3216_of_match);
#endif

#ifdef CONFIG_OF
static struct of_regulator_match xz3216_reg_matches[] = {
	{ .name = "xz_dcdc1", .driver_data = (void *)0},
};

static struct xz3216_board *xz3216_parse_dt(struct xz3216 *xz3216)
{
	struct xz3216_board *pdata;
	struct device_node *regs;
	struct device_node *xz3216_np;
	int count;

	xz3216_np = of_node_get(xz3216->dev->of_node);
	if (!xz3216_np) {
		DBG_ERR("could not find pmic sub-node\n");
		return NULL;
	}
	regs = of_find_node_by_name(xz3216_np, "regulators");
	if (!regs)
		return NULL;
	count = of_regulator_match(xz3216->dev, regs, xz3216_reg_matches,
				   XZ3216_NUM_REGULATORS);
	of_node_put(regs);
	pdata = devm_kzalloc(xz3216->dev, sizeof(*pdata), GFP_KERNEL);
	if (!pdata)
		return NULL;
	pdata->xz3216_init_data = xz3216_reg_matches[0].init_data;
	pdata->of_node = xz3216_reg_matches[0].of_node;
	return pdata;
}

#else
static struct xz3216_board *xz3216_parse_dt(struct i2c_client *i2c)
{
	return NULL;
}
#endif

static int xz3216_i2c_probe(struct i2c_client *i2c,
			    const struct i2c_device_id *id)
{
	struct xz3216 *xz3216;
	struct xz3216_board *pdev;
	const struct of_device_id *match;
	struct regulator_config config = { };
	int ret;
	DBG("%s, line=%d\n", __func__, __LINE__);
	xz3216 = devm_kzalloc(&i2c->dev, sizeof(struct xz3216),
						GFP_KERNEL);
	if (!xz3216) {
		ret = -ENOMEM;
		goto err;
	}

	if (i2c->dev.of_node) {
		match = of_match_device(xz3216_of_match, &i2c->dev);
		if (!match) {
			DBG_ERR("Failed to find matching dt id\n");
			return -EINVAL;
		}
	}

	xz3216->regmap = devm_regmap_init_i2c(i2c, &xz3216_regmap_config);
	if (IS_ERR(xz3216->regmap)) {
		dev_err(&i2c->dev, "Failed to allocate regmap!\n");
		return PTR_ERR(xz3216->regmap);
	}

	xz3216->i2c = i2c;
	xz3216->dev = &i2c->dev;
	i2c_set_clientdata(i2c, xz3216);
	pdev = dev_get_platdata(&i2c->dev);
	if (!pdev)
		pdev = xz3216_parse_dt(xz3216);
	if (pdev) {
		xz3216->num_regulators = XZ3216_NUM_REGULATORS;
		xz3216->rdev = kcalloc(XZ3216_NUM_REGULATORS,
					sizeof(struct regulator_dev),
					GFP_KERNEL);
		if (!xz3216->rdev)
			return -ENOMEM;
		/* Instantiate the regulators */
		xz3216->regulator = pdev->xz3216_init_data;
		if (xz3216->dev->of_node)
			config.of_node = pdev->of_node;
		config.dev = xz3216->dev;
		config.driver_data = xz3216;
		config.init_data = xz3216->regulator;
		xz3216->rdev = devm_regulator_register(xz3216->dev,
						&regulators[0], &config);
		ret = PTR_ERR_OR_ZERO(xz3216->rdev);
		if (ret < 0)
			dev_err(&i2c->dev, "Failed to register regulator!\n");
		return ret;
	}
	return 0;
err:
	return ret;
}

static int xz3216_i2c_remove(struct i2c_client *i2c)
{
	struct xz3216 *xz3216 = i2c_get_clientdata(i2c);

	if (xz3216->rdev)
		regulator_unregister(xz3216->rdev);
	i2c_set_clientdata(i2c, NULL);
	return 0;
}

static const struct i2c_device_id xz3216_i2c_id[] = {
	{ "xz3216", 0 },
	{ }
};

MODULE_DEVICE_TABLE(i2c, xz3216_i2c_id);

static struct i2c_driver xz3216_i2c_driver = {
	.driver = {
		.name = "xz3216",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(xz3216_of_match),
	},
	.probe = xz3216_i2c_probe,
	.remove = xz3216_i2c_remove,
	.id_table = xz3216_i2c_id,
};

static int __init xz3216_module_init(void)
{
	int ret;

	ret = i2c_add_driver(&xz3216_i2c_driver);
	if (ret != 0)
		pr_err("Failed to register I2C driver: %d\n", ret);
	return ret;
}
subsys_initcall_sync(xz3216_module_init);

static void __exit xz3216_module_exit(void)
{
	i2c_del_driver(&xz3216_i2c_driver);
}
module_exit(xz3216_module_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
MODULE_DESCRIPTION("xz3216 PMIC driver");