Linux 4.18.10
[linux/fpc-iii.git] / drivers / video / backlight / ot200_bl.c
blob3acdb9f646ed05792fa047eb5272d03a23e8eef6
1 /*
2 * Copyright (C) 2012 Bachmann electronic GmbH
3 * Christian Gmeiner <christian.gmeiner@gmail.com>
5 * Backlight driver for ot200 visualisation device from
6 * Bachmann electronic GmbH.
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.
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>
20 static struct cs5535_mfgpt_timer *pwm_timer;
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};
33 struct ot200_backlight_data {
34 int current_brightness;
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
44 static int ot200_backlight_update_status(struct backlight_device *bl)
46 struct ot200_backlight_data *data = bl_get_data(bl);
47 int brightness = bl->props.brightness;
49 if (bl->props.state & BL_CORE_FBBLANK)
50 brightness = 0;
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);
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;
66 return 0;
69 static int ot200_backlight_get_brightness(struct backlight_device *bl)
71 struct ot200_backlight_data *data = bl_get_data(bl);
72 return data->current_brightness;
75 static const struct backlight_ops ot200_backlight_ops = {
76 .update_status = ot200_backlight_update_status,
77 .get_brightness = ot200_backlight_get_brightness,
80 static int ot200_backlight_probe(struct platform_device *pdev)
82 struct backlight_device *bl;
83 struct ot200_backlight_data *data;
84 struct backlight_properties props;
85 int retval = 0;
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;
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;
101 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
102 if (!data) {
103 retval = -ENOMEM;
104 goto error_devm_kzalloc;
107 /* setup gpio */
108 cs5535_gpio_set(GPIO_DIMM, GPIO_OUTPUT_ENABLE);
109 cs5535_gpio_set(GPIO_DIMM, GPIO_OUTPUT_AUX1);
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);
116 data->current_brightness = 100;
117 props.max_brightness = 100;
118 props.brightness = 100;
119 props.type = BACKLIGHT_RAW;
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;
130 platform_set_drvdata(pdev, bl);
132 return 0;
134 error_devm_kzalloc:
135 cs5535_mfgpt_free_timer(pwm_timer);
136 return retval;
139 static int ot200_backlight_remove(struct platform_device *pdev)
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]);
147 cs5535_mfgpt_free_timer(pwm_timer);
149 return 0;
152 static struct platform_driver ot200_backlight_driver = {
153 .driver = {
154 .name = "ot200-backlight",
156 .probe = ot200_backlight_probe,
157 .remove = ot200_backlight_remove,
160 module_platform_driver(ot200_backlight_driver);
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");