mt_gpio.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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/* use status register rather than set/clr register */
  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. /*---------------------------------------------------------------------------*/
  53. #define GPIOTAG "[GPIO] "
  54. #define GPIODBG(fmt, arg...) dprintf(CRITICAL, GPIOTAG "%s: " fmt, __FUNCTION__ ,##arg)
  55. #define GPIOERR(fmt, arg...) dprintf(CRITICAL, GPIOTAG "%s: " fmt, __FUNCTION__ ,##arg)
  56. #define GPIOVER(fmt, arg...) dprintf(CRITICAL, GPIOTAG "%s: " fmt, __FUNCTION__ ,##arg)
  57. /******************************************************************************
  58. Enumeration/Structure
  59. ******************************************************************************/
  60. #if (defined(MACH_FPGA) && !defined (FPGA_SIMULATION))
  61. S32 mt_set_gpio_dir(u32 pin, u32 dir)
  62. {
  63. return RSUCCESS;
  64. }
  65. S32 mt_get_gpio_dir(u32 pin)
  66. {
  67. return GPIO_DIR_UNSUPPORTED;
  68. }
  69. S32 mt_set_gpio_pull_enable(u32 pin, u32 enable)
  70. {
  71. return RSUCCESS;
  72. }
  73. S32 mt_get_gpio_pull_enable(u32 pin)
  74. {
  75. return GPIO_PULL_EN_UNSUPPORTED;
  76. }
  77. S32 mt_set_gpio_pull_select(u32 pin, u32 select)
  78. {
  79. return RSUCCESS;
  80. }
  81. S32 mt_get_gpio_pull_select(u32 pin)
  82. {
  83. return GPIO_PULL_UNSUPPORTED;
  84. }
  85. S32 mt_set_gpio_smt(u32 pin, u32 enable)
  86. {
  87. return RSUCCESS;
  88. }
  89. S32 mt_get_gpio_smt(u32 pin)
  90. {
  91. return GPIO_SMT_UNSUPPORTED;
  92. }
  93. S32 mt_set_gpio_ies(u32 pin, u32 enable)
  94. {
  95. return RSUCCESS;
  96. }
  97. S32 mt_get_gpio_ies(u32 pin)
  98. {
  99. return GPIO_IES_UNSUPPORTED;
  100. }
  101. S32 mt_set_gpio_out(u32 pin, u32 output)
  102. {
  103. return RSUCCESS;
  104. }
  105. S32 mt_get_gpio_out(u32 pin)
  106. {
  107. return GPIO_OUT_UNSUPPORTED;
  108. }
  109. S32 mt_get_gpio_in(u32 pin)
  110. {
  111. return GPIO_IN_UNSUPPORTED;
  112. }
  113. S32 mt_set_gpio_mode(u32 pin, u32 mode)
  114. {
  115. return RSUCCESS;
  116. }
  117. S32 mt_get_gpio_mode(u32 pin)
  118. {
  119. return GPIO_MODE_UNSUPPORTED;
  120. }
  121. S32 mt_set_gpio_drving(u32 pin, GPIO_DRV driving)
  122. {
  123. return RSUCCESS;
  124. }
  125. S32 mt_get_gpio_drving(u32 pin)
  126. {
  127. return RSUCCESS;
  128. }
  129. #else //else !(defined(MACH_FPGA) && !defined (FPGA_SIMULATION))
  130. typedef struct {
  131. S32 en_flag;
  132. } GPIO_PUPD_flag;
  133. GPIO_PUPD_flag gpio_pupd_en_flag[MT_GPIO_BASE_MAX] = {{ 0 }};
  134. struct mt_gpio_obj {
  135. GPIO_REGS *reg;
  136. };
  137. static struct mt_gpio_obj gpio_dat = {
  138. .reg = (GPIO_REGS *) (GPIO_BASE),
  139. };
  140. static struct mt_gpio_obj *gpio_obj = &gpio_dat;
  141. /*---------------------------------------------------------------------------*/
  142. S32 mt_set_gpio_dir_chip(u32 pin, u32 dir)
  143. {
  144. u32 bit;
  145. #ifdef GPIO_BRINGUP
  146. u32 reg;
  147. #else
  148. u32 pos;
  149. struct mt_gpio_obj *obj = gpio_obj;
  150. #endif
  151. if (pin >= MAX_GPIO_PIN)
  152. return -ERINVAL;
  153. if (dir >= GPIO_DIR_MAX)
  154. return -ERINVAL;
  155. bit = DIR_offset[pin].offset;
  156. #ifdef GPIO_BRINGUP
  157. reg = GPIO_RD32(DIR_addr[pin].addr);
  158. if (dir == GPIO_DIR_IN)
  159. reg &= (~(1 << bit));
  160. else
  161. reg |= (1 << bit);
  162. GPIO_WR32(DIR_addr[pin].addr, reg);
  163. #else
  164. pos = pin / MAX_GPIO_REG_BITS;
  165. if (dir == GPIO_DIR_IN)
  166. GPIO_SET_BITS((1L << bit), &obj->reg->dir[pos].rst);
  167. else
  168. GPIO_SET_BITS((1L << bit), &obj->reg->dir[pos].set);
  169. #endif
  170. return RSUCCESS;
  171. }
  172. /*---------------------------------------------------------------------------*/
  173. S32 mt_get_gpio_dir_chip(u32 pin)
  174. {
  175. u32 bit;
  176. u32 reg;
  177. if (pin >= MAX_GPIO_PIN)
  178. return -ERINVAL;
  179. bit = DIR_offset[pin].offset;
  180. reg = GPIO_RD32(DIR_addr[pin].addr);
  181. return (((reg & (1L << bit)) != 0) ? 1 : 0);
  182. }
  183. /*---------------------------------------------------------------------------*/
  184. S32 mt_set_gpio_smt_chip(u32 pin, u32 enable)
  185. {
  186. #ifdef GPIO_BRINGUP
  187. u32 reg = 0;
  188. u32 bit = 0;
  189. #endif
  190. if (pin >= MAX_GPIO_PIN)
  191. return -ERINVAL;
  192. #ifdef GPIO_BRINGUP
  193. if (SMT_offset[pin].offset == -1) {
  194. return GPIO_SMT_UNSUPPORTED;
  195. } else {
  196. bit = SMT_offset[pin].offset;
  197. reg = GPIO_RD32(SMT_addr[pin].addr);
  198. if (enable == GPIO_SMT_DISABLE)
  199. reg &= (~(1 << bit));
  200. else
  201. reg |= (1 << bit);
  202. }
  203. GPIO_WR32(SMT_addr[pin].addr, reg);
  204. #else
  205. if (SMT_offset[pin].offset == -1) {
  206. return GPIO_SMT_UNSUPPORTED;
  207. } else {
  208. if (enable == GPIO_SMT_DISABLE)
  209. GPIO_SET_BITS((1L << (SMT_offset[pin].offset)), SMT_addr[pin].addr + 8);
  210. else
  211. GPIO_SET_BITS((1L << (SMT_offset[pin].offset)), SMT_addr[pin].addr + 4);
  212. }
  213. #endif
  214. return RSUCCESS;
  215. }
  216. /*---------------------------------------------------------------------------*/
  217. S32 mt_get_gpio_smt_chip(u32 pin)
  218. {
  219. unsigned long data;
  220. u32 bit = 0;
  221. bit = SMT_offset[pin].offset;
  222. if (pin >= MAX_GPIO_PIN)
  223. return -ERINVAL;
  224. if (SMT_offset[pin].offset == -1) {
  225. return GPIO_SMT_UNSUPPORTED;
  226. } else {
  227. data = GPIO_RD32(SMT_addr[pin].addr);
  228. return (((data & (1L << bit)) != 0) ? 1 : 0);
  229. }
  230. }
  231. /*---------------------------------------------------------------------------*/
  232. S32 mt_set_gpio_ies_chip(u32 pin, u32 enable)
  233. {
  234. if (pin >= MAX_GPIO_PIN)
  235. return -ERINVAL;
  236. if (IES_offset[pin].offset == -1) {
  237. return GPIO_IES_UNSUPPORTED;
  238. } else {
  239. if (enable == GPIO_IES_DISABLE)
  240. GPIO_SET_BITS((1L << (IES_offset[pin].offset)), IES_addr[pin].addr + 8);
  241. else
  242. GPIO_SET_BITS((1L << (IES_offset[pin].offset)), IES_addr[pin].addr + 4);
  243. }
  244. return RSUCCESS;
  245. }
  246. /*---------------------------------------------------------------------------*/
  247. S32 mt_get_gpio_ies_chip(u32 pin)
  248. {
  249. unsigned long data;
  250. if (pin >= MAX_GPIO_PIN)
  251. return -ERINVAL;
  252. if (IES_offset[pin].offset == -1) {
  253. return GPIO_IES_UNSUPPORTED;
  254. } else {
  255. data = GPIO_RD32(IES_addr[pin].addr);
  256. return (((data & (1L << (IES_offset[pin].offset))) != 0) ? 1 : 0);
  257. }
  258. }
  259. /*---------------------------------------------------------------------------*/
  260. /*
  261. r0 ==0 && r1 ==0 High-Z(no pull)
  262. */
  263. S32 mt_set_gpio_pull_select_rx_chip(u32 pin, u32 r0, u32 r1)
  264. {
  265. #ifdef GPIO_BRINGUP
  266. u32 reg = 0;
  267. if (0 == r0) {
  268. reg = GPIO_RD32(R0_addr[pin].addr);
  269. reg &= (~(1 << R0_offset[pin].offset));
  270. GPIO_WR32(R0_addr[pin].addr, reg);
  271. }
  272. if (1 == r0) {
  273. reg = GPIO_RD32(R0_addr[pin].addr);
  274. reg |= (1 << R0_offset[pin].offset);
  275. GPIO_WR32(R0_addr[pin].addr, reg);
  276. }
  277. if (0 == r1) {
  278. reg = GPIO_RD32(R1_addr[pin].addr);
  279. reg &= (~(1 << R1_offset[pin].offset));
  280. GPIO_WR32(R1_addr[pin].addr, reg);
  281. }
  282. if (1 == r1) {
  283. reg = GPIO_RD32(R1_addr[pin].addr);
  284. reg |= (1 << R1_offset[pin].offset);
  285. GPIO_WR32(R1_addr[pin].addr, reg);
  286. }
  287. #else
  288. if (0 == r0)
  289. GPIO_SET_BITS((1L << (R0_offset[pin].offset)), R0_addr[pin].addr + 8);
  290. if (1 == r0)
  291. GPIO_SET_BITS((1L << (R0_offset[pin].offset)), R0_addr[pin].addr + 4);
  292. if (0 == r1)
  293. GPIO_SET_BITS((1L << (R1_offset[pin].offset)), R1_addr[pin].addr + 8);
  294. if (1 == r1)
  295. GPIO_SET_BITS((1L << (R1_offset[pin].offset)), R1_addr[pin].addr + 4);
  296. #endif
  297. return 0;
  298. }
  299. S32 mt_set_gpio_pull_select_chip(u32 pin, u32 select)
  300. {
  301. #ifdef GPIO_BRINGUP
  302. u32 reg = 0;
  303. u32 pu_reg = 0;
  304. u32 pd_reg = 0;
  305. unsigned long pupd_bit = 0;
  306. unsigned long pu_bit = 0;
  307. unsigned long pd_bit = 0;
  308. #endif
  309. if (pin >= MAX_GPIO_PIN)
  310. return -ERINVAL;
  311. #ifdef GPIO_BRINGUP
  312. if ((PUPD_offset[pin].offset == -1) && -1 == PU_offset[pin].offset && -1 == PD_offset[pin].offset)
  313. return GPIO_PULL_UNSUPPORTED;
  314. if (-1 != PUPD_offset[pin].offset) {
  315. /*PULL UP */
  316. mt_set_gpio_pull_select_rx_chip(pin, 0, 1); /*must first set default value r0=0, r1 =1 */
  317. pupd_bit = PUPD_offset[pin].offset;
  318. reg = GPIO_RD32(PUPD_addr[pin].addr);
  319. if (select == GPIO_PULL_UP)
  320. reg &= (~(1 << pupd_bit));
  321. else if (select == GPIO_PULL_DOWN)
  322. reg |= (1 << pupd_bit);
  323. else if (GPIO_NO_PULL == select) {
  324. mt_set_gpio_pull_select_rx_chip(pin, 0, 0);
  325. } else
  326. return -ERWRAPPER;
  327. GPIO_WR32(PUPD_addr[pin].addr, reg);
  328. return RSUCCESS;
  329. }
  330. if (-1 != PU_offset[pin].offset && -1 != PD_offset[pin].offset) {
  331. pu_bit = PU_offset[pin].offset;
  332. pd_bit = PD_offset[pin].offset;
  333. pu_reg = GPIO_RD32(PU_addr[pin].addr);
  334. pd_reg = GPIO_RD32(PD_addr[pin].addr);
  335. if (select == GPIO_PULL_UP) {
  336. pu_reg |= (1 << pu_bit);
  337. pd_reg &= (~(1 << pd_bit));
  338. } else if (GPIO_PULL_DOWN == select) {
  339. pu_reg &= (~(1 << pu_bit));
  340. pd_reg |= (1 << pd_bit);
  341. } else if (GPIO_NO_PULL) {
  342. pd_reg &= (~(1 << pd_bit));
  343. pu_reg &= (~(1 << pu_bit));
  344. } else {
  345. return -ERWRAPPER;
  346. }
  347. GPIO_WR32(PU_addr[pin].addr, pu_reg);
  348. GPIO_WR32(PD_addr[pin].addr, pd_reg);
  349. return RSUCCESS;
  350. } else if (-1 != PU_offset[pin].offset) { /*pu==1 is pull up, pu ==0 is no pull */
  351. pu_reg = GPIO_RD32(PU_addr[pin].addr);
  352. pu_bit = PU_offset[pin].offset;
  353. if (select == GPIO_PULL_UP) {
  354. pu_reg |= (1 << pu_bit);
  355. } else if (GPIO_PULL_DOWN == select) {
  356. return GPIO_NOPULLDOWN;
  357. } else if (GPIO_NO_PULL) {
  358. pu_reg &= (~(1 << pu_bit));
  359. } else
  360. return -ERWRAPPER;
  361. GPIO_WR32(PU_addr[pin].addr, pu_reg);
  362. return RSUCCESS;
  363. } else {
  364. pd_reg = GPIO_RD32(PD_addr[pin].addr);
  365. pd_bit = PD_offset[pin].offset;
  366. if (select == GPIO_PULL_UP) {
  367. return GPIO_NOPULLUP;
  368. } else if (GPIO_PULL_DOWN == select) {
  369. pd_reg |= (1 << pd_bit);
  370. } else if (GPIO_NO_PULL) {
  371. pd_reg &= (~(1 << pd_bit));
  372. } else
  373. return -ERWRAPPER;
  374. GPIO_WR32(PD_addr[pin].addr, pd_reg);
  375. return RSUCCESS;
  376. }
  377. #else
  378. if ((PUPD_offset[pin].offset == -1) && (-1 == PU_offset[pin].offset)
  379. && (-1 == PD_offset[pin].offset)) {
  380. return GPIO_PULL_UNSUPPORTED;
  381. }
  382. if (-1 != PUPD_offset[pin].offset) {
  383. if (select == GPIO_PULL_UP)
  384. GPIO_SET_BITS((1L << (PUPD_offset[pin].offset)), PUPD_addr[pin].addr + 8);
  385. else if (GPIO_PULL_DOWN == select)
  386. GPIO_SET_BITS((1L << (PUPD_offset[pin].offset)), PUPD_addr[pin].addr + 4);
  387. else if (GPIO_NO_PULL)
  388. mt_set_gpio_pull_select_rx_chip(pin, 0, 0);
  389. else
  390. return -ERWRAPPER;
  391. return RSUCCESS;
  392. }
  393. if (-1 != PU_offset[pin].offset && -1 != PD_offset[pin].offset) {
  394. if (select == GPIO_PULL_UP) {
  395. GPIO_SET_BITS((1L << (PU_offset[pin].offset)), PU_addr[pin].addr + 4);
  396. GPIO_SET_BITS((1L << (PD_offset[pin].offset)), PD_addr[pin].addr + 8);
  397. } else if (GPIO_PULL_DOWN == select) {
  398. GPIO_SET_BITS((1L << (PD_offset[pin].offset)), PD_addr[pin].addr + 4);
  399. GPIO_SET_BITS((1L << (PU_offset[pin].offset)), PU_addr[pin].addr + 8);
  400. } else if (GPIO_NO_PULL) {
  401. GPIO_SET_BITS((1L << (PD_offset[pin].offset)), PD_addr[pin].addr + 8);
  402. GPIO_SET_BITS((1L << (PU_offset[pin].offset)), PU_addr[pin].addr + 8);
  403. } else
  404. return -ERWRAPPER;
  405. return RSUCCESS;
  406. } else if (-1 != PU_offset[pin].offset) {
  407. if (select == GPIO_PULL_UP)
  408. GPIO_SET_BITS((1L << (PU_offset[pin].offset)), PU_addr[pin].addr + 4);
  409. else if (GPIO_PULL_DOWN == select)
  410. return GPIO_NOPULLDOWN;
  411. else if (GPIO_NO_PULL)
  412. GPIO_SET_BITS((1L << (PU_offset[pin].offset)), PU_addr[pin].addr + 8);
  413. else
  414. return -ERWRAPPER;
  415. return RSUCCESS;
  416. } else {
  417. if (select == GPIO_PULL_UP) {
  418. return GPIO_NOPULLUP;
  419. } else if (GPIO_PULL_DOWN == select) {
  420. GPIO_SET_BITS((1L << (PD_offset[pin].offset)), PD_addr[pin].addr + 4);
  421. } else if (GPIO_NO_PULL) {
  422. GPIO_SET_BITS((1L << (PD_offset[pin].offset)), PD_addr[pin].addr + 8);
  423. } else
  424. return -ERWRAPPER;
  425. return RSUCCESS;
  426. }
  427. #endif
  428. }
  429. /*---------------------------------------------------------------------------*/
  430. S32 mt_get_gpio_pull_select_chip(u32 pin)
  431. {
  432. unsigned long data;
  433. unsigned long r0;
  434. unsigned long r1;
  435. unsigned long pu = 0;
  436. unsigned long pd = 0;
  437. if ((PUPD_offset[pin].offset == -1) && -1 == PU_offset[pin].offset && -1 == PD_offset[pin].offset) {
  438. return GPIO_PULL_UNSUPPORTED;
  439. }
  440. /*pupd==0 is pull up or no pull */
  441. if (-1 != PUPD_offset[pin].offset) {
  442. data = GPIO_RD32(PUPD_addr[pin].addr);
  443. r0 = GPIO_RD32(R0_addr[pin].addr);
  444. r1 = GPIO_RD32(R1_addr[pin].addr);
  445. if (0 == (data & (1L << (PUPD_offset[pin].offset)))) {
  446. if (0 == (r0 & (1L << (R0_offset[pin].offset)))
  447. && 0 == (r1 & (1L << (R1_offset[pin].offset))))
  448. return GPIO_NO_PULL; /*High Z(no pull) */
  449. else
  450. return GPIO_PULL_UP; /* pull up */
  451. }
  452. /*pupd==1 is pull down or no pull */
  453. if (0 != (data & (1L << (PUPD_offset[pin].offset)))) {
  454. if (0 == (r0 & (1L << (R0_offset[pin].offset)))
  455. && 0 == (r1 & (1L << (R1_offset[pin].offset))))
  456. return GPIO_NO_PULL; /*High Z(no pull) */
  457. else
  458. return GPIO_PULL_DOWN; /* pull down */
  459. }
  460. }
  461. if (-1 != PU_offset[pin].offset && -1 != PD_offset[pin].offset) {
  462. pu = GPIO_RD32(PU_addr[pin].addr);
  463. pd = GPIO_RD32(PD_addr[pin].addr);
  464. if (0 != (pu & (1L << (PU_offset[pin].offset))))
  465. pu = 1;
  466. else
  467. pu = 0;
  468. if (0 != (pd & (1L << (PD_offset[pin].offset))))
  469. pd = 1;
  470. else
  471. pd = 0;
  472. if (0 == pu && 0 == pd) /*no pull */
  473. return GPIO_NO_PULL;
  474. else if (1 == pu && 1 == pd)
  475. return -1; /* err */
  476. else if (1 == pu && 0 == pd)
  477. return GPIO_PULL_UP; /* pull up */
  478. else
  479. return GPIO_PULL_DOWN; /* pull down */
  480. } else if (-1 != PU_offset[pin].offset) { /*pu==1 is pull up, pu ==0 is no pull */
  481. pu = GPIO_RD32(PU_addr[pin].addr);
  482. if (0 != (pu & (1L << (PU_offset[pin].offset))))
  483. return GPIO_PULL_UP; /* pull up */
  484. else
  485. return GPIO_NO_PULL; /*High Z(no pull) */
  486. } else {
  487. pd = GPIO_RD32(PD_addr[pin].addr);
  488. if (0 != (pd & (1L << (PD_offset[pin].offset))))
  489. return GPIO_PULL_DOWN; /* pull down */
  490. else
  491. return GPIO_NO_PULL; /*High Z(no pull) */
  492. }
  493. }
  494. /*---------------------------------------------------------------------------*/
  495. S32 mt_set_gpio_pull_enable_chip(u32 pin, u32 enable)
  496. {
  497. int err = 0;
  498. if (pin >= MAX_GPIO_PIN)
  499. return -ERINVAL;
  500. if ((PUPD_offset[pin].offset == -1) && -1 == PU_offset[pin].offset && -1 == PD_offset[pin].offset)
  501. return GPIO_PULL_UNSUPPORTED;
  502. if (GPIO_PULL_DISABLE == enable) {
  503. /*r0==0 && r1 ==0 */
  504. err = mt_set_gpio_pull_select_chip(pin, GPIO_NO_PULL);
  505. return err;
  506. } else {
  507. err = mt_set_gpio_pull_select_chip(pin, GPIO_PULL_UP);
  508. return err;
  509. }
  510. return RSUCCESS;
  511. }
  512. /*---------------------------------------------------------------------------*/
  513. S32 mt_get_gpio_pull_enable_chip(u32 pin)
  514. {
  515. int res = 0;
  516. if (pin >= MAX_GPIO_PIN)
  517. return -ERINVAL;
  518. res = mt_get_gpio_pull_select_chip(pin);
  519. if (2 == res)
  520. return 0; /*disable */
  521. if (1 == res || 0 == res)
  522. return 1; /*enable */
  523. if (-1 == res)
  524. return -1;
  525. return -ERWRAPPER;
  526. }
  527. /*---------------------------------------------------------------------------*/
  528. S32 mt_set_gpio_driving_chip(u32 pin, GPIO_DRV driving)
  529. {
  530. u32 bit = 0;
  531. #ifdef GPIO_BRINGUP
  532. u32 reg = 0;
  533. #endif
  534. if (pin >= MAX_GPIO_PIN)
  535. return -ERINVAL;
  536. bit = DRV_offset[pin].offset;
  537. if (-1 == DRV_width[pin].width)
  538. return -ERWRAPPER;
  539. #ifdef GPIO_BRINGUP
  540. if (3 == DRV_width[pin].width) {
  541. if (driving > 0x7)
  542. return -ERWRAPPER;
  543. reg = GPIO_RD32(DRV_addr[pin].addr);
  544. reg &= ~(0x7 << bit);
  545. GPIO_WR32(DRV_addr[pin].addr, reg);
  546. reg = GPIO_RD32(DRV_addr[pin].addr);
  547. reg |= (driving << bit);
  548. GPIO_WR32(DRV_addr[pin].addr, reg);
  549. } else if (2 == DRV_width[pin].width) {
  550. if (driving > 0x3)
  551. return -ERWRAPPER;
  552. reg = GPIO_RD32(DRV_addr[pin].addr);
  553. reg &= ~(0x3 << bit);
  554. GPIO_WR32(DRV_addr[pin].addr, reg);
  555. reg = GPIO_RD32(DRV_addr[pin].addr);
  556. reg |= (driving << bit);
  557. GPIO_WR32(DRV_addr[pin].addr, reg);
  558. } else if (1 == DRV_width[pin].width) {
  559. if (driving > 1)
  560. return -ERWRAPPER;
  561. reg = GPIO_RD32(DRV_addr[pin].addr);
  562. reg &= ~(0x1 << bit);
  563. GPIO_WR32(DRV_addr[pin].addr, reg);
  564. reg = GPIO_RD32(DRV_addr[pin].addr);
  565. reg |= (driving << bit);
  566. GPIO_WR32(DRV_addr[pin].addr, reg);
  567. }
  568. #else
  569. if (3 == DRV_width[pin].width) {
  570. if (driving > 0x7)
  571. return -ERWRAPPER;
  572. /*clear all bit first*/
  573. GPIO_SET_BITS((0x7 << bit), DRV_addr[pin].addr + 8);
  574. GPIO_SET_BITS((driving << bit), DRV_addr[pin].addr + 4);
  575. } else if (2 == DRV_width[pin].width) {
  576. if (driving > 0x3)
  577. return -ERWRAPPER;
  578. /*clear all bit first*/
  579. GPIO_SET_BITS((0x3 << bit), DRV_addr[pin].addr + 8);
  580. GPIO_SET_BITS((driving << bit), DRV_addr[pin].addr + 4);
  581. } else if (1 == DRV_width[pin].width) {
  582. if (driving > 1)
  583. return -ERWRAPPER;
  584. /*clear all bit first*/
  585. GPIO_SET_BITS((0x1 << bit), DRV_addr[pin].addr + 8);
  586. GPIO_SET_BITS((driving << bit), DRV_addr[pin].addr + 4);
  587. }
  588. #endif
  589. return RSUCCESS;
  590. }
  591. /*---------------------------------------------------------------------------*/
  592. S32 mt_get_gpio_driving_chip(u32 pin)
  593. {
  594. u32 bit;
  595. if (pin >= MAX_GPIO_PIN)
  596. return -ERINVAL;
  597. bit = DRV_offset[pin].offset;
  598. if (3 == DRV_width[pin].width)
  599. return (GPIO_RD32(DRV_addr[pin].addr) >> bit) &0x07;
  600. else if (2 == DRV_width[pin].width)
  601. return (GPIO_RD32(DRV_addr[pin].addr) >> bit) &0x03;
  602. else if (1 == DRV_width[pin].width)
  603. return (GPIO_RD32(DRV_addr[pin].addr) >> bit) &0x01;
  604. return -ERWRAPPER;
  605. }
  606. /*---------------------------------------------------------------------------*/
  607. S32 mt_set_gpio_out_chip(u32 pin, u32 output)
  608. {
  609. u32 bit;
  610. #ifdef GPIO_BRINGUP
  611. u32 reg = 0;
  612. #else
  613. u32 pos = 0;
  614. struct mt_gpio_obj *obj = gpio_obj;
  615. #endif
  616. if (pin >= MAX_GPIO_PIN)
  617. return -ERINVAL;
  618. if (output >= GPIO_OUT_MAX)
  619. return -ERINVAL;
  620. bit = DATAOUT_offset[pin].offset;
  621. #ifdef GPIO_BRINGUP
  622. reg = GPIO_RD32(DATAOUT_addr[pin].addr);
  623. if (output == GPIO_OUT_ZERO)
  624. reg &= (~(1 << bit));
  625. else
  626. reg |= (1 << bit);
  627. GPIO_WR32(DATAOUT_addr[pin].addr, reg);
  628. #else
  629. pos = pin / MAX_GPIO_REG_BITS;
  630. if (output == GPIO_OUT_ZERO)
  631. GPIO_SET_BITS((1L << bit), &obj->reg->dout[pos].rst);
  632. else
  633. GPIO_SET_BITS((1L << bit), &obj->reg->dout[pos].set);
  634. #endif
  635. return RSUCCESS;
  636. }
  637. /*---------------------------------------------------------------------------*/
  638. S32 mt_get_gpio_out_chip(u32 pin)
  639. {
  640. u32 pos;
  641. u32 bit;
  642. u32 reg;
  643. struct mt_gpio_obj *obj = gpio_obj;
  644. if (!obj)
  645. return -ERACCESS;
  646. if (pin >= MAX_GPIO_PIN)
  647. return -ERINVAL;
  648. pos = pin / MAX_GPIO_REG_BITS;
  649. bit = pin % MAX_GPIO_REG_BITS;
  650. reg = GPIO_RD32(&obj->reg->dout[pos].val);
  651. return (((reg & (1L << bit)) != 0) ? 1 : 0);
  652. }
  653. /*---------------------------------------------------------------------------*/
  654. S32 mt_get_gpio_in_chip(u32 pin)
  655. {
  656. u32 bit;
  657. u32 reg;
  658. struct mt_gpio_obj *obj = gpio_obj;
  659. if (!obj)
  660. return -ERACCESS;
  661. if (pin >= MAX_GPIO_PIN)
  662. return -ERINVAL;
  663. bit = DATAIN_offset[pin].offset;
  664. reg = GPIO_RD32(DATAIN_addr[pin].addr);
  665. return (((reg & (1L << bit)) != 0) ? 1 : 0);
  666. }
  667. /*---------------------------------------------------------------------------*/
  668. S32 mt_set_gpio_mode_chip(u32 pin, u32 mode)
  669. {
  670. u32 bit;
  671. u32 reg;
  672. #ifdef GPIO_BRINGUP
  673. u32 mask = (1L << GPIO_MODE_BITS) - 1;
  674. #else
  675. u32 pos;
  676. struct mt_gpio_obj *obj = gpio_obj;
  677. #endif
  678. if (pin >= MAX_GPIO_PIN)
  679. return -ERINVAL;
  680. if (mode >= GPIO_MODE_MAX)
  681. return -ERINVAL;
  682. bit = MODE_offset[pin].offset;
  683. #ifdef GPIO_BRINGUP
  684. mode = mode & 0x7;
  685. reg = GPIO_RD32(MODE_addr[pin].addr);
  686. reg &= (~(mask << bit));
  687. reg |= (mode << bit);
  688. GPIO_WR32(MODE_addr[pin].addr, reg);
  689. #else
  690. pos = pin / MAX_GPIO_MODE_PER_REG;
  691. reg = ((1L << (GPIO_MODE_BITS * bit)) << 3) | (mode << (GPIO_MODE_BITS * bit));
  692. GPIO_WR32(&obj->reg->mode[pos]._align1, reg);
  693. #endif
  694. return RSUCCESS;
  695. }
  696. /*---------------------------------------------------------------------------*/
  697. S32 mt_get_gpio_mode_chip(u32 pin)
  698. {
  699. u32 bit;
  700. u32 reg;
  701. u32 mask = (1L << GPIO_MODE_BITS) - 1;
  702. if (pin >= MAX_GPIO_PIN)
  703. return -ERINVAL;
  704. bit = MODE_offset[pin].offset;
  705. reg = GPIO_RD32(MODE_addr[pin].addr);
  706. return ((reg >> bit) & mask);
  707. }
  708. /*---------------------------------------------------------------------------*/
  709. void mt_gpio_pin_decrypt(u32 * cipher)
  710. {
  711. /*just for debug, find out who used pin number directly */
  712. if ((*cipher & (0x80000000)) == 0) {
  713. /*GPIOERR("Pin %d decrypt warning! \n", *cipher);*/
  714. }
  715. *cipher &= ~(0x80000000);
  716. return;
  717. }
  718. /*set GPIO function in fact*/
  719. /*---------------------------------------------------------------------------*/
  720. S32 mt_set_gpio_dir(u32 pin, u32 dir)
  721. {
  722. mt_gpio_pin_decrypt(&pin);
  723. return mt_set_gpio_dir_chip(pin, dir);
  724. }
  725. /*---------------------------------------------------------------------------*/
  726. S32 mt_get_gpio_dir(u32 pin)
  727. {
  728. mt_gpio_pin_decrypt(&pin);
  729. return mt_get_gpio_dir_chip(pin);
  730. }
  731. /*---------------------------------------------------------------------------*/
  732. S32 mt_set_gpio_pull_enable(u32 pin, u32 enable)
  733. {
  734. mt_gpio_pin_decrypt(&pin);
  735. return mt_set_gpio_pull_enable_chip(pin, enable);
  736. }
  737. /*---------------------------------------------------------------------------*/
  738. S32 mt_get_gpio_pull_enable(u32 pin)
  739. {
  740. mt_gpio_pin_decrypt(&pin);
  741. return mt_get_gpio_pull_enable_chip(pin);
  742. }
  743. /*---------------------------------------------------------------------------*/
  744. S32 mt_set_gpio_pull_select(u32 pin, u32 select)
  745. {
  746. mt_gpio_pin_decrypt(&pin);
  747. return mt_set_gpio_pull_select_chip(pin, select);
  748. }
  749. /*---------------------------------------------------------------------------*/
  750. S32 mt_get_gpio_pull_select(u32 pin)
  751. {
  752. mt_gpio_pin_decrypt(&pin);
  753. return mt_get_gpio_pull_select_chip(pin);
  754. }
  755. /*---------------------------------------------------------------------------*/
  756. S32 mt_set_gpio_smt(u32 pin, u32 enable)
  757. {
  758. mt_gpio_pin_decrypt(&pin);
  759. return mt_set_gpio_smt_chip(pin, enable);
  760. }
  761. /*---------------------------------------------------------------------------*/
  762. S32 mt_get_gpio_smt(u32 pin)
  763. {
  764. mt_gpio_pin_decrypt(&pin);
  765. return mt_get_gpio_smt_chip(pin);
  766. }
  767. /*---------------------------------------------------------------------------*/
  768. S32 mt_set_gpio_ies(u32 pin, u32 enable)
  769. {
  770. mt_gpio_pin_decrypt(&pin);
  771. return mt_set_gpio_ies_chip(pin, enable);
  772. }
  773. /*---------------------------------------------------------------------------*/
  774. S32 mt_get_gpio_ies(u32 pin)
  775. {
  776. mt_gpio_pin_decrypt(&pin);
  777. return mt_get_gpio_ies_chip(pin);
  778. }
  779. /*---------------------------------------------------------------------------*/
  780. S32 mt_set_gpio_out(u32 pin, u32 output)
  781. {
  782. mt_gpio_pin_decrypt(&pin);
  783. return mt_set_gpio_out_chip(pin, output);
  784. }
  785. /*---------------------------------------------------------------------------*/
  786. S32 mt_get_gpio_out(u32 pin)
  787. {
  788. mt_gpio_pin_decrypt(&pin);
  789. return mt_get_gpio_out_chip(pin);
  790. }
  791. /*---------------------------------------------------------------------------*/
  792. S32 mt_get_gpio_in(u32 pin)
  793. {
  794. mt_gpio_pin_decrypt(&pin);
  795. return mt_get_gpio_in_chip(pin);
  796. }
  797. /*---------------------------------------------------------------------------*/
  798. S32 mt_set_gpio_mode(u32 pin, u32 mode)
  799. {
  800. mt_gpio_pin_decrypt(&pin);
  801. return mt_set_gpio_mode_chip(pin, mode);
  802. }
  803. /*---------------------------------------------------------------------------*/
  804. S32 mt_get_gpio_mode(u32 pin)
  805. {
  806. mt_gpio_pin_decrypt(&pin);
  807. return mt_get_gpio_mode_chip(pin);
  808. }
  809. /*---------------------------------------------------------------------------*/
  810. S32 mt_set_gpio_drving(u32 pin, GPIO_DRV driving)
  811. {
  812. mt_gpio_pin_decrypt(&pin);
  813. return mt_set_gpio_driving_chip(pin, driving);
  814. }
  815. /*---------------------------------------------------------------------------*/
  816. S32 mt_get_gpio_drving(u32 pin)
  817. {
  818. mt_gpio_pin_decrypt(&pin);
  819. return mt_get_gpio_driving_chip(pin);
  820. }
  821. /*Special request for reading input value high, low, floating*/
  822. S32 mt_get_gpio_value(int gpio)
  823. {
  824. char value_up, value_down;
  825. mt_set_gpio_mode(gpio, GPIO_MODE_00);
  826. mt_set_gpio_dir(gpio, GPIO_DIR_IN);
  827. mt_set_gpio_pull_enable(gpio, GPIO_PULL_ENABLE);
  828. mt_set_gpio_pull_select(gpio, GPIO_PULL_UP);
  829. value_up = mt_get_gpio_in(gpio);
  830. mt_set_gpio_pull_select(gpio, GPIO_PULL_DOWN);
  831. value_down = mt_get_gpio_in(gpio);
  832. return value_down | (value_up << 0x01);
  833. }
  834. /*****************************************************************************/
  835. /* sysfs operation */
  836. /*****************************************************************************/
  837. #endif