pinctrl-rockchip.c 72 KB

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