# SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
#
# Copyright (c) 2023 MediaTek Inc.
import sys
import xlrd

###enter file name
#excel_filename_PWS_table=raw_input('Enter PWS excel fle:')
#if excel_filename_PWS_table == '' :
#    print "Input Error....\n"
#    sys.exit()

###Excel file handle
# book_pws = xlrd.open_workbook(excel_filename_PWS_table)
book_spi = xlrd.open_workbook("spi_device_gen.xls")
#print "The number of sheets is ", book_pws.nsheets
#print "Sheet(s) name(s):", book_pws.sheet_names()

#print "input_source_list dir ", input_source_list
###text file handle
#open file (pws_setting_info_table.h)

#get sheet info sheet
sheet_list_info = book_spi.sheet_by_name("Sheet_Info")
sheet_dev_info = sheet_list_info.col_values(0, 2, None)
space = ""
tab = "	"
LicenseString = '\
// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)\n\
/*\n\
 * Copyright (c) 2020 MediaTek Inc.\n\
 */\n'
IncludeString = '\n\
#include <linux/module.h>\n\
#include <linux/platform_device.h>\n\
#include <linux/of_device.h>\n\
#include <linux/delay.h>\n\
#include <linux/version.h>\n\
#include <linux/module.h>\n\
#include <linux/of.h>\n\
#include <linux/err.h>\n\
#include <linux/spi/spi.h>\n\
#include <linux/interrupt.h>\n\
#include <linux/irq.h>\n\
#include <linux/of_irq.h>\n\
#include <linux/of_address.h>\n\
#include <linux/gpio/consumer.h>\n\
#include <linux/pinctrl/consumer.h>\n\
#include <linux/errno.h>\n\
#include <linux/mutex.h>\n'
StructString ='\n\
	uint8_t u1cmdlen;\n\
	uint8_t *u1cmd;\n\
	uint32_t u4wrbytes;\n\
	uint8_t *u1wrbuf;\n\
	uint32_t u4rbytes;\n\
	uint8_t *u1rrbuf;\n\
	uint32_t u4wandrbytes;\n\
	uint8_t *u1wandrbuf;\n\
	u32 speed;\n\
};\n'
ReturnString = '\n\
	if (status)\n\
		printk("%s", __func__);\n\
	return status;\n\
}\n'
WrteInitString = '\
{\n\
	int status = 0;\n\
	struct spi_transfer x;\n\
	struct spi_message msg;\n'
WrteApiString = '\n\
	memset(&x, 0, sizeof(x));\n\
	spi_message_init(&msg);\n\
	x.tx_buf = packet->u1wrbuf;\n\
	x.rx_buf = NULL;\n\
	x.len        = packet->u4wrbytes;\n\
	x.tx_nbits    = SPI_NBITS_SINGLE;\n\
	x.rx_nbits    = SPI_NBITS_SINGLE;\n\
	x.speed_hz    = packet->speed;\n\
	x.bits_per_word = 8;\n\
	x.cs_change = 1;\n\
	spi_message_add_tail(&x, &msg);\n\
	status = spi_sync(dvb->pdev, &msg);'
WrteCmdDatInitString = '\
{\n\
	int status = 0;\n\
	struct spi_transfer x[2];\n\
	struct spi_message msg;\n'
WrteCmdDatApiString = '\n\
	memset(&x, 0, sizeof(x));\n\
	spi_message_init(&msg);\n\
	x[0].tx_buf = packet->u1cmd;\n\
	x[0].rx_buf = NULL;\n\
	x[0].len        = packet->u1cmdlen;\n\
	x[0].tx_nbits    = SPI_NBITS_SINGLE;\n\
	x[0].rx_nbits    = SPI_NBITS_SINGLE;\n\
	x[0].speed_hz    = packet->speed;\n\
	x[0].bits_per_word = 8;\n\
	x[0].cs_change = 0;\n\
	spi_message_add_tail(&x[0], &msg);\n\
	x[1].tx_buf = packet->u1wrbuf;\n\
	x[1].rx_buf = NULL;\n\
	x[1].len        = packet->u4wrbytes;\n\
	x[1].tx_nbits    = SPI_NBITS_SINGLE;\n\
	x[1].rx_nbits    = SPI_NBITS_SINGLE;\n\
	x[1].speed_hz    = packet->speed;\n\
	x[1].bits_per_word = 8;\n\
	x[1].cs_change = 1;\n\
	spi_message_add_tail(&x[1], &msg);\n\
	status = spi_sync(dvb->pdev, &msg);'
