Clean up Voltage Regulator And CLK hacks and utilize clock framework changes

Change-Id: I4e1a2a075601bcc3bc502905f4008d0f260e6346
Signed-off-by: wthomas <wthomas@codeaurora.org>
diff --git a/nss_core.h b/nss_core.h
index db484f5..e1d935e 100755
--- a/nss_core.h
+++ b/nss_core.h
@@ -139,26 +139,23 @@
 
 /*
  * NSS Frequency Defines and Values
+ *
+ * INFO: The LOW and MAX value together describe the "performance" band that we should operate the frequency at.
+ *
  */
-#define NSS_FREQ_110		110000000
-#define NSS_FREQ_110_DVDR	5
-#define NSS_FREQ_110_MIN	0x03000
-#define NSS_FREQ_110_MAX	0x14000
-#define NSS_FREQ_110_TURBO	0
+#define NSS_FREQ_110		110000000	/* Frequency in hz */
+#define NSS_FREQ_110_MIN	0x03000		/* Instructions Per ms Min */
+#define NSS_FREQ_110_MAX	0x14000		/* Instructions Per ms Max */
 
-#define NSS_FREQ_275		275000000
+#define NSS_FREQ_275		275000000	/* Frequency in hz */
 
-#define NSS_FREQ_550		550000000
-#define NSS_FREQ_550_DVDR	1
-#define NSS_FREQ_550_MIN	0x14000
-#define NSS_FREQ_550_MAX	0x40000
-#define NSS_FREQ_550_TURBO	0
+#define NSS_FREQ_550		550000000	/* Frequency in hz */
+#define NSS_FREQ_550_MIN	0x14000		/* Instructions Per ms Min */
+#define NSS_FREQ_550_MAX	0x40000		/* Instructions Per ms Max */
 
-#define NSS_FREQ_733		733000000
-#define NSS_FREQ_733_DVDR	1
-#define NSS_FREQ_733_MIN	0x40000
-#define NSS_FREQ_733_MAX	0x50000
-#define NSS_FREQ_733_TURBO	1
+#define NSS_FREQ_733		733000000	/* Frequency in hz */
+#define NSS_FREQ_733_MIN	0x40000		/* Instructions Per ms Min */
+#define NSS_FREQ_733_MAX	0x50000		/* Instructions Per ms Max */
 
 /*
  * IPV4 node statistics
@@ -579,8 +576,6 @@
  */
 struct nss_scale_info {
 	uint32_t frequency;	/* Frequency in Mhz */
-	uint32_t divider;	/* Dividing the PLL Freq */
-	uint32_t turbo;		/* Flag to indicate turbo mode for hv */
 	uint32_t minimum;	/* Minimum INST_CNT per Sec */
 	uint32_t maximum;	/* Maximum INST_CNT per Sec */
 };
