Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 10 Jul 2012 18:13:22 -0400
From:      Justin Hibbits <chmeeedalf@gmail.com>
To:        FreeBSD PowerPC ML <freebsd-ppc@freebsd.org>
Subject:   CFT: nVidia backlight driver
Message-ID:  <F63802D4-202C-4A2A-9AA7-7A1CCAF74780@gmail.com>

next in thread | raw e-mail | index | archive | help

--Apple-Mail-2-726466157
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed;
	delsp=yes
Content-Transfer-Encoding: 7bit

Here's a backlight driver for those pbook/ibook/imac users with nVidia  
chips.  I've only tested it on Peter Grehan's PowerBook 12" (GeForce  
FX Go5200), so it's not tested on anything else.  It _shouldn't_ harm  
the card, but don't blame me if it does :) I'd like to make sure it  
does work on more than this machine before I commit it.

- Justin

--Apple-Mail-2-726466157
Content-Disposition: attachment;
	filename=nvbl.diff
Content-Type: application/octet-stream;
	x-unix-mode=0644;
	name="nvbl.diff"
Content-Transfer-Encoding: 7bit

Index: sys/powerpc/conf/GENERIC64
===================================================================
--- sys/powerpc/conf/GENERIC64	(revision 238269)
+++ sys/powerpc/conf/GENERIC64	(working copy)
@@ -194,6 +194,7 @@
 device		powermac_nvram	# Open Firmware configuration NVRAM
 device		smu		# Apple System Management Unit
 device		atibl		# ATI-based backlight driver for PowerBooks/iBooks
+device		nvbl		# nVidia-based backlight driver for PowerBooks/iBooks
 
 # ADB support
 device		adb
Index: sys/powerpc/conf/GENERIC
===================================================================
--- sys/powerpc/conf/GENERIC	(revision 238269)
+++ sys/powerpc/conf/GENERIC	(working copy)
@@ -197,6 +197,7 @@
 device		smu		# Apple System Management Unit
 device		windtunnel	# Apple G4 MDD fan controller
 device		atibl		# ATI-based backlight driver for PowerBooks/iBooks
+device		nvbl		# nVidia-based backlight driver for PowerBooks/iBooks
 
 # ADB support
 device		adb
