pinctrl-mt7622.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. /*
  2. * MediaTek MT7622 Pinctrl Driver
  3. *
  4. * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/gpio.h>
  16. #include <linux/gpio/driver.h>
  17. #include <linux/io.h>
  18. #include <linux/init.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/of.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pinctrl/pinctrl.h>
  25. #include <linux/pinctrl/pinmux.h>
  26. #include <linux/pinctrl/pinconf.h>
  27. #include <linux/pinctrl/pinconf-generic.h>
  28. #include <linux/regmap.h>
  29. #include "../core.h"
  30. #include "../pinconf.h"
  31. #include "../pinmux.h"
  32. #include "mtk-eint.h"
  33. #define PINCTRL_PINCTRL_DEV KBUILD_MODNAME
  34. #define MTK_RANGE(_a) { .range = (_a), .nranges = ARRAY_SIZE(_a), }
  35. #define PINCTRL_PIN_GROUP(name, id) \
  36. { \
  37. name, \
  38. id##_pins, \
  39. ARRAY_SIZE(id##_pins), \
  40. id##_funcs, \
  41. }
  42. #define MTK_GPIO_MODE 1
  43. #define MTK_INPUT 0
  44. #define MTK_OUTPUT 1
  45. #define MTK_DISABLE 0
  46. #define MTK_ENABLE 1
  47. /* Custom pinconf parameters */
  48. #define MTK_PIN_CONFIG_TDSEL (PIN_CONFIG_END + 1)
  49. #define MTK_PIN_CONFIG_RDSEL (PIN_CONFIG_END + 2)
  50. /* List these attributes which could be modified for the pin */
  51. enum {
  52. PINCTRL_PIN_REG_MODE,
  53. PINCTRL_PIN_REG_DIR,
  54. PINCTRL_PIN_REG_DI,
  55. PINCTRL_PIN_REG_DO,
  56. PINCTRL_PIN_REG_SR,
  57. PINCTRL_PIN_REG_SMT,
  58. PINCTRL_PIN_REG_PD,
  59. PINCTRL_PIN_REG_PU,
  60. PINCTRL_PIN_REG_E4,
  61. PINCTRL_PIN_REG_E8,
  62. PINCTRL_PIN_REG_TDSEL,
  63. PINCTRL_PIN_REG_RDSEL,
  64. PINCTRL_PIN_REG_MAX,
  65. };
  66. /* struct mtk_pin_field - the structure that holds the information of the field
  67. * used to describe the attribute for the pin
  68. * @offset: the register offset relative to the base address
  69. * @mask: the mask used to filter out the field from the register
  70. * @bitpos: the start bit relative to the register
  71. * @next: the indication that the field would be extended to the
  72. next register
  73. */
  74. struct mtk_pin_field {
  75. u32 offset;
  76. u32 mask;
  77. u8 bitpos;
  78. u8 next;
  79. };
  80. /* struct mtk_pin_field_calc - the structure that holds the range providing
  81. * the guide used to look up the relevant field
  82. * @s_pin: the start pin within the range
  83. * @e_pin: the end pin within the range
  84. * @s_addr: the start address for the range
  85. * @x_addrs: the address distance between two consecutive registers
  86. * within the range
  87. * @s_bit: the start bit for the first register within the range
  88. * @x_bits: the bit distance between two consecutive pins within
  89. * the range
  90. */
  91. struct mtk_pin_field_calc {
  92. u16 s_pin;
  93. u16 e_pin;
  94. u32 s_addr;
  95. u8 x_addrs;
  96. u8 s_bit;
  97. u8 x_bits;
  98. };
  99. /* struct mtk_pin_reg_calc - the structure that holds all ranges used to
  100. * determine which register the pin would make use of
  101. * for certain pin attribute.
  102. * @range: the start address for the range
  103. * @nranges: the number of items in the range
  104. */
  105. struct mtk_pin_reg_calc {
  106. const struct mtk_pin_field_calc *range;
  107. unsigned int nranges;
  108. };
  109. /* struct mtk_pin_soc - the structure that holds SoC-specific data */
  110. struct mtk_pin_soc {
  111. const struct mtk_pin_reg_calc *reg_cal;
  112. const struct pinctrl_pin_desc *pins;
  113. unsigned int npins;
  114. const struct group_desc *grps;
  115. unsigned int ngrps;
  116. const struct function_desc *funcs;
  117. unsigned int nfuncs;
  118. const struct mtk_eint_regs *eint_regs;
  119. const struct mtk_eint_hw *eint_hw;
  120. };
  121. struct mtk_pinctrl {
  122. struct pinctrl_dev *pctrl;
  123. void __iomem *base;
  124. struct device *dev;
  125. struct gpio_chip chip;
  126. const struct mtk_pin_soc *soc;
  127. struct mtk_eint *eint;
  128. };
  129. static const struct mtk_pin_field_calc mt7622_pin_mode_range[] = {
  130. {0, 0, 0x320, 0x10, 16, 4},
  131. {1, 4, 0x3a0, 0x10, 16, 4},
  132. {5, 5, 0x320, 0x10, 0, 4},
  133. {6, 6, 0x300, 0x10, 4, 4},
  134. {7, 7, 0x300, 0x10, 4, 4},
  135. {8, 9, 0x350, 0x10, 20, 4},
  136. {10, 10, 0x300, 0x10, 8, 4},
  137. {11, 11, 0x300, 0x10, 8, 4},
  138. {12, 12, 0x300, 0x10, 8, 4},
  139. {13, 13, 0x300, 0x10, 8, 4},
  140. {14, 15, 0x320, 0x10, 4, 4},
  141. {16, 17, 0x320, 0x10, 20, 4},
  142. {18, 21, 0x310, 0x10, 16, 4},
  143. {22, 22, 0x380, 0x10, 16, 4},
  144. {23, 23, 0x300, 0x10, 24, 4},
  145. {24, 24, 0x300, 0x10, 24, 4},
  146. {25, 25, 0x300, 0x10, 12, 4},
  147. {25, 25, 0x300, 0x10, 12, 4},
  148. {26, 26, 0x300, 0x10, 12, 4},
  149. {27, 27, 0x300, 0x10, 12, 4},
  150. {28, 28, 0x300, 0x10, 12, 4},
  151. {29, 29, 0x300, 0x10, 12, 4},
  152. {30, 30, 0x300, 0x10, 12, 4},
  153. {31, 31, 0x300, 0x10, 12, 4},
  154. {32, 32, 0x300, 0x10, 12, 4},
  155. {33, 33, 0x300, 0x10, 12, 4},
  156. {34, 34, 0x300, 0x10, 12, 4},
  157. {35, 35, 0x300, 0x10, 12, 4},
  158. {36, 36, 0x300, 0x10, 12, 4},
  159. {37, 37, 0x300, 0x10, 20, 4},
  160. {38, 38, 0x300, 0x10, 20, 4},
  161. {39, 39, 0x300, 0x10, 20, 4},
  162. {40, 40, 0x300, 0x10, 20, 4},
  163. {41, 41, 0x300, 0x10, 20, 4},
  164. {42, 42, 0x300, 0x10, 20, 4},
  165. {43, 43, 0x300, 0x10, 20, 4},
  166. {44, 44, 0x300, 0x10, 20, 4},
  167. {45, 46, 0x300, 0x10, 20, 4},
  168. {47, 47, 0x300, 0x10, 20, 4},
  169. {48, 48, 0x300, 0x10, 20, 4},
  170. {49, 49, 0x300, 0x10, 20, 4},
  171. {50, 50, 0x300, 0x10, 20, 4},
  172. {51, 70, 0x330, 0x10, 4, 4},
  173. {71, 71, 0x300, 0x10, 16, 4},
  174. {72, 72, 0x300, 0x10, 16, 4},
  175. {73, 76, 0x310, 0x10, 0, 4},
  176. {77, 77, 0x320, 0x10, 28, 4},
  177. {78, 78, 0x320, 0x10, 12, 4},
  178. {79, 82, 0x3a0, 0x10, 0, 4},
  179. {83, 83, 0x350, 0x10, 28, 4},
  180. {84, 84, 0x330, 0x10, 0, 4},
  181. {85, 90, 0x360, 0x10, 4, 4},
  182. {91, 94, 0x390, 0x10, 16, 4},
  183. {95, 97, 0x380, 0x10, 20, 4},
  184. {98, 101, 0x390, 0x10, 0, 4},
  185. {102, 102, 0x360, 0x10, 0, 4},
  186. };
  187. static const struct mtk_pin_field_calc mt7622_pin_dir_range[] = {
  188. {0, 102, 0x0, 0x10, 0, 1},
  189. };
  190. static const struct mtk_pin_field_calc mt7622_pin_di_range[] = {
  191. {0, 102, 0x200, 0x10, 0, 1},
  192. };
  193. static const struct mtk_pin_field_calc mt7622_pin_do_range[] = {
  194. {0, 102, 0x100, 0x10, 0, 1},
  195. };
  196. static const struct mtk_pin_field_calc mt7622_pin_sr_range[] = {
  197. {0, 31, 0x910, 0x10, 0, 1},
  198. {32, 50, 0xa10, 0x10, 0, 1},
  199. {51, 70, 0x810, 0x10, 0, 1},
  200. {71, 72, 0xb10, 0x10, 0, 1},
  201. {73, 86, 0xb10, 0x10, 4, 1},
  202. {87, 90, 0xc10, 0x10, 0, 1},
  203. {91, 102, 0xb10, 0x10, 18, 1},
  204. };
  205. static const struct mtk_pin_field_calc mt7622_pin_smt_range[] = {
  206. {0, 31, 0x920, 0x10, 0, 1},
  207. {32, 50, 0xa20, 0x10, 0, 1},
  208. {51, 70, 0x820, 0x10, 0, 1},
  209. {71, 72, 0xb20, 0x10, 0, 1},
  210. {73, 86, 0xb20, 0x10, 4, 1},
  211. {87, 90, 0xc20, 0x10, 0, 1},
  212. {91, 102, 0xb20, 0x10, 18, 1},
  213. };
  214. static const struct mtk_pin_field_calc mt7622_pin_pu_range[] = {
  215. {0, 31, 0x930, 0x10, 0, 1},
  216. {32, 50, 0xa30, 0x10, 0, 1},
  217. {51, 70, 0x830, 0x10, 0, 1},
  218. {71, 72, 0xb30, 0x10, 0, 1},
  219. {73, 86, 0xb30, 0x10, 4, 1},
  220. {87, 90, 0xc30, 0x10, 0, 1},
  221. {91, 102, 0xb30, 0x10, 18, 1},
  222. };
  223. static const struct mtk_pin_field_calc mt7622_pin_pd_range[] = {
  224. {0, 31, 0x940, 0x10, 0, 1},
  225. {32, 50, 0xa40, 0x10, 0, 1},
  226. {51, 70, 0x840, 0x10, 0, 1},
  227. {71, 72, 0xb40, 0x10, 0, 1},
  228. {73, 86, 0xb40, 0x10, 4, 1},
  229. {87, 90, 0xc40, 0x10, 0, 1},
  230. {91, 102, 0xb40, 0x10, 18, 1},
  231. };
  232. static const struct mtk_pin_field_calc mt7622_pin_e4_range[] = {
  233. {0, 31, 0x960, 0x10, 0, 1},
  234. {32, 50, 0xa60, 0x10, 0, 1},
  235. {51, 70, 0x860, 0x10, 0, 1},
  236. {71, 72, 0xb60, 0x10, 0, 1},
  237. {73, 86, 0xb60, 0x10, 4, 1},
  238. {87, 90, 0xc60, 0x10, 0, 1},
  239. {91, 102, 0xb60, 0x10, 18, 1},
  240. };
  241. static const struct mtk_pin_field_calc mt7622_pin_e8_range[] = {
  242. {0, 31, 0x970, 0x10, 0, 1},
  243. {32, 50, 0xa70, 0x10, 0, 1},
  244. {51, 70, 0x870, 0x10, 0, 1},
  245. {71, 72, 0xb70, 0x10, 0, 1},
  246. {73, 86, 0xb70, 0x10, 4, 1},
  247. {87, 90, 0xc70, 0x10, 0, 1},
  248. {91, 102, 0xb70, 0x10, 18, 1},
  249. };
  250. static const struct mtk_pin_field_calc mt7622_pin_tdsel_range[] = {
  251. {0, 31, 0x980, 0x4, 0, 4},
  252. {32, 50, 0xa80, 0x4, 0, 4},
  253. {51, 70, 0x880, 0x4, 0, 4},
  254. {71, 72, 0xb80, 0x4, 0, 4},
  255. {73, 86, 0xb80, 0x4, 16, 4},
  256. {87, 90, 0xc80, 0x4, 0, 4},
  257. {91, 102, 0xb88, 0x4, 8, 4},
  258. };
  259. static const struct mtk_pin_field_calc mt7622_pin_rdsel_range[] = {
  260. {0, 31, 0x990, 0x4, 0, 6},
  261. {32, 50, 0xa90, 0x4, 0, 6},
  262. {51, 58, 0x890, 0x4, 0, 6},
  263. {59, 60, 0x894, 0x4, 28, 6},
  264. {61, 62, 0x894, 0x4, 16, 6},
  265. {63, 66, 0x898, 0x4, 8, 6},
  266. {67, 68, 0x89c, 0x4, 12, 6},
  267. {69, 70, 0x89c, 0x4, 0, 6},
  268. {71, 72, 0xb90, 0x4, 0, 6},
  269. {73, 86, 0xb90, 0x4, 24, 6},
  270. {87, 90, 0xc90, 0x4, 0, 6},
  271. {91, 102, 0xb9c, 0x4, 12, 6},
  272. };
  273. static const struct mtk_pin_reg_calc mt7622_reg_cals[PINCTRL_PIN_REG_MAX] = {
  274. [PINCTRL_PIN_REG_MODE] = MTK_RANGE(mt7622_pin_mode_range),
  275. [PINCTRL_PIN_REG_DIR] = MTK_RANGE(mt7622_pin_dir_range),
  276. [PINCTRL_PIN_REG_DI] = MTK_RANGE(mt7622_pin_di_range),
  277. [PINCTRL_PIN_REG_DO] = MTK_RANGE(mt7622_pin_do_range),
  278. [PINCTRL_PIN_REG_SR] = MTK_RANGE(mt7622_pin_sr_range),
  279. [PINCTRL_PIN_REG_SMT] = MTK_RANGE(mt7622_pin_smt_range),
  280. [PINCTRL_PIN_REG_PU] = MTK_RANGE(mt7622_pin_pu_range),
  281. [PINCTRL_PIN_REG_PD] = MTK_RANGE(mt7622_pin_pd_range),
  282. [PINCTRL_PIN_REG_E4] = MTK_RANGE(mt7622_pin_e4_range),
  283. [PINCTRL_PIN_REG_E8] = MTK_RANGE(mt7622_pin_e8_range),
  284. [PINCTRL_PIN_REG_TDSEL] = MTK_RANGE(mt7622_pin_tdsel_range),
  285. [PINCTRL_PIN_REG_RDSEL] = MTK_RANGE(mt7622_pin_rdsel_range),
  286. };
  287. static const struct pinctrl_pin_desc mt7622_pins[] = {
  288. PINCTRL_PIN(0, "GPIO_A"),
  289. PINCTRL_PIN(1, "I2S1_IN"),
  290. PINCTRL_PIN(2, "I2S1_OUT"),
  291. PINCTRL_PIN(3, "I2S_BCLK"),
  292. PINCTRL_PIN(4, "I2S_WS"),
  293. PINCTRL_PIN(5, "I2S_MCLK"),
  294. PINCTRL_PIN(6, "TXD0"),
  295. PINCTRL_PIN(7, "RXD0"),
  296. PINCTRL_PIN(8, "SPI_WP"),
  297. PINCTRL_PIN(9, "SPI_HOLD"),
  298. PINCTRL_PIN(10, "SPI_CLK"),
  299. PINCTRL_PIN(11, "SPI_MOSI"),
  300. PINCTRL_PIN(12, "SPI_MISO"),
  301. PINCTRL_PIN(13, "SPI_CS"),
  302. PINCTRL_PIN(14, "I2C_SDA"),
  303. PINCTRL_PIN(15, "I2C_SCL"),
  304. PINCTRL_PIN(16, "I2S2_IN"),
  305. PINCTRL_PIN(17, "I2S3_IN"),
  306. PINCTRL_PIN(18, "I2S4_IN"),
  307. PINCTRL_PIN(19, "I2S2_OUT"),
  308. PINCTRL_PIN(20, "I2S3_OUT"),
  309. PINCTRL_PIN(21, "I2S4_OUT"),
  310. PINCTRL_PIN(22, "GPIO_B"),
  311. PINCTRL_PIN(23, "MDC"),
  312. PINCTRL_PIN(24, "MDIO"),
  313. PINCTRL_PIN(25, "G2_TXD0"),
  314. PINCTRL_PIN(26, "G2_TXD1"),
  315. PINCTRL_PIN(27, "G2_TXD2"),
  316. PINCTRL_PIN(28, "G2_TXD3"),
  317. PINCTRL_PIN(29, "G2_TXEN"),
  318. PINCTRL_PIN(30, "G2_TXC"),
  319. PINCTRL_PIN(31, "G2_RXD0"),
  320. PINCTRL_PIN(32, "G2_RXD1"),
  321. PINCTRL_PIN(33, "G2_RXD2"),
  322. PINCTRL_PIN(34, "G2_RXD3"),
  323. PINCTRL_PIN(35, "G2_RXDV"),
  324. PINCTRL_PIN(36, "G2_RXC"),
  325. PINCTRL_PIN(37, "NCEB"),
  326. PINCTRL_PIN(38, "NWEB"),
  327. PINCTRL_PIN(39, "NREB"),
  328. PINCTRL_PIN(40, "NDL4"),
  329. PINCTRL_PIN(41, "NDL5"),
  330. PINCTRL_PIN(42, "NDL6"),
  331. PINCTRL_PIN(43, "NDL7"),
  332. PINCTRL_PIN(44, "NRB"),
  333. PINCTRL_PIN(45, "NCLE"),
  334. PINCTRL_PIN(46, "NALE"),
  335. PINCTRL_PIN(47, "NDL0"),
  336. PINCTRL_PIN(48, "NDL1"),
  337. PINCTRL_PIN(49, "NDL2"),
  338. PINCTRL_PIN(50, "NDL3"),
  339. PINCTRL_PIN(51, "MDI_TP_P0"),
  340. PINCTRL_PIN(52, "MDI_TN_P0"),
  341. PINCTRL_PIN(53, "MDI_RP_P0"),
  342. PINCTRL_PIN(54, "MDI_RN_P0"),
  343. PINCTRL_PIN(55, "MDI_TP_P1"),
  344. PINCTRL_PIN(56, "MDI_TN_P1"),
  345. PINCTRL_PIN(57, "MDI_RP_P1"),
  346. PINCTRL_PIN(58, "MDI_RN_P1"),
  347. PINCTRL_PIN(59, "MDI_RP_P2"),
  348. PINCTRL_PIN(60, "MDI_RN_P2"),
  349. PINCTRL_PIN(61, "MDI_TP_P2"),
  350. PINCTRL_PIN(62, "MDI_TN_P2"),
  351. PINCTRL_PIN(63, "MDI_TP_P3"),
  352. PINCTRL_PIN(64, "MDI_TN_P3"),
  353. PINCTRL_PIN(65, "MDI_RP_P3"),
  354. PINCTRL_PIN(66, "MDI_RN_P3"),
  355. PINCTRL_PIN(67, "MDI_RP_P4"),
  356. PINCTRL_PIN(68, "MDI_RN_P4"),
  357. PINCTRL_PIN(69, "MDI_TP_P4"),
  358. PINCTRL_PIN(70, "MDI_TN_P4"),
  359. PINCTRL_PIN(71, "PMIC_SCL"),
  360. PINCTRL_PIN(72, "PMIC_SDA"),
  361. PINCTRL_PIN(73, "SPIC1_CLK"),
  362. PINCTRL_PIN(74, "SPIC1_MOSI"),
  363. PINCTRL_PIN(75, "SPIC1_MISO"),
  364. PINCTRL_PIN(76, "SPIC1_CS"),
  365. PINCTRL_PIN(77, "GPIO_D"),
  366. PINCTRL_PIN(78, "WATCHDOG"),
  367. PINCTRL_PIN(79, "RTS3_N"),
  368. PINCTRL_PIN(80, "CTS3_N"),
  369. PINCTRL_PIN(81, "TXD3"),
  370. PINCTRL_PIN(82, "RXD3"),
  371. PINCTRL_PIN(83, "PERST0_N"),
  372. PINCTRL_PIN(84, "PERST1_N"),
  373. PINCTRL_PIN(85, "WLED_N"),
  374. PINCTRL_PIN(86, "EPHY_LED0_N"),
  375. PINCTRL_PIN(87, "AUXIN0"),
  376. PINCTRL_PIN(88, "AUXIN1"),
  377. PINCTRL_PIN(89, "AUXIN2"),
  378. PINCTRL_PIN(90, "AUXIN3"),
  379. PINCTRL_PIN(91, "TXD4"),
  380. PINCTRL_PIN(92, "RXD4"),
  381. PINCTRL_PIN(93, "RTS4_N"),
  382. PINCTRL_PIN(94, "CTS4_N"),
  383. PINCTRL_PIN(95, "PWM1"),
  384. PINCTRL_PIN(96, "PWM2"),
  385. PINCTRL_PIN(97, "PWM3"),
  386. PINCTRL_PIN(98, "PWM4"),
  387. PINCTRL_PIN(99, "PWM5"),
  388. PINCTRL_PIN(100, "PWM6"),
  389. PINCTRL_PIN(101, "PWM7"),
  390. PINCTRL_PIN(102, "GPIO_E"),
  391. };
  392. /* List all groups consisting of these pins dedicated to the enablement of
  393. * certain hardware block and the corresponding mode for all of the pins. The
  394. * hardware probably has multiple combinations of these pinouts.
  395. */
  396. /* EMMC */
  397. static int mt7622_emmc_pins[] = { 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, };
  398. static int mt7622_emmc_funcs[] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, };
  399. static int mt7622_emmc_rst_pins[] = { 37, };
  400. static int mt7622_emmc_rst_funcs[] = { 1, };
  401. /* LED for EPHY */
  402. static int mt7622_ephy_leds_pins[] = { 86, 91, 92, 93, 94, };
  403. static int mt7622_ephy_leds_funcs[] = { 0, 0, 0, 0, 0, };
  404. static int mt7622_ephy0_led_pins[] = { 86, };
  405. static int mt7622_ephy0_led_funcs[] = { 0, };
  406. static int mt7622_ephy1_led_pins[] = { 91, };
  407. static int mt7622_ephy1_led_funcs[] = { 2, };
  408. static int mt7622_ephy2_led_pins[] = { 92, };
  409. static int mt7622_ephy2_led_funcs[] = { 2, };
  410. static int mt7622_ephy3_led_pins[] = { 93, };
  411. static int mt7622_ephy3_led_funcs[] = { 2, };
  412. static int mt7622_ephy4_led_pins[] = { 94, };
  413. static int mt7622_ephy4_led_funcs[] = { 2, };
  414. /* Embedded Switch */
  415. static int mt7622_esw_pins[] = { 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
  416. 62, 63, 64, 65, 66, 67, 68, 69, 70, };
  417. static int mt7622_esw_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  418. 0, 0, 0, 0, 0, 0, 0, 0, 0, };
  419. static int mt7622_esw_p0_p1_pins[] = { 51, 52, 53, 54, 55, 56, 57, 58, };
  420. static int mt7622_esw_p0_p1_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, };
  421. static int mt7622_esw_p2_p3_p4_pins[] = { 59, 60, 61, 62, 63, 64, 65, 66, 67,
  422. 68, 69, 70, };
  423. static int mt7622_esw_p2_p3_p4_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0,
  424. 0, 0, 0, };
  425. /* RGMII via ESW */
  426. static int mt7622_rgmii_via_esw_pins[] = { 59, 60, 61, 62, 63, 64, 65, 66,
  427. 67, 68, 69, 70, };
  428. static int mt7622_rgmii_via_esw_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  429. 0, };
  430. /* RGMII via GMAC1 */
  431. static int mt7622_rgmii_via_gmac1_pins[] = { 59, 60, 61, 62, 63, 64, 65, 66,
  432. 67, 68, 69, 70, };
  433. static int mt7622_rgmii_via_gmac1_funcs[] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  434. 2, };
  435. /* RGMII via GMAC2 */
  436. static int mt7622_rgmii_via_gmac2_pins[] = { 25, 26, 27, 28, 29, 30, 31, 32,
  437. 33, 34, 35, 36, };
  438. static int mt7622_rgmii_via_gmac2_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  439. 0, };
  440. /* I2C */
  441. static int mt7622_i2c0_pins[] = { 14, 15, };
  442. static int mt7622_i2c0_funcs[] = { 0, 0, };
  443. static int mt7622_i2c1_0_pins[] = { 55, 56, };
  444. static int mt7622_i2c1_0_funcs[] = { 0, 0, };
  445. static int mt7622_i2c1_1_pins[] = { 73, 74, };
  446. static int mt7622_i2c1_1_funcs[] = { 3, 3, };
  447. static int mt7622_i2c1_2_pins[] = { 87, 88, };
  448. static int mt7622_i2c1_2_funcs[] = { 0, 0, };
  449. static int mt7622_i2c2_0_pins[] = { 57, 58, };
  450. static int mt7622_i2c2_0_funcs[] = { 0, 0, };
  451. static int mt7622_i2c2_1_pins[] = { 75, 76, };
  452. static int mt7622_i2c2_1_funcs[] = { 3, 3, };
  453. static int mt7622_i2c2_2_pins[] = { 89, 90, };
  454. static int mt7622_i2c2_2_funcs[] = { 0, 0, };
  455. /* I2S */
  456. static int mt7622_i2s_in_mclk_bclk_ws_pins[] = { 3, 4, 5, };
  457. static int mt7622_i2s_in_mclk_bclk_ws_funcs[] = { 3, 3, 0, };
  458. static int mt7622_i2s1_in_data_pins[] = { 1, };
  459. static int mt7622_i2s1_in_data_funcs[] = { 0, };
  460. static int mt7622_i2s2_in_data_pins[] = { 16, };
  461. static int mt7622_i2s2_in_data_funcs[] = { 0, };
  462. static int mt7622_i2s3_in_data_pins[] = { 17, };
  463. static int mt7622_i2s3_in_data_funcs[] = { 0, };
  464. static int mt7622_i2s4_in_data_pins[] = { 18, };
  465. static int mt7622_i2s4_in_data_funcs[] = { 0, };
  466. static int mt7622_i2s_out_mclk_bclk_ws_pins[] = { 3, 4, 5, };
  467. static int mt7622_i2s_out_mclk_bclk_ws_funcs[] = { 0, 0, 0, };
  468. static int mt7622_i2s1_out_data_pins[] = { 2, };
  469. static int mt7622_i2s1_out_data_funcs[] = { 0, };
  470. static int mt7622_i2s2_out_data_pins[] = { 19, };
  471. static int mt7622_i2s2_out_data_funcs[] = { 0, };
  472. static int mt7622_i2s3_out_data_pins[] = { 20, };
  473. static int mt7622_i2s3_out_data_funcs[] = { 0, };
  474. static int mt7622_i2s4_out_data_pins[] = { 21, };
  475. static int mt7622_i2s4_out_data_funcs[] = { 0, };
  476. /* IR */
  477. static int mt7622_ir_0_tx_pins[] = { 16, };
  478. static int mt7622_ir_0_tx_funcs[] = { 4, };
  479. static int mt7622_ir_1_tx_pins[] = { 59, };
  480. static int mt7622_ir_1_tx_funcs[] = { 5, };
  481. static int mt7622_ir_2_tx_pins[] = { 99, };
  482. static int mt7622_ir_2_tx_funcs[] = { 3, };
  483. static int mt7622_ir_0_rx_pins[] = { 17, };
  484. static int mt7622_ir_0_rx_funcs[] = { 4, };
  485. static int mt7622_ir_1_rx_pins[] = { 60, };
  486. static int mt7622_ir_1_rx_funcs[] = { 5, };
  487. static int mt7622_ir_2_rx_pins[] = { 100, };
  488. static int mt7622_ir_2_rx_funcs[] = { 3, };
  489. /* MDIO */
  490. static int mt7622_mdc_mdio_pins[] = { 23, 24, };
  491. static int mt7622_mdc_mdio_funcs[] = { 0, 0, };
  492. /* PCIE */
  493. static int mt7622_pcie0_0_waken_pins[] = { 14, };
  494. static int mt7622_pcie0_0_waken_funcs[] = { 2, };
  495. static int mt7622_pcie0_0_clkreq_pins[] = { 15, };
  496. static int mt7622_pcie0_0_clkreq_funcs[] = { 2, };
  497. static int mt7622_pcie0_1_waken_pins[] = { 79, };
  498. static int mt7622_pcie0_1_waken_funcs[] = { 4, };
  499. static int mt7622_pcie0_1_clkreq_pins[] = { 80, };
  500. static int mt7622_pcie0_1_clkreq_funcs[] = { 4, };
  501. static int mt7622_pcie1_0_waken_pins[] = { 14, };
  502. static int mt7622_pcie1_0_waken_funcs[] = { 3, };
  503. static int mt7622_pcie1_0_clkreq_pins[] = { 15, };
  504. static int mt7622_pcie1_0_clkreq_funcs[] = { 3, };
  505. static int mt7622_pcie0_pad_perst_pins[] = { 83, };
  506. static int mt7622_pcie0_pad_perst_funcs[] = { 0, };
  507. static int mt7622_pcie1_pad_perst_pins[] = { 84, };
  508. static int mt7622_pcie1_pad_perst_funcs[] = { 0, };
  509. /* PMIC bus */
  510. static int mt7622_pmic_bus_pins[] = { 71, 72, };
  511. static int mt7622_pmic_bus_funcs[] = { 0, 0, };
  512. /* Parallel NAND */
  513. static int mt7622_pnand_pins[] = { 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  514. 48, 49, 50, };
  515. static int mt7622_pnand_funcs[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  516. 0, };
  517. /* PWM */
  518. static int mt7622_pwm_ch1_0_pins[] = { 51, };
  519. static int mt7622_pwm_ch1_0_funcs[] = { 3, };
  520. static int mt7622_pwm_ch1_1_pins[] = { 73, };
  521. static int mt7622_pwm_ch1_1_funcs[] = { 4, };
  522. static int mt7622_pwm_ch1_2_pins[] = { 95, };
  523. static int mt7622_pwm_ch1_2_funcs[] = { 0, };
  524. static int mt7622_pwm_ch2_0_pins[] = { 52, };
  525. static int mt7622_pwm_ch2_0_funcs[] = { 3, };
  526. static int mt7622_pwm_ch2_1_pins[] = { 74, };
  527. static int mt7622_pwm_ch2_1_funcs[] = { 4, };
  528. static int mt7622_pwm_ch2_2_pins[] = { 96, };
  529. static int mt7622_pwm_ch2_2_funcs[] = { 0, };
  530. static int mt7622_pwm_ch3_0_pins[] = { 53, };
  531. static int mt7622_pwm_ch3_0_funcs[] = { 3, };
  532. static int mt7622_pwm_ch3_1_pins[] = { 75, };
  533. static int mt7622_pwm_ch3_1_funcs[] = { 4, };
  534. static int mt7622_pwm_ch3_2_pins[] = { 97, };
  535. static int mt7622_pwm_ch3_2_funcs[] = { 0, };
  536. static int mt7622_pwm_ch4_0_pins[] = { 54, };
  537. static int mt7622_pwm_ch4_0_funcs[] = { 3, };
  538. static int mt7622_pwm_ch4_1_pins[] = { 67, };
  539. static int mt7622_pwm_ch4_1_funcs[] = { 3, };
  540. static int mt7622_pwm_ch4_2_pins[] = { 76, };
  541. static int mt7622_pwm_ch4_2_funcs[] = { 4, };
  542. static int mt7622_pwm_ch4_3_pins[] = { 98, };
  543. static int mt7622_pwm_ch4_3_funcs[] = { 0, };
  544. static int mt7622_pwm_ch5_0_pins[] = { 68, };
  545. static int mt7622_pwm_ch5_0_funcs[] = { 3, };
  546. static int mt7622_pwm_ch5_1_pins[] = { 77, };
  547. static int mt7622_pwm_ch5_1_funcs[] = { 4, };
  548. static int mt7622_pwm_ch5_2_pins[] = { 99, };
  549. static int mt7622_pwm_ch5_2_funcs[] = { 0, };
  550. static int mt7622_pwm_ch6_0_pins[] = { 69, };
  551. static int mt7622_pwm_ch6_0_funcs[] = { 3, };
  552. static int mt7622_pwm_ch6_1_pins[] = { 78, };
  553. static int mt7622_pwm_ch6_1_funcs[] = { 4, };
  554. static int mt7622_pwm_ch6_2_pins[] = { 81, };
  555. static int mt7622_pwm_ch6_2_funcs[] = { 4, };
  556. static int mt7622_pwm_ch6_3_pins[] = { 100, };
  557. static int mt7622_pwm_ch6_3_funcs[] = { 0, };
  558. static int mt7622_pwm_ch7_0_pins[] = { 70, };
  559. static int mt7622_pwm_ch7_0_funcs[] = { 3, };
  560. static int mt7622_pwm_ch7_1_pins[] = { 82, };
  561. static int mt7622_pwm_ch7_1_funcs[] = { 4, };
  562. static int mt7622_pwm_ch7_2_pins[] = { 101, };
  563. static int mt7622_pwm_ch7_2_funcs[] = { 0, };
  564. /* SD */
  565. static int mt7622_sd_0_pins[] = { 16, 17, 18, 19, 20, 21, };
  566. static int mt7622_sd_0_funcs[] = { 2, 2, 2, 2, 2, 2, };
  567. static int mt7622_sd_1_pins[] = { 25, 26, 27, 28, 29, 30, };
  568. static int mt7622_sd_1_funcs[] = { 2, 2, 2, 2, 2, 2, };
  569. /* Serial NAND */
  570. static int mt7622_snfi_pins[] = { 8, 9, 10, 11, 12, 13, };
  571. static int mt7622_snfi_funcs[] = { 2, 2, 2, 2, 2, 2, };
  572. /* SPI NOR */
  573. static int mt7622_spi_pins[] = { 8, 9, 10, 11, 12, 13 };
  574. static int mt7622_spi_funcs[] = { 0, 0, 0, 0, 0, 0, };
  575. /* SPIC */
  576. static int mt7622_spic0_0_pins[] = { 63, 64, 65, 66, };
  577. static int mt7622_spic0_0_funcs[] = { 4, 4, 4, 4, };
  578. static int mt7622_spic0_1_pins[] = { 79, 80, 81, 82, };
  579. static int mt7622_spic0_1_funcs[] = { 3, 3, 3, 3, };
  580. static int mt7622_spic1_0_pins[] = { 67, 68, 69, 70, };
  581. static int mt7622_spic1_0_funcs[] = { 4, 4, 4, 4, };
  582. static int mt7622_spic1_1_pins[] = { 73, 74, 75, 76, };
  583. static int mt7622_spic1_1_funcs[] = { 0, 0, 0, 0, };
  584. static int mt7622_spic2_0_pins[] = { 10, 11, 12, 13, };
  585. static int mt7622_spic2_0_funcs[] = { 0, 0, 0, 0, };
  586. static int mt7622_spic2_0_wp_hold_pins[] = { 8, 9, };
  587. static int mt7622_spic2_0_wp_hold_funcs[] = { 0, 0, };
  588. /* TDM */
  589. static int mt7622_tdm_0_out_mclk_bclk_ws_pins[] = { 8, 9, 10, };
  590. static int mt7622_tdm_0_out_mclk_bclk_ws_funcs[] = { 3, 3, 3, };
  591. static int mt7622_tdm_0_in_mclk_bclk_ws_pins[] = { 11, 12, 13, };
  592. static int mt7622_tdm_0_in_mclk_bclk_ws_funcs[] = { 3, 3, 3, };
  593. static int mt7622_tdm_0_out_data_pins[] = { 20, };
  594. static int mt7622_tdm_0_out_data_funcs[] = { 3, };
  595. static int mt7622_tdm_0_in_data_pins[] = { 21, };
  596. static int mt7622_tdm_0_in_data_funcs[] = { 3, };
  597. static int mt7622_tdm_1_out_mclk_bclk_ws_pins[] = { 57, 58, 59, };
  598. static int mt7622_tdm_1_out_mclk_bclk_ws_funcs[] = { 3, 3, 3, };
  599. static int mt7622_tdm_1_in_mclk_bclk_ws_pins[] = { 60, 61, 62, };
  600. static int mt7622_tdm_1_in_mclk_bclk_ws_funcs[] = { 3, 3, 3, };
  601. static int mt7622_tdm_1_out_data_pins[] = { 55, };
  602. static int mt7622_tdm_1_out_data_funcs[] = { 3, };
  603. static int mt7622_tdm_1_in_data_pins[] = { 56, };
  604. static int mt7622_tdm_1_in_data_funcs[] = { 3, };
  605. /* UART */
  606. static int mt7622_uart0_0_tx_rx_pins[] = { 6, 7, };
  607. static int mt7622_uart0_0_tx_rx_funcs[] = { 0, 0, };
  608. static int mt7622_uart1_0_tx_rx_pins[] = { 55, 56, };
  609. static int mt7622_uart1_0_tx_rx_funcs[] = { 2, 2, };
  610. static int mt7622_uart1_0_rts_cts_pins[] = { 57, 58, };
  611. static int mt7622_uart1_0_rts_cts_funcs[] = { 2, 2, };
  612. static int mt7622_uart1_1_tx_rx_pins[] = { 73, 74, };
  613. static int mt7622_uart1_1_tx_rx_funcs[] = { 2, 2, };
  614. static int mt7622_uart1_1_rts_cts_pins[] = { 75, 76, };
  615. static int mt7622_uart1_1_rts_cts_funcs[] = { 2, 2, };
  616. static int mt7622_uart2_0_tx_rx_pins[] = { 3, 4, };
  617. static int mt7622_uart2_0_tx_rx_funcs[] = { 2, 2, };
  618. static int mt7622_uart2_0_rts_cts_pins[] = { 1, 2, };
  619. static int mt7622_uart2_0_rts_cts_funcs[] = { 2, 2, };
  620. static int mt7622_uart2_1_tx_rx_pins[] = { 51, 52, };
  621. static int mt7622_uart2_1_tx_rx_funcs[] = { 0, 0, };
  622. static int mt7622_uart2_1_rts_cts_pins[] = { 53, 54, };
  623. static int mt7622_uart2_1_rts_cts_funcs[] = { 0, 0, };
  624. static int mt7622_uart2_2_tx_rx_pins[] = { 59, 60, };
  625. static int mt7622_uart2_2_tx_rx_funcs[] = { 4, 4, };
  626. static int mt7622_uart2_2_rts_cts_pins[] = { 61, 62, };
  627. static int mt7622_uart2_2_rts_cts_funcs[] = { 4, 4, };
  628. static int mt7622_uart2_3_tx_rx_pins[] = { 95, 96, };
  629. static int mt7622_uart2_3_tx_rx_funcs[] = { 3, 3, };
  630. static int mt7622_uart3_0_tx_rx_pins[] = { 57, 58, };
  631. static int mt7622_uart3_0_tx_rx_funcs[] = { 5, 5, };
  632. static int mt7622_uart3_1_tx_rx_pins[] = { 81, 82, };
  633. static int mt7622_uart3_1_tx_rx_funcs[] = { 0, 0, };
  634. static int mt7622_uart3_1_rts_cts_pins[] = { 79, 80, };
  635. static int mt7622_uart3_1_rts_cts_funcs[] = { 0, 0, };
  636. static int mt7622_uart4_0_tx_rx_pins[] = { 61, 62, };
  637. static int mt7622_uart4_0_tx_rx_funcs[] = { 5, 5, };
  638. static int mt7622_uart4_1_tx_rx_pins[] = { 91, 92, };
  639. static int mt7622_uart4_1_tx_rx_funcs[] = { 0, 0, };
  640. static int mt7622_uart4_1_rts_cts_pins[] = { 93, 94 };
  641. static int mt7622_uart4_1_rts_cts_funcs[] = { 0, 0, };
  642. static int mt7622_uart4_2_tx_rx_pins[] = { 97, 98, };
  643. static int mt7622_uart4_2_tx_rx_funcs[] = { 2, 2, };
  644. static int mt7622_uart4_2_rts_cts_pins[] = { 95, 96 };
  645. static int mt7622_uart4_2_rts_cts_funcs[] = { 2, 2, };
  646. /* Watchdog */
  647. static int mt7622_watchdog_pins[] = { 78, };
  648. static int mt7622_watchdog_funcs[] = { 0, };
  649. /* WLAN LED */
  650. static int mt7622_wled_pins[] = { 85, };
  651. static int mt7622_wled_funcs[] = { 0, };
  652. static const struct group_desc mt7622_groups[] = {
  653. PINCTRL_PIN_GROUP("emmc", mt7622_emmc),
  654. PINCTRL_PIN_GROUP("emmc_rst", mt7622_emmc_rst),
  655. PINCTRL_PIN_GROUP("ephy_leds", mt7622_ephy_leds),
  656. PINCTRL_PIN_GROUP("ephy0_led", mt7622_ephy0_led),
  657. PINCTRL_PIN_GROUP("ephy1_led", mt7622_ephy1_led),
  658. PINCTRL_PIN_GROUP("ephy2_led", mt7622_ephy2_led),
  659. PINCTRL_PIN_GROUP("ephy3_led", mt7622_ephy3_led),
  660. PINCTRL_PIN_GROUP("ephy4_led", mt7622_ephy4_led),
  661. PINCTRL_PIN_GROUP("esw", mt7622_esw),
  662. PINCTRL_PIN_GROUP("esw_p0_p1", mt7622_esw_p0_p1),
  663. PINCTRL_PIN_GROUP("esw_p2_p3_p4", mt7622_esw_p2_p3_p4),
  664. PINCTRL_PIN_GROUP("rgmii_via_esw", mt7622_rgmii_via_esw),
  665. PINCTRL_PIN_GROUP("rgmii_via_gmac1", mt7622_rgmii_via_gmac1),
  666. PINCTRL_PIN_GROUP("rgmii_via_gmac2", mt7622_rgmii_via_gmac2),
  667. PINCTRL_PIN_GROUP("i2c0", mt7622_i2c0),
  668. PINCTRL_PIN_GROUP("i2c1_0", mt7622_i2c1_0),
  669. PINCTRL_PIN_GROUP("i2c1_1", mt7622_i2c1_1),
  670. PINCTRL_PIN_GROUP("i2c1_2", mt7622_i2c1_2),
  671. PINCTRL_PIN_GROUP("i2c2_0", mt7622_i2c2_0),
  672. PINCTRL_PIN_GROUP("i2c2_1", mt7622_i2c2_1),
  673. PINCTRL_PIN_GROUP("i2c2_2", mt7622_i2c2_2),
  674. PINCTRL_PIN_GROUP("i2s_out_mclk_bclk_ws", mt7622_i2s_out_mclk_bclk_ws),
  675. PINCTRL_PIN_GROUP("i2s_in_mclk_bclk_ws", mt7622_i2s_in_mclk_bclk_ws),
  676. PINCTRL_PIN_GROUP("i2s1_in_data", mt7622_i2s1_in_data),
  677. PINCTRL_PIN_GROUP("i2s2_in_data", mt7622_i2s2_in_data),
  678. PINCTRL_PIN_GROUP("i2s3_in_data", mt7622_i2s3_in_data),
  679. PINCTRL_PIN_GROUP("i2s4_in_data", mt7622_i2s4_in_data),
  680. PINCTRL_PIN_GROUP("i2s1_out_data", mt7622_i2s1_out_data),
  681. PINCTRL_PIN_GROUP("i2s2_out_data", mt7622_i2s2_out_data),
  682. PINCTRL_PIN_GROUP("i2s3_out_data", mt7622_i2s3_out_data),
  683. PINCTRL_PIN_GROUP("i2s4_out_data", mt7622_i2s4_out_data),
  684. PINCTRL_PIN_GROUP("ir_0_tx", mt7622_ir_0_tx),
  685. PINCTRL_PIN_GROUP("ir_1_tx", mt7622_ir_1_tx),
  686. PINCTRL_PIN_GROUP("ir_2_tx", mt7622_ir_2_tx),
  687. PINCTRL_PIN_GROUP("ir_0_rx", mt7622_ir_0_rx),
  688. PINCTRL_PIN_GROUP("ir_1_rx", mt7622_ir_1_rx),
  689. PINCTRL_PIN_GROUP("ir_2_rx", mt7622_ir_2_rx),
  690. PINCTRL_PIN_GROUP("mdc_mdio", mt7622_mdc_mdio),
  691. PINCTRL_PIN_GROUP("pcie0_0_waken", mt7622_pcie0_0_waken),
  692. PINCTRL_PIN_GROUP("pcie0_0_clkreq", mt7622_pcie0_0_clkreq),
  693. PINCTRL_PIN_GROUP("pcie0_1_waken", mt7622_pcie0_1_waken),
  694. PINCTRL_PIN_GROUP("pcie0_1_clkreq", mt7622_pcie0_1_clkreq),
  695. PINCTRL_PIN_GROUP("pcie1_0_waken", mt7622_pcie1_0_waken),
  696. PINCTRL_PIN_GROUP("pcie1_0_clkreq", mt7622_pcie1_0_clkreq),
  697. PINCTRL_PIN_GROUP("pcie0_pad_perst", mt7622_pcie0_pad_perst),
  698. PINCTRL_PIN_GROUP("pcie1_pad_perst", mt7622_pcie1_pad_perst),
  699. PINCTRL_PIN_GROUP("par_nand", mt7622_pnand),
  700. PINCTRL_PIN_GROUP("pmic_bus", mt7622_pmic_bus),
  701. PINCTRL_PIN_GROUP("pwm_ch1_0", mt7622_pwm_ch1_0),
  702. PINCTRL_PIN_GROUP("pwm_ch1_1", mt7622_pwm_ch1_1),
  703. PINCTRL_PIN_GROUP("pwm_ch1_2", mt7622_pwm_ch1_2),
  704. PINCTRL_PIN_GROUP("pwm_ch2_0", mt7622_pwm_ch2_0),
  705. PINCTRL_PIN_GROUP("pwm_ch2_1", mt7622_pwm_ch2_1),
  706. PINCTRL_PIN_GROUP("pwm_ch2_2", mt7622_pwm_ch2_2),
  707. PINCTRL_PIN_GROUP("pwm_ch3_0", mt7622_pwm_ch3_0),
  708. PINCTRL_PIN_GROUP("pwm_ch3_1", mt7622_pwm_ch3_1),
  709. PINCTRL_PIN_GROUP("pwm_ch3_2", mt7622_pwm_ch3_2),
  710. PINCTRL_PIN_GROUP("pwm_ch4_0", mt7622_pwm_ch4_0),
  711. PINCTRL_PIN_GROUP("pwm_ch4_1", mt7622_pwm_ch4_1),
  712. PINCTRL_PIN_GROUP("pwm_ch4_2", mt7622_pwm_ch4_2),
  713. PINCTRL_PIN_GROUP("pwm_ch4_3", mt7622_pwm_ch4_3),
  714. PINCTRL_PIN_GROUP("pwm_ch5_0", mt7622_pwm_ch5_0),
  715. PINCTRL_PIN_GROUP("pwm_ch5_1", mt7622_pwm_ch5_1),
  716. PINCTRL_PIN_GROUP("pwm_ch5_2", mt7622_pwm_ch5_2),
  717. PINCTRL_PIN_GROUP("pwm_ch6_0", mt7622_pwm_ch6_0),
  718. PINCTRL_PIN_GROUP("pwm_ch6_1", mt7622_pwm_ch6_1),
  719. PINCTRL_PIN_GROUP("pwm_ch6_2", mt7622_pwm_ch6_2),
  720. PINCTRL_PIN_GROUP("pwm_ch6_3", mt7622_pwm_ch6_3),
  721. PINCTRL_PIN_GROUP("pwm_ch7_0", mt7622_pwm_ch7_0),
  722. PINCTRL_PIN_GROUP("pwm_ch7_1", mt7622_pwm_ch7_1),
  723. PINCTRL_PIN_GROUP("pwm_ch7_2", mt7622_pwm_ch7_2),
  724. PINCTRL_PIN_GROUP("sd_0", mt7622_sd_0),
  725. PINCTRL_PIN_GROUP("sd_1", mt7622_sd_1),
  726. PINCTRL_PIN_GROUP("snfi", mt7622_snfi),
  727. PINCTRL_PIN_GROUP("spi_nor", mt7622_spi),
  728. PINCTRL_PIN_GROUP("spic0_0", mt7622_spic0_0),
  729. PINCTRL_PIN_GROUP("spic0_1", mt7622_spic0_1),
  730. PINCTRL_PIN_GROUP("spic1_0", mt7622_spic1_0),
  731. PINCTRL_PIN_GROUP("spic1_1", mt7622_spic1_1),
  732. PINCTRL_PIN_GROUP("spic2_0", mt7622_spic2_0),
  733. PINCTRL_PIN_GROUP("spic2_0_wp_hold", mt7622_spic2_0_wp_hold),
  734. PINCTRL_PIN_GROUP("tdm_0_out_mclk_bclk_ws",
  735. mt7622_tdm_0_out_mclk_bclk_ws),
  736. PINCTRL_PIN_GROUP("tdm_0_in_mclk_bclk_ws",
  737. mt7622_tdm_0_in_mclk_bclk_ws),
  738. PINCTRL_PIN_GROUP("tdm_0_out_data", mt7622_tdm_0_out_data),
  739. PINCTRL_PIN_GROUP("tdm_0_in_data", mt7622_tdm_0_in_data),
  740. PINCTRL_PIN_GROUP("tdm_1_out_mclk_bclk_ws",
  741. mt7622_tdm_1_out_mclk_bclk_ws),
  742. PINCTRL_PIN_GROUP("tdm_1_in_mclk_bclk_ws",
  743. mt7622_tdm_1_in_mclk_bclk_ws),
  744. PINCTRL_PIN_GROUP("tdm_1_out_data", mt7622_tdm_1_out_data),
  745. PINCTRL_PIN_GROUP("tdm_1_in_data", mt7622_tdm_1_in_data),
  746. PINCTRL_PIN_GROUP("uart0_0_tx_rx", mt7622_uart0_0_tx_rx),
  747. PINCTRL_PIN_GROUP("uart1_0_tx_rx", mt7622_uart1_0_tx_rx),
  748. PINCTRL_PIN_GROUP("uart1_0_rts_cts", mt7622_uart1_0_rts_cts),
  749. PINCTRL_PIN_GROUP("uart1_1_tx_rx", mt7622_uart1_1_tx_rx),
  750. PINCTRL_PIN_GROUP("uart1_1_rts_cts", mt7622_uart1_1_rts_cts),
  751. PINCTRL_PIN_GROUP("uart2_0_tx_rx", mt7622_uart2_0_tx_rx),
  752. PINCTRL_PIN_GROUP("uart2_0_rts_cts", mt7622_uart2_0_rts_cts),
  753. PINCTRL_PIN_GROUP("uart2_1_tx_rx", mt7622_uart2_1_tx_rx),
  754. PINCTRL_PIN_GROUP("uart2_1_rts_cts", mt7622_uart2_1_rts_cts),
  755. PINCTRL_PIN_GROUP("uart2_2_tx_rx", mt7622_uart2_2_tx_rx),
  756. PINCTRL_PIN_GROUP("uart2_2_rts_cts", mt7622_uart2_2_rts_cts),
  757. PINCTRL_PIN_GROUP("uart2_3_tx_rx", mt7622_uart2_3_tx_rx),
  758. PINCTRL_PIN_GROUP("uart3_0_tx_rx", mt7622_uart3_0_tx_rx),
  759. PINCTRL_PIN_GROUP("uart3_1_tx_rx", mt7622_uart3_1_tx_rx),
  760. PINCTRL_PIN_GROUP("uart3_1_rts_cts", mt7622_uart3_1_rts_cts),
  761. PINCTRL_PIN_GROUP("uart4_0_tx_rx", mt7622_uart4_0_tx_rx),
  762. PINCTRL_PIN_GROUP("uart4_1_tx_rx", mt7622_uart4_1_tx_rx),
  763. PINCTRL_PIN_GROUP("uart4_1_rts_cts", mt7622_uart4_1_rts_cts),
  764. PINCTRL_PIN_GROUP("uart4_2_tx_rx", mt7622_uart4_2_tx_rx),
  765. PINCTRL_PIN_GROUP("uart4_2_rts_cts", mt7622_uart4_2_rts_cts),
  766. PINCTRL_PIN_GROUP("watchdog", mt7622_watchdog),
  767. PINCTRL_PIN_GROUP("wled", mt7622_wled),
  768. };
  769. /* Joint those groups owning the same capability in user point of view which
  770. * allows that people tend to use through the device tree.
  771. */
  772. static const char *mt7622_emmc_groups[] = { "emmc", "emmc_rst", };
  773. static const char *mt7622_ethernet_groups[] = { "esw", "esw_p0_p1",
  774. "esw_p2_p3_p4", "mdc_mdio",
  775. "rgmii_via_gmac1",
  776. "rgmii_via_gmac2",
  777. "rgmii_via_esw", };
  778. static const char *mt7622_i2c_groups[] = { "i2c0", "i2c1_0", "i2c1_1",
  779. "i2c1_2", "i2c2_0", "i2c2_1",
  780. "i2c2_2", };
  781. static const char *mt7622_i2s_groups[] = { "i2s_out_mclk_bclk_ws",
  782. "i2s_in_mclk_bclk_ws",
  783. "i2s1_in_data", "i2s2_in_data",
  784. "i2s3_in_data", "i2s4_in_data",
  785. "i2s1_out_data", "i2s2_out_data",
  786. "i2s3_out_data", "i2s4_out_data", };
  787. static const char *mt7622_ir_groups[] = { "ir_0_tx", "ir_1_tx", "ir_2_tx",
  788. "ir_0_rx", "ir_1_rx", "ir_2_rx"};
  789. static const char *mt7622_led_groups[] = { "ephy_leds", "ephy0_led",
  790. "ephy1_led", "ephy2_led",
  791. "ephy3_led", "ephy4_led",
  792. "wled", };
  793. static const char *mt7622_flash_groups[] = { "par_nand", "snfi", "spi_nor"};
  794. static const char *mt7622_pcie_groups[] = { "pcie0_0_waken", "pcie0_0_clkreq",
  795. "pcie0_1_waken", "pcie0_1_clkreq",
  796. "pcie1_0_waken", "pcie1_0_clkreq",
  797. "pcie0_pad_perst",
  798. "pcie1_pad_perst", };
  799. static const char *mt7622_pmic_bus_groups[] = { "pmic_bus", };
  800. static const char *mt7622_pwm_groups[] = { "pwm_ch1_0", "pwm_ch1_1",
  801. "pwm_ch1_2", "pwm_ch2_0",
  802. "pwm_ch2_1", "pwm_ch2_2",
  803. "pwm_ch3_0", "pwm_ch3_1",
  804. "pwm_ch3_2", "pwm_ch4_0",
  805. "pwm_ch4_1", "pwm_ch4_2",
  806. "pwm_ch4_3", "pwm_ch5_0",
  807. "pwm_ch5_1", "pwm_ch5_2",
  808. "pwm_ch6_0", "pwm_ch6_1",
  809. "pwm_ch6_2", "pwm_ch6_3",
  810. "pwm_ch7_0", "pwm_ch7_1",
  811. "pwm_ch7_2", };
  812. static const char *mt7622_sd_groups[] = { "sd_0", "sd_1", };
  813. static const char *mt7622_spic_groups[] = { "spic0_0", "spic0_1", "spic1_0",
  814. "spic1_1", "spic2_0",
  815. "spic2_0_wp_hold", };
  816. static const char *mt7622_tdm_groups[] = { "tdm_0_out_mclk_bclk_ws",
  817. "tdm_0_in_mclk_bclk_ws",
  818. "tdm_0_out_data",
  819. "tdm_0_in_data",
  820. "tdm_1_out_mclk_bclk_ws",
  821. "tdm_1_in_mclk_bclk_ws",
  822. "tdm_1_out_data",
  823. "tdm_1_in_data", };
  824. static const char *mt7622_uart_groups[] = { "uart0_0_tx_rx",
  825. "uart1_0_tx_rx", "uart1_0_rts_cts",
  826. "uart1_1_tx_rx", "uart1_1_rts_cts",
  827. "uart2_0_tx_rx", "uart2_0_rts_cts",
  828. "uart2_1_tx_rx", "uart2_1_rts_cts",
  829. "uart2_2_tx_rx", "uart2_2_rts_cts",
  830. "uart2_3_tx_rx",
  831. "uart3_0_tx_rx",
  832. "uart3_1_tx_rx", "uart3_1_rts_cts",
  833. "uart4_0_tx_rx",
  834. "uart4_1_tx_rx", "uart4_1_rts_cts",
  835. "uart4_2_tx_rx",
  836. "uart4_2_rts_cts",};
  837. static const char *mt7622_wdt_groups[] = { "watchdog", };
  838. static const struct function_desc mt7622_functions[] = {
  839. {"emmc", mt7622_emmc_groups, ARRAY_SIZE(mt7622_emmc_groups)},
  840. {"eth", mt7622_ethernet_groups, ARRAY_SIZE(mt7622_ethernet_groups)},
  841. {"i2c", mt7622_i2c_groups, ARRAY_SIZE(mt7622_i2c_groups)},
  842. {"i2s", mt7622_i2s_groups, ARRAY_SIZE(mt7622_i2s_groups)},
  843. {"ir", mt7622_ir_groups, ARRAY_SIZE(mt7622_ir_groups)},
  844. {"led", mt7622_led_groups, ARRAY_SIZE(mt7622_led_groups)},
  845. {"flash", mt7622_flash_groups, ARRAY_SIZE(mt7622_flash_groups)},
  846. {"pcie", mt7622_pcie_groups, ARRAY_SIZE(mt7622_pcie_groups)},
  847. {"pmic", mt7622_pmic_bus_groups, ARRAY_SIZE(mt7622_pmic_bus_groups)},
  848. {"pwm", mt7622_pwm_groups, ARRAY_SIZE(mt7622_pwm_groups)},
  849. {"sd", mt7622_sd_groups, ARRAY_SIZE(mt7622_sd_groups)},
  850. {"spi", mt7622_spic_groups, ARRAY_SIZE(mt7622_spic_groups)},
  851. {"tdm", mt7622_tdm_groups, ARRAY_SIZE(mt7622_tdm_groups)},
  852. {"uart", mt7622_uart_groups, ARRAY_SIZE(mt7622_uart_groups)},
  853. {"watchdog", mt7622_wdt_groups, ARRAY_SIZE(mt7622_wdt_groups)},
  854. };
  855. static const struct pinconf_generic_params mtk_custom_bindings[] = {
  856. {"mediatek,tdsel", MTK_PIN_CONFIG_TDSEL, 0},
  857. {"mediatek,rdsel", MTK_PIN_CONFIG_RDSEL, 0},
  858. };
  859. #ifdef CONFIG_DEBUG_FS
  860. static const struct pin_config_item mtk_conf_items[] = {
  861. PCONFDUMP(MTK_PIN_CONFIG_TDSEL, "tdsel", NULL, true),
  862. PCONFDUMP(MTK_PIN_CONFIG_RDSEL, "rdsel", NULL, true),
  863. };
  864. #endif
  865. static const struct mtk_eint_hw mt7622_eint_hw = {
  866. .port_mask = 7,
  867. .ports = 7,
  868. .ap_num = ARRAY_SIZE(mt7622_pins),
  869. .db_cnt = 20,
  870. };
  871. static const struct mtk_pin_soc mt7622_data = {
  872. .reg_cal = mt7622_reg_cals,
  873. .pins = mt7622_pins,
  874. .npins = ARRAY_SIZE(mt7622_pins),
  875. .grps = mt7622_groups,
  876. .ngrps = ARRAY_SIZE(mt7622_groups),
  877. .funcs = mt7622_functions,
  878. .nfuncs = ARRAY_SIZE(mt7622_functions),
  879. .eint_hw = &mt7622_eint_hw,
  880. };
  881. static void mtk_w32(struct mtk_pinctrl *pctl, u32 reg, u32 val)
  882. {
  883. writel_relaxed(val, pctl->base + reg);
  884. }
  885. static u32 mtk_r32(struct mtk_pinctrl *pctl, u32 reg)
  886. {
  887. return readl_relaxed(pctl->base + reg);
  888. }
  889. static void mtk_rmw(struct mtk_pinctrl *pctl, u32 reg, u32 mask, u32 set)
  890. {
  891. u32 val;
  892. val = mtk_r32(pctl, reg);
  893. val &= ~mask;
  894. val |= set;
  895. mtk_w32(pctl, reg, val);
  896. }
  897. static int mtk_hw_pin_field_lookup(struct mtk_pinctrl *hw, int pin,
  898. const struct mtk_pin_reg_calc *rc,
  899. struct mtk_pin_field *pfd)
  900. {
  901. const struct mtk_pin_field_calc *c, *e;
  902. u32 bits;
  903. c = rc->range;
  904. e = c + rc->nranges;
  905. while (c < e) {
  906. if (pin >= c->s_pin && pin <= c->e_pin)
  907. break;
  908. c++;
  909. }
  910. if (c >= e) {
  911. dev_err(hw->dev, "Out of range for pin = %d\n", pin);
  912. return -EINVAL;
  913. }
  914. /* Caculated bits as the overall offset the pin is located at */
  915. bits = c->s_bit + (pin - c->s_pin) * (c->x_bits);
  916. /* Fill pfd from bits and 32-bit register applied is assumed */
  917. pfd->offset = c->s_addr + c->x_addrs * (bits / 32);
  918. pfd->bitpos = bits % 32;
  919. pfd->mask = (1 << c->x_bits) - 1;
  920. /* pfd->next is used for indicating that bit wrapping-around happens
  921. * which requires the manipulation for bit 0 starting in the next
  922. * register to form the complete field read/write.
  923. */
  924. pfd->next = pfd->bitpos + c->x_bits - 1 > 31 ? c->x_addrs : 0;
  925. return 0;
  926. }
  927. static int mtk_hw_pin_field_get(struct mtk_pinctrl *hw, int pin,
  928. int field, struct mtk_pin_field *pfd)
  929. {
  930. const struct mtk_pin_reg_calc *rc;
  931. if (field < 0 || field >= PINCTRL_PIN_REG_MAX) {
  932. dev_err(hw->dev, "Invalid Field %d\n", field);
  933. return -EINVAL;
  934. }
  935. if (hw->soc->reg_cal && hw->soc->reg_cal[field].range) {
  936. rc = &hw->soc->reg_cal[field];
  937. } else {
  938. dev_err(hw->dev, "Undefined range for field %d\n", field);
  939. return -EINVAL;
  940. }
  941. return mtk_hw_pin_field_lookup(hw, pin, rc, pfd);
  942. }
  943. static void mtk_hw_bits_part(struct mtk_pin_field *pf, int *h, int *l)
  944. {
  945. *l = 32 - pf->bitpos;
  946. *h = get_count_order(pf->mask) - *l;
  947. }
  948. static void mtk_hw_write_cross_field(struct mtk_pinctrl *hw,
  949. struct mtk_pin_field *pf, int value)
  950. {
  951. int nbits_l, nbits_h;
  952. mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
  953. mtk_rmw(hw, pf->offset, pf->mask << pf->bitpos,
  954. (value & pf->mask) << pf->bitpos);
  955. mtk_rmw(hw, pf->offset + pf->next, BIT(nbits_h) - 1,
  956. (value & pf->mask) >> nbits_l);
  957. }
  958. static void mtk_hw_read_cross_field(struct mtk_pinctrl *hw,
  959. struct mtk_pin_field *pf, int *value)
  960. {
  961. int nbits_l, nbits_h, h, l;
  962. mtk_hw_bits_part(pf, &nbits_h, &nbits_l);
  963. l = (mtk_r32(hw, pf->offset) >> pf->bitpos) & (BIT(nbits_l) - 1);
  964. h = (mtk_r32(hw, pf->offset + pf->next)) & (BIT(nbits_h) - 1);
  965. *value = (h << nbits_l) | l;
  966. }
  967. static int mtk_hw_set_value(struct mtk_pinctrl *hw, int pin, int field,
  968. int value)
  969. {
  970. struct mtk_pin_field pf;
  971. int err;
  972. err = mtk_hw_pin_field_get(hw, pin, field, &pf);
  973. if (err)
  974. return err;
  975. if (!pf.next)
  976. mtk_rmw(hw, pf.offset, pf.mask << pf.bitpos,
  977. (value & pf.mask) << pf.bitpos);
  978. else
  979. mtk_hw_write_cross_field(hw, &pf, value);
  980. return 0;
  981. }
  982. static int mtk_hw_get_value(struct mtk_pinctrl *hw, int pin, int field,
  983. int *value)
  984. {
  985. struct mtk_pin_field pf;
  986. int err;
  987. err = mtk_hw_pin_field_get(hw, pin, field, &pf);
  988. if (err)
  989. return err;
  990. if (!pf.next)
  991. *value = (mtk_r32(hw, pf.offset) >> pf.bitpos) & pf.mask;
  992. else
  993. mtk_hw_read_cross_field(hw, &pf, value);
  994. return 0;
  995. }
  996. static int mtk_pinmux_set_mux(struct pinctrl_dev *pctldev,
  997. unsigned int selector, unsigned int group)
  998. {
  999. struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
  1000. struct function_desc *func;
  1001. struct group_desc *grp;
  1002. int i;
  1003. func = pinmux_generic_get_function(pctldev, selector);
  1004. if (!func)
  1005. return -EINVAL;
  1006. grp = pinctrl_generic_get_group(pctldev, group);
  1007. if (!grp)
  1008. return -EINVAL;
  1009. dev_dbg(pctldev->dev, "enable function %s group %s\n",
  1010. func->name, grp->name);
  1011. for (i = 0; i < grp->num_pins; i++) {
  1012. int *pin_modes = grp->data;
  1013. mtk_hw_set_value(hw, grp->pins[i], PINCTRL_PIN_REG_MODE,
  1014. pin_modes[i]);
  1015. }
  1016. return 0;
  1017. }
  1018. static int mtk_pinmux_gpio_request_enable(struct pinctrl_dev *pctldev,
  1019. struct pinctrl_gpio_range *range,
  1020. unsigned int pin)
  1021. {
  1022. struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
  1023. return mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_MODE, MTK_GPIO_MODE);
  1024. }
  1025. static int mtk_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
  1026. struct pinctrl_gpio_range *range,
  1027. unsigned int pin, bool input)
  1028. {
  1029. struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
  1030. /* hardware would take 0 as input direction */
  1031. return mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_DIR, !input);
  1032. }
  1033. static int mtk_pinconf_get(struct pinctrl_dev *pctldev,
  1034. unsigned int pin, unsigned long *config)
  1035. {
  1036. struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
  1037. u32 param = pinconf_to_config_param(*config);
  1038. int val, val2, err, reg, ret = 1;
  1039. switch (param) {
  1040. case PIN_CONFIG_BIAS_DISABLE:
  1041. err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_PU, &val);
  1042. if (err)
  1043. return err;
  1044. err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_PD, &val2);
  1045. if (err)
  1046. return err;
  1047. if (val || val2)
  1048. return -EINVAL;
  1049. break;
  1050. case PIN_CONFIG_BIAS_PULL_UP:
  1051. case PIN_CONFIG_BIAS_PULL_DOWN:
  1052. case PIN_CONFIG_SLEW_RATE:
  1053. reg = (param == PIN_CONFIG_BIAS_PULL_UP) ?
  1054. PINCTRL_PIN_REG_PU :
  1055. (param == PIN_CONFIG_BIAS_PULL_DOWN) ?
  1056. PINCTRL_PIN_REG_PD : PINCTRL_PIN_REG_SR;
  1057. err = mtk_hw_get_value(hw, pin, reg, &val);
  1058. if (err)
  1059. return err;
  1060. if (!val)
  1061. return -EINVAL;
  1062. break;
  1063. case PIN_CONFIG_INPUT_ENABLE:
  1064. case PIN_CONFIG_OUTPUT_ENABLE:
  1065. err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_DIR, &val);
  1066. if (err)
  1067. return err;
  1068. /* HW takes input mode as zero; output mode as non-zero */
  1069. if ((val && param == PIN_CONFIG_INPUT_ENABLE) ||
  1070. (!val && param == PIN_CONFIG_OUTPUT_ENABLE))
  1071. return -EINVAL;
  1072. break;
  1073. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  1074. err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_DIR, &val);
  1075. if (err)
  1076. return err;
  1077. err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_SMT, &val2);
  1078. if (err)
  1079. return err;
  1080. if (val || !val2)
  1081. return -EINVAL;
  1082. break;
  1083. case PIN_CONFIG_DRIVE_STRENGTH:
  1084. err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_E4, &val);
  1085. if (err)
  1086. return err;
  1087. err = mtk_hw_get_value(hw, pin, PINCTRL_PIN_REG_E8, &val2);
  1088. if (err)
  1089. return err;
  1090. /* 4mA when (e8, e4) = (0, 0); 8mA when (e8, e4) = (0, 1)
  1091. * 12mA when (e8, e4) = (1, 0); 16mA when (e8, e4) = (1, 1)
  1092. */
  1093. ret = ((val2 << 1) + val + 1) * 4;
  1094. break;
  1095. case MTK_PIN_CONFIG_TDSEL:
  1096. case MTK_PIN_CONFIG_RDSEL:
  1097. reg = (param == MTK_PIN_CONFIG_TDSEL) ?
  1098. PINCTRL_PIN_REG_TDSEL : PINCTRL_PIN_REG_RDSEL;
  1099. err = mtk_hw_get_value(hw, pin, reg, &val);
  1100. if (err)
  1101. return err;
  1102. ret = val;
  1103. break;
  1104. default:
  1105. return -ENOTSUPP;
  1106. }
  1107. *config = pinconf_to_config_packed(param, ret);
  1108. return 0;
  1109. }
  1110. static int mtk_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  1111. unsigned long *configs, unsigned int num_configs)
  1112. {
  1113. struct mtk_pinctrl *hw = pinctrl_dev_get_drvdata(pctldev);
  1114. u32 reg, param, arg;
  1115. int cfg, err = 0;
  1116. for (cfg = 0; cfg < num_configs; cfg++) {
  1117. param = pinconf_to_config_param(configs[cfg]);
  1118. arg = pinconf_to_config_argument(configs[cfg]);
  1119. switch (param) {
  1120. case PIN_CONFIG_BIAS_DISABLE:
  1121. case PIN_CONFIG_BIAS_PULL_UP:
  1122. case PIN_CONFIG_BIAS_PULL_DOWN:
  1123. arg = (param == PIN_CONFIG_BIAS_DISABLE) ? 0 :
  1124. (param == PIN_CONFIG_BIAS_PULL_UP) ? 1 : 2;
  1125. err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_PU,
  1126. arg & 1);
  1127. if (err)
  1128. goto err;
  1129. err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_PD,
  1130. !!(arg & 2));
  1131. if (err)
  1132. goto err;
  1133. break;
  1134. case PIN_CONFIG_OUTPUT_ENABLE:
  1135. err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_SMT,
  1136. MTK_DISABLE);
  1137. if (err)
  1138. goto err;
  1139. /* else: fall through */
  1140. case PIN_CONFIG_INPUT_ENABLE:
  1141. case PIN_CONFIG_SLEW_RATE:
  1142. reg = (param == PIN_CONFIG_SLEW_RATE) ?
  1143. PINCTRL_PIN_REG_SR : PINCTRL_PIN_REG_DIR;
  1144. arg = (param == PIN_CONFIG_INPUT_ENABLE) ? 0 :
  1145. (param == PIN_CONFIG_OUTPUT_ENABLE) ? 1 : arg;
  1146. err = mtk_hw_set_value(hw, pin, reg, arg);
  1147. if (err)
  1148. goto err;
  1149. break;
  1150. case PIN_CONFIG_OUTPUT:
  1151. err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_DIR,
  1152. MTK_OUTPUT);
  1153. if (err)
  1154. goto err;
  1155. err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_DO,
  1156. arg);
  1157. if (err)
  1158. goto err;
  1159. break;
  1160. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  1161. /* arg = 1: Input mode & SMT enable ;
  1162. * arg = 0: Output mode & SMT disable
  1163. */
  1164. arg = arg ? 2 : 1;
  1165. err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_DIR,
  1166. arg & 1);
  1167. if (err)
  1168. goto err;
  1169. err = mtk_hw_set_value(hw, pin, PINCTRL_PIN_REG_SMT,
  1170. !!(arg & 2));
  1171. if (err)
  1172. goto err;
  1173. break;
  1174. case PIN_CONFIG_DRIVE_STRENGTH:
  1175. /* 4mA when (e8, e4) = (0, 0);
  1176. * 8mA when (e8, e4) = (0, 1);
  1177. * 12mA when (e8, e4) = (1, 0);
  1178. * 16mA when (e8, e4) = (1, 1)
  1179. */
  1180. if (!(arg % 4) && (arg >= 4 && arg <= 16)) {
  1181. arg = arg / 4 - 1;
  1182. err = mtk_hw_set_value(hw, pin,
  1183. PINCTRL_PIN_REG_E4,
  1184. arg & 0x1);
  1185. if (err)
  1186. goto err;
  1187. err = mtk_hw_set_value(hw, pin,
  1188. PINCTRL_PIN_REG_E8,
  1189. (arg & 0x2) >> 1);
  1190. if (err)
  1191. goto err;
  1192. } else {
  1193. err = -ENOTSUPP;
  1194. }
  1195. break;
  1196. case MTK_PIN_CONFIG_TDSEL:
  1197. case MTK_PIN_CONFIG_RDSEL:
  1198. reg = (param == MTK_PIN_CONFIG_TDSEL) ?
  1199. PINCTRL_PIN_REG_TDSEL : PINCTRL_PIN_REG_RDSEL;
  1200. err = mtk_hw_set_value(hw, pin, reg, arg);
  1201. if (err)
  1202. goto err;
  1203. break;
  1204. default:
  1205. err = -ENOTSUPP;
  1206. }
  1207. }
  1208. err:
  1209. return err;
  1210. }
  1211. static int mtk_pinconf_group_get(struct pinctrl_dev *pctldev,
  1212. unsigned int group, unsigned long *config)
  1213. {
  1214. const unsigned int *pins;
  1215. unsigned int i, npins, old = 0;
  1216. int ret;
  1217. ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
  1218. if (ret)
  1219. return ret;
  1220. for (i = 0; i < npins; i++) {
  1221. if (mtk_pinconf_get(pctldev, pins[i], config))
  1222. return -ENOTSUPP;
  1223. /* configs do not match between two pins */
  1224. if (i && old != *config)
  1225. return -ENOTSUPP;
  1226. old = *config;
  1227. }
  1228. return 0;
  1229. }
  1230. static int mtk_pinconf_group_set(struct pinctrl_dev *pctldev,
  1231. unsigned int group, unsigned long *configs,
  1232. unsigned int num_configs)
  1233. {
  1234. const unsigned int *pins;
  1235. unsigned int i, npins;
  1236. int ret;
  1237. ret = pinctrl_generic_get_group_pins(pctldev, group, &pins, &npins);
  1238. if (ret)
  1239. return ret;
  1240. for (i = 0; i < npins; i++) {
  1241. ret = mtk_pinconf_set(pctldev, pins[i], configs, num_configs);
  1242. if (ret)
  1243. return ret;
  1244. }
  1245. return 0;
  1246. }
  1247. static const struct pinctrl_ops mtk_pctlops = {
  1248. .get_groups_count = pinctrl_generic_get_group_count,
  1249. .get_group_name = pinctrl_generic_get_group_name,
  1250. .get_group_pins = pinctrl_generic_get_group_pins,
  1251. .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
  1252. .dt_free_map = pinconf_generic_dt_free_map,
  1253. };
  1254. static const struct pinmux_ops mtk_pmxops = {
  1255. .get_functions_count = pinmux_generic_get_function_count,
  1256. .get_function_name = pinmux_generic_get_function_name,
  1257. .get_function_groups = pinmux_generic_get_function_groups,
  1258. .set_mux = mtk_pinmux_set_mux,
  1259. .gpio_request_enable = mtk_pinmux_gpio_request_enable,
  1260. .gpio_set_direction = mtk_pinmux_gpio_set_direction,
  1261. .strict = true,
  1262. };
  1263. static const struct pinconf_ops mtk_confops = {
  1264. .is_generic = true,
  1265. .pin_config_get = mtk_pinconf_get,
  1266. .pin_config_set = mtk_pinconf_set,
  1267. .pin_config_group_get = mtk_pinconf_group_get,
  1268. .pin_config_group_set = mtk_pinconf_group_set,
  1269. .pin_config_config_dbg_show = pinconf_generic_dump_config,
  1270. };
  1271. static struct pinctrl_desc mtk_desc = {
  1272. .name = PINCTRL_PINCTRL_DEV,
  1273. .pctlops = &mtk_pctlops,
  1274. .pmxops = &mtk_pmxops,
  1275. .confops = &mtk_confops,
  1276. .owner = THIS_MODULE,
  1277. };
  1278. static int mtk_gpio_get(struct gpio_chip *chip, unsigned int gpio)
  1279. {
  1280. struct mtk_pinctrl *hw = gpiochip_get_data(chip);
  1281. int value, err;
  1282. err = mtk_hw_get_value(hw, gpio, PINCTRL_PIN_REG_DI, &value);
  1283. if (err)
  1284. return err;
  1285. return !!value;
  1286. }
  1287. static void mtk_gpio_set(struct gpio_chip *chip, unsigned int gpio, int value)
  1288. {
  1289. struct mtk_pinctrl *hw = gpiochip_get_data(chip);
  1290. mtk_hw_set_value(hw, gpio, PINCTRL_PIN_REG_DO, !!value);
  1291. }
  1292. static int mtk_gpio_direction_input(struct gpio_chip *chip, unsigned int gpio)
  1293. {
  1294. return pinctrl_gpio_direction_input(chip->base + gpio);
  1295. }
  1296. static int mtk_gpio_direction_output(struct gpio_chip *chip, unsigned int gpio,
  1297. int value)
  1298. {
  1299. mtk_gpio_set(chip, gpio, value);
  1300. return pinctrl_gpio_direction_output(chip->base + gpio);
  1301. }
  1302. static int mtk_gpio_to_irq(struct gpio_chip *chip, unsigned int offset)
  1303. {
  1304. struct mtk_pinctrl *hw = gpiochip_get_data(chip);
  1305. unsigned long eint_n;
  1306. if (!hw->eint)
  1307. return -ENOTSUPP;
  1308. eint_n = offset;
  1309. return mtk_eint_find_irq(hw->eint, eint_n);
  1310. }
  1311. static int mtk_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
  1312. unsigned long config)
  1313. {
  1314. struct mtk_pinctrl *hw = gpiochip_get_data(chip);
  1315. unsigned long eint_n;
  1316. u32 debounce;
  1317. if (!hw->eint ||
  1318. pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
  1319. return -ENOTSUPP;
  1320. debounce = pinconf_to_config_argument(config);
  1321. eint_n = offset;
  1322. return mtk_eint_set_debounce(hw->eint, eint_n, debounce);
  1323. }
  1324. static int mtk_build_gpiochip(struct mtk_pinctrl *hw, struct device_node *np)
  1325. {
  1326. struct gpio_chip *chip = &hw->chip;
  1327. int ret;
  1328. chip->label = PINCTRL_PINCTRL_DEV;
  1329. chip->parent = hw->dev;
  1330. chip->request = gpiochip_generic_request;
  1331. chip->free = gpiochip_generic_free;
  1332. chip->direction_input = mtk_gpio_direction_input;
  1333. chip->direction_output = mtk_gpio_direction_output;
  1334. chip->get = mtk_gpio_get;
  1335. chip->set = mtk_gpio_set;
  1336. chip->to_irq = mtk_gpio_to_irq,
  1337. chip->set_config = mtk_gpio_set_config,
  1338. chip->base = -1;
  1339. chip->ngpio = hw->soc->npins;
  1340. chip->of_node = np;
  1341. chip->of_gpio_n_cells = 2;
  1342. ret = gpiochip_add_data(chip, hw);
  1343. if (ret < 0)
  1344. return ret;
  1345. /* Just for backward compatible for these old pinctrl nodes without
  1346. * "gpio-ranges" property. Otherwise, called directly from a
  1347. * DeviceTree-supported pinctrl driver is DEPRECATED.
  1348. * Please see Section 2.1 of
  1349. * Documentation/devicetree/bindings/gpio/gpio.txt on how to
  1350. * bind pinctrl and gpio drivers via the "gpio-ranges" property.
  1351. */
  1352. if (!of_find_property(np, "gpio-ranges", NULL)) {
  1353. ret = gpiochip_add_pin_range(chip, dev_name(hw->dev), 0, 0,
  1354. chip->ngpio);
  1355. if (ret < 0) {
  1356. gpiochip_remove(chip);
  1357. return ret;
  1358. }
  1359. }
  1360. return 0;
  1361. }
  1362. static int mtk_build_groups(struct mtk_pinctrl *hw)
  1363. {
  1364. int err, i;
  1365. for (i = 0; i < hw->soc->ngrps; i++) {
  1366. const struct group_desc *group = hw->soc->grps + i;
  1367. err = pinctrl_generic_add_group(hw->pctrl, group->name,
  1368. group->pins, group->num_pins,
  1369. group->data);
  1370. if (err < 0) {
  1371. dev_err(hw->dev, "Failed to register group %s\n",
  1372. group->name);
  1373. return err;
  1374. }
  1375. }
  1376. return 0;
  1377. }
  1378. static int mtk_build_functions(struct mtk_pinctrl *hw)
  1379. {
  1380. int i, err;
  1381. for (i = 0; i < hw->soc->nfuncs ; i++) {
  1382. const struct function_desc *func = hw->soc->funcs + i;
  1383. err = pinmux_generic_add_function(hw->pctrl, func->name,
  1384. func->group_names,
  1385. func->num_group_names,
  1386. func->data);
  1387. if (err < 0) {
  1388. dev_err(hw->dev, "Failed to register function %s\n",
  1389. func->name);
  1390. return err;
  1391. }
  1392. }
  1393. return 0;
  1394. }
  1395. static int mtk_xt_get_gpio_n(void *data, unsigned long eint_n,
  1396. unsigned int *gpio_n,
  1397. struct gpio_chip **gpio_chip)
  1398. {
  1399. struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
  1400. *gpio_chip = &hw->chip;
  1401. *gpio_n = eint_n;
  1402. return 0;
  1403. }
  1404. static int mtk_xt_get_gpio_state(void *data, unsigned long eint_n)
  1405. {
  1406. struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
  1407. struct gpio_chip *gpio_chip;
  1408. unsigned int gpio_n;
  1409. int err;
  1410. err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip);
  1411. if (err)
  1412. return err;
  1413. return mtk_gpio_get(gpio_chip, gpio_n);
  1414. }
  1415. static int mtk_xt_set_gpio_as_eint(void *data, unsigned long eint_n)
  1416. {
  1417. struct mtk_pinctrl *hw = (struct mtk_pinctrl *)data;
  1418. struct gpio_chip *gpio_chip;
  1419. unsigned int gpio_n;
  1420. int err;
  1421. err = mtk_xt_get_gpio_n(hw, eint_n, &gpio_n, &gpio_chip);
  1422. if (err)
  1423. return err;
  1424. err = mtk_hw_set_value(hw, gpio_n, PINCTRL_PIN_REG_MODE,
  1425. MTK_GPIO_MODE);
  1426. if (err)
  1427. return err;
  1428. err = mtk_hw_set_value(hw, gpio_n, PINCTRL_PIN_REG_DIR, MTK_INPUT);
  1429. if (err)
  1430. return err;
  1431. err = mtk_hw_set_value(hw, gpio_n, PINCTRL_PIN_REG_SMT, MTK_ENABLE);
  1432. if (err)
  1433. return err;
  1434. return 0;
  1435. }
  1436. static const struct mtk_eint_xt mtk_eint_xt = {
  1437. .get_gpio_n = mtk_xt_get_gpio_n,
  1438. .get_gpio_state = mtk_xt_get_gpio_state,
  1439. .set_gpio_as_eint = mtk_xt_set_gpio_as_eint,
  1440. };
  1441. static int
  1442. mtk_build_eint(struct mtk_pinctrl *hw, struct platform_device *pdev)
  1443. {
  1444. struct device_node *np = pdev->dev.of_node;
  1445. struct resource *res;
  1446. if (!IS_ENABLED(CONFIG_EINT_MTK))
  1447. return 0;
  1448. if (!of_property_read_bool(np, "interrupt-controller"))
  1449. return -ENODEV;
  1450. hw->eint = devm_kzalloc(hw->dev, sizeof(*hw->eint), GFP_KERNEL);
  1451. if (!hw->eint)
  1452. return -ENOMEM;
  1453. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "eint");
  1454. if (!res) {
  1455. dev_err(&pdev->dev, "Unable to get eint resource\n");
  1456. return -ENODEV;
  1457. }
  1458. hw->eint->base = devm_ioremap_resource(&pdev->dev, res);
  1459. if (IS_ERR(hw->eint->base))
  1460. return PTR_ERR(hw->eint->base);
  1461. hw->eint->irq = irq_of_parse_and_map(np, 0);
  1462. if (!hw->eint->irq)
  1463. return -EINVAL;
  1464. hw->eint->dev = &pdev->dev;
  1465. hw->eint->hw = hw->soc->eint_hw;
  1466. hw->eint->pctl = hw;
  1467. hw->eint->gpio_xlate = &mtk_eint_xt;
  1468. return mtk_eint_do_init(hw->eint);
  1469. }
  1470. static const struct of_device_id mtk_pinctrl_of_match[] = {
  1471. { .compatible = "mediatek,mt7622-pinctrl", .data = &mt7622_data},
  1472. { }
  1473. };
  1474. static int mtk_pinctrl_probe(struct platform_device *pdev)
  1475. {
  1476. struct resource *res;
  1477. struct mtk_pinctrl *hw;
  1478. const struct of_device_id *of_id =
  1479. of_match_device(mtk_pinctrl_of_match, &pdev->dev);
  1480. int err;
  1481. hw = devm_kzalloc(&pdev->dev, sizeof(*hw), GFP_KERNEL);
  1482. if (!hw)
  1483. return -ENOMEM;
  1484. hw->soc = of_id->data;
  1485. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1486. if (!res) {
  1487. dev_err(&pdev->dev, "missing IO resource\n");
  1488. return -ENXIO;
  1489. }
  1490. hw->dev = &pdev->dev;
  1491. hw->base = devm_ioremap_resource(&pdev->dev, res);
  1492. if (IS_ERR(hw->base))
  1493. return PTR_ERR(hw->base);
  1494. /* Setup pins descriptions per SoC types */
  1495. mtk_desc.pins = hw->soc->pins;
  1496. mtk_desc.npins = hw->soc->npins;
  1497. mtk_desc.num_custom_params = ARRAY_SIZE(mtk_custom_bindings);
  1498. mtk_desc.custom_params = mtk_custom_bindings;
  1499. #ifdef CONFIG_DEBUG_FS
  1500. mtk_desc.custom_conf_items = mtk_conf_items;
  1501. #endif
  1502. err = devm_pinctrl_register_and_init(&pdev->dev, &mtk_desc, hw,
  1503. &hw->pctrl);
  1504. if (err)
  1505. return err;
  1506. /* Setup groups descriptions per SoC types */
  1507. err = mtk_build_groups(hw);
  1508. if (err) {
  1509. dev_err(&pdev->dev, "Failed to build groups\n");
  1510. return err;
  1511. }
  1512. /* Setup functions descriptions per SoC types */
  1513. err = mtk_build_functions(hw);
  1514. if (err) {
  1515. dev_err(&pdev->dev, "Failed to build functions\n");
  1516. return err;
  1517. }
  1518. /* For able to make pinctrl_claim_hogs, we must not enable pinctrl
  1519. * until all groups and functions are being added one.
  1520. */
  1521. err = pinctrl_enable(hw->pctrl);
  1522. if (err)
  1523. return err;
  1524. err = mtk_build_eint(hw, pdev);
  1525. if (err)
  1526. dev_warn(&pdev->dev,
  1527. "Failed to add EINT, but pinctrl still can work\n");
  1528. /* Build gpiochip should be after pinctrl_enable is done */
  1529. err = mtk_build_gpiochip(hw, pdev->dev.of_node);
  1530. if (err) {
  1531. dev_err(&pdev->dev, "Failed to add gpio_chip\n");
  1532. return err;
  1533. }
  1534. platform_set_drvdata(pdev, hw);
  1535. return 0;
  1536. }
  1537. static struct platform_driver mtk_pinctrl_driver = {
  1538. .driver = {
  1539. .name = "mtk-pinctrl",
  1540. .of_match_table = mtk_pinctrl_of_match,
  1541. },
  1542. .probe = mtk_pinctrl_probe,
  1543. };
  1544. static int __init mtk_pinctrl_init(void)
  1545. {
  1546. return platform_driver_register(&mtk_pinctrl_driver);
  1547. }
  1548. arch_initcall(mtk_pinctrl_init);