misc: nct1008: Refactor power rail control
Jinyoung Park [Tue, 4 Jun 2013 13:31:05 +0000 (22:31 +0900)]
Refactor power rail control
- Use devm_regulator_get() instead of regulator_get/put().
- Fix repeated regulator_get calling issue when the regulator is not
available on the system.
- Add return value checking in suspend and resume.

Bug 1325770

Change-Id: I77b457e7b6aa3e9d5ed389cabb9cd0260503eae6
Signed-off-by: Jinyoung Park <jinyoungp@nvidia.com>
Reviewed-on: http://git-master/r/235477
Reviewed-by: Automatic_Commit_Validation_User
GVS: Gerrit_Virtual_Submit
Reviewed-by: Bitan Biswas <bbiswas@nvidia.com>

drivers/misc/nct1008.c

index d8e8e6b..3fa5bf1 100644 (file)
@@ -891,44 +891,25 @@ static irqreturn_t nct1008_irq(int irq, void *dev_id)
        return IRQ_HANDLED;
 }
 
-static void nct1008_power_control(struct nct1008_data *data, bool is_enable)
+static int nct1008_power_control(struct nct1008_data *data, bool enable)
 {
        int ret;
-       const char *name = nct1008_id[data->chip].name;
 
-       if (!data->nct_reg) {
-               data->nct_reg = regulator_get(&data->client->dev, "vdd");
-               if (IS_ERR_OR_NULL(data->nct_reg)) {
-                       if (PTR_ERR(data->nct_reg) == -ENODEV)
-                               dev_info(&data->client->dev,
-                                       "no regulator found for vdd."
-                                       " Assuming vdd is always powered");
-                       else
-                               dev_warn(&data->client->dev, "Error [%ld] in "
-                                       "getting the regulator handle for"
-                                       " vdd\n", PTR_ERR(data->nct_reg));
-                       data->nct_reg = NULL;
-                       return;
-               }
-       }
-       if (is_enable) {
+       if (!data->nct_reg)
+               return 0;
+
+       if (enable) {
                ret = regulator_enable(data->nct_reg);
                msleep(POWER_ON_DELAY);
-       } else {
+       } else
                ret = regulator_disable(data->nct_reg);
-       }
 
        if (ret < 0)
-               dev_err(&data->client->dev, "Error in %s rail vdd_%s, "
-                       "error %d\n", (is_enable) ? "enabling" : "disabling",
-                       name,
-                       ret);
-       else
-               dev_info(&data->client->dev, "success in %s rail vdd_nct%s\n",
-                       (is_enable) ? "enabling" : "disabling",
-                       name);
-
+               dev_err(&data->client->dev,
+                       "%s: Failed to %s regulator vdd, %d\n",
+                       __func__, (enable) ? "enable" : "disable", ret);
 
+       return ret;
 }
 
 static int nct1008_configure_sensor(struct nct1008_data *data)
@@ -1111,12 +1092,17 @@ static int __devinit nct1008_probe(struct i2c_client *client,
        i2c_set_clientdata(client, data);
        mutex_init(&data->mutex);
 
-       nct1008_power_control(data, true);
-       if (!data->nct_reg) {
-               /* power up failure */
-               err = -EIO;
-               goto cleanup;
+       data->nct_reg = devm_regulator_get(&client->dev, "vdd");
+       if (IS_ERR(data->nct_reg)) {
+               dev_warn(&client->dev, "%s: Failed to get regulator vdd, %ld\n",
+                        __func__, PTR_ERR(data->nct_reg));
+               data->nct_reg = NULL;
        }
+
+       err = nct1008_power_control(data, true);
+       if (err < 0)
+               goto cleanup;
+
        /* extended range recommended steps 1 through 4 taken care
         * in nct1008_configure_sensor function */
        err = nct1008_configure_sensor(data);   /* sensor is in standby */
@@ -1203,8 +1189,6 @@ error:
        nct1008_power_control(data, false);
 cleanup:
        mutex_destroy(&data->mutex);
-       if (data->nct_reg)
-               regulator_put(data->nct_reg);
        kfree(data);
        return err;
 }
@@ -1220,8 +1204,6 @@ static int __devexit nct1008_remove(struct i2c_client *client)
        cancel_work_sync(&data->work);
        sysfs_remove_group(&client->dev.kobj, &nct1008_attr_group);
        nct1008_power_control(data, false);
-       if (data->nct_reg)
-               regulator_put(data->nct_reg);
        mutex_destroy(&data->mutex);
        kfree(data);
 
@@ -1249,8 +1231,16 @@ static int nct1008_suspend_powerdown(struct device *dev)
        struct nct1008_data *data = i2c_get_clientdata(client);
 
        disable_irq(client->irq);
+
        err = nct1008_disable(client);
+       if (err < 0) {
+               dev_err(&client->dev, "%s: Failed to disable %s, %d\n",
+                       __func__, client->name, err);
+               return err;
+       }
+
        nct1008_power_control(data, false);
+
        return err;
 }
 
@@ -1332,15 +1322,25 @@ static int nct1008_resume_powerdown(struct device *dev)
        int err = 0;
        struct nct1008_data *data = i2c_get_clientdata(client);
 
-       nct1008_power_control(data, true);
-       nct1008_configure_sensor(data);
-       err = nct1008_enable(client);
+       err = nct1008_power_control(data, true);
        if (err < 0) {
-               dev_err(&client->dev, "Error: %s, error=%d\n",
+               dev_err(&client->dev, "%s: Failed to enable power, %d\n",
                        __func__, err);
                return err;
        }
 
+       err = nct1008_configure_sensor(data);
+       if (err < 0) {
+               dev_err(&client->dev, "%s: Failed to configure sensor, %d\n",
+                       __func__, err);
+               return err;
+       }
+
+       err = nct1008_enable(client);
+       if (err < 0)
+               dev_err(&client->dev, "%s: Failed to enable %s, %d\n",
+                       __func__, client->name, err);
+
        return err;
 }