Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 * Regulator driver for TPS6524x PMIC
  3 *
  4 * Copyright (C) 2010 Texas Instruments
  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 as
  8 * published by the Free Software Foundation version 2.
  9 *
 10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
 11 * whether express or implied; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13 * General Public License for more details.
 14 */
 15
 16#include <linux/kernel.h>
 17#include <linux/module.h>
 18#include <linux/err.h>
 19#include <linux/errno.h>
 20#include <linux/slab.h>
 21#include <linux/spi/spi.h>
 22#include <linux/regulator/driver.h>
 23#include <linux/regulator/machine.h>
 24
 25#define REG_LDO_SET		0x0
 26#define LDO_ILIM_MASK		1	/* 0 = 400-800, 1 = 900-1500 */
 27#define LDO_VSEL_MASK		0x0f
 28#define LDO2_ILIM_SHIFT		12
 29#define LDO2_VSEL_SHIFT		4
 30#define LDO1_ILIM_SHIFT		8
 31#define LDO1_VSEL_SHIFT		0
 32
 33#define REG_BLOCK_EN		0x1
 34#define BLOCK_MASK		1
 35#define BLOCK_LDO1_SHIFT	0
 36#define BLOCK_LDO2_SHIFT	1
 37#define BLOCK_LCD_SHIFT		2
 38#define BLOCK_USB_SHIFT		3
 39
 40#define REG_DCDC_SET		0x2
 41#define DCDC_VDCDC_MASK		0x1f
 42#define DCDC_VDCDC1_SHIFT	0
 43#define DCDC_VDCDC2_SHIFT	5
 44#define DCDC_VDCDC3_SHIFT	10
 45
 46#define REG_DCDC_EN		0x3
 47#define DCDCDCDC_EN_MASK	0x1
 48#define DCDCDCDC1_EN_SHIFT	0
 49#define DCDCDCDC1_PG_MSK	BIT(1)
 50#define DCDCDCDC2_EN_SHIFT	2
 51#define DCDCDCDC2_PG_MSK	BIT(3)
 52#define DCDCDCDC3_EN_SHIFT	4
 53#define DCDCDCDC3_PG_MSK	BIT(5)
 54
 55#define REG_USB			0x4
 56#define USB_ILIM_SHIFT		0
 57#define USB_ILIM_MASK		0x3
 58#define USB_TSD_SHIFT		2
 59#define USB_TSD_MASK		0x3
 60#define USB_TWARN_SHIFT		4
 61#define USB_TWARN_MASK		0x3
 62#define USB_IWARN_SD		BIT(6)
 63#define USB_FAST_LOOP		BIT(7)
 64
 65#define REG_ALARM		0x5
 66#define ALARM_LDO1		BIT(0)
 67#define ALARM_DCDC1		BIT(1)
 68#define ALARM_DCDC2		BIT(2)
 69#define ALARM_DCDC3		BIT(3)
 70#define ALARM_LDO2		BIT(4)
 71#define ALARM_USB_WARN		BIT(5)
 72#define ALARM_USB_ALARM		BIT(6)
 73#define ALARM_LCD		BIT(9)
 74#define ALARM_TEMP_WARM		BIT(10)
 75#define ALARM_TEMP_HOT		BIT(11)
 76#define ALARM_NRST		BIT(14)
 77#define ALARM_POWERUP		BIT(15)
 78
 79#define REG_INT_ENABLE		0x6
 80#define INT_LDO1		BIT(0)
 81#define INT_DCDC1		BIT(1)
 82#define INT_DCDC2		BIT(2)
 83#define INT_DCDC3		BIT(3)
 84#define INT_LDO2		BIT(4)
 85#define INT_USB_WARN		BIT(5)
 86#define INT_USB_ALARM		BIT(6)
 87#define INT_LCD			BIT(9)
 88#define INT_TEMP_WARM		BIT(10)
 89#define INT_TEMP_HOT		BIT(11)
 90#define INT_GLOBAL_EN		BIT(15)
 91
 92#define REG_INT_STATUS		0x7
 93#define STATUS_LDO1		BIT(0)
 94#define STATUS_DCDC1		BIT(1)
 95#define STATUS_DCDC2		BIT(2)
 96#define STATUS_DCDC3		BIT(3)
 97#define STATUS_LDO2		BIT(4)
 98#define STATUS_USB_WARN		BIT(5)
 99#define STATUS_USB_ALARM	BIT(6)
100#define STATUS_LCD		BIT(9)
101#define STATUS_TEMP_WARM	BIT(10)
102#define STATUS_TEMP_HOT		BIT(11)
103
104#define REG_SOFTWARE_RESET	0xb
105#define REG_WRITE_ENABLE	0xd
106#define REG_REV_ID		0xf
107
108#define N_DCDC			3
109#define N_LDO			2
110#define N_SWITCH		2
111#define N_REGULATORS		(N_DCDC + N_LDO + N_SWITCH)
112
 
 
 
