regulator: palmas: fix build warnings
[linux-2.6.git] / drivers / regulator / wm831x-dcdc.c
index 6e18e56..97bb31d 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/regulator/driver.h>
 #include <linux/regulator/machine.h>
 #include <linux/gpio.h>
+#include <linux/slab.h>
 
 #include <linux/mfd/wm831x/core.h>
 #include <linux/mfd/wm831x/regulator.h>
@@ -119,6 +120,7 @@ static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
                return REGULATOR_MODE_IDLE;
        default:
                BUG();
+               return -EINVAL;
        }
 }
 
@@ -265,23 +267,6 @@ static int wm831x_buckv_select_min_voltage(struct regulator_dev *rdev,
        return vsel;
 }
 
-static int wm831x_buckv_select_max_voltage(struct regulator_dev *rdev,
-                                          int min_uV, int max_uV)
-{
-       u16 vsel;
-
-       if (max_uV < 600000 || max_uV > 1800000)
-               return -EINVAL;
-
-       vsel = ((max_uV - 600000) / 12500) + 8;
-
-       if (wm831x_buckv_list_voltage(rdev, vsel) < min_uV ||
-           wm831x_buckv_list_voltage(rdev, vsel) < max_uV)
-               return -EINVAL;
-
-       return vsel;
-}
-
 static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
 {
        struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
@@ -301,7 +286,7 @@ static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
 }
 
 static int wm831x_buckv_set_voltage(struct regulator_dev *rdev,
-                                   int min_uV, int max_uV)
+                                   int min_uV, int max_uV, unsigned *selector)
 {
        struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
        struct wm831x *wm831x = dcdc->wm831x;
@@ -313,6 +298,8 @@ static int wm831x_buckv_set_voltage(struct regulator_dev *rdev,
        if (vsel < 0)
                return vsel;
 
+       *selector = vsel;
+
        /* If this value is already set then do a GPIO update if we can */
        if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
                return wm831x_buckv_set_dvs(rdev, 0);
@@ -334,28 +321,23 @@ static int wm831x_buckv_set_voltage(struct regulator_dev *rdev,
        if (ret < 0)
                return ret;
 
-       /* Set the high voltage as the DVS voltage.  This is optimised
-        * for CPUfreq usage, most processors will keep the maximum
-        * voltage constant and lower the minimum with the frequency. */
-       vsel = wm831x_buckv_select_max_voltage(rdev, min_uV, max_uV);
-       if (vsel < 0) {
-               /* This should never happen - at worst the same vsel
-                * should be chosen */
-               WARN_ON(vsel < 0);
-               return 0;
+       /*
+        * If this VSEL is higher than the last one we've seen then
+        * remember it as the DVS VSEL.  This is optimised for CPUfreq
+        * usage where we want to get to the highest voltage very
+        * quickly.
+        */
+       if (vsel > dcdc->dvs_vsel) {
+               ret = wm831x_set_bits(wm831x, dvs_reg,
+                                     WM831X_DC1_DVS_VSEL_MASK,
+                                     vsel);
+               if (ret == 0)
+                       dcdc->dvs_vsel = vsel;
+               else
+                       dev_warn(wm831x->dev,
+                                "Failed to set DCDC DVS VSEL: %d\n", ret);
        }
 
-       /* Don't bother if it's the same VSEL we're already using */
-       if (vsel == dcdc->on_vsel)
-               return 0;
-
-       ret = wm831x_set_bits(wm831x, dvs_reg, WM831X_DC1_DVS_VSEL_MASK, vsel);
-       if (ret == 0)
-               dcdc->dvs_vsel = vsel;
-       else
-               dev_warn(wm831x->dev, "Failed to set DCDC DVS VSEL: %d\n",
-                        ret);
-
        return 0;
 }
 
@@ -374,14 +356,14 @@ static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
        return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
 }
 
-static int wm831x_buckv_get_voltage(struct regulator_dev *rdev)
+static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
 {
        struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
 
        if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
-               return wm831x_buckv_list_voltage(rdev, dcdc->dvs_vsel);
+               return dcdc->dvs_vsel;
        else
-               return wm831x_buckv_list_voltage(rdev, dcdc->on_vsel);
+               return dcdc->on_vsel;
 }
 
 /* Current limit options */
@@ -398,13 +380,15 @@ static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
        int i;
 
        for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) {
-               if (max_uA <= wm831x_dcdc_ilim[i])
+               if ((min_uA <= wm831x_dcdc_ilim[i]) &&
+                   (wm831x_dcdc_ilim[i] <= max_uA))
                        break;
        }
        if (i == ARRAY_SIZE(wm831x_dcdc_ilim))
                return -EINVAL;
 
-       return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK, i);
+       return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK,
+                              i << WM831X_DC1_HC_THR_SHIFT);
 }
 
 static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
