auxdisplay: hd44780_common_print
authorLars Poeschel <poeschel@lemonage.de>
Tue, 3 Nov 2020 09:58:11 +0000 (10:58 +0100)
committerMiguel Ojeda <ojeda@kernel.org>
Wed, 4 Nov 2020 10:04:03 +0000 (11:04 +0100)
We create a hd44780_common_print function. It is derived from the
original charlcd_print. charlcd_print becomes a device independent print
function, that then only calls via its ops function pointers, into the
print function offered by drivers.

Reported-by: kernel test robot <lkp@intel.com>
Reviewed-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Lars Poeschel <poeschel@lemonage.de>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
drivers/auxdisplay/charlcd.c
drivers/auxdisplay/charlcd.h
drivers/auxdisplay/hd44780.c
drivers/auxdisplay/hd44780_common.c
drivers/auxdisplay/hd44780_common.h

index 1b37d4b..72ed004 100644 (file)
@@ -167,18 +167,15 @@ static void charlcd_home(struct charlcd *lcd)
 
 static void charlcd_print(struct charlcd *lcd, char c)
 {
-       struct hd44780_common *hdc = lcd->drvdata;
+       if (lcd->char_conv)
+               c = lcd->char_conv[(unsigned char)c];
 
-       if (lcd->addr.x < hdc->bwidth) {
-               if (lcd->char_conv)
-                       c = lcd->char_conv[(unsigned char)c];
-               hdc->write_data(hdc, c);
+       if (!lcd->ops->print(lcd, c))
                lcd->addr.x++;
 
-               /* prevents the cursor from wrapping onto the next line */
-               if (lcd->addr.x == hdc->bwidth)
-                       charlcd_gotoxy(lcd);
-       }
+       /* prevents the cursor from wrapping onto the next line */
+       if (lcd->addr.x == lcd->width)
+               charlcd_gotoxy(lcd);
 }
 
 static void charlcd_clear_fast(struct charlcd *lcd)
@@ -192,7 +189,7 @@ static void charlcd_clear_fast(struct charlcd *lcd)
                lcd->ops->clear_fast(lcd);
        else
                for (pos = 0; pos < min(2, lcd->height) * hdc->hwidth; pos++)
-                       hdc->write_data(hdc, ' ');
+                       lcd->ops->print(lcd, ' ');
 
        charlcd_home(lcd);
 }
@@ -433,12 +430,16 @@ static inline int handle_lcd_special_code(struct charlcd *lcd)
                processed = 1;
                break;
        case 'k': {     /* kill end of line */
-               int x;
+               int x, xs, ys;
 
+               xs = lcd->addr.x;
+               ys = lcd->addr.y;
                for (x = lcd->addr.x; x < hdc->bwidth; x++)
-                       hdc->write_data(hdc, ' ');
+                       lcd->ops->print(lcd, ' ');
 
                /* restore cursor position */
+               lcd->addr.x = xs;
+               lcd->addr.y = ys;
                charlcd_gotoxy(lcd);
                processed = 1;
                break;
@@ -591,7 +592,8 @@ static void charlcd_write_char(struct charlcd *lcd, char c)
                         * go to the beginning of the next line
                         */
                        for (; lcd->addr.x < hdc->bwidth; lcd->addr.x++)
-                               hdc->write_data(hdc, ' ');
+                               lcd->ops->print(lcd, ' ');
+
                        lcd->addr.x = 0;
                        lcd->addr.y = (lcd->addr.y + 1) % lcd->height;
                        charlcd_gotoxy(lcd);
index ff4896a..94922e3 100644 (file)
@@ -30,9 +30,21 @@ struct charlcd {
        void *drvdata;
 };
 
+/**
+ * struct charlcd_ops - Functions used by charlcd. Drivers have to implement
+ * these.
+ * @clear_fast: Clear the whole display and set cursor to position 0, 0.
+ * Optional.
+ * @backlight: Turn backlight on or off. Optional.
+ * @print: Print one character to the display at current cursor position.
+ * The cursor is advanced by charlcd.
+ * The buffered cursor position is advanced by charlcd. The cursor should not
+ * wrap to the next line at the end of a line.
+ */
 struct charlcd_ops {
        void (*clear_fast)(struct charlcd *lcd);
        void (*backlight)(struct charlcd *lcd, enum charlcd_onoff on);
+       int (*print)(struct charlcd *lcd, int c);
 };
 
 struct charlcd *charlcd_alloc(void);
index dc47385..9680bb6 100644 (file)
@@ -126,6 +126,7 @@ static void hd44780_write_data_gpio8(struct hd44780_common *hdc, int data)
 
 static const struct charlcd_ops hd44780_ops_gpio8 = {
        .backlight      = hd44780_backlight,
+       .print          = hd44780_common_print,
 };
 
 /* Send a command to the LCD panel in 4 bit GPIO mode */
@@ -169,6 +170,7 @@ static void hd44780_write_data_gpio4(struct hd44780_common *hdc, int data)
 
 static const struct charlcd_ops hd44780_ops_gpio4 = {
        .backlight      = hd44780_backlight,
+       .print          = hd44780_common_print,
 };
 
 static int hd44780_probe(struct platform_device *pdev)
index 4968461..1dfcb2c 100644 (file)
@@ -2,8 +2,22 @@
 #include <linux/module.h>
 #include <linux/slab.h>
 
+#include "charlcd.h"
 #include "hd44780_common.h"
 
+int hd44780_common_print(struct charlcd *lcd, int c)
+{
+       struct hd44780_common *hdc = lcd->drvdata;
+
+       if (lcd->addr.x < hdc->bwidth) {
+               hdc->write_data(hdc, c);
+               return 0;
+       }
+
+       return 1;
+}
+EXPORT_SYMBOL_GPL(hd44780_common_print);
+
 struct hd44780_common *hd44780_common_alloc(void)
 {
        struct hd44780_common *hd;
index 73dd4c7..ac404a4 100644 (file)
@@ -14,4 +14,5 @@ struct hd44780_common {
        void *hd44780;
 };
 
+int hd44780_common_print(struct charlcd *lcd, int c);
 struct hd44780_common *hd44780_common_alloc(void);