regulator: regulator driver for Palmas series chips
[linux-2.6.git] / drivers / regulator / tps6507x-regulator.c
index 8152d65..832833f 100644 (file)
@@ -90,12 +90,6 @@ static const u16 LDO2_VSEL_table[] = {
        3000, 3100, 3200, 3300,
 };
 
-static unsigned int num_voltages[] = {ARRAY_SIZE(VDCDCx_VSEL_table),
-                               ARRAY_SIZE(VDCDCx_VSEL_table),
-                               ARRAY_SIZE(VDCDCx_VSEL_table),
-                               ARRAY_SIZE(LDO1_VSEL_table),
-                               ARRAY_SIZE(LDO2_VSEL_table)};
-
 struct tps_info {
        const char *name;
        unsigned min_uV;
@@ -244,34 +238,16 @@ static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
        return err;
 }
 
-static int tps6507x_pmic_dcdc_is_enabled(struct regulator_dev *dev)
-{
-       struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
-       int data, dcdc = rdev_get_id(dev);
-       u8 shift;
-
-       if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
-               return -EINVAL;
-
-       shift = TPS6507X_MAX_REG_ID - dcdc;
-       data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
-
-       if (data < 0)
-               return data;
-       else
-               return (data & 1<<shift) ? 1 : 0;
-}
-
-static int tps6507x_pmic_ldo_is_enabled(struct regulator_dev *dev)
+static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
 {
        struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
-       int data, ldo = rdev_get_id(dev);
+       int data, rid = rdev_get_id(dev);
        u8 shift;
 
-       if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
+       if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
                return -EINVAL;
 
-       shift = TPS6507X_MAX_REG_ID - ldo;
+       shift = TPS6507X_MAX_REG_ID - rid;
        data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
 
        if (data < 0)
@@ -280,81 +256,65 @@ static int tps6507x_pmic_ldo_is_enabled(struct regulator_dev *dev)
                return (data & 1<<shift) ? 1 : 0;
 }
 
-static int tps6507x_pmic_dcdc_enable(struct regulator_dev *dev)
-{
-       struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
-       int dcdc = rdev_get_id(dev);
-       u8 shift;
-
-       if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
-               return -EINVAL;
-
-       shift = TPS6507X_MAX_REG_ID - dcdc;
-       return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
-}
-
-static int tps6507x_pmic_dcdc_disable(struct regulator_dev *dev)
-{
-       struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
-       int dcdc = rdev_get_id(dev);
-       u8 shift;
-
-       if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
-               return -EINVAL;
-
-       shift = TPS6507X_MAX_REG_ID - dcdc;
-       return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
-                                       1 << shift);
-}
-
-static int tps6507x_pmic_ldo_enable(struct regulator_dev *dev)
+static int tps6507x_pmic_enable(struct regulator_dev *dev)
 {
        struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
-       int ldo = rdev_get_id(dev);
+       int rid = rdev_get_id(dev);
        u8 shift;
 
-       if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
+       if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
                return -EINVAL;
 
-       shift = TPS6507X_MAX_REG_ID - ldo;
+       shift = TPS6507X_MAX_REG_ID - rid;
        return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
 }
 
-static int tps6507x_pmic_ldo_disable(struct regulator_dev *dev)
+static int tps6507x_pmic_disable(struct regulator_dev *dev)
 {
        struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
-       int ldo = rdev_get_id(dev);
+       int rid = rdev_get_id(dev);
        u8 shift;
 
-       if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
+       if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
                return -EINVAL;
 
-       shift = TPS6507X_MAX_REG_ID - ldo;
+       shift = TPS6507X_MAX_REG_ID - rid;
        return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
                                        1 << shift);
 }
 
