Re: [PATCH 3/3] ideapad: add backlight driver

From: Corentin Chary
Date: Wed Jun 22 2011 - 05:20:23 EST


On Wed, Jun 22, 2011 at 10:07 AM, Ike Panhc <ike.pan@xxxxxxxxxxxxx> wrote:
> When acpi_backlight=vendor in cmdline or no backlight support in acpi video
> device, ideapad-laptop will register backlight device and control brightness
> and backlight power via the command in VPC2004.
>
> Signed-off-by: Ike Panhc <ike.pan@xxxxxxxxxxxxx>
> ---
> Âdrivers/platform/x86/ideapad-laptop.c | Â129 ++++++++++++++++++++++++++++++---
> Â1 files changed, 119 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/platform/x86/ideapad-laptop.c b/drivers/platform/x86/ideapad-laptop.c
> index 678bbf7..018c457 100644
> --- a/drivers/platform/x86/ideapad-laptop.c
> +++ b/drivers/platform/x86/ideapad-laptop.c
> @@ -32,6 +32,8 @@
> Â#include <linux/platform_device.h>
> Â#include <linux/input.h>
> Â#include <linux/input/sparse-keymap.h>
> +#include <linux/backlight.h>
> +#include <linux/fb.h>
>
> Â#define IDEAPAD_RFKILL_DEV_NUM (3)
>
> @@ -44,6 +46,7 @@ struct ideapad_private {
> Â Â Â Âstruct rfkill *rfk[IDEAPAD_RFKILL_DEV_NUM];
> Â Â Â Âstruct platform_device *platform_device;
> Â Â Â Âstruct input_dev *inputdev;
> + Â Â Â struct backlight_device *blightdev;
> Â Â Â Âunsigned long cfg;
> Â};
>
> @@ -257,9 +260,8 @@ static struct rfkill_ops ideapad_rfk_ops = {
> Â Â Â Â.set_block = ideapad_rfk_set,
> Â};
>
> -static void ideapad_sync_rfk_state(struct acpi_device *adevice)
> +static void ideapad_sync_rfk_state(struct ideapad_private *priv)
> Â{
> - Â Â Â struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
> Â Â Â Âunsigned long hw_blocked;
> Â Â Â Âint i;
>
> @@ -309,8 +311,7 @@ static int __devinit ideapad_register_rfkill(struct acpi_device *adevice,
> Â Â Â Âreturn 0;
> Â}
>
> -static void __devexit ideapad_unregister_rfkill(struct acpi_device *adevice,
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int dev)
> +static void ideapad_unregister_rfkill(struct acpi_device *adevice, int dev)
> Â{
> Â Â Â Âstruct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
>
> @@ -418,6 +419,97 @@ static void ideapad_input_report(struct ideapad_private *priv,
> Â}
>
> Â/*
> + * backlight
> + */
> +static int ideapad_backlight_get_brightness(struct backlight_device *blightdev)
> +{
> + Â Â Â unsigned long now;
> +
> + Â Â Â if (read_ec_data(ideapad_handle, 0x12, &now))
> + Â Â Â Â Â Â Â return -EAGAIN;
> + Â Â Â return now;
> +}
> +
> +static int ideapad_backlight_update_status(struct backlight_device *blightdev)
> +{
> + Â Â Â if (write_ec_cmd(ideapad_handle, 0x13, blightdev->props.brightness))
> + Â Â Â Â Â Â Â return -EAGAIN;
> + Â Â Â if (write_ec_cmd(ideapad_handle, 0x33,
> + Â Â Â Â Â Â Â Â Â Â Â Âblightdev->props.power == FB_BLANK_POWERDOWN ? 0 : 1))
> + Â Â Â Â Â Â Â return -EAGAIN;
> +
> + Â Â Â return 0;
> +}

I don't think -EAGAIN is a good idea here ... Why not -EIO ?

> +static const struct backlight_ops ideapad_backlight_ops = {
> + Â Â Â .get_brightness = ideapad_backlight_get_brightness,
> + Â Â Â .update_status = ideapad_backlight_update_status,
> +};
> +
> +static int ideapad_backlight_init(struct ideapad_private *priv)
> +{
> + Â Â Â struct backlight_device *blightdev;
> + Â Â Â struct backlight_properties props;
> + Â Â Â unsigned long max, now, power;
> +
> + Â Â Â if (read_ec_data(ideapad_handle, 0x11, &max))
> + Â Â Â Â Â Â Â return -EAGAIN;
> + Â Â Â if (read_ec_data(ideapad_handle, 0x12, &now))
> + Â Â Â Â Â Â Â return -EAGAIN;
> + Â Â Â if (read_ec_data(ideapad_handle, 0x18, &power))
> + Â Â Â Â Â Â Â return -EAGAIN;

Same

> + Â Â Â memset(&props, 0, sizeof(struct backlight_properties));
> + Â Â Â props.max_brightness = max;

You forgot to set backlight type, "props.type = BACKLIGHT_PLATFORM;"

> + Â Â Â blightdev = backlight_device_register("ideapad",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &priv->platform_device->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â priv,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &ideapad_backlight_ops,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &props);
> + Â Â Â if (IS_ERR(blightdev)) {
> + Â Â Â Â Â Â Â pr_err("Could not register backlight device\n");
> + Â Â Â Â Â Â Â return PTR_ERR(blightdev);
> + Â Â Â }
> +
> + Â Â Â priv->blightdev = blightdev;
> + Â Â Â blightdev->props.brightness = now;
> + Â Â Â blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
> + Â Â Â backlight_update_status(blightdev);
> +
> + Â Â Â return 0;
> +}
> +
> +static void ideapad_backlight_exit(struct ideapad_private *priv)
> +{
> + Â Â Â if (priv->blightdev)
> + Â Â Â Â Â Â Â backlight_device_unregister(priv->blightdev);
> + Â Â Â priv->blightdev = NULL;
> +}
> +
> +static void ideapad_backlight_notify_power(struct ideapad_private *priv)
> +{
> + Â Â Â unsigned long power;
> + Â Â Â struct backlight_device *blightdev = priv->blightdev;
> +
> + Â Â Â if (read_ec_data(ideapad_handle, 0x18, &power))
> + Â Â Â Â Â Â Â return;
> + Â Â Â blightdev->props.power = power ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
> +}
> +
> +static void ideapad_backlight_notify_brightness(struct ideapad_private *priv)
> +{
> + Â Â Â unsigned long now;
> +
> + Â Â Â /* if we control brightness via acpi video driver */
> + Â Â Â if (priv->blightdev == NULL) {
> + Â Â Â Â Â Â Â read_ec_data(ideapad_handle, 0x12, &now);
> + Â Â Â Â Â Â Â return;
> + Â Â Â }
> +
> + Â Â Â backlight_force_update(priv->blightdev, BACKLIGHT_UPDATE_HOTKEY);
> +}
> +
> +/*
> Â* module init/exit
> Â*/
> Âstatic const struct acpi_device_id ideapad_device_ids[] = {
> @@ -456,10 +548,19 @@ static int __devinit ideapad_acpi_add(struct acpi_device *adevice)
> Â Â Â Â Â Â Â Âelse
> Â Â Â Â Â Â Â Â Â Â Â Âpriv->rfk[i] = NULL;
> Â Â Â Â}
> - Â Â Â ideapad_sync_rfk_state(adevice);
> + Â Â Â ideapad_sync_rfk_state(priv);
> +
> + Â Â Â if (!acpi_video_backlight_support()) {
> + Â Â Â Â Â Â Â ret = ideapad_backlight_init(priv);
> + Â Â Â Â Â Â Â if (ret && ret != -ENODEV)
> + Â Â Â Â Â Â Â Â Â Â Â goto backlight_failed;
> + Â Â Â }
>
> Â Â Â Âreturn 0;
>
> +backlight_failed:
> + Â Â Â for (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
> + Â Â Â Â Â Â Â ideapad_unregister_rfkill(adevice, i);
> Âinput_failed:
> Â Â Â Âideapad_platform_exit(priv);
> Âplatform_failed:
> @@ -472,6 +573,7 @@ static int __devexit ideapad_acpi_remove(struct acpi_device *adevice, int type)
> Â Â Â Âstruct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
> Â Â Â Âint i;
>
> + Â Â Â ideapad_backlight_exit(priv);
> Â Â Â Âfor (i = 0; i < IDEAPAD_RFKILL_DEV_NUM; i++)
> Â Â Â Â Â Â Â Âideapad_unregister_rfkill(adevice, i);
> Â Â Â Âideapad_input_exit(priv);
> @@ -496,12 +598,19 @@ static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
> Â Â Â Âvpc1 = (vpc2 << 8) | vpc1;
> Â Â Â Âfor (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
> Â Â Â Â Â Â Â Âif (test_bit(vpc_bit, &vpc1)) {
> - Â Â Â Â Â Â Â Â Â Â Â if (vpc_bit == 9)
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ideapad_sync_rfk_state(adevice);
> - Â Â Â Â Â Â Â Â Â Â Â else if (vpc_bit == 4)
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â read_ec_data(handle, 0x12, &vpc2);
> - Â Â Â Â Â Â Â Â Â Â Â else
> + Â Â Â Â Â Â Â Â Â Â Â switch (vpc_bit) {
> + Â Â Â Â Â Â Â Â Â Â Â case 9:
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ideapad_sync_rfk_state(priv);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â Â Â Â Â case 4:
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ideapad_backlight_notify_brightness(priv);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â Â Â Â Â case 2:
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ideapad_backlight_notify_power(priv);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break;
> + Â Â Â Â Â Â Â Â Â Â Â default:
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âideapad_input_report(priv, vpc_bit);
> + Â Â Â Â Â Â Â Â Â Â Â }
> Â Â Â Â Â Â Â Â}
> Â Â Â Â}
> Â}
> --
> 1.7.4.1
>
>

Otherwise, looks ok

--
Corentin Chary
http://xf.iksaif.net
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/