Re: [PATCH RESEND] add led driver for Bachmann's ot200

From: Christian Gmeiner
Date: Thu Dec 15 2011 - 07:10:39 EST


Hi Lars-Peter,

2011/12/14 Lars-Peter Clausen <lars@xxxxxxxxxx>:
> On 12/13/2011 10:57 AM, Christian Gmeiner wrote:
>> From a7fecf3426ef98fdd19e9d2610665b9d1ce358a0 Mon Sep 17 00:00:00 2001
>> From: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
>> Date: Fri, 18 Nov 2011 10:21:48 +0100
>> Subject: [PATCH] add led driver for Bachmann's ot200
>>
>> This patch adds support for leds on Bachmann's ot200 visualisation device.
>> The device has three leds on the back panel (led_err, led_init and led_run)
>> and can handle up to seven leds on the front panel.
>>
>> The driver was written by Linutronix on behalf of
>> Bachmann electronic GmbH.
>>
>> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
>> Signed-off-by: Christian Gmeiner <christian.gmeiner@xxxxxxxxx>
>> ---
>> Âdrivers/leds/Kconfig   Â|  Â8 ++
>> Âdrivers/leds/Makefile   |  Â1 +
>> Âdrivers/leds/leds-ot200.c | Â204 +++++++++++++++++++++++++++++++++++++++++++++
>> Â3 files changed, 213 insertions(+), 0 deletions(-)
>> Âcreate mode 100644 drivers/leds/leds-ot200.c
>>
>> diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
>> index ff203a4..6cf0dd6 100644
>> --- a/drivers/leds/Kconfig
>> +++ b/drivers/leds/Kconfig
>> @@ -387,6 +387,14 @@ config LEDS_RENESAS_TPU
>> Â Â Â Â Âpin function. The latter to support brightness control.
>> Â Â Â Â ÂBrightness control is supported but hardware blinking is not.
>>
>> +config LEDS_OT200
>> + Â Â Â tristate "LED support for Bachmann OT200"
>> + Â Â Â depends on LEDS_CLASS
>> + Â Â Â help
>> + Â Â Â Â This option enables support for the LEDs on the Bachmann OT200. The
>> + Â Â Â Â LEDs are controlled through LPC bus.
>> + Â Â Â Â Say Y to enable LEDs on the Bachmann OT200.
>> +
>> Âconfig LEDS_TRIGGERS
>> Â Â Â Âbool "LED Trigger support"
>> Â Â Â Âdepends on LEDS_CLASS
>> diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
>> index e4f6bf5..0814d42 100644
>> --- a/drivers/leds/Makefile
>> +++ b/drivers/leds/Makefile
>> @@ -43,6 +43,7 @@ obj-$(CONFIG_LEDS_NS2) Â Â Â Â Â Â Â Â Â Â Â Â+= leds-ns2.o
>> Âobj-$(CONFIG_LEDS_NETXBIG) Â Â Â Â Â Â += leds-netxbig.o
>> Âobj-$(CONFIG_LEDS_ASIC3) Â Â Â Â Â Â Â += leds-asic3.o
>> Âobj-$(CONFIG_LEDS_RENESAS_TPU) Â Â Â Â += leds-renesas-tpu.o
>> +obj-$(CONFIG_LEDS_OT200) Â Â Â Â Â Â Â += leds-ot200.o
>>
>> Â# LED SPI Drivers
>> Âobj-$(CONFIG_LEDS_DAC124S085) Â Â Â Â Â+= leds-dac124s085.o
>> diff --git a/drivers/leds/leds-ot200.c b/drivers/leds/leds-ot200.c
>> new file mode 100644
>> index 0000000..60f118b
>> --- /dev/null
>> +++ b/drivers/leds/leds-ot200.c
>> @@ -0,0 +1,204 @@
>> +/*
>> + * Bachmann ot200 leds driver.
>> + *
>> + * Author: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
>> + * License: GPL as published by the FSF.
>> + */
>> +
>> +#include <linux/platform_device.h>
>> +#include <linux/slab.h>
>> +#include <linux/leds.h>
>> +#include <linux/io.h>
>> +#include <linux/module.h>
>> +
>> +struct ot200_led {
>> + Â Â Â struct led_classdev cdev;
>> + Â Â Â char name[8];
>> + Â Â Â unsigned int num;
>> +};
>> +
>> +static u8 old_val;
>> +static u8 old_val_back;
>> +DEFINE_SPINLOCK(value_lock);
>> +
>> +static void ot200_led_set(struct led_classdev *led_cdev,
>> + Â Â Â Â Â Â Â Â Â Â Â enum led_brightness value)
>> +{
>> + Â Â Â struct ot200_led *led = container_of(led_cdev, struct ot200_led, cdev);
>> + Â Â Â u8 val;
>> + Â Â Â u8 mask;
>> + Â Â Â unsigned long flags;
>> +
>> + Â Â Â mask = 1 << led->num;
>> +
>> + Â Â Â spin_lock_irqsave(&value_lock, flags);
>> + Â Â Â val = old_val;
>> +
>> + Â Â Â if (value == LED_OFF)
>> + Â Â Â Â Â Â Â val &= ~mask;
>> + Â Â Â else
>> + Â Â Â Â Â Â Â val |= mask;
>> +
>> + Â Â Â old_val = val;
>> + Â Â Â spin_unlock_irqrestore(&value_lock, flags);
>> + Â Â Â outb(val, 0x49);
>> +}
>> +
>
> These two functions (above and beneath) look rather similar, they could
> probably share most of their code. Maybe use the leds num field to
> distinguish between the two types and choose the address where to write the
> value accordingly.

