Skip to contentSkip to conversation

mfd: regulator: max8998: BUCK1/2 control augmented by GPIO pins

PATCH New Lukasz Majewski 2 participants 6 messages Sep 21, 2010 – Sep 24, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mfd: regulator: max8998: BUCK1/2 control augmented by GPIO pins

This patch facilitates the faster voltage change for BUCK1/2 regulators. It uses MAX8998 GPIO pins to control output voltage. This feature is extremely useful for Dynamic Voltage Scalling (DVS) implementation.
This patch has been tested at Samsung GONI and AQUILA targets. Corresponding platform code will be submitted in a separate patch.

---
 drivers/mfd/max8998.c               |    5 +-
 drivers/regulator/max8998.c         |  192 +++++++++++++++++++++++++++++++----
 include/linux/mfd/max8998-private.h |    9 ++
 include/linux/mfd/max8998.h         |   42 ++++++++
 4 files changed, 228 insertions(+), 20 deletions(-)
drivers/mfd/max8998.c +3 -2
index 310fd80..a720f41 100644
@@ -133,6 +133,7 @@ static int max8998_i2c_probe(struct i2c_client *i2c,
 	max8998->dev = &i2c->dev;
 	max8998->i2c = i2c;
 	max8998->irq = i2c->irq;
+	max8998->type = id->driver_data;
 	if (pdata) {
 		max8998->ono = pdata->ono;
 		max8998->irq_base = pdata->irq_base;
@@ -169,8 +170,8 @@ static int max8998_i2c_remove(struct i2c_client *i2c)
 }
 
 static const struct i2c_device_id max8998_i2c_id[] = {
-	{ "max8998", 0 },
-	{ "lp3974", 0 },
+	{ "max8998", TYPE_MAX8998 },
+	{ "lp3974", TYPE_LP3974},
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, max8998_i2c_id);
drivers/regulator/max8998.c +174 -18

272 lines not on this page · raw message

include/linux/mfd/max8998-private.h +9 -0
index 170f665..1a990b8 100644
@@ -101,6 +101,13 @@ enum {
 	MAX8998_IRQ_NR,
 };
 
+/* MAX8998 various variants */
+enum {
+	TYPE_MAX8998 = 0, /* Default */
+	TYPE_LP3974,	/* National version of MAX8998 */
+	TYPE_LP3979,	/* Added AVS */
+};
+
 #define MAX8998_IRQ_DCINF_MASK		(1 << 2)
 #define MAX8998_IRQ_DCINR_MASK		(1 << 3)
 #define MAX8998_IRQ_JIGF_MASK		(1 << 4)
@@ -135,6 +142,7 @@ enum {
  * @ono: power onoff IRQ number for max8998
  * @irq_masks_cur: currently active value
  * @irq_masks_cache: cached hardware value
+ * @type: indicate which max8998 "variant" is used
  */
 struct max8998_dev {
 	struct device *dev;
@@ -148,6 +156,7 @@ struct max8998_dev {
 	int ono;
 	u8 irq_masks_cur[MAX8998_NUM_IRQ_REGS];
 	u8 irq_masks_cache[MAX8998_NUM_IRQ_REGS];
+	int type;
 };
 
 int max8998_irq_init(struct max8998_dev *max8998);
include/linux/mfd/max8998.h +42 -0

54 lines not on this page · raw message

Signature · 1 line
1.7.1
#1 lore raw

PATCH 1/1 mfd: regulator: max8998: BUCK1/2 control augmented by GPIO pins

Quoted 5 lines · Lukasz Majewski

This patch facilitates the faster voltage change for BUCK1/2 regulators. It uses MAX8998 GPIO pins to control output voltage. This feature is extremely useful for Dynamic Voltage Scalling (DVS) implementation.
This patch has been tested at Samsung GONI and AQUILA targets. Corresponding platform code will be submitted in a separate patch.

This would be easier to review if split out into a series - there's at least one change here which is basic code motion, splitting the LDO and DCDC handling into separate functions, and probably more.

drivers/regulator/max8998.c struct max8998_data { · line 43

Quoted 2 lines
drivers/regulator/max8998.c +2 -0
+	unsigned int		buck1_idx;
+	unsigned int		buck2_idx;

Comments explaining what this is for might help.

drivers/regulator/max8998.c static int max8998_set_voltage(struct regulator_dev *rdev, · line 399

Quoted 10 lines
drivers/regulator/max8998.c +10 -0
+	previous_vol = max8998_get_voltage(rdev);
+
+	/* Check if voltage needs to be changed */
+	/* if previous_voltage equal new voltage, return */
+	if (previous_vol == max8998_list_voltage(rdev, i)) {
+		dev_dbg(max8998->dev, "No voltage change, old:%d, new:%d\n",
+			previous_vol, max8998_list_voltage(rdev, i));
+		return ret;
+	}
+	if (ldo == MAX8998_BUCK1) {

Some blank lines would help a lot with the legibility of the code, as would using something other than ldo as the variable identifying the DCDC. Also, this series of if statements should be a switch statement.

drivers/regulator/max8998.c static int max8998_set_voltage(struct regulator_dev *rdev, · line 405

Quoted 5 lines
drivers/regulator/max8998.c +5 -0
+		dvs_arm_no = ARRAY_SIZE(pdata->dvsarm);
+		dev_dbg(max8998->dev,
+			"BUCK1, i:%d, dvs1:%d, dvs2:%d, dvs3:%d, dvs4:%d\n",
+			i, pdata->dvsarm[0], pdata->dvsarm[1],
+			pdata->dvsarm[2], pdata->dvsarm[3]);

All this code is assuming that BUCK1 is supplying the ARM core. While this may be likely the code shouldn't be making that assumption.

drivers/regulator/max8998.c static int max8998_set_voltage(struct regulator_dev *rdev, · line 453

Quoted 9 lines
drivers/regulator/max8998.c +6 -0
+	/* Wait until voltage is stabilized */
+	max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
+	/* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
+	dev_dbg(max8998->dev, "name:%s\n", i2c->name);
+	if (max8998->iodev->type == TYPE_LP3974) {
+		difference = desc->min + desc->step*i - previous_vol/1000;
 		if (difference > 0)
 			udelay(difference / ((val & 0x0f) + 1));
 	}

This code shouldn't be here, you should be providing an enable_time() function.

drivers/regulator/max8998.c static __devinit int max8998_pmic_probe(struct platform_device *pdev) · line 693

Quoted 4 lines
drivers/regulator/max8998.c +4 -0
+	/* Check if platform data for max8998 has been declared */
+	if (pdata->dvsarm[0] != 0 && pdata->dvsarm[1] != 0 &&
+	    pdata->dvsarm[2] != 0 && pdata->dvsarm[3] != 0 &&
+	    pdata->dvsint[0] != 0 && pdata->dvsint[1] != 0) {

Why must GPIOs be specified for both regulators? The user should be able to set only one up to use GPIOs.

drivers/regulator/max8998.c static __devinit int max8998_pmic_probe(struct platform_device *pdev) · line 711

Quoted 3 lines
drivers/regulator/max8998.c +3 -0
+			/* Set default values */
+			max8998->buck1_idx = 3;
+			max8998->buck2_idx = 1;

This looks awfully like it should come from platform data.

drivers/regulator/max8998.c static __devinit int max8998_pmic_probe(struct platform_device *pdev) · line 713

Quoted 1 line
drivers/regulator/max8998.c +1 -0
+	if (pdata->set1 != 0 && pdata->set2 != 0 && pdata->set3 != 0) {

gpio_is_valid().

drivers/regulator/max8998.c static __devinit int max8998_pmic_probe(struct platform_device *pdev) · line 716

Quoted 3 lines
drivers/regulator/max8998.c +3 -0
+			gpio_request(pdata->set1, "PMIC SET1");
+			gpio_direction_output(pdata->set1,
+					      max8998->buck1_idx & 0x1);

Use the name of the chip rather than PMIC, there may be more than one PMIC.

include/linux/mfd/max8998.h enum { · line 58

Quoted 2 lines
include/linux/mfd/max8998.h +2 -0
+enum {
+	MAX8998_DVS_750mV,

I suspect the actual values for this enum are important, I'd expect to see them forced with an explicit = somewhere. I'd also expect this to be hidden in the driver code and users just to specify voltages directly. You already have the code to map voltages into these enums inside the driver so no need to expose it to users.

include/linux/mfd/max8998.h struct max8998_platform_data { · line 117

Quoted 11 lines

/**
* max8998_regulator_data - regulator data
* @id: regulator id

include/linux/mfd/max8998.h +4 -0
@@ -76,6 +111,13 @@ struct max8998_platform_data {
 	int				num_regulators;
 	int				irq_base;
 	int				ono;
+	u8              dvsarm[4];
+	u8		dvsint[2];
+	u8              dvsarm_init;
+	u8              dvsint_init;

As mentioned previously these should be named after the regulators, not after their uses on your boards. You also need some documentation explaining what to do with these fields, they're not immediately obvious.

For the voltage selection values I'm somewhat surprised to see them specified in the platform data - I would instead expect to see them figured out at runtime based on the voltages that are being set.

include/linux/mfd/max8998.h struct max8998_platform_data { · line 120

Quoted 3 lines
include/linux/mfd/max8998.h +3 -0
+	int		set1;
+	int		set2;
+	int		set3;

These need documentation too.

#2 lore raw

PATCH 1/1 mfd: regulator: max8998: BUCK1/2 control augmented by GPIO pins

Quoted 116 lines · On Tue, 21 Sep 2010 16:01:08 +0100 Mark Brown…
Quoted 6 lines · Lukasz Majewski

This patch facilitates the faster voltage change for BUCK1/2 regulators. It uses MAX8998 GPIO pins to control output voltage. This feature is extremely useful for Dynamic Voltage Scalling (DVS) implementation. This patch has been tested at Samsung GONI and AQUILA targets. Corresponding platform code will be submitted in a separate patch.

This would be easier to review if split out into a series - there's at least one change here which is basic code motion, splitting the LDO and DCDC handling into separate functions, and probably more.

drivers/regulator/max8998.c struct max8998_data { · line 43

Quoted 2 lines
drivers/regulator/max8998.c +2 -0
+	unsigned int		buck1_idx;
+	unsigned int		buck2_idx;

Comments explaining what this is for might help.

drivers/regulator/max8998.c static int max8998_set_voltage(struct regulator_dev *rdev, · line 399

Quoted 12 lines
drivers/regulator/max8998.c +6 -0
+	previous_vol = max8998_get_voltage(rdev);
+
+	/* Check if voltage needs to be changed */
+	/* if previous_voltage equal new voltage, return */
+	if (previous_vol == max8998_list_voltage(rdev, i)) {
+		dev_dbg(max8998->dev, "No voltage change, old:%d,

new:%d\n",

(quoted diff) +1 -0
+			previous_vol, max8998_list_voltage(rdev,

i));

drivers/regulator/max8998.c +3 -0
+		return ret;
+	}
+	if (ldo == MAX8998_BUCK1) {

Some blank lines would help a lot with the legibility of the code, as would using something other than ldo as the variable identifying the DCDC. Also, this series of if statements should be a switch statement.

drivers/regulator/max8998.c static int max8998_set_voltage(struct regulator_dev *rdev, · line 405

Quoted 6 lines
drivers/regulator/max8998.c +3 -0
+		dvs_arm_no = ARRAY_SIZE(pdata->dvsarm);
+		dev_dbg(max8998->dev,
+			"BUCK1, i:%d, dvs1:%d, dvs2:%d, dvs3:%d,

dvs4:%d\n",

drivers/regulator/max8998.c +2 -0
+			i, pdata->dvsarm[0], pdata->dvsarm[1],
+			pdata->dvsarm[2], pdata->dvsarm[3]);

All this code is assuming that BUCK1 is supplying the ARM core. While this may be likely the code shouldn't be making that assumption.

drivers/regulator/max8998.c static int max8998_set_voltage(struct regulator_dev *rdev, · line 453

Quoted 10 lines
drivers/regulator/max8998.c +3 -0
+	/* Wait until voltage is stabilized */
+	max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
+	/* lp3974 hasn't got ENRAMP bit - ramp is assumed as true

*/

drivers/regulator/max8998.c +3 -0
+	dev_dbg(max8998->dev, "name:%s\n", i2c->name);
+	if (max8998->iodev->type == TYPE_LP3974) {
+		difference = desc->min + desc->step*i -

previous_vol/1000; if (difference > 0)

drivers/regulator/max8998.c +0 -0
 			udelay(difference / ((val & 0x0f) + 1));
 	}

This code shouldn't be here, you should be providing an enable_time() function.

drivers/regulator/max8998.c static __devinit int max8998_pmic_probe(struct platform_device *pdev) · line 693

Quoted 4 lines
drivers/regulator/max8998.c +4 -0
+	/* Check if platform data for max8998 has been declared */
+	if (pdata->dvsarm[0] != 0 && pdata->dvsarm[1] != 0 &&
+	    pdata->dvsarm[2] != 0 && pdata->dvsarm[3] != 0 &&
+	    pdata->dvsint[0] != 0 && pdata->dvsint[1] != 0) {

Why must GPIOs be specified for both regulators? The user should be able to set only one up to use GPIOs.

drivers/regulator/max8998.c static __devinit int max8998_pmic_probe(struct platform_device *pdev) · line 711

Quoted 3 lines
drivers/regulator/max8998.c +3 -0
+			/* Set default values */
+			max8998->buck1_idx = 3;
+			max8998->buck2_idx = 1;

This looks awfully like it should come from platform data.

Quoted 2 lines
(quoted diff) +1 -0
+	if (pdata->set1 != 0 && pdata->set2 != 0 && pdata->set3 !=

0) {

gpio_is_valid().

Quoted 4 lines
drivers/regulator/max8998.c +3 -0
+			gpio_request(pdata->set1, "PMIC SET1");
+			gpio_direction_output(pdata->set1,
+					      max8998->buck1_idx &

0x1);

Use the name of the chip rather than PMIC, there may be more than one PMIC.

include/linux/mfd/max8998.h enum { · line 58

Quoted 2 lines
include/linux/mfd/max8998.h +2 -0
+enum {
+	MAX8998_DVS_750mV,

I suspect the actual values for this enum are important, I'd expect to see them forced with an explicit = somewhere. I'd also expect this to be hidden in the driver code and users just to specify voltages directly. You already have the code to map voltages into these enums inside the driver so no need to expose it to users.

include/linux/mfd/max8998.h struct max8998_platform_data { · line 117

Quoted 11 lines

/**
* max8998_regulator_data - regulator data
* @id: regulator id

include/linux/mfd/max8998.h +4 -0
@@ -76,6 +111,13 @@ struct max8998_platform_data {
 	int				num_regulators;
 	int				irq_base;
 	int				ono;
+	u8              dvsarm[4];
+	u8		dvsint[2];
+	u8              dvsarm_init;
+	u8              dvsint_init;

As mentioned previously these should be named after the regulators, not after their uses on your boards. You also need some documentation explaining what to do with these fields, they're not immediately obvious.

For the voltage selection values I'm somewhat surprised to see them specified in the platform data - I would instead expect to see them figured out at runtime based on the voltages that are being set.

Quoted 5 lines

include/linux/mfd/max8998.h struct max8998_platform_data { · line 120

Quoted 3 lines
include/linux/mfd/max8998.h +3 -0
+	int		set1;
+	int		set2;
+	int		set3;

These need documentation too.

Hello Mark,

Thank you for valuable comments.

If I can, I'd like to discuss one of them:

Quoted 3 lines

For the voltage selection values I'm somewhat surprised to see them specified in the platform data - I would instead expect to see them figured out at runtime based on the voltages that are being set.

I'd like to ask you for clarification on this statement. Correct me if I'm wrong, but I think that some default voltage values should be declared in platform data (according to the chip manual) to facilitate dynamic voltage scaling feature. I suspect that this information is platform specific and should be adjusted for each chip separately.

Signature · 6 lines
Best regards,

Lukasz Majewski

Samsung Poland R&D Center
Platform Group
#3 lore raw

PATCH 1/1 mfd: regulator: max8998: BUCK1/2 control augmented by GPIO pins

Quoted 11 lines · Lukasz Majewski
Quoted 3 lines · Mark Brown

For the voltage selection values I'm somewhat surprised to see them specified in the platform data - I would instead expect to see them figured out at runtime based on the voltages that are being set.

I'd like to ask you for clarification on this statement. Correct me if I'm wrong, but I think that some default voltage values should be declared in platform data (according to the chip manual) to facilitate dynamic voltage scaling feature. I suspect that this information is platform specific and should be adjusted for each chip separately.

Assume that I've never seen the chip datasheet. What exactly is this platform data configuring?

I would expect to see platform data configuring the default state of the GPIOs at power up but that doesn't seem to be what's being configured here. For anything configured via registers I would expect the driver to read the state of the chip at startup.

#4 lore raw

PATCH 1/1 mfd: regulator: max8998: BUCK1/2 control augmented by GPIO pins

In reply to Mark Brown

Hi Mark,

I'd like to discus the issue with checking if GPIO has been defined at platform code.

As you pointed out user is NOT obliged to define GPIOs (buck1_set1/2 and buck2_set3) at platform data.
It is completely valid to have following definition at platform code:

static struct max8998_platform_data goni_max8998_pdata = {
.num_regulators = ARRAY_SIZE(goni_regulators),
.regulators = goni_regulators,
};

so for the above

struct max8998_platform_data {
	struct max8998_regulator_data	*regulators;
	int				num_regulators;
	int				irq_base;
	int				ono;
	int				buck1_set1;
	int				buck1_set2;
	int				buck2_set3;
};

buck1_set1/2, buck2_set3 are equal to 0.

In the driver it is necessary to distinguish this to choose proper execution path:
1. No GPIOs have been defined at platform - use I2C and default GPIO settings to set voltage for BUCK regulators. It is important to note, that max8998 regulator driver must not alter the GPIOs.
2. GPIOs have been defined at platform data. Use GPIOs to choose proper output voltage for BUCK regulators.

Unfortunately there is problem with this distinction and use of gpio_is_valid.

The gpio_is_valid is simply defined as

return ((unsigned)number) < ARCH_NR_GPIOS;

For which 0 is also a valid GPIO.
One workaround for this is to check explicitly the condition:
pdata->buck1_set1 != 0 , but this is neither elegant nor it prevents the situation when on some architecture GPIO 0 is valid.

In my opinion, better is to define following bitfield to struct max8998_platform_data:
int buck1_set1_gpio_defined :1;
int buck1_set2_gpio_defined :1;
int buck2_set3_gpio_defined :1;

Checking if this is 0 (in a case of not defined GPIO) would solve the problem in a nice way. The quesion is if this solution would be accepted to mainline?

Another, less critical issue:

Quoted 26 lines
Quoted 24 lines
(quoted diff) +1 -0
+		for (j = 0; j < ARRAY_SIZE(pdata->buck1_vol); j++)

{

(quoted diff) +4 -0
+			if (pdata->buck1_vol[j] == i) {
+				max8998->buck1_idx = j;
+				buck1_gpio_set(pdata->buck1_set1,
+					       pdata->buck1_set2,

j);

drivers/regulator/max8998.c +3 -0
+				break;
+			}
+			if (j == (ARRAY_SIZE(pdata->buck1_vol) -

1)) {

drivers/regulator/max8998.c +3 -0
+				/* no predefine regulator found */
+				max8998->buck1_idx = j;
+				ret =

max8998_get_voltage_register(rdev, &reg,
+
&shift,
+
&mask);

(quoted diff) +1 -0
+				ret = max8998_write_reg(i2c, reg,

i);

(quoted diff) +2 -0
+				buck1_gpio_set(pdata->buck1_set1,
+					       pdata->buck1_set2,

j);

Review

Quoted 1 line
(quoted diff) +1 -0
+			}  
Quoted 7 lines

So this essentially reserves one of the voltage selections for register written values and requires the others to be specified as platform data or from the hardware? It'd be nicer to do this using something like rotating through the slots or (better yet) LRU so that the driver just figures out which voltages are being used by the consumer rather than the platform data having to know exactly which voltages will be set by the consumers in advance.

is the replacement algorithm. I think that circular buffer implementation would be a feasible solution for 4/2 bytes elements array.

With more extended functionality, when more voltage levels would be available, more sophisticated approach (like LRU) may be used.

Signature · 6 lines
Best regards,

Lukasz Majewski

Samsung Poland R&D Center
Platform Group
#5 lore raw

PATCH 1/1 mfd: regulator: max8998: BUCK1/2 control augmented by GPIO pins

Quoted 8 lines · Lukasz Majewski

Unfortunately there is problem with this distinction and use of gpio_is_valid.

The gpio_is_valid is simply defined as

return ((unsigned)number) < ARCH_NR_GPIOS;

For which 0 is also a valid GPIO.

I understand that on some platforms it is actually a valid GPIO.

Quoted 3 lines

One workaround for this is to check explicitly the condition:
pdata->buck1_set1 != 0 , but this is neither elegant nor it prevents the situation when on some architecture GPIO 0 is valid.

The standard check is gpio_is_valid() with the GPIO set to -1 if it's not valid.

Quoted 6 lines

is the replacement algorithm. I think that circular buffer implementation would be a feasible solution for 4/2 bytes elements array.

With more extended functionality, when more voltage levels would be available, more sophisticated approach (like LRU) may be used.

The problem here is that this is going to be pretty pessimal if the system is using more than four different voltages; you get no advantage at all from the preprogramming.