xref: /openbmc/linux/drivers/regulator/da9052-regulator.c (revision 9210f05b4eb0e9d08b527417fa6ebc043a232bd6)
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>
2208bf1c0aSAshish Jangam 
2308bf1c0aSAshish Jangam #include <linux/mfd/da9052/da9052.h>
2408bf1c0aSAshish Jangam #include <linux/mfd/da9052/reg.h>
2508bf1c0aSAshish Jangam #include <linux/mfd/da9052/pdata.h>
2608bf1c0aSAshish Jangam 
2708bf1c0aSAshish Jangam /* Buck step size */
2808bf1c0aSAshish Jangam #define DA9052_BUCK_PERI_3uV_STEP		100000
2908bf1c0aSAshish Jangam #define DA9052_BUCK_PERI_REG_MAP_UPTO_3uV	24
3008bf1c0aSAshish Jangam #define DA9052_CONST_3uV			3000000
3108bf1c0aSAshish Jangam 
3208bf1c0aSAshish Jangam #define DA9052_MIN_UA		0
3308bf1c0aSAshish Jangam #define DA9052_MAX_UA		3
3408bf1c0aSAshish Jangam #define DA9052_CURRENT_RANGE	4
3508bf1c0aSAshish Jangam 
3608bf1c0aSAshish Jangam /* Bit masks */
3708bf1c0aSAshish Jangam #define DA9052_BUCK_ILIM_MASK_EVEN	0x0c
3808bf1c0aSAshish Jangam #define DA9052_BUCK_ILIM_MASK_ODD	0xc0
3908bf1c0aSAshish Jangam 
40*9210f05bSAxel Lin /* DA9052 REGULATOR IDs */
41*9210f05bSAxel Lin #define DA9052_ID_BUCK1		0
42*9210f05bSAxel Lin #define DA9052_ID_BUCK2		1
43*9210f05bSAxel Lin #define DA9052_ID_BUCK3		2
44*9210f05bSAxel Lin #define DA9052_ID_BUCK4		3
45*9210f05bSAxel Lin #define DA9052_ID_LDO1		4
46*9210f05bSAxel Lin #define DA9052_ID_LDO2		5
47*9210f05bSAxel Lin #define DA9052_ID_LDO3		6
48*9210f05bSAxel Lin #define DA9052_ID_LDO4		7
49*9210f05bSAxel Lin #define DA9052_ID_LDO5		8
50*9210f05bSAxel Lin #define DA9052_ID_LDO6		9
51*9210f05bSAxel Lin #define DA9052_ID_LDO7		10
52*9210f05bSAxel Lin #define DA9052_ID_LDO8		11
53*9210f05bSAxel Lin #define DA9052_ID_LDO9		12
54*9210f05bSAxel Lin #define DA9052_ID_LDO10		13
55*9210f05bSAxel Lin 
5608bf1c0aSAshish Jangam static const u32 da9052_current_limits[3][4] = {
5708bf1c0aSAshish Jangam 	{700000, 800000, 1000000, 1200000},	/* DA9052-BC BUCKs */
5808bf1c0aSAshish Jangam 	{1600000, 2000000, 2400000, 3000000},	/* DA9053-AA/Bx BUCK-CORE */
5908bf1c0aSAshish Jangam 	{800000, 1000000, 1200000, 1500000},	/* DA9053-AA/Bx BUCK-PRO,
6008bf1c0aSAshish Jangam 						 * BUCK-MEM and BUCK-PERI
6108bf1c0aSAshish Jangam 						*/
6208bf1c0aSAshish Jangam };
6308bf1c0aSAshish Jangam 
6408bf1c0aSAshish Jangam struct da9052_regulator_info {
6508bf1c0aSAshish Jangam 	struct regulator_desc reg_desc;
6608bf1c0aSAshish Jangam 	int step_uV;
6708bf1c0aSAshish Jangam 	int min_uV;
6808bf1c0aSAshish Jangam 	int max_uV;
6908bf1c0aSAshish Jangam 	unsigned char volt_shift;
7008bf1c0aSAshish Jangam 	unsigned char en_bit;
7108bf1c0aSAshish Jangam 	unsigned char activate_bit;
7208bf1c0aSAshish Jangam };
7308bf1c0aSAshish Jangam 
7408bf1c0aSAshish Jangam struct da9052_regulator {
7508bf1c0aSAshish Jangam 	struct da9052 *da9052;
7608bf1c0aSAshish Jangam 	struct da9052_regulator_info *info;
7708bf1c0aSAshish Jangam 	struct regulator_dev *rdev;
7808bf1c0aSAshish Jangam };
7908bf1c0aSAshish Jangam 
8008bf1c0aSAshish Jangam static int verify_range(struct da9052_regulator_info *info,
8108bf1c0aSAshish Jangam 			 int min_uV, int max_uV)
8208bf1c0aSAshish Jangam {
8308bf1c0aSAshish Jangam 	if (min_uV > info->max_uV || max_uV < info->min_uV)
8408bf1c0aSAshish Jangam 		return -EINVAL;
8508bf1c0aSAshish Jangam 
8608bf1c0aSAshish Jangam 	return 0;
8708bf1c0aSAshish Jangam }
8808bf1c0aSAshish Jangam 
8908bf1c0aSAshish Jangam static int da9052_regulator_enable(struct regulator_dev *rdev)
9008bf1c0aSAshish Jangam {
9108bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
9208bf1c0aSAshish Jangam 	struct da9052_regulator_info *info = regulator->info;
9308bf1c0aSAshish Jangam 	int offset = rdev_get_id(rdev);
9408bf1c0aSAshish Jangam 
9508bf1c0aSAshish Jangam 	return da9052_reg_update(regulator->da9052,
9608bf1c0aSAshish Jangam 				 DA9052_BUCKCORE_REG + offset,
9708bf1c0aSAshish Jangam 				 1 << info->en_bit, 1 << info->en_bit);
9808bf1c0aSAshish Jangam }
9908bf1c0aSAshish Jangam 
10008bf1c0aSAshish Jangam static int da9052_regulator_disable(struct regulator_dev *rdev)
10108bf1c0aSAshish Jangam {
10208bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
10308bf1c0aSAshish Jangam 	struct da9052_regulator_info *info = regulator->info;
10408bf1c0aSAshish Jangam 	int offset = rdev_get_id(rdev);
10508bf1c0aSAshish Jangam 
10608bf1c0aSAshish Jangam 	return da9052_reg_update(regulator->da9052,
10708bf1c0aSAshish Jangam 				 DA9052_BUCKCORE_REG + offset,
10808bf1c0aSAshish Jangam 				 1 << info->en_bit, 0);
10908bf1c0aSAshish Jangam }
11008bf1c0aSAshish Jangam 
11108bf1c0aSAshish Jangam static int da9052_regulator_is_enabled(struct regulator_dev *rdev)
11208bf1c0aSAshish Jangam {
11308bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
11408bf1c0aSAshish Jangam 	struct da9052_regulator_info *info = regulator->info;
11508bf1c0aSAshish Jangam 	int offset = rdev_get_id(rdev);
11608bf1c0aSAshish Jangam 	int ret;
11708bf1c0aSAshish Jangam 
11808bf1c0aSAshish Jangam 	ret = da9052_reg_read(regulator->da9052, DA9052_BUCKCORE_REG + offset);
11908bf1c0aSAshish Jangam 	if (ret < 0)
12008bf1c0aSAshish Jangam 		return ret;
12108bf1c0aSAshish Jangam 
12208bf1c0aSAshish Jangam 	return ret & (1 << info->en_bit);
12308bf1c0aSAshish Jangam }
12408bf1c0aSAshish Jangam 
12508bf1c0aSAshish Jangam static int da9052_dcdc_get_current_limit(struct regulator_dev *rdev)
12608bf1c0aSAshish Jangam {
12708bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
12808bf1c0aSAshish Jangam 	int offset = rdev_get_id(rdev);
12908bf1c0aSAshish Jangam 	int ret, row = 2;
13008bf1c0aSAshish Jangam 
13108bf1c0aSAshish Jangam 	ret = da9052_reg_read(regulator->da9052, DA9052_BUCKA_REG + offset/2);
13208bf1c0aSAshish Jangam 	if (ret < 0)
13308bf1c0aSAshish Jangam 		return ret;
13408bf1c0aSAshish Jangam 
13508bf1c0aSAshish Jangam 	/* Determine the even or odd position of the buck current limit
13608bf1c0aSAshish Jangam 	 * register field
13708bf1c0aSAshish Jangam 	*/
13808bf1c0aSAshish Jangam 	if (offset % 2 == 0)
13908bf1c0aSAshish Jangam 		ret = (ret & DA9052_BUCK_ILIM_MASK_EVEN) >> 2;
14008bf1c0aSAshish Jangam 	else
14108bf1c0aSAshish Jangam 		ret = (ret & DA9052_BUCK_ILIM_MASK_ODD) >> 6;
14208bf1c0aSAshish Jangam 
14308bf1c0aSAshish Jangam 	/* Select the appropriate current limit range */
14408bf1c0aSAshish Jangam 	if (regulator->da9052->chip_id == DA9052)
14508bf1c0aSAshish Jangam 		row = 0;
14608bf1c0aSAshish Jangam 	else if (offset == 0)
14708bf1c0aSAshish Jangam 		row = 1;
14808bf1c0aSAshish Jangam 
14908bf1c0aSAshish Jangam 	return da9052_current_limits[row][ret];
15008bf1c0aSAshish Jangam }
15108bf1c0aSAshish Jangam 
15208bf1c0aSAshish Jangam static int da9052_dcdc_set_current_limit(struct regulator_dev *rdev, int min_uA,
15308bf1c0aSAshish Jangam 					  int max_uA)
15408bf1c0aSAshish Jangam {
15508bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
15608bf1c0aSAshish Jangam 	int offset = rdev_get_id(rdev);
15708bf1c0aSAshish Jangam 	int reg_val = 0;
15808bf1c0aSAshish Jangam 	int i, row = 2;
15908bf1c0aSAshish Jangam 
16008bf1c0aSAshish Jangam 	/* Select the appropriate current limit range */
16108bf1c0aSAshish Jangam 	if (regulator->da9052->chip_id == DA9052)
16208bf1c0aSAshish Jangam 		row = 0;
16308bf1c0aSAshish Jangam 	else if (offset == 0)
16408bf1c0aSAshish Jangam 		row = 1;
16508bf1c0aSAshish Jangam 
16608bf1c0aSAshish Jangam 	if (min_uA > da9052_current_limits[row][DA9052_MAX_UA] ||
16708bf1c0aSAshish Jangam 	    max_uA < da9052_current_limits[row][DA9052_MIN_UA])
16808bf1c0aSAshish Jangam 		return -EINVAL;
16908bf1c0aSAshish Jangam 
17008bf1c0aSAshish Jangam 	for (i = 0; i < DA9052_CURRENT_RANGE; i++) {
17108bf1c0aSAshish Jangam 		if (min_uA <= da9052_current_limits[row][i]) {
17208bf1c0aSAshish Jangam 			reg_val = i;
17308bf1c0aSAshish Jangam 			break;
17408bf1c0aSAshish Jangam 		}
17508bf1c0aSAshish Jangam 	}
17608bf1c0aSAshish Jangam 
17708bf1c0aSAshish Jangam 	/* Determine the even or odd position of the buck current limit
17808bf1c0aSAshish Jangam 	 * register field
17908bf1c0aSAshish Jangam 	*/
18008bf1c0aSAshish Jangam 	if (offset % 2 == 0)
18108bf1c0aSAshish Jangam 		return da9052_reg_update(regulator->da9052,
18208bf1c0aSAshish Jangam 					 DA9052_BUCKA_REG + offset/2,
18308bf1c0aSAshish Jangam 					 DA9052_BUCK_ILIM_MASK_EVEN,
18408bf1c0aSAshish Jangam 					 reg_val << 2);
18508bf1c0aSAshish Jangam 	else
18608bf1c0aSAshish Jangam 		return da9052_reg_update(regulator->da9052,
18708bf1c0aSAshish Jangam 					 DA9052_BUCKA_REG + offset/2,
18808bf1c0aSAshish Jangam 					 DA9052_BUCK_ILIM_MASK_ODD,
18908bf1c0aSAshish Jangam 					 reg_val << 6);
19008bf1c0aSAshish Jangam }
19108bf1c0aSAshish Jangam 
19208bf1c0aSAshish Jangam static int da9052_list_buckperi_voltage(struct regulator_dev *rdev,
19308bf1c0aSAshish Jangam 					 unsigned int selector)
19408bf1c0aSAshish Jangam {
19508bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
19608bf1c0aSAshish Jangam 	struct da9052_regulator_info *info = regulator->info;
19708bf1c0aSAshish Jangam 	int volt_uV;
19808bf1c0aSAshish Jangam 
19908bf1c0aSAshish Jangam 	if ((regulator->da9052->chip_id == DA9052) &&
20008bf1c0aSAshish Jangam 	    (selector >= DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)) {
20108bf1c0aSAshish Jangam 		volt_uV = ((DA9052_BUCK_PERI_REG_MAP_UPTO_3uV * info->step_uV)
20208bf1c0aSAshish Jangam 			    + info->min_uV);
20308bf1c0aSAshish Jangam 		volt_uV += (selector - DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)
20408bf1c0aSAshish Jangam 			    * (DA9052_BUCK_PERI_3uV_STEP);
20508bf1c0aSAshish Jangam 	} else
20608bf1c0aSAshish Jangam 			volt_uV = (selector * info->step_uV) + info->min_uV;
20708bf1c0aSAshish Jangam 
20808bf1c0aSAshish Jangam 	if (volt_uV > info->max_uV)
20908bf1c0aSAshish Jangam 		return -EINVAL;
21008bf1c0aSAshish Jangam 
21108bf1c0aSAshish Jangam 	return volt_uV;
21208bf1c0aSAshish Jangam }
21308bf1c0aSAshish Jangam 
21408bf1c0aSAshish Jangam static int da9052_list_voltage(struct regulator_dev *rdev,
21508bf1c0aSAshish Jangam 				unsigned int selector)
21608bf1c0aSAshish Jangam {
21708bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
21808bf1c0aSAshish Jangam 	struct da9052_regulator_info *info = regulator->info;
21908bf1c0aSAshish Jangam 	int volt_uV;
22008bf1c0aSAshish Jangam 
22108bf1c0aSAshish Jangam 	volt_uV = info->min_uV + info->step_uV * selector;
22208bf1c0aSAshish Jangam 
22308bf1c0aSAshish Jangam 	if (volt_uV > info->max_uV)
22408bf1c0aSAshish Jangam 		return -EINVAL;
22508bf1c0aSAshish Jangam 
22608bf1c0aSAshish Jangam 	return volt_uV;
22708bf1c0aSAshish Jangam }
22808bf1c0aSAshish Jangam 
22908bf1c0aSAshish Jangam static int da9052_regulator_set_voltage_int(struct regulator_dev *rdev,
23008bf1c0aSAshish Jangam 					     int min_uV, int max_uV,
23108bf1c0aSAshish Jangam 					     unsigned int *selector)
23208bf1c0aSAshish Jangam {
23308bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
23408bf1c0aSAshish Jangam 	struct da9052_regulator_info *info = regulator->info;
23508bf1c0aSAshish Jangam 	int offset = rdev_get_id(rdev);
23608bf1c0aSAshish Jangam 	int ret;
23708bf1c0aSAshish Jangam 
23808bf1c0aSAshish Jangam 	ret = verify_range(info, min_uV, max_uV);
23908bf1c0aSAshish Jangam 	if (ret < 0)
24008bf1c0aSAshish Jangam 		return ret;
24108bf1c0aSAshish Jangam 
24208bf1c0aSAshish Jangam 	if (min_uV < info->min_uV)
24308bf1c0aSAshish Jangam 		min_uV = info->min_uV;
24408bf1c0aSAshish Jangam 
24593651218SAxel Lin 	*selector = DIV_ROUND_UP(min_uV - info->min_uV, info->step_uV);
24608bf1c0aSAshish Jangam 
24708bf1c0aSAshish Jangam 	ret = da9052_list_voltage(rdev, *selector);
24808bf1c0aSAshish Jangam 	if (ret < 0)
24908bf1c0aSAshish Jangam 		return ret;
25008bf1c0aSAshish Jangam 
25108bf1c0aSAshish Jangam 	return da9052_reg_update(regulator->da9052,
25208bf1c0aSAshish Jangam 				 DA9052_BUCKCORE_REG + offset,
25308bf1c0aSAshish Jangam 				 (1 << info->volt_shift) - 1, *selector);
25408bf1c0aSAshish Jangam }
25508bf1c0aSAshish Jangam 
25608bf1c0aSAshish Jangam static int da9052_set_ldo_voltage(struct regulator_dev *rdev,
25708bf1c0aSAshish Jangam 				   int min_uV, int max_uV,
25808bf1c0aSAshish Jangam 				   unsigned int *selector)
25908bf1c0aSAshish Jangam {
26008bf1c0aSAshish Jangam 	return da9052_regulator_set_voltage_int(rdev, min_uV, max_uV, selector);
26108bf1c0aSAshish Jangam }
26208bf1c0aSAshish Jangam 
26308bf1c0aSAshish Jangam static int da9052_set_ldo5_6_voltage(struct regulator_dev *rdev,
26408bf1c0aSAshish Jangam 				      int min_uV, int max_uV,
26508bf1c0aSAshish Jangam 				      unsigned int *selector)
26608bf1c0aSAshish Jangam {
26708bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
26808bf1c0aSAshish Jangam 	struct da9052_regulator_info *info = regulator->info;
26908bf1c0aSAshish Jangam 	int ret;
27008bf1c0aSAshish Jangam 
27108bf1c0aSAshish Jangam 	ret = da9052_regulator_set_voltage_int(rdev, min_uV, max_uV, selector);
27208bf1c0aSAshish Jangam 	if (ret < 0)
27308bf1c0aSAshish Jangam 		return ret;
27408bf1c0aSAshish Jangam 
27508bf1c0aSAshish Jangam 	/* Some LDOs are DVC controlled which requires enabling of
27608bf1c0aSAshish Jangam 	 * the LDO activate bit to implment the changes on the
27708bf1c0aSAshish Jangam 	 * LDO output.
27808bf1c0aSAshish Jangam 	*/
2794adf9bedSAxel Lin 	return da9052_reg_update(regulator->da9052, DA9052_SUPPLY_REG,
2804adf9bedSAxel Lin 				 info->activate_bit, info->activate_bit);
28108bf1c0aSAshish Jangam }
28208bf1c0aSAshish Jangam 
28308bf1c0aSAshish Jangam static int da9052_set_dcdc_voltage(struct regulator_dev *rdev,
28408bf1c0aSAshish Jangam 				    int min_uV, int max_uV,
28508bf1c0aSAshish Jangam 				    unsigned int *selector)
28608bf1c0aSAshish Jangam {
28708bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
28808bf1c0aSAshish Jangam 	struct da9052_regulator_info *info = regulator->info;
28908bf1c0aSAshish Jangam 	int ret;
29008bf1c0aSAshish Jangam 
29108bf1c0aSAshish Jangam 	ret = da9052_regulator_set_voltage_int(rdev, min_uV, max_uV, selector);
29208bf1c0aSAshish Jangam 	if (ret < 0)
29308bf1c0aSAshish Jangam 		return ret;
29408bf1c0aSAshish Jangam 
29508bf1c0aSAshish Jangam 	/* Some DCDCs are DVC controlled which requires enabling of
29608bf1c0aSAshish Jangam 	 * the DCDC activate bit to implment the changes on the
29708bf1c0aSAshish Jangam 	 * DCDC output.
29808bf1c0aSAshish Jangam 	*/
2994adf9bedSAxel Lin 	return da9052_reg_update(regulator->da9052, DA9052_SUPPLY_REG,
3004adf9bedSAxel Lin 				 info->activate_bit, info->activate_bit);
30108bf1c0aSAshish Jangam }
30208bf1c0aSAshish Jangam 
30308bf1c0aSAshish Jangam static int da9052_get_regulator_voltage_sel(struct regulator_dev *rdev)
30408bf1c0aSAshish Jangam {
30508bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
30608bf1c0aSAshish Jangam 	struct da9052_regulator_info *info = regulator->info;
30708bf1c0aSAshish Jangam 	int offset = rdev_get_id(rdev);
30808bf1c0aSAshish Jangam 	int ret;
30908bf1c0aSAshish Jangam 
31008bf1c0aSAshish Jangam 	ret = da9052_reg_read(regulator->da9052, DA9052_BUCKCORE_REG + offset);
31108bf1c0aSAshish Jangam 	if (ret < 0)
31208bf1c0aSAshish Jangam 		return ret;
31308bf1c0aSAshish Jangam 
31408bf1c0aSAshish Jangam 	ret &= ((1 << info->volt_shift) - 1);
31508bf1c0aSAshish Jangam 
31608bf1c0aSAshish Jangam 	return ret;
31708bf1c0aSAshish Jangam }
31808bf1c0aSAshish Jangam 
31908bf1c0aSAshish Jangam static int da9052_set_buckperi_voltage(struct regulator_dev *rdev, int min_uV,
32008bf1c0aSAshish Jangam 					int max_uV, unsigned int *selector)
32108bf1c0aSAshish Jangam {
32208bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
32308bf1c0aSAshish Jangam 	struct da9052_regulator_info *info = regulator->info;
32408bf1c0aSAshish Jangam 	int offset = rdev_get_id(rdev);
32508bf1c0aSAshish Jangam 	int ret;
32608bf1c0aSAshish Jangam 
32708bf1c0aSAshish Jangam 	ret = verify_range(info, min_uV, max_uV);
32808bf1c0aSAshish Jangam 	if (ret < 0)
32908bf1c0aSAshish Jangam 		return ret;
33008bf1c0aSAshish Jangam 
33108bf1c0aSAshish Jangam 	if (min_uV < info->min_uV)
33208bf1c0aSAshish Jangam 		min_uV = info->min_uV;
33308bf1c0aSAshish Jangam 
33408bf1c0aSAshish Jangam 	if ((regulator->da9052->chip_id == DA9052) &&
33508bf1c0aSAshish Jangam 	    (min_uV >= DA9052_CONST_3uV))
33608bf1c0aSAshish Jangam 		*selector = DA9052_BUCK_PERI_REG_MAP_UPTO_3uV +
33793651218SAxel Lin 			    DIV_ROUND_UP(min_uV - DA9052_CONST_3uV,
33893651218SAxel Lin 					 DA9052_BUCK_PERI_3uV_STEP);
33908bf1c0aSAshish Jangam 	else
34093651218SAxel Lin 		*selector = DIV_ROUND_UP(min_uV - info->min_uV, info->step_uV);
34108bf1c0aSAshish Jangam 
34208bf1c0aSAshish Jangam 	ret = da9052_list_buckperi_voltage(rdev, *selector);
34308bf1c0aSAshish Jangam 	if (ret < 0)
34408bf1c0aSAshish Jangam 		return ret;
34508bf1c0aSAshish Jangam 
34608bf1c0aSAshish Jangam 	return da9052_reg_update(regulator->da9052,
34708bf1c0aSAshish Jangam 				 DA9052_BUCKCORE_REG + offset,
34808bf1c0aSAshish Jangam 				 (1 << info->volt_shift) - 1, *selector);
34908bf1c0aSAshish Jangam }
35008bf1c0aSAshish Jangam 
35108bf1c0aSAshish Jangam static int da9052_get_buckperi_voltage_sel(struct regulator_dev *rdev)
35208bf1c0aSAshish Jangam {
35308bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
35408bf1c0aSAshish Jangam 	struct da9052_regulator_info *info = regulator->info;
35508bf1c0aSAshish Jangam 	int offset = rdev_get_id(rdev);
35608bf1c0aSAshish Jangam 	int ret;
35708bf1c0aSAshish Jangam 
35808bf1c0aSAshish Jangam 	ret = da9052_reg_read(regulator->da9052, DA9052_BUCKCORE_REG + offset);
35908bf1c0aSAshish Jangam 	if (ret < 0)
36008bf1c0aSAshish Jangam 		return ret;
36108bf1c0aSAshish Jangam 
36208bf1c0aSAshish Jangam 	ret &= ((1 << info->volt_shift) - 1);
36308bf1c0aSAshish Jangam 
36408bf1c0aSAshish Jangam 	return ret;
36508bf1c0aSAshish Jangam }
36608bf1c0aSAshish Jangam 
36708bf1c0aSAshish Jangam static struct regulator_ops da9052_buckperi_ops = {
36808bf1c0aSAshish Jangam 	.list_voltage = da9052_list_buckperi_voltage,
36908bf1c0aSAshish Jangam 	.get_voltage_sel = da9052_get_buckperi_voltage_sel,
37008bf1c0aSAshish Jangam 	.set_voltage = da9052_set_buckperi_voltage,
37108bf1c0aSAshish Jangam 
37208bf1c0aSAshish Jangam 	.get_current_limit = da9052_dcdc_get_current_limit,
37308bf1c0aSAshish Jangam 	.set_current_limit = da9052_dcdc_set_current_limit,
37408bf1c0aSAshish Jangam 
37508bf1c0aSAshish Jangam 	.is_enabled = da9052_regulator_is_enabled,
37608bf1c0aSAshish Jangam 	.enable = da9052_regulator_enable,
37708bf1c0aSAshish Jangam 	.disable = da9052_regulator_disable,
37808bf1c0aSAshish Jangam };
37908bf1c0aSAshish Jangam 
38008bf1c0aSAshish Jangam static struct regulator_ops da9052_dcdc_ops = {
38108bf1c0aSAshish Jangam 	.set_voltage = da9052_set_dcdc_voltage,
38208bf1c0aSAshish Jangam 	.get_current_limit = da9052_dcdc_get_current_limit,
38308bf1c0aSAshish Jangam 	.set_current_limit = da9052_dcdc_set_current_limit,
38408bf1c0aSAshish Jangam 
38508bf1c0aSAshish Jangam 	.list_voltage = da9052_list_voltage,
38608bf1c0aSAshish Jangam 	.get_voltage_sel = da9052_get_regulator_voltage_sel,
38708bf1c0aSAshish Jangam 	.is_enabled = da9052_regulator_is_enabled,
38808bf1c0aSAshish Jangam 	.enable = da9052_regulator_enable,
38908bf1c0aSAshish Jangam 	.disable = da9052_regulator_disable,
39008bf1c0aSAshish Jangam };
39108bf1c0aSAshish Jangam 
39208bf1c0aSAshish Jangam static struct regulator_ops da9052_ldo5_6_ops = {
39308bf1c0aSAshish Jangam 	.set_voltage = da9052_set_ldo5_6_voltage,
39408bf1c0aSAshish Jangam 
39508bf1c0aSAshish Jangam 	.list_voltage = da9052_list_voltage,
39608bf1c0aSAshish Jangam 	.get_voltage_sel = da9052_get_regulator_voltage_sel,
39708bf1c0aSAshish Jangam 	.is_enabled = da9052_regulator_is_enabled,
39808bf1c0aSAshish Jangam 	.enable = da9052_regulator_enable,
39908bf1c0aSAshish Jangam 	.disable = da9052_regulator_disable,
40008bf1c0aSAshish Jangam };
40108bf1c0aSAshish Jangam 
40208bf1c0aSAshish Jangam static struct regulator_ops da9052_ldo_ops = {
40308bf1c0aSAshish Jangam 	.set_voltage = da9052_set_ldo_voltage,
40408bf1c0aSAshish Jangam 
40508bf1c0aSAshish Jangam 	.list_voltage = da9052_list_voltage,
40608bf1c0aSAshish Jangam 	.get_voltage_sel = da9052_get_regulator_voltage_sel,
40708bf1c0aSAshish Jangam 	.is_enabled = da9052_regulator_is_enabled,
40808bf1c0aSAshish Jangam 	.enable = da9052_regulator_enable,
40908bf1c0aSAshish Jangam 	.disable = da9052_regulator_disable,
41008bf1c0aSAshish Jangam };
41108bf1c0aSAshish Jangam 
41208bf1c0aSAshish Jangam #define DA9052_LDO5_6(_id, step, min, max, sbits, ebits, abits) \
41308bf1c0aSAshish Jangam {\
41408bf1c0aSAshish Jangam 	.reg_desc = {\
415*9210f05bSAxel Lin 		.name = #_id,\
41608bf1c0aSAshish Jangam 		.ops = &da9052_ldo5_6_ops,\
41708bf1c0aSAshish Jangam 		.type = REGULATOR_VOLTAGE,\
418*9210f05bSAxel Lin 		.id = DA9052_ID_##_id,\
4197b957654SAxel Lin 		.n_voltages = (max - min) / step + 1, \
42008bf1c0aSAshish Jangam 		.owner = THIS_MODULE,\
42108bf1c0aSAshish Jangam 	},\
42208bf1c0aSAshish Jangam 	.min_uV = (min) * 1000,\
42308bf1c0aSAshish Jangam 	.max_uV = (max) * 1000,\
42408bf1c0aSAshish Jangam 	.step_uV = (step) * 1000,\
42508bf1c0aSAshish Jangam 	.volt_shift = (sbits),\
42608bf1c0aSAshish Jangam 	.en_bit = (ebits),\
42708bf1c0aSAshish Jangam 	.activate_bit = (abits),\
42808bf1c0aSAshish Jangam }
42908bf1c0aSAshish Jangam 
43008bf1c0aSAshish Jangam #define DA9052_LDO(_id, step, min, max, sbits, ebits, abits) \
43108bf1c0aSAshish Jangam {\
43208bf1c0aSAshish Jangam 	.reg_desc = {\
433*9210f05bSAxel Lin 		.name = #_id,\
43408bf1c0aSAshish Jangam 		.ops = &da9052_ldo_ops,\
43508bf1c0aSAshish Jangam 		.type = REGULATOR_VOLTAGE,\
436*9210f05bSAxel Lin 		.id = DA9052_ID_##_id,\
4377b957654SAxel Lin 		.n_voltages = (max - min) / step + 1, \
43808bf1c0aSAshish Jangam 		.owner = THIS_MODULE,\
43908bf1c0aSAshish Jangam 	},\
44008bf1c0aSAshish Jangam 	.min_uV = (min) * 1000,\
44108bf1c0aSAshish Jangam 	.max_uV = (max) * 1000,\
44208bf1c0aSAshish Jangam 	.step_uV = (step) * 1000,\
44308bf1c0aSAshish Jangam 	.volt_shift = (sbits),\
44408bf1c0aSAshish Jangam 	.en_bit = (ebits),\
44508bf1c0aSAshish Jangam 	.activate_bit = (abits),\
44608bf1c0aSAshish Jangam }
44708bf1c0aSAshish Jangam 
44808bf1c0aSAshish Jangam #define DA9052_DCDC(_id, step, min, max, sbits, ebits, abits) \
44908bf1c0aSAshish Jangam {\
45008bf1c0aSAshish Jangam 	.reg_desc = {\
451*9210f05bSAxel Lin 		.name = #_id,\
45208bf1c0aSAshish Jangam 		.ops = &da9052_dcdc_ops,\
45308bf1c0aSAshish Jangam 		.type = REGULATOR_VOLTAGE,\
454*9210f05bSAxel Lin 		.id = DA9052_ID_##_id,\
4557b957654SAxel Lin 		.n_voltages = (max - min) / step + 1, \
45608bf1c0aSAshish Jangam 		.owner = THIS_MODULE,\
45708bf1c0aSAshish Jangam 	},\
45808bf1c0aSAshish Jangam 	.min_uV = (min) * 1000,\
45908bf1c0aSAshish Jangam 	.max_uV = (max) * 1000,\
46008bf1c0aSAshish Jangam 	.step_uV = (step) * 1000,\
46108bf1c0aSAshish Jangam 	.volt_shift = (sbits),\
46208bf1c0aSAshish Jangam 	.en_bit = (ebits),\
46308bf1c0aSAshish Jangam 	.activate_bit = (abits),\
46408bf1c0aSAshish Jangam }
46508bf1c0aSAshish Jangam 
46608bf1c0aSAshish Jangam #define DA9052_BUCKPERI(_id, step, min, max, sbits, ebits, abits) \
46708bf1c0aSAshish Jangam {\
46808bf1c0aSAshish Jangam 	.reg_desc = {\
469*9210f05bSAxel Lin 		.name = #_id,\
47008bf1c0aSAshish Jangam 		.ops = &da9052_buckperi_ops,\
47108bf1c0aSAshish Jangam 		.type = REGULATOR_VOLTAGE,\
472*9210f05bSAxel Lin 		.id = DA9052_ID_##_id,\
4737b957654SAxel Lin 		.n_voltages = (max - min) / step + 1, \
47408bf1c0aSAshish Jangam 		.owner = THIS_MODULE,\
47508bf1c0aSAshish Jangam 	},\
47608bf1c0aSAshish Jangam 	.min_uV = (min) * 1000,\
47708bf1c0aSAshish Jangam 	.max_uV = (max) * 1000,\
47808bf1c0aSAshish Jangam 	.step_uV = (step) * 1000,\
47908bf1c0aSAshish Jangam 	.volt_shift = (sbits),\
48008bf1c0aSAshish Jangam 	.en_bit = (ebits),\
48108bf1c0aSAshish Jangam 	.activate_bit = (abits),\
48208bf1c0aSAshish Jangam }
48308bf1c0aSAshish Jangam 
4846242eae9SAxel Lin static struct da9052_regulator_info da9052_regulator_info[] = {
485*9210f05bSAxel Lin 	DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
486*9210f05bSAxel Lin 	DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
487*9210f05bSAxel Lin 	DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
488*9210f05bSAxel Lin 	DA9052_BUCKPERI(BUCK4, 50, 1800, 3600, 5, 6, 0),
489*9210f05bSAxel Lin 	DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
490*9210f05bSAxel Lin 	DA9052_LDO5_6(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
491*9210f05bSAxel Lin 	DA9052_LDO5_6(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
492*9210f05bSAxel Lin 	DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
493*9210f05bSAxel Lin 	DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
494*9210f05bSAxel Lin 	DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
495*9210f05bSAxel Lin 	DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
496*9210f05bSAxel Lin 	DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
497*9210f05bSAxel Lin 	DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
498*9210f05bSAxel Lin 	DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
49908bf1c0aSAshish Jangam };
50008bf1c0aSAshish Jangam 
5016242eae9SAxel Lin static struct da9052_regulator_info da9053_regulator_info[] = {
502*9210f05bSAxel Lin 	DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
503*9210f05bSAxel Lin 	DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
504*9210f05bSAxel Lin 	DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
505*9210f05bSAxel Lin 	DA9052_BUCKPERI(BUCK4, 25, 925, 2500, 6, 6, 0),
506*9210f05bSAxel Lin 	DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
507*9210f05bSAxel Lin 	DA9052_LDO5_6(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
508*9210f05bSAxel Lin 	DA9052_LDO5_6(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
509*9210f05bSAxel Lin 	DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
510*9210f05bSAxel Lin 	DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
511*9210f05bSAxel Lin 	DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
512*9210f05bSAxel Lin 	DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
513*9210f05bSAxel Lin 	DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
514*9210f05bSAxel Lin 	DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
515*9210f05bSAxel Lin 	DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
51608bf1c0aSAshish Jangam };
51708bf1c0aSAshish Jangam 
51808bf1c0aSAshish Jangam static inline struct da9052_regulator_info *find_regulator_info(u8 chip_id,
51908bf1c0aSAshish Jangam 								 int id)
52008bf1c0aSAshish Jangam {
52108bf1c0aSAshish Jangam 	struct da9052_regulator_info *info;
52208bf1c0aSAshish Jangam 	int i;
52308bf1c0aSAshish Jangam 
524984b5a6bSAshish Jangam 	switch (chip_id) {
525984b5a6bSAshish Jangam 	case DA9052:
52608bf1c0aSAshish Jangam 		for (i = 0; i < ARRAY_SIZE(da9052_regulator_info); i++) {
52708bf1c0aSAshish Jangam 			info = &da9052_regulator_info[i];
52808bf1c0aSAshish Jangam 			if (info->reg_desc.id == id)
52908bf1c0aSAshish Jangam 				return info;
53008bf1c0aSAshish Jangam 		}
531984b5a6bSAshish Jangam 		break;
532984b5a6bSAshish Jangam 	case DA9053_AA:
533984b5a6bSAshish Jangam 	case DA9053_BA:
534984b5a6bSAshish Jangam 	case DA9053_BB:
53508bf1c0aSAshish Jangam 		for (i = 0; i < ARRAY_SIZE(da9053_regulator_info); i++) {
53608bf1c0aSAshish Jangam 			info = &da9053_regulator_info[i];
53708bf1c0aSAshish Jangam 			if (info->reg_desc.id == id)
53808bf1c0aSAshish Jangam 				return info;
53908bf1c0aSAshish Jangam 		}
540984b5a6bSAshish Jangam 		break;
54108bf1c0aSAshish Jangam 	}
54208bf1c0aSAshish Jangam 
54308bf1c0aSAshish Jangam 	return NULL;
54408bf1c0aSAshish Jangam }
54508bf1c0aSAshish Jangam 
54608bf1c0aSAshish Jangam static int __devinit da9052_regulator_probe(struct platform_device *pdev)
54708bf1c0aSAshish Jangam {
54808bf1c0aSAshish Jangam 	struct da9052_regulator *regulator;
54908bf1c0aSAshish Jangam 	struct da9052 *da9052;
55008bf1c0aSAshish Jangam 	struct da9052_pdata *pdata;
55108bf1c0aSAshish Jangam 	int ret;
55208bf1c0aSAshish Jangam 
553984b5a6bSAshish Jangam 	regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9052_regulator),
554984b5a6bSAshish Jangam 				 GFP_KERNEL);
55508bf1c0aSAshish Jangam 	if (!regulator)
55608bf1c0aSAshish Jangam 		return -ENOMEM;
55708bf1c0aSAshish Jangam 
55808bf1c0aSAshish Jangam 	da9052 = dev_get_drvdata(pdev->dev.parent);
55908bf1c0aSAshish Jangam 	pdata = da9052->dev->platform_data;
56008bf1c0aSAshish Jangam 	regulator->da9052 = da9052;
56108bf1c0aSAshish Jangam 
56208bf1c0aSAshish Jangam 	regulator->info = find_regulator_info(regulator->da9052->chip_id,
56308bf1c0aSAshish Jangam 					      pdev->id);
56408bf1c0aSAshish Jangam 	if (regulator->info == NULL) {
56508bf1c0aSAshish Jangam 		dev_err(&pdev->dev, "invalid regulator ID specified\n");
56608bf1c0aSAshish Jangam 		ret = -EINVAL;
56708bf1c0aSAshish Jangam 		goto err;
56808bf1c0aSAshish Jangam 	}
56908bf1c0aSAshish Jangam 	regulator->rdev = regulator_register(&regulator->info->reg_desc,
57008bf1c0aSAshish Jangam 					     &pdev->dev,
57108bf1c0aSAshish Jangam 					     pdata->regulators[pdev->id],
57247aed92cSMark Brown 					     regulator, NULL);
57308bf1c0aSAshish Jangam 	if (IS_ERR(regulator->rdev)) {
57408bf1c0aSAshish Jangam 		dev_err(&pdev->dev, "failed to register regulator %s\n",
57508bf1c0aSAshish Jangam 			regulator->info->reg_desc.name);
57608bf1c0aSAshish Jangam 		ret = PTR_ERR(regulator->rdev);
57708bf1c0aSAshish Jangam 		goto err;
57808bf1c0aSAshish Jangam 	}
57908bf1c0aSAshish Jangam 
58008bf1c0aSAshish Jangam 	platform_set_drvdata(pdev, regulator);
58108bf1c0aSAshish Jangam 
58208bf1c0aSAshish Jangam 	return 0;
58308bf1c0aSAshish Jangam err:
584984b5a6bSAshish Jangam 	devm_kfree(&pdev->dev, regulator);
58508bf1c0aSAshish Jangam 	return ret;
58608bf1c0aSAshish Jangam }
58708bf1c0aSAshish Jangam 
58808bf1c0aSAshish Jangam static int __devexit da9052_regulator_remove(struct platform_device *pdev)
58908bf1c0aSAshish Jangam {
59008bf1c0aSAshish Jangam 	struct da9052_regulator *regulator = platform_get_drvdata(pdev);
59108bf1c0aSAshish Jangam 
59208bf1c0aSAshish Jangam 	regulator_unregister(regulator->rdev);
593984b5a6bSAshish Jangam 	devm_kfree(&pdev->dev, regulator);
59408bf1c0aSAshish Jangam 
59508bf1c0aSAshish Jangam 	return 0;
59608bf1c0aSAshish Jangam }
59708bf1c0aSAshish Jangam 
59808bf1c0aSAshish Jangam static struct platform_driver da9052_regulator_driver = {
59908bf1c0aSAshish Jangam 	.probe = da9052_regulator_probe,
60008bf1c0aSAshish Jangam 	.remove = __devexit_p(da9052_regulator_remove),
60108bf1c0aSAshish Jangam 	.driver = {
60208bf1c0aSAshish Jangam 		.name = "da9052-regulator",
60308bf1c0aSAshish Jangam 		.owner = THIS_MODULE,
60408bf1c0aSAshish Jangam 	},
60508bf1c0aSAshish Jangam };
60608bf1c0aSAshish Jangam 
60708bf1c0aSAshish Jangam static int __init da9052_regulator_init(void)
60808bf1c0aSAshish Jangam {
60908bf1c0aSAshish Jangam 	return platform_driver_register(&da9052_regulator_driver);
61008bf1c0aSAshish Jangam }
61108bf1c0aSAshish Jangam subsys_initcall(da9052_regulator_init);
61208bf1c0aSAshish Jangam 
61308bf1c0aSAshish Jangam static void __exit da9052_regulator_exit(void)
61408bf1c0aSAshish Jangam {
61508bf1c0aSAshish Jangam 	platform_driver_unregister(&da9052_regulator_driver);
61608bf1c0aSAshish Jangam }
61708bf1c0aSAshish Jangam module_exit(da9052_regulator_exit);
61808bf1c0aSAshish Jangam 
61908bf1c0aSAshish Jangam MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
62008bf1c0aSAshish Jangam MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9052 PMIC");
62108bf1c0aSAshish Jangam MODULE_LICENSE("GPL");
62208bf1c0aSAshish Jangam MODULE_ALIAS("platform:da9052-regulator");
623