pinctrl-rockchip.c 94 KB

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