2 * WonderMedia WM8505 Frame Buffer device driver
4 * Copyright (C) 2010 Ed Spiridonov <edo.rus@gmail.com>
5 * Based on vt8500lcdfb.c
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
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.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/string.h>
22 #include <linux/slab.h>
23 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/platform_device.h>
30 #include <linux/wait.h>
32 #include <linux/of_fdt.h>
33 #include <linux/memblock.h>
35 #include "wm8505fb_regs.h"
36 #include "wmt_ge_rops.h"
38 #define DRIVER_NAME "wm8505-fb"
40 #define to_wm8505fb_info(__info) container_of(__info, \
41 struct wm8505fb_info, fb)
42 struct wm8505fb_info {
44 void __iomem *regbase;
45 unsigned int contrast;
49 static int wm8505fb_init_hw(struct fb_info *info)
51 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
55 /* I know the purpose only of few registers, so clear unknown */
56 for (i = 0; i < 0x200; i += 4)
57 writel(0, fbi->regbase + i);
59 /* Set frame buffer address */
60 writel(fbi->fb.fix.smem_start, fbi->regbase + WMT_GOVR_FBADDR);
61 writel(fbi->fb.fix.smem_start, fbi->regbase + WMT_GOVR_FBADDR1);
64 * Set in-memory picture format to RGB
65 * 0x31C sets the correct color mode (RGB565) for WM8650
66 * Bit 8+9 (0x300) are ignored on WM8505 as reserved
68 writel(0x31c, fbi->regbase + WMT_GOVR_COLORSPACE);
69 writel(1, fbi->regbase + WMT_GOVR_COLORSPACE1);
71 /* Virtual buffer size */
72 writel(info->var.xres, fbi->regbase + WMT_GOVR_XRES);
73 writel(info->var.xres_virtual, fbi->regbase + WMT_GOVR_XRES_VIRTUAL);
76 writel(0xf, fbi->regbase + WMT_GOVR_FHI);
77 writel(4, fbi->regbase + WMT_GOVR_DVO_SET);
78 writel(1, fbi->regbase + WMT_GOVR_MIF_ENABLE);
79 writel(1, fbi->regbase + WMT_GOVR_REG_UPDATE);
84 static int wm8505fb_set_timing(struct fb_info *info)
86 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
88 int h_start = info->var.left_margin;
89 int h_end = h_start + info->var.xres;
90 int h_all = h_end + info->var.right_margin;
91 int h_sync = info->var.hsync_len;
93 int v_start = info->var.upper_margin;
94 int v_end = v_start + info->var.yres;
95 int v_all = v_end + info->var.lower_margin;
96 int v_sync = info->var.vsync_len;
98 writel(0, fbi->regbase + WMT_GOVR_TG);
100 writel(h_start, fbi->regbase + WMT_GOVR_TIMING_H_START);
101 writel(h_end, fbi->regbase + WMT_GOVR_TIMING_H_END);
102 writel(h_all, fbi->regbase + WMT_GOVR_TIMING_H_ALL);
103 writel(h_sync, fbi->regbase + WMT_GOVR_TIMING_H_SYNC);
105 writel(v_start, fbi->regbase + WMT_GOVR_TIMING_V_START);
106 writel(v_end, fbi->regbase + WMT_GOVR_TIMING_V_END);
107 writel(v_all, fbi->regbase + WMT_GOVR_TIMING_V_ALL);
108 writel(v_sync, fbi->regbase + WMT_GOVR_TIMING_V_SYNC);
110 writel(1, fbi->regbase + WMT_GOVR_TG);
116 static int wm8505fb_set_par(struct fb_info *info)
118 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
123 if (info->var.bits_per_pixel == 32) {
124 info->var.red.offset = 16;
125 info->var.red.length = 8;
126 info->var.red.msb_right = 0;
127 info->var.green.offset = 8;
128 info->var.green.length = 8;
129 info->var.green.msb_right = 0;
130 info->var.blue.offset = 0;
131 info->var.blue.length = 8;
132 info->var.blue.msb_right = 0;
133 info->fix.visual = FB_VISUAL_TRUECOLOR;
134 info->fix.line_length = info->var.xres_virtual << 2;
135 } else if (info->var.bits_per_pixel == 16) {
136 info->var.red.offset = 11;
137 info->var.red.length = 5;
138 info->var.red.msb_right = 0;
139 info->var.green.offset = 5;
140 info->var.green.length = 6;
141 info->var.green.msb_right = 0;
142 info->var.blue.offset = 0;
143 info->var.blue.length = 5;
144 info->var.blue.msb_right = 0;
145 info->fix.visual = FB_VISUAL_TRUECOLOR;
146 info->fix.line_length = info->var.xres_virtual << 1;
149 wm8505fb_set_timing(info);
151 writel(fbi->contrast<<16 | fbi->contrast<<8 | fbi->contrast,
152 fbi->regbase + WMT_GOVR_CONTRAST);
157 static ssize_t contrast_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
160 struct fb_info *info = dev_get_drvdata(dev);
161 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
163 return sprintf(buf, "%d\n", fbi->contrast);
166 static ssize_t contrast_store(struct device *dev,
167 struct device_attribute *attr,
168 const char *buf, size_t count)
170 struct fb_info *info = dev_get_drvdata(dev);
171 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
174 if (strict_strtoul(buf, 10, &tmp) || (tmp > 0xff))
178 wm8505fb_set_par(info);
183 static DEVICE_ATTR(contrast, 0644, contrast_show, contrast_store);
185 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
188 chan >>= 16 - bf->length;
189 return chan << bf->offset;
192 static int wm8505fb_setcolreg(unsigned regno, unsigned red, unsigned green,
193 unsigned blue, unsigned transp,
194 struct fb_info *info) {
195 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
201 if (info->var.grayscale)
203 (19595 * red + 38470 * green + 7471 * blue) >> 16;
205 switch (fbi->fb.fix.visual) {
206 case FB_VISUAL_TRUECOLOR:
208 u32 *pal = info->pseudo_palette;
210 val = chan_to_field(red, &fbi->fb.var.red);
211 val |= chan_to_field(green, &fbi->fb.var.green);
212 val |= chan_to_field(blue, &fbi->fb.var.blue);
223 static int wm8505fb_pan_display(struct fb_var_screeninfo *var,
224 struct fb_info *info)
226 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
228 writel(var->xoffset, fbi->regbase + WMT_GOVR_XPAN);
229 writel(var->yoffset, fbi->regbase + WMT_GOVR_YPAN);
233 static int wm8505fb_blank(int blank, struct fb_info *info)
235 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
238 case FB_BLANK_UNBLANK:
239 wm8505fb_set_timing(info);
242 writel(0, fbi->regbase + WMT_GOVR_TIMING_V_SYNC);
249 static struct fb_ops wm8505fb_ops = {
250 .owner = THIS_MODULE,
251 .fb_set_par = wm8505fb_set_par,
252 .fb_setcolreg = wm8505fb_setcolreg,
253 .fb_fillrect = wmt_ge_fillrect,
254 .fb_copyarea = wmt_ge_copyarea,
255 .fb_imageblit = sys_imageblit,
256 .fb_sync = wmt_ge_sync,
257 .fb_pan_display = wm8505fb_pan_display,
258 .fb_blank = wm8505fb_blank,
261 static int wm8505fb_probe(struct platform_device *pdev)
263 struct wm8505fb_info *fbi;
264 struct resource *res;
268 struct fb_videomode of_mode;
269 struct device_node *np;
271 dma_addr_t fb_mem_phys;
272 unsigned long fb_mem_len;
278 fbi = devm_kzalloc(&pdev->dev, sizeof(struct wm8505fb_info) +
279 sizeof(u32) * 16, GFP_KERNEL);
281 dev_err(&pdev->dev, "Failed to initialize framebuffer device\n");
286 strcpy(fbi->fb.fix.id, DRIVER_NAME);
288 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
289 fbi->fb.fix.xpanstep = 1;
290 fbi->fb.fix.ypanstep = 1;
291 fbi->fb.fix.ywrapstep = 0;
292 fbi->fb.fix.accel = FB_ACCEL_NONE;
294 fbi->fb.fbops = &wm8505fb_ops;
295 fbi->fb.flags = FBINFO_DEFAULT
296 | FBINFO_HWACCEL_COPYAREA
297 | FBINFO_HWACCEL_FILLRECT
298 | FBINFO_HWACCEL_XPAN
299 | FBINFO_HWACCEL_YPAN
301 | FBINFO_PARTIAL_PAN_OK;
305 addr = addr + sizeof(struct wm8505fb_info);
306 fbi->fb.pseudo_palette = addr;
308 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
310 dev_err(&pdev->dev, "no I/O memory resource defined\n");
315 res = request_mem_region(res->start, resource_size(res), DRIVER_NAME);
317 dev_err(&pdev->dev, "failed to request I/O memory\n");
322 fbi->regbase = ioremap(res->start, resource_size(res));
323 if (fbi->regbase == NULL) {
324 dev_err(&pdev->dev, "failed to map I/O memory\n");
326 goto failed_free_res;
329 np = of_parse_phandle(pdev->dev.of_node, "default-mode", 0);
331 pr_err("%s: No display description in Device Tree\n", __func__);
333 goto failed_free_res;
337 * This code is copied from Sascha Hauer's of_videomode helper
338 * and can be replaced with a call to the helper once mainlined
341 ret |= of_property_read_u32(np, "hactive", &of_mode.xres);
342 ret |= of_property_read_u32(np, "vactive", &of_mode.yres);
343 ret |= of_property_read_u32(np, "hback-porch", &of_mode.left_margin);
344 ret |= of_property_read_u32(np, "hfront-porch", &of_mode.right_margin);
345 ret |= of_property_read_u32(np, "hsync-len", &of_mode.hsync_len);
346 ret |= of_property_read_u32(np, "vback-porch", &of_mode.upper_margin);
347 ret |= of_property_read_u32(np, "vfront-porch", &of_mode.lower_margin);
348 ret |= of_property_read_u32(np, "vsync-len", &of_mode.vsync_len);
349 ret |= of_property_read_u32(np, "bpp", &bpp);
351 pr_err("%s: Unable to read display properties\n", __func__);
352 goto failed_free_res;
355 of_mode.vmode = FB_VMODE_NONINTERLACED;
356 fb_videomode_to_var(&fbi->fb.var, &of_mode);
358 fbi->fb.var.nonstd = 0;
359 fbi->fb.var.activate = FB_ACTIVATE_NOW;
361 fbi->fb.var.height = -1;
362 fbi->fb.var.width = -1;
364 /* try allocating the framebuffer */
365 fb_mem_len = of_mode.xres * of_mode.yres * 2 * (bpp / 8);
366 fb_mem_virt = dma_alloc_coherent(&pdev->dev, fb_mem_len, &fb_mem_phys,
369 pr_err("%s: Failed to allocate framebuffer\n", __func__);
373 fbi->fb.var.xres_virtual = of_mode.xres;
374 fbi->fb.var.yres_virtual = of_mode.yres * 2;
375 fbi->fb.var.bits_per_pixel = bpp;
377 fbi->fb.fix.smem_start = fb_mem_phys;
378 fbi->fb.fix.smem_len = fb_mem_len;
379 fbi->fb.screen_base = fb_mem_virt;
380 fbi->fb.screen_size = fb_mem_len;
382 if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
383 dev_err(&pdev->dev, "Failed to allocate color map\n");
388 wm8505fb_init_hw(&fbi->fb);
390 fbi->contrast = 0x80;
391 ret = wm8505fb_set_par(&fbi->fb);
393 dev_err(&pdev->dev, "Failed to set parameters\n");
394 goto failed_free_cmap;
397 platform_set_drvdata(pdev, fbi);
399 ret = register_framebuffer(&fbi->fb);
402 "Failed to register framebuffer device: %d\n", ret);
403 goto failed_free_cmap;
406 ret = device_create_file(&pdev->dev, &dev_attr_contrast);
408 printk(KERN_WARNING "fb%d: failed to register attributes (%d)\n",
412 printk(KERN_INFO "fb%d: %s frame buffer at 0x%lx-0x%lx\n",
413 fbi->fb.node, fbi->fb.fix.id, fbi->fb.fix.smem_start,
414 fbi->fb.fix.smem_start + fbi->fb.fix.smem_len - 1);
419 if (fbi->fb.cmap.len)
420 fb_dealloc_cmap(&fbi->fb.cmap);
422 iounmap(fbi->regbase);
424 release_mem_region(res->start, resource_size(res));
426 platform_set_drvdata(pdev, NULL);
432 static int wm8505fb_remove(struct platform_device *pdev)
434 struct wm8505fb_info *fbi = platform_get_drvdata(pdev);
435 struct resource *res;
437 device_remove_file(&pdev->dev, &dev_attr_contrast);
439 unregister_framebuffer(&fbi->fb);
441 writel(0, fbi->regbase);
443 if (fbi->fb.cmap.len)
444 fb_dealloc_cmap(&fbi->fb.cmap);
446 iounmap(fbi->regbase);
448 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
449 release_mem_region(res->start, resource_size(res));
456 static const struct of_device_id wmt_dt_ids[] = {
457 { .compatible = "wm,wm8505-fb", },
461 static struct platform_driver wm8505fb_driver = {
462 .probe = wm8505fb_probe,
463 .remove = wm8505fb_remove,
465 .owner = THIS_MODULE,
467 .of_match_table = of_match_ptr(wmt_dt_ids),
471 module_platform_driver(wm8505fb_driver);
473 MODULE_AUTHOR("Ed Spiridonov <edo.rus@gmail.com>");
474 MODULE_DESCRIPTION("Framebuffer driver for WMT WM8505");
475 MODULE_LICENSE("GPL v2");
476 MODULE_DEVICE_TABLE(of, wmt_dt_ids);