command: Remove the cmd_tbl_t typedef
[oweals/u-boot.git] / board / work-microwave / work_92105 / work_92105_display.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * work_92105 display support
4  *
5  * (C) Copyright 2014  DENX Software Engineering GmbH
6  * Written-by: Albert ARIBAUD <albert.aribaud@3adev.fr>
7  *
8  * The work_92105 display is a HD44780-compatible module
9  * controlled through a MAX6957AAX SPI port expander, two
10  * MAX518 I2C DACs and native LPC32xx GPO 15.
11  */
12
13 #include <common.h>
14 #include <command.h>
15 #include <asm/arch/sys_proto.h>
16 #include <asm/arch/cpu.h>
17 #include <asm/arch/emc.h>
18 #include <asm/gpio.h>
19 #include <env.h>
20 #include <spi.h>
21 #include <i2c.h>
22 #include <version.h>
23 #include <vsprintf.h>
24
25 /*
26  * GPO 15 in port 3 is gpio 3*32+15 = 111
27  */
28
29 #define GPO_15 111
30
31 /**
32  * MAX6957AAX registers that we will be using
33  */
34
35 #define MAX6957_CONF            0x04
36
37 #define MAX6957_CONF_08_11      0x0A
38 #define MAX6957_CONF_12_15      0x0B
39 #define MAX6957_CONF_16_19      0x0C
40
41 /**
42  * Individual gpio ports (one per gpio) to HD44780
43  */
44
45 #define MAX6957AAX_HD44780_RS   0x29
46 #define MAX6957AAX_HD44780_R_W  0x2A
47 #define MAX6957AAX_HD44780_EN   0x2B
48 #define MAX6957AAX_HD44780_DATA 0x4C
49
50 /**
51  * Display controller instructions
52  */
53
54 /* Function set: eight bits, two lines, 8-dot font */
55 #define HD44780_FUNCTION_SET            0x38
56
57 /* Display ON / OFF: turn display on */
58 #define HD44780_DISPLAY_ON_OFF_CONTROL  0x0C
59
60 /* Entry mode: increment */
61 #define HD44780_ENTRY_MODE_SET          0x06
62
63 /* Clear */
64 #define HD44780_CLEAR_DISPLAY           0x01
65
66 /* Set DDRAM addr (to be ORed with exact address) */
67 #define HD44780_SET_DDRAM_ADDR          0x80
68
69 /* Set CGRAM addr (to be ORed with exact address) */
70 #define HD44780_SET_CGRAM_ADDR          0x40
71
72 /**
73  * Default value for contrats
74  */
75
76 #define CONTRAST_DEFAULT  25
77
78 /**
79  * Define slave as a module-wide local to save passing it around,
80  * plus we will need it after init for the "hd44780" command.
81  */
82
83 static struct spi_slave *slave;
84
85 /*
86  * Write a value into a MAX6957AAX register.
87  */
88
89 static void max6957aax_write(uint8_t reg, uint8_t value)
90 {
91         uint8_t dout[2];
92
93         dout[0] = reg;
94         dout[1] = value;
95         gpio_set_value(GPO_15, 0);
96         /* do SPI read/write (passing din==dout is OK) */
97         spi_xfer(slave, 16, dout, dout, SPI_XFER_BEGIN | SPI_XFER_END);
98         gpio_set_value(GPO_15, 1);
99 }
100
101 /*
102  * Read a value from a MAX6957AAX register.
103  *
104  * According to the MAX6957AAX datasheet, we should release the chip
105  * select halfway through the read sequence, when the actual register
106  * value is read; but the WORK_92105 hardware prevents the MAX6957AAX
107  * SPI OUT from reaching the LPC32XX SIP MISO if chip is not selected.
108  * so let's release the CS an hold it again while reading the result.
109  */
110
111 static uint8_t max6957aax_read(uint8_t reg)
112 {
113         uint8_t dout[2], din[2];
114
115         /* send read command */
116         dout[0] = reg | 0x80; /* set bit 7 to indicate read */
117         dout[1] = 0;
118         gpio_set_value(GPO_15, 0);
119         /* do SPI read/write (passing din==dout is OK) */
120         spi_xfer(slave, 16, dout, dout, SPI_XFER_BEGIN | SPI_XFER_END);
121         /* latch read command */
122         gpio_set_value(GPO_15, 1);
123         /* read register -- din = noop on xmit, din[1] = reg on recv */
124         din[0] = 0;
125         din[1] = 0;
126         gpio_set_value(GPO_15, 0);
127         /* do SPI read/write (passing din==dout is OK) */
128         spi_xfer(slave, 16, din, din, SPI_XFER_BEGIN | SPI_XFER_END);
129         /* end of read. */
130         gpio_set_value(GPO_15, 1);
131         return din[1];
132 }
133
134 static void hd44780_instruction(unsigned long instruction)
135 {
136         max6957aax_write(MAX6957AAX_HD44780_RS, 0);
137         max6957aax_write(MAX6957AAX_HD44780_R_W, 0);
138         max6957aax_write(MAX6957AAX_HD44780_EN, 1);
139         max6957aax_write(MAX6957AAX_HD44780_DATA, instruction);
140         max6957aax_write(MAX6957AAX_HD44780_EN, 0);
141         /* HD44780 takes 37 us for most instructions, 1520 for clear */
142         if (instruction == HD44780_CLEAR_DISPLAY)
143                 udelay(2000);
144         else
145                 udelay(100);
146 }
147
148 static void hd44780_write_char(char c)
149 {
150         max6957aax_write(MAX6957AAX_HD44780_RS, 1);
151         max6957aax_write(MAX6957AAX_HD44780_R_W, 0);
152         max6957aax_write(MAX6957AAX_HD44780_EN, 1);
153         max6957aax_write(MAX6957AAX_HD44780_DATA, c);
154         max6957aax_write(MAX6957AAX_HD44780_EN, 0);
155         /* HD44780 takes 37 us to write to DDRAM or CGRAM */
156         udelay(100);
157 }
158
159 static void hd44780_write_str(char *s)
160 {
161         max6957aax_write(MAX6957AAX_HD44780_RS, 1);
162         max6957aax_write(MAX6957AAX_HD44780_R_W, 0);
163         while (*s) {
164                 max6957aax_write(MAX6957AAX_HD44780_EN, 1);
165                 max6957aax_write(MAX6957AAX_HD44780_DATA, *s);
166                 max6957aax_write(MAX6957AAX_HD44780_EN, 0);
167                 s++;
168                 /* HD44780 takes 37 us to write to DDRAM or CGRAM */
169                 udelay(100);
170         }
171 }
172
173 /*
174  * Existing user code might expect these custom characters to be
175  * recognized and displayed on the LCD
176  */
177
178 static u8 char_gen_chars[] = {
179         /* #8, empty rectangle */
180         0x1F, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1F,
181         /* #9, filled right arrow */
182         0x10, 0x18, 0x1C, 0x1E, 0x1C, 0x18, 0x10, 0x00,
183         /* #10, filled left arrow */
184         0x01, 0x03, 0x07, 0x0F, 0x07, 0x03, 0x01, 0x00,
185         /* #11, up and down arrow */
186         0x04, 0x0E, 0x1F, 0x00, 0x00, 0x1F, 0x0E, 0x04,
187         /* #12, plus/minus */
188         0x04, 0x04, 0x1F, 0x04, 0x04, 0x00, 0x1F, 0x00,
189         /* #13, fat exclamation mark */
190         0x06, 0x06, 0x06, 0x06, 0x00, 0x06, 0x06, 0x00,
191         /* #14, empty square */
192         0x00, 0x1F, 0x11, 0x11, 0x11, 0x1F, 0x00, 0x00,
193         /* #15, struck out square */
194         0x00, 0x1F, 0x19, 0x15, 0x13, 0x1F, 0x00, 0x00,
195 };
196
197 static void hd44780_init_char_gen(void)
198 {
199         int i;
200
201         hd44780_instruction(HD44780_SET_CGRAM_ADDR);
202
203         for (i = 0; i < sizeof(char_gen_chars); i++)
204                 hd44780_write_char(char_gen_chars[i]);
205
206         hd44780_instruction(HD44780_SET_DDRAM_ADDR);
207 }
208
209 void work_92105_display_init(void)
210 {
211         int claim_err;
212         char *display_contrast_str;
213         uint8_t display_contrast = CONTRAST_DEFAULT;
214         uint8_t enable_backlight = 0x96;
215
216         slave = spi_setup_slave(0, 0, 500000, 0);
217
218         if (!slave) {
219                 printf("Failed to set up SPI slave\n");
220                 return;
221         }
222
223         claim_err = spi_claim_bus(slave);
224
225         if (claim_err)
226                 debug("Failed to claim SPI bus: %d\n", claim_err);
227
228         /* enable backlight */
229         i2c_write(0x2c, 0x01, 1, &enable_backlight, 1);
230
231         /* set display contrast */
232         display_contrast_str = env_get("fwopt_dispcontrast");
233         if (display_contrast_str)
234                 display_contrast = simple_strtoul(display_contrast_str,
235                         NULL, 10);
236         i2c_write(0x2c, 0x00, 1, &display_contrast, 1);
237
238         /* request GPO_15 as an output initially set to 1 */
239         gpio_request(GPO_15, "MAX6957_nCS");
240         gpio_direction_output(GPO_15, 1);
241
242         /* enable MAX6957 portexpander */
243         max6957aax_write(MAX6957_CONF, 0x01);
244         /* configure pin 8 as input, pins 9..19 as outputs */
245         max6957aax_write(MAX6957_CONF_08_11, 0x56);
246         max6957aax_write(MAX6957_CONF_12_15, 0x55);
247         max6957aax_write(MAX6957_CONF_16_19, 0x55);
248
249         /* initialize HD44780 */
250         max6957aax_write(MAX6957AAX_HD44780_EN, 0);
251         hd44780_instruction(HD44780_FUNCTION_SET);
252         hd44780_instruction(HD44780_DISPLAY_ON_OFF_CONTROL);
253         hd44780_instruction(HD44780_ENTRY_MODE_SET);
254
255         /* write custom character glyphs */
256         hd44780_init_char_gen();
257
258         /* Show U-Boot version, date and time as a sign-of-life */
259         hd44780_instruction(HD44780_CLEAR_DISPLAY);
260         hd44780_instruction(HD44780_SET_DDRAM_ADDR | 0);
261         hd44780_write_str(U_BOOT_VERSION);
262         hd44780_instruction(HD44780_SET_DDRAM_ADDR | 64);
263         hd44780_write_str(U_BOOT_DATE);
264         hd44780_instruction(HD44780_SET_DDRAM_ADDR | 64 | 20);
265         hd44780_write_str(U_BOOT_TIME);
266 }
267
268 #ifdef CONFIG_CMD_MAX6957
269
270 static int do_max6957aax(struct cmd_tbl *cmdtp, int flag, int argc,
271                          char *const argv[])
272 {
273         int reg, val;
274
275         if (argc != 3)
276                 return CMD_RET_USAGE;
277         switch (argv[1][0]) {
278         case 'r':
279         case 'R':
280                 reg = simple_strtoul(argv[2], NULL, 0);
281                 val = max6957aax_read(reg);
282                 printf("MAX6957 reg 0x%02x read 0x%02x\n", reg, val);
283                 return 0;
284         default:
285                 reg = simple_strtoul(argv[1], NULL, 0);
286                 val = simple_strtoul(argv[2], NULL, 0);
287                 max6957aax_write(reg, val);
288                 printf("MAX6957 reg 0x%02x wrote 0x%02x\n", reg, val);
289                 return 0;
290         }
291         return 1;
292 }
293
294 #ifdef CONFIG_SYS_LONGHELP
295 static char max6957aax_help_text[] =
296         "max6957aax - write or read display register:\n"
297                 "\tmax6957aax R|r reg - read display register;\n"
298                 "\tmax6957aax reg val - write display register.";
299 #endif
300
301 U_BOOT_CMD(
302         max6957aax, 6, 1, do_max6957aax,
303         "SPI MAX6957 display write/read",
304         max6957aax_help_text
305 );
306 #endif /* CONFIG_CMD_MAX6957 */
307
308 #ifdef CONFIG_CMD_HD44760
309
310 /*
311  * We need the HUSH parser because we need string arguments, and
312  * only HUSH can understand them.
313  */
314
315 #if !defined(CONFIG_HUSH_PARSER)
316 #error CONFIG_CMD_HD44760 requires CONFIG_HUSH_PARSER
317 #endif
318
319 static int do_hd44780(struct cmd_tbl *cmdtp, int flag, int argc,
320                       char *const argv[])
321 {
322         char *cmd;
323
324         if (argc != 3)
325                 return CMD_RET_USAGE;
326
327         cmd = argv[1];
328
329         if (strcasecmp(cmd, "cmd") == 0)
330                 hd44780_instruction(simple_strtol(argv[2], NULL, 0));
331         else if (strcasecmp(cmd, "data") == 0)
332                 hd44780_write_char(simple_strtol(argv[2], NULL, 0));
333         else if (strcasecmp(cmd, "str") == 0)
334                 hd44780_write_str(argv[2]);
335         return 0;
336 }
337
338 #ifdef CONFIG_SYS_LONGHELP
339 static char hd44780_help_text[] =
340         "hd44780 - control LCD driver:\n"
341                 "\thd44780 cmd <val> - send command <val> to driver;\n"
342                 "\thd44780 data <val> - send data <val> to driver;\n"
343                 "\thd44780 str \"<text>\" - send \"<text>\" to driver.";
344 #endif
345
346 U_BOOT_CMD(
347         hd44780, 6, 1, do_hd44780,
348         "HD44780 LCD driver control",
349         hd44780_help_text
350 );
351 #endif /* CONFIG_CMD_HD44760 */