pinctrl-rockchip.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845
  1. /*
  2. * Pinctrl driver for Rockchip SoCs
  3. *
  4. * Copyright (c) 2013 MundoReader S.L.
  5. * Author: Heiko Stuebner <heiko@sntech.de>
  6. *
  7. * With some ideas taken from pinctrl-samsung:
  8. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  9. * http://www.samsung.com
  10. * Copyright (c) 2012 Linaro Ltd
  11. * http://www.linaro.org
  12. *
  13. * and pinctrl-at91:
  14. * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2 as published
  18. * by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. */
  25. #include <linux/init.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/io.h>
  28. #include <linux/bitops.h>
  29. #include <linux/gpio.h>
  30. #include <linux/of_address.h>
  31. #include <linux/of_irq.h>
  32. #include <linux/pinctrl/machine.h>
  33. #include <linux/pinctrl/pinconf.h>
  34. #include <linux/pinctrl/pinctrl.h>
  35. #include <linux/pinctrl/pinmux.h>
  36. #include <linux/pinctrl/pinconf-generic.h>
  37. #include <linux/irqchip/chained_irq.h>
  38. #include <linux/clk.h>
  39. #include <linux/regmap.h>
  40. #include <linux/mfd/syscon.h>
  41. #include <dt-bindings/pinctrl/rockchip.h>
  42. #include "core.h"
  43. #include "pinconf.h"
  44. /* GPIO control registers */
  45. #define GPIO_SWPORT_DR 0x00
  46. #define GPIO_SWPORT_DDR 0x04
  47. #define GPIO_INTEN 0x30
  48. #define GPIO_INTMASK 0x34
  49. #define GPIO_INTTYPE_LEVEL 0x38
  50. #define GPIO_INT_POLARITY 0x3c
  51. #define GPIO_INT_STATUS 0x40
  52. #define GPIO_INT_RAWSTATUS 0x44
  53. #define GPIO_DEBOUNCE 0x48
  54. #define GPIO_PORTS_EOI 0x4c
  55. #define GPIO_EXT_PORT 0x50
  56. #define GPIO_LS_SYNC 0x60
  57. enum rockchip_pinctrl_type {
  58. PX30,
  59. RV1108,
  60. RK2928,
  61. RK3066B,
  62. RK3128,
  63. RK3188,
  64. RK3288,
  65. RK3368,
  66. RK3399,
  67. };
  68. /**
  69. * Encode variants of iomux registers into a type variable
  70. */
  71. #define IOMUX_GPIO_ONLY BIT(0)
  72. #define IOMUX_WIDTH_4BIT BIT(1)
  73. #define IOMUX_SOURCE_PMU BIT(2)
  74. #define IOMUX_UNROUTED BIT(3)
  75. #define IOMUX_WIDTH_3BIT BIT(4)
  76. /**
  77. * @type: iomux variant using IOMUX_* constants
  78. * @offset: if initialized to -1 it will be autocalculated, by specifying
  79. * an initial offset value the relevant source offset can be reset
  80. * to a new value for autocalculating the following iomux registers.
  81. */
  82. struct rockchip_iomux {
  83. int type;
  84. int offset;
  85. };
  86. /**
  87. * enum type index corresponding to rockchip_perpin_drv_list arrays index.
  88. */
  89. enum rockchip_pin_drv_type {
  90. DRV_TYPE_IO_DEFAULT = 0,
  91. DRV_TYPE_IO_1V8_OR_3V0,
  92. DRV_TYPE_IO_1V8_ONLY,
  93. DRV_TYPE_IO_1V8_3V0_AUTO,
  94. DRV_TYPE_IO_3V3_ONLY,
  95. DRV_TYPE_MAX
  96. };
  97. /**
  98. * enum type index corresponding to rockchip_pull_list arrays index.
  99. */
  100. enum rockchip_pin_pull_type {
  101. PULL_TYPE_IO_DEFAULT = 0,
  102. PULL_TYPE_IO_1V8_ONLY,
  103. PULL_TYPE_MAX
  104. };
  105. /**
  106. * @drv_type: drive strength variant using rockchip_perpin_drv_type
  107. * @offset: if initialized to -1 it will be autocalculated, by specifying
  108. * an initial offset value the relevant source offset can be reset
  109. * to a new value for autocalculating the following drive strength
  110. * registers. if used chips own cal_drv func instead to calculate
  111. * registers offset, the variant could be ignored.
  112. */
  113. struct rockchip_drv {
  114. enum rockchip_pin_drv_type drv_type;
  115. int offset;
  116. };
  117. /**
  118. * @reg_base: register base of the gpio bank
  119. * @reg_pull: optional separate register for additional pull settings
  120. * @clk: clock of the gpio bank
  121. * @irq: interrupt of the gpio bank
  122. * @saved_masks: Saved content of GPIO_INTEN at suspend time.
  123. * @pin_base: first pin number
  124. * @nr_pins: number of pins in this bank
  125. * @name: name of the bank
  126. * @bank_num: number of the bank, to account for holes
  127. * @iomux: array describing the 4 iomux sources of the bank
  128. * @drv: array describing the 4 drive strength sources of the bank
  129. * @pull_type: array describing the 4 pull type sources of the bank
  130. * @valid: is all necessary information present
  131. * @of_node: dt node of this bank
  132. * @drvdata: common pinctrl basedata
  133. * @domain: irqdomain of the gpio bank
  134. * @gpio_chip: gpiolib chip
  135. * @grange: gpio range
  136. * @slock: spinlock for the gpio bank
  137. * @route_mask: bits describing the routing pins of per bank
  138. */
  139. struct rockchip_pin_bank {
  140. void __iomem *reg_base;
  141. struct regmap *regmap_pull;
  142. struct clk *clk;
  143. int irq;
  144. u32 saved_masks;
  145. u32 pin_base;
  146. u8 nr_pins;
  147. char *name;
  148. u8 bank_num;
  149. struct rockchip_iomux iomux[4];
  150. struct rockchip_drv drv[4];
  151. enum rockchip_pin_pull_type pull_type[4];
  152. bool valid;
  153. struct device_node *of_node;
  154. struct rockchip_pinctrl *drvdata;
  155. struct irq_domain *domain;
  156. struct gpio_chip gpio_chip;
  157. struct pinctrl_gpio_range grange;
  158. raw_spinlock_t slock;
  159. u32 toggle_edge_mode;
  160. u32 recalced_mask;
  161. u32 route_mask;
  162. };
  163. #define PIN_BANK(id, pins, label) \
  164. { \
  165. .bank_num = id, \
  166. .nr_pins = pins, \
  167. .name = label, \
  168. .iomux = { \
  169. { .offset = -1 }, \
  170. { .offset = -1 }, \
  171. { .offset = -1 }, \
  172. { .offset = -1 }, \
  173. }, \
  174. }
  175. #define PIN_BANK_IOMUX_FLAGS(id, pins, label, iom0, iom1, iom2, iom3) \
  176. { \
  177. .bank_num = id, \
  178. .nr_pins = pins, \
  179. .name = label, \
  180. .iomux = { \
  181. { .type = iom0, .offset = -1 }, \
  182. { .type = iom1, .offset = -1 }, \
  183. { .type = iom2, .offset = -1 }, \
  184. { .type = iom3, .offset = -1 }, \
  185. }, \
  186. }
  187. #define PIN_BANK_DRV_FLAGS(id, pins, label, type0, type1, type2, type3) \
  188. { \
  189. .bank_num = id, \
  190. .nr_pins = pins, \
  191. .name = label, \
  192. .iomux = { \
  193. { .offset = -1 }, \
  194. { .offset = -1 }, \
  195. { .offset = -1 }, \
  196. { .offset = -1 }, \
  197. }, \
  198. .drv = { \
  199. { .drv_type = type0, .offset = -1 }, \
  200. { .drv_type = type1, .offset = -1 }, \
  201. { .drv_type = type2, .offset = -1 }, \
  202. { .drv_type = type3, .offset = -1 }, \
  203. }, \
  204. }
  205. #define PIN_BANK_DRV_FLAGS_PULL_FLAGS(id, pins, label, drv0, drv1, \
  206. drv2, drv3, pull0, pull1, \
  207. pull2, pull3) \
  208. { \
  209. .bank_num = id, \
  210. .nr_pins = pins, \
  211. .name = label, \
  212. .iomux = { \
  213. { .offset = -1 }, \
  214. { .offset = -1 }, \
  215. { .offset = -1 }, \
  216. { .offset = -1 }, \
  217. }, \
  218. .drv = { \
  219. { .drv_type = drv0, .offset = -1 }, \
  220. { .drv_type = drv1, .offset = -1 }, \
  221. { .drv_type = drv2, .offset = -1 }, \
  222. { .drv_type = drv3, .offset = -1 }, \
  223. }, \
  224. .pull_type[0] = pull0, \
  225. .pull_type[1] = pull1, \
  226. .pull_type[2] = pull2, \
  227. .pull_type[3] = pull3, \
  228. }
  229. #define PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(id, pins, label, iom0, iom1, \
  230. iom2, iom3, drv0, drv1, drv2, \
  231. drv3, offset0, offset1, \
  232. offset2, offset3) \
  233. { \
  234. .bank_num = id, \
  235. .nr_pins = pins, \
  236. .name = label, \
  237. .iomux = { \
  238. { .type = iom0, .offset = -1 }, \
  239. { .type = iom1, .offset = -1 }, \
  240. { .type = iom2, .offset = -1 }, \
  241. { .type = iom3, .offset = -1 }, \
  242. }, \
  243. .drv = { \
  244. { .drv_type = drv0, .offset = offset0 }, \
  245. { .drv_type = drv1, .offset = offset1 }, \
  246. { .drv_type = drv2, .offset = offset2 }, \
  247. { .drv_type = drv3, .offset = offset3 }, \
  248. }, \
  249. }
  250. #define PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(id, pins, \
  251. label, iom0, iom1, iom2, \
  252. iom3, drv0, drv1, drv2, \
  253. drv3, offset0, offset1, \
  254. offset2, offset3, pull0, \
  255. pull1, pull2, pull3) \
  256. { \
  257. .bank_num = id, \
  258. .nr_pins = pins, \
  259. .name = label, \
  260. .iomux = { \
  261. { .type = iom0, .offset = -1 }, \
  262. { .type = iom1, .offset = -1 }, \
  263. { .type = iom2, .offset = -1 }, \
  264. { .type = iom3, .offset = -1 }, \
  265. }, \
  266. .drv = { \
  267. { .drv_type = drv0, .offset = offset0 }, \
  268. { .drv_type = drv1, .offset = offset1 }, \
  269. { .drv_type = drv2, .offset = offset2 }, \
  270. { .drv_type = drv3, .offset = offset3 }, \
  271. }, \
  272. .pull_type[0] = pull0, \
  273. .pull_type[1] = pull1, \
  274. .pull_type[2] = pull2, \
  275. .pull_type[3] = pull3, \
  276. }
  277. /**
  278. * struct rockchip_mux_recalced_data: represent a pin iomux data.
  279. * @num: bank number.
  280. * @pin: pin number.
  281. * @bit: index at register.
  282. * @reg: register offset.
  283. * @mask: mask bit
  284. */
  285. struct rockchip_mux_recalced_data {
  286. u8 num;
  287. u8 pin;
  288. u32 reg;
  289. u8 bit;
  290. u8 mask;
  291. };
  292. /**
  293. * struct rockchip_mux_recalced_data: represent a pin iomux data.
  294. * @bank_num: bank number.
  295. * @pin: index at register or used to calc index.
  296. * @func: the min pin.
  297. * @route_offset: the max pin.
  298. * @route_val: the register offset.
  299. */
  300. struct rockchip_mux_route_data {
  301. u8 bank_num;
  302. u8 pin;
  303. u8 func;
  304. u32 route_offset;
  305. u32 route_val;
  306. };
  307. /**
  308. */
  309. struct rockchip_pin_ctrl {
  310. struct rockchip_pin_bank *pin_banks;
  311. u32 nr_banks;
  312. u32 nr_pins;
  313. char *label;
  314. enum rockchip_pinctrl_type type;
  315. int grf_mux_offset;
  316. int pmu_mux_offset;
  317. int grf_drv_offset;
  318. int pmu_drv_offset;
  319. struct rockchip_mux_recalced_data *iomux_recalced;
  320. u32 niomux_recalced;
  321. struct rockchip_mux_route_data *iomux_routes;
  322. u32 niomux_routes;
  323. void (*pull_calc_reg)(struct rockchip_pin_bank *bank,
  324. int pin_num, struct regmap **regmap,
  325. int *reg, u8 *bit);
  326. void (*drv_calc_reg)(struct rockchip_pin_bank *bank,
  327. int pin_num, struct regmap **regmap,
  328. int *reg, u8 *bit);
  329. int (*schmitt_calc_reg)(struct rockchip_pin_bank *bank,
  330. int pin_num, struct regmap **regmap,
  331. int *reg, u8 *bit);
  332. };
  333. struct rockchip_pin_config {
  334. unsigned int func;
  335. unsigned long *configs;
  336. unsigned int nconfigs;
  337. };
  338. /**
  339. * struct rockchip_pin_group: represent group of pins of a pinmux function.
  340. * @name: name of the pin group, used to lookup the group.
  341. * @pins: the pins included in this group.
  342. * @npins: number of pins included in this group.
  343. * @func: the mux function number to be programmed when selected.
  344. * @configs: the config values to be set for each pin
  345. * @nconfigs: number of configs for each pin
  346. */
  347. struct rockchip_pin_group {
  348. const char *name;
  349. unsigned int npins;
  350. unsigned int *pins;
  351. struct rockchip_pin_config *data;
  352. };
  353. /**
  354. * struct rockchip_pmx_func: represent a pin function.
  355. * @name: name of the pin function, used to lookup the function.
  356. * @groups: one or more names of pin groups that provide this function.
  357. * @num_groups: number of groups included in @groups.
  358. */
  359. struct rockchip_pmx_func {
  360. const char *name;
  361. const char **groups;
  362. u8 ngroups;
  363. };
  364. struct rockchip_pinctrl {
  365. struct regmap *regmap_base;
  366. int reg_size;
  367. struct regmap *regmap_pull;
  368. struct regmap *regmap_pmu;
  369. struct device *dev;
  370. struct rockchip_pin_ctrl *ctrl;
  371. struct pinctrl_desc pctl;
  372. struct pinctrl_dev *pctl_dev;
  373. struct rockchip_pin_group *groups;
  374. unsigned int ngroups;
  375. struct rockchip_pmx_func *functions;
  376. unsigned int nfunctions;
  377. };
  378. static struct regmap_config rockchip_regmap_config = {
  379. .reg_bits = 32,
  380. .val_bits = 32,
  381. .reg_stride = 4,
  382. };
  383. static inline const struct rockchip_pin_group *pinctrl_name_to_group(
  384. const struct rockchip_pinctrl *info,
  385. const char *name)
  386. {
  387. int i;
  388. for (i = 0; i < info->ngroups; i++) {
  389. if (!strcmp(info->groups[i].name, name))
  390. return &info->groups[i];
  391. }
  392. return NULL;
  393. }
  394. /*
  395. * given a pin number that is local to a pin controller, find out the pin bank
  396. * and the register base of the pin bank.
  397. */
  398. static struct rockchip_pin_bank *pin_to_bank(struct rockchip_pinctrl *info,
  399. unsigned pin)
  400. {
  401. struct rockchip_pin_bank *b = info->ctrl->pin_banks;
  402. while (pin >= (b->pin_base + b->nr_pins))
  403. b++;
  404. return b;
  405. }
  406. static struct rockchip_pin_bank *bank_num_to_bank(
  407. struct rockchip_pinctrl *info,
  408. unsigned num)
  409. {
  410. struct rockchip_pin_bank *b = info->ctrl->pin_banks;
  411. int i;
  412. for (i = 0; i < info->ctrl->nr_banks; i++, b++) {
  413. if (b->bank_num == num)
  414. return b;
  415. }
  416. return ERR_PTR(-EINVAL);
  417. }
  418. /*
  419. * Pinctrl_ops handling
  420. */
  421. static int rockchip_get_groups_count(struct pinctrl_dev *pctldev)
  422. {
  423. struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  424. return info->ngroups;
  425. }
  426. static const char *rockchip_get_group_name(struct pinctrl_dev *pctldev,
  427. unsigned selector)
  428. {
  429. struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  430. return info->groups[selector].name;
  431. }
  432. static int rockchip_get_group_pins(struct pinctrl_dev *pctldev,
  433. unsigned selector, const unsigned **pins,
  434. unsigned *npins)
  435. {
  436. struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  437. if (selector >= info->ngroups)
  438. return -EINVAL;
  439. *pins = info->groups[selector].pins;
  440. *npins = info->groups[selector].npins;
  441. return 0;
  442. }
  443. static int rockchip_dt_node_to_map(struct pinctrl_dev *pctldev,
  444. struct device_node *np,
  445. struct pinctrl_map **map, unsigned *num_maps)
  446. {
  447. struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  448. const struct rockchip_pin_group *grp;
  449. struct pinctrl_map *new_map;
  450. struct device_node *parent;
  451. int map_num = 1;
  452. int i;
  453. /*
  454. * first find the group of this node and check if we need to create
  455. * config maps for pins
  456. */
  457. grp = pinctrl_name_to_group(info, np->name);
  458. if (!grp) {
  459. dev_err(info->dev, "unable to find group for node %s\n",
  460. np->name);
  461. return -EINVAL;
  462. }
  463. map_num += grp->npins;
  464. new_map = kcalloc(map_num, sizeof(*new_map), GFP_KERNEL);
  465. if (!new_map)
  466. return -ENOMEM;
  467. *map = new_map;
  468. *num_maps = map_num;
  469. /* create mux map */
  470. parent = of_get_parent(np);
  471. if (!parent) {
  472. kfree(new_map);
  473. return -EINVAL;
  474. }
  475. new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
  476. new_map[0].data.mux.function = parent->name;
  477. new_map[0].data.mux.group = np->name;
  478. of_node_put(parent);
  479. /* create config map */
  480. new_map++;
  481. for (i = 0; i < grp->npins; i++) {
  482. new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
  483. new_map[i].data.configs.group_or_pin =
  484. pin_get_name(pctldev, grp->pins[i]);
  485. new_map[i].data.configs.configs = grp->data[i].configs;
  486. new_map[i].data.configs.num_configs = grp->data[i].nconfigs;
  487. }
  488. dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
  489. (*map)->data.mux.function, (*map)->data.mux.group, map_num);
  490. return 0;
  491. }
  492. static void rockchip_dt_free_map(struct pinctrl_dev *pctldev,
  493. struct pinctrl_map *map, unsigned num_maps)
  494. {
  495. kfree(map);
  496. }
  497. static const struct pinctrl_ops rockchip_pctrl_ops = {
  498. .get_groups_count = rockchip_get_groups_count,
  499. .get_group_name = rockchip_get_group_name,
  500. .get_group_pins = rockchip_get_group_pins,
  501. .dt_node_to_map = rockchip_dt_node_to_map,
  502. .dt_free_map = rockchip_dt_free_map,
  503. };
  504. /*
  505. * Hardware access
  506. */
  507. static struct rockchip_mux_recalced_data rv1108_mux_recalced_data[] = {
  508. {
  509. .num = 1,
  510. .pin = 0,
  511. .reg = 0x418,
  512. .bit = 0,
  513. .mask = 0x3
  514. }, {
  515. .num = 1,
  516. .pin = 1,
  517. .reg = 0x418,
  518. .bit = 2,
  519. .mask = 0x3
  520. }, {
  521. .num = 1,
  522. .pin = 2,
  523. .reg = 0x418,
  524. .bit = 4,
  525. .mask = 0x3
  526. }, {
  527. .num = 1,
  528. .pin = 3,
  529. .reg = 0x418,
  530. .bit = 6,
  531. .mask = 0x3
  532. }, {
  533. .num = 1,
  534. .pin = 4,
  535. .reg = 0x418,
  536. .bit = 8,
  537. .mask = 0x3
  538. }, {
  539. .num = 1,
  540. .pin = 5,
  541. .reg = 0x418,
  542. .bit = 10,
  543. .mask = 0x3
  544. }, {
  545. .num = 1,
  546. .pin = 6,
  547. .reg = 0x418,
  548. .bit = 12,
  549. .mask = 0x3
  550. }, {
  551. .num = 1,
  552. .pin = 7,
  553. .reg = 0x418,
  554. .bit = 14,
  555. .mask = 0x3
  556. }, {
  557. .num = 1,
  558. .pin = 8,
  559. .reg = 0x41c,
  560. .bit = 0,
  561. .mask = 0x3
  562. }, {
  563. .num = 1,
  564. .pin = 9,
  565. .reg = 0x41c,
  566. .bit = 2,
  567. .mask = 0x3
  568. },
  569. };
  570. static struct rockchip_mux_recalced_data rk3128_mux_recalced_data[] = {
  571. {
  572. .num = 2,
  573. .pin = 20,
  574. .reg = 0xe8,
  575. .bit = 0,
  576. .mask = 0x7
  577. }, {
  578. .num = 2,
  579. .pin = 21,
  580. .reg = 0xe8,
  581. .bit = 4,
  582. .mask = 0x7
  583. }, {
  584. .num = 2,
  585. .pin = 22,
  586. .reg = 0xe8,
  587. .bit = 8,
  588. .mask = 0x7
  589. }, {
  590. .num = 2,
  591. .pin = 23,
  592. .reg = 0xe8,
  593. .bit = 12,
  594. .mask = 0x7
  595. }, {
  596. .num = 2,
  597. .pin = 24,
  598. .reg = 0xd4,
  599. .bit = 12,
  600. .mask = 0x7
  601. },
  602. };
  603. static struct rockchip_mux_recalced_data rk3328_mux_recalced_data[] = {
  604. {
  605. .num = 2,
  606. .pin = 12,
  607. .reg = 0x24,
  608. .bit = 8,
  609. .mask = 0x3
  610. }, {
  611. .num = 2,
  612. .pin = 15,
  613. .reg = 0x28,
  614. .bit = 0,
  615. .mask = 0x7
  616. }, {
  617. .num = 2,
  618. .pin = 23,
  619. .reg = 0x30,
  620. .bit = 14,
  621. .mask = 0x3
  622. },
  623. };
  624. static void rockchip_get_recalced_mux(struct rockchip_pin_bank *bank, int pin,
  625. int *reg, u8 *bit, int *mask)
  626. {
  627. struct rockchip_pinctrl *info = bank->drvdata;
  628. struct rockchip_pin_ctrl *ctrl = info->ctrl;
  629. struct rockchip_mux_recalced_data *data;
  630. int i;
  631. for (i = 0; i < ctrl->niomux_recalced; i++) {
  632. data = &ctrl->iomux_recalced[i];
  633. if (data->num == bank->bank_num &&
  634. data->pin == pin)
  635. break;
  636. }
  637. if (i >= ctrl->niomux_recalced)
  638. return;
  639. *reg = data->reg;
  640. *mask = data->mask;
  641. *bit = data->bit;
  642. }
  643. static struct rockchip_mux_route_data px30_mux_route_data[] = {
  644. {
  645. /* cif-d2m0 */
  646. .bank_num = 2,
  647. .pin = 0,
  648. .func = 1,
  649. .route_offset = 0x184,
  650. .route_val = BIT(16 + 7),
  651. }, {
  652. /* cif-d2m1 */
  653. .bank_num = 3,
  654. .pin = 3,
  655. .func = 3,
  656. .route_offset = 0x184,
  657. .route_val = BIT(16 + 7) | BIT(7),
  658. }, {
  659. /* pdm-m0 */
  660. .bank_num = 3,
  661. .pin = 22,
  662. .func = 2,
  663. .route_offset = 0x184,
  664. .route_val = BIT(16 + 8),
  665. }, {
  666. /* pdm-m1 */
  667. .bank_num = 2,
  668. .pin = 22,
  669. .func = 1,
  670. .route_offset = 0x184,
  671. .route_val = BIT(16 + 8) | BIT(8),
  672. }, {
  673. /* uart2-rxm0 */
  674. .bank_num = 1,
  675. .pin = 27,
  676. .func = 2,
  677. .route_offset = 0x184,
  678. .route_val = BIT(16 + 10),
  679. }, {
  680. /* uart2-rxm1 */
  681. .bank_num = 2,
  682. .pin = 14,
  683. .func = 2,
  684. .route_offset = 0x184,
  685. .route_val = BIT(16 + 10) | BIT(10),
  686. }, {
  687. /* uart3-rxm0 */
  688. .bank_num = 0,
  689. .pin = 17,
  690. .func = 2,
  691. .route_offset = 0x184,
  692. .route_val = BIT(16 + 9),
  693. }, {
  694. /* uart3-rxm1 */
  695. .bank_num = 1,
  696. .pin = 15,
  697. .func = 2,
  698. .route_offset = 0x184,
  699. .route_val = BIT(16 + 9) | BIT(9),
  700. },
  701. };
  702. static struct rockchip_mux_route_data rk3128_mux_route_data[] = {
  703. {
  704. /* spi-0 */
  705. .bank_num = 1,
  706. .pin = 10,
  707. .func = 1,
  708. .route_offset = 0x144,
  709. .route_val = BIT(16 + 3) | BIT(16 + 4),
  710. }, {
  711. /* spi-1 */
  712. .bank_num = 1,
  713. .pin = 27,
  714. .func = 3,
  715. .route_offset = 0x144,
  716. .route_val = BIT(16 + 3) | BIT(16 + 4) | BIT(3),
  717. }, {
  718. /* spi-2 */
  719. .bank_num = 0,
  720. .pin = 13,
  721. .func = 2,
  722. .route_offset = 0x144,
  723. .route_val = BIT(16 + 3) | BIT(16 + 4) | BIT(4),
  724. }, {
  725. /* i2s-0 */
  726. .bank_num = 1,
  727. .pin = 5,
  728. .func = 1,
  729. .route_offset = 0x144,
  730. .route_val = BIT(16 + 5),
  731. }, {
  732. /* i2s-1 */
  733. .bank_num = 0,
  734. .pin = 14,
  735. .func = 1,
  736. .route_offset = 0x144,
  737. .route_val = BIT(16 + 5) | BIT(5),
  738. }, {
  739. /* emmc-0 */
  740. .bank_num = 1,
  741. .pin = 22,
  742. .func = 2,
  743. .route_offset = 0x144,
  744. .route_val = BIT(16 + 6),
  745. }, {
  746. /* emmc-1 */
  747. .bank_num = 2,
  748. .pin = 4,
  749. .func = 2,
  750. .route_offset = 0x144,
  751. .route_val = BIT(16 + 6) | BIT(6),
  752. },
  753. };
  754. static struct rockchip_mux_route_data rk3228_mux_route_data[] = {
  755. {
  756. /* pwm0-0 */
  757. .bank_num = 0,
  758. .pin = 26,
  759. .func = 1,
  760. .route_offset = 0x50,
  761. .route_val = BIT(16),
  762. }, {
  763. /* pwm0-1 */
  764. .bank_num = 3,
  765. .pin = 21,
  766. .func = 1,
  767. .route_offset = 0x50,
  768. .route_val = BIT(16) | BIT(0),
  769. }, {
  770. /* pwm1-0 */
  771. .bank_num = 0,
  772. .pin = 27,
  773. .func = 1,
  774. .route_offset = 0x50,
  775. .route_val = BIT(16 + 1),
  776. }, {
  777. /* pwm1-1 */
  778. .bank_num = 0,
  779. .pin = 30,
  780. .func = 2,
  781. .route_offset = 0x50,
  782. .route_val = BIT(16 + 1) | BIT(1),
  783. }, {
  784. /* pwm2-0 */
  785. .bank_num = 0,
  786. .pin = 28,
  787. .func = 1,
  788. .route_offset = 0x50,
  789. .route_val = BIT(16 + 2),
  790. }, {
  791. /* pwm2-1 */
  792. .bank_num = 1,
  793. .pin = 12,
  794. .func = 2,
  795. .route_offset = 0x50,
  796. .route_val = BIT(16 + 2) | BIT(2),
  797. }, {
  798. /* pwm3-0 */
  799. .bank_num = 3,
  800. .pin = 26,
  801. .func = 1,
  802. .route_offset = 0x50,
  803. .route_val = BIT(16 + 3),
  804. }, {
  805. /* pwm3-1 */
  806. .bank_num = 1,
  807. .pin = 11,
  808. .func = 2,
  809. .route_offset = 0x50,
  810. .route_val = BIT(16 + 3) | BIT(3),
  811. }, {
  812. /* sdio-0_d0 */
  813. .bank_num = 1,
  814. .pin = 1,
  815. .func = 1,
  816. .route_offset = 0x50,
  817. .route_val = BIT(16 + 4),
  818. }, {
  819. /* sdio-1_d0 */
  820. .bank_num = 3,
  821. .pin = 2,
  822. .func = 1,
  823. .route_offset = 0x50,
  824. .route_val = BIT(16 + 4) | BIT(4),
  825. }, {
  826. /* spi-0_rx */
  827. .bank_num = 0,
  828. .pin = 13,
  829. .func = 2,
  830. .route_offset = 0x50,
  831. .route_val = BIT(16 + 5),
  832. }, {
  833. /* spi-1_rx */
  834. .bank_num = 2,
  835. .pin = 0,
  836. .func = 2,
  837. .route_offset = 0x50,
  838. .route_val = BIT(16 + 5) | BIT(5),
  839. }, {
  840. /* emmc-0_cmd */
  841. .bank_num = 1,
  842. .pin = 22,
  843. .func = 2,
  844. .route_offset = 0x50,
  845. .route_val = BIT(16 + 7),
  846. }, {
  847. /* emmc-1_cmd */
  848. .bank_num = 2,
  849. .pin = 4,
  850. .func = 2,
  851. .route_offset = 0x50,
  852. .route_val = BIT(16 + 7) | BIT(7),
  853. }, {
  854. /* uart2-0_rx */
  855. .bank_num = 1,
  856. .pin = 19,
  857. .func = 2,
  858. .route_offset = 0x50,
  859. .route_val = BIT(16 + 8),
  860. }, {
  861. /* uart2-1_rx */
  862. .bank_num = 1,
  863. .pin = 10,
  864. .func = 2,
  865. .route_offset = 0x50,
  866. .route_val = BIT(16 + 8) | BIT(8),
  867. }, {
  868. /* uart1-0_rx */
  869. .bank_num = 1,
  870. .pin = 10,
  871. .func = 1,
  872. .route_offset = 0x50,
  873. .route_val = BIT(16 + 11),
  874. }, {
  875. /* uart1-1_rx */
  876. .bank_num = 3,
  877. .pin = 13,
  878. .func = 1,
  879. .route_offset = 0x50,
  880. .route_val = BIT(16 + 11) | BIT(11),
  881. },
  882. };
  883. static struct rockchip_mux_route_data rk3288_mux_route_data[] = {
  884. {
  885. /* edphdmi_cecinoutt1 */
  886. .bank_num = 7,
  887. .pin = 16,
  888. .func = 2,
  889. .route_offset = 0x264,
  890. .route_val = BIT(16 + 12) | BIT(12),
  891. }, {
  892. /* edphdmi_cecinout */
  893. .bank_num = 7,
  894. .pin = 23,
  895. .func = 4,
  896. .route_offset = 0x264,
  897. .route_val = BIT(16 + 12),
  898. },
  899. };
  900. static struct rockchip_mux_route_data rk3328_mux_route_data[] = {
  901. {
  902. /* uart2dbg_rxm0 */
  903. .bank_num = 1,
  904. .pin = 1,
  905. .func = 2,
  906. .route_offset = 0x50,
  907. .route_val = BIT(16) | BIT(16 + 1),
  908. }, {
  909. /* uart2dbg_rxm1 */
  910. .bank_num = 2,
  911. .pin = 1,
  912. .func = 1,
  913. .route_offset = 0x50,
  914. .route_val = BIT(16) | BIT(16 + 1) | BIT(0),
  915. }, {
  916. /* gmac-m1_rxd0 */
  917. .bank_num = 1,
  918. .pin = 11,
  919. .func = 2,
  920. .route_offset = 0x50,
  921. .route_val = BIT(16 + 2) | BIT(2),
  922. }, {
  923. /* gmac-m1-optimized_rxd3 */
  924. .bank_num = 1,
  925. .pin = 14,
  926. .func = 2,
  927. .route_offset = 0x50,
  928. .route_val = BIT(16 + 10) | BIT(10),
  929. }, {
  930. /* pdm_sdi0m0 */
  931. .bank_num = 2,
  932. .pin = 19,
  933. .func = 2,
  934. .route_offset = 0x50,
  935. .route_val = BIT(16 + 3),
  936. }, {
  937. /* pdm_sdi0m1 */
  938. .bank_num = 1,
  939. .pin = 23,
  940. .func = 3,
  941. .route_offset = 0x50,
  942. .route_val = BIT(16 + 3) | BIT(3),
  943. }, {
  944. /* spi_rxdm2 */
  945. .bank_num = 3,
  946. .pin = 2,
  947. .func = 4,
  948. .route_offset = 0x50,
  949. .route_val = BIT(16 + 4) | BIT(16 + 5) | BIT(5),
  950. }, {
  951. /* i2s2_sdim0 */
  952. .bank_num = 1,
  953. .pin = 24,
  954. .func = 1,
  955. .route_offset = 0x50,
  956. .route_val = BIT(16 + 6),
  957. }, {
  958. /* i2s2_sdim1 */
  959. .bank_num = 3,
  960. .pin = 2,
  961. .func = 6,
  962. .route_offset = 0x50,
  963. .route_val = BIT(16 + 6) | BIT(6),
  964. }, {
  965. /* card_iom1 */
  966. .bank_num = 2,
  967. .pin = 22,
  968. .func = 3,
  969. .route_offset = 0x50,
  970. .route_val = BIT(16 + 7) | BIT(7),
  971. }, {
  972. /* tsp_d5m1 */
  973. .bank_num = 2,
  974. .pin = 16,
  975. .func = 3,
  976. .route_offset = 0x50,
  977. .route_val = BIT(16 + 8) | BIT(8),
  978. }, {
  979. /* cif_data5m1 */
  980. .bank_num = 2,
  981. .pin = 16,
  982. .func = 4,
  983. .route_offset = 0x50,
  984. .route_val = BIT(16 + 9) | BIT(9),
  985. },
  986. };
  987. static struct rockchip_mux_route_data rk3399_mux_route_data[] = {
  988. {
  989. /* uart2dbga_rx */
  990. .bank_num = 4,
  991. .pin = 8,
  992. .func = 2,
  993. .route_offset = 0xe21c,
  994. .route_val = BIT(16 + 10) | BIT(16 + 11),
  995. }, {
  996. /* uart2dbgb_rx */
  997. .bank_num = 4,
  998. .pin = 16,
  999. .func = 2,
  1000. .route_offset = 0xe21c,
  1001. .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(10),
  1002. }, {
  1003. /* uart2dbgc_rx */
  1004. .bank_num = 4,
  1005. .pin = 19,
  1006. .func = 1,
  1007. .route_offset = 0xe21c,
  1008. .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(11),
  1009. }, {
  1010. /* pcie_clkreqn */
  1011. .bank_num = 2,
  1012. .pin = 26,
  1013. .func = 2,
  1014. .route_offset = 0xe21c,
  1015. .route_val = BIT(16 + 14),
  1016. }, {
  1017. /* pcie_clkreqnb */
  1018. .bank_num = 4,
  1019. .pin = 24,
  1020. .func = 1,
  1021. .route_offset = 0xe21c,
  1022. .route_val = BIT(16 + 14) | BIT(14),
  1023. },
  1024. };
  1025. static bool rockchip_get_mux_route(struct rockchip_pin_bank *bank, int pin,
  1026. int mux, u32 *reg, u32 *value)
  1027. {
  1028. struct rockchip_pinctrl *info = bank->drvdata;
  1029. struct rockchip_pin_ctrl *ctrl = info->ctrl;
  1030. struct rockchip_mux_route_data *data;
  1031. int i;
  1032. for (i = 0; i < ctrl->niomux_routes; i++) {
  1033. data = &ctrl->iomux_routes[i];
  1034. if ((data->bank_num == bank->bank_num) &&
  1035. (data->pin == pin) && (data->func == mux))
  1036. break;
  1037. }
  1038. if (i >= ctrl->niomux_routes)
  1039. return false;
  1040. *reg = data->route_offset;
  1041. *value = data->route_val;
  1042. return true;
  1043. }
  1044. static int rockchip_get_mux(struct rockchip_pin_bank *bank, int pin)
  1045. {
  1046. struct rockchip_pinctrl *info = bank->drvdata;
  1047. int iomux_num = (pin / 8);
  1048. struct regmap *regmap;
  1049. unsigned int val;
  1050. int reg, ret, mask, mux_type;
  1051. u8 bit;
  1052. if (iomux_num > 3)
  1053. return -EINVAL;
  1054. if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
  1055. dev_err(info->dev, "pin %d is unrouted\n", pin);
  1056. return -EINVAL;
  1057. }
  1058. if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
  1059. return RK_FUNC_GPIO;
  1060. regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
  1061. ? info->regmap_pmu : info->regmap_base;
  1062. /* get basic quadrupel of mux registers and the correct reg inside */
  1063. mux_type = bank->iomux[iomux_num].type;
  1064. reg = bank->iomux[iomux_num].offset;
  1065. if (mux_type & IOMUX_WIDTH_4BIT) {
  1066. if ((pin % 8) >= 4)
  1067. reg += 0x4;
  1068. bit = (pin % 4) * 4;
  1069. mask = 0xf;
  1070. } else if (mux_type & IOMUX_WIDTH_3BIT) {
  1071. if ((pin % 8) >= 5)
  1072. reg += 0x4;
  1073. bit = (pin % 8 % 5) * 3;
  1074. mask = 0x7;
  1075. } else {
  1076. bit = (pin % 8) * 2;
  1077. mask = 0x3;
  1078. }
  1079. if (bank->recalced_mask & BIT(pin))
  1080. rockchip_get_recalced_mux(bank, pin, &reg, &bit, &mask);
  1081. ret = regmap_read(regmap, reg, &val);
  1082. if (ret)
  1083. return ret;
  1084. return ((val >> bit) & mask);
  1085. }
  1086. static int rockchip_verify_mux(struct rockchip_pin_bank *bank,
  1087. int pin, int mux)
  1088. {
  1089. struct rockchip_pinctrl *info = bank->drvdata;
  1090. int iomux_num = (pin / 8);
  1091. if (iomux_num > 3)
  1092. return -EINVAL;
  1093. if (bank->iomux[iomux_num].type & IOMUX_UNROUTED) {
  1094. dev_err(info->dev, "pin %d is unrouted\n", pin);
  1095. return -EINVAL;
  1096. }
  1097. if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY) {
  1098. if (mux != RK_FUNC_GPIO) {
  1099. dev_err(info->dev,
  1100. "pin %d only supports a gpio mux\n", pin);
  1101. return -ENOTSUPP;
  1102. }
  1103. }
  1104. return 0;
  1105. }
  1106. /*
  1107. * Set a new mux function for a pin.
  1108. *
  1109. * The register is divided into the upper and lower 16 bit. When changing
  1110. * a value, the previous register value is not read and changed. Instead
  1111. * it seems the changed bits are marked in the upper 16 bit, while the
  1112. * changed value gets set in the same offset in the lower 16 bit.
  1113. * All pin settings seem to be 2 bit wide in both the upper and lower
  1114. * parts.
  1115. * @bank: pin bank to change
  1116. * @pin: pin to change
  1117. * @mux: new mux function to set
  1118. */
  1119. static int rockchip_set_mux(struct rockchip_pin_bank *bank, int pin, int mux)
  1120. {
  1121. struct rockchip_pinctrl *info = bank->drvdata;
  1122. int iomux_num = (pin / 8);
  1123. struct regmap *regmap;
  1124. int reg, ret, mask, mux_type;
  1125. u8 bit;
  1126. u32 data, rmask, route_reg, route_val;
  1127. ret = rockchip_verify_mux(bank, pin, mux);
  1128. if (ret < 0)
  1129. return ret;
  1130. if (bank->iomux[iomux_num].type & IOMUX_GPIO_ONLY)
  1131. return 0;
  1132. dev_dbg(info->dev, "setting mux of GPIO%d-%d to %d\n",
  1133. bank->bank_num, pin, mux);
  1134. regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU)
  1135. ? info->regmap_pmu : info->regmap_base;
  1136. /* get basic quadrupel of mux registers and the correct reg inside */
  1137. mux_type = bank->iomux[iomux_num].type;
  1138. reg = bank->iomux[iomux_num].offset;
  1139. if (mux_type & IOMUX_WIDTH_4BIT) {
  1140. if ((pin % 8) >= 4)
  1141. reg += 0x4;
  1142. bit = (pin % 4) * 4;
  1143. mask = 0xf;
  1144. } else if (mux_type & IOMUX_WIDTH_3BIT) {
  1145. if ((pin % 8) >= 5)
  1146. reg += 0x4;
  1147. bit = (pin % 8 % 5) * 3;
  1148. mask = 0x7;
  1149. } else {
  1150. bit = (pin % 8) * 2;
  1151. mask = 0x3;
  1152. }
  1153. if (bank->recalced_mask & BIT(pin))
  1154. rockchip_get_recalced_mux(bank, pin, &reg, &bit, &mask);
  1155. if (bank->route_mask & BIT(pin)) {
  1156. if (rockchip_get_mux_route(bank, pin, mux, &route_reg,
  1157. &route_val)) {
  1158. ret = regmap_write(regmap, route_reg, route_val);
  1159. if (ret)
  1160. return ret;
  1161. }
  1162. }
  1163. data = (mask << (bit + 16));
  1164. rmask = data | (data >> 16);
  1165. data |= (mux & mask) << bit;
  1166. ret = regmap_update_bits(regmap, reg, rmask, data);
  1167. return ret;
  1168. }
  1169. #define PX30_PULL_PMU_OFFSET 0x10
  1170. #define PX30_PULL_GRF_OFFSET 0x60
  1171. #define PX30_PULL_BITS_PER_PIN 2
  1172. #define PX30_PULL_PINS_PER_REG 8
  1173. #define PX30_PULL_BANK_STRIDE 16
  1174. static void px30_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
  1175. int pin_num, struct regmap **regmap,
  1176. int *reg, u8 *bit)
  1177. {
  1178. struct rockchip_pinctrl *info = bank->drvdata;
  1179. /* The first 32 pins of the first bank are located in PMU */
  1180. if (bank->bank_num == 0) {
  1181. *regmap = info->regmap_pmu;
  1182. *reg = PX30_PULL_PMU_OFFSET;
  1183. } else {
  1184. *regmap = info->regmap_base;
  1185. *reg = PX30_PULL_GRF_OFFSET;
  1186. /* correct the offset, as we're starting with the 2nd bank */
  1187. *reg -= 0x10;
  1188. *reg += bank->bank_num * PX30_PULL_BANK_STRIDE;
  1189. }
  1190. *reg += ((pin_num / PX30_PULL_PINS_PER_REG) * 4);
  1191. *bit = (pin_num % PX30_PULL_PINS_PER_REG);
  1192. *bit *= PX30_PULL_BITS_PER_PIN;
  1193. }
  1194. #define PX30_DRV_PMU_OFFSET 0x20
  1195. #define PX30_DRV_GRF_OFFSET 0xf0
  1196. #define PX30_DRV_BITS_PER_PIN 2
  1197. #define PX30_DRV_PINS_PER_REG 8
  1198. #define PX30_DRV_BANK_STRIDE 16
  1199. static void px30_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
  1200. int pin_num, struct regmap **regmap,
  1201. int *reg, u8 *bit)
  1202. {
  1203. struct rockchip_pinctrl *info = bank->drvdata;
  1204. /* The first 32 pins of the first bank are located in PMU */
  1205. if (bank->bank_num == 0) {
  1206. *regmap = info->regmap_pmu;
  1207. *reg = PX30_DRV_PMU_OFFSET;
  1208. } else {
  1209. *regmap = info->regmap_base;
  1210. *reg = PX30_DRV_GRF_OFFSET;
  1211. /* correct the offset, as we're starting with the 2nd bank */
  1212. *reg -= 0x10;
  1213. *reg += bank->bank_num * PX30_DRV_BANK_STRIDE;
  1214. }
  1215. *reg += ((pin_num / PX30_DRV_PINS_PER_REG) * 4);
  1216. *bit = (pin_num % PX30_DRV_PINS_PER_REG);
  1217. *bit *= PX30_DRV_BITS_PER_PIN;
  1218. }
  1219. #define PX30_SCHMITT_PMU_OFFSET 0x38
  1220. #define PX30_SCHMITT_GRF_OFFSET 0xc0
  1221. #define PX30_SCHMITT_PINS_PER_PMU_REG 16
  1222. #define PX30_SCHMITT_BANK_STRIDE 16
  1223. #define PX30_SCHMITT_PINS_PER_GRF_REG 8
  1224. static int px30_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
  1225. int pin_num,
  1226. struct regmap **regmap,
  1227. int *reg, u8 *bit)
  1228. {
  1229. struct rockchip_pinctrl *info = bank->drvdata;
  1230. int pins_per_reg;
  1231. if (bank->bank_num == 0) {
  1232. *regmap = info->regmap_pmu;
  1233. *reg = PX30_SCHMITT_PMU_OFFSET;
  1234. pins_per_reg = PX30_SCHMITT_PINS_PER_PMU_REG;
  1235. } else {
  1236. *regmap = info->regmap_base;
  1237. *reg = PX30_SCHMITT_GRF_OFFSET;
  1238. pins_per_reg = PX30_SCHMITT_PINS_PER_GRF_REG;
  1239. *reg += (bank->bank_num - 1) * PX30_SCHMITT_BANK_STRIDE;
  1240. }
  1241. *reg += ((pin_num / pins_per_reg) * 4);
  1242. *bit = pin_num % pins_per_reg;
  1243. return 0;
  1244. }
  1245. #define RV1108_PULL_PMU_OFFSET 0x10
  1246. #define RV1108_PULL_OFFSET 0x110
  1247. #define RV1108_PULL_PINS_PER_REG 8
  1248. #define RV1108_PULL_BITS_PER_PIN 2
  1249. #define RV1108_PULL_BANK_STRIDE 16
  1250. static void rv1108_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
  1251. int pin_num, struct regmap **regmap,
  1252. int *reg, u8 *bit)
  1253. {
  1254. struct rockchip_pinctrl *info = bank->drvdata;
  1255. /* The first 24 pins of the first bank are located in PMU */
  1256. if (bank->bank_num == 0) {
  1257. *regmap = info->regmap_pmu;
  1258. *reg = RV1108_PULL_PMU_OFFSET;
  1259. } else {
  1260. *reg = RV1108_PULL_OFFSET;
  1261. *regmap = info->regmap_base;
  1262. /* correct the offset, as we're starting with the 2nd bank */
  1263. *reg -= 0x10;
  1264. *reg += bank->bank_num * RV1108_PULL_BANK_STRIDE;
  1265. }
  1266. *reg += ((pin_num / RV1108_PULL_PINS_PER_REG) * 4);
  1267. *bit = (pin_num % RV1108_PULL_PINS_PER_REG);
  1268. *bit *= RV1108_PULL_BITS_PER_PIN;
  1269. }
  1270. #define RV1108_DRV_PMU_OFFSET 0x20
  1271. #define RV1108_DRV_GRF_OFFSET 0x210
  1272. #define RV1108_DRV_BITS_PER_PIN 2
  1273. #define RV1108_DRV_PINS_PER_REG 8
  1274. #define RV1108_DRV_BANK_STRIDE 16
  1275. static void rv1108_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
  1276. int pin_num, struct regmap **regmap,
  1277. int *reg, u8 *bit)
  1278. {
  1279. struct rockchip_pinctrl *info = bank->drvdata;
  1280. /* The first 24 pins of the first bank are located in PMU */
  1281. if (bank->bank_num == 0) {
  1282. *regmap = info->regmap_pmu;
  1283. *reg = RV1108_DRV_PMU_OFFSET;
  1284. } else {
  1285. *regmap = info->regmap_base;
  1286. *reg = RV1108_DRV_GRF_OFFSET;
  1287. /* correct the offset, as we're starting with the 2nd bank */
  1288. *reg -= 0x10;
  1289. *reg += bank->bank_num * RV1108_DRV_BANK_STRIDE;
  1290. }
  1291. *reg += ((pin_num / RV1108_DRV_PINS_PER_REG) * 4);
  1292. *bit = pin_num % RV1108_DRV_PINS_PER_REG;
  1293. *bit *= RV1108_DRV_BITS_PER_PIN;
  1294. }
  1295. #define RV1108_SCHMITT_PMU_OFFSET 0x30
  1296. #define RV1108_SCHMITT_GRF_OFFSET 0x388
  1297. #define RV1108_SCHMITT_BANK_STRIDE 8
  1298. #define RV1108_SCHMITT_PINS_PER_GRF_REG 16
  1299. #define RV1108_SCHMITT_PINS_PER_PMU_REG 8
  1300. static int rv1108_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
  1301. int pin_num,
  1302. struct regmap **regmap,
  1303. int *reg, u8 *bit)
  1304. {
  1305. struct rockchip_pinctrl *info = bank->drvdata;
  1306. int pins_per_reg;
  1307. if (bank->bank_num == 0) {
  1308. *regmap = info->regmap_pmu;
  1309. *reg = RV1108_SCHMITT_PMU_OFFSET;
  1310. pins_per_reg = RV1108_SCHMITT_PINS_PER_PMU_REG;
  1311. } else {
  1312. *regmap = info->regmap_base;
  1313. *reg = RV1108_SCHMITT_GRF_OFFSET;
  1314. pins_per_reg = RV1108_SCHMITT_PINS_PER_GRF_REG;
  1315. *reg += (bank->bank_num - 1) * RV1108_SCHMITT_BANK_STRIDE;
  1316. }
  1317. *reg += ((pin_num / pins_per_reg) * 4);
  1318. *bit = pin_num % pins_per_reg;
  1319. return 0;
  1320. }
  1321. #define RK2928_PULL_OFFSET 0x118
  1322. #define RK2928_PULL_PINS_PER_REG 16
  1323. #define RK2928_PULL_BANK_STRIDE 8
  1324. static void rk2928_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
  1325. int pin_num, struct regmap **regmap,
  1326. int *reg, u8 *bit)
  1327. {
  1328. struct rockchip_pinctrl *info = bank->drvdata;
  1329. *regmap = info->regmap_base;
  1330. *reg = RK2928_PULL_OFFSET;
  1331. *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE;
  1332. *reg += (pin_num / RK2928_PULL_PINS_PER_REG) * 4;
  1333. *bit = pin_num % RK2928_PULL_PINS_PER_REG;
  1334. };
  1335. #define RK3128_PULL_OFFSET 0x118
  1336. static void rk3128_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
  1337. int pin_num, struct regmap **regmap,
  1338. int *reg, u8 *bit)
  1339. {
  1340. struct rockchip_pinctrl *info = bank->drvdata;
  1341. *regmap = info->regmap_base;
  1342. *reg = RK3128_PULL_OFFSET;
  1343. *reg += bank->bank_num * RK2928_PULL_BANK_STRIDE;
  1344. *reg += ((pin_num / RK2928_PULL_PINS_PER_REG) * 4);
  1345. *bit = pin_num % RK2928_PULL_PINS_PER_REG;
  1346. }
  1347. #define RK3188_PULL_OFFSET 0x164
  1348. #define RK3188_PULL_BITS_PER_PIN 2
  1349. #define RK3188_PULL_PINS_PER_REG 8
  1350. #define RK3188_PULL_BANK_STRIDE 16
  1351. #define RK3188_PULL_PMU_OFFSET 0x64
  1352. static void rk3188_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
  1353. int pin_num, struct regmap **regmap,
  1354. int *reg, u8 *bit)
  1355. {
  1356. struct rockchip_pinctrl *info = bank->drvdata;
  1357. /* The first 12 pins of the first bank are located elsewhere */
  1358. if (bank->bank_num == 0 && pin_num < 12) {
  1359. *regmap = info->regmap_pmu ? info->regmap_pmu
  1360. : bank->regmap_pull;
  1361. *reg = info->regmap_pmu ? RK3188_PULL_PMU_OFFSET : 0;
  1362. *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
  1363. *bit = pin_num % RK3188_PULL_PINS_PER_REG;
  1364. *bit *= RK3188_PULL_BITS_PER_PIN;
  1365. } else {
  1366. *regmap = info->regmap_pull ? info->regmap_pull
  1367. : info->regmap_base;
  1368. *reg = info->regmap_pull ? 0 : RK3188_PULL_OFFSET;
  1369. /* correct the offset, as it is the 2nd pull register */
  1370. *reg -= 4;
  1371. *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
  1372. *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
  1373. /*
  1374. * The bits in these registers have an inverse ordering
  1375. * with the lowest pin being in bits 15:14 and the highest
  1376. * pin in bits 1:0
  1377. */
  1378. *bit = 7 - (pin_num % RK3188_PULL_PINS_PER_REG);
  1379. *bit *= RK3188_PULL_BITS_PER_PIN;
  1380. }
  1381. }
  1382. #define RK3288_PULL_OFFSET 0x140
  1383. static void rk3288_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
  1384. int pin_num, struct regmap **regmap,
  1385. int *reg, u8 *bit)
  1386. {
  1387. struct rockchip_pinctrl *info = bank->drvdata;
  1388. /* The first 24 pins of the first bank are located in PMU */
  1389. if (bank->bank_num == 0) {
  1390. *regmap = info->regmap_pmu;
  1391. *reg = RK3188_PULL_PMU_OFFSET;
  1392. *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
  1393. *bit = pin_num % RK3188_PULL_PINS_PER_REG;
  1394. *bit *= RK3188_PULL_BITS_PER_PIN;
  1395. } else {
  1396. *regmap = info->regmap_base;
  1397. *reg = RK3288_PULL_OFFSET;
  1398. /* correct the offset, as we're starting with the 2nd bank */
  1399. *reg -= 0x10;
  1400. *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
  1401. *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
  1402. *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
  1403. *bit *= RK3188_PULL_BITS_PER_PIN;
  1404. }
  1405. }
  1406. #define RK3288_DRV_PMU_OFFSET 0x70
  1407. #define RK3288_DRV_GRF_OFFSET 0x1c0
  1408. #define RK3288_DRV_BITS_PER_PIN 2
  1409. #define RK3288_DRV_PINS_PER_REG 8
  1410. #define RK3288_DRV_BANK_STRIDE 16
  1411. static void rk3288_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
  1412. int pin_num, struct regmap **regmap,
  1413. int *reg, u8 *bit)
  1414. {
  1415. struct rockchip_pinctrl *info = bank->drvdata;
  1416. /* The first 24 pins of the first bank are located in PMU */
  1417. if (bank->bank_num == 0) {
  1418. *regmap = info->regmap_pmu;
  1419. *reg = RK3288_DRV_PMU_OFFSET;
  1420. *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
  1421. *bit = pin_num % RK3288_DRV_PINS_PER_REG;
  1422. *bit *= RK3288_DRV_BITS_PER_PIN;
  1423. } else {
  1424. *regmap = info->regmap_base;
  1425. *reg = RK3288_DRV_GRF_OFFSET;
  1426. /* correct the offset, as we're starting with the 2nd bank */
  1427. *reg -= 0x10;
  1428. *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
  1429. *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
  1430. *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
  1431. *bit *= RK3288_DRV_BITS_PER_PIN;
  1432. }
  1433. }
  1434. #define RK3228_PULL_OFFSET 0x100
  1435. static void rk3228_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
  1436. int pin_num, struct regmap **regmap,
  1437. int *reg, u8 *bit)
  1438. {
  1439. struct rockchip_pinctrl *info = bank->drvdata;
  1440. *regmap = info->regmap_base;
  1441. *reg = RK3228_PULL_OFFSET;
  1442. *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
  1443. *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
  1444. *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
  1445. *bit *= RK3188_PULL_BITS_PER_PIN;
  1446. }
  1447. #define RK3228_DRV_GRF_OFFSET 0x200
  1448. static void rk3228_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
  1449. int pin_num, struct regmap **regmap,
  1450. int *reg, u8 *bit)
  1451. {
  1452. struct rockchip_pinctrl *info = bank->drvdata;
  1453. *regmap = info->regmap_base;
  1454. *reg = RK3228_DRV_GRF_OFFSET;
  1455. *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
  1456. *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
  1457. *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
  1458. *bit *= RK3288_DRV_BITS_PER_PIN;
  1459. }
  1460. #define RK3368_PULL_GRF_OFFSET 0x100
  1461. #define RK3368_PULL_PMU_OFFSET 0x10
  1462. static void rk3368_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
  1463. int pin_num, struct regmap **regmap,
  1464. int *reg, u8 *bit)
  1465. {
  1466. struct rockchip_pinctrl *info = bank->drvdata;
  1467. /* The first 32 pins of the first bank are located in PMU */
  1468. if (bank->bank_num == 0) {
  1469. *regmap = info->regmap_pmu;
  1470. *reg = RK3368_PULL_PMU_OFFSET;
  1471. *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
  1472. *bit = pin_num % RK3188_PULL_PINS_PER_REG;
  1473. *bit *= RK3188_PULL_BITS_PER_PIN;
  1474. } else {
  1475. *regmap = info->regmap_base;
  1476. *reg = RK3368_PULL_GRF_OFFSET;
  1477. /* correct the offset, as we're starting with the 2nd bank */
  1478. *reg -= 0x10;
  1479. *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
  1480. *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
  1481. *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
  1482. *bit *= RK3188_PULL_BITS_PER_PIN;
  1483. }
  1484. }
  1485. #define RK3368_DRV_PMU_OFFSET 0x20
  1486. #define RK3368_DRV_GRF_OFFSET 0x200
  1487. static void rk3368_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
  1488. int pin_num, struct regmap **regmap,
  1489. int *reg, u8 *bit)
  1490. {
  1491. struct rockchip_pinctrl *info = bank->drvdata;
  1492. /* The first 32 pins of the first bank are located in PMU */
  1493. if (bank->bank_num == 0) {
  1494. *regmap = info->regmap_pmu;
  1495. *reg = RK3368_DRV_PMU_OFFSET;
  1496. *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
  1497. *bit = pin_num % RK3288_DRV_PINS_PER_REG;
  1498. *bit *= RK3288_DRV_BITS_PER_PIN;
  1499. } else {
  1500. *regmap = info->regmap_base;
  1501. *reg = RK3368_DRV_GRF_OFFSET;
  1502. /* correct the offset, as we're starting with the 2nd bank */
  1503. *reg -= 0x10;
  1504. *reg += bank->bank_num * RK3288_DRV_BANK_STRIDE;
  1505. *reg += ((pin_num / RK3288_DRV_PINS_PER_REG) * 4);
  1506. *bit = (pin_num % RK3288_DRV_PINS_PER_REG);
  1507. *bit *= RK3288_DRV_BITS_PER_PIN;
  1508. }
  1509. }
  1510. #define RK3399_PULL_GRF_OFFSET 0xe040
  1511. #define RK3399_PULL_PMU_OFFSET 0x40
  1512. #define RK3399_DRV_3BITS_PER_PIN 3
  1513. static void rk3399_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank,
  1514. int pin_num, struct regmap **regmap,
  1515. int *reg, u8 *bit)
  1516. {
  1517. struct rockchip_pinctrl *info = bank->drvdata;
  1518. /* The bank0:16 and bank1:32 pins are located in PMU */
  1519. if ((bank->bank_num == 0) || (bank->bank_num == 1)) {
  1520. *regmap = info->regmap_pmu;
  1521. *reg = RK3399_PULL_PMU_OFFSET;
  1522. *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
  1523. *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
  1524. *bit = pin_num % RK3188_PULL_PINS_PER_REG;
  1525. *bit *= RK3188_PULL_BITS_PER_PIN;
  1526. } else {
  1527. *regmap = info->regmap_base;
  1528. *reg = RK3399_PULL_GRF_OFFSET;
  1529. /* correct the offset, as we're starting with the 3rd bank */
  1530. *reg -= 0x20;
  1531. *reg += bank->bank_num * RK3188_PULL_BANK_STRIDE;
  1532. *reg += ((pin_num / RK3188_PULL_PINS_PER_REG) * 4);
  1533. *bit = (pin_num % RK3188_PULL_PINS_PER_REG);
  1534. *bit *= RK3188_PULL_BITS_PER_PIN;
  1535. }
  1536. }
  1537. static void rk3399_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank,
  1538. int pin_num, struct regmap **regmap,
  1539. int *reg, u8 *bit)
  1540. {
  1541. struct rockchip_pinctrl *info = bank->drvdata;
  1542. int drv_num = (pin_num / 8);
  1543. /* The bank0:16 and bank1:32 pins are located in PMU */
  1544. if ((bank->bank_num == 0) || (bank->bank_num == 1))
  1545. *regmap = info->regmap_pmu;
  1546. else
  1547. *regmap = info->regmap_base;
  1548. *reg = bank->drv[drv_num].offset;
  1549. if ((bank->drv[drv_num].drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
  1550. (bank->drv[drv_num].drv_type == DRV_TYPE_IO_3V3_ONLY))
  1551. *bit = (pin_num % 8) * 3;
  1552. else
  1553. *bit = (pin_num % 8) * 2;
  1554. }
  1555. static int rockchip_perpin_drv_list[DRV_TYPE_MAX][8] = {
  1556. { 2, 4, 8, 12, -1, -1, -1, -1 },
  1557. { 3, 6, 9, 12, -1, -1, -1, -1 },
  1558. { 5, 10, 15, 20, -1, -1, -1, -1 },
  1559. { 4, 6, 8, 10, 12, 14, 16, 18 },
  1560. { 4, 7, 10, 13, 16, 19, 22, 26 }
  1561. };
  1562. static int rockchip_get_drive_perpin(struct rockchip_pin_bank *bank,
  1563. int pin_num)
  1564. {
  1565. struct rockchip_pinctrl *info = bank->drvdata;
  1566. struct rockchip_pin_ctrl *ctrl = info->ctrl;
  1567. struct regmap *regmap;
  1568. int reg, ret;
  1569. u32 data, temp, rmask_bits;
  1570. u8 bit;
  1571. int drv_type = bank->drv[pin_num / 8].drv_type;
  1572. ctrl->drv_calc_reg(bank, pin_num, &regmap, &reg, &bit);
  1573. switch (drv_type) {
  1574. case DRV_TYPE_IO_1V8_3V0_AUTO:
  1575. case DRV_TYPE_IO_3V3_ONLY:
  1576. rmask_bits = RK3399_DRV_3BITS_PER_PIN;
  1577. switch (bit) {
  1578. case 0 ... 12:
  1579. /* regular case, nothing to do */
  1580. break;
  1581. case 15:
  1582. /*
  1583. * drive-strength offset is special, as it is
  1584. * spread over 2 registers
  1585. */
  1586. ret = regmap_read(regmap, reg, &data);
  1587. if (ret)
  1588. return ret;
  1589. ret = regmap_read(regmap, reg + 0x4, &temp);
  1590. if (ret)
  1591. return ret;
  1592. /*
  1593. * the bit data[15] contains bit 0 of the value
  1594. * while temp[1:0] contains bits 2 and 1
  1595. */
  1596. data >>= 15;
  1597. temp &= 0x3;
  1598. temp <<= 1;
  1599. data |= temp;
  1600. return rockchip_perpin_drv_list[drv_type][data];
  1601. case 18 ... 21:
  1602. /* setting fully enclosed in the second register */
  1603. reg += 4;
  1604. bit -= 16;
  1605. break;
  1606. default:
  1607. dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n",
  1608. bit, drv_type);
  1609. return -EINVAL;
  1610. }
  1611. break;
  1612. case DRV_TYPE_IO_DEFAULT:
  1613. case DRV_TYPE_IO_1V8_OR_3V0:
  1614. case DRV_TYPE_IO_1V8_ONLY:
  1615. rmask_bits = RK3288_DRV_BITS_PER_PIN;
  1616. break;
  1617. default:
  1618. dev_err(info->dev, "unsupported pinctrl drive type: %d\n",
  1619. drv_type);
  1620. return -EINVAL;
  1621. }
  1622. ret = regmap_read(regmap, reg, &data);
  1623. if (ret)
  1624. return ret;
  1625. data >>= bit;
  1626. data &= (1 << rmask_bits) - 1;
  1627. return rockchip_perpin_drv_list[drv_type][data];
  1628. }
  1629. static int rockchip_set_drive_perpin(struct rockchip_pin_bank *bank,
  1630. int pin_num, int strength)
  1631. {
  1632. struct rockchip_pinctrl *info = bank->drvdata;
  1633. struct rockchip_pin_ctrl *ctrl = info->ctrl;
  1634. struct regmap *regmap;
  1635. int reg, ret, i;
  1636. u32 data, rmask, rmask_bits, temp;
  1637. u8 bit;
  1638. int drv_type = bank->drv[pin_num / 8].drv_type;
  1639. dev_dbg(info->dev, "setting drive of GPIO%d-%d to %d\n",
  1640. bank->bank_num, pin_num, strength);
  1641. ctrl->drv_calc_reg(bank, pin_num, &regmap, &reg, &bit);
  1642. ret = -EINVAL;
  1643. for (i = 0; i < ARRAY_SIZE(rockchip_perpin_drv_list[drv_type]); i++) {
  1644. if (rockchip_perpin_drv_list[drv_type][i] == strength) {
  1645. ret = i;
  1646. break;
  1647. } else if (rockchip_perpin_drv_list[drv_type][i] < 0) {
  1648. ret = rockchip_perpin_drv_list[drv_type][i];
  1649. break;
  1650. }
  1651. }
  1652. if (ret < 0) {
  1653. dev_err(info->dev, "unsupported driver strength %d\n",
  1654. strength);
  1655. return ret;
  1656. }
  1657. switch (drv_type) {
  1658. case DRV_TYPE_IO_1V8_3V0_AUTO:
  1659. case DRV_TYPE_IO_3V3_ONLY:
  1660. rmask_bits = RK3399_DRV_3BITS_PER_PIN;
  1661. switch (bit) {
  1662. case 0 ... 12:
  1663. /* regular case, nothing to do */
  1664. break;
  1665. case 15:
  1666. /*
  1667. * drive-strength offset is special, as it is spread
  1668. * over 2 registers, the bit data[15] contains bit 0
  1669. * of the value while temp[1:0] contains bits 2 and 1
  1670. */
  1671. data = (ret & 0x1) << 15;
  1672. temp = (ret >> 0x1) & 0x3;
  1673. rmask = BIT(15) | BIT(31);
  1674. data |= BIT(31);
  1675. ret = regmap_update_bits(regmap, reg, rmask, data);
  1676. if (ret)
  1677. return ret;
  1678. rmask = 0x3 | (0x3 << 16);
  1679. temp |= (0x3 << 16);
  1680. reg += 0x4;
  1681. ret = regmap_update_bits(regmap, reg, rmask, temp);
  1682. return ret;
  1683. case 18 ... 21:
  1684. /* setting fully enclosed in the second register */
  1685. reg += 4;
  1686. bit -= 16;
  1687. break;
  1688. default:
  1689. dev_err(info->dev, "unsupported bit: %d for pinctrl drive type: %d\n",
  1690. bit, drv_type);
  1691. return -EINVAL;
  1692. }
  1693. break;
  1694. case DRV_TYPE_IO_DEFAULT:
  1695. case DRV_TYPE_IO_1V8_OR_3V0:
  1696. case DRV_TYPE_IO_1V8_ONLY:
  1697. rmask_bits = RK3288_DRV_BITS_PER_PIN;
  1698. break;
  1699. default:
  1700. dev_err(info->dev, "unsupported pinctrl drive type: %d\n",
  1701. drv_type);
  1702. return -EINVAL;
  1703. }
  1704. /* enable the write to the equivalent lower bits */
  1705. data = ((1 << rmask_bits) - 1) << (bit + 16);
  1706. rmask = data | (data >> 16);
  1707. data |= (ret << bit);
  1708. ret = regmap_update_bits(regmap, reg, rmask, data);
  1709. return ret;
  1710. }
  1711. static int rockchip_pull_list[PULL_TYPE_MAX][4] = {
  1712. {
  1713. PIN_CONFIG_BIAS_DISABLE,
  1714. PIN_CONFIG_BIAS_PULL_UP,
  1715. PIN_CONFIG_BIAS_PULL_DOWN,
  1716. PIN_CONFIG_BIAS_BUS_HOLD
  1717. },
  1718. {
  1719. PIN_CONFIG_BIAS_DISABLE,
  1720. PIN_CONFIG_BIAS_PULL_DOWN,
  1721. PIN_CONFIG_BIAS_DISABLE,
  1722. PIN_CONFIG_BIAS_PULL_UP
  1723. },
  1724. };
  1725. static int rockchip_get_pull(struct rockchip_pin_bank *bank, int pin_num)
  1726. {
  1727. struct rockchip_pinctrl *info = bank->drvdata;
  1728. struct rockchip_pin_ctrl *ctrl = info->ctrl;
  1729. struct regmap *regmap;
  1730. int reg, ret, pull_type;
  1731. u8 bit;
  1732. u32 data;
  1733. /* rk3066b does support any pulls */
  1734. if (ctrl->type == RK3066B)
  1735. return PIN_CONFIG_BIAS_DISABLE;
  1736. ctrl->pull_calc_reg(bank, pin_num, &regmap, &reg, &bit);
  1737. ret = regmap_read(regmap, reg, &data);
  1738. if (ret)
  1739. return ret;
  1740. switch (ctrl->type) {
  1741. case RK2928:
  1742. case RK3128:
  1743. return !(data & BIT(bit))
  1744. ? PIN_CONFIG_BIAS_PULL_PIN_DEFAULT
  1745. : PIN_CONFIG_BIAS_DISABLE;
  1746. case PX30:
  1747. case RV1108:
  1748. case RK3188:
  1749. case RK3288:
  1750. case RK3368:
  1751. case RK3399:
  1752. pull_type = bank->pull_type[pin_num / 8];
  1753. data >>= bit;
  1754. data &= (1 << RK3188_PULL_BITS_PER_PIN) - 1;
  1755. return rockchip_pull_list[pull_type][data];
  1756. default:
  1757. dev_err(info->dev, "unsupported pinctrl type\n");
  1758. return -EINVAL;
  1759. };
  1760. }
  1761. static int rockchip_set_pull(struct rockchip_pin_bank *bank,
  1762. int pin_num, int pull)
  1763. {
  1764. struct rockchip_pinctrl *info = bank->drvdata;
  1765. struct rockchip_pin_ctrl *ctrl = info->ctrl;
  1766. struct regmap *regmap;
  1767. int reg, ret, i, pull_type;
  1768. u8 bit;
  1769. u32 data, rmask;
  1770. dev_dbg(info->dev, "setting pull of GPIO%d-%d to %d\n",
  1771. bank->bank_num, pin_num, pull);
  1772. /* rk3066b does support any pulls */
  1773. if (ctrl->type == RK3066B)
  1774. return pull ? -EINVAL : 0;
  1775. ctrl->pull_calc_reg(bank, pin_num, &regmap, &reg, &bit);
  1776. switch (ctrl->type) {
  1777. case RK2928:
  1778. case RK3128:
  1779. data = BIT(bit + 16);
  1780. if (pull == PIN_CONFIG_BIAS_DISABLE)
  1781. data |= BIT(bit);
  1782. ret = regmap_write(regmap, reg, data);
  1783. break;
  1784. case PX30:
  1785. case RV1108:
  1786. case RK3188:
  1787. case RK3288:
  1788. case RK3368:
  1789. case RK3399:
  1790. pull_type = bank->pull_type[pin_num / 8];
  1791. ret = -EINVAL;
  1792. for (i = 0; i < ARRAY_SIZE(rockchip_pull_list[pull_type]);
  1793. i++) {
  1794. if (rockchip_pull_list[pull_type][i] == pull) {
  1795. ret = i;
  1796. break;
  1797. }
  1798. }
  1799. if (ret < 0) {
  1800. dev_err(info->dev, "unsupported pull setting %d\n",
  1801. pull);
  1802. return ret;
  1803. }
  1804. /* enable the write to the equivalent lower bits */
  1805. data = ((1 << RK3188_PULL_BITS_PER_PIN) - 1) << (bit + 16);
  1806. rmask = data | (data >> 16);
  1807. data |= (ret << bit);
  1808. ret = regmap_update_bits(regmap, reg, rmask, data);
  1809. break;
  1810. default:
  1811. dev_err(info->dev, "unsupported pinctrl type\n");
  1812. return -EINVAL;
  1813. }
  1814. return ret;
  1815. }
  1816. #define RK3328_SCHMITT_BITS_PER_PIN 1
  1817. #define RK3328_SCHMITT_PINS_PER_REG 16
  1818. #define RK3328_SCHMITT_BANK_STRIDE 8
  1819. #define RK3328_SCHMITT_GRF_OFFSET 0x380
  1820. static int rk3328_calc_schmitt_reg_and_bit(struct rockchip_pin_bank *bank,
  1821. int pin_num,
  1822. struct regmap **regmap,
  1823. int *reg, u8 *bit)
  1824. {
  1825. struct rockchip_pinctrl *info = bank->drvdata;
  1826. *regmap = info->regmap_base;
  1827. *reg = RK3328_SCHMITT_GRF_OFFSET;
  1828. *reg += bank->bank_num * RK3328_SCHMITT_BANK_STRIDE;
  1829. *reg += ((pin_num / RK3328_SCHMITT_PINS_PER_REG) * 4);
  1830. *bit = pin_num % RK3328_SCHMITT_PINS_PER_REG;
  1831. return 0;
  1832. }
  1833. static int rockchip_get_schmitt(struct rockchip_pin_bank *bank, int pin_num)
  1834. {
  1835. struct rockchip_pinctrl *info = bank->drvdata;
  1836. struct rockchip_pin_ctrl *ctrl = info->ctrl;
  1837. struct regmap *regmap;
  1838. int reg, ret;
  1839. u8 bit;
  1840. u32 data;
  1841. ret = ctrl->schmitt_calc_reg(bank, pin_num, &regmap, &reg, &bit);
  1842. if (ret)
  1843. return ret;
  1844. ret = regmap_read(regmap, reg, &data);
  1845. if (ret)
  1846. return ret;
  1847. data >>= bit;
  1848. return data & 0x1;
  1849. }
  1850. static int rockchip_set_schmitt(struct rockchip_pin_bank *bank,
  1851. int pin_num, int enable)
  1852. {
  1853. struct rockchip_pinctrl *info = bank->drvdata;
  1854. struct rockchip_pin_ctrl *ctrl = info->ctrl;
  1855. struct regmap *regmap;
  1856. int reg, ret;
  1857. u8 bit;
  1858. u32 data, rmask;
  1859. dev_dbg(info->dev, "setting input schmitt of GPIO%d-%d to %d\n",
  1860. bank->bank_num, pin_num, enable);
  1861. ret = ctrl->schmitt_calc_reg(bank, pin_num, &regmap, &reg, &bit);
  1862. if (ret)
  1863. return ret;
  1864. /* enable the write to the equivalent lower bits */
  1865. data = BIT(bit + 16) | (enable << bit);
  1866. rmask = BIT(bit + 16) | BIT(bit);
  1867. return regmap_update_bits(regmap, reg, rmask, data);
  1868. }
  1869. /*
  1870. * Pinmux_ops handling
  1871. */
  1872. static int rockchip_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
  1873. {
  1874. struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  1875. return info->nfunctions;
  1876. }
  1877. static const char *rockchip_pmx_get_func_name(struct pinctrl_dev *pctldev,
  1878. unsigned selector)
  1879. {
  1880. struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  1881. return info->functions[selector].name;
  1882. }
  1883. static int rockchip_pmx_get_groups(struct pinctrl_dev *pctldev,
  1884. unsigned selector, const char * const **groups,
  1885. unsigned * const num_groups)
  1886. {
  1887. struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  1888. *groups = info->functions[selector].groups;
  1889. *num_groups = info->functions[selector].ngroups;
  1890. return 0;
  1891. }
  1892. static int rockchip_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
  1893. unsigned group)
  1894. {
  1895. struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  1896. const unsigned int *pins = info->groups[group].pins;
  1897. const struct rockchip_pin_config *data = info->groups[group].data;
  1898. struct rockchip_pin_bank *bank;
  1899. int cnt, ret = 0;
  1900. dev_dbg(info->dev, "enable function %s group %s\n",
  1901. info->functions[selector].name, info->groups[group].name);
  1902. /*
  1903. * for each pin in the pin group selected, program the corresponding
  1904. * pin function number in the config register.
  1905. */
  1906. for (cnt = 0; cnt < info->groups[group].npins; cnt++) {
  1907. bank = pin_to_bank(info, pins[cnt]);
  1908. ret = rockchip_set_mux(bank, pins[cnt] - bank->pin_base,
  1909. data[cnt].func);
  1910. if (ret)
  1911. break;
  1912. }
  1913. if (ret) {
  1914. /* revert the already done pin settings */
  1915. for (cnt--; cnt >= 0; cnt--)
  1916. rockchip_set_mux(bank, pins[cnt] - bank->pin_base, 0);
  1917. return ret;
  1918. }
  1919. return 0;
  1920. }
  1921. static int rockchip_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  1922. {
  1923. struct rockchip_pin_bank *bank = gpiochip_get_data(chip);
  1924. u32 data;
  1925. int ret;
  1926. ret = clk_enable(bank->clk);
  1927. if (ret < 0) {
  1928. dev_err(bank->drvdata->dev,
  1929. "failed to enable clock for bank %s\n", bank->name);
  1930. return ret;
  1931. }
  1932. data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
  1933. clk_disable(bank->clk);
  1934. return !(data & BIT(offset));
  1935. }
  1936. /*
  1937. * The calls to gpio_direction_output() and gpio_direction_input()
  1938. * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
  1939. * function called from the gpiolib interface).
  1940. */
  1941. static int _rockchip_pmx_gpio_set_direction(struct gpio_chip *chip,
  1942. int pin, bool input)
  1943. {
  1944. struct rockchip_pin_bank *bank;
  1945. int ret;
  1946. unsigned long flags;
  1947. u32 data;
  1948. bank = gpiochip_get_data(chip);
  1949. ret = rockchip_set_mux(bank, pin, RK_FUNC_GPIO);
  1950. if (ret < 0)
  1951. return ret;
  1952. clk_enable(bank->clk);
  1953. raw_spin_lock_irqsave(&bank->slock, flags);
  1954. data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
  1955. /* set bit to 1 for output, 0 for input */
  1956. if (!input)
  1957. data |= BIT(pin);
  1958. else
  1959. data &= ~BIT(pin);
  1960. writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
  1961. raw_spin_unlock_irqrestore(&bank->slock, flags);
  1962. clk_disable(bank->clk);
  1963. return 0;
  1964. }
  1965. static int rockchip_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  1966. struct pinctrl_gpio_range *range,
  1967. unsigned offset, bool input)
  1968. {
  1969. struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  1970. struct gpio_chip *chip;
  1971. int pin;
  1972. chip = range->gc;
  1973. pin = offset - chip->base;
  1974. dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n",
  1975. offset, range->name, pin, input ? "input" : "output");
  1976. return _rockchip_pmx_gpio_set_direction(chip, offset - chip->base,
  1977. input);
  1978. }
  1979. static const struct pinmux_ops rockchip_pmx_ops = {
  1980. .get_functions_count = rockchip_pmx_get_funcs_count,
  1981. .get_function_name = rockchip_pmx_get_func_name,
  1982. .get_function_groups = rockchip_pmx_get_groups,
  1983. .set_mux = rockchip_pmx_set,
  1984. .gpio_set_direction = rockchip_pmx_gpio_set_direction,
  1985. };
  1986. /*
  1987. * Pinconf_ops handling
  1988. */
  1989. static bool rockchip_pinconf_pull_valid(struct rockchip_pin_ctrl *ctrl,
  1990. enum pin_config_param pull)
  1991. {
  1992. switch (ctrl->type) {
  1993. case RK2928:
  1994. case RK3128:
  1995. return (pull == PIN_CONFIG_BIAS_PULL_PIN_DEFAULT ||
  1996. pull == PIN_CONFIG_BIAS_DISABLE);
  1997. case RK3066B:
  1998. return pull ? false : true;
  1999. case PX30:
  2000. case RV1108:
  2001. case RK3188:
  2002. case RK3288:
  2003. case RK3368:
  2004. case RK3399:
  2005. return (pull != PIN_CONFIG_BIAS_PULL_PIN_DEFAULT);
  2006. }
  2007. return false;
  2008. }
  2009. static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
  2010. static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset);
  2011. /* set the pin config settings for a specified pin */
  2012. static int rockchip_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  2013. unsigned long *configs, unsigned num_configs)
  2014. {
  2015. struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  2016. struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
  2017. enum pin_config_param param;
  2018. u32 arg;
  2019. int i;
  2020. int rc;
  2021. for (i = 0; i < num_configs; i++) {
  2022. param = pinconf_to_config_param(configs[i]);
  2023. arg = pinconf_to_config_argument(configs[i]);
  2024. switch (param) {
  2025. case PIN_CONFIG_BIAS_DISABLE:
  2026. rc = rockchip_set_pull(bank, pin - bank->pin_base,
  2027. param);
  2028. if (rc)
  2029. return rc;
  2030. break;
  2031. case PIN_CONFIG_BIAS_PULL_UP:
  2032. case PIN_CONFIG_BIAS_PULL_DOWN:
  2033. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  2034. case PIN_CONFIG_BIAS_BUS_HOLD:
  2035. if (!rockchip_pinconf_pull_valid(info->ctrl, param))
  2036. return -ENOTSUPP;
  2037. if (!arg)
  2038. return -EINVAL;
  2039. rc = rockchip_set_pull(bank, pin - bank->pin_base,
  2040. param);
  2041. if (rc)
  2042. return rc;
  2043. break;
  2044. case PIN_CONFIG_OUTPUT:
  2045. rockchip_gpio_set(&bank->gpio_chip,
  2046. pin - bank->pin_base, arg);
  2047. rc = _rockchip_pmx_gpio_set_direction(&bank->gpio_chip,
  2048. pin - bank->pin_base, false);
  2049. if (rc)
  2050. return rc;
  2051. break;
  2052. case PIN_CONFIG_DRIVE_STRENGTH:
  2053. /* rk3288 is the first with per-pin drive-strength */
  2054. if (!info->ctrl->drv_calc_reg)
  2055. return -ENOTSUPP;
  2056. rc = rockchip_set_drive_perpin(bank,
  2057. pin - bank->pin_base, arg);
  2058. if (rc < 0)
  2059. return rc;
  2060. break;
  2061. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  2062. if (!info->ctrl->schmitt_calc_reg)
  2063. return -ENOTSUPP;
  2064. rc = rockchip_set_schmitt(bank,
  2065. pin - bank->pin_base, arg);
  2066. if (rc < 0)
  2067. return rc;
  2068. break;
  2069. default:
  2070. return -ENOTSUPP;
  2071. break;
  2072. }
  2073. } /* for each config */
  2074. return 0;
  2075. }
  2076. /* get the pin config settings for a specified pin */
  2077. static int rockchip_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  2078. unsigned long *config)
  2079. {
  2080. struct rockchip_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  2081. struct rockchip_pin_bank *bank = pin_to_bank(info, pin);
  2082. enum pin_config_param param = pinconf_to_config_param(*config);
  2083. u16 arg;
  2084. int rc;
  2085. switch (param) {
  2086. case PIN_CONFIG_BIAS_DISABLE:
  2087. if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
  2088. return -EINVAL;
  2089. arg = 0;
  2090. break;
  2091. case PIN_CONFIG_BIAS_PULL_UP:
  2092. case PIN_CONFIG_BIAS_PULL_DOWN:
  2093. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  2094. case PIN_CONFIG_BIAS_BUS_HOLD:
  2095. if (!rockchip_pinconf_pull_valid(info->ctrl, param))
  2096. return -ENOTSUPP;
  2097. if (rockchip_get_pull(bank, pin - bank->pin_base) != param)
  2098. return -EINVAL;
  2099. arg = 1;
  2100. break;
  2101. case PIN_CONFIG_OUTPUT:
  2102. rc = rockchip_get_mux(bank, pin - bank->pin_base);
  2103. if (rc != RK_FUNC_GPIO)
  2104. return -EINVAL;
  2105. rc = rockchip_gpio_get(&bank->gpio_chip, pin - bank->pin_base);
  2106. if (rc < 0)
  2107. return rc;
  2108. arg = rc ? 1 : 0;
  2109. break;
  2110. case PIN_CONFIG_DRIVE_STRENGTH:
  2111. /* rk3288 is the first with per-pin drive-strength */
  2112. if (!info->ctrl->drv_calc_reg)
  2113. return -ENOTSUPP;
  2114. rc = rockchip_get_drive_perpin(bank, pin - bank->pin_base);
  2115. if (rc < 0)
  2116. return rc;
  2117. arg = rc;
  2118. break;
  2119. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  2120. if (!info->ctrl->schmitt_calc_reg)
  2121. return -ENOTSUPP;
  2122. rc = rockchip_get_schmitt(bank, pin - bank->pin_base);
  2123. if (rc < 0)
  2124. return rc;
  2125. arg = rc;
  2126. break;
  2127. default:
  2128. return -ENOTSUPP;
  2129. break;
  2130. }
  2131. *config = pinconf_to_config_packed(param, arg);
  2132. return 0;
  2133. }
  2134. static const struct pinconf_ops rockchip_pinconf_ops = {
  2135. .pin_config_get = rockchip_pinconf_get,
  2136. .pin_config_set = rockchip_pinconf_set,
  2137. .is_generic = true,
  2138. };
  2139. static const struct of_device_id rockchip_bank_match[] = {
  2140. { .compatible = "rockchip,gpio-bank" },
  2141. { .compatible = "rockchip,rk3188-gpio-bank0" },
  2142. {},
  2143. };
  2144. static void rockchip_pinctrl_child_count(struct rockchip_pinctrl *info,
  2145. struct device_node *np)
  2146. {
  2147. struct device_node *child;
  2148. for_each_child_of_node(np, child) {
  2149. if (of_match_node(rockchip_bank_match, child))
  2150. continue;
  2151. info->nfunctions++;
  2152. info->ngroups += of_get_child_count(child);
  2153. }
  2154. }
  2155. static int rockchip_pinctrl_parse_groups(struct device_node *np,
  2156. struct rockchip_pin_group *grp,
  2157. struct rockchip_pinctrl *info,
  2158. u32 index)
  2159. {
  2160. struct rockchip_pin_bank *bank;
  2161. int size;
  2162. const __be32 *list;
  2163. int num;
  2164. int i, j;
  2165. int ret;
  2166. dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
  2167. /* Initialise group */
  2168. grp->name = np->name;
  2169. /*
  2170. * the binding format is rockchip,pins = <bank pin mux CONFIG>,
  2171. * do sanity check and calculate pins number
  2172. */
  2173. list = of_get_property(np, "rockchip,pins", &size);
  2174. /* we do not check return since it's safe node passed down */
  2175. size /= sizeof(*list);
  2176. if (!size || size % 4) {
  2177. dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
  2178. return -EINVAL;
  2179. }
  2180. grp->npins = size / 4;
  2181. grp->pins = devm_kcalloc(info->dev, grp->npins, sizeof(unsigned int),
  2182. GFP_KERNEL);
  2183. grp->data = devm_kcalloc(info->dev,
  2184. grp->npins,
  2185. sizeof(struct rockchip_pin_config),
  2186. GFP_KERNEL);
  2187. if (!grp->pins || !grp->data)
  2188. return -ENOMEM;
  2189. for (i = 0, j = 0; i < size; i += 4, j++) {
  2190. const __be32 *phandle;
  2191. struct device_node *np_config;
  2192. num = be32_to_cpu(*list++);
  2193. bank = bank_num_to_bank(info, num);
  2194. if (IS_ERR(bank))
  2195. return PTR_ERR(bank);
  2196. grp->pins[j] = bank->pin_base + be32_to_cpu(*list++);
  2197. grp->data[j].func = be32_to_cpu(*list++);
  2198. phandle = list++;
  2199. if (!phandle)
  2200. return -EINVAL;
  2201. np_config = of_find_node_by_phandle(be32_to_cpup(phandle));
  2202. ret = pinconf_generic_parse_dt_config(np_config, NULL,
  2203. &grp->data[j].configs, &grp->data[j].nconfigs);
  2204. if (ret)
  2205. return ret;
  2206. }
  2207. return 0;
  2208. }
  2209. static int rockchip_pinctrl_parse_functions(struct device_node *np,
  2210. struct rockchip_pinctrl *info,
  2211. u32 index)
  2212. {
  2213. struct device_node *child;
  2214. struct rockchip_pmx_func *func;
  2215. struct rockchip_pin_group *grp;
  2216. int ret;
  2217. static u32 grp_index;
  2218. u32 i = 0;
  2219. dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
  2220. func = &info->functions[index];
  2221. /* Initialise function */
  2222. func->name = np->name;
  2223. func->ngroups = of_get_child_count(np);
  2224. if (func->ngroups <= 0)
  2225. return 0;
  2226. func->groups = devm_kcalloc(info->dev,
  2227. func->ngroups, sizeof(char *), GFP_KERNEL);
  2228. if (!func->groups)
  2229. return -ENOMEM;
  2230. for_each_child_of_node(np, child) {
  2231. func->groups[i] = child->name;
  2232. grp = &info->groups[grp_index++];
  2233. ret = rockchip_pinctrl_parse_groups(child, grp, info, i++);
  2234. if (ret) {
  2235. of_node_put(child);
  2236. return ret;
  2237. }
  2238. }
  2239. return 0;
  2240. }
  2241. static int rockchip_pinctrl_parse_dt(struct platform_device *pdev,
  2242. struct rockchip_pinctrl *info)
  2243. {
  2244. struct device *dev = &pdev->dev;
  2245. struct device_node *np = dev->of_node;
  2246. struct device_node *child;
  2247. int ret;
  2248. int i;
  2249. rockchip_pinctrl_child_count(info, np);
  2250. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  2251. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  2252. info->functions = devm_kcalloc(dev,
  2253. info->nfunctions,
  2254. sizeof(struct rockchip_pmx_func),
  2255. GFP_KERNEL);
  2256. if (!info->functions)
  2257. return -EINVAL;
  2258. info->groups = devm_kcalloc(dev,
  2259. info->ngroups,
  2260. sizeof(struct rockchip_pin_group),
  2261. GFP_KERNEL);
  2262. if (!info->groups)
  2263. return -EINVAL;
  2264. i = 0;
  2265. for_each_child_of_node(np, child) {
  2266. if (of_match_node(rockchip_bank_match, child))
  2267. continue;
  2268. ret = rockchip_pinctrl_parse_functions(child, info, i++);
  2269. if (ret) {
  2270. dev_err(&pdev->dev, "failed to parse function\n");
  2271. of_node_put(child);
  2272. return ret;
  2273. }
  2274. }
  2275. return 0;
  2276. }
  2277. static int rockchip_pinctrl_register(struct platform_device *pdev,
  2278. struct rockchip_pinctrl *info)
  2279. {
  2280. struct pinctrl_desc *ctrldesc = &info->pctl;
  2281. struct pinctrl_pin_desc *pindesc, *pdesc;
  2282. struct rockchip_pin_bank *pin_bank;
  2283. int pin, bank, ret;
  2284. int k;
  2285. ctrldesc->name = "rockchip-pinctrl";
  2286. ctrldesc->owner = THIS_MODULE;
  2287. ctrldesc->pctlops = &rockchip_pctrl_ops;
  2288. ctrldesc->pmxops = &rockchip_pmx_ops;
  2289. ctrldesc->confops = &rockchip_pinconf_ops;
  2290. pindesc = devm_kcalloc(&pdev->dev,
  2291. info->ctrl->nr_pins, sizeof(*pindesc),
  2292. GFP_KERNEL);
  2293. if (!pindesc)
  2294. return -ENOMEM;
  2295. ctrldesc->pins = pindesc;
  2296. ctrldesc->npins = info->ctrl->nr_pins;
  2297. pdesc = pindesc;
  2298. for (bank = 0 , k = 0; bank < info->ctrl->nr_banks; bank++) {
  2299. pin_bank = &info->ctrl->pin_banks[bank];
  2300. for (pin = 0; pin < pin_bank->nr_pins; pin++, k++) {
  2301. pdesc->number = k;
  2302. pdesc->name = kasprintf(GFP_KERNEL, "%s-%d",
  2303. pin_bank->name, pin);
  2304. pdesc++;
  2305. }
  2306. }
  2307. ret = rockchip_pinctrl_parse_dt(pdev, info);
  2308. if (ret)
  2309. return ret;
  2310. info->pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, info);
  2311. if (IS_ERR(info->pctl_dev)) {
  2312. dev_err(&pdev->dev, "could not register pinctrl driver\n");
  2313. return PTR_ERR(info->pctl_dev);
  2314. }
  2315. for (bank = 0; bank < info->ctrl->nr_banks; ++bank) {
  2316. pin_bank = &info->ctrl->pin_banks[bank];
  2317. pin_bank->grange.name = pin_bank->name;
  2318. pin_bank->grange.id = bank;
  2319. pin_bank->grange.pin_base = pin_bank->pin_base;
  2320. pin_bank->grange.base = pin_bank->gpio_chip.base;
  2321. pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
  2322. pin_bank->grange.gc = &pin_bank->gpio_chip;
  2323. pinctrl_add_gpio_range(info->pctl_dev, &pin_bank->grange);
  2324. }
  2325. return 0;
  2326. }
  2327. /*
  2328. * GPIO handling
  2329. */
  2330. static void rockchip_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  2331. {
  2332. struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
  2333. void __iomem *reg = bank->reg_base + GPIO_SWPORT_DR;
  2334. unsigned long flags;
  2335. u32 data;
  2336. clk_enable(bank->clk);
  2337. raw_spin_lock_irqsave(&bank->slock, flags);
  2338. data = readl(reg);
  2339. data &= ~BIT(offset);
  2340. if (value)
  2341. data |= BIT(offset);
  2342. writel(data, reg);
  2343. raw_spin_unlock_irqrestore(&bank->slock, flags);
  2344. clk_disable(bank->clk);
  2345. }
  2346. /*
  2347. * Returns the level of the pin for input direction and setting of the DR
  2348. * register for output gpios.
  2349. */
  2350. static int rockchip_gpio_get(struct gpio_chip *gc, unsigned offset)
  2351. {
  2352. struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
  2353. u32 data;
  2354. clk_enable(bank->clk);
  2355. data = readl(bank->reg_base + GPIO_EXT_PORT);
  2356. clk_disable(bank->clk);
  2357. data >>= offset;
  2358. data &= 1;
  2359. return data;
  2360. }
  2361. /*
  2362. * gpiolib gpio_direction_input callback function. The setting of the pin
  2363. * mux function as 'gpio input' will be handled by the pinctrl subsystem
  2364. * interface.
  2365. */
  2366. static int rockchip_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  2367. {
  2368. return pinctrl_gpio_direction_input(gc->base + offset);
  2369. }
  2370. /*
  2371. * gpiolib gpio_direction_output callback function. The setting of the pin
  2372. * mux function as 'gpio output' will be handled by the pinctrl subsystem
  2373. * interface.
  2374. */
  2375. static int rockchip_gpio_direction_output(struct gpio_chip *gc,
  2376. unsigned offset, int value)
  2377. {
  2378. rockchip_gpio_set(gc, offset, value);
  2379. return pinctrl_gpio_direction_output(gc->base + offset);
  2380. }
  2381. static void rockchip_gpio_set_debounce(struct gpio_chip *gc,
  2382. unsigned int offset, bool enable)
  2383. {
  2384. struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
  2385. void __iomem *reg = bank->reg_base + GPIO_DEBOUNCE;
  2386. unsigned long flags;
  2387. u32 data;
  2388. clk_enable(bank->clk);
  2389. raw_spin_lock_irqsave(&bank->slock, flags);
  2390. data = readl(reg);
  2391. if (enable)
  2392. data |= BIT(offset);
  2393. else
  2394. data &= ~BIT(offset);
  2395. writel(data, reg);
  2396. raw_spin_unlock_irqrestore(&bank->slock, flags);
  2397. clk_disable(bank->clk);
  2398. }
  2399. /*
  2400. * gpiolib set_config callback function. The setting of the pin
  2401. * mux function as 'gpio output' will be handled by the pinctrl subsystem
  2402. * interface.
  2403. */
  2404. static int rockchip_gpio_set_config(struct gpio_chip *gc, unsigned int offset,
  2405. unsigned long config)
  2406. {
  2407. enum pin_config_param param = pinconf_to_config_param(config);
  2408. switch (param) {
  2409. case PIN_CONFIG_INPUT_DEBOUNCE:
  2410. rockchip_gpio_set_debounce(gc, offset, true);
  2411. /*
  2412. * Rockchip's gpio could only support up to one period
  2413. * of the debounce clock(pclk), which is far away from
  2414. * satisftying the requirement, as pclk is usually near
  2415. * 100MHz shared by all peripherals. So the fact is it
  2416. * has crippled debounce capability could only be useful
  2417. * to prevent any spurious glitches from waking up the system
  2418. * if the gpio is conguired as wakeup interrupt source. Let's
  2419. * still return -ENOTSUPP as before, to make sure the caller
  2420. * of gpiod_set_debounce won't change its behaviour.
  2421. */
  2422. default:
  2423. return -ENOTSUPP;
  2424. }
  2425. }
  2426. /*
  2427. * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
  2428. * and a virtual IRQ, if not already present.
  2429. */
  2430. static int rockchip_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  2431. {
  2432. struct rockchip_pin_bank *bank = gpiochip_get_data(gc);
  2433. unsigned int virq;
  2434. if (!bank->domain)
  2435. return -ENXIO;
  2436. clk_enable(bank->clk);
  2437. virq = irq_create_mapping(bank->domain, offset);
  2438. clk_disable(bank->clk);
  2439. return (virq) ? : -ENXIO;
  2440. }
  2441. static const struct gpio_chip rockchip_gpiolib_chip = {
  2442. .request = gpiochip_generic_request,
  2443. .free = gpiochip_generic_free,
  2444. .set = rockchip_gpio_set,
  2445. .get = rockchip_gpio_get,
  2446. .get_direction = rockchip_gpio_get_direction,
  2447. .direction_input = rockchip_gpio_direction_input,
  2448. .direction_output = rockchip_gpio_direction_output,
  2449. .set_config = rockchip_gpio_set_config,
  2450. .to_irq = rockchip_gpio_to_irq,
  2451. .owner = THIS_MODULE,
  2452. };
  2453. /*
  2454. * Interrupt handling
  2455. */
  2456. static void rockchip_irq_demux(struct irq_desc *desc)
  2457. {
  2458. struct irq_chip *chip = irq_desc_get_chip(desc);
  2459. struct rockchip_pin_bank *bank = irq_desc_get_handler_data(desc);
  2460. u32 pend;
  2461. dev_dbg(bank->drvdata->dev, "got irq for bank %s\n", bank->name);
  2462. chained_irq_enter(chip, desc);
  2463. pend = readl_relaxed(bank->reg_base + GPIO_INT_STATUS);
  2464. while (pend) {
  2465. unsigned int irq, virq;
  2466. irq = __ffs(pend);
  2467. pend &= ~BIT(irq);
  2468. virq = irq_linear_revmap(bank->domain, irq);
  2469. if (!virq) {
  2470. dev_err(bank->drvdata->dev, "unmapped irq %d\n", irq);
  2471. continue;
  2472. }
  2473. dev_dbg(bank->drvdata->dev, "handling irq %d\n", irq);
  2474. /*
  2475. * Triggering IRQ on both rising and falling edge
  2476. * needs manual intervention.
  2477. */
  2478. if (bank->toggle_edge_mode & BIT(irq)) {
  2479. u32 data, data_old, polarity;
  2480. unsigned long flags;
  2481. data = readl_relaxed(bank->reg_base + GPIO_EXT_PORT);
  2482. do {
  2483. raw_spin_lock_irqsave(&bank->slock, flags);
  2484. polarity = readl_relaxed(bank->reg_base +
  2485. GPIO_INT_POLARITY);
  2486. if (data & BIT(irq))
  2487. polarity &= ~BIT(irq);
  2488. else
  2489. polarity |= BIT(irq);
  2490. writel(polarity,
  2491. bank->reg_base + GPIO_INT_POLARITY);
  2492. raw_spin_unlock_irqrestore(&bank->slock, flags);
  2493. data_old = data;
  2494. data = readl_relaxed(bank->reg_base +
  2495. GPIO_EXT_PORT);
  2496. } while ((data & BIT(irq)) != (data_old & BIT(irq)));
  2497. }
  2498. generic_handle_irq(virq);
  2499. }
  2500. chained_irq_exit(chip, desc);
  2501. }
  2502. static int rockchip_irq_set_type(struct irq_data *d, unsigned int type)
  2503. {
  2504. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  2505. struct rockchip_pin_bank *bank = gc->private;
  2506. u32 mask = BIT(d->hwirq);
  2507. u32 polarity;
  2508. u32 level;
  2509. u32 data;
  2510. unsigned long flags;
  2511. int ret;
  2512. /* make sure the pin is configured as gpio input */
  2513. ret = rockchip_set_mux(bank, d->hwirq, RK_FUNC_GPIO);
  2514. if (ret < 0)
  2515. return ret;
  2516. clk_enable(bank->clk);
  2517. raw_spin_lock_irqsave(&bank->slock, flags);
  2518. data = readl_relaxed(bank->reg_base + GPIO_SWPORT_DDR);
  2519. data &= ~mask;
  2520. writel_relaxed(data, bank->reg_base + GPIO_SWPORT_DDR);
  2521. raw_spin_unlock_irqrestore(&bank->slock, flags);
  2522. if (type & IRQ_TYPE_EDGE_BOTH)
  2523. irq_set_handler_locked(d, handle_edge_irq);
  2524. else
  2525. irq_set_handler_locked(d, handle_level_irq);
  2526. raw_spin_lock_irqsave(&bank->slock, flags);
  2527. irq_gc_lock(gc);
  2528. level = readl_relaxed(gc->reg_base + GPIO_INTTYPE_LEVEL);
  2529. polarity = readl_relaxed(gc->reg_base + GPIO_INT_POLARITY);
  2530. switch (type) {
  2531. case IRQ_TYPE_EDGE_BOTH:
  2532. bank->toggle_edge_mode |= mask;
  2533. level |= mask;
  2534. /*
  2535. * Determine gpio state. If 1 next interrupt should be falling
  2536. * otherwise rising.
  2537. */
  2538. data = readl(bank->reg_base + GPIO_EXT_PORT);
  2539. if (data & mask)
  2540. polarity &= ~mask;
  2541. else
  2542. polarity |= mask;
  2543. break;
  2544. case IRQ_TYPE_EDGE_RISING:
  2545. bank->toggle_edge_mode &= ~mask;
  2546. level |= mask;
  2547. polarity |= mask;
  2548. break;
  2549. case IRQ_TYPE_EDGE_FALLING:
  2550. bank->toggle_edge_mode &= ~mask;
  2551. level |= mask;
  2552. polarity &= ~mask;
  2553. break;
  2554. case IRQ_TYPE_LEVEL_HIGH:
  2555. bank->toggle_edge_mode &= ~mask;
  2556. level &= ~mask;
  2557. polarity |= mask;
  2558. break;
  2559. case IRQ_TYPE_LEVEL_LOW:
  2560. bank->toggle_edge_mode &= ~mask;
  2561. level &= ~mask;
  2562. polarity &= ~mask;
  2563. break;
  2564. default:
  2565. irq_gc_unlock(gc);
  2566. raw_spin_unlock_irqrestore(&bank->slock, flags);
  2567. clk_disable(bank->clk);
  2568. return -EINVAL;
  2569. }
  2570. writel_relaxed(level, gc->reg_base + GPIO_INTTYPE_LEVEL);
  2571. writel_relaxed(polarity, gc->reg_base + GPIO_INT_POLARITY);
  2572. irq_gc_unlock(gc);
  2573. raw_spin_unlock_irqrestore(&bank->slock, flags);
  2574. clk_disable(bank->clk);
  2575. return 0;
  2576. }
  2577. static void rockchip_irq_suspend(struct irq_data *d)
  2578. {
  2579. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  2580. struct rockchip_pin_bank *bank = gc->private;
  2581. clk_enable(bank->clk);
  2582. bank->saved_masks = irq_reg_readl(gc, GPIO_INTMASK);
  2583. irq_reg_writel(gc, ~gc->wake_active, GPIO_INTMASK);
  2584. clk_disable(bank->clk);
  2585. }
  2586. static void rockchip_irq_resume(struct irq_data *d)
  2587. {
  2588. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  2589. struct rockchip_pin_bank *bank = gc->private;
  2590. clk_enable(bank->clk);
  2591. irq_reg_writel(gc, bank->saved_masks, GPIO_INTMASK);
  2592. clk_disable(bank->clk);
  2593. }
  2594. static void rockchip_irq_enable(struct irq_data *d)
  2595. {
  2596. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  2597. struct rockchip_pin_bank *bank = gc->private;
  2598. clk_enable(bank->clk);
  2599. irq_gc_mask_clr_bit(d);
  2600. }
  2601. static void rockchip_irq_disable(struct irq_data *d)
  2602. {
  2603. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  2604. struct rockchip_pin_bank *bank = gc->private;
  2605. irq_gc_mask_set_bit(d);
  2606. clk_disable(bank->clk);
  2607. }
  2608. static int rockchip_interrupts_register(struct platform_device *pdev,
  2609. struct rockchip_pinctrl *info)
  2610. {
  2611. struct rockchip_pin_ctrl *ctrl = info->ctrl;
  2612. struct rockchip_pin_bank *bank = ctrl->pin_banks;
  2613. unsigned int clr = IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
  2614. struct irq_chip_generic *gc;
  2615. int ret;
  2616. int i, j;
  2617. for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
  2618. if (!bank->valid) {
  2619. dev_warn(&pdev->dev, "bank %s is not valid\n",
  2620. bank->name);
  2621. continue;
  2622. }
  2623. ret = clk_enable(bank->clk);
  2624. if (ret) {
  2625. dev_err(&pdev->dev, "failed to enable clock for bank %s\n",
  2626. bank->name);
  2627. continue;
  2628. }
  2629. bank->domain = irq_domain_add_linear(bank->of_node, 32,
  2630. &irq_generic_chip_ops, NULL);
  2631. if (!bank->domain) {
  2632. dev_warn(&pdev->dev, "could not initialize irq domain for bank %s\n",
  2633. bank->name);
  2634. clk_disable(bank->clk);
  2635. continue;
  2636. }
  2637. ret = irq_alloc_domain_generic_chips(bank->domain, 32, 1,
  2638. "rockchip_gpio_irq", handle_level_irq,
  2639. clr, 0, IRQ_GC_INIT_MASK_CACHE);
  2640. if (ret) {
  2641. dev_err(&pdev->dev, "could not alloc generic chips for bank %s\n",
  2642. bank->name);
  2643. irq_domain_remove(bank->domain);
  2644. clk_disable(bank->clk);
  2645. continue;
  2646. }
  2647. /*
  2648. * Linux assumes that all interrupts start out disabled/masked.
  2649. * Our driver only uses the concept of masked and always keeps
  2650. * things enabled, so for us that's all masked and all enabled.
  2651. */
  2652. writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTMASK);
  2653. writel_relaxed(0xffffffff, bank->reg_base + GPIO_INTEN);
  2654. gc = irq_get_domain_generic_chip(bank->domain, 0);
  2655. gc->reg_base = bank->reg_base;
  2656. gc->private = bank;
  2657. gc->chip_types[0].regs.mask = GPIO_INTMASK;
  2658. gc->chip_types[0].regs.ack = GPIO_PORTS_EOI;
  2659. gc->chip_types[0].chip.irq_ack = irq_gc_ack_set_bit;
  2660. gc->chip_types[0].chip.irq_mask = irq_gc_mask_set_bit;
  2661. gc->chip_types[0].chip.irq_unmask = irq_gc_mask_clr_bit;
  2662. gc->chip_types[0].chip.irq_enable = rockchip_irq_enable;
  2663. gc->chip_types[0].chip.irq_disable = rockchip_irq_disable;
  2664. gc->chip_types[0].chip.irq_set_wake = irq_gc_set_wake;
  2665. gc->chip_types[0].chip.irq_suspend = rockchip_irq_suspend;
  2666. gc->chip_types[0].chip.irq_resume = rockchip_irq_resume;
  2667. gc->chip_types[0].chip.irq_set_type = rockchip_irq_set_type;
  2668. gc->wake_enabled = IRQ_MSK(bank->nr_pins);
  2669. irq_set_chained_handler_and_data(bank->irq,
  2670. rockchip_irq_demux, bank);
  2671. /* map the gpio irqs here, when the clock is still running */
  2672. for (j = 0 ; j < 32 ; j++)
  2673. irq_create_mapping(bank->domain, j);
  2674. clk_disable(bank->clk);
  2675. }
  2676. return 0;
  2677. }
  2678. static int rockchip_gpiolib_register(struct platform_device *pdev,
  2679. struct rockchip_pinctrl *info)
  2680. {
  2681. struct rockchip_pin_ctrl *ctrl = info->ctrl;
  2682. struct rockchip_pin_bank *bank = ctrl->pin_banks;
  2683. struct gpio_chip *gc;
  2684. int ret;
  2685. int i;
  2686. for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
  2687. if (!bank->valid) {
  2688. dev_warn(&pdev->dev, "bank %s is not valid\n",
  2689. bank->name);
  2690. continue;
  2691. }
  2692. bank->gpio_chip = rockchip_gpiolib_chip;
  2693. gc = &bank->gpio_chip;
  2694. gc->base = bank->pin_base;
  2695. gc->ngpio = bank->nr_pins;
  2696. gc->parent = &pdev->dev;
  2697. gc->of_node = bank->of_node;
  2698. gc->label = bank->name;
  2699. ret = gpiochip_add_data(gc, bank);
  2700. if (ret) {
  2701. dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
  2702. gc->label, ret);
  2703. goto fail;
  2704. }
  2705. }
  2706. rockchip_interrupts_register(pdev, info);
  2707. return 0;
  2708. fail:
  2709. for (--i, --bank; i >= 0; --i, --bank) {
  2710. if (!bank->valid)
  2711. continue;
  2712. gpiochip_remove(&bank->gpio_chip);
  2713. }
  2714. return ret;
  2715. }
  2716. static int rockchip_gpiolib_unregister(struct platform_device *pdev,
  2717. struct rockchip_pinctrl *info)
  2718. {
  2719. struct rockchip_pin_ctrl *ctrl = info->ctrl;
  2720. struct rockchip_pin_bank *bank = ctrl->pin_banks;
  2721. int i;
  2722. for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
  2723. if (!bank->valid)
  2724. continue;
  2725. gpiochip_remove(&bank->gpio_chip);
  2726. }
  2727. return 0;
  2728. }
  2729. static int rockchip_get_bank_data(struct rockchip_pin_bank *bank,
  2730. struct rockchip_pinctrl *info)
  2731. {
  2732. struct resource res;
  2733. void __iomem *base;
  2734. if (of_address_to_resource(bank->of_node, 0, &res)) {
  2735. dev_err(info->dev, "cannot find IO resource for bank\n");
  2736. return -ENOENT;
  2737. }
  2738. bank->reg_base = devm_ioremap_resource(info->dev, &res);
  2739. if (IS_ERR(bank->reg_base))
  2740. return PTR_ERR(bank->reg_base);
  2741. /*
  2742. * special case, where parts of the pull setting-registers are
  2743. * part of the PMU register space
  2744. */
  2745. if (of_device_is_compatible(bank->of_node,
  2746. "rockchip,rk3188-gpio-bank0")) {
  2747. struct device_node *node;
  2748. node = of_parse_phandle(bank->of_node->parent,
  2749. "rockchip,pmu", 0);
  2750. if (!node) {
  2751. if (of_address_to_resource(bank->of_node, 1, &res)) {
  2752. dev_err(info->dev, "cannot find IO resource for bank\n");
  2753. return -ENOENT;
  2754. }
  2755. base = devm_ioremap_resource(info->dev, &res);
  2756. if (IS_ERR(base))
  2757. return PTR_ERR(base);
  2758. rockchip_regmap_config.max_register =
  2759. resource_size(&res) - 4;
  2760. rockchip_regmap_config.name =
  2761. "rockchip,rk3188-gpio-bank0-pull";
  2762. bank->regmap_pull = devm_regmap_init_mmio(info->dev,
  2763. base,
  2764. &rockchip_regmap_config);
  2765. }
  2766. of_node_put(node);
  2767. }
  2768. bank->irq = irq_of_parse_and_map(bank->of_node, 0);
  2769. bank->clk = of_clk_get(bank->of_node, 0);
  2770. if (IS_ERR(bank->clk))
  2771. return PTR_ERR(bank->clk);
  2772. return clk_prepare(bank->clk);
  2773. }
  2774. static const struct of_device_id rockchip_pinctrl_dt_match[];
  2775. /* retrieve the soc specific data */
  2776. static struct rockchip_pin_ctrl *rockchip_pinctrl_get_soc_data(
  2777. struct rockchip_pinctrl *d,
  2778. struct platform_device *pdev)
  2779. {
  2780. const struct of_device_id *match;
  2781. struct device_node *node = pdev->dev.of_node;
  2782. struct device_node *np;
  2783. struct rockchip_pin_ctrl *ctrl;
  2784. struct rockchip_pin_bank *bank;
  2785. int grf_offs, pmu_offs, drv_grf_offs, drv_pmu_offs, i, j;
  2786. match = of_match_node(rockchip_pinctrl_dt_match, node);
  2787. ctrl = (struct rockchip_pin_ctrl *)match->data;
  2788. for_each_child_of_node(node, np) {
  2789. if (!of_find_property(np, "gpio-controller", NULL))
  2790. continue;
  2791. bank = ctrl->pin_banks;
  2792. for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
  2793. if (!strcmp(bank->name, np->name)) {
  2794. bank->of_node = np;
  2795. if (!rockchip_get_bank_data(bank, d))
  2796. bank->valid = true;
  2797. break;
  2798. }
  2799. }
  2800. }
  2801. grf_offs = ctrl->grf_mux_offset;
  2802. pmu_offs = ctrl->pmu_mux_offset;
  2803. drv_pmu_offs = ctrl->pmu_drv_offset;
  2804. drv_grf_offs = ctrl->grf_drv_offset;
  2805. bank = ctrl->pin_banks;
  2806. for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
  2807. int bank_pins = 0;
  2808. raw_spin_lock_init(&bank->slock);
  2809. bank->drvdata = d;
  2810. bank->pin_base = ctrl->nr_pins;
  2811. ctrl->nr_pins += bank->nr_pins;
  2812. /* calculate iomux and drv offsets */
  2813. for (j = 0; j < 4; j++) {
  2814. struct rockchip_iomux *iom = &bank->iomux[j];
  2815. struct rockchip_drv *drv = &bank->drv[j];
  2816. int inc;
  2817. if (bank_pins >= bank->nr_pins)
  2818. break;
  2819. /* preset iomux offset value, set new start value */
  2820. if (iom->offset >= 0) {
  2821. if (iom->type & IOMUX_SOURCE_PMU)
  2822. pmu_offs = iom->offset;
  2823. else
  2824. grf_offs = iom->offset;
  2825. } else { /* set current iomux offset */
  2826. iom->offset = (iom->type & IOMUX_SOURCE_PMU) ?
  2827. pmu_offs : grf_offs;
  2828. }
  2829. /* preset drv offset value, set new start value */
  2830. if (drv->offset >= 0) {
  2831. if (iom->type & IOMUX_SOURCE_PMU)
  2832. drv_pmu_offs = drv->offset;
  2833. else
  2834. drv_grf_offs = drv->offset;
  2835. } else { /* set current drv offset */
  2836. drv->offset = (iom->type & IOMUX_SOURCE_PMU) ?
  2837. drv_pmu_offs : drv_grf_offs;
  2838. }
  2839. dev_dbg(d->dev, "bank %d, iomux %d has iom_offset 0x%x drv_offset 0x%x\n",
  2840. i, j, iom->offset, drv->offset);
  2841. /*
  2842. * Increase offset according to iomux width.
  2843. * 4bit iomux'es are spread over two registers.
  2844. */
  2845. inc = (iom->type & (IOMUX_WIDTH_4BIT |
  2846. IOMUX_WIDTH_3BIT)) ? 8 : 4;
  2847. if (iom->type & IOMUX_SOURCE_PMU)
  2848. pmu_offs += inc;
  2849. else
  2850. grf_offs += inc;
  2851. /*
  2852. * Increase offset according to drv width.
  2853. * 3bit drive-strenth'es are spread over two registers.
  2854. */
  2855. if ((drv->drv_type == DRV_TYPE_IO_1V8_3V0_AUTO) ||
  2856. (drv->drv_type == DRV_TYPE_IO_3V3_ONLY))
  2857. inc = 8;
  2858. else
  2859. inc = 4;
  2860. if (iom->type & IOMUX_SOURCE_PMU)
  2861. drv_pmu_offs += inc;
  2862. else
  2863. drv_grf_offs += inc;
  2864. bank_pins += 8;
  2865. }
  2866. /* calculate the per-bank recalced_mask */
  2867. for (j = 0; j < ctrl->niomux_recalced; j++) {
  2868. int pin = 0;
  2869. if (ctrl->iomux_recalced[j].num == bank->bank_num) {
  2870. pin = ctrl->iomux_recalced[j].pin;
  2871. bank->recalced_mask |= BIT(pin);
  2872. }
  2873. }
  2874. /* calculate the per-bank route_mask */
  2875. for (j = 0; j < ctrl->niomux_routes; j++) {
  2876. int pin = 0;
  2877. if (ctrl->iomux_routes[j].bank_num == bank->bank_num) {
  2878. pin = ctrl->iomux_routes[j].pin;
  2879. bank->route_mask |= BIT(pin);
  2880. }
  2881. }
  2882. }
  2883. return ctrl;
  2884. }
  2885. #define RK3288_GRF_GPIO6C_IOMUX 0x64
  2886. #define GPIO6C6_SEL_WRITE_ENABLE BIT(28)
  2887. static u32 rk3288_grf_gpio6c_iomux;
  2888. static int __maybe_unused rockchip_pinctrl_suspend(struct device *dev)
  2889. {
  2890. struct rockchip_pinctrl *info = dev_get_drvdata(dev);
  2891. int ret = pinctrl_force_sleep(info->pctl_dev);
  2892. if (ret)
  2893. return ret;
  2894. /*
  2895. * RK3288 GPIO6_C6 mux would be modified by Maskrom when resume, so save
  2896. * the setting here, and restore it at resume.
  2897. */
  2898. if (info->ctrl->type == RK3288) {
  2899. ret = regmap_read(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
  2900. &rk3288_grf_gpio6c_iomux);
  2901. if (ret) {
  2902. pinctrl_force_default(info->pctl_dev);
  2903. return ret;
  2904. }
  2905. }
  2906. return 0;
  2907. }
  2908. static int __maybe_unused rockchip_pinctrl_resume(struct device *dev)
  2909. {
  2910. struct rockchip_pinctrl *info = dev_get_drvdata(dev);
  2911. int ret;
  2912. if (info->ctrl->type == RK3288) {
  2913. ret = regmap_write(info->regmap_base, RK3288_GRF_GPIO6C_IOMUX,
  2914. rk3288_grf_gpio6c_iomux |
  2915. GPIO6C6_SEL_WRITE_ENABLE);
  2916. if (ret)
  2917. return ret;
  2918. }
  2919. return pinctrl_force_default(info->pctl_dev);
  2920. }
  2921. static SIMPLE_DEV_PM_OPS(rockchip_pinctrl_dev_pm_ops, rockchip_pinctrl_suspend,
  2922. rockchip_pinctrl_resume);
  2923. static int rockchip_pinctrl_probe(struct platform_device *pdev)
  2924. {
  2925. struct rockchip_pinctrl *info;
  2926. struct device *dev = &pdev->dev;
  2927. struct rockchip_pin_ctrl *ctrl;
  2928. struct device_node *np = pdev->dev.of_node, *node;
  2929. struct resource *res;
  2930. void __iomem *base;
  2931. int ret;
  2932. if (!dev->of_node) {
  2933. dev_err(dev, "device tree node not found\n");
  2934. return -ENODEV;
  2935. }
  2936. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  2937. if (!info)
  2938. return -ENOMEM;
  2939. info->dev = dev;
  2940. ctrl = rockchip_pinctrl_get_soc_data(info, pdev);
  2941. if (!ctrl) {
  2942. dev_err(dev, "driver data not available\n");
  2943. return -EINVAL;
  2944. }
  2945. info->ctrl = ctrl;
  2946. node = of_parse_phandle(np, "rockchip,grf", 0);
  2947. if (node) {
  2948. info->regmap_base = syscon_node_to_regmap(node);
  2949. if (IS_ERR(info->regmap_base))
  2950. return PTR_ERR(info->regmap_base);
  2951. } else {
  2952. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2953. base = devm_ioremap_resource(&pdev->dev, res);
  2954. if (IS_ERR(base))
  2955. return PTR_ERR(base);
  2956. rockchip_regmap_config.max_register = resource_size(res) - 4;
  2957. rockchip_regmap_config.name = "rockchip,pinctrl";
  2958. info->regmap_base = devm_regmap_init_mmio(&pdev->dev, base,
  2959. &rockchip_regmap_config);
  2960. /* to check for the old dt-bindings */
  2961. info->reg_size = resource_size(res);
  2962. /* Honor the old binding, with pull registers as 2nd resource */
  2963. if (ctrl->type == RK3188 && info->reg_size < 0x200) {
  2964. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  2965. base = devm_ioremap_resource(&pdev->dev, res);
  2966. if (IS_ERR(base))
  2967. return PTR_ERR(base);
  2968. rockchip_regmap_config.max_register =
  2969. resource_size(res) - 4;
  2970. rockchip_regmap_config.name = "rockchip,pinctrl-pull";
  2971. info->regmap_pull = devm_regmap_init_mmio(&pdev->dev,
  2972. base,
  2973. &rockchip_regmap_config);
  2974. }
  2975. }
  2976. /* try to find the optional reference to the pmu syscon */
  2977. node = of_parse_phandle(np, "rockchip,pmu", 0);
  2978. if (node) {
  2979. info->regmap_pmu = syscon_node_to_regmap(node);
  2980. if (IS_ERR(info->regmap_pmu))
  2981. return PTR_ERR(info->regmap_pmu);
  2982. }
  2983. ret = rockchip_gpiolib_register(pdev, info);
  2984. if (ret)
  2985. return ret;
  2986. ret = rockchip_pinctrl_register(pdev, info);
  2987. if (ret) {
  2988. rockchip_gpiolib_unregister(pdev, info);
  2989. return ret;
  2990. }
  2991. platform_set_drvdata(pdev, info);
  2992. return 0;
  2993. }
  2994. static struct rockchip_pin_bank px30_pin_banks[] = {
  2995. PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
  2996. IOMUX_SOURCE_PMU,
  2997. IOMUX_SOURCE_PMU,
  2998. IOMUX_SOURCE_PMU
  2999. ),
  3000. PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_WIDTH_4BIT,
  3001. IOMUX_WIDTH_4BIT,
  3002. IOMUX_WIDTH_4BIT,
  3003. IOMUX_WIDTH_4BIT
  3004. ),
  3005. PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", IOMUX_WIDTH_4BIT,
  3006. IOMUX_WIDTH_4BIT,
  3007. IOMUX_WIDTH_4BIT,
  3008. IOMUX_WIDTH_4BIT
  3009. ),
  3010. PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", IOMUX_WIDTH_4BIT,
  3011. IOMUX_WIDTH_4BIT,
  3012. IOMUX_WIDTH_4BIT,
  3013. IOMUX_WIDTH_4BIT
  3014. ),
  3015. };
  3016. static struct rockchip_pin_ctrl px30_pin_ctrl = {
  3017. .pin_banks = px30_pin_banks,
  3018. .nr_banks = ARRAY_SIZE(px30_pin_banks),
  3019. .label = "PX30-GPIO",
  3020. .type = PX30,
  3021. .grf_mux_offset = 0x0,
  3022. .pmu_mux_offset = 0x0,
  3023. .iomux_routes = px30_mux_route_data,
  3024. .niomux_routes = ARRAY_SIZE(px30_mux_route_data),
  3025. .pull_calc_reg = px30_calc_pull_reg_and_bit,
  3026. .drv_calc_reg = px30_calc_drv_reg_and_bit,
  3027. .schmitt_calc_reg = px30_calc_schmitt_reg_and_bit,
  3028. };
  3029. static struct rockchip_pin_bank rv1108_pin_banks[] = {
  3030. PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
  3031. IOMUX_SOURCE_PMU,
  3032. IOMUX_SOURCE_PMU,
  3033. IOMUX_SOURCE_PMU),
  3034. PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
  3035. PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, 0),
  3036. PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, 0),
  3037. };
  3038. static struct rockchip_pin_ctrl rv1108_pin_ctrl = {
  3039. .pin_banks = rv1108_pin_banks,
  3040. .nr_banks = ARRAY_SIZE(rv1108_pin_banks),
  3041. .label = "RV1108-GPIO",
  3042. .type = RV1108,
  3043. .grf_mux_offset = 0x10,
  3044. .pmu_mux_offset = 0x0,
  3045. .iomux_recalced = rv1108_mux_recalced_data,
  3046. .niomux_recalced = ARRAY_SIZE(rv1108_mux_recalced_data),
  3047. .pull_calc_reg = rv1108_calc_pull_reg_and_bit,
  3048. .drv_calc_reg = rv1108_calc_drv_reg_and_bit,
  3049. .schmitt_calc_reg = rv1108_calc_schmitt_reg_and_bit,
  3050. };
  3051. static struct rockchip_pin_bank rk2928_pin_banks[] = {
  3052. PIN_BANK(0, 32, "gpio0"),
  3053. PIN_BANK(1, 32, "gpio1"),
  3054. PIN_BANK(2, 32, "gpio2"),
  3055. PIN_BANK(3, 32, "gpio3"),
  3056. };
  3057. static struct rockchip_pin_ctrl rk2928_pin_ctrl = {
  3058. .pin_banks = rk2928_pin_banks,
  3059. .nr_banks = ARRAY_SIZE(rk2928_pin_banks),
  3060. .label = "RK2928-GPIO",
  3061. .type = RK2928,
  3062. .grf_mux_offset = 0xa8,
  3063. .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
  3064. };
  3065. static struct rockchip_pin_bank rk3036_pin_banks[] = {
  3066. PIN_BANK(0, 32, "gpio0"),
  3067. PIN_BANK(1, 32, "gpio1"),
  3068. PIN_BANK(2, 32, "gpio2"),
  3069. };
  3070. static struct rockchip_pin_ctrl rk3036_pin_ctrl = {
  3071. .pin_banks = rk3036_pin_banks,
  3072. .nr_banks = ARRAY_SIZE(rk3036_pin_banks),
  3073. .label = "RK3036-GPIO",
  3074. .type = RK2928,
  3075. .grf_mux_offset = 0xa8,
  3076. .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
  3077. };
  3078. static struct rockchip_pin_bank rk3066a_pin_banks[] = {
  3079. PIN_BANK(0, 32, "gpio0"),
  3080. PIN_BANK(1, 32, "gpio1"),
  3081. PIN_BANK(2, 32, "gpio2"),
  3082. PIN_BANK(3, 32, "gpio3"),
  3083. PIN_BANK(4, 32, "gpio4"),
  3084. PIN_BANK(6, 16, "gpio6"),
  3085. };
  3086. static struct rockchip_pin_ctrl rk3066a_pin_ctrl = {
  3087. .pin_banks = rk3066a_pin_banks,
  3088. .nr_banks = ARRAY_SIZE(rk3066a_pin_banks),
  3089. .label = "RK3066a-GPIO",
  3090. .type = RK2928,
  3091. .grf_mux_offset = 0xa8,
  3092. .pull_calc_reg = rk2928_calc_pull_reg_and_bit,
  3093. };
  3094. static struct rockchip_pin_bank rk3066b_pin_banks[] = {
  3095. PIN_BANK(0, 32, "gpio0"),
  3096. PIN_BANK(1, 32, "gpio1"),
  3097. PIN_BANK(2, 32, "gpio2"),
  3098. PIN_BANK(3, 32, "gpio3"),
  3099. };
  3100. static struct rockchip_pin_ctrl rk3066b_pin_ctrl = {
  3101. .pin_banks = rk3066b_pin_banks,
  3102. .nr_banks = ARRAY_SIZE(rk3066b_pin_banks),
  3103. .label = "RK3066b-GPIO",
  3104. .type = RK3066B,
  3105. .grf_mux_offset = 0x60,
  3106. };
  3107. static struct rockchip_pin_bank rk3128_pin_banks[] = {
  3108. PIN_BANK(0, 32, "gpio0"),
  3109. PIN_BANK(1, 32, "gpio1"),
  3110. PIN_BANK(2, 32, "gpio2"),
  3111. PIN_BANK(3, 32, "gpio3"),
  3112. };
  3113. static struct rockchip_pin_ctrl rk3128_pin_ctrl = {
  3114. .pin_banks = rk3128_pin_banks,
  3115. .nr_banks = ARRAY_SIZE(rk3128_pin_banks),
  3116. .label = "RK3128-GPIO",
  3117. .type = RK3128,
  3118. .grf_mux_offset = 0xa8,
  3119. .iomux_recalced = rk3128_mux_recalced_data,
  3120. .niomux_recalced = ARRAY_SIZE(rk3128_mux_recalced_data),
  3121. .iomux_routes = rk3128_mux_route_data,
  3122. .niomux_routes = ARRAY_SIZE(rk3128_mux_route_data),
  3123. .pull_calc_reg = rk3128_calc_pull_reg_and_bit,
  3124. };
  3125. static struct rockchip_pin_bank rk3188_pin_banks[] = {
  3126. PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_GPIO_ONLY, 0, 0, 0),
  3127. PIN_BANK(1, 32, "gpio1"),
  3128. PIN_BANK(2, 32, "gpio2"),
  3129. PIN_BANK(3, 32, "gpio3"),
  3130. };
  3131. static struct rockchip_pin_ctrl rk3188_pin_ctrl = {
  3132. .pin_banks = rk3188_pin_banks,
  3133. .nr_banks = ARRAY_SIZE(rk3188_pin_banks),
  3134. .label = "RK3188-GPIO",
  3135. .type = RK3188,
  3136. .grf_mux_offset = 0x60,
  3137. .pull_calc_reg = rk3188_calc_pull_reg_and_bit,
  3138. };
  3139. static struct rockchip_pin_bank rk3228_pin_banks[] = {
  3140. PIN_BANK(0, 32, "gpio0"),
  3141. PIN_BANK(1, 32, "gpio1"),
  3142. PIN_BANK(2, 32, "gpio2"),
  3143. PIN_BANK(3, 32, "gpio3"),
  3144. };
  3145. static struct rockchip_pin_ctrl rk3228_pin_ctrl = {
  3146. .pin_banks = rk3228_pin_banks,
  3147. .nr_banks = ARRAY_SIZE(rk3228_pin_banks),
  3148. .label = "RK3228-GPIO",
  3149. .type = RK3288,
  3150. .grf_mux_offset = 0x0,
  3151. .iomux_routes = rk3228_mux_route_data,
  3152. .niomux_routes = ARRAY_SIZE(rk3228_mux_route_data),
  3153. .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
  3154. .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
  3155. };
  3156. static struct rockchip_pin_bank rk3288_pin_banks[] = {
  3157. PIN_BANK_IOMUX_FLAGS(0, 24, "gpio0", IOMUX_SOURCE_PMU,
  3158. IOMUX_SOURCE_PMU,
  3159. IOMUX_SOURCE_PMU,
  3160. IOMUX_UNROUTED
  3161. ),
  3162. PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", IOMUX_UNROUTED,
  3163. IOMUX_UNROUTED,
  3164. IOMUX_UNROUTED,
  3165. 0
  3166. ),
  3167. PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0, 0, 0, IOMUX_UNROUTED),
  3168. PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3", 0, 0, 0, IOMUX_WIDTH_4BIT),
  3169. PIN_BANK_IOMUX_FLAGS(4, 32, "gpio4", IOMUX_WIDTH_4BIT,
  3170. IOMUX_WIDTH_4BIT,
  3171. 0,
  3172. 0
  3173. ),
  3174. PIN_BANK_IOMUX_FLAGS(5, 32, "gpio5", IOMUX_UNROUTED,
  3175. 0,
  3176. 0,
  3177. IOMUX_UNROUTED
  3178. ),
  3179. PIN_BANK_IOMUX_FLAGS(6, 32, "gpio6", 0, 0, 0, IOMUX_UNROUTED),
  3180. PIN_BANK_IOMUX_FLAGS(7, 32, "gpio7", 0,
  3181. 0,
  3182. IOMUX_WIDTH_4BIT,
  3183. IOMUX_UNROUTED
  3184. ),
  3185. PIN_BANK(8, 16, "gpio8"),
  3186. };
  3187. static struct rockchip_pin_ctrl rk3288_pin_ctrl = {
  3188. .pin_banks = rk3288_pin_banks,
  3189. .nr_banks = ARRAY_SIZE(rk3288_pin_banks),
  3190. .label = "RK3288-GPIO",
  3191. .type = RK3288,
  3192. .grf_mux_offset = 0x0,
  3193. .pmu_mux_offset = 0x84,
  3194. .iomux_routes = rk3288_mux_route_data,
  3195. .niomux_routes = ARRAY_SIZE(rk3288_mux_route_data),
  3196. .pull_calc_reg = rk3288_calc_pull_reg_and_bit,
  3197. .drv_calc_reg = rk3288_calc_drv_reg_and_bit,
  3198. };
  3199. static struct rockchip_pin_bank rk3328_pin_banks[] = {
  3200. PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", 0, 0, 0, 0),
  3201. PIN_BANK_IOMUX_FLAGS(1, 32, "gpio1", 0, 0, 0, 0),
  3202. PIN_BANK_IOMUX_FLAGS(2, 32, "gpio2", 0,
  3203. IOMUX_WIDTH_3BIT,
  3204. IOMUX_WIDTH_3BIT,
  3205. 0),
  3206. PIN_BANK_IOMUX_FLAGS(3, 32, "gpio3",
  3207. IOMUX_WIDTH_3BIT,
  3208. IOMUX_WIDTH_3BIT,
  3209. 0,
  3210. 0),
  3211. };
  3212. static struct rockchip_pin_ctrl rk3328_pin_ctrl = {
  3213. .pin_banks = rk3328_pin_banks,
  3214. .nr_banks = ARRAY_SIZE(rk3328_pin_banks),
  3215. .label = "RK3328-GPIO",
  3216. .type = RK3288,
  3217. .grf_mux_offset = 0x0,
  3218. .iomux_recalced = rk3328_mux_recalced_data,
  3219. .niomux_recalced = ARRAY_SIZE(rk3328_mux_recalced_data),
  3220. .iomux_routes = rk3328_mux_route_data,
  3221. .niomux_routes = ARRAY_SIZE(rk3328_mux_route_data),
  3222. .pull_calc_reg = rk3228_calc_pull_reg_and_bit,
  3223. .drv_calc_reg = rk3228_calc_drv_reg_and_bit,
  3224. .schmitt_calc_reg = rk3328_calc_schmitt_reg_and_bit,
  3225. };
  3226. static struct rockchip_pin_bank rk3368_pin_banks[] = {
  3227. PIN_BANK_IOMUX_FLAGS(0, 32, "gpio0", IOMUX_SOURCE_PMU,
  3228. IOMUX_SOURCE_PMU,
  3229. IOMUX_SOURCE_PMU,
  3230. IOMUX_SOURCE_PMU
  3231. ),
  3232. PIN_BANK(1, 32, "gpio1"),
  3233. PIN_BANK(2, 32, "gpio2"),
  3234. PIN_BANK(3, 32, "gpio3"),
  3235. };
  3236. static struct rockchip_pin_ctrl rk3368_pin_ctrl = {
  3237. .pin_banks = rk3368_pin_banks,
  3238. .nr_banks = ARRAY_SIZE(rk3368_pin_banks),
  3239. .label = "RK3368-GPIO",
  3240. .type = RK3368,
  3241. .grf_mux_offset = 0x0,
  3242. .pmu_mux_offset = 0x0,
  3243. .pull_calc_reg = rk3368_calc_pull_reg_and_bit,
  3244. .drv_calc_reg = rk3368_calc_drv_reg_and_bit,
  3245. };
  3246. static struct rockchip_pin_bank rk3399_pin_banks[] = {
  3247. PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(0, 32, "gpio0",
  3248. IOMUX_SOURCE_PMU,
  3249. IOMUX_SOURCE_PMU,
  3250. IOMUX_SOURCE_PMU,
  3251. IOMUX_SOURCE_PMU,
  3252. DRV_TYPE_IO_1V8_ONLY,
  3253. DRV_TYPE_IO_1V8_ONLY,
  3254. DRV_TYPE_IO_DEFAULT,
  3255. DRV_TYPE_IO_DEFAULT,
  3256. 0x80,
  3257. 0x88,
  3258. -1,
  3259. -1,
  3260. PULL_TYPE_IO_1V8_ONLY,
  3261. PULL_TYPE_IO_1V8_ONLY,
  3262. PULL_TYPE_IO_DEFAULT,
  3263. PULL_TYPE_IO_DEFAULT
  3264. ),
  3265. PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(1, 32, "gpio1", IOMUX_SOURCE_PMU,
  3266. IOMUX_SOURCE_PMU,
  3267. IOMUX_SOURCE_PMU,
  3268. IOMUX_SOURCE_PMU,
  3269. DRV_TYPE_IO_1V8_OR_3V0,
  3270. DRV_TYPE_IO_1V8_OR_3V0,
  3271. DRV_TYPE_IO_1V8_OR_3V0,
  3272. DRV_TYPE_IO_1V8_OR_3V0,
  3273. 0xa0,
  3274. 0xa8,
  3275. 0xb0,
  3276. 0xb8
  3277. ),
  3278. PIN_BANK_DRV_FLAGS_PULL_FLAGS(2, 32, "gpio2", DRV_TYPE_IO_1V8_OR_3V0,
  3279. DRV_TYPE_IO_1V8_OR_3V0,
  3280. DRV_TYPE_IO_1V8_ONLY,
  3281. DRV_TYPE_IO_1V8_ONLY,
  3282. PULL_TYPE_IO_DEFAULT,
  3283. PULL_TYPE_IO_DEFAULT,
  3284. PULL_TYPE_IO_1V8_ONLY,
  3285. PULL_TYPE_IO_1V8_ONLY
  3286. ),
  3287. PIN_BANK_DRV_FLAGS(3, 32, "gpio3", DRV_TYPE_IO_3V3_ONLY,
  3288. DRV_TYPE_IO_3V3_ONLY,
  3289. DRV_TYPE_IO_3V3_ONLY,
  3290. DRV_TYPE_IO_1V8_OR_3V0
  3291. ),
  3292. PIN_BANK_DRV_FLAGS(4, 32, "gpio4", DRV_TYPE_IO_1V8_OR_3V0,
  3293. DRV_TYPE_IO_1V8_3V0_AUTO,
  3294. DRV_TYPE_IO_1V8_OR_3V0,
  3295. DRV_TYPE_IO_1V8_OR_3V0
  3296. ),
  3297. };
  3298. static struct rockchip_pin_ctrl rk3399_pin_ctrl = {
  3299. .pin_banks = rk3399_pin_banks,
  3300. .nr_banks = ARRAY_SIZE(rk3399_pin_banks),
  3301. .label = "RK3399-GPIO",
  3302. .type = RK3399,
  3303. .grf_mux_offset = 0xe000,
  3304. .pmu_mux_offset = 0x0,
  3305. .grf_drv_offset = 0xe100,
  3306. .pmu_drv_offset = 0x80,
  3307. .iomux_routes = rk3399_mux_route_data,
  3308. .niomux_routes = ARRAY_SIZE(rk3399_mux_route_data),
  3309. .pull_calc_reg = rk3399_calc_pull_reg_and_bit,
  3310. .drv_calc_reg = rk3399_calc_drv_reg_and_bit,
  3311. };
  3312. static const struct of_device_id rockchip_pinctrl_dt_match[] = {
  3313. { .compatible = "rockchip,px30-pinctrl",
  3314. .data = &px30_pin_ctrl },
  3315. { .compatible = "rockchip,rv1108-pinctrl",
  3316. .data = &rv1108_pin_ctrl },
  3317. { .compatible = "rockchip,rk2928-pinctrl",
  3318. .data = &rk2928_pin_ctrl },
  3319. { .compatible = "rockchip,rk3036-pinctrl",
  3320. .data = &rk3036_pin_ctrl },
  3321. { .compatible = "rockchip,rk3066a-pinctrl",
  3322. .data = &rk3066a_pin_ctrl },
  3323. { .compatible = "rockchip,rk3066b-pinctrl",
  3324. .data = &rk3066b_pin_ctrl },
  3325. { .compatible = "rockchip,rk3128-pinctrl",
  3326. .data = (void *)&rk3128_pin_ctrl },
  3327. { .compatible = "rockchip,rk3188-pinctrl",
  3328. .data = &rk3188_pin_ctrl },
  3329. { .compatible = "rockchip,rk3228-pinctrl",
  3330. .data = &rk3228_pin_ctrl },
  3331. { .compatible = "rockchip,rk3288-pinctrl",
  3332. .data = &rk3288_pin_ctrl },
  3333. { .compatible = "rockchip,rk3328-pinctrl",
  3334. .data = &rk3328_pin_ctrl },
  3335. { .compatible = "rockchip,rk3368-pinctrl",
  3336. .data = &rk3368_pin_ctrl },
  3337. { .compatible = "rockchip,rk3399-pinctrl",
  3338. .data = &rk3399_pin_ctrl },
  3339. {},
  3340. };
  3341. static struct platform_driver rockchip_pinctrl_driver = {
  3342. .probe = rockchip_pinctrl_probe,
  3343. .driver = {
  3344. .name = "rockchip-pinctrl",
  3345. .pm = &rockchip_pinctrl_dev_pm_ops,
  3346. .of_match_table = rockchip_pinctrl_dt_match,
  3347. },
  3348. };
  3349. static int __init rockchip_pinctrl_drv_register(void)
  3350. {
  3351. return platform_driver_register(&rockchip_pinctrl_driver);
  3352. }
  3353. postcore_initcall(rockchip_pinctrl_drv_register);