Index: sys/powerpc/powermac/nvbl.c
===================================================================
--- sys/powerpc/powermac/nvbl.c	(revision 0)
+++ sys/powerpc/powermac/nvbl.c	(working copy)
@@ -0,0 +1,209 @@
+/*-
+ * Copyright (c) 2012 Justin Hibbits
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD: head/sys/powerpc/powermac/nvbl.c 232177 2012-02-26 13:45:25Z jhibbits $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/systm.h>
+#include <sys/module.h>
+#include <sys/kernel.h>
+#include <sys/rman.h>
+#include <sys/sysctl.h>
+
+#include <machine/bus.h>
+
+#include <dev/ofw/openfirm.h>
+
+#define NVIDIA_BRIGHT_MIN     (0x0ec)
+#define NVIDIA_BRIGHT_MAX     (0x538)
+#define NVIDIA_BRIGHT_SCALE   ((NVIDIA_BRIGHT_MAX - NVIDIA_BRIGHT_MIN)/100)
+/* nVidia's MMIO registers are at PCI BAR[0] */
+#define NVIDIA_MMIO_REG       (0x0)
+#define  NVIDIA_MMIO_PMC       (0x0)
+#define   NVIDIA_PMC_OFF         (NVIDIA_MMIO_PMC + 0x10f0)
+#define    NVIDIA_PMC_BL_SHIFT    (16)
+#define    NVIDIA_PMC_BL_EN       (1 << 31)
+#define  NVIDIA_MMIO_PCRTC0    (0x00600000)
+#define   NVIDIA_PCRTC0_BL       (NVIDIA_MMIO_PCRTC0 + 0x081C)
+#define   NVIDIA_PCRTC0_MASK     (0xFFFFFFFC)
+#define  NVIDIA_MMIO_PRAMDAC   (0x00680000)
+#define   NVIDIA_PRAMDAC_FPCTL  (NVIDIA_MMIO_PRAMDAC + 0x0848)
+#define    NVIDIA_FPCTL_MASK    (0xCFFFFFCC)
+#define    NVIDIA_PRAMDAC_NO_LEVEL (0x20000022)
+
+
+struct nvbl_softc {
+	device_t	 dev;
+	struct resource *sc_memr;
+	uint32_t     sc_saved_ramdac;
+};
+
+static void nvbl_identify(driver_t *driver, device_t parent);
+static int nvbl_probe(device_t dev);
+static int nvbl_attach(device_t dev);
+static int nvbl_setlevel(struct nvbl_softc *sc, int newlevel);
+static int nvbl_getlevel(struct nvbl_softc *sc);
+static int nvbl_sysctl(SYSCTL_HANDLER_ARGS);
+
+static device_method_t nvbl_methods[] = {
+	/* Device interface */
+	DEVMETHOD(device_identify, nvbl_identify),
+	DEVMETHOD(device_probe, nvbl_probe),
+	DEVMETHOD(device_attach, nvbl_attach),
+	{0, 0},
+};
+
+static driver_t	nvbl_driver = {
+	"backlight",
+	nvbl_methods,
+	sizeof(struct nvbl_softc)
+};
+
+static devclass_t nvbl_devclass;
+
+DRIVER_MODULE(nvbl, vgapci, nvbl_driver, nvbl_devclass, 0, 0);
+
+static void
+nvbl_identify(driver_t *driver, device_t parent)
+{
+	if (device_find_child(parent, "backlight", -1) == NULL)
+		device_add_child(parent, "backlight", -1);
+}
+
+static int
+nvbl_probe(device_t dev)
+{
+	char		control[8];
+	phandle_t	handle;
+
+	handle = OF_finddevice("mac-io/backlight");
+
+	if (handle <= 0)
+		return (ENXIO);
+
+	if (OF_getprop(handle, "backlight-control", &control, sizeof(control)) < 0)
+		return (ENXIO);
+
+	if (strcmp(control, "mnca") != 0)
+		return (ENXIO);
+
+	device_set_desc(dev, "PowerBook backlight for nVidia graphics");
+
+	return (0);
+}
+
+static int
+nvbl_attach(device_t dev)
+{
+	struct nvbl_softc	*sc;
+	struct sysctl_ctx_list *ctx;
+	struct sysctl_oid *tree;
+	int			 rid;
+
+	sc = device_get_softc(dev);
+
+	rid = 0x10;	/* BAR[0], for the MMIO register */
+	sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
+			RF_ACTIVE | RF_SHAREABLE);
+	if (sc->sc_memr == NULL) {
+		device_printf(dev, "Could not alloc mem resource!\n");
+		return (ENXIO);
+	}
+
+	sc->sc_saved_ramdac = bus_read_4(sc->sc_memr, NVIDIA_PRAMDAC_FPCTL) &
+		(~NVIDIA_FPCTL_MASK);
+
+	/* Turn on big-endian mode */
+	if (!(bus_read_stream_4(sc->sc_memr, NVIDIA_MMIO_PMC + 4) & 0x01000001)) {
+		bus_write_stream_4(sc->sc_memr, NVIDIA_MMIO_PMC + 4, 0x01000001);
+		mb();
+	}
+
+	ctx = device_get_sysctl_ctx(dev);
+	tree = device_get_sysctl_tree(dev);
+
+	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
+			"level", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
+			nvbl_sysctl, "I", "Backlight level (0-100)");
+
+	return (0);
+}
+
+static int
+nvbl_setlevel(struct nvbl_softc *sc, int newlevel)
+{
+	uint32_t pmc_reg;
+
+	if (newlevel > 100)
+		newlevel = 100;
+
+	if (newlevel < 0)
+		newlevel = 0;
+
+	if (newlevel > 0)
+		newlevel = (newlevel * NVIDIA_BRIGHT_SCALE) + NVIDIA_BRIGHT_MIN;
+
+	pmc_reg = bus_read_stream_4(sc->sc_memr, NVIDIA_PMC_OFF) & 0xffff;
+	pmc_reg |= NVIDIA_PMC_BL_EN | (newlevel << NVIDIA_PMC_BL_SHIFT);
+	bus_write_stream_4(sc->sc_memr, NVIDIA_PMC_OFF, pmc_reg);
+
+	return (0);
+}
+
+static int
+nvbl_getlevel(struct nvbl_softc *sc)
+{
+	uint16_t level;
+
+	level = bus_read_stream_2(sc->sc_memr, NVIDIA_PMC_OFF) & 0x7fff;
+
+	if (level  < NVIDIA_BRIGHT_MIN)
+		return 0;
+
+	level = (level - NVIDIA_BRIGHT_MIN) / NVIDIA_BRIGHT_SCALE;
+
+	return (level);
+}
+
+static int
+nvbl_sysctl(SYSCTL_HANDLER_ARGS)
+{
+	struct nvbl_softc *sc;
+	int newlevel, error;
+
+	sc = arg1;
+
+	newlevel = nvbl_getlevel(sc);
+
+	error = sysctl_handle_int(oidp, &newlevel, 0, req);
+
+	if (error || !req->newptr)
+		return (error);
+
+	return (nvbl_setlevel(sc, newlevel));
+}
Index: sys/conf/files.powerpc
===================================================================
--- sys/conf/files.powerpc	(revision 238269)
+++ sys/conf/files.powerpc	(working copy)
@@ -160,6 +160,7 @@
 powerpc/powermac/kiic.c		optional	powermac kiic
 powerpc/powermac/macgpio.c	optional	powermac pci 
 powerpc/powermac/macio.c	optional	powermac pci
+powerpc/powermac/nvbl.c		optional	powermac nvbl
 powerpc/powermac/openpic_macio.c optional	powermac pci
 powerpc/powermac/platform_powermac.c optional	powermac
 powerpc/powermac/powermac_thermal.c optional	powermac

--Apple-Mail-2-726466157
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed
Content-Transfer-Encoding: 7bit



--Apple-Mail-2-726466157--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?F63802D4-202C-4A2A-9AA7-7A1CCAF74780>