linux/drivers/video/backlight/ot200_bl.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2012 Bachmann electronic GmbH
   3 *      Christian Gmeiner <christian.gmeiner@gmail.com>
   4 *
   5 * Backlight driver for ot200 visualisation device from
   6 * Bachmann electronic GmbH.
   7 *
   8 * This program is free software; you can redistribute it and/or modify it
   9 * under the terms of the GNU General Public License version 2 as published by
  10 * the Free Software Foundation.
  11 */
  12
  13#include <linux/module.h>
  14#include <linux/fb.h>
  15#include <linux/backlight.h>
  16#include <linux/gpio.h>
  17#include <linux/platform_device.h>
  18#include <linux/cs5535.h>
  19
  20static struct cs5535_mfgpt_timer *pwm_timer;
  21
  22/* this array defines the mapping of brightness in % to pwm frequency */
  23static const u8 dim_table[101] = {0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
  24                                  2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
  25                                  4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9,
  26                                  10, 10, 11, 11, 12, 12, 13, 14, 15, 15, 16,
  27                                  17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28,
  28                                  30, 31, 33, 35, 37, 39, 41, 43, 45, 47, 50,
  29                                  53, 55, 58, 61, 65, 68, 72, 75, 79, 84, 88,
  30                                  93, 97, 103, 108, 114, 120, 126, 133, 140,
  31                                  147, 155, 163};
  32
  33struct ot200_backlight_data {
  34        int current_brightness;
  35};
  36
  37#define GPIO_DIMM       27
  38#define SCALE           1
  39#define CMP1MODE        0x2     /* compare on GE; output high on compare
  40                                 * greater than or equal */
  41#define PWM_SETUP       (SCALE | CMP1MODE << 6 | MFGPT_SETUP_CNTEN)
  42#define MAX_COMP2       163
  43
  44static int ot200_backlight_update_status(struct backlight_device *bl)
  45{
  46        struct ot200_backlight_data *data = bl_get_data(bl);
  47        int brightness = bl->props.brightness;
  48
  49        if (bl->props.state & BL_CORE_FBBLANK)
  50                brightness = 0;
  51
  52        /* enable or disable PWM timer */
  53        if (brightness == 0)
  54                cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, 0);
  55        else if (data->current_brightness == 0) {
  56                cs5535_mfgpt_write(pwm_timer, MFGPT_REG_COUNTER, 0);
  57                cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP,
  58                        MFGPT_SETUP_CNTEN);
  59        }
  60
  61        /* apply new brightness value */
  62        cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP1,
  63                MAX_COMP2 - dim_table[brightness]);
  64        data->current_brightness = brightness;
  65
  66        return 0;
  67}
  68
  69static int ot200_backlight_get_brightness(struct backlight_device *bl)
  70{
  71        struct ot200_backlight_data *data = bl_get_data(bl);
  72        return data->current_brightness;
  73}
  74
  75static const struct backlight_ops ot200_backlight_ops = {
  76        .update_status  = ot200_backlight_update_status,
  77        .get_brightness = ot200_backlight_get_brightness,
  78};
  79
  80static int ot200_backlight_probe(struct platform_device *pdev)
  81{
  82        struct backlight_device *bl;
  83        struct ot200_backlight_data *data;
  84        struct backlight_properties props;
  85        int retval = 0;
  86
  87        /* request gpio */
  88        if (devm_gpio_request(&pdev->dev, GPIO_DIMM,
  89                                "ot200 backlight dimmer") < 0) {
  90                dev_err(&pdev->dev, "failed to request GPIO %d\n", GPIO_DIMM);
  91                return -ENODEV;
  92        }
  93
  94        /* request timer */
  95        pwm_timer = cs5535_mfgpt_alloc_timer(7, MFGPT_DOMAIN_ANY);
  96        if (!pwm_timer) {
  97                dev_err(&pdev->dev, "MFGPT 7 not available\n");
  98                return -ENODEV;
  99        }
 100
 101        data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
 102        if (!data) {
 103                retval = -ENOMEM;
 104                goto error_devm_kzalloc;
 105        }
 106
 107        /* setup gpio */
 108        cs5535_gpio_set(GPIO_DIMM, GPIO_OUTPUT_ENABLE);
 109        cs5535_gpio_set(GPIO_DIMM, GPIO_OUTPUT_AUX1);
 110
 111        /* setup timer */
 112        cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP1, 0);
 113        cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP2, MAX_COMP2);
 114        cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, PWM_SETUP);
 115
 116        data->current_brightness = 100;
 117        props.max_brightness = 100;
 118        props.brightness = 100;
 119        props.type = BACKLIGHT_RAW;
 120
 121        bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev),
 122                                        &pdev->dev, data, &ot200_backlight_ops,
 123                                        &props);
 124        if (IS_ERR(bl)) {
 125                dev_err(&pdev->dev, "failed to register backlight\n");
 126                retval = PTR_ERR(bl);
 127                goto error_devm_kzalloc;
 128        }
 129
 130        platform_set_drvdata(pdev, bl);
 131
 132        return 0;
 133
 134error_devm_kzalloc:
 135        cs5535_mfgpt_free_timer(pwm_timer);
 136        return retval;
 137}
 138
 139static int ot200_backlight_remove(struct platform_device *pdev)
 140{
 141        /* on module unload set brightness to 100% */
 142        cs5535_mfgpt_write(pwm_timer, MFGPT_REG_COUNTER, 0);
 143        cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
 144        cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP1,
 145                MAX_COMP2 - dim_table[100]);
 146
 147        cs5535_mfgpt_free_timer(pwm_timer);
 148
 149        return 0;
 150}
 151
 152static struct platform_driver ot200_backlight_driver = {
 153        .driver         = {
 154                .name   = "ot200-backlight",
 155        },
 156        .probe          = ot200_backlight_probe,
 157        .remove         = ot200_backlight_remove,
 158};
 159
 160module_platform_driver(ot200_backlight_driver);
 161
 162MODULE_DESCRIPTION("backlight driver for ot200 visualisation device");
 163MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
 164MODULE_LICENSE("GPL");
 165MODULE_ALIAS("platform:ot200-backlight");
 166