@@ -418,12 +402,13 @@ static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
        if (val < 0)
                return val;
 
-       return wm831x_dcdc_ilim[val & WM831X_DC1_HC_THR_MASK];
+       val = (val & WM831X_DC1_HC_THR_MASK) >> WM831X_DC1_HC_THR_SHIFT;
+       return wm831x_dcdc_ilim[val];
 }
 
 static struct regulator_ops wm831x_buckv_ops = {
        .set_voltage = wm831x_buckv_set_voltage,
-       .get_voltage = wm831x_buckv_get_voltage,
+       .get_voltage_sel = wm831x_buckv_get_voltage_sel,
        .list_voltage = wm831x_buckv_list_voltage,
        .set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
        .set_current_limit = wm831x_buckv_set_current_limit,
@@ -452,27 +437,6 @@ static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
        if (!pdata || !pdata->dvs_gpio)
                return;
 
-       switch (pdata->dvs_control_src) {
-       case 1:
-               ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
-               break;
-       case 2:
-               ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
-               break;
-       default:
-               dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
-                       pdata->dvs_control_src, dcdc->name);
-               return;
-       }
-
-       ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
-                             WM831X_DC1_DVS_SRC_MASK, ctrl);
-       if (ret < 0) {
-               dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
-                       dcdc->name, ret);
-               return;
-       }
-
        ret = gpio_request(pdata->dvs_gpio, "DCDC DVS");
        if (ret < 0) {
                dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
@@ -494,23 +458,64 @@ static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
        }
 
        dcdc->dvs_gpio = pdata->dvs_gpio;
+
+       switch (pdata->dvs_control_src) {
+       case 1:
+               ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
+               break;
+       case 2:
+               ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
+               break;
+       default:
+               dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
+                       pdata->dvs_control_src, dcdc->name);
+               return;
+       }
+
+       /* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL
+        * to make bootstrapping a bit smoother.
+        */
+       if (!dcdc->dvs_vsel) {
+               ret = wm831x_set_bits(wm831x,
+                                     dcdc->base + WM831X_DCDC_DVS_CONTROL,
+                                     WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel);
+               if (ret == 0)
+                       dcdc->dvs_vsel = dcdc->on_vsel;
+               else
+                       dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n",
+                                ret);
+       }
+
+       ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
+                             WM831X_DC1_DVS_SRC_MASK, ctrl);
+       if (ret < 0) {
+               dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
+                       dcdc->name, ret);
+       }
 }
 
 static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
 {
        struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
        struct wm831x_pdata *pdata = wm831x->dev->platform_data;
-       int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
+       int id;
        struct wm831x_dcdc *dcdc;
        struct resource *res;
        int ret, irq;
 
+       if (pdata && pdata->wm831x_num)
+               id = (pdata->wm831x_num * 10) + 1;
+       else
+               id = 0;
+       id = pdev->id - id;
+
        dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
 
        if (pdata == NULL || pdata->dcdc[id] == NULL)
                return -ENODEV;
 
-       dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
+       dcdc = devm_kzalloc(&pdev->dev,  sizeof(struct wm831x_dcdc),
+                           GFP_KERNEL);
        if (dcdc == NULL) {
                dev_err(&pdev->dev, "Unable to allocate private data\n");
                return -ENOMEM;
@@ -541,7 +546,7 @@ static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
        }
        dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
 
-       ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
+       ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL);
        if (ret < 0) {
                dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
                goto err;
@@ -552,7 +557,7 @@ static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
                wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data);
 
        dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
