xref: /linux/drivers/regulator/fan53555.c (revision f2a9eb975ab2e848af68db26646793f5d9d097e8)
149d8c599SYunfan Zhang /*
249d8c599SYunfan Zhang  * FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver.
349d8c599SYunfan Zhang  *
449d8c599SYunfan Zhang  * Supported Part Numbers:
549d8c599SYunfan Zhang  * FAN53555UC00X/01X/03X/04X/05X
649d8c599SYunfan Zhang  *
749d8c599SYunfan Zhang  * Copyright (c) 2012 Marvell Technology Ltd.
849d8c599SYunfan Zhang  * Yunfan Zhang <yfzhang@marvell.com>
949d8c599SYunfan Zhang  *
1049d8c599SYunfan Zhang  * This package is free software; you can redistribute it and/or modify
1149d8c599SYunfan Zhang  * it under the terms of the GNU General Public License version 2 as
1249d8c599SYunfan Zhang  * published by the Free Software Foundation.
1349d8c599SYunfan Zhang  *
1449d8c599SYunfan Zhang  */
1549d8c599SYunfan Zhang #include <linux/module.h>
1649d8c599SYunfan Zhang #include <linux/param.h>
1749d8c599SYunfan Zhang #include <linux/err.h>
1849d8c599SYunfan Zhang #include <linux/platform_device.h>
1949d8c599SYunfan Zhang #include <linux/regulator/driver.h>
2049d8c599SYunfan Zhang #include <linux/regulator/machine.h>
2191f23d8fSHeiko Stuebner #include <linux/regulator/of_regulator.h>
2291f23d8fSHeiko Stuebner #include <linux/of_device.h>
2349d8c599SYunfan Zhang #include <linux/i2c.h>
2449d8c599SYunfan Zhang #include <linux/slab.h>
2549d8c599SYunfan Zhang #include <linux/regmap.h>
2649d8c599SYunfan Zhang #include <linux/regulator/fan53555.h>
2749d8c599SYunfan Zhang 
2849d8c599SYunfan Zhang /* Voltage setting */
2949d8c599SYunfan Zhang #define FAN53555_VSEL0		0x00
3049d8c599SYunfan Zhang #define FAN53555_VSEL1		0x01
3149d8c599SYunfan Zhang /* Control register */
3249d8c599SYunfan Zhang #define FAN53555_CONTROL	0x02
3349d8c599SYunfan Zhang /* IC Type */
3449d8c599SYunfan Zhang #define FAN53555_ID1		0x03
3549d8c599SYunfan Zhang /* IC mask version */
3649d8c599SYunfan Zhang #define FAN53555_ID2		0x04
3749d8c599SYunfan Zhang /* Monitor register */
3849d8c599SYunfan Zhang #define FAN53555_MONITOR	0x05
3949d8c599SYunfan Zhang 
4049d8c599SYunfan Zhang /* VSEL bit definitions */
4149d8c599SYunfan Zhang #define VSEL_BUCK_EN	(1 << 7)
4249d8c599SYunfan Zhang #define VSEL_MODE		(1 << 6)
4349d8c599SYunfan Zhang /* Chip ID and Verison */
4449d8c599SYunfan Zhang #define DIE_ID		0x0F	/* ID1 */
4549d8c599SYunfan Zhang #define DIE_REV		0x0F	/* ID2 */
4649d8c599SYunfan Zhang /* Control bit definitions */
4749d8c599SYunfan Zhang #define CTL_OUTPUT_DISCHG	(1 << 7)
4849d8c599SYunfan Zhang #define CTL_SLEW_MASK		(0x7 << 4)
4949d8c599SYunfan Zhang #define CTL_SLEW_SHIFT		4
5049d8c599SYunfan Zhang #define CTL_RESET			(1 << 2)
51*f2a9eb97SBjorn Andersson #define CTL_MODE_VSEL0_MODE	BIT(0)
52*f2a9eb97SBjorn Andersson #define CTL_MODE_VSEL1_MODE	BIT(1)
5349d8c599SYunfan Zhang 
5449d8c599SYunfan Zhang #define FAN53555_NVOLTAGES	64	/* Numbers of voltages */
55*f2a9eb97SBjorn Andersson #define FAN53526_NVOLTAGES	128
5649d8c599SYunfan Zhang 
57ee30928aSHeiko Stuebner enum fan53555_vendor {
58*f2a9eb97SBjorn Andersson 	FAN53526_VENDOR_FAIRCHILD = 0,
59*f2a9eb97SBjorn Andersson 	FAN53555_VENDOR_FAIRCHILD,
60ee30928aSHeiko Stuebner 	FAN53555_VENDOR_SILERGY,
61ee30928aSHeiko Stuebner };
62ee30928aSHeiko Stuebner 
63*f2a9eb97SBjorn Andersson enum {
64*f2a9eb97SBjorn Andersson 	FAN53526_CHIP_ID_01 = 1,
65*f2a9eb97SBjorn Andersson };
66*f2a9eb97SBjorn Andersson 
67*f2a9eb97SBjorn Andersson enum {
68*f2a9eb97SBjorn Andersson 	FAN53526_CHIP_REV_08 = 8,
69*f2a9eb97SBjorn Andersson };
70*f2a9eb97SBjorn Andersson 
7149d8c599SYunfan Zhang /* IC Type */
7249d8c599SYunfan Zhang enum {
7349d8c599SYunfan Zhang 	FAN53555_CHIP_ID_00 = 0,
7449d8c599SYunfan Zhang 	FAN53555_CHIP_ID_01,
7549d8c599SYunfan Zhang 	FAN53555_CHIP_ID_02,
7649d8c599SYunfan Zhang 	FAN53555_CHIP_ID_03,
7749d8c599SYunfan Zhang 	FAN53555_CHIP_ID_04,
7849d8c599SYunfan Zhang 	FAN53555_CHIP_ID_05,
795e39cf49SWadim Egorov 	FAN53555_CHIP_ID_08 = 8,
8049d8c599SYunfan Zhang };
8149d8c599SYunfan Zhang 
82e57cbb70SWadim Egorov /* IC mask revision */
83e57cbb70SWadim Egorov enum {
84e57cbb70SWadim Egorov 	FAN53555_CHIP_REV_00 = 0x3,
85e57cbb70SWadim Egorov 	FAN53555_CHIP_REV_13 = 0xf,
86e57cbb70SWadim Egorov };
87e57cbb70SWadim Egorov 
88ee30928aSHeiko Stuebner enum {
89ee30928aSHeiko Stuebner 	SILERGY_SYR82X = 8,
90ee30928aSHeiko Stuebner };
91ee30928aSHeiko Stuebner 
9249d8c599SYunfan Zhang struct fan53555_device_info {
93ee30928aSHeiko Stuebner 	enum fan53555_vendor vendor;
9449d8c599SYunfan Zhang 	struct regmap *regmap;
9549d8c599SYunfan Zhang 	struct device *dev;
9649d8c599SYunfan Zhang 	struct regulator_desc desc;
9749d8c599SYunfan Zhang 	struct regulator_dev *rdev;
9849d8c599SYunfan Zhang 	struct regulator_init_data *regulator;
9949d8c599SYunfan Zhang 	/* IC Type and Rev */
10049d8c599SYunfan Zhang 	int chip_id;
10149d8c599SYunfan Zhang 	int chip_rev;
10249d8c599SYunfan Zhang 	/* Voltage setting register */
10349d8c599SYunfan Zhang 	unsigned int vol_reg;
10449d8c599SYunfan Zhang 	unsigned int sleep_reg;
10549d8c599SYunfan Zhang 	/* Voltage range and step(linear) */
10649d8c599SYunfan Zhang 	unsigned int vsel_min;
10749d8c599SYunfan Zhang 	unsigned int vsel_step;
108*f2a9eb97SBjorn Andersson 	unsigned int vsel_count;
10949d8c599SYunfan Zhang 	/* Voltage slew rate limiting */
11049d8c599SYunfan Zhang 	unsigned int slew_rate;
111*f2a9eb97SBjorn Andersson 	/* Mode */
112*f2a9eb97SBjorn Andersson 	unsigned int mode_reg;
113*f2a9eb97SBjorn Andersson 	unsigned int mode_mask;
11449d8c599SYunfan Zhang 	/* Sleep voltage cache */
11549d8c599SYunfan Zhang 	unsigned int sleep_vol_cache;
11649d8c599SYunfan Zhang };
11749d8c599SYunfan Zhang 
11849d8c599SYunfan Zhang static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV)
11949d8c599SYunfan Zhang {
12049d8c599SYunfan Zhang 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
12149d8c599SYunfan Zhang 	int ret;
12249d8c599SYunfan Zhang 
12349d8c599SYunfan Zhang 	if (di->sleep_vol_cache == uV)
12449d8c599SYunfan Zhang 		return 0;
12549d8c599SYunfan Zhang 	ret = regulator_map_voltage_linear(rdev, uV, uV);
12649d8c599SYunfan Zhang 	if (ret < 0)
127145fe1e1SSachin Kamat 		return ret;
12849d8c599SYunfan Zhang 	ret = regmap_update_bits(di->regmap, di->sleep_reg,
129*f2a9eb97SBjorn Andersson 				 di->desc.vsel_mask, ret);
13049d8c599SYunfan Zhang 	if (ret < 0)
131145fe1e1SSachin Kamat 		return ret;
13249d8c599SYunfan Zhang 	/* Cache the sleep voltage setting.
13349d8c599SYunfan Zhang 	 * Might not be the real voltage which is rounded */
13449d8c599SYunfan Zhang 	di->sleep_vol_cache = uV;
13549d8c599SYunfan Zhang 
13649d8c599SYunfan Zhang 	return 0;
13749d8c599SYunfan Zhang }
13849d8c599SYunfan Zhang 
139ab7cad33Szhangqing static int fan53555_set_suspend_enable(struct regulator_dev *rdev)
140ab7cad33Szhangqing {
141ab7cad33Szhangqing 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
142ab7cad33Szhangqing 
143ab7cad33Szhangqing 	return regmap_update_bits(di->regmap, di->sleep_reg,
144ab7cad33Szhangqing 				  VSEL_BUCK_EN, VSEL_BUCK_EN);
145ab7cad33Szhangqing }
146ab7cad33Szhangqing 
147ab7cad33Szhangqing static int fan53555_set_suspend_disable(struct regulator_dev *rdev)
148ab7cad33Szhangqing {
149ab7cad33Szhangqing 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
150ab7cad33Szhangqing 
151ab7cad33Szhangqing 	return regmap_update_bits(di->regmap, di->sleep_reg,
152ab7cad33Szhangqing 				  VSEL_BUCK_EN, 0);
153ab7cad33Szhangqing }
154ab7cad33Szhangqing 
15549d8c599SYunfan Zhang static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode)
15649d8c599SYunfan Zhang {
15749d8c599SYunfan Zhang 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
15849d8c599SYunfan Zhang 
15949d8c599SYunfan Zhang 	switch (mode) {
16049d8c599SYunfan Zhang 	case REGULATOR_MODE_FAST:
161*f2a9eb97SBjorn Andersson 		regmap_update_bits(di->regmap, di->mode_reg,
162*f2a9eb97SBjorn Andersson 				   di->mode_mask, di->mode_mask);
16349d8c599SYunfan Zhang 		break;
16449d8c599SYunfan Zhang 	case REGULATOR_MODE_NORMAL:
165*f2a9eb97SBjorn Andersson 		regmap_update_bits(di->regmap, di->vol_reg, di->mode_mask, 0);
16649d8c599SYunfan Zhang 		break;
16749d8c599SYunfan Zhang 	default:
16849d8c599SYunfan Zhang 		return -EINVAL;
16949d8c599SYunfan Zhang 	}
17049d8c599SYunfan Zhang 	return 0;
17149d8c599SYunfan Zhang }
17249d8c599SYunfan Zhang 
17349d8c599SYunfan Zhang static unsigned int fan53555_get_mode(struct regulator_dev *rdev)
17449d8c599SYunfan Zhang {
17549d8c599SYunfan Zhang 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
17649d8c599SYunfan Zhang 	unsigned int val;
17749d8c599SYunfan Zhang 	int ret = 0;
17849d8c599SYunfan Zhang 
179*f2a9eb97SBjorn Andersson 	ret = regmap_read(di->regmap, di->mode_reg, &val);
18049d8c599SYunfan Zhang 	if (ret < 0)
18149d8c599SYunfan Zhang 		return ret;
182*f2a9eb97SBjorn Andersson 	if (val & di->mode_mask)
18349d8c599SYunfan Zhang 		return REGULATOR_MODE_FAST;
18449d8c599SYunfan Zhang 	else
18549d8c599SYunfan Zhang 		return REGULATOR_MODE_NORMAL;
18649d8c599SYunfan Zhang }
18749d8c599SYunfan Zhang 
188121b567dSKrzysztof Kozlowski static const int slew_rates[] = {
189dd7e71fbSHeiko Stuebner 	64000,
190dd7e71fbSHeiko Stuebner 	32000,
191dd7e71fbSHeiko Stuebner 	16000,
192dd7e71fbSHeiko Stuebner 	 8000,
193dd7e71fbSHeiko Stuebner 	 4000,
194dd7e71fbSHeiko Stuebner 	 2000,
195dd7e71fbSHeiko Stuebner 	 1000,
196dd7e71fbSHeiko Stuebner 	  500,
197dd7e71fbSHeiko Stuebner };
198dd7e71fbSHeiko Stuebner 
199dd7e71fbSHeiko Stuebner static int fan53555_set_ramp(struct regulator_dev *rdev, int ramp)
200dd7e71fbSHeiko Stuebner {
201dd7e71fbSHeiko Stuebner 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
202dd7e71fbSHeiko Stuebner 	int regval = -1, i;
203dd7e71fbSHeiko Stuebner 
204dd7e71fbSHeiko Stuebner 	for (i = 0; i < ARRAY_SIZE(slew_rates); i++) {
205dd7e71fbSHeiko Stuebner 		if (ramp <= slew_rates[i])
206dd7e71fbSHeiko Stuebner 			regval = i;
207dd7e71fbSHeiko Stuebner 		else
208dd7e71fbSHeiko Stuebner 			break;
209dd7e71fbSHeiko Stuebner 	}
210dd7e71fbSHeiko Stuebner 
211dd7e71fbSHeiko Stuebner 	if (regval < 0) {
212dd7e71fbSHeiko Stuebner 		dev_err(di->dev, "unsupported ramp value %d\n", ramp);
213dd7e71fbSHeiko Stuebner 		return -EINVAL;
214dd7e71fbSHeiko Stuebner 	}
215dd7e71fbSHeiko Stuebner 
216dd7e71fbSHeiko Stuebner 	return regmap_update_bits(di->regmap, FAN53555_CONTROL,
217dd7e71fbSHeiko Stuebner 				  CTL_SLEW_MASK, regval << CTL_SLEW_SHIFT);
218dd7e71fbSHeiko Stuebner }
219dd7e71fbSHeiko Stuebner 
22071880ab2SBhumika Goyal static const struct regulator_ops fan53555_regulator_ops = {
22149d8c599SYunfan Zhang 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
22249d8c599SYunfan Zhang 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
223fda87a42SHeiko Stuebner 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
22449d8c599SYunfan Zhang 	.map_voltage = regulator_map_voltage_linear,
22549d8c599SYunfan Zhang 	.list_voltage = regulator_list_voltage_linear,
22649d8c599SYunfan Zhang 	.set_suspend_voltage = fan53555_set_suspend_voltage,
22749d8c599SYunfan Zhang 	.enable = regulator_enable_regmap,
22849d8c599SYunfan Zhang 	.disable = regulator_disable_regmap,
22949d8c599SYunfan Zhang 	.is_enabled = regulator_is_enabled_regmap,
23049d8c599SYunfan Zhang 	.set_mode = fan53555_set_mode,
23149d8c599SYunfan Zhang 	.get_mode = fan53555_get_mode,
232dd7e71fbSHeiko Stuebner 	.set_ramp_delay = fan53555_set_ramp,
233ab7cad33Szhangqing 	.set_suspend_enable = fan53555_set_suspend_enable,
234ab7cad33Szhangqing 	.set_suspend_disable = fan53555_set_suspend_disable,
23549d8c599SYunfan Zhang };
23649d8c599SYunfan Zhang 
237*f2a9eb97SBjorn Andersson static int fan53526_voltages_setup_fairchild(struct fan53555_device_info *di)
238*f2a9eb97SBjorn Andersson {
239*f2a9eb97SBjorn Andersson 	/* Init voltage range and step */
240*f2a9eb97SBjorn Andersson 	switch (di->chip_id) {
241*f2a9eb97SBjorn Andersson 	case FAN53526_CHIP_ID_01:
242*f2a9eb97SBjorn Andersson 		switch (di->chip_rev) {
243*f2a9eb97SBjorn Andersson 		case FAN53526_CHIP_REV_08:
244*f2a9eb97SBjorn Andersson 			di->vsel_min = 600000;
245*f2a9eb97SBjorn Andersson 			di->vsel_step = 6250;
246*f2a9eb97SBjorn Andersson 			break;
247*f2a9eb97SBjorn Andersson 		default:
248*f2a9eb97SBjorn Andersson 			dev_err(di->dev,
249*f2a9eb97SBjorn Andersson 				"Chip ID %d with rev %d not supported!\n",
250*f2a9eb97SBjorn Andersson 				di->chip_id, di->chip_rev);
251*f2a9eb97SBjorn Andersson 			return -EINVAL;
252*f2a9eb97SBjorn Andersson 		}
253*f2a9eb97SBjorn Andersson 		break;
254*f2a9eb97SBjorn Andersson 	default:
255*f2a9eb97SBjorn Andersson 		dev_err(di->dev,
256*f2a9eb97SBjorn Andersson 			"Chip ID %d not supported!\n", di->chip_id);
257*f2a9eb97SBjorn Andersson 		return -EINVAL;
258*f2a9eb97SBjorn Andersson 	}
259*f2a9eb97SBjorn Andersson 
260*f2a9eb97SBjorn Andersson 	di->vsel_count = FAN53526_NVOLTAGES;
261*f2a9eb97SBjorn Andersson 
262*f2a9eb97SBjorn Andersson 	return 0;
263*f2a9eb97SBjorn Andersson }
264*f2a9eb97SBjorn Andersson 
265ee30928aSHeiko Stuebner static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di)
26649d8c599SYunfan Zhang {
26749d8c599SYunfan Zhang 	/* Init voltage range and step */
26849d8c599SYunfan Zhang 	switch (di->chip_id) {
26949d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_00:
270e57cbb70SWadim Egorov 		switch (di->chip_rev) {
271e57cbb70SWadim Egorov 		case FAN53555_CHIP_REV_00:
272e57cbb70SWadim Egorov 			di->vsel_min = 600000;
273e57cbb70SWadim Egorov 			di->vsel_step = 10000;
274e57cbb70SWadim Egorov 			break;
275e57cbb70SWadim Egorov 		case FAN53555_CHIP_REV_13:
276e57cbb70SWadim Egorov 			di->vsel_min = 800000;
277e57cbb70SWadim Egorov 			di->vsel_step = 10000;
278e57cbb70SWadim Egorov 			break;
279e57cbb70SWadim Egorov 		default:
280e57cbb70SWadim Egorov 			dev_err(di->dev,
281e57cbb70SWadim Egorov 				"Chip ID %d with rev %d not supported!\n",
282e57cbb70SWadim Egorov 				di->chip_id, di->chip_rev);
283e57cbb70SWadim Egorov 			return -EINVAL;
284e57cbb70SWadim Egorov 		}
285e57cbb70SWadim Egorov 		break;
28649d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_01:
28749d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_03:
28849d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_05:
2895e39cf49SWadim Egorov 	case FAN53555_CHIP_ID_08:
29049d8c599SYunfan Zhang 		di->vsel_min = 600000;
29149d8c599SYunfan Zhang 		di->vsel_step = 10000;
29249d8c599SYunfan Zhang 		break;
29349d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_04:
29449d8c599SYunfan Zhang 		di->vsel_min = 603000;
29549d8c599SYunfan Zhang 		di->vsel_step = 12826;
29649d8c599SYunfan Zhang 		break;
29749d8c599SYunfan Zhang 	default:
29849d8c599SYunfan Zhang 		dev_err(di->dev,
299ee30928aSHeiko Stuebner 			"Chip ID %d not supported!\n", di->chip_id);
30049d8c599SYunfan Zhang 		return -EINVAL;
30149d8c599SYunfan Zhang 	}
302dd7e71fbSHeiko Stuebner 
303*f2a9eb97SBjorn Andersson 	di->vsel_count = FAN53555_NVOLTAGES;
304*f2a9eb97SBjorn Andersson 
305dd7e71fbSHeiko Stuebner 	return 0;
30649d8c599SYunfan Zhang }
30749d8c599SYunfan Zhang 
308ee30928aSHeiko Stuebner static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di)
309ee30928aSHeiko Stuebner {
310ee30928aSHeiko Stuebner 	/* Init voltage range and step */
311ee30928aSHeiko Stuebner 	switch (di->chip_id) {
312ee30928aSHeiko Stuebner 	case SILERGY_SYR82X:
313ee30928aSHeiko Stuebner 		di->vsel_min = 712500;
314ee30928aSHeiko Stuebner 		di->vsel_step = 12500;
315ee30928aSHeiko Stuebner 		break;
316ee30928aSHeiko Stuebner 	default:
317ee30928aSHeiko Stuebner 		dev_err(di->dev,
318ee30928aSHeiko Stuebner 			"Chip ID %d not supported!\n", di->chip_id);
319ee30928aSHeiko Stuebner 		return -EINVAL;
320ee30928aSHeiko Stuebner 	}
321ee30928aSHeiko Stuebner 
322*f2a9eb97SBjorn Andersson 	di->vsel_count = FAN53555_NVOLTAGES;
323*f2a9eb97SBjorn Andersson 
324ee30928aSHeiko Stuebner 	return 0;
325ee30928aSHeiko Stuebner }
326ee30928aSHeiko Stuebner 
327ee30928aSHeiko Stuebner /* For 00,01,03,05 options:
328ee30928aSHeiko Stuebner  * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
329ee30928aSHeiko Stuebner  * For 04 option:
330ee30928aSHeiko Stuebner  * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
331ee30928aSHeiko Stuebner  * */
332ee30928aSHeiko Stuebner static int fan53555_device_setup(struct fan53555_device_info *di,
333ee30928aSHeiko Stuebner 				struct fan53555_platform_data *pdata)
334ee30928aSHeiko Stuebner {
335ee30928aSHeiko Stuebner 	int ret = 0;
336ee30928aSHeiko Stuebner 
337ee30928aSHeiko Stuebner 	/* Setup voltage control register */
338ee30928aSHeiko Stuebner 	switch (pdata->sleep_vsel_id) {
339ee30928aSHeiko Stuebner 	case FAN53555_VSEL_ID_0:
340ee30928aSHeiko Stuebner 		di->sleep_reg = FAN53555_VSEL0;
341ee30928aSHeiko Stuebner 		di->vol_reg = FAN53555_VSEL1;
342ee30928aSHeiko Stuebner 		break;
343ee30928aSHeiko Stuebner 	case FAN53555_VSEL_ID_1:
344ee30928aSHeiko Stuebner 		di->sleep_reg = FAN53555_VSEL1;
345ee30928aSHeiko Stuebner 		di->vol_reg = FAN53555_VSEL0;
346ee30928aSHeiko Stuebner 		break;
347ee30928aSHeiko Stuebner 	default:
348ee30928aSHeiko Stuebner 		dev_err(di->dev, "Invalid VSEL ID!\n");
349ee30928aSHeiko Stuebner 		return -EINVAL;
350ee30928aSHeiko Stuebner 	}
351ee30928aSHeiko Stuebner 
352*f2a9eb97SBjorn Andersson 	/* Setup mode control register */
353ee30928aSHeiko Stuebner 	switch (di->vendor) {
354*f2a9eb97SBjorn Andersson 	case FAN53526_VENDOR_FAIRCHILD:
355*f2a9eb97SBjorn Andersson 		di->mode_reg = FAN53555_CONTROL;
356*f2a9eb97SBjorn Andersson 
357*f2a9eb97SBjorn Andersson 		switch (pdata->sleep_vsel_id) {
358*f2a9eb97SBjorn Andersson 		case FAN53555_VSEL_ID_0:
359*f2a9eb97SBjorn Andersson 			di->mode_mask = CTL_MODE_VSEL1_MODE;
360*f2a9eb97SBjorn Andersson 			break;
361*f2a9eb97SBjorn Andersson 		case FAN53555_VSEL_ID_1:
362*f2a9eb97SBjorn Andersson 			di->mode_mask = CTL_MODE_VSEL0_MODE;
363*f2a9eb97SBjorn Andersson 			break;
364*f2a9eb97SBjorn Andersson 		}
365*f2a9eb97SBjorn Andersson 		break;
366*f2a9eb97SBjorn Andersson 	case FAN53555_VENDOR_FAIRCHILD:
367*f2a9eb97SBjorn Andersson 	case FAN53555_VENDOR_SILERGY:
368*f2a9eb97SBjorn Andersson 		di->mode_reg = di->vol_reg;
369*f2a9eb97SBjorn Andersson 		di->mode_mask = VSEL_MODE;
370*f2a9eb97SBjorn Andersson 		break;
371*f2a9eb97SBjorn Andersson 	default:
372*f2a9eb97SBjorn Andersson 		dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
373*f2a9eb97SBjorn Andersson 		return -EINVAL;
374*f2a9eb97SBjorn Andersson 	}
375*f2a9eb97SBjorn Andersson 
376*f2a9eb97SBjorn Andersson 	/* Setup voltage range */
377*f2a9eb97SBjorn Andersson 	switch (di->vendor) {
378*f2a9eb97SBjorn Andersson 	case FAN53526_VENDOR_FAIRCHILD:
379*f2a9eb97SBjorn Andersson 		ret = fan53526_voltages_setup_fairchild(di);
380*f2a9eb97SBjorn Andersson 		break;
381ee30928aSHeiko Stuebner 	case FAN53555_VENDOR_FAIRCHILD:
382ee30928aSHeiko Stuebner 		ret = fan53555_voltages_setup_fairchild(di);
383ee30928aSHeiko Stuebner 		break;
384ee30928aSHeiko Stuebner 	case FAN53555_VENDOR_SILERGY:
385ee30928aSHeiko Stuebner 		ret = fan53555_voltages_setup_silergy(di);
386ee30928aSHeiko Stuebner 		break;
387ee30928aSHeiko Stuebner 	default:
388fe230531SAxel Lin 		dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
389ee30928aSHeiko Stuebner 		return -EINVAL;
390ee30928aSHeiko Stuebner 	}
391ee30928aSHeiko Stuebner 
392ee30928aSHeiko Stuebner 	return ret;
393ee30928aSHeiko Stuebner }
394ee30928aSHeiko Stuebner 
39549d8c599SYunfan Zhang static int fan53555_regulator_register(struct fan53555_device_info *di,
39649d8c599SYunfan Zhang 			struct regulator_config *config)
39749d8c599SYunfan Zhang {
39849d8c599SYunfan Zhang 	struct regulator_desc *rdesc = &di->desc;
39949d8c599SYunfan Zhang 
40049d8c599SYunfan Zhang 	rdesc->name = "fan53555-reg";
4013415d601SHeiko Stuebner 	rdesc->supply_name = "vin";
40249d8c599SYunfan Zhang 	rdesc->ops = &fan53555_regulator_ops;
40349d8c599SYunfan Zhang 	rdesc->type = REGULATOR_VOLTAGE;
404*f2a9eb97SBjorn Andersson 	rdesc->n_voltages = di->vsel_count;
40549d8c599SYunfan Zhang 	rdesc->enable_reg = di->vol_reg;
40649d8c599SYunfan Zhang 	rdesc->enable_mask = VSEL_BUCK_EN;
40749d8c599SYunfan Zhang 	rdesc->min_uV = di->vsel_min;
40849d8c599SYunfan Zhang 	rdesc->uV_step = di->vsel_step;
40949d8c599SYunfan Zhang 	rdesc->vsel_reg = di->vol_reg;
410*f2a9eb97SBjorn Andersson 	rdesc->vsel_mask = di->vsel_count - 1;
41149d8c599SYunfan Zhang 	rdesc->owner = THIS_MODULE;
41249d8c599SYunfan Zhang 
413b15f5f76SAxel Lin 	di->rdev = devm_regulator_register(di->dev, &di->desc, config);
4148c6ffba0SRusty Russell 	return PTR_ERR_OR_ZERO(di->rdev);
41549d8c599SYunfan Zhang }
41649d8c599SYunfan Zhang 
417121b567dSKrzysztof Kozlowski static const struct regmap_config fan53555_regmap_config = {
41849d8c599SYunfan Zhang 	.reg_bits = 8,
41949d8c599SYunfan Zhang 	.val_bits = 8,
42049d8c599SYunfan Zhang };
42149d8c599SYunfan Zhang 
42291f23d8fSHeiko Stuebner static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev,
423072e78b1SJavier Martinez Canillas 					      struct device_node *np,
424072e78b1SJavier Martinez Canillas 					      const struct regulator_desc *desc)
42591f23d8fSHeiko Stuebner {
42691f23d8fSHeiko Stuebner 	struct fan53555_platform_data *pdata;
42791f23d8fSHeiko Stuebner 	int ret;
42891f23d8fSHeiko Stuebner 	u32 tmp;
42991f23d8fSHeiko Stuebner 
43091f23d8fSHeiko Stuebner 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
43191f23d8fSHeiko Stuebner 	if (!pdata)
43291f23d8fSHeiko Stuebner 		return NULL;
43391f23d8fSHeiko Stuebner 
434072e78b1SJavier Martinez Canillas 	pdata->regulator = of_get_regulator_init_data(dev, np, desc);
43591f23d8fSHeiko Stuebner 
43691f23d8fSHeiko Stuebner 	ret = of_property_read_u32(np, "fcs,suspend-voltage-selector",
43791f23d8fSHeiko Stuebner 				   &tmp);
43891f23d8fSHeiko Stuebner 	if (!ret)
43991f23d8fSHeiko Stuebner 		pdata->sleep_vsel_id = tmp;
44091f23d8fSHeiko Stuebner 
44191f23d8fSHeiko Stuebner 	return pdata;
44291f23d8fSHeiko Stuebner }
44391f23d8fSHeiko Stuebner 
44491f23d8fSHeiko Stuebner static const struct of_device_id fan53555_dt_ids[] = {
44591f23d8fSHeiko Stuebner 	{
446*f2a9eb97SBjorn Andersson 		.compatible = "fcs,fan53526",
447*f2a9eb97SBjorn Andersson 		.data = (void *)FAN53526_VENDOR_FAIRCHILD,
448*f2a9eb97SBjorn Andersson 	}, {
44991f23d8fSHeiko Stuebner 		.compatible = "fcs,fan53555",
450ee30928aSHeiko Stuebner 		.data = (void *)FAN53555_VENDOR_FAIRCHILD
451ee30928aSHeiko Stuebner 	}, {
452ee30928aSHeiko Stuebner 		.compatible = "silergy,syr827",
453ee30928aSHeiko Stuebner 		.data = (void *)FAN53555_VENDOR_SILERGY,
454ee30928aSHeiko Stuebner 	}, {
455ee30928aSHeiko Stuebner 		.compatible = "silergy,syr828",
456ee30928aSHeiko Stuebner 		.data = (void *)FAN53555_VENDOR_SILERGY,
45791f23d8fSHeiko Stuebner 	},
45891f23d8fSHeiko Stuebner 	{ }
45991f23d8fSHeiko Stuebner };
46091f23d8fSHeiko Stuebner MODULE_DEVICE_TABLE(of, fan53555_dt_ids);
46191f23d8fSHeiko Stuebner 
462a5023574SBill Pemberton static int fan53555_regulator_probe(struct i2c_client *client,
46349d8c599SYunfan Zhang 				const struct i2c_device_id *id)
46449d8c599SYunfan Zhang {
46591f23d8fSHeiko Stuebner 	struct device_node *np = client->dev.of_node;
46649d8c599SYunfan Zhang 	struct fan53555_device_info *di;
46749d8c599SYunfan Zhang 	struct fan53555_platform_data *pdata;
46849d8c599SYunfan Zhang 	struct regulator_config config = { };
46949d8c599SYunfan Zhang 	unsigned int val;
47049d8c599SYunfan Zhang 	int ret;
47149d8c599SYunfan Zhang 
472072e78b1SJavier Martinez Canillas 	di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info),
473072e78b1SJavier Martinez Canillas 					GFP_KERNEL);
474072e78b1SJavier Martinez Canillas 	if (!di)
475072e78b1SJavier Martinez Canillas 		return -ENOMEM;
476072e78b1SJavier Martinez Canillas 
477dff91d0bSJingoo Han 	pdata = dev_get_platdata(&client->dev);
47891f23d8fSHeiko Stuebner 	if (!pdata)
479072e78b1SJavier Martinez Canillas 		pdata = fan53555_parse_dt(&client->dev, np, &di->desc);
48091f23d8fSHeiko Stuebner 
48149d8c599SYunfan Zhang 	if (!pdata || !pdata->regulator) {
48249d8c599SYunfan Zhang 		dev_err(&client->dev, "Platform data not found!\n");
48349d8c599SYunfan Zhang 		return -ENODEV;
48449d8c599SYunfan Zhang 	}
48549d8c599SYunfan Zhang 
486e13426bfSAxel Lin 	di->regulator = pdata->regulator;
487ee30928aSHeiko Stuebner 	if (client->dev.of_node) {
488d110e3e9SJisheng Zhang 		di->vendor =
489d110e3e9SJisheng Zhang 			(unsigned long)of_device_get_match_data(&client->dev);
490ee30928aSHeiko Stuebner 	} else {
491dd7e71fbSHeiko Stuebner 		/* if no ramp constraint set, get the pdata ramp_delay */
492dd7e71fbSHeiko Stuebner 		if (!di->regulator->constraints.ramp_delay) {
49391f23d8fSHeiko Stuebner 			int slew_idx = (pdata->slew_rate & 0x7)
49491f23d8fSHeiko Stuebner 						? pdata->slew_rate : 0;
495dd7e71fbSHeiko Stuebner 
49691f23d8fSHeiko Stuebner 			di->regulator->constraints.ramp_delay
49791f23d8fSHeiko Stuebner 						= slew_rates[slew_idx];
49891f23d8fSHeiko Stuebner 		}
499ee30928aSHeiko Stuebner 
500ee30928aSHeiko Stuebner 		di->vendor = id->driver_data;
501dd7e71fbSHeiko Stuebner 	}
502dd7e71fbSHeiko Stuebner 
50349d8c599SYunfan Zhang 	di->regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config);
50449d8c599SYunfan Zhang 	if (IS_ERR(di->regmap)) {
50549d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to allocate regmap!\n");
50649d8c599SYunfan Zhang 		return PTR_ERR(di->regmap);
50749d8c599SYunfan Zhang 	}
50849d8c599SYunfan Zhang 	di->dev = &client->dev;
50949d8c599SYunfan Zhang 	i2c_set_clientdata(client, di);
51049d8c599SYunfan Zhang 	/* Get chip ID */
51149d8c599SYunfan Zhang 	ret = regmap_read(di->regmap, FAN53555_ID1, &val);
51249d8c599SYunfan Zhang 	if (ret < 0) {
51349d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to get chip ID!\n");
514145fe1e1SSachin Kamat 		return ret;
51549d8c599SYunfan Zhang 	}
51649d8c599SYunfan Zhang 	di->chip_id = val & DIE_ID;
51749d8c599SYunfan Zhang 	/* Get chip revision */
51849d8c599SYunfan Zhang 	ret = regmap_read(di->regmap, FAN53555_ID2, &val);
51949d8c599SYunfan Zhang 	if (ret < 0) {
52049d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to get chip Rev!\n");
521145fe1e1SSachin Kamat 		return ret;
52249d8c599SYunfan Zhang 	}
52349d8c599SYunfan Zhang 	di->chip_rev = val & DIE_REV;
52449d8c599SYunfan Zhang 	dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n",
52549d8c599SYunfan Zhang 				di->chip_id, di->chip_rev);
52649d8c599SYunfan Zhang 	/* Device init */
52749d8c599SYunfan Zhang 	ret = fan53555_device_setup(di, pdata);
52849d8c599SYunfan Zhang 	if (ret < 0) {
52949d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to setup device!\n");
53049d8c599SYunfan Zhang 		return ret;
53149d8c599SYunfan Zhang 	}
53249d8c599SYunfan Zhang 	/* Register regulator */
53349d8c599SYunfan Zhang 	config.dev = di->dev;
53449d8c599SYunfan Zhang 	config.init_data = di->regulator;
53549d8c599SYunfan Zhang 	config.regmap = di->regmap;
53649d8c599SYunfan Zhang 	config.driver_data = di;
53791f23d8fSHeiko Stuebner 	config.of_node = np;
53891f23d8fSHeiko Stuebner 
53949d8c599SYunfan Zhang 	ret = fan53555_regulator_register(di, &config);
54049d8c599SYunfan Zhang 	if (ret < 0)
54149d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to register regulator!\n");
54249d8c599SYunfan Zhang 	return ret;
54349d8c599SYunfan Zhang 
54449d8c599SYunfan Zhang }
54549d8c599SYunfan Zhang 
54649d8c599SYunfan Zhang static const struct i2c_device_id fan53555_id[] = {
547ee30928aSHeiko Stuebner 	{
548*f2a9eb97SBjorn Andersson 		.name = "fan53526",
549*f2a9eb97SBjorn Andersson 		.driver_data = FAN53526_VENDOR_FAIRCHILD
550*f2a9eb97SBjorn Andersson 	}, {
551ee30928aSHeiko Stuebner 		.name = "fan53555",
552ee30928aSHeiko Stuebner 		.driver_data = FAN53555_VENDOR_FAIRCHILD
553ee30928aSHeiko Stuebner 	}, {
554fc1111b8SGuillaume Tucker 		.name = "syr827",
555fc1111b8SGuillaume Tucker 		.driver_data = FAN53555_VENDOR_SILERGY
556fc1111b8SGuillaume Tucker 	}, {
557fc1111b8SGuillaume Tucker 		.name = "syr828",
558ee30928aSHeiko Stuebner 		.driver_data = FAN53555_VENDOR_SILERGY
559ee30928aSHeiko Stuebner 	},
56049d8c599SYunfan Zhang 	{ },
56149d8c599SYunfan Zhang };
562e80c47bdSJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, fan53555_id);
56349d8c599SYunfan Zhang 
56449d8c599SYunfan Zhang static struct i2c_driver fan53555_regulator_driver = {
56549d8c599SYunfan Zhang 	.driver = {
56649d8c599SYunfan Zhang 		.name = "fan53555-regulator",
56791f23d8fSHeiko Stuebner 		.of_match_table = of_match_ptr(fan53555_dt_ids),
56849d8c599SYunfan Zhang 	},
56949d8c599SYunfan Zhang 	.probe = fan53555_regulator_probe,
57049d8c599SYunfan Zhang 	.id_table = fan53555_id,
57149d8c599SYunfan Zhang };
57249d8c599SYunfan Zhang 
57349d8c599SYunfan Zhang module_i2c_driver(fan53555_regulator_driver);
57449d8c599SYunfan Zhang 
57549d8c599SYunfan Zhang MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
57649d8c599SYunfan Zhang MODULE_DESCRIPTION("FAN53555 regulator driver");
57749d8c599SYunfan Zhang MODULE_LICENSE("GPL v2");
578