diff --git a/drivers/misc/rk803.c b/drivers/misc/rk803.c index e4dcc5c9503e..3a5f1c875fcc 100644 --- a/drivers/misc/rk803.c +++ b/drivers/misc/rk803.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #define RK803_CHIPID1 0x0A @@ -59,6 +60,12 @@ enum SL_LED_CURRENT { LED_3200MA }; +static const char * const rk803_supply_names[] = { + "dvdd", /* Digital power */ +}; + +#define RK803_NUM_SUPPLIES ARRAY_SIZE(rk803_supply_names) + struct rk803_data { struct i2c_client *client; struct regmap *regmap; @@ -69,6 +76,7 @@ struct rk803_data { struct gpio_desc *gpio_encc1; struct gpio_desc *gpio_encc2; struct miscdevice misc; + struct regulator_bulk_data supplies[RK803_NUM_SUPPLIES]; }; static const struct of_device_id rk803_of_match[] = { @@ -76,6 +84,27 @@ static const struct of_device_id rk803_of_match[] = { { }, }; +static int rk803_power_on(struct rk803_data *rk803) +{ + int ret; + struct device *dev = &rk803->client->dev; + + ret = regulator_bulk_enable(RK803_NUM_SUPPLIES, rk803->supplies); + if (ret < 0) { + dev_err(dev, "Failed to enable regulators\n"); + return ret; + } + + usleep_range(1000, 2000); + + return 0; +} + +static void rk803_power_off(struct rk803_data *rk803) +{ + regulator_bulk_disable(RK803_NUM_SUPPLIES, rk803->supplies); +} + static ssize_t rk803_i2c_write_reg(struct rk803_data *rk803, uint8_t reg, uint8_t val) { @@ -156,6 +185,18 @@ static const struct file_operations rk803_fops = { #endif }; +static int rk803_configure_regulators(struct rk803_data *rk803) +{ + unsigned int i; + + for (i = 0; i < RK803_NUM_SUPPLIES; i++) + rk803->supplies[i].supply = rk803_supply_names[i]; + + return devm_regulator_bulk_get(&rk803->client->dev, + RK803_NUM_SUPPLIES, + rk803->supplies); +} + static int rk803_probe(struct i2c_client *client, const struct i2c_device_id *id) { @@ -168,18 +209,34 @@ rk803_probe(struct i2c_client *client, const struct i2c_device_id *id) struct regmap_config regmap_config = { }; int ret; + rk803 = devm_kzalloc(dev, sizeof(*rk803), GFP_KERNEL); + if (!rk803) + return -ENOMEM; + + rk803->client = client; + + ret = rk803_configure_regulators(rk803); + if (ret) { + dev_err(dev, "Failed to get power regulators\n"); + return ret; + } + + rk803_power_on(rk803); + /* check chip id */ msb = i2c_smbus_read_byte_data(client, RK803_CHIPID1); if (msb < 0) { dev_err(dev, "failed to read the chip1 id at 0x%x\n", RK803_CHIPID1); - return msb; + ret = -EPROBE_DEFER; + goto error; } lsb = i2c_smbus_read_byte_data(client, RK803_CHIPID2); if (lsb < 0) { dev_err(dev, "failed to read the chip2 id at 0x%x\n", RK803_CHIPID2); - return lsb; + ret = lsb; + goto error; } chipid = ((msb << 8) | lsb); @@ -190,15 +247,12 @@ rk803_probe(struct i2c_client *client, const struct i2c_device_id *id) regmap_config.disable_locking = true; regmap = devm_regmap_init_i2c(client, ®map_config); - if (IS_ERR(regmap)) - return PTR_ERR(regmap); - - rk803 = devm_kzalloc(dev, sizeof(*rk803), GFP_KERNEL); - if (!rk803) - return -ENOMEM; + if (IS_ERR(regmap)) { + ret = PTR_ERR(regmap); + goto error; + } rk803->chip_id = chipid; - rk803->client = client; rk803->regmap = regmap; rk803->current1 = IR_LED_DEFAULT_CURRENT; rk803->current2 = PRO_LED_DEFAULT_CURRENT; @@ -206,12 +260,14 @@ rk803_probe(struct i2c_client *client, const struct i2c_device_id *id) rk803->gpio_encc1 = devm_gpiod_get(dev, "gpio-encc1", GPIOD_OUT_LOW); if (IS_ERR(rk803->gpio_encc1)) { dev_err(dev, "can not find gpio_encc1\n"); - return PTR_ERR(rk803->gpio_encc1); + ret = PTR_ERR(rk803->gpio_encc1); + goto error; } rk803->gpio_encc2 = devm_gpiod_get(dev, "gpio-encc2", GPIOD_OUT_LOW); if (IS_ERR(rk803->gpio_encc2)) { dev_err(dev, "can not find gpio_encc2\n"); - return PTR_ERR(rk803->gpio_encc2); + ret = PTR_ERR(rk803->gpio_encc2); + goto error; } /* OVP */ @@ -239,11 +295,15 @@ rk803_probe(struct i2c_client *client, const struct i2c_device_id *id) if (ret < 0) { dev_err(&client->dev, "Error: misc_register returned %d\n", ret); - return ret; + goto error; } dev_info(dev, "rk803 probe ok!\n"); return 0; + +error: + rk803_power_off(rk803); + return ret; } static int rk803_remove(struct i2c_client *client) @@ -252,6 +312,9 @@ static int rk803_remove(struct i2c_client *client) rk803 = i2c_get_clientdata(client); misc_deregister(&rk803->misc); + + rk803_power_off(rk803); + return 0; }