xref: /linux/drivers/regulator/fan53555.c (revision 5e39cf49729b910795daa0b86052463d23c0a18d)
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 #define VSEL_NSEL_MASK	0x3F
4449d8c599SYunfan Zhang /* Chip ID and Verison */
4549d8c599SYunfan Zhang #define DIE_ID		0x0F	/* ID1 */
4649d8c599SYunfan Zhang #define DIE_REV		0x0F	/* ID2 */
4749d8c599SYunfan Zhang /* Control bit definitions */
4849d8c599SYunfan Zhang #define CTL_OUTPUT_DISCHG	(1 << 7)
4949d8c599SYunfan Zhang #define CTL_SLEW_MASK		(0x7 << 4)
5049d8c599SYunfan Zhang #define CTL_SLEW_SHIFT		4
5149d8c599SYunfan Zhang #define CTL_RESET			(1 << 2)
5249d8c599SYunfan Zhang 
5349d8c599SYunfan Zhang #define FAN53555_NVOLTAGES	64	/* Numbers of voltages */
5449d8c599SYunfan Zhang 
55ee30928aSHeiko Stuebner enum fan53555_vendor {
56ee30928aSHeiko Stuebner 	FAN53555_VENDOR_FAIRCHILD = 0,
57ee30928aSHeiko Stuebner 	FAN53555_VENDOR_SILERGY,
58ee30928aSHeiko Stuebner };
59ee30928aSHeiko Stuebner 
6049d8c599SYunfan Zhang /* IC Type */
6149d8c599SYunfan Zhang enum {
6249d8c599SYunfan Zhang 	FAN53555_CHIP_ID_00 = 0,
6349d8c599SYunfan Zhang 	FAN53555_CHIP_ID_01,
6449d8c599SYunfan Zhang 	FAN53555_CHIP_ID_02,
6549d8c599SYunfan Zhang 	FAN53555_CHIP_ID_03,
6649d8c599SYunfan Zhang 	FAN53555_CHIP_ID_04,
6749d8c599SYunfan Zhang 	FAN53555_CHIP_ID_05,
68*5e39cf49SWadim Egorov 	FAN53555_CHIP_ID_08 = 8,
6949d8c599SYunfan Zhang };
7049d8c599SYunfan Zhang 
71ee30928aSHeiko Stuebner enum {
72ee30928aSHeiko Stuebner 	SILERGY_SYR82X = 8,
73ee30928aSHeiko Stuebner };
74ee30928aSHeiko Stuebner 
7549d8c599SYunfan Zhang struct fan53555_device_info {
76ee30928aSHeiko Stuebner 	enum fan53555_vendor vendor;
7749d8c599SYunfan Zhang 	struct regmap *regmap;
7849d8c599SYunfan Zhang 	struct device *dev;
7949d8c599SYunfan Zhang 	struct regulator_desc desc;
8049d8c599SYunfan Zhang 	struct regulator_dev *rdev;
8149d8c599SYunfan Zhang 	struct regulator_init_data *regulator;
8249d8c599SYunfan Zhang 	/* IC Type and Rev */
8349d8c599SYunfan Zhang 	int chip_id;
8449d8c599SYunfan Zhang 	int chip_rev;
8549d8c599SYunfan Zhang 	/* Voltage setting register */
8649d8c599SYunfan Zhang 	unsigned int vol_reg;
8749d8c599SYunfan Zhang 	unsigned int sleep_reg;
8849d8c599SYunfan Zhang 	/* Voltage range and step(linear) */
8949d8c599SYunfan Zhang 	unsigned int vsel_min;
9049d8c599SYunfan Zhang 	unsigned int vsel_step;
9149d8c599SYunfan Zhang 	/* Voltage slew rate limiting */
9249d8c599SYunfan Zhang 	unsigned int slew_rate;
9349d8c599SYunfan Zhang 	/* Sleep voltage cache */
9449d8c599SYunfan Zhang 	unsigned int sleep_vol_cache;
9549d8c599SYunfan Zhang };
9649d8c599SYunfan Zhang 
9749d8c599SYunfan Zhang static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV)
9849d8c599SYunfan Zhang {
9949d8c599SYunfan Zhang 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
10049d8c599SYunfan Zhang 	int ret;
10149d8c599SYunfan Zhang 
10249d8c599SYunfan Zhang 	if (di->sleep_vol_cache == uV)
10349d8c599SYunfan Zhang 		return 0;
10449d8c599SYunfan Zhang 	ret = regulator_map_voltage_linear(rdev, uV, uV);
10549d8c599SYunfan Zhang 	if (ret < 0)
106145fe1e1SSachin Kamat 		return ret;
10749d8c599SYunfan Zhang 	ret = regmap_update_bits(di->regmap, di->sleep_reg,
10849d8c599SYunfan Zhang 					VSEL_NSEL_MASK, ret);
10949d8c599SYunfan Zhang 	if (ret < 0)
110145fe1e1SSachin Kamat 		return ret;
11149d8c599SYunfan Zhang 	/* Cache the sleep voltage setting.
11249d8c599SYunfan Zhang 	 * Might not be the real voltage which is rounded */
11349d8c599SYunfan Zhang 	di->sleep_vol_cache = uV;
11449d8c599SYunfan Zhang 
11549d8c599SYunfan Zhang 	return 0;
11649d8c599SYunfan Zhang }
11749d8c599SYunfan Zhang 
118ab7cad33Szhangqing static int fan53555_set_suspend_enable(struct regulator_dev *rdev)
119ab7cad33Szhangqing {
120ab7cad33Szhangqing 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
121ab7cad33Szhangqing 
122ab7cad33Szhangqing 	return regmap_update_bits(di->regmap, di->sleep_reg,
123ab7cad33Szhangqing 				  VSEL_BUCK_EN, VSEL_BUCK_EN);
124ab7cad33Szhangqing }
125ab7cad33Szhangqing 
126ab7cad33Szhangqing static int fan53555_set_suspend_disable(struct regulator_dev *rdev)
127ab7cad33Szhangqing {
128ab7cad33Szhangqing 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
129ab7cad33Szhangqing 
130ab7cad33Szhangqing 	return regmap_update_bits(di->regmap, di->sleep_reg,
131ab7cad33Szhangqing 				  VSEL_BUCK_EN, 0);
132ab7cad33Szhangqing }
133ab7cad33Szhangqing 
13449d8c599SYunfan Zhang static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode)
13549d8c599SYunfan Zhang {
13649d8c599SYunfan Zhang 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
13749d8c599SYunfan Zhang 
13849d8c599SYunfan Zhang 	switch (mode) {
13949d8c599SYunfan Zhang 	case REGULATOR_MODE_FAST:
14049d8c599SYunfan Zhang 		regmap_update_bits(di->regmap, di->vol_reg,
14149d8c599SYunfan Zhang 				VSEL_MODE, VSEL_MODE);
14249d8c599SYunfan Zhang 		break;
14349d8c599SYunfan Zhang 	case REGULATOR_MODE_NORMAL:
14449d8c599SYunfan Zhang 		regmap_update_bits(di->regmap, di->vol_reg, VSEL_MODE, 0);
14549d8c599SYunfan Zhang 		break;
14649d8c599SYunfan Zhang 	default:
14749d8c599SYunfan Zhang 		return -EINVAL;
14849d8c599SYunfan Zhang 	}
14949d8c599SYunfan Zhang 	return 0;
15049d8c599SYunfan Zhang }
15149d8c599SYunfan Zhang 
15249d8c599SYunfan Zhang static unsigned int fan53555_get_mode(struct regulator_dev *rdev)
15349d8c599SYunfan Zhang {
15449d8c599SYunfan Zhang 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
15549d8c599SYunfan Zhang 	unsigned int val;
15649d8c599SYunfan Zhang 	int ret = 0;
15749d8c599SYunfan Zhang 
15849d8c599SYunfan Zhang 	ret = regmap_read(di->regmap, di->vol_reg, &val);
15949d8c599SYunfan Zhang 	if (ret < 0)
16049d8c599SYunfan Zhang 		return ret;
16149d8c599SYunfan Zhang 	if (val & VSEL_MODE)
16249d8c599SYunfan Zhang 		return REGULATOR_MODE_FAST;
16349d8c599SYunfan Zhang 	else
16449d8c599SYunfan Zhang 		return REGULATOR_MODE_NORMAL;
16549d8c599SYunfan Zhang }
16649d8c599SYunfan Zhang 
167121b567dSKrzysztof Kozlowski static const int slew_rates[] = {
168dd7e71fbSHeiko Stuebner 	64000,
169dd7e71fbSHeiko Stuebner 	32000,
170dd7e71fbSHeiko Stuebner 	16000,
171dd7e71fbSHeiko Stuebner 	 8000,
172dd7e71fbSHeiko Stuebner 	 4000,
173dd7e71fbSHeiko Stuebner 	 2000,
174dd7e71fbSHeiko Stuebner 	 1000,
175dd7e71fbSHeiko Stuebner 	  500,
176dd7e71fbSHeiko Stuebner };
177dd7e71fbSHeiko Stuebner 
178dd7e71fbSHeiko Stuebner static int fan53555_set_ramp(struct regulator_dev *rdev, int ramp)
179dd7e71fbSHeiko Stuebner {
180dd7e71fbSHeiko Stuebner 	struct fan53555_device_info *di = rdev_get_drvdata(rdev);
181dd7e71fbSHeiko Stuebner 	int regval = -1, i;
182dd7e71fbSHeiko Stuebner 
183dd7e71fbSHeiko Stuebner 	for (i = 0; i < ARRAY_SIZE(slew_rates); i++) {
184dd7e71fbSHeiko Stuebner 		if (ramp <= slew_rates[i])
185dd7e71fbSHeiko Stuebner 			regval = i;
186dd7e71fbSHeiko Stuebner 		else
187dd7e71fbSHeiko Stuebner 			break;
188dd7e71fbSHeiko Stuebner 	}
189dd7e71fbSHeiko Stuebner 
190dd7e71fbSHeiko Stuebner 	if (regval < 0) {
191dd7e71fbSHeiko Stuebner 		dev_err(di->dev, "unsupported ramp value %d\n", ramp);
192dd7e71fbSHeiko Stuebner 		return -EINVAL;
193dd7e71fbSHeiko Stuebner 	}
194dd7e71fbSHeiko Stuebner 
195dd7e71fbSHeiko Stuebner 	return regmap_update_bits(di->regmap, FAN53555_CONTROL,
196dd7e71fbSHeiko Stuebner 				  CTL_SLEW_MASK, regval << CTL_SLEW_SHIFT);
197dd7e71fbSHeiko Stuebner }
198dd7e71fbSHeiko Stuebner 
19949d8c599SYunfan Zhang static struct regulator_ops fan53555_regulator_ops = {
20049d8c599SYunfan Zhang 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
20149d8c599SYunfan Zhang 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
202fda87a42SHeiko Stuebner 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
20349d8c599SYunfan Zhang 	.map_voltage = regulator_map_voltage_linear,
20449d8c599SYunfan Zhang 	.list_voltage = regulator_list_voltage_linear,
20549d8c599SYunfan Zhang 	.set_suspend_voltage = fan53555_set_suspend_voltage,
20649d8c599SYunfan Zhang 	.enable = regulator_enable_regmap,
20749d8c599SYunfan Zhang 	.disable = regulator_disable_regmap,
20849d8c599SYunfan Zhang 	.is_enabled = regulator_is_enabled_regmap,
20949d8c599SYunfan Zhang 	.set_mode = fan53555_set_mode,
21049d8c599SYunfan Zhang 	.get_mode = fan53555_get_mode,
211dd7e71fbSHeiko Stuebner 	.set_ramp_delay = fan53555_set_ramp,
212ab7cad33Szhangqing 	.set_suspend_enable = fan53555_set_suspend_enable,
213ab7cad33Szhangqing 	.set_suspend_disable = fan53555_set_suspend_disable,
21449d8c599SYunfan Zhang };
21549d8c599SYunfan Zhang 
216ee30928aSHeiko Stuebner static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di)
21749d8c599SYunfan Zhang {
21849d8c599SYunfan Zhang 	/* Init voltage range and step */
21949d8c599SYunfan Zhang 	switch (di->chip_id) {
22049d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_00:
22149d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_01:
22249d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_03:
22349d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_05:
224*5e39cf49SWadim Egorov 	case FAN53555_CHIP_ID_08:
22549d8c599SYunfan Zhang 		di->vsel_min = 600000;
22649d8c599SYunfan Zhang 		di->vsel_step = 10000;
22749d8c599SYunfan Zhang 		break;
22849d8c599SYunfan Zhang 	case FAN53555_CHIP_ID_04:
22949d8c599SYunfan Zhang 		di->vsel_min = 603000;
23049d8c599SYunfan Zhang 		di->vsel_step = 12826;
23149d8c599SYunfan Zhang 		break;
23249d8c599SYunfan Zhang 	default:
23349d8c599SYunfan Zhang 		dev_err(di->dev,
234ee30928aSHeiko Stuebner 			"Chip ID %d not supported!\n", di->chip_id);
23549d8c599SYunfan Zhang 		return -EINVAL;
23649d8c599SYunfan Zhang 	}
237dd7e71fbSHeiko Stuebner 
238dd7e71fbSHeiko Stuebner 	return 0;
23949d8c599SYunfan Zhang }
24049d8c599SYunfan Zhang 
241ee30928aSHeiko Stuebner static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di)
242ee30928aSHeiko Stuebner {
243ee30928aSHeiko Stuebner 	/* Init voltage range and step */
244ee30928aSHeiko Stuebner 	switch (di->chip_id) {
245ee30928aSHeiko Stuebner 	case SILERGY_SYR82X:
246ee30928aSHeiko Stuebner 		di->vsel_min = 712500;
247ee30928aSHeiko Stuebner 		di->vsel_step = 12500;
248ee30928aSHeiko Stuebner 		break;
249ee30928aSHeiko Stuebner 	default:
250ee30928aSHeiko Stuebner 		dev_err(di->dev,
251ee30928aSHeiko Stuebner 			"Chip ID %d not supported!\n", di->chip_id);
252ee30928aSHeiko Stuebner 		return -EINVAL;
253ee30928aSHeiko Stuebner 	}
254ee30928aSHeiko Stuebner 
255ee30928aSHeiko Stuebner 	return 0;
256ee30928aSHeiko Stuebner }
257ee30928aSHeiko Stuebner 
258ee30928aSHeiko Stuebner /* For 00,01,03,05 options:
259ee30928aSHeiko Stuebner  * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
260ee30928aSHeiko Stuebner  * For 04 option:
261ee30928aSHeiko Stuebner  * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
262ee30928aSHeiko Stuebner  * */
263ee30928aSHeiko Stuebner static int fan53555_device_setup(struct fan53555_device_info *di,
264ee30928aSHeiko Stuebner 				struct fan53555_platform_data *pdata)
265ee30928aSHeiko Stuebner {
266ee30928aSHeiko Stuebner 	int ret = 0;
267ee30928aSHeiko Stuebner 
268ee30928aSHeiko Stuebner 	/* Setup voltage control register */
269ee30928aSHeiko Stuebner 	switch (pdata->sleep_vsel_id) {
270ee30928aSHeiko Stuebner 	case FAN53555_VSEL_ID_0:
271ee30928aSHeiko Stuebner 		di->sleep_reg = FAN53555_VSEL0;
272ee30928aSHeiko Stuebner 		di->vol_reg = FAN53555_VSEL1;
273ee30928aSHeiko Stuebner 		break;
274ee30928aSHeiko Stuebner 	case FAN53555_VSEL_ID_1:
275ee30928aSHeiko Stuebner 		di->sleep_reg = FAN53555_VSEL1;
276ee30928aSHeiko Stuebner 		di->vol_reg = FAN53555_VSEL0;
277ee30928aSHeiko Stuebner 		break;
278ee30928aSHeiko Stuebner 	default:
279ee30928aSHeiko Stuebner 		dev_err(di->dev, "Invalid VSEL ID!\n");
280ee30928aSHeiko Stuebner 		return -EINVAL;
281ee30928aSHeiko Stuebner 	}
282ee30928aSHeiko Stuebner 
283ee30928aSHeiko Stuebner 	switch (di->vendor) {
284ee30928aSHeiko Stuebner 	case FAN53555_VENDOR_FAIRCHILD:
285ee30928aSHeiko Stuebner 		ret = fan53555_voltages_setup_fairchild(di);
286ee30928aSHeiko Stuebner 		break;
287ee30928aSHeiko Stuebner 	case FAN53555_VENDOR_SILERGY:
288ee30928aSHeiko Stuebner 		ret = fan53555_voltages_setup_silergy(di);
289ee30928aSHeiko Stuebner 		break;
290ee30928aSHeiko Stuebner 	default:
291fe230531SAxel Lin 		dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
292ee30928aSHeiko Stuebner 		return -EINVAL;
293ee30928aSHeiko Stuebner 	}
294ee30928aSHeiko Stuebner 
295ee30928aSHeiko Stuebner 	return ret;
296ee30928aSHeiko Stuebner }
297ee30928aSHeiko Stuebner 
29849d8c599SYunfan Zhang static int fan53555_regulator_register(struct fan53555_device_info *di,
29949d8c599SYunfan Zhang 			struct regulator_config *config)
30049d8c599SYunfan Zhang {
30149d8c599SYunfan Zhang 	struct regulator_desc *rdesc = &di->desc;
30249d8c599SYunfan Zhang 
30349d8c599SYunfan Zhang 	rdesc->name = "fan53555-reg";
3043415d601SHeiko Stuebner 	rdesc->supply_name = "vin";
30549d8c599SYunfan Zhang 	rdesc->ops = &fan53555_regulator_ops;
30649d8c599SYunfan Zhang 	rdesc->type = REGULATOR_VOLTAGE;
30749d8c599SYunfan Zhang 	rdesc->n_voltages = FAN53555_NVOLTAGES;
30849d8c599SYunfan Zhang 	rdesc->enable_reg = di->vol_reg;
30949d8c599SYunfan Zhang 	rdesc->enable_mask = VSEL_BUCK_EN;
31049d8c599SYunfan Zhang 	rdesc->min_uV = di->vsel_min;
31149d8c599SYunfan Zhang 	rdesc->uV_step = di->vsel_step;
31249d8c599SYunfan Zhang 	rdesc->vsel_reg = di->vol_reg;
31349d8c599SYunfan Zhang 	rdesc->vsel_mask = VSEL_NSEL_MASK;
31449d8c599SYunfan Zhang 	rdesc->owner = THIS_MODULE;
31549d8c599SYunfan Zhang 
316b15f5f76SAxel Lin 	di->rdev = devm_regulator_register(di->dev, &di->desc, config);
3178c6ffba0SRusty Russell 	return PTR_ERR_OR_ZERO(di->rdev);
31849d8c599SYunfan Zhang }
31949d8c599SYunfan Zhang 
320121b567dSKrzysztof Kozlowski static const struct regmap_config fan53555_regmap_config = {
32149d8c599SYunfan Zhang 	.reg_bits = 8,
32249d8c599SYunfan Zhang 	.val_bits = 8,
32349d8c599SYunfan Zhang };
32449d8c599SYunfan Zhang 
32591f23d8fSHeiko Stuebner static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev,
326072e78b1SJavier Martinez Canillas 					      struct device_node *np,
327072e78b1SJavier Martinez Canillas 					      const struct regulator_desc *desc)
32891f23d8fSHeiko Stuebner {
32991f23d8fSHeiko Stuebner 	struct fan53555_platform_data *pdata;
33091f23d8fSHeiko Stuebner 	int ret;
33191f23d8fSHeiko Stuebner 	u32 tmp;
33291f23d8fSHeiko Stuebner 
33391f23d8fSHeiko Stuebner 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
33491f23d8fSHeiko Stuebner 	if (!pdata)
33591f23d8fSHeiko Stuebner 		return NULL;
33691f23d8fSHeiko Stuebner 
337072e78b1SJavier Martinez Canillas 	pdata->regulator = of_get_regulator_init_data(dev, np, desc);
33891f23d8fSHeiko Stuebner 
33991f23d8fSHeiko Stuebner 	ret = of_property_read_u32(np, "fcs,suspend-voltage-selector",
34091f23d8fSHeiko Stuebner 				   &tmp);
34191f23d8fSHeiko Stuebner 	if (!ret)
34291f23d8fSHeiko Stuebner 		pdata->sleep_vsel_id = tmp;
34391f23d8fSHeiko Stuebner 
34491f23d8fSHeiko Stuebner 	return pdata;
34591f23d8fSHeiko Stuebner }
34691f23d8fSHeiko Stuebner 
34791f23d8fSHeiko Stuebner static const struct of_device_id fan53555_dt_ids[] = {
34891f23d8fSHeiko Stuebner 	{
34991f23d8fSHeiko Stuebner 		.compatible = "fcs,fan53555",
350ee30928aSHeiko Stuebner 		.data = (void *)FAN53555_VENDOR_FAIRCHILD
351ee30928aSHeiko Stuebner 	}, {
352ee30928aSHeiko Stuebner 		.compatible = "silergy,syr827",
353ee30928aSHeiko Stuebner 		.data = (void *)FAN53555_VENDOR_SILERGY,
354ee30928aSHeiko Stuebner 	}, {
355ee30928aSHeiko Stuebner 		.compatible = "silergy,syr828",
356ee30928aSHeiko Stuebner 		.data = (void *)FAN53555_VENDOR_SILERGY,
35791f23d8fSHeiko Stuebner 	},
35891f23d8fSHeiko Stuebner 	{ }
35991f23d8fSHeiko Stuebner };
36091f23d8fSHeiko Stuebner MODULE_DEVICE_TABLE(of, fan53555_dt_ids);
36191f23d8fSHeiko Stuebner 
362a5023574SBill Pemberton static int fan53555_regulator_probe(struct i2c_client *client,
36349d8c599SYunfan Zhang 				const struct i2c_device_id *id)
36449d8c599SYunfan Zhang {
36591f23d8fSHeiko Stuebner 	struct device_node *np = client->dev.of_node;
36649d8c599SYunfan Zhang 	struct fan53555_device_info *di;
36749d8c599SYunfan Zhang 	struct fan53555_platform_data *pdata;
36849d8c599SYunfan Zhang 	struct regulator_config config = { };
36949d8c599SYunfan Zhang 	unsigned int val;
37049d8c599SYunfan Zhang 	int ret;
37149d8c599SYunfan Zhang 
372072e78b1SJavier Martinez Canillas 	di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info),
373072e78b1SJavier Martinez Canillas 					GFP_KERNEL);
374072e78b1SJavier Martinez Canillas 	if (!di)
375072e78b1SJavier Martinez Canillas 		return -ENOMEM;
376072e78b1SJavier Martinez Canillas 
377dff91d0bSJingoo Han 	pdata = dev_get_platdata(&client->dev);
37891f23d8fSHeiko Stuebner 	if (!pdata)
379072e78b1SJavier Martinez Canillas 		pdata = fan53555_parse_dt(&client->dev, np, &di->desc);
38091f23d8fSHeiko Stuebner 
38149d8c599SYunfan Zhang 	if (!pdata || !pdata->regulator) {
38249d8c599SYunfan Zhang 		dev_err(&client->dev, "Platform data not found!\n");
38349d8c599SYunfan Zhang 		return -ENODEV;
38449d8c599SYunfan Zhang 	}
38549d8c599SYunfan Zhang 
386e13426bfSAxel Lin 	di->regulator = pdata->regulator;
387ee30928aSHeiko Stuebner 	if (client->dev.of_node) {
388ee30928aSHeiko Stuebner 		const struct of_device_id *match;
389ee30928aSHeiko Stuebner 
390ee30928aSHeiko Stuebner 		match = of_match_device(of_match_ptr(fan53555_dt_ids),
391ee30928aSHeiko Stuebner 					&client->dev);
392ee30928aSHeiko Stuebner 		if (!match)
393ee30928aSHeiko Stuebner 			return -ENODEV;
394ee30928aSHeiko Stuebner 
3956f34163cSHeiko Stübner 		di->vendor = (unsigned long) match->data;
396ee30928aSHeiko Stuebner 	} else {
397dd7e71fbSHeiko Stuebner 		/* if no ramp constraint set, get the pdata ramp_delay */
398dd7e71fbSHeiko Stuebner 		if (!di->regulator->constraints.ramp_delay) {
39991f23d8fSHeiko Stuebner 			int slew_idx = (pdata->slew_rate & 0x7)
40091f23d8fSHeiko Stuebner 						? pdata->slew_rate : 0;
401dd7e71fbSHeiko Stuebner 
40291f23d8fSHeiko Stuebner 			di->regulator->constraints.ramp_delay
40391f23d8fSHeiko Stuebner 						= slew_rates[slew_idx];
40491f23d8fSHeiko Stuebner 		}
405ee30928aSHeiko Stuebner 
406ee30928aSHeiko Stuebner 		di->vendor = id->driver_data;
407dd7e71fbSHeiko Stuebner 	}
408dd7e71fbSHeiko Stuebner 
40949d8c599SYunfan Zhang 	di->regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config);
41049d8c599SYunfan Zhang 	if (IS_ERR(di->regmap)) {
41149d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to allocate regmap!\n");
41249d8c599SYunfan Zhang 		return PTR_ERR(di->regmap);
41349d8c599SYunfan Zhang 	}
41449d8c599SYunfan Zhang 	di->dev = &client->dev;
41549d8c599SYunfan Zhang 	i2c_set_clientdata(client, di);
41649d8c599SYunfan Zhang 	/* Get chip ID */
41749d8c599SYunfan Zhang 	ret = regmap_read(di->regmap, FAN53555_ID1, &val);
41849d8c599SYunfan Zhang 	if (ret < 0) {
41949d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to get chip ID!\n");
420145fe1e1SSachin Kamat 		return ret;
42149d8c599SYunfan Zhang 	}
42249d8c599SYunfan Zhang 	di->chip_id = val & DIE_ID;
42349d8c599SYunfan Zhang 	/* Get chip revision */
42449d8c599SYunfan Zhang 	ret = regmap_read(di->regmap, FAN53555_ID2, &val);
42549d8c599SYunfan Zhang 	if (ret < 0) {
42649d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to get chip Rev!\n");
427145fe1e1SSachin Kamat 		return ret;
42849d8c599SYunfan Zhang 	}
42949d8c599SYunfan Zhang 	di->chip_rev = val & DIE_REV;
43049d8c599SYunfan Zhang 	dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n",
43149d8c599SYunfan Zhang 				di->chip_id, di->chip_rev);
43249d8c599SYunfan Zhang 	/* Device init */
43349d8c599SYunfan Zhang 	ret = fan53555_device_setup(di, pdata);
43449d8c599SYunfan Zhang 	if (ret < 0) {
43549d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to setup device!\n");
43649d8c599SYunfan Zhang 		return ret;
43749d8c599SYunfan Zhang 	}
43849d8c599SYunfan Zhang 	/* Register regulator */
43949d8c599SYunfan Zhang 	config.dev = di->dev;
44049d8c599SYunfan Zhang 	config.init_data = di->regulator;
44149d8c599SYunfan Zhang 	config.regmap = di->regmap;
44249d8c599SYunfan Zhang 	config.driver_data = di;
44391f23d8fSHeiko Stuebner 	config.of_node = np;
44491f23d8fSHeiko Stuebner 
44549d8c599SYunfan Zhang 	ret = fan53555_regulator_register(di, &config);
44649d8c599SYunfan Zhang 	if (ret < 0)
44749d8c599SYunfan Zhang 		dev_err(&client->dev, "Failed to register regulator!\n");
44849d8c599SYunfan Zhang 	return ret;
44949d8c599SYunfan Zhang 
45049d8c599SYunfan Zhang }
45149d8c599SYunfan Zhang 
45249d8c599SYunfan Zhang static const struct i2c_device_id fan53555_id[] = {
453ee30928aSHeiko Stuebner 	{
454ee30928aSHeiko Stuebner 		.name = "fan53555",
455ee30928aSHeiko Stuebner 		.driver_data = FAN53555_VENDOR_FAIRCHILD
456ee30928aSHeiko Stuebner 	}, {
457ee30928aSHeiko Stuebner 		.name = "syr82x",
458ee30928aSHeiko Stuebner 		.driver_data = FAN53555_VENDOR_SILERGY
459ee30928aSHeiko Stuebner 	},
46049d8c599SYunfan Zhang 	{ },
46149d8c599SYunfan Zhang };
462e80c47bdSJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, fan53555_id);
46349d8c599SYunfan Zhang 
46449d8c599SYunfan Zhang static struct i2c_driver fan53555_regulator_driver = {
46549d8c599SYunfan Zhang 	.driver = {
46649d8c599SYunfan Zhang 		.name = "fan53555-regulator",
46791f23d8fSHeiko Stuebner 		.of_match_table = of_match_ptr(fan53555_dt_ids),
46849d8c599SYunfan Zhang 	},
46949d8c599SYunfan Zhang 	.probe = fan53555_regulator_probe,
47049d8c599SYunfan Zhang 	.id_table = fan53555_id,
47149d8c599SYunfan Zhang };
47249d8c599SYunfan Zhang 
47349d8c599SYunfan Zhang module_i2c_driver(fan53555_regulator_driver);
47449d8c599SYunfan Zhang 
47549d8c599SYunfan Zhang MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
47649d8c599SYunfan Zhang MODULE_DESCRIPTION("FAN53555 regulator driver");
47749d8c599SYunfan Zhang MODULE_LICENSE("GPL v2");
478