1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Bachmann electronic GmbH
4 * Christian Gmeiner <christian.gmeiner@gmail.com>
6 * Backlight driver for ot200 visualisation device from
7 * Bachmann electronic GmbH.
10 #include <linux/module.h>
12 #include <linux/backlight.h>
13 #include <linux/gpio.h>
14 #include <linux/platform_device.h>
15 #include <linux/cs5535.h>
17 static struct cs5535_mfgpt_timer
*pwm_timer
;
19 /* this array defines the mapping of brightness in % to pwm frequency */
20 static 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,
30 struct ot200_backlight_data
{
31 int current_brightness
;
36 #define CMP1MODE 0x2 /* compare on GE; output high on compare
37 * greater than or equal */
38 #define PWM_SETUP (SCALE | CMP1MODE << 6 | MFGPT_SETUP_CNTEN)
41 static int ot200_backlight_update_status(struct backlight_device
*bl
)
43 struct ot200_backlight_data
*data
= bl_get_data(bl
);
44 int brightness
= bl
->props
.brightness
;
46 if (bl
->props
.state
& BL_CORE_FBBLANK
)
49 /* enable or disable PWM timer */
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
,
58 /* apply new brightness value */
59 cs5535_mfgpt_write(pwm_timer
, MFGPT_REG_CMP1
,
60 MAX_COMP2
- dim_table
[brightness
]);
61 data
->current_brightness
= brightness
;
66 static int ot200_backlight_get_brightness(struct backlight_device
*bl
)
68 struct ot200_backlight_data
*data
= bl_get_data(bl
);
69 return data
->current_brightness
;
72 static const struct backlight_ops ot200_backlight_ops
= {
73 .update_status
= ot200_backlight_update_status
,
74 .get_brightness
= ot200_backlight_get_brightness
,
77 static int ot200_backlight_probe(struct platform_device
*pdev
)
79 struct backlight_device
*bl
;
80 struct ot200_backlight_data
*data
;
81 struct backlight_properties props
;
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
);
92 pwm_timer
= cs5535_mfgpt_alloc_timer(7, MFGPT_DOMAIN_ANY
);
94 dev_err(&pdev
->dev
, "MFGPT 7 not available\n");
98 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
101 goto error_devm_kzalloc
;
105 cs5535_gpio_set(GPIO_DIMM
, GPIO_OUTPUT_ENABLE
);
106 cs5535_gpio_set(GPIO_DIMM
, GPIO_OUTPUT_AUX1
);
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
);
113 data
->current_brightness
= 100;
114 props
.max_brightness
= 100;
115 props
.brightness
= 100;
116 props
.type
= BACKLIGHT_RAW
;
118 bl
= devm_backlight_device_register(&pdev
->dev
, dev_name(&pdev
->dev
),
119 &pdev
->dev
, data
, &ot200_backlight_ops
,
122 dev_err(&pdev
->dev
, "failed to register backlight\n");
123 retval
= PTR_ERR(bl
);
124 goto error_devm_kzalloc
;
127 platform_set_drvdata(pdev
, bl
);
132 cs5535_mfgpt_free_timer(pwm_timer
);
136 static int ot200_backlight_remove(struct platform_device
*pdev
)
138 /* on module unload set brightness to 100% */
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]);
144 cs5535_mfgpt_free_timer(pwm_timer
);
149 static struct platform_driver ot200_backlight_driver
= {
151 .name
= "ot200-backlight",
153 .probe
= ot200_backlight_probe
,
154 .remove
= ot200_backlight_remove
,
157 module_platform_driver(ot200_backlight_driver
);
159 MODULE_DESCRIPTION("backlight driver for ot200 visualisation device");
160 MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
161 MODULE_LICENSE("GPL");
162 MODULE_ALIAS("platform:ot200-backlight");