113#define CMD_READ(reg)		((reg) << 6)
114#define CMD_WRITE(reg)		(BIT(5) | (reg) << 6)
115#define STAT_CLK		BIT(3)
116#define STAT_WRITE		BIT(2)
117#define STAT_INVALID		BIT(1)
118#define STAT_WP			BIT(0)
119
120struct field {
121	int		reg;
122	int		shift;
123	int		mask;
124};
125
126struct supply_info {
127	const char	*name;
128	int		n_voltages;
129	const unsigned int *voltages;
 
130	int		n_ilimsels;
131	const unsigned int *ilimsels;
 
 
132	struct field	enable, voltage, ilimsel;
133};
134
135struct tps6524x {
136	struct device		*dev;
137	struct spi_device	*spi;
138	struct mutex		lock;
139	struct regulator_desc	desc[N_REGULATORS];
140	struct regulator_dev	*rdev[N_REGULATORS];
141};
142
143static int __read_reg(struct tps6524x *hw, int reg)
144{
145	int error = 0;
146	u16 cmd = CMD_READ(reg), in;
147	u8 status;
148	struct spi_message m;
149	struct spi_transfer t[3];
150
151	spi_message_init(&m);
152	memset(t, 0, sizeof(t));
153
154	t[0].tx_buf = &cmd;
155	t[0].len = 2;
156	t[0].bits_per_word = 12;
157	spi_message_add_tail(&t[0], &m);
158
159	t[1].rx_buf = &in;
160	t[1].len = 2;
161	t[1].bits_per_word = 16;
162	spi_message_add_tail(&t[1], &m);
163
164	t[2].rx_buf = &status;
165	t[2].len = 1;
166	t[2].bits_per_word = 4;
167	spi_message_add_tail(&t[2], &m);
168
169	error = spi_sync(hw->spi, &m);
170	if (error < 0)
171		return error;
172
173	dev_dbg(hw->dev, "read reg %d, data %x, status %x\n",
174		reg, in, status);
175
176	if (!(status & STAT_CLK) || (status & STAT_WRITE))
177		return -EIO;
178
179	if (status & STAT_INVALID)
180		return -EINVAL;
181
182	return in;
183}
184
185static int read_reg(struct tps6524x *hw, int reg)
186{
187	int ret;
188
189	mutex_lock(&hw->lock);
190	ret = __read_reg(hw, reg);
191	mutex_unlock(&hw->lock);
192
193	return ret;
194}
195
196static int __write_reg(struct tps6524x *hw, int reg, int val)
197{
198	int error = 0;
199	u16 cmd = CMD_WRITE(reg), out = val;
200	u8 status;
201	struct spi_message m;
202	struct spi_transfer t[3];
203
204	spi_message_init(&m);
205	memset(t, 0, sizeof(t));
206
207	t[0].tx_buf = &cmd;
208	t[0].len = 2;
209	t[0].bits_per_word = 12;
210	spi_message_add_tail(&t[0], &m);
211
212	t[1].tx_buf = &out;
213	t[1].len = 2;
214	t[1].bits_per_word = 16;
215	spi_message_add_tail(&t[1], &m);
216
217	t[2].rx_buf = &status;
218	t[2].len = 1;
219	t[2].bits_per_word = 4;
220	spi_message_add_tail(&t[2], &m);
221
222	error = spi_sync(hw->spi, &m);
223	if (error < 0)
224		return error;
225
226	dev_dbg(hw->dev, "wrote reg %d, data %x, status %x\n",
227		reg, out, status);
228
229	if (!(status & STAT_CLK) || !(status & STAT_WRITE))
230		return -EIO;
231
232	if (status & (STAT_INVALID | STAT_WP))
233		return -EINVAL;
234
235	return error;
236}
237
238static int __rmw_reg(struct tps6524x *hw, int reg, int mask, int val)
239{
240	int ret;
241
242	ret = __read_reg(hw, reg);
243	if (ret < 0)
244		return ret;
245
246	ret &= ~mask;
247	ret |= val;
248
249	ret = __write_reg(hw, reg, ret);
250
251	return (ret < 0) ? ret : 0;
252}
253
254static int rmw_protect(struct tps6524x *hw, int reg, int mask, int val)
255{
256	int ret;
257
258	mutex_lock(&hw->lock);
259
260	ret = __write_reg(hw, REG_WRITE_ENABLE, 1);
261	if (ret) {
262		dev_err(hw->dev, "failed to set write enable\n");
263		goto error;
264	}
265
266	ret = __rmw_reg(hw, reg, mask, val);
267	if (ret)
268		dev_err(hw->dev, "failed to rmw register %d\n", reg);
269
270	ret = __write_reg(hw, REG_WRITE_ENABLE, 0);
271	if (ret) {
272		dev_err(hw->dev, "failed to clear write enable\n");
273		goto error;
274	}
275
276error:
277	mutex_unlock(&hw->lock);
278
279	return ret;
280}
281
282static int read_field(struct tps6524x *hw, const struct field *field)
283{
284	int tmp;
285
286	tmp = read_reg(hw, field->reg);
287	if (tmp < 0)
288		return tmp;
289
290	return (tmp >> field->shift) & field->mask;
291}
292
293static int write_field(struct tps6524x *hw, const struct field *field,
294		       int val)
295{
296	if (val & ~field->mask)
297		return -EOVERFLOW;
298
299	return rmw_protect(hw, field->reg,
300				    field->mask << field->shift,
301				    val << field->shift);
302}
303
304static const unsigned int dcdc1_voltages[] = {
305	 800000,  825000,  850000,  875000,
306	 900000,  925000,  950000,  975000,
307	1000000, 1025000, 1050000, 1075000,
308	1100000, 1125000, 1150000, 1175000,
309	1200000, 1225000, 1250000, 1275000,
310	1300000, 1325000, 1350000, 1375000,
311	1400000, 1425000, 1450000, 1475000,
312	1500000, 1525000, 1550000, 1575000,
313};
314
315static const unsigned int dcdc2_voltages[] = {
316	1400000, 1450000, 1500000, 1550000,
317	1600000, 1650000, 1700000, 1750000,
318	1800000, 1850000, 1900000, 1950000,
319	2000000, 2050000, 2100000, 2150000,
320	2200000, 2250000, 2300000, 2350000,
321	2400000, 2450000, 2500000, 2550000,
322	2600000, 2650000, 2700000, 2750000,
323	2800000, 2850000, 2900000, 2950000,
324};
325
326static const unsigned int dcdc3_voltages[] = {
327	2400000, 2450000, 2500000, 2550000, 2600000,
328	2650000, 2700000, 2750000, 2800000, 2850000,
329	2900000, 2950000, 3000000, 3050000, 3100000,
330	3150000, 3200000, 3250000, 3300000, 3350000,
331	3400000, 3450000, 3500000, 3550000, 3600000,
332};
333
334static const unsigned int ldo1_voltages[] = {
335	4300000, 4350000, 4400000, 4450000,
336	4500000, 4550000, 4600000, 4650000,
337	4700000, 4750000, 4800000, 4850000,
338	4900000, 4950000, 5000000, 5050000,
339};
340
341static const unsigned int ldo2_voltages[] = {
342	1100000, 1150000, 1200000, 1250000,
343	1300000, 1700000, 1750000, 1800000,
344	1850000, 1900000, 3150000, 3200000,
345	3250000, 3300000, 3350000, 3400000,
346};
347
348static const unsigned int fixed_5000000_voltage[] = {
349	5000000
350};
351
352static const unsigned int ldo_ilimsel[] = {
353	400000, 1500000
354};
355
356static const unsigned int usb_ilimsel[] = {
357	200000, 400000, 800000, 1000000
358};
359
360static const unsigned int fixed_2400000_ilimsel[] = {
361	2400000
362};
363
364static const unsigned int fixed_1200000_ilimsel[] = {
365	1200000
366};
367
368static const unsigned int fixed_400000_ilimsel[] = {
369	400000
370};
371
372#define __MK_FIELD(_reg, _mask, _shift) \
373	{ .reg = (_reg), .mask = (_mask), .shift = (_shift), }
374
375static const struct supply_info supply_info[N_REGULATORS] = {
376	{
377		.name		= "DCDC1",
 
378		.n_voltages	= ARRAY_SIZE(dcdc1_voltages),
379		.voltages	= dcdc1_voltages,
380		.n_ilimsels	= ARRAY_SIZE(fixed_2400000_ilimsel),
381		.ilimsels	= fixed_2400000_ilimsel,
382		.enable		= __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
383					     DCDCDCDC1_EN_SHIFT),
384		.voltage	= __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
385					     DCDC_VDCDC1_SHIFT),
386	},
387	{
388		.name		= "DCDC2",
 
389		.n_voltages	= ARRAY_SIZE(dcdc2_voltages),
390		.voltages	= dcdc2_voltages,
391		.n_ilimsels	= ARRAY_SIZE(fixed_1200000_ilimsel),
392		.ilimsels	= fixed_1200000_ilimsel,
393		.enable		= __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
394					     DCDCDCDC2_EN_SHIFT),
395		.voltage	= __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
396					     DCDC_VDCDC2_SHIFT),
397	},
398	{
399		.name		= "DCDC3",
 
400		.n_voltages	= ARRAY_SIZE(dcdc3_voltages),
401		.voltages	= dcdc3_voltages,
402		.n_ilimsels	= ARRAY_SIZE(fixed_1200000_ilimsel),
403		.ilimsels	= fixed_1200000_ilimsel,
404		.enable		= __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
405					DCDCDCDC3_EN_SHIFT),
406		.voltage	= __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
407					     DCDC_VDCDC3_SHIFT),
408	},
409	{
410		.name		= "LDO1",
411		.n_voltages	= ARRAY_SIZE(ldo1_voltages),
412		.voltages	= ldo1_voltages,
413		.n_ilimsels	= ARRAY_SIZE(ldo_ilimsel),
414		.ilimsels	= ldo_ilimsel,
415		.enable		= __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
416					     BLOCK_LDO1_SHIFT),
417		.voltage	= __MK_FIELD(REG_LDO_SET, LDO_VSEL_MASK,
418					     LDO1_VSEL_SHIFT),
419		.ilimsel	= __MK_FIELD(REG_LDO_SET, LDO_ILIM_MASK,
420					     LDO1_ILIM_SHIFT),
421	},
422	{
423		.name		= "LDO2",
424		.n_voltages	= ARRAY_SIZE(ldo2_voltages),
425		.voltages	= ldo2_voltages,
426		.n_ilimsels	= ARRAY_SIZE(ldo_ilimsel),
427		.ilimsels	= ldo_ilimsel,
428		.enable		= __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
429					     BLOCK_LDO2_SHIFT),
430		.voltage	= __MK_FIELD(REG_LDO_SET, LDO_VSEL_MASK,
431					     LDO2_VSEL_SHIFT),
432		.ilimsel	= __MK_FIELD(REG_LDO_SET, LDO_ILIM_MASK,
433					     LDO2_ILIM_SHIFT),
434	},
435	{
436		.name		= "USB",
437		.n_voltages	= ARRAY_SIZE(fixed_5000000_voltage),
438		.voltages	= fixed_5000000_voltage,
439		.n_ilimsels	= ARRAY_SIZE(usb_ilimsel),
440		.ilimsels	= usb_ilimsel,
441		.enable		= __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
442					     BLOCK_USB_SHIFT),
443		.ilimsel	= __MK_FIELD(REG_USB, USB_ILIM_MASK,
444					     USB_ILIM_SHIFT),
445	},
446	{
447		.name		= "LCD",
448		.n_voltages	= ARRAY_SIZE(fixed_5000000_voltage),
449		.voltages	= fixed_5000000_voltage,
450		.n_ilimsels	= ARRAY_SIZE(fixed_400000_ilimsel),
451		.ilimsels	= fixed_400000_ilimsel,
452		.enable		= __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
453					     BLOCK_LCD_SHIFT),
454	},
455};
456
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
457static int set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
458{
459	const struct supply_info *info;
460	struct tps6524x *hw;
461
462	hw	= rdev_get_drvdata(rdev);
463	info	= &supply_info[rdev_get_id(rdev)];
464
465	if (rdev->desc->n_voltages == 1)
466		return -EINVAL;
467
468	return write_field(hw, &info->voltage, selector);
469}
470
471static int get_voltage_sel(struct regulator_dev *rdev)
472{
473	const struct supply_info *info;
474	struct tps6524x *hw;
475	int ret;
476
477	hw	= rdev_get_drvdata(rdev);
478	info	= &supply_info[rdev_get_id(rdev)];
479
480	if (rdev->desc->n_voltages == 1)
481		return 0;
482
483	ret = read_field(hw, &info->voltage);
484	if (ret < 0)
485		return ret;
486	if (WARN_ON(ret >= info->n_voltages))
487		return -EIO;
488
489	return ret;
490}
491
492static int set_current_limit(struct regulator_dev *rdev, int min_uA,
493			     int max_uA)
494{
495	const struct supply_info *info;
496	struct tps6524x *hw;
497	int i;
498
499	hw	= rdev_get_drvdata(rdev);
500	info	= &supply_info[rdev_get_id(rdev)];
501
502	if (info->n_ilimsels == 1)
503		return -EINVAL;
504
505	for (i = info->n_ilimsels - 1; i >= 0; i--) {
506		if (min_uA <= info->ilimsels[i] &&
507		    max_uA >= info->ilimsels[i])
508			return write_field(hw, &info->ilimsel, i);
509	}
 
 
510
511	return -EINVAL;
512}
513
514static int get_current_limit(struct regulator_dev *rdev)
515{
516	const struct supply_info *info;
517	struct tps6524x *hw;
518	int ret;
519
520	hw	= rdev_get_drvdata(rdev);
521	info	= &supply_info[rdev_get_id(rdev)];
522
523	if (info->n_ilimsels == 1)
524		return info->ilimsels[0];
525
526	ret = read_field(hw, &info->ilimsel);
527	if (ret < 0)
528		return ret;
529	if (WARN_ON(ret >= info->n_ilimsels))
530		return -EIO;
531
532	return info->ilimsels[ret];
533}
534
535static int enable_supply(struct regulator_dev *rdev)
536{
537	const struct supply_info *info;
538	struct tps6524x *hw;
539
540	hw	= rdev_get_drvdata(rdev);
541	info	= &supply_info[rdev_get_id(rdev)];
542
543	return write_field(hw, &info->enable, 1);
544}
545
546static int disable_supply(struct regulator_dev *rdev)
547{
548	const struct supply_info *info;
549	struct tps6524x *hw;
550
551	hw	= rdev_get_drvdata(rdev);
552	info	= &supply_info[rdev_get_id(rdev)];
553
554	return write_field(hw, &info->enable, 0);
555}
556
557static int is_supply_enabled(struct regulator_dev *rdev)
558{
559	const struct supply_info *info;
560	struct tps6524x *hw;
561
562	hw	= rdev_get_drvdata(rdev);
563	info	= &supply_info[rdev_get_id(rdev)];
564
565	return read_field(hw, &info->enable);
566}
567
568static struct regulator_ops regulator_ops = {
569	.is_enabled		= is_supply_enabled,
570	.enable			= enable_supply,
571	.disable		= disable_supply,
572	.get_voltage_sel	= get_voltage_sel,
573	.set_voltage_sel	= set_voltage_sel,
574	.list_voltage		= regulator_list_voltage_table,
575	.map_voltage		= regulator_map_voltage_ascend,
576	.set_current_limit	= set_current_limit,
577	.get_current_limit	= get_current_limit,
578};
579
580static int pmic_probe(struct spi_device *spi)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
581{
582	struct tps6524x *hw;
583	struct device *dev = &spi->dev;
584	const struct supply_info *info = supply_info;
585	struct regulator_init_data *init_data;
586	struct regulator_config config = { };
587	int i;
588
589	init_data = dev_get_platdata(dev);
590	if (!init_data) {
591		dev_err(dev, "could not find regulator platform data\n");
592		return -EINVAL;
593	}
594
595	hw = devm_kzalloc(&spi->dev, sizeof(struct tps6524x), GFP_KERNEL);
596	if (!hw)
 
597		return -ENOMEM;
598
599	spi_set_drvdata(spi, hw);
600
601	memset(hw, 0, sizeof(struct tps6524x));
602	hw->dev = dev;
603	hw->spi = spi_dev_get(spi);
604	mutex_init(&hw->lock);
605
606	for (i = 0; i < N_REGULATORS; i++, info++, init_data++) {
607		hw->desc[i].name	= info->name;
608		hw->desc[i].id		= i;
609		hw->desc[i].n_voltages	= info->n_voltages;
610		hw->desc[i].volt_table	= info->voltages;
611		hw->desc[i].ops		= &regulator_ops;
612		hw->desc[i].type	= REGULATOR_VOLTAGE;
613		hw->desc[i].owner	= THIS_MODULE;
614
 
 
 
615		config.dev = dev;
616		config.init_data = init_data;
617		config.driver_data = hw;
618
619		hw->rdev[i] = devm_regulator_register(dev, &hw->desc[i],
620						      &config);
621		if (IS_ERR(hw->rdev[i]))
622			return PTR_ERR(hw->rdev[i]);
 
 
623	}
624
625	return 0;
 
 
 
 
626}
627
628static struct spi_driver pmic_driver = {
629	.probe		= pmic_probe,
 
630	.driver		= {
631		.name	= "tps6524x",
 
632	},
633};
634
635module_spi_driver(pmic_driver);
636
637MODULE_DESCRIPTION("TPS6524X PMIC Driver");
638MODULE_AUTHOR("Cyril Chemparathy");
639MODULE_LICENSE("GPL");
640MODULE_ALIAS("spi:tps6524x");
v3.5.6
  1/*
  2 * Regulator driver for TPS6524x PMIC
  3 *
  4 * Copyright (C) 2010 Texas Instruments
  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 as
  8 * published by the Free Software Foundation version 2.
  9 *
 10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
 11 * whether express or implied; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13 * General Public License for more details.
 14 */
 15
 16#include <linux/kernel.h>
 17#include <linux/module.h>
 18#include <linux/err.h>
 19#include <linux/errno.h>
 20#include <linux/slab.h>
 21#include <linux/spi/spi.h>
 22#include <linux/regulator/driver.h>
 23#include <linux/regulator/machine.h>
 24
 25#define REG_LDO_SET		0x0
 26#define LDO_ILIM_MASK		1	/* 0 = 400-800, 1 = 900-1500 */
 27#define LDO_VSEL_MASK		0x0f
 28#define LDO2_ILIM_SHIFT		12
 29#define LDO2_VSEL_SHIFT		4
 30#define LDO1_ILIM_SHIFT		8
 31#define LDO1_VSEL_SHIFT		0
 32
 33#define REG_BLOCK_EN		0x1
 34#define BLOCK_MASK		1
 35#define BLOCK_LDO1_SHIFT	0
 36#define BLOCK_LDO2_SHIFT	1
 37#define BLOCK_LCD_SHIFT		2
 38#define BLOCK_USB_SHIFT		3
 39
 40#define REG_DCDC_SET		0x2
 41#define DCDC_VDCDC_MASK		0x1f
 42#define DCDC_VDCDC1_SHIFT	0
 43#define DCDC_VDCDC2_SHIFT	5
 44#define DCDC_VDCDC3_SHIFT	10
 45
 46#define REG_DCDC_EN		0x3
 47#define DCDCDCDC_EN_MASK	0x1
 48#define DCDCDCDC1_EN_SHIFT	0
 49#define DCDCDCDC1_PG_MSK	BIT(1)
 50#define DCDCDCDC2_EN_SHIFT	2
 51#define DCDCDCDC2_PG_MSK	BIT(3)
 52#define DCDCDCDC3_EN_SHIFT	4
 53#define DCDCDCDC3_PG_MSK	BIT(5)
 54
 55#define REG_USB			0x4
 56#define USB_ILIM_SHIFT		0
 57#define USB_ILIM_MASK		0x3
 58#define USB_TSD_SHIFT		2
 59#define USB_TSD_MASK		0x3
 60#define USB_TWARN_SHIFT		4
 61#define USB_TWARN_MASK		0x3
 62#define USB_IWARN_SD		BIT(6)
 63#define USB_FAST_LOOP		BIT(7)
 64
 65#define REG_ALARM		0x5
 66#define ALARM_LDO1		BIT(0)
 67#define ALARM_DCDC1		BIT(1)
 68#define ALARM_DCDC2		BIT(2)
 69#define ALARM_DCDC3		BIT(3)
 70#define ALARM_LDO2		BIT(4)
 71#define ALARM_USB_WARN		BIT(5)
 72#define ALARM_USB_ALARM		BIT(6)
 73#define ALARM_LCD		BIT(9)
 74#define ALARM_TEMP_WARM		BIT(10)
 75#define ALARM_TEMP_HOT		BIT(11)
 76#define ALARM_NRST		BIT(14)
 77#define ALARM_POWERUP		BIT(15)
 78
 79#define REG_INT_ENABLE		0x6
 80#define INT_LDO1		BIT(0)
 81#define INT_DCDC1		BIT(1)
 82#define INT_DCDC2		BIT(2)
 83#define INT_DCDC3		BIT(3)
 84#define INT_LDO2		BIT(4)
 85#define INT_USB_WARN		BIT(5)
 86#define INT_USB_ALARM		BIT(6)
 87#define INT_LCD			BIT(9)
 88#define INT_TEMP_WARM		BIT(10)
 89#define INT_TEMP_HOT		BIT(11)
 90#define INT_GLOBAL_EN		BIT(15)
 91
 92#define REG_INT_STATUS		0x7
 93#define STATUS_LDO1		BIT(0)
 94#define STATUS_DCDC1		BIT(1)
 95#define STATUS_DCDC2		BIT(2)
 96#define STATUS_DCDC3		BIT(3)
 97#define STATUS_LDO2		BIT(4)
 98#define STATUS_USB_WARN		BIT(5)
 99#define STATUS_USB_ALARM	BIT(6)