WrteCmdDatReadInitString = '\
{\n\
	int status = 0;\n\
	struct spi_transfer x[4];\n\
	struct spi_message msg;\n'
WrteCmdDatReadApiString = '\n\
	memset(&x, 0, sizeof(x));\n\
	spi_message_init(&msg);\n\
	x[0].tx_buf = packet->u1cmd;\n\
	x[0].rx_buf = NULL;\n\
	x[0].len        = packet->u1cmdlen;\n\
	x[0].tx_nbits    = SPI_NBITS_SINGLE;\n\
	x[0].rx_nbits    = SPI_NBITS_SINGLE;\n\
	x[0].speed_hz    = packet->speed;\n\
	x[0].bits_per_word = 8;\n\
	x[0].cs_change = 0;\n\
	spi_message_add_tail(&x[0], &msg);\n\
	x[1].tx_buf = packet->u1wrbuf;\n\
	x[1].rx_buf = NULL;\n\
	x[1].len        = packet->u4wrbytes;\n\
	x[1].tx_nbits    = SPI_NBITS_SINGLE;\n\
	x[1].rx_nbits    = SPI_NBITS_SINGLE;\n\
	x[1].speed_hz    = packet->speed;\n\
	x[1].bits_per_word = 8;\n\
	x[1].cs_change = 0;\n\
	spi_message_add_tail(&x[1], &msg);\n\
	x[2].tx_buf = packet->u1wandrbuf;\n\
	x[2].rx_buf = NULL;\n\
	x[2].len        = packet->u4wandrbytes;\n\
	x[2].tx_nbits    = SPI_NBITS_SINGLE;\n\
	x[2].rx_nbits    = SPI_NBITS_SINGLE;\n\
	x[2].speed_hz    = packet->speed;\n\
	x[2].bits_per_word = 8;\n\
	x[2].cs_change = 0;\n\
	spi_message_add_tail(&x[2], &msg);\n\
	x[3].tx_buf = NULL;\n\
	x[3].rx_buf = packet->u1rrbuf;\n\
	x[3].len        = packet->u4rbytes;\n\
	x[3].tx_nbits    = SPI_NBITS_SINGLE;\n\
	x[3].rx_nbits    = SPI_NBITS_SINGLE;\n\
	x[3].speed_hz    = packet->speed;\n\
	x[3].bits_per_word = 8;\n\
	x[3].cs_change = 1;\n\
	spi_message_add_tail(&x[2], &msg);\n\
	status = spi_sync(dvb->pdev, &msg);'
WrteDatReadInitString = '\
{\n\
	int status = 0;\n\
	struct spi_transfer x[2];\n\
	struct spi_message msg;\n'
WrteDatReadApiString = '\n\
	memset(&x, 0, sizeof(x));\n\
	spi_message_init(&msg);\n\
	x[0].tx_buf = packet->u1cmd;\n\
	x[0].rx_buf = NULL;\n\
	x[0].len        = packet->u1cmdlen;\n\
	x[0].tx_nbits    = SPI_NBITS_SINGLE;\n\
	x[0].rx_nbits    = SPI_NBITS_SINGLE;\n\
	x[0].speed_hz    = packet->speed;\n\
	x[0].bits_per_word = 8;\n\
	x[0].cs_change = 0;\n\
	spi_message_add_tail(&x[0], &msg);\n\
	x[1].tx_buf = NULL;\n\
	x[1].rx_buf = packet->u1rrbuf;\n\
	x[1].len        = packet->u4rbytes;\n\
	x[1].tx_nbits    = SPI_NBITS_SINGLE;\n\
	x[1].rx_nbits    = SPI_NBITS_SINGLE;\n\
	x[1].speed_hz    = packet->speed;\n\
	x[1].bits_per_word = 8;\n\
	x[1].cs_change = 1;\n\
	spi_message_add_tail(&x[1], &msg);\n\
	status = spi_sync(dvb->pdev, &msg);'
ReadInitString = '\
{\n\
	int status = 0;\n\
	struct spi_transfer x;\n\
	struct spi_message msg;\n'
