xref: /linux/drivers/regulator/da9052-regulator.c (revision dff91d0b721bf8f036c1071a8f16a7effaa87514)
108bf1c0aSAshish Jangam /*
208bf1c0aSAshish Jangam * da9052-regulator.c: Regulator driver for DA9052
308bf1c0aSAshish Jangam *
408bf1c0aSAshish Jangam * Copyright(c) 2011 Dialog Semiconductor Ltd.
508bf1c0aSAshish Jangam *
608bf1c0aSAshish Jangam * Author: David Dajun Chen <dchen@diasemi.com>
708bf1c0aSAshish Jangam *
808bf1c0aSAshish Jangam * This program is free software; you can redistribute it and/or modify
908bf1c0aSAshish Jangam * it under the terms of the GNU General Public License as published by
1008bf1c0aSAshish Jangam * the Free Software Foundation; either version 2 of the License, or
1108bf1c0aSAshish Jangam * (at your option) any later version.
1208bf1c0aSAshish Jangam *
1308bf1c0aSAshish Jangam */
1408bf1c0aSAshish Jangam 
1508bf1c0aSAshish Jangam #include <linux/module.h>
1608bf1c0aSAshish Jangam #include <linux/moduleparam.h>
1708bf1c0aSAshish Jangam #include <linux/init.h>
1808bf1c0aSAshish Jangam #include <linux/err.h>
1908bf1c0aSAshish Jangam #include <linux/platform_device.h>
2008bf1c0aSAshish Jangam #include <linux/regulator/driver.h>
2108bf1c0aSAshish Jangam #include <linux/regulator/machine.h>
2288c84c14SYing-Chun Liu (PaulLiu) #ifdef CONFIG_OF
23cd22a965SMark Brown #include <linux/of.h>
2488c84c14SYing-Chun Liu (PaulLiu) #include <linux/regulator/of_regulator.h>
2588c84c14SYing-Chun Liu (PaulLiu) #endif
2608bf1c0aSAshish Jangam 
2708bf1c0aSAshish Jangam #include <linux/mfd/da9052/da9052.h>
2808bf1c0aSAshish Jangam #include <linux/mfd/da9052/reg.h>
2908bf1c0aSAshish Jangam #include <linux/mfd/da9052/pdata.h>
3008bf1c0aSAshish Jangam 
3108bf1c0aSAshish Jangam /* Buck step size */
3208bf1c0aSAshish Jangam #define DA9052_BUCK_PERI_3uV_STEP		100000
3308bf1c0aSAshish Jangam #define DA9052_BUCK_PERI_REG_MAP_UPTO_3uV	24
3408bf1c0aSAshish Jangam #define DA9052_CONST_3uV			3000000
3508bf1c0aSAshish Jangam 
3608bf1c0aSAshish Jangam #define DA9052_MIN_UA		0
3708bf1c0aSAshish Jangam #define DA9052_MAX_UA		3
3808bf1c0aSAshish Jangam #define DA9052_CURRENT_RANGE	4
3908bf1c0aSAshish Jangam 
4008bf1c0aSAshish Jangam /* Bit masks */
4108bf1c0aSAshish Jangam #define DA9052_BUCK_ILIM_MASK_EVEN	0x0c
4208bf1c0aSAshish Jangam #define DA9052_BUCK_ILIM_MASK_ODD	0xc0
4308bf1c0aSAshish Jangam 
449210f05bSAxel Lin /* DA9052 REGULATOR IDs */
459210f05bSAxel Lin #define DA9052_ID_BUCK1		0
469210f05bSAxel Lin #define DA9052_ID_BUCK2		1
479210f05bSAxel Lin #define DA9052_ID_BUCK3		2
489210f05bSAxel Lin #define DA9052_ID_BUCK4		3
499210f05bSAxel Lin #define DA9052_ID_LDO1		4
509210f05bSAxel Lin #define DA9052_ID_LDO2		5
519210f05bSAxel Lin #define DA9052_ID_LDO3		6
529210f05bSAxel Lin #define DA9052_ID_LDO4		7
539210f05bSAxel Lin #define DA9052_ID_LDO5		8
549210f05bSAxel Lin #define DA9052_ID_LDO6		9
559210f05bSAxel Lin #define DA9052_ID_LDO7		10
569210f05bSAxel Lin #define DA9052_ID_LDO8		11
579210f05bSAxel Lin #define DA9052_ID_LDO9		12
589210f05bSAxel Lin #define DA9052_ID_LDO10		13
599210f05bSAxel Lin 
6008bf1c0aSAshish Jangam static const u32 da9052_current_limits[3][4] = {
6108bf1c0aSAshish Jangam 	{700000, 800000, 1000000, 1200000},	/* DA9052-BC BUCKs */
6208bf1c0aSAshish Jangam 	{1600000, 2000000, 2400000, 3000000},	/* DA9053-AA/Bx BUCK-CORE */
6308bf1c0aSAshish Jangam 	{800000, 1000000, 1200000, 1500000},	/* DA9053-AA/Bx BUCK-PRO,
6408bf1c0aSAshish Jangam 						 * BUCK-MEM and BUCK-PERI
6508bf1c0aSAshish Jangam 						*/
6608bf1c0aSAshish Jangam };
6708bf1c0aSAshish Jangam 
6808bf1c0aSAshish Jangam struct da9052_regulator_info {
6908bf1c0aSAshish Jangam 	struct regulator_desc reg_desc;
7008bf1c0aSAshish Jangam 	int step_uV;
7108bf1c0aSAshish Jangam 	int min_uV;
7208bf1c0aSAshish Jangam 	int max_uV;
7308bf1c0aSAshish Jangam };
7408bf1c0aSAshish Jangam 
7508bf1c0aSAshish Jangam struct da9052_regulator {
7608bf1c0aSAshish Jangam 	struct da9052 *da9052;
7708bf1c0aSAshish Jangam 	struct da9052_regulator_info *info;
7808bf1c0aSAshish Jangam 	struct regulator_dev *rdev;
7908bf1c0aSAshish Jangam };
8008bf1c0aSAshish Jangam 
8108bf1c0aSAshish Jangam static int verify_range(struct da9052_regulator_info *info,
8208bf1c0aSAshish Jangam 			 int min_uV, int max_uV)
8308bf1c0aSAshish Jangam {
8408bf1c0aSAshish Jangam 	if (min_uV > info->max_uV || max_uV < info->min_uV)
8508bf1c0aSAshish Jangam 		return -EINVAL;
8608bf1c0aSAshish Jangam 
8708bf1c0aSAshish Jangam 	return 0;
8808bf1c0aSAshish Jangam }
8908bf1c0aSAshish Jangam 
9008bf1c0aSAshish Jangam static int da9052_dcdc_get_current_limit(struct regulator_dev *rdev)
9108bf1c0aSAshish Jangam {
9208bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
9308bf1c0aSAshish Jangam 	int offset = rdev_get_id(rdev);
9408bf1c0aSAshish Jangam 	int ret, row = 2;
9508bf1c0aSAshish Jangam 
9608bf1c0aSAshish Jangam 	ret = da9052_reg_read(regulator->da9052, DA9052_BUCKA_REG + offset/2);
9708bf1c0aSAshish Jangam 	if (ret < 0)
9808bf1c0aSAshish Jangam 		return ret;
9908bf1c0aSAshish Jangam 
10008bf1c0aSAshish Jangam 	/* Determine the even or odd position of the buck current limit
10108bf1c0aSAshish Jangam 	 * register field
10208bf1c0aSAshish Jangam 	*/
10308bf1c0aSAshish Jangam 	if (offset % 2 == 0)
10408bf1c0aSAshish Jangam 		ret = (ret & DA9052_BUCK_ILIM_MASK_EVEN) >> 2;
10508bf1c0aSAshish Jangam 	else
10608bf1c0aSAshish Jangam 		ret = (ret & DA9052_BUCK_ILIM_MASK_ODD) >> 6;
10708bf1c0aSAshish Jangam 
10808bf1c0aSAshish Jangam 	/* Select the appropriate current limit range */
10908bf1c0aSAshish Jangam 	if (regulator->da9052->chip_id == DA9052)
11008bf1c0aSAshish Jangam 		row = 0;
11108bf1c0aSAshish Jangam 	else if (offset == 0)
11208bf1c0aSAshish Jangam 		row = 1;
11308bf1c0aSAshish Jangam 
11408bf1c0aSAshish Jangam 	return da9052_current_limits[row][ret];
11508bf1c0aSAshish Jangam }
11608bf1c0aSAshish Jangam 
11708bf1c0aSAshish Jangam static int da9052_dcdc_set_current_limit(struct regulator_dev *rdev, int min_uA,
11808bf1c0aSAshish Jangam 					  int max_uA)
11908bf1c0aSAshish Jangam {
12008bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
12108bf1c0aSAshish Jangam 	int offset = rdev_get_id(rdev);
12208bf1c0aSAshish Jangam 	int reg_val = 0;
12308bf1c0aSAshish Jangam 	int i, row = 2;
12408bf1c0aSAshish Jangam 
12508bf1c0aSAshish Jangam 	/* Select the appropriate current limit range */
12608bf1c0aSAshish Jangam 	if (regulator->da9052->chip_id == DA9052)
12708bf1c0aSAshish Jangam 		row = 0;
12808bf1c0aSAshish Jangam 	else if (offset == 0)
12908bf1c0aSAshish Jangam 		row = 1;
13008bf1c0aSAshish Jangam 
13119d23c21SAxel Lin 	for (i = DA9052_CURRENT_RANGE - 1; i >= 0; i--) {
1321e369bcdSAxel Lin 		if ((min_uA <= da9052_current_limits[row][i]) &&
1331e369bcdSAxel Lin 		    (da9052_current_limits[row][i] <= max_uA)) {
13408bf1c0aSAshish Jangam 			reg_val = i;
13508bf1c0aSAshish Jangam 			break;
13608bf1c0aSAshish Jangam 		}
13708bf1c0aSAshish Jangam 	}
13808bf1c0aSAshish Jangam 
1391e369bcdSAxel Lin 	if (i < 0)
1401e369bcdSAxel Lin 		return -EINVAL;
1411e369bcdSAxel Lin 
14208bf1c0aSAshish Jangam 	/* Determine the even or odd position of the buck current limit
14308bf1c0aSAshish Jangam 	 * register field
14408bf1c0aSAshish Jangam 	*/
14508bf1c0aSAshish Jangam 	if (offset % 2 == 0)
14608bf1c0aSAshish Jangam 		return da9052_reg_update(regulator->da9052,
14708bf1c0aSAshish Jangam 					 DA9052_BUCKA_REG + offset/2,
14808bf1c0aSAshish Jangam 					 DA9052_BUCK_ILIM_MASK_EVEN,
14908bf1c0aSAshish Jangam 					 reg_val << 2);
15008bf1c0aSAshish Jangam 	else
15108bf1c0aSAshish Jangam 		return da9052_reg_update(regulator->da9052,
15208bf1c0aSAshish Jangam 					 DA9052_BUCKA_REG + offset/2,
15308bf1c0aSAshish Jangam 					 DA9052_BUCK_ILIM_MASK_ODD,
15408bf1c0aSAshish Jangam 					 reg_val << 6);
15508bf1c0aSAshish Jangam }
15608bf1c0aSAshish Jangam 
15708bf1c0aSAshish Jangam static int da9052_list_voltage(struct regulator_dev *rdev,
15808bf1c0aSAshish Jangam 				unsigned int selector)
15908bf1c0aSAshish Jangam {
16008bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
16108bf1c0aSAshish Jangam 	struct da9052_regulator_info *info = regulator->info;
1620ec446eaSAxel Lin 	int id = rdev_get_id(rdev);
16308bf1c0aSAshish Jangam 	int volt_uV;
16408bf1c0aSAshish Jangam 
1650ec446eaSAxel Lin 	if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052)
1660ec446eaSAxel Lin 		&& (selector >= DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)) {
1670ec446eaSAxel Lin 		volt_uV = ((DA9052_BUCK_PERI_REG_MAP_UPTO_3uV * info->step_uV)
1680ec446eaSAxel Lin 			  + info->min_uV);
1690ec446eaSAxel Lin 		volt_uV += (selector - DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)
1700ec446eaSAxel Lin 				    * (DA9052_BUCK_PERI_3uV_STEP);
1710ec446eaSAxel Lin 	} else {
1720ec446eaSAxel Lin 		volt_uV = (selector * info->step_uV) + info->min_uV;
1730ec446eaSAxel Lin 	}
17408bf1c0aSAshish Jangam 
17508bf1c0aSAshish Jangam 	if (volt_uV > info->max_uV)
17608bf1c0aSAshish Jangam 		return -EINVAL;
17708bf1c0aSAshish Jangam 
17808bf1c0aSAshish Jangam 	return volt_uV;
17908bf1c0aSAshish Jangam }
18008bf1c0aSAshish Jangam 
1814923b48bSAxel Lin static int da9052_map_voltage(struct regulator_dev *rdev,
1824923b48bSAxel Lin 			      int min_uV, int max_uV)
18308bf1c0aSAshish Jangam {
18408bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
18508bf1c0aSAshish Jangam 	struct da9052_regulator_info *info = regulator->info;
1860ec446eaSAxel Lin 	int id = rdev_get_id(rdev);
1874923b48bSAxel Lin 	int ret, sel;
18808bf1c0aSAshish Jangam 
18908bf1c0aSAshish Jangam 	ret = verify_range(info, min_uV, max_uV);
19008bf1c0aSAshish Jangam 	if (ret < 0)
19108bf1c0aSAshish Jangam 		return ret;
19208bf1c0aSAshish Jangam 
19308bf1c0aSAshish Jangam 	if (min_uV < info->min_uV)
19408bf1c0aSAshish Jangam 		min_uV = info->min_uV;
19508bf1c0aSAshish Jangam 
1960ec446eaSAxel Lin 	if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052)
1970ec446eaSAxel Lin 		&& (min_uV >= DA9052_CONST_3uV)) {
1984923b48bSAxel Lin 			sel = DA9052_BUCK_PERI_REG_MAP_UPTO_3uV +
1990ec446eaSAxel Lin 			      DIV_ROUND_UP(min_uV - DA9052_CONST_3uV,
2000ec446eaSAxel Lin 					   DA9052_BUCK_PERI_3uV_STEP);
2010ec446eaSAxel Lin 	} else {
2024923b48bSAxel Lin 		sel = DIV_ROUND_UP(min_uV - info->min_uV, info->step_uV);
2030ec446eaSAxel Lin 	}
20408bf1c0aSAshish Jangam 
2054923b48bSAxel Lin 	ret = da9052_list_voltage(rdev, sel);
20608bf1c0aSAshish Jangam 	if (ret < 0)
20708bf1c0aSAshish Jangam 		return ret;
20808bf1c0aSAshish Jangam 
2094923b48bSAxel Lin 	return sel;
2104923b48bSAxel Lin }
2114923b48bSAxel Lin 
21208bf1c0aSAshish Jangam static struct regulator_ops da9052_dcdc_ops = {
21308bf1c0aSAshish Jangam 	.get_current_limit = da9052_dcdc_get_current_limit,
21408bf1c0aSAshish Jangam 	.set_current_limit = da9052_dcdc_set_current_limit,
21508bf1c0aSAshish Jangam 
21608bf1c0aSAshish Jangam 	.list_voltage = da9052_list_voltage,
2174923b48bSAxel Lin 	.map_voltage = da9052_map_voltage,
21809812bc4SAxel Lin 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
21968f75060SAxel Lin 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
2200d481f74SAxel Lin 	.is_enabled = regulator_is_enabled_regmap,
2210d481f74SAxel Lin 	.enable = regulator_enable_regmap,
2220d481f74SAxel Lin 	.disable = regulator_disable_regmap,
22308bf1c0aSAshish Jangam };
22408bf1c0aSAshish Jangam 
22508bf1c0aSAshish Jangam static struct regulator_ops da9052_ldo_ops = {
22608bf1c0aSAshish Jangam 	.list_voltage = da9052_list_voltage,
2274923b48bSAxel Lin 	.map_voltage = da9052_map_voltage,
22809812bc4SAxel Lin 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
22968f75060SAxel Lin 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
2300d481f74SAxel Lin 	.is_enabled = regulator_is_enabled_regmap,
2310d481f74SAxel Lin 	.enable = regulator_enable_regmap,
2320d481f74SAxel Lin 	.disable = regulator_disable_regmap,
23308bf1c0aSAshish Jangam };
23408bf1c0aSAshish Jangam 
23508bf1c0aSAshish Jangam #define DA9052_LDO(_id, step, min, max, sbits, ebits, abits) \
23608bf1c0aSAshish Jangam {\
23708bf1c0aSAshish Jangam 	.reg_desc = {\
2389210f05bSAxel Lin 		.name = #_id,\
23908bf1c0aSAshish Jangam 		.ops = &da9052_ldo_ops,\
24008bf1c0aSAshish Jangam 		.type = REGULATOR_VOLTAGE,\
2419210f05bSAxel Lin 		.id = DA9052_ID_##_id,\
2427b957654SAxel Lin 		.n_voltages = (max - min) / step + 1, \
24308bf1c0aSAshish Jangam 		.owner = THIS_MODULE,\
24409812bc4SAxel Lin 		.vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
24509812bc4SAxel Lin 		.vsel_mask = (1 << (sbits)) - 1,\
24668f75060SAxel Lin 		.apply_reg = DA9052_SUPPLY_REG, \
24768f75060SAxel Lin 		.apply_bit = (abits), \
2480d481f74SAxel Lin 		.enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
2490d481f74SAxel Lin 		.enable_mask = 1 << (ebits),\
25008bf1c0aSAshish Jangam 	},\
25108bf1c0aSAshish Jangam 	.min_uV = (min) * 1000,\
25208bf1c0aSAshish Jangam 	.max_uV = (max) * 1000,\
25308bf1c0aSAshish Jangam 	.step_uV = (step) * 1000,\
25408bf1c0aSAshish Jangam }
25508bf1c0aSAshish Jangam 
25608bf1c0aSAshish Jangam #define DA9052_DCDC(_id, step, min, max, sbits, ebits, abits) \
25708bf1c0aSAshish Jangam {\
25808bf1c0aSAshish Jangam 	.reg_desc = {\
2599210f05bSAxel Lin 		.name = #_id,\
26008bf1c0aSAshish Jangam 		.ops = &da9052_dcdc_ops,\
26108bf1c0aSAshish Jangam 		.type = REGULATOR_VOLTAGE,\
2629210f05bSAxel Lin 		.id = DA9052_ID_##_id,\
2637b957654SAxel Lin 		.n_voltages = (max - min) / step + 1, \
26408bf1c0aSAshish Jangam 		.owner = THIS_MODULE,\
26509812bc4SAxel Lin 		.vsel_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
26609812bc4SAxel Lin 		.vsel_mask = (1 << (sbits)) - 1,\
26768f75060SAxel Lin 		.apply_reg = DA9052_SUPPLY_REG, \
26868f75060SAxel Lin 		.apply_bit = (abits), \
2690d481f74SAxel Lin 		.enable_reg = DA9052_BUCKCORE_REG + DA9052_ID_##_id, \
2700d481f74SAxel Lin 		.enable_mask = 1 << (ebits),\
27108bf1c0aSAshish Jangam 	},\
27208bf1c0aSAshish Jangam 	.min_uV = (min) * 1000,\
27308bf1c0aSAshish Jangam 	.max_uV = (max) * 1000,\
27408bf1c0aSAshish Jangam 	.step_uV = (step) * 1000,\
27508bf1c0aSAshish Jangam }
27608bf1c0aSAshish Jangam 
2776242eae9SAxel Lin static struct da9052_regulator_info da9052_regulator_info[] = {
2789210f05bSAxel Lin 	DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
2799210f05bSAxel Lin 	DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
2809210f05bSAxel Lin 	DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
2810ec446eaSAxel Lin 	DA9052_DCDC(BUCK4, 50, 1800, 3600, 5, 6, 0),
2829210f05bSAxel Lin 	DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
2830ec446eaSAxel Lin 	DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
2840ec446eaSAxel Lin 	DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
2859210f05bSAxel Lin 	DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
2869210f05bSAxel Lin 	DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
2879210f05bSAxel Lin 	DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
2889210f05bSAxel Lin 	DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
2899210f05bSAxel Lin 	DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
2909210f05bSAxel Lin 	DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
2919210f05bSAxel Lin 	DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
29208bf1c0aSAshish Jangam };
29308bf1c0aSAshish Jangam 
2946242eae9SAxel Lin static struct da9052_regulator_info da9053_regulator_info[] = {
2959210f05bSAxel Lin 	DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
2969210f05bSAxel Lin 	DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
2979210f05bSAxel Lin 	DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
2980ec446eaSAxel Lin 	DA9052_DCDC(BUCK4, 25, 925, 2500, 6, 6, 0),
2999210f05bSAxel Lin 	DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
3000ec446eaSAxel Lin 	DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
3010ec446eaSAxel Lin 	DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
3029210f05bSAxel Lin 	DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
3039210f05bSAxel Lin 	DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
3049210f05bSAxel Lin 	DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
3059210f05bSAxel Lin 	DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
3069210f05bSAxel Lin 	DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
3079210f05bSAxel Lin 	DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
3089210f05bSAxel Lin 	DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
30908bf1c0aSAshish Jangam };
31008bf1c0aSAshish Jangam 
31108bf1c0aSAshish Jangam static inline struct da9052_regulator_info *find_regulator_info(u8 chip_id,
31208bf1c0aSAshish Jangam 								 int id)
31308bf1c0aSAshish Jangam {
31408bf1c0aSAshish Jangam 	struct da9052_regulator_info *info;
31508bf1c0aSAshish Jangam 	int i;
31608bf1c0aSAshish Jangam 
317984b5a6bSAshish Jangam 	switch (chip_id) {
318984b5a6bSAshish Jangam 	case DA9052:
31908bf1c0aSAshish Jangam 		for (i = 0; i < ARRAY_SIZE(da9052_regulator_info); i++) {
32008bf1c0aSAshish Jangam 			info = &da9052_regulator_info[i];
32108bf1c0aSAshish Jangam 			if (info->reg_desc.id == id)
32208bf1c0aSAshish Jangam 				return info;
32308bf1c0aSAshish Jangam 		}
324984b5a6bSAshish Jangam 		break;
325984b5a6bSAshish Jangam 	case DA9053_AA:
326984b5a6bSAshish Jangam 	case DA9053_BA:
327984b5a6bSAshish Jangam 	case DA9053_BB:
32808bf1c0aSAshish Jangam 		for (i = 0; i < ARRAY_SIZE(da9053_regulator_info); i++) {
32908bf1c0aSAshish Jangam 			info = &da9053_regulator_info[i];
33008bf1c0aSAshish Jangam 			if (info->reg_desc.id == id)
33108bf1c0aSAshish Jangam 				return info;
33208bf1c0aSAshish Jangam 		}
333984b5a6bSAshish Jangam 		break;
33408bf1c0aSAshish Jangam 	}
33508bf1c0aSAshish Jangam 
33608bf1c0aSAshish Jangam 	return NULL;
33708bf1c0aSAshish Jangam }
33808bf1c0aSAshish Jangam 
339a5023574SBill Pemberton static int da9052_regulator_probe(struct platform_device *pdev)
34008bf1c0aSAshish Jangam {
341c172708dSMark Brown 	struct regulator_config config = { };
34208bf1c0aSAshish Jangam 	struct da9052_regulator *regulator;
34308bf1c0aSAshish Jangam 	struct da9052 *da9052;
34408bf1c0aSAshish Jangam 	struct da9052_pdata *pdata;
34508bf1c0aSAshish Jangam 
346984b5a6bSAshish Jangam 	regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9052_regulator),
347984b5a6bSAshish Jangam 				 GFP_KERNEL);
34808bf1c0aSAshish Jangam 	if (!regulator)
34908bf1c0aSAshish Jangam 		return -ENOMEM;
35008bf1c0aSAshish Jangam 
35108bf1c0aSAshish Jangam 	da9052 = dev_get_drvdata(pdev->dev.parent);
352*dff91d0bSJingoo Han 	pdata = dev_get_platdata(da9052->dev);
35308bf1c0aSAshish Jangam 	regulator->da9052 = da9052;
35408bf1c0aSAshish Jangam 
35508bf1c0aSAshish Jangam 	regulator->info = find_regulator_info(regulator->da9052->chip_id,
35608bf1c0aSAshish Jangam 					      pdev->id);
35708bf1c0aSAshish Jangam 	if (regulator->info == NULL) {
35808bf1c0aSAshish Jangam 		dev_err(&pdev->dev, "invalid regulator ID specified\n");
3597eb6444fSAxel Lin 		return -EINVAL;
36008bf1c0aSAshish Jangam 	}
361c172708dSMark Brown 
362c172708dSMark Brown 	config.dev = &pdev->dev;
363c172708dSMark Brown 	config.driver_data = regulator;
3640d481f74SAxel Lin 	config.regmap = da9052->regmap;
36588c84c14SYing-Chun Liu (PaulLiu) 	if (pdata && pdata->regulators) {
36688c84c14SYing-Chun Liu (PaulLiu) 		config.init_data = pdata->regulators[pdev->id];
36788c84c14SYing-Chun Liu (PaulLiu) 	} else {
36888c84c14SYing-Chun Liu (PaulLiu) #ifdef CONFIG_OF
369c92f5dd2SAxel Lin 		struct device_node *nproot, *np;
37088c84c14SYing-Chun Liu (PaulLiu) 
371c92f5dd2SAxel Lin 		nproot = of_node_get(da9052->dev->of_node);
37288c84c14SYing-Chun Liu (PaulLiu) 		if (!nproot)
37388c84c14SYing-Chun Liu (PaulLiu) 			return -ENODEV;
37488c84c14SYing-Chun Liu (PaulLiu) 
37588c84c14SYing-Chun Liu (PaulLiu) 		nproot = of_find_node_by_name(nproot, "regulators");
37688c84c14SYing-Chun Liu (PaulLiu) 		if (!nproot)
37788c84c14SYing-Chun Liu (PaulLiu) 			return -ENODEV;
37888c84c14SYing-Chun Liu (PaulLiu) 
379b86bbdddSAxel Lin 		for_each_child_of_node(nproot, np) {
38088c84c14SYing-Chun Liu (PaulLiu) 			if (!of_node_cmp(np->name,
38188c84c14SYing-Chun Liu (PaulLiu) 					 regulator->info->reg_desc.name)) {
38288c84c14SYing-Chun Liu (PaulLiu) 				config.init_data = of_get_regulator_init_data(
38388c84c14SYing-Chun Liu (PaulLiu) 					&pdev->dev, np);
384e76b9cc7SAxel Lin 				config.of_node = np;
38588c84c14SYing-Chun Liu (PaulLiu) 				break;
38688c84c14SYing-Chun Liu (PaulLiu) 			}
38788c84c14SYing-Chun Liu (PaulLiu) 		}
388c92f5dd2SAxel Lin 		of_node_put(nproot);
38988c84c14SYing-Chun Liu (PaulLiu) #endif
39088c84c14SYing-Chun Liu (PaulLiu) 	}
391c172708dSMark Brown 
39208bf1c0aSAshish Jangam 	regulator->rdev = regulator_register(&regulator->info->reg_desc,
393c172708dSMark Brown 					     &config);
39408bf1c0aSAshish Jangam 	if (IS_ERR(regulator->rdev)) {
39508bf1c0aSAshish Jangam 		dev_err(&pdev->dev, "failed to register regulator %s\n",
39608bf1c0aSAshish Jangam 			regulator->info->reg_desc.name);
3977eb6444fSAxel Lin 		return PTR_ERR(regulator->rdev);
39808bf1c0aSAshish Jangam 	}
39908bf1c0aSAshish Jangam 
40008bf1c0aSAshish Jangam 	platform_set_drvdata(pdev, regulator);
40108bf1c0aSAshish Jangam 
40208bf1c0aSAshish Jangam 	return 0;
40308bf1c0aSAshish Jangam }
40408bf1c0aSAshish Jangam 
4058dc995f5SBill Pemberton static int da9052_regulator_remove(struct platform_device *pdev)
40608bf1c0aSAshish Jangam {
40708bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = platform_get_drvdata(pdev);
40808bf1c0aSAshish Jangam 
40908bf1c0aSAshish Jangam 	regulator_unregister(regulator->rdev);
41008bf1c0aSAshish Jangam 	return 0;
41108bf1c0aSAshish Jangam }
41208bf1c0aSAshish Jangam 
41308bf1c0aSAshish Jangam static struct platform_driver da9052_regulator_driver = {
41408bf1c0aSAshish Jangam 	.probe = da9052_regulator_probe,
4155eb9f2b9SBill Pemberton 	.remove = da9052_regulator_remove,
41608bf1c0aSAshish Jangam 	.driver = {
41708bf1c0aSAshish Jangam 		.name = "da9052-regulator",
41808bf1c0aSAshish Jangam 		.owner = THIS_MODULE,
41908bf1c0aSAshish Jangam 	},
42008bf1c0aSAshish Jangam };
42108bf1c0aSAshish Jangam 
42208bf1c0aSAshish Jangam static int __init da9052_regulator_init(void)
42308bf1c0aSAshish Jangam {
42408bf1c0aSAshish Jangam 	return platform_driver_register(&da9052_regulator_driver);
42508bf1c0aSAshish Jangam }
42608bf1c0aSAshish Jangam subsys_initcall(da9052_regulator_init);
42708bf1c0aSAshish Jangam 
42808bf1c0aSAshish Jangam static void __exit da9052_regulator_exit(void)
42908bf1c0aSAshish Jangam {
43008bf1c0aSAshish Jangam 	platform_driver_unregister(&da9052_regulator_driver);
43108bf1c0aSAshish Jangam }
43208bf1c0aSAshish Jangam module_exit(da9052_regulator_exit);
43308bf1c0aSAshish Jangam 
43408bf1c0aSAshish Jangam MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
43508bf1c0aSAshish Jangam MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9052 PMIC");
43608bf1c0aSAshish Jangam MODULE_LICENSE("GPL");
43708bf1c0aSAshish Jangam MODULE_ALIAS("platform:da9052-regulator");
438