2 * Cobalt/SEAD3 LCD frame buffer driver.
4 * Copyright (C) 2008 Yoichi Yuasa <yuasa@linux-mips.org>
5 * Copyright (C) 2012 MIPS Technologies, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <linux/delay.h>
23 #include <linux/init.h>
25 #include <linux/ioport.h>
26 #include <linux/uaccess.h>
27 #include <linux/platform_device.h>
28 #include <linux/module.h>
31 * Cursor position address
33 * Y+----+----+----+---+----+----+
34 * 0|0x00|0x01|0x02|...|0x0e|0x0f|
35 * +----+----+----+---+----+----+
36 * 1|0x40|0x41|0x42|...|0x4e|0x4f|
37 * +----+----+----+---+----+----+
39 #define LCD_DATA_REG_OFFSET 0x10
40 #define LCD_XRES_MAX 16
41 #define LCD_YRES_MAX 2
42 #define LCD_CHARS_MAX 32
44 #define LCD_CLEAR 0x01
45 #define LCD_CURSOR_MOVE_HOME 0x02
46 #define LCD_RESET 0x06
48 #define LCD_CURSOR_OFF 0x0c
49 #define LCD_CURSOR_BLINK_OFF 0x0e
50 #define LCD_CURSOR_ON 0x0f
51 #define LCD_ON LCD_CURSOR_ON
52 #define LCD_CURSOR_MOVE_LEFT 0x10
53 #define LCD_CURSOR_MOVE_RIGHT 0x14
54 #define LCD_DISPLAY_LEFT 0x18
55 #define LCD_DISPLAY_RIGHT 0x1c
56 #define LCD_PRERESET 0x3f /* execute 4 times continuously */
59 #define LCD_GRAPHIC_MODE 0x40
60 #define LCD_TEXT_MODE 0x80
61 #define LCD_CUR_POS_MASK 0x7f
63 #define LCD_CUR_POS(x) ((x) & LCD_CUR_POS_MASK)
64 #define LCD_TEXT_POS(x) ((x) | LCD_TEXT_MODE)
66 static inline void lcd_write_control(struct fb_info
*info
, u8 control
)
68 writel((u32
)control
<< 24, info
->screen_base
);
71 static inline u8
lcd_read_control(struct fb_info
*info
)
73 return readl(info
->screen_base
) >> 24;
76 static inline void lcd_write_data(struct fb_info
*info
, u8 data
)
78 writel((u32
)data
<< 24, info
->screen_base
+ LCD_DATA_REG_OFFSET
);
81 static inline u8
lcd_read_data(struct fb_info
*info
)
83 return readl(info
->screen_base
+ LCD_DATA_REG_OFFSET
) >> 24;
86 static int lcd_busy_wait(struct fb_info
*info
)
89 int timeout
= 10, retval
= 0;
92 val
= lcd_read_control(info
);
97 if (msleep_interruptible(1))
109 static void lcd_clear(struct fb_info
*info
)
113 for (i
= 0; i
< 4; i
++) {
116 lcd_write_control(info
, LCD_PRERESET
);
121 lcd_write_control(info
, LCD_CLEAR
);
125 lcd_write_control(info
, LCD_RESET
);
128 static struct fb_fix_screeninfo cobalt_lcdfb_fix
= {
130 .type
= FB_TYPE_TEXT
,
131 .type_aux
= FB_AUX_TEXT_MDA
,
132 .visual
= FB_VISUAL_MONO01
,
133 .line_length
= LCD_XRES_MAX
,
134 .accel
= FB_ACCEL_NONE
,
137 static ssize_t
cobalt_lcdfb_read(struct fb_info
*info
, char __user
*buf
,
138 size_t count
, loff_t
*ppos
)
140 char src
[LCD_CHARS_MAX
];
145 if (pos
>= LCD_CHARS_MAX
|| count
== 0)
148 if (count
> LCD_CHARS_MAX
)
149 count
= LCD_CHARS_MAX
;
151 if (pos
+ count
> LCD_CHARS_MAX
)
152 count
= LCD_CHARS_MAX
- pos
;
154 for (len
= 0; len
< count
; len
++) {
155 retval
= lcd_busy_wait(info
);
159 lcd_write_control(info
, LCD_TEXT_POS(pos
));
161 retval
= lcd_busy_wait(info
);
165 src
[len
] = lcd_read_data(info
);
172 if (retval
< 0 && signal_pending(current
))
175 if (copy_to_user(buf
, src
, len
))
183 static ssize_t
cobalt_lcdfb_write(struct fb_info
*info
, const char __user
*buf
,
184 size_t count
, loff_t
*ppos
)
186 char dst
[LCD_CHARS_MAX
];
191 if (pos
>= LCD_CHARS_MAX
|| count
== 0)
194 if (count
> LCD_CHARS_MAX
)
195 count
= LCD_CHARS_MAX
;
197 if (pos
+ count
> LCD_CHARS_MAX
)
198 count
= LCD_CHARS_MAX
- pos
;
200 if (copy_from_user(dst
, buf
, count
))
203 for (len
= 0; len
< count
; len
++) {
204 retval
= lcd_busy_wait(info
);
208 lcd_write_control(info
, LCD_TEXT_POS(pos
));
210 retval
= lcd_busy_wait(info
);
214 lcd_write_data(info
, dst
[len
]);
221 if (retval
< 0 && signal_pending(current
))
229 static int cobalt_lcdfb_blank(int blank_mode
, struct fb_info
*info
)
233 retval
= lcd_busy_wait(info
);
237 switch (blank_mode
) {
238 case FB_BLANK_UNBLANK
:
239 lcd_write_control(info
, LCD_ON
);
242 lcd_write_control(info
, LCD_OFF
);
249 static int cobalt_lcdfb_cursor(struct fb_info
*info
, struct fb_cursor
*cursor
)
254 switch (cursor
->set
) {
256 x
= cursor
->image
.dx
;
257 y
= cursor
->image
.dy
;
258 if (x
>= LCD_XRES_MAX
|| y
>= LCD_YRES_MAX
)
261 retval
= lcd_busy_wait(info
);
265 lcd_write_control(info
,
266 LCD_TEXT_POS(info
->fix
.line_length
* y
+ x
));
272 retval
= lcd_busy_wait(info
);
277 lcd_write_control(info
, LCD_CURSOR_ON
);
279 lcd_write_control(info
, LCD_CURSOR_OFF
);
284 static struct fb_ops cobalt_lcd_fbops
= {
285 .owner
= THIS_MODULE
,
286 .fb_read
= cobalt_lcdfb_read
,
287 .fb_write
= cobalt_lcdfb_write
,
288 .fb_blank
= cobalt_lcdfb_blank
,
289 .fb_cursor
= cobalt_lcdfb_cursor
,
292 static int cobalt_lcdfb_probe(struct platform_device
*dev
)
294 struct fb_info
*info
;
295 struct resource
*res
;
298 info
= framebuffer_alloc(0, &dev
->dev
);
302 res
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
304 framebuffer_release(info
);
308 info
->screen_size
= resource_size(res
);
309 info
->screen_base
= devm_ioremap(&dev
->dev
, res
->start
,
311 if (!info
->screen_base
) {
312 framebuffer_release(info
);
316 info
->fbops
= &cobalt_lcd_fbops
;
317 info
->fix
= cobalt_lcdfb_fix
;
318 info
->fix
.smem_start
= res
->start
;
319 info
->fix
.smem_len
= info
->screen_size
;
320 info
->pseudo_palette
= NULL
;
322 info
->flags
= FBINFO_DEFAULT
;
324 retval
= register_framebuffer(info
);
326 framebuffer_release(info
);
330 platform_set_drvdata(dev
, info
);
334 fb_info(info
, "Cobalt server LCD frame buffer device\n");
339 static int cobalt_lcdfb_remove(struct platform_device
*dev
)
341 struct fb_info
*info
;
343 info
= platform_get_drvdata(dev
);
345 unregister_framebuffer(info
);
346 framebuffer_release(info
);
352 static struct platform_driver cobalt_lcdfb_driver
= {
353 .probe
= cobalt_lcdfb_probe
,
354 .remove
= cobalt_lcdfb_remove
,
356 .name
= "cobalt-lcd",
359 module_platform_driver(cobalt_lcdfb_driver
);
361 MODULE_LICENSE("GPL v2");
362 MODULE_AUTHOR("Yoichi Yuasa");
363 MODULE_DESCRIPTION("Cobalt server LCD frame buffer driver");