Linux 3.11-rc3
[cris-mirror.git] / drivers / video / wm8505fb.c
blob3072f30cad1984d23309a2d3430adc2f60d625f8
1 /*
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/delay.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/fb.h>
20 #include <linux/errno.h>
21 #include <linux/err.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/io.h>
25 #include <linux/kernel.h>
26 #include <linux/memblock.h>
27 #include <linux/mm.h>
28 #include <linux/module.h>
29 #include <linux/of.h>
30 #include <linux/of_fdt.h>
31 #include <linux/platform_device.h>
32 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/wait.h>
35 #include <video/of_display_timing.h>
37 #include "wm8505fb_regs.h"
38 #include "wmt_ge_rops.h"
40 #define DRIVER_NAME "wm8505-fb"
42 #define to_wm8505fb_info(__info) container_of(__info, \
43 struct wm8505fb_info, fb)
44 struct wm8505fb_info {
45 struct fb_info fb;
46 void __iomem *regbase;
47 unsigned int contrast;
51 static int wm8505fb_init_hw(struct fb_info *info)
53 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
55 int i;
57 /* I know the purpose only of few registers, so clear unknown */
58 for (i = 0; i < 0x200; i += 4)
59 writel(0, fbi->regbase + i);
61 /* Set frame buffer address */
62 writel(fbi->fb.fix.smem_start, fbi->regbase + WMT_GOVR_FBADDR);
63 writel(fbi->fb.fix.smem_start, fbi->regbase + WMT_GOVR_FBADDR1);
66 * Set in-memory picture format to RGB
67 * 0x31C sets the correct color mode (RGB565) for WM8650
68 * Bit 8+9 (0x300) are ignored on WM8505 as reserved
70 writel(0x31c, fbi->regbase + WMT_GOVR_COLORSPACE);
71 writel(1, fbi->regbase + WMT_GOVR_COLORSPACE1);
73 /* Virtual buffer size */
74 writel(info->var.xres, fbi->regbase + WMT_GOVR_XRES);
75 writel(info->var.xres_virtual, fbi->regbase + WMT_GOVR_XRES_VIRTUAL);
77 /* black magic ;) */
78 writel(0xf, fbi->regbase + WMT_GOVR_FHI);
79 writel(4, fbi->regbase + WMT_GOVR_DVO_SET);
80 writel(1, fbi->regbase + WMT_GOVR_MIF_ENABLE);
81 writel(1, fbi->regbase + WMT_GOVR_REG_UPDATE);
83 return 0;
86 static int wm8505fb_set_timing(struct fb_info *info)
88 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
90 int h_start = info->var.left_margin;
91 int h_end = h_start + info->var.xres;
92 int h_all = h_end + info->var.right_margin;
93 int h_sync = info->var.hsync_len;
95 int v_start = info->var.upper_margin;
96 int v_end = v_start + info->var.yres;
97 int v_all = v_end + info->var.lower_margin;
98 int v_sync = info->var.vsync_len;
100 writel(0, fbi->regbase + WMT_GOVR_TG);
102 writel(h_start, fbi->regbase + WMT_GOVR_TIMING_H_START);
103 writel(h_end, fbi->regbase + WMT_GOVR_TIMING_H_END);
104 writel(h_all, fbi->regbase + WMT_GOVR_TIMING_H_ALL);
105 writel(h_sync, fbi->regbase + WMT_GOVR_TIMING_H_SYNC);
107 writel(v_start, fbi->regbase + WMT_GOVR_TIMING_V_START);
108 writel(v_end, fbi->regbase + WMT_GOVR_TIMING_V_END);
109 writel(v_all, fbi->regbase + WMT_GOVR_TIMING_V_ALL);
110 writel(v_sync, fbi->regbase + WMT_GOVR_TIMING_V_SYNC);
112 writel(1, fbi->regbase + WMT_GOVR_TG);
114 return 0;
118 static int wm8505fb_set_par(struct fb_info *info)
120 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
122 if (!fbi)
123 return -EINVAL;
125 if (info->var.bits_per_pixel == 32) {
126 info->var.red.offset = 16;
127 info->var.red.length = 8;
128 info->var.red.msb_right = 0;
129 info->var.green.offset = 8;
130 info->var.green.length = 8;
131 info->var.green.msb_right = 0;
132 info->var.blue.offset = 0;
133 info->var.blue.length = 8;
134 info->var.blue.msb_right = 0;
135 info->fix.visual = FB_VISUAL_TRUECOLOR;
136 info->fix.line_length = info->var.xres_virtual << 2;
137 } else if (info->var.bits_per_pixel == 16) {
138 info->var.red.offset = 11;
139 info->var.red.length = 5;
140 info->var.red.msb_right = 0;
141 info->var.green.offset = 5;
142 info->var.green.length = 6;
143 info->var.green.msb_right = 0;
144 info->var.blue.offset = 0;
145 info->var.blue.length = 5;
146 info->var.blue.msb_right = 0;
147 info->fix.visual = FB_VISUAL_TRUECOLOR;
148 info->fix.line_length = info->var.xres_virtual << 1;
151 wm8505fb_set_timing(info);
153 writel(fbi->contrast<<16 | fbi->contrast<<8 | fbi->contrast,
154 fbi->regbase + WMT_GOVR_CONTRAST);
156 return 0;
159 static ssize_t contrast_show(struct device *dev,
160 struct device_attribute *attr, char *buf)
162 struct fb_info *info = dev_get_drvdata(dev);
163 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
165 return sprintf(buf, "%d\n", fbi->contrast);
168 static ssize_t contrast_store(struct device *dev,
169 struct device_attribute *attr,
170 const char *buf, size_t count)
172 struct fb_info *info = dev_get_drvdata(dev);
173 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
174 unsigned long tmp;
176 if (kstrtoul(buf, 10, &tmp) || (tmp > 0xff))
177 return -EINVAL;
178 fbi->contrast = tmp;
180 wm8505fb_set_par(info);
182 return count;
185 static DEVICE_ATTR(contrast, 0644, contrast_show, contrast_store);
187 static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
189 chan &= 0xffff;
190 chan >>= 16 - bf->length;
191 return chan << bf->offset;
194 static int wm8505fb_setcolreg(unsigned regno, unsigned red, unsigned green,
195 unsigned blue, unsigned transp,
196 struct fb_info *info) {
197 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
198 int ret = 1;
199 unsigned int val;
200 if (regno >= 256)
201 return -EINVAL;
203 if (info->var.grayscale)
204 red = green = blue =
205 (19595 * red + 38470 * green + 7471 * blue) >> 16;
207 switch (fbi->fb.fix.visual) {
208 case FB_VISUAL_TRUECOLOR:
209 if (regno < 16) {
210 u32 *pal = info->pseudo_palette;
212 val = chan_to_field(red, &fbi->fb.var.red);
213 val |= chan_to_field(green, &fbi->fb.var.green);
214 val |= chan_to_field(blue, &fbi->fb.var.blue);
216 pal[regno] = val;
217 ret = 0;
219 break;
222 return ret;
225 static int wm8505fb_pan_display(struct fb_var_screeninfo *var,
226 struct fb_info *info)
228 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
230 writel(var->xoffset, fbi->regbase + WMT_GOVR_XPAN);
231 writel(var->yoffset, fbi->regbase + WMT_GOVR_YPAN);
232 return 0;
235 static int wm8505fb_blank(int blank, struct fb_info *info)
237 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
239 switch (blank) {
240 case FB_BLANK_UNBLANK:
241 wm8505fb_set_timing(info);
242 break;
243 default:
244 writel(0, fbi->regbase + WMT_GOVR_TIMING_V_SYNC);
245 break;
248 return 0;
251 static struct fb_ops wm8505fb_ops = {
252 .owner = THIS_MODULE,
253 .fb_set_par = wm8505fb_set_par,
254 .fb_setcolreg = wm8505fb_setcolreg,
255 .fb_fillrect = wmt_ge_fillrect,
256 .fb_copyarea = wmt_ge_copyarea,
257 .fb_imageblit = sys_imageblit,
258 .fb_sync = wmt_ge_sync,
259 .fb_pan_display = wm8505fb_pan_display,
260 .fb_blank = wm8505fb_blank,
263 static int wm8505fb_probe(struct platform_device *pdev)
265 struct wm8505fb_info *fbi;
266 struct resource *res;
267 struct display_timings *disp_timing;
268 void *addr;
269 int ret;
271 struct fb_videomode mode;
272 u32 bpp;
273 dma_addr_t fb_mem_phys;
274 unsigned long fb_mem_len;
275 void *fb_mem_virt;
277 fbi = devm_kzalloc(&pdev->dev, sizeof(struct wm8505fb_info) +
278 sizeof(u32) * 16, GFP_KERNEL);
279 if (!fbi) {
280 dev_err(&pdev->dev, "Failed to initialize framebuffer device\n");
281 return -ENOMEM;
284 strcpy(fbi->fb.fix.id, DRIVER_NAME);
286 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
287 fbi->fb.fix.xpanstep = 1;
288 fbi->fb.fix.ypanstep = 1;
289 fbi->fb.fix.ywrapstep = 0;
290 fbi->fb.fix.accel = FB_ACCEL_NONE;
292 fbi->fb.fbops = &wm8505fb_ops;
293 fbi->fb.flags = FBINFO_DEFAULT
294 | FBINFO_HWACCEL_COPYAREA
295 | FBINFO_HWACCEL_FILLRECT
296 | FBINFO_HWACCEL_XPAN
297 | FBINFO_HWACCEL_YPAN
298 | FBINFO_VIRTFB
299 | FBINFO_PARTIAL_PAN_OK;
300 fbi->fb.node = -1;
302 addr = fbi;
303 addr = addr + sizeof(struct wm8505fb_info);
304 fbi->fb.pseudo_palette = addr;
306 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
307 fbi->regbase = devm_ioremap_resource(&pdev->dev, res);
308 if (IS_ERR(fbi->regbase))
309 return PTR_ERR(fbi->regbase);
311 disp_timing = of_get_display_timings(pdev->dev.of_node);
312 if (!disp_timing)
313 return -EINVAL;
315 ret = of_get_fb_videomode(pdev->dev.of_node, &mode, OF_USE_NATIVE_MODE);
316 if (ret)
317 return ret;
319 ret = of_property_read_u32(pdev->dev.of_node, "bits-per-pixel", &bpp);
320 if (ret)
321 return ret;
323 fb_videomode_to_var(&fbi->fb.var, &mode);
325 fbi->fb.var.nonstd = 0;
326 fbi->fb.var.activate = FB_ACTIVATE_NOW;
328 fbi->fb.var.height = -1;
329 fbi->fb.var.width = -1;
331 /* try allocating the framebuffer */
332 fb_mem_len = mode.xres * mode.yres * 2 * (bpp / 8);
333 fb_mem_virt = dmam_alloc_coherent(&pdev->dev, fb_mem_len, &fb_mem_phys,
334 GFP_KERNEL);
335 if (!fb_mem_virt) {
336 pr_err("%s: Failed to allocate framebuffer\n", __func__);
337 return -ENOMEM;
340 fbi->fb.var.xres_virtual = mode.xres;
341 fbi->fb.var.yres_virtual = mode.yres * 2;
342 fbi->fb.var.bits_per_pixel = bpp;
344 fbi->fb.fix.smem_start = fb_mem_phys;
345 fbi->fb.fix.smem_len = fb_mem_len;
346 fbi->fb.screen_base = fb_mem_virt;
347 fbi->fb.screen_size = fb_mem_len;
349 fbi->contrast = 0x10;
350 ret = wm8505fb_set_par(&fbi->fb);
351 if (ret) {
352 dev_err(&pdev->dev, "Failed to set parameters\n");
353 return ret;
356 if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
357 dev_err(&pdev->dev, "Failed to allocate color map\n");
358 return -ENOMEM;
361 wm8505fb_init_hw(&fbi->fb);
363 platform_set_drvdata(pdev, fbi);
365 ret = register_framebuffer(&fbi->fb);
366 if (ret < 0) {
367 dev_err(&pdev->dev,
368 "Failed to register framebuffer device: %d\n", ret);
369 if (fbi->fb.cmap.len)
370 fb_dealloc_cmap(&fbi->fb.cmap);
371 return ret;
374 ret = device_create_file(&pdev->dev, &dev_attr_contrast);
375 if (ret < 0) {
376 printk(KERN_WARNING "fb%d: failed to register attributes (%d)\n",
377 fbi->fb.node, ret);
380 printk(KERN_INFO "fb%d: %s frame buffer at 0x%lx-0x%lx\n",
381 fbi->fb.node, fbi->fb.fix.id, fbi->fb.fix.smem_start,
382 fbi->fb.fix.smem_start + fbi->fb.fix.smem_len - 1);
384 return 0;
387 static int wm8505fb_remove(struct platform_device *pdev)
389 struct wm8505fb_info *fbi = platform_get_drvdata(pdev);
391 device_remove_file(&pdev->dev, &dev_attr_contrast);
393 unregister_framebuffer(&fbi->fb);
395 writel(0, fbi->regbase);
397 if (fbi->fb.cmap.len)
398 fb_dealloc_cmap(&fbi->fb.cmap);
400 return 0;
403 static const struct of_device_id wmt_dt_ids[] = {
404 { .compatible = "wm,wm8505-fb", },
408 static struct platform_driver wm8505fb_driver = {
409 .probe = wm8505fb_probe,
410 .remove = wm8505fb_remove,
411 .driver = {
412 .owner = THIS_MODULE,
413 .name = DRIVER_NAME,
414 .of_match_table = of_match_ptr(wmt_dt_ids),
418 module_platform_driver(wm8505fb_driver);
420 MODULE_AUTHOR("Ed Spiridonov <edo.rus@gmail.com>");
421 MODULE_DESCRIPTION("Framebuffer driver for WMT WM8505");
422 MODULE_LICENSE("GPL v2");
423 MODULE_DEVICE_TABLE(of, wmt_dt_ids);