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