Sounds like a good idea.

>
>> +static void ot200_led_back_set(struct led_classdev *led_cdev,
>> + Â Â Â Â Â Â Â Â Â Â Â enum led_brightness value)
>> +{
>> + Â Â Â struct ot200_led *led = container_of(led_cdev, struct ot200_led, cdev);
>> + Â Â Â u8 val;
>> + Â Â Â u8 mask;
>> + Â Â Â unsigned long flags;
>> +
>> + Â Â Â mask = 1 << led->num;
>> +
>> + Â Â Â spin_lock_irqsave(&value_lock, flags);
>> + Â Â Â val = old_val_back;
>> +
>> + Â Â Â if (value == LED_OFF)
>> + Â Â Â Â Â Â Â val &= ~mask;
>> + Â Â Â else
>> + Â Â Â Â Â Â Â val |= mask;
>> +
>> + Â Â Â old_val_back = val;
>> + Â Â Â spin_unlock_irqrestore(&value_lock, flags);
>> + Â Â Â outb(val, 0x5a);
>> +}
>> +
>> +static int __devinit create_ot200_led(struct platform_device *pdev, int num,
>> + Â Â Â Â Â Â Â struct ot200_led *led)
>> +{
>> + Â Â Â int ret;
>> +
>> + Â Â Â num += 1;
>> + Â Â Â snprintf(led->name, sizeof(led->name), "led%d", num);
>> + Â Â Â led->num = 7 - num;
>> + Â Â Â led->cdev.name = led->name;
>> + Â Â Â led->cdev.default_trigger = NULL;
>> + Â Â Â led->cdev.blink_set = NULL;
>> + Â Â Â led->cdev.brightness_set = ot200_led_set;
>> +
>> + Â Â Â ret = led_classdev_register(&pdev->dev, &led->cdev);
>> + Â Â Â if (ret < 0)
>> + Â Â Â Â Â Â Â goto out;
>> +
>> + Â Â Â dev_set_drvdata(led->cdev.dev, led);
>> + Â Â Â return 0;
>> +
>> + Â Â Â led_classdev_unregister(&led->cdev);
>> +out:
>> + Â Â Â return ret;
>> +}
>
> These two look also rather similar.

Correct... will improve it in v2.