-static int tps6507x_pmic_dcdc_get_voltage(struct regulator_dev *dev)
+static int tps6507x_pmic_get_voltage(struct regulator_dev *dev)
 {
        struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
-       int data, dcdc = rdev_get_id(dev);
-       u8 reg;
+       int data, rid = rdev_get_id(dev);
+       u8 reg, mask;
 
-       switch (dcdc) {
+       switch (rid) {
        case TPS6507X_DCDC_1:
                reg = TPS6507X_REG_DEFDCDC1;
+               mask = TPS6507X_DEFDCDCX_DCDC_MASK;
                break;
        case TPS6507X_DCDC_2:
-               if (tps->info[dcdc]->defdcdc_default)
+               if (tps->info[rid]->defdcdc_default)
                        reg = TPS6507X_REG_DEFDCDC2_HIGH;
                else
                        reg = TPS6507X_REG_DEFDCDC2_LOW;
+               mask = TPS6507X_DEFDCDCX_DCDC_MASK;
                break;
        case TPS6507X_DCDC_3:
-               if (tps->info[dcdc]->defdcdc_default)
+               if (tps->info[rid]->defdcdc_default)
                        reg = TPS6507X_REG_DEFDCDC3_HIGH;
                else
                        reg = TPS6507X_REG_DEFDCDC3_LOW;
+               mask = TPS6507X_DEFDCDCX_DCDC_MASK;
+               break;
+       case TPS6507X_LDO_1:
+               reg = TPS6507X_REG_LDO_CTRL1;
+               mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
+               break;
+       case TPS6507X_LDO_2:
+               reg = TPS6507X_REG_DEFLDO2;
+               mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
                break;
        default:
                return -EINVAL;
@@ -364,190 +324,85 @@ static int tps6507x_pmic_dcdc_get_voltage(struct regulator_dev *dev)
        if (data < 0)
                return data;
 
-       data &= TPS6507X_DEFDCDCX_DCDC_MASK;
-       return tps->info[dcdc]->table[data] * 1000;
+       data &= mask;
+       return tps->info[rid]->table[data] * 1000;
 }
 
-static int tps6507x_pmic_dcdc_set_voltage(struct regulator_dev *dev,
-                               int min_uV, int max_uV)
+static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
+                                         unsigned selector)
 {
        struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
-       int data, vsel, dcdc = rdev_get_id(dev);
-       u8 reg;
+       int data, rid = rdev_get_id(dev);
+       u8 reg, mask;
 
-       switch (dcdc) {
+       switch (rid) {
        case TPS6507X_DCDC_1:
                reg = TPS6507X_REG_DEFDCDC1;
+               mask = TPS6507X_DEFDCDCX_DCDC_MASK;
                break;
        case TPS6507X_DCDC_2:
-               if (tps->info[dcdc]->defdcdc_default)
+               if (tps->info[rid]->defdcdc_default)
                        reg = TPS6507X_REG_DEFDCDC2_HIGH;
                else
                        reg = TPS6507X_REG_DEFDCDC2_LOW;
+               mask = TPS6507X_DEFDCDCX_DCDC_MASK;
                break;
        case TPS6507X_DCDC_3:
-               if (tps->info[dcdc]->defdcdc_default)
+               if (tps->info[rid]->defdcdc_default)
                        reg = TPS6507X_REG_DEFDCDC3_HIGH;
                else
                        reg = TPS6507X_REG_DEFDCDC3_LOW;
+               mask = TPS6507X_DEFDCDCX_DCDC_MASK;
+               break;
+       case TPS6507X_LDO_1:
+               reg = TPS6507X_REG_LDO_CTRL1;
+               mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
+               break;
+       case TPS6507X_LDO_2:
+               reg = TPS6507X_REG_DEFLDO2;
+               mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
                break;
        default:
                return -EINVAL;
        }
 
-       if (min_uV < tps->info[dcdc]->min_uV
-               || min_uV > tps->info[dcdc]->max_uV)
-               return -EINVAL;
-       if (max_uV < tps->info[dcdc]->min_uV
-               || max_uV > tps->info[dcdc]->max_uV)
-               return -EINVAL;
-
-       for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) {
-               int mV = tps->info[dcdc]->table[vsel];
-               int uV = mV * 1000;
-
-               /* Break at the first in-range value */
-               if (min_uV <= uV && uV <= max_uV)
-                       break;
-       }
-
-       /* write to the register in case we found a match */
-       if (vsel == tps->info[dcdc]->table_len)
-               return -EINVAL;
-
-       data = tps6507x_pmic_reg_read(tps, reg);
-       if (data < 0)
-               return data;
-
-       data &= ~TPS6507X_DEFDCDCX_DCDC_MASK;
-       data |= vsel;
-
-       return tps6507x_pmic_reg_write(tps, reg, data);
-}
-
-static int tps6507x_pmic_ldo_get_voltage(struct regulator_dev *dev)
-{
-       struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
-       int data, ldo = rdev_get_id(dev);
-       u8 reg, mask;
-
-       if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
-               return -EINVAL;
-       else {
-               reg = (ldo == TPS6507X_LDO_1 ?
-                       TPS6507X_REG_LDO_CTRL1 : TPS6507X_REG_DEFLDO2);
-               mask = (ldo == TPS6507X_LDO_1 ?
-                       TPS6507X_REG_LDO_CTRL1_LDO1_MASK :
-                               TPS6507X_REG_DEFLDO2_LDO2_MASK);
-       }
-
-       data = tps6507x_pmic_reg_read(tps, reg);
-       if (data < 0)
-               return data;
-
-       data &= mask;
-       return tps->info[ldo]->table[data] * 1000;
-}
-
-static int tps6507x_pmic_ldo_set_voltage(struct regulator_dev *dev,
-                               int min_uV, int max_uV)
-{
-       struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
-       int data, vsel, ldo = rdev_get_id(dev);
-       u8 reg, mask;
-
-       if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
-               return -EINVAL;
-       else {
-               reg = (ldo == TPS6507X_LDO_1 ?
-                       TPS6507X_REG_LDO_CTRL1 : TPS6507X_REG_DEFLDO2);
-               mask = (ldo == TPS6507X_LDO_1 ?
-                       TPS6507X_REG_LDO_CTRL1_LDO1_MASK :
-                               TPS6507X_REG_DEFLDO2_LDO2_MASK);
-       }
-
-       if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV)
-               return -EINVAL;
-       if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV)
-               return -EINVAL;
-
-       for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) {
-               int mV = tps->info[ldo]->table[vsel];
-               int uV = mV * 1000;
-
-               /* Break at the first in-range value */
-               if (min_uV <= uV && uV <= max_uV)
-                       break;
-       }
-
-       if (vsel == tps->info[ldo]->table_len)
-               return -EINVAL;
-
        data = tps6507x_pmic_reg_read(tps, reg);
        if (data < 0)
                return data;
 
        data &= ~mask;