@@ -610,8 +605,6 @@
 typedef struct {
 	struct work_struct my_work;	/* Work Structure */
 	uint32_t frequency;		/* Frequency To Change */
-	uint32_t divider;		/* Divider To Achieve Change */
-	uint32_t turbo;			/* Flag to indicate turbo */
 } nss_work_t;
 
 /*
@@ -622,8 +615,6 @@
 					struct sk_buff *nbuf, uint16_t qid,
 					uint8_t buffer_type, uint16_t flags);
 extern int32_t nss_core_send_crypto(struct nss_ctx_instance *nss_ctx, void *buf, uint32_t buf_paddr, uint16_t len);
-extern void nss_change_frequency_nominal(uint32_t frequency, uint32_t frequency_divider);
-extern void nss_change_frequency_turbo(uint32_t frequency, uint32_t frequency_divider);
 extern void nss_wq_function( struct work_struct *work);
 
 /*
diff --git a/nss_hal/ipq806x/nss_hal_pvt.c b/nss_hal/ipq806x/nss_hal_pvt.c
index f1a0dca..4d7de32 100755
--- a/nss_hal/ipq806x/nss_hal_pvt.c
+++ b/nss_hal/ipq806x/nss_hal_pvt.c
@@ -95,6 +95,34 @@
 }
 
 /*
+ * nss_hal_pvt_register_dump
+ *	Dump Registers Regarding NSS
+ */
+void nss_hal_pvt_register_dump(void) {
+	nss_trace("NSSFB0_CLK_SRC_CTL	: %x\n", clk_reg_read_32(NSSFB0_CLK_SRC_CTL));
+	nss_trace("NSSFB1_CLK_SRC_CTL	: %x\n", clk_reg_read_32(NSSFB1_CLK_SRC_CTL));
+	nss_trace("NSSFB0_CLK_SRC0_NS	: %x\n", clk_reg_read_32(NSSFB0_CLK_SRC0_NS));
+	nss_trace("NSSFB0_CLK_SRC1_NS	: %x\n", clk_reg_read_32(NSSFB0_CLK_SRC1_NS));
+	nss_trace("NSSFB1_CLK_SRC0_NS	: %x\n", clk_reg_read_32(NSSFB1_CLK_SRC0_NS));
+	nss_trace("NSSFB1_CLK_SRC1_NS	: %x\n", clk_reg_read_32(NSSFB1_CLK_SRC1_NS));
+	nss_trace("\n");
+	nss_trace("PLL_ENA_NSS	: %x\n", clk_reg_read_32(PLL_ENA_NSS));
+	nss_trace("PLL18_L_VAL	: %x\n", clk_reg_read_32(PLL18_L_VAL));
+	nss_trace("PLL18_M_VAL	: %x\n", clk_reg_read_32(PLL18_M_VAL));
+	nss_trace("PLL18_N_VAL	: %x\n", clk_reg_read_32(PLL18_N_VAL));
+	nss_trace("PLL18_CONFIG	: %x\n", clk_reg_read_32(PLL18_CONFIG));
+	nss_trace("PLL18_TEST_CTL: %x\n", clk_reg_read_32(PLL18_TEST_CTL));
+	nss_trace("\n");
+	nss_trace("UBI32_COREn_CLK_SRC0_CTL Core 0: %x\n", clk_reg_read_32(UBI32_COREn_CLK_SRC_CTL(0)));
+	nss_trace("UBI32_COREn_CLK_SRC0_CTL Core 1: %x\n", clk_reg_read_32(UBI32_COREn_CLK_SRC_CTL(1)));
+	nss_trace("UBI32_COREn_CLK_SRC0_NS Core 0: %x\n", clk_reg_read_32(UBI32_COREn_CLK_SRC0_NS(0)));
+	nss_trace("UBI32_COREn_CLK_SRC0_NS Core 1: %x\n", clk_reg_read_32(UBI32_COREn_CLK_SRC0_NS(1)));
+	nss_trace("UBI32_COREn_CLK_SRC0_MD Core 0: %x\n", clk_reg_read_32(UBI32_COREn_CLK_SRC0_MD(0)));
+	nss_trace("UBI32_COREn_CLK_SRC0_MD Core 1: %x\n", clk_reg_read_32(UBI32_COREn_CLK_SRC0_MD(1)));
+	nss_trace("\n\n\n");
+}
+
+/*
  * nss_hal_pvt_divide_pll
  *	Divide PLL by int val
  */
@@ -119,8 +147,8 @@
 	nss_trace("NSSFB0_CLK_SRC1_NS  : %x\n", clk_reg_read_32(NSSFB0_CLK_SRC1_NS));
 	nss_trace("NSSFB1_CLK_SRC0_NS  : %x\n", clk_reg_read_32(NSSFB1_CLK_SRC0_NS));
 	nss_trace("NSSFB1_CLK_SRC1_NS  : %x\n", clk_reg_read_32(NSSFB1_CLK_SRC1_NS));
-	nss_trace("PLL_ENA_NSS	    : %x\n", clk_reg_read_32(PLL_ENA_NSS));
 	nss_trace("\n");
+	nss_trace("PLL_ENA_NSS	: %x\n", clk_reg_read_32(PLL_ENA_NSS));
 	nss_trace("PLL18_L_VAL  : %x\n", clk_reg_read_32(PLL18_L_VAL));
 	nss_trace("PLL18_M_VAL  : %x\n", clk_reg_read_32(PLL18_M_VAL));
 	nss_trace("PLL18_N_VAL  : %x\n", clk_reg_read_32(PLL18_N_VAL));
diff --git a/nss_hal/ipq806x/nss_hal_pvt.h b/nss_hal/ipq806x/nss_hal_pvt.h
index 1691f9f..4a58396 100755
--- a/nss_hal/ipq806x/nss_hal_pvt.h
+++ b/nss_hal/ipq806x/nss_hal_pvt.h
@@ -75,5 +75,6 @@
 extern uint32_t nss_hal_pvt_divide_pll18(uint32_t core_id, uint32_t divider);
 extern void nss_hal_pvt_pll_change(uint32_t pll);
 extern uint32_t nss_hal_pvt_enable_pll18(uint32_t speed);
+extern void nss_hal_pvt_register_dump(void);
 
 #endif /* __NSS_HAL_PVT_H */
