summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorRobert Marklund <robert.marklund@stericsson.com>2011-03-21 16:53:46 +0100
committerPhilippe Langlais <philippe.langlais@linaro.org>2011-05-03 08:25:34 +0200
commitb33331106e0272cf81a6d406be0d9168302a77d2 (patch)
tree3827a7c466edc58f8c740a7154d119993a6d7609 /drivers
parent345f699c3e77063700ec7ad57bc2d6f5df5c544f (diff)
misc/bh1780gli: add PM features
misc/bh1780gli: change unsigned long to long Signed-off-by: Robert Marklund <robert.marklund@stericsson.com>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/misc/bh1780gli.c106
1 files changed, 70 insertions, 36 deletions
diff --git a/drivers/misc/bh1780gli.c b/drivers/misc/bh1780gli.c
index d5f3a3fd231..0c94e481f67 100644
--- a/drivers/misc/bh1780gli.c
+++ b/drivers/misc/bh1780gli.c
@@ -18,10 +18,12 @@
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/i2c.h>
+#include <linux/err.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
+#include <linux/regulator/consumer.h>
#define BH1780_REG_CONTROL 0x80
#define BH1780_REG_PARTID 0x8A
@@ -39,6 +41,7 @@
struct bh1780_data {
struct i2c_client *client;
+ struct regulator *regulator;
int power_state;
/* lock for sysfs operations */
struct mutex lock;
@@ -71,6 +74,9 @@ static ssize_t bh1780_show_lux(struct device *dev,
struct bh1780_data *ddata = platform_get_drvdata(pdev);
int lsb, msb;
+ if (ddata->power_state == BH1780_POFF)
+ return -EINVAL;
+
lsb = bh1780_read(ddata, BH1780_REG_DLOW, "DLOW");
if (lsb < 0)
return lsb;
@@ -88,13 +94,9 @@ static ssize_t bh1780_show_power_state(struct device *dev,
{
struct platform_device *pdev = to_platform_device(dev);
struct bh1780_data *ddata = platform_get_drvdata(pdev);
- int state;
- state = bh1780_read(ddata, BH1780_REG_CONTROL, "CONTROL");
- if (state < 0)
- return state;
-
- return sprintf(buf, "%d\n", state & BH1780_POWMASK);
+ /* we already maintain a sw state */
+ return sprintf(buf, "%d\n", ddata->power_state);
}
static ssize_t bh1780_store_power_state(struct device *dev,
@@ -103,7 +105,7 @@ static ssize_t bh1780_store_power_state(struct device *dev,
{
struct platform_device *pdev = to_platform_device(dev);
struct bh1780_data *ddata = platform_get_drvdata(pdev);
- unsigned long val;
+ long val;
int error;
error = strict_strtoul(buf, 0, &val);
@@ -115,12 +117,22 @@ static ssize_t bh1780_store_power_state(struct device *dev,
mutex_lock(&ddata->lock);
+ if (ddata->power_state == val)
+ return count;
+
+ if (ddata->power_state == BH1780_POFF)
+ regulator_enable(ddata->regulator);
+
error = bh1780_write(ddata, BH1780_REG_CONTROL, val, "CONTROL");
if (error < 0) {
mutex_unlock(&ddata->lock);
+ regulator_disable(ddata->regulator);
return error;
}
+ if (val == BH1780_POFF)
+ regulator_disable(ddata->regulator);
+
msleep(BH1780_PON_DELAY);
ddata->power_state = val;
mutex_unlock(&ddata->lock);
@@ -130,7 +142,7 @@ static ssize_t bh1780_store_power_state(struct device *dev,
static DEVICE_ATTR(lux, S_IRUGO, bh1780_show_lux, NULL);
-static DEVICE_ATTR(power_state, S_IWUSR | S_IRUGO,
+static DEVICE_ATTR(power_state, S_IWUGO | S_IRUGO,
bh1780_show_power_state, bh1780_store_power_state);
static struct attribute *bh1780_attributes[] = {
@@ -152,21 +164,37 @@ static int __devinit bh1780_probe(struct i2c_client *client,
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) {
ret = -EIO;
- goto err_op_failed;
+ return ret;
}
ddata = kzalloc(sizeof(struct bh1780_data), GFP_KERNEL);
if (ddata == NULL) {
+ dev_err(&client->dev, "failed to alloc ddata\n");
ret = -ENOMEM;
- goto err_op_failed;
+ return ret;
}
ddata->client = client;
i2c_set_clientdata(client, ddata);
+ dev_set_name(&client->dev, "bh1780");
+
+ ddata->regulator = regulator_get(&client->dev, "v-als");
+ if (IS_ERR(ddata->regulator)) {
+ dev_err(&client->dev, "failed to get regulator\n");
+ ret = PTR_ERR(ddata->regulator);
+ goto free_ddata;
+ }
+
+ regulator_enable(ddata->regulator);
ret = bh1780_read(ddata, BH1780_REG_PARTID, "PART ID");
- if (ret < 0)
- goto err_op_failed;
+ if (ret < 0) {
+ dev_err(&client->dev, "failed to read part ID\n");
+ goto put_regulator;
+ }
+
+ regulator_disable(ddata->regulator);
+ ddata->power_state = BH1780_POFF;
dev_info(&client->dev, "Ambient Light Sensor, Rev : %d\n",
(ret & BH1780_REVMASK));
@@ -175,11 +203,14 @@ static int __devinit bh1780_probe(struct i2c_client *client,
ret = sysfs_create_group(&client->dev.kobj, &bh1780_attr_group);
if (ret)
- goto err_op_failed;
+ goto put_regulator;
return 0;
-err_op_failed:
+put_regulator:
+ regulator_disable(ddata->regulator);
+ regulator_put(ddata->regulator);
+free_ddata:
kfree(ddata);
return ret;
}
@@ -196,17 +227,13 @@ static int __devexit bh1780_remove(struct i2c_client *client)
}
#ifdef CONFIG_PM
-static int bh1780_suspend(struct i2c_client *client, pm_message_t mesg)
+static int bh1780_suspend(struct device *dev)
{
- struct bh1780_data *ddata;
- int state, ret;
-
- ddata = i2c_get_clientdata(client);
- state = bh1780_read(ddata, BH1780_REG_CONTROL, "CONTROL");
- if (state < 0)
- return state;
+ struct bh1780_data *ddata = dev_get_drvdata(dev);
+ int ret = 0;
- ddata->power_state = state & BH1780_POWMASK;
+ if (ddata->power_state == BH1780_POFF)
+ return 0;
ret = bh1780_write(ddata, BH1780_REG_CONTROL, BH1780_POFF,
"CONTROL");
@@ -214,28 +241,34 @@ static int bh1780_suspend(struct i2c_client *client, pm_message_t mesg)
if (ret < 0)
return ret;
+ regulator_disable(ddata->regulator);
+
return 0;
}
-static int bh1780_resume(struct i2c_client *client)
+static int bh1780_resume(struct device *dev)
{
- struct bh1780_data *ddata;
- int state, ret;
+ struct bh1780_data *ddata = dev_get_drvdata(dev);
+ int ret = 0;
- ddata = i2c_get_clientdata(client);
- state = ddata->power_state;
+ if (ddata->power_state == BH1780_POFF)
+ return 0;
- ret = bh1780_write(ddata, BH1780_REG_CONTROL, state,
- "CONTROL");
+ regulator_enable(ddata->regulator);
+ ret = bh1780_write(ddata, BH1780_REG_CONTROL, ddata->power_state,
+ "CONTROL");
if (ret < 0)
return ret;
return 0;
}
-#else
-#define bh1780_suspend NULL
-#define bh1780_resume NULL
+
+static const struct dev_pm_ops bh1780_dev_pm_ops = {
+ .suspend = bh1780_suspend,
+ .resume = bh1780_resume,
+};
+
#endif /* CONFIG_PM */
static const struct i2c_device_id bh1780_id[] = {
@@ -247,10 +280,11 @@ static struct i2c_driver bh1780_driver = {
.probe = bh1780_probe,
.remove = bh1780_remove,
.id_table = bh1780_id,
- .suspend = bh1780_suspend,
- .resume = bh1780_resume,
.driver = {
- .name = "bh1780"
+ .name = "bh1780",
+#ifdef CONFIG_PM
+ .pm = &bh1780_dev_pm_ops,
+#endif
},
};