mt_gpio.c 26 KB

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