xref: /openbmc/linux/drivers/video/fbdev/wm8505fb.c (revision a347279d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  WonderMedia WM8505 Frame Buffer device driver
4  *
5  *  Copyright (C) 2010 Ed Spiridonov <edo.rus@gmail.com>
6  *    Based on vt8500lcdfb.c
7  */
8 
9 #include <linux/delay.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/fb.h>
12 #include <linux/errno.h>
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/kernel.h>
18 #include <linux/memblock.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/of_fdt.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/wait.h>
27 #include <video/of_display_timing.h>
28 
29 #include "wm8505fb_regs.h"
30 #include "wmt_ge_rops.h"
31 
32 #define DRIVER_NAME "wm8505-fb"
33 
34 #define to_wm8505fb_info(__info) container_of(__info, \
35 						struct wm8505fb_info, fb)
36 struct wm8505fb_info {
37 	struct fb_info		fb;
38 	void __iomem		*regbase;
39 	unsigned int		contrast;
40 };
41 
42 
43 static int wm8505fb_init_hw(struct fb_info *info)
44 {
45 	struct wm8505fb_info *fbi = to_wm8505fb_info(info);
46 
47 	int i;
48 
49 	/* I know the purpose only of few registers, so clear unknown */
50 	for (i = 0; i < 0x200; i += 4)
51 		writel(0, fbi->regbase + i);
52 
53 	/* Set frame buffer address */
54 	writel(fbi->fb.fix.smem_start, fbi->regbase + WMT_GOVR_FBADDR);
55 	writel(fbi->fb.fix.smem_start, fbi->regbase + WMT_GOVR_FBADDR1);
56 
57 	/*
58 	 * Set in-memory picture format to RGB
59 	 * 0x31C sets the correct color mode (RGB565) for WM8650
60 	 * Bit 8+9 (0x300) are ignored on WM8505 as reserved
61 	 */
62 	writel(0x31c,		       fbi->regbase + WMT_GOVR_COLORSPACE);
63 	writel(1,		       fbi->regbase + WMT_GOVR_COLORSPACE1);
64 
65 	/* Virtual buffer size */
66 	writel(info->var.xres,	       fbi->regbase + WMT_GOVR_XRES);
67 	writel(info->var.xres_virtual, fbi->regbase + WMT_GOVR_XRES_VIRTUAL);
68 
69 	/* black magic ;) */
70 	writel(0xf,		       fbi->regbase + WMT_GOVR_FHI);
71 	writel(4,		       fbi->regbase + WMT_GOVR_DVO_SET);
72 	writel(1,		       fbi->regbase + WMT_GOVR_MIF_ENABLE);
73 	writel(1,		       fbi->regbase + WMT_GOVR_REG_UPDATE);
74 
75 	return 0;
76 }
77 
78 static int wm8505fb_set_timing(struct fb_info *info)
79 {
80 	struct wm8505fb_info *fbi = to_wm8505fb_info(info);
81 
82 	int h_start = info->var.left_margin;
83 	int h_end = h_start + info->var.xres;
84 	int h_all = h_end + info->var.right_margin;
85 	int h_sync = info->var.hsync_len;
86 
87 	int v_start = info->var.upper_margin;
88 	int v_end = v_start + info->var.yres;
89 	int v_all = v_end + info->var.lower_margin;
90 	int v_sync = info->var.vsync_len;
91 
92 	writel(0, fbi->regbase + WMT_GOVR_TG);
93 
94 	writel(h_start, fbi->regbase + WMT_GOVR_TIMING_H_START);
95 	writel(h_end,   fbi->regbase + WMT_GOVR_TIMING_H_END);
96 	writel(h_all,   fbi->regbase + WMT_GOVR_TIMING_H_ALL);
97 	writel(h_sync,  fbi->regbase + WMT_GOVR_TIMING_H_SYNC);
98 
99 	writel(v_start, fbi->regbase + WMT_GOVR_TIMING_V_START);
100 	writel(v_end,   fbi->regbase + WMT_GOVR_TIMING_V_END);
101 	writel(v_all,   fbi->regbase + WMT_GOVR_TIMING_V_ALL);
102 	writel(v_sync,  fbi->regbase + WMT_GOVR_TIMING_V_SYNC);
103 
104 	writel(1, fbi->regbase + WMT_GOVR_TG);
105 
106 	return 0;
107 }
108 
109 
110 static int wm8505fb_set_par(struct fb_info *info)
111 {
112 	struct wm8505fb_info *fbi = to_wm8505fb_info(info);
113 
114 	if (!fbi)
115 		return -EINVAL;
116 
117 	if (info->var.bits_per_pixel == 32) {
118 		info->var.red.offset = 16;
119 		info->var.red.length = 8;
120 		info->var.red.msb_right = 0;
121 		info->var.green.offset = 8;
122 		info->var.green.length = 8;
123 		info->var.green.msb_right = 0;
124 		info->var.blue.offset = 0;
125 		info->var.blue.length = 8;
126 		info->var.blue.msb_right = 0;
127 		info->fix.visual = FB_VISUAL_TRUECOLOR;
128 		info->fix.line_length = info->var.xres_virtual << 2;
129 	} else if (info->var.bits_per_pixel == 16) {
130 		info->var.red.offset = 11;
131 		info->var.red.length = 5;
132 		info->var.red.msb_right = 0;
133 		info->var.green.offset = 5;
134 		info->var.green.length = 6;
135 		info->var.green.msb_right = 0;
136 		info->var.blue.offset = 0;
137 		info->var.blue.length = 5;
138 		info->var.blue.msb_right = 0;
139 		info->fix.visual = FB_VISUAL_TRUECOLOR;
140 		info->fix.line_length = info->var.xres_virtual << 1;
141 	}
142 
143 	wm8505fb_set_timing(info);
144 
145 	writel(fbi->contrast<<16 | fbi->contrast<<8 | fbi->contrast,
146 		fbi->regbase + WMT_GOVR_CONTRAST);
147 
148 	return 0;
149 }
150 
151 static ssize_t contrast_show(struct device *dev,
152 			     struct device_attribute *attr, char *buf)
153 {
154 	struct fb_info *info = dev_get_drvdata(dev);
155 	struct wm8505fb_info *fbi = to_wm8505fb_info(info);
156 
157 	return sprintf(buf, "%u\n", fbi->contrast);
158 }
159 
160 static ssize_t contrast_store(struct device *dev,
161 			      struct device_attribute *attr,
162 			      const char *buf, size_t count)
163 {
164 	struct fb_info *info = dev_get_drvdata(dev);
165 	struct wm8505fb_info *fbi = to_wm8505fb_info(info);
166 	unsigned long tmp;
167 
168 	if (kstrtoul(buf, 10, &tmp) || (tmp > 0xff))
169 		return -EINVAL;
170 	fbi->contrast = tmp;
171 
172 	wm8505fb_set_par(info);
173 
174 	return count;
175 }
176 
177 static DEVICE_ATTR_RW(contrast);
178 
179 static struct attribute *wm8505fb_attrs[] = {
180 	&dev_attr_contrast.attr,
181 	NULL,
182 };
183 ATTRIBUTE_GROUPS(wm8505fb);
184 
185 static 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 
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);
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 
223 static 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 
233 static 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 
249 static const 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 
261 static int wm8505fb_probe(struct platform_device *pdev)
262 {
263 	struct wm8505fb_info	*fbi;
264 	struct display_timings *disp_timing;
265 	void			*addr;
266 	int ret;
267 
268 	struct fb_videomode	mode;
269 	u32			bpp;
270 	dma_addr_t fb_mem_phys;
271 	unsigned long fb_mem_len;
272 	void *fb_mem_virt;
273 
274 	fbi = devm_kzalloc(&pdev->dev, sizeof(struct wm8505fb_info) +
275 			sizeof(u32) * 16, GFP_KERNEL);
276 	if (!fbi)
277 		return -ENOMEM;
278 
279 	strcpy(fbi->fb.fix.id, DRIVER_NAME);
280 
281 	fbi->fb.fix.type	= FB_TYPE_PACKED_PIXELS;
282 	fbi->fb.fix.xpanstep	= 1;
283 	fbi->fb.fix.ypanstep	= 1;
284 	fbi->fb.fix.ywrapstep	= 0;
285 	fbi->fb.fix.accel	= FB_ACCEL_NONE;
286 
287 	fbi->fb.fbops		= &wm8505fb_ops;
288 	fbi->fb.flags		= FBINFO_DEFAULT
289 				| FBINFO_HWACCEL_COPYAREA
290 				| FBINFO_HWACCEL_FILLRECT
291 				| FBINFO_HWACCEL_XPAN
292 				| FBINFO_HWACCEL_YPAN
293 				| FBINFO_VIRTFB
294 				| FBINFO_PARTIAL_PAN_OK;
295 	fbi->fb.node		= -1;
296 
297 	addr = fbi;
298 	addr = addr + sizeof(struct wm8505fb_info);
299 	fbi->fb.pseudo_palette	= addr;
300 
301 	fbi->regbase = devm_platform_ioremap_resource(pdev, 0);
302 	if (IS_ERR(fbi->regbase))
303 		return PTR_ERR(fbi->regbase);
304 
305 	disp_timing = of_get_display_timings(pdev->dev.of_node);
306 	if (!disp_timing)
307 		return -EINVAL;
308 
309 	ret = of_get_fb_videomode(pdev->dev.of_node, &mode, OF_USE_NATIVE_MODE);
310 	if (ret)
311 		return ret;
312 
313 	ret = of_property_read_u32(pdev->dev.of_node, "bits-per-pixel", &bpp);
314 	if (ret)
315 		return ret;
316 
317 	fb_videomode_to_var(&fbi->fb.var, &mode);
318 
319 	fbi->fb.var.nonstd		= 0;
320 	fbi->fb.var.activate		= FB_ACTIVATE_NOW;
321 
322 	fbi->fb.var.height		= -1;
323 	fbi->fb.var.width		= -1;
324 
325 	/* try allocating the framebuffer */
326 	fb_mem_len = mode.xres * mode.yres * 2 * (bpp / 8);
327 	fb_mem_virt = dmam_alloc_coherent(&pdev->dev, fb_mem_len, &fb_mem_phys,
328 				GFP_KERNEL);
329 	if (!fb_mem_virt) {
330 		pr_err("%s: Failed to allocate framebuffer\n", __func__);
331 		return -ENOMEM;
332 	}
333 
334 	fbi->fb.var.xres_virtual	= mode.xres;
335 	fbi->fb.var.yres_virtual	= mode.yres * 2;
336 	fbi->fb.var.bits_per_pixel	= bpp;
337 
338 	fbi->fb.fix.smem_start		= fb_mem_phys;
339 	fbi->fb.fix.smem_len		= fb_mem_len;
340 	fbi->fb.screen_buffer		= fb_mem_virt;
341 	fbi->fb.screen_size		= fb_mem_len;
342 
343 	fbi->contrast = 0x10;
344 	ret = wm8505fb_set_par(&fbi->fb);
345 	if (ret) {
346 		dev_err(&pdev->dev, "Failed to set parameters\n");
347 		return ret;
348 	}
349 
350 	if (fb_alloc_cmap(&fbi->fb.cmap, 256, 0) < 0) {
351 		dev_err(&pdev->dev, "Failed to allocate color map\n");
352 		return -ENOMEM;
353 	}
354 
355 	wm8505fb_init_hw(&fbi->fb);
356 
357 	platform_set_drvdata(pdev, fbi);
358 
359 	ret = register_framebuffer(&fbi->fb);
360 	if (ret < 0) {
361 		dev_err(&pdev->dev,
362 			"Failed to register framebuffer device: %d\n", ret);
363 		if (fbi->fb.cmap.len)
364 			fb_dealloc_cmap(&fbi->fb.cmap);
365 		return ret;
366 	}
367 
368 	fb_info(&fbi->fb, "%s frame buffer at 0x%lx-0x%lx\n",
369 		fbi->fb.fix.id, fbi->fb.fix.smem_start,
370 		fbi->fb.fix.smem_start + fbi->fb.fix.smem_len - 1);
371 
372 	return 0;
373 }
374 
375 static int wm8505fb_remove(struct platform_device *pdev)
376 {
377 	struct wm8505fb_info *fbi = platform_get_drvdata(pdev);
378 
379 	unregister_framebuffer(&fbi->fb);
380 
381 	writel(0, fbi->regbase);
382 
383 	if (fbi->fb.cmap.len)
384 		fb_dealloc_cmap(&fbi->fb.cmap);
385 
386 	return 0;
387 }
388 
389 static const struct of_device_id wmt_dt_ids[] = {
390 	{ .compatible = "wm,wm8505-fb", },
391 	{}
392 };
393 
394 static struct platform_driver wm8505fb_driver = {
395 	.probe		= wm8505fb_probe,
396 	.remove		= wm8505fb_remove,
397 	.driver		= {
398 		.name	= DRIVER_NAME,
399 		.of_match_table = wmt_dt_ids,
400 		.dev_groups	= wm8505fb_groups,
401 	},
402 };
403 
404 module_platform_driver(wm8505fb_driver);
405 
406 MODULE_AUTHOR("Ed Spiridonov <edo.rus@gmail.com>");
407 MODULE_DESCRIPTION("Framebuffer driver for WMT WM8505");
408 MODULE_LICENSE("GPL v2");
409 MODULE_DEVICE_TABLE(of, wmt_dt_ids);
410