gpio.c 27 KB

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