Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 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> |
Greg Kroah-Hartman | 16559ae | 2013-02-04 15:35:26 -0800 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 18 | #include <linux/cs5535.h> |
| 19 | |
| 20 | static struct cs5535_mfgpt_timer *pwm_timer; |
| 21 | |
| 22 | /* this array defines the mapping of brightness in % to pwm frequency */ |
| 23 | static 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 | |
| 33 | struct 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 | |
| 44 | static 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 | |
| 69 | static 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 | |
| 75 | static const struct backlight_ops ot200_backlight_ops = { |
| 76 | .update_status = ot200_backlight_update_status, |
| 77 | .get_brightness = ot200_backlight_get_brightness, |
| 78 | }; |
| 79 | |
| 80 | static 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 */ |
Jingoo Han | f673934 | 2012-07-30 14:40:37 -0700 | [diff] [blame] | 88 | if (devm_gpio_request(&pdev->dev, GPIO_DIMM, |
| 89 | "ot200 backlight dimmer") < 0) { |
Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 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"); |
Jingoo Han | f673934 | 2012-07-30 14:40:37 -0700 | [diff] [blame] | 98 | return -ENODEV; |
Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jingoo Han | f1b60d4 | 2012-07-30 14:40:33 -0700 | [diff] [blame] | 101 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 102 | if (!data) { |
| 103 | retval = -ENOMEM; |
Jingoo Han | f1b60d4 | 2012-07-30 14:40:33 -0700 | [diff] [blame] | 104 | goto error_devm_kzalloc; |
Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 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 | |
Jingoo Han | 443956f | 2014-01-23 15:54:27 -0800 | [diff] [blame] | 121 | bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev), |
| 122 | &pdev->dev, data, &ot200_backlight_ops, |
| 123 | &props); |
Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 124 | if (IS_ERR(bl)) { |
| 125 | dev_err(&pdev->dev, "failed to register backlight\n"); |
| 126 | retval = PTR_ERR(bl); |
Jingoo Han | f1b60d4 | 2012-07-30 14:40:33 -0700 | [diff] [blame] | 127 | goto error_devm_kzalloc; |
Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | platform_set_drvdata(pdev, bl); |
| 131 | |
| 132 | return 0; |
| 133 | |
Jingoo Han | f1b60d4 | 2012-07-30 14:40:33 -0700 | [diff] [blame] | 134 | error_devm_kzalloc: |
Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 135 | cs5535_mfgpt_free_timer(pwm_timer); |
Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 136 | return retval; |
| 137 | } |
| 138 | |
| 139 | static int ot200_backlight_remove(struct platform_device *pdev) |
| 140 | { |
Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 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); |
Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 148 | |
Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static struct platform_driver ot200_backlight_driver = { |
| 153 | .driver = { |
| 154 | .name = "ot200-backlight", |
Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 155 | }, |
| 156 | .probe = ot200_backlight_probe, |
| 157 | .remove = ot200_backlight_remove, |
| 158 | }; |
| 159 | |
| 160 | module_platform_driver(ot200_backlight_driver); |
| 161 | |
| 162 | MODULE_DESCRIPTION("backlight driver for ot200 visualisation device"); |
| 163 | MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>"); |
| 164 | MODULE_LICENSE("GPL"); |
| 165 | MODULE_ALIAS("platform:ot200-backlight"); |