diff --git a/nss_init.c b/nss_init.c
index f715126..d1efa0b 100755
--- a/nss_init.c
+++ b/nss_init.c
@@ -22,6 +22,7 @@
 
 #include "nss_core.h"
 #include <nss_hal.h>
+#include <nss_clocks.h>
 
 #include <linux/module.h>
 #include <linux/platform_device.h>
@@ -31,6 +32,7 @@
 
 #include <linux/sysctl.h>
 #include <linux/regulator/consumer.h>
+#include <linux/clk.h>
 
 /*
  * Declare module parameters
@@ -64,22 +66,11 @@
  * Global declarations
  */
 
-
-/*
- * Define for RPM Nominal and Turbo for NSS
- */
-#define NSS_NOM_VCC  1050000
-#define NSS_TURB_VCC 1150000
-
-/*
- * Global Handlers for the nss regulators
- */
-struct regulator *nss0_vreg;
-
 /*
  * Handler to send NSS messages
  */
 void *nss_freq_change_context;
+struct clk *nss_core0_clk;
 
 /*
  * Top level nss context structure
@@ -153,32 +144,17 @@
 	 * Both NSS cores controlled by same regulator, Hook only Once
 	 */
 	if (!nss_dev->id) {
-		nss0_vreg = devm_regulator_get(&nss_dev->dev, "VDD_UBI0");
+		nss_core0_clk = clk_get(&nss_dev->dev, "nss_core_clk");
+		if (IS_ERR(nss_core0_clk)) {
 
-		if (IS_ERR(nss0_vreg)) {
-
-			err = PTR_ERR(nss0_vreg);
+			err = PTR_ERR(nss_core0_clk);
 			nss_info("%p: Regulator %s get failed, err=%d\n", nss_ctx, dev_name(&nss_dev->dev), err);
 			return err;
 
-		} else {
-
-			nss_info("%p: Regulator %s get success\n", nss_ctx, dev_name(&nss_dev->dev));
-
-			err = regulator_enable(nss0_vreg);
-			if (err) {
-				nss_info("%p: Regulator %s enable voltage failed, err=%d\n", nss_ctx, dev_name(&nss_dev->dev), err);
-				return err;
-			}
-
-			err = regulator_set_voltage(nss0_vreg, NSS_NOM_VCC, NSS_NOM_VCC);
-			if (err) {
-				nss_info("%p: Regulator %s set voltage failed, err=%d\n", nss_ctx, dev_name(&nss_dev->dev), err);
-				return err;
-			}
-
-
 		}
+		clk_set_rate(nss_core0_clk, NSS_FREQ_550);
+		clk_prepare(nss_core0_clk);
+		clk_enable(nss_core0_clk);
 	}
 
 	/*
@@ -452,10 +428,9 @@
 
 /*
  ***************************************************************************************************
- * nss_wq_function() is used to queue up requests to change NSS frequencies. The frequency
- * changes may be nominal or turbo and is determined by the turbo value. The functions
- * nss_change_frequency_turbo() or nss_change_frequency_nominal() are used. The auto rate algorithmn
- * will queue up requests or the procfs may also queue up these requests.
+ * nss_wq_function() is used to queue up requests to change NSS frequencies.
+ * The function will take care of NSS notices and also control clock.
+ * The auto rate algorithmn will queue up requests or the procfs may also queue up these requests.
  ***************************************************************************************************
  */
 
@@ -467,76 +442,20 @@
 {
 	nss_work_t *my_work = (nss_work_t *)work;
 
-	if (my_work->turbo) {
-		nss_change_frequency_turbo(my_work->frequency, my_work->divider);
-	} else {
-		nss_change_frequency_nominal(my_work->frequency, my_work->divider);
-	}
+	nss_freq_change(nss_freq_change_context, my_work->frequency, 0);
+	clk_set_rate(nss_core0_clk, my_work->frequency);
+	nss_freq_change(nss_freq_change_context, my_work->frequency, 1);
 
 	kfree((void *)work);
 }
 
 /*
- * nss_change_frequency_turbo()
- *	Change the 733mhz clock by 1 divider
- */
-void nss_change_frequency_turbo (uint32_t frequency, uint32_t frequency_divider)
-{
-	int ret;
-
-	ret = regulator_set_voltage(nss0_vreg, NSS_TURB_VCC, NSS_TURB_VCC);
-	if (ret) {
-		nss_info("Regulator set voltage failed, err=%d\n", ret);
-	}
-
-	nss_freq_change(nss_freq_change_context, 533000000, 0);
-	nss_hal_pvt_pll_change(11);
-
-	nss_hal_pvt_enable_pll18(1466);
-	ret = nss_hal_pvt_divide_pll18(0, frequency_divider);
-	if (!ret) {
-		nss_info("PLL Divide failed, err=%d\n", ret);
-	}
-
-	nss_freq_change(nss_freq_change_context, frequency, 0);
-	nss_hal_pvt_pll_change(18);
-}
-
-/*
- * nss_change_frequency_nominal()
- *	Change the 550mhz clock by 1/2/5 divider
- */
-void nss_change_frequency_nominal (uint32_t frequency, uint32_t frequency_divider)
-{
-	int ret;
-
-	nss_freq_change(nss_freq_change_context, 533000000, 0);
-	nss_hal_pvt_pll_change(11);
-
-	nss_hal_pvt_enable_pll18(1100);
-	ret = nss_hal_pvt_divide_pll18(0, frequency_divider);
-	if (!ret) {
-		nss_info("PLL Divide failed, err=%d\n", ret);
-	}
-
-	nss_freq_change(nss_freq_change_context, frequency, 0);
-	nss_hal_pvt_pll_change(18);
-
-	ret = regulator_set_voltage(nss0_vreg, NSS_NOM_VCC, NSS_NOM_VCC);
-	if (ret) {
-		nss_info("Regulator set voltage failed, err=%d\n", ret);
-	}
-}
-
-/*
  * nss_current_freq_handler()
  *	Handle Userspace Frequency Change Requests
  */
 static int nss_current_freq_handler (ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
 {
 	int ret;
-	uint32_t turbo;
-	uint32_t divider;
 
 	BUG_ON(!nss_wq);
 
@@ -552,20 +471,8 @@
 	nss_runtime_samples.freq_scale_ready = 0;
 
 	/* If support NSS freq is in the table send the new frequency request to NSS */
-	if (nss_cmd_buf.current_freq == NSS_FREQ_110) {
-		divider = 5;
-		turbo = 0;
-	} else if (nss_cmd_buf.current_freq == NSS_FREQ_275) {
-		divider = 2;
-		turbo = 0;
-	} else if (nss_cmd_buf.current_freq == NSS_FREQ_550) {
-		divider = 1;
-		turbo = 0;
-	} else if (nss_cmd_buf.current_freq == NSS_FREQ_733) {
-		divider = 1;
-		turbo = 1;
-	} else {
-		nss_info("Frequency not found. Please check Frequency Table\n");
+	if ((nss_cmd_buf.current_freq != NSS_FREQ_110) && (nss_cmd_buf.current_freq != NSS_FREQ_275) && (nss_cmd_buf.current_freq != NSS_FREQ_550) && (nss_cmd_buf.current_freq != NSS_FREQ_733)) {
+		printk("Frequency not found. Please check Frequency Table\n");
 		return ret;
 	}
 
@@ -576,8 +483,6 @@
 	}
 	INIT_WORK((struct work_struct *)nss_work, nss_wq_function);
 	nss_work->frequency = nss_cmd_buf.current_freq;
-	nss_work->divider = divider;
-	nss_work->turbo = turbo;
 	queue_work(nss_wq, (struct work_struct *)nss_work);
 
 	return ret;
@@ -597,34 +502,38 @@
 		return ret;
 	}
 
-	if (nss_cmd_buf.auto_scale == 1) {
-		/*
-		 * Auto Scaling is already being done
-		 */
-		if (nss_runtime_samples.freq_scale_ready == 1) {
-			return ret;
-		}
-
-		/*
-		 * Setup default values - Middle of Freq Scale Band
-		 */
-		nss_runtime_samples.freq_scale_index = 1;
-		nss_cmd_buf.current_freq = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency;
-		nss_change_frequency_nominal(nss_cmd_buf.current_freq, 1);
-
-		nss_runtime_samples.freq_scale_ready = 1;
-
-	} else {
+	if (nss_cmd_buf.auto_scale != 1) {
 		/*
 		 * Auto Scaling is already disabled
 		 */
-		if ( nss_runtime_samples.freq_scale_ready == 0) {
-			return ret;
-		}
-
 		nss_runtime_samples.freq_scale_ready = 0;
+		return ret;
 	}
 
+	/*
+	 * Auto Scaling is already being done
+	 */
+	if (nss_runtime_samples.freq_scale_ready == 1) {
+		return ret;
+	}
+
+	/*
+	 * Setup default values - Middle of Freq Scale Band
+	 */
+	nss_runtime_samples.freq_scale_index = 1;
+	nss_cmd_buf.current_freq = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].frequency;
+
+	nss_work = (nss_work_t *)kmalloc(sizeof(nss_work_t), GFP_KERNEL);
+	if (!nss_work) {
+		nss_info("NSS Freq WQ kmalloc fail");
+		return ret;
+	}
+	INIT_WORK((struct work_struct *)nss_work, nss_wq_function);
+	nss_work->frequency = nss_cmd_buf.current_freq;
+	queue_work(nss_wq, (struct work_struct *)nss_work);
+
+	nss_runtime_samples.freq_scale_ready = 1;
+
 	return ret;
 }
 
@@ -742,20 +651,14 @@
 	 * Setup Runtime Sample values
 	 */
 	nss_runtime_samples.freq_scale[0].frequency = 	NSS_FREQ_110;
-	nss_runtime_samples.freq_scale[0].divider = 	NSS_FREQ_110_DVDR;
 	nss_runtime_samples.freq_scale[0].minimum =	NSS_FREQ_110_MIN;
 	nss_runtime_samples.freq_scale[0].maximum = 	NSS_FREQ_110_MAX;
-	nss_runtime_samples.freq_scale[0].turbo = 	NSS_FREQ_110_TURBO;
 	nss_runtime_samples.freq_scale[1].frequency = 	NSS_FREQ_550;
-	nss_runtime_samples.freq_scale[1].divider = 	NSS_FREQ_550_DVDR;
 	nss_runtime_samples.freq_scale[1].minimum = 	NSS_FREQ_550_MIN;
 	nss_runtime_samples.freq_scale[1].maximum = 	NSS_FREQ_550_MAX;
-	nss_runtime_samples.freq_scale[1].turbo = 	NSS_FREQ_550_TURBO;
 	nss_runtime_samples.freq_scale[2].frequency = 	NSS_FREQ_733;
-	nss_runtime_samples.freq_scale[2].divider = 	NSS_FREQ_733_DVDR;
 	nss_runtime_samples.freq_scale[2].minimum = 	NSS_FREQ_733_MIN;
 	nss_runtime_samples.freq_scale[2].maximum = 	NSS_FREQ_733_MAX;
-	nss_runtime_samples.freq_scale[2].turbo = 	NSS_FREQ_733_TURBO;
 	nss_runtime_samples.freq_scale_index = 1;
 	nss_runtime_samples.freq_scale_ready = 0;
 	nss_runtime_samples.freq_scale_rate_limit = 0;
diff --git a/nss_tx_rx.c b/nss_tx_rx.c
index faafa50..738e2d2 100755
--- a/nss_tx_rx.c
+++ b/nss_tx_rx.c
@@ -64,28 +64,29 @@
 
 /*
  * nss_rx_metadata_nss_freq_ack()
- *     Handle the nss ack of frequency change.
+ *	Handle the nss ack of frequency change.
  */
 static void nss_rx_metadata_nss_freq_ack(struct nss_ctx_instance *nss_ctx, struct nss_freq_ack *nfa)
 {
 	if (nfa->ack_status == NSS_ACK_STARTED) {
-
 		/*
 		 * NSS finished start noficiation - HW change clocks and send end notification
 		 */
-
 		nss_info("%p: NSS ACK Received: %d - Change HW CLK/Send Finish to NSS\n", nss_ctx, nfa->ack_status);
-		nss_freq_change(nss_freq_change_context, nfa->freq_current, 1);
 
-	} else if (nfa->ack_status == NSS_ACK_FINISHED) {
+		return;
+	}
 
+	if (nfa->ack_status == NSS_ACK_FINISHED) {
 		/*
 		 * NSS finished end notification - Done
 		 */
-		nss_info("%p: NSS Finish End Notification ACK: %d - Running: %dmhz\n", nss_ctx, nfa->ack_status, nfa->freq_current);
-	} else {
-		nss_info("%p: NSS had an error - Running: %dmhz\n", nss_ctx, nfa->freq_current);
+		nss_info("%p: NSS ACK Received: %d - End Notification ACK - Running: %dmhz\n", nss_ctx, nfa->ack_status, nfa->freq_current);
+
+		return;
 	}
+
+	nss_info("%p: NSS had an error - Running: %dmhz\n", nss_ctx, nfa->freq_current);
 }
 
 /*
@@ -675,8 +676,6 @@
 
 	INIT_WORK((struct work_struct *)nss_work, nss_wq_function);
 	nss_work->frequency = nss_cmd_buf.current_freq;
-	nss_work->divider = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].divider;
-	nss_work->turbo = nss_runtime_samples.freq_scale[nss_runtime_samples.freq_scale_index].turbo;
 	queue_work(nss_wq, (struct work_struct *)nss_work);
 }