mt_gpio.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /******************************************************************************
  2. * mt_gpio.c - MTKLinux GPIO Device Driver
  3. *
  4. * Copyright 2008-2009 MediaTek Co.,Ltd.
  5. *
  6. * DESCRIPTION:
  7. * This file provid the other drivers GPIO relative functions
  8. *
  9. ******************************************************************************/
  10. #include <platform/mt_reg_base.h>
  11. #include <platform/mt_gpio.h>
  12. #include <platform/gpio_cfg.h>
  13. #include <debug.h>
  14. /******************************************************************************
  15. MACRO Definition
  16. ******************************************************************************/
  17. //#define GIO_SLFTEST
  18. #define GPIO_DEVICE "mt-gpio"
  19. #define VERSION GPIO_DEVICE
  20. #define GPIO_BRINGUP
  21. /*---------------------------------------------------------------------------*/
  22. #define GPIO_WR32(addr, data) DRV_WriteReg32(addr,data)
  23. #define GPIO_RD32(addr) DRV_Reg32(addr)
  24. #define GPIO_SW_SET_BITS(BIT,REG) GPIO_WR32(REG,GPIO_RD32(REG) | ((unsigned long)(BIT)))
  25. #define GPIO_SET_BITS(BIT,REG) ((*(volatile u32*)(REG)) = (u32)(BIT))
  26. #define GPIO_CLR_BITS(BIT,REG) ((*(volatile u32*)(REG)) &= ~((u32)(BIT)))
  27. /*---------------------------------------------------------------------------*/
  28. #define TRUE 1
  29. #define FALSE 0
  30. /*---------------------------------------------------------------------------*/
  31. //#define MAX_GPIO_REG_BITS 16
  32. //#define MAX_GPIO_MODE_PER_REG 5
  33. //#define GPIO_MODE_BITS 3
  34. /*---------------------------------------------------------------------------*/
  35. #define GPIOTAG "[GPIO] "
  36. #define GPIOLOG(fmt, arg...) dprintf(INFO,GPIOTAG fmt, ##arg)
  37. #define GPIOMSG(fmt, arg...) dprintf(INFO,fmt, ##arg)
  38. #define GPIOERR(fmt, arg...) dprintf(INFO,GPIOTAG "%5d: "fmt, __LINE__, ##arg)
  39. #define GPIOFUC(fmt, arg...) //dprintk(INFO,GPIOTAG "%s\n", __FUNCTION__)
  40. #define GIO_INVALID_OBJ(ptr) ((ptr) != gpio_obj)
  41. /******************************************************************************
  42. Enumeration/Structure
  43. ******************************************************************************/
  44. #if defined(MACH_FPGA)
  45. S32 mt_set_gpio_dir(u32 pin, u32 dir) {return RSUCCESS;}
  46. S32 mt_get_gpio_dir(u32 pin) {return GPIO_DIR_UNSUPPORTED;}
  47. S32 mt_set_gpio_pull_enable(u32 pin, u32 enable) {return RSUCCESS;}
  48. S32 mt_get_gpio_pull_enable(u32 pin) {return GPIO_PULL_EN_UNSUPPORTED;}
  49. S32 mt_set_gpio_pull_select(u32 pin, u32 select) {return RSUCCESS;}
  50. S32 mt_get_gpio_pull_select(u32 pin) {return GPIO_PULL_UNSUPPORTED;}
  51. S32 mt_set_gpio_smt(u32 pin, u32 enable) {return RSUCCESS;}
  52. S32 mt_get_gpio_smt(u32 pin) {return GPIO_SMT_UNSUPPORTED;}
  53. S32 mt_set_gpio_ies(u32 pin, u32 enable) {return RSUCCESS;}
  54. S32 mt_get_gpio_ies(u32 pin) {return GPIO_IES_UNSUPPORTED;}
  55. S32 mt_set_gpio_out(u32 pin, u32 output) {return RSUCCESS;}
  56. S32 mt_get_gpio_out(u32 pin) {return GPIO_OUT_UNSUPPORTED;}
  57. S32 mt_get_gpio_in(u32 pin) {return GPIO_IN_UNSUPPORTED;}
  58. S32 mt_set_gpio_mode(u32 pin, u32 mode) {return RSUCCESS;}
  59. S32 mt_get_gpio_mode(u32 pin) {return GPIO_MODE_UNSUPPORTED;}
  60. #else
  61. typedef struct
  62. {
  63. S32 en_flag;// 1 enable
  64. }GPIO_PUPD_flag;
  65. GPIO_PUPD_flag gpio_pupd_en_flag[MT_GPIO_BASE_MAX] = {{0}};
  66. struct mt_gpio_obj {
  67. GPIO_REGS *reg;
  68. };
  69. static struct mt_gpio_obj gpio_dat = {
  70. .reg = (GPIO_REGS*)(GPIO_BASE),
  71. };
  72. static struct mt_gpio_obj *gpio_obj = &gpio_dat;
  73. /*---------------------------------------------------------------------------*/
  74. S32 mt_set_gpio_dir_chip(u32 pin, u32 dir)
  75. {
  76. #ifndef GPIO_BRINGUP
  77. u32 pos;
  78. #endif
  79. u32 bit;
  80. u32 reg;
  81. if (pin >= MAX_GPIO_PIN)
  82. return -ERINVAL;
  83. if (dir >= GPIO_DIR_MAX)
  84. return -ERINVAL;
  85. //pos = pin / MAX_GPIO_REG_BITS;
  86. bit = DIR_offset[pin].offset;
  87. #ifdef GPIO_BRINGUP
  88. reg = GPIO_RD32(DIR_addr[pin].addr);
  89. if (dir == GPIO_DIR_IN)
  90. reg &= (~(1<<bit));
  91. else
  92. reg |= (1 << bit);
  93. GPIO_WR32(DIR_addr[pin].addr,reg);
  94. #else
  95. if (dir == GPIO_DIR_IN)
  96. GPIO_SET_BITS((1L << bit), &obj->reg->dir[pos].rst);
  97. else
  98. GPIO_SET_BITS((1L << bit), &obj->reg->dir[pos].set);
  99. #endif
  100. return RSUCCESS;
  101. }
  102. /*---------------------------------------------------------------------------*/
  103. S32 mt_get_gpio_dir_chip(u32 pin)
  104. {
  105. u32 bit;
  106. u32 reg;
  107. struct mt_gpio_obj *obj = gpio_obj;
  108. if (!obj)
  109. return -ERACCESS;
  110. if (pin >= MAX_GPIO_PIN)
  111. return -ERINVAL;
  112. bit = DIR_offset[pin].offset;
  113. reg = GPIO_RD32(DIR_addr[pin].addr);
  114. return (((reg & (1L << bit)) != 0)? 1: 0);
  115. }
  116. /*---------------------------------------------------------------------------*/
  117. S32 mt_set_gpio_pull_enable_chip(u32 pin, u32 enable)
  118. {
  119. u32 reg=0;
  120. u32 bit=0;
  121. if (pin >= MAX_GPIO_PIN)
  122. return -ERINVAL;
  123. #ifdef GPIO_BRINGUP
  124. if(PULLEN_offset[pin].offset == -1 && pupd_offset[pin].offset==-1){
  125. return GPIO_PULL_EN_UNSUPPORTED;
  126. }
  127. /*
  128. if(pin>=160 && pin <=165){
  129. bit = pupd_offset[pin].offset;
  130. reg = GPIO_RD32(pupd_addr[pin].addr);
  131. if (enable == GPIO_PULL_DISABLE){
  132. reg &= ~(0x7 << bit);
  133. }
  134. else{
  135. reg |=(1 << bit);
  136. }
  137. GPIO_WR32(pupd_addr[pin].addr, reg);
  138. return RSUCCESS;
  139. }
  140. */
  141. if(PULLEN_offset[pin].offset != -1){
  142. bit = PULLEN_offset[pin].offset;
  143. reg = GPIO_RD32(PULLEN_addr[pin].addr);
  144. if (enable == GPIO_PULL_DISABLE)
  145. reg &= (~(1<<bit));
  146. else
  147. reg |= (1 << bit);
  148. GPIO_WR32(PULLEN_addr[pin].addr, reg);
  149. }
  150. else
  151. {
  152. bit = pupd_offset[pin].offset;
  153. reg = GPIO_RD32(pupd_addr[pin].addr);
  154. if (enable == GPIO_PULL_DISABLE){
  155. reg &= (~(0x7 << bit)); //0x0 or 0x4 disable
  156. gpio_pupd_en_flag[pin].en_flag=0;
  157. }
  158. else{
  159. reg |=(1 << bit);
  160. gpio_pupd_en_flag[pin].en_flag=1;
  161. }
  162. GPIO_WR32(pupd_addr[pin].addr, reg);
  163. }
  164. #else
  165. if(PULLEN_offset[pin].offset == -1){
  166. return GPIO_PULL_EN_UNSUPPORTED;
  167. }
  168. else{
  169. if (enable == GPIO_PULL_DISABLE)
  170. GPIO_SET_BITS((1L << (PULLEN_offset[pin].offset)), PULLEN_addr[pin].addr + 8);
  171. else
  172. GPIO_SET_BITS((1L << (PULLEN_offset[pin].offset)), PULLEN_addr[pin].addr + 4);
  173. }
  174. #endif
  175. return RSUCCESS;
  176. }
  177. /*---------------------------------------------------------------------------*/
  178. S32 mt_get_gpio_pull_enable_chip(u32 pin)
  179. {
  180. unsigned long data;
  181. u32 bit=0;
  182. if (pin >= MAX_GPIO_PIN)
  183. return -ERINVAL;
  184. if(PULLEN_offset[pin].offset == -1 && pupd_offset[pin].offset==-1){
  185. return GPIO_PULL_EN_UNSUPPORTED;
  186. }
  187. if(PULLEN_offset[pin].offset != -1){
  188. bit = PULLEN_offset[pin].offset;
  189. data = GPIO_RD32(PULLEN_addr[pin].addr);
  190. return (((data & (1L << bit)) != 0)? 1: 0);
  191. }else{
  192. bit = pupd_offset[pin].offset;
  193. data = GPIO_RD32(pupd_addr[pin].addr);
  194. return (((data & (0x7 << bit)) != 0)? 1: 0);
  195. }
  196. }
  197. /*---------------------------------------------------------------------------*/
  198. S32 mt_set_gpio_smt_chip(u32 pin, u32 enable)
  199. {
  200. //unsigned long flags;
  201. u32 reg=0;
  202. u32 bit=0;
  203. if (pin >= MAX_GPIO_PIN)
  204. return -ERINVAL;
  205. #ifdef GPIO_BRINGUP
  206. if(SMT_offset[pin].offset == -1){
  207. return GPIO_SMT_UNSUPPORTED;
  208. }
  209. else{
  210. bit = SMT_offset[pin].offset;
  211. reg = GPIO_RD32(SMT_addr[pin].addr);
  212. if (enable == GPIO_SMT_DISABLE)
  213. reg &= (~(1<<bit));
  214. else
  215. reg |= (1 << bit);
  216. }
  217. //printf("SMT addr(%x),value(%x)\n",SMT_addr[pin].addr,GPIO_RD32(SMT_addr[pin].addr));
  218. GPIO_WR32(SMT_addr[pin].addr, reg);
  219. #else
  220. if(SMT_offset[pin].offset == -1){
  221. return GPIO_SMT_UNSUPPORTED;
  222. }
  223. else{
  224. if (enable == GPIO_SMT_DISABLE)
  225. GPIO_SET_BITS((1L << (SMT_offset[pin].offset)), SMT_addr[pin].addr + 8);
  226. else
  227. GPIO_SET_BITS((1L << (SMT_offset[pin].offset)), SMT_addr[pin].addr + 4);
  228. }
  229. #endif
  230. return RSUCCESS;
  231. }
  232. /*---------------------------------------------------------------------------*/
  233. S32 mt_get_gpio_smt_chip(u32 pin)
  234. {
  235. unsigned long data;
  236. u32 bit =0;
  237. bit = SMT_offset[pin].offset;
  238. if (pin >= MAX_GPIO_PIN)
  239. return -ERINVAL;
  240. if(SMT_offset[pin].offset == -1){
  241. return GPIO_SMT_UNSUPPORTED;
  242. }
  243. else{
  244. data = GPIO_RD32(SMT_addr[pin].addr);
  245. return (((data & (1L << bit)) != 0)? 1: 0);
  246. }
  247. }
  248. /*---------------------------------------------------------------------------*/
  249. S32 mt_set_gpio_ies_chip(u32 pin, u32 enable)
  250. {
  251. //unsigned long flags;
  252. if (pin >= MAX_GPIO_PIN)
  253. return -ERINVAL;
  254. if(IES_offset[pin].offset == -1){
  255. return GPIO_IES_UNSUPPORTED;
  256. }
  257. else{
  258. if (enable == GPIO_IES_DISABLE)
  259. GPIO_SET_BITS((1L << (IES_offset[pin].offset)), IES_addr[pin].addr + 8);
  260. else
  261. GPIO_SET_BITS((1L << (IES_offset[pin].offset)), IES_addr[pin].addr + 4);
  262. }
  263. return RSUCCESS;
  264. }
  265. /*---------------------------------------------------------------------------*/
  266. S32 mt_get_gpio_ies_chip(u32 pin)
  267. {
  268. unsigned long data;
  269. if (pin >= MAX_GPIO_PIN)
  270. return -ERINVAL;
  271. if(IES_offset[pin].offset == -1){
  272. return GPIO_IES_UNSUPPORTED;
  273. }
  274. else{
  275. data = GPIO_RD32(IES_addr[pin].addr);
  276. return (((data & (1L << (IES_offset[pin].offset))) != 0)? 1: 0);
  277. }
  278. }
  279. /*---------------------------------------------------------------------------*/
  280. S32 mt_set_gpio_pull_select_chip(u32 pin, u32 select)
  281. {
  282. //unsigned long flags;
  283. u32 reg=0;
  284. unsigned long bit = 0;
  285. u32 mask = (1L << 4) - 1;
  286. if (pin >= MAX_GPIO_PIN)
  287. return -ERINVAL;
  288. #ifdef GPIO_BRINGUP
  289. if((PULL_offset[pin].offset == -1) &&(pupd_offset[pin].offset == -1)) {
  290. return GPIO_PULL_UNSUPPORTED;
  291. }
  292. if(pin>=160 && pin <=165){
  293. if(0==gpio_pupd_en_flag[pin].en_flag){
  294. return RSUCCESS;
  295. }
  296. bit =pupd_offset[pin].offset;//special spec in pupd reg different form pullsel
  297. reg = GPIO_RD32(pupd_addr[pin].addr);
  298. if (select == GPIO_PULL_UP)//0x2 pull up
  299. {
  300. reg &= (~(mask << bit));
  301. reg |= (0x2 << bit);
  302. }
  303. else //0x6 pull down
  304. {
  305. reg &= (~(mask << bit));
  306. reg |= (0x6 << bit);
  307. }
  308. //printf("fwq pullset pin=%d,3.............\n",pin);
  309. GPIO_WR32(pupd_addr[pin].addr, reg);
  310. return RSUCCESS;
  311. }
  312. //printf("fwq pullset pin=%d,select(%d)\n",pin,select);
  313. if(PULL_offset[pin].offset != -1){
  314. bit = PULL_offset[pin].offset;
  315. reg = GPIO_RD32(PULL_addr[pin].addr);
  316. if (select == GPIO_PULL_DOWN)
  317. reg &= (~(1<<bit));
  318. else
  319. reg |= (1 << bit);
  320. //printf("fwq pullset pin=%d,2.............\n",pin);
  321. GPIO_WR32(PULL_addr[pin].addr, reg);
  322. }
  323. else {
  324. if(0==gpio_pupd_en_flag[pin].en_flag){
  325. return RSUCCESS;
  326. }
  327. bit =pupd_offset[pin].offset+2;//special spec in pupd reg different form pullsel
  328. reg = GPIO_RD32(pupd_addr[pin].addr);
  329. if (select == GPIO_PULL_UP)
  330. reg &= (~(1<<bit));
  331. else
  332. reg |= (1 << bit);
  333. //printf("fwq pullset pin=%d,3.............\n",pin);
  334. GPIO_WR32(pupd_addr[pin].addr, reg);
  335. }
  336. #else
  337. if((PULL_offset[pin].offset == -1) ){
  338. return GPIO_PULL_UNSUPPORTED;
  339. }
  340. else{
  341. if (select == GPIO_PULL_DOWN)
  342. GPIO_SET_BITS((1L << (PULL_offset[pin].offset)), PULL_addr[pin].addr + 8);
  343. else
  344. GPIO_SET_BITS((1L << (PULL_offset[pin].offset)), PULL_addr[pin].addr + 4);
  345. }
  346. #endif
  347. return RSUCCESS;
  348. }
  349. /*---------------------------------------------------------------------------*/
  350. S32 mt_get_gpio_pull_select_chip(u32 pin)
  351. {
  352. unsigned long data;
  353. unsigned long bit = 0;
  354. u32 mask = (1L << 4) - 1;
  355. if(pin>=160 && pin <=165){
  356. bit =pupd_offset[pin].offset;
  357. data = GPIO_RD32(pupd_addr[pin].addr);
  358. return (((((data >> bit) & mask)==0x2))? 1: 0);//0x2 pull up
  359. }
  360. if((PULL_offset[pin].offset == -1) && (pupd_offset[pin].offset == -1)){
  361. return GPIO_PULL_UNSUPPORTED;
  362. }
  363. else if(PULL_offset[pin].offset == -1){
  364. data = GPIO_RD32(pupd_addr[pin].addr);
  365. return (((data & (1L << (pupd_offset[pin].offset+2))) != 0)? 0: 1);
  366. }
  367. else{
  368. data = GPIO_RD32(PULL_addr[pin].addr);
  369. return (((data & (1L << (PULL_offset[pin].offset))) != 0)? 1: 0);
  370. }
  371. #if 0
  372. unsigned long data;
  373. if (pin >= MAX_GPIO_PIN)
  374. return -ERINVAL;
  375. if((PULL_offset[pin].offset == -1) ){
  376. return GPIO_PULL_UNSUPPORTED;
  377. }
  378. else{
  379. data = GPIO_RD32(PULL_addr[pin].addr);
  380. return (((data & (1L << (PULL_offset[pin].offset))) != 0)? 1: 0);
  381. }
  382. #endif
  383. }
  384. /*---------------------------------------------------------------------------*/
  385. S32 mt_set_gpio_out_chip(u32 pin, u32 output)
  386. {
  387. #ifndef GPIO_BRINGUP
  388. u32 pos;
  389. #endif
  390. u32 bit;
  391. u32 reg=0;
  392. //struct mt_gpio_obj *obj = gpio_obj;
  393. if (pin >= MAX_GPIO_PIN)
  394. return -ERINVAL;
  395. if (output >= GPIO_OUT_MAX)
  396. return -ERINVAL;
  397. bit = DATAOUT_offset[pin].offset;
  398. #ifdef GPIO_BRINGUP
  399. reg = GPIO_RD32(DATAOUT_addr[pin].addr);
  400. if (output == GPIO_OUT_ZERO)
  401. reg &= (~(1<<bit));
  402. else
  403. reg |= (1 << bit);
  404. GPIO_WR32(DATAOUT_addr[pin].addr,reg);
  405. #else
  406. if (output == GPIO_OUT_ZERO)
  407. GPIO_SET_BITS((1L << bit), &obj->reg->dout[pos].rst);
  408. else
  409. GPIO_SET_BITS((1L << bit), &obj->reg->dout[pos].set);
  410. #endif
  411. return RSUCCESS;
  412. }
  413. /*---------------------------------------------------------------------------*/
  414. S32 mt_get_gpio_out_chip(u32 pin)
  415. {
  416. u32 pos;
  417. u32 bit;
  418. u32 reg;
  419. struct mt_gpio_obj *obj = gpio_obj;
  420. if (!obj)
  421. return -ERACCESS;
  422. if (pin >= MAX_GPIO_PIN)
  423. return -ERINVAL;
  424. pos = pin / MAX_GPIO_REG_BITS;
  425. bit = pin % MAX_GPIO_REG_BITS;
  426. reg = GPIO_RD32(&obj->reg->dout[pos].val);
  427. return (((reg & (1L << bit)) != 0)? 1: 0);
  428. }
  429. /*---------------------------------------------------------------------------*/
  430. S32 mt_get_gpio_in_chip(u32 pin)
  431. {
  432. u32 bit;
  433. u32 reg;
  434. struct mt_gpio_obj *obj = gpio_obj;
  435. if (!obj)
  436. return -ERACCESS;
  437. if (pin >= MAX_GPIO_PIN)
  438. return -ERINVAL;
  439. bit = DIN_offset[pin].offset;
  440. reg = GPIO_RD32(DIN_addr[pin].addr);
  441. return (((reg & (1L << bit)) != 0)? 1: 0);
  442. }
  443. /*---------------------------------------------------------------------------*/
  444. S32 mt_set_gpio_mode_chip(u32 pin, u32 mode)
  445. {
  446. #ifndef GPIO_BRINGUP
  447. u32 pos;
  448. #endif
  449. u32 bit;
  450. u32 reg;
  451. u32 mask = (1L << GPIO_MODE_BITS) - 1;
  452. struct mt_gpio_obj *obj = gpio_obj;
  453. if (!obj)
  454. return -ERACCESS;
  455. if (pin >= MAX_GPIO_PIN)
  456. return -ERINVAL;
  457. if (mode >= GPIO_MODE_MAX)
  458. return -ERINVAL;
  459. //pos = pin / MAX_GPIO_MODE_PER_REG;
  460. bit = MODE_offset[pin].offset;
  461. #ifdef GPIO_BRINGUP
  462. mode = mode & mask;
  463. //printf("fwq pin=%d,mode=%d, offset=%d\n",pin,mode,bit);
  464. reg = GPIO_RD32(MODE_addr[pin].addr);
  465. //printf("fwq pin=%d,beforereg[%x]=%x\n",pin,MODE_addr[pin].addr,reg);
  466. reg &= (~(mask << bit));
  467. //printf("fwq pin=%d,clr=%x\n",pin,~(mask << (GPIO_MODE_BITS*bit)));
  468. reg |= (mode << bit);
  469. //printf("fwq pin=%d,reg[%x]=%x\n",pin,MODE_addr[pin].addr,reg);
  470. GPIO_WR32( MODE_addr[pin].addr,reg);
  471. //printf("fwq pin=%d,moderead=%x\n",pin,GPIO_RD32(MODE_addr[pin].addr));
  472. #else
  473. reg = ((1L << (GPIO_MODE_BITS*bit)) << 3) | (mode << (GPIO_MODE_BITS*bit));
  474. GPIO_WR32(&obj->reg->mode[pos]._align1, reg);
  475. #endif
  476. return RSUCCESS;
  477. }
  478. /*---------------------------------------------------------------------------*/
  479. S32 mt_get_gpio_mode_chip(u32 pin)
  480. {
  481. u32 bit;
  482. u32 reg;
  483. u32 mask = (1L << GPIO_MODE_BITS) - 1;
  484. //struct mt_gpio_obj *obj = gpio_obj;
  485. if (pin >= MAX_GPIO_PIN)
  486. return -ERINVAL;
  487. bit = MODE_offset[pin].offset;
  488. // printf("fwqread bit=%d,offset=%d",bit,MODE_offset[pin].offset);
  489. //reg = GPIO_RD32(&obj->reg->mode[pos].val);
  490. reg = GPIO_RD32(MODE_addr[pin].addr);
  491. // printf("fwqread pin=%d,moderead=%x, bit=%d\n",pin,GPIO_RD32(MODE_addr[pin].addr),bit);
  492. return ((reg >> bit) & mask);
  493. }
  494. /*---------------------------------------------------------------------------*/
  495. void mt_gpio_pin_decrypt(u32 *cipher)
  496. {
  497. //just for debug, find out who used pin number directly
  498. if((*cipher & (0x80000000)) == 0){
  499. #ifndef DUMMY_AP
  500. GPIOERR("GPIO%u HARDCODE warning!!! \n",(unsigned int)*cipher);
  501. //dump_stack();
  502. //return;
  503. #endif
  504. }
  505. //GPIOERR("Pin magic number is %x\n",*cipher);
  506. *cipher &= ~(0x80000000);
  507. return;
  508. }
  509. //set GPIO function in fact
  510. /*---------------------------------------------------------------------------*/
  511. S32 mt_set_gpio_dir(u32 pin, u32 dir)
  512. {
  513. mt_gpio_pin_decrypt(&pin);
  514. return mt_set_gpio_dir_chip(pin,dir);
  515. }
  516. /*---------------------------------------------------------------------------*/
  517. S32 mt_get_gpio_dir(u32 pin)
  518. {
  519. mt_gpio_pin_decrypt(&pin);
  520. return mt_get_gpio_dir_chip(pin);
  521. }
  522. /*---------------------------------------------------------------------------*/
  523. S32 mt_set_gpio_pull_enable(u32 pin, u32 enable)
  524. {
  525. mt_gpio_pin_decrypt(&pin);
  526. return mt_set_gpio_pull_enable_chip(pin,enable);
  527. }
  528. /*---------------------------------------------------------------------------*/
  529. S32 mt_get_gpio_pull_enable(u32 pin)
  530. {
  531. mt_gpio_pin_decrypt(&pin);
  532. return mt_get_gpio_pull_enable_chip(pin);
  533. }
  534. /*---------------------------------------------------------------------------*/
  535. S32 mt_set_gpio_pull_select(u32 pin, u32 select)
  536. {
  537. mt_gpio_pin_decrypt(&pin);
  538. return mt_set_gpio_pull_select_chip(pin,select);
  539. }
  540. /*---------------------------------------------------------------------------*/
  541. S32 mt_get_gpio_pull_select(u32 pin)
  542. {
  543. mt_gpio_pin_decrypt(&pin);
  544. return mt_get_gpio_pull_select_chip(pin);
  545. }
  546. /*---------------------------------------------------------------------------*/
  547. S32 mt_set_gpio_smt(u32 pin, u32 enable)
  548. {
  549. mt_gpio_pin_decrypt(&pin);
  550. return mt_set_gpio_smt_chip(pin,enable);
  551. }
  552. /*---------------------------------------------------------------------------*/
  553. S32 mt_get_gpio_smt(u32 pin)
  554. {
  555. mt_gpio_pin_decrypt(&pin);
  556. return mt_get_gpio_smt_chip(pin);
  557. }
  558. /*---------------------------------------------------------------------------*/
  559. S32 mt_set_gpio_ies(u32 pin, u32 enable)
  560. {
  561. mt_gpio_pin_decrypt(&pin);
  562. return mt_set_gpio_ies_chip(pin,enable);
  563. }
  564. /*---------------------------------------------------------------------------*/
  565. S32 mt_get_gpio_ies(u32 pin)
  566. {
  567. mt_gpio_pin_decrypt(&pin);
  568. return mt_get_gpio_ies_chip(pin);
  569. }
  570. /*---------------------------------------------------------------------------*/
  571. S32 mt_set_gpio_out(u32 pin, u32 output)
  572. {
  573. mt_gpio_pin_decrypt(&pin);
  574. return mt_set_gpio_out_chip(pin,output);
  575. }
  576. /*---------------------------------------------------------------------------*/
  577. S32 mt_get_gpio_out(u32 pin)
  578. {
  579. mt_gpio_pin_decrypt(&pin);
  580. return mt_get_gpio_out_chip(pin);
  581. }
  582. /*---------------------------------------------------------------------------*/
  583. S32 mt_get_gpio_in(u32 pin)
  584. {
  585. mt_gpio_pin_decrypt(&pin);
  586. return mt_get_gpio_in_chip(pin);
  587. }
  588. /*---------------------------------------------------------------------------*/
  589. S32 mt_set_gpio_mode(u32 pin, u32 mode)
  590. {
  591. mt_gpio_pin_decrypt(&pin);
  592. return mt_set_gpio_mode_chip(pin,mode);
  593. }
  594. /*---------------------------------------------------------------------------*/
  595. S32 mt_get_gpio_mode(u32 pin)
  596. {
  597. mt_gpio_pin_decrypt(&pin);
  598. return mt_get_gpio_mode_chip(pin);
  599. }
  600. /*****************************************************************************/
  601. /* sysfs operation */
  602. /*****************************************************************************/
  603. #if 0
  604. void mt_gpio_self_test(void)
  605. {
  606. int i, val;
  607. for (i = 0; i < GPIO_MAX; i++)
  608. {
  609. S32 res,old;
  610. GPIOMSG("GPIO-%3d test\n", i);
  611. /*direction test*/
  612. old = mt_get_gpio_dir(i);
  613. if (old == 0 || old == 1) {
  614. GPIOLOG(" dir old = %d\n", old);
  615. } else {
  616. GPIOERR(" test dir fail: %d\n", old);
  617. break;
  618. }
  619. if ((res = mt_set_gpio_dir(i, GPIO_DIR_OUT)) != RSUCCESS) {
  620. GPIOERR(" set dir out fail: %d\n", res);
  621. break;
  622. } else if ((res = mt_get_gpio_dir(i)) != GPIO_DIR_OUT) {
  623. GPIOERR(" get dir out fail: %d\n", res);
  624. break;
  625. } else {
  626. /*output test*/
  627. S32 out = mt_get_gpio_out(i);
  628. if (out != 0 && out != 1) {
  629. GPIOERR(" get out fail = %d\n", old);
  630. break;
  631. }
  632. for (val = 0; val < GPIO_OUT_MAX; val++) {
  633. if ((res = mt_set_gpio_out(i,0)) != RSUCCESS) {
  634. GPIOERR(" set out[%d] fail: %d\n", val, res);
  635. break;
  636. } else if ((res = mt_get_gpio_out(i)) != 0) {
  637. GPIOERR(" get out[%d] fail: %d\n", val, res);
  638. break;
  639. }
  640. }
  641. if ((res = mt_set_gpio_out(i,out)) != RSUCCESS)
  642. {
  643. GPIOERR(" restore out fail: %d\n", res);
  644. break;
  645. }
  646. }
  647. if ((res = mt_set_gpio_dir(i, GPIO_DIR_IN)) != RSUCCESS) {
  648. GPIOERR(" set dir in fail: %d\n", res);
  649. break;
  650. } else if ((res = mt_get_gpio_dir(i)) != GPIO_DIR_IN) {
  651. GPIOERR(" get dir in fail: %d\n", res);
  652. break;
  653. } else {
  654. GPIOLOG(" input data = %d\n", res);
  655. }
  656. if ((res = mt_set_gpio_dir(i, old)) != RSUCCESS) {
  657. GPIOERR(" restore dir fail: %d\n", res);
  658. break;
  659. }
  660. for (val = 0; val < GPIO_PULL_EN_MAX; val++) {
  661. if ((res = mt_set_gpio_pull_enable(i,val)) != RSUCCESS) {
  662. GPIOERR(" set pullen[%d] fail: %d\n", val, res);
  663. break;
  664. } else if ((res = mt_get_gpio_pull_enable(i)) != val) {
  665. GPIOERR(" get pullen[%d] fail: %d\n", val, res);
  666. break;
  667. }
  668. }
  669. if ((res = mt_set_gpio_pull_enable(i, old)) != RSUCCESS) {
  670. GPIOERR(" restore pullen fail: %d\n", res);
  671. break;
  672. }
  673. /*pull select test*/
  674. old = mt_get_gpio_pull_select(i);
  675. if (old == 0 || old == 1)
  676. GPIOLOG(" pullsel old = %d\n", old);
  677. else {
  678. GPIOERR(" pullsel fail: %d\n", old);
  679. break;
  680. }
  681. for (val = 0; val < GPIO_PULL_MAX; val++) {
  682. if ((res = mt_set_gpio_pull_select(i,val)) != RSUCCESS) {
  683. GPIOERR(" set pullsel[%d] fail: %d\n", val, res);
  684. break;
  685. } else if ((res = mt_get_gpio_pull_select(i)) != val) {
  686. GPIOERR(" get pullsel[%d] fail: %d\n", val, res);
  687. break;
  688. }
  689. }
  690. if ((res = mt_set_gpio_pull_select(i, old)) != RSUCCESS)
  691. {
  692. GPIOERR(" restore pullsel fail: %d\n", res);
  693. break;
  694. }
  695. /*mode control*/
  696. old = mt_get_gpio_mode(i);
  697. if ((old >= GPIO_MODE_00) && (val < GPIO_MODE_MAX)){
  698. GPIOLOG(" mode old = %d\n", old);
  699. }
  700. else{
  701. GPIOERR(" get mode fail: %d\n", old);
  702. break;
  703. }
  704. for (val = 0; val < GPIO_MODE_MAX; val++) {
  705. if ((res = mt_set_gpio_mode(i, val)) != RSUCCESS) {
  706. GPIOERR("set mode[%d] fail: %d\n", val, res);
  707. break;
  708. } else if ((res = mt_get_gpio_mode(i)) != val) {
  709. GPIOERR("get mode[%d] fail: %d\n", val, res);
  710. break;
  711. }
  712. }
  713. if ((res = mt_set_gpio_mode(i,old)) != RSUCCESS) {
  714. GPIOERR(" restore mode fail: %d\n", res);
  715. break;
  716. }
  717. }
  718. GPIOLOG("GPIO test done\n");
  719. }
  720. /*----------------------------------------------------------------------------*/
  721. void mt_gpio_dump(void)
  722. {
  723. GPIO_REGS *regs = (GPIO_REGS*)(GPIO_BASE);
  724. int idx;
  725. GPIOMSG("---# dir #-----------------------------------------------------------------\n");
  726. for (idx = 0; idx < sizeof(regs->dir)/sizeof(regs->dir[0]); idx++) {
  727. GPIOMSG("0x%04X ", regs->dir[idx].val);
  728. if (7 == (idx % 8)) GPIOMSG("\n");
  729. }
  730. GPIOMSG("\n---# pullen #--------------------------------------------------------------\n");
  731. for (idx = 0; idx < sizeof(regs->pullen)/sizeof(regs->pullen[0]); idx++) {
  732. GPIOMSG("0x%04X ", regs->pullen[idx].val);
  733. if (7 == (idx % 8)) GPIOMSG("\n");
  734. }
  735. GPIOMSG("\n---# pullsel #-------------------------------------------------------------\n");
  736. for (idx = 0; idx < sizeof(regs->pullsel)/sizeof(regs->pullsel[0]); idx++) {
  737. GPIOMSG("0x%04X ", regs->pullsel[idx].val);
  738. if (7 == (idx % 8)) GPIOMSG("\n");
  739. }
  740. GPIOMSG("\n---# dout #----------------------------------------------------------------\n");
  741. for (idx = 0; idx < sizeof(regs->dout)/sizeof(regs->dout[0]); idx++) {
  742. GPIOMSG("0x%04X ", regs->dout[idx].val);
  743. if (7 == (idx % 8)) GPIOMSG("\n");
  744. }
  745. GPIOMSG("\n---# din #----------------------------------------------------------------\n");
  746. for (idx = 0; idx < sizeof(regs->din)/sizeof(regs->din[0]); idx++) {
  747. GPIOMSG("0x%04X ", regs->din[idx].val);
  748. if (7 == (idx % 8)) GPIOMSG("\n");
  749. }
  750. GPIOMSG("\n---# mode #----------------------------------------------------------------\n");
  751. for (idx = 0; idx < sizeof(regs->mode)/sizeof(regs->mode[0]); idx++) {
  752. GPIOMSG("0x%04X ", regs->mode[idx].val);
  753. if (7 == (idx % 8)) GPIOMSG("\n");
  754. }
  755. GPIOMSG("sim0 0x%04X ", regs->sim_ctrl[0].val);
  756. GPIOMSG("sim1 0x%04X ", regs->sim_ctrl[1].val);
  757. GPIOMSG("sim2 0x%04X ", regs->sim_ctrl[2].val);
  758. GPIOMSG("sim3 0x%04X ", regs->sim_ctrl[3].val);
  759. GPIOMSG("keypad0 0x%04X ", regs->kpad_ctrl[0].val);
  760. GPIOMSG("keypad1 0x%04X ", regs->kpad_ctrl[1].val);
  761. GPIOMSG("\n---------------------------------------------------------------------------\n");
  762. }
  763. /*---------------------------------------------------------------------------*/
  764. void mt_gpio_read_pin(GPIO_CFG* cfg, int method)
  765. {
  766. if (method == 0) {
  767. GPIO_REGS *cur = (GPIO_REGS*)GPIO_BASE;
  768. u32 mask = (1L << GPIO_MODE_BITS) - 1;
  769. int num, bit,idx=cfg->no;
  770. num = idx / MAX_GPIO_REG_BITS;
  771. bit = idx % MAX_GPIO_REG_BITS;
  772. cfg->pullsel= (cur->pullsel[num].val & (1L << bit)) ? (1) : (0);
  773. cfg->din = (cur->din[num].val & (1L << bit)) ? (1) : (0);
  774. cfg->dout = (cur->dout[num].val & (1L << bit)) ? (1) : (0);
  775. cfg->pullen = (cur->pullen[num].val & (1L << bit)) ? (1) : (0);
  776. cfg->dir = (cur->dir[num].val & (1L << bit)) ? (1) : (0);
  777. num = idx / MAX_GPIO_MODE_PER_REG;
  778. bit = idx % MAX_GPIO_MODE_PER_REG;
  779. cfg->mode = (cur->mode[num].val >> (GPIO_MODE_BITS*bit)) & mask;
  780. } else if (method == 1) {
  781. int idx=cfg->no;
  782. cfg->pullsel= mt_get_gpio_pull_select(idx);
  783. cfg->din = mt_get_gpio_in(idx);
  784. cfg->dout = mt_get_gpio_out(idx);
  785. cfg->pullen = mt_get_gpio_pull_enable(idx);
  786. cfg->dir = mt_get_gpio_dir(idx);
  787. cfg->mode = mt_get_gpio_mode(idx);
  788. }
  789. }
  790. /*---------------------------------------------------------------------------*/
  791. void mt_gpio_dump_addr(void)
  792. {
  793. int idx;
  794. struct mt_gpio_obj *obj = gpio_obj;
  795. GPIO_REGS *reg = obj->reg;
  796. GPIOMSG("# direction\n");
  797. for (idx = 0; idx < sizeof(reg->dir)/sizeof(reg->dir[0]); idx++)
  798. 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);
  799. GPIOMSG("# pull enable\n");
  800. for (idx = 0; idx < sizeof(reg->pullen)/sizeof(reg->pullen[0]); idx++)
  801. 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);
  802. GPIOMSG("# pull select\n");
  803. for (idx = 0; idx < sizeof(reg->pullsel)/sizeof(reg->pullsel[0]); idx++)
  804. 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);
  805. GPIOMSG("# data output\n");
  806. for (idx = 0; idx < sizeof(reg->dout)/sizeof(reg->dout[0]); idx++)
  807. 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);
  808. GPIOMSG("# data input\n");
  809. for (idx = 0; idx < sizeof(reg->din)/sizeof(reg->din[0]); idx++)
  810. GPIOMSG("val[%2d] %p\n", idx, &reg->din[idx].val);
  811. GPIOMSG("# mode\n");
  812. for (idx = 0; idx < sizeof(reg->mode)/sizeof(reg->mode[0]); idx++)
  813. 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);
  814. }
  815. #endif
  816. #endif