mt_ptp.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537
  1. unsigned int reg_dump_addr_off[] = {
  2. 0x0000,
  3. 0x0004,
  4. 0x0008,
  5. 0x000C,
  6. 0x0010,
  7. 0x0014,
  8. 0x0018,
  9. 0x001c,
  10. 0x0024,
  11. 0x0028,
  12. 0x002c,
  13. 0x0030,
  14. 0x0034,
  15. 0x0038,
  16. 0x003c,
  17. 0x0040,
  18. 0x0044,
  19. 0x0048,
  20. 0x004c,
  21. 0x0050,
  22. 0x0054,
  23. 0x0058,
  24. 0x005c,
  25. 0x0060,
  26. 0x0064,
  27. 0x0068,
  28. 0x006c,
  29. 0x0070,
  30. 0x0074,
  31. 0x0078,
  32. 0x007c,
  33. 0x0080,
  34. 0x0084,
  35. 0x0088,
  36. 0x008c,
  37. 0x0090,
  38. 0x0094,
  39. 0x0098,
  40. 0x00a0,
  41. 0x00a4,
  42. 0x00a8,
  43. 0x00B0,
  44. 0x00B4,
  45. 0x00B8,
  46. 0x00BC,
  47. 0x00C0,
  48. 0x00C4,
  49. 0x00C8,
  50. 0x00CC,
  51. 0x00F0,
  52. 0x00F4,
  53. 0x00F8,
  54. 0x00FC,
  55. 0x0200,
  56. 0x0204,
  57. 0x0208,
  58. 0x020C,
  59. 0x0210,
  60. 0x0214,
  61. 0x0218,
  62. 0x021C,
  63. 0x0220,
  64. 0x0224,
  65. 0x0228,
  66. 0x022C,
  67. 0x0230,
  68. 0x0234,
  69. 0x0238,
  70. 0x023C,
  71. 0x0240,
  72. 0x0244,
  73. 0x0248,
  74. 0x024C,
  75. 0x0250,
  76. 0x0254,
  77. 0x0258,
  78. 0x025C,
  79. 0x0260,
  80. 0x0264,
  81. 0x0268,
  82. 0x026C,
  83. 0x0270,
  84. 0x0274,
  85. 0x0278,
  86. 0x027C,
  87. 0x0280,
  88. 0x0284,
  89. 0x0400,
  90. 0x0404,
  91. 0x0408,
  92. 0x040C,
  93. 0x0410,
  94. 0x0414,
  95. 0x0418,
  96. 0x041C,
  97. 0x0420,
  98. 0x0424,
  99. 0x0428,
  100. 0x042C,
  101. 0x0430,
  102. };
  103. /*
  104. * @file mt_ptp.c
  105. * @brief Driver for PTP
  106. *
  107. */
  108. #define __MT_PTP_C__
  109. /*
  110. * Include files
  111. */
  112. /* system includes */
  113. #include <linux/init.h>
  114. #include <linux/module.h>
  115. #include <linux/kernel.h>
  116. #include <linux/proc_fs.h>
  117. #include <linux/seq_file.h>
  118. #include <linux/spinlock.h>
  119. #include <linux/kthread.h>
  120. #include <linux/hrtimer.h>
  121. #include <linux/ktime.h>
  122. #include <linux/interrupt.h>
  123. #include <linux/syscore_ops.h>
  124. #include <linux/platform_device.h>
  125. #include <linux/completion.h>
  126. #include <linux/fs.h>
  127. #include <linux/file.h>
  128. #include <linux/uaccess.h>
  129. #include <linux/seq_file.h>
  130. #include <asm/io.h>
  131. #include "mt_ptp.h"
  132. #include "mt_cpufreq.h"
  133. #include "mach/mt_thermal.h"
  134. #include "mach/mt_clkmgr.h"
  135. #include "mach/mt_freqhopping.h"
  136. #include "mt-plat/upmu_common.h"
  137. #ifdef CONFIG_OF
  138. #include <linux/of.h>
  139. #include <linux/of_irq.h>
  140. #include <linux/of_address.h>
  141. #include <linux/of_fdt.h>
  142. #endif
  143. /* local includes */
  144. #include <mt_spm.h>
  145. #include "aee.h"
  146. #include <linux/gpio.h>
  147. /* Global variable for slow idle*/
  148. unsigned int ptp_data[3] = { 0, 0, 0 };
  149. struct ptp_det;
  150. struct ptp_ctrl;
  151. static void ptp_set_ptp_volt(struct ptp_det *det);
  152. static void ptp_restore_ptp_volt(struct ptp_det *det);
  153. #define CONFIG_PTP_SHOWLOG 1
  154. #define EN_ISR_LOG (0)
  155. #define PTP_GET_REAL_VAL (1) /* get val from efuse */
  156. #define SET_PMIC_VOLT (1) /* apply PMIC voltage */
  157. #define DUMP_DATA_TO_DE (0)
  158. #define LOG_INTERVAL (2LL * NSEC_PER_SEC)
  159. #define NR_FREQ 8
  160. /*
  161. * 100 us, This is the PTP Detector sampling time as represented in
  162. * cycles of bclk_ck during INIT. 52 MHz
  163. */
  164. /* #define DETWINDOW_VAL 0xa28 */
  165. #define DETWINDOW_VAL 0x514
  166. #define PTP_VOLT_TO_PMIC_VAL(volt) (((volt) - 70000 + 625 - 1) / 625)
  167. #define PTP_PMIC_VAL_TO_VOLT(pmic) (((pmic) * 625) + 60000)
  168. /* offset 0x10(16 steps) for CPU/GPU DVFS */
  169. #define PTPOD_PMIC_OFFSET (0x10)
  170. #define VMAX_VAL PTP_VOLT_TO_PMIC_VAL(125000)
  171. #define VMIN_VAL PTP_VOLT_TO_PMIC_VAL(95000)
  172. #define DTHI_VAL 0x01
  173. #define DTLO_VAL 0xfe
  174. #define DETMAX_VAL 0xffff
  175. #define AGECONFIG_VAL 0x555555
  176. #define AGEM_VAL 0x0
  177. #define DVTFIXED_VAL 0x6
  178. #define VCO_VAL 0x28
  179. #define DCCONFIG_VAL 0x555555
  180. /*
  181. * bit operation
  182. */
  183. #undef BIT
  184. #define BIT(bit) (1U << (bit))
  185. #define MSB(range) (1 ? range)
  186. #define LSB(range) (0 ? range)
  187. /*
  188. * Genearte a mask wher MSB to LSB are all 0b1
  189. * @r: Range in the form of MSB:LSB
  190. */
  191. #define BITMASK(r) \
  192. (((unsigned) -1 >> (31 - MSB(r))) & ~((1U << LSB(r)) - 1))
  193. /*
  194. * Set value at MSB:LSB. For example, BITS(7:3, 0x5A)
  195. * will return a value where bit 3 to bit 7 is 0x5A
  196. * @r: Range in the form of MSB:LSB
  197. */
  198. /* BITS(MSB:LSB, value) => Set value at MSB:LSB */
  199. #define BITS(r, val) ((val << LSB(r)) & BITMASK(r))
  200. /*
  201. * LOG
  202. */
  203. #define ptp_emerg(fmt, args...) pr_err("[PTP] " fmt, ##args)
  204. #define ptp_alert(fmt, args...) pr_err("[PTP] " fmt, ##args)
  205. #define ptp_crit(fmt, args...) pr_err("[PTP] " fmt, ##args)
  206. #define ptp_error(fmt, args...) pr_err("[PTP] " fmt, ##args)
  207. #define ptp_warning(fmt, args...) pr_warn("[PTP] " fmt, ##args)
  208. #define ptp_notice(fmt, args...) pr_warn("[PTP] " fmt, ##args)
  209. #define ptp_info(fmt, args...) pr_warn("[PTP] " fmt, ##args)
  210. #define ptp_debug(fmt, args...) pr_warn("[PTP] " fmt, ##args)
  211. #if EN_ISR_LOG
  212. #define ptp_isr_info(fmt, args...) ptp_notice(fmt, ##args)
  213. #else
  214. #define ptp_isr_info(fmt, args...)
  215. #endif
  216. #define FUNC_LV_MODULE BIT(0) /* module, platform driver interface */
  217. #define FUNC_LV_CPUFREQ BIT(1) /* cpufreq driver interface */
  218. #define FUNC_LV_API BIT(2) /* mt_cpufreq driver global function */
  219. #define FUNC_LV_LOCAL BIT(3) /* mt_cpufreq driver lcaol function */
  220. #define FUNC_LV_HELP BIT(4) /* mt_cpufreq driver help function */
  221. static unsigned int func_lv_mask;
  222. #if defined(CONFIG_PTP_SHOWLOG)
  223. #define FUNC_ENTER(lv) do { if ((lv) & func_lv_mask) ptp_debug(">> %s()\n", __func__); } while (0)
  224. #define FUNC_EXIT(lv) do { if ((lv) & func_lv_mask) ptp_debug("<< %s():%d\n", __func__, __LINE__); } while (0)
  225. #else
  226. #define FUNC_ENTER(lv)
  227. #define FUNC_EXIT(lv)
  228. #endif /* CONFIG_CPU_DVFS_SHOWLOG */
  229. /*
  230. * REG ACCESS
  231. */
  232. #define ptp_read(addr) __raw_readl(addr)
  233. #define ptp_read_field(addr, range) \
  234. ((ptp_read(addr) & BITMASK(range)) >> LSB(range))
  235. #define ptp_write(addr, val) mt_reg_sync_writel(val, addr)
  236. /*
  237. * Write a field of a register.
  238. * @addr: Address of the register
  239. * @range: The field bit range in the form of MSB:LSB
  240. * @val: The value to be written to the field
  241. */
  242. #define ptp_write_field(addr, range, val) \
  243. ptp_write(addr, (ptp_read(addr) & ~BITMASK(range)) | BITS(range, val))
  244. /*
  245. * Helper macros
  246. */
  247. /* PTP detector is disabled by who */
  248. enum {
  249. BY_PROCFS = BIT(0),
  250. BY_INIT_ERROR = BIT(1),
  251. BY_MON_ERROR = BIT(2),
  252. };
  253. #ifdef CONFIG_OF
  254. void __iomem *ptpod_base;
  255. static u32 ptpod_irq_number;
  256. int ptpod_phy_base;
  257. #endif
  258. /*
  259. * iterate over list of detectors
  260. * @det: the detector * to use as a loop cursor.
  261. */
  262. #define for_each_det(det) for (det = ptp_detectors; det < (ptp_detectors + ARRAY_SIZE(ptp_detectors)); det++)
  263. /*
  264. * iterate over list of detectors and its controller
  265. * @det: the detector * to use as a loop cursor.
  266. * @ctrl: the ptp_ctrl * to use as ctrl pointer of current det.
  267. */
  268. #define for_each_det_ctrl(det, ctrl) \
  269. for (det = ptp_detectors, \
  270. ctrl = id_to_ptp_ctrl(det->ctrl_id); \
  271. det < (ptp_detectors + ARRAY_SIZE(ptp_detectors)); \
  272. det++, \
  273. ctrl = id_to_ptp_ctrl(det->ctrl_id))
  274. /*
  275. * iterate over list of controllers
  276. * @pos: the ptp_ctrl * to use as a loop cursor.
  277. */
  278. #define for_each_ctrl(ctrl) for (ctrl = ptp_ctrls; ctrl < (ptp_ctrls + ARRAY_SIZE(ptp_ctrls)); ctrl++)
  279. /*
  280. * Given a ptp_det * in ptp_detectors. Return the id.
  281. * @det: pointer to a ptp_det in ptp_detectors
  282. */
  283. #define det_to_id(det) ((det) - &ptp_detectors[0])
  284. /*
  285. * Given a ptp_ctrl * in ptp_ctrls. Return the id.
  286. * @det: pointer to a ptp_ctrl in ptp_ctrls
  287. */
  288. #define ctrl_to_id(ctrl) ((ctrl) - &ptp_ctrls[0])
  289. /*
  290. * Check if a detector has a feature
  291. * @det: pointer to a ptp_det to be check
  292. * @feature: enum ptp_features to be checked
  293. */
  294. #define HAS_FEATURE(det, feature) ((det)->features & feature)
  295. #define PERCENT(numerator, denominator) \
  296. (unsigned char)(((numerator) * 100 + (denominator) - 1) / (denominator))
  297. typedef enum {
  298. PTP_PHASE_INIT01 = 0,
  299. PTP_PHASE_INIT02,
  300. PTP_PHASE_MON,
  301. NR_PTP_PHASE,
  302. } ptp_phase;
  303. enum {
  304. PTP_VOLT_NONE = 0,
  305. PTP_VOLT_UPDATE = BIT(0),
  306. PTP_VOLT_RESTORE = BIT(1),
  307. };
  308. struct ptp_ctrl {
  309. const char *name;
  310. ptp_det_id det_id;
  311. /* struct completion init_done; */
  312. /* atomic_t in_init; */
  313. /* for voltage setting thread */
  314. wait_queue_head_t wq;
  315. int volt_update;
  316. struct task_struct *thread;
  317. };
  318. struct ptp_det_ops {
  319. /* interface to PTP-OD */
  320. void (*enable)(struct ptp_det *det, int reason);
  321. void (*disable)(struct ptp_det *det, int reason);
  322. void (*disable_locked)(struct ptp_det *det, int reason);
  323. void (*switch_bank)(struct ptp_det *det);
  324. int (*init01)(struct ptp_det *det);
  325. int (*init02)(struct ptp_det *det);
  326. int (*mon_mode)(struct ptp_det *det);
  327. int (*get_status)(struct ptp_det *det);
  328. void (*dump_status)(struct ptp_det *det);
  329. void (*set_phase)(struct ptp_det *det, ptp_phase phase);
  330. /* interface to thermal */
  331. int (*get_temp)(struct ptp_det *det);
  332. /* interface to DVFS */
  333. int (*get_volt)(struct ptp_det *det);
  334. int (*set_volt)(struct ptp_det *det);
  335. void (*restore_default_volt)(struct ptp_det *det);
  336. void (*get_freq_table)(struct ptp_det *det);
  337. };
  338. enum ptp_features {
  339. FEA_INIT01 = BIT(PTP_PHASE_INIT01),
  340. FEA_INIT02 = BIT(PTP_PHASE_INIT02),
  341. FEA_MON = BIT(PTP_PHASE_MON),
  342. };
  343. struct ptp_det {
  344. const char *name;
  345. struct ptp_det_ops *ops;
  346. int status;
  347. int features;
  348. ptp_ctrl_id ctrl_id;
  349. /* devinfo */
  350. unsigned int PTPINITEN;
  351. unsigned int PTPMONEN;
  352. unsigned int MDES;
  353. unsigned int BDES;
  354. unsigned int DCMDET;
  355. unsigned int DCBDET;
  356. unsigned int AGEDELTA;
  357. unsigned int MTDES;
  358. /* constant */
  359. unsigned int DETWINDOW;
  360. unsigned int VMAX;
  361. unsigned int VMIN;
  362. unsigned int DTHI;
  363. unsigned int DTLO;
  364. unsigned int VBOOT;
  365. unsigned int DETMAX;
  366. unsigned int AGECONFIG;
  367. unsigned int AGEM;
  368. unsigned int DVTFIXED;
  369. unsigned int VCO;
  370. unsigned int DCCONFIG;
  371. unsigned int DCVOFFSETIN;
  372. unsigned int AGEVOFFSETIN;
  373. /* for debug */
  374. unsigned int dcvalues[NR_PTP_PHASE];
  375. unsigned int ptp_freqpct30[NR_PTP_PHASE];
  376. unsigned int ptp_26c[NR_PTP_PHASE];
  377. unsigned int ptp_vop30[NR_PTP_PHASE];
  378. unsigned int ptp_ptpen[NR_PTP_PHASE];
  379. #if DUMP_DATA_TO_DE
  380. unsigned int reg_dump_data[ARRAY_SIZE(reg_dump_addr_off)][NR_PTP_PHASE];
  381. #endif
  382. /* slope */
  383. unsigned int MTS;
  384. unsigned int BTS;
  385. /* dvfs */
  386. unsigned int num_freq_tbl;
  387. unsigned int max_freq_khz;
  388. unsigned char freq_tbl[NR_FREQ];
  389. unsigned int volt_tbl[NR_FREQ];
  390. unsigned int volt_tbl_init2[NR_FREQ];
  391. unsigned int volt_tbl_pmic[NR_FREQ];
  392. unsigned int volt_tbl_bin[NR_FREQ];
  393. int volt_offset;
  394. int disabled;
  395. };
  396. struct ptp_devinfo {
  397. /* M_HW_RES0 10206180 */
  398. unsigned int CPU_BDES:8;
  399. unsigned int CPU_MDES:8;
  400. unsigned int CPU_DCBDET:8;
  401. unsigned int CPU_DCMDET:8;
  402. /* M_HW_RES1 10206184 */
  403. unsigned int GPU_MTDES:8;
  404. unsigned int GPU_AGEDELTA:8;
  405. unsigned int CPU_MTDES:8;
  406. unsigned int CPU_AGEDELTA:8;
  407. /* M_HW_RES2 10206188 */
  408. #ifdef CONFIG_ARCH_MT6735
  409. unsigned int SOC_VOLTBIN:2;
  410. unsigned int LTE_VOLTBIN:2;
  411. unsigned int GPU_BDES:4;
  412. #else
  413. #ifdef CONFIG_ARCH_MT6753
  414. unsigned int GPU_BDES:8;
  415. #else
  416. unsigned int SOC_VOLTBIN:2;
  417. unsigned int LTE_VOLTBIN:2;
  418. unsigned int SOC_VOLTBIN_550:2;
  419. unsigned int GPU_BDES:2;
  420. #endif
  421. #endif
  422. unsigned int GPU_MDES:8;
  423. unsigned int GPU_DCBDET:8;
  424. unsigned int GPU_DCMDET:8;
  425. /* M_HW_RES3 1020618C */
  426. unsigned int M_HW_RES3:32;
  427. /* M_HW_RES4 10206190 */
  428. #ifdef CONFIG_ARCH_MT6753
  429. unsigned int LTE_VOLTBIN:2;
  430. unsigned int LTE_BDES:6;
  431. #else
  432. unsigned int LTE_BDES:8;
  433. #endif
  434. unsigned int LTE_MDES:8;
  435. unsigned int LTE_DCBDET:8;
  436. unsigned int LTE_DCMDET:8;
  437. /* M_HW_RES5 10206194 */
  438. unsigned int PTPINITEN:1;
  439. unsigned int PTPMONEN:1;
  440. unsigned int Bodybias:1;
  441. unsigned int PTPOD_T:1;
  442. unsigned int EPS:1;
  443. unsigned int M_HW_RES5_OTHERS:11;
  444. unsigned int LTE_MTDES:8;
  445. unsigned int LTE_AGEDELTA:8;
  446. /* M_HW_RES6 10206270 */
  447. unsigned int LotID:32;
  448. /* M_HW_RES7 102061B0 */
  449. unsigned int WaferID:32;
  450. };
  451. /*
  452. *Local variable definition
  453. */
  454. static int ptp_probe(struct platform_device *pdev);
  455. static int ptp_suspend(struct platform_device *pdev, pm_message_t state);
  456. static int ptp_resume(struct platform_device *pdev);
  457. /*
  458. * lock
  459. */
  460. static DEFINE_SPINLOCK(ptp_spinlock);
  461. /*
  462. * PTP controllers
  463. */
  464. struct ptp_ctrl ptp_ctrls[NR_PTP_CTRL] = {
  465. [PTP_CTRL_CPU] = {
  466. .name = __stringify(PTP_CTRL_CPU),
  467. .det_id = PTP_DET_CPU,
  468. },
  469. };
  470. /*
  471. * PTP detectors
  472. */
  473. static void base_ops_enable(struct ptp_det *det, int reason);
  474. static void base_ops_disable(struct ptp_det *det, int reason);
  475. static void base_ops_disable_locked(struct ptp_det *det, int reason);
  476. static void base_ops_switch_bank(struct ptp_det *det);
  477. static int base_ops_init01(struct ptp_det *det);
  478. static int base_ops_init02(struct ptp_det *det);
  479. static int base_ops_mon_mode(struct ptp_det *det);
  480. static int base_ops_get_status(struct ptp_det *det);
  481. static void base_ops_dump_status(struct ptp_det *det);
  482. static void base_ops_set_phase(struct ptp_det *det, ptp_phase phase);
  483. static int base_ops_get_temp(struct ptp_det *det);
  484. static int base_ops_get_volt(struct ptp_det *det);
  485. static int base_ops_set_volt(struct ptp_det *det);
  486. static void base_ops_restore_default_volt(struct ptp_det *det);
  487. static void base_ops_get_freq_table(struct ptp_det *det);
  488. static int get_volt_cpu(struct ptp_det *det);
  489. static int set_volt_cpu(struct ptp_det *det);
  490. static void restore_default_volt_cpu(struct ptp_det *det);
  491. static void get_freq_table_cpu(struct ptp_det *det);
  492. #define BASE_OP(fn) .fn = base_ops_ ## fn
  493. static struct ptp_det_ops ptp_det_base_ops = {
  494. BASE_OP(enable),
  495. BASE_OP(disable),
  496. BASE_OP(disable_locked),
  497. BASE_OP(switch_bank),
  498. BASE_OP(init01),
  499. BASE_OP(init02),
  500. BASE_OP(mon_mode),
  501. BASE_OP(get_status),
  502. BASE_OP(dump_status),
  503. BASE_OP(set_phase),
  504. BASE_OP(get_temp),
  505. BASE_OP(get_volt),
  506. BASE_OP(set_volt),
  507. BASE_OP(restore_default_volt),
  508. BASE_OP(get_freq_table),
  509. };
  510. static struct ptp_det_ops cpu_det_ops = {
  511. .get_volt = get_volt_cpu,
  512. .set_volt = set_volt_cpu,
  513. .restore_default_volt = restore_default_volt_cpu,
  514. .get_freq_table = get_freq_table_cpu,
  515. };
  516. static struct ptp_det ptp_detectors[NR_PTP_DET] = {
  517. [PTP_DET_CPU] = {
  518. .name = __stringify(PTP_DET_CPU),
  519. .ops = &cpu_det_ops,
  520. .ctrl_id = PTP_CTRL_CPU,
  521. .features = FEA_INIT01 | FEA_INIT02 | FEA_MON,
  522. #ifdef CONFIG_ARCH_MT6735
  523. .max_freq_khz = 1300000,
  524. #else
  525. #ifdef CONFIG_ARCH_MT6753
  526. .max_freq_khz = 1495000,
  527. #else
  528. .max_freq_khz = 1000000,
  529. #endif
  530. #endif
  531. .VBOOT = PTP_VOLT_TO_PMIC_VAL(112500),
  532. },
  533. };
  534. static struct ptp_devinfo ptp_devinfo;
  535. static unsigned int ptp_level; /* debug info */
  536. unsigned int stress_result = 1; /* ATE stress */
  537. /*
  538. * timer for log
  539. */
  540. static struct hrtimer ptp_log_timer;
  541. static struct ptp_det *id_to_ptp_det(ptp_det_id id)
  542. {
  543. if (likely(id < NR_PTP_DET))
  544. return &ptp_detectors[id];
  545. else
  546. return NULL;
  547. }
  548. static struct ptp_ctrl *id_to_ptp_ctrl(ptp_ctrl_id id)
  549. {
  550. if (likely(id < NR_PTP_CTRL))
  551. return &ptp_ctrls[id];
  552. else
  553. return NULL;
  554. }
  555. static void base_ops_enable(struct ptp_det *det, int reason)
  556. {
  557. FUNC_ENTER(FUNC_LV_HELP);
  558. det->disabled &= ~reason;
  559. FUNC_EXIT(FUNC_LV_HELP);
  560. }
  561. static void base_ops_switch_bank(struct ptp_det *det)
  562. {
  563. FUNC_ENTER(FUNC_LV_HELP);
  564. ptp_write_field(PTP_PTPCORESEL, 2:0, det->ctrl_id);
  565. FUNC_EXIT(FUNC_LV_HELP);
  566. }
  567. static void base_ops_disable_locked(struct ptp_det *det, int reason)
  568. {
  569. FUNC_ENTER(FUNC_LV_HELP);
  570. /* disable PTP */
  571. ptp_write(PTP_PTPEN, 0x0);
  572. /* Clear PTP interrupt PTPINTSTS */
  573. ptp_write(PTP_PTPINTSTS, 0x00ffffff);
  574. switch (reason) {
  575. case BY_MON_ERROR:
  576. /* set init2 value to DVFS table (PMIC) */
  577. memcpy(det->volt_tbl, det->volt_tbl_init2, sizeof(det->volt_tbl_init2));
  578. ptp_set_ptp_volt(det);
  579. break;
  580. case BY_INIT_ERROR:
  581. case BY_PROCFS:
  582. default:
  583. /* restore default DVFS table (PMIC) */
  584. ptp_restore_ptp_volt(det);
  585. break;
  586. }
  587. ptp_notice("Disable PTP-OD[%s] done.\n", det->name);
  588. det->disabled |= reason;
  589. FUNC_EXIT(FUNC_LV_HELP);
  590. }
  591. static void base_ops_disable(struct ptp_det *det, int reason)
  592. {
  593. unsigned long flags;
  594. FUNC_ENTER(FUNC_LV_HELP);
  595. mt_ptp_lock(&flags);
  596. det->ops->switch_bank(det);
  597. det->ops->disable_locked(det, reason);
  598. mt_ptp_unlock(&flags);
  599. FUNC_EXIT(FUNC_LV_HELP);
  600. }
  601. static int base_ops_init01(struct ptp_det *det)
  602. {
  603. /* struct ptp_ctrl *ctrl = id_to_ptp_ctrl(det->ctrl_id); */
  604. FUNC_ENTER(FUNC_LV_HELP);
  605. if (unlikely(!HAS_FEATURE(det, FEA_INIT01))) {
  606. ptp_notice("det %s has no INIT01\n", det->name);
  607. FUNC_EXIT(FUNC_LV_HELP);
  608. return -1;
  609. }
  610. if (det->disabled & BY_PROCFS) {
  611. ptp_notice("[%s] Disabled by PROCFS\n", __func__);
  612. FUNC_EXIT(FUNC_LV_HELP);
  613. return -2;
  614. }
  615. ptp_notice("%s(%s) start (ptp_level = 0x%08X).\n", __func__, det->name, ptp_level);
  616. /* atomic_inc(&ctrl->in_init); */
  617. /* ptp_init01_prepare(det); */
  618. /* det->ops->dump_status(det); */
  619. det->ops->set_phase(det, PTP_PHASE_INIT01);
  620. FUNC_EXIT(FUNC_LV_HELP);
  621. return 0;
  622. }
  623. static int base_ops_init02(struct ptp_det *det)
  624. {
  625. FUNC_ENTER(FUNC_LV_HELP);
  626. if (unlikely(!HAS_FEATURE(det, FEA_INIT02))) {
  627. ptp_notice("det %s has no INIT02\n", det->name);
  628. FUNC_EXIT(FUNC_LV_HELP);
  629. return -1;
  630. }
  631. if (det->disabled & BY_PROCFS) {
  632. ptp_notice("[%s] Disabled by PROCFS\n", __func__);
  633. FUNC_EXIT(FUNC_LV_HELP);
  634. return -2;
  635. }
  636. /* ptp_notice("%s(%s) start (ptp_level = 0x%08X).\n", __func__, det->name, ptp_level);
  637. ptp_notice("DCVOFFSETIN = 0x%08X\n", det->DCVOFFSETIN);
  638. ptp_notice("AGEVOFFSETIN = 0x%08X\n", det->AGEVOFFSETIN); */
  639. /* det->ops->dump_status(det); */
  640. det->ops->set_phase(det, PTP_PHASE_INIT02);
  641. FUNC_EXIT(FUNC_LV_HELP);
  642. return 0;
  643. }
  644. static int base_ops_mon_mode(struct ptp_det *det)
  645. {
  646. struct TS_PTPOD ts_info;
  647. thermal_bank_name ts_bank;
  648. FUNC_ENTER(FUNC_LV_HELP);
  649. if (!HAS_FEATURE(det, FEA_MON)) {
  650. ptp_notice("det %s has no MON mode\n", det->name);
  651. FUNC_EXIT(FUNC_LV_HELP);
  652. return -1;
  653. }
  654. if (det->disabled & BY_PROCFS) {
  655. ptp_notice("[%s] Disabled by PROCFS\n", __func__);
  656. FUNC_EXIT(FUNC_LV_HELP);
  657. return -2;
  658. }
  659. /* ptp_notice("%s(%s) start (ptp_level = 0x%08X).\n", __func__, det->name, ptp_level); */
  660. ts_bank = det->ctrl_id;
  661. get_thermal_slope_intercept(&ts_info, ts_bank);
  662. det->MTS = ts_info.ts_MTS;
  663. det->BTS = ts_info.ts_BTS;
  664. if ((det->PTPINITEN == 0x0) || (det->PTPMONEN == 0x0)) {
  665. ptp_notice("PTPINITEN = 0x%08X, PTPMONEN = 0x%08X\n", det->PTPINITEN,
  666. det->PTPMONEN);
  667. FUNC_EXIT(FUNC_LV_HELP);
  668. return 1;
  669. }
  670. det->ops->set_phase(det, PTP_PHASE_MON);
  671. FUNC_EXIT(FUNC_LV_HELP);
  672. return 0;
  673. }
  674. static int base_ops_get_status(struct ptp_det *det)
  675. {
  676. int status;
  677. unsigned long flags;
  678. FUNC_ENTER(FUNC_LV_HELP);
  679. mt_ptp_lock(&flags);
  680. det->ops->switch_bank(det);
  681. status = (ptp_read(PTP_PTPEN) != 0) ? 1 : 0;
  682. mt_ptp_unlock(&flags);
  683. FUNC_EXIT(FUNC_LV_HELP);
  684. return status;
  685. }
  686. static void base_ops_dump_status(struct ptp_det *det)
  687. {
  688. int i;
  689. FUNC_ENTER(FUNC_LV_HELP);
  690. ptp_isr_info("[%s]\n", det->name);
  691. ptp_isr_info("PTPINITEN = 0x%08X\n", det->PTPINITEN);
  692. ptp_isr_info("PTPMONEN = 0x%08X\n", det->PTPMONEN);
  693. ptp_isr_info("MDES = 0x%08X\n", det->MDES);
  694. ptp_isr_info("BDES = 0x%08X\n", det->BDES);
  695. ptp_isr_info("DCMDET = 0x%08X\n", det->DCMDET);
  696. ptp_isr_info("DCCONFIG = 0x%08X\n", det->DCCONFIG);
  697. ptp_isr_info("DCBDET = 0x%08X\n", det->DCBDET);
  698. ptp_isr_info("AGECONFIG = 0x%08X\n", det->AGECONFIG);
  699. ptp_isr_info("AGEM = 0x%08X\n", det->AGEM);
  700. ptp_isr_info("AGEDELTA = 0x%08X\n", det->AGEDELTA);
  701. ptp_isr_info("DVTFIXED = 0x%08X\n", det->DVTFIXED);
  702. ptp_isr_info("MTDES = 0x%08X\n", det->MTDES);
  703. ptp_isr_info("VCO = 0x%08X\n", det->VCO);
  704. ptp_isr_info("DETWINDOW = 0x%08X\n", det->DETWINDOW);
  705. ptp_isr_info("VMAX = 0x%08X\n", det->VMAX);
  706. ptp_isr_info("VMIN = 0x%08X\n", det->VMIN);
  707. ptp_isr_info("DTHI = 0x%08X\n", det->DTHI);
  708. ptp_isr_info("DTLO = 0x%08X\n", det->DTLO);
  709. ptp_isr_info("VBOOT = 0x%08X\n", det->VBOOT);
  710. ptp_isr_info("DETMAX = 0x%08X\n", det->DETMAX);
  711. ptp_isr_info("DCVOFFSETIN = 0x%08X\n", det->DCVOFFSETIN);
  712. ptp_isr_info("AGEVOFFSETIN = 0x%08X\n", det->AGEVOFFSETIN);
  713. ptp_isr_info("MTS = 0x%08X\n", det->MTS);
  714. ptp_isr_info("BTS = 0x%08X\n", det->BTS);
  715. ptp_isr_info("num_freq_tbl = %d\n", det->num_freq_tbl);
  716. for (i = 0; i < det->num_freq_tbl; i++)
  717. ptp_isr_info("freq_tbl[%d] = %d\n", i, det->freq_tbl[i]);
  718. for (i = 0; i < det->num_freq_tbl; i++)
  719. ptp_isr_info("volt_tbl[%d] = %d\n", i, det->volt_tbl[i]);
  720. for (i = 0; i < det->num_freq_tbl; i++)
  721. ptp_isr_info("volt_tbl_init2[%d] = %d\n", i, det->volt_tbl_init2[i]);
  722. for (i = 0; i < det->num_freq_tbl; i++)
  723. ptp_isr_info("volt_tbl_pmic[%d] = %d\n", i, det->volt_tbl_pmic[i]);
  724. FUNC_EXIT(FUNC_LV_HELP);
  725. }
  726. static void base_ops_set_phase(struct ptp_det *det, ptp_phase phase)
  727. {
  728. unsigned int i, filter, val;
  729. FUNC_ENTER(FUNC_LV_HELP);
  730. det->ops->switch_bank(det);
  731. /* config PTP register */
  732. ptp_write(PTP_DESCHAR, ((det->BDES << 8) & 0xff00) | (det->MDES & 0xff));
  733. ptp_write(PTP_TEMPCHAR,
  734. (((det->VCO << 16) & 0xff0000) |
  735. ((det->MTDES << 8) & 0xff00) | (det->DVTFIXED & 0xff)));
  736. ptp_write(PTP_DETCHAR, ((det->DCBDET << 8) & 0xff00) | (det->DCMDET & 0xff));
  737. ptp_write(PTP_AGECHAR, ((det->AGEDELTA << 8) & 0xff00) | (det->AGEM & 0xff));
  738. ptp_write(PTP_DCCONFIG, det->DCCONFIG);
  739. ptp_write(PTP_AGECONFIG, det->AGECONFIG);
  740. if (PTP_PHASE_MON == phase)
  741. ptp_write(PTP_TSCALCS, ((det->BTS << 12) & 0xfff000) | (det->MTS & 0xfff));
  742. if (det->AGEM == 0x0)
  743. ptp_write(PTP_RUNCONFIG, 0x80000000);
  744. else {
  745. val = 0x0;
  746. for (i = 0; i < 24; i += 2) {
  747. filter = 0x3 << i;
  748. if (((det->AGECONFIG) & filter) == 0x0)
  749. val |= (0x1 << i);
  750. else
  751. val |= ((det->AGECONFIG) & filter);
  752. }
  753. ptp_write(PTP_RUNCONFIG, val);
  754. }
  755. ptp_write(PTP_FREQPCT30,
  756. ((det->freq_tbl[3] << 24) & 0xff000000) |
  757. ((det->freq_tbl[2] << 16) & 0xff0000) |
  758. ((det->freq_tbl[1] << 8) & 0xff00) | (det->freq_tbl[0] & 0xff));
  759. ptp_write(PTP_FREQPCT74,
  760. ((det->freq_tbl[7] << 24) & 0xff000000) |
  761. ((det->freq_tbl[6] << 16) & 0xff0000) |
  762. ((det->freq_tbl[5] << 8) & 0xff00) | ((det->freq_tbl[4]) & 0xff));
  763. ptp_write(PTP_LIMITVALS,
  764. ((det->VMAX << 24) & 0xff000000) |
  765. ((det->VMIN << 16) & 0xff0000) |
  766. ((det->DTHI << 8) & 0xff00) | (det->DTLO & 0xff));
  767. ptp_write(PTP_VBOOT, (((det->VBOOT) & 0xff)));
  768. ptp_write(PTP_DETWINDOW, (((det->DETWINDOW) & 0xffff)));
  769. ptp_write(PTP_PTPCONFIG, (((det->DETMAX) & 0xffff)));
  770. /* clear all pending PTP interrupt & config PTPINTEN */
  771. ptp_write(PTP_PTPINTSTS, 0xffffffff);
  772. switch (phase) {
  773. case PTP_PHASE_INIT01:
  774. ptp_write(PTP_PTPINTEN, 0x00005f01);
  775. /* enable PTP INIT measurement */
  776. ptp_write(PTP_PTPEN, 0x00000001);
  777. break;
  778. case PTP_PHASE_INIT02:
  779. ptp_write(PTP_PTPINTEN, 0x00005f01);
  780. ptp_write(PTP_INIT2VALS,
  781. ((det->AGEVOFFSETIN << 16) & 0xffff0000) | (det->DCVOFFSETIN & 0xffff));
  782. /* enable PTP INIT measurement */
  783. ptp_write(PTP_PTPEN, 0x00000005);
  784. break;
  785. case PTP_PHASE_MON:
  786. ptp_write(PTP_PTPINTEN, 0x00FF0000);
  787. /* enable PTP monitor mode */
  788. ptp_write(PTP_PTPEN, 0x00000002);
  789. break;
  790. default:
  791. BUG();
  792. break;
  793. }
  794. FUNC_EXIT(FUNC_LV_HELP);
  795. }
  796. static int base_ops_get_temp(struct ptp_det *det)
  797. {
  798. thermal_bank_name ts_bank;
  799. FUNC_ENTER(FUNC_LV_HELP);
  800. ts_bank = THERMAL_BANK0;
  801. FUNC_EXIT(FUNC_LV_HELP);
  802. return tscpu_get_temp_by_bank(ts_bank);
  803. }
  804. static int base_ops_get_volt(struct ptp_det *det)
  805. {
  806. FUNC_ENTER(FUNC_LV_HELP);
  807. ptp_warning("[%s] default func\n", __func__);
  808. FUNC_EXIT(FUNC_LV_HELP);
  809. return 0;
  810. }
  811. static int base_ops_set_volt(struct ptp_det *det)
  812. {
  813. FUNC_ENTER(FUNC_LV_HELP);
  814. ptp_warning("[%s] default func\n", __func__);
  815. FUNC_EXIT(FUNC_LV_HELP);
  816. return 0;
  817. }
  818. static void base_ops_restore_default_volt(struct ptp_det *det)
  819. {
  820. FUNC_ENTER(FUNC_LV_HELP);
  821. ptp_warning("[%s] default func\n", __func__);
  822. FUNC_EXIT(FUNC_LV_HELP);
  823. }
  824. static void base_ops_get_freq_table(struct ptp_det *det)
  825. {
  826. FUNC_ENTER(FUNC_LV_HELP);
  827. det->freq_tbl[0] = 100;
  828. det->num_freq_tbl = 1;
  829. FUNC_EXIT(FUNC_LV_HELP);
  830. }
  831. /* Will return 10uV */
  832. static int get_volt_cpu(struct ptp_det *det)
  833. {
  834. FUNC_ENTER(FUNC_LV_HELP);
  835. return mt_cpufreq_get_cur_volt(MT_CPU_DVFS_LITTLE); /* unit mv * 100 = 10uv */
  836. FUNC_EXIT(FUNC_LV_HELP);
  837. }
  838. /* volt_tbl_pmic is convert from 10uV */
  839. static int set_volt_cpu(struct ptp_det *det)
  840. {
  841. FUNC_ENTER(FUNC_LV_HELP);
  842. FUNC_EXIT(FUNC_LV_HELP);
  843. return mt_cpufreq_update_volt(MT_CPU_DVFS_LITTLE, det->volt_tbl_pmic, det->num_freq_tbl);
  844. }
  845. static void restore_default_volt_cpu(struct ptp_det *det)
  846. {
  847. FUNC_ENTER(FUNC_LV_HELP);
  848. mt_cpufreq_restore_default_volt(MT_CPU_DVFS_LITTLE);
  849. FUNC_EXIT(FUNC_LV_HELP);
  850. }
  851. static void get_freq_table_cpu(struct ptp_det *det)
  852. {
  853. int i;
  854. enum mt_cpu_dvfs_id cpu;
  855. FUNC_ENTER(FUNC_LV_HELP);
  856. cpu = MT_CPU_DVFS_LITTLE;
  857. /* det->max_freq_khz = mt_cpufreq_get_freq_by_idx(cpu, 0); */
  858. for (i = 0; i < NR_FREQ; i++) {
  859. det->freq_tbl[i] = PERCENT(mt_cpufreq_get_freq_by_idx(cpu, i), det->max_freq_khz);
  860. if (0 == det->freq_tbl[i])
  861. break;
  862. }
  863. det->num_freq_tbl = i;
  864. FUNC_EXIT(FUNC_LV_HELP);
  865. }
  866. void mt_ptp_lock(unsigned long *flags)
  867. {
  868. spin_lock_irqsave(&ptp_spinlock, *flags);
  869. }
  870. EXPORT_SYMBOL(mt_ptp_lock);
  871. void mt_ptp_unlock(unsigned long *flags)
  872. {
  873. spin_unlock_irqrestore(&ptp_spinlock, *flags);
  874. }
  875. EXPORT_SYMBOL(mt_ptp_unlock);
  876. /*
  877. * timer for log
  878. */
  879. static enum hrtimer_restart ptp_log_timer_func(struct hrtimer *timer)
  880. {
  881. struct ptp_det *det;
  882. FUNC_ENTER(FUNC_LV_HELP);
  883. for_each_det(det) {
  884. ptp_notice(
  885. "PTP_LOG: PTPOD [%s](%d) -(%d, %d, %d, %d, %d, %d, %d, %d)-(%d, %d, %d, %d, %d, %d, %d, %d)\n",
  886. det->name, det->ops->get_temp(det),
  887. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[0]),
  888. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[1]),
  889. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[2]),
  890. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[3]),
  891. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[4]),
  892. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[5]),
  893. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[6]),
  894. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[7]),
  895. det->freq_tbl[0],
  896. det->freq_tbl[1],
  897. det->freq_tbl[2],
  898. det->freq_tbl[3],
  899. det->freq_tbl[4], det->freq_tbl[5], det->freq_tbl[6], det->freq_tbl[7]);
  900. }
  901. hrtimer_forward_now(timer, ns_to_ktime(LOG_INTERVAL));
  902. FUNC_EXIT(FUNC_LV_HELP);
  903. return HRTIMER_RESTART;
  904. }
  905. /*
  906. * Thread for voltage setting
  907. */
  908. static int ptp_volt_thread_handler(void *data)
  909. {
  910. struct ptp_ctrl *ctrl = (struct ptp_ctrl *)data;
  911. struct ptp_det *det = id_to_ptp_det(ctrl->det_id);
  912. FUNC_ENTER(FUNC_LV_HELP);
  913. do {
  914. wait_event_interruptible(ctrl->wq, ctrl->volt_update);
  915. if ((ctrl->volt_update & PTP_VOLT_UPDATE) && det->ops->set_volt)
  916. det->ops->set_volt(det);
  917. if ((ctrl->volt_update & PTP_VOLT_RESTORE) && det->ops->restore_default_volt)
  918. det->ops->restore_default_volt(det);
  919. ctrl->volt_update = PTP_VOLT_NONE;
  920. } while (!kthread_should_stop());
  921. FUNC_EXIT(FUNC_LV_HELP);
  922. return 0;
  923. }
  924. static void inherit_base_det(struct ptp_det *det)
  925. {
  926. FUNC_ENTER(FUNC_LV_HELP);
  927. #define INIT_OP(ops, func) \
  928. do { \
  929. if (ops->func == NULL) \
  930. ops->func = ptp_det_base_ops.func; \
  931. } while (0)
  932. INIT_OP(det->ops, disable);
  933. INIT_OP(det->ops, disable_locked);
  934. INIT_OP(det->ops, switch_bank);
  935. INIT_OP(det->ops, init01);
  936. INIT_OP(det->ops, init02);
  937. INIT_OP(det->ops, mon_mode);
  938. INIT_OP(det->ops, get_status);
  939. INIT_OP(det->ops, dump_status);
  940. INIT_OP(det->ops, set_phase);
  941. INIT_OP(det->ops, get_temp);
  942. INIT_OP(det->ops, get_volt);
  943. INIT_OP(det->ops, set_volt);
  944. INIT_OP(det->ops, restore_default_volt);
  945. INIT_OP(det->ops, get_freq_table);
  946. FUNC_EXIT(FUNC_LV_HELP);
  947. }
  948. static void ptp_init_ctrl(struct ptp_ctrl *ctrl)
  949. {
  950. FUNC_ENTER(FUNC_LV_HELP);
  951. init_waitqueue_head(&ctrl->wq);
  952. ctrl->thread = kthread_run(ptp_volt_thread_handler, ctrl, ctrl->name);
  953. if (IS_ERR(ctrl->thread))
  954. ptp_error("Create %s thread failed: %ld\n", ctrl->name,
  955. PTR_ERR(ctrl->thread));
  956. FUNC_EXIT(FUNC_LV_HELP);
  957. }
  958. #define _BIT_(_bit_) (unsigned)(1 << (_bit_))
  959. #define _BITS_(_bits_, _val_) ((((unsigned) -1 >> (31 - ((1) ? _bits_))) & ~((1U << ((0) ? _bits_)) - 1)) & ((_val_)<<((0) ? _bits_)))
  960. #define _BITMASK_(_bits_) (((unsigned) -1 >> (31 - ((1) ? _bits_))) & ~((1U << ((0) ? _bits_)) - 1))
  961. #define _GET_BITS_VAL_(_bits_, _val_) (((_val_) & (_BITMASK_(_bits_))) >> ((0) ? _bits_))
  962. static void ptp_init_det(struct ptp_det *det, struct ptp_devinfo *devinfo)
  963. {
  964. unsigned int segment_code = _GET_BITS_VAL_(31 : 25, get_devinfo_with_index(47));
  965. unsigned int down_grade_bit = _GET_BITS_VAL_(20 : 20, get_devinfo_with_index(24));
  966. ptp_det_id det_id = det_to_id(det);
  967. FUNC_ENTER(FUNC_LV_HELP);
  968. ptp_notice("det name=%s,det_id=%d\n", det->name, det_id);
  969. inherit_base_det(det);
  970. /* init with devinfo */
  971. det->PTPINITEN = devinfo->PTPINITEN;
  972. det->PTPMONEN = devinfo->PTPMONEN;
  973. /* init with constant */
  974. det->DETWINDOW = DETWINDOW_VAL;
  975. det->VMAX = VMAX_VAL;
  976. det->VMIN = VMIN_VAL;
  977. det->DTHI = DTHI_VAL;
  978. det->DTLO = DTLO_VAL;
  979. det->DETMAX = DETMAX_VAL;
  980. det->AGECONFIG = AGECONFIG_VAL;
  981. det->AGEM = AGEM_VAL;
  982. det->DVTFIXED = DVTFIXED_VAL;
  983. det->VCO = VCO_VAL;
  984. det->DCCONFIG = DCCONFIG_VAL;
  985. if (NULL != det->ops->get_volt) {
  986. det->VBOOT = PTP_VOLT_TO_PMIC_VAL(det->ops->get_volt(det));
  987. ptp_alert("@%s(), det->VBOOT = %d\n", __func__, det->VBOOT);
  988. }
  989. switch (det_id) {
  990. case PTP_DET_CPU:
  991. det->MDES = devinfo->CPU_MDES;
  992. det->BDES = devinfo->CPU_BDES;
  993. det->DCMDET = devinfo->CPU_DCMDET;
  994. det->DCBDET = devinfo->CPU_DCBDET;
  995. switch (segment_code) {
  996. case 0x4A:
  997. case 0x4B:
  998. case 0x52:
  999. case 0x53:
  1000. if (down_grade_bit)
  1001. det->volt_offset = 0x2;
  1002. det->DVTFIXED = 0x8;
  1003. break;
  1004. default:
  1005. det->DVTFIXED = 0x6;
  1006. break;
  1007. }
  1008. break;
  1009. default:
  1010. ptp_error("[%s]: Unknown det_id %d\n", __func__, det_id);
  1011. break;
  1012. }
  1013. switch (det->ctrl_id) {
  1014. case PTP_CTRL_CPU:
  1015. det->AGEDELTA = devinfo->CPU_AGEDELTA;
  1016. det->MTDES = devinfo->CPU_MTDES;
  1017. break;
  1018. default:
  1019. ptp_error("[%s]: Unknown ctrl_id %d\n", __func__, det->ctrl_id);
  1020. break;
  1021. }
  1022. /* get DVFS frequency table */
  1023. det->ops->get_freq_table(det);
  1024. FUNC_EXIT(FUNC_LV_HELP);
  1025. }
  1026. int __attribute__((weak)) tscpu_is_temp_valid(void)
  1027. {
  1028. return 1;
  1029. }
  1030. void __attribute__((weak)) pmic_force_vproc_pwm(unsigned int en)
  1031. {
  1032. }
  1033. static void ptp_set_ptp_volt(struct ptp_det *det)
  1034. {
  1035. #if SET_PMIC_VOLT
  1036. int i, cur_temp, low_temp_offset;
  1037. struct ptp_ctrl *ctrl = id_to_ptp_ctrl(det->ctrl_id);
  1038. int tscpu_bank0_temp_is_valid = tscpu_is_temp_valid();
  1039. cur_temp = det->ops->get_temp(det);
  1040. ptp_debug("ptp_set_ptp_volt(): cur_temp = %d, valid = %d\n", cur_temp, tscpu_bank0_temp_is_valid);
  1041. if (!tscpu_bank0_temp_is_valid || cur_temp <= 33000) {
  1042. low_temp_offset = 10;
  1043. ctrl->volt_update |= PTP_VOLT_UPDATE;
  1044. } else {
  1045. low_temp_offset = 0;
  1046. ctrl->volt_update |= PTP_VOLT_UPDATE;
  1047. }
  1048. for (i = 0; i < det->num_freq_tbl; i++)
  1049. det->volt_tbl_pmic[i] =
  1050. clamp(det->volt_tbl[i] + det->volt_offset + low_temp_offset,
  1051. det->VMIN + PTPOD_PMIC_OFFSET, det->VMAX + PTPOD_PMIC_OFFSET);
  1052. wake_up_interruptible(&ctrl->wq);
  1053. #endif
  1054. FUNC_ENTER(FUNC_LV_HELP);
  1055. FUNC_EXIT(FUNC_LV_HELP);
  1056. }
  1057. static void ptp_restore_ptp_volt(struct ptp_det *det)
  1058. {
  1059. #if SET_PMIC_VOLT
  1060. struct ptp_ctrl *ctrl = id_to_ptp_ctrl(det->ctrl_id);
  1061. ctrl->volt_update |= PTP_VOLT_RESTORE;
  1062. wake_up_interruptible(&ctrl->wq);
  1063. #endif
  1064. FUNC_ENTER(FUNC_LV_HELP);
  1065. FUNC_EXIT(FUNC_LV_HELP);
  1066. }
  1067. static void mt_ptp_reg_dump(void)
  1068. {
  1069. struct ptp_det *det;
  1070. unsigned long flags;
  1071. FUNC_ENTER(FUNC_LV_HELP);
  1072. ptp_isr_info("PTP_REVISIONID = 0x%08X\n", ptp_read(PTP_REVISIONID));
  1073. ptp_isr_info("PTP_TEMPMONCTL0 = 0x%08X\n", ptp_read(PTP_TEMPMONCTL0));
  1074. ptp_isr_info("PTP_TEMPMONCTL1 = 0x%08X\n", ptp_read(PTP_TEMPMONCTL1));
  1075. ptp_isr_info("PTP_TEMPMONCTL2 = 0x%08X\n", ptp_read(PTP_TEMPMONCTL2));
  1076. ptp_isr_info("PTP_TEMPMONINT = 0x%08X\n", ptp_read(PTP_TEMPMONINT));
  1077. ptp_isr_info("PTP_TEMPMONINTSTS = 0x%08X\n", ptp_read(PTP_TEMPMONINTSTS));
  1078. ptp_isr_info("PTP_TEMPMONIDET0 = 0x%08X\n", ptp_read(PTP_TEMPMONIDET0));
  1079. ptp_isr_info("PTP_TEMPMONIDET1 = 0x%08X\n", ptp_read(PTP_TEMPMONIDET1));
  1080. ptp_isr_info("PTP_TEMPMONIDET2 = 0x%08X\n", ptp_read(PTP_TEMPMONIDET2));
  1081. ptp_isr_info("PTP_TEMPH2NTHRE = 0x%08X\n", ptp_read(PTP_TEMPH2NTHRE));
  1082. ptp_isr_info("PTP_TEMPHTHRE = 0x%08X\n", ptp_read(PTP_TEMPHTHRE));
  1083. ptp_isr_info("PTP_TEMPCTHRE = 0x%08X\n", ptp_read(PTP_TEMPCTHRE));
  1084. ptp_isr_info("PTP_TEMPOFFSETH = 0x%08X\n", ptp_read(PTP_TEMPOFFSETH));
  1085. ptp_isr_info("PTP_TEMPOFFSETL = 0x%08X\n", ptp_read(PTP_TEMPOFFSETL));
  1086. ptp_isr_info("PTP_TEMPMSRCTL0 = 0x%08X\n", ptp_read(PTP_TEMPMSRCTL0));
  1087. ptp_isr_info("PTP_TEMPMSRCTL1 = 0x%08X\n", ptp_read(PTP_TEMPMSRCTL1));
  1088. ptp_isr_info("PTP_TEMPAHBPOLL = 0x%08X\n", ptp_read(PTP_TEMPAHBPOLL));
  1089. ptp_isr_info("PTP_TEMPAHBTO = 0x%08X\n", ptp_read(PTP_TEMPAHBTO));
  1090. ptp_isr_info("PTP_TEMPADCPNP0 = 0x%08X\n", ptp_read(PTP_TEMPADCPNP0));
  1091. ptp_isr_info("PTP_TEMPADCPNP1 = 0x%08X\n", ptp_read(PTP_TEMPADCPNP1));
  1092. ptp_isr_info("PTP_TEMPADCPNP2 = 0x%08X\n", ptp_read(PTP_TEMPADCPNP2));
  1093. ptp_isr_info("PTP_TEMPADCMUX = 0x%08X\n", ptp_read(PTP_TEMPADCMUX));
  1094. ptp_isr_info("PTP_TEMPADCEXT = 0x%08X\n", ptp_read(PTP_TEMPADCEXT));
  1095. ptp_isr_info("PTP_TEMPADCEXT1 = 0x%08X\n", ptp_read(PTP_TEMPADCEXT1));
  1096. ptp_isr_info("PTP_TEMPADCEN = 0x%08X\n", ptp_read(PTP_TEMPADCEN));
  1097. ptp_isr_info("PTP_TEMPPNPMUXADDR = 0x%08X\n", ptp_read(PTP_TEMPPNPMUXADDR));
  1098. ptp_isr_info("PTP_TEMPADCMUXADDR = 0x%08X\n", ptp_read(PTP_TEMPADCMUXADDR));
  1099. ptp_isr_info("PTP_TEMPADCEXTADDR = 0x%08X\n", ptp_read(PTP_TEMPADCEXTADDR));
  1100. ptp_isr_info("PTP_TEMPADCEXT1ADDR = 0x%08X\n", ptp_read(PTP_TEMPADCEXT1ADDR));
  1101. ptp_isr_info("PTP_TEMPADCENADDR = 0x%08X\n", ptp_read(PTP_TEMPADCENADDR));
  1102. ptp_isr_info("PTP_TEMPADCVALIDADDR = 0x%08X\n", ptp_read(PTP_TEMPADCVALIDADDR));
  1103. ptp_isr_info("PTP_TEMPADCVOLTADDR = 0x%08X\n", ptp_read(PTP_TEMPADCVOLTADDR));
  1104. ptp_isr_info("PTP_TEMPRDCTRL = 0x%08X\n", ptp_read(PTP_TEMPRDCTRL));
  1105. ptp_isr_info("PTP_TEMPADCVALIDMASK = 0x%08X\n", ptp_read(PTP_TEMPADCVALIDMASK));
  1106. ptp_isr_info("PTP_TEMPADCVOLTAGESHIFT = 0x%08X\n", ptp_read(PTP_TEMPADCVOLTAGESHIFT));
  1107. ptp_isr_info("PTP_TEMPADCWRITECTRL = 0x%08X\n", ptp_read(PTP_TEMPADCWRITECTRL));
  1108. ptp_isr_info("PTP_TEMPMSR0 = 0x%08X\n", ptp_read(PTP_TEMPMSR0));
  1109. ptp_isr_info("PTP_TEMPMSR1 = 0x%08X\n", ptp_read(PTP_TEMPMSR1));
  1110. ptp_isr_info("PTP_TEMPMSR2 = 0x%08X\n", ptp_read(PTP_TEMPMSR2));
  1111. ptp_isr_info("PTP_TEMPIMMD0 = 0x%08X\n", ptp_read(PTP_TEMPIMMD0));
  1112. ptp_isr_info("PTP_TEMPIMMD1 = 0x%08X\n", ptp_read(PTP_TEMPIMMD1));
  1113. ptp_isr_info("PTP_TEMPIMMD2 = 0x%08X\n", ptp_read(PTP_TEMPIMMD2));
  1114. ptp_isr_info("PTP_TEMPMONIDET3 = 0x%08X\n", ptp_read(PTP_TEMPMONIDET3));
  1115. ptp_isr_info("PTP_TEMPADCPNP3 = 0x%08X\n", ptp_read(PTP_TEMPADCPNP3));
  1116. ptp_isr_info("PTP_TEMPMSR3 = 0x%08X\n", ptp_read(PTP_TEMPMSR3));
  1117. ptp_isr_info("PTP_TEMPIMMD3 = 0x%08X\n", ptp_read(PTP_TEMPIMMD3));
  1118. ptp_isr_info("PTP_TEMPPROTCTL = 0x%08X\n", ptp_read(PTP_TEMPPROTCTL));
  1119. ptp_isr_info("PTP_TEMPPROTTA = 0x%08X\n", ptp_read(PTP_TEMPPROTTA));
  1120. ptp_isr_info("PTP_TEMPPROTTB = 0x%08X\n", ptp_read(PTP_TEMPPROTTB));
  1121. ptp_isr_info("PTP_TEMPPROTTC = 0x%08X\n", ptp_read(PTP_TEMPPROTTC));
  1122. ptp_isr_info("PTP_TEMPSPARE0 = 0x%08X\n", ptp_read(PTP_TEMPSPARE0));
  1123. ptp_isr_info("PTP_TEMPSPARE1 = 0x%08X\n", ptp_read(PTP_TEMPSPARE1));
  1124. ptp_isr_info("PTP_TEMPSPARE2 = 0x%08X\n", ptp_read(PTP_TEMPSPARE2));
  1125. ptp_isr_info("PTP_TEMPSPARE3 = 0x%08X\n", ptp_read(PTP_TEMPSPARE3));
  1126. for_each_det(det) {
  1127. mt_ptp_lock(&flags);
  1128. det->ops->switch_bank(det);
  1129. ptp_isr_info("PTP_DESCHAR[%s] = 0x%08X\n", det->name, ptp_read(PTP_DESCHAR));
  1130. ptp_isr_info("PTP_TEMPCHAR[%s] = 0x%08X\n", det->name, ptp_read(PTP_TEMPCHAR));
  1131. ptp_isr_info("PTP_DETCHAR[%s] = 0x%08X\n", det->name, ptp_read(PTP_DETCHAR));
  1132. ptp_isr_info("PTP_AGECHAR[%s] = 0x%08X\n", det->name, ptp_read(PTP_AGECHAR));
  1133. ptp_isr_info("PTP_DCCONFIG[%s] = 0x%08X\n", det->name, ptp_read(PTP_DCCONFIG));
  1134. ptp_isr_info("PTP_AGECONFIG[%s] = 0x%08X\n", det->name, ptp_read(PTP_AGECONFIG));
  1135. ptp_isr_info("PTP_FREQPCT30[%s] = 0x%08X\n", det->name, ptp_read(PTP_FREQPCT30));
  1136. ptp_isr_info("PTP_FREQPCT74[%s] = 0x%08X\n", det->name, ptp_read(PTP_FREQPCT74));
  1137. ptp_isr_info("PTP_LIMITVALS[%s] = 0x%08X\n", det->name, ptp_read(PTP_LIMITVALS));
  1138. ptp_isr_info("PTP_VBOOT[%s] = 0x%08X\n", det->name, ptp_read(PTP_VBOOT));
  1139. ptp_isr_info("PTP_DETWINDOW[%s] = 0x%08X\n", det->name, ptp_read(PTP_DETWINDOW));
  1140. ptp_isr_info("PTP_PTPCONFIG[%s] = 0x%08X\n", det->name, ptp_read(PTP_PTPCONFIG));
  1141. ptp_isr_info("PTP_TSCALCS[%s] = 0x%08X\n", det->name, ptp_read(PTP_TSCALCS));
  1142. ptp_isr_info("PTP_RUNCONFIG[%s] = 0x%08X\n", det->name, ptp_read(PTP_RUNCONFIG));
  1143. ptp_isr_info("PTP_PTPEN[%s] = 0x%08X\n", det->name, ptp_read(PTP_PTPEN));
  1144. ptp_isr_info("PTP_INIT2VALS[%s] = 0x%08X\n", det->name, ptp_read(PTP_INIT2VALS));
  1145. ptp_isr_info("PTP_DCVALUES[%s] = 0x%08X\n", det->name, ptp_read(PTP_DCVALUES));
  1146. ptp_isr_info("PTP_AGEVALUES[%s] = 0x%08X\n", det->name, ptp_read(PTP_AGEVALUES));
  1147. ptp_isr_info("PTP_VOP30[%s] = 0x%08X\n", det->name, ptp_read(PTP_VOP30));
  1148. ptp_isr_info("PTP_VOP74[%s] = 0x%08X\n", det->name, ptp_read(PTP_VOP74));
  1149. ptp_isr_info("PTP_TEMP[%s] = 0x%08X\n", det->name, ptp_read(PTP_TEMP));
  1150. ptp_isr_info("PTP_PTPINTSTS[%s] = 0x%08X\n", det->name, ptp_read(PTP_PTPINTSTS));
  1151. ptp_isr_info("PTP_PTPINTSTSRAW[%s] = 0x%08X\n", det->name,
  1152. ptp_read(PTP_PTPINTSTSRAW));
  1153. ptp_isr_info("PTP_PTPINTEN[%s] = 0x%08X\n", det->name, ptp_read(PTP_PTPINTEN));
  1154. ptp_isr_info("PTP_SMSTATE0[%s] = 0x%08X\n", det->name, ptp_read(PTP_SMSTATE0));
  1155. ptp_isr_info("PTP_SMSTATE1[%s] = 0x%08X\n", det->name, ptp_read(PTP_SMSTATE1));
  1156. mt_ptp_unlock(&flags);
  1157. }
  1158. ptp_isr_info("PTP_PTPCORESEL = 0x%08X\n", ptp_read(PTP_PTPCORESEL));
  1159. ptp_isr_info("PTP_THERMINTST = 0x%08X\n", ptp_read(PTP_THERMINTST));
  1160. ptp_isr_info("PTP_PTPODINTST = 0x%08X\n", ptp_read(PTP_PTPODINTST));
  1161. ptp_isr_info("PTP_THSTAGE0ST = 0x%08X\n", ptp_read(PTP_THSTAGE0ST));
  1162. ptp_isr_info("PTP_THSTAGE1ST = 0x%08X\n", ptp_read(PTP_THSTAGE1ST));
  1163. ptp_isr_info("PTP_THSTAGE2ST = 0x%08X\n", ptp_read(PTP_THSTAGE2ST));
  1164. ptp_isr_info("PTP_THAHBST0 = 0x%08X\n", ptp_read(PTP_THAHBST0));
  1165. ptp_isr_info("PTP_THAHBST1 = 0x%08X\n", ptp_read(PTP_THAHBST1));
  1166. ptp_isr_info("PTP_PTPSPARE0 = 0x%08X\n", ptp_read(PTP_PTPSPARE0));
  1167. ptp_isr_info("PTP_PTPSPARE1 = 0x%08X\n", ptp_read(PTP_PTPSPARE1));
  1168. ptp_isr_info("PTP_PTPSPARE2 = 0x%08X\n", ptp_read(PTP_PTPSPARE2));
  1169. ptp_isr_info("PTP_PTPSPARE3 = 0x%08X\n", ptp_read(PTP_PTPSPARE3));
  1170. ptp_isr_info("PTP_THSLPEVEB = 0x%08X\n", ptp_read(PTP_THSLPEVEB));
  1171. FUNC_EXIT(FUNC_LV_HELP);
  1172. }
  1173. static inline void handle_init01_isr(struct ptp_det *det)
  1174. {
  1175. FUNC_ENTER(FUNC_LV_LOCAL);
  1176. ptp_isr_info("@ %s(%s)\n", __func__, det->name);
  1177. det->dcvalues[PTP_PHASE_INIT01] = ptp_read(PTP_DCVALUES);
  1178. det->ptp_freqpct30[PTP_PHASE_INIT01] = ptp_read(PTP_FREQPCT30);
  1179. det->ptp_26c[PTP_PHASE_INIT01] = ptp_read(PTP_PTPINTEN + 0x10);
  1180. det->ptp_vop30[PTP_PHASE_INIT01] = ptp_read(PTP_VOP30);
  1181. det->ptp_ptpen[PTP_PHASE_INIT01] = ptp_read(PTP_PTPEN);
  1182. #if DUMP_DATA_TO_DE
  1183. {
  1184. int i;
  1185. for (i = 0; i < ARRAY_SIZE(reg_dump_addr_off); i++) {
  1186. det->reg_dump_data[i][PTP_PHASE_INIT01] =
  1187. ptp_read(PTP_BASEADDR + reg_dump_addr_off[i]);
  1188. ptp_isr_info("0x%lx === 0x%08x\n",
  1189. (unsigned long)PTP_BASEADDR + reg_dump_addr_off[i],
  1190. det->reg_dump_data[i][PTP_PHASE_INIT01]
  1191. );
  1192. } }
  1193. #endif
  1194. det->DCVOFFSETIN = ~(ptp_read(PTP_DCVALUES) & 0xffff) + 1;
  1195. det->AGEVOFFSETIN = ptp_read(PTP_AGEVALUES) & 0xffff;
  1196. /*
  1197. * Set PTPEN.PTPINITEN/PTPEN.PTPINIT2EN = 0x0 &
  1198. * Clear PTP INIT interrupt PTPINTSTS = 0x00000001
  1199. */
  1200. ptp_write(PTP_PTPEN, 0x0);
  1201. ptp_write(PTP_PTPINTSTS, 0x1);
  1202. /* ptp_init01_finish(det); */
  1203. det->ops->init02(det);
  1204. FUNC_EXIT(FUNC_LV_LOCAL);
  1205. }
  1206. static inline void handle_init02_isr(struct ptp_det *det)
  1207. {
  1208. unsigned int temp;
  1209. int i;
  1210. /* struct ptp_ctrl *ctrl = id_to_ptp_ctrl(det->ctrl_id); */
  1211. FUNC_ENTER(FUNC_LV_LOCAL);
  1212. ptp_isr_info("@ %s(%s)\n", __func__, det->name);
  1213. det->dcvalues[PTP_PHASE_INIT02] = ptp_read(PTP_DCVALUES);
  1214. det->ptp_freqpct30[PTP_PHASE_INIT02] = ptp_read(PTP_FREQPCT30);
  1215. det->ptp_26c[PTP_PHASE_INIT02] = ptp_read(PTP_PTPINTEN + 0x10);
  1216. det->ptp_vop30[PTP_PHASE_INIT02] = ptp_read(PTP_VOP30);
  1217. det->ptp_ptpen[PTP_PHASE_INIT02] = ptp_read(PTP_PTPEN);
  1218. #if DUMP_DATA_TO_DE
  1219. {
  1220. int i;
  1221. for (i = 0; i < ARRAY_SIZE(reg_dump_addr_off); i++) {
  1222. det->reg_dump_data[i][PTP_PHASE_INIT02] =
  1223. ptp_read(PTP_BASEADDR + reg_dump_addr_off[i]);
  1224. ptp_isr_info("0x%lx === 0x%08x\n",
  1225. (unsigned long)PTP_BASEADDR + reg_dump_addr_off[i],
  1226. det->reg_dump_data[i][PTP_PHASE_INIT02]
  1227. );
  1228. } }
  1229. #endif
  1230. temp = ptp_read(PTP_VOP30);
  1231. /* PTP_VOP30=>pmic value */
  1232. det->volt_tbl[0] = (temp & 0xff) + PTPOD_PMIC_OFFSET;
  1233. det->volt_tbl[1] = ((temp >> 8) & 0xff) + PTPOD_PMIC_OFFSET;
  1234. det->volt_tbl[2] = ((temp >> 16) & 0xff) + PTPOD_PMIC_OFFSET;
  1235. det->volt_tbl[3] = ((temp >> 24) & 0xff) + PTPOD_PMIC_OFFSET;
  1236. temp = ptp_read(PTP_VOP74);
  1237. /* PTP_VOP74=>pmic value */
  1238. det->volt_tbl[4] = (temp & 0xff) + PTPOD_PMIC_OFFSET;
  1239. det->volt_tbl[5] = ((temp >> 8) & 0xff) + PTPOD_PMIC_OFFSET;
  1240. det->volt_tbl[6] = ((temp >> 16) & 0xff) + PTPOD_PMIC_OFFSET;
  1241. det->volt_tbl[7] = ((temp >> 24) & 0xff) + PTPOD_PMIC_OFFSET;
  1242. /* backup to volt_tbl_init2 */
  1243. memcpy(det->volt_tbl_init2, det->volt_tbl, sizeof(det->volt_tbl_init2));
  1244. for (i = 0; i < NR_FREQ; i++) {
  1245. ptp_isr_info("ptp_detectors[%s].volt_tbl[%d] = 0x%08X (%d)\n",
  1246. det->name, i, det->volt_tbl[i],
  1247. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl[i]));
  1248. }
  1249. ptp_isr_info("ptp_level = 0x%08X\n", ptp_level);
  1250. ptp_set_ptp_volt(det);
  1251. if (stress_result == 1)
  1252. stress_result = 0;
  1253. /*
  1254. * Set PTPEN.PTPINITEN/PTPEN.PTPINIT2EN = 0x0 &
  1255. * Clear PTP INIT interrupt PTPINTSTS = 0x00000001
  1256. */
  1257. ptp_write(PTP_PTPEN, 0x0);
  1258. ptp_write(PTP_PTPINTSTS, 0x1);
  1259. /* atomic_dec(&ctrl->in_init); */
  1260. /* complete(&ctrl->init_done); */
  1261. det->ops->mon_mode(det);
  1262. FUNC_EXIT(FUNC_LV_LOCAL);
  1263. }
  1264. static inline void handle_init_err_isr(struct ptp_det *det)
  1265. {
  1266. FUNC_ENTER(FUNC_LV_LOCAL);
  1267. ptp_isr_info("====================================================\n");
  1268. ptp_isr_info("PTP init err: PTPEN(%p) = 0x%08X, PTPINTSTS(%p) = 0x%08X\n",
  1269. PTP_PTPEN, ptp_read(PTP_PTPEN), PTP_PTPINTSTS, ptp_read(PTP_PTPINTSTS));
  1270. ptp_isr_info("PTP_SMSTATE0 (%p) = 0x%08X\n", PTP_SMSTATE0, ptp_read(PTP_SMSTATE0));
  1271. ptp_isr_info("PTP_SMSTATE1 (%p) = 0x%08X\n", PTP_SMSTATE1, ptp_read(PTP_SMSTATE1));
  1272. ptp_isr_info("====================================================\n");
  1273. det->ops->disable_locked(det, BY_INIT_ERROR);
  1274. FUNC_EXIT(FUNC_LV_LOCAL);
  1275. }
  1276. static inline void handle_mon_mode_isr(struct ptp_det *det)
  1277. {
  1278. unsigned int temp;
  1279. int i;
  1280. FUNC_ENTER(FUNC_LV_LOCAL);
  1281. ptp_isr_info("@ %s(%s)\n", __func__, det->name);
  1282. ptp_isr_info("cpu_temp=%d\n", tscpu_get_temp_by_bank(THERMAL_BANK0));
  1283. det->dcvalues[PTP_PHASE_MON] = ptp_read(PTP_DCVALUES);
  1284. det->ptp_freqpct30[PTP_PHASE_MON] = ptp_read(PTP_FREQPCT30);
  1285. det->ptp_26c[PTP_PHASE_MON] = ptp_read(PTP_PTPINTEN + 0x10);
  1286. det->ptp_vop30[PTP_PHASE_MON] = ptp_read(PTP_VOP30);
  1287. det->ptp_ptpen[PTP_PHASE_MON] = ptp_read(PTP_PTPEN);
  1288. #if DUMP_DATA_TO_DE
  1289. {
  1290. int i;
  1291. for (i = 0; i < ARRAY_SIZE(reg_dump_addr_off); i++) {
  1292. det->reg_dump_data[i][PTP_PHASE_MON] =
  1293. ptp_read(PTP_BASEADDR + reg_dump_addr_off[i]);
  1294. ptp_isr_info("0x%lx === 0x%08x\n",
  1295. (unsigned long)PTP_BASEADDR + reg_dump_addr_off[i],
  1296. det->reg_dump_data[i][PTP_PHASE_MON]
  1297. );
  1298. }
  1299. }
  1300. #endif
  1301. /* check if thermal sensor init completed? */
  1302. temp = (ptp_read(PTP_TEMP) & 0xff);
  1303. if ((temp > 0x4b) && (temp < 0xd3)) {
  1304. ptp_isr_info("thermal sensor init has not been completed.(temp = 0x%08X)\n", temp);
  1305. goto out;
  1306. }
  1307. temp = ptp_read(PTP_VOP30);
  1308. det->volt_tbl[0] = (temp & 0xff) + PTPOD_PMIC_OFFSET;
  1309. det->volt_tbl[1] = ((temp >> 8) & 0xff) + PTPOD_PMIC_OFFSET;
  1310. det->volt_tbl[2] = ((temp >> 16) & 0xff) + PTPOD_PMIC_OFFSET;
  1311. det->volt_tbl[3] = ((temp >> 24) & 0xff) + PTPOD_PMIC_OFFSET;
  1312. temp = ptp_read(PTP_VOP74);
  1313. det->volt_tbl[4] = (temp & 0xff) + PTPOD_PMIC_OFFSET;
  1314. det->volt_tbl[5] = ((temp >> 8) & 0xff) + PTPOD_PMIC_OFFSET;
  1315. det->volt_tbl[6] = ((temp >> 16) & 0xff) + PTPOD_PMIC_OFFSET;
  1316. det->volt_tbl[7] = ((temp >> 24) & 0xff) + PTPOD_PMIC_OFFSET;
  1317. for (i = 0; i < NR_FREQ; i++)
  1318. ptp_isr_info("ptp_detectors[%s].volt_tbl[%d] = 0x%08X (%d)\n",
  1319. det->name, i, det->volt_tbl[i],
  1320. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl[i]));
  1321. /* ptp_isr_info("ptp_level = 0x%08X\n", ptp_level); */
  1322. ptp_set_ptp_volt(det);
  1323. out:
  1324. /* Clear PTP INIT interrupt PTPINTSTS = 0x00ff0000 */
  1325. ptp_write(PTP_PTPINTSTS, 0x00ff0000);
  1326. FUNC_EXIT(FUNC_LV_LOCAL);
  1327. }
  1328. static inline void handle_mon_err_isr(struct ptp_det *det)
  1329. {
  1330. FUNC_ENTER(FUNC_LV_LOCAL);
  1331. /* PTP Monitor mode error handler */
  1332. ptp_isr_info("====================================================\n");
  1333. ptp_isr_info("PTP mon err: PTPEN(%p) = 0x%08X, PTPINTSTS(%p) = 0x%08X\n",
  1334. PTP_PTPEN, ptp_read(PTP_PTPEN), PTP_PTPINTSTS, ptp_read(PTP_PTPINTSTS));
  1335. ptp_isr_info("PTP_SMSTATE0 (%p) = 0x%08X\n", PTP_SMSTATE0, ptp_read(PTP_SMSTATE0));
  1336. ptp_isr_info("PTP_SMSTATE1 (%p) = 0x%08X\n", PTP_SMSTATE1, ptp_read(PTP_SMSTATE1));
  1337. ptp_isr_info("PTP_TEMP (%p) = 0x%08X\n", PTP_TEMP, ptp_read(PTP_TEMP));
  1338. ptp_isr_info("PTP_TEMPMSR0 (%p) = 0x%08X\n", PTP_TEMPMSR0, ptp_read(PTP_TEMPMSR0));
  1339. ptp_isr_info("PTP_TEMPMSR1 (%p) = 0x%08X\n", PTP_TEMPMSR1, ptp_read(PTP_TEMPMSR1));
  1340. ptp_isr_info("PTP_TEMPMSR2 (%p) = 0x%08X\n", PTP_TEMPMSR2, ptp_read(PTP_TEMPMSR2));
  1341. ptp_isr_info("PTP_TEMPMONCTL0 (%p) = 0x%08X\n", PTP_TEMPMONCTL0, ptp_read(PTP_TEMPMONCTL0));
  1342. ptp_isr_info("PTP_TEMPMSRCTL1 (%p) = 0x%08X\n", PTP_TEMPMSRCTL1, ptp_read(PTP_TEMPMSRCTL1));
  1343. ptp_isr_info("====================================================\n");
  1344. det->ops->disable_locked(det, BY_MON_ERROR);
  1345. FUNC_EXIT(FUNC_LV_LOCAL);
  1346. }
  1347. static inline void ptp_isr_handler(struct ptp_det *det)
  1348. {
  1349. unsigned int PTPINTSTS, PTPEN;
  1350. FUNC_ENTER(FUNC_LV_LOCAL);
  1351. PTPINTSTS = ptp_read(PTP_PTPINTSTS);
  1352. PTPEN = ptp_read(PTP_PTPEN);
  1353. ptp_isr_info("[%s]\n", det->name);
  1354. ptp_isr_info("PTPINTSTS = 0x%08X\n", PTPINTSTS);
  1355. ptp_isr_info("PTP_PTPEN = 0x%08X\n", PTPEN);
  1356. ptp_isr_info("*(%p) = 0x%08X\n", PTP_DCVALUES, ptp_read(PTP_DCVALUES));
  1357. ptp_isr_info("*(%p) = 0x%08X\n", PTP_AGECOUNT, ptp_read(PTP_AGECOUNT));
  1358. if (PTPINTSTS == 0x1) { /* PTP init1 or init2 */
  1359. if ((PTPEN & 0x7) == 0x1) /* PTP init1 */
  1360. handle_init01_isr(det);
  1361. else if ((PTPEN & 0x7) == 0x5) /* PTP init2 */
  1362. handle_init02_isr(det);
  1363. else {
  1364. handle_init_err_isr(det);
  1365. }
  1366. } else if ((PTPINTSTS & 0x00ff0000) != 0x0)
  1367. handle_mon_mode_isr(det);
  1368. else {
  1369. if (((PTPEN & 0x7) == 0x1) || ((PTPEN & 0x7) == 0x5))
  1370. handle_init_err_isr(det);
  1371. else
  1372. handle_mon_err_isr(det);
  1373. }
  1374. FUNC_EXIT(FUNC_LV_LOCAL);
  1375. }
  1376. static irqreturn_t ptp_isr(int irq, void *dev_id)
  1377. {
  1378. unsigned long flags;
  1379. struct ptp_det *det = NULL;
  1380. int i;
  1381. FUNC_ENTER(FUNC_LV_MODULE);
  1382. /* mt_ptp_reg_dump(); */
  1383. mt_ptp_lock(&flags);
  1384. for (i = 0; i < NR_PTP_CTRL; i++) {
  1385. /* if (i == PTP_CTRL_VCORE) */
  1386. /* continue; */
  1387. if ((BIT(i) & ptp_read(PTP_PTPODINTST)))
  1388. continue;
  1389. det = &ptp_detectors[i];
  1390. det->ops->switch_bank(det);
  1391. /* mt_ptp_reg_dump_locked(); */
  1392. ptp_isr_handler(det);
  1393. }
  1394. mt_ptp_unlock(&flags);
  1395. FUNC_EXIT(FUNC_LV_MODULE);
  1396. return IRQ_HANDLED;
  1397. }
  1398. void ptp_init01(void)
  1399. {
  1400. struct ptp_det *det;
  1401. struct ptp_ctrl *ctrl;
  1402. FUNC_ENTER(FUNC_LV_LOCAL);
  1403. for_each_det_ctrl(det, ctrl) {
  1404. {
  1405. unsigned long flag;
  1406. unsigned int vboot;
  1407. vboot = PTP_VOLT_TO_PMIC_VAL(det->ops->get_volt(det));
  1408. ptp_alert("@%s(),vboot = %d\n", __func__, vboot);
  1409. if (vboot != det->VBOOT) {
  1410. ptp_error("@%s():%d, get_volt(%s) = 0x%08X, VBOOT = 0x%08X\n",
  1411. __func__, __LINE__, det->name, vboot, det->VBOOT);
  1412. aee_kernel_warning("mt_ptp",
  1413. "@%s():%d, get_volt(%s) = 0x%08X, VBOOT = 0x%08X\n",
  1414. __func__, __LINE__, det->name, vboot,
  1415. det->VBOOT);
  1416. }
  1417. mt_ptp_lock(&flag);
  1418. det->ops->init01(det);
  1419. mt_ptp_unlock(&flag);
  1420. }
  1421. }
  1422. FUNC_EXIT(FUNC_LV_LOCAL);
  1423. }
  1424. void ptp_init02(void)
  1425. {
  1426. struct ptp_det *det;
  1427. struct ptp_ctrl *ctrl;
  1428. FUNC_ENTER(FUNC_LV_LOCAL);
  1429. for_each_det_ctrl(det, ctrl) {
  1430. if (HAS_FEATURE(det, FEA_MON)) {
  1431. unsigned long flag;
  1432. mt_ptp_lock(&flag);
  1433. det->ops->init02(det);
  1434. mt_ptp_unlock(&flag);
  1435. }
  1436. }
  1437. FUNC_EXIT(FUNC_LV_LOCAL);
  1438. }
  1439. #if EN_PTP_OD
  1440. /* leakage */
  1441. unsigned int leakage_core;
  1442. unsigned int leakage_gpu;
  1443. unsigned int leakage_sram2;
  1444. unsigned int leakage_sram1;
  1445. void get_devinfo(struct ptp_devinfo *p)
  1446. {
  1447. int *val = (int *)p;
  1448. FUNC_ENTER(FUNC_LV_HELP);
  1449. val[0] = get_devinfo_with_index(7); /* ptp_read(0x10206180); */
  1450. val[1] = get_devinfo_with_index(8); /* ptp_read(0x10206184); */
  1451. val[2] = get_devinfo_with_index(9); /* ptp_read(0x10206188); */
  1452. val[3] = get_devinfo_with_index(14); /* ptp_read(0x1020618C); */
  1453. val[4] = get_devinfo_with_index(15); /* ptp_read(0x10206190); */
  1454. val[5] = get_devinfo_with_index(16); /* ptp_read(0x10206194); */
  1455. val[6] = get_devinfo_with_index(17); /* ptp_read(0xF0206270); */
  1456. val[7] = get_devinfo_with_index(47); /* ptp_read(0xF02061B0); */
  1457. ptp_crit("val[0]=0x%x\n", val[0]);
  1458. ptp_crit("val[1]=0x%x\n", val[1]);
  1459. ptp_crit("val[2]=0x%x\n", val[2]);
  1460. ptp_crit("val[3]=0x%x\n", val[3]);
  1461. ptp_crit("val[4]=0x%x\n", val[4]);
  1462. ptp_crit("val[5]=0x%x\n", val[5]);
  1463. ptp_crit("val[6]=0x%x\n", val[6]);
  1464. ptp_crit("val[7]=0x%x\n", val[7]);
  1465. ptp_crit("p->PTPINITEN=0x%x\n", p->PTPINITEN);
  1466. ptp_crit("p->PTPMONEN=0x%x\n", p->PTPMONEN);
  1467. FUNC_EXIT(FUNC_LV_HELP);
  1468. }
  1469. static int ptp_probe(struct platform_device *pdev)
  1470. {
  1471. int ret;
  1472. struct ptp_det *det;
  1473. struct ptp_ctrl *ctrl;
  1474. enum mt_cpu_dvfs_id cpu;
  1475. FUNC_ENTER(FUNC_LV_MODULE);
  1476. cpu = MT_CPU_DVFS_LITTLE;
  1477. /* set PTP IRQ */
  1478. ret = request_irq(ptpod_irq_number, ptp_isr, IRQF_TRIGGER_LOW, "ptp", NULL);
  1479. if (ret) {
  1480. ptp_notice("PTP IRQ register failed (%d)\n", ret);
  1481. WARN_ON(1);
  1482. }
  1483. ptp_notice("Set PTP IRQ OK.\n");
  1484. /* ptp_level = mt_ptp_get_level(); */
  1485. /* atomic_set(&ptp_init01_cnt, 0); */
  1486. for_each_ctrl(ctrl) {
  1487. ptp_init_ctrl(ctrl);
  1488. }
  1489. mt_fh_popod_save();
  1490. /* disable DVFS and set vproc = 1.15v (1 GHz) */
  1491. mt_cpufreq_disable_by_ptpod(MT_CPU_DVFS_LITTLE);
  1492. /* Enable PWM mode here */
  1493. if (mt_cpufreq_get_freq_by_idx(cpu, 0) <= 1300000)
  1494. pmic_force_vproc_pwm(1);
  1495. /*for slow idle */
  1496. ptp_data[0] = 0xffffffff;
  1497. for_each_det(det) {
  1498. ptp_init_det(det, &ptp_devinfo);
  1499. }
  1500. ptp_init01();
  1501. /* Disable PWM mode here */
  1502. if (mt_cpufreq_get_freq_by_idx(cpu, 0) <= 1300000)
  1503. pmic_force_vproc_pwm(0);
  1504. ptp_data[0] = 0;
  1505. /* enable DVFS */
  1506. mt_cpufreq_enable_by_ptpod(MT_CPU_DVFS_LITTLE);
  1507. mt_fh_popod_restore();
  1508. FUNC_EXIT(FUNC_LV_MODULE);
  1509. return 0;
  1510. }
  1511. static int ptp_suspend(struct platform_device *pdev, pm_message_t state)
  1512. {
  1513. /*
  1514. kthread_stop(ptp_volt_thread);
  1515. */
  1516. FUNC_ENTER(FUNC_LV_MODULE);
  1517. FUNC_EXIT(FUNC_LV_MODULE);
  1518. return 0;
  1519. }
  1520. static int ptp_resume(struct platform_device *pdev)
  1521. {
  1522. /*
  1523. ptp_volt_thread = kthread_run(ptp_volt_thread_handler, 0, "ptp volt");
  1524. if (IS_ERR(ptp_volt_thread))
  1525. {
  1526. printk("[%s]: failed to create ptp volt thread\n", __func__);
  1527. }
  1528. */
  1529. FUNC_ENTER(FUNC_LV_MODULE);
  1530. ptp_init02();
  1531. FUNC_EXIT(FUNC_LV_MODULE);
  1532. return 0;
  1533. }
  1534. #ifdef CONFIG_OF
  1535. static const struct of_device_id mt_ptpod_of_match[] = {
  1536. {
  1537. .compatible = "mediatek,ptp_fsm_v1",}, {
  1538. },};
  1539. #endif
  1540. static struct platform_driver ptp_driver = {
  1541. .remove = NULL,
  1542. .shutdown = NULL,
  1543. .probe = ptp_probe,
  1544. .suspend = ptp_suspend,
  1545. .resume = ptp_resume,
  1546. .driver = {
  1547. .name = "mt-ptp",
  1548. #ifdef CONFIG_OF
  1549. .of_match_table = mt_ptpod_of_match,
  1550. #endif
  1551. },
  1552. };
  1553. int mt_ptp_opp_num(ptp_det_id id)
  1554. {
  1555. struct ptp_det *det = id_to_ptp_det(id);
  1556. FUNC_ENTER(FUNC_LV_API);
  1557. FUNC_EXIT(FUNC_LV_API);
  1558. return det->num_freq_tbl;
  1559. }
  1560. EXPORT_SYMBOL(mt_ptp_opp_num);
  1561. void mt_ptp_opp_freq(ptp_det_id id, unsigned int *freq)
  1562. {
  1563. struct ptp_det *det = id_to_ptp_det(id);
  1564. int i = 0;
  1565. FUNC_ENTER(FUNC_LV_API);
  1566. for (i = 0; i < det->num_freq_tbl; i++)
  1567. freq[i] = det->freq_tbl[i];
  1568. FUNC_EXIT(FUNC_LV_API);
  1569. }
  1570. EXPORT_SYMBOL(mt_ptp_opp_freq);
  1571. void mt_ptp_opp_status(ptp_det_id id, unsigned int *temp, unsigned int *volt)
  1572. {
  1573. struct ptp_det *det = id_to_ptp_det(id);
  1574. int i = 0;
  1575. FUNC_ENTER(FUNC_LV_API);
  1576. *temp = 0;
  1577. for (i = 0; i < det->num_freq_tbl; i++)
  1578. volt[i] = PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[i]);
  1579. FUNC_EXIT(FUNC_LV_API);
  1580. }
  1581. EXPORT_SYMBOL(mt_ptp_opp_status);
  1582. /*
  1583. * return current PTP stauts
  1584. */
  1585. int mt_ptp_status(ptp_det_id id)
  1586. {
  1587. struct ptp_det *det = id_to_ptp_det(id);
  1588. FUNC_ENTER(FUNC_LV_API);
  1589. BUG_ON(!det);
  1590. BUG_ON(!det->ops);
  1591. BUG_ON(!det->ops->get_status);
  1592. FUNC_EXIT(FUNC_LV_API);
  1593. return det->ops->get_status(det);
  1594. }
  1595. #ifdef CONFIG_PROC_FS
  1596. /*
  1597. *
  1598. * PROCFS interface for debugging
  1599. *
  1600. */
  1601. /*
  1602. * show current PTP stauts
  1603. */
  1604. static int ptp_debug_proc_show(struct seq_file *m, void *v)
  1605. {
  1606. struct ptp_det *det = (struct ptp_det *)m->private;
  1607. FUNC_ENTER(FUNC_LV_HELP);
  1608. seq_printf(m, "PTPOD[%s] %s (ptp_level = 0x%08X)\n",
  1609. det->name, det->ops->get_status(det) ? "enabled" : "disable", ptp_level);
  1610. FUNC_EXIT(FUNC_LV_HELP);
  1611. return 0;
  1612. }
  1613. /*
  1614. * set PTP status by procfs interface
  1615. */
  1616. static ssize_t ptp_debug_proc_write(struct file *file,
  1617. const char __user *buffer, size_t count, loff_t *pos)
  1618. {
  1619. int ret;
  1620. int enabled = 0;
  1621. char *buf = (char *)__get_free_page(GFP_USER);
  1622. struct ptp_det *det = (struct ptp_det *)PDE_DATA(file_inode(file));
  1623. int rc;
  1624. FUNC_ENTER(FUNC_LV_HELP);
  1625. if (!buf) {
  1626. FUNC_EXIT(FUNC_LV_HELP);
  1627. return -ENOMEM;
  1628. }
  1629. ret = -EINVAL;
  1630. if (count >= PAGE_SIZE)
  1631. goto out;
  1632. ret = -EFAULT;
  1633. if (copy_from_user(buf, buffer, count))
  1634. goto out;
  1635. buf[count] = '\0';
  1636. rc = kstrtoint(buf, 10, &enabled);
  1637. if (rc < 0)
  1638. ret = -EINVAL;
  1639. else {
  1640. ret = 0;
  1641. if (0 == enabled)
  1642. det->ops->disable(det, BY_PROCFS);
  1643. }
  1644. out:
  1645. free_page((unsigned long)buf);
  1646. FUNC_EXIT(FUNC_LV_HELP);
  1647. return (ret < 0) ? ret : count;
  1648. }
  1649. /*
  1650. * show current PTP data
  1651. */
  1652. static int ptp_dump_proc_show(struct seq_file *m, void *v)
  1653. {
  1654. struct ptp_det *det;
  1655. int *val = (int *)&ptp_devinfo;
  1656. int i;
  1657. FUNC_ENTER(FUNC_LV_HELP);
  1658. mt_ptp_reg_dump();
  1659. for (i = 0; i < sizeof(struct ptp_devinfo) / sizeof(unsigned int); i++)
  1660. seq_printf(m, "PTP_OD%d\t= 0x%08X\n", i, val[i]);
  1661. for_each_det(det) {
  1662. seq_printf(m, "PTP_DCVALUES[%s]\t= 0x%08X\n", det->name, det->VBOOT);
  1663. for (i = PTP_PHASE_INIT01; i < NR_PTP_PHASE; i++) {
  1664. seq_printf(m,
  1665. "dcvalues=0x%08X, ptp_freqpct30=0x%08X, ptp_26c=0x%08X, ptp_vop30=0x%08X,ptp_ptpen= 0x%08X\n",
  1666. det->dcvalues[i], det->ptp_freqpct30[i], det->ptp_26c[i],
  1667. det->ptp_vop30[i], det->ptp_ptpen[i]
  1668. );
  1669. #if DUMP_DATA_TO_DE
  1670. {
  1671. int j;
  1672. for (j = 0; j < ARRAY_SIZE(reg_dump_addr_off); j++)
  1673. seq_printf(m, "0x%lx === 0x%08x\n",
  1674. (unsigned long)PTP_BASEADDR +
  1675. reg_dump_addr_off[j], det->reg_dump_data[j][i]
  1676. );
  1677. }
  1678. #endif
  1679. }
  1680. }
  1681. FUNC_EXIT(FUNC_LV_HELP);
  1682. return 0;
  1683. }
  1684. /*
  1685. * show current voltage
  1686. */
  1687. static int ptp_cur_volt_proc_show(struct seq_file *m, void *v)
  1688. {
  1689. struct ptp_det *det = (struct ptp_det *)m->private;
  1690. u32 rdata = 0;
  1691. FUNC_ENTER(FUNC_LV_HELP);
  1692. rdata = det->ops->get_volt(det);
  1693. if (rdata != 0)
  1694. seq_printf(m, "%d\n", rdata);
  1695. else
  1696. seq_printf(m, "PTPOD[%s] read current voltage fail\n", det->name);
  1697. FUNC_EXIT(FUNC_LV_HELP);
  1698. return 0;
  1699. }
  1700. static int ptp_stress_result_proc_show(struct seq_file *m, void *v)
  1701. {
  1702. if (stress_result != 0)
  1703. ptp_isr_info("PTP fail to trigger irq\n");
  1704. seq_printf(m, "0x%X\n", stress_result);
  1705. return 0;
  1706. }
  1707. /*
  1708. * show current PTP status
  1709. */
  1710. static int ptp_status_proc_show(struct seq_file *m, void *v)
  1711. {
  1712. struct ptp_det *det = (struct ptp_det *)m->private;
  1713. FUNC_ENTER(FUNC_LV_HELP);
  1714. seq_printf(m,
  1715. "PTP_LOG: PTPOD [%s] (%d) - (%d, %d, %d, %d, %d, %d, %d, %d) - (%d, %d, %d, %d, %d, %d, %d, %d)\n",
  1716. det->name, det->ops->get_temp(det),
  1717. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[0]),
  1718. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[1]),
  1719. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[2]),
  1720. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[3]),
  1721. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[4]),
  1722. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[5]),
  1723. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[6]),
  1724. PTP_PMIC_VAL_TO_VOLT(det->volt_tbl_pmic[7]),
  1725. det->freq_tbl[0],
  1726. det->freq_tbl[1],
  1727. det->freq_tbl[2],
  1728. det->freq_tbl[3],
  1729. det->freq_tbl[4], det->freq_tbl[5], det->freq_tbl[6], det->freq_tbl[7]);
  1730. FUNC_EXIT(FUNC_LV_HELP);
  1731. return 0;
  1732. }
  1733. /*
  1734. * set PTP log enable by procfs interface
  1735. */
  1736. static int ptp_log_en;
  1737. static int ptp_log_en_proc_show(struct seq_file *m, void *v)
  1738. {
  1739. FUNC_ENTER(FUNC_LV_HELP);
  1740. seq_printf(m, "%d\n", ptp_log_en);
  1741. FUNC_EXIT(FUNC_LV_HELP);
  1742. return 0;
  1743. }
  1744. static ssize_t ptp_log_en_proc_write(struct file *file,
  1745. const char __user *buffer, size_t count, loff_t *pos)
  1746. {
  1747. int ret;
  1748. char *buf = (char *)__get_free_page(GFP_USER);
  1749. int rc;
  1750. FUNC_ENTER(FUNC_LV_HELP);
  1751. if (!buf) {
  1752. FUNC_EXIT(FUNC_LV_HELP);
  1753. return -ENOMEM;
  1754. }
  1755. ret = -EINVAL;
  1756. if (count >= PAGE_SIZE)
  1757. goto out;
  1758. ret = -EFAULT;
  1759. if (copy_from_user(buf, buffer, count))
  1760. goto out;
  1761. buf[count] = '\0';
  1762. ret = -EINVAL;
  1763. rc = kstrtoint(buf, 10, &ptp_log_en);
  1764. if (rc < 0) {
  1765. ptp_notice("bad argument!! Should be \"0\" or \"1\"\n");
  1766. goto out;
  1767. }
  1768. ret = 0;
  1769. switch (ptp_log_en) {
  1770. case 0:
  1771. ptp_notice("ptp log disabled.\n");
  1772. hrtimer_cancel(&ptp_log_timer);
  1773. break;
  1774. case 1:
  1775. ptp_notice("ptp log enabled.\n");
  1776. hrtimer_start(&ptp_log_timer, ns_to_ktime(LOG_INTERVAL), HRTIMER_MODE_REL);
  1777. break;
  1778. default:
  1779. ptp_error("bad argument!! Should be \"0\" or \"1\"\n");
  1780. ret = -EINVAL;
  1781. }
  1782. out:
  1783. free_page((unsigned long)buf);
  1784. FUNC_EXIT(FUNC_LV_HELP);
  1785. return (ret < 0) ? ret : count;
  1786. }
  1787. /*
  1788. * show PTP offset
  1789. */
  1790. static int ptp_offset_proc_show(struct seq_file *m, void *v)
  1791. {
  1792. struct ptp_det *det = (struct ptp_det *)m->private;
  1793. FUNC_ENTER(FUNC_LV_HELP);
  1794. seq_printf(m, "%d\n", det->volt_offset);
  1795. FUNC_EXIT(FUNC_LV_HELP);
  1796. return 0;
  1797. }
  1798. /*
  1799. * set PTP offset by procfs
  1800. */
  1801. static ssize_t ptp_offset_proc_write(struct file *file,
  1802. const char __user *buffer, size_t count, loff_t *pos)
  1803. {
  1804. int ret;
  1805. char *buf = (char *)__get_free_page(GFP_USER);
  1806. int offset = 0;
  1807. struct ptp_det *det = (struct ptp_det *)PDE_DATA(file_inode(file));
  1808. int rc;
  1809. FUNC_ENTER(FUNC_LV_HELP);
  1810. if (!buf) {
  1811. FUNC_EXIT(FUNC_LV_HELP);
  1812. return -ENOMEM;
  1813. }
  1814. ret = -EINVAL;
  1815. if (count >= PAGE_SIZE)
  1816. goto out;
  1817. ret = -EFAULT;
  1818. if (copy_from_user(buf, buffer, count))
  1819. goto out;
  1820. buf[count] = '\0';
  1821. rc = kstrtoint(buf, 10, &offset);
  1822. if (rc < 0) {
  1823. ret = -EINVAL;
  1824. ptp_notice("bad argument_1!! argument should be \"0\"\n");
  1825. } else {
  1826. ret = 0;
  1827. det->volt_offset = offset;
  1828. ptp_set_ptp_volt(det);
  1829. }
  1830. out:
  1831. free_page((unsigned long)buf);
  1832. FUNC_EXIT(FUNC_LV_HELP);
  1833. return (ret < 0) ? ret : count;
  1834. }
  1835. #define PROC_FOPS_RW(name) \
  1836. static int name ## _proc_open(struct inode *inode, \
  1837. struct file *file) \
  1838. { \
  1839. return single_open(file, name ## _proc_show, \
  1840. PDE_DATA(inode)); \
  1841. } \
  1842. static const struct file_operations name ## _proc_fops = { \
  1843. .owner = THIS_MODULE, \
  1844. .open = name ## _proc_open, \
  1845. .read = seq_read, \
  1846. .llseek = seq_lseek, \
  1847. .release = single_release, \
  1848. .write = name ## _proc_write, \
  1849. }
  1850. #define PROC_FOPS_RO(name) \
  1851. static int name ## _proc_open(struct inode *inode, \
  1852. struct file *file) \
  1853. { \
  1854. return single_open(file, name ## _proc_show, \
  1855. PDE_DATA(inode)); \
  1856. } \
  1857. static const struct file_operations name ## _proc_fops = { \
  1858. .owner = THIS_MODULE, \
  1859. .open = name ## _proc_open, \
  1860. .read = seq_read, \
  1861. .llseek = seq_lseek, \
  1862. .release = single_release, \
  1863. }
  1864. #define PROC_ENTRY(name) {__stringify(name), &name ## _proc_fops}
  1865. PROC_FOPS_RW(ptp_debug);
  1866. PROC_FOPS_RO(ptp_dump);
  1867. PROC_FOPS_RO(ptp_stress_result);
  1868. PROC_FOPS_RW(ptp_log_en);
  1869. PROC_FOPS_RO(ptp_status);
  1870. PROC_FOPS_RO(ptp_cur_volt);
  1871. PROC_FOPS_RW(ptp_offset);
  1872. static int create_procfs(void)
  1873. {
  1874. struct proc_dir_entry *ptp_dir = NULL;
  1875. struct proc_dir_entry *det_dir = NULL;
  1876. int i;
  1877. struct ptp_det *det;
  1878. struct pentry {
  1879. const char *name;
  1880. const struct file_operations *fops;
  1881. };
  1882. struct pentry det_entries[] = {
  1883. PROC_ENTRY(ptp_debug),
  1884. PROC_ENTRY(ptp_status),
  1885. PROC_ENTRY(ptp_cur_volt),
  1886. PROC_ENTRY(ptp_offset),
  1887. };
  1888. struct pentry ptp_entries[] = {
  1889. PROC_ENTRY(ptp_dump),
  1890. PROC_ENTRY(ptp_log_en),
  1891. PROC_ENTRY(ptp_stress_result),
  1892. };
  1893. FUNC_ENTER(FUNC_LV_HELP);
  1894. ptp_dir = proc_mkdir("ptp", NULL);
  1895. if (!ptp_dir) {
  1896. ptp_error("[%s]: mkdir /proc/ptp failed\n", __func__);
  1897. FUNC_EXIT(FUNC_LV_HELP);
  1898. return -1;
  1899. }
  1900. for (i = 0; i < ARRAY_SIZE(ptp_entries); i++) {
  1901. if (!proc_create
  1902. (ptp_entries[i].name, S_IRUGO | S_IWUSR | S_IWGRP, ptp_dir,
  1903. ptp_entries[i].fops)) {
  1904. ptp_error("[%s]: create /proc/ptp/%s failed\n", __func__,
  1905. ptp_entries[i].name);
  1906. FUNC_EXIT(FUNC_LV_HELP);
  1907. return -3;
  1908. }
  1909. }
  1910. for_each_det(det) {
  1911. det_dir = proc_mkdir(det->name, ptp_dir);
  1912. if (!det_dir) {
  1913. ptp_error("[%s]: mkdir /proc/ptp/%s failed\n", __func__, det->name);
  1914. FUNC_EXIT(FUNC_LV_HELP);
  1915. return -2;
  1916. }
  1917. for (i = 0; i < ARRAY_SIZE(det_entries); i++) {
  1918. if (!proc_create_data
  1919. (det_entries[i].name, S_IRUGO | S_IWUSR | S_IWGRP, det_dir,
  1920. det_entries[i].fops, det)) {
  1921. ptp_error("[%s]: create /proc/ptp/%s/%s failed\n", __func__,
  1922. det->name, det_entries[i].name);
  1923. FUNC_EXIT(FUNC_LV_HELP);
  1924. return -3;
  1925. }
  1926. }
  1927. }
  1928. FUNC_EXIT(FUNC_LV_HELP);
  1929. return 0;
  1930. }
  1931. #endif
  1932. int get_ptpod_status(void)
  1933. {
  1934. get_devinfo(&ptp_devinfo);
  1935. return ptp_devinfo.PTPINITEN;
  1936. }
  1937. EXPORT_SYMBOL(get_ptpod_status);
  1938. #define VCORE_VOLT_0 1250000
  1939. #define VCORE_VOLT_1 1150000
  1940. #define VCORE_VOLT_2 1050000
  1941. unsigned int vcore0;
  1942. unsigned int vcore1;
  1943. unsigned int vcore2;
  1944. unsigned int have_550;
  1945. static int __init dt_get_ptp_devinfo(unsigned long node, const char *uname, int depth, void *data)
  1946. {
  1947. struct devinfo_ptp_tag *tags;
  1948. unsigned int size = 0;
  1949. if (depth != 1 || (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  1950. return 0;
  1951. tags = (struct devinfo_ptp_tag *)of_get_flat_dt_prop(node, "atag,ptp", &size);
  1952. if (tags) {
  1953. vcore0 = tags->volt0;
  1954. vcore1 = tags->volt1;
  1955. vcore2 = tags->volt2;
  1956. have_550 = tags->have_550;
  1957. ptp_notice("[PTP][VCORE] - Kernel Got from DT (0x%0X, 0x%0X, 0x%0X, 0x%0X)\n", vcore0,
  1958. vcore1, vcore2, have_550);
  1959. }
  1960. return 1;
  1961. }
  1962. unsigned int is_have_550(void)
  1963. {
  1964. return have_550;
  1965. }
  1966. unsigned int get_vcore_ptp_volt(int uv)
  1967. {
  1968. unsigned int ret;
  1969. switch (uv) {
  1970. case VCORE_VOLT_0:
  1971. ret = vcore0;
  1972. break;
  1973. case VCORE_VOLT_1:
  1974. ret = vcore1;
  1975. break;
  1976. case VCORE_VOLT_2:
  1977. ret = vcore2;
  1978. break;
  1979. default:
  1980. ret = PTP_VOLT_TO_PMIC_VAL(uv / 10) + PTPOD_PMIC_OFFSET;
  1981. break;
  1982. }
  1983. if (ret == 0)
  1984. ret = PTP_VOLT_TO_PMIC_VAL(uv / 10) + PTPOD_PMIC_OFFSET;
  1985. return ret;
  1986. }
  1987. static int __init vcore_ptp_init(void)
  1988. {
  1989. of_scan_flat_dt(dt_get_ptp_devinfo, NULL);
  1990. return 0;
  1991. }
  1992. void process_voltage_bin(struct ptp_devinfo *devinfo)
  1993. {
  1994. if (gpio_get_value(130)) {
  1995. switch (devinfo->LTE_VOLTBIN) {
  1996. case 0:
  1997. mt_cpufreq_set_lte_volt(PTP_VOLT_TO_PMIC_VAL(110625) + PTPOD_PMIC_OFFSET);
  1998. ptp_notice("VLTE voltage bin to 1.10625V\n");
  1999. break;
  2000. case 1:
  2001. mt_cpufreq_set_lte_volt(PTP_VOLT_TO_PMIC_VAL(105000) + PTPOD_PMIC_OFFSET);
  2002. ptp_notice("VLTE voltage bin to 1.05V\n");
  2003. break;
  2004. case 2:
  2005. mt_cpufreq_set_lte_volt(PTP_VOLT_TO_PMIC_VAL(100000) + PTPOD_PMIC_OFFSET);
  2006. ptp_notice("VLTE voltage bin to 1.0V\n");
  2007. break;
  2008. default:
  2009. mt_cpufreq_set_lte_volt(PTP_VOLT_TO_PMIC_VAL(110625) + PTPOD_PMIC_OFFSET);
  2010. ptp_notice("VLTE voltage bin to 1.10625V\n");
  2011. break;
  2012. };
  2013. }
  2014. /* mt_cpufreq_set_lte_volt(det->volt_tbl_bin[0]); */
  2015. }
  2016. /*
  2017. * Module driver
  2018. */
  2019. static int __init ptp_init(void)
  2020. {
  2021. int err = 0;
  2022. struct device_node *node = NULL;
  2023. node = of_find_compatible_node(NULL, NULL, "mediatek,ptp_fsm_v1");
  2024. if (node) {
  2025. /* Setup IO addresses */
  2026. ptpod_base = of_iomap(node, 0);
  2027. }
  2028. /*get ptpod irq num */
  2029. ptpod_irq_number = irq_of_parse_and_map(node, 0);
  2030. if (!ptpod_irq_number)
  2031. return 0;
  2032. get_devinfo(&ptp_devinfo);
  2033. process_voltage_bin(&ptp_devinfo);
  2034. if (0 == ptp_devinfo.PTPINITEN) {
  2035. ptp_notice("PTPINITEN = 0x%08X\n", ptp_devinfo.PTPINITEN);
  2036. FUNC_EXIT(FUNC_LV_MODULE);
  2037. return 0;
  2038. }
  2039. /*
  2040. * init timer for log / volt
  2041. */
  2042. hrtimer_init(&ptp_log_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2043. ptp_log_timer.function = ptp_log_timer_func;
  2044. create_procfs();
  2045. err = platform_driver_register(&ptp_driver);
  2046. if (err) {
  2047. ptp_notice("PTP driver callback register failed..\n");
  2048. FUNC_EXIT(FUNC_LV_MODULE);
  2049. return err;
  2050. }
  2051. FUNC_EXIT(FUNC_LV_MODULE);
  2052. return 0;
  2053. }
  2054. static void __exit ptp_exit(void)
  2055. {
  2056. FUNC_ENTER(FUNC_LV_MODULE);
  2057. ptp_notice("PTP de-initialization\n");
  2058. FUNC_EXIT(FUNC_LV_MODULE);
  2059. } arch_initcall(vcore_ptp_init);
  2060. #ifndef CONFIG_MTK_FPGA
  2061. late_initcall(ptp_init);
  2062. #endif
  2063. #endif
  2064. MODULE_DESCRIPTION("MediaTek PTPOD Driver v0.3");
  2065. MODULE_LICENSE("GPL");
  2066. #undef __MT_PTP_C__