| Yoichi Yuasa | 5abe3b4 | 2008-07-23 21:31:40 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Cobalt server LCD frame buffer driver. | 
|  | 3 | * | 
|  | 4 | *  Copyright (C) 2008  Yoichi Yuasa <yoichi_yuasa@tripeaks.co.jp> | 
|  | 5 | * | 
|  | 6 | *  This program is free software; you can redistribute it and/or modify | 
|  | 7 | *  it under the terms of the GNU General Public License as published by | 
|  | 8 | *  the Free Software Foundation; either version 2 of the License, or | 
|  | 9 | *  (at your option) any later version. | 
|  | 10 | * | 
|  | 11 | *  This program is distributed in the hope that it will be useful, | 
|  | 12 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 13 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 14 | *  GNU General Public License for more details. | 
|  | 15 | * | 
|  | 16 | *  You should have received a copy of the GNU General Public License | 
|  | 17 | *  along with this program; if not, write to the Free Software | 
|  | 18 | *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 
|  | 19 | */ | 
|  | 20 | #include <linux/delay.h> | 
|  | 21 | #include <linux/fb.h> | 
|  | 22 | #include <linux/init.h> | 
|  | 23 | #include <linux/io.h> | 
|  | 24 | #include <linux/ioport.h> | 
|  | 25 | #include <linux/uaccess.h> | 
|  | 26 | #include <linux/platform_device.h> | 
|  | 27 |  | 
|  | 28 | /* | 
|  | 29 | * Cursor position address | 
|  | 30 | * \X  0    1    2  ...  14   15 | 
|  | 31 | * Y+----+----+----+---+----+----+ | 
|  | 32 | * 0|0x00|0x01|0x02|...|0x0e|0x0f| | 
|  | 33 | *  +----+----+----+---+----+----+ | 
|  | 34 | * 1|0x40|0x41|0x42|...|0x4e|0x4f| | 
|  | 35 | *  +----+----+----+---+----+----+ | 
|  | 36 | */ | 
|  | 37 | #define LCD_DATA_REG_OFFSET	0x10 | 
|  | 38 | #define LCD_XRES_MAX		16 | 
|  | 39 | #define LCD_YRES_MAX		2 | 
|  | 40 | #define LCD_CHARS_MAX		32 | 
|  | 41 |  | 
|  | 42 | #define LCD_CLEAR		0x01 | 
|  | 43 | #define LCD_CURSOR_MOVE_HOME	0x02 | 
|  | 44 | #define LCD_RESET		0x06 | 
|  | 45 | #define LCD_OFF			0x08 | 
|  | 46 | #define LCD_CURSOR_OFF		0x0c | 
|  | 47 | #define LCD_CURSOR_BLINK_OFF	0x0e | 
|  | 48 | #define LCD_CURSOR_ON		0x0f | 
|  | 49 | #define LCD_ON			LCD_CURSOR_ON | 
|  | 50 | #define LCD_CURSOR_MOVE_LEFT	0x10 | 
|  | 51 | #define LCD_CURSOR_MOVE_RIGHT	0x14 | 
|  | 52 | #define LCD_DISPLAY_LEFT	0x18 | 
|  | 53 | #define LCD_DISPLAY_RIGHT	0x1c | 
|  | 54 | #define LCD_PRERESET		0x3f	/* execute 4 times continuously */ | 
|  | 55 | #define LCD_BUSY		0x80 | 
|  | 56 |  | 
|  | 57 | #define LCD_GRAPHIC_MODE	0x40 | 
|  | 58 | #define LCD_TEXT_MODE		0x80 | 
|  | 59 | #define LCD_CUR_POS_MASK	0x7f | 
|  | 60 |  | 
|  | 61 | #define LCD_CUR_POS(x)		((x) & LCD_CUR_POS_MASK) | 
|  | 62 | #define LCD_TEXT_POS(x)		((x) | LCD_TEXT_MODE) | 
|  | 63 |  | 
|  | 64 | static inline void lcd_write_control(struct fb_info *info, u8 control) | 
|  | 65 | { | 
|  | 66 | writel((u32)control << 24, info->screen_base); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | static inline u8 lcd_read_control(struct fb_info *info) | 
|  | 70 | { | 
|  | 71 | return readl(info->screen_base) >> 24; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | static inline void lcd_write_data(struct fb_info *info, u8 data) | 
|  | 75 | { | 
|  | 76 | writel((u32)data << 24, info->screen_base + LCD_DATA_REG_OFFSET); | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | static inline u8 lcd_read_data(struct fb_info *info) | 
|  | 80 | { | 
|  | 81 | return readl(info->screen_base + LCD_DATA_REG_OFFSET) >> 24; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | static int lcd_busy_wait(struct fb_info *info) | 
|  | 85 | { | 
|  | 86 | u8 val = 0; | 
|  | 87 | int timeout = 10, retval = 0; | 
|  | 88 |  | 
|  | 89 | do { | 
|  | 90 | val = lcd_read_control(info); | 
|  | 91 | val &= LCD_BUSY; | 
|  | 92 | if (val != LCD_BUSY) | 
|  | 93 | break; | 
|  | 94 |  | 
|  | 95 | if (msleep_interruptible(1)) | 
|  | 96 | return -EINTR; | 
|  | 97 |  | 
|  | 98 | timeout--; | 
|  | 99 | } while (timeout); | 
|  | 100 |  | 
|  | 101 | if (val == LCD_BUSY) | 
|  | 102 | retval = -EBUSY; | 
|  | 103 |  | 
|  | 104 | return retval; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | static void lcd_clear(struct fb_info *info) | 
|  | 108 | { | 
|  | 109 | int i; | 
|  | 110 |  | 
|  | 111 | for (i = 0; i < 4; i++) { | 
|  | 112 | udelay(150); | 
|  | 113 |  | 
|  | 114 | lcd_write_control(info, LCD_PRERESET); | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | udelay(150); | 
|  | 118 |  | 
|  | 119 | lcd_write_control(info, LCD_CLEAR); | 
|  | 120 |  | 
|  | 121 | udelay(150); | 
|  | 122 |  | 
|  | 123 | lcd_write_control(info, LCD_RESET); | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | static struct fb_fix_screeninfo cobalt_lcdfb_fix __initdata = { | 
|  | 127 | .id		= "cobalt-lcd", | 
|  | 128 | .type		= FB_TYPE_TEXT, | 
|  | 129 | .type_aux	= FB_AUX_TEXT_MDA, | 
|  | 130 | .visual		= FB_VISUAL_MONO01, | 
|  | 131 | .line_length	= LCD_XRES_MAX, | 
|  | 132 | .accel		= FB_ACCEL_NONE, | 
|  | 133 | }; | 
|  | 134 |  | 
|  | 135 | static ssize_t cobalt_lcdfb_read(struct fb_info *info, char __user *buf, | 
|  | 136 | size_t count, loff_t *ppos) | 
|  | 137 | { | 
|  | 138 | char src[LCD_CHARS_MAX]; | 
|  | 139 | unsigned long pos; | 
|  | 140 | int len, retval = 0; | 
|  | 141 |  | 
|  | 142 | pos = *ppos; | 
|  | 143 | if (pos >= LCD_CHARS_MAX || count == 0) | 
|  | 144 | return 0; | 
|  | 145 |  | 
|  | 146 | if (count > LCD_CHARS_MAX) | 
|  | 147 | count = LCD_CHARS_MAX; | 
|  | 148 |  | 
|  | 149 | if (pos + count > LCD_CHARS_MAX) | 
|  | 150 | count = LCD_CHARS_MAX - pos; | 
|  | 151 |  | 
|  | 152 | for (len = 0; len < count; len++) { | 
|  | 153 | retval = lcd_busy_wait(info); | 
|  | 154 | if (retval < 0) | 
|  | 155 | break; | 
|  | 156 |  | 
|  | 157 | lcd_write_control(info, LCD_TEXT_POS(pos)); | 
|  | 158 |  | 
|  | 159 | retval = lcd_busy_wait(info); | 
|  | 160 | if (retval < 0) | 
|  | 161 | break; | 
|  | 162 |  | 
|  | 163 | src[len] = lcd_read_data(info); | 
|  | 164 | if (pos == 0x0f) | 
|  | 165 | pos = 0x40; | 
|  | 166 | else | 
|  | 167 | pos++; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | if (retval < 0 && signal_pending(current)) | 
|  | 171 | return -ERESTARTSYS; | 
|  | 172 |  | 
|  | 173 | if (copy_to_user(buf, src, len)) | 
|  | 174 | return -EFAULT; | 
|  | 175 |  | 
|  | 176 | *ppos += len; | 
|  | 177 |  | 
|  | 178 | return len; | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | static ssize_t cobalt_lcdfb_write(struct fb_info *info, const char __user *buf, | 
|  | 182 | size_t count, loff_t *ppos) | 
|  | 183 | { | 
|  | 184 | char dst[LCD_CHARS_MAX]; | 
|  | 185 | unsigned long pos; | 
|  | 186 | int len, retval = 0; | 
|  | 187 |  | 
|  | 188 | pos = *ppos; | 
|  | 189 | if (pos >= LCD_CHARS_MAX || count == 0) | 
|  | 190 | return 0; | 
|  | 191 |  | 
|  | 192 | if (count > LCD_CHARS_MAX) | 
|  | 193 | count = LCD_CHARS_MAX; | 
|  | 194 |  | 
|  | 195 | if (pos + count > LCD_CHARS_MAX) | 
|  | 196 | count = LCD_CHARS_MAX - pos; | 
|  | 197 |  | 
|  | 198 | if (copy_from_user(dst, buf, count)) | 
|  | 199 | return -EFAULT; | 
|  | 200 |  | 
|  | 201 | for (len = 0; len < count; len++) { | 
|  | 202 | retval = lcd_busy_wait(info); | 
|  | 203 | if (retval < 0) | 
|  | 204 | break; | 
|  | 205 |  | 
|  | 206 | lcd_write_control(info, LCD_TEXT_POS(pos)); | 
|  | 207 |  | 
|  | 208 | retval = lcd_busy_wait(info); | 
|  | 209 | if (retval < 0) | 
|  | 210 | break; | 
|  | 211 |  | 
|  | 212 | lcd_write_data(info, dst[len]); | 
|  | 213 | if (pos == 0x0f) | 
|  | 214 | pos = 0x40; | 
|  | 215 | else | 
|  | 216 | pos++; | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | if (retval < 0 && signal_pending(current)) | 
|  | 220 | return -ERESTARTSYS; | 
|  | 221 |  | 
|  | 222 | *ppos += len; | 
|  | 223 |  | 
|  | 224 | return len; | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | static int cobalt_lcdfb_blank(int blank_mode, struct fb_info *info) | 
|  | 228 | { | 
|  | 229 | int retval; | 
|  | 230 |  | 
|  | 231 | retval = lcd_busy_wait(info); | 
|  | 232 | if (retval < 0) | 
|  | 233 | return retval; | 
|  | 234 |  | 
|  | 235 | switch (blank_mode) { | 
|  | 236 | case FB_BLANK_UNBLANK: | 
|  | 237 | lcd_write_control(info, LCD_ON); | 
|  | 238 | break; | 
|  | 239 | default: | 
|  | 240 | lcd_write_control(info, LCD_OFF); | 
|  | 241 | break; | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | return 0; | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | static int cobalt_lcdfb_cursor(struct fb_info *info, struct fb_cursor *cursor) | 
|  | 248 | { | 
|  | 249 | u32 x, y; | 
|  | 250 | int retval; | 
|  | 251 |  | 
|  | 252 | switch (cursor->set) { | 
|  | 253 | case FB_CUR_SETPOS: | 
|  | 254 | x = cursor->image.dx; | 
|  | 255 | y = cursor->image.dy; | 
|  | 256 | if (x >= LCD_XRES_MAX || y >= LCD_YRES_MAX) | 
|  | 257 | return -EINVAL; | 
|  | 258 |  | 
|  | 259 | retval = lcd_busy_wait(info); | 
|  | 260 | if (retval < 0) | 
|  | 261 | return retval; | 
|  | 262 |  | 
|  | 263 | lcd_write_control(info, | 
|  | 264 | LCD_TEXT_POS(info->fix.line_length * y + x)); | 
|  | 265 | break; | 
|  | 266 | default: | 
|  | 267 | return -EINVAL; | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | retval = lcd_busy_wait(info); | 
|  | 271 | if (retval < 0) | 
|  | 272 | return retval; | 
|  | 273 |  | 
|  | 274 | if (cursor->enable) | 
|  | 275 | lcd_write_control(info, LCD_CURSOR_ON); | 
|  | 276 | else | 
|  | 277 | lcd_write_control(info, LCD_CURSOR_OFF); | 
|  | 278 |  | 
|  | 279 | return 0; | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | static struct fb_ops cobalt_lcd_fbops = { | 
|  | 283 | .owner		= THIS_MODULE, | 
|  | 284 | .fb_read	= cobalt_lcdfb_read, | 
|  | 285 | .fb_write	= cobalt_lcdfb_write, | 
|  | 286 | .fb_blank	= cobalt_lcdfb_blank, | 
|  | 287 | .fb_cursor	= cobalt_lcdfb_cursor, | 
|  | 288 | }; | 
|  | 289 |  | 
|  | 290 | static int __init cobalt_lcdfb_probe(struct platform_device *dev) | 
|  | 291 | { | 
|  | 292 | struct fb_info *info; | 
|  | 293 | struct resource *res; | 
|  | 294 | int retval; | 
|  | 295 |  | 
|  | 296 | info = framebuffer_alloc(0, &dev->dev); | 
|  | 297 | if (!info) | 
|  | 298 | return -ENOMEM; | 
|  | 299 |  | 
|  | 300 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); | 
|  | 301 | if (!res) { | 
|  | 302 | framebuffer_release(info); | 
|  | 303 | return -EBUSY; | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | info->screen_size = res->end - res->start + 1; | 
|  | 307 | info->screen_base = ioremap(res->start, info->screen_size); | 
|  | 308 | info->fbops = &cobalt_lcd_fbops; | 
|  | 309 | info->fix = cobalt_lcdfb_fix; | 
|  | 310 | info->fix.smem_start = res->start; | 
|  | 311 | info->fix.smem_len = info->screen_size; | 
|  | 312 | info->pseudo_palette = NULL; | 
|  | 313 | info->par = NULL; | 
|  | 314 | info->flags = FBINFO_DEFAULT; | 
|  | 315 |  | 
|  | 316 | retval = register_framebuffer(info); | 
|  | 317 | if (retval < 0) { | 
|  | 318 | iounmap(info->screen_base); | 
|  | 319 | framebuffer_release(info); | 
|  | 320 | return retval; | 
|  | 321 | } | 
|  | 322 |  | 
|  | 323 | platform_set_drvdata(dev, info); | 
|  | 324 |  | 
|  | 325 | lcd_clear(info); | 
|  | 326 |  | 
|  | 327 | printk(KERN_INFO "fb%d: Cobalt server LCD frame buffer device\n", | 
|  | 328 | info->node); | 
|  | 329 |  | 
|  | 330 | return 0; | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | static int __devexit cobalt_lcdfb_remove(struct platform_device *dev) | 
|  | 334 | { | 
|  | 335 | struct fb_info *info; | 
|  | 336 |  | 
|  | 337 | info = platform_get_drvdata(dev); | 
|  | 338 | if (info) { | 
|  | 339 | iounmap(info->screen_base); | 
|  | 340 | unregister_framebuffer(info); | 
|  | 341 | framebuffer_release(info); | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | return 0; | 
|  | 345 | } | 
|  | 346 |  | 
|  | 347 | static struct platform_driver cobalt_lcdfb_driver = { | 
|  | 348 | .probe	= cobalt_lcdfb_probe, | 
|  | 349 | .remove	= __devexit_p(cobalt_lcdfb_remove), | 
|  | 350 | .driver	= { | 
|  | 351 | .name	= "cobalt-lcd", | 
|  | 352 | .owner	= THIS_MODULE, | 
|  | 353 | }, | 
|  | 354 | }; | 
|  | 355 |  | 
|  | 356 | static int __init cobalt_lcdfb_init(void) | 
|  | 357 | { | 
|  | 358 | return platform_driver_register(&cobalt_lcdfb_driver); | 
|  | 359 | } | 
|  | 360 |  | 
|  | 361 | static void __exit cobalt_lcdfb_exit(void) | 
|  | 362 | { | 
|  | 363 | platform_driver_unregister(&cobalt_lcdfb_driver); | 
|  | 364 | } | 
|  | 365 |  | 
|  | 366 | module_init(cobalt_lcdfb_init); | 
|  | 367 | module_exit(cobalt_lcdfb_exit); | 
|  | 368 |  | 
|  | 369 | MODULE_LICENSE("GPL v2"); | 
|  | 370 | MODULE_AUTHOR("Yoichi Yuasa"); | 
|  | 371 | MODULE_DESCRIPTION("Cobalt server LCD frame buffer driver"); |