100#define STATUS_LCD		BIT(9)
101#define STATUS_TEMP_WARM	BIT(10)
102#define STATUS_TEMP_HOT		BIT(11)
103
104#define REG_SOFTWARE_RESET	0xb
105#define REG_WRITE_ENABLE	0xd
106#define REG_REV_ID		0xf
107
108#define N_DCDC			3
109#define N_LDO			2
110#define N_SWITCH		2
111#define N_REGULATORS		(N_DCDC + N_LDO + N_SWITCH)
112
113#define FIXED_ILIMSEL		BIT(0)
114#define FIXED_VOLTAGE		BIT(1)
115
116#define CMD_READ(reg)		((reg) << 6)
117#define CMD_WRITE(reg)		(BIT(5) | (reg) << 6)
118#define STAT_CLK		BIT(3)
119#define STAT_WRITE		BIT(2)
120#define STAT_INVALID		BIT(1)
121#define STAT_WP			BIT(0)
122
123struct field {
124	int		reg;
125	int		shift;
126	int		mask;
127};
128
129struct supply_info {
130	const char	*name;
131	int		n_voltages;
132	const int	*voltages;
133	int		fixed_voltage;
134	int		n_ilimsels;
135	const int	*ilimsels;
136	int		fixed_ilimsel;
137	int		flags;
138	struct field	enable, voltage, ilimsel;
139};
140
141struct tps6524x {
142	struct device		*dev;
143	struct spi_device	*spi;
144	struct mutex		lock;
145	struct regulator_desc	desc[N_REGULATORS];
146	struct regulator_dev	*rdev[N_REGULATORS];
147};
148
149static int __read_reg(struct tps6524x *hw, int reg)
150{
151	int error = 0;
152	u16 cmd = CMD_READ(reg), in;
153	u8 status;
154	struct spi_message m;
155	struct spi_transfer t[3];
156
157	spi_message_init(&m);
158	memset(t, 0, sizeof(t));
159
160	t[0].tx_buf = &cmd;
161	t[0].len = 2;
162	t[0].bits_per_word = 12;
163	spi_message_add_tail(&t[0], &m);
164
165	t[1].rx_buf = &in;
166	t[1].len = 2;
167	t[1].bits_per_word = 16;
168	spi_message_add_tail(&t[1], &m);
169
170	t[2].rx_buf = &status;
171	t[2].len = 1;
172	t[2].bits_per_word = 4;
173	spi_message_add_tail(&t[2], &m);
174
175	error = spi_sync(hw->spi, &m);
176	if (error < 0)
177		return error;
178
179	dev_dbg(hw->dev, "read reg %d, data %x, status %x\n",
180		reg, in, status);
181
182	if (!(status & STAT_CLK) || (status & STAT_WRITE))
183		return -EIO;
184
185	if (status & STAT_INVALID)
186		return -EINVAL;
187
188	return in;
189}
190
191static int read_reg(struct tps6524x *hw, int reg)
192{
193	int ret;
194
195	mutex_lock(&hw->lock);
196	ret = __read_reg(hw, reg);
197	mutex_unlock(&hw->lock);
198
199	return ret;
200}
201
202static int __write_reg(struct tps6524x *hw, int reg, int val)
203{
204	int error = 0;
205	u16 cmd = CMD_WRITE(reg), out = val;
206	u8 status;
207	struct spi_message m;
208	struct spi_transfer t[3];
209
210	spi_message_init(&m);
211	memset(t, 0, sizeof(t));
212
213	t[0].tx_buf = &cmd;
214	t[0].len = 2;
215	t[0].bits_per_word = 12;
216	spi_message_add_tail(&t[0], &m);
217
218	t[1].tx_buf = &out;
219	t[1].len = 2;
220	t[1].bits_per_word = 16;
221	spi_message_add_tail(&t[1], &m);
222
223	t[2].rx_buf = &status;
224	t[2].len = 1;
225	t[2].bits_per_word = 4;
226	spi_message_add_tail(&t[2], &m);
227
228	error = spi_sync(hw->spi, &m);
229	if (error < 0)
230		return error;
231
232	dev_dbg(hw->dev, "wrote reg %d, data %x, status %x\n",
233		reg, out, status);
234
235	if (!(status & STAT_CLK) || !(status & STAT_WRITE))
236		return -EIO;
237
238	if (status & (STAT_INVALID | STAT_WP))
239		return -EINVAL;
240
241	return error;
242}
243
244static int __rmw_reg(struct tps6524x *hw, int reg, int mask, int val)
245{
246	int ret;
247
248	ret = __read_reg(hw, reg);
249	if (ret < 0)
250		return ret;
251
252	ret &= ~mask;
253	ret |= val;
254
255	ret = __write_reg(hw, reg, ret);
256
257	return (ret < 0) ? ret : 0;
258}
259
260static int rmw_protect(struct tps6524x *hw, int reg, int mask, int val)
261{
262	int ret;
263
264	mutex_lock(&hw->lock);
265
266	ret = __write_reg(hw, REG_WRITE_ENABLE, 1);
267	if (ret) {
268		dev_err(hw->dev, "failed to set write enable\n");
269		goto error;
270	}
271
272	ret = __rmw_reg(hw, reg, mask, val);
273	if (ret)
274		dev_err(hw->dev, "failed to rmw register %d\n", reg);
275
276	ret = __write_reg(hw, REG_WRITE_ENABLE, 0);
277	if (ret) {
278		dev_err(hw->dev, "failed to clear write enable\n");
279		goto error;
280	}
281
282error:
283	mutex_unlock(&hw->lock);
284
285	return ret;
286}
287
288static int read_field(struct tps6524x *hw, const struct field *field)
289{
290	int tmp;
291
292	tmp = read_reg(hw, field->reg);
293	if (tmp < 0)
294		return tmp;
295
296	return (tmp >> field->shift) & field->mask;
297}
298
299static int write_field(struct tps6524x *hw, const struct field *field,
300		       int val)
301{
302	if (val & ~field->mask)
303		return -EOVERFLOW;
304
305	return rmw_protect(hw, field->reg,
306				    field->mask << field->shift,
307				    val << field->shift);
308}
309
310static const int dcdc1_voltages[] = {
311	 800000,  825000,  850000,  875000,
312	 900000,  925000,  950000,  975000,
313	1000000, 1025000, 1050000, 1075000,
314	1100000, 1125000, 1150000, 1175000,
315	1200000, 1225000, 1250000, 1275000,
316	1300000, 1325000, 1350000, 1375000,
317	1400000, 1425000, 1450000, 1475000,
318	1500000, 1525000, 1550000, 1575000,
319};
320
321static const int dcdc2_voltages[] = {
322	1400000, 1450000, 1500000, 1550000,
323	1600000, 1650000, 1700000, 1750000,
324	1800000, 1850000, 1900000, 1950000,
325	2000000, 2050000, 2100000, 2150000,
326	2200000, 2250000, 2300000, 2350000,
327	2400000, 2450000, 2500000, 2550000,
328	2600000, 2650000, 2700000, 2750000,
329	2800000, 2850000, 2900000, 2950000,
330};
331
332static const int dcdc3_voltages[] = {
333	2400000, 2450000, 2500000, 2550000, 2600000,
334	2650000, 2700000, 2750000, 2800000, 2850000,
335	2900000, 2950000, 3000000, 3050000, 3100000,
336	3150000, 3200000, 3250000, 3300000, 3350000,
337	3400000, 3450000, 3500000, 3550000, 3600000,
338};
339
340static const int ldo1_voltages[] = {
341	4300000, 4350000, 4400000, 4450000,
342	4500000, 4550000, 4600000, 4650000,
343	4700000, 4750000, 4800000, 4850000,
344	4900000, 4950000, 5000000, 5050000,
345};
346
347static const int ldo2_voltages[] = {
348	1100000, 1150000, 1200000, 1250000,
349	1300000, 1700000, 1750000, 1800000,
350	1850000, 1900000, 3150000, 3200000,
351	3250000, 3300000, 3350000, 3400000,
352};
353
354static const int ldo_ilimsel[] = {
 
 
 
 
355	400000, 1500000
356};
357
358static const int usb_ilimsel[] = {
359	200000, 400000, 800000, 1000000
360};
361
 
 
 
 
 
 
 
 
 
 
 
 
362#define __MK_FIELD(_reg, _mask, _shift) \
363	{ .reg = (_reg), .mask = (_mask), .shift = (_shift), }
364
365static const struct supply_info supply_info[N_REGULATORS] = {
366	{
367		.name		= "DCDC1",
368		.flags		= FIXED_ILIMSEL,
369		.n_voltages	= ARRAY_SIZE(dcdc1_voltages),
370		.voltages	= dcdc1_voltages,
371		.fixed_ilimsel	= 2400000,
 
372		.enable		= __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
373					     DCDCDCDC1_EN_SHIFT),
374		.voltage	= __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
375					     DCDC_VDCDC1_SHIFT),
376	},
377	{
378		.name		= "DCDC2",
379		.flags		= FIXED_ILIMSEL,
380		.n_voltages	= ARRAY_SIZE(dcdc2_voltages),
381		.voltages	= dcdc2_voltages,
382		.fixed_ilimsel	= 1200000,
 
383		.enable		= __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
384					     DCDCDCDC2_EN_SHIFT),
385		.voltage	= __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
386					     DCDC_VDCDC2_SHIFT),
387	},
388	{
389		.name		= "DCDC3",
390		.flags		= FIXED_ILIMSEL,
391		.n_voltages	= ARRAY_SIZE(dcdc3_voltages),
392		.voltages	= dcdc3_voltages,
393		.fixed_ilimsel	= 1200000,
 
394		.enable		= __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
395					DCDCDCDC3_EN_SHIFT),
396		.voltage	= __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
397					     DCDC_VDCDC3_SHIFT),
398	},
399	{
400		.name		= "LDO1",
401		.n_voltages	= ARRAY_SIZE(ldo1_voltages),
402		.voltages	= ldo1_voltages,
403		.n_ilimsels	= ARRAY_SIZE(ldo_ilimsel),
404		.ilimsels	= ldo_ilimsel,
405		.enable		= __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
406					     BLOCK_LDO1_SHIFT),
407		.voltage	= __MK_FIELD(REG_LDO_SET, LDO_VSEL_MASK,
408					     LDO1_VSEL_SHIFT),
409		.ilimsel	= __MK_FIELD(REG_LDO_SET, LDO_ILIM_MASK,
410					     LDO1_ILIM_SHIFT),
411	},
412	{
413		.name		= "LDO2",
414		.n_voltages	= ARRAY_SIZE(ldo2_voltages),
415		.voltages	= ldo2_voltages,
416		.n_ilimsels	= ARRAY_SIZE(ldo_ilimsel),
417		.ilimsels	= ldo_ilimsel,
418		.enable		= __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
419					     BLOCK_LDO2_SHIFT),
420		.voltage	= __MK_FIELD(REG_LDO_SET, LDO_VSEL_MASK,
421					     LDO2_VSEL_SHIFT),
422		.ilimsel	= __MK_FIELD(REG_LDO_SET, LDO_ILIM_MASK,
423					     LDO2_ILIM_SHIFT),
424	},
425	{
426		.name		= "USB",
427		.flags		= FIXED_VOLTAGE,
428		.fixed_voltage	= 5000000,
429		.n_ilimsels	= ARRAY_SIZE(usb_ilimsel),
430		.ilimsels	= usb_ilimsel,
431		.enable		= __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
432					     BLOCK_USB_SHIFT),
433		.ilimsel	= __MK_FIELD(REG_USB, USB_ILIM_MASK,
434					     USB_ILIM_SHIFT),
435	},
436	{
437		.name		= "LCD",
438		.flags		= FIXED_VOLTAGE | FIXED_ILIMSEL,
439		.fixed_voltage	= 5000000,
440		.fixed_ilimsel	=  400000,
 
441		.enable		= __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
442					     BLOCK_LCD_SHIFT),
443	},
444};
445
446static int list_voltage(struct regulator_dev *rdev, unsigned selector)
447{
448	const struct supply_info *info;
449	struct tps6524x *hw;
450
451	hw	= rdev_get_drvdata(rdev);
452	info	= &supply_info[rdev_get_id(rdev)];
453
454	if (info->flags & FIXED_VOLTAGE)
455		return selector ? -EINVAL : info->fixed_voltage;
456
457	return ((selector < info->n_voltages) ?
458		info->voltages[selector] : -EINVAL);
459}
460
461static int set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
462{
463	const struct supply_info *info;
464	struct tps6524x *hw;
465
466	hw	= rdev_get_drvdata(rdev);
467	info	= &supply_info[rdev_get_id(rdev)];
468
469	if (info->flags & FIXED_VOLTAGE)
470		return -EINVAL;
471
472	return write_field(hw, &info->voltage, selector);
473}
474
475static int get_voltage_sel(struct regulator_dev *rdev)
476{
477	const struct supply_info *info;
478	struct tps6524x *hw;
479	int ret;
480
481	hw	= rdev_get_drvdata(rdev);
482	info	= &supply_info[rdev_get_id(rdev)];
483
484	if (info->flags & FIXED_VOLTAGE)
485		return 0;
486
487	ret = read_field(hw, &info->voltage);
488	if (ret < 0)
489		return ret;
490	if (WARN_ON(ret >= info->n_voltages))
491		return -EIO;
492
493	return ret;
494}
495
496static int set_current_limit(struct regulator_dev *rdev, int min_uA,
497			     int max_uA)
498{
499	const struct supply_info *info;
500	struct tps6524x *hw;
501	int i;
502
503	hw	= rdev_get_drvdata(rdev);
504	info	= &supply_info[rdev_get_id(rdev)];
505
506	if (info->flags & FIXED_ILIMSEL)
507		return -EINVAL;
508
509	for (i = 0; i < info->n_ilimsels; i++)
510		if (min_uA <= info->ilimsels[i] &&
511		    max_uA >= info->ilimsels[i])
512			break;
513
514	if (i >= info->n_ilimsels)
515		return -EINVAL;
516
517	return write_field(hw, &info->ilimsel, i);
518}
519
520static int get_current_limit(struct regulator_dev *rdev)
521{
522	const struct supply_info *info;
523	struct tps6524x *hw;
524	int ret;
525
526	hw	= rdev_get_drvdata(rdev);
527	info	= &supply_info[rdev_get_id(rdev)];
528
529	if (info->flags & FIXED_ILIMSEL)
530		return info->fixed_ilimsel;
531
532	ret = read_field(hw, &info->ilimsel);
533	if (ret < 0)
534		return ret;
535	if (WARN_ON(ret >= info->n_ilimsels))
536		return -EIO;
537
538	return info->ilimsels[ret];
539}
540
541static int enable_supply(struct regulator_dev *rdev)
542{
543	const struct supply_info *info;
544	struct tps6524x *hw;
545
546	hw	= rdev_get_drvdata(rdev);
547	info	= &supply_info[rdev_get_id(rdev)];
548
549	return write_field(hw, &info->enable, 1);
550}
551
552static int disable_supply(struct regulator_dev *rdev)
553{
554	const struct supply_info *info;
555	struct tps6524x *hw;
556
557	hw	= rdev_get_drvdata(rdev);
558	info	= &supply_info[rdev_get_id(rdev)];
559
560	return write_field(hw, &info->enable, 0);
561}
562
563static int is_supply_enabled(struct regulator_dev *rdev)
564{
565	const struct supply_info *info;
566	struct tps6524x *hw;
567
568	hw	= rdev_get_drvdata(rdev);
569	info	= &supply_info[rdev_get_id(rdev)];
570
571	return read_field(hw, &info->enable);
572}
573
574static struct regulator_ops regulator_ops = {
575	.is_enabled		= is_supply_enabled,
576	.enable			= enable_supply,
577	.disable		= disable_supply,
578	.get_voltage_sel	= get_voltage_sel,
579	.set_voltage_sel	= set_voltage_sel,
580	.list_voltage		= list_voltage,
 
581	.set_current_limit	= set_current_limit,
582	.get_current_limit	= get_current_limit,
583};
584
585static int pmic_remove(struct spi_device *spi)
586{
587	struct tps6524x *hw = spi_get_drvdata(spi);
588	int i;
589
590	if (!hw)
591		return 0;
592	for (i = 0; i < N_REGULATORS; i++) {
593		if (hw->rdev[i])
594			regulator_unregister(hw->rdev[i]);
595		hw->rdev[i] = NULL;
596	}
597	spi_set_drvdata(spi, NULL);
598	return 0;
599}
600
601static int __devinit pmic_probe(struct spi_device *spi)
602{
603	struct tps6524x *hw;
604	struct device *dev = &spi->dev;
605	const struct supply_info *info = supply_info;
606	struct regulator_init_data *init_data;
607	struct regulator_config config = { };
608	int ret = 0, i;
609
610	init_data = dev->platform_data;
611	if (!init_data) {
612		dev_err(dev, "could not find regulator platform data\n");
613		return -EINVAL;
614	}
615
616	hw = devm_kzalloc(&spi->dev, sizeof(struct tps6524x), GFP_KERNEL);
617	if (!hw) {
618		dev_err(dev, "cannot allocate regulator private data\n");
619		return -ENOMEM;
620	}
621	spi_set_drvdata(spi, hw);
622
623	memset(hw, 0, sizeof(struct tps6524x));
624	hw->dev = dev;
625	hw->spi = spi_dev_get(spi);
626	mutex_init(&hw->lock);
627
628	for (i = 0; i < N_REGULATORS; i++, info++, init_data++) {
629		hw->desc[i].name	= info->name;
630		hw->desc[i].id		= i;
631		hw->desc[i].n_voltages	= info->n_voltages;
 
632		hw->desc[i].ops		= &regulator_ops;
633		hw->desc[i].type	= REGULATOR_VOLTAGE;
634		hw->desc[i].owner	= THIS_MODULE;
635
636		if (info->flags & FIXED_VOLTAGE)
637			hw->desc[i].n_voltages = 1;
638
639		config.dev = dev;
640		config.init_data = init_data;
641		config.driver_data = hw;
642
643		hw->rdev[i] = regulator_register(&hw->desc[i], &config);
644		if (IS_ERR(hw->rdev[i])) {
645			ret = PTR_ERR(hw->rdev[i]);
646			hw->rdev[i] = NULL;
647			goto fail;
648		}
649	}
650
651	return 0;
652
653fail:
654	pmic_remove(spi);
655	return ret;
656}
657
658static struct spi_driver pmic_driver = {
659	.probe		= pmic_probe,
660	.remove		= __devexit_p(pmic_remove),
661	.driver		= {
662		.name	= "tps6524x",
663		.owner	= THIS_MODULE,
664	},
665};
666
667module_spi_driver(pmic_driver);
668
669MODULE_DESCRIPTION("TPS6524X PMIC Driver");
670MODULE_AUTHOR("Cyril Chemparathy");
671MODULE_LICENSE("GPL");
672MODULE_ALIAS("spi:tps6524x");