aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/regulator/tps6130x-regulator.c
blob: 35fe37b76ddc645f141ca90aa2096cd62cf0b9fb (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
/*
 * tps6130x-regulator.c
 *
 * Regulator driver for TPS6130x
 *
 * Copyright (C) 2011 Texas Instrument Incorporated - http://www.ti.com/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation version 2.
 *
 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
 * whether express or implied; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/regulator/tps6130x.h>

/* Regulator id */
#define TPS6130X_VOUT			0

/* Register definitions */
#define TPS6130X_REGISTER1		0x01
#define TPS6130X_REGISTER2		0x02
#define TPS6130X_REGISTER3		0x03
#define TPS6130X_REGISTER4		0x04
#define TPS6130X_REGISTER5		0x05
#define TPS6130X_REGISTER6		0x06
#define TPS6130X_REGISTER7		0x07

/* REGISTER1 bitfields */
#define TPS6130X_ENVM			0x80
#define TPS6130X_MODE_CTRL_MASK		0x60
#define TPS6130X_SHUTDOWN_MODE		(0 << 5)
#define TPS6130X_DC_LIGHT_MODE		(1 << 5)
#define TPS6130X_DC_LIGHT_FLASH_MODE	(2 << 5)
#define TPS6130X_VOUT_MODE		(3 << 5)

/* REGISTER6 bitfields */
#define TPS6130X_OV_MASK		0x0F

/* REGISTER7 bitfields */
#define TPS6130X_REVID_MASK		0x07

/* Supported voltage values for voltage regulation mode */
#define TPS6130X_VOUT_MIN_UV		3825000
#define TPS6130X_VOUT_MAX_UV		5700000
#define TPS6130X_VOUT_STEP		125000
#define TPS6130X_VOUT_MAX_SEL		0x0F

/* Regulator specific details */
struct tps_info {
	const char *name;
	int min_uV;
	int max_uV;
	int step;
	int vsel_max;
	int vsel;
};

/* PMIC details */
struct tps_pmic {
	struct regulator_desc *desc;
	struct i2c_client *client;
	struct regulator_dev *rdev;
	struct tps6130x_platform_data *pdata;
	struct tps_info *info;
	struct mutex io_lock;
	int enabled;
};

static int tps6130x_reg_read(struct tps_pmic *tps, u8 reg)
{
	int data;

	data = i2c_smbus_read_byte_data(tps->client, reg);
	if (data < 0)
		dev_err(&tps->client->dev, "Read from reg 0x%x failed\n", reg);

	return data;
}

static int tps6130x_reg_write(struct tps_pmic *tps, u8 reg, u8 val)
{
	int ret;

	ret = i2c_smbus_write_byte_data(tps->client, reg, val);
	if (ret < 0)
		dev_err(&tps->client->dev, "Write to reg 0x%x failed\n", reg);

	return ret;
}

static int tps6130x_update_bits(struct tps_pmic *tps, u8 reg,
				u8 mask, u8 value)
{
	int old_val, new_val, ret = 0;

	mutex_lock(&tps->io_lock);

	old_val = tps6130x_reg_read(tps, reg);
	if (old_val < 0) {
		ret = old_val;
		goto err;
	}

	new_val = (old_val & ~mask) | value;
	if (old_val != new_val)
		ret = tps6130x_reg_write(tps, reg, new_val);

err:
	mutex_unlock(&tps->io_lock);
	return ret;
}

static int tps6130x_chip_enable(struct tps_pmic *tps, int on)
{
	struct i2c_client *client = tps->client;
	int ret = 0;

	/* tps61301/tps61305 supports external NRESET */
	if (tps->pdata && tps->pdata->chip_enable) {
		ret = tps->pdata->chip_enable(on);
		if (ret) {
			dev_err(&client->dev, "failed to enable %d\n", ret);
			return ret;
		}
	}

	tps->enabled = on;

	return ret;
}

/* Operations permitted on VOUT */

static int tps6130x_vout_is_enabled(struct regulator_dev *dev)
{
	struct tps_pmic *tps = rdev_get_drvdata(dev);
	int data, vout = 0;

	if (!tps->enabled)
		return 0;

	data = tps6130x_reg_read(tps, TPS6130X_REGISTER1);
	if (data < 0)
		return data;

	/* tps61300/tps61301 have ENVM to force voltage regulation mode */
	if (data & TPS6130X_ENVM)
		vout = 1;

	/* device operating in voltage regulation mode */
	if ((data & TPS6130X_MODE_CTRL_MASK) == TPS6130X_VOUT_MODE)
		vout = 1;

	return vout;
}

static int tps6130x_vout_enable(struct regulator_dev *dev)
{
	struct tps_pmic *tps = rdev_get_drvdata(dev);
	struct tps_info *info = tps->info;
	int ret;

	ret = tps6130x_chip_enable(tps, 1);
	if (ret)
		return ret;

	/* set constant voltage source mode */
	ret = tps6130x_update_bits(tps, TPS6130X_REGISTER1,
				   TPS6130X_MODE_CTRL_MASK,
				   TPS6130X_VOUT_MODE);
	if (ret)
		goto err1;

	/* output voltage must be set after voltage mode has been enabled  */
	ret = tps6130x_update_bits(tps, TPS6130X_REGISTER6,
				   TPS6130X_OV_MASK, info->vsel);
	if (ret)
		goto err2;

	return 0;

err2:
	tps6130x_update_bits(tps, TPS6130X_REGISTER1,
			     TPS6130X_MODE_CTRL_MASK,
			     TPS6130X_SHUTDOWN_MODE);
err1:
	tps6130x_chip_enable(tps, 0);
	return ret;
}

static int tps6130x_vout_disable(struct regulator_dev *dev)
{
	struct tps_pmic *tps = rdev_get_drvdata(dev);
	int ret;

	/* may not disable voltage output if ENVM is forced */
	ret = tps6130x_update_bits(tps, TPS6130X_REGISTER1,
				   TPS6130X_MODE_CTRL_MASK,
				   TPS6130X_SHUTDOWN_MODE);
	if (ret)
		return ret;

	ret = tps6130x_chip_enable(tps, 0);

	return ret;
}

static int tps6130x_vout_list_voltage(struct regulator_dev *dev,
				      unsigned selector)
{
	struct tps_pmic *tps = rdev_get_drvdata(dev);
	struct tps_info *info = tps->info;

	if (selector > info->vsel_max)
		return -EINVAL;

	return info->min_uV + selector * info->step;
}

static int tps6130x_vout_get_voltage(struct regulator_dev *dev)
{
	struct tps_pmic *tps = rdev_get_drvdata(dev);
	struct tps_info *info = tps->info;

	/* output voltage cannot be read from REGISTER6 */
	return info->min_uV + info->vsel * info->step;
}

static int tps6130x_vout_set_voltage(struct regulator_dev *dev,
				     int min_uV, int max_uV, unsigned *selector)
{
	struct tps_pmic *tps = rdev_get_drvdata(dev);
	struct tps_info *info = tps->info;
	int vsel, ret;

	if ((min_uV < info->min_uV) || (min_uV > info->max_uV))
		return -EINVAL;

	if ((max_uV < info->min_uV) || (max_uV > info->max_uV))
		return -EINVAL;

	vsel = (min_uV - info->min_uV + (info->step - 1)) / info->step;

	ret = tps6130x_vout_list_voltage(dev, vsel);
	if ((ret < 0) || (ret > max_uV))
		return -EINVAL;

	/*
	 * new target voltage must be set after
	 * voltage mode operation has been enabled
	 */
	info->vsel = vsel;
	*selector = vsel;

	BUG_ON(vsel < 0);
	BUG_ON(vsel > TPS6130X_VOUT_MAX_SEL);

	return 0;
}

static struct regulator_ops tps6130x_ops = {
	.is_enabled	= tps6130x_vout_is_enabled,
	.enable		= tps6130x_vout_enable,
	.disable	= tps6130x_vout_disable,
	.get_voltage	= tps6130x_vout_get_voltage,
	.set_voltage	= tps6130x_vout_set_voltage,
	.list_voltage	= tps6130x_vout_list_voltage,
};

static struct regulator_desc tps6130x_desc = {
	.name		= "VOUT",
	.id		= TPS6130X_VOUT,
	.ops		= &tps6130x_ops,
	.n_voltages	= TPS6130X_VOUT_MAX_SEL + 1,
	.type		= REGULATOR_VOLTAGE,
	.owner		= THIS_MODULE,
};

static int tps6130x_probe(struct i2c_client *client,
			  const struct i2c_device_id *id)
{
	struct tps_info *info = (void *)id->driver_data;
	struct regulator_init_data *init_data;
	struct regulator_dev *rdev;
	struct tps_pmic *tps;
	int revid, ret;

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
		return -EIO;

	init_data = client->dev.platform_data;
	if (!init_data)
		return -EIO;

	tps = kzalloc(sizeof(*tps), GFP_KERNEL);
	if (!tps)
		return -ENOMEM;

	mutex_init(&tps->io_lock);
	tps->client = client;
	tps->info = info;
	tps->desc = &tps6130x_desc;

	if (init_data->driver_data)
		tps->pdata = init_data->driver_data;

	/* register the regulator */
	rdev = regulator_register(tps->desc, &client->dev, init_data, tps);
	if (IS_ERR(rdev)) {
		dev_err(&client->dev, "failed to register %s\n", id->name);
		ret = PTR_ERR(rdev);
		goto reg_err;
	}

	tps->rdev = rdev;
	i2c_set_clientdata(client, tps);

	ret = tps6130x_chip_enable(tps, 1);
	if (ret)
		goto enable_err;

	revid = tps6130x_reg_read(tps, TPS6130X_REGISTER7);
	if (revid < 0) {
		dev_err(&client->dev, "failed to access device\n");
		ret = revid;
		goto rev_err;
	}

	dev_info(&client->dev, "Revision %d\n", revid & TPS6130X_REVID_MASK);

	/* default output voltage: 4.950V */
	tps->info->vsel = 9;

	ret = tps6130x_chip_enable(tps, 0);
	if (ret)
		goto rev_err;

	return 0;

rev_err:
	tps6130x_chip_enable(tps, 0);
enable_err:
	i2c_set_clientdata(client, NULL);
	regulator_unregister(tps->rdev);
reg_err:
	mutex_destroy(&tps->io_lock);
	tps->client = NULL;
	kfree(tps);
	return ret;
}

static int __devexit tps6130x_remove(struct i2c_client *client)
{
	struct tps_pmic *tps = i2c_get_clientdata(client);

	regulator_unregister(tps->rdev);
	mutex_destroy(&tps->io_lock);
	tps->client = NULL;
	i2c_set_clientdata(client, NULL);
	kfree(tps);

	return 0;
}

static struct tps_info tps6130x_regs = {
	.name	= "VOUT",
	.min_uV	= TPS6130X_VOUT_MIN_UV,
	.max_uV	= TPS6130X_VOUT_MAX_UV,
	.step	= TPS6130X_VOUT_STEP,
	.vsel_max = TPS6130X_VOUT_MAX_SEL,
};

static const struct i2c_device_id tps6130x_id[] = {
	{
		.name = "tps6130x",
		.driver_data = (unsigned long)&tps6130x_regs,
	},
	{ },
};
MODULE_DEVICE_TABLE(i2c, tps6130x_id);

static struct i2c_driver tps6130x_i2c_driver = {
	.driver = {
		.name	= "tps6130x",
		.owner	= THIS_MODULE,
	},
	.probe		= tps6130x_probe,
	.remove		= __devexit_p(tps6130x_remove),
	.id_table	= tps6130x_id,
};

static int __init tps6130x_init(void)
{
	return i2c_add_driver(&tps6130x_i2c_driver);
}
subsys_initcall(tps6130x_init);

static void __exit tps6130x_cleanup(void)
{
	i2c_del_driver(&tps6130x_i2c_driver);
}
module_exit(tps6130x_cleanup);

MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
MODULE_DESCRIPTION("TPS6130X voltage regulator driver");
MODULE_LICENSE("GPL v2");