pinctrl-rockchip.c 88 KB

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