xref: /linux/drivers/video/fbdev/clps711x-fb.c (revision bb9707077b4ee5f77bc9939b057ff8a0d410296f)
1 /*
2  * Cirrus Logic CLPS711X FB driver
3  *
4  * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
5  * Based on driver by Russell King <rmk@arm.linux.org.uk>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12 
13 #include <linux/clk.h>
14 #include <linux/fb.h>
15 #include <linux/io.h>
16 #include <linux/lcd.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/mfd/syscon.h>
22 #include <linux/mfd/syscon/clps711x.h>
23 #include <linux/regulator/consumer.h>
24 #include <video/of_display_timing.h>
25 
26 #define CLPS711X_FB_NAME	"clps711x-fb"
27 #define CLPS711X_FB_BPP_MAX	(4)
28 
29 /* Registers relative to LCDCON */
30 #define CLPS711X_LCDCON		(0x0000)
31 # define LCDCON_GSEN		BIT(30)
32 # define LCDCON_GSMD		BIT(31)
33 #define CLPS711X_PALLSW		(0x0280)
34 #define CLPS711X_PALMSW		(0x02c0)
35 #define CLPS711X_FBADDR		(0x0d40)
36 
37 struct clps711x_fb_info {
38 	struct clk		*clk;
39 	void __iomem		*base;
40 	struct regmap		*syscon;
41 	resource_size_t		buffsize;
42 	struct fb_videomode	mode;
43 	struct regulator	*lcd_pwr;
44 	u32			ac_prescale;
45 	bool			cmap_invert;
46 };
47 
48 static int clps711x_fb_setcolreg(u_int regno, u_int red, u_int green,
49 				 u_int blue, u_int transp, struct fb_info *info)
50 {
51 	struct clps711x_fb_info *cfb = info->par;
52 	u32 level, mask, shift;
53 
54 	if (regno >= BIT(info->var.bits_per_pixel))
55 		return -EINVAL;
56 
57 	shift = 4 * (regno & 7);
58 	mask  = 0xf << shift;
59 	/* gray = 0.30*R + 0.58*G + 0.11*B */
60 	level = (((red * 77 + green * 151 + blue * 28) >> 20) << shift) & mask;
61 	if (cfb->cmap_invert)
62 		level = 0xf - level;
63 
64 	regno = (regno < 8) ? CLPS711X_PALLSW : CLPS711X_PALMSW;
65 
66 	writel((readl(cfb->base + regno) & ~mask) | level, cfb->base + regno);
67 
68 	return 0;
69 }
70 
71 static int clps711x_fb_check_var(struct fb_var_screeninfo *var,
72 				 struct fb_info *info)
73 {
74 	u32 val;
75 
76 	if (var->bits_per_pixel < 1 ||
77 	    var->bits_per_pixel > CLPS711X_FB_BPP_MAX)
78 		return -EINVAL;
79 
80 	if (!var->pixclock)
81 		return -EINVAL;
82 
83 	val = DIV_ROUND_UP(var->xres, 16) - 1;
84 	if (val < 0x01 || val > 0x3f)
85 		return -EINVAL;
86 
87 	val = DIV_ROUND_UP(var->yres * var->xres * var->bits_per_pixel, 128);
88 	val--;
89 	if (val < 0x001 || val > 0x1fff)
90 		return -EINVAL;
91 
92 	var->transp.msb_right	= 0;
93 	var->transp.offset	= 0;
94 	var->transp.length	= 0;
95 	var->red.msb_right	= 0;
96 	var->red.offset		= 0;
97 	var->red.length		= var->bits_per_pixel;
98 	var->green		= var->red;
99 	var->blue		= var->red;
100 	var->grayscale		= var->bits_per_pixel > 1;
101 
102 	return 0;
103 }
104 
105 static int clps711x_fb_set_par(struct fb_info *info)
106 {
107 	struct clps711x_fb_info *cfb = info->par;
108 	resource_size_t size;
109 	u32 lcdcon, pps;
110 
111 	size = (info->var.xres * info->var.yres * info->var.bits_per_pixel) / 8;
112 	if (size > cfb->buffsize)
113 		return -EINVAL;
114 
115 	switch (info->var.bits_per_pixel) {
116 	case 1:
117 		info->fix.visual = FB_VISUAL_MONO01;
118 		break;
119 	case 2:
120 	case 4:
121 		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
122 		break;
123 	default:
124 		return -EINVAL;
125 	}
126 
127 	info->fix.line_length = info->var.xres * info->var.bits_per_pixel / 8;
128 	info->fix.smem_len = size;
129 
130 	lcdcon = (info->var.xres * info->var.yres *
131 		  info->var.bits_per_pixel) / 128 - 1;
132 	lcdcon |= ((info->var.xres / 16) - 1) << 13;
133 	lcdcon |= (cfb->ac_prescale & 0x1f) << 25;
134 
135 	pps = clk_get_rate(cfb->clk) / (PICOS2KHZ(info->var.pixclock) * 1000);
136 	if (pps)
137 		pps--;
138 	lcdcon |= (pps & 0x3f) << 19;
139 
140 	if (info->var.bits_per_pixel == 4)
141 		lcdcon |= LCDCON_GSMD;
142 	if (info->var.bits_per_pixel >= 2)
143 		lcdcon |= LCDCON_GSEN;
144 
145 	/* LCDCON must only be changed while the LCD is disabled */
146 	regmap_update_bits(cfb->syscon, SYSCON_OFFSET, SYSCON1_LCDEN, 0);
147 	writel(lcdcon, cfb->base + CLPS711X_LCDCON);
148 	regmap_update_bits(cfb->syscon, SYSCON_OFFSET,
149 			   SYSCON1_LCDEN, SYSCON1_LCDEN);
150 
151 	return 0;
152 }
153 
154 static int clps711x_fb_blank(int blank, struct fb_info *info)
155 {
156 	/* Return happy */
157 	return 0;
158 }
159 
160 static struct fb_ops clps711x_fb_ops = {
161 	.owner		= THIS_MODULE,
162 	.fb_setcolreg	= clps711x_fb_setcolreg,
163 	.fb_check_var	= clps711x_fb_check_var,
164 	.fb_set_par	= clps711x_fb_set_par,
165 	.fb_blank	= clps711x_fb_blank,
166 	.fb_fillrect	= sys_fillrect,
167 	.fb_copyarea	= sys_copyarea,
168 	.fb_imageblit	= sys_imageblit,
169 };
170 
171 static int clps711x_lcd_check_fb(struct lcd_device *lcddev, struct fb_info *fi)
172 {
173 	struct clps711x_fb_info *cfb = dev_get_drvdata(&lcddev->dev);
174 
175 	return (!fi || fi->par == cfb) ? 1 : 0;
176 }
177 
178 static int clps711x_lcd_get_power(struct lcd_device *lcddev)
179 {
180 	struct clps711x_fb_info *cfb = dev_get_drvdata(&lcddev->dev);
181 
182 	if (!IS_ERR_OR_NULL(cfb->lcd_pwr))
183 		if (!regulator_is_enabled(cfb->lcd_pwr))
184 			return FB_BLANK_NORMAL;
185 
186 	return FB_BLANK_UNBLANK;
187 }
188 
189 static int clps711x_lcd_set_power(struct lcd_device *lcddev, int blank)
190 {
191 	struct clps711x_fb_info *cfb = dev_get_drvdata(&lcddev->dev);
192 
193 	if (!IS_ERR_OR_NULL(cfb->lcd_pwr)) {
194 		if (blank == FB_BLANK_UNBLANK) {
195 			if (!regulator_is_enabled(cfb->lcd_pwr))
196 				return regulator_enable(cfb->lcd_pwr);
197 		} else {
198 			if (regulator_is_enabled(cfb->lcd_pwr))
199 				return regulator_disable(cfb->lcd_pwr);
200 		}
201 	}
202 
203 	return 0;
204 }
205 
206 static struct lcd_ops clps711x_lcd_ops = {
207 	.check_fb	= clps711x_lcd_check_fb,
208 	.get_power	= clps711x_lcd_get_power,
209 	.set_power	= clps711x_lcd_set_power,
210 };
211 
212 static int clps711x_fb_probe(struct platform_device *pdev)
213 {
214 	struct device *dev = &pdev->dev;
215 	struct device_node *disp, *np = dev->of_node;
216 	struct clps711x_fb_info *cfb;
217 	struct lcd_device *lcd;
218 	struct fb_info *info;
219 	struct resource *res;
220 	int ret = -ENOENT;
221 	u32 val;
222 
223 	if (fb_get_options(CLPS711X_FB_NAME, NULL))
224 		return -ENODEV;
225 
226 	info = framebuffer_alloc(sizeof(*cfb), dev);
227 	if (!info)
228 		return -ENOMEM;
229 
230 	cfb = info->par;
231 	platform_set_drvdata(pdev, info);
232 
233 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
234 	if (!res)
235 		goto out_fb_release;
236 	cfb->base = devm_ioremap(dev, res->start, resource_size(res));
237 	if (!cfb->base) {
238 		ret = -ENOMEM;
239 		goto out_fb_release;
240 	}
241 
242 	info->fix.mmio_start = res->start;
243 	info->fix.mmio_len = resource_size(res);
244 
245 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
246 	info->screen_base = devm_ioremap_resource(dev, res);
247 	if (IS_ERR(info->screen_base)) {
248 		ret = PTR_ERR(info->screen_base);
249 		goto out_fb_release;
250 	}
251 
252 	/* Physical address should be aligned to 256 MiB */
253 	if (res->start & 0x0fffffff) {
254 		ret = -EINVAL;
255 		goto out_fb_release;
256 	}
257 
258 	info->apertures = alloc_apertures(1);
259 	if (!info->apertures) {
260 		ret = -ENOMEM;
261 		goto out_fb_release;
262 	}
263 
264 	cfb->buffsize = resource_size(res);
265 	info->fix.smem_start = res->start;
266 	info->apertures->ranges[0].base = info->fix.smem_start;
267 	info->apertures->ranges[0].size = cfb->buffsize;
268 
269 	cfb->clk = devm_clk_get(dev, NULL);
270 	if (IS_ERR(cfb->clk)) {
271 		ret = PTR_ERR(cfb->clk);
272 		goto out_fb_release;
273 	}
274 
275 	cfb->syscon =
276 		syscon_regmap_lookup_by_compatible("cirrus,clps711x-syscon1");
277 	if (IS_ERR(cfb->syscon)) {
278 		ret = PTR_ERR(cfb->syscon);
279 		goto out_fb_release;
280 	}
281 
282 	disp = of_parse_phandle(np, "display", 0);
283 	if (!disp) {
284 		dev_err(&pdev->dev, "No display defined\n");
285 		ret = -ENODATA;
286 		goto out_fb_release;
287 	}
288 
289 	ret = of_get_fb_videomode(disp, &cfb->mode, OF_USE_NATIVE_MODE);
290 	if (ret)
291 		goto out_fb_release;
292 
293 	of_property_read_u32(disp, "ac-prescale", &cfb->ac_prescale);
294 	cfb->cmap_invert = of_property_read_bool(disp, "cmap-invert");
295 
296 	ret = of_property_read_u32(disp, "bits-per-pixel",
297 				   &info->var.bits_per_pixel);
298 	if (ret)
299 		goto out_fb_release;
300 
301 	/* Force disable LCD on any mismatch */
302 	if (info->fix.smem_start != (readb(cfb->base + CLPS711X_FBADDR) << 28))
303 		regmap_update_bits(cfb->syscon, SYSCON_OFFSET,
304 				   SYSCON1_LCDEN, 0);
305 
306 	ret = regmap_read(cfb->syscon, SYSCON_OFFSET, &val);
307 	if (ret)
308 		goto out_fb_release;
309 
310 	if (!(val & SYSCON1_LCDEN)) {
311 		/* Setup start FB address */
312 		writeb(info->fix.smem_start >> 28, cfb->base + CLPS711X_FBADDR);
313 		/* Clean FB memory */
314 		memset_io(info->screen_base, 0, cfb->buffsize);
315 	}
316 
317 	cfb->lcd_pwr = devm_regulator_get(dev, "lcd");
318 	if (PTR_ERR(cfb->lcd_pwr) == -EPROBE_DEFER) {
319 		ret = -EPROBE_DEFER;
320 		goto out_fb_release;
321 	}
322 
323 	info->fbops = &clps711x_fb_ops;
324 	info->flags = FBINFO_DEFAULT;
325 	info->var.activate = FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW;
326 	info->var.height = -1;
327 	info->var.width = -1;
328 	info->var.vmode = FB_VMODE_NONINTERLACED;
329 	info->fix.type = FB_TYPE_PACKED_PIXELS;
330 	info->fix.accel = FB_ACCEL_NONE;
331 	strlcpy(info->fix.id, CLPS711X_FB_NAME, sizeof(info->fix.id));
332 	fb_videomode_to_var(&info->var, &cfb->mode);
333 
334 	ret = fb_alloc_cmap(&info->cmap, BIT(CLPS711X_FB_BPP_MAX), 0);
335 	if (ret)
336 		goto out_fb_release;
337 
338 	ret = fb_set_var(info, &info->var);
339 	if (ret)
340 		goto out_fb_dealloc_cmap;
341 
342 	ret = register_framebuffer(info);
343 	if (ret)
344 		goto out_fb_dealloc_cmap;
345 
346 	lcd = devm_lcd_device_register(dev, "clps711x-lcd", dev, cfb,
347 				       &clps711x_lcd_ops);
348 	if (!IS_ERR(lcd))
349 		return 0;
350 
351 	ret = PTR_ERR(lcd);
352 	unregister_framebuffer(info);
353 
354 out_fb_dealloc_cmap:
355 	regmap_update_bits(cfb->syscon, SYSCON_OFFSET, SYSCON1_LCDEN, 0);
356 	fb_dealloc_cmap(&info->cmap);
357 
358 out_fb_release:
359 	framebuffer_release(info);
360 
361 	return ret;
362 }
363 
364 static int clps711x_fb_remove(struct platform_device *pdev)
365 {
366 	struct fb_info *info = platform_get_drvdata(pdev);
367 	struct clps711x_fb_info *cfb = info->par;
368 
369 	regmap_update_bits(cfb->syscon, SYSCON_OFFSET, SYSCON1_LCDEN, 0);
370 
371 	unregister_framebuffer(info);
372 	fb_dealloc_cmap(&info->cmap);
373 	framebuffer_release(info);
374 
375 	return 0;
376 }
377 
378 static const struct of_device_id clps711x_fb_dt_ids[] = {
379 	{ .compatible = "cirrus,clps711x-fb", },
380 	{ }
381 };
382 MODULE_DEVICE_TABLE(of, clps711x_fb_dt_ids);
383 
384 static struct platform_driver clps711x_fb_driver = {
385 	.driver	= {
386 		.name		= CLPS711X_FB_NAME,
387 		.of_match_table	= clps711x_fb_dt_ids,
388 	},
389 	.probe	= clps711x_fb_probe,
390 	.remove	= clps711x_fb_remove,
391 };
392 module_platform_driver(clps711x_fb_driver);
393 
394 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
395 MODULE_DESCRIPTION("Cirrus Logic CLPS711X FB driver");
396 MODULE_LICENSE("GPL");
397