pinctrl-mtk-common_debug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright (c) 2018 MediaTek Inc.
  3. * Author: Light Hsieh <light.hsieh@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #if defined(CONFIG_PINCTRL_MTK_ALTERNATIVE)
  15. static int mtk_pinctrl_get_gpio_output(struct mtk_pinctrl *pctl, int pin)
  16. {
  17. return mtk_pinctrl_get_gpio_value(pctl, pin,
  18. pctl->devdata->n_pin_dout, pctl->devdata->pin_dout_grps);
  19. }
  20. static int mtk_pinctrl_set_gpio_output(struct mtk_pinctrl *pctl,
  21. int pin, int value)
  22. {
  23. #ifndef GPIO_DEBUG
  24. return mtk_pinctrl_update_gpio_value(pctl, pin, value,
  25. pctl->devdata->n_pin_dout, pctl->devdata->pin_dout_grps);
  26. #else
  27. pr_info("config pin = %d, value = %d\n", pin, value);
  28. mtk_pinctrl_update_gpio_value(pctl, pin, value,
  29. pctl->devdata->n_pin_dout, pctl->devdata->pin_dout_grps);
  30. pr_info("set pin = %d, value = %d\n", pin,
  31. mtk_pinctrl_get_gpio_output(pctl, pin));
  32. return 0;
  33. #endif
  34. }
  35. static int mtk_pinctrl_get_gpio_input(struct mtk_pinctrl *pctl, int pin)
  36. {
  37. return mtk_pinctrl_get_gpio_value(pctl, pin,
  38. pctl->devdata->n_pin_din, pctl->devdata->pin_din_grps);
  39. }
  40. static int mtk_pinctrl_get_gpio_direction(struct mtk_pinctrl *pctl, int pin)
  41. {
  42. return mtk_pinctrl_get_gpio_value(pctl, pin,
  43. pctl->devdata->n_pin_dir, pctl->devdata->pin_dir_grps);
  44. }
  45. static int mtk_pinctrl_set_gpio_direction(struct mtk_pinctrl *pctl,
  46. int pin, bool input)
  47. {
  48. #ifndef GPIO_DEBUG
  49. return mtk_pinctrl_update_gpio_value(pctl, pin, input,
  50. pctl->devdata->n_pin_dir, pctl->devdata->pin_dir_grps);
  51. #else
  52. pr_info("config pin = %d, dir = %d\n", pin, input);
  53. mtk_pinctrl_update_gpio_value(pctl, pin, input,
  54. pctl->devdata->n_pin_dir, pctl->devdata->pin_dir_grps);
  55. pr_info("set pin = %d, dir = %d\n", pin,
  56. mtk_pinctrl_get_gpio_direction(pctl, pin));
  57. return 0;
  58. #endif
  59. }
  60. static int mtk_pinctrl_get_gpio_mode(struct mtk_pinctrl *pctl, int pin)
  61. {
  62. return mtk_pinctrl_get_gpio_value(pctl, pin,
  63. pctl->devdata->n_pin_mode, pctl->devdata->pin_mode_grps);
  64. }
  65. static int mtk_pinctrl_set_gpio_mode(struct mtk_pinctrl *pctl,
  66. int pin, unsigned long mode)
  67. {
  68. #ifndef GPIO_DEBUG
  69. return mtk_pinctrl_update_gpio_value(pctl, pin, mode,
  70. pctl->devdata->n_pin_mode, pctl->devdata->pin_mode_grps);
  71. #else
  72. pr_info("config pin = %d, mode = %d\n", pin, (int)mode);
  73. mtk_pinctrl_update_gpio_value(pctl, pin, mode,
  74. pctl->devdata->n_pin_mode, pctl->devdata->pin_mode_grps);
  75. pr_info("set pin = %d, mode = %d\n", pin,
  76. mtk_pinctrl_get_gpio_mode(pctl, pin));
  77. return 0;
  78. #endif
  79. }
  80. static int mtk_pinctrl_get_gpio_driving(struct mtk_pinctrl *pctl, int pin)
  81. {
  82. return mtk_pinctrl_get_gpio_value(pctl, pin,
  83. pctl->devdata->n_pin_drv, pctl->devdata->pin_drv_grps);
  84. }
  85. static int mtk_pinctrl_set_gpio_driving(struct mtk_pinctrl *pctl,
  86. int pin, unsigned char driving)
  87. {
  88. #ifndef GPIO_DEBUG
  89. return mtk_pinctrl_update_gpio_value(pctl, pin, driving,
  90. pctl->devdata->n_pin_drv, pctl->devdata->pin_drv_grps);
  91. #else
  92. pr_info("config pin = %d, driving = %d\n", pin, driving);
  93. mtk_pinctrl_update_gpio_value(pctl, pin, driving,
  94. pctl->devdata->n_pin_drv, pctl->devdata->pin_drv_grps);
  95. pr_info("set pin = %d, driving = %d\n", pin,
  96. mtk_pinctrl_get_gpio_driving(pctl, pin));
  97. return 0;
  98. #endif
  99. }
  100. static int mtk_pinctrl_get_gpio_smt(struct mtk_pinctrl *pctl, int pin)
  101. {
  102. return mtk_pinctrl_get_gpio_value(pctl, pin,
  103. pctl->devdata->n_pin_smt, pctl->devdata->pin_smt_grps);
  104. }
  105. static int mtk_pinctrl_set_gpio_smt(struct mtk_pinctrl *pctl,
  106. int pin, bool enable)
  107. {
  108. return mtk_pinctrl_update_gpio_value(pctl, pin, enable,
  109. pctl->devdata->n_pin_smt, pctl->devdata->pin_smt_grps);
  110. }
  111. static int mtk_pinctrl_get_gpio_ies(struct mtk_pinctrl *pctl, int pin)
  112. {
  113. return mtk_pinctrl_get_gpio_value(pctl, pin,
  114. pctl->devdata->n_pin_ies, pctl->devdata->pin_ies_grps);
  115. }
  116. static int mtk_pinctrl_set_gpio_ies(struct mtk_pinctrl *pctl,
  117. int pin, bool enable)
  118. {
  119. return mtk_pinctrl_update_gpio_value(pctl, pin, enable,
  120. pctl->devdata->n_pin_ies, pctl->devdata->pin_ies_grps);
  121. }
  122. static int mtk_pinmux_get(struct gpio_chip *chip, unsigned int offset)
  123. {
  124. unsigned int reg_addr;
  125. unsigned char bit;
  126. unsigned int pinmux = 0;
  127. unsigned int mask = (1L << GPIO_MODE_BITS) - 1;
  128. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  129. if (pctl->devdata->pin_mode_grps)
  130. return mtk_pinctrl_get_gpio_mode(pctl, offset);
  131. reg_addr = ((offset / MAX_GPIO_MODE_PER_REG) << pctl->devdata->port_shf)
  132. + pctl->devdata->pinmux_offset;
  133. bit = offset % MAX_GPIO_MODE_PER_REG;
  134. mask <<= (GPIO_MODE_BITS * bit);
  135. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &pinmux);
  136. return ((pinmux & mask) >> (GPIO_MODE_BITS * bit));
  137. }
  138. static int mtk_gpio_get_in(struct gpio_chip *chip, unsigned int offset)
  139. {
  140. unsigned int reg_addr;
  141. unsigned int bit;
  142. unsigned int read_val = 0;
  143. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  144. if (pctl->devdata->pin_din_grps)
  145. return mtk_pinctrl_get_gpio_input(pctl, offset);
  146. reg_addr = mtk_get_port(pctl, offset) +
  147. pctl->devdata->din_offset;
  148. bit = BIT(offset & 0xf);
  149. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &read_val);
  150. return !!(read_val & bit);
  151. }
  152. static int mtk_gpio_get_out(struct gpio_chip *chip, unsigned int offset)
  153. {
  154. unsigned int reg_addr;
  155. unsigned int bit;
  156. unsigned int read_val = 0;
  157. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  158. if (pctl->devdata->pin_dout_grps)
  159. return mtk_pinctrl_get_gpio_output(pctl, offset);
  160. reg_addr = mtk_get_port(pctl, offset) +
  161. pctl->devdata->dout_offset;
  162. bit = BIT(offset & pctl->devdata->port_mask);
  163. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &read_val);
  164. return !!(read_val & bit);
  165. }
  166. static int mtk_pullen_get(struct gpio_chip *chip, unsigned int offset)
  167. {
  168. unsigned int reg_addr;
  169. unsigned int bit;
  170. unsigned int pull_en = 0;
  171. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  172. int samereg = 0;
  173. /* For phone, if the pull_en have been implemented */
  174. if (pctl->devdata->mtk_pctl_get_pull_en)
  175. return pctl->devdata->mtk_pctl_get_pull_en(pctl, offset);
  176. if (pctl->devdata->spec_pull_get) {
  177. samereg =
  178. pctl->devdata->spec_pull_get(mtk_get_regmap(pctl,
  179. offset), offset);
  180. if (samereg != -1) {
  181. pull_en = (samereg >> 1) & 0x3;
  182. return pull_en;
  183. }
  184. }
  185. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->pullen_offset;
  186. bit = BIT(offset & pctl->devdata->port_mask);
  187. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &pull_en);
  188. return !!(pull_en & bit);
  189. }
  190. static int mtk_pullsel_get(struct gpio_chip *chip, unsigned int offset)
  191. {
  192. unsigned int reg_addr;
  193. unsigned int bit;
  194. unsigned int pull_sel = 0;
  195. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  196. if (pctl->devdata->mtk_pctl_get_pull_sel)
  197. return pctl->devdata->mtk_pctl_get_pull_sel(pctl, offset);
  198. if (pctl->devdata->spec_pull_get) {
  199. pull_sel =
  200. pctl->devdata->spec_pull_get(mtk_get_regmap(pctl,
  201. offset), offset);
  202. if (pull_sel != -1)
  203. return pull_sel;
  204. }
  205. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->pullsel_offset;
  206. bit = BIT(offset & pctl->devdata->port_mask);
  207. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &pull_sel);
  208. return !!(pull_sel & bit);
  209. }
  210. static int mtk_ies_get(struct gpio_chip *chip, unsigned int offset)
  211. {
  212. unsigned int reg_addr;
  213. unsigned char bit;
  214. unsigned int ies;
  215. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  216. if (pctl->devdata->pin_ies_grps)
  217. return mtk_pinctrl_get_gpio_ies(pctl, offset);
  218. if (!pctl->devdata->spec_ies_get ||
  219. pctl->devdata->ies_offset == MTK_PINCTRL_NOT_SUPPORT)
  220. return -1;
  221. /**
  222. * Due to some soc are not support ies config, add this special
  223. * control to handle it.
  224. */
  225. if (pctl->devdata->spec_ies_get) {
  226. ies = pctl->devdata->spec_ies_get(mtk_get_regmap(pctl, offset),
  227. offset);
  228. if (ies != -1)
  229. return ies;
  230. }
  231. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->ies_offset;
  232. bit = BIT(offset & pctl->devdata->port_mask);
  233. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &ies);
  234. return !!(ies & bit);
  235. }
  236. static int mtk_smt_get(struct gpio_chip *chip, unsigned int offset)
  237. {
  238. unsigned int reg_addr;
  239. unsigned char bit;
  240. unsigned int smt;
  241. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  242. if (pctl->devdata->pin_smt_grps)
  243. return mtk_pinctrl_get_gpio_smt(pctl, offset);
  244. if (!pctl->devdata->spec_smt_get ||
  245. pctl->devdata->smt_offset == MTK_PINCTRL_NOT_SUPPORT)
  246. return -1;
  247. /**
  248. * Due to some soc are not support smt config, add this special
  249. * control to handle it.
  250. */
  251. if (pctl->devdata->spec_smt_get) {
  252. smt = pctl->devdata->spec_smt_get(mtk_get_regmap(pctl, offset),
  253. offset);
  254. if (smt != -1)
  255. return smt;
  256. }
  257. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->smt_offset;
  258. bit = BIT(offset & pctl->devdata->port_mask);
  259. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &smt);
  260. return !!(smt & bit);
  261. }
  262. static int mtk_driving_get(struct gpio_chip *chip, unsigned int offset)
  263. {
  264. const struct mtk_pin_drv_grp *pin_drv;
  265. unsigned int val = 0;
  266. unsigned int bits, mask, shift;
  267. const struct mtk_drv_group_desc *drv_grp;
  268. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  269. if (offset >= pctl->devdata->npins)
  270. return -1;
  271. if (pctl->devdata->pin_drv_grps)
  272. return mtk_pinctrl_get_gpio_driving(pctl, offset);
  273. pin_drv = mtk_find_pin_drv_grp_by_pin(pctl, offset);
  274. if (!pin_drv || pin_drv->grp > pctl->devdata->n_grp_cls)
  275. return -1;
  276. drv_grp = pctl->devdata->grp_desc + pin_drv->grp;
  277. bits = drv_grp->high_bit - drv_grp->low_bit + 1;
  278. mask = BIT(bits) - 1;
  279. shift = pin_drv->bit + drv_grp->low_bit;
  280. mask <<= shift;
  281. regmap_read(mtk_get_regmap(pctl, offset), pin_drv->offset, &val);
  282. return ((val & mask) >> shift);
  283. }
  284. static ssize_t mtk_gpio_show_pin(struct device *dev,
  285. struct device_attribute *attr, char *buf)
  286. {
  287. int len = 0;
  288. int bufLen = PAGE_SIZE;
  289. struct mtk_pinctrl *pctl = dev_get_drvdata(dev);
  290. struct gpio_chip *chip = pctl->chip;
  291. unsigned int i;
  292. int pull_val;
  293. len += snprintf(buf+len, bufLen-len,
  294. "PIN: [MODE] [DIR] [DOUT] [DIN] [PULL_EN] [PULL_SEL] [IES] [SMT] [DRIVE] ( [R1] [R0] )\n");
  295. for (i = 0; i < chip->ngpio; i++) {
  296. pull_val = mtk_pullsel_get(chip, i);
  297. len += snprintf(buf+len, bufLen-len,
  298. "%4d:% d% d% d% d% d% d% d% d% d",
  299. i,
  300. mtk_pinmux_get(chip, i),
  301. !mtk_gpio_get_direction(chip, i),
  302. mtk_gpio_get_out(chip, i),
  303. mtk_gpio_get_in(chip, i),
  304. mtk_pullen_get(chip, i),
  305. (pull_val >= 0) ? (pull_val&1) : -1,
  306. mtk_ies_get(chip, i),
  307. mtk_smt_get(chip, i),
  308. mtk_driving_get(chip, i));
  309. if ((pull_val & 8) && (pull_val >= 0))
  310. len += snprintf(buf+len, bufLen-len, " %d %d",
  311. !!(pull_val&4), !!(pull_val&2));
  312. len += snprintf(buf+len, bufLen-len, "\n");
  313. }
  314. return len;
  315. }
  316. void gpio_dump_regs_range(int start, int end)
  317. {
  318. struct gpio_chip *chip;
  319. unsigned int i;
  320. int pull_val;
  321. if (!pctl_alt) {
  322. pr_debug("[gpio_dump_regs]error: pinctrl does not exist\n");
  323. return;
  324. }
  325. chip = pctl_alt->chip;
  326. pr_debug("PIN: [MODE] [DIR] [DOUT] [DIN][PULL_EN] [PULL_SEL] [IES] [SMT] [DRIVE] ( [R1] [R0] )\n");
  327. if (start < 0) {
  328. start = 0;
  329. end = chip->ngpio-1;
  330. }
  331. if (end > chip->ngpio - 1)
  332. end = chip->ngpio - 1;
  333. for (i = start; i <= end; i++) {
  334. pull_val = mtk_pullsel_get(chip, i);
  335. pr_debug("%4d: %d%d%d%d%d%d%d%d%d",
  336. i, mtk_pinmux_get(chip, i),
  337. !mtk_gpio_get_direction(chip, i),
  338. mtk_gpio_get_out(chip, i),
  339. mtk_gpio_get_in(chip, i),
  340. mtk_pullen_get(chip, i),
  341. (pull_val >= 0) ? (pull_val&1) : -1,
  342. mtk_ies_get(chip, i),
  343. mtk_smt_get(chip, i),
  344. mtk_driving_get(chip, i));
  345. if ((pull_val & MTK_PUPD_R1R0_BIT_SUPPORT) && (pull_val >= 0))
  346. pr_debug(" %d %d\n", !!(pull_val&4), !!(pull_val&2));
  347. else
  348. pr_debug("\n");
  349. }
  350. }
  351. void gpio_dump_regs(void)
  352. {
  353. gpio_dump_regs_range(-1, -1);
  354. }
  355. static ssize_t mtk_gpio_store_pin(struct device *dev,
  356. struct device_attribute *attr, const char *buf, size_t count)
  357. {
  358. int pin, val;
  359. int val_set;
  360. struct mtk_pinctrl *pctl = dev_get_drvdata(dev);
  361. struct pinctrl_dev *pctldev = pctl->pctl_dev;
  362. if (!strncmp(buf, "mode", 4) &&
  363. (sscanf(buf+4, "%d %d", &pin, &val) == 2)) {
  364. val_set = mtk_pmx_set_mode(pctldev, pin, val);
  365. } else if (!strncmp(buf, "dir", 3) &&
  366. (sscanf(buf+3, "%d %d", &pin, &val) == 2)) {
  367. val_set = mtk_pmx_gpio_set_direction(pctldev, NULL, pin, !val);
  368. } else if (!strncmp(buf, "out", 3) &&
  369. (sscanf(buf+3, "%d %d", &pin, &val) == 2)) {
  370. mtk_pmx_gpio_set_direction(pctldev, NULL, pin, false);
  371. mtk_gpio_set(pctl->chip, pin, val);
  372. } else if (!strncmp(buf, "pullen", 6) &&
  373. (sscanf(buf+6, "%d %d", &pin, &val) == 2)) {
  374. val_set = mtk_pconf_set_pull_select(pctl, pin, !!val,
  375. false, MTK_PUPD_SET_R1R0_00 + val);
  376. } else if (!strncmp(buf, "pullsel", 7) &&
  377. (sscanf(buf+7, "%d %d", &pin, &val) == 2)) {
  378. val_set = mtk_pconf_set_pull_select(pctl, pin, true,
  379. !!val, MTK_PUPD_SET_R1R0_01);
  380. } else if (!strncmp(buf, "ies", 3) &&
  381. (sscanf(buf+3, "%d %d", &pin, &val) == 2)) {
  382. val_set = mtk_pconf_set_ies_smt(pctl, pin, val,
  383. PIN_CONFIG_INPUT_ENABLE);
  384. } else if (!strncmp(buf, "smt", 3) &&
  385. (sscanf(buf+3, "%d %d", &pin, &val) == 2)) {
  386. val_set = mtk_pconf_set_ies_smt(pctl, pin, val,
  387. PIN_CONFIG_INPUT_SCHMITT_ENABLE);
  388. }
  389. return count;
  390. }
  391. static DEVICE_ATTR(mt_gpio, 0664, mtk_gpio_show_pin, mtk_gpio_store_pin);
  392. static struct device_attribute *gpio_attr_list[] = {
  393. &dev_attr_mt_gpio,
  394. };
  395. static int mtk_gpio_create_attr(struct device *dev)
  396. {
  397. int idx, err = 0;
  398. int num = ARRAY_SIZE(gpio_attr_list);
  399. if (!dev)
  400. return -EINVAL;
  401. for (idx = 0; idx < num; idx++) {
  402. err = device_create_file(dev, gpio_attr_list[idx]);
  403. if (err)
  404. break;
  405. }
  406. return err;
  407. }
  408. int mtk_pctrl_get_gpio_chip_base(void)
  409. {
  410. if (pctl_alt)
  411. return pctl_alt->chip->base;
  412. pr_info("mtk_pinctrl is not initialized\n");
  413. return 0;
  414. }
  415. #endif