Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Backlight code for nVidia based graphic cards |
| 3 | * |
| 4 | * Copyright 2004 Antonino Daplas <adaplas@pol.net> |
| 5 | * Copyright (c) 2006 Michael Hanselmann <linux-kernel@hansmi.ch> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/backlight.h> |
| 13 | #include <linux/fb.h> |
| 14 | #include <linux/pci.h> |
| 15 | #include "nv_local.h" |
| 16 | #include "nv_type.h" |
| 17 | #include "nv_proto.h" |
| 18 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 19 | /* We do not have any information about which values are allowed, thus |
| 20 | * we used safe values. |
| 21 | */ |
| 22 | #define MIN_LEVEL 0x158 |
| 23 | #define MAX_LEVEL 0x534 |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 24 | #define LEVEL_STEP ((MAX_LEVEL - MIN_LEVEL) / FB_BACKLIGHT_MAX) |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 25 | |
| 26 | static struct backlight_properties nvidia_bl_data; |
| 27 | |
| 28 | static int nvidia_bl_get_level_brightness(struct nvidia_par *par, |
| 29 | int level) |
| 30 | { |
| 31 | struct fb_info *info = pci_get_drvdata(par->pci_dev); |
| 32 | int nlevel; |
| 33 | |
| 34 | /* Get and convert the value */ |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 35 | /* No locking of bl_curve since we read a single value */ |
Michael Hanselmann | e01af03 | 2006-07-10 04:44:45 -0700 | [diff] [blame] | 36 | nlevel = MIN_LEVEL + info->bl_curve[level] * LEVEL_STEP; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 37 | |
| 38 | if (nlevel < 0) |
| 39 | nlevel = 0; |
| 40 | else if (nlevel < MIN_LEVEL) |
| 41 | nlevel = MIN_LEVEL; |
| 42 | else if (nlevel > MAX_LEVEL) |
| 43 | nlevel = MAX_LEVEL; |
| 44 | |
| 45 | return nlevel; |
| 46 | } |
| 47 | |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 48 | static int nvidia_bl_update_status(struct backlight_device *bd) |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 49 | { |
| 50 | struct nvidia_par *par = class_get_devdata(&bd->class_dev); |
| 51 | u32 tmp_pcrt, tmp_pmc, fpcontrol; |
| 52 | int level; |
| 53 | |
| 54 | if (!par->FlatPanel) |
| 55 | return 0; |
| 56 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame^] | 57 | if (bd->props.power != FB_BLANK_UNBLANK || |
| 58 | bd->props.fb_blank != FB_BLANK_UNBLANK) |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 59 | level = 0; |
| 60 | else |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame^] | 61 | level = bd->props.brightness; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 62 | |
| 63 | tmp_pmc = NV_RD32(par->PMC, 0x10F0) & 0x0000FFFF; |
| 64 | tmp_pcrt = NV_RD32(par->PCRTC0, 0x081C) & 0xFFFFFFFC; |
| 65 | fpcontrol = NV_RD32(par->PRAMDAC, 0x0848) & 0xCFFFFFCC; |
| 66 | |
| 67 | if (level > 0) { |
| 68 | tmp_pcrt |= 0x1; |
| 69 | tmp_pmc |= (1 << 31); /* backlight bit */ |
| 70 | tmp_pmc |= nvidia_bl_get_level_brightness(par, level) << 16; |
| 71 | fpcontrol |= par->fpSyncs; |
| 72 | } else |
| 73 | fpcontrol |= 0x20000022; |
| 74 | |
| 75 | NV_WR32(par->PCRTC0, 0x081C, tmp_pcrt); |
| 76 | NV_WR32(par->PMC, 0x10F0, tmp_pmc); |
| 77 | NV_WR32(par->PRAMDAC, 0x848, fpcontrol); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int nvidia_bl_get_brightness(struct backlight_device *bd) |
| 83 | { |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame^] | 84 | return bd->props.brightness; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame^] | 87 | static struct backlight_ops nvidia_bl_ops = { |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 88 | .get_brightness = nvidia_bl_get_brightness, |
| 89 | .update_status = nvidia_bl_update_status, |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 92 | void nvidia_bl_init(struct nvidia_par *par) |
| 93 | { |
| 94 | struct fb_info *info = pci_get_drvdata(par->pci_dev); |
| 95 | struct backlight_device *bd; |
| 96 | char name[12]; |
| 97 | |
| 98 | if (!par->FlatPanel) |
| 99 | return; |
| 100 | |
| 101 | #ifdef CONFIG_PMAC_BACKLIGHT |
| 102 | if (!machine_is(powermac) || |
| 103 | !pmac_has_backlight_type("mnca")) |
| 104 | return; |
| 105 | #endif |
| 106 | |
| 107 | snprintf(name, sizeof(name), "nvidiabl%d", info->node); |
| 108 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame^] | 109 | bd = backlight_device_register(name, info->dev, par, &nvidia_bl_ops); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 110 | if (IS_ERR(bd)) { |
| 111 | info->bl_dev = NULL; |
Benjamin Herrenschmidt | 98a3c78 | 2006-08-31 14:04:34 +1000 | [diff] [blame] | 112 | printk(KERN_WARNING "nvidia: Backlight registration failed\n"); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 113 | goto error; |
| 114 | } |
| 115 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 116 | info->bl_dev = bd; |
| 117 | fb_bl_default_curve(info, 0, |
| 118 | 0x158 * FB_BACKLIGHT_MAX / MAX_LEVEL, |
| 119 | 0x534 * FB_BACKLIGHT_MAX / MAX_LEVEL); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 120 | |
Richard Purdie | 599a52d | 2007-02-10 23:07:48 +0000 | [diff] [blame^] | 121 | bd->props.max_brightness = FB_BACKLIGHT_LEVELS - 1; |
| 122 | bd->props.brightness = nvidia_bl_data.max_brightness; |
| 123 | bd->props.power = FB_BLANK_UNBLANK; |
Richard Purdie | 28ee086 | 2007-02-08 22:25:09 +0000 | [diff] [blame] | 124 | backlight_update_status(bd); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 125 | |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 126 | printk("nvidia: Backlight initialized (%s)\n", name); |
| 127 | |
| 128 | return; |
| 129 | |
| 130 | error: |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | void nvidia_bl_exit(struct nvidia_par *par) |
| 135 | { |
| 136 | struct fb_info *info = pci_get_drvdata(par->pci_dev); |
Richard Purdie | 37ce69a | 2007-02-10 14:10:33 +0000 | [diff] [blame] | 137 | struct backlight_device *bd = info->bl_dev; |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 138 | |
Richard Purdie | 321709c | 2007-02-10 15:04:08 +0000 | [diff] [blame] | 139 | backlight_device_unregister(bd); |
| 140 | printk("nvidia: Backlight unloaded\n"); |
Michael Hanselmann | 5474c12 | 2006-06-25 05:47:08 -0700 | [diff] [blame] | 141 | } |