mt_gpio.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. #include <platform/mt_reg_base.h>
  32. #include <platform/mt_gpio.h>
  33. #include <platform/gpio_cfg.h>
  34. #include <debug.h>
  35. /******************************************************************************
  36. MACRO Definition
  37. ******************************************************************************/
  38. //#define GIO_SLFTEST
  39. #define GPIO_BRINGUP
  40. #define GPIO_DEVICE "mt-gpio"
  41. #define VERSION GPIO_DEVICE
  42. /*---------------------------------------------------------------------------*/
  43. #define GPIO_WR32(addr, data) DRV_WriteReg32(addr,data)
  44. #define GPIO_RD32(addr) DRV_Reg32(addr)
  45. #define GPIO_SW_SET_BITS(BIT,REG) GPIO_WR32(REG,GPIO_RD32(REG) | ((unsigned long)(BIT)))
  46. #define GPIO_SET_BITS(BIT,REG) ((*(volatile u32*)(REG)) = (u32)(BIT))
  47. #define GPIO_CLR_BITS(BIT,REG) ((*(volatile u32*)(REG)) &= ~((u32)(BIT)))
  48. /*---------------------------------------------------------------------------*/
  49. #define TRUE 1
  50. #define FALSE 0
  51. /*---------------------------------------------------------------------------*/
  52. //#define MAX_GPIO_REG_BITS 16
  53. //#define MAX_GPIO_MODE_PER_REG 5
  54. //#define GPIO_MODE_BITS 3
  55. /*---------------------------------------------------------------------------*/
  56. #define GPIOTAG "[GPIO] "
  57. #define GPIOLOG(fmt, arg...) dprintf(INFO,GPIOTAG fmt, ##arg)
  58. #define GPIOMSG(fmt, arg...) dprintf(INFO,fmt, ##arg)
  59. #define GPIOERR(fmt, arg...) dprintf(INFO,GPIOTAG "%5d: "fmt, __LINE__, ##arg)
  60. #define GPIOFUC(fmt, arg...) //dprintk(INFO,GPIOTAG "%s\n", __FUNCTION__)
  61. #define GIO_INVALID_OBJ(ptr) ((ptr) != gpio_obj)
  62. /******************************************************************************
  63. Enumeration/Structure
  64. ******************************************************************************/
  65. #if defined(MACH_FPGA)
  66. S32 mt_set_gpio_dir(u32 pin, u32 dir) {return RSUCCESS;}
  67. S32 mt_get_gpio_dir(u32 pin) {return GPIO_DIR_UNSUPPORTED;}
  68. S32 mt_set_gpio_pull_enable(u32 pin, u32 enable) {return RSUCCESS;}
  69. S32 mt_get_gpio_pull_enable(u32 pin) {return GPIO_PULL_EN_UNSUPPORTED;}
  70. S32 mt_set_gpio_pull_select(u32 pin, u32 select) {return RSUCCESS;}
  71. S32 mt_get_gpio_pull_select(u32 pin) {return GPIO_PULL_UNSUPPORTED;}
  72. S32 mt_set_gpio_smt(u32 pin, u32 enable) {return RSUCCESS;}
  73. S32 mt_get_gpio_smt(u32 pin) {return GPIO_SMT_UNSUPPORTED;}
  74. S32 mt_set_gpio_ies(u32 pin, u32 enable) {return RSUCCESS;}
  75. S32 mt_get_gpio_ies(u32 pin) {return GPIO_IES_UNSUPPORTED;}
  76. S32 mt_set_gpio_out(u32 pin, u32 output) {return RSUCCESS;}
  77. S32 mt_get_gpio_out(u32 pin) {return GPIO_OUT_UNSUPPORTED;}
  78. S32 mt_get_gpio_in(u32 pin) {return GPIO_IN_UNSUPPORTED;}
  79. S32 mt_set_gpio_mode(u32 pin, u32 mode) {return RSUCCESS;}
  80. S32 mt_get_gpio_mode(u32 pin) {return GPIO_MODE_UNSUPPORTED;}
  81. #else
  82. struct mt_gpio_obj {
  83. GPIO_REGS *reg;
  84. };
  85. static struct mt_gpio_obj gpio_dat = {
  86. .reg = (GPIO_REGS*)(GPIO_BASE),
  87. };
  88. static struct mt_gpio_obj *gpio_obj = &gpio_dat;
  89. #define REGSET (4)
  90. #define REGCLR (8)
  91. /*---------------------------------------------------------------------------*/
  92. S32 mt_set_gpio_dir_chip(u32 pin, u32 dir)
  93. {
  94. u32 bit;
  95. u32 reg;
  96. if (pin >= MAX_GPIO_PIN)
  97. return -ERINVAL;
  98. if (dir >= GPIO_DIR_MAX)
  99. return -ERINVAL;
  100. bit = DIR_offset[pin].offset;
  101. reg = GPIO_RD32(DIR_addr[pin].addr);
  102. if (dir == GPIO_DIR_IN)
  103. reg &= (~(1<<bit));
  104. else
  105. reg |= (1 << bit);
  106. GPIO_WR32(DIR_addr[pin].addr,reg);
  107. return RSUCCESS;
  108. }
  109. /*---------------------------------------------------------------------------*/
  110. S32 mt_get_gpio_dir_chip(u32 pin)
  111. {
  112. u32 pos;
  113. u32 bit;
  114. u32 reg;
  115. struct mt_gpio_obj *obj = gpio_obj;
  116. if (!obj)
  117. return -ERACCESS;
  118. if (pin >= MAX_GPIO_PIN)
  119. return -ERINVAL;
  120. pos = pin / MAX_GPIO_REG_BITS;
  121. bit = pin % MAX_GPIO_REG_BITS;
  122. reg = GPIO_RD32(&obj->reg->dir[pos].val);
  123. return (((reg & (1L << bit)) != 0)? 1: 0);
  124. }
  125. /*---------------------------------------------------------------------------*/
  126. S32 mt_set_gpio_pull_enable_chip(u32 pin, u32 enable)
  127. {
  128. u32 reg;
  129. u32 bit;
  130. if (pin >= MAX_GPIO_PIN)
  131. return -ERINVAL;
  132. /*PUPD type and PULLEN type IO abstraction*/
  133. if ((PULLEN_offset[pin].offset == -1) && (PUPD_offset[pin].offset == -1)) {
  134. return GPIO_PULL_EN_UNSUPPORTED;
  135. }
  136. if (PULLEN_offset[pin].offset != -1) {
  137. if (enable == GPIO_PULL_DISABLE)
  138. GPIO_SET_BITS((1L << (PULLEN_offset[pin].offset)), PULLEN_addr[pin].addr + REGCLR);
  139. else
  140. GPIO_SET_BITS((1L << (PULLEN_offset[pin].offset)), PULLEN_addr[pin].addr + REGSET);
  141. } else {
  142. bit = PUPD_offset[pin].offset;
  143. reg = GPIO_RD32(PUPD_addr[pin].addr);
  144. if (enable == GPIO_PULL_DISABLE) {
  145. reg &= (~(0x7 << bit)); //0x0 or 0x4 disable
  146. } else {
  147. reg |=(1 << bit);
  148. }
  149. GPIO_WR32(PUPD_addr[pin].addr, reg);
  150. }
  151. return RSUCCESS;
  152. }
  153. /*---------------------------------------------------------------------------*/
  154. S32 mt_get_gpio_pull_enable_chip(u32 pin)
  155. {
  156. unsigned long data;
  157. if (pin >= MAX_GPIO_PIN)
  158. return -ERINVAL;
  159. if (PULLEN_offset[pin].offset == -1) {
  160. return GPIO_PULL_EN_UNSUPPORTED;
  161. } else {
  162. data = GPIO_RD32(PULLEN_addr[pin].addr);
  163. return (((data & (1L << (PULLEN_offset[pin].offset))) != 0)? 1: 0);
  164. }
  165. }
  166. /*---------------------------------------------------------------------------*/
  167. S32 mt_set_gpio_smt_chip(u32 pin, u32 enable)
  168. {
  169. u32 reg=0;
  170. u32 bit=0;
  171. if (pin >= MAX_GPIO_PIN)
  172. return -ERINVAL;
  173. if (SMT_offset[pin].offset == -1) {
  174. return GPIO_SMT_UNSUPPORTED;
  175. } else {
  176. bit = SMT_offset[pin].offset;
  177. reg = GPIO_RD32(SMT_addr[pin].addr);
  178. if (enable == GPIO_SMT_DISABLE)
  179. //reg &= (~(1<<bit));
  180. GPIO_SET_BITS((1L << bit), SMT_addr[pin].addr + REGCLR);
  181. else
  182. //reg |= (1 << bit);
  183. GPIO_SET_BITS((1L << bit), SMT_addr[pin].addr + REGSET);
  184. }
  185. //GPIO_WR32(SMT_addr[pin].addr, reg);
  186. return RSUCCESS;
  187. }
  188. /*---------------------------------------------------------------------------*/
  189. S32 mt_get_gpio_smt_chip(u32 pin)
  190. {
  191. unsigned long data;
  192. u32 bit =0;
  193. bit = SMT_offset[pin].offset;
  194. if (pin >= MAX_GPIO_PIN)
  195. return -ERINVAL;
  196. if (SMT_offset[pin].offset == -1) {
  197. return GPIO_SMT_UNSUPPORTED;
  198. } else {
  199. data = GPIO_RD32(SMT_addr[pin].addr);
  200. return (((data & (1L << bit)) != 0)? 1: 0);
  201. }
  202. }
  203. /*---------------------------------------------------------------------------*/
  204. S32 mt_set_gpio_ies_chip(u32 pin, u32 enable)
  205. {
  206. //unsigned long flags;
  207. if (pin >= MAX_GPIO_PIN)
  208. return -ERINVAL;
  209. if (IES_offset[pin].offset == -1) {
  210. return GPIO_IES_UNSUPPORTED;
  211. } else {
  212. if (enable == GPIO_IES_DISABLE)
  213. GPIO_SET_BITS((1L << (IES_offset[pin].offset)), IES_addr[pin].addr + REGCLR);
  214. else
  215. GPIO_SET_BITS((1L << (IES_offset[pin].offset)), IES_addr[pin].addr + REGSET);
  216. }
  217. return RSUCCESS;
  218. }
  219. /*---------------------------------------------------------------------------*/
  220. S32 mt_get_gpio_ies_chip(u32 pin)
  221. {
  222. unsigned long data;
  223. if (pin >= MAX_GPIO_PIN)
  224. return -ERINVAL;
  225. if (IES_offset[pin].offset == -1) {
  226. return GPIO_IES_UNSUPPORTED;
  227. } else {
  228. data = GPIO_RD32(IES_addr[pin].addr);
  229. return (((data & (1L << (IES_offset[pin].offset))) != 0)? 1: 0);
  230. }
  231. }
  232. /*---------------------------------------------------------------------------*/
  233. S32 mt_set_gpio_pull_select_chip(u32 pin, u32 select)
  234. {
  235. //unsigned long flags;
  236. if (pin >= MAX_GPIO_PIN)
  237. return -ERINVAL;
  238. if ((PULLSEL_offset[pin].offset == -1)&& (PUPD_offset[pin].offset == -1)) {
  239. return GPIO_PULL_UNSUPPORTED;
  240. } else if (PULLSEL_offset[pin].offset == -1) {
  241. //spin_lock_irqsave(&mtk_gpio_lock, flags);
  242. /*
  243. * SIM1, SIM2 may need special care for resistance selection
  244. */
  245. if (select == GPIO_PULL_DOWN) {
  246. GPIO_SET_BITS((1L << (PUPD_offset[pin].offset+2)), PUPD_addr[pin].addr + REGSET);
  247. } else {
  248. GPIO_SET_BITS((1L << (PUPD_offset[pin].offset+2)), PUPD_addr[pin].addr + REGCLR);
  249. }
  250. //spin_unlock_irqrestore(&mtk_gpio_lock, flags);
  251. } else {
  252. if (select == GPIO_PULL_DOWN)
  253. GPIO_SET_BITS((1L << (PULLSEL_offset[pin].offset)), PULLSEL_addr[pin].addr + REGCLR);
  254. else
  255. GPIO_SET_BITS((1L << (PULLSEL_offset[pin].offset)), PULLSEL_addr[pin].addr + REGSET);
  256. }
  257. return RSUCCESS;
  258. }
  259. /*---------------------------------------------------------------------------*/
  260. S32 mt_get_gpio_pull_select_chip(u32 pin)
  261. {
  262. unsigned long data;
  263. if (pin >= MAX_GPIO_PIN)
  264. return -ERINVAL;
  265. if ((PULLSEL_offset[pin].offset == -1) && (PUPD_offset[pin].offset == -1)) {
  266. return GPIO_PULL_UNSUPPORTED;
  267. } else if (PULLSEL_offset[pin].offset == -1) {
  268. data = GPIO_RD32(PUPD_addr[pin].addr);
  269. return (((data & (1L << (PUPD_offset[pin].offset+2))) != 0)? 0: 1);
  270. } else {
  271. data = GPIO_RD32(PULLSEL_addr[pin].addr);
  272. return (((data & (1L << (PULLSEL_offset[pin].offset))) != 0)? 1: 0);
  273. }
  274. }
  275. /*---------------------------------------------------------------------------*/
  276. S32 mt_set_gpio_out_chip(u32 pin, u32 output)
  277. {
  278. u32 pos;
  279. u32 bit;
  280. u32 reg=0;
  281. //struct mt_gpio_obj *obj = gpio_obj;
  282. if (pin >= MAX_GPIO_PIN)
  283. return -ERINVAL;
  284. if (output >= GPIO_OUT_MAX)
  285. return -ERINVAL;
  286. bit = DATAOUT_offset[pin].offset;
  287. reg = GPIO_RD32(DATAOUT_addr[pin].addr);
  288. if (output == GPIO_OUT_ZERO)
  289. reg &= (~(1<<bit));
  290. else
  291. reg |= (1 << bit);
  292. GPIO_WR32(DATAOUT_addr[pin].addr,reg);
  293. return RSUCCESS;
  294. }
  295. /*---------------------------------------------------------------------------*/
  296. S32 mt_get_gpio_out_chip(u32 pin)
  297. {
  298. u32 bit;
  299. u32 reg;
  300. if (pin >= MAX_GPIO_PIN)
  301. return -ERINVAL;
  302. bit = DATAOUT_offset[pin].offset;
  303. reg = GPIO_RD32(DATAOUT_addr[pin].addr);
  304. return (((reg & (1L << bit)) != 0)? 1: 0);
  305. }
  306. /*---------------------------------------------------------------------------*/
  307. S32 mt_get_gpio_in_chip(u32 pin)
  308. {
  309. u32 pos;
  310. u32 bit;
  311. u32 reg;
  312. struct mt_gpio_obj *obj = gpio_obj;
  313. if (!obj)
  314. return -ERACCESS;
  315. if (pin >= MAX_GPIO_PIN)
  316. return -ERINVAL;
  317. pos = pin / MAX_GPIO_REG_BITS;
  318. bit = pin % MAX_GPIO_REG_BITS;
  319. reg = GPIO_RD32(&obj->reg->din[pos].val);
  320. return (((reg & (1L << bit)) != 0)? 1: 0);
  321. }
  322. /*---------------------------------------------------------------------------*/
  323. S32 mt_set_gpio_mode_chip(u32 pin, u32 mode)
  324. {
  325. u32 pos;
  326. u32 bit;
  327. u32 reg;
  328. struct mt_gpio_obj *obj = gpio_obj;
  329. if (!obj)
  330. return -ERACCESS;
  331. if (pin >= MAX_GPIO_PIN)
  332. return -ERINVAL;
  333. if (mode >= GPIO_MODE_MAX)
  334. return -ERINVAL;
  335. #ifdef MODE_MWR
  336. /*
  337. * if MWR is supported, no need to read before write.
  338. * MWR[3:0] has 4 bits not 3 bits ; additional 1 bit,MWR[3], is for update enable
  339. */
  340. reg = 0;
  341. pos = pin / MAX_GPIO_MODE_PER_REG;
  342. bit = pin % MAX_GPIO_MODE_PER_REG;
  343. reg = ((1L << (GPIO_MODE_BITS*bit)) << 3) | (mode << (GPIO_MODE_BITS*bit));
  344. GPIO_WR32(&obj->reg->mode[pos]._align1, reg);
  345. #else
  346. /* There is no mwr register for simple register setting
  347. * Need 1R+1W to set MODE registers
  348. */
  349. u32 mask = (1L << GPIO_MODE_BITS) - 1;
  350. bit = MODE_offset[pin].offset;
  351. mode = mode & mask;
  352. reg = GPIO_RD32(MODE_addr[pin].addr);
  353. reg &= (~(mask << bit));
  354. reg |= (mode << bit);
  355. GPIO_WR32( MODE_addr[pin].addr,reg);
  356. #endif
  357. return RSUCCESS;
  358. }
  359. /*---------------------------------------------------------------------------*/
  360. S32 mt_get_gpio_mode_chip(u32 pin)
  361. {
  362. u32 bit;
  363. u32 reg;
  364. u32 mask = (1L << GPIO_MODE_BITS) - 1;
  365. if (pin >= MAX_GPIO_PIN)
  366. return -ERINVAL;
  367. bit = MODE_offset[pin].offset;
  368. reg = GPIO_RD32(MODE_addr[pin].addr);
  369. return ((reg >> bit) & mask);
  370. }
  371. /*---------------------------------------------------------------------------*/
  372. void mt_gpio_pin_decrypt(u32 *cipher)
  373. {
  374. //just for debug, find out who used pin number directly
  375. if ((*cipher & (0x80000000)) == 0) {
  376. GPIOERR("GPIO%u HARDCODE warning!!! \n",(unsigned int)*cipher);
  377. //dump_stack();
  378. //return;
  379. }
  380. //GPIOERR("Pin magic number is %x\n",*cipher);
  381. *cipher &= ~(0x80000000);
  382. return;
  383. }
  384. //set GPIO function in fact
  385. /*---------------------------------------------------------------------------*/
  386. S32 mt_set_gpio_dir(u32 pin, u32 dir)
  387. {
  388. mt_gpio_pin_decrypt(&pin);
  389. return mt_set_gpio_dir_chip(pin,dir);
  390. }
  391. /*---------------------------------------------------------------------------*/
  392. S32 mt_get_gpio_dir(u32 pin)
  393. {
  394. mt_gpio_pin_decrypt(&pin);
  395. return mt_get_gpio_dir_chip(pin);
  396. }
  397. /*---------------------------------------------------------------------------*/
  398. S32 mt_set_gpio_pull_enable(u32 pin, u32 enable)
  399. {
  400. mt_gpio_pin_decrypt(&pin);
  401. return mt_set_gpio_pull_enable_chip(pin,enable);
  402. }
  403. /*---------------------------------------------------------------------------*/
  404. S32 mt_get_gpio_pull_enable(u32 pin)
  405. {
  406. mt_gpio_pin_decrypt(&pin);
  407. return mt_get_gpio_pull_enable_chip(pin);
  408. }
  409. /*---------------------------------------------------------------------------*/
  410. S32 mt_set_gpio_pull_select(u32 pin, u32 select)
  411. {
  412. mt_gpio_pin_decrypt(&pin);
  413. return mt_set_gpio_pull_select_chip(pin,select);
  414. }
  415. /*---------------------------------------------------------------------------*/
  416. S32 mt_get_gpio_pull_select(u32 pin)
  417. {
  418. mt_gpio_pin_decrypt(&pin);
  419. return mt_get_gpio_pull_select_chip(pin);
  420. }
  421. /*---------------------------------------------------------------------------*/
  422. S32 mt_set_gpio_smt(u32 pin, u32 enable)
  423. {
  424. mt_gpio_pin_decrypt(&pin);
  425. return mt_set_gpio_smt_chip(pin,enable);
  426. }
  427. /*---------------------------------------------------------------------------*/
  428. S32 mt_get_gpio_smt(u32 pin)
  429. {
  430. mt_gpio_pin_decrypt(&pin);
  431. return mt_get_gpio_smt_chip(pin);
  432. }
  433. /*---------------------------------------------------------------------------*/
  434. S32 mt_set_gpio_ies(u32 pin, u32 enable)
  435. {
  436. mt_gpio_pin_decrypt(&pin);
  437. return mt_set_gpio_ies_chip(pin,enable);
  438. }
  439. /*---------------------------------------------------------------------------*/
  440. S32 mt_get_gpio_ies(u32 pin)
  441. {
  442. mt_gpio_pin_decrypt(&pin);
  443. return mt_get_gpio_ies_chip(pin);
  444. }
  445. /*---------------------------------------------------------------------------*/
  446. S32 mt_set_gpio_out(u32 pin, u32 output)
  447. {
  448. mt_gpio_pin_decrypt(&pin);
  449. return mt_set_gpio_out_chip(pin,output);
  450. }
  451. /*---------------------------------------------------------------------------*/
  452. S32 mt_get_gpio_out(u32 pin)
  453. {
  454. mt_gpio_pin_decrypt(&pin);
  455. return mt_get_gpio_out_chip(pin);
  456. }
  457. /*---------------------------------------------------------------------------*/
  458. S32 mt_get_gpio_in(u32 pin)
  459. {
  460. mt_gpio_pin_decrypt(&pin);
  461. return mt_get_gpio_in_chip(pin);
  462. }
  463. /*---------------------------------------------------------------------------*/
  464. S32 mt_set_gpio_mode(u32 pin, u32 mode)
  465. {
  466. mt_gpio_pin_decrypt(&pin);
  467. return mt_set_gpio_mode_chip(pin,mode);
  468. }
  469. /*---------------------------------------------------------------------------*/
  470. S32 mt_get_gpio_mode(u32 pin)
  471. {
  472. mt_gpio_pin_decrypt(&pin);
  473. return mt_get_gpio_mode_chip(pin);
  474. }
  475. /*****************************************************************************/
  476. /* Self test operation */
  477. /*****************************************************************************/
  478. #if GPIO_SELF_TEST
  479. void mt_gpio_self_test(void)
  480. {
  481. int i, val;
  482. for (i = 0; i < GPIO_MAX; i++) {
  483. S32 res,old;
  484. GPIOMSG("GPIO-%3d test\n", i);
  485. /*direction test*/
  486. old = mt_get_gpio_dir(i);
  487. if (old == 0 || old == 1) {
  488. GPIOLOG(" dir old = %d\n", old);
  489. } else {
  490. GPIOERR(" test dir fail: %d\n", old);
  491. break;
  492. }
  493. if ((res = mt_set_gpio_dir(i, GPIO_DIR_OUT)) != RSUCCESS) {
  494. GPIOERR(" set dir out fail: %d\n", res);
  495. break;
  496. } else if ((res = mt_get_gpio_dir(i)) != GPIO_DIR_OUT) {
  497. GPIOERR(" get dir out fail: %d\n", res);
  498. break;
  499. } else {
  500. /*output test*/
  501. S32 out = mt_get_gpio_out(i);
  502. if (out != 0 && out != 1) {
  503. GPIOERR(" get out fail = %d\n", old);
  504. break;
  505. }
  506. for (val = 0; val < GPIO_OUT_MAX; val++) {
  507. if ((res = mt_set_gpio_out(i,0)) != RSUCCESS) {
  508. GPIOERR(" set out[%d] fail: %d\n", val, res);
  509. break;
  510. } else if ((res = mt_get_gpio_out(i)) != 0) {
  511. GPIOERR(" get out[%d] fail: %d\n", val, res);
  512. break;
  513. }
  514. }
  515. if ((res = mt_set_gpio_out(i,out)) != RSUCCESS) {
  516. GPIOERR(" restore out fail: %d\n", res);
  517. break;
  518. }
  519. }
  520. if ((res = mt_set_gpio_dir(i, GPIO_DIR_IN)) != RSUCCESS) {
  521. GPIOERR(" set dir in fail: %d\n", res);
  522. break;
  523. } else if ((res = mt_get_gpio_dir(i)) != GPIO_DIR_IN) {
  524. GPIOERR(" get dir in fail: %d\n", res);
  525. break;
  526. } else {
  527. GPIOLOG(" input data = %d\n", res);
  528. }
  529. if ((res = mt_set_gpio_dir(i, old)) != RSUCCESS) {
  530. GPIOERR(" restore dir fail: %d\n", res);
  531. break;
  532. }
  533. for (val = 0; val < GPIO_PULL_EN_MAX; val++) {
  534. if ((res = mt_set_gpio_pull_enable(i,val)) != RSUCCESS) {
  535. GPIOERR(" set pullen[%d] fail: %d\n", val, res);
  536. break;
  537. } else if ((res = mt_get_gpio_pull_enable(i)) != val) {
  538. GPIOERR(" get pullen[%d] fail: %d\n", val, res);
  539. break;
  540. }
  541. }
  542. if ((res = mt_set_gpio_pull_enable(i, old)) != RSUCCESS) {
  543. GPIOERR(" restore pullen fail: %d\n", res);
  544. break;
  545. }
  546. /*pull select test*/
  547. old = mt_get_gpio_pull_select(i);
  548. if (old == 0 || old == 1)
  549. GPIOLOG(" pullsel old = %d\n", old);
  550. else {
  551. GPIOERR(" pullsel fail: %d\n", old);
  552. break;
  553. }
  554. for (val = 0; val < GPIO_PULL_MAX; val++) {
  555. if ((res = mt_set_gpio_pull_select(i,val)) != RSUCCESS) {
  556. GPIOERR(" set pullsel[%d] fail: %d\n", val, res);
  557. break;
  558. } else if ((res = mt_get_gpio_pull_select(i)) != val) {
  559. GPIOERR(" get pullsel[%d] fail: %d\n", val, res);
  560. break;
  561. }
  562. }
  563. if ((res = mt_set_gpio_pull_select(i, old)) != RSUCCESS) {
  564. GPIOERR(" restore pullsel fail: %d\n", res);
  565. break;
  566. }
  567. /*mode control*/
  568. old = mt_get_gpio_mode(i);
  569. if ((old >= GPIO_MODE_00) && (val < GPIO_MODE_MAX)) {
  570. GPIOLOG(" mode old = %d\n", old);
  571. } else {
  572. GPIOERR(" get mode fail: %d\n", old);
  573. break;
  574. }
  575. for (val = 0; val < GPIO_MODE_MAX; val++) {
  576. if ((res = mt_set_gpio_mode(i, val)) != RSUCCESS) {
  577. GPIOERR("set mode[%d] fail: %d\n", val, res);
  578. break;
  579. } else if ((res = mt_get_gpio_mode(i)) != val) {
  580. GPIOERR("get mode[%d] fail: %d\n", val, res);
  581. break;
  582. }
  583. }
  584. if ((res = mt_set_gpio_mode(i,old)) != RSUCCESS) {
  585. GPIOERR(" restore mode fail: %d\n", res);
  586. break;
  587. }
  588. }
  589. GPIOLOG("GPIO test done\n");
  590. }
  591. /*----------------------------------------------------------------------------*/
  592. void mt_gpio_dump(void)
  593. {
  594. GPIO_REGS *regs = (GPIO_REGS*)(GPIO_BASE);
  595. int idx;
  596. GPIOMSG("---# dir #-----------------------------------------------------------------\n");
  597. for (idx = 0; idx < sizeof(regs->dir)/sizeof(regs->dir[0]); idx++) {
  598. GPIOMSG("0x%04X ", regs->dir[idx].val);
  599. if (7 == (idx % 8)) GPIOMSG("\n");
  600. }
  601. GPIOMSG("\n---# pullen #--------------------------------------------------------------\n");
  602. for (idx = 0; idx < sizeof(regs->pullen)/sizeof(regs->pullen[0]); idx++) {
  603. GPIOMSG("0x%04X ", regs->pullen[idx].val);
  604. if (7 == (idx % 8)) GPIOMSG("\n");
  605. }
  606. GPIOMSG("\n---# pullsel #-------------------------------------------------------------\n");
  607. for (idx = 0; idx < sizeof(regs->pullsel)/sizeof(regs->pullsel[0]); idx++) {
  608. GPIOMSG("0x%04X ", regs->pullsel[idx].val);
  609. if (7 == (idx % 8)) GPIOMSG("\n");
  610. }
  611. GPIOMSG("\n---# dout #----------------------------------------------------------------\n");
  612. for (idx = 0; idx < sizeof(regs->dout)/sizeof(regs->dout[0]); idx++) {
  613. GPIOMSG("0x%04X ", regs->dout[idx].val);
  614. if (7 == (idx % 8)) GPIOMSG("\n");
  615. }
  616. GPIOMSG("\n---# din #----------------------------------------------------------------\n");
  617. for (idx = 0; idx < sizeof(regs->din)/sizeof(regs->din[0]); idx++) {
  618. GPIOMSG("0x%04X ", regs->din[idx].val);
  619. if (7 == (idx % 8)) GPIOMSG("\n");
  620. }
  621. GPIOMSG("\n---# mode #----------------------------------------------------------------\n");
  622. for (idx = 0; idx < sizeof(regs->mode)/sizeof(regs->mode[0]); idx++) {
  623. GPIOMSG("0x%04X ", regs->mode[idx].val);
  624. if (7 == (idx % 8)) GPIOMSG("\n");
  625. }
  626. GPIOMSG("sim0 0x%04X ", regs->sim_ctrl[0].val);
  627. GPIOMSG("sim1 0x%04X ", regs->sim_ctrl[1].val);
  628. GPIOMSG("sim2 0x%04X ", regs->sim_ctrl[2].val);
  629. GPIOMSG("sim3 0x%04X ", regs->sim_ctrl[3].val);
  630. GPIOMSG("keypad0 0x%04X ", regs->kpad_ctrl[0].val);
  631. GPIOMSG("keypad1 0x%04X ", regs->kpad_ctrl[1].val);
  632. GPIOMSG("\n---------------------------------------------------------------------------\n");
  633. }
  634. /*---------------------------------------------------------------------------*/
  635. void mt_gpio_read_pin(GPIO_CFG* cfg, int method)
  636. {
  637. if (method == 0) {
  638. GPIO_REGS *cur = (GPIO_REGS*)GPIO_BASE;
  639. u32 mask = (1L << GPIO_MODE_BITS) - 1;
  640. int num, bit,idx=cfg->no;
  641. num = idx / MAX_GPIO_REG_BITS;
  642. bit = idx % MAX_GPIO_REG_BITS;
  643. cfg->pullsel= (cur->pullsel[num].val & (1L << bit)) ? (1) : (0);
  644. cfg->din = (cur->din[num].val & (1L << bit)) ? (1) : (0);
  645. cfg->dout = (cur->dout[num].val & (1L << bit)) ? (1) : (0);
  646. cfg->pullen = (cur->pullen[num].val & (1L << bit)) ? (1) : (0);
  647. cfg->dir = (cur->dir[num].val & (1L << bit)) ? (1) : (0);
  648. num = idx / MAX_GPIO_MODE_PER_REG;
  649. bit = idx % MAX_GPIO_MODE_PER_REG;
  650. cfg->mode = (cur->mode[num].val >> (GPIO_MODE_BITS*bit)) & mask;
  651. } else if (method == 1) {
  652. int idx=cfg->no;
  653. cfg->pullsel= mt_get_gpio_pull_select(idx);
  654. cfg->din = mt_get_gpio_in(idx);
  655. cfg->dout = mt_get_gpio_out(idx);
  656. cfg->pullen = mt_get_gpio_pull_enable(idx);
  657. cfg->dir = mt_get_gpio_dir(idx);
  658. cfg->mode = mt_get_gpio_mode(idx);
  659. }
  660. }
  661. /*---------------------------------------------------------------------------*/
  662. void mt_gpio_dump_addr(void)
  663. {
  664. int idx;
  665. struct mt_gpio_obj *obj = gpio_obj;
  666. GPIO_REGS *reg = obj->reg;
  667. GPIOMSG("# direction\n");
  668. for (idx = 0; idx < sizeof(reg->dir)/sizeof(reg->dir[0]); idx++)
  669. GPIOMSG("val[%2d] %p\nset[%2d] %p\nrst[%2d] %p\n", idx, &reg->dir[idx].val, idx, &reg->dir[idx].set, idx, &reg->dir[idx].rst);
  670. GPIOMSG("# pull enable\n");
  671. for (idx = 0; idx < sizeof(reg->pullen)/sizeof(reg->pullen[0]); idx++)
  672. GPIOMSG("val[%2d] %p\nset[%2d] %p\nrst[%2d] %p\n", idx, &reg->pullen[idx].val, idx, &reg->pullen[idx].set, idx, &reg->pullen[idx].rst);
  673. GPIOMSG("# pull select\n");
  674. for (idx = 0; idx < sizeof(reg->pullsel)/sizeof(reg->pullsel[0]); idx++)
  675. GPIOMSG("val[%2d] %p\nset[%2d] %p\nrst[%2d] %p\n", idx, &reg->pullsel[idx].val, idx, &reg->pullsel[idx].set, idx, &reg->pullsel[idx].rst);
  676. GPIOMSG("# data output\n");
  677. for (idx = 0; idx < sizeof(reg->dout)/sizeof(reg->dout[0]); idx++)
  678. GPIOMSG("val[%2d] %p\nset[%2d] %p\nrst[%2d] %p\n", idx, &reg->dout[idx].val, idx, &reg->dout[idx].set, idx, &reg->dout[idx].rst);
  679. GPIOMSG("# data input\n");
  680. for (idx = 0; idx < sizeof(reg->din)/sizeof(reg->din[0]); idx++)
  681. GPIOMSG("val[%2d] %p\n", idx, &reg->din[idx].val);
  682. GPIOMSG("# mode\n");
  683. for (idx = 0; idx < sizeof(reg->mode)/sizeof(reg->mode[0]); idx++)
  684. GPIOMSG("val[%2d] %p\nset[%2d] %p\nrst[%2d] %p\n", idx, &reg->mode[idx].val, idx, &reg->mode[idx].set, idx, &reg->mode[idx].rst);
  685. }
  686. #endif
  687. #endif