Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/video/amba-clcd.c |
| 3 | * |
| 4 | * Copyright (C) 2001 ARM Limited, by David A Rusling |
| 5 | * Updated to 2.5, Deep Blue Solutions Ltd. |
| 6 | * |
| 7 | * This file is subject to the terms and conditions of the GNU General Public |
| 8 | * License. See the file COPYING in the main directory of this archive |
| 9 | * for more details. |
| 10 | * |
| 11 | * ARM PrimeCell PL110 Color LCD Controller |
| 12 | */ |
Russell King | e0a8ba2 | 2013-06-27 10:29:32 +0100 | [diff] [blame] | 13 | #include <linux/dma-mapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/string.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/fb.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/ioport.h> |
| 24 | #include <linux/list.h> |
Russell King | a62c80e | 2006-01-07 13:52:45 +0000 | [diff] [blame] | 25 | #include <linux/amba/bus.h> |
| 26 | #include <linux/amba/clcd.h> |
Jon Medhurst (Tixy) | 2b6c53b | 2014-08-20 13:41:04 +0100 | [diff] [blame] | 27 | #include <linux/bitops.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 28 | #include <linux/clk.h> |
Russell King | 934848d | 2009-01-08 09:58:51 +0000 | [diff] [blame] | 29 | #include <linux/hardirq.h> |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 30 | #include <linux/of.h> |
| 31 | #include <linux/of_address.h> |
| 32 | #include <linux/of_graph.h> |
Linus Walleij | c38162b | 2016-06-16 11:36:13 +0200 | [diff] [blame] | 33 | #include <linux/backlight.h> |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 34 | #include <video/display_timing.h> |
| 35 | #include <video/of_display_timing.h> |
| 36 | #include <video/videomode.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Walleij | 1d3f0cb | 2016-06-16 11:36:17 +0200 | [diff] [blame^] | 38 | #include "amba-clcd-nomadik.h" |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define to_clcd(info) container_of(info, struct clcd_fb, fb) |
| 41 | |
| 42 | /* This is limited to 16 characters when displayed by X startup */ |
| 43 | static const char *clcd_name = "CLCD FB"; |
| 44 | |
| 45 | /* |
| 46 | * Unfortunately, the enable/disable functions may be called either from |
| 47 | * process or IRQ context, and we _need_ to delay. This is _not_ good. |
| 48 | */ |
| 49 | static inline void clcdfb_sleep(unsigned int ms) |
| 50 | { |
| 51 | if (in_atomic()) { |
| 52 | mdelay(ms); |
| 53 | } else { |
| 54 | msleep(ms); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static inline void clcdfb_set_start(struct clcd_fb *fb) |
| 59 | { |
| 60 | unsigned long ustart = fb->fb.fix.smem_start; |
| 61 | unsigned long lstart; |
| 62 | |
| 63 | ustart += fb->fb.var.yoffset * fb->fb.fix.line_length; |
| 64 | lstart = ustart + fb->fb.var.yres * fb->fb.fix.line_length / 2; |
| 65 | |
| 66 | writel(ustart, fb->regs + CLCD_UBAS); |
| 67 | writel(lstart, fb->regs + CLCD_LBAS); |
| 68 | } |
| 69 | |
| 70 | static void clcdfb_disable(struct clcd_fb *fb) |
| 71 | { |
| 72 | u32 val; |
| 73 | |
| 74 | if (fb->board->disable) |
| 75 | fb->board->disable(fb); |
| 76 | |
Linus Walleij | c38162b | 2016-06-16 11:36:13 +0200 | [diff] [blame] | 77 | if (fb->panel->backlight) { |
| 78 | fb->panel->backlight->props.power = FB_BLANK_POWERDOWN; |
| 79 | backlight_update_status(fb->panel->backlight); |
| 80 | } |
| 81 | |
Russell King | 3f17522c | 2010-02-12 14:32:01 +0000 | [diff] [blame] | 82 | val = readl(fb->regs + fb->off_cntl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | if (val & CNTL_LCDPWR) { |
| 84 | val &= ~CNTL_LCDPWR; |
Russell King | 3f17522c | 2010-02-12 14:32:01 +0000 | [diff] [blame] | 85 | writel(val, fb->regs + fb->off_cntl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | clcdfb_sleep(20); |
| 88 | } |
| 89 | if (val & CNTL_LCDEN) { |
| 90 | val &= ~CNTL_LCDEN; |
Russell King | 3f17522c | 2010-02-12 14:32:01 +0000 | [diff] [blame] | 91 | writel(val, fb->regs + fb->off_cntl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Disable CLCD clock source. |
| 96 | */ |
Russell King | 99c796d | 2010-08-17 22:13:22 +0100 | [diff] [blame] | 97 | if (fb->clk_enabled) { |
| 98 | fb->clk_enabled = false; |
| 99 | clk_disable(fb->clk); |
| 100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static void clcdfb_enable(struct clcd_fb *fb, u32 cntl) |
| 104 | { |
| 105 | /* |
| 106 | * Enable the CLCD clock source. |
| 107 | */ |
Russell King | 99c796d | 2010-08-17 22:13:22 +0100 | [diff] [blame] | 108 | if (!fb->clk_enabled) { |
| 109 | fb->clk_enabled = true; |
| 110 | clk_enable(fb->clk); |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | * Bring up by first enabling.. |
| 115 | */ |
| 116 | cntl |= CNTL_LCDEN; |
Russell King | 3f17522c | 2010-02-12 14:32:01 +0000 | [diff] [blame] | 117 | writel(cntl, fb->regs + fb->off_cntl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
| 119 | clcdfb_sleep(20); |
| 120 | |
| 121 | /* |
| 122 | * and now apply power. |
| 123 | */ |
| 124 | cntl |= CNTL_LCDPWR; |
Russell King | 3f17522c | 2010-02-12 14:32:01 +0000 | [diff] [blame] | 125 | writel(cntl, fb->regs + fb->off_cntl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | /* |
Linus Walleij | c38162b | 2016-06-16 11:36:13 +0200 | [diff] [blame] | 128 | * Turn on backlight |
| 129 | */ |
| 130 | if (fb->panel->backlight) { |
| 131 | fb->panel->backlight->props.power = FB_BLANK_UNBLANK; |
| 132 | backlight_update_status(fb->panel->backlight); |
| 133 | } |
| 134 | |
| 135 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | * finally, enable the interface. |
| 137 | */ |
| 138 | if (fb->board->enable) |
| 139 | fb->board->enable(fb); |
| 140 | } |
| 141 | |
| 142 | static int |
| 143 | clcdfb_set_bitfields(struct clcd_fb *fb, struct fb_var_screeninfo *var) |
| 144 | { |
Russell King | 7b4e9ce | 2011-01-21 14:03:28 +0000 | [diff] [blame] | 145 | u32 caps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | int ret = 0; |
| 147 | |
Russell King | 7b4e9ce | 2011-01-21 14:03:28 +0000 | [diff] [blame] | 148 | if (fb->panel->caps && fb->board->caps) |
| 149 | caps = fb->panel->caps & fb->board->caps; |
| 150 | else { |
| 151 | /* Old way of specifying what can be used */ |
| 152 | caps = fb->panel->cntl & CNTL_BGR ? |
| 153 | CLCD_CAP_BGR : CLCD_CAP_RGB; |
| 154 | /* But mask out 444 modes as they weren't supported */ |
| 155 | caps &= ~CLCD_CAP_444; |
| 156 | } |
| 157 | |
| 158 | /* Only TFT panels can do RGB888/BGR888 */ |
| 159 | if (!(fb->panel->cntl & CNTL_LCDTFT)) |
| 160 | caps &= ~CLCD_CAP_888; |
| 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | memset(&var->transp, 0, sizeof(var->transp)); |
Russell King | c43e6f0 | 2006-01-26 14:12:06 +0000 | [diff] [blame] | 163 | |
| 164 | var->red.msb_right = 0; |
| 165 | var->green.msb_right = 0; |
| 166 | var->blue.msb_right = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | switch (var->bits_per_pixel) { |
| 169 | case 1: |
| 170 | case 2: |
| 171 | case 4: |
| 172 | case 8: |
Russell King | 7b4e9ce | 2011-01-21 14:03:28 +0000 | [diff] [blame] | 173 | /* If we can't do 5551, reject */ |
| 174 | caps &= CLCD_CAP_5551; |
| 175 | if (!caps) { |
| 176 | ret = -EINVAL; |
| 177 | break; |
| 178 | } |
| 179 | |
Russell King | c4d12b9 | 2005-04-28 10:38:19 +0100 | [diff] [blame] | 180 | var->red.length = var->bits_per_pixel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | var->red.offset = 0; |
Russell King | c4d12b9 | 2005-04-28 10:38:19 +0100 | [diff] [blame] | 182 | var->green.length = var->bits_per_pixel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | var->green.offset = 0; |
Russell King | c4d12b9 | 2005-04-28 10:38:19 +0100 | [diff] [blame] | 184 | var->blue.length = var->bits_per_pixel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | var->blue.offset = 0; |
| 186 | break; |
Russell King | 7b4e9ce | 2011-01-21 14:03:28 +0000 | [diff] [blame] | 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | case 16: |
Russell King | 7b4e9ce | 2011-01-21 14:03:28 +0000 | [diff] [blame] | 189 | /* If we can't do 444, 5551 or 565, reject */ |
| 190 | if (!(caps & (CLCD_CAP_444 | CLCD_CAP_5551 | CLCD_CAP_565))) { |
| 191 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | break; |
| 193 | } |
Russell King | 7b4e9ce | 2011-01-21 14:03:28 +0000 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * Green length can be 4, 5 or 6 depending whether |
| 197 | * we're operating in 444, 5551 or 565 mode. |
| 198 | */ |
| 199 | if (var->green.length == 4 && caps & CLCD_CAP_444) |
| 200 | caps &= CLCD_CAP_444; |
| 201 | if (var->green.length == 5 && caps & CLCD_CAP_5551) |
| 202 | caps &= CLCD_CAP_5551; |
| 203 | else if (var->green.length == 6 && caps & CLCD_CAP_565) |
| 204 | caps &= CLCD_CAP_565; |
| 205 | else { |
| 206 | /* |
| 207 | * PL110 officially only supports RGB555, |
| 208 | * but may be wired up to allow RGB565. |
| 209 | */ |
| 210 | if (caps & CLCD_CAP_565) { |
| 211 | var->green.length = 6; |
| 212 | caps &= CLCD_CAP_565; |
| 213 | } else if (caps & CLCD_CAP_5551) { |
| 214 | var->green.length = 5; |
| 215 | caps &= CLCD_CAP_5551; |
| 216 | } else { |
| 217 | var->green.length = 4; |
| 218 | caps &= CLCD_CAP_444; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (var->green.length >= 5) { |
| 223 | var->red.length = 5; |
| 224 | var->blue.length = 5; |
| 225 | } else { |
| 226 | var->red.length = 4; |
| 227 | var->blue.length = 4; |
| 228 | } |
| 229 | break; |
Linus Walleij | 046ad6c | 2016-06-16 11:36:16 +0200 | [diff] [blame] | 230 | case 24: |
| 231 | if (fb->vendor->packed_24_bit_pixels) { |
| 232 | var->red.length = 8; |
| 233 | var->green.length = 8; |
| 234 | var->blue.length = 8; |
| 235 | } else { |
| 236 | ret = -EINVAL; |
| 237 | } |
| 238 | break; |
Russell King | 7b4e9ce | 2011-01-21 14:03:28 +0000 | [diff] [blame] | 239 | case 32: |
| 240 | /* If we can't do 888, reject */ |
| 241 | caps &= CLCD_CAP_888; |
| 242 | if (!caps) { |
| 243 | ret = -EINVAL; |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | var->red.length = 8; |
| 248 | var->green.length = 8; |
| 249 | var->blue.length = 8; |
| 250 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | default: |
| 252 | ret = -EINVAL; |
| 253 | break; |
| 254 | } |
| 255 | |
Russell King | c43e6f0 | 2006-01-26 14:12:06 +0000 | [diff] [blame] | 256 | /* |
| 257 | * >= 16bpp displays have separate colour component bitfields |
| 258 | * encoded in the pixel data. Calculate their position from |
| 259 | * the bitfield length defined above. |
| 260 | */ |
| 261 | if (ret == 0 && var->bits_per_pixel >= 16) { |
Russell King | 7b4e9ce | 2011-01-21 14:03:28 +0000 | [diff] [blame] | 262 | bool bgr, rgb; |
| 263 | |
| 264 | bgr = caps & CLCD_CAP_BGR && var->blue.offset == 0; |
| 265 | rgb = caps & CLCD_CAP_RGB && var->red.offset == 0; |
| 266 | |
| 267 | if (!bgr && !rgb) |
| 268 | /* |
| 269 | * The requested format was not possible, try just |
| 270 | * our capabilities. One of BGR or RGB must be |
| 271 | * supported. |
| 272 | */ |
| 273 | bgr = caps & CLCD_CAP_BGR; |
| 274 | |
| 275 | if (bgr) { |
Russell King | c43e6f0 | 2006-01-26 14:12:06 +0000 | [diff] [blame] | 276 | var->blue.offset = 0; |
| 277 | var->green.offset = var->blue.offset + var->blue.length; |
| 278 | var->red.offset = var->green.offset + var->green.length; |
| 279 | } else { |
| 280 | var->red.offset = 0; |
| 281 | var->green.offset = var->red.offset + var->red.length; |
| 282 | var->blue.offset = var->green.offset + var->green.length; |
| 283 | } |
| 284 | } |
| 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | static int clcdfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info) |
| 290 | { |
| 291 | struct clcd_fb *fb = to_clcd(info); |
| 292 | int ret = -EINVAL; |
| 293 | |
| 294 | if (fb->board->check) |
| 295 | ret = fb->board->check(fb, var); |
Russell King | 82235e9 | 2005-04-28 10:43:52 +0100 | [diff] [blame] | 296 | |
| 297 | if (ret == 0 && |
| 298 | var->xres_virtual * var->bits_per_pixel / 8 * |
| 299 | var->yres_virtual > fb->fb.fix.smem_len) |
| 300 | ret = -EINVAL; |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | if (ret == 0) |
| 303 | ret = clcdfb_set_bitfields(fb, var); |
| 304 | |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | static int clcdfb_set_par(struct fb_info *info) |
| 309 | { |
| 310 | struct clcd_fb *fb = to_clcd(info); |
| 311 | struct clcd_regs regs; |
| 312 | |
| 313 | fb->fb.fix.line_length = fb->fb.var.xres_virtual * |
| 314 | fb->fb.var.bits_per_pixel / 8; |
| 315 | |
| 316 | if (fb->fb.var.bits_per_pixel <= 8) |
| 317 | fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR; |
| 318 | else |
| 319 | fb->fb.fix.visual = FB_VISUAL_TRUECOLOR; |
| 320 | |
| 321 | fb->board->decode(fb, ®s); |
| 322 | |
| 323 | clcdfb_disable(fb); |
| 324 | |
Linus Walleij | 046ad6c | 2016-06-16 11:36:16 +0200 | [diff] [blame] | 325 | /* Some variants must be clocked here */ |
| 326 | if (fb->vendor->clock_timregs && !fb->clk_enabled) { |
| 327 | fb->clk_enabled = true; |
| 328 | clk_enable(fb->clk); |
| 329 | } |
| 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | writel(regs.tim0, fb->regs + CLCD_TIM0); |
| 332 | writel(regs.tim1, fb->regs + CLCD_TIM1); |
| 333 | writel(regs.tim2, fb->regs + CLCD_TIM2); |
| 334 | writel(regs.tim3, fb->regs + CLCD_TIM3); |
| 335 | |
| 336 | clcdfb_set_start(fb); |
| 337 | |
| 338 | clk_set_rate(fb->clk, (1000000000 / regs.pixclock) * 1000); |
| 339 | |
| 340 | fb->clcd_cntl = regs.cntl; |
| 341 | |
| 342 | clcdfb_enable(fb, regs.cntl); |
| 343 | |
| 344 | #ifdef DEBUG |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 345 | printk(KERN_INFO |
| 346 | "CLCD: Registers set to\n" |
| 347 | " %08x %08x %08x %08x\n" |
| 348 | " %08x %08x %08x %08x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | readl(fb->regs + CLCD_TIM0), readl(fb->regs + CLCD_TIM1), |
| 350 | readl(fb->regs + CLCD_TIM2), readl(fb->regs + CLCD_TIM3), |
| 351 | readl(fb->regs + CLCD_UBAS), readl(fb->regs + CLCD_LBAS), |
Russell King | 3f17522c | 2010-02-12 14:32:01 +0000 | [diff] [blame] | 352 | readl(fb->regs + fb->off_ienb), readl(fb->regs + fb->off_cntl)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | #endif |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static inline u32 convert_bitfield(int val, struct fb_bitfield *bf) |
| 359 | { |
| 360 | unsigned int mask = (1 << bf->length) - 1; |
| 361 | |
| 362 | return (val >> (16 - bf->length) & mask) << bf->offset; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Set a single color register. The values supplied have a 16 bit |
| 367 | * magnitude. Return != 0 for invalid regno. |
| 368 | */ |
| 369 | static int |
| 370 | clcdfb_setcolreg(unsigned int regno, unsigned int red, unsigned int green, |
| 371 | unsigned int blue, unsigned int transp, struct fb_info *info) |
| 372 | { |
| 373 | struct clcd_fb *fb = to_clcd(info); |
| 374 | |
| 375 | if (regno < 16) |
| 376 | fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) | |
| 377 | convert_bitfield(blue, &fb->fb.var.blue) | |
| 378 | convert_bitfield(green, &fb->fb.var.green) | |
| 379 | convert_bitfield(red, &fb->fb.var.red); |
| 380 | |
Russell King | 1ddb8a1 | 2005-04-30 22:39:51 +0100 | [diff] [blame] | 381 | if (fb->fb.fix.visual == FB_VISUAL_PSEUDOCOLOR && regno < 256) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | int hw_reg = CLCD_PALETTE + ((regno * 2) & ~3); |
| 383 | u32 val, mask, newval; |
| 384 | |
| 385 | newval = (red >> 11) & 0x001f; |
| 386 | newval |= (green >> 6) & 0x03e0; |
| 387 | newval |= (blue >> 1) & 0x7c00; |
| 388 | |
| 389 | /* |
| 390 | * 3.2.11: if we're configured for big endian |
| 391 | * byte order, the palette entries are swapped. |
| 392 | */ |
| 393 | if (fb->clcd_cntl & CNTL_BEBO) |
| 394 | regno ^= 1; |
| 395 | |
| 396 | if (regno & 1) { |
| 397 | newval <<= 16; |
| 398 | mask = 0x0000ffff; |
| 399 | } else { |
| 400 | mask = 0xffff0000; |
| 401 | } |
| 402 | |
| 403 | val = readl(fb->regs + hw_reg) & mask; |
| 404 | writel(val | newval, fb->regs + hw_reg); |
| 405 | } |
| 406 | |
| 407 | return regno > 255; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Blank the screen if blank_mode != 0, else unblank. If blank == NULL |
| 412 | * then the caller blanks by setting the CLUT (Color Look Up Table) to all |
| 413 | * black. Return 0 if blanking succeeded, != 0 if un-/blanking failed due |
| 414 | * to e.g. a video mode which doesn't support it. Implements VESA suspend |
| 415 | * and powerdown modes on hardware that supports disabling hsync/vsync: |
| 416 | * blank_mode == 2: suspend vsync |
| 417 | * blank_mode == 3: suspend hsync |
| 418 | * blank_mode == 4: powerdown |
| 419 | */ |
| 420 | static int clcdfb_blank(int blank_mode, struct fb_info *info) |
| 421 | { |
| 422 | struct clcd_fb *fb = to_clcd(info); |
| 423 | |
| 424 | if (blank_mode != 0) { |
| 425 | clcdfb_disable(fb); |
| 426 | } else { |
| 427 | clcdfb_enable(fb, fb->clcd_cntl); |
| 428 | } |
| 429 | return 0; |
| 430 | } |
| 431 | |
Christoph Hellwig | 216d526 | 2006-01-14 13:21:25 -0800 | [diff] [blame] | 432 | static int clcdfb_mmap(struct fb_info *info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | struct vm_area_struct *vma) |
| 434 | { |
| 435 | struct clcd_fb *fb = to_clcd(info); |
| 436 | unsigned long len, off = vma->vm_pgoff << PAGE_SHIFT; |
| 437 | int ret = -EINVAL; |
| 438 | |
| 439 | len = info->fix.smem_len; |
| 440 | |
| 441 | if (off <= len && vma->vm_end - vma->vm_start <= len - off && |
| 442 | fb->board->mmap) |
| 443 | ret = fb->board->mmap(fb, vma); |
| 444 | |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | static struct fb_ops clcdfb_ops = { |
| 449 | .owner = THIS_MODULE, |
| 450 | .fb_check_var = clcdfb_check_var, |
| 451 | .fb_set_par = clcdfb_set_par, |
| 452 | .fb_setcolreg = clcdfb_setcolreg, |
| 453 | .fb_blank = clcdfb_blank, |
| 454 | .fb_fillrect = cfb_fillrect, |
| 455 | .fb_copyarea = cfb_copyarea, |
| 456 | .fb_imageblit = cfb_imageblit, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | .fb_mmap = clcdfb_mmap, |
| 458 | }; |
| 459 | |
| 460 | static int clcdfb_register(struct clcd_fb *fb) |
| 461 | { |
| 462 | int ret; |
| 463 | |
Russell King | 3f17522c | 2010-02-12 14:32:01 +0000 | [diff] [blame] | 464 | /* |
| 465 | * ARM PL111 always has IENB at 0x1c; it's only PL110 |
| 466 | * which is reversed on some platforms. |
| 467 | */ |
| 468 | if (amba_manf(fb->dev) == 0x41 && amba_part(fb->dev) == 0x111) { |
| 469 | fb->off_ienb = CLCD_PL111_IENB; |
| 470 | fb->off_cntl = CLCD_PL111_CNTL; |
| 471 | } else { |
Linus Walleij | f36fdac | 2016-02-23 11:01:38 +0100 | [diff] [blame] | 472 | if (of_machine_is_compatible("arm,versatile-ab") || |
| 473 | of_machine_is_compatible("arm,versatile-pb")) { |
| 474 | fb->off_ienb = CLCD_PL111_IENB; |
| 475 | fb->off_cntl = CLCD_PL111_CNTL; |
| 476 | } else { |
| 477 | fb->off_ienb = CLCD_PL110_IENB; |
| 478 | fb->off_cntl = CLCD_PL110_CNTL; |
| 479 | } |
Russell King | 3f17522c | 2010-02-12 14:32:01 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Russell King | ee569c4 | 2008-11-30 17:38:14 +0000 | [diff] [blame] | 482 | fb->clk = clk_get(&fb->dev->dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | if (IS_ERR(fb->clk)) { |
| 484 | ret = PTR_ERR(fb->clk); |
| 485 | goto out; |
| 486 | } |
| 487 | |
Russell King | 99df4ee | 2011-09-22 12:34:31 +0100 | [diff] [blame] | 488 | ret = clk_prepare(fb->clk); |
| 489 | if (ret) |
| 490 | goto free_clk; |
| 491 | |
Loïc Minier | 17e8c4e | 2011-06-20 20:44:17 +0000 | [diff] [blame] | 492 | fb->fb.device = &fb->dev->dev; |
| 493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | fb->fb.fix.mmio_start = fb->dev->res.start; |
Linus Walleij | dc890c2 | 2009-06-07 23:27:31 +0100 | [diff] [blame] | 495 | fb->fb.fix.mmio_len = resource_size(&fb->dev->res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
| 497 | fb->regs = ioremap(fb->fb.fix.mmio_start, fb->fb.fix.mmio_len); |
| 498 | if (!fb->regs) { |
| 499 | printk(KERN_ERR "CLCD: unable to remap registers\n"); |
| 500 | ret = -ENOMEM; |
Russell King | 99df4ee | 2011-09-22 12:34:31 +0100 | [diff] [blame] | 501 | goto clk_unprep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | fb->fb.fbops = &clcdfb_ops; |
| 505 | fb->fb.flags = FBINFO_FLAG_DEFAULT; |
| 506 | fb->fb.pseudo_palette = fb->cmap; |
| 507 | |
| 508 | strncpy(fb->fb.fix.id, clcd_name, sizeof(fb->fb.fix.id)); |
| 509 | fb->fb.fix.type = FB_TYPE_PACKED_PIXELS; |
| 510 | fb->fb.fix.type_aux = 0; |
| 511 | fb->fb.fix.xpanstep = 0; |
| 512 | fb->fb.fix.ypanstep = 0; |
| 513 | fb->fb.fix.ywrapstep = 0; |
| 514 | fb->fb.fix.accel = FB_ACCEL_NONE; |
| 515 | |
| 516 | fb->fb.var.xres = fb->panel->mode.xres; |
| 517 | fb->fb.var.yres = fb->panel->mode.yres; |
| 518 | fb->fb.var.xres_virtual = fb->panel->mode.xres; |
| 519 | fb->fb.var.yres_virtual = fb->panel->mode.yres; |
| 520 | fb->fb.var.bits_per_pixel = fb->panel->bpp; |
| 521 | fb->fb.var.grayscale = fb->panel->grayscale; |
| 522 | fb->fb.var.pixclock = fb->panel->mode.pixclock; |
| 523 | fb->fb.var.left_margin = fb->panel->mode.left_margin; |
| 524 | fb->fb.var.right_margin = fb->panel->mode.right_margin; |
| 525 | fb->fb.var.upper_margin = fb->panel->mode.upper_margin; |
| 526 | fb->fb.var.lower_margin = fb->panel->mode.lower_margin; |
| 527 | fb->fb.var.hsync_len = fb->panel->mode.hsync_len; |
| 528 | fb->fb.var.vsync_len = fb->panel->mode.vsync_len; |
| 529 | fb->fb.var.sync = fb->panel->mode.sync; |
| 530 | fb->fb.var.vmode = fb->panel->mode.vmode; |
| 531 | fb->fb.var.activate = FB_ACTIVATE_NOW; |
| 532 | fb->fb.var.nonstd = 0; |
| 533 | fb->fb.var.height = fb->panel->height; |
| 534 | fb->fb.var.width = fb->panel->width; |
| 535 | fb->fb.var.accel_flags = 0; |
| 536 | |
| 537 | fb->fb.monspecs.hfmin = 0; |
| 538 | fb->fb.monspecs.hfmax = 100000; |
| 539 | fb->fb.monspecs.vfmin = 0; |
| 540 | fb->fb.monspecs.vfmax = 400; |
| 541 | fb->fb.monspecs.dclkmin = 1000000; |
| 542 | fb->fb.monspecs.dclkmax = 100000000; |
| 543 | |
| 544 | /* |
| 545 | * Make sure that the bitfields are set appropriately. |
| 546 | */ |
| 547 | clcdfb_set_bitfields(fb, &fb->fb.var); |
| 548 | |
| 549 | /* |
| 550 | * Allocate colourmap. |
| 551 | */ |
Andres Salomon | 909baf0 | 2009-03-31 15:25:29 -0700 | [diff] [blame] | 552 | ret = fb_alloc_cmap(&fb->fb.cmap, 256, 0); |
| 553 | if (ret) |
| 554 | goto unmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | /* |
| 557 | * Ensure interrupts are disabled. |
| 558 | */ |
Russell King | 3f17522c | 2010-02-12 14:32:01 +0000 | [diff] [blame] | 559 | writel(0, fb->regs + fb->off_ienb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
| 561 | fb_set_var(&fb->fb, &fb->fb.var); |
| 562 | |
Russell King | ff64332 | 2011-01-19 21:10:24 +0000 | [diff] [blame] | 563 | dev_info(&fb->dev->dev, "%s hardware, %s display\n", |
| 564 | fb->board->name, fb->panel->mode.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
| 566 | ret = register_framebuffer(&fb->fb); |
| 567 | if (ret == 0) |
| 568 | goto out; |
| 569 | |
| 570 | printk(KERN_ERR "CLCD: cannot register framebuffer (%d)\n", ret); |
| 571 | |
Andres Salomon | 909baf0 | 2009-03-31 15:25:29 -0700 | [diff] [blame] | 572 | fb_dealloc_cmap(&fb->fb.cmap); |
| 573 | unmap: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | iounmap(fb->regs); |
Russell King | 99df4ee | 2011-09-22 12:34:31 +0100 | [diff] [blame] | 575 | clk_unprep: |
| 576 | clk_unprepare(fb->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | free_clk: |
| 578 | clk_put(fb->clk); |
| 579 | out: |
| 580 | return ret; |
| 581 | } |
| 582 | |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 583 | #ifdef CONFIG_OF |
| 584 | static int clcdfb_of_get_dpi_panel_mode(struct device_node *node, |
Linus Walleij | af29897 | 2016-06-16 11:36:14 +0200 | [diff] [blame] | 585 | struct clcd_panel *clcd_panel) |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 586 | { |
| 587 | int err; |
| 588 | struct display_timing timing; |
| 589 | struct videomode video; |
| 590 | |
| 591 | err = of_get_display_timing(node, "panel-timing", &timing); |
| 592 | if (err) |
| 593 | return err; |
| 594 | |
| 595 | videomode_from_timing(&timing, &video); |
| 596 | |
Linus Walleij | af29897 | 2016-06-16 11:36:14 +0200 | [diff] [blame] | 597 | err = fb_videomode_from_videomode(&video, &clcd_panel->mode); |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 598 | if (err) |
| 599 | return err; |
| 600 | |
Linus Walleij | af29897 | 2016-06-16 11:36:14 +0200 | [diff] [blame] | 601 | /* Set up some inversion flags */ |
| 602 | if (timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE) |
| 603 | clcd_panel->tim2 |= TIM2_IPC; |
| 604 | else if (!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)) |
| 605 | /* |
| 606 | * To preserve backwards compatibility, the IPC (inverted |
| 607 | * pixel clock) flag needs to be set on any display that |
| 608 | * doesn't explicitly specify that the pixel clock is |
| 609 | * active on the negative or positive edge. |
| 610 | */ |
| 611 | clcd_panel->tim2 |= TIM2_IPC; |
| 612 | |
| 613 | if (timing.flags & DISPLAY_FLAGS_HSYNC_LOW) |
| 614 | clcd_panel->tim2 |= TIM2_IHS; |
| 615 | |
| 616 | if (timing.flags & DISPLAY_FLAGS_VSYNC_LOW) |
| 617 | clcd_panel->tim2 |= TIM2_IVS; |
| 618 | |
| 619 | if (timing.flags & DISPLAY_FLAGS_DE_LOW) |
| 620 | clcd_panel->tim2 |= TIM2_IOE; |
| 621 | |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static int clcdfb_snprintf_mode(char *buf, int size, struct fb_videomode *mode) |
| 626 | { |
| 627 | return snprintf(buf, size, "%ux%u@%u", mode->xres, mode->yres, |
| 628 | mode->refresh); |
| 629 | } |
| 630 | |
Linus Walleij | c38162b | 2016-06-16 11:36:13 +0200 | [diff] [blame] | 631 | static int clcdfb_of_get_backlight(struct device_node *endpoint, |
| 632 | struct clcd_panel *clcd_panel) |
| 633 | { |
| 634 | struct device_node *panel; |
| 635 | struct device_node *backlight; |
| 636 | |
| 637 | panel = of_graph_get_remote_port_parent(endpoint); |
| 638 | if (!panel) |
| 639 | return -ENODEV; |
| 640 | |
| 641 | /* Look up the optional backlight phandle */ |
| 642 | backlight = of_parse_phandle(panel, "backlight", 0); |
| 643 | if (backlight) { |
| 644 | clcd_panel->backlight = of_find_backlight_by_node(backlight); |
| 645 | of_node_put(backlight); |
| 646 | |
| 647 | if (!clcd_panel->backlight) |
| 648 | return -EPROBE_DEFER; |
| 649 | } |
| 650 | return 0; |
| 651 | } |
| 652 | |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 653 | static int clcdfb_of_get_mode(struct device *dev, struct device_node *endpoint, |
Linus Walleij | af29897 | 2016-06-16 11:36:14 +0200 | [diff] [blame] | 654 | struct clcd_panel *clcd_panel) |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 655 | { |
| 656 | int err; |
| 657 | struct device_node *panel; |
Linus Walleij | af29897 | 2016-06-16 11:36:14 +0200 | [diff] [blame] | 658 | struct fb_videomode *mode; |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 659 | char *name; |
| 660 | int len; |
| 661 | |
| 662 | panel = of_graph_get_remote_port_parent(endpoint); |
| 663 | if (!panel) |
| 664 | return -ENODEV; |
| 665 | |
| 666 | /* Only directly connected DPI panels supported for now */ |
| 667 | if (of_device_is_compatible(panel, "panel-dpi")) |
Linus Walleij | af29897 | 2016-06-16 11:36:14 +0200 | [diff] [blame] | 668 | err = clcdfb_of_get_dpi_panel_mode(panel, clcd_panel); |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 669 | else |
| 670 | err = -ENOENT; |
| 671 | if (err) |
| 672 | return err; |
Linus Walleij | af29897 | 2016-06-16 11:36:14 +0200 | [diff] [blame] | 673 | mode = &clcd_panel->mode; |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 674 | |
| 675 | len = clcdfb_snprintf_mode(NULL, 0, mode); |
| 676 | name = devm_kzalloc(dev, len + 1, GFP_KERNEL); |
Kiran Padwal | 11f09e5 | 2015-02-11 15:06:45 +0530 | [diff] [blame] | 677 | if (!name) |
| 678 | return -ENOMEM; |
| 679 | |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 680 | clcdfb_snprintf_mode(name, len + 1, mode); |
| 681 | mode->name = name; |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | static int clcdfb_of_init_tft_panel(struct clcd_fb *fb, u32 r0, u32 g0, u32 b0) |
| 687 | { |
| 688 | static struct { |
| 689 | unsigned int part; |
| 690 | u32 r0, g0, b0; |
| 691 | u32 caps; |
| 692 | } panels[] = { |
| 693 | { 0x110, 1, 7, 13, CLCD_CAP_5551 }, |
| 694 | { 0x110, 0, 8, 16, CLCD_CAP_888 }, |
Linus Walleij | 03d14c3 | 2016-06-16 11:36:15 +0200 | [diff] [blame] | 695 | { 0x110, 16, 8, 0, CLCD_CAP_888 }, |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 696 | { 0x111, 4, 14, 20, CLCD_CAP_444 }, |
| 697 | { 0x111, 3, 11, 19, CLCD_CAP_444 | CLCD_CAP_5551 }, |
| 698 | { 0x111, 3, 10, 19, CLCD_CAP_444 | CLCD_CAP_5551 | |
| 699 | CLCD_CAP_565 }, |
| 700 | { 0x111, 0, 8, 16, CLCD_CAP_444 | CLCD_CAP_5551 | |
| 701 | CLCD_CAP_565 | CLCD_CAP_888 }, |
| 702 | }; |
| 703 | int i; |
| 704 | |
Linus Walleij | af29897 | 2016-06-16 11:36:14 +0200 | [diff] [blame] | 705 | /* Bypass pixel clock divider */ |
| 706 | fb->panel->tim2 |= TIM2_BCD; |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 707 | |
| 708 | /* TFT display, vert. comp. interrupt at the start of the back porch */ |
| 709 | fb->panel->cntl |= CNTL_LCDTFT | CNTL_LCDVCOMP(1); |
| 710 | |
| 711 | fb->panel->caps = 0; |
| 712 | |
| 713 | /* Match the setup with known variants */ |
| 714 | for (i = 0; i < ARRAY_SIZE(panels) && !fb->panel->caps; i++) { |
| 715 | if (amba_part(fb->dev) != panels[i].part) |
| 716 | continue; |
| 717 | if (g0 != panels[i].g0) |
| 718 | continue; |
| 719 | if (r0 == panels[i].r0 && b0 == panels[i].b0) |
Pawel Moll | e4cf39e | 2014-09-10 15:15:53 +0100 | [diff] [blame] | 720 | fb->panel->caps = panels[i].caps; |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 721 | } |
| 722 | |
Linus Walleij | 03d14c3 | 2016-06-16 11:36:15 +0200 | [diff] [blame] | 723 | /* |
| 724 | * If we actually physically connected the R lines to B and |
| 725 | * vice versa |
| 726 | */ |
| 727 | if (r0 != 0 && b0 == 0) |
| 728 | fb->panel->bgr_connection = true; |
| 729 | |
Linus Walleij | 046ad6c | 2016-06-16 11:36:16 +0200 | [diff] [blame] | 730 | if (fb->panel->caps && fb->vendor->st_bitmux_control) { |
| 731 | /* |
| 732 | * Set up the special bits for the Nomadik control register |
| 733 | * (other platforms tend to do this through an external |
| 734 | * register). |
| 735 | */ |
| 736 | |
| 737 | /* Offset of the highest used color */ |
| 738 | int maxoff = max3(r0, g0, b0); |
| 739 | /* Most significant bit out, highest used bit */ |
| 740 | int msb = 0; |
| 741 | |
| 742 | if (fb->panel->caps & CLCD_CAP_888) { |
| 743 | msb = maxoff + 8 - 1; |
| 744 | } else if (fb->panel->caps & CLCD_CAP_565) { |
| 745 | msb = maxoff + 5 - 1; |
| 746 | fb->panel->cntl |= CNTL_ST_1XBPP_565; |
| 747 | } else if (fb->panel->caps & CLCD_CAP_5551) { |
| 748 | msb = maxoff + 5 - 1; |
| 749 | fb->panel->cntl |= CNTL_ST_1XBPP_5551; |
| 750 | } else if (fb->panel->caps & CLCD_CAP_444) { |
| 751 | msb = maxoff + 4 - 1; |
| 752 | fb->panel->cntl |= CNTL_ST_1XBPP_444; |
| 753 | } |
| 754 | |
| 755 | /* Send out as many bits as we need */ |
| 756 | if (msb > 17) |
| 757 | fb->panel->cntl |= CNTL_ST_CDWID_24; |
| 758 | else if (msb > 15) |
| 759 | fb->panel->cntl |= CNTL_ST_CDWID_18; |
| 760 | else if (msb > 11) |
| 761 | fb->panel->cntl |= CNTL_ST_CDWID_16; |
| 762 | else |
| 763 | fb->panel->cntl |= CNTL_ST_CDWID_12; |
| 764 | } |
| 765 | |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 766 | return fb->panel->caps ? 0 : -EINVAL; |
| 767 | } |
| 768 | |
| 769 | static int clcdfb_of_init_display(struct clcd_fb *fb) |
| 770 | { |
| 771 | struct device_node *endpoint; |
| 772 | int err; |
Jon Medhurst (Tixy) | 2b6c53b | 2014-08-20 13:41:04 +0100 | [diff] [blame] | 773 | unsigned int bpp; |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 774 | u32 max_bandwidth; |
| 775 | u32 tft_r0b0g0[3]; |
| 776 | |
| 777 | fb->panel = devm_kzalloc(&fb->dev->dev, sizeof(*fb->panel), GFP_KERNEL); |
| 778 | if (!fb->panel) |
| 779 | return -ENOMEM; |
| 780 | |
Linus Walleij | 046ad6c | 2016-06-16 11:36:16 +0200 | [diff] [blame] | 781 | /* |
| 782 | * Fetch the panel endpoint. |
| 783 | */ |
| 784 | if (!endpoint) { |
| 785 | endpoint = of_graph_get_next_endpoint(fb->dev->dev.of_node, |
| 786 | NULL); |
| 787 | if (!endpoint) |
| 788 | return -ENODEV; |
| 789 | } |
| 790 | |
| 791 | if (fb->vendor->init_panel) { |
| 792 | err = fb->vendor->init_panel(fb, endpoint); |
| 793 | if (err) |
| 794 | return err; |
| 795 | } |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 796 | |
Linus Walleij | c38162b | 2016-06-16 11:36:13 +0200 | [diff] [blame] | 797 | err = clcdfb_of_get_backlight(endpoint, fb->panel); |
| 798 | if (err) |
| 799 | return err; |
| 800 | |
Linus Walleij | af29897 | 2016-06-16 11:36:14 +0200 | [diff] [blame] | 801 | err = clcdfb_of_get_mode(&fb->dev->dev, endpoint, fb->panel); |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 802 | if (err) |
| 803 | return err; |
| 804 | |
| 805 | err = of_property_read_u32(fb->dev->dev.of_node, "max-memory-bandwidth", |
| 806 | &max_bandwidth); |
Jon Medhurst (Tixy) | 2b6c53b | 2014-08-20 13:41:04 +0100 | [diff] [blame] | 807 | if (!err) { |
| 808 | /* |
| 809 | * max_bandwidth is in bytes per second and pixclock in |
| 810 | * pico-seconds, so the maximum allowed bits per pixel is |
| 811 | * 8 * max_bandwidth / (PICOS2KHZ(pixclock) * 1000) |
| 812 | * Rearrange this calculation to avoid overflow and then ensure |
| 813 | * result is a valid format. |
| 814 | */ |
| 815 | bpp = max_bandwidth / (1000 / 8) |
| 816 | / PICOS2KHZ(fb->panel->mode.pixclock); |
| 817 | bpp = rounddown_pow_of_two(bpp); |
| 818 | if (bpp > 32) |
| 819 | bpp = 32; |
| 820 | } else |
| 821 | bpp = 32; |
| 822 | fb->panel->bpp = bpp; |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 823 | |
| 824 | #ifdef CONFIG_CPU_BIG_ENDIAN |
| 825 | fb->panel->cntl |= CNTL_BEBO; |
| 826 | #endif |
| 827 | fb->panel->width = -1; |
| 828 | fb->panel->height = -1; |
| 829 | |
| 830 | if (of_property_read_u32_array(endpoint, |
| 831 | "arm,pl11x,tft-r0g0b0-pads", |
Linus Walleij | 046ad6c | 2016-06-16 11:36:16 +0200 | [diff] [blame] | 832 | tft_r0b0g0, ARRAY_SIZE(tft_r0b0g0)) != 0) |
| 833 | return -ENOENT; |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 834 | |
Linus Walleij | 046ad6c | 2016-06-16 11:36:16 +0200 | [diff] [blame] | 835 | return clcdfb_of_init_tft_panel(fb, tft_r0b0g0[0], |
| 836 | tft_r0b0g0[1], tft_r0b0g0[2]); |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | static int clcdfb_of_vram_setup(struct clcd_fb *fb) |
| 840 | { |
| 841 | int err; |
| 842 | struct device_node *memory; |
| 843 | u64 size; |
| 844 | |
| 845 | err = clcdfb_of_init_display(fb); |
| 846 | if (err) |
| 847 | return err; |
| 848 | |
| 849 | memory = of_parse_phandle(fb->dev->dev.of_node, "memory-region", 0); |
| 850 | if (!memory) |
| 851 | return -ENODEV; |
| 852 | |
| 853 | fb->fb.screen_base = of_iomap(memory, 0); |
| 854 | if (!fb->fb.screen_base) |
| 855 | return -ENOMEM; |
| 856 | |
| 857 | fb->fb.fix.smem_start = of_translate_address(memory, |
| 858 | of_get_address(memory, 0, &size, NULL)); |
| 859 | fb->fb.fix.smem_len = size; |
| 860 | |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | static int clcdfb_of_vram_mmap(struct clcd_fb *fb, struct vm_area_struct *vma) |
| 865 | { |
| 866 | unsigned long off, user_size, kernel_size; |
| 867 | |
| 868 | |
| 869 | off = vma->vm_pgoff << PAGE_SHIFT; |
| 870 | user_size = vma->vm_end - vma->vm_start; |
| 871 | kernel_size = fb->fb.fix.smem_len; |
| 872 | |
| 873 | if (off >= kernel_size || user_size > (kernel_size - off)) |
| 874 | return -ENXIO; |
| 875 | |
| 876 | return remap_pfn_range(vma, vma->vm_start, |
| 877 | __phys_to_pfn(fb->fb.fix.smem_start) + vma->vm_pgoff, |
| 878 | user_size, |
| 879 | pgprot_writecombine(vma->vm_page_prot)); |
| 880 | } |
| 881 | |
| 882 | static void clcdfb_of_vram_remove(struct clcd_fb *fb) |
| 883 | { |
| 884 | iounmap(fb->fb.screen_base); |
| 885 | } |
| 886 | |
| 887 | static int clcdfb_of_dma_setup(struct clcd_fb *fb) |
| 888 | { |
| 889 | unsigned long framesize; |
| 890 | dma_addr_t dma; |
| 891 | int err; |
| 892 | |
| 893 | err = clcdfb_of_init_display(fb); |
| 894 | if (err) |
| 895 | return err; |
| 896 | |
| 897 | framesize = fb->panel->mode.xres * fb->panel->mode.yres * |
| 898 | fb->panel->bpp / 8; |
| 899 | fb->fb.screen_base = dma_alloc_coherent(&fb->dev->dev, framesize, |
| 900 | &dma, GFP_KERNEL); |
| 901 | if (!fb->fb.screen_base) |
| 902 | return -ENOMEM; |
| 903 | |
| 904 | fb->fb.fix.smem_start = dma; |
| 905 | fb->fb.fix.smem_len = framesize; |
| 906 | |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | static int clcdfb_of_dma_mmap(struct clcd_fb *fb, struct vm_area_struct *vma) |
| 911 | { |
Luis R. Rodriguez | f6e4566 | 2016-01-22 18:34:22 -0800 | [diff] [blame] | 912 | return dma_mmap_wc(&fb->dev->dev, vma, fb->fb.screen_base, |
| 913 | fb->fb.fix.smem_start, fb->fb.fix.smem_len); |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | static void clcdfb_of_dma_remove(struct clcd_fb *fb) |
| 917 | { |
| 918 | dma_free_coherent(&fb->dev->dev, fb->fb.fix.smem_len, |
| 919 | fb->fb.screen_base, fb->fb.fix.smem_start); |
| 920 | } |
| 921 | |
| 922 | static struct clcd_board *clcdfb_of_get_board(struct amba_device *dev) |
| 923 | { |
| 924 | struct clcd_board *board = devm_kzalloc(&dev->dev, sizeof(*board), |
| 925 | GFP_KERNEL); |
| 926 | struct device_node *node = dev->dev.of_node; |
| 927 | |
| 928 | if (!board) |
| 929 | return NULL; |
| 930 | |
| 931 | board->name = of_node_full_name(node); |
| 932 | board->caps = CLCD_CAP_ALL; |
| 933 | board->check = clcdfb_check; |
| 934 | board->decode = clcdfb_decode; |
| 935 | if (of_find_property(node, "memory-region", NULL)) { |
| 936 | board->setup = clcdfb_of_vram_setup; |
| 937 | board->mmap = clcdfb_of_vram_mmap; |
| 938 | board->remove = clcdfb_of_vram_remove; |
| 939 | } else { |
| 940 | board->setup = clcdfb_of_dma_setup; |
| 941 | board->mmap = clcdfb_of_dma_mmap; |
| 942 | board->remove = clcdfb_of_dma_remove; |
| 943 | } |
| 944 | |
| 945 | return board; |
| 946 | } |
| 947 | #else |
Pawel Moll | 1d5167b | 2014-08-01 15:43:34 +0100 | [diff] [blame] | 948 | static struct clcd_board *clcdfb_of_get_board(struct amba_device *dev) |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 949 | { |
| 950 | return NULL; |
| 951 | } |
| 952 | #endif |
| 953 | |
Russell King | aa25afa | 2011-02-19 15:55:00 +0000 | [diff] [blame] | 954 | static int clcdfb_probe(struct amba_device *dev, const struct amba_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | { |
Jingoo Han | 46d2db8 | 2013-09-17 14:03:53 +0900 | [diff] [blame] | 956 | struct clcd_board *board = dev_get_platdata(&dev->dev); |
Linus Walleij | 046ad6c | 2016-06-16 11:36:16 +0200 | [diff] [blame] | 957 | struct clcd_vendor_data *vendor = id->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | struct clcd_fb *fb; |
| 959 | int ret; |
| 960 | |
| 961 | if (!board) |
Pawel Moll | d10715b | 2014-06-24 12:55:11 +0100 | [diff] [blame] | 962 | board = clcdfb_of_get_board(dev); |
| 963 | |
| 964 | if (!board) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | return -EINVAL; |
| 966 | |
Linus Walleij | 046ad6c | 2016-06-16 11:36:16 +0200 | [diff] [blame] | 967 | if (vendor->init_board) { |
| 968 | ret = vendor->init_board(dev, board); |
| 969 | if (ret) |
| 970 | return ret; |
| 971 | } |
| 972 | |
Russell King | e0a8ba2 | 2013-06-27 10:29:32 +0100 | [diff] [blame] | 973 | ret = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32)); |
| 974 | if (ret) |
| 975 | goto out; |
| 976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | ret = amba_request_regions(dev, NULL); |
| 978 | if (ret) { |
| 979 | printk(KERN_ERR "CLCD: unable to reserve regs region\n"); |
| 980 | goto out; |
| 981 | } |
| 982 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 983 | fb = kzalloc(sizeof(struct clcd_fb), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | if (!fb) { |
| 985 | printk(KERN_INFO "CLCD: could not allocate new clcd_fb struct\n"); |
| 986 | ret = -ENOMEM; |
| 987 | goto free_region; |
| 988 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
| 990 | fb->dev = dev; |
Linus Walleij | 046ad6c | 2016-06-16 11:36:16 +0200 | [diff] [blame] | 991 | fb->vendor = vendor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | fb->board = board; |
| 993 | |
Linus Walleij | 046ad6c | 2016-06-16 11:36:16 +0200 | [diff] [blame] | 994 | dev_info(&fb->dev->dev, "PL%03x designer %02x rev%u at 0x%08llx\n", |
| 995 | amba_part(dev), amba_manf(dev), amba_rev(dev), |
Russell King | ff64332 | 2011-01-19 21:10:24 +0000 | [diff] [blame] | 996 | (unsigned long long)dev->res.start); |
| 997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | ret = fb->board->setup(fb); |
| 999 | if (ret) |
| 1000 | goto free_fb; |
| 1001 | |
Linus Walleij | 1d3f0cb | 2016-06-16 11:36:17 +0200 | [diff] [blame^] | 1002 | ret = clcdfb_register(fb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | if (ret == 0) { |
| 1004 | amba_set_drvdata(dev, fb); |
| 1005 | goto out; |
| 1006 | } |
| 1007 | |
| 1008 | fb->board->remove(fb); |
| 1009 | free_fb: |
| 1010 | kfree(fb); |
| 1011 | free_region: |
| 1012 | amba_release_regions(dev); |
| 1013 | out: |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
| 1017 | static int clcdfb_remove(struct amba_device *dev) |
| 1018 | { |
| 1019 | struct clcd_fb *fb = amba_get_drvdata(dev); |
| 1020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | clcdfb_disable(fb); |
| 1022 | unregister_framebuffer(&fb->fb); |
Andres Salomon | 909baf0 | 2009-03-31 15:25:29 -0700 | [diff] [blame] | 1023 | if (fb->fb.cmap.len) |
| 1024 | fb_dealloc_cmap(&fb->fb.cmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | iounmap(fb->regs); |
Russell King | 99df4ee | 2011-09-22 12:34:31 +0100 | [diff] [blame] | 1026 | clk_unprepare(fb->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | clk_put(fb->clk); |
| 1028 | |
| 1029 | fb->board->remove(fb); |
| 1030 | |
| 1031 | kfree(fb); |
| 1032 | |
| 1033 | amba_release_regions(dev); |
| 1034 | |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
Linus Walleij | 046ad6c | 2016-06-16 11:36:16 +0200 | [diff] [blame] | 1038 | static struct clcd_vendor_data vendor_arm = { |
| 1039 | /* No special business */ |
| 1040 | }; |
| 1041 | |
| 1042 | static struct clcd_vendor_data vendor_nomadik = { |
| 1043 | .clock_timregs = true, |
| 1044 | .packed_24_bit_pixels = true, |
| 1045 | .st_bitmux_control = true, |
Linus Walleij | 1d3f0cb | 2016-06-16 11:36:17 +0200 | [diff] [blame^] | 1046 | .init_board = nomadik_clcd_init_board, |
| 1047 | .init_panel = nomadik_clcd_init_panel, |
Linus Walleij | 046ad6c | 2016-06-16 11:36:16 +0200 | [diff] [blame] | 1048 | }; |
| 1049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | static struct amba_id clcdfb_id_table[] = { |
| 1051 | { |
| 1052 | .id = 0x00041110, |
Russell King | e831556 | 2005-11-02 14:40:35 +0000 | [diff] [blame] | 1053 | .mask = 0x000ffffe, |
Linus Walleij | 046ad6c | 2016-06-16 11:36:16 +0200 | [diff] [blame] | 1054 | .data = &vendor_arm, |
| 1055 | }, |
| 1056 | /* ST Electronics Nomadik variant */ |
| 1057 | { |
| 1058 | .id = 0x00180110, |
| 1059 | .mask = 0x00fffffe, |
| 1060 | .data = &vendor_nomadik, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | }, |
| 1062 | { 0, 0 }, |
| 1063 | }; |
| 1064 | |
Dave Martin | 6054f9b | 2011-10-05 15:15:23 +0100 | [diff] [blame] | 1065 | MODULE_DEVICE_TABLE(amba, clcdfb_id_table); |
| 1066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | static struct amba_driver clcd_driver = { |
| 1068 | .drv = { |
Russell King | e831556 | 2005-11-02 14:40:35 +0000 | [diff] [blame] | 1069 | .name = "clcd-pl11x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | }, |
| 1071 | .probe = clcdfb_probe, |
| 1072 | .remove = clcdfb_remove, |
| 1073 | .id_table = clcdfb_id_table, |
| 1074 | }; |
| 1075 | |
Russell King | 2c25013 | 2005-11-08 14:44:15 +0000 | [diff] [blame] | 1076 | static int __init amba_clcdfb_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | { |
| 1078 | if (fb_get_options("ambafb", NULL)) |
| 1079 | return -ENODEV; |
| 1080 | |
| 1081 | return amba_driver_register(&clcd_driver); |
| 1082 | } |
| 1083 | |
| 1084 | module_init(amba_clcdfb_init); |
| 1085 | |
| 1086 | static void __exit amba_clcdfb_exit(void) |
| 1087 | { |
| 1088 | amba_driver_unregister(&clcd_driver); |
| 1089 | } |
| 1090 | |
| 1091 | module_exit(amba_clcdfb_exit); |
| 1092 | |
| 1093 | MODULE_DESCRIPTION("ARM PrimeCell PL110 CLCD core driver"); |
| 1094 | MODULE_LICENSE("GPL"); |