ReadApiString = '\n\
	memset(&x, 0, sizeof(x));\n\
	spi_message_init(&msg);\n\
	x.tx_buf = NULL;\n\
	x.rx_buf = packet->u1rrbuf;\n\
	x.len        = packet->u4rbytes;\n\
	x.tx_nbits    = SPI_NBITS_SINGLE;\n\
	x.rx_nbits    = SPI_NBITS_SINGLE;\n\
	x.speed_hz    = packet->speed;\n\
	x.bits_per_word = 8;\n\
	x.cs_change = 1;\n\
	spi_message_add_tail(&x, &msg);\n\
	status = spi_sync(dvb->pdev, &msg);'
WrteDatRead2CsInitString = '\
{\n\
	int status = 0;\n\
	struct spi_transfer x[2];\n\
	struct spi_message msg;\n'
WrteDatRead2CsApiString = '\n\
	memset(&x, 0, sizeof(x));\n\
	spi_message_init(&msg);\n\
	x[0].tx_buf = packet->u1cmd;\n\
	x[0].rx_buf = NULL;\n\
	x[0].len        = packet->u1cmdlen;\n\
	x[0].tx_nbits    = SPI_NBITS_SINGLE;\n\
	x[0].rx_nbits    = SPI_NBITS_SINGLE;\n\
	x[0].speed_hz    = packet->speed;\n\
	x[0].bits_per_word = 8;\n\
	x[0].cs_change = 1;\n\
	spi_message_add_tail(&x[0], &msg);\n\
	x[1].tx_buf = NULL;\n\
	x[1].rx_buf = packet->u1rrbuf;\n\
	x[1].len        = packet->u4rbytes;\n\
	x[1].tx_nbits    = SPI_NBITS_SINGLE;\n\
	x[1].rx_nbits    = SPI_NBITS_SINGLE;\n\
	x[1].speed_hz    = packet->speed;\n\
	x[1].bits_per_word = 8;\n\
	x[1].cs_change = 1;\n\
	spi_message_add_tail(&x[1], &msg);\n\
	status = spi_sync(dvb->pdev, &msg);'
