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