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