blob: 19e2e7ff492aa62b1a37afa3252fcc3a4b78feae [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
Alexey Charkovd6ff7d02010-11-09 02:42:39 +030017#include <linux/delay.h>
Tony Prisk7ab0a482013-04-03 07:20:38 +130018#include <linux/dma-mapping.h>
Alexey Charkovd6ff7d02010-11-09 02:42:39 +030019#include <linux/fb.h>
Tony Prisk7ab0a482013-04-03 07:20:38 +130020#include <linux/errno.h>
Alexey Charkovd6ff7d02010-11-09 02:42:39 +030021#include <linux/init.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
Tony Prisk7ab0a482013-04-03 07:20:38 +130024#include <linux/kernel.h>
25#include <linux/memblock.h>
26#include <linux/mm.h>
27#include <linux/module.h>
Tony Priske7b99532012-08-03 20:58:22 +120028#include <linux/of.h>
29#include <linux/of_fdt.h>
Tony Prisk7ab0a482013-04-03 07:20:38 +130030#include <linux/platform_device.h>
31#include <linux/slab.h>
32#include <linux/string.h>
33#include <linux/wait.h>
34#include <video/of_display_timing.h>
Alexey Charkovd6ff7d02010-11-09 02:42:39 +030035
Alexey Charkovd6ff7d02010-11-09 02:42:39 +030036#include "wm8505fb_regs.h"
37#include "wmt_ge_rops.h"
38
39#define DRIVER_NAME "wm8505-fb"
40
41#define to_wm8505fb_info(__info) container_of(__info, \
42 struct wm8505fb_info, fb)
43struct wm8505fb_info {
44 struct fb_info fb;
45 void __iomem *regbase;
46 unsigned int contrast;
47};
48
49
50static int wm8505fb_init_hw(struct fb_info *info)
51{
52 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
53
54 int i;
55
56 /* I know the purpose only of few registers, so clear unknown */
57 for (i = 0; i < 0x200; i += 4)
58 writel(0, fbi->regbase + i);
59
60 /* Set frame buffer address */
61 writel(fbi->fb.fix.smem_start, fbi->regbase + WMT_GOVR_FBADDR);
62 writel(fbi->fb.fix.smem_start, fbi->regbase + WMT_GOVR_FBADDR1);
63
Tony Priske7b99532012-08-03 20:58:22 +120064 /*
65 * Set in-memory picture format to RGB
66 * 0x31C sets the correct color mode (RGB565) for WM8650
67 * Bit 8+9 (0x300) are ignored on WM8505 as reserved
68 */
69 writel(0x31c, fbi->regbase + WMT_GOVR_COLORSPACE);
Alexey Charkovd6ff7d02010-11-09 02:42:39 +030070 writel(1, fbi->regbase + WMT_GOVR_COLORSPACE1);
71
72 /* Virtual buffer size */
73 writel(info->var.xres, fbi->regbase + WMT_GOVR_XRES);
74 writel(info->var.xres_virtual, fbi->regbase + WMT_GOVR_XRES_VIRTUAL);
75
76 /* black magic ;) */
77 writel(0xf, fbi->regbase + WMT_GOVR_FHI);
78 writel(4, fbi->regbase + WMT_GOVR_DVO_SET);
79 writel(1, fbi->regbase + WMT_GOVR_MIF_ENABLE);
80 writel(1, fbi->regbase + WMT_GOVR_REG_UPDATE);
81
82 return 0;
83}
84
85static int wm8505fb_set_timing(struct fb_info *info)
86{
87 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
88
89 int h_start = info->var.left_margin;
90 int h_end = h_start + info->var.xres;
91 int h_all = h_end + info->var.right_margin;
92 int h_sync = info->var.hsync_len;
93
94 int v_start = info->var.upper_margin;
95 int v_end = v_start + info->var.yres;
96 int v_all = v_end + info->var.lower_margin;
Alexey Charkov623eb152010-12-20 16:09:57 +030097 int v_sync = info->var.vsync_len;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +030098
99 writel(0, fbi->regbase + WMT_GOVR_TG);
100
101 writel(h_start, fbi->regbase + WMT_GOVR_TIMING_H_START);
102 writel(h_end, fbi->regbase + WMT_GOVR_TIMING_H_END);
103 writel(h_all, fbi->regbase + WMT_GOVR_TIMING_H_ALL);
104 writel(h_sync, fbi->regbase + WMT_GOVR_TIMING_H_SYNC);
105
106 writel(v_start, fbi->regbase + WMT_GOVR_TIMING_V_START);
107 writel(v_end, fbi->regbase + WMT_GOVR_TIMING_V_END);
108 writel(v_all, fbi->regbase + WMT_GOVR_TIMING_V_ALL);
109 writel(v_sync, fbi->regbase + WMT_GOVR_TIMING_V_SYNC);
110
111 writel(1, fbi->regbase + WMT_GOVR_TG);
112
113 return 0;
114}
115
116
117static int wm8505fb_set_par(struct fb_info *info)
118{
119 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
120
121 if (!fbi)
122 return -EINVAL;
123
124 if (info->var.bits_per_pixel == 32) {
125 info->var.red.offset = 16;
126 info->var.red.length = 8;
127 info->var.red.msb_right = 0;
128 info->var.green.offset = 8;
129 info->var.green.length = 8;
130 info->var.green.msb_right = 0;
131 info->var.blue.offset = 0;
132 info->var.blue.length = 8;
133 info->var.blue.msb_right = 0;
134 info->fix.visual = FB_VISUAL_TRUECOLOR;
135 info->fix.line_length = info->var.xres_virtual << 2;
Tony Priske7b99532012-08-03 20:58:22 +1200136 } else if (info->var.bits_per_pixel == 16) {
137 info->var.red.offset = 11;
138 info->var.red.length = 5;
139 info->var.red.msb_right = 0;
140 info->var.green.offset = 5;
141 info->var.green.length = 6;
142 info->var.green.msb_right = 0;
143 info->var.blue.offset = 0;
144 info->var.blue.length = 5;
145 info->var.blue.msb_right = 0;
146 info->fix.visual = FB_VISUAL_TRUECOLOR;
147 info->fix.line_length = info->var.xres_virtual << 1;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300148 }
149
150 wm8505fb_set_timing(info);
151
152 writel(fbi->contrast<<16 | fbi->contrast<<8 | fbi->contrast,
153 fbi->regbase + WMT_GOVR_CONTRAST);
154
155 return 0;
156}
157
158static ssize_t contrast_show(struct device *dev,
159 struct device_attribute *attr, char *buf)
160{
161 struct fb_info *info = dev_get_drvdata(dev);
162 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
163
164 return sprintf(buf, "%d\n", fbi->contrast);
165}
166
167static ssize_t contrast_store(struct device *dev,
168 struct device_attribute *attr,
169 const char *buf, size_t count)
170{
171 struct fb_info *info = dev_get_drvdata(dev);
172 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
173 unsigned long tmp;
174
175 if (strict_strtoul(buf, 10, &tmp) || (tmp > 0xff))
176 return -EINVAL;
177 fbi->contrast = tmp;
178
179 wm8505fb_set_par(info);
180
181 return count;
182}
183
184static DEVICE_ATTR(contrast, 0644, contrast_show, contrast_store);
185
186static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
187{
188 chan &= 0xffff;
189 chan >>= 16 - bf->length;
190 return chan << bf->offset;
191}
192
193static int wm8505fb_setcolreg(unsigned regno, unsigned red, unsigned green,
194 unsigned blue, unsigned transp,
195 struct fb_info *info) {
196 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
197 int ret = 1;
198 unsigned int val;
199 if (regno >= 256)
200 return -EINVAL;
201
202 if (info->var.grayscale)
203 red = green = blue =
204 (19595 * red + 38470 * green + 7471 * blue) >> 16;
205
206 switch (fbi->fb.fix.visual) {
207 case FB_VISUAL_TRUECOLOR:
208 if (regno < 16) {
209 u32 *pal = info->pseudo_palette;
210
211 val = chan_to_field(red, &fbi->fb.var.red);
212 val |= chan_to_field(green, &fbi->fb.var.green);
213 val |= chan_to_field(blue, &fbi->fb.var.blue);
214
215 pal[regno] = val;
216 ret = 0;
217 }
218 break;
219 }
220
221 return ret;
222}
223
224static int wm8505fb_pan_display(struct fb_var_screeninfo *var,
225 struct fb_info *info)
226{
227 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
228
229 writel(var->xoffset, fbi->regbase + WMT_GOVR_XPAN);
230 writel(var->yoffset, fbi->regbase + WMT_GOVR_YPAN);
231 return 0;
232}
233
234static int wm8505fb_blank(int blank, struct fb_info *info)
235{
236 struct wm8505fb_info *fbi = to_wm8505fb_info(info);
237
238 switch (blank) {
239 case FB_BLANK_UNBLANK:
240 wm8505fb_set_timing(info);
241 break;
242 default:
243 writel(0, fbi->regbase + WMT_GOVR_TIMING_V_SYNC);
244 break;
245 }
246
247 return 0;
248}
249
250static struct fb_ops wm8505fb_ops = {
251 .owner = THIS_MODULE,
252 .fb_set_par = wm8505fb_set_par,
253 .fb_setcolreg = wm8505fb_setcolreg,
254 .fb_fillrect = wmt_ge_fillrect,
255 .fb_copyarea = wmt_ge_copyarea,
256 .fb_imageblit = sys_imageblit,
257 .fb_sync = wmt_ge_sync,
258 .fb_pan_display = wm8505fb_pan_display,
259 .fb_blank = wm8505fb_blank,
260};
261
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800262static int wm8505fb_probe(struct platform_device *pdev)
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300263{
264 struct wm8505fb_info *fbi;
Tony Prisk7ab0a482013-04-03 07:20:38 +1300265 struct resource *res;
266 struct display_timings *disp_timing;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300267 void *addr;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300268 int ret;
269
Tony Prisk7ab0a482013-04-03 07:20:38 +1300270 struct fb_videomode mode;
Tony Priske7b99532012-08-03 20:58:22 +1200271 u32 bpp;
272 dma_addr_t fb_mem_phys;
273 unsigned long fb_mem_len;
274 void *fb_mem_virt;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300275
Tony Priske7b99532012-08-03 20:58:22 +1200276 fbi = devm_kzalloc(&pdev->dev, sizeof(struct wm8505fb_info) +
277 sizeof(u32) * 16, GFP_KERNEL);
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300278 if (!fbi) {
279 dev_err(&pdev->dev, "Failed to initialize framebuffer device\n");
Julia Lawall9e340fd2013-04-03 07:20:36 +1300280 return -ENOMEM;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300281 }
282
283 strcpy(fbi->fb.fix.id, DRIVER_NAME);
284
285 fbi->fb.fix.type = FB_TYPE_PACKED_PIXELS;
286 fbi->fb.fix.xpanstep = 1;
287 fbi->fb.fix.ypanstep = 1;
288 fbi->fb.fix.ywrapstep = 0;
289 fbi->fb.fix.accel = FB_ACCEL_NONE;
290
291 fbi->fb.fbops = &wm8505fb_ops;
292 fbi->fb.flags = FBINFO_DEFAULT
293 | FBINFO_HWACCEL_COPYAREA
294 | FBINFO_HWACCEL_FILLRECT
295 | FBINFO_HWACCEL_XPAN
296 | FBINFO_HWACCEL_YPAN
297 | FBINFO_VIRTFB
298 | FBINFO_PARTIAL_PAN_OK;
299 fbi->fb.node = -1;
300
301 addr = fbi;
302 addr = addr + sizeof(struct wm8505fb_info);
303 fbi->fb.pseudo_palette = addr;
304
305 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Julia Lawall9e340fd2013-04-03 07:20:36 +1300306 fbi->regbase = devm_request_and_ioremap(&pdev->dev, res);
307 if (fbi->regbase == NULL)
308 return -EBUSY;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300309
Tony Prisk7ab0a482013-04-03 07:20:38 +1300310 disp_timing = of_get_display_timings(pdev->dev.of_node);
311 if (!disp_timing)
Julia Lawall9e340fd2013-04-03 07:20:36 +1300312 return -EINVAL;
Tony Priske7b99532012-08-03 20:58:22 +1200313
Tony Prisk7ab0a482013-04-03 07:20:38 +1300314 ret = of_get_fb_videomode(pdev->dev.of_node, &mode, OF_USE_NATIVE_MODE);
315 if (ret)
316 return ret;
Tony Priske7b99532012-08-03 20:58:22 +1200317
Tony Prisk7ab0a482013-04-03 07:20:38 +1300318 ret = of_property_read_u32(pdev->dev.of_node, "bits-per-pixel", &bpp);
319 if (ret)
320 return ret;
321
322 fb_videomode_to_var(&fbi->fb.var, &mode);
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300323
324 fbi->fb.var.nonstd = 0;
325 fbi->fb.var.activate = FB_ACTIVATE_NOW;
326
327 fbi->fb.var.height = -1;
328 fbi->fb.var.width = -1;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300329
Tony Priske7b99532012-08-03 20:58:22 +1200330 /* try allocating the framebuffer */
Tony Prisk7ab0a482013-04-03 07:20:38 +1300331 fb_mem_len = mode.xres * mode.yres * 2 * (bpp / 8);
Julia Lawall9e340fd2013-04-03 07:20:36 +1300332 fb_mem_virt = dmam_alloc_coherent(&pdev->dev, fb_mem_len, &fb_mem_phys,
Tony Priske7b99532012-08-03 20:58:22 +1200333 GFP_KERNEL);
334 if (!fb_mem_virt) {
335 pr_err("%s: Failed to allocate framebuffer\n", __func__);
336 return -ENOMEM;
Julia Lawall9e340fd2013-04-03 07:20:36 +1300337 }
Tony Priske7b99532012-08-03 20:58:22 +1200338
Tony Prisk7ab0a482013-04-03 07:20:38 +1300339 fbi->fb.var.xres_virtual = mode.xres;
340 fbi->fb.var.yres_virtual = mode.yres * 2;
Tony Priske7b99532012-08-03 20:58:22 +1200341 fbi->fb.var.bits_per_pixel = bpp;
342
343 fbi->fb.fix.smem_start = fb_mem_phys;
344 fbi->fb.fix.smem_len = fb_mem_len;
345 fbi->fb.screen_base = fb_mem_virt;
346 fbi->fb.screen_size = fb_mem_len;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300347
Tony Priskde68efc2013-04-03 07:20:37 +1300348 fbi->contrast = 0x10;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300349 ret = wm8505fb_set_par(&fbi->fb);
350 if (ret) {
351 dev_err(&pdev->dev, "Failed to set parameters\n");
Julia Lawall9e340fd2013-04-03 07:20:36 +1300352 return ret;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300353 }
354
Julia Lawall9e340fd2013-04-03 07:20:36 +1300355 if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
356 dev_err(&pdev->dev, "Failed to allocate color map\n");
357 return -ENOMEM;
358 }
359
360 wm8505fb_init_hw(&fbi->fb);
361
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300362 platform_set_drvdata(pdev, fbi);
363
364 ret = register_framebuffer(&fbi->fb);
365 if (ret < 0) {
366 dev_err(&pdev->dev,
367 "Failed to register framebuffer device: %d\n", ret);
Julia Lawall9e340fd2013-04-03 07:20:36 +1300368 if (fbi->fb.cmap.len)
369 fb_dealloc_cmap(&fbi->fb.cmap);
370 return ret;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300371 }
372
373 ret = device_create_file(&pdev->dev, &dev_attr_contrast);
374 if (ret < 0) {
375 printk(KERN_WARNING "fb%d: failed to register attributes (%d)\n",
376 fbi->fb.node, ret);
377 }
378
379 printk(KERN_INFO "fb%d: %s frame buffer at 0x%lx-0x%lx\n",
380 fbi->fb.node, fbi->fb.fix.id, fbi->fb.fix.smem_start,
381 fbi->fb.fix.smem_start + fbi->fb.fix.smem_len - 1);
382
383 return 0;
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300384}
385
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800386static int wm8505fb_remove(struct platform_device *pdev)
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300387{
388 struct wm8505fb_info *fbi = platform_get_drvdata(pdev);
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300389
390 device_remove_file(&pdev->dev, &dev_attr_contrast);
391
392 unregister_framebuffer(&fbi->fb);
393
394 writel(0, fbi->regbase);
395
396 if (fbi->fb.cmap.len)
397 fb_dealloc_cmap(&fbi->fb.cmap);
398
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300399 return 0;
400}
401
Tony Priske7b99532012-08-03 20:58:22 +1200402static const struct of_device_id wmt_dt_ids[] = {
403 { .compatible = "wm,wm8505-fb", },
404 {}
405};
406
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300407static struct platform_driver wm8505fb_driver = {
408 .probe = wm8505fb_probe,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800409 .remove = wm8505fb_remove,
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300410 .driver = {
411 .owner = THIS_MODULE,
412 .name = DRIVER_NAME,
Tony Priske7b99532012-08-03 20:58:22 +1200413 .of_match_table = of_match_ptr(wmt_dt_ids),
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300414 },
415};
416
Axel Lin4277f2c2011-11-26 10:25:54 +0800417module_platform_driver(wm8505fb_driver);
Alexey Charkovd6ff7d02010-11-09 02:42:39 +0300418
419MODULE_AUTHOR("Ed Spiridonov <edo.rus@gmail.com>");
420MODULE_DESCRIPTION("Framebuffer driver for WMT WM8505");
Tony Priske7b99532012-08-03 20:58:22 +1200421MODULE_LICENSE("GPL v2");
422MODULE_DEVICE_TABLE(of, wmt_dt_ids);