-                                            pdata->dcdc[id], dcdc);
+                                            pdata->dcdc[id], dcdc, NULL);
        if (IS_ERR(dcdc->regulator)) {
                ret = PTR_ERR(dcdc->regulator);
                dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
@@ -561,9 +566,8 @@ static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
        }
 
        irq = platform_get_irq_byname(pdev, "UV");
-       ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
-                                IRQF_TRIGGER_RISING, dcdc->name,
-                                dcdc);
+       ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
+                                  IRQF_TRIGGER_RISING, dcdc->name, dcdc);
        if (ret != 0) {
                dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
                        irq, ret);
@@ -571,9 +575,8 @@ static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
        }
 
        irq = platform_get_irq_byname(pdev, "HC");
-       ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_oc_irq,
-                                IRQF_TRIGGER_RISING, dcdc->name,
-                                dcdc);
+       ret = request_threaded_irq(irq, NULL, wm831x_dcdc_oc_irq,
+                                  IRQF_TRIGGER_RISING, dcdc->name, dcdc);
        if (ret != 0) {
                dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
                        irq, ret);
@@ -585,29 +588,26 @@ static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
        return 0;
 
 err_uv:
-       wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
+       free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
 err_regulator:
        regulator_unregister(dcdc->regulator);
 err:
        if (dcdc->dvs_gpio)
                gpio_free(dcdc->dvs_gpio);
-       kfree(dcdc);
        return ret;
 }
 
 static __devexit int wm831x_buckv_remove(struct platform_device *pdev)
 {
        struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
-       struct wm831x *wm831x = dcdc->wm831x;
 
        platform_set_drvdata(pdev, NULL);
 
-       wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "HC"), dcdc);
-       wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
+       free_irq(platform_get_irq_byname(pdev, "HC"), dcdc);
+       free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
        regulator_unregister(dcdc->regulator);
        if (dcdc->dvs_gpio)
                gpio_free(dcdc->dvs_gpio);
-       kfree(dcdc);
 
        return 0;
 }
@@ -635,7 +635,7 @@ static int wm831x_buckp_list_voltage(struct regulator_dev *rdev,
 }
 
 static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg,
-                                       int min_uV, int max_uV)
+                                       int min_uV, int max_uV, int *selector)
 {
        struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
        struct wm831x *wm831x = dcdc->wm831x;
@@ -649,16 +649,20 @@ static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg,
        if (wm831x_buckp_list_voltage(rdev, vsel) > max_uV)
                return -EINVAL;
 
+       *selector = vsel;
+
        return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, vsel);
 }
 
 static int wm831x_buckp_set_voltage(struct regulator_dev *rdev,
-                                   int min_uV, int max_uV)
+                                   int min_uV, int max_uV,
+                                   unsigned *selector)
 {
        struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
        u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
 
-       return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV);
+       return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV,
+                                           selector);
 }
 
 static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev,
@@ -666,11 +670,12 @@ static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev,
 {
        struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
        u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
+       unsigned selector;
 
-       return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV);
+       return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV, &selector);
 }
 
