forked from patjak/mba6x_bl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mba6x_bl.c
413 lines (332 loc) · 9.2 KB
/
mba6x_bl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* MacBook Air 6,1 and 6,2 (mid 2013) backlight driver
*
* Copyright (C) 2014 Patrik Jakobsson ([email protected])
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation.
*
*/
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/notifier.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/backlight.h>
#include <linux/acpi.h>
#include <acpi/acpi.h>
#include <acpi/video.h>
#define LP8550_SMBUS_ADDR (0x58 >> 1)
#define LP8550_REG_BRIGHTNESS 0
#define LP8550_REG_DEV_CTL 1
#define LP8550_REG_FAULT 2
#define LP8550_REG_IDENT 3
#define LP8550_REG_DIRECT_CTL 4
#define LP8550_REG_TEMP_MSB 5 /* Must be read before TEMP_LSB */
#define LP8550_REG_TEMP_LSB 6
#define INIT_BRIGHTNESS 150
#define MAX_RETRIES 20
static struct platform_device *platform_device;
static struct backlight_device *backlight_device;
static int backlight_notify(struct notifier_block *self, unsigned long action,
void *data);
struct notifier_block backlight_nb = {
.notifier_call = backlight_notify,
};
static struct {
struct delayed_work work;
struct mutex mutex;
struct {
u8 brightness; /* Brightness control */
u8 dev_ctl; /* Device control */
u8 fault; /* Fault indication */
u8 ident; /* Identification */
u8 direct_ctl; /* Direct control */
u8 temp_msb; /* Temperature MSB */
u8 temp_lsb; /* Temperature LSB */
} lp8550_regs;
} dev_priv;
static int lp8550_reg_read(u8 reg, u8 *val)
{
acpi_status status;
acpi_handle handle;
struct acpi_object_list arg_list;
struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
union acpi_object args[2];
union acpi_object *result;
int ret = 0;
mutex_lock(&dev_priv.mutex);
status = acpi_get_handle(NULL, "\\_SB.PCI0.SBUS.SRDB", &handle);
if (ACPI_FAILURE(status)) {
pr_err("mba6x_bl: Failed to get acpi handle\n");
ret = -ENODEV;
goto out;
}
args[0].type = ACPI_TYPE_INTEGER;
args[0].integer.value = (LP8550_SMBUS_ADDR << 1) | 1;
args[1].type = ACPI_TYPE_INTEGER;
args[1].integer.value = reg;
arg_list.count = 2;
arg_list.pointer = args;
status = acpi_evaluate_object(handle, NULL, &arg_list, &buffer);
if (ACPI_FAILURE(status)) {
pr_err("mba6x_bl: Failed to read reg: 0x%x\n", reg);
ret = -ENODEV;
goto out;
}
result = buffer.pointer;
if (result->type != ACPI_TYPE_INTEGER) {
pr_err("mba6x_bl: Invalid response in reg: 0x%x (len: %Ld)\n",
reg, buffer.length);
ret = -EINVAL;
goto out;
}
*val = (u8)result->integer.value;
out:
mutex_unlock(&dev_priv.mutex);
kfree(buffer.pointer);
return ret;
}
static int lp8550_reg_write(u8 reg, u8 val)
{
acpi_status status;
acpi_handle handle;
struct acpi_object_list arg_list;
struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
union acpi_object args[3];
union acpi_object *result;
int ret = 0;
u8 read_val;
mutex_lock(&dev_priv.mutex);
status = acpi_get_handle(NULL, "\\_SB.PCI0.SBUS.SWRB", &handle);
if (ACPI_FAILURE(status)) {
pr_err("mba6x_bl: Failed to get acpi handle\n");
ret = -ENODEV;
goto out;
}
args[0].type = ACPI_TYPE_INTEGER;
args[0].integer.value = (LP8550_SMBUS_ADDR << 1) & ~1;
args[1].type = ACPI_TYPE_INTEGER;
args[1].integer.value = reg;
args[2].type = ACPI_TYPE_INTEGER;
args[2].integer.value = val;
arg_list.count = 3;
arg_list.pointer = args;
status = acpi_evaluate_object(handle, NULL, &arg_list, &buffer);
if (ACPI_FAILURE(status)) {
pr_err("mba6x_bl: Failed to write reg: 0x%x\n", reg);
ret = -ENODEV;
goto out;
}
result = buffer.pointer;
if (result->type != ACPI_TYPE_INTEGER || result->integer.value != 1) {
pr_err("mba6x_bl: Invalid response at reg: 0x%x (len: %Ld)\n",
reg, buffer.length);
ret = -EINVAL;
goto out;
}
out:
mutex_unlock(&dev_priv.mutex);
/* Read back the register to see if it stuck */
lp8550_reg_read(reg, &read_val);
if (!ret && read_val != val) {
pr_err("mba6x_bl: Read-back failed at reg: 0x%x, val: 0x%x\n",
reg, val);
ret = -EINVAL;
}
kfree(buffer.pointer);
return ret;
}
static inline int map_brightness(int b)
{
return ((b * b + 254) / 255);
}
static int set_brightness(int brightness)
{
int ret;
if (brightness < 0 || brightness > 255)
return -EINVAL;
ret = lp8550_reg_write(LP8550_REG_DEV_CTL, 0x05);
if (ret)
return -ENODEV;
brightness = map_brightness(brightness);
ret = lp8550_reg_write(LP8550_REG_BRIGHTNESS, (u8)brightness);
if (ret)
return -ENODEV;
return ret;
}
static int get_brightness(struct backlight_device *bdev)
{
return bdev->props.brightness;
}
static int update_status(struct backlight_device *bdev)
{
/* We lock when writing to the SMBUS so work must be scheduled */
schedule_delayed_work(&dev_priv.work, 0);
return 0;
}
static int lp8550_probe(void)
{
u8 val;
int ret;
ret = lp8550_reg_read(LP8550_REG_IDENT, &val);
if (ret)
return ret;
if (val != 0xfc)
return -ENODEV;
pr_info("mba6x_bl: Found LP8550 backlight driver\n");
return 0;
}
static int lp8550_save(void)
{
int ret;
ret = lp8550_reg_read(LP8550_REG_DEV_CTL,
&dev_priv.lp8550_regs.dev_ctl);
if (ret)
return ret;
ret = lp8550_reg_read(LP8550_REG_BRIGHTNESS,
&dev_priv.lp8550_regs.brightness);
return ret;
}
static int lp8550_restore(void)
{
int ret;
ret = lp8550_reg_write(LP8550_REG_BRIGHTNESS,
dev_priv.lp8550_regs.brightness);
if (ret)
return ret;
ret = lp8550_reg_write(LP8550_REG_DEV_CTL,
dev_priv.lp8550_regs.dev_ctl);
return ret;
}
static struct backlight_ops backlight_ops = {
.update_status = update_status,
.get_brightness = get_brightness,
};
static struct platform_driver drv;
static void brightness_work(struct work_struct *work)
{
int ret, i;
/* Retry for a maximum of 2 seconds */
for (i = 0; i < MAX_RETRIES; i++) {
ret = set_brightness(backlight_device->props.brightness);
if (!ret)
break;
msleep(100);
}
if (i >= MAX_RETRIES) {
pr_info("mba6x_bl: failed to set brightness\n");
return;
} else if (i > 0) {
pr_info("mba6x_bl: set brightness retries = %d\n", i);
}
if (backlight_device->props.brightness == 0)
backlight_device->props.power = 4;
else
backlight_device->props.power = 0;
}
static int backlight_notify(struct notifier_block *self, unsigned long action,
void *data)
{
schedule_delayed_work(&dev_priv.work, 100);
return 0;
}
static int platform_probe(struct platform_device *dev)
{
struct backlight_properties props;
int ret;
mutex_init(&dev_priv.mutex);
INIT_DELAYED_WORK(&dev_priv.work, brightness_work);
ret = lp8550_probe();
if (ret)
return ret;
ret = lp8550_save();
if (ret)
return ret;
memset(&props, 0, sizeof(struct backlight_properties));
props.max_brightness = 255;
props.type = BACKLIGHT_FIRMWARE;
props.power = 0; /* Power is on */
backlight_device = backlight_device_register("mba6x_backlight",
NULL, NULL,
&backlight_ops, &props);
if (IS_ERR(backlight_device)) {
pr_err("mba6x_bl: Failed to register backlight device\n");
return PTR_ERR(backlight_device);
}
backlight_device->props.brightness = INIT_BRIGHTNESS;
backlight_update_status(backlight_device);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0)
acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
#else
acpi_video_dmi_promote_vendor();
#endif
acpi_video_unregister();
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 15, 0)
backlight_register_notifier(&backlight_nb);
#endif
return 0;
}
static int platform_remove(struct platform_device *dev)
{
backlight_device_unregister(backlight_device);
lp8550_restore();
pr_info("mba6x_bl: Restored old configuration\n");
return 0;
}
static int platform_resume(struct platform_device *dev)
{
/* We must delay this work to let the SMBUS initialize */
schedule_delayed_work(&dev_priv.work, 100);
return 0;
}
static void platform_shutdown(struct platform_device *dev)
{
/* We must restore or screen might go black on reboot */
lp8550_restore();
}
static struct platform_driver drv = {
.probe = platform_probe,
.remove = platform_remove,
.resume = platform_resume,
.shutdown = platform_shutdown,
.driver = {
.name = "mba6x_bl",
.owner = THIS_MODULE,
},
};
static int __init mba6x_bl_init(void)
{
int ret;
ret = platform_driver_register(&drv);
if (ret) {
pr_err("Failed to register platform driver\n");
return ret;
}
platform_device = platform_device_register_simple("mba6x_bl", -1, NULL,
0);
return 0;
}
static void __exit mba6x_bl_exit(void)
{
platform_device_unregister(platform_device);
platform_driver_unregister(&drv);
}
module_init(mba6x_bl_init);
module_exit(mba6x_bl_exit);
MODULE_AUTHOR("Patrik Jakobsson <[email protected]>");
MODULE_DESCRIPTION("MacBook Air 6,1 and 6,2 backlight driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("dmi:*:pnMacBookAir6*");
MODULE_ALIAS("dmi:*:pnMacBookAir7*");