History

auxdisplay: charlcd: Add support for 4-bit interfaces

In 4-bit mode, 8-bit commands and data are written using two raw writes to the data interface: high nibble first, low nibble last. This must be handled by the low-level driver. However, as we don't know in which mode (4-bit or 8-bit) nor 4-bit phase the LCD was left, initialization must always be handled using raw writes, and needs to configure the LCD for 8-bit mode first.

Geert Uytterhoeven committed 9 years ago

Commit ac201471 parent 39f8ea42 files · +32 −6Browse files

Unified · Split

2 files
  1. drivers/auxdisplay/charlcd.c +30 −6
  2. include/misc/charlcd.h +2 −0
drivers/auxdisplay/charlcd.c+30−6
@@ -223,24 +223,46 @@ static void charlcd_clear_display(struct charlcd *lcd) · 222 unmodified lines
223223
224224 static int charlcd_init_display(struct charlcd *lcd)
225225 {
226+ void (*write_cmd_raw)(struct charlcd *lcd, int cmd);
226227 struct charlcd_priv *priv = to_priv(lcd);
228+ u8 init;
229+
230+ if (lcd->ifwidth != 4 && lcd->ifwidth != 8)
231+ return -EINVAL;
227232
228233 priv->flags = ((lcd->height > 1) ? LCD_FLAG_N : 0) | LCD_FLAG_D |
229234 LCD_FLAG_C | LCD_FLAG_B;
230235
231236 long_sleep(20); /* wait 20 ms after power-up for the paranoid */
232237
233− /* 8bits, 1 line, small fonts; let's do it 3 times */
234− lcd->ops->write_cmd(lcd, LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
238+ /*
239+ * 8-bit mode, 1 line, small fonts; let's do it 3 times, to make sure
240+ * the LCD is in 8-bit mode afterwards
241+ */
242+ init = LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS;
243+ if (lcd->ifwidth == 4) {
244+ init >>= 4;
245+ write_cmd_raw = lcd->ops->write_cmd_raw4;
246+ } else {
247+ write_cmd_raw = lcd->ops->write_cmd;
248+ }
249+ write_cmd_raw(lcd, init);
235250 long_sleep(10);
236− lcd->ops->write_cmd(lcd, LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
251+ write_cmd_raw(lcd, init);
237252 long_sleep(10);
238− lcd->ops->write_cmd(lcd, LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS);
253+ write_cmd_raw(lcd, init);
239254 long_sleep(10);
240255
256+ if (lcd->ifwidth == 4) {
257+ /* Switch to 4-bit mode, 1 line, small fonts */
258+ lcd->ops->write_cmd_raw4(lcd, LCD_CMD_FUNCTION_SET >> 4);
259+ long_sleep(10);
260+ }
261+
241262 /* set font height and lines number */
242263 lcd->ops->write_cmd(lcd,
243− LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS |
264+ LCD_CMD_FUNCTION_SET |
265+ ((lcd->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
244266 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
245267 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
246268 long_sleep(10);
@@ -482,7 +504,8 @@ static inline int handle_lcd_special_code(struct charlcd *lcd) · 235 unmodified lines
482504 /* check whether one of F,N flags was changed */
483505 else if ((oldflags ^ priv->flags) & (LCD_FLAG_F | LCD_FLAG_N))
484506 lcd->ops->write_cmd(lcd,
485− LCD_CMD_FUNCTION_SET | LCD_CMD_DATA_LEN_8BITS |
507+ LCD_CMD_FUNCTION_SET |
508+ ((lcd->ifwidth == 8) ? LCD_CMD_DATA_LEN_8BITS : 0) |
486509 ((priv->flags & LCD_FLAG_F) ? LCD_CMD_FONT_5X10_DOTS : 0) |
487510 ((priv->flags & LCD_FLAG_N) ? LCD_CMD_TWO_LINES : 0));
488511 /* check whether L flag was changed */
@@ -716,6 +739,7 @@ struct charlcd *charlcd_alloc(unsigned int drvdata_size) · 227 unmodified lines
716739 priv->esc_seq.len = -1;
717740
718741 lcd = &priv->lcd;
742+ lcd->ifwidth = 8;
719743 lcd->bwidth = DEFAULT_LCD_BWIDTH;
720744 lcd->hwidth = DEFAULT_LCD_HWIDTH;
721745 lcd->drvdata = priv->drvdata;
include/misc/charlcd.h+2−0
@@ -14,6 +14,7 @@ struct charlcd { · 13 unmodified lines
1414 const struct charlcd_ops *ops;
1515 const unsigned char *char_conv; /* Optional */
1616
17+ int ifwidth; /* 4-bit or 8-bit (default) */
1718 int height;
1819 int width;
1920 int bwidth; /* Default set by charlcd_alloc() */
@@ -28,6 +29,7 @@ struct charlcd_ops { · 8 unmodified lines
2829 void (*write_data)(struct charlcd *lcd, int data);
2930
3031 /* Optional */
32+ void (*write_cmd_raw4)(struct charlcd *lcd, int cmd); /* 4-bit only */
3133 void (*clear_fast)(struct charlcd *lcd);
3234 void (*backlight)(struct charlcd *lcd, int on);
3335 };