xref: /linux/drivers/regulator/fan53555.c (revision 5365e3df422938e6b34e1afdd2ff1cfc5768290e)
16a1beee2SAxel Lin // SPDX-License-Identifier: GPL-2.0
26a1beee2SAxel Lin //
36a1beee2SAxel Lin // FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver.
46a1beee2SAxel Lin //
56a1beee2SAxel Lin // Supported Part Numbers:
66a1beee2SAxel Lin // FAN53555UC00X/01X/03X/04X/05X
76a1beee2SAxel Lin //
86a1beee2SAxel Lin // Copyright (c) 2012 Marvell Technology Ltd.
96a1beee2SAxel Lin // Yunfan Zhang <yfzhang@marvell.com>
106a1beee2SAxel Lin 
1149d8c599SYunfan Zhang #include <linux/module.h>
1249d8c599SYunfan Zhang #include <linux/param.h>
1349d8c599SYunfan Zhang #include <linux/err.h>
1449d8c599SYunfan Zhang #include <linux/platform_device.h>
1549d8c599SYunfan Zhang #include <linux/regulator/driver.h>
1649d8c599SYunfan Zhang #include <linux/regulator/machine.h>
1791f23d8fSHeiko Stuebner #include <linux/regulator/of_regulator.h>
1891f23d8fSHeiko Stuebner #include <linux/of_device.h>
1949d8c599SYunfan Zhang #include <linux/i2c.h>
2049d8c599SYunfan Zhang #include <linux/slab.h>
2149d8c599SYunfan Zhang #include <linux/regmap.h>
2249d8c599SYunfan Zhang #include <linux/regulator/fan53555.h>
2349d8c599SYunfan Zhang 
2449d8c599SYunfan Zhang /* Voltage setting */
2549d8c599SYunfan Zhang #define FAN53555_VSEL0		0x00
2649d8c599SYunfan Zhang #define FAN53555_VSEL1		0x01
2749d8c599SYunfan Zhang /* Control register */
2849d8c599SYunfan Zhang #define FAN53555_CONTROL	0x02
2949d8c599SYunfan Zhang /* IC Type */
3049d8c599SYunfan Zhang #define FAN53555_ID1		0x03
3149d8c599SYunfan Zhang /* IC mask version */
3249d8c599SYunfan Zhang #define FAN53555_ID2		0x04
3349d8c599SYunfan Zhang /* Monitor register */
3449d8c599SYunfan Zhang #define FAN53555_MONITOR	0x05
3549d8c599SYunfan Zhang 
3649d8c599SYunfan Zhang /* VSEL bit definitions */
3749d8c599SYunfan Zhang #define VSEL_BUCK_EN	(1 << 7)
3849d8c599SYunfan Zhang #define VSEL_MODE		(1 << 6)
3949d8c599SYunfan Zhang /* Chip ID and Verison */
4049d8c599SYunfan Zhang #define DIE_ID		0x0F	/* ID1 */
4149d8c599SYunfan Zhang #define DIE_REV		0x0F	/* ID2 */
4249d8c599SYunfan Zhang /* Control bit definitions */
4349d8c599SYunfan Zhang #define CTL_OUTPUT_DISCHG	(1 << 7)
4449d8c599SYunfan Zhang #define CTL_SLEW_MASK		(0x7 << 4)
4549d8c599SYunfan Zhang #define CTL_SLEW_SHIFT		4
4649d8c599SYunfan Zhang #define CTL_RESET			(1 << 2)
47f2a9eb97SBjorn Andersson #define CTL_MODE_VSEL0_MODE	BIT(0)
48f2a9eb97SBjorn Andersson #define CTL_MODE_VSEL1_MODE	BIT(1)
4949d8c599SYunfan Zhang 
5049d8c599SYunfan Zhang #define FAN53555_NVOLTAGES	64	/* Numbers of voltages */
51f2a9eb97SBjorn Andersson #define FAN53526_NVOLTAGES	128
5249d8c599SYunfan Zhang 
53ee30928aSHeiko Stuebner enum fan53555_vendor {
54f2a9eb97SBjorn Andersson 	FAN53526_VENDOR_FAIRCHILD = 0,
55f2a9eb97SBjorn Andersson 	FAN53555_VENDOR_FAIRCHILD,
56ee30928aSHeiko Stuebner 	FAN53555_VENDOR_SILERGY,
57ee30928aSHeiko Stuebner };
58ee30928aSHeiko Stuebner 
59f2a9eb97SBjorn Andersson enum {
60f2a9eb97SBjorn Andersson 	FAN53526_CHIP_ID_01 = 1,
61f2a9eb97SBjorn Andersson };
62f2a9eb97SBjorn Andersson 
63f2a9eb97SBjorn Andersson enum {
64f2a9eb97SBjorn Andersson 	FAN53526_CHIP_REV_08 = 8,
65f2a9eb97SBjorn Andersson };
66f2a9eb97SBjorn Andersson 
6749d8c599SYunfan Zhang /* IC Type */
6849d8c599SYunfan Zhang enum {
6949d8c599SYunfan Zhang 	FAN53555_CHIP_ID_00 = 0,
7049d8c599SYunfan Zhang 	FAN53555_CHIP_ID_01,
7149d8c599SYunfan Zhang 	FAN53555_CHIP_ID_02,
7249d8c599SYunfan Zhang 	FAN53555_CHIP_ID_03,
7349d8c599SYunfan Zhang 	FAN53555_CHIP_ID_04,
7449d8c599SYunfan Zhang 	FAN53555_CHIP_ID_05,
755e39cf49SWadim Egorov 	FAN53555_CHIP_ID_08 = 8,
7649d8c599SYunfan Zhang };
7749d8c599SYunfan Zhang 
78e57cbb70SWadim Egorov /* IC mask revision */
79e57cbb70SWadim Egorov enum {
80e57cbb70SWadim Egorov 	FAN53555_CHIP_REV_00 = 0x3,
81e57cbb70SWadim Egorov 	FAN53555_CHIP_REV_13 = 0xf,
82e57cbb70SWadim Egorov };
83e57cbb70SWadim Egorov 
84ee30928aSHeiko Stuebner enum {
85ee30928aSHeiko Stuebner 	SILERGY_SYR82X = 8,
86*5365e3dfSVasily Khoruzhick 	SILERGY_SYR83X = 9,
87ee30928aSHeiko Stuebner };
88ee30928aSHeiko Stuebner 
8949d8c599SYunfan Zhang struct fan53555_device_info {
90ee30928aSHeiko Stuebner 	enum fan53555_vendor vendor;
9149d8c599SYunfan Zhang 	struct device *dev;
9249d8c599SYunfan Zhang 	struct regulator_desc desc;
9349d8c599SYunfan Zhang 	struct regulator_init_data *regulator;
9449d8c599SYunfan Zhang 	/* IC Type and Rev */
9549d8c599SYunfan Zhang 	int chip_id;
9649d8c599SYunfan Zhang 	int chip_rev;
9749d8c599SYunfan Zhang 	/* Voltage setting register */
9849d8c599SYunfan Zhang 	unsigned int vol_reg;
9949d8c599SYunfan Zhang 	unsigned int sleep_reg;
10049d8c599SYunfan Zhang 	/* Voltage range and step(linear) */
10149d8c599SYunfan Zhang 	unsigned int vsel_min;
10249d8c599SYunfan Zhang 	unsigned int vsel_step;
103f2a9eb97SBjorn Andersson 	unsigned int vsel_count;
104f2a9eb97SBjorn Andersson 	/* Mode */
105f2a9eb97SBjorn Andersson 	unsigned int mode_reg;
106f2a9eb97SBjorn Andersson 	unsigned int mode_mask;
10749d8c599SYunfan Zhang 	/* Sleep voltage cache */
10849d8c599SYunfan Zhang 	unsigned int sleep_vol_cache;
10949d8c599SYunfan Zhang };
11049d8c599SYunfan Zhang 
11149d8c599SYunfan Zhang static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV)
11249d8c599SYunfan Zhang {
11349d8c599SYunfan Zhang 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
11449d8c599SYunfan Zhang 	int ret;
11549d8c599SYunfan Zhang 
11649d8c599SYunfan Zhang 	if (di->sleep_vol_cache == uV)
11749d8c599SYunfan Zhang 		return 0;
11849d8c599SYunfan Zhang 	ret = regulator_map_voltage_linear(rdev, uV, uV);
11949d8c599SYunfan Zhang 	if (ret < 0)
120145fe1e1SSachin Kamat 		return ret;
121a69929c7SAxel Lin 	ret = regmap_update_bits(rdev->regmap, di->sleep_reg,
122f2a9eb97SBjorn Andersson 				 di->desc.vsel_mask, ret);
12349d8c599SYunfan Zhang 	if (ret < 0)
124145fe1e1SSachin Kamat 		return ret;
12549d8c599SYunfan Zhang 	/* Cache the sleep voltage setting.
12649d8c599SYunfan Zhang 	 * Might not be the real voltage which is rounded */
12749d8c599SYunfan Zhang 	di->sleep_vol_cache = uV;
12849d8c599SYunfan Zhang 
12949d8c599SYunfan Zhang 	return 0;
13049d8c599SYunfan Zhang }
13149d8c599SYunfan Zhang 
132ab7cad33Szhangqing static int fan53555_set_suspend_enable(struct regulator_dev *rdev)
133ab7cad33Szhangqing {
134ab7cad33Szhangqing 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
135ab7cad33Szhangqing 
136a69929c7SAxel Lin 	return regmap_update_bits(rdev->regmap, di->sleep_reg,
137ab7cad33Szhangqing 				  VSEL_BUCK_EN, VSEL_BUCK_EN);
138ab7cad33Szhangqing }
139ab7cad33Szhangqing 
140ab7cad33Szhangqing static int fan53555_set_suspend_disable(struct regulator_dev *rdev)
141ab7cad33Szhangqing {
142ab7cad33Szhangqing 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
143ab7cad33Szhangqing 
144a69929c7SAxel Lin 	return regmap_update_bits(rdev->regmap, di->sleep_reg,
145ab7cad33Szhangqing 				  VSEL_BUCK_EN, 0);
146ab7cad33Szhangqing }
147ab7cad33Szhangqing 
14849d8c599SYunfan Zhang static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode)
14949d8c599SYunfan Zhang {
15049d8c599SYunfan Zhang 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
15149d8c599SYunfan Zhang 
15249d8c599SYunfan Zhang 	switch (mode) {
15349d8c599SYunfan Zhang 	case REGULATOR_MODE_FAST:
154a69929c7SAxel Lin 		regmap_update_bits(rdev->regmap, di->mode_reg,
155f2a9eb97SBjorn Andersson 				   di->mode_mask, di->mode_mask);
15649d8c599SYunfan Zhang 		break;
15749d8c599SYunfan Zhang 	case REGULATOR_MODE_NORMAL:
158a69929c7SAxel Lin 		regmap_update_bits(rdev->regmap, di->vol_reg, di->mode_mask, 0);
15949d8c599SYunfan Zhang 		break;
16049d8c599SYunfan Zhang 	default:
16149d8c599SYunfan Zhang 		return -EINVAL;
16249d8c599SYunfan Zhang 	}
16349d8c599SYunfan Zhang 	return 0;
16449d8c599SYunfan Zhang }
16549d8c599SYunfan Zhang 
16649d8c599SYunfan Zhang static unsigned int fan53555_get_mode(struct regulator_dev *rdev)
16749d8c599SYunfan Zhang {
16849d8c599SYunfan Zhang 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
16949d8c599SYunfan Zhang 	unsigned int val;
17049d8c599SYunfan Zhang 	int ret = 0;
17149d8c599SYunfan Zhang 
172a69929c7SAxel Lin 	ret = regmap_read(rdev->regmap, di->mode_reg, &val);
17349d8c599SYunfan Zhang 	if (ret < 0)
17449d8c599SYunfan Zhang 		return ret;
175f2a9eb97SBjorn Andersson 	if (val & di->mode_mask)
17649d8c599SYunfan Zhang 		return REGULATOR_MODE_FAST;
17749d8c599SYunfan Zhang 	else
17849d8c599SYunfan Zhang 		return REGULATOR_MODE_NORMAL;
17949d8c599SYunfan Zhang }
18049d8c599SYunfan Zhang 
181121b567dSKrzysztof Kozlowski static const int slew_rates[] = {
182dd7e71fbSHeiko Stuebner 	64000,
183dd7e71fbSHeiko Stuebner 	32000,
184dd7e71fbSHeiko Stuebner 	16000,
185dd7e71fbSHeiko Stuebner 	 8000,
186dd7e71fbSHeiko Stuebner 	 4000,
187dd7e71fbSHeiko Stuebner 	 2000,
188dd7e71fbSHeiko Stuebner 	 1000,
189dd7e71fbSHeiko Stuebner 	  500,
190dd7e71fbSHeiko Stuebner };
191dd7e71fbSHeiko Stuebner 
192dd7e71fbSHeiko Stuebner static int fan53555_set_ramp(struct regulator_dev *rdev, int ramp)
193dd7e71fbSHeiko Stuebner {
194dd7e71fbSHeiko Stuebner 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
195dd7e71fbSHeiko Stuebner 	int regval = -1, i;
196dd7e71fbSHeiko Stuebner 
197dd7e71fbSHeiko Stuebner 	for (i = 0; i < ARRAY_SIZE(slew_rates); i++) {
198dd7e71fbSHeiko Stuebner 		if (ramp <= slew_rates[i])
199dd7e71fbSHeiko Stuebner 			regval = i;
200dd7e71fbSHeiko Stuebner 		else
201dd7e71fbSHeiko Stuebner 			break;
202dd7e71fbSHeiko Stuebner 	}
203dd7e71fbSHeiko Stuebner 
204dd7e71fbSHeiko Stuebner 	if (regval < 0) {
205dd7e71fbSHeiko Stuebner 		dev_err(di->dev, "unsupported ramp value %d\n", ramp);
206dd7e71fbSHeiko Stuebner 		return -EINVAL;
207dd7e71fbSHeiko Stuebner 	}
208dd7e71fbSHeiko Stuebner 
209a69929c7SAxel Lin 	return regmap_update_bits(rdev->regmap, FAN53555_CONTROL,
210dd7e71fbSHeiko Stuebner 				  CTL_SLEW_MASK, regval << CTL_SLEW_SHIFT);
211dd7e71fbSHeiko Stuebner }
212dd7e71fbSHeiko Stuebner 
21371880ab2SBhumika Goyal static const struct regulator_ops fan53555_regulator_ops = {
21449d8c599SYunfan Zhang 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
21549d8c599SYunfan Zhang 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
216fda87a42SHeiko Stuebner 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
21749d8c599SYunfan Zhang 	.map_voltage = regulator_map_voltage_linear,
21849d8c599SYunfan Zhang 	.list_voltage = regulator_list_voltage_linear,
21949d8c599SYunfan Zhang 	.set_suspend_voltage = fan53555_set_suspend_voltage,
22049d8c599SYunfan Zhang 	.enable = regulator_enable_regmap,
22149d8c599SYunfan Zhang 	.disable = regulator_disable_regmap,
22249d8c599SYunfan Zhang 	.is_enabled = regulator_is_enabled_regmap,
22349d8c599SYunfan Zhang 	.set_mode = fan53555_set_mode,
22449d8c599SYunfan Zhang 	.get_mode = fan53555_get_mode,
225dd7e71fbSHeiko Stuebner 	.set_ramp_delay = fan53555_set_ramp,
226ab7cad33Szhangqing 	.set_suspend_enable = fan53555_set_suspend_enable,
227ab7cad33Szhangqing 	.set_suspend_disable = fan53555_set_suspend_disable,
22849d8c599SYunfan Zhang };
22949d8c599SYunfan Zhang 
230f2a9eb97SBjorn Andersson static int fan53526_voltages_setup_fairchild(struct fan53555_device_info *di)
231f2a9eb97SBjorn Andersson {
232f2a9eb97SBjorn Andersson 	/* Init voltage range and step */
233f2a9eb97SBjorn Andersson 	switch (di->chip_id) {
234f2a9eb97SBjorn Andersson 	case FAN53526_CHIP_ID_01:
235f2a9eb97SBjorn Andersson 		switch (di->chip_rev) {
236f2a9eb97SBjorn Andersson 		case FAN53526_CHIP_REV_08:
237f2a9eb97SBjorn Andersson 			di->vsel_min = 600000;
238f2a9eb97SBjorn Andersson 			di->vsel_step = 6250;
239f2a9eb97SBjorn Andersson 			break;
240f2a9eb97SBjorn Andersson 		default:
241f2a9eb97SBjorn Andersson 			dev_err(di->dev,
242f2a9eb97SBjorn Andersson 				"Chip ID %d with rev %d not supported!\n",
243f2a9eb97SBjorn Andersson 				di->chip_id, di->chip_rev);
244f2a9eb97SBjorn Andersson 			return -EINVAL;
245f2a9eb97SBjorn Andersson 		}
246f2a9eb97SBjorn Andersson 		break;
247f2a9eb97SBjorn Andersson 	default:
248f2a9eb97SBjorn Andersson 		dev_err(di->dev,
249f2a9eb97SBjorn Andersson 			"Chip ID %d not supported!\n", di->chip_id);
250f2a9eb97SBjorn Andersson 		return -EINVAL;
251f2a9eb97SBjorn Andersson 	}
252f2a9eb97SBjorn Andersson 
253f2a9eb97SBjorn Andersson 	di->vsel_count = FAN53526_NVOLTAGES;
254f2a9eb97SBjorn Andersson 
255f2a9eb97SBjorn Andersson 	return 0;
256f2a9eb97SBjorn Andersson }
257f2a9eb97SBjorn Andersson 
258ee30928aSHeiko Stuebner static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di)
25949d8c599SYunfan Zhang {
26049d8c599SYunfan Zhang 	/* Init voltage range and step */
26149d8c599SYunfan Zhang 	switch (di->chip_id) {
26249d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_00:
263e57cbb70SWadim Egorov 		switch (di->chip_rev) {
264e57cbb70SWadim Egorov 		case FAN53555_CHIP_REV_00:
265e57cbb70SWadim Egorov 			di->vsel_min = 600000;
266e57cbb70SWadim Egorov 			di->vsel_step = 10000;
267e57cbb70SWadim Egorov 			break;
268e57cbb70SWadim Egorov 		case FAN53555_CHIP_REV_13:
269e57cbb70SWadim Egorov 			di->vsel_min = 800000;
270e57cbb70SWadim Egorov 			di->vsel_step = 10000;
271e57cbb70SWadim Egorov 			break;
272e57cbb70SWadim Egorov 		default:
273e57cbb70SWadim Egorov 			dev_err(di->dev,
274e57cbb70SWadim Egorov 				"Chip ID %d with rev %d not supported!\n",
275e57cbb70SWadim Egorov 				di->chip_id, di->chip_rev);
276e57cbb70SWadim Egorov 			return -EINVAL;
277e57cbb70SWadim Egorov 		}
278e57cbb70SWadim Egorov 		break;
27949d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_01:
28049d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_03:
28149d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_05:
2825e39cf49SWadim Egorov 	case FAN53555_CHIP_ID_08:
28349d8c599SYunfan Zhang 		di->vsel_min = 600000;
28449d8c599SYunfan Zhang 		di->vsel_step = 10000;
28549d8c599SYunfan Zhang 		break;
28649d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_04:
28749d8c599SYunfan Zhang 		di->vsel_min = 603000;
28849d8c599SYunfan Zhang 		di->vsel_step = 12826;
28949d8c599SYunfan Zhang 		break;
29049d8c599SYunfan Zhang 	default:
29149d8c599SYunfan Zhang 		dev_err(di->dev,
292ee30928aSHeiko Stuebner 			"Chip ID %d not supported!\n", di->chip_id);
29349d8c599SYunfan Zhang 		return -EINVAL;
29449d8c599SYunfan Zhang 	}
295dd7e71fbSHeiko Stuebner 
296f2a9eb97SBjorn Andersson 	di->vsel_count = FAN53555_NVOLTAGES;
297f2a9eb97SBjorn Andersson 
298dd7e71fbSHeiko Stuebner 	return 0;
29949d8c599SYunfan Zhang }
30049d8c599SYunfan Zhang 
301ee30928aSHeiko Stuebner static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di)
302ee30928aSHeiko Stuebner {
303ee30928aSHeiko Stuebner 	/* Init voltage range and step */
304ee30928aSHeiko Stuebner 	switch (di->chip_id) {
305ee30928aSHeiko Stuebner 	case SILERGY_SYR82X:
306*5365e3dfSVasily Khoruzhick 	case SILERGY_SYR83X:
307ee30928aSHeiko Stuebner 		di->vsel_min = 712500;
308ee30928aSHeiko Stuebner 		di->vsel_step = 12500;
309ee30928aSHeiko Stuebner 		break;
310ee30928aSHeiko Stuebner 	default:
311ee30928aSHeiko Stuebner 		dev_err(di->dev,
312ee30928aSHeiko Stuebner 			"Chip ID %d not supported!\n", di->chip_id);
313ee30928aSHeiko Stuebner 		return -EINVAL;
314ee30928aSHeiko Stuebner 	}
315ee30928aSHeiko Stuebner 
316f2a9eb97SBjorn Andersson 	di->vsel_count = FAN53555_NVOLTAGES;
317f2a9eb97SBjorn Andersson 
318ee30928aSHeiko Stuebner 	return 0;
319ee30928aSHeiko Stuebner }
320ee30928aSHeiko Stuebner 
321ee30928aSHeiko Stuebner /* For 00,01,03,05 options:
322ee30928aSHeiko Stuebner  * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
323ee30928aSHeiko Stuebner  * For 04 option:
324ee30928aSHeiko Stuebner  * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
325ee30928aSHeiko Stuebner  * */
326ee30928aSHeiko Stuebner static int fan53555_device_setup(struct fan53555_device_info *di,
327ee30928aSHeiko Stuebner 				struct fan53555_platform_data *pdata)
328ee30928aSHeiko Stuebner {
329ee30928aSHeiko Stuebner 	int ret = 0;
330ee30928aSHeiko Stuebner 
331ee30928aSHeiko Stuebner 	/* Setup voltage control register */
332ee30928aSHeiko Stuebner 	switch (pdata->sleep_vsel_id) {
333ee30928aSHeiko Stuebner 	case FAN53555_VSEL_ID_0:
334ee30928aSHeiko Stuebner 		di->sleep_reg = FAN53555_VSEL0;
335ee30928aSHeiko Stuebner 		di->vol_reg = FAN53555_VSEL1;
336ee30928aSHeiko Stuebner 		break;
337ee30928aSHeiko Stuebner 	case FAN53555_VSEL_ID_1:
338ee30928aSHeiko Stuebner 		di->sleep_reg = FAN53555_VSEL1;
339ee30928aSHeiko Stuebner 		di->vol_reg = FAN53555_VSEL0;
340ee30928aSHeiko Stuebner 		break;
341ee30928aSHeiko Stuebner 	default:
342ee30928aSHeiko Stuebner 		dev_err(di->dev, "Invalid VSEL ID!\n");
343ee30928aSHeiko Stuebner 		return -EINVAL;
344ee30928aSHeiko Stuebner 	}
345ee30928aSHeiko Stuebner 
346f2a9eb97SBjorn Andersson 	/* Setup mode control register */
347ee30928aSHeiko Stuebner 	switch (di->vendor) {
348f2a9eb97SBjorn Andersson 	case FAN53526_VENDOR_FAIRCHILD:
349f2a9eb97SBjorn Andersson 		di->mode_reg = FAN53555_CONTROL;
350f2a9eb97SBjorn Andersson 
351f2a9eb97SBjorn Andersson 		switch (pdata->sleep_vsel_id) {
352f2a9eb97SBjorn Andersson 		case FAN53555_VSEL_ID_0:
353f2a9eb97SBjorn Andersson 			di->mode_mask = CTL_MODE_VSEL1_MODE;
354f2a9eb97SBjorn Andersson 			break;
355f2a9eb97SBjorn Andersson 		case FAN53555_VSEL_ID_1:
356f2a9eb97SBjorn Andersson 			di->mode_mask = CTL_MODE_VSEL0_MODE;
357f2a9eb97SBjorn Andersson 			break;
358f2a9eb97SBjorn Andersson 		}
359f2a9eb97SBjorn Andersson 		break;
360f2a9eb97SBjorn Andersson 	case FAN53555_VENDOR_FAIRCHILD:
361f2a9eb97SBjorn Andersson 	case FAN53555_VENDOR_SILERGY:
362f2a9eb97SBjorn Andersson 		di->mode_reg = di->vol_reg;
363f2a9eb97SBjorn Andersson 		di->mode_mask = VSEL_MODE;
364f2a9eb97SBjorn Andersson 		break;
365f2a9eb97SBjorn Andersson 	default:
366f2a9eb97SBjorn Andersson 		dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
367f2a9eb97SBjorn Andersson 		return -EINVAL;
368f2a9eb97SBjorn Andersson 	}
369f2a9eb97SBjorn Andersson 
370f2a9eb97SBjorn Andersson 	/* Setup voltage range */
371f2a9eb97SBjorn Andersson 	switch (di->vendor) {
372f2a9eb97SBjorn Andersson 	case FAN53526_VENDOR_FAIRCHILD:
373f2a9eb97SBjorn Andersson 		ret = fan53526_voltages_setup_fairchild(di);
374f2a9eb97SBjorn Andersson 		break;
375ee30928aSHeiko Stuebner 	case FAN53555_VENDOR_FAIRCHILD:
376ee30928aSHeiko Stuebner 		ret = fan53555_voltages_setup_fairchild(di);
377ee30928aSHeiko Stuebner 		break;
378ee30928aSHeiko Stuebner 	case FAN53555_VENDOR_SILERGY:
379ee30928aSHeiko Stuebner 		ret = fan53555_voltages_setup_silergy(di);
380ee30928aSHeiko Stuebner 		break;
381ee30928aSHeiko Stuebner 	default:
382fe230531SAxel Lin 		dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
383ee30928aSHeiko Stuebner 		return -EINVAL;
384ee30928aSHeiko Stuebner 	}
385ee30928aSHeiko Stuebner 
386ee30928aSHeiko Stuebner 	return ret;
387ee30928aSHeiko Stuebner }
388ee30928aSHeiko Stuebner 
38949d8c599SYunfan Zhang static int fan53555_regulator_register(struct fan53555_device_info *di,
39049d8c599SYunfan Zhang 			struct regulator_config *config)
39149d8c599SYunfan Zhang {
39249d8c599SYunfan Zhang 	struct regulator_desc *rdesc = &di->desc;
393a69929c7SAxel Lin 	struct regulator_dev *rdev;
39449d8c599SYunfan Zhang 
39549d8c599SYunfan Zhang 	rdesc->name = "fan53555-reg";
3963415d601SHeiko Stuebner 	rdesc->supply_name = "vin";
39749d8c599SYunfan Zhang 	rdesc->ops = &fan53555_regulator_ops;
39849d8c599SYunfan Zhang 	rdesc->type = REGULATOR_VOLTAGE;
399f2a9eb97SBjorn Andersson 	rdesc->n_voltages = di->vsel_count;
40049d8c599SYunfan Zhang 	rdesc->enable_reg = di->vol_reg;
40149d8c599SYunfan Zhang 	rdesc->enable_mask = VSEL_BUCK_EN;
40249d8c599SYunfan Zhang 	rdesc->min_uV = di->vsel_min;
40349d8c599SYunfan Zhang 	rdesc->uV_step = di->vsel_step;
40449d8c599SYunfan Zhang 	rdesc->vsel_reg = di->vol_reg;
405f2a9eb97SBjorn Andersson 	rdesc->vsel_mask = di->vsel_count - 1;
40649d8c599SYunfan Zhang 	rdesc->owner = THIS_MODULE;
40749d8c599SYunfan Zhang 
408a69929c7SAxel Lin 	rdev = devm_regulator_register(di->dev, &di->desc, config);
409a69929c7SAxel Lin 	return PTR_ERR_OR_ZERO(rdev);
41049d8c599SYunfan Zhang }
41149d8c599SYunfan Zhang 
412121b567dSKrzysztof Kozlowski static const struct regmap_config fan53555_regmap_config = {
41349d8c599SYunfan Zhang 	.reg_bits = 8,
41449d8c599SYunfan Zhang 	.val_bits = 8,
41549d8c599SYunfan Zhang };
41649d8c599SYunfan Zhang 
41791f23d8fSHeiko Stuebner static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev,
418072e78b1SJavier Martinez Canillas 					      struct device_node *np,
419072e78b1SJavier Martinez Canillas 					      const struct regulator_desc *desc)
42091f23d8fSHeiko Stuebner {
42191f23d8fSHeiko Stuebner 	struct fan53555_platform_data *pdata;
42291f23d8fSHeiko Stuebner 	int ret;
42391f23d8fSHeiko Stuebner 	u32 tmp;
42491f23d8fSHeiko Stuebner 
42591f23d8fSHeiko Stuebner 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
42691f23d8fSHeiko Stuebner 	if (!pdata)
42791f23d8fSHeiko Stuebner 		return NULL;
42891f23d8fSHeiko Stuebner 
429072e78b1SJavier Martinez Canillas 	pdata->regulator = of_get_regulator_init_data(dev, np, desc);
43091f23d8fSHeiko Stuebner 
43191f23d8fSHeiko Stuebner 	ret = of_property_read_u32(np, "fcs,suspend-voltage-selector",
43291f23d8fSHeiko Stuebner 				   &tmp);
43391f23d8fSHeiko Stuebner 	if (!ret)
43491f23d8fSHeiko Stuebner 		pdata->sleep_vsel_id = tmp;
43591f23d8fSHeiko Stuebner 
43691f23d8fSHeiko Stuebner 	return pdata;
43791f23d8fSHeiko Stuebner }
43891f23d8fSHeiko Stuebner 
43991f23d8fSHeiko Stuebner static const struct of_device_id fan53555_dt_ids[] = {
44091f23d8fSHeiko Stuebner 	{
441f2a9eb97SBjorn Andersson 		.compatible = "fcs,fan53526",
442f2a9eb97SBjorn Andersson 		.data = (void *)FAN53526_VENDOR_FAIRCHILD,
443f2a9eb97SBjorn Andersson 	}, {
44491f23d8fSHeiko Stuebner 		.compatible = "fcs,fan53555",
445ee30928aSHeiko Stuebner 		.data = (void *)FAN53555_VENDOR_FAIRCHILD
446ee30928aSHeiko Stuebner 	}, {
447ee30928aSHeiko Stuebner 		.compatible = "silergy,syr827",
448ee30928aSHeiko Stuebner 		.data = (void *)FAN53555_VENDOR_SILERGY,
449ee30928aSHeiko Stuebner 	}, {
450ee30928aSHeiko Stuebner 		.compatible = "silergy,syr828",
451ee30928aSHeiko Stuebner 		.data = (void *)FAN53555_VENDOR_SILERGY,
45291f23d8fSHeiko Stuebner 	},
45391f23d8fSHeiko Stuebner 	{ }
45491f23d8fSHeiko Stuebner };
45591f23d8fSHeiko Stuebner MODULE_DEVICE_TABLE(of, fan53555_dt_ids);
45691f23d8fSHeiko Stuebner 
457a5023574SBill Pemberton static int fan53555_regulator_probe(struct i2c_client *client,
45849d8c599SYunfan Zhang 				const struct i2c_device_id *id)
45949d8c599SYunfan Zhang {
46091f23d8fSHeiko Stuebner 	struct device_node *np = client->dev.of_node;
46149d8c599SYunfan Zhang 	struct fan53555_device_info *di;
46249d8c599SYunfan Zhang 	struct fan53555_platform_data *pdata;
46349d8c599SYunfan Zhang 	struct regulator_config config = { };
464a69929c7SAxel Lin 	struct regmap *regmap;
46549d8c599SYunfan Zhang 	unsigned int val;
46649d8c599SYunfan Zhang 	int ret;
46749d8c599SYunfan Zhang 
468072e78b1SJavier Martinez Canillas 	di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info),
469072e78b1SJavier Martinez Canillas 					GFP_KERNEL);
470072e78b1SJavier Martinez Canillas 	if (!di)
471072e78b1SJavier Martinez Canillas 		return -ENOMEM;
472072e78b1SJavier Martinez Canillas 
473dff91d0bSJingoo Han 	pdata = dev_get_platdata(&client->dev);
47491f23d8fSHeiko Stuebner 	if (!pdata)
475072e78b1SJavier Martinez Canillas 		pdata = fan53555_parse_dt(&client->dev, np, &di->desc);
47691f23d8fSHeiko Stuebner 
47749d8c599SYunfan Zhang 	if (!pdata || !pdata->regulator) {
47849d8c599SYunfan Zhang 		dev_err(&client->dev, "Platform data not found!\n");
47949d8c599SYunfan Zhang 		return -ENODEV;
48049d8c599SYunfan Zhang 	}
48149d8c599SYunfan Zhang 
482e13426bfSAxel Lin 	di->regulator = pdata->regulator;
483ee30928aSHeiko Stuebner 	if (client->dev.of_node) {
484d110e3e9SJisheng Zhang 		di->vendor =
485d110e3e9SJisheng Zhang 			(unsigned long)of_device_get_match_data(&client->dev);
486ee30928aSHeiko Stuebner 	} else {
487dd7e71fbSHeiko Stuebner 		/* if no ramp constraint set, get the pdata ramp_delay */
488dd7e71fbSHeiko Stuebner 		if (!di->regulator->constraints.ramp_delay) {
48987919e0cSAxel Lin 			if (pdata->slew_rate >= ARRAY_SIZE(slew_rates)) {
49087919e0cSAxel Lin 				dev_err(&client->dev, "Invalid slew_rate\n");
49187919e0cSAxel Lin 				return -EINVAL;
49287919e0cSAxel Lin 			}
493dd7e71fbSHeiko Stuebner 
49491f23d8fSHeiko Stuebner 			di->regulator->constraints.ramp_delay
49587919e0cSAxel Lin 					= slew_rates[pdata->slew_rate];
49691f23d8fSHeiko Stuebner 		}
497ee30928aSHeiko Stuebner 
498ee30928aSHeiko Stuebner 		di->vendor = id->driver_data;
499dd7e71fbSHeiko Stuebner 	}
500dd7e71fbSHeiko Stuebner 
501a69929c7SAxel Lin 	regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config);
502a69929c7SAxel Lin 	if (IS_ERR(regmap)) {
50349d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to allocate regmap!\n");
504a69929c7SAxel Lin 		return PTR_ERR(regmap);
50549d8c599SYunfan Zhang 	}
50649d8c599SYunfan Zhang 	di->dev = &client->dev;
50749d8c599SYunfan Zhang 	i2c_set_clientdata(client, di);
50849d8c599SYunfan Zhang 	/* Get chip ID */
509a69929c7SAxel Lin 	ret = regmap_read(regmap, FAN53555_ID1, &val);
51049d8c599SYunfan Zhang 	if (ret < 0) {
51149d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to get chip ID!\n");
512145fe1e1SSachin Kamat 		return ret;
51349d8c599SYunfan Zhang 	}
51449d8c599SYunfan Zhang 	di->chip_id = val & DIE_ID;
51549d8c599SYunfan Zhang 	/* Get chip revision */
516a69929c7SAxel Lin 	ret = regmap_read(regmap, FAN53555_ID2, &val);
51749d8c599SYunfan Zhang 	if (ret < 0) {
51849d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to get chip Rev!\n");
519145fe1e1SSachin Kamat 		return ret;
52049d8c599SYunfan Zhang 	}
52149d8c599SYunfan Zhang 	di->chip_rev = val & DIE_REV;
52249d8c599SYunfan Zhang 	dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n",
52349d8c599SYunfan Zhang 				di->chip_id, di->chip_rev);
52449d8c599SYunfan Zhang 	/* Device init */
52549d8c599SYunfan Zhang 	ret = fan53555_device_setup(di, pdata);
52649d8c599SYunfan Zhang 	if (ret < 0) {
52749d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to setup device!\n");
52849d8c599SYunfan Zhang 		return ret;
52949d8c599SYunfan Zhang 	}
53049d8c599SYunfan Zhang 	/* Register regulator */
53149d8c599SYunfan Zhang 	config.dev = di->dev;
53249d8c599SYunfan Zhang 	config.init_data = di->regulator;
533a69929c7SAxel Lin 	config.regmap = regmap;
53449d8c599SYunfan Zhang 	config.driver_data = di;
53591f23d8fSHeiko Stuebner 	config.of_node = np;
53691f23d8fSHeiko Stuebner 
53749d8c599SYunfan Zhang 	ret = fan53555_regulator_register(di, &config);
53849d8c599SYunfan Zhang 	if (ret < 0)
53949d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to register regulator!\n");
54049d8c599SYunfan Zhang 	return ret;
54149d8c599SYunfan Zhang 
54249d8c599SYunfan Zhang }
54349d8c599SYunfan Zhang 
54449d8c599SYunfan Zhang static const struct i2c_device_id fan53555_id[] = {
545ee30928aSHeiko Stuebner 	{
546f2a9eb97SBjorn Andersson 		.name = "fan53526",
547f2a9eb97SBjorn Andersson 		.driver_data = FAN53526_VENDOR_FAIRCHILD
548f2a9eb97SBjorn Andersson 	}, {
549ee30928aSHeiko Stuebner 		.name = "fan53555",
550ee30928aSHeiko Stuebner 		.driver_data = FAN53555_VENDOR_FAIRCHILD
551ee30928aSHeiko Stuebner 	}, {
552fc1111b8SGuillaume Tucker 		.name = "syr827",
553fc1111b8SGuillaume Tucker 		.driver_data = FAN53555_VENDOR_SILERGY
554fc1111b8SGuillaume Tucker 	}, {
555fc1111b8SGuillaume Tucker 		.name = "syr828",
556ee30928aSHeiko Stuebner 		.driver_data = FAN53555_VENDOR_SILERGY
557ee30928aSHeiko Stuebner 	},
55849d8c599SYunfan Zhang 	{ },
55949d8c599SYunfan Zhang };
560e80c47bdSJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, fan53555_id);
56149d8c599SYunfan Zhang 
56249d8c599SYunfan Zhang static struct i2c_driver fan53555_regulator_driver = {
56349d8c599SYunfan Zhang 	.driver = {
56449d8c599SYunfan Zhang 		.name = "fan53555-regulator",
56591f23d8fSHeiko Stuebner 		.of_match_table = of_match_ptr(fan53555_dt_ids),
56649d8c599SYunfan Zhang 	},
56749d8c599SYunfan Zhang 	.probe = fan53555_regulator_probe,
56849d8c599SYunfan Zhang 	.id_table = fan53555_id,
56949d8c599SYunfan Zhang };
57049d8c599SYunfan Zhang 
57149d8c599SYunfan Zhang module_i2c_driver(fan53555_regulator_driver);
57249d8c599SYunfan Zhang 
57349d8c599SYunfan Zhang MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
57449d8c599SYunfan Zhang MODULE_DESCRIPTION("FAN53555 regulator driver");
57549d8c599SYunfan Zhang MODULE_LICENSE("GPL v2");
576