>
>> +
>> +static int __devinit create_ot200_led_back(struct platform_device *pdev,
>> + Â Â Â Â Â Â Â int num, struct ot200_led *led)
>> +{
>> + Â Â Â char *led_name;
>> + Â Â Â int ret;
>> +
>> + Â Â Â switch (num) {
>> + Â Â Â case 0:
>> + Â Â Â Â Â Â Â led_name = "run";
>> + Â Â Â Â Â Â Â break;
>> + Â Â Â case 1:
>> + Â Â Â Â Â Â Â led_name = "init";
>> + Â Â Â Â Â Â Â break;
>> + Â Â Â case 2:
>> + Â Â Â Â Â Â Â led_name = "err";
>> + Â Â Â Â Â Â Â break;
>> + Â Â Â default:
>> + Â Â Â Â Â Â Â BUG();
>> + Â Â Â };
>
> Since your led names are const and do not depend on platform data a global
> static array containing all the names (including the led prefix) should
> probably work as well.
>

I am fine with this.

>> +
>> + Â Â Â snprintf(led->name, sizeof(led->name), "led_%s", led_name);
>> + Â Â Â led->num = num;
>> + Â Â Â led->cdev.name = led->name;
>> + Â Â Â led->cdev.blink_set = NULL;
>> + Â Â Â led->cdev.brightness_set = ot200_led_back_set;
>> +
>> + Â Â Â ret = led_classdev_register(&pdev->dev, &led->cdev);
>> + Â Â Â if (ret < 0)
>> + Â Â Â Â Â Â Â goto out;
>> +
>> + Â Â Â dev_set_drvdata(led->cdev.dev, led);
>> + Â Â Â return 0;
>> +
>> + Â Â Â led_classdev_unregister(&led->cdev);
>> +out:
>> + Â Â Â return ret;
>> +
>> +}
>> +
>> +#define NUM_LEDS Â Â Â 7
>> +#define NUM_LEDS_BACK Â3
>> +#define NUM_LEDS_TOTAL (NUM_LEDS + NUM_LEDS_BACK)
>> +
>> +static int __devinit ot200_led_probe(struct platform_device *pdev)
>> +{
>> + Â Â Â struct ot200_led *leds;
>> + Â Â Â int i;
>> + Â Â Â int ret;
>> +
>> + Â Â Â leds = kzalloc(sizeof(struct ot200_led) * NUM_LEDS_TOTAL, GFP_KERNEL);
>
> kcalloc
>

okay

>> + Â Â Â if (!leds)
>> + Â Â Â Â Â Â Â return -ENOMEM;
>> +
>> + Â Â Â for (i = 0; i < NUM_LEDS; i++) {
>> + Â Â Â Â Â Â Â ret = create_ot200_led(pdev, i, &leds[i]);
>> + Â Â Â Â Â Â Â if (ret < 0)
>> + Â Â Â Â Â Â Â Â Â Â Â goto err;
>> + Â Â Â }
>> +
>> + Â Â Â for (i = NUM_LEDS; i < NUM_LEDS + NUM_LEDS_BACK; i++) {
>> + Â Â Â Â Â Â Â ret = create_ot200_led_back(pdev, i - NUM_LEDS, &leds[i]);
>> + Â Â Â Â Â Â Â if (ret < 0)
>> + Â Â Â Â Â Â Â Â Â Â Â goto err;
>> + Â Â Â }
>> +
>> + Â Â Â platform_set_drvdata(pdev, leds);
>> + Â Â Â outb(0x0, 0x49); Â Â Â Â/* turn of all front leds */
>
> off
>

ups

>> + Â Â Â outb(0x2, 0x5a); Â Â Â Â/* turn on init led */
>> + Â Â Â return 0;
>> +
>> +err:
>> + Â Â Â for (i = i - 1; i >= 0; i--)
>> + Â Â Â Â Â Â Â led_classdev_unregister(&leds[i].cdev);
>> +
>> + Â Â Â kfree(leds);
>> +
>> + Â Â Â return ret;
>> +}
>> +
>> +static int __devexit ot200_led_remove(struct platform_device *pdev)
>> +{
>> + Â Â Â int i;
>> + Â Â Â struct ot200_led *leds;
>> +
>> + Â Â Â leds = platform_get_drvdata(pdev);
>> +
>> + Â Â Â for (i = 0; i < NUM_LEDS_TOTAL; i++)
>> + Â Â Â Â Â Â Â led_classdev_unregister(&leds[i].cdev);
>> +
>> + Â Â Â kfree(leds);
>> + Â Â Â platform_set_drvdata(pdev, NULL);
>
> platform_set_drvdata(pdev, NULL) is not necessary.

okay

>
>> +
>> + Â Â Â return 0;
>> +}
>> +
>> +static struct platform_driver ot200_led_driver = {
>> +    .probe     Â= ot200_led_probe,
>> +    .remove     = __devexit_p(ot200_led_remove),
>> +    .driver     = {
>> +        .name  = "leds-ot200",
>> + Â Â Â Â Â Â Â .owner Â= THIS_MODULE,
>> + Â Â Â },
>> +};
>> +
>> +module_platform_driver(ot200_led_driver);
>> +
>> +MODULE_AUTHOR("Sebastian A. Siewior <bigeasy@xxxxxxxxxxxxx>");
>> +MODULE_DESCRIPTION("ot200 LED driver");
>> +MODULE_LICENSE("GPL");
>> +MODULE_ALIAS("platform:leds-ot200");
>> --
>> 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/
>
--
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/