Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Driver for AT91/AT32 LCD Controller |
| 3 | * |
| 4 | * Copyright (C) 2007 Atmel Corporation |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License. See the file COPYING in the main directory of this archive for |
| 8 | * more details. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/dma-mapping.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/fb.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/delay.h> |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 19 | #include <linux/backlight.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/gfp.h> |
Paul Gortmaker | 355b200 | 2011-07-03 16:17:28 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
Jean-Christophe PLAGNIOL-VILLARD | bcd2360 | 2012-10-30 05:12:23 +0800 | [diff] [blame] | 22 | #include <linux/platform_data/atmel.h> |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 23 | #include <video/of_display_timing.h> |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 24 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 25 | #include <mach/cpu.h> |
Russell King | 60e8972 | 2011-07-26 10:56:19 +0100 | [diff] [blame] | 26 | #include <asm/gpio.h> |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 27 | |
| 28 | #include <video/atmel_lcdc.h> |
| 29 | |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 30 | struct atmel_lcdfb_config { |
| 31 | bool have_alt_pixclock; |
| 32 | bool have_hozval; |
| 33 | bool have_intensity_bit; |
| 34 | }; |
| 35 | |
| 36 | /* LCD Controller info data structure, stored in device platform_data */ |
| 37 | struct atmel_lcdfb_info { |
| 38 | spinlock_t lock; |
| 39 | struct fb_info *info; |
| 40 | void __iomem *mmio; |
| 41 | int irq_base; |
| 42 | struct work_struct task; |
| 43 | |
| 44 | unsigned int smem_len; |
| 45 | struct platform_device *pdev; |
| 46 | struct clk *bus_clk; |
| 47 | struct clk *lcdc_clk; |
| 48 | |
| 49 | struct backlight_device *backlight; |
| 50 | u8 bl_power; |
| 51 | u8 saved_lcdcon; |
| 52 | |
| 53 | u32 pseudo_palette[16]; |
| 54 | bool have_intensity_bit; |
| 55 | |
| 56 | struct atmel_lcdfb_pdata pdata; |
| 57 | |
| 58 | struct atmel_lcdfb_config *config; |
| 59 | }; |
| 60 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 61 | #define lcdc_readl(sinfo, reg) __raw_readl((sinfo)->mmio+(reg)) |
| 62 | #define lcdc_writel(sinfo, reg, val) __raw_writel((val), (sinfo)->mmio+(reg)) |
| 63 | |
| 64 | /* configurable parameters */ |
| 65 | #define ATMEL_LCDC_CVAL_DEFAULT 0xc8 |
Nicolas Ferre | 53b7479 | 2009-05-28 14:34:36 -0700 | [diff] [blame] | 66 | #define ATMEL_LCDC_DMA_BURST_LEN 8 /* words */ |
| 67 | #define ATMEL_LCDC_FIFO_SIZE 512 /* words */ |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 68 | |
Johan Hovold | bbd44f6b | 2013-02-07 16:31:58 +0100 | [diff] [blame] | 69 | static struct atmel_lcdfb_config at91sam9261_config = { |
| 70 | .have_hozval = true, |
| 71 | .have_intensity_bit = true, |
| 72 | }; |
| 73 | |
| 74 | static struct atmel_lcdfb_config at91sam9263_config = { |
| 75 | .have_intensity_bit = true, |
| 76 | }; |
| 77 | |
| 78 | static struct atmel_lcdfb_config at91sam9g10_config = { |
| 79 | .have_hozval = true, |
| 80 | }; |
| 81 | |
| 82 | static struct atmel_lcdfb_config at91sam9g45_config = { |
| 83 | .have_alt_pixclock = true, |
| 84 | }; |
| 85 | |
| 86 | static struct atmel_lcdfb_config at91sam9g45es_config = { |
| 87 | }; |
| 88 | |
| 89 | static struct atmel_lcdfb_config at91sam9rl_config = { |
| 90 | .have_intensity_bit = true, |
| 91 | }; |
| 92 | |
| 93 | static struct atmel_lcdfb_config at32ap_config = { |
| 94 | .have_hozval = true, |
| 95 | }; |
| 96 | |
| 97 | static const struct platform_device_id atmel_lcdfb_devtypes[] = { |
| 98 | { |
| 99 | .name = "at91sam9261-lcdfb", |
| 100 | .driver_data = (unsigned long)&at91sam9261_config, |
| 101 | }, { |
| 102 | .name = "at91sam9263-lcdfb", |
| 103 | .driver_data = (unsigned long)&at91sam9263_config, |
| 104 | }, { |
| 105 | .name = "at91sam9g10-lcdfb", |
| 106 | .driver_data = (unsigned long)&at91sam9g10_config, |
| 107 | }, { |
| 108 | .name = "at91sam9g45-lcdfb", |
| 109 | .driver_data = (unsigned long)&at91sam9g45_config, |
| 110 | }, { |
| 111 | .name = "at91sam9g45es-lcdfb", |
| 112 | .driver_data = (unsigned long)&at91sam9g45es_config, |
| 113 | }, { |
| 114 | .name = "at91sam9rl-lcdfb", |
| 115 | .driver_data = (unsigned long)&at91sam9rl_config, |
| 116 | }, { |
| 117 | .name = "at32ap-lcdfb", |
| 118 | .driver_data = (unsigned long)&at32ap_config, |
| 119 | }, { |
| 120 | /* terminator */ |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | static struct atmel_lcdfb_config * |
| 125 | atmel_lcdfb_get_config(struct platform_device *pdev) |
| 126 | { |
| 127 | unsigned long data; |
| 128 | |
| 129 | data = platform_get_device_id(pdev)->driver_data; |
| 130 | |
| 131 | return (struct atmel_lcdfb_config *)data; |
| 132 | } |
| 133 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 134 | #if defined(CONFIG_ARCH_AT91) |
Haavard Skinnemoen | e730d8b0a | 2008-08-12 15:08:56 -0700 | [diff] [blame] | 135 | #define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \ |
| 136 | | FBINFO_PARTIAL_PAN_OK \ |
| 137 | | FBINFO_HWACCEL_YPAN) |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 138 | |
| 139 | static inline void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo, |
Laurent Pinchart | b3e9c12 | 2011-05-25 11:34:52 +0200 | [diff] [blame] | 140 | struct fb_var_screeninfo *var, |
| 141 | struct fb_info *info) |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 142 | { |
| 143 | |
| 144 | } |
| 145 | #elif defined(CONFIG_AVR32) |
| 146 | #define ATMEL_LCDFB_FBINFO_DEFAULT (FBINFO_DEFAULT \ |
| 147 | | FBINFO_PARTIAL_PAN_OK \ |
| 148 | | FBINFO_HWACCEL_XPAN \ |
| 149 | | FBINFO_HWACCEL_YPAN) |
| 150 | |
| 151 | static void atmel_lcdfb_update_dma2d(struct atmel_lcdfb_info *sinfo, |
Laurent Pinchart | b3e9c12 | 2011-05-25 11:34:52 +0200 | [diff] [blame] | 152 | struct fb_var_screeninfo *var, |
| 153 | struct fb_info *info) |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 154 | { |
| 155 | u32 dma2dcfg; |
| 156 | u32 pixeloff; |
| 157 | |
Laurent Pinchart | b3e9c12 | 2011-05-25 11:34:52 +0200 | [diff] [blame] | 158 | pixeloff = (var->xoffset * info->var.bits_per_pixel) & 0x1f; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 159 | |
Laurent Pinchart | b3e9c12 | 2011-05-25 11:34:52 +0200 | [diff] [blame] | 160 | dma2dcfg = (info->var.xres_virtual - info->var.xres) |
| 161 | * info->var.bits_per_pixel / 8; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 162 | dma2dcfg |= pixeloff << ATMEL_LCDC_PIXELOFF_OFFSET; |
| 163 | lcdc_writel(sinfo, ATMEL_LCDC_DMA2DCFG, dma2dcfg); |
| 164 | |
| 165 | /* Update configuration */ |
| 166 | lcdc_writel(sinfo, ATMEL_LCDC_DMACON, |
| 167 | lcdc_readl(sinfo, ATMEL_LCDC_DMACON) |
| 168 | | ATMEL_LCDC_DMAUPDT); |
| 169 | } |
| 170 | #endif |
| 171 | |
Andreas Bießmann | 7cdcdb6 | 2011-02-11 15:19:43 +0000 | [diff] [blame] | 172 | static u32 contrast_ctr = ATMEL_LCDC_PS_DIV8 |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 173 | | ATMEL_LCDC_POL_POSITIVE |
| 174 | | ATMEL_LCDC_ENA_PWMENABLE; |
| 175 | |
| 176 | #ifdef CONFIG_BACKLIGHT_ATMEL_LCDC |
| 177 | |
| 178 | /* some bl->props field just changed */ |
| 179 | static int atmel_bl_update_status(struct backlight_device *bl) |
| 180 | { |
| 181 | struct atmel_lcdfb_info *sinfo = bl_get_data(bl); |
| 182 | int power = sinfo->bl_power; |
| 183 | int brightness = bl->props.brightness; |
| 184 | |
| 185 | /* REVISIT there may be a meaningful difference between |
| 186 | * fb_blank and power ... there seem to be some cases |
| 187 | * this doesn't handle correctly. |
| 188 | */ |
| 189 | if (bl->props.fb_blank != sinfo->bl_power) |
| 190 | power = bl->props.fb_blank; |
| 191 | else if (bl->props.power != sinfo->bl_power) |
| 192 | power = bl->props.power; |
| 193 | |
| 194 | if (brightness < 0 && power == FB_BLANK_UNBLANK) |
| 195 | brightness = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL); |
| 196 | else if (power != FB_BLANK_UNBLANK) |
| 197 | brightness = 0; |
| 198 | |
| 199 | lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, brightness); |
Alexander Stein | acfdc2e | 2011-10-05 09:59:58 +0200 | [diff] [blame] | 200 | if (contrast_ctr & ATMEL_LCDC_POL_POSITIVE) |
| 201 | lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 202 | brightness ? contrast_ctr : 0); |
Alexander Stein | acfdc2e | 2011-10-05 09:59:58 +0200 | [diff] [blame] | 203 | else |
| 204 | lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr); |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 205 | |
| 206 | bl->props.fb_blank = bl->props.power = sinfo->bl_power = power; |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static int atmel_bl_get_brightness(struct backlight_device *bl) |
| 212 | { |
| 213 | struct atmel_lcdfb_info *sinfo = bl_get_data(bl); |
| 214 | |
| 215 | return lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_VAL); |
| 216 | } |
| 217 | |
Lionel Debroux | acc2472 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 218 | static const struct backlight_ops atmel_lcdc_bl_ops = { |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 219 | .update_status = atmel_bl_update_status, |
| 220 | .get_brightness = atmel_bl_get_brightness, |
| 221 | }; |
| 222 | |
| 223 | static void init_backlight(struct atmel_lcdfb_info *sinfo) |
| 224 | { |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 225 | struct backlight_properties props; |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 226 | struct backlight_device *bl; |
| 227 | |
| 228 | sinfo->bl_power = FB_BLANK_UNBLANK; |
| 229 | |
| 230 | if (sinfo->backlight) |
| 231 | return; |
| 232 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 233 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 234 | props.type = BACKLIGHT_RAW; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 235 | props.max_brightness = 0xff; |
| 236 | bl = backlight_device_register("backlight", &sinfo->pdev->dev, sinfo, |
| 237 | &atmel_lcdc_bl_ops, &props); |
Julien Brunel | cf7b9a1 | 2008-11-19 15:36:07 -0800 | [diff] [blame] | 238 | if (IS_ERR(bl)) { |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 239 | dev_err(&sinfo->pdev->dev, "error %ld on backlight register\n", |
| 240 | PTR_ERR(bl)); |
| 241 | return; |
| 242 | } |
| 243 | sinfo->backlight = bl; |
| 244 | |
| 245 | bl->props.power = FB_BLANK_UNBLANK; |
| 246 | bl->props.fb_blank = FB_BLANK_UNBLANK; |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 247 | bl->props.brightness = atmel_bl_get_brightness(bl); |
| 248 | } |
| 249 | |
| 250 | static void exit_backlight(struct atmel_lcdfb_info *sinfo) |
| 251 | { |
Richard Genoud | 56c21b5 | 2013-05-31 15:49:35 +0000 | [diff] [blame] | 252 | if (!sinfo->backlight) |
| 253 | return; |
| 254 | |
| 255 | if (sinfo->backlight->ops) { |
| 256 | sinfo->backlight->props.power = FB_BLANK_POWERDOWN; |
| 257 | sinfo->backlight->ops->update_status(sinfo->backlight); |
| 258 | } |
| 259 | backlight_device_unregister(sinfo->backlight); |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | #else |
| 263 | |
| 264 | static void init_backlight(struct atmel_lcdfb_info *sinfo) |
| 265 | { |
| 266 | dev_warn(&sinfo->pdev->dev, "backlight control is not available\n"); |
| 267 | } |
| 268 | |
| 269 | static void exit_backlight(struct atmel_lcdfb_info *sinfo) |
| 270 | { |
| 271 | } |
| 272 | |
| 273 | #endif |
| 274 | |
| 275 | static void init_contrast(struct atmel_lcdfb_info *sinfo) |
| 276 | { |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 277 | struct atmel_lcdfb_pdata *pdata = &sinfo->pdata; |
| 278 | |
Andreas Bießmann | 7cdcdb6 | 2011-02-11 15:19:43 +0000 | [diff] [blame] | 279 | /* contrast pwm can be 'inverted' */ |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 280 | if (pdata->lcdcon_pol_negative) |
Andreas Bießmann | 7cdcdb6 | 2011-02-11 15:19:43 +0000 | [diff] [blame] | 281 | contrast_ctr &= ~(ATMEL_LCDC_POL_POSITIVE); |
| 282 | |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 283 | /* have some default contrast/backlight settings */ |
| 284 | lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, contrast_ctr); |
| 285 | lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_VAL, ATMEL_LCDC_CVAL_DEFAULT); |
| 286 | |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 287 | if (pdata->lcdcon_is_backlight) |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 288 | init_backlight(sinfo); |
| 289 | } |
| 290 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 291 | |
| 292 | static struct fb_fix_screeninfo atmel_lcdfb_fix __initdata = { |
| 293 | .type = FB_TYPE_PACKED_PIXELS, |
| 294 | .visual = FB_VISUAL_TRUECOLOR, |
| 295 | .xpanstep = 0, |
Haavard Skinnemoen | e730d8b0a | 2008-08-12 15:08:56 -0700 | [diff] [blame] | 296 | .ypanstep = 1, |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 297 | .ywrapstep = 0, |
| 298 | .accel = FB_ACCEL_NONE, |
| 299 | }; |
| 300 | |
Johan Hovold | 934a50b | 2013-02-07 16:31:57 +0100 | [diff] [blame] | 301 | static unsigned long compute_hozval(struct atmel_lcdfb_info *sinfo, |
| 302 | unsigned long xres) |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 303 | { |
Johan Hovold | 934a50b | 2013-02-07 16:31:57 +0100 | [diff] [blame] | 304 | unsigned long lcdcon2; |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 305 | unsigned long value; |
| 306 | |
Johan Hovold | bbd44f6b | 2013-02-07 16:31:58 +0100 | [diff] [blame] | 307 | if (!sinfo->config->have_hozval) |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 308 | return xres; |
| 309 | |
Johan Hovold | 934a50b | 2013-02-07 16:31:57 +0100 | [diff] [blame] | 310 | lcdcon2 = lcdc_readl(sinfo, ATMEL_LCDC_LCDCON2); |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 311 | value = xres; |
| 312 | if ((lcdcon2 & ATMEL_LCDC_DISTYPE) != ATMEL_LCDC_DISTYPE_TFT) { |
| 313 | /* STN display */ |
| 314 | if ((lcdcon2 & ATMEL_LCDC_DISTYPE) == ATMEL_LCDC_DISTYPE_STNCOLOR) { |
| 315 | value *= 3; |
| 316 | } |
| 317 | if ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_4 |
| 318 | || ( (lcdcon2 & ATMEL_LCDC_IFWIDTH) == ATMEL_LCDC_IFWIDTH_8 |
| 319 | && (lcdcon2 & ATMEL_LCDC_SCANMOD) == ATMEL_LCDC_SCANMOD_DUAL )) |
| 320 | value = DIV_ROUND_UP(value, 4); |
| 321 | else |
| 322 | value = DIV_ROUND_UP(value, 8); |
| 323 | } |
| 324 | |
| 325 | return value; |
| 326 | } |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 327 | |
Haavard Skinnemoen | 3aa04f1 | 2008-09-13 02:33:23 -0700 | [diff] [blame] | 328 | static void atmel_lcdfb_stop_nowait(struct atmel_lcdfb_info *sinfo) |
| 329 | { |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 330 | struct atmel_lcdfb_pdata *pdata = &sinfo->pdata; |
| 331 | |
Haavard Skinnemoen | 3aa04f1 | 2008-09-13 02:33:23 -0700 | [diff] [blame] | 332 | /* Turn off the LCD controller and the DMA controller */ |
| 333 | lcdc_writel(sinfo, ATMEL_LCDC_PWRCON, |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 334 | pdata->guard_time << ATMEL_LCDC_GUARDT_OFFSET); |
Haavard Skinnemoen | 3aa04f1 | 2008-09-13 02:33:23 -0700 | [diff] [blame] | 335 | |
| 336 | /* Wait for the LCDC core to become idle */ |
| 337 | while (lcdc_readl(sinfo, ATMEL_LCDC_PWRCON) & ATMEL_LCDC_BUSY) |
| 338 | msleep(10); |
| 339 | |
| 340 | lcdc_writel(sinfo, ATMEL_LCDC_DMACON, 0); |
| 341 | } |
| 342 | |
| 343 | static void atmel_lcdfb_stop(struct atmel_lcdfb_info *sinfo) |
| 344 | { |
| 345 | atmel_lcdfb_stop_nowait(sinfo); |
| 346 | |
| 347 | /* Wait for DMA engine to become idle... */ |
| 348 | while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY) |
| 349 | msleep(10); |
| 350 | } |
| 351 | |
| 352 | static void atmel_lcdfb_start(struct atmel_lcdfb_info *sinfo) |
| 353 | { |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 354 | struct atmel_lcdfb_pdata *pdata = &sinfo->pdata; |
| 355 | |
| 356 | lcdc_writel(sinfo, ATMEL_LCDC_DMACON, pdata->default_dmacon); |
Haavard Skinnemoen | 3aa04f1 | 2008-09-13 02:33:23 -0700 | [diff] [blame] | 357 | lcdc_writel(sinfo, ATMEL_LCDC_PWRCON, |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 358 | (pdata->guard_time << ATMEL_LCDC_GUARDT_OFFSET) |
Haavard Skinnemoen | 3aa04f1 | 2008-09-13 02:33:23 -0700 | [diff] [blame] | 359 | | ATMEL_LCDC_PWR); |
| 360 | } |
| 361 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 362 | static void atmel_lcdfb_update_dma(struct fb_info *info, |
| 363 | struct fb_var_screeninfo *var) |
| 364 | { |
| 365 | struct atmel_lcdfb_info *sinfo = info->par; |
| 366 | struct fb_fix_screeninfo *fix = &info->fix; |
| 367 | unsigned long dma_addr; |
| 368 | |
| 369 | dma_addr = (fix->smem_start + var->yoffset * fix->line_length |
Laurent Pinchart | b3e9c12 | 2011-05-25 11:34:52 +0200 | [diff] [blame] | 370 | + var->xoffset * info->var.bits_per_pixel / 8); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 371 | |
| 372 | dma_addr &= ~3UL; |
| 373 | |
| 374 | /* Set framebuffer DMA base address and pixel offset */ |
| 375 | lcdc_writel(sinfo, ATMEL_LCDC_DMABADDR1, dma_addr); |
| 376 | |
Laurent Pinchart | b3e9c12 | 2011-05-25 11:34:52 +0200 | [diff] [blame] | 377 | atmel_lcdfb_update_dma2d(sinfo, var, info); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static inline void atmel_lcdfb_free_video_memory(struct atmel_lcdfb_info *sinfo) |
| 381 | { |
| 382 | struct fb_info *info = sinfo->info; |
| 383 | |
| 384 | dma_free_writecombine(info->device, info->fix.smem_len, |
| 385 | info->screen_base, info->fix.smem_start); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * atmel_lcdfb_alloc_video_memory - Allocate framebuffer memory |
| 390 | * @sinfo: the frame buffer to allocate memory for |
Krzysztof Helt | 1d01e83 | 2009-07-08 22:26:16 +0200 | [diff] [blame] | 391 | * |
| 392 | * This function is called only from the atmel_lcdfb_probe() |
| 393 | * so no locking by fb_info->mm_lock around smem_len setting is needed. |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 394 | */ |
| 395 | static int atmel_lcdfb_alloc_video_memory(struct atmel_lcdfb_info *sinfo) |
| 396 | { |
| 397 | struct fb_info *info = sinfo->info; |
| 398 | struct fb_var_screeninfo *var = &info->var; |
Haavard Skinnemoen | ea757ac | 2008-08-12 15:08:57 -0700 | [diff] [blame] | 399 | unsigned int smem_len; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 400 | |
Haavard Skinnemoen | ea757ac | 2008-08-12 15:08:57 -0700 | [diff] [blame] | 401 | smem_len = (var->xres_virtual * var->yres_virtual |
| 402 | * ((var->bits_per_pixel + 7) / 8)); |
| 403 | info->fix.smem_len = max(smem_len, sinfo->smem_len); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 404 | |
| 405 | info->screen_base = dma_alloc_writecombine(info->device, info->fix.smem_len, |
| 406 | (dma_addr_t *)&info->fix.smem_start, GFP_KERNEL); |
| 407 | |
| 408 | if (!info->screen_base) { |
| 409 | return -ENOMEM; |
| 410 | } |
| 411 | |
Haavard Skinnemoen | 01d3a5e | 2008-04-28 02:15:19 -0700 | [diff] [blame] | 412 | memset(info->screen_base, 0, info->fix.smem_len); |
| 413 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 414 | return 0; |
| 415 | } |
| 416 | |
Nicolas Ferre | 968910b | 2008-07-23 21:31:34 -0700 | [diff] [blame] | 417 | static const struct fb_videomode *atmel_lcdfb_choose_mode(struct fb_var_screeninfo *var, |
| 418 | struct fb_info *info) |
| 419 | { |
| 420 | struct fb_videomode varfbmode; |
| 421 | const struct fb_videomode *fbmode = NULL; |
| 422 | |
| 423 | fb_var_to_videomode(&varfbmode, var); |
| 424 | fbmode = fb_find_nearest_mode(&varfbmode, &info->modelist); |
| 425 | if (fbmode) |
| 426 | fb_videomode_to_var(var, fbmode); |
| 427 | return fbmode; |
| 428 | } |
| 429 | |
| 430 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 431 | /** |
| 432 | * atmel_lcdfb_check_var - Validates a var passed in. |
| 433 | * @var: frame buffer variable screen structure |
| 434 | * @info: frame buffer structure that represents a single frame buffer |
| 435 | * |
| 436 | * Checks to see if the hardware supports the state requested by |
| 437 | * var passed in. This function does not alter the hardware |
| 438 | * state!!! This means the data stored in struct fb_info and |
| 439 | * struct atmel_lcdfb_info do not change. This includes the var |
| 440 | * inside of struct fb_info. Do NOT change these. This function |
| 441 | * can be called on its own if we intent to only test a mode and |
| 442 | * not actually set it. The stuff in modedb.c is a example of |
| 443 | * this. If the var passed in is slightly off by what the |
| 444 | * hardware can support then we alter the var PASSED in to what |
| 445 | * we can do. If the hardware doesn't support mode change a |
| 446 | * -EINVAL will be returned by the upper layers. You don't need |
| 447 | * to implement this function then. If you hardware doesn't |
| 448 | * support changing the resolution then this function is not |
| 449 | * needed. In this case the driver would just provide a var that |
| 450 | * represents the static state the screen is in. |
| 451 | * |
| 452 | * Returns negative errno on error, or zero on success. |
| 453 | */ |
| 454 | static int atmel_lcdfb_check_var(struct fb_var_screeninfo *var, |
| 455 | struct fb_info *info) |
| 456 | { |
| 457 | struct device *dev = info->device; |
| 458 | struct atmel_lcdfb_info *sinfo = info->par; |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 459 | struct atmel_lcdfb_pdata *pdata = &sinfo->pdata; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 460 | unsigned long clk_value_khz; |
| 461 | |
| 462 | clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000; |
| 463 | |
| 464 | dev_dbg(dev, "%s:\n", __func__); |
Nicolas Ferre | 968910b | 2008-07-23 21:31:34 -0700 | [diff] [blame] | 465 | |
| 466 | if (!(var->pixclock && var->bits_per_pixel)) { |
| 467 | /* choose a suitable mode if possible */ |
| 468 | if (!atmel_lcdfb_choose_mode(var, info)) { |
| 469 | dev_err(dev, "needed value not specified\n"); |
| 470 | return -EINVAL; |
| 471 | } |
| 472 | } |
| 473 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 474 | dev_dbg(dev, " resolution: %ux%u\n", var->xres, var->yres); |
| 475 | dev_dbg(dev, " pixclk: %lu KHz\n", PICOS2KHZ(var->pixclock)); |
| 476 | dev_dbg(dev, " bpp: %u\n", var->bits_per_pixel); |
| 477 | dev_dbg(dev, " clk: %lu KHz\n", clk_value_khz); |
| 478 | |
Ben Nizette | 97b9a5a | 2009-06-16 15:34:24 -0700 | [diff] [blame] | 479 | if (PICOS2KHZ(var->pixclock) > clk_value_khz) { |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 480 | dev_err(dev, "%lu KHz pixel clock is too fast\n", PICOS2KHZ(var->pixclock)); |
| 481 | return -EINVAL; |
| 482 | } |
| 483 | |
Nicolas Ferre | 968910b | 2008-07-23 21:31:34 -0700 | [diff] [blame] | 484 | /* Do not allow to have real resoulution larger than virtual */ |
| 485 | if (var->xres > var->xres_virtual) |
| 486 | var->xres_virtual = var->xres; |
| 487 | |
| 488 | if (var->yres > var->yres_virtual) |
| 489 | var->yres_virtual = var->yres; |
| 490 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 491 | /* Force same alignment for each line */ |
| 492 | var->xres = (var->xres + 3) & ~3UL; |
| 493 | var->xres_virtual = (var->xres_virtual + 3) & ~3UL; |
| 494 | |
| 495 | var->red.msb_right = var->green.msb_right = var->blue.msb_right = 0; |
| 496 | var->transp.msb_right = 0; |
| 497 | var->transp.offset = var->transp.length = 0; |
| 498 | var->xoffset = var->yoffset = 0; |
| 499 | |
Stanislaw Gruszka | f928ac0a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 500 | if (info->fix.smem_len) { |
| 501 | unsigned int smem_len = (var->xres_virtual * var->yres_virtual |
| 502 | * ((var->bits_per_pixel + 7) / 8)); |
Richard Genoud | 65ac057 | 2013-05-31 14:28:38 +0000 | [diff] [blame] | 503 | if (smem_len > info->fix.smem_len) { |
| 504 | dev_err(dev, "Frame buffer is too small (%u) for screen size (need at least %u)\n", |
| 505 | info->fix.smem_len, smem_len); |
Stanislaw Gruszka | f928ac0a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 506 | return -EINVAL; |
Richard Genoud | 65ac057 | 2013-05-31 14:28:38 +0000 | [diff] [blame] | 507 | } |
Stanislaw Gruszka | f928ac0a | 2008-10-15 22:03:43 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Haavard Skinnemoen | 162b3a0 | 2008-02-06 01:39:11 -0800 | [diff] [blame] | 510 | /* Saturate vertical and horizontal timings at maximum values */ |
| 511 | var->vsync_len = min_t(u32, var->vsync_len, |
| 512 | (ATMEL_LCDC_VPW >> ATMEL_LCDC_VPW_OFFSET) + 1); |
| 513 | var->upper_margin = min_t(u32, var->upper_margin, |
| 514 | ATMEL_LCDC_VBP >> ATMEL_LCDC_VBP_OFFSET); |
| 515 | var->lower_margin = min_t(u32, var->lower_margin, |
| 516 | ATMEL_LCDC_VFP); |
| 517 | var->right_margin = min_t(u32, var->right_margin, |
Florian Tobias Schandinat | 6b3cbe4 | 2012-01-11 22:26:59 +0000 | [diff] [blame] | 518 | (ATMEL_LCDC_HFP >> ATMEL_LCDC_HFP_OFFSET) + 1); |
Haavard Skinnemoen | 162b3a0 | 2008-02-06 01:39:11 -0800 | [diff] [blame] | 519 | var->hsync_len = min_t(u32, var->hsync_len, |
| 520 | (ATMEL_LCDC_HPW >> ATMEL_LCDC_HPW_OFFSET) + 1); |
| 521 | var->left_margin = min_t(u32, var->left_margin, |
| 522 | ATMEL_LCDC_HBP + 1); |
| 523 | |
| 524 | /* Some parameters can't be zero */ |
| 525 | var->vsync_len = max_t(u32, var->vsync_len, 1); |
| 526 | var->right_margin = max_t(u32, var->right_margin, 1); |
| 527 | var->hsync_len = max_t(u32, var->hsync_len, 1); |
| 528 | var->left_margin = max_t(u32, var->left_margin, 1); |
| 529 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 530 | switch (var->bits_per_pixel) { |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 531 | case 1: |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 532 | case 2: |
| 533 | case 4: |
| 534 | case 8: |
| 535 | var->red.offset = var->green.offset = var->blue.offset = 0; |
| 536 | var->red.length = var->green.length = var->blue.length |
| 537 | = var->bits_per_pixel; |
| 538 | break; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 539 | case 16: |
Johan Hovold | a79eac7 | 2013-02-05 14:35:11 +0100 | [diff] [blame] | 540 | /* Older SOCs use IBGR:555 rather than BGR:565. */ |
Johan Hovold | bbd44f6b | 2013-02-07 16:31:58 +0100 | [diff] [blame] | 541 | if (sinfo->config->have_intensity_bit) |
Johan Hovold | a79eac7 | 2013-02-05 14:35:11 +0100 | [diff] [blame] | 542 | var->green.length = 5; |
| 543 | else |
| 544 | var->green.length = 6; |
| 545 | |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 546 | if (pdata->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) { |
Johan Hovold | a79eac7 | 2013-02-05 14:35:11 +0100 | [diff] [blame] | 547 | /* RGB:5X5 mode */ |
| 548 | var->red.offset = var->green.length + 5; |
Nicolas Ferre | fd08580 | 2008-04-28 02:15:21 -0700 | [diff] [blame] | 549 | var->blue.offset = 0; |
Nicolas Ferre | fd08580 | 2008-04-28 02:15:21 -0700 | [diff] [blame] | 550 | } else { |
Johan Hovold | a79eac7 | 2013-02-05 14:35:11 +0100 | [diff] [blame] | 551 | /* BGR:5X5 mode */ |
Nicolas Ferre | fd08580 | 2008-04-28 02:15:21 -0700 | [diff] [blame] | 552 | var->red.offset = 0; |
Johan Hovold | a79eac7 | 2013-02-05 14:35:11 +0100 | [diff] [blame] | 553 | var->blue.offset = var->green.length + 5; |
Nicolas Ferre | fd08580 | 2008-04-28 02:15:21 -0700 | [diff] [blame] | 554 | } |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 555 | var->green.offset = 5; |
Nicolas Ferre | fd08580 | 2008-04-28 02:15:21 -0700 | [diff] [blame] | 556 | var->red.length = var->blue.length = 5; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 557 | break; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 558 | case 32: |
Haavard Skinnemoen | 4440e0e | 2007-07-21 04:38:02 -0700 | [diff] [blame] | 559 | var->transp.offset = 24; |
| 560 | var->transp.length = 8; |
| 561 | /* fall through */ |
| 562 | case 24: |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 563 | if (pdata->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) { |
Nicolas Ferre | fd08580 | 2008-04-28 02:15:21 -0700 | [diff] [blame] | 564 | /* RGB:888 mode */ |
| 565 | var->red.offset = 16; |
| 566 | var->blue.offset = 0; |
| 567 | } else { |
| 568 | /* BGR:888 mode */ |
| 569 | var->red.offset = 0; |
| 570 | var->blue.offset = 16; |
| 571 | } |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 572 | var->green.offset = 8; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 573 | var->red.length = var->green.length = var->blue.length = 8; |
| 574 | break; |
| 575 | default: |
| 576 | dev_err(dev, "color depth %d not supported\n", |
| 577 | var->bits_per_pixel); |
| 578 | return -EINVAL; |
| 579 | } |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
Nicolas Ferre | d22579b | 2008-07-23 21:31:20 -0700 | [diff] [blame] | 584 | /* |
| 585 | * LCD reset sequence |
| 586 | */ |
| 587 | static void atmel_lcdfb_reset(struct atmel_lcdfb_info *sinfo) |
| 588 | { |
| 589 | might_sleep(); |
| 590 | |
Haavard Skinnemoen | 3aa04f1 | 2008-09-13 02:33:23 -0700 | [diff] [blame] | 591 | atmel_lcdfb_stop(sinfo); |
| 592 | atmel_lcdfb_start(sinfo); |
Nicolas Ferre | d22579b | 2008-07-23 21:31:20 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 595 | /** |
| 596 | * atmel_lcdfb_set_par - Alters the hardware state. |
| 597 | * @info: frame buffer structure that represents a single frame buffer |
| 598 | * |
| 599 | * Using the fb_var_screeninfo in fb_info we set the resolution |
| 600 | * of the this particular framebuffer. This function alters the |
| 601 | * par AND the fb_fix_screeninfo stored in fb_info. It doesn't |
| 602 | * not alter var in fb_info since we are using that data. This |
| 603 | * means we depend on the data in var inside fb_info to be |
| 604 | * supported by the hardware. atmel_lcdfb_check_var is always called |
| 605 | * before atmel_lcdfb_set_par to ensure this. Again if you can't |
| 606 | * change the resolution you don't need this function. |
| 607 | * |
| 608 | */ |
| 609 | static int atmel_lcdfb_set_par(struct fb_info *info) |
| 610 | { |
| 611 | struct atmel_lcdfb_info *sinfo = info->par; |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 612 | struct atmel_lcdfb_pdata *pdata = &sinfo->pdata; |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 613 | unsigned long hozval_linesz; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 614 | unsigned long value; |
| 615 | unsigned long clk_value_khz; |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 616 | unsigned long bits_per_line; |
Nicolas Ferre | 431861c | 2009-11-11 14:26:35 -0800 | [diff] [blame] | 617 | unsigned long pix_factor = 2; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 618 | |
Nicolas Ferre | d22579b | 2008-07-23 21:31:20 -0700 | [diff] [blame] | 619 | might_sleep(); |
| 620 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 621 | dev_dbg(info->device, "%s:\n", __func__); |
| 622 | dev_dbg(info->device, " * resolution: %ux%u (%ux%u virtual)\n", |
| 623 | info->var.xres, info->var.yres, |
| 624 | info->var.xres_virtual, info->var.yres_virtual); |
| 625 | |
Haavard Skinnemoen | 3aa04f1 | 2008-09-13 02:33:23 -0700 | [diff] [blame] | 626 | atmel_lcdfb_stop_nowait(sinfo); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 627 | |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 628 | if (info->var.bits_per_pixel == 1) |
| 629 | info->fix.visual = FB_VISUAL_MONO01; |
| 630 | else if (info->var.bits_per_pixel <= 8) |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 631 | info->fix.visual = FB_VISUAL_PSEUDOCOLOR; |
| 632 | else |
| 633 | info->fix.visual = FB_VISUAL_TRUECOLOR; |
| 634 | |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 635 | bits_per_line = info->var.xres_virtual * info->var.bits_per_pixel; |
| 636 | info->fix.line_length = DIV_ROUND_UP(bits_per_line, 8); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 637 | |
| 638 | /* Re-initialize the DMA engine... */ |
| 639 | dev_dbg(info->device, " * update DMA engine\n"); |
| 640 | atmel_lcdfb_update_dma(info, &info->var); |
| 641 | |
| 642 | /* ...set frame size and burst length = 8 words (?) */ |
| 643 | value = (info->var.yres * info->var.xres * info->var.bits_per_pixel) / 32; |
| 644 | value |= ((ATMEL_LCDC_DMA_BURST_LEN - 1) << ATMEL_LCDC_BLENGTH_OFFSET); |
| 645 | lcdc_writel(sinfo, ATMEL_LCDC_DMAFRMCFG, value); |
| 646 | |
| 647 | /* Now, the LCDC core... */ |
| 648 | |
| 649 | /* Set pixel clock */ |
Johan Hovold | bbd44f6b | 2013-02-07 16:31:58 +0100 | [diff] [blame] | 650 | if (sinfo->config->have_alt_pixclock) |
Nicolas Ferre | 431861c | 2009-11-11 14:26:35 -0800 | [diff] [blame] | 651 | pix_factor = 1; |
| 652 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 653 | clk_value_khz = clk_get_rate(sinfo->lcdc_clk) / 1000; |
| 654 | |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 655 | value = DIV_ROUND_UP(clk_value_khz, PICOS2KHZ(info->var.pixclock)); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 656 | |
Nicolas Ferre | 431861c | 2009-11-11 14:26:35 -0800 | [diff] [blame] | 657 | if (value < pix_factor) { |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 658 | dev_notice(info->device, "Bypassing pixel clock divider\n"); |
| 659 | lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, ATMEL_LCDC_BYPASS); |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 660 | } else { |
Nicolas Ferre | 431861c | 2009-11-11 14:26:35 -0800 | [diff] [blame] | 661 | value = (value / pix_factor) - 1; |
Nicolas Ferre | baf6332 | 2008-05-12 14:02:25 -0700 | [diff] [blame] | 662 | dev_dbg(info->device, " * programming CLKVAL = 0x%08lx\n", |
| 663 | value); |
| 664 | lcdc_writel(sinfo, ATMEL_LCDC_LCDCON1, |
| 665 | value << ATMEL_LCDC_CLKVAL_OFFSET); |
Nicolas Ferre | 431861c | 2009-11-11 14:26:35 -0800 | [diff] [blame] | 666 | info->var.pixclock = |
| 667 | KHZ2PICOS(clk_value_khz / (pix_factor * (value + 1))); |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 668 | dev_dbg(info->device, " updated pixclk: %lu KHz\n", |
| 669 | PICOS2KHZ(info->var.pixclock)); |
| 670 | } |
| 671 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 672 | |
| 673 | /* Initialize control register 2 */ |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 674 | value = pdata->default_lcdcon2; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 675 | |
| 676 | if (!(info->var.sync & FB_SYNC_HOR_HIGH_ACT)) |
| 677 | value |= ATMEL_LCDC_INVLINE_INVERTED; |
| 678 | if (!(info->var.sync & FB_SYNC_VERT_HIGH_ACT)) |
| 679 | value |= ATMEL_LCDC_INVFRAME_INVERTED; |
| 680 | |
| 681 | switch (info->var.bits_per_pixel) { |
| 682 | case 1: value |= ATMEL_LCDC_PIXELSIZE_1; break; |
| 683 | case 2: value |= ATMEL_LCDC_PIXELSIZE_2; break; |
| 684 | case 4: value |= ATMEL_LCDC_PIXELSIZE_4; break; |
| 685 | case 8: value |= ATMEL_LCDC_PIXELSIZE_8; break; |
| 686 | case 15: /* fall through */ |
| 687 | case 16: value |= ATMEL_LCDC_PIXELSIZE_16; break; |
| 688 | case 24: value |= ATMEL_LCDC_PIXELSIZE_24; break; |
| 689 | case 32: value |= ATMEL_LCDC_PIXELSIZE_32; break; |
| 690 | default: BUG(); break; |
| 691 | } |
| 692 | dev_dbg(info->device, " * LCDCON2 = %08lx\n", value); |
| 693 | lcdc_writel(sinfo, ATMEL_LCDC_LCDCON2, value); |
| 694 | |
| 695 | /* Vertical timing */ |
| 696 | value = (info->var.vsync_len - 1) << ATMEL_LCDC_VPW_OFFSET; |
| 697 | value |= info->var.upper_margin << ATMEL_LCDC_VBP_OFFSET; |
| 698 | value |= info->var.lower_margin; |
| 699 | dev_dbg(info->device, " * LCDTIM1 = %08lx\n", value); |
| 700 | lcdc_writel(sinfo, ATMEL_LCDC_TIM1, value); |
| 701 | |
| 702 | /* Horizontal timing */ |
Florian Tobias Schandinat | 6b3cbe4 | 2012-01-11 22:26:59 +0000 | [diff] [blame] | 703 | value = (info->var.right_margin - 1) << ATMEL_LCDC_HFP_OFFSET; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 704 | value |= (info->var.hsync_len - 1) << ATMEL_LCDC_HPW_OFFSET; |
| 705 | value |= (info->var.left_margin - 1); |
| 706 | dev_dbg(info->device, " * LCDTIM2 = %08lx\n", value); |
| 707 | lcdc_writel(sinfo, ATMEL_LCDC_TIM2, value); |
| 708 | |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 709 | /* Horizontal value (aka line size) */ |
Johan Hovold | 934a50b | 2013-02-07 16:31:57 +0100 | [diff] [blame] | 710 | hozval_linesz = compute_hozval(sinfo, info->var.xres); |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 711 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 712 | /* Display size */ |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 713 | value = (hozval_linesz - 1) << ATMEL_LCDC_HOZVAL_OFFSET; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 714 | value |= info->var.yres - 1; |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 715 | dev_dbg(info->device, " * LCDFRMCFG = %08lx\n", value); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 716 | lcdc_writel(sinfo, ATMEL_LCDC_LCDFRMCFG, value); |
| 717 | |
| 718 | /* FIFO Threshold: Use formula from data sheet */ |
| 719 | value = ATMEL_LCDC_FIFO_SIZE - (2 * ATMEL_LCDC_DMA_BURST_LEN + 3); |
| 720 | lcdc_writel(sinfo, ATMEL_LCDC_FIFO, value); |
| 721 | |
| 722 | /* Toggle LCD_MODE every frame */ |
| 723 | lcdc_writel(sinfo, ATMEL_LCDC_MVAL, 0); |
| 724 | |
| 725 | /* Disable all interrupts */ |
| 726 | lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL); |
Nicolas Ferre | d22579b | 2008-07-23 21:31:20 -0700 | [diff] [blame] | 727 | /* Enable FIFO & DMA errors */ |
| 728 | lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 729 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 730 | /* ...wait for DMA engine to become idle... */ |
| 731 | while (lcdc_readl(sinfo, ATMEL_LCDC_DMACON) & ATMEL_LCDC_DMABUSY) |
| 732 | msleep(10); |
| 733 | |
Haavard Skinnemoen | 3aa04f1 | 2008-09-13 02:33:23 -0700 | [diff] [blame] | 734 | atmel_lcdfb_start(sinfo); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 735 | |
| 736 | dev_dbg(info->device, " * DONE\n"); |
| 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | static inline unsigned int chan_to_field(unsigned int chan, const struct fb_bitfield *bf) |
| 742 | { |
| 743 | chan &= 0xffff; |
| 744 | chan >>= 16 - bf->length; |
| 745 | return chan << bf->offset; |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * atmel_lcdfb_setcolreg - Optional function. Sets a color register. |
| 750 | * @regno: Which register in the CLUT we are programming |
| 751 | * @red: The red value which can be up to 16 bits wide |
| 752 | * @green: The green value which can be up to 16 bits wide |
| 753 | * @blue: The blue value which can be up to 16 bits wide. |
| 754 | * @transp: If supported the alpha value which can be up to 16 bits wide. |
| 755 | * @info: frame buffer info structure |
| 756 | * |
| 757 | * Set a single color register. The values supplied have a 16 bit |
| 758 | * magnitude which needs to be scaled in this function for the hardware. |
| 759 | * Things to take into consideration are how many color registers, if |
| 760 | * any, are supported with the current color visual. With truecolor mode |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 761 | * no color palettes are supported. Here a pseudo palette is created |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 762 | * which we store the value in pseudo_palette in struct fb_info. For |
| 763 | * pseudocolor mode we have a limited color palette. To deal with this |
| 764 | * we can program what color is displayed for a particular pixel value. |
| 765 | * DirectColor is similar in that we can program each color field. If |
| 766 | * we have a static colormap we don't need to implement this function. |
| 767 | * |
| 768 | * Returns negative errno on error, or zero on success. In an |
| 769 | * ideal world, this would have been the case, but as it turns |
| 770 | * out, the other drivers return 1 on failure, so that's what |
| 771 | * we're going to do. |
| 772 | */ |
| 773 | static int atmel_lcdfb_setcolreg(unsigned int regno, unsigned int red, |
| 774 | unsigned int green, unsigned int blue, |
| 775 | unsigned int transp, struct fb_info *info) |
| 776 | { |
| 777 | struct atmel_lcdfb_info *sinfo = info->par; |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 778 | struct atmel_lcdfb_pdata *pdata = &sinfo->pdata; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 779 | unsigned int val; |
| 780 | u32 *pal; |
| 781 | int ret = 1; |
| 782 | |
| 783 | if (info->var.grayscale) |
| 784 | red = green = blue = (19595 * red + 38470 * green |
| 785 | + 7471 * blue) >> 16; |
| 786 | |
| 787 | switch (info->fix.visual) { |
| 788 | case FB_VISUAL_TRUECOLOR: |
| 789 | if (regno < 16) { |
| 790 | pal = info->pseudo_palette; |
| 791 | |
| 792 | val = chan_to_field(red, &info->var.red); |
| 793 | val |= chan_to_field(green, &info->var.green); |
| 794 | val |= chan_to_field(blue, &info->var.blue); |
| 795 | |
| 796 | pal[regno] = val; |
| 797 | ret = 0; |
| 798 | } |
| 799 | break; |
| 800 | |
| 801 | case FB_VISUAL_PSEUDOCOLOR: |
| 802 | if (regno < 256) { |
Johan Hovold | bbd44f6b | 2013-02-07 16:31:58 +0100 | [diff] [blame] | 803 | if (sinfo->config->have_intensity_bit) { |
Peter Korsgaard | 5d67b89 | 2011-10-13 16:45:52 +0200 | [diff] [blame] | 804 | /* old style I+BGR:555 */ |
| 805 | val = ((red >> 11) & 0x001f); |
| 806 | val |= ((green >> 6) & 0x03e0); |
| 807 | val |= ((blue >> 1) & 0x7c00); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 808 | |
Peter Korsgaard | 5d67b89 | 2011-10-13 16:45:52 +0200 | [diff] [blame] | 809 | /* |
| 810 | * TODO: intensity bit. Maybe something like |
| 811 | * ~(red[10] ^ green[10] ^ blue[10]) & 1 |
| 812 | */ |
| 813 | } else { |
| 814 | /* new style BGR:565 / RGB:565 */ |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 815 | if (pdata->lcd_wiring_mode == ATMEL_LCDC_WIRING_RGB) { |
Peter Korsgaard | 5d67b89 | 2011-10-13 16:45:52 +0200 | [diff] [blame] | 816 | val = ((blue >> 11) & 0x001f); |
| 817 | val |= ((red >> 0) & 0xf800); |
| 818 | } else { |
| 819 | val = ((red >> 11) & 0x001f); |
| 820 | val |= ((blue >> 0) & 0xf800); |
| 821 | } |
| 822 | |
| 823 | val |= ((green >> 5) & 0x07e0); |
| 824 | } |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 825 | |
| 826 | lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val); |
| 827 | ret = 0; |
| 828 | } |
| 829 | break; |
Nicolas Ferre | 250a269 | 2007-07-21 04:37:59 -0700 | [diff] [blame] | 830 | |
| 831 | case FB_VISUAL_MONO01: |
| 832 | if (regno < 2) { |
| 833 | val = (regno == 0) ? 0x00 : 0x1F; |
| 834 | lcdc_writel(sinfo, ATMEL_LCDC_LUT(regno), val); |
| 835 | ret = 0; |
| 836 | } |
| 837 | break; |
| 838 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | return ret; |
| 842 | } |
| 843 | |
| 844 | static int atmel_lcdfb_pan_display(struct fb_var_screeninfo *var, |
| 845 | struct fb_info *info) |
| 846 | { |
| 847 | dev_dbg(info->device, "%s\n", __func__); |
| 848 | |
| 849 | atmel_lcdfb_update_dma(info, var); |
| 850 | |
| 851 | return 0; |
| 852 | } |
| 853 | |
Andreas Bießmann | bed7bdd | 2011-02-11 15:19:44 +0000 | [diff] [blame] | 854 | static int atmel_lcdfb_blank(int blank_mode, struct fb_info *info) |
| 855 | { |
| 856 | struct atmel_lcdfb_info *sinfo = info->par; |
| 857 | |
| 858 | switch (blank_mode) { |
| 859 | case FB_BLANK_UNBLANK: |
| 860 | case FB_BLANK_NORMAL: |
| 861 | atmel_lcdfb_start(sinfo); |
| 862 | break; |
| 863 | case FB_BLANK_VSYNC_SUSPEND: |
| 864 | case FB_BLANK_HSYNC_SUSPEND: |
| 865 | break; |
| 866 | case FB_BLANK_POWERDOWN: |
| 867 | atmel_lcdfb_stop(sinfo); |
| 868 | break; |
| 869 | default: |
| 870 | return -EINVAL; |
| 871 | } |
| 872 | |
| 873 | /* let fbcon do a soft blank for us */ |
| 874 | return ((blank_mode == FB_BLANK_NORMAL) ? 1 : 0); |
| 875 | } |
| 876 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 877 | static struct fb_ops atmel_lcdfb_ops = { |
| 878 | .owner = THIS_MODULE, |
| 879 | .fb_check_var = atmel_lcdfb_check_var, |
| 880 | .fb_set_par = atmel_lcdfb_set_par, |
| 881 | .fb_setcolreg = atmel_lcdfb_setcolreg, |
Andreas Bießmann | bed7bdd | 2011-02-11 15:19:44 +0000 | [diff] [blame] | 882 | .fb_blank = atmel_lcdfb_blank, |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 883 | .fb_pan_display = atmel_lcdfb_pan_display, |
| 884 | .fb_fillrect = cfb_fillrect, |
| 885 | .fb_copyarea = cfb_copyarea, |
| 886 | .fb_imageblit = cfb_imageblit, |
| 887 | }; |
| 888 | |
| 889 | static irqreturn_t atmel_lcdfb_interrupt(int irq, void *dev_id) |
| 890 | { |
| 891 | struct fb_info *info = dev_id; |
| 892 | struct atmel_lcdfb_info *sinfo = info->par; |
| 893 | u32 status; |
| 894 | |
| 895 | status = lcdc_readl(sinfo, ATMEL_LCDC_ISR); |
Nicolas Ferre | d22579b | 2008-07-23 21:31:20 -0700 | [diff] [blame] | 896 | if (status & ATMEL_LCDC_UFLWI) { |
| 897 | dev_warn(info->device, "FIFO underflow %#x\n", status); |
| 898 | /* reset DMA and FIFO to avoid screen shifting */ |
| 899 | schedule_work(&sinfo->task); |
| 900 | } |
| 901 | lcdc_writel(sinfo, ATMEL_LCDC_ICR, status); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 902 | return IRQ_HANDLED; |
| 903 | } |
| 904 | |
Nicolas Ferre | d22579b | 2008-07-23 21:31:20 -0700 | [diff] [blame] | 905 | /* |
| 906 | * LCD controller task (to reset the LCD) |
| 907 | */ |
| 908 | static void atmel_lcdfb_task(struct work_struct *work) |
| 909 | { |
| 910 | struct atmel_lcdfb_info *sinfo = |
| 911 | container_of(work, struct atmel_lcdfb_info, task); |
| 912 | |
| 913 | atmel_lcdfb_reset(sinfo); |
| 914 | } |
| 915 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 916 | static int __init atmel_lcdfb_init_fbinfo(struct atmel_lcdfb_info *sinfo) |
| 917 | { |
| 918 | struct fb_info *info = sinfo->info; |
| 919 | int ret = 0; |
| 920 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 921 | info->var.activate |= FB_ACTIVATE_FORCE | FB_ACTIVATE_NOW; |
| 922 | |
| 923 | dev_info(info->device, |
| 924 | "%luKiB frame buffer at %08lx (mapped at %p)\n", |
| 925 | (unsigned long)info->fix.smem_len / 1024, |
| 926 | (unsigned long)info->fix.smem_start, |
| 927 | info->screen_base); |
| 928 | |
| 929 | /* Allocate colormap */ |
| 930 | ret = fb_alloc_cmap(&info->cmap, 256, 0); |
| 931 | if (ret < 0) |
| 932 | dev_err(info->device, "Alloc color map failed\n"); |
| 933 | |
| 934 | return ret; |
| 935 | } |
| 936 | |
| 937 | static void atmel_lcdfb_start_clock(struct atmel_lcdfb_info *sinfo) |
| 938 | { |
Boris BREZILLON | 6e665fb | 2013-07-18 09:40:25 +0200 | [diff] [blame] | 939 | clk_prepare_enable(sinfo->bus_clk); |
| 940 | clk_prepare_enable(sinfo->lcdc_clk); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | static void atmel_lcdfb_stop_clock(struct atmel_lcdfb_info *sinfo) |
| 944 | { |
Boris BREZILLON | 6e665fb | 2013-07-18 09:40:25 +0200 | [diff] [blame] | 945 | clk_disable_unprepare(sinfo->bus_clk); |
| 946 | clk_disable_unprepare(sinfo->lcdc_clk); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | |
| 950 | static int __init atmel_lcdfb_probe(struct platform_device *pdev) |
| 951 | { |
| 952 | struct device *dev = &pdev->dev; |
| 953 | struct fb_info *info; |
| 954 | struct atmel_lcdfb_info *sinfo; |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 955 | struct atmel_lcdfb_pdata *pdata; |
Nicolas Ferre | 968910b | 2008-07-23 21:31:34 -0700 | [diff] [blame] | 956 | struct fb_videomode fbmode; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 957 | struct resource *regs = NULL; |
| 958 | struct resource *map = NULL; |
| 959 | int ret; |
| 960 | |
| 961 | dev_dbg(dev, "%s BEGIN\n", __func__); |
| 962 | |
| 963 | ret = -ENOMEM; |
| 964 | info = framebuffer_alloc(sizeof(struct atmel_lcdfb_info), dev); |
| 965 | if (!info) { |
| 966 | dev_err(dev, "cannot allocate memory\n"); |
| 967 | goto out; |
| 968 | } |
| 969 | |
| 970 | sinfo = info->par; |
| 971 | |
Jingoo Han | a5d58be | 2013-09-17 14:06:39 +0900 | [diff] [blame] | 972 | if (dev_get_platdata(dev)) { |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 973 | pdata = dev_get_platdata(dev); |
| 974 | sinfo->pdata = *pdata; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 975 | } else { |
| 976 | dev_err(dev, "cannot get default configuration\n"); |
| 977 | goto free_info; |
| 978 | } |
| 979 | sinfo->info = info; |
| 980 | sinfo->pdev = pdev; |
Johan Hovold | bbd44f6b | 2013-02-07 16:31:58 +0100 | [diff] [blame] | 981 | sinfo->config = atmel_lcdfb_get_config(pdev); |
| 982 | if (!sinfo->config) |
| 983 | goto free_info; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 984 | |
| 985 | strcpy(info->fix.id, sinfo->pdev->name); |
| 986 | info->flags = ATMEL_LCDFB_FBINFO_DEFAULT; |
| 987 | info->pseudo_palette = sinfo->pseudo_palette; |
| 988 | info->fbops = &atmel_lcdfb_ops; |
| 989 | |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 990 | memcpy(&info->monspecs, pdata->default_monspecs, sizeof(info->monspecs)); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 991 | info->fix = atmel_lcdfb_fix; |
| 992 | |
| 993 | /* Enable LCDC Clocks */ |
Johan Hovold | 557b7d5 | 2013-02-07 16:31:56 +0100 | [diff] [blame] | 994 | sinfo->bus_clk = clk_get(dev, "hclk"); |
| 995 | if (IS_ERR(sinfo->bus_clk)) { |
| 996 | ret = PTR_ERR(sinfo->bus_clk); |
| 997 | goto free_info; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 998 | } |
| 999 | sinfo->lcdc_clk = clk_get(dev, "lcdc_clk"); |
| 1000 | if (IS_ERR(sinfo->lcdc_clk)) { |
| 1001 | ret = PTR_ERR(sinfo->lcdc_clk); |
| 1002 | goto put_bus_clk; |
| 1003 | } |
| 1004 | atmel_lcdfb_start_clock(sinfo); |
| 1005 | |
| 1006 | ret = fb_find_mode(&info->var, info, NULL, info->monspecs.modedb, |
| 1007 | info->monspecs.modedb_len, info->monspecs.modedb, |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 1008 | pdata->default_bpp); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1009 | if (!ret) { |
| 1010 | dev_err(dev, "no suitable video mode found\n"); |
| 1011 | goto stop_clk; |
| 1012 | } |
| 1013 | |
| 1014 | |
| 1015 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1016 | if (!regs) { |
| 1017 | dev_err(dev, "resources unusable\n"); |
| 1018 | ret = -ENXIO; |
| 1019 | goto stop_clk; |
| 1020 | } |
| 1021 | |
| 1022 | sinfo->irq_base = platform_get_irq(pdev, 0); |
| 1023 | if (sinfo->irq_base < 0) { |
| 1024 | dev_err(dev, "unable to get irq\n"); |
| 1025 | ret = sinfo->irq_base; |
| 1026 | goto stop_clk; |
| 1027 | } |
| 1028 | |
| 1029 | /* Initialize video memory */ |
| 1030 | map = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1031 | if (map) { |
| 1032 | /* use a pre-allocated memory buffer */ |
| 1033 | info->fix.smem_start = map->start; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 1034 | info->fix.smem_len = resource_size(map); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1035 | if (!request_mem_region(info->fix.smem_start, |
| 1036 | info->fix.smem_len, pdev->name)) { |
| 1037 | ret = -EBUSY; |
| 1038 | goto stop_clk; |
| 1039 | } |
| 1040 | |
| 1041 | info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len); |
Peter Senna Tschudin | 130320b | 2012-09-18 14:07:55 +0200 | [diff] [blame] | 1042 | if (!info->screen_base) { |
| 1043 | ret = -ENOMEM; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1044 | goto release_intmem; |
Peter Senna Tschudin | 130320b | 2012-09-18 14:07:55 +0200 | [diff] [blame] | 1045 | } |
Haavard Skinnemoen | 01d3a5e | 2008-04-28 02:15:19 -0700 | [diff] [blame] | 1046 | |
| 1047 | /* |
| 1048 | * Don't clear the framebuffer -- someone may have set |
| 1049 | * up a splash image. |
| 1050 | */ |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1051 | } else { |
Masanari Iida | ff0c264 | 2012-07-22 00:23:15 +0900 | [diff] [blame] | 1052 | /* allocate memory buffer */ |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1053 | ret = atmel_lcdfb_alloc_video_memory(sinfo); |
| 1054 | if (ret < 0) { |
| 1055 | dev_err(dev, "cannot allocate framebuffer: %d\n", ret); |
| 1056 | goto stop_clk; |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | /* LCDC registers */ |
| 1061 | info->fix.mmio_start = regs->start; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 1062 | info->fix.mmio_len = resource_size(regs); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1063 | |
| 1064 | if (!request_mem_region(info->fix.mmio_start, |
| 1065 | info->fix.mmio_len, pdev->name)) { |
| 1066 | ret = -EBUSY; |
| 1067 | goto free_fb; |
| 1068 | } |
| 1069 | |
| 1070 | sinfo->mmio = ioremap(info->fix.mmio_start, info->fix.mmio_len); |
| 1071 | if (!sinfo->mmio) { |
| 1072 | dev_err(dev, "cannot map LCDC registers\n"); |
Peter Senna Tschudin | 130320b | 2012-09-18 14:07:55 +0200 | [diff] [blame] | 1073 | ret = -ENOMEM; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1074 | goto release_mem; |
| 1075 | } |
| 1076 | |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 1077 | /* Initialize PWM for contrast or backlight ("off") */ |
| 1078 | init_contrast(sinfo); |
| 1079 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1080 | /* interrupt */ |
| 1081 | ret = request_irq(sinfo->irq_base, atmel_lcdfb_interrupt, 0, pdev->name, info); |
| 1082 | if (ret) { |
| 1083 | dev_err(dev, "request_irq failed: %d\n", ret); |
| 1084 | goto unmap_mmio; |
| 1085 | } |
| 1086 | |
Nicolas Ferre | d22579b | 2008-07-23 21:31:20 -0700 | [diff] [blame] | 1087 | /* Some operations on the LCDC might sleep and |
| 1088 | * require a preemptible task context */ |
| 1089 | INIT_WORK(&sinfo->task, atmel_lcdfb_task); |
| 1090 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1091 | ret = atmel_lcdfb_init_fbinfo(sinfo); |
| 1092 | if (ret < 0) { |
| 1093 | dev_err(dev, "init fbinfo failed: %d\n", ret); |
| 1094 | goto unregister_irqs; |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * This makes sure that our colour bitfield |
| 1099 | * descriptors are correctly initialised. |
| 1100 | */ |
| 1101 | atmel_lcdfb_check_var(&info->var, info); |
| 1102 | |
| 1103 | ret = fb_set_var(info, &info->var); |
| 1104 | if (ret) { |
| 1105 | dev_warn(dev, "unable to set display parameters\n"); |
| 1106 | goto free_cmap; |
| 1107 | } |
| 1108 | |
| 1109 | dev_set_drvdata(dev, info); |
| 1110 | |
| 1111 | /* |
| 1112 | * Tell the world that we're ready to go |
| 1113 | */ |
| 1114 | ret = register_framebuffer(info); |
| 1115 | if (ret < 0) { |
| 1116 | dev_err(dev, "failed to register framebuffer device: %d\n", ret); |
Sachin Kamat | a36bf19 | 2013-09-20 12:02:10 +0530 | [diff] [blame] | 1117 | goto free_cmap; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Nicolas Ferre | 968910b | 2008-07-23 21:31:34 -0700 | [diff] [blame] | 1120 | /* add selected videomode to modelist */ |
| 1121 | fb_var_to_videomode(&fbmode, &info->var); |
| 1122 | fb_add_videomode(&fbmode, &info->modelist); |
| 1123 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1124 | /* Power up the LCDC screen */ |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 1125 | if (pdata->atmel_lcdfb_power_control) |
| 1126 | pdata->atmel_lcdfb_power_control(1); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1127 | |
Claudio Scordino | 93f6ced | 2009-10-09 12:20:21 +0200 | [diff] [blame] | 1128 | dev_info(dev, "fb%d: Atmel LCDC at 0x%08lx (mapped at %p), irq %d\n", |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1129 | info->node, info->fix.mmio_start, sinfo->mmio, sinfo->irq_base); |
| 1130 | |
| 1131 | return 0; |
| 1132 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1133 | free_cmap: |
| 1134 | fb_dealloc_cmap(&info->cmap); |
| 1135 | unregister_irqs: |
Nicolas Ferre | d22579b | 2008-07-23 21:31:20 -0700 | [diff] [blame] | 1136 | cancel_work_sync(&sinfo->task); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1137 | free_irq(sinfo->irq_base, info); |
| 1138 | unmap_mmio: |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 1139 | exit_backlight(sinfo); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1140 | iounmap(sinfo->mmio); |
| 1141 | release_mem: |
| 1142 | release_mem_region(info->fix.mmio_start, info->fix.mmio_len); |
| 1143 | free_fb: |
| 1144 | if (map) |
| 1145 | iounmap(info->screen_base); |
| 1146 | else |
| 1147 | atmel_lcdfb_free_video_memory(sinfo); |
| 1148 | |
| 1149 | release_intmem: |
| 1150 | if (map) |
| 1151 | release_mem_region(info->fix.smem_start, info->fix.smem_len); |
| 1152 | stop_clk: |
| 1153 | atmel_lcdfb_stop_clock(sinfo); |
| 1154 | clk_put(sinfo->lcdc_clk); |
| 1155 | put_bus_clk: |
Johan Hovold | 557b7d5 | 2013-02-07 16:31:56 +0100 | [diff] [blame] | 1156 | clk_put(sinfo->bus_clk); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1157 | free_info: |
| 1158 | framebuffer_release(info); |
| 1159 | out: |
| 1160 | dev_dbg(dev, "%s FAILED\n", __func__); |
| 1161 | return ret; |
| 1162 | } |
| 1163 | |
| 1164 | static int __exit atmel_lcdfb_remove(struct platform_device *pdev) |
| 1165 | { |
| 1166 | struct device *dev = &pdev->dev; |
| 1167 | struct fb_info *info = dev_get_drvdata(dev); |
Stanislaw Gruszka | 34a35bd | 2008-09-05 14:00:22 -0700 | [diff] [blame] | 1168 | struct atmel_lcdfb_info *sinfo; |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 1169 | struct atmel_lcdfb_pdata *pdata; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1170 | |
Stanislaw Gruszka | 34a35bd | 2008-09-05 14:00:22 -0700 | [diff] [blame] | 1171 | if (!info || !info->par) |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1172 | return 0; |
Stanislaw Gruszka | 34a35bd | 2008-09-05 14:00:22 -0700 | [diff] [blame] | 1173 | sinfo = info->par; |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 1174 | pdata = &sinfo->pdata; |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1175 | |
Nicolas Ferre | d22579b | 2008-07-23 21:31:20 -0700 | [diff] [blame] | 1176 | cancel_work_sync(&sinfo->task); |
David Brownell | a9a84c3 | 2008-02-06 01:39:26 -0800 | [diff] [blame] | 1177 | exit_backlight(sinfo); |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 1178 | if (pdata->atmel_lcdfb_power_control) |
| 1179 | pdata->atmel_lcdfb_power_control(0); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1180 | unregister_framebuffer(info); |
| 1181 | atmel_lcdfb_stop_clock(sinfo); |
| 1182 | clk_put(sinfo->lcdc_clk); |
Johan Hovold | 557b7d5 | 2013-02-07 16:31:56 +0100 | [diff] [blame] | 1183 | clk_put(sinfo->bus_clk); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1184 | fb_dealloc_cmap(&info->cmap); |
| 1185 | free_irq(sinfo->irq_base, info); |
| 1186 | iounmap(sinfo->mmio); |
| 1187 | release_mem_region(info->fix.mmio_start, info->fix.mmio_len); |
| 1188 | if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) { |
| 1189 | iounmap(info->screen_base); |
| 1190 | release_mem_region(info->fix.smem_start, info->fix.smem_len); |
| 1191 | } else { |
| 1192 | atmel_lcdfb_free_video_memory(sinfo); |
| 1193 | } |
| 1194 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1195 | framebuffer_release(info); |
| 1196 | |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
David Brownell | cf19a37 | 2008-04-28 02:15:20 -0700 | [diff] [blame] | 1200 | #ifdef CONFIG_PM |
| 1201 | |
| 1202 | static int atmel_lcdfb_suspend(struct platform_device *pdev, pm_message_t mesg) |
| 1203 | { |
| 1204 | struct fb_info *info = platform_get_drvdata(pdev); |
| 1205 | struct atmel_lcdfb_info *sinfo = info->par; |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 1206 | struct atmel_lcdfb_pdata *pdata = &sinfo->pdata; |
David Brownell | cf19a37 | 2008-04-28 02:15:20 -0700 | [diff] [blame] | 1207 | |
Haavard Skinnemoen | 3aa04f1 | 2008-09-13 02:33:23 -0700 | [diff] [blame] | 1208 | /* |
| 1209 | * We don't want to handle interrupts while the clock is |
| 1210 | * stopped. It may take forever. |
| 1211 | */ |
| 1212 | lcdc_writel(sinfo, ATMEL_LCDC_IDR, ~0UL); |
| 1213 | |
Hubert Feurstein | 9f10650 | 2012-01-09 17:23:57 +0100 | [diff] [blame] | 1214 | sinfo->saved_lcdcon = lcdc_readl(sinfo, ATMEL_LCDC_CONTRAST_CTR); |
David Brownell | cf19a37 | 2008-04-28 02:15:20 -0700 | [diff] [blame] | 1215 | lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, 0); |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 1216 | if (pdata->atmel_lcdfb_power_control) |
| 1217 | pdata->atmel_lcdfb_power_control(0); |
Haavard Skinnemoen | 3aa04f1 | 2008-09-13 02:33:23 -0700 | [diff] [blame] | 1218 | |
| 1219 | atmel_lcdfb_stop(sinfo); |
David Brownell | cf19a37 | 2008-04-28 02:15:20 -0700 | [diff] [blame] | 1220 | atmel_lcdfb_stop_clock(sinfo); |
Haavard Skinnemoen | 3aa04f1 | 2008-09-13 02:33:23 -0700 | [diff] [blame] | 1221 | |
David Brownell | cf19a37 | 2008-04-28 02:15:20 -0700 | [diff] [blame] | 1222 | return 0; |
| 1223 | } |
| 1224 | |
| 1225 | static int atmel_lcdfb_resume(struct platform_device *pdev) |
| 1226 | { |
| 1227 | struct fb_info *info = platform_get_drvdata(pdev); |
| 1228 | struct atmel_lcdfb_info *sinfo = info->par; |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 1229 | struct atmel_lcdfb_pdata *pdata = &sinfo->pdata; |
David Brownell | cf19a37 | 2008-04-28 02:15:20 -0700 | [diff] [blame] | 1230 | |
| 1231 | atmel_lcdfb_start_clock(sinfo); |
Haavard Skinnemoen | 3aa04f1 | 2008-09-13 02:33:23 -0700 | [diff] [blame] | 1232 | atmel_lcdfb_start(sinfo); |
Jean-Christophe PLAGNIOL-VILLARD | 8af2c28 | 2013-03-28 22:53:42 +0800 | [diff] [blame^] | 1233 | if (pdata->atmel_lcdfb_power_control) |
| 1234 | pdata->atmel_lcdfb_power_control(1); |
David Brownell | cf19a37 | 2008-04-28 02:15:20 -0700 | [diff] [blame] | 1235 | lcdc_writel(sinfo, ATMEL_LCDC_CONTRAST_CTR, sinfo->saved_lcdcon); |
Haavard Skinnemoen | 3aa04f1 | 2008-09-13 02:33:23 -0700 | [diff] [blame] | 1236 | |
| 1237 | /* Enable FIFO & DMA errors */ |
| 1238 | lcdc_writel(sinfo, ATMEL_LCDC_IER, ATMEL_LCDC_UFLWI |
| 1239 | | ATMEL_LCDC_OWRI | ATMEL_LCDC_MERI); |
| 1240 | |
David Brownell | cf19a37 | 2008-04-28 02:15:20 -0700 | [diff] [blame] | 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | #else |
| 1245 | #define atmel_lcdfb_suspend NULL |
| 1246 | #define atmel_lcdfb_resume NULL |
| 1247 | #endif |
| 1248 | |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1249 | static struct platform_driver atmel_lcdfb_driver = { |
| 1250 | .remove = __exit_p(atmel_lcdfb_remove), |
David Brownell | cf19a37 | 2008-04-28 02:15:20 -0700 | [diff] [blame] | 1251 | .suspend = atmel_lcdfb_suspend, |
| 1252 | .resume = atmel_lcdfb_resume, |
Johan Hovold | bbd44f6b | 2013-02-07 16:31:58 +0100 | [diff] [blame] | 1253 | .id_table = atmel_lcdfb_devtypes, |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1254 | .driver = { |
| 1255 | .name = "atmel_lcdfb", |
| 1256 | .owner = THIS_MODULE, |
| 1257 | }, |
| 1258 | }; |
| 1259 | |
Fabio Porcedda | 3ccbf89 | 2013-03-15 14:02:38 +0100 | [diff] [blame] | 1260 | module_platform_driver_probe(atmel_lcdfb_driver, atmel_lcdfb_probe); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1261 | |
| 1262 | MODULE_DESCRIPTION("AT91/AT32 LCD Controller framebuffer driver"); |
Nicolas Ferre | 8f4c79c | 2008-01-14 00:55:13 -0800 | [diff] [blame] | 1263 | MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>"); |
Nicolas Ferre | 1434058 | 2007-05-10 22:23:26 -0700 | [diff] [blame] | 1264 | MODULE_LICENSE("GPL"); |