pinctrl-rockchip.c 70 KB

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