fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / video / aty / aty128fb.c
index b7fc32c..2e976ff 100644 (file)
  */
 
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/kernel.h>
 #include <linux/errno.h>
 #include <linux/string.h>
 #include <linux/mm.h>
-#include <linux/tty.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/delay.h>
 #include <linux/pci.h>
 #include <linux/ioport.h>
 #include <linux/console.h>
+#include <linux/backlight.h>
 #include <asm/io.h>
 
 #ifdef CONFIG_PPC_PMAC
+#include <asm/machdep.h>
 #include <asm/pmac_feature.h>
 #include <asm/prom.h>
 #include <asm/pci-bridge.h>
@@ -98,7 +98,7 @@
 
 #ifndef CONFIG_PPC_PMAC
 /* default mode */
-static struct fb_var_screeninfo default_var __initdata = {
+static struct fb_var_screeninfo default_var __devinitdata = {
        /* 640x480, 60 Hz, Non-Interlaced (25.175 MHz dotclock) */
        640, 480, 640, 480, 0, 0, 8, 0,
        {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0},
@@ -121,7 +121,7 @@ static struct fb_var_screeninfo default_var = {
 
 /* default modedb mode */
 /* 640x480, 60 Hz, Non-Interlaced (25.172 MHz dotclock) */
-static struct fb_videomode defaultmode __initdata = {
+static struct fb_videomode defaultmode __devinitdata = {
        .refresh =      60,
        .xres =         640,
        .yres =         480,
@@ -166,7 +166,7 @@ static const char *r128_family[] __devinitdata = {
 static int aty128_probe(struct pci_dev *pdev,
                                const struct pci_device_id *ent);
 static void aty128_remove(struct pci_dev *pdev);
-static int aty128_pci_suspend(struct pci_dev *pdev, u32 state);
+static int aty128_pci_suspend(struct pci_dev *pdev, pm_message_t state);
 static int aty128_pci_resume(struct pci_dev *pdev);
 static int aty128_do_resume(struct pci_dev *pdev);
 
@@ -333,7 +333,7 @@ static const struct aty128_meminfo sdr_sgram =
 static const struct aty128_meminfo ddr_sgram =
        { 4, 4, 3, 3, 2, 3, 1, 16, 31, 16, "64-bit DDR SGRAM" };
 
-static struct fb_fix_screeninfo aty128fb_fix __initdata = {
+static struct fb_fix_screeninfo aty128fb_fix __devinitdata = {
        .id             = "ATY Rage128",
        .type           = FB_TYPE_PACKED_PIXELS,
        .visual         = FB_VISUAL_PSEUDOCOLOR,
@@ -343,17 +343,15 @@ static struct fb_fix_screeninfo aty128fb_fix __initdata = {
        .accel          = FB_ACCEL_ATI_RAGE128,
 };
 
-static char *mode_option __initdata = NULL;
+static char *mode_option __devinitdata = NULL;
 
 #ifdef CONFIG_PPC_PMAC
-static int default_vmode __initdata = VMODE_1024_768_60;
-static int default_cmode __initdata = CMODE_8;
+static int default_vmode __devinitdata = VMODE_1024_768_60;
+static int default_cmode __devinitdata = CMODE_8;
 #endif
 
-#ifdef CONFIG_PMAC_PBOOK
-static int default_crt_on __initdata = 0;
-static int default_lcd_on __initdata = 1;
-#endif
+static int default_crt_on __devinitdata = 0;
+static int default_lcd_on __devinitdata = 1;
 
 #ifdef CONFIG_MTRR
 static int mtrr = 1;
@@ -425,11 +423,6 @@ struct aty128fb_par {
 
 #define round_div(n, d) ((n+(d/2))/d)
 
-    /*
-     *  Interface used by the world
-     */
-int aty128fb_init(void);
-
 static int aty128fb_check_var(struct fb_var_screeninfo *var,
                              struct fb_info *info);
 static int aty128fb_set_par(struct fb_info *info);
@@ -438,8 +431,7 @@ static int aty128fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
 static int aty128fb_pan_display(struct fb_var_screeninfo *var,
                           struct fb_info *fb);
 static int aty128fb_blank(int blank, struct fb_info *fb);
-static int aty128fb_ioctl(struct inode *inode, struct file *file, u_int cmd,
-                         u_long arg, struct fb_info *info);
+static int aty128fb_ioctl(struct fb_info *info, u_int cmd, unsigned long arg);
 static int aty128fb_sync(struct fb_info *info);
 
     /*
@@ -451,9 +443,9 @@ static int aty128_encode_var(struct fb_var_screeninfo *var,
 static int aty128_decode_var(struct fb_var_screeninfo *var,
                              struct aty128fb_par *par);
 #if 0
-static void __init aty128_get_pllinfo(struct aty128fb_par *par,
+static void __devinit aty128_get_pllinfo(struct aty128fb_par *par,
                                      void __iomem *bios);
-static void __init __iomem *aty128_map_ROM(struct pci_dev *pdev, const struct aty128fb_par *par);
+static void __devinit __iomem *aty128_map_ROM(struct pci_dev *pdev, const struct aty128fb_par *par);
 #endif
 static void aty128_timings(struct aty128fb_par *par);
 static void aty128_init_engine(struct aty128fb_par *par);
@@ -464,6 +456,10 @@ static void wait_for_fifo(u16 entries, struct aty128fb_par *par);
 static void wait_for_idle(struct aty128fb_par *par);
 static u32 depth_to_dst(u32 depth);
 
+#ifdef CONFIG_FB_ATY128_BACKLIGHT
+static void aty128_bl_set_power(struct fb_info *info, int power);
+#endif
+
 #define BIOS_IN8(v)    (readb(bios + (v)))
 #define BIOS_IN16(v)   (readb(bios + (v)) | \
                          (readb(bios + (v) + 1) << 8))
@@ -485,18 +481,7 @@ static struct fb_ops aty128fb_ops = {
        .fb_fillrect    = cfb_fillrect,
        .fb_copyarea    = cfb_copyarea,
        .fb_imageblit   = cfb_imageblit,
-       .fb_cursor      = soft_cursor,
-};
-
-#ifdef CONFIG_PMAC_BACKLIGHT
-static int aty128_set_backlight_enable(int on, int level, void* data);
-static int aty128_set_backlight_level(int level, void* data);
-
-static struct backlight_controller aty128_backlight_controller = {
-       aty128_set_backlight_enable,
-       aty128_set_backlight_level
 };
-#endif /* CONFIG_PMAC_BACKLIGHT */
 
     /*
      * Functions to read from/write to the mmio registers
@@ -590,7 +575,7 @@ static void aty_pll_writeupdate(const struct aty128fb_par *par)
 
 
 /* write to the scratch register to test r/w functionality */
-static int __init register_test(const struct aty128fb_par *par)
+static int __devinit register_test(const struct aty128fb_par *par)
 {
        u32 val;
        int flag = 0;
@@ -789,7 +774,7 @@ static u32 depth_to_dst(u32 depth)
 
 
 #ifndef __sparc__
-static void __iomem * __init aty128_map_ROM(const struct aty128fb_par *par, struct pci_dev *dev)
+static void __iomem * __devinit aty128_map_ROM(const struct aty128fb_par *par, struct pci_dev *dev)
 {
        u16 dptr;
        u8 rom_type;
@@ -813,8 +798,8 @@ static void __iomem * __init aty128_map_ROM(const struct aty128fb_par *par, stru
 
        /* Very simple test to make sure it appeared */
        if (BIOS_IN16(0) != 0xaa55) {
-               printk(KERN_ERR "aty128fb: Invalid ROM signature %x should be 0xaa55\n",
-                      BIOS_IN16(0));
+               printk(KERN_DEBUG "aty128fb: Invalid ROM signature %x should "
+                       " be 0xaa55\n", BIOS_IN16(0));
                goto failed;
        }
 
@@ -873,7 +858,7 @@ static void __iomem * __init aty128_map_ROM(const struct aty128fb_par *par, stru
        return NULL;
 }
 
-static void __init aty128_get_pllinfo(struct aty128fb_par *par, unsigned char __iomem *bios)
+static void __devinit aty128_get_pllinfo(struct aty128fb_par *par, unsigned char __iomem *bios)
 {
        unsigned int bios_hdr;
        unsigned int bios_pll;
@@ -920,7 +905,7 @@ static void __iomem *  __devinit aty128_find_mem_vbios(struct aty128fb_par *par)
 #endif /* ndef(__sparc__) */
 
 /* fill in known card constants if pll_block is not available */
-static void __init aty128_timings(struct aty128fb_par *par)
+static void __devinit aty128_timings(struct aty128fb_par *par)
 {
 #ifdef CONFIG_PPC_OF
        /* instead of a table lookup, assume OF has properly
@@ -1254,7 +1239,6 @@ static int aty128_crtc_to_var(const struct aty128_crtc *crtc,
        return 0;
 }
 
-#ifdef CONFIG_PMAC_PBOOK
 static void aty128_set_crt_enable(struct aty128fb_par *par, int on)
 {
        if (on) {
@@ -1267,19 +1251,21 @@ static void aty128_set_crt_enable(struct aty128fb_par *par, int on)
 static void aty128_set_lcd_enable(struct aty128fb_par *par, int on)
 {
        u32 reg;
+#ifdef CONFIG_FB_ATY128_BACKLIGHT
+       struct fb_info *info = pci_get_drvdata(par->pdev);
+#endif
 
        if (on) {
                reg = aty_ld_le32(LVDS_GEN_CNTL);
                reg |= LVDS_ON | LVDS_EN | LVDS_BLON | LVDS_DIGION;
                reg &= ~LVDS_DISPLAY_DIS;
                aty_st_le32(LVDS_GEN_CNTL, reg);
-#ifdef CONFIG_PMAC_BACKLIGHT
-               aty128_set_backlight_enable(get_backlight_enable(),
-                                           get_backlight_level(), par);
+#ifdef CONFIG_FB_ATY128_BACKLIGHT
+               aty128_bl_set_power(info, FB_BLANK_UNBLANK);
 #endif 
        } else {
-#ifdef CONFIG_PMAC_BACKLIGHT
-               aty128_set_backlight_enable(0, 0, par);
+#ifdef CONFIG_FB_ATY128_BACKLIGHT
+               aty128_bl_set_power(info, FB_BLANK_POWERDOWN);
 #endif 
                reg = aty_ld_le32(LVDS_GEN_CNTL);
                reg |= LVDS_DISPLAY_DIS;
@@ -1289,7 +1275,6 @@ static void aty128_set_lcd_enable(struct aty128fb_par *par, int on)
                aty_st_le32(LVDS_GEN_CNTL, reg);
        }
 }
-#endif /* CONFIG_PMAC_PBOOK */
 
 static void aty128_set_pll(struct aty128_pll *pll, const struct aty128fb_par *par)
 {
@@ -1337,7 +1322,7 @@ static int aty128_var_to_pll(u32 period_in_ps, struct aty128_pll *pll,
        unsigned char post_dividers[] = {1,2,4,8,3,6,12};
        u32 output_freq;
        u32 vclk;        /* in .01 MHz */
-       int i;
+       int i = 0;
        u32 n, d;
 
        vclk = 100000000 / period_in_ps;        /* convert units to 10 kHz */
@@ -1348,18 +1333,24 @@ static int aty128_var_to_pll(u32 period_in_ps, struct aty128_pll *pll,
        if (vclk * 12 < c.ppll_min)
                vclk = c.ppll_min/12;
 
+       pll->post_divider = -1;
+
        /* now, find an acceptable divider */
        for (i = 0; i < sizeof(post_dividers); i++) {
                output_freq = post_dividers[i] * vclk;
-               if (output_freq >= c.ppll_min && output_freq <= c.ppll_max)
+               if (output_freq >= c.ppll_min && output_freq <= c.ppll_max) {
+                       pll->post_divider = post_dividers[i];
                        break;
+               }
        }
 
+       if (pll->post_divider < 0)
+               return -EINVAL;
+
        /* calculate feedback divider */
        n = c.ref_divider * output_freq;
        d = c.ref_clk;
 
-       pll->post_divider = post_dividers[i];
        pll->feedback_divider = round_div(n, d);
        pll->vclk = vclk;
 
@@ -1496,12 +1487,10 @@ static int aty128fb_set_par(struct fb_info *info)
        info->fix.visual = par->crtc.bpp == 8 ? FB_VISUAL_PSEUDOCOLOR
                : FB_VISUAL_DIRECTCOLOR;
 
-#ifdef CONFIG_PMAC_PBOOK
        if (par->chip_gen == rage_M3) {
                aty128_set_crt_enable(par, par->crt_on);
                aty128_set_lcd_enable(par, par->lcd_on);
        }
-#endif
        if (par->accel_flags & FB_ACCELF_TEXT)
                aty128_init_engine(par);
 
@@ -1648,7 +1637,8 @@ static int aty128fb_sync(struct fb_info *info)
        return 0;
 }
 
-int __init aty128fb_setup(char *options)
+#ifndef MODULE
+static int __devinit aty128fb_setup(char *options)
 {
        char *this_opt;
 
@@ -1656,7 +1646,6 @@ int __init aty128fb_setup(char *options)
                return 0;
 
        while ((this_opt = strsep(&options, ",")) != NULL) {
-#ifdef CONFIG_PMAC_PBOOK
                if (!strncmp(this_opt, "lcd:", 4)) {
                        default_lcd_on = simple_strtoul(this_opt+4, NULL, 0);
                        continue;
@@ -1664,7 +1653,6 @@ int __init aty128fb_setup(char *options)
                        default_crt_on = simple_strtoul(this_opt+4, NULL, 0);
                        continue;
                }
-#endif
 #ifdef CONFIG_MTRR
                if(!strncmp(this_opt, "nomtrr", 6)) {
                        mtrr = 0;
@@ -1701,7 +1689,213 @@ int __init aty128fb_setup(char *options)
        }
        return 0;
 }
+#endif  /*  MODULE  */
+
+/* Backlight */
+#ifdef CONFIG_FB_ATY128_BACKLIGHT
+#define MAX_LEVEL 0xFF
+
+static struct backlight_properties aty128_bl_data;
+
+/* Call with fb_info->bl_mutex held */
+static int aty128_bl_get_level_brightness(struct aty128fb_par *par,
+               int level)
+{
+       struct fb_info *info = pci_get_drvdata(par->pdev);
+       int atylevel;
+
+       /* Get and convert the value */
+       atylevel = MAX_LEVEL -
+               (info->bl_curve[level] * FB_BACKLIGHT_MAX / MAX_LEVEL);
+
+       if (atylevel < 0)
+               atylevel = 0;
+       else if (atylevel > MAX_LEVEL)
+               atylevel = MAX_LEVEL;
+
+       return atylevel;
+}
+
+/* We turn off the LCD completely instead of just dimming the backlight.
+ * This provides greater power saving and the display is useless without
+ * backlight anyway
+ */
+#define BACKLIGHT_LVDS_OFF
+/* That one prevents proper CRT output with LCD off */
+#undef BACKLIGHT_DAC_OFF
+
+/* Call with fb_info->bl_mutex held */
+static int __aty128_bl_update_status(struct backlight_device *bd)
+{
+       struct aty128fb_par *par = class_get_devdata(&bd->class_dev);
+       unsigned int reg = aty_ld_le32(LVDS_GEN_CNTL);
+       int level;
+
+       if (bd->props->power != FB_BLANK_UNBLANK ||
+           bd->props->fb_blank != FB_BLANK_UNBLANK ||
+           !par->lcd_on)
+               level = 0;
+       else
+               level = bd->props->brightness;
+
+       reg |= LVDS_BL_MOD_EN | LVDS_BLON;
+       if (level > 0) {
+               reg |= LVDS_DIGION;
+               if (!(reg & LVDS_ON)) {
+                       reg &= ~LVDS_BLON;
+                       aty_st_le32(LVDS_GEN_CNTL, reg);
+                       aty_ld_le32(LVDS_GEN_CNTL);
+                       mdelay(10);
+                       reg |= LVDS_BLON;
+                       aty_st_le32(LVDS_GEN_CNTL, reg);
+               }
+               reg &= ~LVDS_BL_MOD_LEVEL_MASK;
+               reg |= (aty128_bl_get_level_brightness(par, level) << LVDS_BL_MOD_LEVEL_SHIFT);
+#ifdef BACKLIGHT_LVDS_OFF
+               reg |= LVDS_ON | LVDS_EN;
+               reg &= ~LVDS_DISPLAY_DIS;
+#endif
+               aty_st_le32(LVDS_GEN_CNTL, reg);
+#ifdef BACKLIGHT_DAC_OFF
+               aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) & (~DAC_PDWN));
+#endif
+       } else {
+               reg &= ~LVDS_BL_MOD_LEVEL_MASK;
+               reg |= (aty128_bl_get_level_brightness(par, 0) << LVDS_BL_MOD_LEVEL_SHIFT);
+#ifdef BACKLIGHT_LVDS_OFF
+               reg |= LVDS_DISPLAY_DIS;
+               aty_st_le32(LVDS_GEN_CNTL, reg);
+               aty_ld_le32(LVDS_GEN_CNTL);
+               udelay(10);
+               reg &= ~(LVDS_ON | LVDS_EN | LVDS_BLON | LVDS_DIGION);
+#endif
+               aty_st_le32(LVDS_GEN_CNTL, reg);
+#ifdef BACKLIGHT_DAC_OFF
+               aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) | DAC_PDWN);
+#endif
+       }
+
+       return 0;
+}
+
+static int aty128_bl_update_status(struct backlight_device *bd)
+{
+       struct aty128fb_par *par = class_get_devdata(&bd->class_dev);
+       struct fb_info *info = pci_get_drvdata(par->pdev);
+       int ret;
+
+       mutex_lock(&info->bl_mutex);
+       ret = __aty128_bl_update_status(bd);
+       mutex_unlock(&info->bl_mutex);
+
+       return ret;
+}
+
+static int aty128_bl_get_brightness(struct backlight_device *bd)
+{
+       return bd->props->brightness;
+}
+
+static struct backlight_properties aty128_bl_data = {
+       .owner          = THIS_MODULE,
+       .get_brightness = aty128_bl_get_brightness,
+       .update_status  = aty128_bl_update_status,
+       .max_brightness = (FB_BACKLIGHT_LEVELS - 1),
+};
+
+static void aty128_bl_set_power(struct fb_info *info, int power)
+{
+       mutex_lock(&info->bl_mutex);
+
+       if (info->bl_dev) {
+               down(&info->bl_dev->sem);
+               info->bl_dev->props->power = power;
+               __aty128_bl_update_status(info->bl_dev);
+               up(&info->bl_dev->sem);
+       }
+
+       mutex_unlock(&info->bl_mutex);
+}
+
+static void aty128_bl_init(struct aty128fb_par *par)
+{
+       struct fb_info *info = pci_get_drvdata(par->pdev);
+       struct backlight_device *bd;
+       char name[12];
+
+       /* Could be extended to Rage128Pro LVDS output too */
+       if (par->chip_gen != rage_M3)
+               return;
+
+#ifdef CONFIG_PMAC_BACKLIGHT
+       if (!pmac_has_backlight_type("ati"))
+               return;
+#endif
+
+       snprintf(name, sizeof(name), "aty128bl%d", info->node);
+
+       bd = backlight_device_register(name, info->dev, par, &aty128_bl_data);
+       if (IS_ERR(bd)) {
+               info->bl_dev = NULL;
+               printk(KERN_WARNING "aty128: Backlight registration failed\n");
+               goto error;
+       }
+
+       mutex_lock(&info->bl_mutex);
+       info->bl_dev = bd;
+       fb_bl_default_curve(info, 0,
+                63 * FB_BACKLIGHT_MAX / MAX_LEVEL,
+               219 * FB_BACKLIGHT_MAX / MAX_LEVEL);
+       mutex_unlock(&info->bl_mutex);
+
+       down(&bd->sem);
+       bd->props->brightness = aty128_bl_data.max_brightness;
+       bd->props->power = FB_BLANK_UNBLANK;
+       bd->props->update_status(bd);
+       up(&bd->sem);
+
+#ifdef CONFIG_PMAC_BACKLIGHT
+       mutex_lock(&pmac_backlight_mutex);
+       if (!pmac_backlight)
+               pmac_backlight = bd;
+       mutex_unlock(&pmac_backlight_mutex);
+#endif
+
+       printk("aty128: Backlight initialized (%s)\n", name);
+
+       return;
+
+error:
+       return;
+}
+
+static void aty128_bl_exit(struct aty128fb_par *par)
+{
+       struct fb_info *info = pci_get_drvdata(par->pdev);
+
+#ifdef CONFIG_PMAC_BACKLIGHT
+       mutex_lock(&pmac_backlight_mutex);
+#endif
+
+       mutex_lock(&info->bl_mutex);
+       if (info->bl_dev) {
+#ifdef CONFIG_PMAC_BACKLIGHT
+               if (pmac_backlight == info->bl_dev)
+                       pmac_backlight = NULL;
+#endif
 
+               backlight_device_unregister(info->bl_dev);
+               info->bl_dev = NULL;
+
+               printk("aty128: Backlight unloaded\n");
+       }
+       mutex_unlock(&info->bl_mutex);
+
+#ifdef CONFIG_PMAC_BACKLIGHT
+       mutex_unlock(&pmac_backlight_mutex);
+#endif
+}
+#endif /* CONFIG_FB_ATY128_BACKLIGHT */
 
 /*
  *  Initialisation
@@ -1719,7 +1913,7 @@ static void aty128_early_resume(void *data)
 }
 #endif /* CONFIG_PPC_PMAC */
 
-static int __init aty128_init(struct pci_dev *pdev, const struct pci_device_id *ent)
+static int __devinit aty128_init(struct pci_dev *pdev, const struct pci_device_id *ent)
 {
        struct fb_info *info = pci_get_drvdata(pdev);
        struct aty128fb_par *par = info->par;
@@ -1728,18 +1922,15 @@ static int __init aty128_init(struct pci_dev *pdev, const struct pci_device_id *
        u8 chip_rev;
        u32 dac;
 
-       if (!par->vram_size)    /* may have already been probed */
-               par->vram_size = aty_ld_le32(CONFIG_MEMSIZE) & 0x03FFFFFF;
-
        /* Get the chip revision */
        chip_rev = (aty_ld_le32(CONFIG_CNTL) >> 16) & 0x1F;
 
        strcpy(video_card, "Rage128 XX ");
        video_card[8] = ent->device >> 8;
        video_card[9] = ent->device & 0xFF;
-           
+
        /* range check to make sure */
-       if (ent->driver_data < (sizeof(r128_family)/sizeof(char *)))
+       if (ent->driver_data < ARRAY_SIZE(r128_family))
            strncat(video_card, r128_family[ent->driver_data], sizeof(video_card));
 
        printk(KERN_INFO "aty128fb: %s [chip rev 0x%x] ", video_card, chip_rev);
@@ -1755,14 +1946,12 @@ static int __init aty128_init(struct pci_dev *pdev, const struct pci_device_id *
        info->fbops = &aty128fb_ops;
        info->flags = FBINFO_FLAG_DEFAULT;
 
-#ifdef CONFIG_PMAC_PBOOK
        par->lcd_on = default_lcd_on;
        par->crt_on = default_crt_on;
-#endif
 
        var = default_var;
 #ifdef CONFIG_PPC_PMAC
-       if (_machine == _MACH_Pmac) {
+       if (machine_is(powermac)) {
                /* Indicate sleep capability */
                if (par->chip_gen == rage_M3) {
                        pmac_call_feature(PMAC_FTR_DEVICE_CAN_WAKE, NULL, 0, 1);
@@ -1845,20 +2034,18 @@ static int __init aty128_init(struct pci_dev *pdev, const struct pci_device_id *
 
        aty128_init_engine(par);
 
-       if (register_framebuffer(info) < 0)
-               return 0;
-
-#ifdef CONFIG_PMAC_BACKLIGHT
-       /* Could be extended to Rage128Pro LVDS output too */
-       if (par->chip_gen == rage_M3)
-               register_backlight_controller(&aty128_backlight_controller, par, "ati");
-#endif /* CONFIG_PMAC_BACKLIGHT */
-
        par->pm_reg = pci_find_capability(pdev, PCI_CAP_ID_PM);
        par->pdev = pdev;
        par->asleep = 0;
        par->lock_blank = 0;
-       
+
+#ifdef CONFIG_FB_ATY128_BACKLIGHT
+       aty128_bl_init(par);
+#endif
+
+       if (register_framebuffer(info) < 0)
+               return 0;
+
        printk(KERN_INFO "fb%d: %s frame buffer device on %s\n",
               info->node, info->fix.id, video_card);
 
@@ -1867,7 +2054,7 @@ static int __init aty128_init(struct pci_dev *pdev, const struct pci_device_id *
 
 #ifdef CONFIG_PCI
 /* register a card    ++ajoshi */
-static int __init aty128_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
+static int __devinit aty128_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 {
        unsigned long fb_addr, reg_addr;
        struct aty128fb_par *par;
@@ -1908,7 +2095,6 @@ static int __init aty128_probe(struct pci_dev *pdev, const struct pci_device_id
        par = info->par;
 
        info->pseudo_palette = par->pseudo_palette;
-       info->fix = aty128fb_fix;
 
        /* Virtualize mmio region */
        info->fix.mmio_start = reg_addr;
@@ -1994,6 +2180,10 @@ static void __devexit aty128_remove(struct pci_dev *pdev)
 
        par = info->par;
 
+#ifdef CONFIG_FB_ATY128_BACKLIGHT
+       aty128_bl_exit(par);
+#endif
+
        unregister_framebuffer(info);
 #ifdef CONFIG_MTRR
        if (par->mtrr.vram_valid)
@@ -2024,10 +2214,10 @@ static int aty128fb_blank(int blank, struct fb_info *fb)
        if (par->lock_blank || par->asleep)
                return 0;
 
-#ifdef CONFIG_PMAC_BACKLIGHT
-       if ((_machine == _MACH_Pmac) && blank)
-               set_backlight_enable(0);
-#endif /* CONFIG_PMAC_BACKLIGHT */
+#ifdef CONFIG_FB_ATY128_BACKLIGHT
+       if (machine_is(powermac) && blank)
+               aty128_bl_set_power(fb, FB_BLANK_POWERDOWN);
+#endif
 
        if (blank & FB_BLANK_VSYNC_SUSPEND)
                state |= 2;
@@ -2038,16 +2228,16 @@ static int aty128fb_blank(int blank, struct fb_info *fb)
 
        aty_st_8(CRTC_EXT_CNTL+1, state);
 
-#ifdef CONFIG_PMAC_PBOOK
        if (par->chip_gen == rage_M3) {
                aty128_set_crt_enable(par, par->crt_on && !blank);
                aty128_set_lcd_enable(par, par->lcd_on && !blank);
        }
-#endif 
-#ifdef CONFIG_PMAC_BACKLIGHT
-       if ((_machine == _MACH_Pmac) && !blank)
-               set_backlight_enable(1);
-#endif /* CONFIG_PMAC_BACKLIGHT */
+
+#ifdef CONFIG_FB_ATY128_BACKLIGHT
+       if (machine_is(powermac) && !blank)
+               aty128_bl_set_power(fb, FB_BLANK_UNBLANK);
+#endif
+
        return 0;
 }
 
@@ -2124,10 +2314,8 @@ static int aty128fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
 /* in param: u32*      backlight value: 0 to 15 */
 #define FBIO_ATY128_SET_MIRROR _IOW('@', 2, __u32)
 
-static int aty128fb_ioctl(struct inode *inode, struct file *file, u_int cmd,
-                         u_long arg, struct fb_info *info)
+static int aty128fb_ioctl(struct fb_info *info, u_int cmd, u_long arg)
 {
-#ifdef CONFIG_PMAC_PBOOK
        struct aty128fb_par *par = info->par;
        u32 value;
        int rc;
@@ -2152,77 +2340,9 @@ static int aty128fb_ioctl(struct inode *inode, struct file *file, u_int cmd,
                value = (par->crt_on << 1) | par->lcd_on;
                return put_user(value, (__u32 __user *)arg);
        }
-#endif
        return -EINVAL;
 }
 
-#ifdef CONFIG_PMAC_BACKLIGHT
-static int backlight_conv[] = {
-       0xff, 0xc0, 0xb5, 0xaa, 0x9f, 0x94, 0x89, 0x7e,
-       0x73, 0x68, 0x5d, 0x52, 0x47, 0x3c, 0x31, 0x24
-};
-
-/* We turn off the LCD completely instead of just dimming the backlight.
- * This provides greater power saving and the display is useless without
- * backlight anyway
- */
-#define BACKLIGHT_LVDS_OFF
-/* That one prevents proper CRT output with LCD off */
-#undef BACKLIGHT_DAC_OFF
-
-static int aty128_set_backlight_enable(int on, int level, void *data)
-{
-       struct aty128fb_par *par = data;
-       unsigned int reg = aty_ld_le32(LVDS_GEN_CNTL);
-
-       if (!par->lcd_on)
-               on = 0;
-       reg |= LVDS_BL_MOD_EN | LVDS_BLON;
-       if (on && level > BACKLIGHT_OFF) {
-               reg |= LVDS_DIGION;
-               if (!(reg & LVDS_ON)) {
-                       reg &= ~LVDS_BLON;
-                       aty_st_le32(LVDS_GEN_CNTL, reg);
-                       (void)aty_ld_le32(LVDS_GEN_CNTL);
-                       mdelay(10);
-                       reg |= LVDS_BLON;
-                       aty_st_le32(LVDS_GEN_CNTL, reg);
-               }
-               reg &= ~LVDS_BL_MOD_LEVEL_MASK;
-               reg |= (backlight_conv[level] << LVDS_BL_MOD_LEVEL_SHIFT);
-#ifdef BACKLIGHT_LVDS_OFF
-               reg |= LVDS_ON | LVDS_EN;
-               reg &= ~LVDS_DISPLAY_DIS;
-#endif
-               aty_st_le32(LVDS_GEN_CNTL, reg);
-#ifdef BACKLIGHT_DAC_OFF
-               aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) & (~DAC_PDWN));
-#endif         
-       } else {
-               reg &= ~LVDS_BL_MOD_LEVEL_MASK;
-               reg |= (backlight_conv[0] << LVDS_BL_MOD_LEVEL_SHIFT);
-#ifdef BACKLIGHT_LVDS_OFF
-               reg |= LVDS_DISPLAY_DIS;
-               aty_st_le32(LVDS_GEN_CNTL, reg);
-               (void)aty_ld_le32(LVDS_GEN_CNTL);
-               udelay(10);
-               reg &= ~(LVDS_ON | LVDS_EN | LVDS_BLON | LVDS_DIGION);
-#endif         
-               aty_st_le32(LVDS_GEN_CNTL, reg);
-#ifdef BACKLIGHT_DAC_OFF
-               aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) | DAC_PDWN);
-#endif         
-       }
-
-       return 0;
-}
-
-static int aty128_set_backlight_level(int level, void* data)
-{
-       return aty128_set_backlight_enable(1, level, data);
-}
-#endif /* CONFIG_PMAC_BACKLIGHT */
-
 #if 0
     /*
      *  Accelerated functions
@@ -2330,7 +2450,7 @@ static void aty128_set_suspend(struct aty128fb_par *par, int suspend)
        }
 }
 
-static int aty128_pci_suspend(struct pci_dev *pdev, u32 state)
+static int aty128_pci_suspend(struct pci_dev *pdev, pm_message_t state)
 {
        struct fb_info *info = pci_get_drvdata(pdev);
        struct aty128fb_par *par = info->par;
@@ -2340,17 +2460,16 @@ static int aty128_pci_suspend(struct pci_dev *pdev, u32 state)
         * can properly take care of D3 ? Also, with swsusp, we
         * know we'll be rebooted, ...
         */
-#ifdef CONFIG_PPC_PMAC
+#ifndef CONFIG_PPC_PMAC
        /* HACK ALERT ! Once I find a proper way to say to each driver
         * individually what will happen with it's PCI slot, I'll change
         * that. On laptops, the AGP slot is just unclocked, so D2 is
         * expected, while on desktops, the card is powered off
         */
-       if (state >= 3)
-               state = 2;
+       return 0;
 #endif /* CONFIG_PPC_PMAC */
         
-       if (state != 2 || state == pdev->dev.power.power_state)
+       if (state.event == pdev->dev.power.power_state.event)
                return 0;
 
        printk(KERN_DEBUG "aty128fb: suspending...\n");
@@ -2371,12 +2490,20 @@ static int aty128_pci_suspend(struct pci_dev *pdev, u32 state)
        par->asleep = 1;
        par->lock_blank = 1;
 
+#ifdef CONFIG_PPC_PMAC
+       /* On powermac, we have hooks to properly suspend/resume AGP now,
+        * use them here. We'll ultimately need some generic support here,
+        * but the generic code isn't quite ready for that yet
+        */
+       pmac_suspend_agp_for_card(pdev);
+#endif /* CONFIG_PPC_PMAC */
+
        /* We need a way to make sure the fbdev layer will _not_ touch the
         * framebuffer before we put the chip to suspend state. On 2.4, I
         * used dummy fb ops, 2.5 need proper support for this at the
         * fbdev level
         */
-       if (state == 2)
+       if (state.event != PM_EVENT_ON)
                aty128_set_suspend(par, 1);
 
        release_console_sem();
@@ -2391,12 +2518,11 @@ static int aty128_do_resume(struct pci_dev *pdev)
        struct fb_info *info = pci_get_drvdata(pdev);
        struct aty128fb_par *par = info->par;
 
-       if (pdev->dev.power.power_state == 0)
+       if (pdev->dev.power.power_state.event == PM_EVENT_ON)
                return 0;
 
        /* Wakeup chip */
-       if (pdev->dev.power.power_state == 2)
-               aty128_set_suspend(par, 0);
+       aty128_set_suspend(par, 0);
        par->asleep = 0;
 
        /* Restore display & engine */
@@ -2413,7 +2539,15 @@ static int aty128_do_resume(struct pci_dev *pdev)
        par->lock_blank = 0;
        aty128fb_blank(0, info);
 
-       pdev->dev.power.power_state = 0;
+#ifdef CONFIG_PPC_PMAC
+       /* On powermac, we have hooks to properly suspend/resume AGP now,
+        * use them here. We'll ultimately need some generic support here,
+        * but the generic code isn't quite ready for that yet
+        */
+       pmac_resume_agp_for_card(pdev);
+#endif /* CONFIG_PPC_PMAC */
+
+       pdev->dev.power.power_state = PMSG_ON;
 
        printk(KERN_DEBUG "aty128fb: resumed !\n");
 
@@ -2432,7 +2566,7 @@ static int aty128_pci_resume(struct pci_dev *pdev)
 }
 
 
-int __init aty128fb_init(void)
+static int __devinit aty128fb_init(void)
 {
 #ifndef MODULE
        char *option = NULL;
@@ -2442,7 +2576,7 @@ int __init aty128fb_init(void)
        aty128fb_setup(option);
 #endif
 
-       return pci_module_init(&aty128fb_driver);
+       return pci_register_driver(&aty128fb_driver);
 }
 
 static void __exit aty128fb_exit(void)
@@ -2452,7 +2586,6 @@ static void __exit aty128fb_exit(void)
 
 module_init(aty128fb_init);
 
-#ifdef MODULE
 module_exit(aty128fb_exit);
 
 MODULE_AUTHOR("(c)1999-2003 Brad Douglas <brad@neruo.com>");
@@ -2464,5 +2597,4 @@ MODULE_PARM_DESC(mode_option, "Specify resolution as \"<xres>x<yres>[-<bpp>][@<r
 module_param_named(nomtrr, mtrr, invbool, 0);
 MODULE_PARM_DESC(nomtrr, "bool: Disable MTRR support (0 or 1=disabled) (default=0)");
 #endif
-#endif