-static int wm831x_buckp_get_voltage(struct regulator_dev *rdev)
+static int wm831x_buckp_get_voltage_sel(struct regulator_dev *rdev)
 {
        struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
        struct wm831x *wm831x = dcdc->wm831x;
@@ -681,12 +686,12 @@ static int wm831x_buckp_get_voltage(struct regulator_dev *rdev)
        if (val < 0)
                return val;
 
-       return wm831x_buckp_list_voltage(rdev, val & WM831X_DC3_ON_VSEL_MASK);
+       return val & WM831X_DC3_ON_VSEL_MASK;
 }
 
 static struct regulator_ops wm831x_buckp_ops = {
        .set_voltage = wm831x_buckp_set_voltage,
-       .get_voltage = wm831x_buckp_get_voltage,
+       .get_voltage_sel = wm831x_buckp_get_voltage_sel,
        .list_voltage = wm831x_buckp_list_voltage,
        .set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
 
@@ -703,17 +708,24 @@ static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
 {
        struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
        struct wm831x_pdata *pdata = wm831x->dev->platform_data;
-       int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
+       int id;
        struct wm831x_dcdc *dcdc;
        struct resource *res;
        int ret, irq;
 
+       if (pdata && pdata->wm831x_num)
+               id = (pdata->wm831x_num * 10) + 1;
+       else
+               id = 0;
+       id = pdev->id - id;
+
        dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
 
        if (pdata == NULL || pdata->dcdc[id] == NULL)
                return -ENODEV;
 
-       dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
+       dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
+                           GFP_KERNEL);
        if (dcdc == NULL) {
                dev_err(&pdev->dev, "Unable to allocate private data\n");
                return -ENOMEM;
@@ -738,7 +750,7 @@ static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
        dcdc->desc.owner = THIS_MODULE;
 
        dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
-                                            pdata->dcdc[id], dcdc);
+                                            pdata->dcdc[id], dcdc, NULL);
        if (IS_ERR(dcdc->regulator)) {
                ret = PTR_ERR(dcdc->regulator);
                dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
@@ -747,9 +759,8 @@ static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
        }
 
        irq = platform_get_irq_byname(pdev, "UV");
-       ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
-                                IRQF_TRIGGER_RISING, dcdc->name,
-                                dcdc);
+       ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
+                                  IRQF_TRIGGER_RISING, dcdc->name, dcdc);
        if (ret != 0) {
                dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
                        irq, ret);
@@ -763,20 +774,17 @@ static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
 err_regulator:
        regulator_unregister(dcdc->regulator);
 err:
-       kfree(dcdc);
        return ret;
 }
 
 static __devexit int wm831x_buckp_remove(struct platform_device *pdev)
 {
        struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
-       struct wm831x *wm831x = dcdc->wm831x;
 
        platform_set_drvdata(pdev, NULL);
 
-       wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
+       free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
        regulator_unregister(dcdc->regulator);
-       kfree(dcdc);
 
        return 0;
 }
@@ -867,7 +875,7 @@ static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
        dcdc->desc.owner = THIS_MODULE;
 
        dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
-                                            pdata->dcdc[id], dcdc);
+                                            pdata->dcdc[id], dcdc, NULL);
        if (IS_ERR(dcdc->regulator)) {
                ret = PTR_ERR(dcdc->regulator);
                dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
@@ -876,9 +884,9 @@ static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
        }
 
        irq = platform_get_irq_byname(pdev, "UV");
-       ret = wm831x_request_irq(wm831x, irq, wm831x_dcdc_uv_irq,
-                                IRQF_TRIGGER_RISING, dcdc->name,
-                                dcdc);
+       ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
+                                  IRQF_TRIGGER_RISING, dcdc->name,
+                                  dcdc);
        if (ret != 0) {
                dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
                        irq, ret);
@@ -899,11 +907,10 @@ err:
 static __devexit int wm831x_boostp_remove(struct platform_device *pdev)
 {
        struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
-       struct wm831x *wm831x = dcdc->wm831x;
 
        platform_set_drvdata(pdev, NULL);
 
-       wm831x_free_irq(wm831x, platform_get_irq_byname(pdev, "UV"), dcdc);
+       free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
        regulator_unregister(dcdc->regulator);
        kfree(dcdc);
 
@@ -967,7 +974,7 @@ static __devinit int wm831x_epe_probe(struct platform_device *pdev)
        dcdc->desc.owner = THIS_MODULE;
 
        dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
-                                            pdata->epe[id], dcdc);
+                                            pdata->epe[id], dcdc, NULL);
        if (IS_ERR(dcdc->regulator)) {
                ret = PTR_ERR(dcdc->regulator);
                dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
@@ -1043,3 +1050,4 @@ MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
 MODULE_LICENSE("GPL");
 MODULE_ALIAS("platform:wm831x-buckv");
 MODULE_ALIAS("platform:wm831x-buckp");
+MODULE_ALIAS("platform:wm831x-epe");