for x in sheet_dev_info:
   sheet_IP = book_spi.sheet_by_name(x)
   sheet_IP_FileName = sheet_IP.col_values(1, 2, None)
   sheet_IP_DeviceName = sheet_IP.col_values(2, 2, None)
   sheet_IP_StructName = sheet_IP.col_values(3, 1, None)
   sheet_IP_StructNodeName = sheet_IP.col_values(3, 2, None)
   sheet_IP_StructDev = sheet_IP.col_values(4, 1, None)
   sheet_IP_StructDevName = sheet_IP.col_values(4, 2, None)
   sheet_IP_StructGpioDev = sheet_IP.col_values(5, 1, None)
   sheet_IP_StructPacket = sheet_IP.col_values(6, 1, None)
   sheet_IP_StructGpioDevName = sheet_IP.col_values(5, 2, None)
   sheet_IP_W_Api_chk = sheet_IP.col_values(2, 10, None)
   sheet_IP_W_CmdDat_Api_chk = sheet_IP.col_values(2, 15, None)
   sheet_IP_W_CmdDatR_Api_chk = sheet_IP.col_values(2, 20, None)
   sheet_IP_W_DatR_Api_chk = sheet_IP.col_values(2, 25, None)
   sheet_IP_R_Api_chk = sheet_IP.col_values(2, 30, None)
   sheet_IP_WR2CS_Api_chk = sheet_IP.col_values(2, 35, None)
   sheet_IP_W_Api_nme = sheet_IP.col_values(2, 9, None)
   sheet_IP_W_CmdDat_Api_nme = sheet_IP.col_values(2, 14, None)
   sheet_IP_W_CmdDatR_Api_nme = sheet_IP.col_values(2, 19, None)
   sheet_IP_W_DatR_Api_nme = sheet_IP.col_values(2, 24, None)
   sheet_IP_R_Api_nme = sheet_IP.col_values(2, 29, None)
   sheet_IP_WR2CS_Api_nme = sheet_IP.col_values(2, 34, None)
   sheet_DeviceName_len = len(sheet_IP_FileName) -1 #zero-based
   sheet_index = 0
   #Calculate Vendor String Len and Reorder
   #while sheet_index <= sheet_DeviceName_len :
   w_api_chk_string = sheet_IP_W_Api_chk[sheet_index]
   w_cmddat_api_chk_string = sheet_IP_W_CmdDat_Api_chk[sheet_index]
   w_cmddat_r_api_chk_string = sheet_IP_W_CmdDatR_Api_chk[sheet_index]
   w_dat_r_api_chk_string = sheet_IP_W_DatR_Api_chk[sheet_index]
   r_api_chk_string = sheet_IP_R_Api_chk[sheet_index]
   wr2cs_api_chk_string = sheet_IP_WR2CS_Api_chk[sheet_index]
   w_api_nme_string = sheet_IP_W_Api_nme[sheet_index]
   w_cmddat_api_nme_string = sheet_IP_W_CmdDat_Api_nme[sheet_index]
   w_cmddat_r_api_nme_string = sheet_IP_W_CmdDatR_Api_nme[sheet_index]
   w_dat_r_api_nme_string = sheet_IP_W_DatR_Api_nme[sheet_index]
   r_api_nme_string = sheet_IP_R_Api_nme[sheet_index]
   wr2cs_api_nme_string = sheet_IP_WR2CS_Api_nme[sheet_index]
   filestring = sheet_IP_FileName[sheet_index]+'.c'
   devstring = sheet_IP_DeviceName[sheet_index]
   structstring = sheet_IP_StructName[sheet_index]
   strnodestring = sheet_IP_StructNodeName[sheet_index]
   structdevstr = sheet_IP_StructDev[sheet_index]
   structdevnamestr = sheet_IP_StructDevName[sheet_index]
   gpidevnamstr = sheet_IP_StructGpioDevName[sheet_index]
   strgpinamstr = sheet_IP_StructGpioDev[sheet_index]
   packetnamstr = sheet_IP_StructPacket[sheet_index]
   staticstr = 'static'
   device_spi_str = 'spidev = pdev;'
   drvstr = '_driver = {'
   dotstr = '.'
   probstr = 'probe'
   rmstr = '.remove ='
   dtstr = '_dt_match'
   opsstr ='_pm_ops'
   suspstr = 'suspend'
   rsmstr = 'resume'
   rmonlystr = 'remove'
   equstr = '='
   pointstr = '*'
   dashstr = '_'
   tab_next = '		'
   calbackstr ='(struct device *pdev)'
   apicalstr ='static int'
   bparenthlstr ='{'
   bparenthrstr ='}'
   parenthlstr ='('
   parenthrstr =')'
   pdevstr ='pdev'
   semstr = ';'
   intstr = 'int'
   voidstr = 'void'
   retstr = 'ret'
   nullstr = 'NULL'
   getspidevstr ='spi_get_drvdata'
   rtnstr ='return'
   zerostr = '0'
   klocstr = 'kzalloc'
   sizestr = 'sizeof'
   gfptypestr = 'GFP_KERNEL'
   comastr = ','
   ifstr = 'if'
   clamstr ='!'
   aimstr ='->'
   andstr ='&'
   devstr ='dev'
   quostr ='"'
   colonstr = ':'
   gotostr ='goto'
   nonetstr ='-ENONET'
   errstr = 'err_exit'
   rtnfaillocatstr ='return -ENOMEM'
   gpigetstr = 'devm_gpiod_get'
   higpiostr = 'GPIOD_OUT_HIGH'
   freestr = 'kfree'
   packetstr = 'packet'
   setprivstr = 'spi_set_drvdata'
   setgpistr = 'gpiod_set_value'
   exportsymbolstr = 'EXPORT_SYMBOL'
   mutexstr = 'DEFINE_MUTEX'
   lockstr = 'lock'
   mutex_lockstr = 'mutex_lock'
   mutex_unlockstr = 'mutex_unlock'
   pntaddrstr = '&'
   errcatstr = devstring+dashstr+probstr+dashstr+errstr
   equacatstr = space.rjust(1)+equstr+space.rjust(1)
   rtnzerostr = rtnstr+space.rjust(1)+zerostr+semstr
   apistrucstr = parenthlstr+packetnamstr+space.rjust(1)+pointstr+packetstr+parenthrstr+'\n'
   mutex_lockapistr = tab.rjust(1)+mutex_lockstr+parenthlstr+pntaddrstr+devstring+dashstr+lockstr+parenthrstr+semstr+'\n'
   mutex_unlockapistr = tab.rjust(1)+mutex_unlockstr+parenthlstr+pntaddrstr+devstring+dashstr+lockstr+parenthrstr+semstr+'\n'
   spi_table_file = open(filestring,'w')
   #License and include
   spi_table_file.write(LicenseString)
   spi_table_file.write(IncludeString)
   spi_table_file.write('\n')
   #Init Device structure
   spi_table_file.write(structstring+space.rjust(1)+bparenthlstr+'\n')
   spi_table_file.write(tab.rjust(1)+structdevstr+space.rjust(1)+pointstr+structdevnamestr+semstr+'\n')
   spi_table_file.write(tab.rjust(1)+strgpinamstr+space.rjust(1)+pointstr+gpidevnamstr+semstr+'\n')
   spi_table_file.write(bparenthrstr+semstr+'\n')
   spi_table_file.write('\n')
   #Init packet structure Dat
   spi_table_file.write(packetnamstr+space.rjust(1)+bparenthlstr)
   spi_table_file.write(StructString)
   spi_table_file.write('\n')
   #declare structure Dat
   spi_table_file.write(staticstr+space.rjust(1)+structstring+space.rjust(1)+pointstr+strnodestring+semstr+'\n')
   #declare lock Dat
   spi_table_file.write(staticstr+space.rjust(1)+mutexstr+parenthlstr+devstring+dashstr+lockstr+parenthrstr+semstr+'\n')
   spi_table_file.write('\n')
   #Gen API code
   if w_api_chk_string == 'Y':
      spi_table_file.write(intstr+space.rjust(1)+devstring+dashstr+w_api_nme_string+apistrucstr)
      spi_table_file.write(WrteInitString)
      spi_table_file.write('\n')
      spi_table_file.write(mutex_lockapistr)
      spi_table_file.write(WrteApiString)
      spi_table_file.write('\n')
      spi_table_file.write('\n')
      spi_table_file.write(mutex_unlockapistr)
      spi_table_file.write(ReturnString)
      spi_table_file.write(exportsymbolstr+parenthlstr+devstring+dashstr+w_api_nme_string+parenthrstr+semstr+'\n')
   spi_table_file.write('\n')
   if w_cmddat_api_chk_string == 'Y':
      spi_table_file.write(intstr+space.rjust(1)+devstring+dashstr+w_cmddat_api_nme_string+apistrucstr)
      spi_table_file.write(WrteCmdDatInitString)
      spi_table_file.write('\n')
      spi_table_file.write(mutex_lockapistr)
      spi_table_file.write(WrteCmdDatApiString)
      spi_table_file.write('\n')
      spi_table_file.write('\n')
      spi_table_file.write(mutex_unlockapistr)
      spi_table_file.write(ReturnString)
      spi_table_file.write(exportsymbolstr+parenthlstr+devstring+dashstr+w_cmddat_api_nme_string+parenthrstr+semstr+'\n')
   spi_table_file.write('\n')
   if w_cmddat_r_api_chk_string == 'Y':
      spi_table_file.write(intstr+space.rjust(1)+devstring+dashstr+w_cmddat_r_api_nme_string+apistrucstr)
      spi_table_file.write(WrteCmdDatReadInitString)
      spi_table_file.write('\n')
      spi_table_file.write(mutex_lockapistr)
      spi_table_file.write(WrteCmdDatReadApiString)
      spi_table_file.write('\n')
      spi_table_file.write('\n')
      spi_table_file.write(mutex_unlockapistr)
      spi_table_file.write(ReturnString)
      spi_table_file.write(exportsymbolstr+parenthlstr+devstring+dashstr+w_cmddat_r_api_nme_string+parenthrstr+semstr+'\n')
   spi_table_file.write('\n')
   if w_dat_r_api_chk_string == 'Y':
      spi_table_file.write(intstr+space.rjust(1)+devstring+dashstr+w_dat_r_api_nme_string+apistrucstr)
      spi_table_file.write(WrteDatReadInitString)
      spi_table_file.write('\n')
      spi_table_file.write(mutex_lockapistr)
      spi_table_file.write(WrteDatReadApiString)
      spi_table_file.write('\n')
      spi_table_file.write('\n')
      spi_table_file.write(mutex_unlockapistr)
      spi_table_file.write(ReturnString)
      spi_table_file.write(exportsymbolstr+parenthlstr+devstring+dashstr+w_dat_r_api_nme_string+parenthrstr+semstr+'\n')
   spi_table_file.write('\n')
   if r_api_chk_string == 'Y':
      spi_table_file.write(intstr+space.rjust(1)+devstring+dashstr+r_api_nme_string+apistrucstr)
      spi_table_file.write(ReadInitString)
      spi_table_file.write('\n')
      spi_table_file.write(mutex_lockapistr)
      spi_table_file.write(ReadApiString)
      spi_table_file.write('\n')
      spi_table_file.write('\n')
      spi_table_file.write(mutex_unlockapistr)
      spi_table_file.write(ReturnString)
      spi_table_file.write(exportsymbolstr+parenthlstr+devstring+dashstr+r_api_nme_string+parenthrstr+semstr+'\n')
   spi_table_file.write('\n')
   if wr2cs_api_chk_string == 'Y':
      spi_table_file.write(intstr+space.rjust(1)+devstring+dashstr+wr2cs_api_nme_string+apistrucstr)
      spi_table_file.write(WrteDatRead2CsInitString)
      spi_table_file.write('\n')
      spi_table_file.write(mutex_lockapistr)
      spi_table_file.write(WrteDatRead2CsApiString)
      spi_table_file.write('\n')
      spi_table_file.write('\n')
      spi_table_file.write(mutex_unlockapistr)
      spi_table_file.write(ReturnString)
      spi_table_file.write(exportsymbolstr+parenthlstr+devstring+dashstr+wr2cs_api_nme_string+parenthrstr+semstr+'\n')
   spi_table_file.write('\n')
   #Gen GPIO Set code
   spi_table_file.write(voidstr+space.rjust(1)+devstring+dashstr+gpidevnamstr+parenthlstr+intstr+space.rjust(1)+gpidevnamstr+parenthrstr+'\n')
   spi_table_file.write(bparenthlstr+'\n')
   spi_table_file.write(tab.rjust(1)+setgpistr+parenthlstr+strnodestring+aimstr+gpidevnamstr+comastr+space.rjust(1)+gpidevnamstr+parenthrstr+semstr+'\n')
   spi_table_file.write(bparenthrstr+'\n')
   spi_table_file.write(exportsymbolstr+parenthlstr+devstring+dashstr+gpidevnamstr+parenthrstr+semstr+'\n')
   spi_table_file.write('\n')
   #Gen probe init code
   spi_table_file.write(apicalstr+space.rjust(1)+devstring+dashstr+probstr+parenthlstr+structdevstr+space.rjust(1)+pointstr+structdevnamestr+parenthrstr+'\n')
   spi_table_file.write(bparenthlstr+'\n')
   spi_table_file.write(tab.rjust(1)+intstr+space.rjust(1)+retstr+semstr+'\n')
   spi_table_file.write('\n')
   spi_table_file.write(tab.rjust(1)+strnodestring+equacatstr+klocstr+parenthlstr+sizestr+parenthlstr+structstring+parenthrstr+comastr+space.rjust(1)+gfptypestr+parenthrstr+semstr+'\n')
   spi_table_file.write(tab.rjust(1)+ifstr+space.rjust(1)+parenthlstr+clamstr+strnodestring+parenthrstr+'\n')
   spi_table_file.write(tab_next+rtnfaillocatstr+semstr+'\n')
   spi_table_file.write(tab.rjust(1)+strnodestring+aimstr+structdevnamestr+equacatstr+structdevnamestr+semstr+'\n')
   #devm_gpiod_get
   spi_table_file.write(tab.rjust(1)+strnodestring+aimstr+gpidevnamstr+equacatstr+gpigetstr+parenthlstr+andstr+structdevnamestr+aimstr+devstr+comastr)
   spi_table_file.write(space.rjust(1)+quostr+gpidevnamstr+quostr+comastr+space.rjust(1)+higpiostr+parenthrstr+semstr+'\n')
   #devm_gpiod_get ci_reset check
   spi_table_file.write(tab.rjust(1)+ifstr+space.rjust(1)+parenthlstr+clamstr+strnodestring+aimstr+gpidevnamstr+parenthrstr+space.rjust(1)+bparenthlstr+'\n')
   spi_table_file.write(tab_next+retstr+equacatstr+nonetstr+semstr+'\n')
   spi_table_file.write(tab_next+gotostr+space.rjust(1)+errcatstr+semstr+'\n')
   spi_table_file.write(tab.rjust(1)+bparenthrstr+'\n')
   spi_table_file.write(tab.rjust(1)+setprivstr+parenthlstr+structdevnamestr+comastr+space.rjust(1)+strnodestring+parenthrstr+semstr+'\n')
   spi_table_file.write('\n')
   spi_table_file.write(tab.rjust(1)+rtnstr+space.rjust(1)+retstr+semstr+'\n')
   spi_table_file.write('\n')
   spi_table_file.write(errcatstr+colonstr+'\n')
   spi_table_file.write(tab.rjust(1)+ifstr+space.rjust(1)+parenthlstr+strnodestring+parenthrstr+'\n')
   spi_table_file.write(tab_next+freestr+parenthlstr+strnodestring+parenthrstr+semstr+'\n')
   spi_table_file.write(tab.rjust(1)+rtnstr+space.rjust(1)+retstr+semstr+'\n')
   spi_table_file.write(bparenthrstr+'\n')
   spi_table_file.write('\n')
   #Gen Resume callback code
   spi_table_file.write(apicalstr+space.rjust(1)+devstring+dashstr+rsmstr+calbackstr+'\n')
   spi_table_file.write(bparenthlstr+'\n')
   #spi_table_file.write(tab.rjust(1)+structstring+space.rjust(1)+pointstr+strnodestring+equacatstr+getspidevstr+parenthlstr+strnodestring+parenthrstr+semstr+'\n')
   spi_table_file.write('\n')
   spi_table_file.write(tab.rjust(1)+rtnzerostr+'\n')
   spi_table_file.write(bparenthrstr+'\n')
   spi_table_file.write('\n')
   #Gen Suspend callback code
   spi_table_file.write(apicalstr+space.rjust(1)+devstring+dashstr+suspstr+calbackstr+'\n')
   spi_table_file.write(bparenthlstr+'\n')
   #spi_table_file.write(tab.rjust(1)+structstring+space.rjust(1)+pointstr+strnodestring+equacatstr+getspidevstr+parenthlstr+strnodestring+parenthrstr+semstr+'\n')
   spi_table_file.write('\n')
   spi_table_file.write(tab.rjust(1)+rtnzerostr+'\n')
   spi_table_file.write(bparenthrstr+'\n')
   spi_table_file.write('\n')
   #Gen remove callback code
   #Gen pmops code
   spi_table_file.write('static const struct dev_pm_ops'+space.rjust(1)+devstring+opsstr+' = {'+'\n')
   spi_table_file.write(tab.rjust(1)+'.'+suspstr+equacatstr+devstring+dashstr+suspstr+','+'\n')
   spi_table_file.write(tab.rjust(1)+'.'+rsmstr+equacatstr+devstring+dashstr+rsmstr+','+'\n')
   spi_table_file.write('};'+'\n')
   spi_table_file.write('\n')
   #Gen dtmatch table code
   spi_table_file.write('static const struct of_device_id'+space.rjust(1)+devstring+dtstr+'[] = {'+'\n')
   spi_table_file.write(tab.rjust(1)+bparenthlstr+'.compatible ='+space.rjust(1)+'"mtk,'+devstring+'"},'+'\n')
   spi_table_file.write(tab.rjust(1)+'{},'+'\n')
   spi_table_file.write('};'+'\n')
   spi_table_file.write('MODULE_DEVICE_TABLE(of,'+space.rjust(1)+devstring+dtstr+');'+'\n')
   spi_table_file.write('\n')
   #Gen device module code
   spi_table_file.write('static struct spi_driver'+space.rjust(1)+devstring+drvstr+'\n')
   spi_table_file.write(tab.rjust(1)+dotstr+probstr+equacatstr+devstring+dashstr+probstr+','+'\n')
   #spi_table_file.write(tab.rjust(1)+rmstr+space.rjust(1)+devstring+'_remove,'+'\n')
   spi_table_file.write(tab.rjust(1)+rmstr+space.rjust(1)+nullstr+comastr+'\n')
   spi_table_file.write(tab.rjust(1)+'.driver = {'+'\n')
   spi_table_file.write(tab_next+'.name = "'+devstring+'",'+'\n')
   spi_table_file.write(tab_next+'.owner = THIS_MODULE,'+'\n')
   spi_table_file.write(tab_next+'.of_match_table ='+space.rjust(1)+devstring+dtstr+','+'\n')
   spi_table_file.write(tab_next+'.pm ='+space.rjust(1)+pntaddrstr+devstring+opsstr+'\n')
   spi_table_file.write(tab.rjust(1)+'},'+'\n')
   spi_table_file.write('};'+'\n')
   spi_table_file.write('module_spi_driver('+devstring+'_driver'+')'+'\n')
   spi_table_file.close()
        #sheet_index = sheet_index +1


#print "Device SPI generating OK...!!!"




