pinctrl-rockchip.c 78 KB

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