blob: 020df1ddb674540d3f7532832d9565d561000bb7 [file] [log] [blame]
Alexey Charkovd6ff7d02010-11-09 02:42:39 +03001/*
2 * WonderMedia WM8505 Frame Buffer device driver
3 *
4 * Copyright (C) 2010 Ed Spiridonov <edo.rus@gmail.com>
5 * Based on vt8500lcdfb.c
6 *
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.
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
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/errno.h>
20#include <linux/string.h>
21#include <linux/mm.h>
22#include <linux/slab.h>
23#include <linux/delay.h>
24#include <linux/fb.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/io.h>
28#include <linux/dma-mapping.h>
29#include <linux/platform_device.h>
30#include <linux/wait.h>
Tony Priske7b99532012-08-03 20:58:22 +120031#include <linux/of.h>
32#include <linux/of_fdt.h>
33#include <linux/memblock.h>
Alexey Charkovd6ff7d02010-11-09 02:42:39 +030034
Alexey Charkovd6ff7d02010-11-09 02:42:39 +030035#include "wm8505fb_regs.h"
36#include "wmt_ge_rops.h"
37
38#define DRIVER_NAME "wm8505-fb"
39
40#define to_wm8505fb_info(__info) container_of(__info, \
41 struct wm8505fb_info, fb)
42struct wm8505fb_info {
43 struct fb_info fb;
44 void __iomem *regbase;
45 unsigned int contrast;
46};
47
48
49static int wm8505fb_init_hw(struct fb_info *info)
50{
51 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
52
53 int i;
54
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);
58
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);
62
Tony Priske7b99532012-08-03 20:58:22 +120063 /*
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
67 */
68 writel(0x31c, fbi->regbase + WMT_GOVR_COLORSPACE);
Alexey Charkovd6ff7d02010-11-09 02:42:39 +030069 writel(1, fbi->regbase + WMT_GOVR_COLORSPACE1);
70
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);
74
75 /* black magic ;) */
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);
80
81 return 0;
82}
83
84static int wm8505fb_set_timing(struct fb_info *info)
85{
86 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
87
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;
92
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;
Alexey Charkov623eb152010-12-20 16:09:57 +030096 int v_sync = info->var.vsync_len;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +030097
98 writel(0, fbi->regbase + WMT_GOVR_TG);
99
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);
104
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);
109
110 writel(1, fbi->regbase + WMT_GOVR_TG);
111
112 return 0;
113}
114
115
116static int wm8505fb_set_par(struct fb_info *info)
117{
118 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
119
120 if (!fbi)
121 return -EINVAL;
122
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;
Tony Priske7b99532012-08-03 20:58:22 +1200135 } 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;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300147 }
148
149 wm8505fb_set_timing(info);
150
151 writel(fbi->contrast<<16 | fbi->contrast<<8 | fbi->contrast,
152 fbi->regbase + WMT_GOVR_CONTRAST);
153
154 return 0;
155}
156
157static ssize_t contrast_show(struct device *dev,
158 struct device_attribute *attr, char *buf)
159{
160 struct fb_info *info = dev_get_drvdata(dev);
161 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
162
163 return sprintf(buf, "%d\n", fbi->contrast);
164}
165
166static ssize_t contrast_store(struct device *dev,
167 struct device_attribute *attr,
168 const char *buf, size_t count)
169{
170 struct fb_info *info = dev_get_drvdata(dev);
171 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
172 unsigned long tmp;
173
174 if (strict_strtoul(buf, 10, &tmp) || (tmp > 0xff))
175 return -EINVAL;
176 fbi->contrast = tmp;
177
178 wm8505fb_set_par(info);
179
180 return count;
181}
182
183static DEVICE_ATTR(contrast, 0644, contrast_show, contrast_store);
184
185static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
186{
187 chan &= 0xffff;
188 chan >>= 16 - bf->length;
189 return chan << bf->offset;
190}
191
192static 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);
196 int ret = 1;
197 unsigned int val;
198 if (regno >= 256)
199 return -EINVAL;
200
201 if (info->var.grayscale)
202 red = green = blue =
203 (19595 * red + 38470 * green + 7471 * blue) >> 16;
204
205 switch (fbi->fb.fix.visual) {
206 case FB_VISUAL_TRUECOLOR:
207 if (regno < 16) {
208 u32 *pal = info->pseudo_palette;
209
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);
213
214 pal[regno] = val;
215 ret = 0;
216 }
217 break;
218 }
219
220 return ret;
221}
222
223static int wm8505fb_pan_display(struct fb_var_screeninfo *var,
224 struct fb_info *info)
225{
226 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
227
228 writel(var->xoffset, fbi->regbase + WMT_GOVR_XPAN);
229 writel(var->yoffset, fbi->regbase + WMT_GOVR_YPAN);
230 return 0;
231}
232
233static int wm8505fb_blank(int blank, struct fb_info *info)
234{
235 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
236
237 switch (blank) {
238 case FB_BLANK_UNBLANK:
239 wm8505fb_set_timing(info);
240 break;
241 default:
242 writel(0, fbi->regbase + WMT_GOVR_TIMING_V_SYNC);
243 break;
244 }
245
246 return 0;
247}
248
249static 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,
259};
260
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800261static int wm8505fb_probe(struct platform_device *pdev)
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300262{
263 struct wm8505fb_info *fbi;
264 struct resource *res;
265 void *addr;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300266 int ret;
267
Tony Priske7b99532012-08-03 20:58:22 +1200268 struct fb_videomode of_mode;
269 struct device_node *np;
270 u32 bpp;
271 dma_addr_t fb_mem_phys;
272 unsigned long fb_mem_len;
273 void *fb_mem_virt;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300274
Tony Priske7b99532012-08-03 20:58:22 +1200275 fbi = devm_kzalloc(&pdev->dev, sizeof(struct wm8505fb_info) +
276 sizeof(u32) * 16, GFP_KERNEL);
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300277 if (!fbi) {
278 dev_err(&pdev->dev, "Failed to initialize framebuffer device\n");
Julia Lawall9e340fd2013-04-03 07:20:36 +1300279 return -ENOMEM;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300280 }
281
282 strcpy(fbi->fb.fix.id, DRIVER_NAME);
283
284 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
285 fbi->fb.fix.xpanstep = 1;
286 fbi->fb.fix.ypanstep = 1;
287 fbi->fb.fix.ywrapstep = 0;
288 fbi->fb.fix.accel = FB_ACCEL_NONE;
289
290 fbi->fb.fbops = &wm8505fb_ops;
291 fbi->fb.flags = FBINFO_DEFAULT
292 | FBINFO_HWACCEL_COPYAREA
293 | FBINFO_HWACCEL_FILLRECT
294 | FBINFO_HWACCEL_XPAN
295 | FBINFO_HWACCEL_YPAN
296 | FBINFO_VIRTFB
297 | FBINFO_PARTIAL_PAN_OK;
298 fbi->fb.node = -1;
299
300 addr = fbi;
301 addr = addr + sizeof(struct wm8505fb_info);
302 fbi->fb.pseudo_palette = addr;
303
304 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall9e340fd2013-04-03 07:20:36 +1300305 fbi->regbase = devm_request_and_ioremap(&pdev->dev, res);
306 if (fbi->regbase == NULL)
307 return -EBUSY;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300308
Tony Priske7b99532012-08-03 20:58:22 +1200309 np = of_parse_phandle(pdev->dev.of_node, "default-mode", 0);
310 if (!np) {
311 pr_err("%s: No display description in Device Tree\n", __func__);
Julia Lawall9e340fd2013-04-03 07:20:36 +1300312 return -EINVAL;
Tony Priske7b99532012-08-03 20:58:22 +1200313 }
314
315 /*
316 * This code is copied from Sascha Hauer's of_videomode helper
317 * and can be replaced with a call to the helper once mainlined
318 */
319 ret = 0;
320 ret |= of_property_read_u32(np, "hactive", &of_mode.xres);
321 ret |= of_property_read_u32(np, "vactive", &of_mode.yres);
322 ret |= of_property_read_u32(np, "hback-porch", &of_mode.left_margin);
323 ret |= of_property_read_u32(np, "hfront-porch", &of_mode.right_margin);
324 ret |= of_property_read_u32(np, "hsync-len", &of_mode.hsync_len);
325 ret |= of_property_read_u32(np, "vback-porch", &of_mode.upper_margin);
326 ret |= of_property_read_u32(np, "vfront-porch", &of_mode.lower_margin);
327 ret |= of_property_read_u32(np, "vsync-len", &of_mode.vsync_len);
328 ret |= of_property_read_u32(np, "bpp", &bpp);
329 if (ret) {
330 pr_err("%s: Unable to read display properties\n", __func__);
Julia Lawall9e340fd2013-04-03 07:20:36 +1300331 return -EINVAL;
Tony Priske7b99532012-08-03 20:58:22 +1200332 }
333
334 of_mode.vmode = FB_VMODE_NONINTERLACED;
335 fb_videomode_to_var(&fbi->fb.var, &of_mode);
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300336
337 fbi->fb.var.nonstd = 0;
338 fbi->fb.var.activate = FB_ACTIVATE_NOW;
339
340 fbi->fb.var.height = -1;
341 fbi->fb.var.width = -1;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300342
Tony Priske7b99532012-08-03 20:58:22 +1200343 /* try allocating the framebuffer */
344 fb_mem_len = of_mode.xres * of_mode.yres * 2 * (bpp / 8);
Julia Lawall9e340fd2013-04-03 07:20:36 +1300345 fb_mem_virt = dmam_alloc_coherent(&pdev->dev, fb_mem_len, &fb_mem_phys,
Tony Priske7b99532012-08-03 20:58:22 +1200346 GFP_KERNEL);
347 if (!fb_mem_virt) {
348 pr_err("%s: Failed to allocate framebuffer\n", __func__);
349 return -ENOMEM;
Julia Lawall9e340fd2013-04-03 07:20:36 +1300350 }
Tony Priske7b99532012-08-03 20:58:22 +1200351
352 fbi->fb.var.xres_virtual = of_mode.xres;
353 fbi->fb.var.yres_virtual = of_mode.yres * 2;
354 fbi->fb.var.bits_per_pixel = bpp;
355
356 fbi->fb.fix.smem_start = fb_mem_phys;
357 fbi->fb.fix.smem_len = fb_mem_len;
358 fbi->fb.screen_base = fb_mem_virt;
359 fbi->fb.screen_size = fb_mem_len;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300360
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300361 fbi->contrast = 0x80;
362 ret = wm8505fb_set_par(&fbi->fb);
363 if (ret) {
364 dev_err(&pdev->dev, "Failed to set parameters\n");
Julia Lawall9e340fd2013-04-03 07:20:36 +1300365 return ret;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300366 }
367
Julia Lawall9e340fd2013-04-03 07:20:36 +1300368 if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
369 dev_err(&pdev->dev, "Failed to allocate color map\n");
370 return -ENOMEM;
371 }
372
373 wm8505fb_init_hw(&fbi->fb);
374
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300375 platform_set_drvdata(pdev, fbi);
376
377 ret = register_framebuffer(&fbi->fb);
378 if (ret < 0) {
379 dev_err(&pdev->dev,
380 "Failed to register framebuffer device: %d\n", ret);
Julia Lawall9e340fd2013-04-03 07:20:36 +1300381 if (fbi->fb.cmap.len)
382 fb_dealloc_cmap(&fbi->fb.cmap);
383 return ret;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300384 }
385
386 ret = device_create_file(&pdev->dev, &dev_attr_contrast);
387 if (ret < 0) {
388 printk(KERN_WARNING "fb%d: failed to register attributes (%d)\n",
389 fbi->fb.node, ret);
390 }
391
392 printk(KERN_INFO "fb%d: %s frame buffer at 0x%lx-0x%lx\n",
393 fbi->fb.node, fbi->fb.fix.id, fbi->fb.fix.smem_start,
394 fbi->fb.fix.smem_start + fbi->fb.fix.smem_len - 1);
395
396 return 0;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300397}
398
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800399static int wm8505fb_remove(struct platform_device *pdev)
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300400{
401 struct wm8505fb_info *fbi = platform_get_drvdata(pdev);
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300402
403 device_remove_file(&pdev->dev, &dev_attr_contrast);
404
405 unregister_framebuffer(&fbi->fb);
406
407 writel(0, fbi->regbase);
408
409 if (fbi->fb.cmap.len)
410 fb_dealloc_cmap(&fbi->fb.cmap);
411
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300412 return 0;
413}
414
Tony Priske7b99532012-08-03 20:58:22 +1200415static const struct of_device_id wmt_dt_ids[] = {
416 { .compatible = "wm,wm8505-fb", },
417 {}
418};
419
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300420static struct platform_driver wm8505fb_driver = {
421 .probe = wm8505fb_probe,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800422 .remove = wm8505fb_remove,
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300423 .driver = {
424 .owner = THIS_MODULE,
425 .name = DRIVER_NAME,
Tony Priske7b99532012-08-03 20:58:22 +1200426 .of_match_table = of_match_ptr(wmt_dt_ids),
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300427 },
428};
429
Axel Lin4277f2c2011-11-26 10:25:54 +0800430module_platform_driver(wm8505fb_driver);
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300431
432MODULE_AUTHOR("Ed Spiridonov <edo.rus@gmail.com>");
433MODULE_DESCRIPTION("Framebuffer driver for WMT WM8505");
Tony Priske7b99532012-08-03 20:58:22 +1200434MODULE_LICENSE("GPL v2");
435MODULE_DEVICE_TABLE(of, wmt_dt_ids);