From 69cea1f60bd1b22f382093feabf7fdec69f10f70 Mon Sep 17 00:00:00 2001
From: Koen Kooi <koen@dominion.thruhere.net>
Date: Tue, 31 Jan 2012 17:02:10 +0100
Subject: [PATCH 16/21] beaglebone: enable PWM for lcd backlight <- backlight
 is inverted

Signed-off-by: Koen Kooi <koen@dominion.thruhere.net>
---
 arch/arm/mach-omap2/board-am335xevm.c |   54 ++++++++++++++++++++++++++------
 arch/arm/mach-omap2/devices.c         |   13 ++++++++
 arch/arm/mach-omap2/devices.h         |    1 +
 3 files changed, 58 insertions(+), 10 deletions(-)

diff --git a/arch/arm/mach-omap2/board-am335xevm.c b/arch/arm/mach-omap2/board-am335xevm.c
index 177ab9e..6a77194 100644
--- a/arch/arm/mach-omap2/board-am335xevm.c
+++ b/arch/arm/mach-omap2/board-am335xevm.c
@@ -36,6 +36,7 @@
 #include <linux/mfd/tps65910.h>
 #include <linux/mfd/tps65217.h>
 #include <linux/pwm_backlight.h>
+#include <linux/pwm/pwm.h>
 
 /* LCD controller is similar to DA850 */
 #include <video/da8xx-fb.h>
