backlight: pwm_bl: Add support for EDP
Hayden Du [Mon, 3 Jun 2013 11:28:44 +0000 (19:28 +0800)]
Added EDP support for pwm backlight

bug 1299129

Change-Id: I0164a1b5c79dad36651641720aca0a8b0f715c4c
Signed-off-by: Hayden Du <haydend@nvidia.com>
Reviewed-on: http://git-master/r/234977
Reviewed-by: Laxman Dewangan <ldewangan@nvidia.com>
Reviewed-by: Automatic_Commit_Validation_User

drivers/video/backlight/pwm_bl.c
include/linux/pwm_backlight.h

index b5ed7e9..10a6b9b 100644 (file)
@@ -29,6 +29,7 @@
 #include <linux/pwm.h>
 #include <linux/pwm_backlight.h>
 #include <linux/slab.h>
+#include <linux/edp.h>
 
 struct pwm_bl_data {
        struct pwm_device       *pwm;
@@ -36,6 +37,8 @@ struct pwm_bl_data {
        unsigned int            period;
        unsigned int            lth_brightness;
        unsigned int            pwm_gpio;
+       struct edp_client *tegra_pwm_bl_edp_client;
+       int *edp_brightness_states;
        int                     (*notify)(struct device *,
                                          int brightness);
        void                    (*notify_after)(struct device *,
@@ -49,6 +52,11 @@ static int pwm_backlight_update_status(struct backlight_device *bl)
        struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
        int brightness = bl->props.brightness;
        int max = bl->props.max_brightness;
+       int approved;
+       int edp_state;
+       int i;
+       int ret;
+
        if (pb->display_init && !pb->display_init(pb->dev))
                brightness = 0;
 
@@ -61,6 +69,18 @@ static int pwm_backlight_update_status(struct backlight_device *bl)
        if (pb->notify)
                brightness = pb->notify(pb->dev, brightness);
 
+       if (pb->tegra_pwm_bl_edp_client) {
+               for (i = 0; i < TEGRA_PWM_BL_EDP_NUM_STATES; i++) {
+                       if (brightness >= pb->edp_brightness_states[i])
+                               break;
+               }
+               edp_state = i;
+               ret = edp_update_client_request(pb->tegra_pwm_bl_edp_client,
+                                       edp_state, &approved);
+               if (ret || approved != edp_state)
+                       dev_err(&bl->dev, "E state transition failed\n");
+       }
+
        if (brightness == 0) {
                pwm_config(pb->pwm, 0, pb->period);
                pwm_disable(pb->pwm);
@@ -90,6 +110,27 @@ static int pwm_backlight_check_fb(struct backlight_device *bl,
        return !pb->check_fb || pb->check_fb(pb->dev, info);
 }
 
+static void pwm_backlight_edpcb(unsigned int new_state, void *priv_data)
+{
+       struct backlight_device *bl = (struct backlight_device *) priv_data;
+       struct pwm_bl_data *pb = dev_get_drvdata(&bl->dev);
+       int max = bl->props.max_brightness;
+       int brightness = pb->edp_brightness_states[new_state];
+
+       if (brightness == 0) {
+               pwm_config(pb->pwm, 0, pb->period);
+               pwm_disable(pb->pwm);
+       } else {
+               brightness = pb->lth_brightness +
+                       (brightness * (pb->period - pb->lth_brightness) / max);
+               pwm_config(pb->pwm, brightness, pb->period);
+               pwm_enable(pb->pwm);
+       }
+
+       if (pb->notify_after)
+               pb->notify_after(pb->dev, brightness);
+}
+
 static const struct backlight_ops pwm_backlight_ops = {
        .update_status  = pwm_backlight_update_status,
        .get_brightness = pwm_backlight_get_brightness,
@@ -102,6 +143,7 @@ static int pwm_backlight_probe(struct platform_device *pdev)
        struct platform_pwm_backlight_data *data = pdev->dev.platform_data;
        struct backlight_device *bl;
        struct pwm_bl_data *pb;
+       struct edp_manager *battery_manager = NULL;
        int ret;
 
        if (!data) {
@@ -131,6 +173,7 @@ static int pwm_backlight_probe(struct platform_device *pdev)
        pb->dev = &pdev->dev;
        pb->display_init = data->init;
        pb->pwm_gpio = data->pwm_gpio;
+       pb->edp_brightness_states = data->edp_brightness;
 
        pb->pwm = pwm_request(data->pwm_id, "backlight");
        if (IS_ERR(pb->pwm)) {
@@ -158,6 +201,51 @@ static int pwm_backlight_probe(struct platform_device *pdev)
                goto err_bl;
        }
 
+       pb->tegra_pwm_bl_edp_client = devm_kzalloc(&pdev->dev,
+                       sizeof(struct edp_client), GFP_KERNEL);
+       if (IS_ERR_OR_NULL(pb->tegra_pwm_bl_edp_client)) {
+               dev_err(&pdev->dev, "could not allocate edp client\n");
+               return PTR_ERR(pb->tegra_pwm_bl_edp_client);
+       }
+       strncpy(pb->tegra_pwm_bl_edp_client->name,
+                       "backlight", EDP_NAME_LEN - 1);
+       pb->tegra_pwm_bl_edp_client->name[EDP_NAME_LEN - 1] = '\0';
+       pb->tegra_pwm_bl_edp_client->states = data->edp_states;
+       pb->tegra_pwm_bl_edp_client->num_states = TEGRA_PWM_BL_EDP_NUM_STATES;
+       pb->tegra_pwm_bl_edp_client->e0_index = TEGRA_PWM_BL_EDP_ZERO;
+       pb->tegra_pwm_bl_edp_client->private_data = bl;
+       pb->tegra_pwm_bl_edp_client->priority = EDP_MAX_PRIO + 2;
+       pb->tegra_pwm_bl_edp_client->throttle = pwm_backlight_edpcb;
+       pb->tegra_pwm_bl_edp_client->notify_promotion = pwm_backlight_edpcb;
+
+       battery_manager = edp_get_manager("battery");
+       if (!battery_manager) {
+               dev_err(&pdev->dev, "unable to get edp manager\n");
+       } else {
+               ret = edp_register_client(battery_manager,
+                                       pb->tegra_pwm_bl_edp_client);
+               if (ret) {
+                       dev_err(&pdev->dev, "unable to register edp client\n");
+               } else {
+                       ret = edp_update_client_request(
+                                       pb->tegra_pwm_bl_edp_client,
+                                               TEGRA_PWM_BL_EDP_ZERO, NULL);
+                       if (ret) {
+                               dev_err(&pdev->dev,
+                                       "unable to set E0 EDP state\n");
+                               edp_unregister_client(
+                                       pb->tegra_pwm_bl_edp_client);
+                       } else {
+                               goto edp_success;
+                       }
+               }
+       }
+
+       devm_kfree(&pdev->dev, pb->tegra_pwm_bl_edp_client);
+       pb->tegra_pwm_bl_edp_client = NULL;
+
+edp_success:
+
        bl->props.brightness = data->dft_brightness;
        backlight_update_status(bl);
 
index 7b4365d..ea5c0a0 100644 (file)
@@ -1,11 +1,36 @@
-/*
- * Generic PWM backlight driver data - see drivers/video/backlight/pwm_bl.c
+/* Generic PWM backlight driver data - see drivers/video/backlight/pwm_bl.c
+ *
+ * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
 #ifndef __LINUX_PWM_BACKLIGHT_H
 #define __LINUX_PWM_BACKLIGHT_H
 
 #include <linux/backlight.h>
 
+enum tegra_pwm_bl_edp_states {
+       TEGRA_PWM_BL_EDP_NEG_3,
+       TEGRA_PWM_BL_EDP_NEG_2,
+       TEGRA_PWM_BL_EDP_NEG_1,
+       TEGRA_PWM_BL_EDP_ZERO,
+       TEGRA_PWM_BL_EDP_1,
+       TEGRA_PWM_BL_EDP_2,
+       TEGRA_PWM_BL_EDP_NUM_STATES,
+};
+
 struct platform_pwm_backlight_data {
        int pwm_id;
        unsigned int max_brightness;
@@ -13,6 +38,8 @@ struct platform_pwm_backlight_data {
        unsigned int lth_brightness;
        unsigned int pwm_period_ns;
        unsigned int pwm_gpio;
+       unsigned int edp_states[TEGRA_PWM_BL_EDP_NUM_STATES];
+       unsigned int edp_brightness[TEGRA_PWM_BL_EDP_NUM_STATES];
        int (*init)(struct device *dev);
        int (*notify)(struct device *dev, int brightness);
        void (*notify_after)(struct device *dev, int brightness);