Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / video / backlight / kb3886_bl.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Backlight Driver for the KB3886 Backlight
4  *
5  *  Copyright (c) 2007-2008 Claudio Nieder
6  *
7  *  Based on corgi_bl.c by Richard Purdie and kb3886 driver by Robert Woerle
8  */
9
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
14 #include <linux/mutex.h>
15 #include <linux/fb.h>
16 #include <linux/backlight.h>
17 #include <linux/delay.h>
18 #include <linux/dmi.h>
19
20 #define KB3886_PARENT 0x64
21 #define KB3886_IO 0x60
22 #define KB3886_ADC_DAC_PWM 0xC4
23 #define KB3886_PWM0_WRITE 0x81
24 #define KB3886_PWM0_READ 0x41
25
26 static DEFINE_MUTEX(bl_mutex);
27
28 static void kb3886_bl_set_intensity(int intensity)
29 {
30         mutex_lock(&bl_mutex);
31         intensity = intensity&0xff;
32         outb(KB3886_ADC_DAC_PWM, KB3886_PARENT);
33         usleep_range(10000, 11000);
34         outb(KB3886_PWM0_WRITE, KB3886_IO);
35         usleep_range(10000, 11000);
36         outb(intensity, KB3886_IO);
37         mutex_unlock(&bl_mutex);
38 }
39
40 struct kb3886bl_machinfo {
41         int max_intensity;
42         int default_intensity;
43         int limit_mask;
44         void (*set_bl_intensity)(int intensity);
45 };
46
47 static struct kb3886bl_machinfo kb3886_bl_machinfo = {
48         .max_intensity = 0xff,
49         .default_intensity = 0xa0,
50         .limit_mask = 0x7f,
51         .set_bl_intensity = kb3886_bl_set_intensity,
52 };
53
54 static struct platform_device kb3886bl_device = {
55         .name           = "kb3886-bl",
56         .dev            = {
57                 .platform_data  = &kb3886_bl_machinfo,
58         },
59         .id             = -1,
60 };
61
62 static struct platform_device *devices[] __initdata = {
63         &kb3886bl_device,
64 };
65
66 /*
67  * Back to driver
68  */
69
70 static int kb3886bl_intensity;
71 static struct backlight_device *kb3886_backlight_device;
72 static struct kb3886bl_machinfo *bl_machinfo;
73
74 static unsigned long kb3886bl_flags;
75 #define KB3886BL_SUSPENDED     0x01
76
77 static const struct dmi_system_id kb3886bl_device_table[] __initconst = {
78         {
79                 .ident = "Sahara Touch-iT",
80                 .matches = {
81                         DMI_MATCH(DMI_SYS_VENDOR, "SDV"),
82                         DMI_MATCH(DMI_PRODUCT_NAME, "iTouch T201"),
83                 },
84         },
85         { }
86 };
87
88 static int kb3886bl_send_intensity(struct backlight_device *bd)
89 {
90         int intensity = bd->props.brightness;
91
92         if (bd->props.power != FB_BLANK_UNBLANK)
93                 intensity = 0;
94         if (bd->props.fb_blank != FB_BLANK_UNBLANK)
95                 intensity = 0;
96         if (kb3886bl_flags & KB3886BL_SUSPENDED)
97                 intensity = 0;
98
99         bl_machinfo->set_bl_intensity(intensity);
100
101         kb3886bl_intensity = intensity;
102         return 0;
103 }
104
105 #ifdef CONFIG_PM_SLEEP
106 static int kb3886bl_suspend(struct device *dev)
107 {
108         struct backlight_device *bd = dev_get_drvdata(dev);
109
110         kb3886bl_flags |= KB3886BL_SUSPENDED;
111         backlight_update_status(bd);
112         return 0;
113 }
114
115 static int kb3886bl_resume(struct device *dev)
116 {
117         struct backlight_device *bd = dev_get_drvdata(dev);
118
119         kb3886bl_flags &= ~KB3886BL_SUSPENDED;
120         backlight_update_status(bd);
121         return 0;
122 }
123 #endif
124
125 static SIMPLE_DEV_PM_OPS(kb3886bl_pm_ops, kb3886bl_suspend, kb3886bl_resume);
126
127 static int kb3886bl_get_intensity(struct backlight_device *bd)
128 {
129         return kb3886bl_intensity;
130 }
131
132 static const struct backlight_ops kb3886bl_ops = {
133         .get_brightness = kb3886bl_get_intensity,
134         .update_status  = kb3886bl_send_intensity,
135 };
136
137 static int kb3886bl_probe(struct platform_device *pdev)
138 {
139         struct backlight_properties props;
140         struct kb3886bl_machinfo *machinfo = dev_get_platdata(&pdev->dev);
141
142         bl_machinfo = machinfo;
143         if (!machinfo->limit_mask)
144                 machinfo->limit_mask = -1;
145
146         memset(&props, 0, sizeof(struct backlight_properties));
147         props.type = BACKLIGHT_RAW;
148         props.max_brightness = machinfo->max_intensity;
149         kb3886_backlight_device = devm_backlight_device_register(&pdev->dev,
150                                                         "kb3886-bl", &pdev->dev,
151                                                         NULL, &kb3886bl_ops,
152                                                         &props);
153         if (IS_ERR(kb3886_backlight_device))
154                 return PTR_ERR(kb3886_backlight_device);
155
156         platform_set_drvdata(pdev, kb3886_backlight_device);
157
158         kb3886_backlight_device->props.power = FB_BLANK_UNBLANK;
159         kb3886_backlight_device->props.brightness = machinfo->default_intensity;
160         backlight_update_status(kb3886_backlight_device);
161
162         return 0;
163 }
164
165 static struct platform_driver kb3886bl_driver = {
166         .probe          = kb3886bl_probe,
167         .driver         = {
168                 .name   = "kb3886-bl",
169                 .pm     = &kb3886bl_pm_ops,
170         },
171 };
172
173 static int __init kb3886_init(void)
174 {
175         if (!dmi_check_system(kb3886bl_device_table))
176                 return -ENODEV;
177
178         platform_add_devices(devices, ARRAY_SIZE(devices));
179         return platform_driver_register(&kb3886bl_driver);
180 }
181
182 static void __exit kb3886_exit(void)
183 {
184         platform_driver_unregister(&kb3886bl_driver);
185 }
186
187 module_init(kb3886_init);
188 module_exit(kb3886_exit);
189
190 MODULE_AUTHOR("Claudio Nieder <private@claudio.ch>");
191 MODULE_DESCRIPTION("Tabletkiosk Sahara Touch-iT Backlight Driver");
192 MODULE_LICENSE("GPL");
193 MODULE_ALIAS("dmi:*:svnSDV:pniTouchT201:*");