]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/platform/x86/dell-uart-backlight.c
UBUNTU: SAUCE: platform/x86: dell-uart-backlight: increase retry times
[mirror_ubuntu-bionic-kernel.git] / drivers / platform / x86 / dell-uart-backlight.c
CommitLineData
2c36c90f
AK
1/*
2 * Dell AIO Serial Backlight Driver
3 *
4 * Copyright (C) 2017 AceLan Kao <acelan.kao@canonical.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/acpi.h>
21#include <linux/serial_8250.h>
22#include <linux/delay.h>
23#include <linux/backlight.h>
695f4adb 24#include <linux/dmi.h>
2c36c90f
AK
25#include <acpi/video.h>
26
27#include "dell-uart-backlight.h"
28
29struct dell_uart_backlight {
30 struct device *dev;
31 struct backlight_device *dell_uart_bd;
32 struct mutex brightness_mutex;
33 int line;
34 int bl_power;
35};
36struct uart_8250_port *serial8250_get_port(int line);
37static struct tty_struct *tty;
38static struct file *ftty;
39
40unsigned int (*io_serial_in)(struct uart_port *p, int offset);
41int (*uart_write)(struct tty_struct *tty, const unsigned char *buf, int count);
42int (*uart_chars_in_buffer)(struct tty_struct *tty);
43
3ae4a11c
AK
44static bool force;
45module_param(force, bool, 0444);
46MODULE_PARM_DESC(force, "load the driver regardless of the scalar status");
47
2c36c90f
AK
48static struct dell_uart_bl_cmd uart_cmd[] = {
49 /*
50 * Get Firmware Version: Tool uses this command to get firmware version.
51 * Command: 0x6A 0x06 0x8F (Length:3 Type: 0x0A, Cmd:6 Checksum:0x8F)
52 * Return data: 0x0D 0x06 Data checksum (Length:13,Cmd:0x06,
53 * Data :F/W version(APRILIA=APR27-VXXX,PHINE=PHI23-VXXX),
54 * checksum:SUM(Length and Cmd and Data)xor 0xFF .
55 */
56 [DELL_UART_GET_FIRMWARE_VER] = {
57 .cmd = {0x6A, 0x06, 0x8F},
58 .tx_len = 3,
59 },
42f03ced
AK
60 /*
61 * Get Scalar Status: Tool uses this command to check if scalar IC controls brightness.
62 * Command: 0x6A 0x1F 0x8F (Length:3 Type: 0x0A, Cmd:0x1F Checksum:0x76)
63 * Return data: 0x04 0x1F Data checksum
64 * (Data = 0: scalar cannot adjust brightness, Data = 1: scalar can adjust brightness)
65 */
66 [DELL_UART_GET_SCALAR] = {
67 .cmd = {0x6A,0x1F,0x76},
68 .ret = {0x04,0x1F,0x00,0x00},
69 .tx_len = 3,
70 .rx_len = 4,
71 },
2c36c90f
AK
72 /*
73 * Get Brightness level: Application uses this command for scaler to
74 * get brightness.
75 * Command: 0x6A 0x0C 0x89
76 * (Length:3 Type: 0x0A, Cmd:0x0C, Checksum:0x89)
77 * Return data: 0x04 0x0C Data checksum
78 * (Length:4 Cmd: 0x0C Data: brightness level
79 * checksum: SUM(Length and Cmd and Data)xor 0xFF)
80 * brightness level which ranges from 0~100.
81 */
82 [DELL_UART_GET_BRIGHTNESS] = {
83 .cmd = {0x6A, 0x0C, 0x89},
84 .ret = {0x04, 0x0C, 0x00, 0x00},
85 .tx_len = 3,
86 .rx_len = 4,
87 },
88 /* Set Brightness level: Application uses this command for scaler to
89 * set brightness.
90 * Command: 0x8A 0x0B Byte2 Checksum (Length:4 Type: 0x0A, Cmd:0x0B)
91 * where Byte2 is the brightness level which ranges from 0~100.
92 * Return data: 0x03 0x0B 0xF1(Length:3,Cmd:B,checksum:0xF1)
93 * Scaler must send the 3bytes ack within 1 second when success,
94 * other value if error
95 */
96 [DELL_UART_SET_BRIGHTNESS] = {
97 .cmd = {0x8A, 0x0B, 0x0, 0x0},
98 .ret = {0x03, 0x0B, 0xF1},
99 .tx_len = 4,
100 .rx_len = 3,
101 },
102 /*
103 * Screen ON/OFF Control: Application uses this command to control
104 * screen ON or OFF.
105 * Command: 0x8A 0x0E Byte2 Checksum (Length:4 Type: 0x0A, Cmd:0x0E)
106 * where
107 * Byte2=0 to turn OFF the screen.
108 * Byte2=1 to turn ON the screen
109 * Other value of Byte2 is reserved and invalid.
110 * Return data: 0x03 0x0E 0xEE(Length:3,Cmd:E,checksum:0xEE)
111 */
112 [DELL_UART_SET_BACKLIGHT_POWER] = {
113 .cmd = {0x8A, 0x0E, 0x00, 0x0},
114 .ret = {0x03, 0x0E, 0xEE},
115 .tx_len = 4,
116 .rx_len = 3,
117 },
118};
119
695f4adb
AK
120static const struct dmi_system_id dell_uart_backlight_alpha_platform[] __initconst = {
121 {
122 .ident = "Dell Inspiron 7777 AIO",
123 .matches = {
124 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
125 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7777 AIO"),
126 },
127 },
128 {
129 .ident = "Dell Inspiron 5477 AIO",
130 .matches = {
131 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
132 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5477 AIO"),
133 },
134 },
135 {
136 .ident = "Dell OptiPlex 7769 AIO",
137 .matches = {
138 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
139 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7769 AIO"),
140 },
141 },
142 {
143 .ident = "Dell OptiPlex 5260 AIO",
144 .matches = {
145 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
146 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 5260 AIO"),
147 },
148 },
149 { }
150};
151
2c36c90f
AK
152static int dell_uart_write(struct uart_8250_port *up, __u8 *buf, int len)
153{
154 int actual = 0;
155 struct uart_port *port = &up->port;
156
157 tty_port_tty_wakeup(&port->state->port);
158 tty = tty_port_tty_get(&port->state->port);
159 actual = uart_write(tty, buf, len);
160 while (uart_chars_in_buffer(tty))
161 udelay(10);
162
163 return actual;
164}
165
166static int dell_uart_read(struct uart_8250_port *up, __u8 *buf, int len)
167{
168 int i, retry;
169 unsigned long flags;
170
171 spin_lock_irqsave(&up->port.lock, flags);
172 for (i = 0; i < len; i++) {
173 retry = 10;
174 while (!(io_serial_in(&up->port, UART_LSR) & UART_LSR_DR)) {
175 if (--retry == 0)
176 break;
177 mdelay(20);
178 }
179
180 if (retry == 0)
181 break;
182 buf[i] = io_serial_in(&up->port, UART_RX);
183 }
184 spin_unlock_irqrestore(&up->port.lock, flags);
185
186 return i;
187}
188
189static void dell_uart_dump_cmd(const char *func, const char *prefix,
190 const char *cmd, int len)
191{
192 char buf[80];
193
194 snprintf(buf, 80, "dell_uart_backlight:%s:%s", func, prefix);
195 if (len != 0)
196 print_hex_dump_debug(buf, DUMP_PREFIX_NONE,
197 16, 1, cmd, len, false);
198 else
199 pr_debug("dell_uart_backlight:%s:%sNULL\n", func, prefix);
200
201}
202
203/*
204 * checksum: SUM(Length and Cmd and Data)xor 0xFF)
205 */
206static unsigned char dell_uart_checksum(unsigned char *buf, int len)
207{
208 unsigned char val = 0;
209
210 while (len-- > 0)
211 val += buf[len];
212
213 return val ^ 0xff;
214}
215
216/*
217 * There is no command to get backlight power status,
218 * so we set the backlight power to "on" while initializing,
219 * and then track and report its status by bl_power variable
220 */
221static inline int dell_uart_get_bl_power(struct dell_uart_backlight *dell_pdata)
222{
223 return dell_pdata->bl_power;
224}
225
226static int dell_uart_set_bl_power(struct backlight_device *bd, int power)
227{
228 struct dell_uart_bl_cmd *bl_cmd =
229 &uart_cmd[DELL_UART_SET_BACKLIGHT_POWER];
230 struct dell_uart_backlight *dell_pdata = bl_get_data(bd);
231 struct uart_8250_port *uart = serial8250_get_port(dell_pdata->line);
232 int rx_len;
233
234 if (power != FB_BLANK_POWERDOWN)
235 power = FB_BLANK_UNBLANK;
236
237 bl_cmd->cmd[2] = power ? 0 : 1;
238 bl_cmd->cmd[3] = dell_uart_checksum(bl_cmd->cmd, bl_cmd->tx_len - 1);
239
240 dell_uart_dump_cmd(__func__, "tx: ", bl_cmd->cmd, bl_cmd->tx_len);
241
242 if (mutex_lock_killable(&dell_pdata->brightness_mutex) < 0) {
243 pr_debug("Failed to get mutex_lock");
244 return 0;
245 }
246
247 dell_uart_write(uart, bl_cmd->cmd, bl_cmd->tx_len);
248 rx_len = dell_uart_read(uart, bl_cmd->ret, bl_cmd->rx_len);
249
250 mutex_unlock(&dell_pdata->brightness_mutex);
251
252 dell_uart_dump_cmd(__func__, "rx: ", bl_cmd->ret, rx_len);
253
254 bd->props.power = power;
255 dell_pdata->bl_power = power;
256
257 return 0;
258}
259
260static int dell_uart_get_brightness(struct backlight_device *bd)
261{
262 struct dell_uart_bl_cmd *bl_cmd = &uart_cmd[DELL_UART_GET_BRIGHTNESS];
263 struct dell_uart_backlight *dell_pdata = bl_get_data(bd);
264 struct uart_8250_port *uart = serial8250_get_port(dell_pdata->line);
265 int rx_len, brightness = 0;
266
267 dell_uart_dump_cmd(__func__, "tx: ", bl_cmd->cmd, bl_cmd->tx_len);
268
269 if (mutex_lock_killable(&dell_pdata->brightness_mutex) < 0) {
270 pr_debug("Failed to get mutex_lock");
271 return 0;
272 }
273
274 dell_uart_write(uart, bl_cmd->cmd, bl_cmd->tx_len);
275 rx_len = dell_uart_read(uart, bl_cmd->ret, bl_cmd->rx_len);
276
277 mutex_unlock(&dell_pdata->brightness_mutex);
278
279 dell_uart_dump_cmd(__func__, "rx: ", bl_cmd->ret, rx_len);
280
281 brightness = (unsigned int)bl_cmd->ret[2];
282
283 return brightness;
284}
285
286static int dell_uart_update_status(struct backlight_device *bd)
287{
288 struct dell_uart_bl_cmd *bl_cmd = &uart_cmd[DELL_UART_SET_BRIGHTNESS];
289 struct dell_uart_backlight *dell_pdata = bl_get_data(bd);
290 struct uart_8250_port *uart = serial8250_get_port(dell_pdata->line);
291 int rx_len;
292
293 bl_cmd->cmd[2] = bd->props.brightness;
294 bl_cmd->cmd[3] = dell_uart_checksum(bl_cmd->cmd, bl_cmd->tx_len - 1);
295
296 dell_uart_dump_cmd(__func__, "tx: ", bl_cmd->cmd, bl_cmd->tx_len);
297
298 if (mutex_lock_killable(&dell_pdata->brightness_mutex) < 0) {
299 pr_debug("Failed to get mutex_lock");
300 return 0;
301 }
302
303 dell_uart_write(uart, bl_cmd->cmd, bl_cmd->tx_len);
304 rx_len = dell_uart_read(uart, bl_cmd->ret, bl_cmd->rx_len);
305
306 mutex_unlock(&dell_pdata->brightness_mutex);
307
308 dell_uart_dump_cmd(__func__, "rx: ", bl_cmd->ret, rx_len);
309
310 if (bd->props.power != dell_uart_get_bl_power(dell_pdata))
311 dell_uart_set_bl_power(bd, bd->props.power);
312
313 return 0;
314}
315
316static int dell_uart_get_scalar_status(struct dell_uart_backlight *dell_pdata)
317{
318 struct dell_uart_bl_cmd *bl_cmd = &uart_cmd[DELL_UART_GET_SCALAR];
319 struct uart_8250_port *uart = serial8250_get_port(dell_pdata->line);
320 int rx_len;
ed491f25 321 int status = 0, retry = 50;
2c36c90f
AK
322
323 dell_uart_dump_cmd(__func__, "tx: ", bl_cmd->cmd, bl_cmd->tx_len);
324
325 if (mutex_lock_killable(&dell_pdata->brightness_mutex) < 0) {
326 pr_debug("Failed to get mutex_lock");
327 return 0;
328 }
329
330 dell_uart_write(uart, bl_cmd->cmd, bl_cmd->tx_len);
7ea80b62
AK
331 do {
332 rx_len = dell_uart_read(uart, bl_cmd->ret, bl_cmd->rx_len);
333 if (rx_len == 0)
334 msleep(100);
335 } while (rx_len == 0 && --retry);
2c36c90f
AK
336
337 mutex_unlock(&dell_pdata->brightness_mutex);
338
339 dell_uart_dump_cmd(__func__, "rx: ", bl_cmd->ret, rx_len);
340
341 if (rx_len == 4)
342 status = (unsigned int)bl_cmd->ret[2];
343
344 return status;
345}
346
347static int dell_uart_show_firmware_ver(struct dell_uart_backlight *dell_pdata)
348{
349 struct dell_uart_bl_cmd *bl_cmd = &uart_cmd[DELL_UART_GET_FIRMWARE_VER];
350 struct uart_8250_port *uart = serial8250_get_port(dell_pdata->line);
351 int rx_len = 0, retry = 10;
352
353 dell_uart_dump_cmd(__func__, "tx: ", bl_cmd->cmd, bl_cmd->tx_len);
354
355 if (mutex_lock_killable(&dell_pdata->brightness_mutex) < 0) {
356 pr_debug("Failed to get mutex_lock");
357 return -1;
358 }
359
360 dell_uart_write(uart, bl_cmd->cmd, bl_cmd->tx_len);
361 while (retry-- > 0) {
362 /* first byte is data length */
363 dell_uart_read(uart, bl_cmd->ret, 1);
364 rx_len = (int)bl_cmd->ret[0];
365 if (bl_cmd->ret[0] > 80 || bl_cmd->ret[0] == 0) {
366 pr_debug("Failed to get firmware version\n");
367 if (retry == 0) {
368 mutex_unlock(&dell_pdata->brightness_mutex);
369 return -1;
370 }
371 msleep(100);
372 continue;
373 }
374
375 dell_uart_read(uart, bl_cmd->ret+1, rx_len-1);
376 break;
377 }
378 mutex_unlock(&dell_pdata->brightness_mutex);
379
380 dell_uart_dump_cmd(__func__, "rx: ", bl_cmd->ret, rx_len);
381
382 pr_debug("Firmare str(%d)= %s\n", (int)bl_cmd->ret[0], bl_cmd->ret+2);
383 return rx_len;
384}
385
386static const struct backlight_ops dell_uart_backlight_ops = {
387 .get_brightness = dell_uart_get_brightness,
388 .update_status = dell_uart_update_status,
389};
390
391static int dell_uart_startup(struct dell_uart_backlight *dell_pdata)
392{
393 struct uart_8250_port *uartp;
394 struct uart_port *port;
395
396 dell_pdata->line = 0;
397 uartp = serial8250_get_port(dell_pdata->line);
398 port = &uartp->port;
399 tty = port->state->port.tty;
400 io_serial_in = port->serial_in;
401 uart_write = tty->driver->ops->write;
402 uart_chars_in_buffer = tty->driver->ops->chars_in_buffer;
403
404 return 0;
405}
406
407static int dell_uart_bl_add(struct acpi_device *dev)
408{
409 struct dell_uart_backlight *dell_pdata;
410 struct backlight_properties props;
411 struct backlight_device *dell_uart_bd;
412
413 dell_pdata = kzalloc(sizeof(struct dell_uart_backlight), GFP_KERNEL);
414 if (!dell_pdata) {
415 pr_debug("Failed to allocate memory for dell_uart_backlight\n");
695f4adb 416 return -ENOMEM;
2c36c90f
AK
417 }
418 dell_pdata->dev = &dev->dev;
419 dell_uart_startup(dell_pdata);
420 dev->driver_data = dell_pdata;
421
422 mutex_init(&dell_pdata->brightness_mutex);
423
695f4adb
AK
424 if (!force) {
425 if (dmi_check_system(dell_uart_backlight_alpha_platform)) {
426 /* try another command to make sure there is no scalar IC */
427 if (dell_uart_show_firmware_ver(dell_pdata) <= 4) {
428 pr_debug("Scalar is not in charge of brightness adjustment.\n");
429 kzfree(dell_pdata);
430 return -ENODEV;
431 }
432 }
433 else if (!dell_uart_get_scalar_status(dell_pdata)) {
434 pr_debug("Scalar is not in charge of brightness adjustment.\n");
435 kzfree(dell_pdata);
436 return -ENODEV;
437 }
2c36c90f 438 }
23552db7 439 dell_uart_show_firmware_ver(dell_pdata);
2c36c90f
AK
440
441 memset(&props, 0, sizeof(struct backlight_properties));
442 props.type = BACKLIGHT_PLATFORM;
443 props.max_brightness = 100;
444
445 dell_uart_bd = backlight_device_register("dell_uart_backlight",
446 &dev->dev,
447 dell_pdata,
448 &dell_uart_backlight_ops,
449 &props);
450 if (IS_ERR(dell_uart_bd)) {
451 kzfree(dell_pdata);
452 pr_debug("Backlight registration failed\n");
695f4adb 453 return PTR_ERR(dell_uart_bd);
2c36c90f
AK
454 }
455
456 dell_pdata->dell_uart_bd = dell_uart_bd;
457
458 dell_uart_set_bl_power(dell_uart_bd, FB_BLANK_UNBLANK);
459 dell_uart_bd->props.brightness = 100;
460 backlight_update_status(dell_uart_bd);
461
462 /* unregister acpi backlight interface */
463 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
464
465 return 0;
466}
467
468static int dell_uart_bl_remove(struct acpi_device *dev)
469{
470 struct dell_uart_backlight *dell_pdata = dev->driver_data;
471
472 backlight_device_unregister(dell_pdata->dell_uart_bd);
473 kzfree(dell_pdata);
474
475 return 0;
476}
477
478static int dell_uart_bl_suspend(struct device *dev)
479{
480 filp_close(ftty, NULL);
481 return 0;
482}
483
484static int dell_uart_bl_resume(struct device *dev)
485{
486 ftty = filp_open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY, 0);
487 return 0;
488}
489
490static SIMPLE_DEV_PM_OPS(dell_uart_bl_pm, dell_uart_bl_suspend, dell_uart_bl_resume);
491
492static const struct acpi_device_id dell_uart_bl_ids[] = {
493 {"DELL0501", 0},
494 {"", 0},
495};
496
497static struct acpi_driver dell_uart_backlight_driver = {
498 .name = "Dell AIO serial backlight",
499 .ids = dell_uart_bl_ids,
500 .ops = {
501 .add = dell_uart_bl_add,
502 .remove = dell_uart_bl_remove,
503 },
504 .drv.pm = &dell_uart_bl_pm,
505};
506
507static int __init dell_uart_bl_init(void)
508{
509 ftty = filp_open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY, 0);
510 if (IS_ERR(ftty)) {
511 pr_debug("cannot open /dev/ttyS0\n");
512 return -EINVAL;
513 }
514
515 return acpi_bus_register_driver(&dell_uart_backlight_driver);
516}
517
518static void __exit dell_uart_bl_exit(void)
519{
520 filp_close(ftty, NULL);
521
522 acpi_bus_unregister_driver(&dell_uart_backlight_driver);
523}
524
525module_init(dell_uart_bl_init);
526module_exit(dell_uart_bl_exit);
527MODULE_DEVICE_TABLE(acpi, dell_uart_bl_ids);
528MODULE_DESCRIPTION("Dell AIO Serial Backlight module");
529MODULE_AUTHOR("AceLan Kao <acelan.kao@canonical.com>");
530MODULE_LICENSE("GPL");