mt_ptp.c 62 KB

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