-       data |= vsel;
+       data |= selector;
 
        return tps6507x_pmic_reg_write(tps, reg, data);
 }
 
-static int tps6507x_pmic_dcdc_list_voltage(struct regulator_dev *dev,
-                                       unsigned selector)
-{
-       struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
-       int dcdc = rdev_get_id(dev);
-
-       if (dcdc < TPS6507X_DCDC_1 || dcdc > TPS6507X_DCDC_3)
-               return -EINVAL;
-
-       if (selector >= tps->info[dcdc]->table_len)
-               return -EINVAL;
-       else
-               return tps->info[dcdc]->table[selector] * 1000;
-}
-
-static int tps6507x_pmic_ldo_list_voltage(struct regulator_dev *dev,
+static int tps6507x_pmic_list_voltage(struct regulator_dev *dev,
                                        unsigned selector)
 {
        struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
-       int ldo = rdev_get_id(dev);
+       int rid = rdev_get_id(dev);
 
-       if (ldo < TPS6507X_LDO_1 || ldo > TPS6507X_LDO_2)
+       if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
                return -EINVAL;
 
-       if (selector >= tps->info[ldo]->table_len)
+       if (selector >= tps->info[rid]->table_len)
                return -EINVAL;
        else
-               return tps->info[ldo]->table[selector] * 1000;
+               return tps->info[rid]->table[selector] * 1000;
 }
 
-/* Operations permitted on VDCDCx */
-static struct regulator_ops tps6507x_pmic_dcdc_ops = {
-       .is_enabled = tps6507x_pmic_dcdc_is_enabled,
-       .enable = tps6507x_pmic_dcdc_enable,
-       .disable = tps6507x_pmic_dcdc_disable,
-       .get_voltage = tps6507x_pmic_dcdc_get_voltage,
-       .set_voltage = tps6507x_pmic_dcdc_set_voltage,
-       .list_voltage = tps6507x_pmic_dcdc_list_voltage,
-};
-
-/* Operations permitted on LDOx */
-static struct regulator_ops tps6507x_pmic_ldo_ops = {
-       .is_enabled = tps6507x_pmic_ldo_is_enabled,
-       .enable = tps6507x_pmic_ldo_enable,
-       .disable = tps6507x_pmic_ldo_disable,
-       .get_voltage = tps6507x_pmic_ldo_get_voltage,
-       .set_voltage = tps6507x_pmic_ldo_set_voltage,
-       .list_voltage = tps6507x_pmic_ldo_list_voltage,
+static struct regulator_ops tps6507x_pmic_ops = {
+       .is_enabled = tps6507x_pmic_is_enabled,
+       .enable = tps6507x_pmic_enable,
+       .disable = tps6507x_pmic_disable,
+       .get_voltage = tps6507x_pmic_get_voltage,
+       .set_voltage_sel = tps6507x_pmic_set_voltage_sel,
+       .list_voltage = tps6507x_pmic_list_voltage,
 };
 
-static __devinit
-int tps6507x_pmic_probe(struct platform_device *pdev)
+static __devinit int tps6507x_pmic_probe(struct platform_device *pdev)
 {
        struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
-       static int desc_id;
        struct tps_info *info = &tps6507x_pmic_regs[0];
        struct regulator_init_data *init_data;
        struct regulator_dev *rdev;
@@ -592,15 +447,14 @@ int tps6507x_pmic_probe(struct platform_device *pdev)
                }
 
                tps->desc[i].name = info->name;
-               tps->desc[i].id = desc_id++;
-               tps->desc[i].n_voltages = num_voltages[i];
-               tps->desc[i].ops = (i > TPS6507X_DCDC_3 ?
-               &tps6507x_pmic_ldo_ops : &tps6507x_pmic_dcdc_ops);
+               tps->desc[i].id = i;
+               tps->desc[i].n_voltages = info->table_len;
+               tps->desc[i].ops = &tps6507x_pmic_ops;
                tps->desc[i].type = REGULATOR_VOLTAGE;
                tps->desc[i].owner = THIS_MODULE;
 
                rdev = regulator_register(&tps->desc[i],
-                                       tps6507x_dev->dev, init_data, tps);
+                                       tps6507x_dev->dev, init_data, tps, NULL);
                if (IS_ERR(rdev)) {
                        dev_err(tps6507x_dev->dev,
                                "failed to register %s regulator\n",
@@ -614,6 +468,7 @@ int tps6507x_pmic_probe(struct platform_device *pdev)
        }
 
        tps6507x_dev->pmic = tps;
+       platform_set_drvdata(pdev, tps6507x_dev);
 
        return 0;
 
@@ -625,12 +480,6 @@ fail:
        return error;
 }
 
-/**
- * tps6507x_remove - TPS6507x driver i2c remove handler
- * @client: i2c driver client device structure
- *
- * Unregister TPS driver as an i2c client device driver
- */
 static int __devexit tps6507x_pmic_remove(struct platform_device *pdev)
 {
        struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev);
@@ -654,22 +503,12 @@ static struct platform_driver tps6507x_pmic_driver = {
        .remove = __devexit_p(tps6507x_pmic_remove),
 };
 
-/**
- * tps6507x_pmic_init
- *
- * Module init function
- */
 static int __init tps6507x_pmic_init(void)
 {
        return platform_driver_register(&tps6507x_pmic_driver);
 }
 subsys_initcall(tps6507x_pmic_init);
 
-/**
- * tps6507x_pmic_cleanup
- *
- * Module exit function
- */
 static void __exit tps6507x_pmic_cleanup(void)
 {
        platform_driver_unregister(&tps6507x_pmic_driver);