gpio.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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/mt_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 ((PULLEN_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 (PULLEN_offset[pin].offset == (S8)-1)
  300. GPIO_CLR_BITS(PUPD_addr[pin].addr, 3L << PUPD_offset[pin].offset);
  301. else
  302. GPIO_CLR_BITS(PULLEN_addr[pin].addr, 1L << PULLEN_offset[pin].offset);
  303. } else if (en == GPIO_PULL_ENABLE) {
  304. if (PULLEN_offset[pin].offset == (S8)-1) {
  305. /* For PUPD+R0+R1 Type, mt_set_gpio_pull_enable does not know
  306. * which one between PU and PD shall be enabled.
  307. * Use R0 to guarantee at one resistor is set when lk
  308. * apply default setting
  309. */
  310. GPIO_SET_BITS(PUPD_addr[pin].addr, 1L << PUPD_offset[pin].offset);
  311. GPIO_CLR_BITS(PUPD_addr[pin].addr, 1L << (PUPD_offset[pin].offset + 1));
  312. } else {
  313. /* For PULLEN + PULLSEL Type */
  314. GPIO_SET_BITS(PULLEN_addr[pin].addr, 1L << PULLEN_offset[pin].offset);
  315. }
  316. } else if (en == GPIO_PULL_ENABLE_R0) {
  317. if (PUPD_offset[pin].offset == (S8)-1)
  318. return GPIO_PULL_EN_UNSUPPORTED;
  319. /* IOConfig does not support MWR operation, so use CLR + SET */
  320. GPIO_SET_BITS(PUPD_addr[pin].addr, 1L << PUPD_offset[pin].offset);
  321. GPIO_CLR_BITS(PUPD_addr[pin].addr, 1L << (PUPD_offset[pin].offset + 1));
  322. } else if (en == GPIO_PULL_ENABLE_R1) {
  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_CLR_BITS(PUPD_addr[pin].addr, 1L << PUPD_offset[pin].offset);
  327. GPIO_SET_BITS(PUPD_addr[pin].addr, 1L << (PUPD_offset[pin].offset + 1));
  328. } else if (en == GPIO_PULL_ENABLE_R0R1) {
  329. if (PUPD_offset[pin].offset == (S8)-1)
  330. return GPIO_PULL_EN_UNSUPPORTED;
  331. GPIO_SET_BITS(PUPD_addr[pin].addr, 3L << PUPD_offset[pin].offset);
  332. } else {
  333. return -ERINVAL;
  334. }
  335. return RSUCCESS;
  336. }
  337. /*---------------------------------------------------------------------------*/
  338. S32 mt_get_gpio_pull_enable_chip(u32 pin)
  339. {
  340. u32 reg;
  341. if (pin >= MAX_GPIO_PIN)
  342. return -ERINVAL;
  343. if ((PULLEN_offset[pin].offset == (S8)-1) && (PUPD_offset[pin].offset == (S8)-1))
  344. return GPIO_PULL_EN_UNSUPPORTED;
  345. if (PULLEN_offset[pin].offset == (S8)-1) {
  346. reg = GPIO_RD32(PUPD_addr[pin].addr);
  347. return ((reg & (3L << PUPD_offset[pin].offset)) ? 1 : 0);
  348. } else if (PUPD_offset[pin].offset == (S8)-1) {
  349. reg = GPIO_RD32(PULLEN_addr[pin].addr);
  350. return ((reg & (1L << PULLEN_offset[pin].offset)) ? 1 : 0);
  351. }
  352. return -ERINVAL;
  353. }
  354. /*---------------------------------------------------------------------------*/
  355. S32 mt_set_gpio_pull_select_chip(u32 pin, u32 sel)
  356. {
  357. if (pin >= MAX_GPIO_PIN)
  358. return -ERINVAL;
  359. if ((PULLSEL_offset[pin].offset == (S8)-1) && (PUPD_offset[pin].offset == (S8)-1))
  360. return GPIO_PULL_UNSUPPORTED;
  361. if (sel == GPIO_NO_PULL) {
  362. /* Regard No PULL as PULL disable + pull down */
  363. mt_set_gpio_pull_enable_chip(pin, GPIO_PULL_DISABLE);
  364. if (PULLSEL_offset[pin].offset == (S8)-1)
  365. GPIO_SET_BITS(PUPD_addr[pin].addr, 1L << (PUPD_offset[pin].offset + 2));
  366. else
  367. GPIO_CLR_BITS(PULLSEL_addr[pin].addr, 1L << PULLSEL_offset[pin].offset);
  368. } else if (sel == GPIO_PULL_UP) {
  369. if (PULLSEL_offset[pin].offset == (S8)-1)
  370. GPIO_CLR_BITS(PUPD_addr[pin].addr, 1L << (PUPD_offset[pin].offset + 2));
  371. else
  372. GPIO_SET_BITS(PULLSEL_addr[pin].addr, 1L << PULLSEL_offset[pin].offset);
  373. } else if (sel == GPIO_PULL_DOWN) {
  374. if (PULLSEL_offset[pin].offset == (S8)-1)
  375. GPIO_SET_BITS(PUPD_addr[pin].addr, 1L << (PUPD_offset[pin].offset + 2));
  376. else
  377. GPIO_CLR_BITS(PULLSEL_addr[pin].addr, 1L << PULLSEL_offset[pin].offset);
  378. } else {
  379. return -ERINVAL;
  380. }
  381. return RSUCCESS;
  382. }
  383. /*---------------------------------------------------------------------------*/
  384. /* get pull-up or pull-down, regardless of resistor value */
  385. S32 mt_get_gpio_pull_select_chip(u32 pin)
  386. {
  387. u32 reg;
  388. if (pin >= MAX_GPIO_PIN)
  389. return -ERINVAL;
  390. if ((PULLSEL_offset[pin].offset == (S8)-1) && (PUPD_offset[pin].offset == (S8)-1))
  391. return GPIO_PULL_UNSUPPORTED;
  392. if (PULLSEL_offset[pin].offset == (S8)-1) {
  393. reg = GPIO_RD32(PUPD_addr[pin].addr);
  394. /* Reg value: 0 for PU, 1 for PD --> reverse return value */
  395. return (S32)((reg & (1L << (PUPD_offset[pin].offset+2))) ? GPIO_PULL_DOWN : GPIO_PULL_UP);
  396. } else if (PUPD_offset[pin].offset == (S8)-1) {
  397. reg = GPIO_RD32(PULLSEL_addr[pin].addr);
  398. return (S32)((reg & (1L << PULLSEL_offset[pin].offset)) ? GPIO_PULL_UP : GPIO_PULL_DOWN);
  399. }
  400. return -ERINVAL;
  401. }
  402. /*---------------------------------------------------------------------------*/
  403. void mt_gpio_pin_decrypt(u32 *cipher)
  404. {
  405. //just for debug, find out who used pin number directly
  406. if ((*cipher & (0x80000000)) == 0) {
  407. GPIOERR("Pin %d decrypt warning! \n",*cipher);
  408. //dump_stack();
  409. //return;
  410. }
  411. //GPIOERR("Pin magic number is %x\n",*cipher);
  412. *cipher &= ~(0x80000000);
  413. return;
  414. }
  415. /*---------------------------------------------------------------------------*/
  416. S32 mt_set_gpio_dir(u32 pin, u32 dir)
  417. {
  418. mt_gpio_pin_decrypt(&pin);
  419. return mt_set_gpio_dir_chip(pin,dir);
  420. }
  421. /*---------------------------------------------------------------------------*/
  422. S32 mt_get_gpio_dir(u32 pin)
  423. {
  424. mt_gpio_pin_decrypt(&pin);
  425. return mt_get_gpio_dir_chip(pin);
  426. }
  427. /*---------------------------------------------------------------------------*/
  428. S32 mt_set_gpio_pull_enable(u32 pin, u32 enable)
  429. {
  430. mt_gpio_pin_decrypt(&pin);
  431. return mt_set_gpio_pull_enable_chip(pin,enable);
  432. }
  433. /*---------------------------------------------------------------------------*/
  434. S32 mt_get_gpio_pull_enable(u32 pin)
  435. {
  436. mt_gpio_pin_decrypt(&pin);
  437. return mt_get_gpio_pull_enable_chip(pin);
  438. }
  439. /*---------------------------------------------------------------------------*/
  440. S32 mt_set_gpio_pull_select(u32 pin, u32 select)
  441. {
  442. mt_gpio_pin_decrypt(&pin);
  443. return mt_set_gpio_pull_select_chip(pin,select);
  444. }
  445. /*---------------------------------------------------------------------------*/
  446. S32 mt_get_gpio_pull_select(u32 pin)
  447. {
  448. mt_gpio_pin_decrypt(&pin);
  449. return mt_get_gpio_pull_select_chip(pin);
  450. }
  451. /*---------------------------------------------------------------------------*/
  452. S32 mt_set_gpio_smt(u32 pin, u32 enable)
  453. {
  454. mt_gpio_pin_decrypt(&pin);
  455. return mt_set_gpio_smt_chip(pin,enable);
  456. }
  457. /*---------------------------------------------------------------------------*/
  458. S32 mt_get_gpio_smt(u32 pin)
  459. {
  460. mt_gpio_pin_decrypt(&pin);
  461. return mt_get_gpio_smt_chip(pin);
  462. }
  463. /*---------------------------------------------------------------------------*/
  464. S32 mt_set_gpio_ies(u32 pin, u32 enable)
  465. {
  466. mt_gpio_pin_decrypt(&pin);
  467. return mt_set_gpio_ies_chip(pin,enable);
  468. }
  469. /*---------------------------------------------------------------------------*/
  470. S32 mt_get_gpio_ies(u32 pin)
  471. {
  472. mt_gpio_pin_decrypt(&pin);
  473. return mt_get_gpio_ies_chip(pin);
  474. }
  475. /*---------------------------------------------------------------------------*/
  476. S32 mt_set_gpio_out(u32 pin, u32 output)
  477. {
  478. mt_gpio_pin_decrypt(&pin);
  479. return mt_set_gpio_out_chip(pin,output);
  480. }
  481. /*---------------------------------------------------------------------------*/
  482. S32 mt_get_gpio_out(u32 pin)
  483. {
  484. mt_gpio_pin_decrypt(&pin);
  485. return mt_get_gpio_out_chip(pin);
  486. }
  487. /*---------------------------------------------------------------------------*/
  488. S32 mt_get_gpio_in(u32 pin)
  489. {
  490. mt_gpio_pin_decrypt(&pin);
  491. return mt_get_gpio_in_chip(pin);
  492. }
  493. /*---------------------------------------------------------------------------*/
  494. S32 mt_set_gpio_mode(u32 pin, u32 mode)
  495. {
  496. mt_gpio_pin_decrypt(&pin);
  497. return mt_set_gpio_mode_chip(pin,mode);
  498. }
  499. /*---------------------------------------------------------------------------*/
  500. S32 mt_get_gpio_mode(u32 pin)
  501. {
  502. mt_gpio_pin_decrypt(&pin);
  503. return mt_get_gpio_mode_chip(pin);
  504. }
  505. /*****************************************************************************/
  506. /* Self test operation */
  507. /*****************************************************************************/
  508. #if GPIO_SELF_TEST
  509. void mt_gpio_self_test(void)
  510. {
  511. int i, val;
  512. for (i = 0; i < GPIO_MAX; i++) {
  513. S32 res,old;
  514. GPIOMSG("GPIO-%3d test\n", i);
  515. /*direction test*/
  516. old = mt_get_gpio_dir(i);
  517. if (old == 0 || old == 1) {
  518. GPIOLOG(" dir old = %d\n", old);
  519. } else {
  520. GPIOERR(" test dir fail: %d\n", old);
  521. break;
  522. }
  523. if ((res = mt_set_gpio_dir(i, GPIO_DIR_OUT)) != RSUCCESS) {
  524. GPIOERR(" set dir out fail: %d\n", res);
  525. break;
  526. } else if ((res = mt_get_gpio_dir(i)) != GPIO_DIR_OUT) {
  527. GPIOERR(" get dir out fail: %d\n", res);
  528. break;
  529. } else {
  530. /*output test*/
  531. S32 out = mt_get_gpio_out(i);
  532. if (out != 0 && out != 1) {
  533. GPIOERR(" get out fail = %d\n", old);
  534. break;
  535. }
  536. for (val = 0; val < GPIO_OUT_MAX; val++) {
  537. if ((res = mt_set_gpio_out(i,0)) != RSUCCESS) {
  538. GPIOERR(" set out[%d] fail: %d\n", val, res);
  539. break;
  540. } else if ((res = mt_get_gpio_out(i)) != 0) {
  541. GPIOERR(" get out[%d] fail: %d\n", val, res);
  542. break;
  543. }
  544. }
  545. if ((res = mt_set_gpio_out(i,out)) != RSUCCESS) {
  546. GPIOERR(" restore out fail: %d\n", res);
  547. break;
  548. }
  549. }
  550. if ((res = mt_set_gpio_dir(i, GPIO_DIR_IN)) != RSUCCESS) {
  551. GPIOERR(" set dir in fail: %d\n", res);
  552. break;
  553. } else if ((res = mt_get_gpio_dir(i)) != GPIO_DIR_IN) {
  554. GPIOERR(" get dir in fail: %d\n", res);
  555. break;
  556. } else {
  557. GPIOLOG(" input data = %d\n", res);
  558. }
  559. if ((res = mt_set_gpio_dir(i, old)) != RSUCCESS) {
  560. GPIOERR(" restore dir fail: %d\n", res);
  561. break;
  562. }
  563. for (val = 0; val < GPIO_PULL_EN_MAX; val++) {
  564. if ((res = mt_set_gpio_pull_enable(i,val)) != RSUCCESS) {
  565. GPIOERR(" set pullen[%d] fail: %d\n", val, res);
  566. break;
  567. } else if ((res = mt_get_gpio_pull_enable(i)) != val) {
  568. GPIOERR(" get pullen[%d] fail: %d\n", val, res);
  569. break;
  570. }
  571. }
  572. if ((res = mt_set_gpio_pull_enable(i, old)) != RSUCCESS) {
  573. GPIOERR(" restore pullen fail: %d\n", res);
  574. break;
  575. }
  576. /*pull select test*/
  577. old = mt_get_gpio_pull_select(i);
  578. if (old == 0 || old == 1)
  579. GPIOLOG(" pullsel old = %d\n", old);
  580. else {
  581. GPIOERR(" pullsel fail: %d\n", old);
  582. break;
  583. }
  584. for (val = 0; val < GPIO_PULL_MAX; val++) {
  585. if ((res = mt_set_gpio_pull_select(i,val)) != RSUCCESS) {
  586. GPIOERR(" set pullsel[%d] fail: %d\n", val, res);
  587. break;
  588. } else if ((res = mt_get_gpio_pull_select(i)) != val) {
  589. GPIOERR(" get pullsel[%d] fail: %d\n", val, res);
  590. break;
  591. }
  592. }
  593. if ((res = mt_set_gpio_pull_select(i, old)) != RSUCCESS) {
  594. GPIOERR(" restore pullsel fail: %d\n", res);
  595. break;
  596. }
  597. /*mode control*/
  598. old = mt_get_gpio_mode(i);
  599. if ((old >= GPIO_MODE_00) && (val < GPIO_MODE_MAX)) {
  600. GPIOLOG(" mode old = %d\n", old);
  601. } else {
  602. GPIOERR(" get mode fail: %d\n", old);
  603. break;
  604. }
  605. for (val = 0; val < GPIO_MODE_MAX; val++) {
  606. if ((res = mt_set_gpio_mode(i, val)) != RSUCCESS) {
  607. GPIOERR("set mode[%d] fail: %d\n", val, res);
  608. break;
  609. } else if ((res = mt_get_gpio_mode(i)) != val) {
  610. GPIOERR("get mode[%d] fail: %d\n", val, res);
  611. break;
  612. }
  613. }
  614. if ((res = mt_set_gpio_mode(i,old)) != RSUCCESS) {
  615. GPIOERR(" restore mode fail: %d\n", res);
  616. break;
  617. }
  618. }
  619. GPIOLOG("GPIO test done\n");
  620. }
  621. /*----------------------------------------------------------------------------*/
  622. void mt_gpio_dump(void)
  623. {
  624. GPIO_REGS *regs = (GPIO_REGS*)(GPIO_BASE);
  625. int idx;
  626. GPIOMSG("---# dir #-----------------------------------------------------------------\n");
  627. for (idx = 0; idx < sizeof(regs->dir)/sizeof(regs->dir[0]); idx++) {
  628. GPIOMSG("0x%04X ", regs->dir[idx].val);
  629. if (7 == (idx % 8)) GPIOMSG("\n");
  630. }
  631. #if 0
  632. GPIOMSG("\n---# pullen #--------------------------------------------------------------\n");
  633. for (idx = 0; idx < sizeof(regs->pullen)/sizeof(regs->pullen[0]); idx++) {
  634. GPIOMSG("0x%04X ", regs->pullen[idx].val);
  635. if (7 == (idx % 8)) GPIOMSG("\n");
  636. }
  637. GPIOMSG("\n---# pullsel #-------------------------------------------------------------\n");
  638. for (idx = 0; idx < sizeof(regs->pullsel)/sizeof(regs->pullsel[0]); idx++) {
  639. GPIOMSG("0x%04X ", regs->pullsel[idx].val);
  640. if (7 == (idx % 8)) GPIOMSG("\n");
  641. }
  642. #endif
  643. GPIOMSG("\n---# dout #----------------------------------------------------------------\n");
  644. for (idx = 0; idx < sizeof(regs->dout)/sizeof(regs->dout[0]); idx++) {
  645. GPIOMSG("0x%04X ", regs->dout[idx].val);
  646. if (7 == (idx % 8)) GPIOMSG("\n");
  647. }
  648. GPIOMSG("\n---# din #----------------------------------------------------------------\n");
  649. for (idx = 0; idx < sizeof(regs->din)/sizeof(regs->din[0]); idx++) {
  650. GPIOMSG("0x%04X ", regs->din[idx].val);
  651. if (7 == (idx % 8)) GPIOMSG("\n");
  652. }
  653. GPIOMSG("\n---# mode #----------------------------------------------------------------\n");
  654. for (idx = 0; idx < sizeof(regs->mode)/sizeof(regs->mode[0]); idx++) {
  655. GPIOMSG("0x%04X ", regs->mode[idx].val);
  656. if (7 == (idx % 8)) GPIOMSG("\n");
  657. }
  658. #if 0
  659. GPIOMSG("sim0 0x%04X ", regs->sim_ctrl[0].val);
  660. GPIOMSG("sim1 0x%04X ", regs->sim_ctrl[1].val);
  661. GPIOMSG("sim2 0x%04X ", regs->sim_ctrl[2].val);
  662. GPIOMSG("sim3 0x%04X ", regs->sim_ctrl[3].val);
  663. GPIOMSG("keypad0 0x%04X ", regs->kpad_ctrl[0].val);
  664. GPIOMSG("keypad1 0x%04X ", regs->kpad_ctrl[1].val);
  665. #endif
  666. GPIOMSG("\n---------------------------------------------------------------------------\n");
  667. }
  668. /*---------------------------------------------------------------------------*/
  669. #if 0
  670. void mt_gpio_read_pin(GPIO_CFG* cfg, int method)
  671. {
  672. if (method == 0) {
  673. GPIO_REGS *cur = (GPIO_REGS*)GPIO_BASE;
  674. u32 mask = (1L << GPIO_MODE_BITS) - 1;
  675. int num, bit,idx=cfg->no;
  676. num = idx / MAX_GPIO_REG_BITS;
  677. bit = idx % MAX_GPIO_REG_BITS;
  678. cfg->pullsel= (cur->pullsel[num].val & (1L << bit)) ? (1) : (0);
  679. cfg->din = (cur->din[num].val & (1L << bit)) ? (1) : (0);
  680. cfg->dout = (cur->dout[num].val & (1L << bit)) ? (1) : (0);
  681. cfg->pullen = (cur->pullen[num].val & (1L << bit)) ? (1) : (0);
  682. cfg->dir = (cur->dir[num].val & (1L << bit)) ? (1) : (0);
  683. num = idx / MAX_GPIO_MODE_PER_REG;
  684. bit = idx % MAX_GPIO_MODE_PER_REG;
  685. cfg->mode = (cur->mode[num].val >> (GPIO_MODE_BITS*bit)) & mask;
  686. } else if (method == 1) {
  687. int idx=cfg->no;
  688. cfg->pullsel= mt_get_gpio_pull_select(idx);
  689. cfg->din = mt_get_gpio_in(idx);
  690. cfg->dout = mt_get_gpio_out(idx);
  691. cfg->pullen = mt_get_gpio_pull_enable(idx);
  692. cfg->dir = mt_get_gpio_dir(idx);
  693. cfg->mode = mt_get_gpio_mode(idx);
  694. }
  695. }
  696. #endif
  697. /*---------------------------------------------------------------------------*/
  698. void mt_gpio_dump_addr(void)
  699. {
  700. int idx;
  701. struct mt_gpio_obj *obj = gpio_obj;
  702. GPIO_REGS *reg = obj->reg;
  703. GPIOMSG("# direction\n");
  704. for (idx = 0; idx < sizeof(reg->dir)/sizeof(reg->dir[0]); idx++)
  705. 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);
  706. #if 0
  707. GPIOMSG("# pull enable\n");
  708. for (idx = 0; idx < sizeof(reg->pullen)/sizeof(reg->pullen[0]); idx++)
  709. 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);
  710. GPIOMSG("# pull select\n");
  711. for (idx = 0; idx < sizeof(reg->pullsel)/sizeof(reg->pullsel[0]); idx++)
  712. 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);
  713. #endif
  714. GPIOMSG("# data output\n");
  715. for (idx = 0; idx < sizeof(reg->dout)/sizeof(reg->dout[0]); idx++)
  716. 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);
  717. GPIOMSG("# data input\n");
  718. for (idx = 0; idx < sizeof(reg->din)/sizeof(reg->din[0]); idx++)
  719. GPIOMSG("val[%2d] %p\n", idx, &reg->din[idx].val);
  720. GPIOMSG("# mode\n");
  721. for (idx = 0; idx < sizeof(reg->mode)/sizeof(reg->mode[0]); idx++)
  722. 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);
  723. }
  724. #endif
  725. #endif