1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Cobalt/SEAD3 LCD frame buffer driver.
5 * Copyright (C) 2008 Yoichi Yuasa <yuasa@linux-mips.org>
6 * Copyright (C) 2012 MIPS Technologies, Inc.
8 #include <linux/delay.h>
10 #include <linux/init.h>
12 #include <linux/ioport.h>
13 #include <linux/uaccess.h>
14 #include <linux/platform_device.h>
15 #include <linux/module.h>
16 #include <linux/sched/signal.h>
19 * Cursor position address
21 * Y+----+----+----+---+----+----+
22 * 0|0x00|0x01|0x02|...|0x0e|0x0f|
23 * +----+----+----+---+----+----+
24 * 1|0x40|0x41|0x42|...|0x4e|0x4f|
25 * +----+----+----+---+----+----+
27 #define LCD_DATA_REG_OFFSET 0x10
28 #define LCD_XRES_MAX 16
29 #define LCD_YRES_MAX 2
30 #define LCD_CHARS_MAX 32
32 #define LCD_CLEAR 0x01
33 #define LCD_CURSOR_MOVE_HOME 0x02
34 #define LCD_RESET 0x06
36 #define LCD_CURSOR_OFF 0x0c
37 #define LCD_CURSOR_BLINK_OFF 0x0e
38 #define LCD_CURSOR_ON 0x0f
39 #define LCD_ON LCD_CURSOR_ON
40 #define LCD_CURSOR_MOVE_LEFT 0x10
41 #define LCD_CURSOR_MOVE_RIGHT 0x14
42 #define LCD_DISPLAY_LEFT 0x18
43 #define LCD_DISPLAY_RIGHT 0x1c
44 #define LCD_PRERESET 0x3f /* execute 4 times continuously */
47 #define LCD_GRAPHIC_MODE 0x40
48 #define LCD_TEXT_MODE 0x80
49 #define LCD_CUR_POS_MASK 0x7f
51 #define LCD_CUR_POS(x) ((x) & LCD_CUR_POS_MASK)
52 #define LCD_TEXT_POS(x) ((x) | LCD_TEXT_MODE)
54 static inline void lcd_write_control(struct fb_info
*info
, u8 control
)
56 writel((u32
)control
<< 24, info
->screen_base
);
59 static inline u8
lcd_read_control(struct fb_info
*info
)
61 return readl(info
->screen_base
) >> 24;
64 static inline void lcd_write_data(struct fb_info
*info
, u8 data
)
66 writel((u32
)data
<< 24, info
->screen_base
+ LCD_DATA_REG_OFFSET
);
69 static inline u8
lcd_read_data(struct fb_info
*info
)
71 return readl(info
->screen_base
+ LCD_DATA_REG_OFFSET
) >> 24;
74 static int lcd_busy_wait(struct fb_info
*info
)
77 int timeout
= 10, retval
= 0;
80 val
= lcd_read_control(info
);
85 if (msleep_interruptible(1))
97 static void lcd_clear(struct fb_info
*info
)
101 for (i
= 0; i
< 4; i
++) {
104 lcd_write_control(info
, LCD_PRERESET
);
109 lcd_write_control(info
, LCD_CLEAR
);
113 lcd_write_control(info
, LCD_RESET
);
116 static const struct fb_fix_screeninfo cobalt_lcdfb_fix
= {
118 .type
= FB_TYPE_TEXT
,
119 .type_aux
= FB_AUX_TEXT_MDA
,
120 .visual
= FB_VISUAL_MONO01
,
121 .line_length
= LCD_XRES_MAX
,
122 .accel
= FB_ACCEL_NONE
,
125 static ssize_t
cobalt_lcdfb_read(struct fb_info
*info
, char __user
*buf
,
126 size_t count
, loff_t
*ppos
)
128 char src
[LCD_CHARS_MAX
];
132 if (!info
->screen_base
)
136 if (pos
>= LCD_CHARS_MAX
|| count
== 0)
139 if (count
> LCD_CHARS_MAX
)
140 count
= LCD_CHARS_MAX
;
142 if (pos
+ count
> LCD_CHARS_MAX
)
143 count
= LCD_CHARS_MAX
- pos
;
145 for (len
= 0; len
< count
; len
++) {
146 retval
= lcd_busy_wait(info
);
150 lcd_write_control(info
, LCD_TEXT_POS(pos
));
152 retval
= lcd_busy_wait(info
);
156 src
[len
] = lcd_read_data(info
);
163 if (retval
< 0 && signal_pending(current
))
166 if (copy_to_user(buf
, src
, len
))
174 static ssize_t
cobalt_lcdfb_write(struct fb_info
*info
, const char __user
*buf
,
175 size_t count
, loff_t
*ppos
)
177 char dst
[LCD_CHARS_MAX
];
181 if (!info
->screen_base
)
185 if (pos
>= LCD_CHARS_MAX
|| count
== 0)
188 if (count
> LCD_CHARS_MAX
)
189 count
= LCD_CHARS_MAX
;
191 if (pos
+ count
> LCD_CHARS_MAX
)
192 count
= LCD_CHARS_MAX
- pos
;
194 if (copy_from_user(dst
, buf
, count
))
197 for (len
= 0; len
< count
; len
++) {
198 retval
= lcd_busy_wait(info
);
202 lcd_write_control(info
, LCD_TEXT_POS(pos
));
204 retval
= lcd_busy_wait(info
);
208 lcd_write_data(info
, dst
[len
]);
215 if (retval
< 0 && signal_pending(current
))
223 static int cobalt_lcdfb_blank(int blank_mode
, struct fb_info
*info
)
227 retval
= lcd_busy_wait(info
);
231 switch (blank_mode
) {
232 case FB_BLANK_UNBLANK
:
233 lcd_write_control(info
, LCD_ON
);
236 lcd_write_control(info
, LCD_OFF
);
243 static int cobalt_lcdfb_cursor(struct fb_info
*info
, struct fb_cursor
*cursor
)
248 switch (cursor
->set
) {
250 x
= cursor
->image
.dx
;
251 y
= cursor
->image
.dy
;
252 if (x
>= LCD_XRES_MAX
|| y
>= LCD_YRES_MAX
)
255 retval
= lcd_busy_wait(info
);
259 lcd_write_control(info
,
260 LCD_TEXT_POS(info
->fix
.line_length
* y
+ x
));
266 retval
= lcd_busy_wait(info
);
271 lcd_write_control(info
, LCD_CURSOR_ON
);
273 lcd_write_control(info
, LCD_CURSOR_OFF
);
278 static const struct fb_ops cobalt_lcd_fbops
= {
279 .owner
= THIS_MODULE
,
280 .fb_read
= cobalt_lcdfb_read
,
281 .fb_write
= cobalt_lcdfb_write
,
282 .fb_blank
= cobalt_lcdfb_blank
,
283 __FB_DEFAULT_IOMEM_OPS_DRAW
,
284 .fb_cursor
= cobalt_lcdfb_cursor
,
285 __FB_DEFAULT_IOMEM_OPS_MMAP
,
288 static int cobalt_lcdfb_probe(struct platform_device
*dev
)
290 struct fb_info
*info
;
291 struct resource
*res
;
294 info
= framebuffer_alloc(0, &dev
->dev
);
298 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
300 framebuffer_release(info
);
304 info
->screen_size
= resource_size(res
);
305 info
->screen_base
= devm_ioremap(&dev
->dev
, res
->start
,
307 if (!info
->screen_base
) {
308 framebuffer_release(info
);
312 info
->fbops
= &cobalt_lcd_fbops
;
313 info
->fix
= cobalt_lcdfb_fix
;
314 info
->fix
.smem_start
= res
->start
;
315 info
->fix
.smem_len
= info
->screen_size
;
316 info
->pseudo_palette
= NULL
;
319 retval
= register_framebuffer(info
);
321 framebuffer_release(info
);
325 platform_set_drvdata(dev
, info
);
329 fb_info(info
, "Cobalt server LCD frame buffer device\n");
334 static void cobalt_lcdfb_remove(struct platform_device
*dev
)
336 struct fb_info
*info
;
338 info
= platform_get_drvdata(dev
);
340 unregister_framebuffer(info
);
341 framebuffer_release(info
);
345 static struct platform_driver cobalt_lcdfb_driver
= {
346 .probe
= cobalt_lcdfb_probe
,
347 .remove
= cobalt_lcdfb_remove
,
349 .name
= "cobalt-lcd",
352 module_platform_driver(cobalt_lcdfb_driver
);
354 MODULE_LICENSE("GPL v2");
355 MODULE_AUTHOR("Yoichi Yuasa");
356 MODULE_DESCRIPTION("Cobalt server LCD frame buffer driver");