pinctrl-mtk-common.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  1. /*
  2. * mt65xx pinctrl driver based on Allwinner A1X pinctrl driver.
  3. * Copyright (c) 2014 MediaTek Inc.
  4. * Author:
  5. * Hongzhou.Yang <hongzhou.yang@mediatek.com>
  6. * Maoguang.Meng <maoguang.meng@mediatek.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/device.h>
  18. #include <linux/io.h>
  19. #include <linux/gpio.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/irqchip/chained_irq.h>
  26. #include <linux/pinctrl/consumer.h>
  27. #include <linux/pinctrl/machine.h>
  28. #include <linux/pinctrl/pinconf.h>
  29. #include <linux/pinctrl/pinconf-generic.h>
  30. #include <linux/pinctrl/pinctrl.h>
  31. #include <linux/pinctrl/pinmux.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/slab.h>
  34. #include <linux/bitops.h>
  35. #include <linux/regmap.h>
  36. #include <linux/mfd/syscon.h>
  37. #include <linux/delay.h>
  38. #include <asm/hardirq.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/pm.h>
  41. #include <dt-bindings/pinctrl/mt65xx.h>
  42. #include "../core.h"
  43. #include "../pinconf.h"
  44. #include "../pinctrl-utils.h"
  45. #include "pinctrl-mtk-common.h"
  46. #define MAX_GPIO_MODE_PER_REG 5
  47. #define GPIO_MODE_BITS 3
  48. static const char * const mtk_gpio_functions[] = {
  49. "func0", "func1", "func2", "func3",
  50. "func4", "func5", "func6", "func7",
  51. };
  52. /*
  53. * There are two base address for pull related configuration
  54. * in mt8135, and different GPIO pins use different base address.
  55. * When pin number greater than type1_start and less than type1_end,
  56. * should use the second base address.
  57. */
  58. static struct regmap *mtk_get_regmap(struct mtk_pinctrl *pctl,
  59. unsigned long pin)
  60. {
  61. if (pin >= pctl->devdata->type1_start && pin < pctl->devdata->type1_end)
  62. return pctl->regmap2;
  63. return pctl->regmap1;
  64. }
  65. static unsigned int mtk_get_port(struct mtk_pinctrl *pctl, unsigned long pin)
  66. {
  67. /* Different SoC has different mask and port shift. */
  68. return ((pin >> 4) & pctl->devdata->port_mask)
  69. << pctl->devdata->port_shf;
  70. }
  71. static int mtk_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  72. struct pinctrl_gpio_range *range, unsigned offset,
  73. bool input)
  74. {
  75. unsigned int reg_addr;
  76. unsigned int bit;
  77. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  78. if (pctl->devdata->mt_set_gpio_dir) {
  79. pctl->devdata->mt_set_gpio_dir(offset|0x80000000, (!input));
  80. return 0;
  81. }
  82. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
  83. bit = BIT(offset & 0xf);
  84. if (input)
  85. /* Different SoC has different alignment offset. */
  86. reg_addr = CLR_ADDR(reg_addr, pctl);
  87. else
  88. reg_addr = SET_ADDR(reg_addr, pctl);
  89. regmap_write(mtk_get_regmap(pctl, offset), reg_addr, bit);
  90. return 0;
  91. }
  92. static void mtk_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  93. {
  94. unsigned int reg_addr;
  95. unsigned int bit;
  96. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
  97. if (pctl->devdata->mt_set_gpio_out) {
  98. /* please weiqi to confirm */
  99. pctl->devdata->mt_set_gpio_out(offset|0x80000000, value);
  100. return;
  101. }
  102. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dout_offset;
  103. bit = BIT(offset & 0xf);
  104. if (value)
  105. reg_addr = SET_ADDR(reg_addr, pctl);
  106. else
  107. reg_addr = CLR_ADDR(reg_addr, pctl);
  108. regmap_write(mtk_get_regmap(pctl, offset), reg_addr, bit);
  109. }
  110. static int mtk_pconf_set_ies_smt(struct mtk_pinctrl *pctl, unsigned pin,
  111. int value, enum pin_config_param arg)
  112. {
  113. unsigned int reg_addr, offset;
  114. unsigned int bit;
  115. if (pctl->devdata->mt_set_gpio_ies || pctl->devdata->mt_set_gpio_smt) {
  116. if (arg == PIN_CONFIG_INPUT_ENABLE)
  117. pctl->devdata->mt_set_gpio_ies(pin|0x80000000, value);
  118. else if (arg == PIN_CONFIG_INPUT_SCHMITT_ENABLE)
  119. pctl->devdata->mt_set_gpio_smt(pin|0x80000000, value);
  120. return 0;
  121. }
  122. /**
  123. * Due to some soc are not support ies/smt config, add this special
  124. * control to handle it.
  125. */
  126. if (!pctl->devdata->spec_ies_smt_set &&
  127. pctl->devdata->ies_offset == MTK_PINCTRL_NOT_SUPPORT &&
  128. arg == PIN_CONFIG_INPUT_ENABLE)
  129. return -EINVAL;
  130. if (!pctl->devdata->spec_ies_smt_set &&
  131. pctl->devdata->smt_offset == MTK_PINCTRL_NOT_SUPPORT &&
  132. arg == PIN_CONFIG_INPUT_SCHMITT_ENABLE)
  133. return -EINVAL;
  134. /*
  135. * Due to some pins are irregular, their input enable and smt
  136. * control register are discontinuous, so we need this special handle.
  137. */
  138. if (pctl->devdata->spec_ies_smt_set) {
  139. return pctl->devdata->spec_ies_smt_set(
  140. mtk_get_regmap(pctl, pin),
  141. pin, pctl->devdata->port_align, value, arg);
  142. }
  143. bit = BIT(pin & 0xf);
  144. if (arg == PIN_CONFIG_INPUT_ENABLE)
  145. offset = pctl->devdata->ies_offset;
  146. else
  147. offset = pctl->devdata->smt_offset;
  148. if (value)
  149. reg_addr = SET_ADDR(mtk_get_port(pctl, pin) + offset, pctl);
  150. else
  151. reg_addr = CLR_ADDR(mtk_get_port(pctl, pin) + offset, pctl);
  152. regmap_write(mtk_get_regmap(pctl, pin), reg_addr, bit);
  153. return 0;
  154. }
  155. int mtk_pconf_spec_set_ies_smt_range(struct regmap *regmap,
  156. const struct mtk_pin_ies_smt_set *ies_smt_infos,
  157. unsigned int info_num,
  158. unsigned int pin, unsigned char align, int value)
  159. {
  160. unsigned int i, reg_addr, bit;
  161. for (i = 0; i < info_num; i++) {
  162. if (pin >= ies_smt_infos[i].start &&
  163. pin <= ies_smt_infos[i].end) {
  164. break;
  165. }
  166. }
  167. if (i == info_num)
  168. return -EINVAL;
  169. if (value)
  170. reg_addr = ies_smt_infos[i].offset + align;
  171. else
  172. reg_addr = ies_smt_infos[i].offset + (align << 1);
  173. bit = BIT(ies_smt_infos[i].bit);
  174. regmap_write(regmap, reg_addr, bit);
  175. return 0;
  176. }
  177. static const struct mtk_pin_drv_grp *mtk_find_pin_drv_grp_by_pin(
  178. struct mtk_pinctrl *pctl, unsigned long pin) {
  179. int i;
  180. for (i = 0; i < pctl->devdata->n_pin_drv_grps; i++) {
  181. const struct mtk_pin_drv_grp *pin_drv =
  182. pctl->devdata->pin_drv_grp + i;
  183. if (pin == pin_drv->pin)
  184. return pin_drv;
  185. }
  186. return NULL;
  187. }
  188. static void mtk_pconf_set_direction(struct mtk_pinctrl *pctl, unsigned pin,
  189. int value, enum pin_config_param param)
  190. {
  191. if (pctl->devdata->mt_set_gpio_dir)
  192. pctl->devdata->mt_set_gpio_dir(pin|0x80000000, value);
  193. }
  194. static int mtk_pconf_set_driving(struct mtk_pinctrl *pctl,
  195. unsigned int pin, unsigned char driving)
  196. {
  197. const struct mtk_pin_drv_grp *pin_drv;
  198. unsigned int val;
  199. unsigned int bits, mask, shift;
  200. const struct mtk_drv_group_desc *drv_grp;
  201. if (pin >= pctl->devdata->npins)
  202. return -EINVAL;
  203. pin_drv = mtk_find_pin_drv_grp_by_pin(pctl, pin);
  204. if (!pin_drv || pin_drv->grp > pctl->devdata->n_grp_cls)
  205. return -EINVAL;
  206. drv_grp = pctl->devdata->grp_desc + pin_drv->grp;
  207. if (driving >= drv_grp->min_drv && driving <= drv_grp->max_drv
  208. && !(driving % drv_grp->step)) {
  209. val = driving / drv_grp->step - 1;
  210. bits = drv_grp->high_bit - drv_grp->low_bit + 1;
  211. mask = BIT(bits) - 1;
  212. shift = pin_drv->bit + drv_grp->low_bit;
  213. mask <<= shift;
  214. val <<= shift;
  215. return regmap_update_bits(mtk_get_regmap(pctl, pin),
  216. pin_drv->offset, mask, val);
  217. }
  218. return -EINVAL;
  219. }
  220. int mtk_pctrl_spec_pull_set_samereg(struct regmap *regmap,
  221. const struct mtk_pin_spec_pupd_set_samereg *pupd_infos,
  222. unsigned int info_num, unsigned int pin,
  223. unsigned char align, bool isup, unsigned int r1r0)
  224. {
  225. unsigned int i;
  226. unsigned int reg_pupd, reg_set, reg_rst;
  227. unsigned int bit_pupd, bit_r0, bit_r1;
  228. const struct mtk_pin_spec_pupd_set_samereg *spec_pupd_pin;
  229. bool find = false;
  230. for (i = 0; i < info_num; i++) {
  231. if (pin == pupd_infos[i].pin) {
  232. find = true;
  233. break;
  234. }
  235. }
  236. if (!find)
  237. return -EINVAL;
  238. spec_pupd_pin = pupd_infos + i;
  239. reg_set = spec_pupd_pin->offset + align;
  240. reg_rst = spec_pupd_pin->offset + (align << 1);
  241. if (isup)
  242. reg_pupd = reg_rst;
  243. else
  244. reg_pupd = reg_set;
  245. bit_pupd = BIT(spec_pupd_pin->pupd_bit);
  246. regmap_write(regmap, reg_pupd, bit_pupd);
  247. bit_r0 = BIT(spec_pupd_pin->r0_bit);
  248. bit_r1 = BIT(spec_pupd_pin->r1_bit);
  249. switch (r1r0) {
  250. case MTK_PUPD_SET_R1R0_00:
  251. regmap_write(regmap, reg_rst, bit_r0);
  252. regmap_write(regmap, reg_rst, bit_r1);
  253. break;
  254. case MTK_PUPD_SET_R1R0_01:
  255. regmap_write(regmap, reg_set, bit_r0);
  256. regmap_write(regmap, reg_rst, bit_r1);
  257. break;
  258. case MTK_PUPD_SET_R1R0_10:
  259. regmap_write(regmap, reg_rst, bit_r0);
  260. regmap_write(regmap, reg_set, bit_r1);
  261. break;
  262. case MTK_PUPD_SET_R1R0_11:
  263. regmap_write(regmap, reg_set, bit_r0);
  264. regmap_write(regmap, reg_set, bit_r1);
  265. break;
  266. default:
  267. return -EINVAL;
  268. }
  269. return 0;
  270. }
  271. static int mtk_pconf_set_pull_select(struct mtk_pinctrl *pctl,
  272. unsigned int pin, bool enable, bool isup, unsigned int arg)
  273. {
  274. unsigned int bit;
  275. unsigned int reg_pullen, reg_pullsel;
  276. int ret;
  277. /* Some pins' pull setting are very different,
  278. * they have separate pull up/down bit, R0 and R1
  279. * resistor bit, so we need this special handle.
  280. */
  281. if (pctl->devdata->mt_set_gpio_pull_enable) {
  282. if (enable)
  283. pctl->devdata->mt_set_gpio_pull_enable(pin | 0x80000000, 1);
  284. else {
  285. pctl->devdata->mt_set_gpio_pull_enable(pin | 0x80000000, 0);
  286. return 0;
  287. }
  288. }
  289. if (pctl->devdata->mt_set_gpio_pull_select) {
  290. if (isup)
  291. pctl->devdata->mt_set_gpio_pull_select(pin | 0x80000000, 1);
  292. else
  293. pctl->devdata->mt_set_gpio_pull_select(pin | 0x80000000, 0);
  294. return 0;
  295. }
  296. if (pctl->devdata->spec_pull_set) {
  297. ret = pctl->devdata->spec_pull_set(mtk_get_regmap(pctl, pin),
  298. pin, pctl->devdata->port_align, isup, arg);
  299. if (!ret)
  300. return 0;
  301. }
  302. /* For generic pull config, default arg value should be 0 or 1. */
  303. if (arg != 0 && arg != 1) {
  304. dev_err(pctl->dev, "invalid pull-up argument %d on pin %d .\n",
  305. arg, pin);
  306. return -EINVAL;
  307. }
  308. bit = BIT(pin & 0xf);
  309. if (enable)
  310. reg_pullen = SET_ADDR(mtk_get_port(pctl, pin) +
  311. pctl->devdata->pullen_offset, pctl);
  312. else
  313. reg_pullen = CLR_ADDR(mtk_get_port(pctl, pin) +
  314. pctl->devdata->pullen_offset, pctl);
  315. if (isup)
  316. reg_pullsel = SET_ADDR(mtk_get_port(pctl, pin) +
  317. pctl->devdata->pullsel_offset, pctl);
  318. else
  319. reg_pullsel = CLR_ADDR(mtk_get_port(pctl, pin) +
  320. pctl->devdata->pullsel_offset, pctl);
  321. regmap_write(mtk_get_regmap(pctl, pin), reg_pullen, bit);
  322. regmap_write(mtk_get_regmap(pctl, pin), reg_pullsel, bit);
  323. return 0;
  324. }
  325. static int mtk_pconf_parse_conf(struct pinctrl_dev *pctldev,
  326. unsigned int pin, enum pin_config_param param,
  327. enum pin_config_param arg)
  328. {
  329. int ret = 0;
  330. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  331. switch (param) {
  332. case PIN_CONFIG_BIAS_DISABLE:
  333. ret = mtk_pconf_set_pull_select(pctl, pin, false, false, arg);
  334. break;
  335. case PIN_CONFIG_BIAS_PULL_UP:
  336. ret = mtk_pconf_set_pull_select(pctl, pin, true, true, arg);
  337. break;
  338. case PIN_CONFIG_BIAS_PULL_DOWN:
  339. ret = mtk_pconf_set_pull_select(pctl, pin, true, false, arg);
  340. break;
  341. case PIN_CONFIG_INPUT_ENABLE:
  342. ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
  343. break;
  344. case PIN_CONFIG_OUTPUT:
  345. mtk_gpio_set(pctl->chip, pin, arg);
  346. ret = mtk_pmx_gpio_set_direction(pctldev, NULL, pin, false);
  347. break;
  348. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  349. ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
  350. break;
  351. case PIN_CONFIG_DRIVE_STRENGTH:
  352. ret = mtk_pconf_set_driving(pctl, pin, arg);
  353. break;
  354. case PIN_CONFIG_SLEW_RATE:
  355. mtk_pconf_set_direction(pctl, pin, arg, param);
  356. break;
  357. default:
  358. ret = -EINVAL;
  359. }
  360. return ret;
  361. }
  362. static int mtk_pconf_group_get(struct pinctrl_dev *pctldev,
  363. unsigned group,
  364. unsigned long *config)
  365. {
  366. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  367. *config = pctl->groups[group].config;
  368. return 0;
  369. }
  370. static int mtk_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
  371. unsigned long *configs, unsigned num_configs)
  372. {
  373. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  374. struct mtk_pinctrl_group *g = &pctl->groups[group];
  375. int i, ret;
  376. for (i = 0; i < num_configs; i++) {
  377. ret = mtk_pconf_parse_conf(pctldev, g->pin,
  378. pinconf_to_config_param(configs[i]),
  379. pinconf_to_config_argument(configs[i]));
  380. if (ret < 0)
  381. return ret;
  382. g->config = configs[i];
  383. }
  384. return 0;
  385. }
  386. static const struct pinconf_ops mtk_pconf_ops = {
  387. .pin_config_group_get = mtk_pconf_group_get,
  388. .pin_config_group_set = mtk_pconf_group_set,
  389. };
  390. static struct mtk_pinctrl_group *
  391. mtk_pctrl_find_group_by_pin(struct mtk_pinctrl *pctl, u32 pin)
  392. {
  393. int i;
  394. for (i = 0; i < pctl->ngroups; i++) {
  395. struct mtk_pinctrl_group *grp = pctl->groups + i;
  396. if (grp->pin == pin)
  397. return grp;
  398. }
  399. return NULL;
  400. }
  401. static const struct mtk_desc_function *mtk_pctrl_find_function_by_pin(
  402. struct mtk_pinctrl *pctl, u32 pin_num, u32 fnum)
  403. {
  404. const struct mtk_desc_pin *pin = pctl->devdata->pins + pin_num;
  405. const struct mtk_desc_function *func = pin->functions;
  406. while (func && func->name) {
  407. if (func->muxval == fnum)
  408. return func;
  409. func++;
  410. }
  411. return NULL;
  412. }
  413. static bool mtk_pctrl_is_function_valid(struct mtk_pinctrl *pctl,
  414. u32 pin_num, u32 fnum)
  415. {
  416. int i;
  417. for (i = 0; i < pctl->devdata->npins; i++) {
  418. const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
  419. if (pin->pin.number == pin_num) {
  420. const struct mtk_desc_function *func =
  421. pin->functions;
  422. while (func && func->name) {
  423. if (func->muxval == fnum)
  424. return true;
  425. func++;
  426. }
  427. break;
  428. }
  429. }
  430. return false;
  431. }
  432. static int mtk_pctrl_dt_node_to_map_func(struct mtk_pinctrl *pctl,
  433. u32 pin, u32 fnum, struct mtk_pinctrl_group *grp,
  434. struct pinctrl_map **map, unsigned *reserved_maps,
  435. unsigned *num_maps)
  436. {
  437. bool ret;
  438. if (*num_maps == *reserved_maps)
  439. return -ENOSPC;
  440. (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
  441. (*map)[*num_maps].data.mux.group = grp->name;
  442. ret = mtk_pctrl_is_function_valid(pctl, pin, fnum);
  443. if (!ret) {
  444. dev_err(pctl->dev, "invalid function %d on pin %d .\n",
  445. fnum, pin);
  446. return -EINVAL;
  447. }
  448. (*map)[*num_maps].data.mux.function = mtk_gpio_functions[fnum];
  449. (*num_maps)++;
  450. return 0;
  451. }
  452. static int mtk_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  453. struct device_node *node,
  454. struct pinctrl_map **map,
  455. unsigned *reserved_maps,
  456. unsigned *num_maps)
  457. {
  458. struct property *pins;
  459. u32 pinfunc, pin, func;
  460. int num_pins, num_funcs, maps_per_pin;
  461. unsigned long *configs;
  462. unsigned int num_configs;
  463. bool has_config = 0;
  464. int i, err;
  465. unsigned reserve = 0;
  466. struct mtk_pinctrl_group *grp;
  467. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  468. pins = of_find_property(node, "pinmux", NULL);
  469. if (!pins) {
  470. pins = of_find_property(node, "pins", NULL);
  471. if (!pins) {
  472. dev_err(pctl->dev, "missing pins property in node %s .\n",
  473. node->name);
  474. return -EINVAL;
  475. }
  476. }
  477. err = pinconf_generic_parse_dt_config(node, &configs, &num_configs);
  478. if (num_configs)
  479. has_config = 1;
  480. num_pins = pins->length / sizeof(u32);
  481. num_funcs = num_pins;
  482. maps_per_pin = 0;
  483. if (num_funcs)
  484. maps_per_pin++;
  485. if (has_config && num_pins >= 1)
  486. maps_per_pin++;
  487. if (!num_pins || !maps_per_pin)
  488. return -EINVAL;
  489. reserve = num_pins * maps_per_pin;
  490. err = pinctrl_utils_reserve_map(pctldev, map,
  491. reserved_maps, num_maps, reserve);
  492. if (err < 0)
  493. goto fail;
  494. for (i = 0; i < num_pins; i++) {
  495. err = of_property_read_u32_index(node, "pinmux",
  496. i, &pinfunc);
  497. if (err) {
  498. err = of_property_read_u32_index(node, "pins",
  499. i, &pinfunc);
  500. if (err)
  501. goto fail;
  502. }
  503. pin = MTK_GET_PIN_NO(pinfunc);
  504. func = MTK_GET_PIN_FUNC(pinfunc);
  505. if (pin >= pctl->devdata->npins ||
  506. func >= ARRAY_SIZE(mtk_gpio_functions)) {
  507. dev_err(pctl->dev, "invalid pins value.\n");
  508. err = -EINVAL;
  509. goto fail;
  510. }
  511. grp = mtk_pctrl_find_group_by_pin(pctl, pin);
  512. if (!grp) {
  513. dev_err(pctl->dev, "unable to match pin %d to group\n",
  514. pin);
  515. return -EINVAL;
  516. }
  517. err = mtk_pctrl_dt_node_to_map_func(pctl, pin, func, grp, map,
  518. reserved_maps, num_maps);
  519. if (err < 0)
  520. goto fail;
  521. if (has_config) {
  522. err = pinctrl_utils_add_map_configs(pctldev, map,
  523. reserved_maps, num_maps, grp->name,
  524. configs, num_configs,
  525. PIN_MAP_TYPE_CONFIGS_GROUP);
  526. if (err < 0)
  527. goto fail;
  528. }
  529. }
  530. return 0;
  531. fail:
  532. return err;
  533. }
  534. static int mtk_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  535. struct device_node *np_config,
  536. struct pinctrl_map **map, unsigned *num_maps)
  537. {
  538. struct device_node *np;
  539. unsigned reserved_maps;
  540. int ret;
  541. *map = NULL;
  542. *num_maps = 0;
  543. reserved_maps = 0;
  544. for_each_child_of_node(np_config, np) {
  545. ret = mtk_pctrl_dt_subnode_to_map(pctldev, np, map,
  546. &reserved_maps, num_maps);
  547. if (ret < 0) {
  548. pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
  549. return ret;
  550. }
  551. }
  552. return 0;
  553. }
  554. static int mtk_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
  555. {
  556. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  557. return pctl->ngroups;
  558. }
  559. static const char *mtk_pctrl_get_group_name(struct pinctrl_dev *pctldev,
  560. unsigned group)
  561. {
  562. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  563. return pctl->groups[group].name;
  564. }
  565. static int mtk_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
  566. unsigned group,
  567. const unsigned **pins,
  568. unsigned *num_pins)
  569. {
  570. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  571. *pins = (unsigned *)&pctl->groups[group].pin;
  572. *num_pins = 1;
  573. return 0;
  574. }
  575. static const struct pinctrl_ops mtk_pctrl_ops = {
  576. .dt_node_to_map = mtk_pctrl_dt_node_to_map,
  577. .dt_free_map = pinctrl_utils_dt_free_map,
  578. .get_groups_count = mtk_pctrl_get_groups_count,
  579. .get_group_name = mtk_pctrl_get_group_name,
  580. .get_group_pins = mtk_pctrl_get_group_pins,
  581. };
  582. static int mtk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  583. {
  584. return ARRAY_SIZE(mtk_gpio_functions);
  585. }
  586. static const char *mtk_pmx_get_func_name(struct pinctrl_dev *pctldev,
  587. unsigned selector)
  588. {
  589. return mtk_gpio_functions[selector];
  590. }
  591. static int mtk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  592. unsigned function,
  593. const char * const **groups,
  594. unsigned * const num_groups)
  595. {
  596. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  597. *groups = pctl->grp_names;
  598. *num_groups = pctl->ngroups;
  599. return 0;
  600. }
  601. static int mtk_pmx_set_mode(struct pinctrl_dev *pctldev,
  602. unsigned long pin, unsigned long mode)
  603. {
  604. unsigned int reg_addr;
  605. unsigned char bit;
  606. unsigned int val;
  607. int ret;
  608. unsigned int mask = (1L << GPIO_MODE_BITS) - 1;
  609. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  610. reg_addr = ((pin / MAX_GPIO_MODE_PER_REG) << pctl->devdata->port_shf)
  611. + pctl->devdata->pinmux_offset;
  612. if (pctl->devdata->spec_set_gpio_mode) {
  613. ret = pctl->devdata->spec_set_gpio_mode(pin|0x80000000, mode);
  614. return 0;
  615. }
  616. bit = pin % MAX_GPIO_MODE_PER_REG;
  617. mask <<= (GPIO_MODE_BITS * bit);
  618. val = (mode << (GPIO_MODE_BITS * bit));
  619. return regmap_update_bits(mtk_get_regmap(pctl, pin),
  620. reg_addr, mask, val);
  621. }
  622. static const struct mtk_desc_pin *
  623. mtk_find_pin_by_eint_num(struct mtk_pinctrl *pctl, unsigned int eint_num)
  624. {
  625. int i;
  626. const struct mtk_desc_pin *pin;
  627. for (i = 0; i < pctl->devdata->npins; i++) {
  628. pin = pctl->devdata->pins + i;
  629. if (pin->eint.eintnum == eint_num)
  630. return pin;
  631. }
  632. return NULL;
  633. }
  634. static int mtk_pmx_set_mux(struct pinctrl_dev *pctldev,
  635. unsigned function,
  636. unsigned group)
  637. {
  638. bool ret;
  639. const struct mtk_desc_function *desc;
  640. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  641. struct mtk_pinctrl_group *g = pctl->groups + group;
  642. ret = mtk_pctrl_is_function_valid(pctl, g->pin, function);
  643. if (!ret) {
  644. dev_err(pctl->dev, "invaild function %d on group %d .\n",
  645. function, group);
  646. return -EINVAL;
  647. }
  648. desc = mtk_pctrl_find_function_by_pin(pctl, g->pin, function);
  649. if (!desc)
  650. return -EINVAL;
  651. mtk_pmx_set_mode(pctldev, g->pin, desc->muxval);
  652. return 0;
  653. }
  654. static const struct pinmux_ops mtk_pmx_ops = {
  655. .get_functions_count = mtk_pmx_get_funcs_cnt,
  656. .get_function_name = mtk_pmx_get_func_name,
  657. .get_function_groups = mtk_pmx_get_func_groups,
  658. .set_mux = mtk_pmx_set_mux,
  659. .gpio_set_direction = mtk_pmx_gpio_set_direction,
  660. };
  661. static int mtk_gpio_request(struct gpio_chip *chip, unsigned offset)
  662. {
  663. return pinctrl_request_gpio(chip->base + offset);
  664. }
  665. static void mtk_gpio_free(struct gpio_chip *chip, unsigned offset)
  666. {
  667. pinctrl_free_gpio(chip->base + offset);
  668. }
  669. static int mtk_gpio_direction_input(struct gpio_chip *chip,
  670. unsigned offset)
  671. {
  672. return pinctrl_gpio_direction_input(chip->base + offset);
  673. }
  674. static int mtk_gpio_direction_output(struct gpio_chip *chip,
  675. unsigned offset, int value)
  676. {
  677. mtk_gpio_set(chip, offset, value);
  678. return pinctrl_gpio_direction_output(chip->base + offset);
  679. }
  680. static int mtk_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  681. {
  682. unsigned int reg_addr;
  683. unsigned int bit;
  684. unsigned int read_val = 0;
  685. int dir = 0;
  686. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
  687. if (pctl->devdata->mt_get_gpio_dir) {
  688. dir = pctl->devdata->mt_get_gpio_dir(offset|0x80000000);
  689. return dir;
  690. }
  691. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
  692. bit = BIT(offset & 0xf);
  693. regmap_read(pctl->regmap1, reg_addr, &read_val);
  694. return !!(read_val & bit);
  695. }
  696. static int mtk_gpio_get(struct gpio_chip *chip, unsigned offset)
  697. {
  698. unsigned int reg_addr;
  699. unsigned int bit;
  700. unsigned int read_val = 0;
  701. int value = -1;
  702. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
  703. if (pctl->devdata->mt_get_gpio_out != NULL && pctl->devdata->mt_get_gpio_in != NULL) {
  704. if (mtk_gpio_get_direction(chip, offset))
  705. value = pctl->devdata->mt_get_gpio_out(offset|0x80000000);
  706. else
  707. value = pctl->devdata->mt_get_gpio_in(offset|0x80000000);
  708. return value;
  709. }
  710. if (mtk_gpio_get_direction(chip, offset))
  711. reg_addr = mtk_get_port(pctl, offset) +
  712. pctl->devdata->dout_offset;
  713. else
  714. reg_addr = mtk_get_port(pctl, offset) +
  715. pctl->devdata->din_offset;
  716. bit = BIT(offset & 0xf);
  717. regmap_read(pctl->regmap1, reg_addr, &read_val);
  718. return !!(read_val & bit);
  719. }
  720. static int mtk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  721. {
  722. #ifdef CONFIG_MTK_EIC
  723. return mt_gpio_to_irq(offset);
  724. #else
  725. const struct mtk_desc_pin *pin;
  726. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
  727. struct mtk_pinctrl_group *g = pctl->groups + offset;
  728. int irq;
  729. pin = pctl->devdata->pins + offset;
  730. if (pin->eint.eintnum == NO_EINT_SUPPORT)
  731. return -EINVAL;
  732. mtk_pmx_set_mode(pctl->pctl_dev, g->pin, pin->functions->muxval);
  733. irq = irq_find_mapping(pctl->domain, pin->eint.eintnum);
  734. if (!irq)
  735. return -EINVAL;
  736. return irq;
  737. #endif
  738. }
  739. static int mtk_pinctrl_irq_request_resources(struct irq_data *d)
  740. {
  741. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  742. const struct mtk_desc_pin *pin;
  743. int ret;
  744. pin = mtk_find_pin_by_eint_num(pctl, d->hwirq);
  745. if (!pin) {
  746. dev_err(pctl->dev, "Can not find pin\n");
  747. return -EINVAL;
  748. }
  749. ret = gpio_lock_as_irq(pctl->chip, pin->pin.number);
  750. if (ret) {
  751. dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
  752. irqd_to_hwirq(d));
  753. return ret;
  754. }
  755. /* set mux to INT mode */
  756. mtk_pmx_set_mode(pctl->pctl_dev, pin->pin.number, pin->eint.eintmux);
  757. return 0;
  758. }
  759. static void mtk_pinctrl_irq_release_resources(struct irq_data *d)
  760. {
  761. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  762. const struct mtk_desc_pin *pin;
  763. pin = mtk_find_pin_by_eint_num(pctl, d->hwirq);
  764. if (!pin) {
  765. dev_err(pctl->dev, "Can not find pin\n");
  766. return;
  767. }
  768. gpio_unlock_as_irq(pctl->chip, pin->pin.number);
  769. }
  770. static void __iomem *mtk_eint_get_offset(struct mtk_pinctrl *pctl,
  771. unsigned int eint_num, unsigned int offset)
  772. {
  773. unsigned int eint_base = 0;
  774. void __iomem *reg;
  775. if (eint_num >= pctl->devdata->ap_num)
  776. eint_base = pctl->devdata->ap_num;
  777. reg = pctl->eint_reg_base + offset + ((eint_num - eint_base) / 32) * 4;
  778. return reg;
  779. }
  780. #ifndef CONFIG_MTK_EIC
  781. /*
  782. * mtk_can_en_debounce: Check the EINT number is able to enable debounce or not
  783. * @eint_num: the EINT number to setmtk_pinctrl
  784. */
  785. static unsigned int mtk_eint_can_en_debounce(struct mtk_pinctrl *pctl,
  786. unsigned int eint_num)
  787. {
  788. unsigned int sens;
  789. unsigned int bit = BIT(eint_num % 32);
  790. const struct mtk_eint_offsets *eint_offsets =
  791. &pctl->devdata->eint_offsets;
  792. void __iomem *reg = mtk_eint_get_offset(pctl, eint_num,
  793. eint_offsets->sens);
  794. if (readl(reg) & bit)
  795. sens = MT_LEVEL_SENSITIVE;
  796. else
  797. sens = MT_EDGE_SENSITIVE;
  798. if ((eint_num < pctl->devdata->db_cnt) && (sens != MT_EDGE_SENSITIVE))
  799. return 1;
  800. else
  801. return 0;
  802. }
  803. /*
  804. * mtk_eint_get_mask: To get the eint mask
  805. * @eint_num: the EINT number to get
  806. */
  807. static unsigned int mtk_eint_get_mask(struct mtk_pinctrl *pctl,
  808. unsigned int eint_num)
  809. {
  810. unsigned int bit = BIT(eint_num % 32);
  811. const struct mtk_eint_offsets *eint_offsets =
  812. &pctl->devdata->eint_offsets;
  813. void __iomem *reg = mtk_eint_get_offset(pctl, eint_num,
  814. eint_offsets->mask);
  815. return !!(readl(reg) & bit);
  816. }
  817. #endif
  818. static int mtk_eint_flip_edge(struct mtk_pinctrl *pctl, int hwirq)
  819. {
  820. int start_level, curr_level;
  821. unsigned int reg_offset;
  822. const struct mtk_eint_offsets *eint_offsets = &(pctl->devdata->eint_offsets);
  823. u32 mask = 1 << (hwirq & 0x1f);
  824. u32 port = (hwirq >> 5) & eint_offsets->port_mask;
  825. void __iomem *reg = pctl->eint_reg_base + (port << 2);
  826. const struct mtk_desc_pin *pin;
  827. pin = mtk_find_pin_by_eint_num(pctl, hwirq);
  828. curr_level = mtk_gpio_get(pctl->chip, pin->pin.number);
  829. do {
  830. start_level = curr_level;
  831. if (start_level)
  832. reg_offset = eint_offsets->pol_clr;
  833. else
  834. reg_offset = eint_offsets->pol_set;
  835. writel(mask, reg + reg_offset);
  836. curr_level = mtk_gpio_get(pctl->chip, pin->pin.number);
  837. } while (start_level != curr_level);
  838. return start_level;
  839. }
  840. static void mtk_eint_mask(struct irq_data *d)
  841. {
  842. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  843. const struct mtk_eint_offsets *eint_offsets =
  844. &pctl->devdata->eint_offsets;
  845. u32 mask = BIT(d->hwirq & 0x1f);
  846. void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
  847. eint_offsets->mask_set);
  848. writel(mask, reg);
  849. }
  850. static void mtk_eint_unmask(struct irq_data *d)
  851. {
  852. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  853. const struct mtk_eint_offsets *eint_offsets =
  854. &pctl->devdata->eint_offsets;
  855. u32 mask = BIT(d->hwirq & 0x1f);
  856. void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
  857. eint_offsets->mask_clr);
  858. writel(mask, reg);
  859. if (pctl->eint_dual_edges[d->hwirq])
  860. mtk_eint_flip_edge(pctl, d->hwirq);
  861. }
  862. static int mtk_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
  863. unsigned debounce)
  864. {
  865. #ifdef CONFIG_MTK_EIC
  866. mt_eint_set_hw_debounce(offset, debounce);
  867. return 0;
  868. #else
  869. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
  870. int eint_num, virq, eint_offset;
  871. unsigned int set_offset, bit, clr_bit, clr_offset, rst, i, unmask, dbnc;
  872. static const unsigned int dbnc_arr[] = {0 , 1, 16, 32, 64, 128, 256};
  873. const struct mtk_desc_pin *pin;
  874. struct irq_data *d;
  875. pin = pctl->devdata->pins + offset;
  876. if (pin->eint.eintnum == NO_EINT_SUPPORT)
  877. return -EINVAL;
  878. eint_num = pin->eint.eintnum;
  879. virq = irq_find_mapping(pctl->domain, eint_num);
  880. eint_offset = (eint_num % 4) * 8;
  881. d = irq_get_irq_data(virq);
  882. set_offset = (eint_num / 4) * 4 + pctl->devdata->eint_offsets.dbnc_set;
  883. clr_offset = (eint_num / 4) * 4 + pctl->devdata->eint_offsets.dbnc_clr;
  884. if (!mtk_eint_can_en_debounce(pctl, eint_num))
  885. return -ENOSYS;
  886. dbnc = ARRAY_SIZE(dbnc_arr);
  887. for (i = 0; i < ARRAY_SIZE(dbnc_arr); i++) {
  888. if (debounce <= dbnc_arr[i]) {
  889. dbnc = i;
  890. break;
  891. }
  892. }
  893. if (!mtk_eint_get_mask(pctl, eint_num)) {
  894. mtk_eint_mask(d);
  895. unmask = 1;
  896. } else {
  897. unmask = 0;
  898. }
  899. clr_bit = 0xff << eint_offset;
  900. writel(clr_bit, pctl->eint_reg_base + clr_offset);
  901. bit = ((dbnc << EINT_DBNC_SET_DBNC_BITS) | EINT_DBNC_SET_EN) <<
  902. eint_offset;
  903. rst = EINT_DBNC_RST_BIT << eint_offset;
  904. writel(rst | bit, pctl->eint_reg_base + set_offset);
  905. /* Delay a while (more than 2T) to wait for hw debounce counter reset
  906. work correctly */
  907. udelay(1);
  908. if (unmask == 1)
  909. mtk_eint_unmask(d);
  910. return 0;
  911. #endif
  912. }
  913. static int mtk_pinmux_get(struct gpio_chip *chip, unsigned offset)
  914. {
  915. unsigned int reg_addr;
  916. unsigned char bit;
  917. unsigned int pinmux;
  918. unsigned int mask = (1L << GPIO_MODE_BITS) - 1;
  919. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
  920. reg_addr = ((offset / MAX_GPIO_MODE_PER_REG) << pctl->devdata->port_shf)
  921. + pctl->devdata->pinmux_offset;
  922. bit = offset % MAX_GPIO_MODE_PER_REG;
  923. mask <<= (GPIO_MODE_BITS * bit);
  924. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &pinmux);
  925. return ((pinmux & mask) >> (GPIO_MODE_BITS * bit));
  926. }
  927. static int mtk_gpio_get_in(struct gpio_chip *chip, unsigned offset)
  928. {
  929. unsigned int reg_addr;
  930. unsigned int bit;
  931. unsigned int read_val = 0;
  932. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
  933. reg_addr = mtk_get_port(pctl, offset) +
  934. pctl->devdata->din_offset;
  935. bit = BIT(offset & 0xf);
  936. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &read_val);
  937. return !!(read_val & bit);
  938. }
  939. static int mtk_gpio_get_out(struct gpio_chip *chip, unsigned offset)
  940. {
  941. unsigned int reg_addr;
  942. unsigned int bit;
  943. unsigned int read_val = 0;
  944. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
  945. reg_addr = mtk_get_port(pctl, offset) +
  946. pctl->devdata->dout_offset;
  947. bit = BIT(offset & 0xf);
  948. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &read_val);
  949. return !!(read_val & bit);
  950. }
  951. static int mtk_pullen_get(struct gpio_chip *chip, unsigned offset)
  952. {
  953. unsigned int reg_addr;
  954. unsigned int bit;
  955. unsigned int pull_en = 0;
  956. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
  957. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->pullen_offset;
  958. bit = BIT(offset & 0xf);
  959. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &pull_en);
  960. return !!(pull_en & bit);
  961. }
  962. int mtk_spec_pull_get_samereg(struct regmap *regmap,
  963. const struct mtk_pin_spec_pupd_set_samereg *pupd_infos,
  964. unsigned int info_num, unsigned int pin)
  965. {
  966. unsigned int i;
  967. unsigned int reg_pupd;
  968. unsigned int val, bit_pupd, bit_r0, bit_r1;
  969. const struct mtk_pin_spec_pupd_set_samereg *spec_pupd_pin;
  970. bool find = false;
  971. for (i = 0; i < info_num; i++) {
  972. if (pin == pupd_infos[i].pin) {
  973. find = true;
  974. break;
  975. }
  976. }
  977. if (!find)
  978. return -1;
  979. spec_pupd_pin = pupd_infos + i;
  980. reg_pupd = spec_pupd_pin->offset;
  981. regmap_read(regmap, reg_pupd, &val);
  982. bit_pupd = !(val & BIT(spec_pupd_pin->pupd_bit));
  983. bit_r0 = !!(val & BIT(spec_pupd_pin->r0_bit));
  984. bit_r1 = !!(val & BIT(spec_pupd_pin->r1_bit));
  985. return (bit_pupd)|(bit_r0<<1)|(bit_r1<<2)|(1<<3);
  986. }
  987. static int mtk_pullsel_get(struct gpio_chip *chip, unsigned offset)
  988. {
  989. unsigned int reg_addr;
  990. unsigned int bit;
  991. unsigned int pull_sel = 0;
  992. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
  993. if (!pctl->devdata->spec_pull_get)
  994. return -1;
  995. if (pctl->devdata->spec_pull_get) {
  996. pull_sel = pctl->devdata->spec_pull_get(mtk_get_regmap(pctl, offset), offset);
  997. if (pull_sel != -1)
  998. return pull_sel;
  999. }
  1000. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->pullsel_offset;
  1001. bit = BIT(offset & 0xf);
  1002. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &pull_sel);
  1003. return !!(pull_sel & bit);
  1004. }
  1005. int mtk_spec_get_ies_smt_range(struct regmap *regmap,
  1006. const struct mtk_pin_ies_smt_set *ies_smt_infos,
  1007. unsigned int info_num,
  1008. unsigned int pin)
  1009. {
  1010. unsigned int i, reg_addr, bit, val;
  1011. for (i = 0; i < info_num; i++) {
  1012. if (pin >= ies_smt_infos[i].start &&
  1013. pin <= ies_smt_infos[i].end) {
  1014. break;
  1015. }
  1016. }
  1017. if (i == info_num)
  1018. return -1;
  1019. reg_addr = ies_smt_infos[i].offset;
  1020. bit = BIT(ies_smt_infos[i].bit);
  1021. regmap_read(regmap, reg_addr, &val);
  1022. return !!(val & bit);
  1023. }
  1024. static int mtk_ies_get(struct gpio_chip *chip, unsigned offset)
  1025. {
  1026. unsigned int reg_addr;
  1027. unsigned char bit;
  1028. unsigned int ies;
  1029. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
  1030. if (!pctl->devdata->spec_ies_get ||
  1031. pctl->devdata->ies_offset == MTK_PINCTRL_NOT_SUPPORT)
  1032. return -1;
  1033. /**
  1034. * Due to some soc are not support ies config, add this special
  1035. * control to handle it.
  1036. */
  1037. if (pctl->devdata->spec_ies_get) {
  1038. ies = pctl->devdata->spec_ies_get(mtk_get_regmap(pctl, offset), offset);
  1039. if (ies != -1)
  1040. return ies;
  1041. }
  1042. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->ies_offset;
  1043. bit = BIT(offset & 0xf);
  1044. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &ies);
  1045. return !!(ies & bit);
  1046. }
  1047. static int mtk_smt_get(struct gpio_chip *chip, unsigned offset)
  1048. {
  1049. unsigned int reg_addr;
  1050. unsigned char bit;
  1051. unsigned int smt;
  1052. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
  1053. if (!pctl->devdata->spec_smt_get ||
  1054. pctl->devdata->smt_offset == MTK_PINCTRL_NOT_SUPPORT)
  1055. return -1;
  1056. /**
  1057. * Due to some soc are not support smt config, add this special
  1058. * control to handle it.
  1059. */
  1060. if (pctl->devdata->spec_smt_get) {
  1061. smt = pctl->devdata->spec_smt_get(mtk_get_regmap(pctl, offset), offset);
  1062. if (smt != -1)
  1063. return smt;
  1064. }
  1065. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->smt_offset;
  1066. bit = BIT(offset & 0xf);
  1067. regmap_read(mtk_get_regmap(pctl, offset), reg_addr, &smt);
  1068. return !!(smt & bit);
  1069. }
  1070. static int mtk_driving_get(struct gpio_chip *chip, unsigned offset)
  1071. {
  1072. const struct mtk_pin_drv_grp *pin_drv;
  1073. unsigned int val;
  1074. unsigned int bits, mask, shift;
  1075. const struct mtk_drv_group_desc *drv_grp;
  1076. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->dev);
  1077. if (offset >= pctl->devdata->npins)
  1078. return -1;
  1079. pin_drv = mtk_find_pin_drv_grp_by_pin(pctl, offset);
  1080. if (!pin_drv || pin_drv->grp > pctl->devdata->n_grp_cls)
  1081. return -1;
  1082. drv_grp = pctl->devdata->grp_desc + pin_drv->grp;
  1083. bits = drv_grp->high_bit - drv_grp->low_bit + 1;
  1084. mask = BIT(bits) - 1;
  1085. shift = pin_drv->bit + drv_grp->low_bit;
  1086. mask <<= shift;
  1087. regmap_read(mtk_get_regmap(pctl, offset), pin_drv->offset, &val);
  1088. return ((val & mask) >> shift);
  1089. }
  1090. static ssize_t mt_gpio_show_pin(struct device *dev, struct device_attribute *attr, char *buf)
  1091. {
  1092. int len = 0;
  1093. int bufLen = PAGE_SIZE;
  1094. struct mtk_pinctrl *pctl = dev_get_drvdata(dev);
  1095. struct gpio_chip *chip = pctl->chip;
  1096. unsigned i;
  1097. int pull_val;
  1098. len += snprintf(buf+len, bufLen-len,
  1099. "PIN: [MODE] [DIR] [DOUT] [DIN] [PULL_EN] [PULL_SEL] [IES] [SMT] [DRIVE] ( [R1] [R0] )\n");
  1100. for (i = 0; i < chip->ngpio; i++) {
  1101. pull_val = mtk_pullsel_get(chip, i);
  1102. len += snprintf(buf+len, bufLen-len, "%4d:% d% d% d% d% d% d% d% d% d",
  1103. i,
  1104. mtk_pinmux_get(chip, i),
  1105. mtk_gpio_get_direction(chip, i),
  1106. mtk_gpio_get_out(chip, i),
  1107. mtk_gpio_get_in(chip, i),
  1108. mtk_pullen_get(chip, i),
  1109. (pull_val >= 0) ? (pull_val&1) : -1,
  1110. mtk_ies_get(chip, i),
  1111. mtk_smt_get(chip, i),
  1112. mtk_driving_get(chip, i));
  1113. if ((pull_val & 8) && (pull_val >= 0))
  1114. len += snprintf(buf+len, bufLen-len, " %d %d", !!(pull_val&4), !!(pull_val&2));
  1115. len += snprintf(buf+len, bufLen-len, "\n");
  1116. }
  1117. return len;
  1118. }
  1119. static ssize_t mt_gpio_store_pin(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1120. {
  1121. int pin, val;
  1122. int val_set;
  1123. struct mtk_pinctrl *pctl = dev_get_drvdata(dev);
  1124. struct pinctrl_dev *pctldev = pctl->pctl_dev;
  1125. if (!strncmp(buf, "mode", 4) && (2 == sscanf(buf+4, "%d %d", &pin, &val))) {
  1126. val_set = mtk_pmx_set_mode(pctldev, pin, val);
  1127. } else if (!strncmp(buf, "dir", 3) && (2 == sscanf(buf+3, "%d %d", &pin, &val))) {
  1128. val_set = mtk_pmx_gpio_set_direction(pctldev, NULL, pin, !val);
  1129. } else if (!strncmp(buf, "out", 3) && (2 == sscanf(buf+3, "%d %d", &pin, &val))) {
  1130. mtk_pmx_gpio_set_direction(pctldev, NULL, pin, false);
  1131. mtk_gpio_set(pctl->chip, pin, val);
  1132. } else if (!strncmp(buf, "pullen", 6) && (2 == sscanf(buf+6, "%d %d", &pin, &val))) {
  1133. val_set = mtk_pconf_set_pull_select(pctl, pin, !!val, false, 0);
  1134. } else if (!strncmp(buf, "pullsel", 7) && (2 == sscanf(buf+7, "%d %d", &pin, &val))) {
  1135. val_set = mtk_pconf_set_pull_select(pctl, pin, true, !!val, 0);
  1136. } else if (!strncmp(buf, "ies", 3) && (2 == sscanf(buf+3, "%d %d", &pin, &val))) {
  1137. val_set = mtk_pconf_set_ies_smt(pctl, pin, val, PIN_CONFIG_INPUT_ENABLE);
  1138. } else if (!strncmp(buf, "smt", 3) && (2 == sscanf(buf+3, "%d %d", &pin, &val))) {
  1139. val_set = mtk_pconf_set_ies_smt(pctl, pin, val, PIN_CONFIG_INPUT_SCHMITT_ENABLE);
  1140. }
  1141. return count;
  1142. }
  1143. static DEVICE_ATTR(mt_gpio, 0664, mt_gpio_show_pin, mt_gpio_store_pin);
  1144. static struct device_attribute *gpio_attr_list[] = {
  1145. &dev_attr_mt_gpio,
  1146. };
  1147. static int mt_gpio_create_attr(struct device *dev)
  1148. {
  1149. int idx, err = 0;
  1150. int num = (int)(sizeof(gpio_attr_list)/sizeof(gpio_attr_list[0]));
  1151. if (!dev)
  1152. return -EINVAL;
  1153. for (idx = 0; idx < num; idx++) {
  1154. err = device_create_file(dev, gpio_attr_list[idx]);
  1155. if (err)
  1156. break;
  1157. }
  1158. return err;
  1159. }
  1160. static struct gpio_chip mtk_gpio_chip = {
  1161. .owner = THIS_MODULE,
  1162. .request = mtk_gpio_request,
  1163. .free = mtk_gpio_free,
  1164. .direction_input = mtk_gpio_direction_input,
  1165. .direction_output = mtk_gpio_direction_output,
  1166. .get = mtk_gpio_get,
  1167. .set = mtk_gpio_set,
  1168. .to_irq = mtk_gpio_to_irq,
  1169. .set_debounce = mtk_gpio_set_debounce,
  1170. .of_gpio_n_cells = 2,
  1171. };
  1172. static int mtk_eint_set_type(struct irq_data *d,
  1173. unsigned int type)
  1174. {
  1175. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  1176. const struct mtk_eint_offsets *eint_offsets =
  1177. &pctl->devdata->eint_offsets;
  1178. u32 mask = BIT(d->hwirq & 0x1f);
  1179. void __iomem *reg;
  1180. if (((type & IRQ_TYPE_EDGE_BOTH) && (type & IRQ_TYPE_LEVEL_MASK)) ||
  1181. ((type & IRQ_TYPE_LEVEL_MASK) == IRQ_TYPE_LEVEL_MASK)) {
  1182. dev_err(pctl->dev, "Can't configure IRQ%d (EINT%lu) for type 0x%X\n",
  1183. d->irq, d->hwirq, type);
  1184. return -EINVAL;
  1185. }
  1186. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  1187. pctl->eint_dual_edges[d->hwirq] = 1;
  1188. else
  1189. pctl->eint_dual_edges[d->hwirq] = 0;
  1190. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_EDGE_FALLING)) {
  1191. reg = mtk_eint_get_offset(pctl, d->hwirq,
  1192. eint_offsets->pol_clr);
  1193. writel(mask, reg);
  1194. } else {
  1195. reg = mtk_eint_get_offset(pctl, d->hwirq,
  1196. eint_offsets->pol_set);
  1197. writel(mask, reg);
  1198. }
  1199. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
  1200. reg = mtk_eint_get_offset(pctl, d->hwirq,
  1201. eint_offsets->sens_clr);
  1202. writel(mask, reg);
  1203. } else {
  1204. reg = mtk_eint_get_offset(pctl, d->hwirq,
  1205. eint_offsets->sens_set);
  1206. writel(mask, reg);
  1207. }
  1208. if (pctl->eint_dual_edges[d->hwirq])
  1209. mtk_eint_flip_edge(pctl, d->hwirq);
  1210. return 0;
  1211. }
  1212. static int mtk_eint_irq_set_wake(struct irq_data *d, unsigned int on)
  1213. {
  1214. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  1215. int shift = d->hwirq & 0x1f;
  1216. int reg = d->hwirq >> 5;
  1217. if (on)
  1218. pctl->wake_mask[reg] |= BIT(shift);
  1219. else
  1220. pctl->wake_mask[reg] &= ~BIT(shift);
  1221. return 0;
  1222. }
  1223. static void mtk_eint_chip_write_mask(const struct mtk_eint_offsets *chip,
  1224. void __iomem *eint_reg_base, u32 *buf)
  1225. {
  1226. int port;
  1227. void __iomem *reg;
  1228. for (port = 0; port < chip->ports; port++) {
  1229. reg = eint_reg_base + (port << 2);
  1230. writel_relaxed(~buf[port], reg + chip->mask_set);
  1231. writel_relaxed(buf[port], reg + chip->mask_clr);
  1232. }
  1233. }
  1234. static void mtk_eint_chip_read_mask(const struct mtk_eint_offsets *chip,
  1235. void __iomem *eint_reg_base, u32 *buf)
  1236. {
  1237. int port;
  1238. void __iomem *reg;
  1239. for (port = 0; port < chip->ports; port++) {
  1240. reg = eint_reg_base + chip->mask + (port << 2);
  1241. buf[port] = ~readl_relaxed(reg);
  1242. /* Mask is 0 when irq is enabled, and 1 when disabled. */
  1243. }
  1244. }
  1245. static int mtk_eint_suspend(struct device *device)
  1246. {
  1247. void __iomem *reg;
  1248. struct mtk_pinctrl *pctl = dev_get_drvdata(device);
  1249. const struct mtk_eint_offsets *eint_offsets =
  1250. &pctl->devdata->eint_offsets;
  1251. reg = pctl->eint_reg_base;
  1252. mtk_eint_chip_read_mask(eint_offsets, reg, pctl->cur_mask);
  1253. mtk_eint_chip_write_mask(eint_offsets, reg, pctl->wake_mask);
  1254. return 0;
  1255. }
  1256. static int mtk_eint_resume(struct device *device)
  1257. {
  1258. struct mtk_pinctrl *pctl = dev_get_drvdata(device);
  1259. const struct mtk_eint_offsets *eint_offsets =
  1260. &pctl->devdata->eint_offsets;
  1261. mtk_eint_chip_write_mask(eint_offsets,
  1262. pctl->eint_reg_base, pctl->cur_mask);
  1263. return 0;
  1264. }
  1265. const struct dev_pm_ops mtk_eint_pm_ops = {
  1266. .suspend = mtk_eint_suspend,
  1267. .resume = mtk_eint_resume,
  1268. };
  1269. static void mtk_eint_ack(struct irq_data *d)
  1270. {
  1271. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  1272. const struct mtk_eint_offsets *eint_offsets =
  1273. &pctl->devdata->eint_offsets;
  1274. u32 mask = BIT(d->hwirq & 0x1f);
  1275. void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
  1276. eint_offsets->ack);
  1277. writel(mask, reg);
  1278. }
  1279. static struct irq_chip mtk_pinctrl_irq_chip = {
  1280. .name = "mtk-eint",
  1281. .irq_disable = mtk_eint_mask,
  1282. .irq_mask = mtk_eint_mask,
  1283. .irq_unmask = mtk_eint_unmask,
  1284. .irq_ack = mtk_eint_ack,
  1285. .irq_set_type = mtk_eint_set_type,
  1286. .irq_set_wake = mtk_eint_irq_set_wake,
  1287. .irq_request_resources = mtk_pinctrl_irq_request_resources,
  1288. .irq_release_resources = mtk_pinctrl_irq_release_resources,
  1289. };
  1290. static unsigned int mtk_eint_init(struct mtk_pinctrl *pctl)
  1291. {
  1292. const struct mtk_eint_offsets *eint_offsets =
  1293. &pctl->devdata->eint_offsets;
  1294. void __iomem *reg = pctl->eint_reg_base + eint_offsets->dom_en;
  1295. unsigned int i;
  1296. for (i = 0; i < pctl->devdata->ap_num; i += 32) {
  1297. writel(0xffffffff, reg);
  1298. reg += 4;
  1299. }
  1300. return 0;
  1301. }
  1302. static inline void
  1303. mtk_eint_debounce_process(struct mtk_pinctrl *pctl, int index)
  1304. {
  1305. unsigned int rst, ctrl_offset;
  1306. unsigned int bit, dbnc;
  1307. const struct mtk_eint_offsets *eint_offsets =
  1308. &pctl->devdata->eint_offsets;
  1309. ctrl_offset = (index / 4) * 4 + eint_offsets->dbnc_ctrl;
  1310. dbnc = readl(pctl->eint_reg_base + ctrl_offset);
  1311. bit = EINT_DBNC_SET_EN << ((index % 4) * 8);
  1312. if ((bit & dbnc) > 0) {
  1313. ctrl_offset = (index / 4) * 4 + eint_offsets->dbnc_set;
  1314. rst = EINT_DBNC_RST_BIT << ((index % 4) * 8);
  1315. writel(rst, pctl->eint_reg_base + ctrl_offset);
  1316. }
  1317. }
  1318. static void mtk_eint_irq_handler(unsigned irq, struct irq_desc *desc)
  1319. {
  1320. struct irq_chip *chip = irq_get_chip(irq);
  1321. struct mtk_pinctrl *pctl = irq_get_handler_data(irq);
  1322. unsigned int status, eint_num;
  1323. int offset, index, virq;
  1324. const struct mtk_eint_offsets *eint_offsets =
  1325. &pctl->devdata->eint_offsets;
  1326. void __iomem *reg = mtk_eint_get_offset(pctl, 0, eint_offsets->stat);
  1327. int dual_edges, start_level, curr_level;
  1328. const struct mtk_desc_pin *pin;
  1329. chained_irq_enter(chip, desc);
  1330. for (eint_num = 0; eint_num < pctl->devdata->ap_num; eint_num += 32) {
  1331. status = readl(reg);
  1332. reg += 4;
  1333. while (status) {
  1334. offset = __ffs(status);
  1335. index = eint_num + offset;
  1336. virq = irq_find_mapping(pctl->domain, index);
  1337. status &= ~BIT(offset);
  1338. dual_edges = pctl->eint_dual_edges[index];
  1339. if (dual_edges) {
  1340. /* Clear soft-irq in case we raised it
  1341. last time */
  1342. writel(BIT(offset), reg - eint_offsets->stat +
  1343. eint_offsets->soft_clr);
  1344. pin = mtk_find_pin_by_eint_num(pctl, index);
  1345. start_level = mtk_gpio_get(pctl->chip,
  1346. pin->pin.number);
  1347. }
  1348. generic_handle_irq(virq);
  1349. if (dual_edges) {
  1350. curr_level = mtk_eint_flip_edge(pctl, index);
  1351. /* If level changed, we might lost one edge
  1352. interrupt, raised it through soft-irq */
  1353. if (start_level != curr_level)
  1354. writel(BIT(offset), reg -
  1355. eint_offsets->stat +
  1356. eint_offsets->soft_set);
  1357. }
  1358. if (index < pctl->devdata->db_cnt)
  1359. mtk_eint_debounce_process(pctl , index);
  1360. }
  1361. }
  1362. chained_irq_exit(chip, desc);
  1363. }
  1364. static int mtk_pctrl_build_state(struct platform_device *pdev)
  1365. {
  1366. struct mtk_pinctrl *pctl = platform_get_drvdata(pdev);
  1367. int i;
  1368. pctl->ngroups = pctl->devdata->npins;
  1369. /* Allocate groups */
  1370. pctl->groups = devm_kcalloc(&pdev->dev, pctl->ngroups,
  1371. sizeof(*pctl->groups), GFP_KERNEL);
  1372. if (!pctl->groups)
  1373. return -ENOMEM;
  1374. /* We assume that one pin is one group, use pin name as group name. */
  1375. pctl->grp_names = devm_kcalloc(&pdev->dev, pctl->ngroups,
  1376. sizeof(*pctl->grp_names), GFP_KERNEL);
  1377. if (!pctl->grp_names)
  1378. return -ENOMEM;
  1379. for (i = 0; i < pctl->devdata->npins; i++) {
  1380. const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
  1381. struct mtk_pinctrl_group *group = pctl->groups + i;
  1382. group->name = pin->pin.name;
  1383. group->pin = pin->pin.number;
  1384. pctl->grp_names[i] = pin->pin.name;
  1385. }
  1386. return 0;
  1387. }
  1388. int mtk_pctrl_init(struct platform_device *pdev,
  1389. const struct mtk_pinctrl_devdata *data,
  1390. struct regmap *regmap)
  1391. {
  1392. struct pinctrl_pin_desc *pins;
  1393. struct pinctrl_desc *mtk_pctrl_desc;
  1394. struct mtk_pinctrl *pctl;
  1395. struct device_node *np = pdev->dev.of_node, *node;
  1396. struct property *prop;
  1397. struct resource *res;
  1398. int i, ret, irq, ports_buf;
  1399. pr_warn("mtk_pctrl_init++++++\n");
  1400. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  1401. if (!pctl)
  1402. return -ENOMEM;
  1403. platform_set_drvdata(pdev, pctl);
  1404. prop = of_find_property(np, "pins-are-numbered", NULL);
  1405. if (!prop) {
  1406. dev_err(&pdev->dev, "only support pins-are-numbered format\n");
  1407. return -EINVAL;
  1408. }
  1409. node = of_parse_phandle(np, "mediatek,pctl-regmap", 0);
  1410. if (node) {
  1411. pctl->regmap1 = syscon_node_to_regmap(node);
  1412. if (IS_ERR(pctl->regmap1))
  1413. return PTR_ERR(pctl->regmap1);
  1414. } else if (regmap) {
  1415. pctl->regmap1 = regmap;
  1416. } else {
  1417. dev_err(&pdev->dev, "Pinctrl node has not register regmap.\n");
  1418. return -EINVAL;
  1419. }
  1420. /* Only 8135 has two base addr, other SoCs have only one. */
  1421. node = of_parse_phandle(np, "mediatek,pctl-regmap", 1);
  1422. if (node) {
  1423. pctl->regmap2 = syscon_node_to_regmap(node);
  1424. if (IS_ERR(pctl->regmap2))
  1425. return PTR_ERR(pctl->regmap2);
  1426. }
  1427. pctl->devdata = data;
  1428. ret = mtk_pctrl_build_state(pdev);
  1429. if (ret) {
  1430. dev_err(&pdev->dev, "build state failed: %d\n", ret);
  1431. return -EINVAL;
  1432. }
  1433. pins = devm_kzalloc(&pdev->dev,
  1434. pctl->devdata->npins * sizeof(*pins),
  1435. GFP_KERNEL);
  1436. if (!pins)
  1437. return -ENOMEM;
  1438. for (i = 0; i < pctl->devdata->npins; i++)
  1439. pins[i] = pctl->devdata->pins[i].pin;
  1440. mtk_pctrl_desc = devm_kzalloc(&pdev->dev, sizeof(*mtk_pctrl_desc),
  1441. GFP_KERNEL);
  1442. if (!mtk_pctrl_desc)
  1443. return -ENOMEM;
  1444. mtk_pctrl_desc->name = dev_name(&pdev->dev);
  1445. mtk_pctrl_desc->owner = THIS_MODULE;
  1446. mtk_pctrl_desc->pins = pins;
  1447. mtk_pctrl_desc->npins = pctl->devdata->npins;
  1448. mtk_pctrl_desc->confops = &mtk_pconf_ops;
  1449. mtk_pctrl_desc->pctlops = &mtk_pctrl_ops;
  1450. mtk_pctrl_desc->pmxops = &mtk_pmx_ops;
  1451. pctl->dev = &pdev->dev;
  1452. pctl->pctl_dev = pinctrl_register(mtk_pctrl_desc, &pdev->dev, pctl);
  1453. if (!pctl->pctl_dev) {
  1454. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  1455. return -EINVAL;
  1456. }
  1457. pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
  1458. if (!pctl->chip) {
  1459. ret = -ENOMEM;
  1460. goto pctrl_error;
  1461. }
  1462. *pctl->chip = mtk_gpio_chip;
  1463. pctl->chip->ngpio = pctl->devdata->npins;
  1464. pctl->chip->label = dev_name(&pdev->dev);
  1465. pctl->chip->dev = &pdev->dev;
  1466. pctl->chip->base = -1;
  1467. ret = gpiochip_add(pctl->chip);
  1468. if (ret) {
  1469. ret = -EINVAL;
  1470. goto pctrl_error;
  1471. }
  1472. /* Register the GPIO to pin mappings. */
  1473. ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
  1474. 0, 0, pctl->devdata->npins);
  1475. if (ret) {
  1476. ret = -EINVAL;
  1477. goto chip_error;
  1478. }
  1479. if (mt_gpio_create_attr(&pdev->dev))
  1480. pr_warn("mt_gpio create attribute error\n");
  1481. if (!of_property_read_bool(np, "interrupt-controller"))
  1482. return 0;
  1483. /* Get EINT register base from dts. */
  1484. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1485. if (!res) {
  1486. dev_err(&pdev->dev, "Unable to get Pinctrl resource\n");
  1487. ret = -EINVAL;
  1488. goto chip_error;
  1489. }
  1490. pctl->eint_reg_base = devm_ioremap_resource(&pdev->dev, res);
  1491. if (IS_ERR(pctl->eint_reg_base)) {
  1492. ret = -EINVAL;
  1493. goto chip_error;
  1494. }
  1495. ports_buf = pctl->devdata->eint_offsets.ports;
  1496. pctl->wake_mask = devm_kcalloc(&pdev->dev, ports_buf,
  1497. sizeof(*pctl->wake_mask), GFP_KERNEL);
  1498. if (!pctl->wake_mask) {
  1499. ret = -ENOMEM;
  1500. goto chip_error;
  1501. }
  1502. pctl->cur_mask = devm_kcalloc(&pdev->dev, ports_buf,
  1503. sizeof(*pctl->cur_mask), GFP_KERNEL);
  1504. if (!pctl->cur_mask) {
  1505. ret = -ENOMEM;
  1506. goto chip_error;
  1507. }
  1508. pctl->eint_dual_edges = devm_kcalloc(&pdev->dev, pctl->devdata->ap_num,
  1509. sizeof(int), GFP_KERNEL);
  1510. if (!pctl->eint_dual_edges) {
  1511. ret = -ENOMEM;
  1512. goto chip_error;
  1513. }
  1514. irq = irq_of_parse_and_map(np, 0);
  1515. if (!irq) {
  1516. dev_err(&pdev->dev, "couldn't parse and map irq\n");
  1517. ret = -EINVAL;
  1518. goto chip_error;
  1519. }
  1520. pctl->domain = irq_domain_add_linear(np,
  1521. pctl->devdata->ap_num, &irq_domain_simple_ops, NULL);
  1522. if (!pctl->domain) {
  1523. dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
  1524. ret = -ENOMEM;
  1525. goto chip_error;
  1526. }
  1527. mtk_eint_init(pctl);
  1528. for (i = 0; i < pctl->devdata->ap_num; i++) {
  1529. int virq = irq_create_mapping(pctl->domain, i);
  1530. irq_set_chip_and_handler(virq, &mtk_pinctrl_irq_chip,
  1531. handle_level_irq);
  1532. irq_set_chip_data(virq, pctl);
  1533. set_irq_flags(virq, IRQF_VALID);
  1534. };
  1535. irq_set_chained_handler(irq, mtk_eint_irq_handler);
  1536. irq_set_handler_data(irq, pctl);
  1537. set_irq_flags(irq, IRQF_VALID);
  1538. pr_warn("mtk_pctrl_init------ ok\n");
  1539. return 0;
  1540. chip_error:
  1541. gpiochip_remove(pctl->chip);
  1542. pctrl_error:
  1543. pinctrl_unregister(pctl->pctl_dev);
  1544. pr_warn("mtk_pctrl_init------ fail\n");
  1545. return ret;
  1546. }
  1547. MODULE_LICENSE("GPL");
  1548. MODULE_DESCRIPTION("MediaTek Pinctrl&EINT Driver");
  1549. MODULE_AUTHOR("Hongzhou Yang <hongzhou.yang@mediatek.com>");
  1550. MODULE_AUTHOR("Maoguang Meng <maoguang.meng@mediatek.com>");