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/io.h>
14 #include <linux/platform_device.h>
15 #include <linux/mutex.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
kb3886_bl_set_intensity(int intensity)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
kb3886bl_send_intensity(struct backlight_device * bd)88 static int kb3886bl_send_intensity(struct backlight_device *bd)
89 {
90 int intensity = backlight_get_brightness(bd);
91
92 if (kb3886bl_flags & KB3886BL_SUSPENDED)
93 intensity = 0;
94
95 bl_machinfo->set_bl_intensity(intensity);
96
97 kb3886bl_intensity = intensity;
98 return 0;
99 }
100
101 #ifdef CONFIG_PM_SLEEP
kb3886bl_suspend(struct device * dev)102 static int kb3886bl_suspend(struct device *dev)
103 {
104 struct backlight_device *bd = dev_get_drvdata(dev);
105
106 kb3886bl_flags |= KB3886BL_SUSPENDED;
107 backlight_update_status(bd);
108 return 0;
109 }
110
kb3886bl_resume(struct device * dev)111 static int kb3886bl_resume(struct device *dev)
112 {
113 struct backlight_device *bd = dev_get_drvdata(dev);
114
115 kb3886bl_flags &= ~KB3886BL_SUSPENDED;
116 backlight_update_status(bd);
117 return 0;
118 }
119 #endif
120
121 static SIMPLE_DEV_PM_OPS(kb3886bl_pm_ops, kb3886bl_suspend, kb3886bl_resume);
122
kb3886bl_get_intensity(struct backlight_device * bd)123 static int kb3886bl_get_intensity(struct backlight_device *bd)
124 {
125 return kb3886bl_intensity;
126 }
127
128 static const struct backlight_ops kb3886bl_ops = {
129 .get_brightness = kb3886bl_get_intensity,
130 .update_status = kb3886bl_send_intensity,
131 };
132
kb3886bl_probe(struct platform_device * pdev)133 static int kb3886bl_probe(struct platform_device *pdev)
134 {
135 struct backlight_properties props;
136 struct kb3886bl_machinfo *machinfo = dev_get_platdata(&pdev->dev);
137
138 bl_machinfo = machinfo;
139 if (!machinfo->limit_mask)
140 machinfo->limit_mask = -1;
141
142 memset(&props, 0, sizeof(struct backlight_properties));
143 props.type = BACKLIGHT_RAW;
144 props.max_brightness = machinfo->max_intensity;
145 kb3886_backlight_device = devm_backlight_device_register(&pdev->dev,
146 "kb3886-bl", &pdev->dev,
147 NULL, &kb3886bl_ops,
148 &props);
149 if (IS_ERR(kb3886_backlight_device))
150 return PTR_ERR(kb3886_backlight_device);
151
152 platform_set_drvdata(pdev, kb3886_backlight_device);
153
154 kb3886_backlight_device->props.power = BACKLIGHT_POWER_ON;
155 kb3886_backlight_device->props.brightness = machinfo->default_intensity;
156 backlight_update_status(kb3886_backlight_device);
157
158 return 0;
159 }
160
161 static struct platform_driver kb3886bl_driver = {
162 .probe = kb3886bl_probe,
163 .driver = {
164 .name = "kb3886-bl",
165 .pm = &kb3886bl_pm_ops,
166 },
167 };
168
kb3886_init(void)169 static int __init kb3886_init(void)
170 {
171 if (!dmi_check_system(kb3886bl_device_table))
172 return -ENODEV;
173
174 platform_add_devices(devices, ARRAY_SIZE(devices));
175 return platform_driver_register(&kb3886bl_driver);
176 }
177
kb3886_exit(void)178 static void __exit kb3886_exit(void)
179 {
180 platform_driver_unregister(&kb3886bl_driver);
181 }
182
183 module_init(kb3886_init);
184 module_exit(kb3886_exit);
185
186 MODULE_AUTHOR("Claudio Nieder <private@claudio.ch>");
187 MODULE_DESCRIPTION("Tabletkiosk Sahara Touch-iT Backlight Driver");
188 MODULE_LICENSE("GPL");
189 MODULE_ALIAS("dmi:*:svnSDV:pniTouchT201:*");
190