sdio_autok_proc.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. #include <linux/module.h>
  2. #include <linux/proc_fs.h>
  3. #include <linux/uaccess.h>
  4. #include <linux/kthread.h>
  5. #include <linux/mmc/card.h>
  6. #include <linux/mmc/host.h>
  7. #include <linux/mmc/sdio_func.h>
  8. #include <../drivers/mmc/core/core.h>
  9. #include "mt_sd.h"
  10. #include "sdio_autok.h"
  11. /* #include "autok.h" */
  12. #include <linux/kobject.h>
  13. #include <linux/string.h>
  14. #include <linux/sysfs.h>
  15. #include <linux/init.h>
  16. #include <linux/list.h>
  17. #include <linux/delay.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/mm.h> /* mmap related stuff */
  21. #include <linux/slab.h> /* kmalloc/kfree/kzalloc */
  22. #define AUTOK_THREAD
  23. #define MAX_ARGS_BUF 2048
  24. #define DEVNAME_SIZE 80
  25. /* #define UT_TEST */
  26. static struct kobject *autok_kobj;
  27. /* stage1 kobjects */
  28. static struct kobject *stage1_kobj;
  29. static struct kobject **s1_host_kobj;
  30. static struct attribute **stage1_attrs;
  31. static struct kobj_attribute **stage1_kattr;
  32. /* stage2 kobjects */
  33. static struct kobject *stage2_kobj;
  34. struct attribute **stage2_attrs;
  35. struct kobj_attribute **stage2_kattr;
  36. /* host kobjects */
  37. struct attribute **host_attrs;
  38. struct kobj_attribute **host_kattr;
  39. struct dentry *autok_log_entry;
  40. char *autok_log_info = NULL;
  41. enum STAGE1_NODES {
  42. VOLTAGE,
  43. PARAMS,
  44. DONE,
  45. LOG,
  46. TOTAL_STAGE1_NODE_COUNT
  47. };
  48. const char stage1_nodes[][DEVNAME_SIZE] = { "VOLTAGE", "PARAMS", "DONE", "LOG" };
  49. enum HOST_NODES {
  50. READY,
  51. DEBUG,
  52. PARAM_COUNT,
  53. SS_CORNER,
  54. SUGGEST_VOL,
  55. TOTAL_HOST_NODE_COUNT
  56. };
  57. const char host_nodes[][DEVNAME_SIZE] = {
  58. "ready", "debug", "param_count", "ss_corner", "suggest_vol"
  59. };
  60. #ifdef UT_TEST
  61. u8 is_first_stage1 = 1; /* UT usage */
  62. #endif
  63. #ifndef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  64. #define DMA_ON 0
  65. #define DMA_OFF 1
  66. #endif
  67. struct autok_predata *p_autok_predata;
  68. struct autok_predata *p_single_autok;
  69. static int sdio_host_debug = 1;
  70. static int is_full_log;
  71. static unsigned int *suggest_vol;
  72. static unsigned int suggest_vol_count;
  73. #ifdef AUTOK_THREAD
  74. /* #define msdc_dma_status() ((sdr_read32(MSDC_CFG) & MSDC_CFG_PIO) >> 3) */
  75. #define msdc_dma_on() sdr_clr_bits(MSDC_CFG, MSDC_CFG_PIO)
  76. #define msdc_dma_off() sdr_set_bits(MSDC_CFG, MSDC_CFG_PIO)
  77. #define msdc_dma_status() ((sdr_read32(MSDC_CFG) & MSDC_CFG_PIO) >> 3)
  78. /* extern void mt_cpufreq_disable(unsigned int type, bool disabled); */
  79. /* struct mmc_host *mmc; */
  80. struct sdio_autok_thread_data *p_autok_thread_data;
  81. struct task_struct *task;
  82. u32 *cur_voltage;
  83. static int fake_sdio_device(void *data);
  84. struct task_struct *task2;
  85. void autok_claim_host(struct msdc_host *host)
  86. {
  87. mmc_claim_host(host->mmc);
  88. pr_debug("[%s] msdc%d host claimed\n", __func__, host->id);
  89. }
  90. void autok_release_host(struct msdc_host *host)
  91. {
  92. mmc_release_host(host->mmc);
  93. pr_debug("[%s] msdc%d host released\n", __func__, host->id);
  94. }
  95. /* keep track of how many times it is mmapped */
  96. void autok_mmap_open(struct vm_area_struct *vma)
  97. {
  98. struct log_mmap_info *info = (struct log_mmap_info *)vma->vm_private_data;
  99. info->reference++;
  100. }
  101. void autok_mmap_close(struct vm_area_struct *vma)
  102. {
  103. struct log_mmap_info *info = (struct log_mmap_info *)vma->vm_private_data;
  104. info->reference--;
  105. }
  106. /* nopage is called the first time a memory area is accessed which is not in memory,
  107. * it does the actual mapping between kernel and user space memory
  108. */
  109. int autok_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  110. {
  111. unsigned long offset;
  112. struct log_mmap_info *dev = vma->vm_private_data;
  113. int result = VM_FAULT_SIGBUS;
  114. struct page *page;
  115. /* void *pageptr = NULL; default to "missing" */
  116. pgoff_t pgoff = vmf->pgoff;
  117. offset = (pgoff << PAGE_SHIFT) + (vma->vm_pgoff << PAGE_SHIFT);
  118. if (!dev->data) {
  119. pr_err("no data\n");
  120. return 0;
  121. }
  122. if (offset >= dev->size)
  123. return 0;
  124. /* offset >>= PAGE_SHIFT; offset is a number of pages */
  125. /* got it, now convert pointer to a struct page and increment the count */
  126. page = virt_to_page(&dev->data[offset]);
  127. get_page(page);
  128. vmf->page = page;
  129. result = 0;
  130. return result;
  131. }
  132. struct vm_operations_struct autok_mmap_vm_ops = {
  133. .open = autok_mmap_open,
  134. .close = autok_mmap_close,
  135. .fault = autok_mmap_fault,
  136. };
  137. int autok_log_mmap(struct file *filp, struct vm_area_struct *vma)
  138. {
  139. vma->vm_ops = &autok_mmap_vm_ops;
  140. vma->vm_flags |= (VM_DONTEXPAND | VM_DONTDUMP);
  141. /* assign the file private data to the vm private data */
  142. vma->vm_private_data = filp->private_data;
  143. autok_mmap_open(vma);
  144. return 0;
  145. }
  146. int autok_log_close(struct inode *inode, struct file *filp)
  147. {
  148. struct log_mmap_info *info = filp->private_data;
  149. /* obtain new memory */
  150. kfree(autok_log_info);
  151. kfree(info);
  152. filp->private_data = NULL;
  153. autok_log_info = NULL;
  154. return 0;
  155. }
  156. int autok_log_open(struct inode *inode, struct file *filp)
  157. {
  158. struct log_mmap_info *info = kmalloc(sizeof(struct log_mmap_info), GFP_KERNEL);
  159. /* obtain new memory */
  160. /* assign this info struct to the file */
  161. info->size = LOG_SIZE;
  162. info->data = autok_log_info;
  163. filp->private_data = info;
  164. return 0;
  165. }
  166. static const struct file_operations autok_log_fops = {
  167. .open = autok_log_open,
  168. .release = autok_log_close,
  169. .mmap = autok_log_mmap,
  170. };
  171. static int autok_calibration_done(int id, struct sdio_autok_thread_data *autok_thread_data)
  172. {
  173. /* int err=0; */
  174. /* char cali_done[80] = "CALI_DONE"; */
  175. if (autok_thread_data->is_autok_done[id] == 0)
  176. autok_thread_data->is_autok_done[id] = 1;
  177. /*err = send_autok_uevent(cali_done, autok_thread_data->host);
  178. if(err < 0)
  179. return err;
  180. */
  181. return 0;
  182. }
  183. #include <linux/time.h>
  184. static int autok_thread_func(void *data)
  185. {
  186. struct sdio_autok_thread_data *autok_thread_data;
  187. /* struct sched_param param = { .sched_priority = 99 }; */
  188. unsigned int vcore_uv = 0;
  189. struct msdc_host *host;
  190. struct mmc_host *mmc;
  191. char stage = 0;
  192. int i, j;
  193. int res = 0;
  194. int doStg2 = 0;
  195. void __iomem *base;
  196. u32 dma;
  197. struct timeval t0, t1;
  198. int time_in_s, time_in_ms;
  199. unsigned int hz;
  200. /* unsigned long flags; */
  201. autok_thread_data = (struct sdio_autok_thread_data *)data;
  202. /* sched_setscheduler(current, SCHED_FIFO, &param); */
  203. /* preempt_disable(); */
  204. host = autok_thread_data->host;
  205. mmc = host->mmc;
  206. stage = autok_thread_data->stage;
  207. base = host->base;
  208. dma = msdc_dma_status();
  209. /* Inform msdc_set_mclk() auto-K is going to process */
  210. sdio_autok_processed = 1;
  211. /* Set clock to card max clock */
  212. hz = mmc->ios.clock;
  213. mmc_set_clock(mmc, 50000000);
  214. mmc_set_clock(mmc, hz);
  215. msdc_sdio_set_long_timing_delay_by_freq(host, mmc->ios.clock);
  216. msdc_ungate_clock(host);
  217. /* Set PIO mode */
  218. msdc_dma_off();
  219. vcore_uv = autok_get_current_vcore_offset();
  220. /* End of initialize */
  221. do_gettimeofday(&t0);
  222. if (autok_thread_data->log != NULL)
  223. log_info = autok_thread_data->log;
  224. if (stage == 1) {
  225. /* call stage 1 auto-K callback function */
  226. autok_thread_data->is_autok_done[host->id] = 0;
  227. res = msdc_autok_stg1_cal(host, vcore_uv, autok_thread_data->p_autok_predata);
  228. if (res) {
  229. pr_err
  230. ("[%s] Auto-K stage 1 fail, res = %d, set msdc parameter settings stored in nvram to 0\n",
  231. __func__, res);
  232. memset(autok_thread_data->p_autok_predata->ai_data[0], 0,
  233. autok_thread_data->p_autok_predata->param_count *
  234. sizeof(unsigned int));
  235. autok_thread_data->is_autok_done[host->id] = 2;
  236. }
  237. } else if (stage == 2) {
  238. /* call stage 2 auto-K callback function */
  239. /* check if msdc params of different volt are all 0, if so, that means auto-K stg1 failed */
  240. for (i = 0; i < autok_thread_data->p_autok_predata->vol_count; i++) {
  241. for (j = 0; j < autok_thread_data->p_autok_predata->param_count; j++) {
  242. if (autok_thread_data->p_autok_predata->ai_data[i][j].data.sel != 0) {
  243. doStg2 = 1;
  244. break;
  245. }
  246. }
  247. if (doStg2)
  248. break;
  249. }
  250. if (doStg2) {
  251. res =
  252. msdc_autok_stg2_cal(host, autok_thread_data->p_autok_predata, vcore_uv);
  253. if (res) {
  254. pr_err
  255. ("[%s] Auto-K stage 2 fail, res = %d, downgrade SDIO freq to 50MHz\n",
  256. __func__, res);
  257. mmc->ios.clock = 50 * 1000 * 1000;
  258. mmc_set_clock(mmc, mmc->ios.clock);
  259. msdc_sdio_set_long_timing_delay_by_freq(host, mmc->ios.clock);
  260. sdio_autok_processed = 0;
  261. for (i = 0; i < HOST_MAX_NUM; i++) {
  262. if (autok_thread_data->p_autok_progress[i].host_id == -1) {
  263. break;
  264. } else if (autok_thread_data->p_autok_progress[i].host_id ==
  265. host->id) {
  266. autok_thread_data->p_autok_progress[i].fail = 1;
  267. }
  268. }
  269. }
  270. } else { /* Auto-K stg1 failed */
  271. pr_err("[%s] Auto-K stage 1 fail, downgrade SDIO freq to 50MHz\n",
  272. __func__);
  273. mmc->ios.clock = 50 * 1000 * 1000;
  274. mmc_set_clock(mmc, mmc->ios.clock);
  275. msdc_sdio_set_long_timing_delay_by_freq(host, mmc->ios.clock);
  276. sdio_autok_processed = 0;
  277. for (i = 0; i < HOST_MAX_NUM; i++) {
  278. if (autok_thread_data->p_autok_progress[i].host_id == -1) {
  279. break;
  280. } else if (autok_thread_data->p_autok_progress[i].host_id ==
  281. host->id) {
  282. autok_thread_data->p_autok_progress[i].fail = 1;
  283. }
  284. }
  285. }
  286. log_info = NULL;
  287. } else {
  288. pr_err("[%s] stage %d doesn't support in auto-K\n", __func__, stage);
  289. /* return -EFAULT; */
  290. }
  291. do_gettimeofday(&t1);
  292. if (dma == DMA_ON)
  293. msdc_dma_on();
  294. msdc_gate_clock(host, 1);
  295. /* [FIXDONE] Tell native module that auto-K has finished */
  296. if (stage == 1)
  297. autok_calibration_done(host->id, autok_thread_data);
  298. else if (stage == 2) {
  299. for (i = 0; i < HOST_MAX_NUM; i++) {
  300. if (autok_thread_data->p_autok_progress[i].host_id == -1) {
  301. break;
  302. } else if (autok_thread_data->p_autok_progress[i].host_id == host->id) {
  303. autok_thread_data->p_autok_progress[i].done = 1;
  304. if (autok_thread_data->p_autok_progress[i].done > 0)
  305. complete(&autok_thread_data->autok_completion[i]);
  306. }
  307. }
  308. }
  309. time_in_s = (t1.tv_sec - t0.tv_sec);
  310. time_in_ms = (t1.tv_usec - t0.tv_usec) >> 10;
  311. pr_debug("\n[AUTOKK][Stage%d] Timediff is %d.%d(s)\n", (int)stage, time_in_s, time_in_ms);
  312. /* preempt_enable(); */
  313. return 0;
  314. }
  315. #endif
  316. int send_autok_uevent(char *text, struct msdc_host *host)
  317. {
  318. int err = 0;
  319. char *envp[3];
  320. char *host_buf;
  321. char *what_buf;
  322. /* struct msdc_host *host = mtk_msdc_host[id]; */
  323. host_buf = kzalloc(sizeof(char) * 128, GFP_KERNEL);
  324. what_buf = kzalloc(sizeof(char) * 128, GFP_KERNEL);
  325. snprintf(host_buf, MAX_ARGS_BUF - 1, "HOST=%d", host->id);
  326. snprintf(what_buf, MAX_ARGS_BUF - 1, "WHAT=%s", text);
  327. envp[0] = host_buf;
  328. envp[1] = what_buf;
  329. envp[2] = NULL;
  330. if (host != NULL)
  331. err = kobject_uevent_env(&host->mmc->class_dev.kobj, KOBJ_CHANGE, envp);
  332. kfree(host_buf);
  333. kfree(what_buf);
  334. if (err < 0)
  335. pr_err("[%s] kobject_uevent_env error = %d\n", __func__, err);
  336. return err;
  337. }
  338. int wait_sdio_autok_ready(void *data)
  339. {
  340. int i;
  341. int ret = 0;
  342. enum boot_mode_t btmod;
  343. struct mmc_host *mmc = (struct mmc_host *)data;
  344. struct msdc_host *host = NULL;
  345. int id;
  346. unsigned int vcore_uv = 0;
  347. btmod = get_boot_mode();
  348. pr_debug("btmod = %d\n", btmod);
  349. if ((btmod != NORMAL_BOOT) && (btmod != META_BOOT)) {
  350. pr_debug("Not META or normal boot, return directly\n");
  351. return 0;
  352. }
  353. if (1 /*(btmod!=META_BOOT) && (btmod!=FACTORY_BOOT) && (btmod!=ATE_FACTORY_BOOT) */) {
  354. sdio_host_debug = 0;
  355. /* host = mtk_msdc_host[id]; */
  356. host = mmc_priv(mmc);
  357. id = host->id;
  358. #ifndef UT_TEST
  359. /* claim host */
  360. #ifdef CONFIG_SDIOAUTOK_SUPPORT
  361. /* ALPS02017456 */
  362. /* true if dwork was pending, false otherwise */
  363. if (cancel_delayed_work_sync(&(host->set_vcore_workq)) == 0)
  364. pr_debug("** no pending vcore_workq\n");
  365. else
  366. pr_debug("** cancel vcore_workq\n");
  367. /* ALPS02017456 */
  368. pr_debug("wait_sdio_autok_ready(): is_vcorefs_can_work= %d\n",
  369. is_vcorefs_can_work());
  370. if (vcorefs_request_dvfs_opp(KIR_SDIO, OPPI_PERF) != 0) { /* performance mode, return 0 pass */
  371. /* BUG_ON("vcorefs_request_dvfs_opp@OPPI_PERF fail!\n"); */
  372. pr_err("vcorefs_request_dvfs_opp@OPPI_PERF fail!\n");
  373. }
  374. g_autok_vcore_sel[0] = vcorefs_get_curr_voltage();
  375. pr_debug("wait_sdio_autok_ready(): vcorefs_get_curr_voltage= %d\n",
  376. vcorefs_get_curr_voltage());
  377. /* mt_cpufreq_disable(0, true); */
  378. /* FIXME@CCJ mt_vcore_dvfs_disable_by_sdio(0, true); */
  379. /* vcorefs_sdio_lock_dvfs(0); //ccyeh */
  380. #endif
  381. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  382. /* ccyeh atomic_set(&host->ot_work.ot_disable, 1); */
  383. #endif /* MTK_SDIO30_ONLINE_TUNING_SUPPORT */
  384. autok_claim_host(host);
  385. #endif
  386. #ifdef AUTOK_THREAD
  387. for (i = 0; i < HOST_MAX_NUM; i++) {
  388. if (p_autok_thread_data->p_autok_progress[i].host_id == id &&
  389. p_autok_thread_data->p_autok_progress[i].done == 1 &&
  390. p_autok_thread_data->p_autok_progress[i].fail == 0) {
  391. vcore_uv = autok_get_current_vcore_offset();
  392. msdc_autok_stg2_cal(host, &p_autok_predata[id], vcore_uv);
  393. break;
  394. }
  395. }
  396. if (i != HOST_MAX_NUM)
  397. goto EXIT_WAIT_AUTOK_READY;
  398. #endif
  399. for (i = 0; i < HOST_MAX_NUM; i++) {
  400. if (p_autok_thread_data->p_autok_progress[i].host_id == -1
  401. || p_autok_thread_data->p_autok_progress[i].host_id == id) {
  402. send_autok_uevent("s2_ready", host);
  403. init_completion(&p_autok_thread_data->autok_completion[i]);
  404. p_autok_thread_data->p_autok_progress[i].done = 0;
  405. p_autok_thread_data->p_autok_progress[i].fail = 0;
  406. p_autok_thread_data->p_autok_progress[i].host_id = id;
  407. wait_for_completion_interruptible
  408. (&p_autok_thread_data->autok_completion[i]);
  409. for (i = 0; i < HOST_MAX_NUM; i++) {
  410. if (p_autok_thread_data->p_autok_progress[i].host_id == id
  411. && p_autok_thread_data->p_autok_progress[i].fail == 1) {
  412. ret = -1;
  413. break;
  414. }
  415. }
  416. send_autok_uevent("s2_done", host);
  417. break;
  418. }
  419. }
  420. /* reset_autok_cursor(0); */
  421. EXIT_WAIT_AUTOK_READY:
  422. #ifndef UT_TEST
  423. /* release host */
  424. autok_release_host(host);
  425. #ifdef CONFIG_SDIOAUTOK_SUPPORT
  426. /* mt_cpufreq_disable(0, false); */
  427. /* FIXME@CCJ mt_vcore_dvfs_disable_by_sdio(0, false); */
  428. /* vcorefs_sdio_unlock_dvfs(0);//ccyeh */
  429. if (vcorefs_request_dvfs_opp(KIR_SDIO, OPPI_UNREQ) != 0) { /* un-request, return 0 pass */
  430. /* BUG_ON("vcorefs_request_dvfs_opp@OPPI_UNREQ fail!\n"); */
  431. pr_err("vcorefs_request_dvfs_opp@OPPI_UNREQ fail!\n");
  432. }
  433. #endif
  434. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  435. atomic_set(&host->ot_work.autok_done, 1);
  436. atomic_set(&host->ot_work.ot_disable, 0);
  437. #endif /* MTK_SDIO30_ONLINE_TUNING_SUPPORT */
  438. #endif
  439. }
  440. return ret;
  441. }
  442. EXPORT_SYMBOL(wait_sdio_autok_ready);
  443. void init_tune_sdio(struct msdc_host *host)
  444. {
  445. int i;
  446. for (i = 0; i < HOST_MAX_NUM; i++) {
  447. if (p_autok_thread_data->p_autok_progress[i].host_id == host->id) {
  448. if (p_autok_thread_data->p_autok_progress[i].done == 1 &&
  449. p_autok_thread_data->p_autok_progress[i].fail == 0) {
  450. u32 vcore_uv = 0;
  451. vcore_uv = autok_get_current_vcore_offset();
  452. pr_debug
  453. ("init_tune_sdio@msdc_autok_apply_param, id:%d vcore_uv:%d\n",
  454. host->id, vcore_uv);
  455. msdc_autok_apply_param(host, vcore_uv);
  456. /* wait_sdio_autok_ready(host->mmc); */
  457. }
  458. break;
  459. }
  460. }
  461. }
  462. static int print_autok(struct autok_predata *t_predata, char *buf)
  463. {
  464. int offset;
  465. int i, j;
  466. int vol_count;
  467. int param_count;
  468. int param_size;
  469. int width;
  470. /* struct autok_predata *t_predata; */
  471. char data_buf[1024] = "";
  472. /* char *temp_ch; */
  473. offset = 0;
  474. /* t_predata = &p_autok_predata[id]; */
  475. vol_count = t_predata->vol_count;
  476. param_count = t_predata->param_count;
  477. param_size = param_count * sizeof(U_AUTOK_INTERFACE_DATA);
  478. /* len = snprintf(data_buf + count, 32, "%02x", vol_count); */
  479. /* count += len; */
  480. /* len = snprintf(data_buf + count, 32, "%02x", param_count); */
  481. /* count += len; */
  482. data_buf[0] = vol_count;
  483. data_buf[1] = param_count;
  484. offset += 2;
  485. /* temp_ch = (char*)&t_predata->vol_list[0]; */
  486. width = sizeof(unsigned int) / sizeof(char);
  487. for (i = 0; i < vol_count; i++) {
  488. /* len = snprintf(data_buf + count, 32, "%02x", temp_ch[i]); */
  489. /* count += len; */
  490. memcpy(data_buf + offset + i * width, &t_predata->vol_list[i], width);
  491. }
  492. offset += vol_count * width;
  493. width = sizeof(U_AUTOK_INTERFACE_DATA) / sizeof(char);
  494. for (i = 0; i < vol_count; i++) {
  495. /* temp_ch = (char*)&t_predata->ai_data[i][0]; */
  496. for (j = 0; j < param_count; j++) {
  497. /* len = snprintf(data_buf + offset, 32, "%02x", temp_ch[j]); */
  498. /* offset += len; */
  499. /* data_buf[offset+j] = temp_ch[j]; */
  500. memcpy(data_buf + offset + j * width, &t_predata->ai_data[i][j], width);
  501. }
  502. offset += param_count * width;
  503. }
  504. /* len = snprintf(buf, 2048, "%s", data_buf); */
  505. memcpy(buf, data_buf, offset);
  506. return offset;
  507. }
  508. static int store_autok(struct autok_predata *t_predata, const char *buf, int count)
  509. {
  510. U_AUTOK_INTERFACE_DATA **ai_data;
  511. unsigned int *vollist;
  512. int param_size, offset, i;
  513. int width;
  514. int vol_count;
  515. int param_count;
  516. offset = 0;
  517. vol_count = buf[0];
  518. param_count = buf[1];
  519. offset += 2;
  520. vollist = kcalloc(vol_count, sizeof(unsigned int), GFP_KERNEL);
  521. width = sizeof(unsigned int) / sizeof(char);
  522. param_size = param_count * sizeof(U_AUTOK_INTERFACE_DATA);
  523. for (i = 0; i < vol_count; i++) {
  524. vollist[i] = *((unsigned int *)(&buf[offset]));
  525. offset += width;
  526. }
  527. /* if(vol_count*param_size + offset > count) */
  528. /* return count; */
  529. ai_data = kcalloc(vol_count, sizeof(U_AUTOK_INTERFACE_DATA *), GFP_KERNEL);
  530. for (i = 0; i < vol_count; i++) {
  531. ai_data[i] = kzalloc(param_size, GFP_KERNEL);
  532. memcpy(ai_data[i], &buf[offset], param_size);
  533. offset += param_size;
  534. }
  535. t_predata->vol_count = vol_count;
  536. t_predata->param_count = param_count;
  537. t_predata->ai_data = ai_data;
  538. t_predata->vol_list = vollist;
  539. return 0;
  540. }
  541. static ssize_t stage1_store(struct kobject *kobj, struct kobj_attribute *attr,
  542. const char *buf, size_t count)
  543. {
  544. char cur_name[DEVNAME_SIZE] = "";
  545. int test_len, cur_len;
  546. int i, j;
  547. int id;
  548. int select;
  549. struct msdc_host *host;
  550. int ret;
  551. /* char *p_log; */
  552. /* id = 3; */
  553. select = -1;
  554. /* sscanf(kobj->name, "%d", &id); */
  555. kstrtoint(kobj->name, 0, &id);
  556. if (id >= HOST_MAX_NUM) {
  557. pr_err("[%s] id<%d> is bigger than HOST_MAX_NUM<%d>\n", __func__, id, HOST_MAX_NUM);
  558. return count;
  559. }
  560. host = mtk_msdc_host[id];
  561. /* sscanf(attr->attr.name, "%s", cur_name); */
  562. strncpy(cur_name, attr->attr.name, DEVNAME_SIZE);
  563. for (i = 0; i < TOTAL_STAGE1_NODE_COUNT; i++) {
  564. test_len = strlen(stage1_nodes[i]);
  565. cur_len = strlen(cur_name);
  566. if ((test_len == cur_len) && (strncmp(stage1_nodes[i], cur_name, cur_len) == 0)) {
  567. select = i;
  568. break;
  569. }
  570. }
  571. switch (select) {
  572. case VOLTAGE:
  573. /* sscanf(buf, "%u", &cur_voltage[id]); */
  574. kstrtou32(buf, 0, &cur_voltage[id]);
  575. break;
  576. case PARAMS:
  577. memset(cur_name, 0, DEVNAME_SIZE);
  578. cur_name[0] = 1;
  579. cur_name[1] = E_AUTOK_PARM_MAX;
  580. memcpy(&cur_name[2], &cur_voltage[id], sizeof(unsigned int));
  581. store_autok(&p_single_autok[id], cur_name, count);
  582. pr_debug("[AUTOKD] Enter Store Autok");
  583. pr_debug("[AUTOKD] p_single_autok[%d].vol_count=%d", id,
  584. p_single_autok[id].vol_count);
  585. pr_debug("[AUTOKD] p_single_autok[%d].param_count=%d", id,
  586. p_single_autok[id].param_count);
  587. for (i = 0; i < p_single_autok[id].vol_count; i++) {
  588. pr_debug("[AUTOKD] p_single_autok[%d].vol_list[%d]=%d", id, i,
  589. p_single_autok[id].vol_list[i]);
  590. }
  591. for (i = 0; i < p_single_autok[id].vol_count; i++) {
  592. for (j = 0; j < p_single_autok[id].param_count; j++)
  593. pr_debug("[AUTOKD] p_single_autok[%d].ai_data[%d][%d]=%d", id, i, j,
  594. p_single_autok[id].ai_data[i][j].data.sel);
  595. }
  596. /* [FIXDONE] Start to do autok alforithm; data is in p_single_autok */
  597. #ifdef UT_TEST
  598. #error "remove me"
  599. if (is_first_stage1 == 1) {
  600. /* claim host */
  601. #ifdef CONFIG_SDIOAUTOK_SUPPORT
  602. /* mt_cpufreq_disable(0, true); */
  603. /* FIXME@CCJ mt_vcore_dvfs_disable_by_sdio(0, true); */
  604. #endif
  605. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  606. atomic_set(&host->ot_work.ot_disable, 1);
  607. #endif /* MTK_SDIO30_ONLINE_TUNING_SUPPORT */
  608. autok_claim_host(host);
  609. is_first_stage1 = 0;
  610. }
  611. #endif
  612. #ifdef AUTOK_THREAD
  613. p_autok_thread_data->host = host;
  614. p_autok_thread_data->stage = 1;
  615. p_autok_thread_data->p_autok_predata = &p_single_autok[id];
  616. p_autok_thread_data->log = autok_log_info;
  617. task = kthread_run(&autok_thread_func, (void *)(p_autok_thread_data), "autokp");
  618. #endif
  619. break;
  620. case DONE:
  621. /* sscanf(buf, "%d", &i); */
  622. kstrtoint(buf, 0, &i);
  623. p_autok_thread_data->is_autok_done[id] = (u8) i;
  624. break;
  625. case LOG:
  626. /* sscanf(buf, "%d", &i); */
  627. kstrtoint(buf, 0, &i);
  628. if (is_full_log != i) {
  629. is_full_log = i;
  630. if (i == 0) {
  631. debugfs_remove(autok_log_entry);
  632. /* kfree(autok_log_info); */
  633. } else {
  634. autok_log_entry =
  635. debugfs_create_file("autok_log", 0660, NULL, NULL,
  636. &autok_log_fops);
  637. i_gid_write(autok_log_entry->d_inode, 1000);
  638. autok_log_info = kzalloc(LOG_SIZE, GFP_KERNEL);
  639. total_msg_size = 0; /* rewind the counter to 0 when allocate new buffer */
  640. }
  641. }
  642. break;
  643. default:
  644. break;
  645. }
  646. return count;
  647. }
  648. static ssize_t stage1_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  649. {
  650. char cur_name[DEVNAME_SIZE] = "";
  651. char data_buf[1024] = "";
  652. int test_len, cur_len;
  653. int i, j;
  654. int id;
  655. int len, count = 0;
  656. int select;
  657. struct msdc_host *host;
  658. /* char *p_log; */
  659. /* id = 3; */
  660. /* sscanf(kobj->name, "%d", &id); */
  661. kstrtoint(kobj->name, 0, &id);
  662. if (id >= HOST_MAX_NUM) {
  663. pr_err("[%s] id<%d> is bigger than HOST_MAX_NUM<%d>\n", __func__, id, HOST_MAX_NUM);
  664. return count;
  665. }
  666. host = mtk_msdc_host[id];
  667. /* sscanf(attr->attr.name, "%s", cur_name); */
  668. strncpy(cur_name, attr->attr.name, DEVNAME_SIZE);
  669. select = 0;
  670. for (i = 0; i < TOTAL_STAGE1_NODE_COUNT; i++) {
  671. test_len = strlen(stage1_nodes[i]);
  672. cur_len = strlen(cur_name);
  673. if ((test_len == cur_len) && (strncmp(stage1_nodes[i], cur_name, cur_len) == 0)) {
  674. select = i;
  675. break;
  676. }
  677. }
  678. len = 0;
  679. count = 0;
  680. switch (select) {
  681. case VOLTAGE:
  682. len = snprintf(buf, 32, "%d\n", cur_voltage[id]);
  683. break;
  684. case PARAMS:
  685. if (!sdio_host_debug) {
  686. len = print_autok(&p_single_autok[id], (char *)buf);
  687. } else {
  688. len = snprintf(data_buf + count, 32, "\nDetail Information\n");
  689. count += len;
  690. len =
  691. snprintf(data_buf + count, 32, "vol_count:%d\n",
  692. p_single_autok[id].vol_count);
  693. count += len;
  694. len =
  695. snprintf(data_buf + count, 32, "param_count:%d\n",
  696. p_single_autok[id].param_count);
  697. count += len;
  698. for (i = 0; i < p_single_autok[id].vol_count; i++) {
  699. len =
  700. snprintf(data_buf + count, 32, "vol[%d]:%d\n", i,
  701. p_single_autok[id].vol_list[i]);
  702. count += len;
  703. }
  704. /* goto EXIT_STAGE2; */
  705. for (i = 0; i < p_single_autok[id].vol_count; i++) {
  706. for (j = 0; j < p_single_autok[id].param_count; j++) {
  707. len =
  708. snprintf(data_buf + count, 32, "param[%d][%d]:%d\n", i,
  709. j, p_single_autok[id].ai_data[i][j].data.sel);
  710. count += len;
  711. }
  712. }
  713. len = snprintf(buf, 1024, "%s\n", data_buf);
  714. }
  715. break;
  716. case DONE:
  717. len = snprintf(buf, 32, "%d\n", p_autok_thread_data->is_autok_done[id]);
  718. break;
  719. case LOG:
  720. /* if(autok_log_info != NULL) */
  721. len = snprintf(buf, 32, "%d", is_full_log);
  722. break;
  723. }
  724. return len;
  725. }
  726. static int create_stage1_nodes(int size, struct kobject *parent_kobj)
  727. {
  728. #define NODE_COUNT (TOTAL_STAGE1_NODE_COUNT)
  729. int i, j;
  730. int retval = 0;
  731. char *name;
  732. stage1_attrs = kzalloc(NODE_COUNT * size * sizeof(struct attribute *), GFP_KERNEL);
  733. stage1_kattr = kzalloc(NODE_COUNT * size * sizeof(struct kobj_attribute *), GFP_KERNEL);
  734. s1_host_kobj = kcalloc(size, sizeof(struct kobject *), GFP_KERNEL);
  735. for (i = 0; i < size; i++) {
  736. name = kzalloc(DEVNAME_SIZE, GFP_KERNEL);
  737. sprintf(name, "%d", i);
  738. s1_host_kobj[i] = kobject_create_and_add(name, parent_kobj);
  739. if (s1_host_kobj[i] == NULL)
  740. return -ENOMEM;
  741. for (j = 0; j < NODE_COUNT; j++) {
  742. stage1_attrs[i * NODE_COUNT + j] =
  743. kzalloc(sizeof(struct attribute), GFP_KERNEL);
  744. stage1_kattr[i * NODE_COUNT + j] =
  745. kzalloc(sizeof(struct kobj_attribute), GFP_KERNEL);
  746. /* name = kzalloc(DEVNAME_SIZE, GFP_KERNEL); */
  747. /* sprintf(name, "%s", args_type[j]); */
  748. stage1_attrs[i * NODE_COUNT + j]->name = stage1_nodes[j];
  749. stage1_attrs[i * NODE_COUNT + j]->mode = 0660;
  750. stage1_kattr[i * NODE_COUNT + j]->attr = *stage1_attrs[i * NODE_COUNT + j];
  751. stage1_kattr[i * NODE_COUNT + j]->show = stage1_show;
  752. stage1_kattr[i * NODE_COUNT + j]->store = stage1_store;
  753. sysfs_attr_init(&stage1_kattr[i * NODE_COUNT + j]->attr);
  754. retval =
  755. sysfs_create_file(s1_host_kobj[i],
  756. &stage1_kattr[i * NODE_COUNT + j]->attr);
  757. if (retval)
  758. kobject_put(s1_host_kobj[i]);
  759. }
  760. }
  761. p_autok_thread_data->is_autok_done = kcalloc(size, sizeof(u8), GFP_KERNEL);
  762. cur_voltage = kcalloc(size, sizeof(u32), GFP_KERNEL);
  763. p_single_autok = kcalloc(size, sizeof(struct autok_predata), GFP_KERNEL);
  764. return 0;
  765. }
  766. static ssize_t stage2_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  767. {
  768. int id;
  769. int i, j;
  770. int count;
  771. char data_buf[1024] = "";
  772. int len = 0;
  773. id = 2;
  774. count = 0;
  775. /* sscanf(attr->attr.name, "%d", &id); */
  776. kstrtoint(attr->attr.name, 0, &id);
  777. /* =========================================================================// */
  778. if (!sdio_host_debug) {
  779. len = print_autok(&p_autok_predata[id], (char *)buf);
  780. count += len;
  781. } else {
  782. len = snprintf(data_buf + count, 32, "\nDetail Information\n");
  783. count += len;
  784. len =
  785. snprintf(data_buf + count, 32, "vol_count:%d\n", p_autok_predata[id].vol_count);
  786. count += len;
  787. len =
  788. snprintf(data_buf + count, 32, "param_count:%d\n",
  789. p_autok_predata[id].param_count);
  790. count += len;
  791. for (i = 0; i < p_autok_predata[id].vol_count; i++) {
  792. len =
  793. snprintf(data_buf + count, 32, "vol[%d]:%d\n", i,
  794. p_autok_predata[id].vol_list[i]);
  795. count += len;
  796. }
  797. /* goto EXIT_STAGE2; */
  798. for (i = 0; i < p_autok_predata[id].vol_count; i++) {
  799. for (j = 0; j < p_autok_predata[id].param_count; j++) {
  800. len =
  801. snprintf(data_buf + count, 32, "param[%d][%d]:%d\n", i, j,
  802. p_autok_predata[id].ai_data[i][j].data.sel);
  803. count += len;
  804. }
  805. }
  806. len = snprintf(buf, 1024, "%s\n", data_buf);
  807. }
  808. return len;
  809. }
  810. static ssize_t stage2_store(struct kobject *kobj, struct kobj_attribute *attr,
  811. const char *buf, size_t count)
  812. {
  813. int id;
  814. struct msdc_host *host;
  815. /* id = 3; */
  816. /* sscanf(attr->attr.name, "%d", &id); */
  817. kstrtoint(attr->attr.name, 0, &id);
  818. if (id >= HOST_MAX_NUM) {
  819. pr_err("[%s] id<%d> is bigger than HOST_MAX_NUM<%d>\n", __func__, id, HOST_MAX_NUM);
  820. return count;
  821. }
  822. host = mtk_msdc_host[id];
  823. if (count > 2) {
  824. store_autok(&p_autok_predata[id], buf, count);
  825. /* [FIXDONE] Hook the function to apply parameter to respective controller */
  826. #ifdef AUTOK_THREAD
  827. p_autok_thread_data->host = host;
  828. p_autok_thread_data->stage = 2;
  829. p_autok_thread_data->p_autok_predata = &p_autok_predata[id];
  830. p_autok_thread_data->log = autok_log_info;
  831. task = kthread_run(&autok_thread_func, (void *)(p_autok_thread_data), "autokp");
  832. /* autok_thread_func((void *)(p_autok_thread_data)); */
  833. /* wake_up_process(task); */
  834. #endif
  835. #ifdef UT_TEST
  836. #error "remove me"
  837. if (is_first_stage1 == 0) {
  838. /* release host */
  839. autok_release_host(host);
  840. #ifdef CONFIG_SDIOAUTOK_SUPPORT
  841. /* mt_cpufreq_disable(0, false); */
  842. /* FIXME@CCJ mt_vcore_dvfs_disable_by_sdio(0, false); */
  843. #endif
  844. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  845. atomic_set(&host->ot_work.autok_done, 1);
  846. atomic_set(&host->ot_work.ot_disable, 0);
  847. #endif /* MTK_SDIO30_ONLINE_TUNING_SUPPORT */
  848. is_first_stage1 = 1;
  849. }
  850. #endif
  851. }
  852. return count;
  853. }
  854. static int create_stage2_node(int size, struct kobject *parent_kobj)
  855. {
  856. int i;
  857. int retval = 0;
  858. char *name;
  859. stage2_attrs = kcalloc(size, sizeof(struct attribute *), GFP_KERNEL);
  860. stage2_kattr = kcalloc(size, sizeof(struct kobj_attribute *), GFP_KERNEL);
  861. for (i = 0; i < size; i++) {
  862. name = kzalloc(DEVNAME_SIZE, GFP_KERNEL);
  863. sprintf(name, "%d", i);
  864. stage2_attrs[i] = kzalloc(sizeof(struct attribute), GFP_KERNEL);
  865. stage2_kattr[i] = kzalloc(sizeof(struct kobj_attribute), GFP_KERNEL);
  866. stage2_attrs[i]->name = name;
  867. stage2_attrs[i]->mode = 0660;
  868. stage2_kattr[i]->attr = *stage2_attrs[i];
  869. stage2_kattr[i]->show = stage2_show;
  870. stage2_kattr[i]->store = stage2_store;
  871. sysfs_attr_init(&stage2_kattr[i]->attr);
  872. retval = sysfs_create_file(parent_kobj, &stage2_kattr[i]->attr);
  873. if (retval)
  874. kobject_put(parent_kobj);
  875. }
  876. p_autok_predata = kcalloc(size, sizeof(struct autok_predata), GFP_KERNEL);
  877. return 0;
  878. }
  879. static ssize_t host_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  880. {
  881. int len, count;
  882. int i;
  883. int select;
  884. int test_len, cur_len;
  885. char cur_name[80];
  886. char data_buf[1024] = "";
  887. select = 0;
  888. /* sscanf(attr->attr.name, "%s", cur_name); */
  889. strncpy(cur_name, attr->attr.name, 80);
  890. for (i = 0; i < TOTAL_HOST_NODE_COUNT; i++) {
  891. test_len = strlen(host_nodes[i]);
  892. cur_len = strlen(cur_name);
  893. if ((test_len == cur_len) && (strncmp(host_nodes[i], cur_name, cur_len) == 0)) {
  894. select = i;
  895. break;
  896. }
  897. }
  898. len = 0;
  899. count = 0;
  900. switch (select) {
  901. case READY:
  902. for (i = 0; i < HOST_MAX_NUM; i++) {
  903. if (p_autok_thread_data->p_autok_progress[i].host_id == -1)
  904. break;
  905. len =
  906. snprintf(data_buf + count, 32, "%d:%d\t[msdc_id:progress]\n",
  907. p_autok_thread_data->p_autok_progress[i].host_id,
  908. p_autok_thread_data->p_autok_progress[i].done);
  909. count += len;
  910. }
  911. break;
  912. case PARAM_COUNT:
  913. len = snprintf(data_buf, 32, "%d", E_AUTOK_PARM_MAX);
  914. break;
  915. case DEBUG:
  916. len = snprintf(data_buf, 32, "%d", sdio_host_debug);
  917. break;
  918. case SS_CORNER:
  919. len = snprintf(data_buf, 32, "%d", 0);
  920. break;
  921. case SUGGEST_VOL:
  922. if (suggest_vol != NULL) {
  923. cur_len = 0;
  924. memset(data_buf, 0, 1024);
  925. for (i = 0; i < suggest_vol_count; i++) {
  926. test_len = snprintf(data_buf + cur_len, 32, "%u:", suggest_vol[i]);
  927. cur_len += test_len;
  928. }
  929. }
  930. len = snprintf(buf, PAGE_SIZE, "%s", data_buf);
  931. break;
  932. }
  933. len = snprintf(buf, 1024, "%s", data_buf);
  934. return len;
  935. }
  936. static volatile int id_data;
  937. static ssize_t host_store(struct kobject *kobj, struct kobj_attribute *attr,
  938. const char *buf, size_t count)
  939. {
  940. int id;
  941. int i;
  942. int select;
  943. int test_len, cur_len;
  944. char cur_name[80];
  945. select = 0;
  946. /* struct msdc_host *host; */
  947. /* sscanf(attr->attr.name, "%s", cur_name); */
  948. strncpy(cur_name, attr->attr.name, 80);
  949. for (i = 0; i < TOTAL_HOST_NODE_COUNT; i++) {
  950. test_len = strlen(host_nodes[i]);
  951. cur_len = strlen(cur_name);
  952. if ((test_len == cur_len) && (strncmp(host_nodes[i], cur_name, cur_len) == 0)) {
  953. select = i;
  954. break;
  955. }
  956. }
  957. switch (select) {
  958. case READY:
  959. /* sscanf(buf, "%d", &id); */
  960. kstrtoint(buf, 0, &id);
  961. for (i = 0; i < HOST_MAX_NUM; i++) {
  962. if (p_autok_thread_data->p_autok_progress[i].host_id == -1) {
  963. /* p_autok_thread_data->p_autok_progress[i] = id; */
  964. task2 = kthread_run(&fake_sdio_device, (void *)&id, "fake_lte");
  965. break;
  966. } else if (p_autok_thread_data->p_autok_progress[i].host_id == id) {
  967. p_autok_thread_data->p_autok_progress[i].done ^= 1;
  968. if (p_autok_thread_data->p_autok_progress[i].done == 1) {
  969. if (p_autok_thread_data->autok_completion[i].done > 0)
  970. complete(&p_autok_thread_data->autok_completion[i]);
  971. } else {
  972. task2 =
  973. kthread_run(&fake_sdio_device, (void *)&id, "fake_lte");
  974. }
  975. break;
  976. }
  977. }
  978. break;
  979. case DEBUG:
  980. /* sscanf(buf, "%d", &i); */
  981. kstrtoint(buf, 0, &i);
  982. sdio_host_debug = i;
  983. break;
  984. case SS_CORNER:
  985. case PARAM_COUNT:
  986. case SUGGEST_VOL:
  987. default:
  988. break;
  989. }
  990. return count;
  991. }
  992. static int create_host_node(struct kobject *parent_kobj)
  993. {
  994. int i;
  995. int retval = 0;
  996. host_attrs = kzalloc(sizeof(struct attribute *), GFP_KERNEL);
  997. host_kattr = kzalloc(sizeof(struct kobj_attribute *), GFP_KERNEL);
  998. /* name = kzalloc(DEVNAME_SIZE, GFP_KERNEL); */
  999. for (i = 0; i < (sizeof(host_nodes) / sizeof(host_nodes[0])); i++) {
  1000. host_attrs[i] = kzalloc(sizeof(struct attribute), GFP_KERNEL);
  1001. host_kattr[i] = kzalloc(sizeof(struct kobj_attribute), GFP_KERNEL);
  1002. host_attrs[i]->name = host_nodes[i];
  1003. host_attrs[i]->mode = 0660;
  1004. host_kattr[i]->attr = *host_attrs[i];
  1005. host_kattr[i]->show = host_show;
  1006. host_kattr[i]->store = host_store;
  1007. sysfs_attr_init(&host_kattr[i]->attr);
  1008. retval = sysfs_create_file(parent_kobj, &host_kattr[i]->attr);
  1009. }
  1010. p_autok_thread_data->p_autok_progress =
  1011. kzalloc(sizeof(struct autok_progress) * HOST_MAX_NUM, GFP_KERNEL);
  1012. p_autok_thread_data->autok_completion =
  1013. kzalloc(sizeof(struct completion) * HOST_MAX_NUM, GFP_KERNEL);
  1014. for (i = 0; i < HOST_MAX_NUM; i++) {
  1015. p_autok_thread_data->p_autok_progress[i].host_id = -1;
  1016. p_autok_thread_data->p_autok_progress[i].done = 0;
  1017. p_autok_thread_data->p_autok_progress[i].fail = 0;
  1018. init_completion(&p_autok_thread_data->autok_completion[i]);
  1019. }
  1020. return retval;
  1021. }
  1022. struct task_struct *task2;
  1023. static int fake_sdio_device(void *data)
  1024. {
  1025. /* struct msdc_host *host = mtk_msdc_host[3]; */
  1026. struct msdc_host *host = mtk_msdc_host[2]; /* Denali use MSDC2 to connect 6630. */
  1027. wait_sdio_autok_ready(host->mmc);
  1028. return 0;
  1029. }
  1030. static int __init autok_init(void)
  1031. {
  1032. int retval = 0;
  1033. autok_log_entry = debugfs_create_file("autok_log", 0660, NULL, NULL, &autok_log_fops);
  1034. i_gid_write(autok_log_entry->d_inode, 1000);
  1035. autok_log_info = kzalloc(LOG_SIZE, GFP_KERNEL);
  1036. is_full_log = 1;
  1037. /* create node /sys/mtk_sdio */
  1038. autok_kobj = kobject_create_and_add("autok", NULL);
  1039. if (autok_kobj == NULL)
  1040. return -ENOMEM;
  1041. stage1_kobj = kobject_create_and_add("stage1", autok_kobj);
  1042. if (stage1_kobj == NULL)
  1043. return -ENOMEM;
  1044. stage2_kobj = kobject_create_and_add("stage2", autok_kobj);
  1045. if (stage2_kobj == NULL)
  1046. return -ENOMEM;
  1047. #ifdef AUTOK_THREAD
  1048. p_autok_thread_data = kzalloc(sizeof(struct sdio_autok_thread_data), GFP_KERNEL);
  1049. p_autok_thread_data->log = autok_log_info;
  1050. /* msdc_autok_apply_param_2 = msdc_autok_apply_param; */
  1051. #endif
  1052. retval = create_host_node(autok_kobj);
  1053. if (retval != 0)
  1054. return retval;
  1055. retval = create_stage1_nodes(HOST_MAX_NUM, stage1_kobj);
  1056. if (retval != 0)
  1057. return retval;
  1058. retval = create_stage2_node(HOST_MAX_NUM, stage2_kobj);
  1059. if (retval != 0)
  1060. return retval;
  1061. suggest_vol_count = msdc_autok_get_suggetst_vcore(&suggest_vol);
  1062. /* ccyeh suggest_vol_count = 1;//ccyeh FIXME */
  1063. return retval;
  1064. }
  1065. static void __exit autok_exit(void)
  1066. {
  1067. int i, j;
  1068. /* kobject_put(args_kobj); */
  1069. kobject_put(stage2_kobj);
  1070. kobject_put(stage1_kobj);
  1071. kobject_put(autok_kobj);
  1072. for (i = 0; i < HOST_MAX_NUM * TOTAL_STAGE1_NODE_COUNT; i++) {
  1073. struct attribute *attr = *(stage1_attrs + i);
  1074. /* kfree(attr->name); */
  1075. kfree(attr);
  1076. kfree(stage1_kattr[i]);
  1077. }
  1078. kfree(stage1_attrs);
  1079. kfree(stage1_kattr);
  1080. for (i = 0; i < HOST_MAX_NUM; i++)
  1081. kobject_put(s1_host_kobj[i]);
  1082. kfree(s1_host_kobj);
  1083. for (i = 0; i < HOST_MAX_NUM; i++) {
  1084. struct attribute *attr = *(stage2_attrs + i);
  1085. kfree(attr->name);
  1086. kfree(attr);
  1087. kfree(stage2_kattr[i]);
  1088. }
  1089. kfree(stage2_attrs);
  1090. kfree(stage2_kattr);
  1091. for (i = 0; i < sizeof(host_nodes) / sizeof(host_nodes[0]); i++) {
  1092. struct attribute *attr = *(host_attrs + i);
  1093. kfree(attr);
  1094. kfree(host_kattr[i]);
  1095. }
  1096. kfree(host_attrs);
  1097. kfree(host_kattr);
  1098. for (i = 0; i < HOST_MAX_NUM; i++) {
  1099. if (p_autok_predata[i].vol_count == 0 || p_autok_predata[i].vol_list == NULL)
  1100. continue;
  1101. kfree(p_autok_predata[i].vol_list);
  1102. for (j = 0; j < p_autok_predata[i].vol_count; j++)
  1103. kfree(p_autok_predata[i].ai_data[j]);
  1104. }
  1105. kfree(p_autok_predata);
  1106. for (i = 0; i < HOST_MAX_NUM; i++) {
  1107. if (p_single_autok[i].vol_count == 0 || p_single_autok[i].vol_list == NULL)
  1108. continue;
  1109. kfree(p_single_autok[i].vol_list);
  1110. for (j = 0; j < p_single_autok[i].vol_count; j++)
  1111. kfree(p_single_autok[i].ai_data[j]);
  1112. }
  1113. kfree(cur_voltage);
  1114. kfree(p_single_autok);
  1115. kfree(p_autok_thread_data->is_autok_done);
  1116. kfree(p_autok_thread_data->p_autok_progress);
  1117. kfree(p_autok_thread_data->autok_completion);
  1118. /* kthread_stop(task); */
  1119. #ifdef AUTOK_THREAD
  1120. kfree(p_autok_thread_data);
  1121. #endif
  1122. /* debugfs_remove(autok_log_entry); */
  1123. }
  1124. module_init(autok_init);
  1125. module_exit(autok_exit);
  1126. MODULE_AUTHOR("MediaTek Inc.");
  1127. MODULE_DESCRIPTION("MediaTek SDIO Auto-K Proc");
  1128. MODULE_LICENSE("GPL");