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