xref: /linux/drivers/regulator/tps6586x-regulator.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
249610235SMike Rapoport /*
349610235SMike Rapoport  * Regulator driver for TI TPS6586x
449610235SMike Rapoport  *
549610235SMike Rapoport  * Copyright (C) 2010 Compulab Ltd.
649610235SMike Rapoport  * Author: Mike Rapoport <mike@compulab.co.il>
749610235SMike Rapoport  *
849610235SMike Rapoport  * Based on da903x
949610235SMike Rapoport  * Copyright (C) 2006-2008 Marvell International Ltd.
1049610235SMike Rapoport  * Copyright (C) 2008 Compulab Ltd.
1149610235SMike Rapoport  */
1249610235SMike Rapoport 
1349610235SMike Rapoport #include <linux/kernel.h>
1465602c32SPaul Gortmaker #include <linux/module.h>
1549610235SMike Rapoport #include <linux/init.h>
1649610235SMike Rapoport #include <linux/err.h>
1764e48160SLaxman Dewangan #include <linux/of.h>
1849610235SMike Rapoport #include <linux/slab.h>
1949610235SMike Rapoport #include <linux/platform_device.h>
2049610235SMike Rapoport #include <linux/regulator/driver.h>
2149610235SMike Rapoport #include <linux/regulator/machine.h>
2264e48160SLaxman Dewangan #include <linux/regulator/of_regulator.h>
2349610235SMike Rapoport #include <linux/mfd/tps6586x.h>
2449610235SMike Rapoport 
2549610235SMike Rapoport /* supply control and voltage setting  */
2649610235SMike Rapoport #define TPS6586X_SUPPLYENA	0x10
2749610235SMike Rapoport #define TPS6586X_SUPPLYENB	0x11
2849610235SMike Rapoport #define TPS6586X_SUPPLYENC	0x12
2949610235SMike Rapoport #define TPS6586X_SUPPLYEND	0x13
3049610235SMike Rapoport #define TPS6586X_SUPPLYENE	0x14
3149610235SMike Rapoport #define TPS6586X_VCC1		0x20
3249610235SMike Rapoport #define TPS6586X_VCC2		0x21
3349610235SMike Rapoport #define TPS6586X_SM1V1		0x23
3449610235SMike Rapoport #define TPS6586X_SM1V2		0x24
3549610235SMike Rapoport #define TPS6586X_SM1SL		0x25
3649610235SMike Rapoport #define TPS6586X_SM0V1		0x26
3749610235SMike Rapoport #define TPS6586X_SM0V2		0x27
3849610235SMike Rapoport #define TPS6586X_SM0SL		0x28
3949610235SMike Rapoport #define TPS6586X_LDO2AV1	0x29
4049610235SMike Rapoport #define TPS6586X_LDO2AV2	0x2A
4149610235SMike Rapoport #define TPS6586X_LDO2BV1	0x2F
4249610235SMike Rapoport #define TPS6586X_LDO2BV2	0x30
4349610235SMike Rapoport #define TPS6586X_LDO4V1		0x32
4449610235SMike Rapoport #define TPS6586X_LDO4V2		0x33
4549610235SMike Rapoport 
4649610235SMike Rapoport /* converter settings  */
4749610235SMike Rapoport #define TPS6586X_SUPPLYV1	0x41
4849610235SMike Rapoport #define TPS6586X_SUPPLYV2	0x42
4949610235SMike Rapoport #define TPS6586X_SUPPLYV3	0x43
5049610235SMike Rapoport #define TPS6586X_SUPPLYV4	0x44
5149610235SMike Rapoport #define TPS6586X_SUPPLYV5	0x45
5249610235SMike Rapoport #define TPS6586X_SUPPLYV6	0x46
5349610235SMike Rapoport #define TPS6586X_SMODE1		0x47
5449610235SMike Rapoport #define TPS6586X_SMODE2		0x48
5549610235SMike Rapoport 
5649610235SMike Rapoport struct tps6586x_regulator {
5749610235SMike Rapoport 	struct regulator_desc desc;
5849610235SMike Rapoport 
5949610235SMike Rapoport 	int enable_bit[2];
6049610235SMike Rapoport 	int enable_reg[2];
6149610235SMike Rapoport };
6249610235SMike Rapoport 
6325c80445SRikard Falkeborn static const struct regulator_ops tps6586x_rw_regulator_ops = {
64f4647037SAxel Lin 	.list_voltage = regulator_list_voltage_table,
654d673bbcSAxel Lin 	.map_voltage = regulator_map_voltage_ascend,
667c7475c0SAxel Lin 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
67d645d591SAxel Lin 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
6849610235SMike Rapoport 
697c7475c0SAxel Lin 	.is_enabled = regulator_is_enabled_regmap,
707c7475c0SAxel Lin 	.enable = regulator_enable_regmap,
717c7475c0SAxel Lin 	.disable = regulator_disable_regmap,
7249610235SMike Rapoport };
7349610235SMike Rapoport 
7425c80445SRikard Falkeborn static const struct regulator_ops tps6586x_rw_linear_regulator_ops = {
752628b100SAxel Lin 	.list_voltage = regulator_list_voltage_linear,
762628b100SAxel Lin 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
772628b100SAxel Lin 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
782628b100SAxel Lin 
792628b100SAxel Lin 	.is_enabled = regulator_is_enabled_regmap,
802628b100SAxel Lin 	.enable = regulator_enable_regmap,
812628b100SAxel Lin 	.disable = regulator_disable_regmap,
822628b100SAxel Lin };
832628b100SAxel Lin 
8425c80445SRikard Falkeborn static const struct regulator_ops tps6586x_ro_regulator_ops = {
85ad0b40feSAlban Bedel 	.list_voltage = regulator_list_voltage_table,
86ad0b40feSAlban Bedel 	.map_voltage = regulator_map_voltage_ascend,
87ad0b40feSAlban Bedel 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
88ad0b40feSAlban Bedel 
89ad0b40feSAlban Bedel 	.is_enabled = regulator_is_enabled_regmap,
90ad0b40feSAlban Bedel 	.enable = regulator_enable_regmap,
91ad0b40feSAlban Bedel 	.disable = regulator_disable_regmap,
92ad0b40feSAlban Bedel };
93ad0b40feSAlban Bedel 
9425c80445SRikard Falkeborn static const struct regulator_ops tps6586x_sys_regulator_ops = {
9549610235SMike Rapoport };
9649610235SMike Rapoport 
97f4647037SAxel Lin static const unsigned int tps6586x_ldo0_voltages[] = {
98f4647037SAxel Lin 	1200000, 1500000, 1800000, 2500000, 2700000, 2850000, 3100000, 3300000,
9949610235SMike Rapoport };
10049610235SMike Rapoport 
101f4647037SAxel Lin static const unsigned int tps6586x_ldo_voltages[] = {
102f4647037SAxel Lin 	1250000, 1500000, 1800000, 2500000, 2700000, 2850000, 3100000, 3300000,
1034cc2e393SGary King };
1044cc2e393SGary King 
1052628b100SAxel Lin static const unsigned int tps658640_rtc_voltages[] = {
1066c46ccc8SAlban Bedel 	2500000, 2850000, 3100000, 3300000,
1076c46ccc8SAlban Bedel };
1086c46ccc8SAlban Bedel 
109ad0b40feSAlban Bedel #define TPS6586X_REGULATOR(_id, _ops, _pin_name, vdata, vreg, shift, nbits, \
110d645d591SAxel Lin 			   ereg0, ebit0, ereg1, ebit1, goreg, gobit)	\
11149610235SMike Rapoport 	.desc	= {							\
1127c7fac30SLaxman Dewangan 		.supply_name = _pin_name,				\
11349610235SMike Rapoport 		.name	= "REG-" #_id,					\
114ad0b40feSAlban Bedel 		.ops	= &tps6586x_## _ops ## _regulator_ops,		\
11549610235SMike Rapoport 		.type	= REGULATOR_VOLTAGE,				\
11649610235SMike Rapoport 		.id	= TPS6586X_ID_##_id,				\
117844a4f0dSStefan Agner 		.n_voltages = ARRAY_SIZE(vdata##_voltages),		\
118844a4f0dSStefan Agner 		.volt_table = vdata##_voltages,				\
11949610235SMike Rapoport 		.owner	= THIS_MODULE,					\
1207c7475c0SAxel Lin 		.enable_reg = TPS6586X_SUPPLY##ereg0,			\
1217c7475c0SAxel Lin 		.enable_mask = 1 << (ebit0),				\
1227c7475c0SAxel Lin 		.vsel_reg = TPS6586X_##vreg,				\
1237c7475c0SAxel Lin 		.vsel_mask = ((1 << (nbits)) - 1) << (shift),		\
124d645d591SAxel Lin 		.apply_reg = (goreg),				\
125d645d591SAxel Lin 		.apply_bit = (gobit),				\
12649610235SMike Rapoport 	},								\
12749610235SMike Rapoport 	.enable_reg[0]	= TPS6586X_SUPPLY##ereg0,			\
12849610235SMike Rapoport 	.enable_bit[0]	= (ebit0),					\
12949610235SMike Rapoport 	.enable_reg[1]	= TPS6586X_SUPPLY##ereg1,			\
130f4647037SAxel Lin 	.enable_bit[1]	= (ebit1),
13164db657bSDanny Huang 
1322628b100SAxel Lin #define TPS6586X_REGULATOR_LINEAR(_id, _ops, _pin_name, n_volt, min_uv,	\
1332628b100SAxel Lin 				  uv_step, vreg, shift, nbits, ereg0,	\
1342628b100SAxel Lin 				  ebit0, ereg1, ebit1, goreg, gobit)	\
1352628b100SAxel Lin 	.desc	= {							\
1362628b100SAxel Lin 		.supply_name = _pin_name,				\
1372628b100SAxel Lin 		.name	= "REG-" #_id,					\
1382628b100SAxel Lin 		.ops	= &tps6586x_## _ops ## _regulator_ops,		\
1392628b100SAxel Lin 		.type	= REGULATOR_VOLTAGE,				\
1402628b100SAxel Lin 		.id	= TPS6586X_ID_##_id,				\
1412628b100SAxel Lin 		.n_voltages = n_volt,					\
1422628b100SAxel Lin 		.min_uV = min_uv,					\
1432628b100SAxel Lin 		.uV_step = uv_step,					\
1442628b100SAxel Lin 		.owner	= THIS_MODULE,					\
1452628b100SAxel Lin 		.enable_reg = TPS6586X_SUPPLY##ereg0,			\
1462628b100SAxel Lin 		.enable_mask = 1 << (ebit0),				\
1472628b100SAxel Lin 		.vsel_reg = TPS6586X_##vreg,				\
1482628b100SAxel Lin 		.vsel_mask = ((1 << (nbits)) - 1) << (shift),		\
1492628b100SAxel Lin 		.apply_reg = (goreg),				\
1502628b100SAxel Lin 		.apply_bit = (gobit),				\
1512628b100SAxel Lin 	},								\
1522628b100SAxel Lin 	.enable_reg[0]	= TPS6586X_SUPPLY##ereg0,			\
1532628b100SAxel Lin 	.enable_bit[0]	= (ebit0),					\
1542628b100SAxel Lin 	.enable_reg[1]	= TPS6586X_SUPPLY##ereg1,			\
1552628b100SAxel Lin 	.enable_bit[1]	= (ebit1),
1562628b100SAxel Lin 
1577c7fac30SLaxman Dewangan #define TPS6586X_LDO(_id, _pname, vdata, vreg, shift, nbits,		\
15849610235SMike Rapoport 		     ereg0, ebit0, ereg1, ebit1)			\
15964db657bSDanny Huang {									\
160ad0b40feSAlban Bedel 	TPS6586X_REGULATOR(_id, rw, _pname, vdata, vreg, shift, nbits,	\
161ad0b40feSAlban Bedel 			   ereg0, ebit0, ereg1, ebit1, 0, 0)		\
162ad0b40feSAlban Bedel }
163ad0b40feSAlban Bedel 
1642628b100SAxel Lin #define TPS6586X_LDO_LINEAR(_id, _pname, n_volt, min_uv, uv_step, vreg,	\
1652628b100SAxel Lin 			    shift, nbits, ereg0, ebit0, ereg1, ebit1)	\
1662628b100SAxel Lin {									\
1672628b100SAxel Lin 	TPS6586X_REGULATOR_LINEAR(_id, rw_linear, _pname, n_volt,	\
1682628b100SAxel Lin 				  min_uv, uv_step, vreg, shift, nbits,	\
1692628b100SAxel Lin 				  ereg0, ebit0, ereg1, ebit1, 0, 0)	\
1702628b100SAxel Lin }
1712628b100SAxel Lin 
172ad0b40feSAlban Bedel #define TPS6586X_FIXED_LDO(_id, _pname, vdata, vreg, shift, nbits,	\
173ad0b40feSAlban Bedel 			  ereg0, ebit0, ereg1, ebit1)			\
174ad0b40feSAlban Bedel {									\
175ad0b40feSAlban Bedel 	TPS6586X_REGULATOR(_id, ro, _pname, vdata, vreg, shift, nbits,	\
176d645d591SAxel Lin 			   ereg0, ebit0, ereg1, ebit1, 0, 0)		\
17764db657bSDanny Huang }
17849610235SMike Rapoport 
1792628b100SAxel Lin #define TPS6586X_DVM(_id, _pname, n_volt, min_uv, uv_step, vreg, shift,	\
1802628b100SAxel Lin 		     nbits, ereg0, ebit0, ereg1, ebit1, goreg, gobit)	\
18164db657bSDanny Huang {									\
1822628b100SAxel Lin 	TPS6586X_REGULATOR_LINEAR(_id, rw_linear, _pname, n_volt,	\
1832628b100SAxel Lin 				  min_uv, uv_step, vreg, shift, nbits,	\
1842628b100SAxel Lin 				  ereg0, ebit0, ereg1, ebit1, goreg,	\
1852628b100SAxel Lin 				  gobit)				\
18664db657bSDanny Huang }
18749610235SMike Rapoport 
1889394b80cSLaxman Dewangan #define TPS6586X_SYS_REGULATOR()					\
1899394b80cSLaxman Dewangan {									\
1909394b80cSLaxman Dewangan 	.desc	= {							\
1919394b80cSLaxman Dewangan 		.supply_name = "sys",					\
1929394b80cSLaxman Dewangan 		.name	= "REG-SYS",					\
1939394b80cSLaxman Dewangan 		.ops	= &tps6586x_sys_regulator_ops,			\
1949394b80cSLaxman Dewangan 		.type	= REGULATOR_VOLTAGE,				\
1959394b80cSLaxman Dewangan 		.id	= TPS6586X_ID_SYS,				\
1969394b80cSLaxman Dewangan 		.owner	= THIS_MODULE,					\
1979394b80cSLaxman Dewangan 	},								\
1989394b80cSLaxman Dewangan }
1999394b80cSLaxman Dewangan 
20049610235SMike Rapoport static struct tps6586x_regulator tps6586x_regulator[] = {
2019394b80cSLaxman Dewangan 	TPS6586X_SYS_REGULATOR(),
202844a4f0dSStefan Agner 	TPS6586X_LDO(LDO_0, "vinldo01", tps6586x_ldo0, SUPPLYV1, 5, 3, ENC, 0,
203844a4f0dSStefan Agner 					END, 0),
204844a4f0dSStefan Agner 	TPS6586X_LDO(LDO_3, "vinldo23", tps6586x_ldo, SUPPLYV4, 0, 3, ENC, 2,
205844a4f0dSStefan Agner 					END, 2),
206844a4f0dSStefan Agner 	TPS6586X_LDO(LDO_5, "REG-SYS", tps6586x_ldo, SUPPLYV6, 0, 3, ENE, 6,
207844a4f0dSStefan Agner 					ENE, 6),
208844a4f0dSStefan Agner 	TPS6586X_LDO(LDO_6, "vinldo678", tps6586x_ldo, SUPPLYV3, 0, 3, ENC, 4,
209844a4f0dSStefan Agner 					END, 4),
210844a4f0dSStefan Agner 	TPS6586X_LDO(LDO_7, "vinldo678", tps6586x_ldo, SUPPLYV3, 3, 3, ENC, 5,
211844a4f0dSStefan Agner 					END, 5),
212844a4f0dSStefan Agner 	TPS6586X_LDO(LDO_8, "vinldo678", tps6586x_ldo, SUPPLYV2, 5, 3, ENC, 6,
213844a4f0dSStefan Agner 					END, 6),
214844a4f0dSStefan Agner 	TPS6586X_LDO(LDO_9, "vinldo9", tps6586x_ldo, SUPPLYV6, 3, 3, ENE, 7,
215844a4f0dSStefan Agner 					ENE, 7),
216844a4f0dSStefan Agner 	TPS6586X_LDO(LDO_RTC, "REG-SYS", tps6586x_ldo, SUPPLYV4, 3, 3, V4, 7,
217844a4f0dSStefan Agner 					V4, 7),
2182628b100SAxel Lin 	TPS6586X_LDO_LINEAR(LDO_1, "vinldo01", 32, 725000, 25000, SUPPLYV1,
2192628b100SAxel Lin 			    0, 5, ENC, 1, END, 1),
2202628b100SAxel Lin 	TPS6586X_LDO_LINEAR(SM_2, "vin-sm2", 32, 3000000, 50000, SUPPLYV2,
2212628b100SAxel Lin 			    0, 5, ENC, 7, END, 7),
2222628b100SAxel Lin 	TPS6586X_DVM(LDO_2, "vinldo23", 32, 725000, 25000, LDO2BV1, 0, 5,
2232628b100SAxel Lin 		     ENA, 3, ENB, 3, TPS6586X_VCC2, BIT(6)),
2242628b100SAxel Lin 	TPS6586X_DVM(LDO_4, "vinldo4", 32, 1700000, 25000, LDO4V1, 0, 5,
2252628b100SAxel Lin 		     ENC, 3, END, 3, TPS6586X_VCC1, BIT(6)),
2262628b100SAxel Lin 	TPS6586X_DVM(SM_0, "vin-sm0", 32, 725000, 25000, SM0V1, 0, 5,
2272628b100SAxel Lin 		     ENA, 1, ENB, 1, TPS6586X_VCC1, BIT(2)),
2282628b100SAxel Lin 	TPS6586X_DVM(SM_1, "vin-sm1", 32, 725000, 25000, SM1V1, 0, 5,
2292628b100SAxel Lin 		     ENA, 0, ENB, 0, TPS6586X_VCC1, BIT(0)),
23049610235SMike Rapoport };
23149610235SMike Rapoport 
232844a4f0dSStefan Agner static struct tps6586x_regulator tps658623_regulator[] = {
2332628b100SAxel Lin 	TPS6586X_LDO_LINEAR(SM_2, "vin-sm2", 32, 1700000, 25000, SUPPLYV2,
2342628b100SAxel Lin 			    0, 5, ENC, 7, END, 7),
235844a4f0dSStefan Agner };
236844a4f0dSStefan Agner 
2376c46ccc8SAlban Bedel static struct tps6586x_regulator tps658640_regulator[] = {
2386c46ccc8SAlban Bedel 	TPS6586X_LDO(LDO_3, "vinldo23", tps6586x_ldo0, SUPPLYV4, 0, 3,
2396c46ccc8SAlban Bedel 					ENC, 2, END, 2),
2406c46ccc8SAlban Bedel 	TPS6586X_LDO(LDO_5, "REG-SYS", tps6586x_ldo0, SUPPLYV6, 0, 3,
2416c46ccc8SAlban Bedel 					ENE, 6, ENE, 6),
2426c46ccc8SAlban Bedel 	TPS6586X_LDO(LDO_6, "vinldo678", tps6586x_ldo0, SUPPLYV3, 0, 3,
2436c46ccc8SAlban Bedel 					ENC, 4, END, 4),
2446c46ccc8SAlban Bedel 	TPS6586X_LDO(LDO_7, "vinldo678", tps6586x_ldo0, SUPPLYV3, 3, 3,
2456c46ccc8SAlban Bedel 					ENC, 5, END, 5),
2466c46ccc8SAlban Bedel 	TPS6586X_LDO(LDO_8, "vinldo678", tps6586x_ldo0, SUPPLYV2, 5, 3,
2476c46ccc8SAlban Bedel 					ENC, 6, END, 6),
2486c46ccc8SAlban Bedel 	TPS6586X_LDO(LDO_9, "vinldo9", tps6586x_ldo0, SUPPLYV6, 3, 3,
2496c46ccc8SAlban Bedel 					ENE, 7, ENE, 7),
2502628b100SAxel Lin 	TPS6586X_LDO_LINEAR(SM_2, "vin-sm2", 32, 2150000, 50000, SUPPLYV2,
2512628b100SAxel Lin 			    0, 5, ENC, 7, END, 7),
2526c46ccc8SAlban Bedel 
2536c46ccc8SAlban Bedel 	TPS6586X_FIXED_LDO(LDO_RTC, "REG-SYS", tps658640_rtc, SUPPLYV4, 3, 2,
2546c46ccc8SAlban Bedel 					V4, 7, V4, 7),
2556c46ccc8SAlban Bedel };
2566c46ccc8SAlban Bedel 
257844a4f0dSStefan Agner static struct tps6586x_regulator tps658643_regulator[] = {
2582628b100SAxel Lin 	TPS6586X_LDO_LINEAR(SM_2, "vin-sm2", 32, 1025000, 25000, SUPPLYV2,
2592628b100SAxel Lin 			    0, 5, ENC, 7, END, 7),
260844a4f0dSStefan Agner };
261844a4f0dSStefan Agner 
26249610235SMike Rapoport /*
26349610235SMike Rapoport  * TPS6586X has 2 enable bits that are OR'ed to determine the actual
26449610235SMike Rapoport  * regulator state. Clearing one of this bits allows switching
26549610235SMike Rapoport  * regulator on and of with single register write.
26649610235SMike Rapoport  */
tps6586x_regulator_preinit(struct device * parent,struct tps6586x_regulator * ri)26749610235SMike Rapoport static inline int tps6586x_regulator_preinit(struct device *parent,
26849610235SMike Rapoport 					     struct tps6586x_regulator *ri)
26949610235SMike Rapoport {
27049610235SMike Rapoport 	uint8_t val1, val2;
27149610235SMike Rapoport 	int ret;
27249610235SMike Rapoport 
2731dbcf35cSDanny Huang 	if (ri->enable_reg[0] == ri->enable_reg[1] &&
2741dbcf35cSDanny Huang 	    ri->enable_bit[0] == ri->enable_bit[1])
2751dbcf35cSDanny Huang 			return 0;
2761dbcf35cSDanny Huang 
27749610235SMike Rapoport 	ret = tps6586x_read(parent, ri->enable_reg[0], &val1);
27849610235SMike Rapoport 	if (ret)
27949610235SMike Rapoport 		return ret;
28049610235SMike Rapoport 
28149610235SMike Rapoport 	ret = tps6586x_read(parent, ri->enable_reg[1], &val2);
28249610235SMike Rapoport 	if (ret)
28349610235SMike Rapoport 		return ret;
28449610235SMike Rapoport 
2854f586707SDanny Huang 	if (!(val2 & (1 << ri->enable_bit[1])))
28649610235SMike Rapoport 		return 0;
28749610235SMike Rapoport 
28849610235SMike Rapoport 	/*
28949610235SMike Rapoport 	 * The regulator is on, but it's enabled with the bit we don't
29049610235SMike Rapoport 	 * want to use, so we switch the enable bits
29149610235SMike Rapoport 	 */
2924f586707SDanny Huang 	if (!(val1 & (1 << ri->enable_bit[0]))) {
29349610235SMike Rapoport 		ret = tps6586x_set_bits(parent, ri->enable_reg[0],
29449610235SMike Rapoport 					1 << ri->enable_bit[0]);
29549610235SMike Rapoport 		if (ret)
29649610235SMike Rapoport 			return ret;
29749610235SMike Rapoport 	}
29849610235SMike Rapoport 
29949610235SMike Rapoport 	return tps6586x_clr_bits(parent, ri->enable_reg[1],
30049610235SMike Rapoport 				 1 << ri->enable_bit[1]);
30149610235SMike Rapoport }
30249610235SMike Rapoport 
tps6586x_regulator_set_slew_rate(struct platform_device * pdev,int id,struct regulator_init_data * p)30364e48160SLaxman Dewangan static int tps6586x_regulator_set_slew_rate(struct platform_device *pdev,
30464e48160SLaxman Dewangan 			int id, struct regulator_init_data *p)
305500c524aSXin Xie {
306500c524aSXin Xie 	struct device *parent = pdev->dev.parent;
307500c524aSXin Xie 	struct tps6586x_settings *setting = p->driver_data;
308500c524aSXin Xie 	uint8_t reg;
309500c524aSXin Xie 
310500c524aSXin Xie 	if (setting == NULL)
311500c524aSXin Xie 		return 0;
312500c524aSXin Xie 
313500c524aSXin Xie 	if (!(setting->slew_rate & TPS6586X_SLEW_RATE_SET))
314500c524aSXin Xie 		return 0;
315500c524aSXin Xie 
316500c524aSXin Xie 	/* only SM0 and SM1 can have the slew rate settings */
31764e48160SLaxman Dewangan 	switch (id) {
318500c524aSXin Xie 	case TPS6586X_ID_SM_0:
319500c524aSXin Xie 		reg = TPS6586X_SM0SL;
320500c524aSXin Xie 		break;
321500c524aSXin Xie 	case TPS6586X_ID_SM_1:
322500c524aSXin Xie 		reg = TPS6586X_SM1SL;
323500c524aSXin Xie 		break;
324500c524aSXin Xie 	default:
3256673d66eSAxel Lin 		dev_err(&pdev->dev, "Only SM0/SM1 can set slew rate\n");
326500c524aSXin Xie 		return -EINVAL;
327500c524aSXin Xie 	}
328500c524aSXin Xie 
329500c524aSXin Xie 	return tps6586x_write(parent, reg,
330500c524aSXin Xie 			setting->slew_rate & TPS6586X_SLEW_RATE_MASK);
331500c524aSXin Xie }
332500c524aSXin Xie 
find_regulator_info(int id,int version)333844a4f0dSStefan Agner static struct tps6586x_regulator *find_regulator_info(int id, int version)
33449610235SMike Rapoport {
33549610235SMike Rapoport 	struct tps6586x_regulator *ri;
336844a4f0dSStefan Agner 	struct tps6586x_regulator *table = NULL;
337844a4f0dSStefan Agner 	int num;
33849610235SMike Rapoport 	int i;
33949610235SMike Rapoport 
340844a4f0dSStefan Agner 	switch (version) {
341844a4f0dSStefan Agner 	case TPS658623:
342669ca030Sryang 	case TPS658624:
343844a4f0dSStefan Agner 		table = tps658623_regulator;
344844a4f0dSStefan Agner 		num = ARRAY_SIZE(tps658623_regulator);
345844a4f0dSStefan Agner 		break;
3466c46ccc8SAlban Bedel 	case TPS658640:
3476c46ccc8SAlban Bedel 	case TPS658640v2:
3486c46ccc8SAlban Bedel 		table = tps658640_regulator;
3496c46ccc8SAlban Bedel 		num = ARRAY_SIZE(tps658640_regulator);
3506c46ccc8SAlban Bedel 		break;
351844a4f0dSStefan Agner 	case TPS658643:
352844a4f0dSStefan Agner 		table = tps658643_regulator;
353844a4f0dSStefan Agner 		num = ARRAY_SIZE(tps658643_regulator);
354844a4f0dSStefan Agner 		break;
355844a4f0dSStefan Agner 	}
356844a4f0dSStefan Agner 
357844a4f0dSStefan Agner 	/* Search version specific table first */
358844a4f0dSStefan Agner 	if (table) {
359844a4f0dSStefan Agner 		for (i = 0; i < num; i++) {
360844a4f0dSStefan Agner 			ri = &table[i];
361844a4f0dSStefan Agner 			if (ri->desc.id == id)
362844a4f0dSStefan Agner 				return ri;
363844a4f0dSStefan Agner 		}
364844a4f0dSStefan Agner 	}
365844a4f0dSStefan Agner 
36649610235SMike Rapoport 	for (i = 0; i < ARRAY_SIZE(tps6586x_regulator); i++) {
36749610235SMike Rapoport 		ri = &tps6586x_regulator[i];
36849610235SMike Rapoport 		if (ri->desc.id == id)
36949610235SMike Rapoport 			return ri;
37049610235SMike Rapoport 	}
37149610235SMike Rapoport 	return NULL;
37249610235SMike Rapoport }
37349610235SMike Rapoport 
37464e48160SLaxman Dewangan #ifdef CONFIG_OF
37564e48160SLaxman Dewangan static struct of_regulator_match tps6586x_matches[] = {
37664e48160SLaxman Dewangan 	{ .name = "sys",     .driver_data = (void *)TPS6586X_ID_SYS     },
37764e48160SLaxman Dewangan 	{ .name = "sm0",     .driver_data = (void *)TPS6586X_ID_SM_0    },
37864e48160SLaxman Dewangan 	{ .name = "sm1",     .driver_data = (void *)TPS6586X_ID_SM_1    },
37964e48160SLaxman Dewangan 	{ .name = "sm2",     .driver_data = (void *)TPS6586X_ID_SM_2    },
38064e48160SLaxman Dewangan 	{ .name = "ldo0",    .driver_data = (void *)TPS6586X_ID_LDO_0   },
38164e48160SLaxman Dewangan 	{ .name = "ldo1",    .driver_data = (void *)TPS6586X_ID_LDO_1   },
38264e48160SLaxman Dewangan 	{ .name = "ldo2",    .driver_data = (void *)TPS6586X_ID_LDO_2   },
38364e48160SLaxman Dewangan 	{ .name = "ldo3",    .driver_data = (void *)TPS6586X_ID_LDO_3   },
38464e48160SLaxman Dewangan 	{ .name = "ldo4",    .driver_data = (void *)TPS6586X_ID_LDO_4   },
38564e48160SLaxman Dewangan 	{ .name = "ldo5",    .driver_data = (void *)TPS6586X_ID_LDO_5   },
38664e48160SLaxman Dewangan 	{ .name = "ldo6",    .driver_data = (void *)TPS6586X_ID_LDO_6   },
38764e48160SLaxman Dewangan 	{ .name = "ldo7",    .driver_data = (void *)TPS6586X_ID_LDO_7   },
38864e48160SLaxman Dewangan 	{ .name = "ldo8",    .driver_data = (void *)TPS6586X_ID_LDO_8   },
38964e48160SLaxman Dewangan 	{ .name = "ldo9",    .driver_data = (void *)TPS6586X_ID_LDO_9   },
39064e48160SLaxman Dewangan 	{ .name = "ldo_rtc", .driver_data = (void *)TPS6586X_ID_LDO_RTC },
39164e48160SLaxman Dewangan };
39264e48160SLaxman Dewangan 
tps6586x_parse_regulator_dt(struct platform_device * pdev,struct of_regulator_match ** tps6586x_reg_matches)39364e48160SLaxman Dewangan static struct tps6586x_platform_data *tps6586x_parse_regulator_dt(
39464e48160SLaxman Dewangan 		struct platform_device *pdev,
39564e48160SLaxman Dewangan 		struct of_regulator_match **tps6586x_reg_matches)
39664e48160SLaxman Dewangan {
39764e48160SLaxman Dewangan 	const unsigned int num = ARRAY_SIZE(tps6586x_matches);
39864e48160SLaxman Dewangan 	struct device_node *np = pdev->dev.parent->of_node;
39964e48160SLaxman Dewangan 	struct device_node *regs;
40064e48160SLaxman Dewangan 	const char *sys_rail = NULL;
40164e48160SLaxman Dewangan 	unsigned int i;
40264e48160SLaxman Dewangan 	struct tps6586x_platform_data *pdata;
40364e48160SLaxman Dewangan 	int err;
40464e48160SLaxman Dewangan 
405712c967fSLaxman Dewangan 	regs = of_get_child_by_name(np, "regulators");
40664e48160SLaxman Dewangan 	if (!regs) {
40764e48160SLaxman Dewangan 		dev_err(&pdev->dev, "regulator node not found\n");
40864e48160SLaxman Dewangan 		return NULL;
40964e48160SLaxman Dewangan 	}
41064e48160SLaxman Dewangan 
41164e48160SLaxman Dewangan 	err = of_regulator_match(&pdev->dev, regs, tps6586x_matches, num);
4120a4cccaaSGuennadi Liakhovetski 	of_node_put(regs);
41364e48160SLaxman Dewangan 	if (err < 0) {
41464e48160SLaxman Dewangan 		dev_err(&pdev->dev, "Regulator match failed, e %d\n", err);
41564e48160SLaxman Dewangan 		return NULL;
41664e48160SLaxman Dewangan 	}
41764e48160SLaxman Dewangan 
41864e48160SLaxman Dewangan 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
41902e90584SSachin Kamat 	if (!pdata)
42064e48160SLaxman Dewangan 		return NULL;
42164e48160SLaxman Dewangan 
42264e48160SLaxman Dewangan 	for (i = 0; i < num; i++) {
423a70f0d02SDaniel Kurtz 		uintptr_t id;
42464e48160SLaxman Dewangan 		if (!tps6586x_matches[i].init_data)
42564e48160SLaxman Dewangan 			continue;
42664e48160SLaxman Dewangan 
42764e48160SLaxman Dewangan 		pdata->reg_init_data[i] = tps6586x_matches[i].init_data;
428a70f0d02SDaniel Kurtz 		id = (uintptr_t)tps6586x_matches[i].driver_data;
42964e48160SLaxman Dewangan 		if (id == TPS6586X_ID_SYS)
43064e48160SLaxman Dewangan 			sys_rail = pdata->reg_init_data[i]->constraints.name;
43164e48160SLaxman Dewangan 
43264e48160SLaxman Dewangan 		if ((id == TPS6586X_ID_LDO_5) || (id == TPS6586X_ID_LDO_RTC))
43364e48160SLaxman Dewangan 			pdata->reg_init_data[i]->supply_regulator = sys_rail;
43464e48160SLaxman Dewangan 	}
43564e48160SLaxman Dewangan 	*tps6586x_reg_matches = tps6586x_matches;
43664e48160SLaxman Dewangan 	return pdata;
43764e48160SLaxman Dewangan }
43864e48160SLaxman Dewangan #else
tps6586x_parse_regulator_dt(struct platform_device * pdev,struct of_regulator_match ** tps6586x_reg_matches)43964e48160SLaxman Dewangan static struct tps6586x_platform_data *tps6586x_parse_regulator_dt(
44064e48160SLaxman Dewangan 		struct platform_device *pdev,
44164e48160SLaxman Dewangan 		struct of_regulator_match **tps6586x_reg_matches)
44264e48160SLaxman Dewangan {
44364e48160SLaxman Dewangan 	*tps6586x_reg_matches = NULL;
44464e48160SLaxman Dewangan 	return NULL;
44564e48160SLaxman Dewangan }
44664e48160SLaxman Dewangan #endif
44764e48160SLaxman Dewangan 
tps6586x_regulator_probe(struct platform_device * pdev)448a5023574SBill Pemberton static int tps6586x_regulator_probe(struct platform_device *pdev)
44949610235SMike Rapoport {
45049610235SMike Rapoport 	struct tps6586x_regulator *ri = NULL;
451c172708dSMark Brown 	struct regulator_config config = { };
452d6fe2c72SAxel Lin 	struct regulator_dev *rdev;
45364e48160SLaxman Dewangan 	struct regulator_init_data *reg_data;
45464e48160SLaxman Dewangan 	struct tps6586x_platform_data *pdata;
45564e48160SLaxman Dewangan 	struct of_regulator_match *tps6586x_reg_matches = NULL;
456844a4f0dSStefan Agner 	int version;
45764e48160SLaxman Dewangan 	int id;
45849610235SMike Rapoport 	int err;
45949610235SMike Rapoport 
46010835600SLaxman Dewangan 	dev_dbg(&pdev->dev, "Probing regulator\n");
46149610235SMike Rapoport 
46264e48160SLaxman Dewangan 	pdata = dev_get_platdata(pdev->dev.parent);
46364e48160SLaxman Dewangan 	if ((!pdata) && (pdev->dev.parent->of_node))
46464e48160SLaxman Dewangan 		pdata = tps6586x_parse_regulator_dt(pdev,
46564e48160SLaxman Dewangan 					&tps6586x_reg_matches);
46664e48160SLaxman Dewangan 
46764e48160SLaxman Dewangan 	if (!pdata) {
46864e48160SLaxman Dewangan 		dev_err(&pdev->dev, "Platform data not available, exiting\n");
46964e48160SLaxman Dewangan 		return -ENODEV;
47064e48160SLaxman Dewangan 	}
47164e48160SLaxman Dewangan 
472844a4f0dSStefan Agner 	version = tps6586x_get_version(pdev->dev.parent);
473844a4f0dSStefan Agner 
47464e48160SLaxman Dewangan 	for (id = 0; id < TPS6586X_ID_MAX_REGULATOR; ++id) {
47564e48160SLaxman Dewangan 		reg_data = pdata->reg_init_data[id];
47664e48160SLaxman Dewangan 
477844a4f0dSStefan Agner 		ri = find_regulator_info(id, version);
478844a4f0dSStefan Agner 
47964e48160SLaxman Dewangan 		if (!ri) {
48049610235SMike Rapoport 			dev_err(&pdev->dev, "invalid regulator ID specified\n");
481884ea557SSachin Kamat 			return -EINVAL;
48249610235SMike Rapoport 		}
48349610235SMike Rapoport 
48449610235SMike Rapoport 		err = tps6586x_regulator_preinit(pdev->dev.parent, ri);
48564e48160SLaxman Dewangan 		if (err) {
48664e48160SLaxman Dewangan 			dev_err(&pdev->dev,
48764e48160SLaxman Dewangan 				"regulator %d preinit failed, e %d\n", id, err);
488884ea557SSachin Kamat 			return err;
48964e48160SLaxman Dewangan 		}
49049610235SMike Rapoport 
4917c7fac30SLaxman Dewangan 		config.dev = pdev->dev.parent;
49264e48160SLaxman Dewangan 		config.init_data = reg_data;
493c172708dSMark Brown 		config.driver_data = ri;
494c172708dSMark Brown 
49564e48160SLaxman Dewangan 		if (tps6586x_reg_matches)
49664e48160SLaxman Dewangan 			config.of_node = tps6586x_reg_matches[id].of_node;
49764e48160SLaxman Dewangan 
498d6fe2c72SAxel Lin 		rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config);
499d6fe2c72SAxel Lin 		if (IS_ERR(rdev)) {
50049610235SMike Rapoport 			dev_err(&pdev->dev, "failed to register regulator %s\n",
50149610235SMike Rapoport 					ri->desc.name);
502d6fe2c72SAxel Lin 			return PTR_ERR(rdev);
50364e48160SLaxman Dewangan 		}
50464e48160SLaxman Dewangan 
50564e48160SLaxman Dewangan 		if (reg_data) {
50664e48160SLaxman Dewangan 			err = tps6586x_regulator_set_slew_rate(pdev, id,
50764e48160SLaxman Dewangan 					reg_data);
50864e48160SLaxman Dewangan 			if (err < 0) {
50964e48160SLaxman Dewangan 				dev_err(&pdev->dev,
51064e48160SLaxman Dewangan 					"Slew rate config failed, e %d\n", err);
511884ea557SSachin Kamat 				return err;
51264e48160SLaxman Dewangan 			}
51364e48160SLaxman Dewangan 		}
51449610235SMike Rapoport 	}
51549610235SMike Rapoport 
516e7973c3cSAxel Lin 	platform_set_drvdata(pdev, rdev);
51764e48160SLaxman Dewangan 	return 0;
51849610235SMike Rapoport }
51949610235SMike Rapoport 
52049610235SMike Rapoport static struct platform_driver tps6586x_regulator_driver = {
52149610235SMike Rapoport 	.driver	= {
522ec8da805SMarc Dietrich 		.name	= "tps6586x-regulator",
523*259b93b2SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
52449610235SMike Rapoport 	},
52549610235SMike Rapoport 	.probe		= tps6586x_regulator_probe,
52649610235SMike Rapoport };
52749610235SMike Rapoport 
tps6586x_regulator_init(void)52849610235SMike Rapoport static int __init tps6586x_regulator_init(void)
52949610235SMike Rapoport {
53049610235SMike Rapoport 	return platform_driver_register(&tps6586x_regulator_driver);
53149610235SMike Rapoport }
53249610235SMike Rapoport subsys_initcall(tps6586x_regulator_init);
53349610235SMike Rapoport 
tps6586x_regulator_exit(void)53449610235SMike Rapoport static void __exit tps6586x_regulator_exit(void)
53549610235SMike Rapoport {
53649610235SMike Rapoport 	platform_driver_unregister(&tps6586x_regulator_driver);
53749610235SMike Rapoport }
53849610235SMike Rapoport module_exit(tps6586x_regulator_exit);
53949610235SMike Rapoport 
54049610235SMike Rapoport MODULE_LICENSE("GPL");
54149610235SMike Rapoport MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
54249610235SMike Rapoport MODULE_DESCRIPTION("Regulator Driver for TI TPS6586X PMIC");
54349610235SMike Rapoport MODULE_ALIAS("platform:tps6586x-regulator");
544