@@ -131,7 +132,7 @@ static const struct display_panel disp_panel = {
 /* LCD backlight platform Data */
 #define AM335X_BACKLIGHT_MAX_BRIGHTNESS        100
 #define AM335X_BACKLIGHT_DEFAULT_BRIGHTNESS    100
-#define AM335X_PWM_PERIOD_NANO_SECONDS        (1000000 * 10)
+#define AM335X_PWM_PERIOD_NANO_SECONDS        (1000000 * 5)
 
 #define PWM_DEVICE_ID   "ecap.0"
 
@@ -175,6 +176,16 @@ static const struct display_panel bbtoys7_panel = {
 	COLOR_ACTIVE,
 };
 
+#define BBTOYS7LCD_PWM_DEVICE_ID   "ehrpwm.1:0"
+
+static struct platform_pwm_backlight_data bbtoys7lcd_backlight_data = {
+	.pwm_id         = BBTOYS7LCD_PWM_DEVICE_ID,
+	.ch             = -1,
+	.max_brightness = AM335X_BACKLIGHT_MAX_BRIGHTNESS,
+	.dft_brightness = AM335X_BACKLIGHT_DEFAULT_BRIGHTNESS,
+	.pwm_period_ns  = AM335X_PWM_PERIOD_NANO_SECONDS,
+};
+
 static struct lcd_ctrl_config bbtoys7_cfg = {
 	&bbtoys7_panel,
 	.ac_bias		= 255,
@@ -586,9 +597,9 @@ static struct pinmux_config bbtoys7_pin_mux[] = {
 	{"lcd_vsync.lcd_vsync",		OMAP_MUX_MODE0 | AM33XX_PIN_OUTPUT},
 	{"lcd_hsync.lcd_hsync",		OMAP_MUX_MODE0 | AM33XX_PIN_OUTPUT},
 	{"lcd_pclk.lcd_pclk",		OMAP_MUX_MODE0 | AM33XX_PIN_OUTPUT},
-	{"lcd_ac_bias_en.lcd_ac_bias_en", OMAP_MUX_MODE0 | AM33XX_PIN_OUTPUT}, 
-	{"gpmc_a2.gpio1_18", OMAP_MUX_MODE7 | AM33XX_PIN_OUTPUT}, // Backlight
+	{"lcd_ac_bias_en.lcd_ac_bias_en", OMAP_MUX_MODE0 | AM33XX_PIN_OUTPUT},
 	{"ecap0_in_pwm0_out.gpio0_7", OMAP_MUX_MODE7 | AM33XX_PIN_OUTPUT}, // AVDD_EN
+	{"gpmc_a2.ehrpwm1A", OMAP_MUX_MODE6 | AM33XX_PIN_OUTPUT}, // Backlight
 	{NULL, 0},
 };
 
@@ -1121,6 +1132,7 @@ static struct pinmux_config ecap0_pin_mux[] = {
 	{NULL, 0},
 };
 
+static int ehrpwm_backlight_enable;
 static int backlight_enable;
 
 #define AM335XEVM_WLAN_PMENA_GPIO	GPIO_TO_PIN(1, 30)
@@ -1190,6 +1202,30 @@ static int __init ecap0_init(void)
 }
 late_initcall(ecap0_init);
 
+static void enable_ehrpwm1(int evm_id, int profile)
+{
+	ehrpwm_backlight_enable = true;
+}
+
+/* Setup pwm-backlight for bbtoys7lcd */
+static struct platform_device bbtoys7lcd_backlight = {
+	.name           = "pwm-backlight",
+	.id             = -1,
+	.dev            = {
+		.platform_data  = &bbtoys7lcd_backlight_data,
+	}
+};
+
+static int __init ehrpwm1_init(void)
+{
+	int status = 0;
+	if (ehrpwm_backlight_enable) {
+		platform_device_register(&bbtoys7lcd_backlight);
+	}
+	return status;
+}
+late_initcall(ehrpwm1_init);
+
 static int __init conf_disp_pll(int rate)
 {
 	struct clk *disp_pll;
@@ -1224,11 +1260,12 @@ static void lcdc_init(int evm_id, int profile)
 }
 
 #define BEAGLEBONE_LCD_AVDD_EN GPIO_TO_PIN(0, 7)
-#define BEAGLEBONE_LCD_BL GPIO_TO_PIN(1, 18)
 
 static void bbtoys7lcd_init(int evm_id, int profile)
 {
 	setup_pin_mux(bbtoys7_pin_mux);
+	gpio_request(BEAGLEBONE_LCD_AVDD_EN, "BONE_LCD_AVDD_EN");
+	gpio_direction_output(BEAGLEBONE_LCD_AVDD_EN, 1);
 
 	// we are being stupid and setting pixclock from here instead of da8xx-fb.c
 	if (conf_disp_pll(300000000)) {
@@ -1239,12 +1276,7 @@ static void bbtoys7lcd_init(int evm_id, int profile)
 	
 	if (am33xx_register_lcdc(&bbtoys7_pdata))
 		pr_info("Failed to register Beagleboardtoys 7\" LCD cape device\n");
-	
-	gpio_request(BEAGLEBONE_LCD_BL, "BONE_LCD_BL");
-	gpio_direction_output(BEAGLEBONE_LCD_BL, 1);
-	gpio_request(BEAGLEBONE_LCD_AVDD_EN, "BONE_LCD_AVDD_EN");
-	gpio_direction_output(BEAGLEBONE_LCD_AVDD_EN, 1);
-
+		
 	return;
 }
 
@@ -1768,6 +1800,8 @@ static void beaglebone_cape_setup(struct memory_accessor *mem_acc, void *context
 		bbtoys7lcd_init(0,0);
 		pr_info("BeagleBone cape: initializing LCD cape touchscreen\n");
 		tsc_init(0,0);
+		pr_info("BeagleBone cape: Registering PWM backlight for LCD cape\n");
+		enable_ehrpwm1(0,0);
 		beaglebone_tsadcpins_free = 0;
 	}
 	
diff --git a/arch/arm/mach-omap2/devices.c b/arch/arm/mach-omap2/devices.c
index 198df7b..8cd2523 100644
--- a/arch/arm/mach-omap2/devices.c
+++ b/arch/arm/mach-omap2/devices.c
@@ -1278,6 +1278,19 @@ void register_ehrpwm(int max_freq)
 	platform_device_register(&am335x_epwm2_device);
 }
 
+void register_ehrpwm1(int max_freq)
+{
+	int val;
+
+	val = __raw_readw(AM33XX_CTRL_REGADDR(AM33XX_PWMSS_CTRL));
+	val |= PWMSS1_TBCLKEN;
+	__raw_writew(val, AM33XX_CTRL_REGADDR(AM33XX_PWMSS_CTRL));
+	am335x_pwmss_config1.chan_attrib[1].max_freq = max_freq;
+	sema_init(&am335x_pwmss_config1.config_semaphore, 1);
+	am335x_pwmss_config1.version = PWM_VERSION_1;
+	platform_device_register(&am335x_epwm1_device);
+}
+
 static struct resource am335x_ecap0_resurce[] = {
 	{
 		.start = AM33XX_EPWMSS0_BASE ,
diff --git a/arch/arm/mach-omap2/devices.h b/arch/arm/mach-omap2/devices.h
index e086839..5161eb4 100644
--- a/arch/arm/mach-omap2/devices.h
+++ b/arch/arm/mach-omap2/devices.h
@@ -18,5 +18,6 @@ int omap3_init_camera(struct isp_platform_data *pdata);
 
 void __init am335x_register_mcasp1(struct snd_platform_data *pdata);
 extern void register_ehrpwm(int max_freq);
+extern void register_ehrpwm1(int max_freq);
 
 #endif
-- 
1.7.7.4

