Skip to contentSkip to conversation

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

PATCH Superseded Lukasz Majewski 2 participants 6 messages Sep 22, 2010

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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

This patch facilitates the faster voltage change for BUCK1/2 regulators.

Comments and guidlines from Mark Brown have been added.

This patch has been tested at Samsung GONI and AQUILA targets.

Lukasz Majewski (3):
mfd: regulator: max8998: set_voltage_buck routine added
mfd: regulator: max8998: probe routine modified for GPIOs use
mfd: regulator: max8998: mfd code

 drivers/mfd/max8998.c               |    5 +-
 drivers/regulator/max8998.c         |  195 +++++++++++++++++++++++++++++++----
 include/linux/mfd/max8998-private.h |   59 ++++++++++-
 include/linux/mfd/max8998.h         |   14 +++
 4 files changed, 244 insertions(+), 29 deletions(-)
#1 lore raw

PATCH v2 1/3 mfd: regulator: max8998: set_voltage_buck routine added

Separate routine for setting BUCK regulators voltage.

---
 drivers/regulator/max8998.c |  154 +++++++++++++++++++++++++++++++++++++------
 1 files changed, 133 insertions(+), 21 deletions(-)
drivers/regulator/max8998.c +133 -21
index 7f5fe6f..36d214a 100644
@@ -39,6 +39,8 @@ struct max8998_data {
 	struct max8998_dev	*iodev;
 	int			num_regulators;
 	struct regulator_dev	**rdev;
+	unsigned int		buck1_idx; /* voltage index (0 to 3) */
+	unsigned int		buck2_idx; /* voltage index (0 to 1) */
 };
 
 struct voltage_map_desc {
@@ -218,6 +220,7 @@ static int max8998_get_voltage_register(struct regulator_dev *rdev,
 				int *_reg, int *_shift, int *_mask)
 {
 	int ldo = max8998_get_ldo(rdev);
+	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
 	int reg, shift = 0, mask = 0xff;
 
 	switch (ldo) {
@@ -254,10 +257,10 @@ static int max8998_get_voltage_register(struct regulator_dev *rdev,
 		reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
 		break;
 	case MAX8998_BUCK1:
-		reg = MAX8998_REG_BUCK1_DVSARM1;
+		reg = MAX8998_REG_BUCK1_VOL1 + max8998->buck1_idx;
 		break;
 	case MAX8998_BUCK2:
-		reg = MAX8998_REG_BUCK2_DVSINT1;
+		reg = MAX8998_REG_BUCK2_VOL1 + max8998->buck2_idx;
 		break;
 	case MAX8998_BUCK3:
 		reg = MAX8998_REG_BUCK3;
@@ -297,19 +300,16 @@ static int max8998_get_voltage(struct regulator_dev *rdev)
 	return max8998_list_voltage(rdev, val);
 }
 
-static int max8998_set_voltage(struct regulator_dev *rdev,
+static int max8998_set_voltage_ldo(struct regulator_dev *rdev,
 				int min_uV, int max_uV)
 {
 	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
 	struct i2c_client *i2c = max8998->iodev->i2c;
 	int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
-	int previous_vol = 0;
 	const struct voltage_map_desc *desc;
 	int ldo = max8998_get_ldo(rdev);
 	int reg, shift = 0, mask, ret;
 	int i = 0;
-	u8 val;
-	bool en_ramp = false;
 
 	if (ldo >= ARRAY_SIZE(ldo_voltage_map))
 		return -EINVAL;
@@ -332,24 +332,135 @@ static int max8998_set_voltage(struct regulator_dev *rdev,
 	if (ret)
 		return ret;
 
-	/* wait for RAMP_UP_DELAY if rdev is BUCK1/2 and
-	 * ENRAMP is ON */
-	if (ldo == MAX8998_BUCK1 || ldo == MAX8998_BUCK2) {
-		max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
-		if (val & (1 << 4)) {
-			en_ramp = true;
-			previous_vol = max8998_get_voltage(rdev);
-		}
+	ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
+
+	return ret;
+}
+
+static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
+{
+	gpio_set_value(gpio1, v & 0x1);
+	gpio_set_value(gpio2, (v >> 1) & 0x1);
+}
+
+static inline void buck2_gpio_set(int gpio, int v)
+{
+	gpio_set_value(gpio, v & 0x1);
+}
+
+static int max8998_set_voltage_buck(struct regulator_dev *rdev,
+				    int min_uV, int max_uV)
+{
+	struct max8998_data *max8998 = rdev_get_drvdata(rdev);
+	struct max8998_platform_data *pdata =
+		dev_get_platdata(max8998->iodev->dev);
+	struct i2c_client *i2c = max8998->iodev->i2c;
+	int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
+	const struct voltage_map_desc *desc;
+	int buck = max8998_get_ldo(rdev);
+	int reg, shift = 0, mask, ret;
+	int difference = 0, i = 0, j = 0, previous_vol = 0;
+	u8 val = 0;
+
+	if (buck >= ARRAY_SIZE(ldo_voltage_map))
+		return -EINVAL;
+
+	desc = ldo_voltage_map[buck];
+	if (desc == NULL)
+		return -EINVAL;
+
+	if (max_vol < desc->min || min_vol > desc->max)
+		return -EINVAL;
+
+	while (desc->min + desc->step*i < min_vol &&
+	       desc->min + desc->step*i < desc->max)
+		i++;
+
+	if (desc->min + desc->step*i > max_vol)
+		return -EINVAL;
+
+	ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
+	if (ret)
+		return ret;
+
+	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;
 	}
 
-	ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
+	switch (buck) {
+	case MAX8998_BUCK1:
+		dev_dbg(max8998->dev,
+			"BUCK1, i:%d, buck1_vol1:%d, buck1_vol2:%d\n\
+			 buck1_vol3:%d, buck1_vol4:%d\n",
+			i, pdata->buck1_vol[0], pdata->buck1_vol[1],
+			pdata->buck1_vol[2], pdata->buck1_vol[3]);
+
+		for (j = 0; j < ARRAY_SIZE(pdata->buck1_vol); j++) {
+			if (pdata->buck1_vol[j] == i) {
+				max8998->buck1_idx = j;
+				buck1_gpio_set(pdata->buck1_set1,
+					       pdata->buck1_set2, j);
+				break;
+			}
+			if (j == (ARRAY_SIZE(pdata->buck1_vol) - 1)) {
+				/* no predefine regulator found */
+				max8998->buck1_idx = j;
+				ret = max8998_get_voltage_register(rdev, &reg,
+								   &shift,
+								   &mask);
+				ret = max8998_write_reg(i2c, reg, i);
+				buck1_gpio_set(pdata->buck1_set1,
+					       pdata->buck1_set2, j);
+			}
+		}
+		break;
+
+	case MAX8998_BUCK2:
+		dev_dbg(max8998->dev,
+			"BUCK2, i:%d buck2_vol1:%d, buck2_vol2:%d\n"
+			, i, pdata->buck2_vol[0], pdata->buck2_vol[1]);
+
+		if (pdata->buck2_vol[0] == i) {
+			max8998->buck1_idx = 0;
+			buck2_gpio_set(pdata->buck2_set3, 0);
+		} else {
+			max8998->buck1_idx = 1;
+			ret = max8998_get_voltage_register(rdev, &reg,
+							   &shift, &mask);
+			ret = max8998_write_reg(i2c, reg, i);
+			buck2_gpio_set(pdata->buck2_set3, 1);
+		}
+		break;
 
-	if (en_ramp == true) {
-		int difference = desc->min + desc->step*i - previous_vol/1000;
-		if (difference > 0)
-			udelay(difference / ((val & 0x0f) + 1));
+	case MAX8998_BUCK3:
+	case MAX8998_BUCK4:
+		ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
+		break;
 	}
 
+	/* Voltage stabilization */
+	max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
+
+	dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d, SET3:%d\n",
+		i2c->name, gpio_get_value(pdata->buck1_set1),
+		gpio_get_value(pdata->buck1_set2),
+		gpio_get_value(pdata->buck2_set3));
+
+	/* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
+	/* MAX8998 has ENRAMP bit implemented, so test it*/
+	if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
+		return ret;
+
+	difference = desc->min + desc->step*i - previous_vol/1000;
+	if (difference > 0)
+		udelay(difference / ((val & 0x0f) + 1));
+
 	return ret;
 }
 
@@ -359,7 +470,7 @@ static struct regulator_ops max8998_ldo_ops = {
 	.enable			= max8998_ldo_enable,
 	.disable		= max8998_ldo_disable,
 	.get_voltage		= max8998_get_voltage,
-	.set_voltage		= max8998_set_voltage,
+	.set_voltage		= max8998_set_voltage_ldo,
 	.set_suspend_enable	= max8998_ldo_enable,
 	.set_suspend_disable	= max8998_ldo_disable,
 };
@@ -370,7 +481,7 @@ static struct regulator_ops max8998_buck_ops = {
 	.enable			= max8998_ldo_enable,
 	.disable		= max8998_ldo_disable,
 	.get_voltage		= max8998_get_voltage,
-	.set_voltage		= max8998_set_voltage,
+	.set_voltage		= max8998_set_voltage_buck,
 	.set_suspend_enable	= max8998_ldo_enable,
 	.set_suspend_disable	= max8998_ldo_disable,
 };
@@ -543,6 +654,7 @@ static __devinit int max8998_pmic_probe(struct platform_device *pdev)
 	struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
 	struct regulator_dev **rdev;
 	struct max8998_data *max8998;
+	struct i2c_client *i2c;
 	int i, ret, size;
 
 	if (!pdata) {
Signature · 1 line
1.7.1
#2 lore raw

PATCH v2 1/3 mfd: regulator: max8998: set_voltage_buck routine added

Quoted 1 line · Lukasz Majewski

Separate routine for setting BUCK regulators voltage.

This is doing more than that...

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

Quoted 2 lines
drivers/regulator/max8998.c +2 -0
+	unsigned int		buck1_idx; /* voltage index (0 to 3) */
+	unsigned int		buck2_idx; /* voltage index (0 to 1) */

...it's also changing things over to use this new GPIO based selection scheme. This is rather missing the point of my suggestion to split the code reorganisation out from the new functionality - the goal is to just have the code motion and then separately add the new functionality.

See also my comments about ensuring each patch in the series is buildable.

Quoted 7 lines
drivers/regulator/max8998.c +7 -0
+		for (j = 0; j < ARRAY_SIZE(pdata->buck1_vol); j++) {
+			if (pdata->buck1_vol[j] == i) {
+				max8998->buck1_idx = j;
+				buck1_gpio_set(pdata->buck1_set1,
+					       pdata->buck1_set2, j);
+				break;
+			}

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

Quoted 10 lines
drivers/regulator/max8998.c +10 -0
+			if (j == (ARRAY_SIZE(pdata->buck1_vol) - 1)) {
+				/* no predefine regulator found */
+				max8998->buck1_idx = j;
+				ret = max8998_get_voltage_register(rdev, &reg,
+								   &shift,
+								   &mask);
+				ret = max8998_write_reg(i2c, reg, i);
+				buck1_gpio_set(pdata->buck1_set1,
+					       pdata->buck1_set2, j);
+			}

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.

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

Quoted 10 lines
drivers/regulator/max8998.c +10 -0
+		if (pdata->buck2_vol[0] == i) {
+			max8998->buck1_idx = 0;
+			buck2_gpio_set(pdata->buck2_set3, 0);
+		} else {
+			max8998->buck1_idx = 1;
+			ret = max8998_get_voltage_register(rdev, &reg,
+							   &shift, &mask);
+			ret = max8998_write_reg(i2c, reg, i);
+			buck2_gpio_set(pdata->buck2_set3, 1);
+		}

Ditto here. The WM831x driver is an example of being smarter about this.

#3 lore raw

PATCH v2 2/3 mfd: regulator: max8998: probe routine modified for GPIOs use

In reply to Lukasz Majewski
---
 drivers/regulator/max8998.c |   41 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 41 insertions(+), 0 deletions(-)
drivers/regulator/max8998.c +41 -0
index 36d214a..873a565 100644
@@ -678,6 +678,47 @@ static __devinit int max8998_pmic_probe(struct platform_device *pdev)
 	max8998->iodev = iodev;
 	max8998->num_regulators = pdata->num_regulators;
 	platform_set_drvdata(pdev, max8998);
+	i2c = max8998->iodev->i2c;
+
+	/* Check if platform data for max8998 has been declared */
+	if (pdata->buck1_vol[0] != 0 && pdata->buck1_vol[1] != 0 &&
+	    pdata->buck1_vol[2] != 0 && pdata->buck1_vol[3] != 0) {
+
+		for (i = 0; i < ARRAY_SIZE(pdata->buck1_vol); ++i) {
+			max8998_write_reg(i2c,
+					  MAX8998_REG_BUCK1_VOL1 + i,
+					  pdata->buck1_vol[i]);
+		}
+
+		/* Set default buck1 index - choose default voltage value */
+		max8998->buck1_idx = pdata->buck1_init_idx;
+	}
+
+	if (pdata->buck2_vol[0] != 0 && pdata->buck2_vol[1] != 0) {
+		max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOL1,
+				  pdata->buck2_vol[0]);
+		max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOL2,
+				  pdata->buck2_vol[1]);
+
+		/* Set default buck2 index - choose default voltage value */
+		max8998->buck2_idx = pdata->buck2_init_idx;
+	}
+
+	if (gpio_is_valid(pdata->buck1_set1) &&
+	    gpio_is_valid(pdata->buck1_set2)) {
+		gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
+		gpio_direction_output(pdata->buck1_set1,
+				      max8998->buck1_idx & 0x1);
+
+		gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
+		gpio_direction_output(pdata->buck1_set2,
+				      (max8998->buck1_idx >> 1) & 0x1);
+	}
+	if (gpio_is_valid(pdata->buck2_set3)) {
+		gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
+		gpio_direction_output(pdata->buck2_set3,
+				      max8998->buck2_idx & 0x1);
+	}
 
 	for (i = 0; i < pdata->num_regulators; i++) {
 		const struct voltage_map_desc *desc;
Signature · 1 line
1.7.1
#4 lore raw

PATCH v2 3/3 mfd: regulator: max8998: mfd code

In reply to Lukasz Majewski
---
 drivers/mfd/max8998.c               |    5 ++-
 include/linux/mfd/max8998-private.h |   59 +++++++++++++++++++++++++++++++---
 include/linux/mfd/max8998.h         |   14 ++++++++
 3 files changed, 70 insertions(+), 8 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);
include/linux/mfd/max8998-private.h +53 -6

89 lines not on this page · raw message

include/linux/mfd/max8998.h +14 -0
index d47ed4c..bc9bcc3 100644
@@ -70,12 +70,26 @@ struct max8998_regulator_data {
  * @num_regulators: number of regultors used
  * @irq_base: base IRQ number for max8998, required for IRQs
  * @ono: power onoff IRQ number for max8998
+ * @buck1_vol: BUCK1 voltage levels
+ * @buck2_vol: BUCK2 voltage levels
+ * @buck1_init_idx: BUCK1 initial voltage index
+ * @buck2_init_idx: BUCK2 initial voltage index
+ * @buck1_set1: BUCK1 gpio pin 1 to set output voltage
+ * @buck1_set2: BUCK1 gpio pin 2 to set output voltage
+ * @buck2_set3: BUCK2 gpio pin to set output voltage
  */
 struct max8998_platform_data {
 	struct max8998_regulator_data	*regulators;
 	int				num_regulators;
 	int				irq_base;
 	int				ono;
+	u8                              buck1_vol[4];
+	u8				buck2_vol[2];
+	u8                              buck1_init_idx;
+	u8                              buck2_init_idx;
+	int				buck1_set1;
+	int				buck1_set2;
+	int				buck2_set3;
 };
 
 #endif /*  __LINUX_MFD_MAX8998_H */
Signature · 1 line
1.7.1
#5 lore raw

PATCH v2 3/3 mfd: regulator: max8998: mfd code

On Wed, Sep 22, 2010 at 05:30:10PM +0200, Lukasz Majewski wrote:

Please provide a more descriptive changelog which says what the functional changes are; it's clear that the changes are in MFD but what do they do?

drivers/mfd/max8998.c static int max8998_i2c_probe(struct i2c_client *i2c, · line 136

Quoted 1 line
drivers/mfd/max8998.c +1 -0
+	max8998->type = id->driver_data;

This needs to be the first patch in the series, I think - the others won't build without this. Each patch in the series should leave the kernel buildable, partly for review (so we see things added before they are used) and partly so that things like git bisect work well.

include/linux/mfd/max8998-private.h enum { · line 107

Quoted 4 lines
include/linux/mfd/max8998-private.h +4 -0
+/* MAX8998 output voltages */
+enum {
+	MAX8998_DVS_750mV = 0,
+	MAX8998_DVS_775mV,

My previous comments about this being better as a value than an enumerated type still apply.

Quoted 2 lines

+ * @buck1_vol: BUCK1 voltage levels
+ * @buck2_vol: BUCK2 voltage levels

So as far as I can tell (and I may be missing something since "voltage levels" and code is all I have) these are the voltages programmed for the various voltage selections at system startup? I would very strongly expect these to be read back from the chip when the driver starts when.

Quoted 2 lines

+ * @buck1_init_idx: BUCK1 initial voltage index
+ * @buck2_init_idx: BUCK2 initial voltage index

It'd be clearer to describe these as the initial GPIO setting.