pinctrl-nomadik.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280
  1. /*
  2. * Generic GPIO driver for logic cells found in the Nomadik SoC
  3. *
  4. * Copyright (C) 2008,2009 STMicroelectronics
  5. * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it>
  6. * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com>
  7. * Copyright (C) 2011 Linus Walleij <linus.walleij@linaro.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/io.h>
  19. #include <linux/clk.h>
  20. #include <linux/err.h>
  21. #include <linux/gpio.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/irqdomain.h>
  26. #include <linux/slab.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_address.h>
  29. #include <linux/pinctrl/machine.h>
  30. #include <linux/pinctrl/pinctrl.h>
  31. #include <linux/pinctrl/pinmux.h>
  32. #include <linux/pinctrl/pinconf.h>
  33. /* Since we request GPIOs from ourself */
  34. #include <linux/pinctrl/consumer.h>
  35. #include <linux/platform_data/pinctrl-nomadik.h>
  36. #include <asm/mach/irq.h>
  37. #include "pinctrl-nomadik.h"
  38. #include "core.h"
  39. /*
  40. * The GPIO module in the Nomadik family of Systems-on-Chip is an
  41. * AMBA device, managing 32 pins and alternate functions. The logic block
  42. * is currently used in the Nomadik and ux500.
  43. *
  44. * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
  45. */
  46. struct nmk_gpio_chip {
  47. struct gpio_chip chip;
  48. struct irq_domain *domain;
  49. void __iomem *addr;
  50. struct clk *clk;
  51. unsigned int bank;
  52. unsigned int parent_irq;
  53. int secondary_parent_irq;
  54. u32 (*get_secondary_status)(unsigned int bank);
  55. void (*set_ioforce)(bool enable);
  56. spinlock_t lock;
  57. bool sleepmode;
  58. /* Keep track of configured edges */
  59. u32 edge_rising;
  60. u32 edge_falling;
  61. u32 real_wake;
  62. u32 rwimsc;
  63. u32 fwimsc;
  64. u32 rimsc;
  65. u32 fimsc;
  66. u32 pull_up;
  67. u32 lowemi;
  68. };
  69. /**
  70. * struct nmk_pinctrl - state container for the Nomadik pin controller
  71. * @dev: containing device pointer
  72. * @pctl: corresponding pin controller device
  73. * @soc: SoC data for this specific chip
  74. * @prcm_base: PRCM register range virtual base
  75. */
  76. struct nmk_pinctrl {
  77. struct device *dev;
  78. struct pinctrl_dev *pctl;
  79. const struct nmk_pinctrl_soc_data *soc;
  80. void __iomem *prcm_base;
  81. };
  82. static struct nmk_gpio_chip *
  83. nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
  84. static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
  85. #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
  86. static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
  87. unsigned offset, int gpio_mode)
  88. {
  89. u32 bit = 1 << offset;
  90. u32 afunc, bfunc;
  91. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
  92. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
  93. if (gpio_mode & NMK_GPIO_ALT_A)
  94. afunc |= bit;
  95. if (gpio_mode & NMK_GPIO_ALT_B)
  96. bfunc |= bit;
  97. writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
  98. writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
  99. }
  100. static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
  101. unsigned offset, enum nmk_gpio_slpm mode)
  102. {
  103. u32 bit = 1 << offset;
  104. u32 slpm;
  105. slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
  106. if (mode == NMK_GPIO_SLPM_NOCHANGE)
  107. slpm |= bit;
  108. else
  109. slpm &= ~bit;
  110. writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
  111. }
  112. static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
  113. unsigned offset, enum nmk_gpio_pull pull)
  114. {
  115. u32 bit = 1 << offset;
  116. u32 pdis;
  117. pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
  118. if (pull == NMK_GPIO_PULL_NONE) {
  119. pdis |= bit;
  120. nmk_chip->pull_up &= ~bit;
  121. } else {
  122. pdis &= ~bit;
  123. }
  124. writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
  125. if (pull == NMK_GPIO_PULL_UP) {
  126. nmk_chip->pull_up |= bit;
  127. writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
  128. } else if (pull == NMK_GPIO_PULL_DOWN) {
  129. nmk_chip->pull_up &= ~bit;
  130. writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
  131. }
  132. }
  133. static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
  134. unsigned offset, bool lowemi)
  135. {
  136. u32 bit = BIT(offset);
  137. bool enabled = nmk_chip->lowemi & bit;
  138. if (lowemi == enabled)
  139. return;
  140. if (lowemi)
  141. nmk_chip->lowemi |= bit;
  142. else
  143. nmk_chip->lowemi &= ~bit;
  144. writel_relaxed(nmk_chip->lowemi,
  145. nmk_chip->addr + NMK_GPIO_LOWEMI);
  146. }
  147. static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
  148. unsigned offset)
  149. {
  150. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  151. }
  152. static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
  153. unsigned offset, int val)
  154. {
  155. if (val)
  156. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
  157. else
  158. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
  159. }
  160. static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
  161. unsigned offset, int val)
  162. {
  163. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
  164. __nmk_gpio_set_output(nmk_chip, offset, val);
  165. }
  166. static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
  167. unsigned offset, int gpio_mode,
  168. bool glitch)
  169. {
  170. u32 rwimsc = nmk_chip->rwimsc;
  171. u32 fwimsc = nmk_chip->fwimsc;
  172. if (glitch && nmk_chip->set_ioforce) {
  173. u32 bit = BIT(offset);
  174. /* Prevent spurious wakeups */
  175. writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
  176. writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
  177. nmk_chip->set_ioforce(true);
  178. }
  179. __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
  180. if (glitch && nmk_chip->set_ioforce) {
  181. nmk_chip->set_ioforce(false);
  182. writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
  183. writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
  184. }
  185. }
  186. static void
  187. nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
  188. {
  189. u32 falling = nmk_chip->fimsc & BIT(offset);
  190. u32 rising = nmk_chip->rimsc & BIT(offset);
  191. int gpio = nmk_chip->chip.base + offset;
  192. int irq = irq_find_mapping(nmk_chip->domain, offset);
  193. struct irq_data *d = irq_get_irq_data(irq);
  194. if (!rising && !falling)
  195. return;
  196. if (!d || !irqd_irq_disabled(d))
  197. return;
  198. if (rising) {
  199. nmk_chip->rimsc &= ~BIT(offset);
  200. writel_relaxed(nmk_chip->rimsc,
  201. nmk_chip->addr + NMK_GPIO_RIMSC);
  202. }
  203. if (falling) {
  204. nmk_chip->fimsc &= ~BIT(offset);
  205. writel_relaxed(nmk_chip->fimsc,
  206. nmk_chip->addr + NMK_GPIO_FIMSC);
  207. }
  208. dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
  209. }
  210. static void nmk_write_masked(void __iomem *reg, u32 mask, u32 value)
  211. {
  212. u32 val;
  213. val = readl(reg);
  214. val = ((val & ~mask) | (value & mask));
  215. writel(val, reg);
  216. }
  217. static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
  218. unsigned offset, unsigned alt_num)
  219. {
  220. int i;
  221. u16 reg;
  222. u8 bit;
  223. u8 alt_index;
  224. const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
  225. const u16 *gpiocr_regs;
  226. if (!npct->prcm_base)
  227. return;
  228. if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
  229. dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
  230. alt_num);
  231. return;
  232. }
  233. for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
  234. if (npct->soc->altcx_pins[i].pin == offset)
  235. break;
  236. }
  237. if (i == npct->soc->npins_altcx) {
  238. dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
  239. offset);
  240. return;
  241. }
  242. pin_desc = npct->soc->altcx_pins + i;
  243. gpiocr_regs = npct->soc->prcm_gpiocr_registers;
  244. /*
  245. * If alt_num is NULL, just clear current ALTCx selection
  246. * to make sure we come back to a pure ALTC selection
  247. */
  248. if (!alt_num) {
  249. for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
  250. if (pin_desc->altcx[i].used == true) {
  251. reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
  252. bit = pin_desc->altcx[i].control_bit;
  253. if (readl(npct->prcm_base + reg) & BIT(bit)) {
  254. nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
  255. dev_dbg(npct->dev,
  256. "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
  257. offset, i+1);
  258. }
  259. }
  260. }
  261. return;
  262. }
  263. alt_index = alt_num - 1;
  264. if (pin_desc->altcx[alt_index].used == false) {
  265. dev_warn(npct->dev,
  266. "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
  267. offset, alt_num);
  268. return;
  269. }
  270. /*
  271. * Check if any other ALTCx functions are activated on this pin
  272. * and disable it first.
  273. */
  274. for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
  275. if (i == alt_index)
  276. continue;
  277. if (pin_desc->altcx[i].used == true) {
  278. reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
  279. bit = pin_desc->altcx[i].control_bit;
  280. if (readl(npct->prcm_base + reg) & BIT(bit)) {
  281. nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
  282. dev_dbg(npct->dev,
  283. "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
  284. offset, i+1);
  285. }
  286. }
  287. }
  288. reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
  289. bit = pin_desc->altcx[alt_index].control_bit;
  290. dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
  291. offset, alt_index+1);
  292. nmk_write_masked(npct->prcm_base + reg, BIT(bit), BIT(bit));
  293. }
  294. static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
  295. pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
  296. {
  297. static const char *afnames[] = {
  298. [NMK_GPIO_ALT_GPIO] = "GPIO",
  299. [NMK_GPIO_ALT_A] = "A",
  300. [NMK_GPIO_ALT_B] = "B",
  301. [NMK_GPIO_ALT_C] = "C"
  302. };
  303. static const char *pullnames[] = {
  304. [NMK_GPIO_PULL_NONE] = "none",
  305. [NMK_GPIO_PULL_UP] = "up",
  306. [NMK_GPIO_PULL_DOWN] = "down",
  307. [3] /* illegal */ = "??"
  308. };
  309. static const char *slpmnames[] = {
  310. [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
  311. [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
  312. };
  313. int pin = PIN_NUM(cfg);
  314. int pull = PIN_PULL(cfg);
  315. int af = PIN_ALT(cfg);
  316. int slpm = PIN_SLPM(cfg);
  317. int output = PIN_DIR(cfg);
  318. int val = PIN_VAL(cfg);
  319. bool glitch = af == NMK_GPIO_ALT_C;
  320. dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
  321. pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
  322. output ? "output " : "input",
  323. output ? (val ? "high" : "low") : "");
  324. if (sleep) {
  325. int slpm_pull = PIN_SLPM_PULL(cfg);
  326. int slpm_output = PIN_SLPM_DIR(cfg);
  327. int slpm_val = PIN_SLPM_VAL(cfg);
  328. af = NMK_GPIO_ALT_GPIO;
  329. /*
  330. * The SLPM_* values are normal values + 1 to allow zero to
  331. * mean "same as normal".
  332. */
  333. if (slpm_pull)
  334. pull = slpm_pull - 1;
  335. if (slpm_output)
  336. output = slpm_output - 1;
  337. if (slpm_val)
  338. val = slpm_val - 1;
  339. dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
  340. pin,
  341. slpm_pull ? pullnames[pull] : "same",
  342. slpm_output ? (output ? "output" : "input") : "same",
  343. slpm_val ? (val ? "high" : "low") : "same");
  344. }
  345. if (output)
  346. __nmk_gpio_make_output(nmk_chip, offset, val);
  347. else {
  348. __nmk_gpio_make_input(nmk_chip, offset);
  349. __nmk_gpio_set_pull(nmk_chip, offset, pull);
  350. }
  351. __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
  352. /*
  353. * If the pin is switching to altfunc, and there was an interrupt
  354. * installed on it which has been lazy disabled, actually mask the
  355. * interrupt to prevent spurious interrupts that would occur while the
  356. * pin is under control of the peripheral. Only SKE does this.
  357. */
  358. if (af != NMK_GPIO_ALT_GPIO)
  359. nmk_gpio_disable_lazy_irq(nmk_chip, offset);
  360. /*
  361. * If we've backed up the SLPM registers (glitch workaround), modify
  362. * the backups since they will be restored.
  363. */
  364. if (slpmregs) {
  365. if (slpm == NMK_GPIO_SLPM_NOCHANGE)
  366. slpmregs[nmk_chip->bank] |= BIT(offset);
  367. else
  368. slpmregs[nmk_chip->bank] &= ~BIT(offset);
  369. } else
  370. __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
  371. __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
  372. }
  373. /*
  374. * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
  375. * - Save SLPM registers
  376. * - Set SLPM=0 for the IOs you want to switch and others to 1
  377. * - Configure the GPIO registers for the IOs that are being switched
  378. * - Set IOFORCE=1
  379. * - Modify the AFLSA/B registers for the IOs that are being switched
  380. * - Set IOFORCE=0
  381. * - Restore SLPM registers
  382. * - Any spurious wake up event during switch sequence to be ignored and
  383. * cleared
  384. */
  385. static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
  386. {
  387. int i;
  388. for (i = 0; i < NUM_BANKS; i++) {
  389. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  390. unsigned int temp = slpm[i];
  391. if (!chip)
  392. break;
  393. clk_enable(chip->clk);
  394. slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
  395. writel(temp, chip->addr + NMK_GPIO_SLPC);
  396. }
  397. }
  398. static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
  399. {
  400. int i;
  401. for (i = 0; i < NUM_BANKS; i++) {
  402. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  403. if (!chip)
  404. break;
  405. writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
  406. clk_disable(chip->clk);
  407. }
  408. }
  409. static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
  410. {
  411. static unsigned int slpm[NUM_BANKS];
  412. unsigned long flags;
  413. bool glitch = false;
  414. int ret = 0;
  415. int i;
  416. for (i = 0; i < num; i++) {
  417. if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
  418. glitch = true;
  419. break;
  420. }
  421. }
  422. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  423. if (glitch) {
  424. memset(slpm, 0xff, sizeof(slpm));
  425. for (i = 0; i < num; i++) {
  426. int pin = PIN_NUM(cfgs[i]);
  427. int offset = pin % NMK_GPIO_PER_CHIP;
  428. if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
  429. slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
  430. }
  431. nmk_gpio_glitch_slpm_init(slpm);
  432. }
  433. for (i = 0; i < num; i++) {
  434. struct nmk_gpio_chip *nmk_chip;
  435. int pin = PIN_NUM(cfgs[i]);
  436. nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
  437. if (!nmk_chip) {
  438. ret = -EINVAL;
  439. break;
  440. }
  441. clk_enable(nmk_chip->clk);
  442. spin_lock(&nmk_chip->lock);
  443. __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
  444. cfgs[i], sleep, glitch ? slpm : NULL);
  445. spin_unlock(&nmk_chip->lock);
  446. clk_disable(nmk_chip->clk);
  447. }
  448. if (glitch)
  449. nmk_gpio_glitch_slpm_restore(slpm);
  450. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  451. return ret;
  452. }
  453. /**
  454. * nmk_config_pin - configure a pin's mux attributes
  455. * @cfg: pin confguration
  456. * @sleep: Non-zero to apply the sleep mode configuration
  457. * Configures a pin's mode (alternate function or GPIO), its pull up status,
  458. * and its sleep mode based on the specified configuration. The @cfg is
  459. * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
  460. * are constructed using, and can be further enhanced with, the macros in
  461. * <linux/platform_data/pinctrl-nomadik.h>
  462. *
  463. * If a pin's mode is set to GPIO, it is configured as an input to avoid
  464. * side-effects. The gpio can be manipulated later using standard GPIO API
  465. * calls.
  466. */
  467. int nmk_config_pin(pin_cfg_t cfg, bool sleep)
  468. {
  469. return __nmk_config_pins(&cfg, 1, sleep);
  470. }
  471. EXPORT_SYMBOL(nmk_config_pin);
  472. /**
  473. * nmk_config_pins - configure several pins at once
  474. * @cfgs: array of pin configurations
  475. * @num: number of elments in the array
  476. *
  477. * Configures several pins using nmk_config_pin(). Refer to that function for
  478. * further information.
  479. */
  480. int nmk_config_pins(pin_cfg_t *cfgs, int num)
  481. {
  482. return __nmk_config_pins(cfgs, num, false);
  483. }
  484. EXPORT_SYMBOL(nmk_config_pins);
  485. int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
  486. {
  487. return __nmk_config_pins(cfgs, num, true);
  488. }
  489. EXPORT_SYMBOL(nmk_config_pins_sleep);
  490. /**
  491. * nmk_gpio_set_slpm() - configure the sleep mode of a pin
  492. * @gpio: pin number
  493. * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
  494. *
  495. * This register is actually in the pinmux layer, not the GPIO block itself.
  496. * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
  497. * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
  498. * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
  499. * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
  500. * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
  501. * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
  502. *
  503. * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
  504. * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
  505. * entered) regardless of the altfunction selected. Also wake-up detection is
  506. * ENABLED.
  507. *
  508. * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
  509. * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
  510. * (for altfunction GPIO) or respective on-chip peripherals (for other
  511. * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
  512. *
  513. * Note that enable_irq_wake() will automatically enable wakeup detection.
  514. */
  515. int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
  516. {
  517. struct nmk_gpio_chip *nmk_chip;
  518. unsigned long flags;
  519. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  520. if (!nmk_chip)
  521. return -EINVAL;
  522. clk_enable(nmk_chip->clk);
  523. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  524. spin_lock(&nmk_chip->lock);
  525. __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
  526. spin_unlock(&nmk_chip->lock);
  527. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  528. clk_disable(nmk_chip->clk);
  529. return 0;
  530. }
  531. /**
  532. * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
  533. * @gpio: pin number
  534. * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
  535. *
  536. * Enables/disables pull up/down on a specified pin. This only takes effect if
  537. * the pin is configured as an input (either explicitly or by the alternate
  538. * function).
  539. *
  540. * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
  541. * configured as an input. Otherwise, due to the way the controller registers
  542. * work, this function will change the value output on the pin.
  543. */
  544. int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
  545. {
  546. struct nmk_gpio_chip *nmk_chip;
  547. unsigned long flags;
  548. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  549. if (!nmk_chip)
  550. return -EINVAL;
  551. clk_enable(nmk_chip->clk);
  552. spin_lock_irqsave(&nmk_chip->lock, flags);
  553. __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
  554. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  555. clk_disable(nmk_chip->clk);
  556. return 0;
  557. }
  558. /* Mode functions */
  559. /**
  560. * nmk_gpio_set_mode() - set the mux mode of a gpio pin
  561. * @gpio: pin number
  562. * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
  563. * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
  564. *
  565. * Sets the mode of the specified pin to one of the alternate functions or
  566. * plain GPIO.
  567. */
  568. int nmk_gpio_set_mode(int gpio, int gpio_mode)
  569. {
  570. struct nmk_gpio_chip *nmk_chip;
  571. unsigned long flags;
  572. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  573. if (!nmk_chip)
  574. return -EINVAL;
  575. clk_enable(nmk_chip->clk);
  576. spin_lock_irqsave(&nmk_chip->lock, flags);
  577. __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
  578. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  579. clk_disable(nmk_chip->clk);
  580. return 0;
  581. }
  582. EXPORT_SYMBOL(nmk_gpio_set_mode);
  583. static int __maybe_unused nmk_prcm_gpiocr_get_mode(struct pinctrl_dev *pctldev, int gpio)
  584. {
  585. int i;
  586. u16 reg;
  587. u8 bit;
  588. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  589. const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
  590. const u16 *gpiocr_regs;
  591. if (!npct->prcm_base)
  592. return NMK_GPIO_ALT_C;
  593. for (i = 0; i < npct->soc->npins_altcx; i++) {
  594. if (npct->soc->altcx_pins[i].pin == gpio)
  595. break;
  596. }
  597. if (i == npct->soc->npins_altcx)
  598. return NMK_GPIO_ALT_C;
  599. pin_desc = npct->soc->altcx_pins + i;
  600. gpiocr_regs = npct->soc->prcm_gpiocr_registers;
  601. for (i = 0; i < PRCM_IDX_GPIOCR_ALTC_MAX; i++) {
  602. if (pin_desc->altcx[i].used == true) {
  603. reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
  604. bit = pin_desc->altcx[i].control_bit;
  605. if (readl(npct->prcm_base + reg) & BIT(bit))
  606. return NMK_GPIO_ALT_C+i+1;
  607. }
  608. }
  609. return NMK_GPIO_ALT_C;
  610. }
  611. int nmk_gpio_get_mode(int gpio)
  612. {
  613. struct nmk_gpio_chip *nmk_chip;
  614. u32 afunc, bfunc, bit;
  615. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  616. if (!nmk_chip)
  617. return -EINVAL;
  618. bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
  619. clk_enable(nmk_chip->clk);
  620. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
  621. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
  622. clk_disable(nmk_chip->clk);
  623. return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
  624. }
  625. EXPORT_SYMBOL(nmk_gpio_get_mode);
  626. /* IRQ functions */
  627. static inline int nmk_gpio_get_bitmask(int gpio)
  628. {
  629. return 1 << (gpio % NMK_GPIO_PER_CHIP);
  630. }
  631. static void nmk_gpio_irq_ack(struct irq_data *d)
  632. {
  633. struct nmk_gpio_chip *nmk_chip;
  634. nmk_chip = irq_data_get_irq_chip_data(d);
  635. if (!nmk_chip)
  636. return;
  637. clk_enable(nmk_chip->clk);
  638. writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
  639. clk_disable(nmk_chip->clk);
  640. }
  641. enum nmk_gpio_irq_type {
  642. NORMAL,
  643. WAKE,
  644. };
  645. static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
  646. int gpio, enum nmk_gpio_irq_type which,
  647. bool enable)
  648. {
  649. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  650. u32 *rimscval;
  651. u32 *fimscval;
  652. u32 rimscreg;
  653. u32 fimscreg;
  654. if (which == NORMAL) {
  655. rimscreg = NMK_GPIO_RIMSC;
  656. fimscreg = NMK_GPIO_FIMSC;
  657. rimscval = &nmk_chip->rimsc;
  658. fimscval = &nmk_chip->fimsc;
  659. } else {
  660. rimscreg = NMK_GPIO_RWIMSC;
  661. fimscreg = NMK_GPIO_FWIMSC;
  662. rimscval = &nmk_chip->rwimsc;
  663. fimscval = &nmk_chip->fwimsc;
  664. }
  665. /* we must individually set/clear the two edges */
  666. if (nmk_chip->edge_rising & bitmask) {
  667. if (enable)
  668. *rimscval |= bitmask;
  669. else
  670. *rimscval &= ~bitmask;
  671. writel(*rimscval, nmk_chip->addr + rimscreg);
  672. }
  673. if (nmk_chip->edge_falling & bitmask) {
  674. if (enable)
  675. *fimscval |= bitmask;
  676. else
  677. *fimscval &= ~bitmask;
  678. writel(*fimscval, nmk_chip->addr + fimscreg);
  679. }
  680. }
  681. static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
  682. int gpio, bool on)
  683. {
  684. /*
  685. * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
  686. * disabled, since setting SLPM to 1 increases power consumption, and
  687. * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
  688. */
  689. if (nmk_chip->sleepmode && on) {
  690. __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
  691. NMK_GPIO_SLPM_WAKEUP_ENABLE);
  692. }
  693. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
  694. }
  695. static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
  696. {
  697. struct nmk_gpio_chip *nmk_chip;
  698. unsigned long flags;
  699. u32 bitmask;
  700. nmk_chip = irq_data_get_irq_chip_data(d);
  701. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  702. if (!nmk_chip)
  703. return -EINVAL;
  704. clk_enable(nmk_chip->clk);
  705. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  706. spin_lock(&nmk_chip->lock);
  707. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
  708. if (!(nmk_chip->real_wake & bitmask))
  709. __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
  710. spin_unlock(&nmk_chip->lock);
  711. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  712. clk_disable(nmk_chip->clk);
  713. return 0;
  714. }
  715. static void nmk_gpio_irq_mask(struct irq_data *d)
  716. {
  717. nmk_gpio_irq_maskunmask(d, false);
  718. }
  719. static void nmk_gpio_irq_unmask(struct irq_data *d)
  720. {
  721. nmk_gpio_irq_maskunmask(d, true);
  722. }
  723. static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  724. {
  725. struct nmk_gpio_chip *nmk_chip;
  726. unsigned long flags;
  727. u32 bitmask;
  728. nmk_chip = irq_data_get_irq_chip_data(d);
  729. if (!nmk_chip)
  730. return -EINVAL;
  731. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  732. clk_enable(nmk_chip->clk);
  733. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  734. spin_lock(&nmk_chip->lock);
  735. if (irqd_irq_disabled(d))
  736. __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
  737. if (on)
  738. nmk_chip->real_wake |= bitmask;
  739. else
  740. nmk_chip->real_wake &= ~bitmask;
  741. spin_unlock(&nmk_chip->lock);
  742. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  743. clk_disable(nmk_chip->clk);
  744. return 0;
  745. }
  746. static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  747. {
  748. bool enabled = !irqd_irq_disabled(d);
  749. bool wake = irqd_is_wakeup_set(d);
  750. struct nmk_gpio_chip *nmk_chip;
  751. unsigned long flags;
  752. u32 bitmask;
  753. nmk_chip = irq_data_get_irq_chip_data(d);
  754. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  755. if (!nmk_chip)
  756. return -EINVAL;
  757. if (type & IRQ_TYPE_LEVEL_HIGH)
  758. return -EINVAL;
  759. if (type & IRQ_TYPE_LEVEL_LOW)
  760. return -EINVAL;
  761. clk_enable(nmk_chip->clk);
  762. spin_lock_irqsave(&nmk_chip->lock, flags);
  763. if (enabled)
  764. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
  765. if (enabled || wake)
  766. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
  767. nmk_chip->edge_rising &= ~bitmask;
  768. if (type & IRQ_TYPE_EDGE_RISING)
  769. nmk_chip->edge_rising |= bitmask;
  770. nmk_chip->edge_falling &= ~bitmask;
  771. if (type & IRQ_TYPE_EDGE_FALLING)
  772. nmk_chip->edge_falling |= bitmask;
  773. if (enabled)
  774. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
  775. if (enabled || wake)
  776. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
  777. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  778. clk_disable(nmk_chip->clk);
  779. return 0;
  780. }
  781. static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
  782. {
  783. struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
  784. clk_enable(nmk_chip->clk);
  785. nmk_gpio_irq_unmask(d);
  786. return 0;
  787. }
  788. static void nmk_gpio_irq_shutdown(struct irq_data *d)
  789. {
  790. struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
  791. nmk_gpio_irq_mask(d);
  792. clk_disable(nmk_chip->clk);
  793. }
  794. static struct irq_chip nmk_gpio_irq_chip = {
  795. .name = "Nomadik-GPIO",
  796. .irq_ack = nmk_gpio_irq_ack,
  797. .irq_mask = nmk_gpio_irq_mask,
  798. .irq_unmask = nmk_gpio_irq_unmask,
  799. .irq_set_type = nmk_gpio_irq_set_type,
  800. .irq_set_wake = nmk_gpio_irq_set_wake,
  801. .irq_startup = nmk_gpio_irq_startup,
  802. .irq_shutdown = nmk_gpio_irq_shutdown,
  803. .flags = IRQCHIP_MASK_ON_SUSPEND,
  804. };
  805. static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
  806. u32 status)
  807. {
  808. struct nmk_gpio_chip *nmk_chip;
  809. struct irq_chip *host_chip = irq_get_chip(irq);
  810. chained_irq_enter(host_chip, desc);
  811. nmk_chip = irq_get_handler_data(irq);
  812. while (status) {
  813. int bit = __ffs(status);
  814. generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
  815. status &= ~BIT(bit);
  816. }
  817. chained_irq_exit(host_chip, desc);
  818. }
  819. static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  820. {
  821. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  822. u32 status;
  823. clk_enable(nmk_chip->clk);
  824. status = readl(nmk_chip->addr + NMK_GPIO_IS);
  825. clk_disable(nmk_chip->clk);
  826. __nmk_gpio_irq_handler(irq, desc, status);
  827. }
  828. static void nmk_gpio_secondary_irq_handler(unsigned int irq,
  829. struct irq_desc *desc)
  830. {
  831. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  832. u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
  833. __nmk_gpio_irq_handler(irq, desc, status);
  834. }
  835. static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
  836. {
  837. irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
  838. irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
  839. if (nmk_chip->secondary_parent_irq >= 0) {
  840. irq_set_chained_handler(nmk_chip->secondary_parent_irq,
  841. nmk_gpio_secondary_irq_handler);
  842. irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
  843. }
  844. return 0;
  845. }
  846. /* I/O Functions */
  847. static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
  848. {
  849. /*
  850. * Map back to global GPIO space and request muxing, the direction
  851. * parameter does not matter for this controller.
  852. */
  853. int gpio = chip->base + offset;
  854. return pinctrl_request_gpio(gpio);
  855. }
  856. static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
  857. {
  858. int gpio = chip->base + offset;
  859. pinctrl_free_gpio(gpio);
  860. }
  861. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
  862. {
  863. struct nmk_gpio_chip *nmk_chip =
  864. container_of(chip, struct nmk_gpio_chip, chip);
  865. clk_enable(nmk_chip->clk);
  866. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  867. clk_disable(nmk_chip->clk);
  868. return 0;
  869. }
  870. static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
  871. {
  872. struct nmk_gpio_chip *nmk_chip =
  873. container_of(chip, struct nmk_gpio_chip, chip);
  874. u32 bit = 1 << offset;
  875. int value;
  876. clk_enable(nmk_chip->clk);
  877. value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  878. clk_disable(nmk_chip->clk);
  879. return value;
  880. }
  881. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  882. int val)
  883. {
  884. struct nmk_gpio_chip *nmk_chip =
  885. container_of(chip, struct nmk_gpio_chip, chip);
  886. clk_enable(nmk_chip->clk);
  887. __nmk_gpio_set_output(nmk_chip, offset, val);
  888. clk_disable(nmk_chip->clk);
  889. }
  890. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
  891. int val)
  892. {
  893. struct nmk_gpio_chip *nmk_chip =
  894. container_of(chip, struct nmk_gpio_chip, chip);
  895. clk_enable(nmk_chip->clk);
  896. __nmk_gpio_make_output(nmk_chip, offset, val);
  897. clk_disable(nmk_chip->clk);
  898. return 0;
  899. }
  900. static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  901. {
  902. struct nmk_gpio_chip *nmk_chip =
  903. container_of(chip, struct nmk_gpio_chip, chip);
  904. return irq_create_mapping(nmk_chip->domain, offset);
  905. }
  906. #ifdef CONFIG_DEBUG_FS
  907. #include <linux/seq_file.h>
  908. static void nmk_gpio_dbg_show_one(struct seq_file *s,
  909. struct pinctrl_dev *pctldev, struct gpio_chip *chip,
  910. unsigned offset, unsigned gpio)
  911. {
  912. const char *label = gpiochip_is_requested(chip, offset);
  913. struct nmk_gpio_chip *nmk_chip =
  914. container_of(chip, struct nmk_gpio_chip, chip);
  915. int mode;
  916. bool is_out;
  917. bool pull;
  918. u32 bit = 1 << offset;
  919. const char *modes[] = {
  920. [NMK_GPIO_ALT_GPIO] = "gpio",
  921. [NMK_GPIO_ALT_A] = "altA",
  922. [NMK_GPIO_ALT_B] = "altB",
  923. [NMK_GPIO_ALT_C] = "altC",
  924. [NMK_GPIO_ALT_C+1] = "altC1",
  925. [NMK_GPIO_ALT_C+2] = "altC2",
  926. [NMK_GPIO_ALT_C+3] = "altC3",
  927. [NMK_GPIO_ALT_C+4] = "altC4",
  928. };
  929. clk_enable(nmk_chip->clk);
  930. is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
  931. pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
  932. mode = nmk_gpio_get_mode(gpio);
  933. if ((mode == NMK_GPIO_ALT_C) && pctldev)
  934. mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio);
  935. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
  936. gpio, label ?: "(none)",
  937. is_out ? "out" : "in ",
  938. chip->get
  939. ? (chip->get(chip, offset) ? "hi" : "lo")
  940. : "? ",
  941. (mode < 0) ? "unknown" : modes[mode],
  942. pull ? "pull" : "none");
  943. if (label && !is_out) {
  944. int irq = gpio_to_irq(gpio);
  945. struct irq_desc *desc = irq_to_desc(irq);
  946. /* This races with request_irq(), set_irq_type(),
  947. * and set_irq_wake() ... but those are "rare".
  948. */
  949. if (irq >= 0 && desc->action) {
  950. char *trigger;
  951. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  952. if (nmk_chip->edge_rising & bitmask)
  953. trigger = "edge-rising";
  954. else if (nmk_chip->edge_falling & bitmask)
  955. trigger = "edge-falling";
  956. else
  957. trigger = "edge-undefined";
  958. seq_printf(s, " irq-%d %s%s",
  959. irq, trigger,
  960. irqd_is_wakeup_set(&desc->irq_data)
  961. ? " wakeup" : "");
  962. }
  963. }
  964. clk_disable(nmk_chip->clk);
  965. }
  966. static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  967. {
  968. unsigned i;
  969. unsigned gpio = chip->base;
  970. for (i = 0; i < chip->ngpio; i++, gpio++) {
  971. nmk_gpio_dbg_show_one(s, NULL, chip, i, gpio);
  972. seq_printf(s, "\n");
  973. }
  974. }
  975. #else
  976. static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
  977. struct pinctrl_dev *pctldev,
  978. struct gpio_chip *chip,
  979. unsigned offset, unsigned gpio)
  980. {
  981. }
  982. #define nmk_gpio_dbg_show NULL
  983. #endif
  984. /* This structure is replicated for each GPIO block allocated at probe time */
  985. static struct gpio_chip nmk_gpio_template = {
  986. .request = nmk_gpio_request,
  987. .free = nmk_gpio_free,
  988. .direction_input = nmk_gpio_make_input,
  989. .get = nmk_gpio_get_input,
  990. .direction_output = nmk_gpio_make_output,
  991. .set = nmk_gpio_set_output,
  992. .to_irq = nmk_gpio_to_irq,
  993. .dbg_show = nmk_gpio_dbg_show,
  994. .can_sleep = 0,
  995. };
  996. void nmk_gpio_clocks_enable(void)
  997. {
  998. int i;
  999. for (i = 0; i < NUM_BANKS; i++) {
  1000. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  1001. if (!chip)
  1002. continue;
  1003. clk_enable(chip->clk);
  1004. }
  1005. }
  1006. void nmk_gpio_clocks_disable(void)
  1007. {
  1008. int i;
  1009. for (i = 0; i < NUM_BANKS; i++) {
  1010. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  1011. if (!chip)
  1012. continue;
  1013. clk_disable(chip->clk);
  1014. }
  1015. }
  1016. /*
  1017. * Called from the suspend/resume path to only keep the real wakeup interrupts
  1018. * (those that have had set_irq_wake() called on them) as wakeup interrupts,
  1019. * and not the rest of the interrupts which we needed to have as wakeups for
  1020. * cpuidle.
  1021. *
  1022. * PM ops are not used since this needs to be done at the end, after all the
  1023. * other drivers are done with their suspend callbacks.
  1024. */
  1025. void nmk_gpio_wakeups_suspend(void)
  1026. {
  1027. int i;
  1028. for (i = 0; i < NUM_BANKS; i++) {
  1029. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  1030. if (!chip)
  1031. break;
  1032. clk_enable(chip->clk);
  1033. writel(chip->rwimsc & chip->real_wake,
  1034. chip->addr + NMK_GPIO_RWIMSC);
  1035. writel(chip->fwimsc & chip->real_wake,
  1036. chip->addr + NMK_GPIO_FWIMSC);
  1037. clk_disable(chip->clk);
  1038. }
  1039. }
  1040. void nmk_gpio_wakeups_resume(void)
  1041. {
  1042. int i;
  1043. for (i = 0; i < NUM_BANKS; i++) {
  1044. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  1045. if (!chip)
  1046. break;
  1047. clk_enable(chip->clk);
  1048. writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
  1049. writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
  1050. clk_disable(chip->clk);
  1051. }
  1052. }
  1053. /*
  1054. * Read the pull up/pull down status.
  1055. * A bit set in 'pull_up' means that pull up
  1056. * is selected if pull is enabled in PDIS register.
  1057. * Note: only pull up/down set via this driver can
  1058. * be detected due to HW limitations.
  1059. */
  1060. void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
  1061. {
  1062. if (gpio_bank < NUM_BANKS) {
  1063. struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
  1064. if (!chip)
  1065. return;
  1066. *pull_up = chip->pull_up;
  1067. }
  1068. }
  1069. static int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
  1070. irq_hw_number_t hwirq)
  1071. {
  1072. struct nmk_gpio_chip *nmk_chip = d->host_data;
  1073. if (!nmk_chip)
  1074. return -EINVAL;
  1075. irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
  1076. set_irq_flags(irq, IRQF_VALID);
  1077. irq_set_chip_data(irq, nmk_chip);
  1078. irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
  1079. return 0;
  1080. }
  1081. const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
  1082. .map = nmk_gpio_irq_map,
  1083. .xlate = irq_domain_xlate_twocell,
  1084. };
  1085. static int nmk_gpio_probe(struct platform_device *dev)
  1086. {
  1087. struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
  1088. struct device_node *np = dev->dev.of_node;
  1089. struct nmk_gpio_chip *nmk_chip;
  1090. struct gpio_chip *chip;
  1091. struct resource *res;
  1092. struct clk *clk;
  1093. int secondary_irq;
  1094. void __iomem *base;
  1095. int irq_start = 0;
  1096. int irq;
  1097. int ret;
  1098. if (!pdata && !np) {
  1099. dev_err(&dev->dev, "No platform data or device tree found\n");
  1100. return -ENODEV;
  1101. }
  1102. if (np) {
  1103. pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
  1104. if (!pdata)
  1105. return -ENOMEM;
  1106. if (of_get_property(np, "st,supports-sleepmode", NULL))
  1107. pdata->supports_sleepmode = true;
  1108. if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
  1109. dev_err(&dev->dev, "gpio-bank property not found\n");
  1110. return -EINVAL;
  1111. }
  1112. pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
  1113. pdata->num_gpio = NMK_GPIO_PER_CHIP;
  1114. }
  1115. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1116. if (!res)
  1117. return -ENOENT;
  1118. irq = platform_get_irq(dev, 0);
  1119. if (irq < 0)
  1120. return irq;
  1121. secondary_irq = platform_get_irq(dev, 1);
  1122. if (secondary_irq >= 0 && !pdata->get_secondary_status)
  1123. return -EINVAL;
  1124. base = devm_ioremap_resource(&dev->dev, res);
  1125. if (IS_ERR(base))
  1126. return PTR_ERR(base);
  1127. clk = devm_clk_get(&dev->dev, NULL);
  1128. if (IS_ERR(clk))
  1129. return PTR_ERR(clk);
  1130. clk_prepare(clk);
  1131. nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
  1132. if (!nmk_chip)
  1133. return -ENOMEM;
  1134. /*
  1135. * The virt address in nmk_chip->addr is in the nomadik register space,
  1136. * so we can simply convert the resource address, without remapping
  1137. */
  1138. nmk_chip->bank = dev->id;
  1139. nmk_chip->clk = clk;
  1140. nmk_chip->addr = base;
  1141. nmk_chip->chip = nmk_gpio_template;
  1142. nmk_chip->parent_irq = irq;
  1143. nmk_chip->secondary_parent_irq = secondary_irq;
  1144. nmk_chip->get_secondary_status = pdata->get_secondary_status;
  1145. nmk_chip->set_ioforce = pdata->set_ioforce;
  1146. nmk_chip->sleepmode = pdata->supports_sleepmode;
  1147. spin_lock_init(&nmk_chip->lock);
  1148. chip = &nmk_chip->chip;
  1149. chip->base = pdata->first_gpio;
  1150. chip->ngpio = pdata->num_gpio;
  1151. chip->label = pdata->name ?: dev_name(&dev->dev);
  1152. chip->dev = &dev->dev;
  1153. chip->owner = THIS_MODULE;
  1154. clk_enable(nmk_chip->clk);
  1155. nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
  1156. clk_disable(nmk_chip->clk);
  1157. #ifdef CONFIG_OF_GPIO
  1158. chip->of_node = np;
  1159. #endif
  1160. ret = gpiochip_add(&nmk_chip->chip);
  1161. if (ret)
  1162. return ret;
  1163. BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
  1164. nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
  1165. platform_set_drvdata(dev, nmk_chip);
  1166. if (!np)
  1167. irq_start = pdata->first_irq;
  1168. nmk_chip->domain = irq_domain_add_simple(np,
  1169. NMK_GPIO_PER_CHIP, irq_start,
  1170. &nmk_gpio_irq_simple_ops, nmk_chip);
  1171. if (!nmk_chip->domain) {
  1172. dev_err(&dev->dev, "failed to create irqdomain\n");
  1173. /* Just do this, no matter if it fails */
  1174. ret = gpiochip_remove(&nmk_chip->chip);
  1175. return -ENOSYS;
  1176. }
  1177. nmk_gpio_init_irq(nmk_chip);
  1178. dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
  1179. return 0;
  1180. }
  1181. static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
  1182. {
  1183. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1184. return npct->soc->ngroups;
  1185. }
  1186. static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
  1187. unsigned selector)
  1188. {
  1189. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1190. return npct->soc->groups[selector].name;
  1191. }
  1192. static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  1193. const unsigned **pins,
  1194. unsigned *num_pins)
  1195. {
  1196. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1197. *pins = npct->soc->groups[selector].pins;
  1198. *num_pins = npct->soc->groups[selector].npins;
  1199. return 0;
  1200. }
  1201. static struct pinctrl_gpio_range *
  1202. nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
  1203. {
  1204. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1205. int i;
  1206. for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
  1207. struct pinctrl_gpio_range *range;
  1208. range = &npct->soc->gpio_ranges[i];
  1209. if (offset >= range->pin_base &&
  1210. offset <= (range->pin_base + range->npins - 1))
  1211. return range;
  1212. }
  1213. return NULL;
  1214. }
  1215. static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  1216. unsigned offset)
  1217. {
  1218. struct pinctrl_gpio_range *range;
  1219. struct gpio_chip *chip;
  1220. range = nmk_match_gpio_range(pctldev, offset);
  1221. if (!range || !range->gc) {
  1222. seq_printf(s, "invalid pin offset");
  1223. return;
  1224. }
  1225. chip = range->gc;
  1226. nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base, offset);
  1227. }
  1228. static void nmk_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
  1229. struct pinctrl_map *map, unsigned num_maps)
  1230. {
  1231. int i;
  1232. for (i = 0; i < num_maps; i++)
  1233. if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  1234. kfree(map[i].data.configs.configs);
  1235. kfree(map);
  1236. }
  1237. static int nmk_dt_reserve_map(struct pinctrl_map **map, unsigned *reserved_maps,
  1238. unsigned *num_maps, unsigned reserve)
  1239. {
  1240. unsigned old_num = *reserved_maps;
  1241. unsigned new_num = *num_maps + reserve;
  1242. struct pinctrl_map *new_map;
  1243. if (old_num >= new_num)
  1244. return 0;
  1245. new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
  1246. if (!new_map)
  1247. return -ENOMEM;
  1248. memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
  1249. *map = new_map;
  1250. *reserved_maps = new_num;
  1251. return 0;
  1252. }
  1253. static int nmk_dt_add_map_mux(struct pinctrl_map **map, unsigned *reserved_maps,
  1254. unsigned *num_maps, const char *group,
  1255. const char *function)
  1256. {
  1257. if (*num_maps == *reserved_maps)
  1258. return -ENOSPC;
  1259. (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
  1260. (*map)[*num_maps].data.mux.group = group;
  1261. (*map)[*num_maps].data.mux.function = function;
  1262. (*num_maps)++;
  1263. return 0;
  1264. }
  1265. static int nmk_dt_add_map_configs(struct pinctrl_map **map,
  1266. unsigned *reserved_maps,
  1267. unsigned *num_maps, const char *group,
  1268. unsigned long *configs, unsigned num_configs)
  1269. {
  1270. unsigned long *dup_configs;
  1271. if (*num_maps == *reserved_maps)
  1272. return -ENOSPC;
  1273. dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
  1274. GFP_KERNEL);
  1275. if (!dup_configs)
  1276. return -ENOMEM;
  1277. (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
  1278. (*map)[*num_maps].data.configs.group_or_pin = group;
  1279. (*map)[*num_maps].data.configs.configs = dup_configs;
  1280. (*map)[*num_maps].data.configs.num_configs = num_configs;
  1281. (*num_maps)++;
  1282. return 0;
  1283. }
  1284. #define NMK_CONFIG_PIN(x, y) { .property = x, .config = y, }
  1285. #define NMK_CONFIG_PIN_ARRAY(x, y) { .property = x, .choice = y, \
  1286. .size = ARRAY_SIZE(y), }
  1287. static const unsigned long nmk_pin_input_modes[] = {
  1288. PIN_INPUT_NOPULL,
  1289. PIN_INPUT_PULLUP,
  1290. PIN_INPUT_PULLDOWN,
  1291. };
  1292. static const unsigned long nmk_pin_output_modes[] = {
  1293. PIN_OUTPUT_LOW,
  1294. PIN_OUTPUT_HIGH,
  1295. PIN_DIR_OUTPUT,
  1296. };
  1297. static const unsigned long nmk_pin_sleep_modes[] = {
  1298. PIN_SLEEPMODE_DISABLED,
  1299. PIN_SLEEPMODE_ENABLED,
  1300. };
  1301. static const unsigned long nmk_pin_sleep_input_modes[] = {
  1302. PIN_SLPM_INPUT_NOPULL,
  1303. PIN_SLPM_INPUT_PULLUP,
  1304. PIN_SLPM_INPUT_PULLDOWN,
  1305. PIN_SLPM_DIR_INPUT,
  1306. };
  1307. static const unsigned long nmk_pin_sleep_output_modes[] = {
  1308. PIN_SLPM_OUTPUT_LOW,
  1309. PIN_SLPM_OUTPUT_HIGH,
  1310. PIN_SLPM_DIR_OUTPUT,
  1311. };
  1312. static const unsigned long nmk_pin_sleep_wakeup_modes[] = {
  1313. PIN_SLPM_WAKEUP_DISABLE,
  1314. PIN_SLPM_WAKEUP_ENABLE,
  1315. };
  1316. static const unsigned long nmk_pin_gpio_modes[] = {
  1317. PIN_GPIOMODE_DISABLED,
  1318. PIN_GPIOMODE_ENABLED,
  1319. };
  1320. static const unsigned long nmk_pin_sleep_pdis_modes[] = {
  1321. PIN_SLPM_PDIS_DISABLED,
  1322. PIN_SLPM_PDIS_ENABLED,
  1323. };
  1324. struct nmk_cfg_param {
  1325. const char *property;
  1326. unsigned long config;
  1327. const unsigned long *choice;
  1328. int size;
  1329. };
  1330. static const struct nmk_cfg_param nmk_cfg_params[] = {
  1331. NMK_CONFIG_PIN_ARRAY("ste,input", nmk_pin_input_modes),
  1332. NMK_CONFIG_PIN_ARRAY("ste,output", nmk_pin_output_modes),
  1333. NMK_CONFIG_PIN_ARRAY("ste,sleep", nmk_pin_sleep_modes),
  1334. NMK_CONFIG_PIN_ARRAY("ste,sleep-input", nmk_pin_sleep_input_modes),
  1335. NMK_CONFIG_PIN_ARRAY("ste,sleep-output", nmk_pin_sleep_output_modes),
  1336. NMK_CONFIG_PIN_ARRAY("ste,sleep-wakeup", nmk_pin_sleep_wakeup_modes),
  1337. NMK_CONFIG_PIN_ARRAY("ste,gpio", nmk_pin_gpio_modes),
  1338. NMK_CONFIG_PIN_ARRAY("ste,sleep-pull-disable", nmk_pin_sleep_pdis_modes),
  1339. };
  1340. static int nmk_dt_pin_config(int index, int val, unsigned long *config)
  1341. {
  1342. int ret = 0;
  1343. if (nmk_cfg_params[index].choice == NULL)
  1344. *config = nmk_cfg_params[index].config;
  1345. else {
  1346. /* test if out of range */
  1347. if (val < nmk_cfg_params[index].size) {
  1348. *config = nmk_cfg_params[index].config |
  1349. nmk_cfg_params[index].choice[val];
  1350. }
  1351. }
  1352. return ret;
  1353. }
  1354. static const char *nmk_find_pin_name(struct pinctrl_dev *pctldev, const char *pin_name)
  1355. {
  1356. int i, pin_number;
  1357. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1358. if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
  1359. for (i = 0; i < npct->soc->npins; i++)
  1360. if (npct->soc->pins[i].number == pin_number)
  1361. return npct->soc->pins[i].name;
  1362. return NULL;
  1363. }
  1364. static bool nmk_pinctrl_dt_get_config(struct device_node *np,
  1365. unsigned long *configs)
  1366. {
  1367. bool has_config = 0;
  1368. unsigned long cfg = 0;
  1369. int i, val, ret;
  1370. for (i = 0; i < ARRAY_SIZE(nmk_cfg_params); i++) {
  1371. ret = of_property_read_u32(np,
  1372. nmk_cfg_params[i].property, &val);
  1373. if (ret != -EINVAL) {
  1374. if (nmk_dt_pin_config(i, val, &cfg) == 0) {
  1375. *configs |= cfg;
  1376. has_config = 1;
  1377. }
  1378. }
  1379. }
  1380. return has_config;
  1381. }
  1382. int nmk_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  1383. struct device_node *np,
  1384. struct pinctrl_map **map,
  1385. unsigned *reserved_maps,
  1386. unsigned *num_maps)
  1387. {
  1388. int ret;
  1389. const char *function = NULL;
  1390. unsigned long configs = 0;
  1391. bool has_config = 0;
  1392. unsigned reserve = 0;
  1393. struct property *prop;
  1394. const char *group, *gpio_name;
  1395. struct device_node *np_config;
  1396. ret = of_property_read_string(np, "ste,function", &function);
  1397. if (ret >= 0)
  1398. reserve = 1;
  1399. has_config = nmk_pinctrl_dt_get_config(np, &configs);
  1400. np_config = of_parse_phandle(np, "ste,config", 0);
  1401. if (np_config)
  1402. has_config |= nmk_pinctrl_dt_get_config(np_config, &configs);
  1403. ret = of_property_count_strings(np, "ste,pins");
  1404. if (ret < 0)
  1405. goto exit;
  1406. if (has_config)
  1407. reserve++;
  1408. reserve *= ret;
  1409. ret = nmk_dt_reserve_map(map, reserved_maps, num_maps, reserve);
  1410. if (ret < 0)
  1411. goto exit;
  1412. of_property_for_each_string(np, "ste,pins", prop, group) {
  1413. if (function) {
  1414. ret = nmk_dt_add_map_mux(map, reserved_maps, num_maps,
  1415. group, function);
  1416. if (ret < 0)
  1417. goto exit;
  1418. }
  1419. if (has_config) {
  1420. gpio_name = nmk_find_pin_name(pctldev, group);
  1421. ret = nmk_dt_add_map_configs(map, reserved_maps, num_maps,
  1422. gpio_name, &configs, 1);
  1423. if (ret < 0)
  1424. goto exit;
  1425. }
  1426. }
  1427. exit:
  1428. return ret;
  1429. }
  1430. int nmk_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  1431. struct device_node *np_config,
  1432. struct pinctrl_map **map, unsigned *num_maps)
  1433. {
  1434. unsigned reserved_maps;
  1435. struct device_node *np;
  1436. int ret;
  1437. reserved_maps = 0;
  1438. *map = NULL;
  1439. *num_maps = 0;
  1440. for_each_child_of_node(np_config, np) {
  1441. ret = nmk_pinctrl_dt_subnode_to_map(pctldev, np, map,
  1442. &reserved_maps, num_maps);
  1443. if (ret < 0) {
  1444. nmk_pinctrl_dt_free_map(pctldev, *map, *num_maps);
  1445. return ret;
  1446. }
  1447. }
  1448. return 0;
  1449. }
  1450. static const struct pinctrl_ops nmk_pinctrl_ops = {
  1451. .get_groups_count = nmk_get_groups_cnt,
  1452. .get_group_name = nmk_get_group_name,
  1453. .get_group_pins = nmk_get_group_pins,
  1454. .pin_dbg_show = nmk_pin_dbg_show,
  1455. .dt_node_to_map = nmk_pinctrl_dt_node_to_map,
  1456. .dt_free_map = nmk_pinctrl_dt_free_map,
  1457. };
  1458. static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  1459. {
  1460. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1461. return npct->soc->nfunctions;
  1462. }
  1463. static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
  1464. unsigned function)
  1465. {
  1466. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1467. return npct->soc->functions[function].name;
  1468. }
  1469. static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  1470. unsigned function,
  1471. const char * const **groups,
  1472. unsigned * const num_groups)
  1473. {
  1474. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1475. *groups = npct->soc->functions[function].groups;
  1476. *num_groups = npct->soc->functions[function].ngroups;
  1477. return 0;
  1478. }
  1479. static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
  1480. unsigned group)
  1481. {
  1482. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1483. const struct nmk_pingroup *g;
  1484. static unsigned int slpm[NUM_BANKS];
  1485. unsigned long flags;
  1486. bool glitch;
  1487. int ret = -EINVAL;
  1488. int i;
  1489. g = &npct->soc->groups[group];
  1490. if (g->altsetting < 0)
  1491. return -EINVAL;
  1492. dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
  1493. /*
  1494. * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
  1495. * we may pass through an undesired state. In this case we take
  1496. * some extra care.
  1497. *
  1498. * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
  1499. * - Save SLPM registers (since we have a shadow register in the
  1500. * nmk_chip we're using that as backup)
  1501. * - Set SLPM=0 for the IOs you want to switch and others to 1
  1502. * - Configure the GPIO registers for the IOs that are being switched
  1503. * - Set IOFORCE=1
  1504. * - Modify the AFLSA/B registers for the IOs that are being switched
  1505. * - Set IOFORCE=0
  1506. * - Restore SLPM registers
  1507. * - Any spurious wake up event during switch sequence to be ignored
  1508. * and cleared
  1509. *
  1510. * We REALLY need to save ALL slpm registers, because the external
  1511. * IOFORCE will switch *all* ports to their sleepmode setting to as
  1512. * to avoid glitches. (Not just one port!)
  1513. */
  1514. glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
  1515. if (glitch) {
  1516. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  1517. /* Initially don't put any pins to sleep when switching */
  1518. memset(slpm, 0xff, sizeof(slpm));
  1519. /*
  1520. * Then mask the pins that need to be sleeping now when we're
  1521. * switching to the ALT C function.
  1522. */
  1523. for (i = 0; i < g->npins; i++)
  1524. slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
  1525. nmk_gpio_glitch_slpm_init(slpm);
  1526. }
  1527. for (i = 0; i < g->npins; i++) {
  1528. struct pinctrl_gpio_range *range;
  1529. struct nmk_gpio_chip *nmk_chip;
  1530. struct gpio_chip *chip;
  1531. unsigned bit;
  1532. range = nmk_match_gpio_range(pctldev, g->pins[i]);
  1533. if (!range) {
  1534. dev_err(npct->dev,
  1535. "invalid pin offset %d in group %s at index %d\n",
  1536. g->pins[i], g->name, i);
  1537. goto out_glitch;
  1538. }
  1539. if (!range->gc) {
  1540. dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
  1541. g->pins[i], g->name, i);
  1542. goto out_glitch;
  1543. }
  1544. chip = range->gc;
  1545. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1546. dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
  1547. clk_enable(nmk_chip->clk);
  1548. bit = g->pins[i] % NMK_GPIO_PER_CHIP;
  1549. /*
  1550. * If the pin is switching to altfunc, and there was an
  1551. * interrupt installed on it which has been lazy disabled,
  1552. * actually mask the interrupt to prevent spurious interrupts
  1553. * that would occur while the pin is under control of the
  1554. * peripheral. Only SKE does this.
  1555. */
  1556. nmk_gpio_disable_lazy_irq(nmk_chip, bit);
  1557. __nmk_gpio_set_mode_safe(nmk_chip, bit,
  1558. (g->altsetting & NMK_GPIO_ALT_C), glitch);
  1559. clk_disable(nmk_chip->clk);
  1560. /*
  1561. * Call PRCM GPIOCR config function in case ALTC
  1562. * has been selected:
  1563. * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
  1564. * must be set.
  1565. * - If selection is pure ALTC and previous selection was ALTCx,
  1566. * then some bits in PRCM GPIOCR registers must be cleared.
  1567. */
  1568. if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
  1569. nmk_prcm_altcx_set_mode(npct, g->pins[i],
  1570. g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
  1571. }
  1572. /* When all pins are successfully reconfigured we get here */
  1573. ret = 0;
  1574. out_glitch:
  1575. if (glitch) {
  1576. nmk_gpio_glitch_slpm_restore(slpm);
  1577. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  1578. }
  1579. return ret;
  1580. }
  1581. static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
  1582. unsigned function, unsigned group)
  1583. {
  1584. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1585. const struct nmk_pingroup *g;
  1586. g = &npct->soc->groups[group];
  1587. if (g->altsetting < 0)
  1588. return;
  1589. /* Poke out the mux, set the pin to some default state? */
  1590. dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
  1591. }
  1592. static int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
  1593. struct pinctrl_gpio_range *range,
  1594. unsigned offset)
  1595. {
  1596. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1597. struct nmk_gpio_chip *nmk_chip;
  1598. struct gpio_chip *chip;
  1599. unsigned bit;
  1600. if (!range) {
  1601. dev_err(npct->dev, "invalid range\n");
  1602. return -EINVAL;
  1603. }
  1604. if (!range->gc) {
  1605. dev_err(npct->dev, "missing GPIO chip in range\n");
  1606. return -EINVAL;
  1607. }
  1608. chip = range->gc;
  1609. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1610. dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
  1611. clk_enable(nmk_chip->clk);
  1612. bit = offset % NMK_GPIO_PER_CHIP;
  1613. /* There is no glitch when converting any pin to GPIO */
  1614. __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
  1615. clk_disable(nmk_chip->clk);
  1616. return 0;
  1617. }
  1618. static void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
  1619. struct pinctrl_gpio_range *range,
  1620. unsigned offset)
  1621. {
  1622. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1623. dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
  1624. /* Set the pin to some default state, GPIO is usually default */
  1625. }
  1626. static const struct pinmux_ops nmk_pinmux_ops = {
  1627. .get_functions_count = nmk_pmx_get_funcs_cnt,
  1628. .get_function_name = nmk_pmx_get_func_name,
  1629. .get_function_groups = nmk_pmx_get_func_groups,
  1630. .enable = nmk_pmx_enable,
  1631. .disable = nmk_pmx_disable,
  1632. .gpio_request_enable = nmk_gpio_request_enable,
  1633. .gpio_disable_free = nmk_gpio_disable_free,
  1634. };
  1635. static int nmk_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
  1636. unsigned long *config)
  1637. {
  1638. /* Not implemented */
  1639. return -EINVAL;
  1640. }
  1641. static int nmk_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
  1642. unsigned long config)
  1643. {
  1644. static const char *pullnames[] = {
  1645. [NMK_GPIO_PULL_NONE] = "none",
  1646. [NMK_GPIO_PULL_UP] = "up",
  1647. [NMK_GPIO_PULL_DOWN] = "down",
  1648. [3] /* illegal */ = "??"
  1649. };
  1650. static const char *slpmnames[] = {
  1651. [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
  1652. [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
  1653. };
  1654. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1655. struct nmk_gpio_chip *nmk_chip;
  1656. struct pinctrl_gpio_range *range;
  1657. struct gpio_chip *chip;
  1658. unsigned bit;
  1659. /*
  1660. * The pin config contains pin number and altfunction fields, here
  1661. * we just ignore that part. It's being handled by the framework and
  1662. * pinmux callback respectively.
  1663. */
  1664. pin_cfg_t cfg = (pin_cfg_t) config;
  1665. int pull = PIN_PULL(cfg);
  1666. int slpm = PIN_SLPM(cfg);
  1667. int output = PIN_DIR(cfg);
  1668. int val = PIN_VAL(cfg);
  1669. bool lowemi = PIN_LOWEMI(cfg);
  1670. bool gpiomode = PIN_GPIOMODE(cfg);
  1671. bool sleep = PIN_SLEEPMODE(cfg);
  1672. range = nmk_match_gpio_range(pctldev, pin);
  1673. if (!range) {
  1674. dev_err(npct->dev, "invalid pin offset %d\n", pin);
  1675. return -EINVAL;
  1676. }
  1677. if (!range->gc) {
  1678. dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
  1679. pin);
  1680. return -EINVAL;
  1681. }
  1682. chip = range->gc;
  1683. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1684. if (sleep) {
  1685. int slpm_pull = PIN_SLPM_PULL(cfg);
  1686. int slpm_output = PIN_SLPM_DIR(cfg);
  1687. int slpm_val = PIN_SLPM_VAL(cfg);
  1688. /* All pins go into GPIO mode at sleep */
  1689. gpiomode = true;
  1690. /*
  1691. * The SLPM_* values are normal values + 1 to allow zero to
  1692. * mean "same as normal".
  1693. */
  1694. if (slpm_pull)
  1695. pull = slpm_pull - 1;
  1696. if (slpm_output)
  1697. output = slpm_output - 1;
  1698. if (slpm_val)
  1699. val = slpm_val - 1;
  1700. dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
  1701. pin,
  1702. slpm_pull ? pullnames[pull] : "same",
  1703. slpm_output ? (output ? "output" : "input") : "same",
  1704. slpm_val ? (val ? "high" : "low") : "same");
  1705. }
  1706. dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
  1707. pin, cfg, pullnames[pull], slpmnames[slpm],
  1708. output ? "output " : "input",
  1709. output ? (val ? "high" : "low") : "",
  1710. lowemi ? "on" : "off");
  1711. clk_enable(nmk_chip->clk);
  1712. bit = pin % NMK_GPIO_PER_CHIP;
  1713. if (gpiomode)
  1714. /* No glitch when going to GPIO mode */
  1715. __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
  1716. if (output)
  1717. __nmk_gpio_make_output(nmk_chip, bit, val);
  1718. else {
  1719. __nmk_gpio_make_input(nmk_chip, bit);
  1720. __nmk_gpio_set_pull(nmk_chip, bit, pull);
  1721. }
  1722. /* TODO: isn't this only applicable on output pins? */
  1723. __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
  1724. __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
  1725. clk_disable(nmk_chip->clk);
  1726. return 0;
  1727. }
  1728. static const struct pinconf_ops nmk_pinconf_ops = {
  1729. .pin_config_get = nmk_pin_config_get,
  1730. .pin_config_set = nmk_pin_config_set,
  1731. };
  1732. static struct pinctrl_desc nmk_pinctrl_desc = {
  1733. .name = "pinctrl-nomadik",
  1734. .pctlops = &nmk_pinctrl_ops,
  1735. .pmxops = &nmk_pinmux_ops,
  1736. .confops = &nmk_pinconf_ops,
  1737. .owner = THIS_MODULE,
  1738. };
  1739. static const struct of_device_id nmk_pinctrl_match[] = {
  1740. {
  1741. .compatible = "stericsson,nmk-pinctrl-stn8815",
  1742. .data = (void *)PINCTRL_NMK_STN8815,
  1743. },
  1744. {
  1745. .compatible = "stericsson,nmk-pinctrl",
  1746. .data = (void *)PINCTRL_NMK_DB8500,
  1747. },
  1748. {
  1749. .compatible = "stericsson,nmk-pinctrl-db8540",
  1750. .data = (void *)PINCTRL_NMK_DB8540,
  1751. },
  1752. {},
  1753. };
  1754. static int nmk_pinctrl_suspend(struct platform_device *pdev, pm_message_t state)
  1755. {
  1756. struct nmk_pinctrl *npct;
  1757. npct = platform_get_drvdata(pdev);
  1758. if (!npct)
  1759. return -EINVAL;
  1760. return pinctrl_force_sleep(npct->pctl);
  1761. }
  1762. static int nmk_pinctrl_resume(struct platform_device *pdev)
  1763. {
  1764. struct nmk_pinctrl *npct;
  1765. npct = platform_get_drvdata(pdev);
  1766. if (!npct)
  1767. return -EINVAL;
  1768. return pinctrl_force_default(npct->pctl);
  1769. }
  1770. static int nmk_pinctrl_probe(struct platform_device *pdev)
  1771. {
  1772. const struct platform_device_id *platid = platform_get_device_id(pdev);
  1773. struct device_node *np = pdev->dev.of_node;
  1774. struct device_node *prcm_np;
  1775. struct nmk_pinctrl *npct;
  1776. struct resource *res;
  1777. unsigned int version = 0;
  1778. int i;
  1779. npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
  1780. if (!npct)
  1781. return -ENOMEM;
  1782. if (platid)
  1783. version = platid->driver_data;
  1784. else if (np) {
  1785. const struct of_device_id *match;
  1786. match = of_match_device(nmk_pinctrl_match, &pdev->dev);
  1787. if (!match)
  1788. return -ENODEV;
  1789. version = (unsigned int) match->data;
  1790. }
  1791. /* Poke in other ASIC variants here */
  1792. if (version == PINCTRL_NMK_STN8815)
  1793. nmk_pinctrl_stn8815_init(&npct->soc);
  1794. if (version == PINCTRL_NMK_DB8500)
  1795. nmk_pinctrl_db8500_init(&npct->soc);
  1796. if (version == PINCTRL_NMK_DB8540)
  1797. nmk_pinctrl_db8540_init(&npct->soc);
  1798. if (np) {
  1799. prcm_np = of_parse_phandle(np, "prcm", 0);
  1800. if (prcm_np)
  1801. npct->prcm_base = of_iomap(prcm_np, 0);
  1802. }
  1803. /* Allow platform passed information to over-write DT. */
  1804. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1805. if (res)
  1806. npct->prcm_base = devm_ioremap(&pdev->dev, res->start,
  1807. resource_size(res));
  1808. if (!npct->prcm_base) {
  1809. if (version == PINCTRL_NMK_STN8815) {
  1810. dev_info(&pdev->dev,
  1811. "No PRCM base, "
  1812. "assuming no ALT-Cx control is available\n");
  1813. } else {
  1814. dev_err(&pdev->dev, "missing PRCM base address\n");
  1815. return -EINVAL;
  1816. }
  1817. }
  1818. /*
  1819. * We need all the GPIO drivers to probe FIRST, or we will not be able
  1820. * to obtain references to the struct gpio_chip * for them, and we
  1821. * need this to proceed.
  1822. */
  1823. for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
  1824. if (!nmk_gpio_chips[npct->soc->gpio_ranges[i].id]) {
  1825. dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
  1826. return -EPROBE_DEFER;
  1827. }
  1828. npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[npct->soc->gpio_ranges[i].id]->chip;
  1829. }
  1830. nmk_pinctrl_desc.pins = npct->soc->pins;
  1831. nmk_pinctrl_desc.npins = npct->soc->npins;
  1832. npct->dev = &pdev->dev;
  1833. npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
  1834. if (!npct->pctl) {
  1835. dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
  1836. return -EINVAL;
  1837. }
  1838. /* We will handle a range of GPIO pins */
  1839. for (i = 0; i < npct->soc->gpio_num_ranges; i++)
  1840. pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
  1841. platform_set_drvdata(pdev, npct);
  1842. dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
  1843. return 0;
  1844. }
  1845. static const struct of_device_id nmk_gpio_match[] = {
  1846. { .compatible = "st,nomadik-gpio", },
  1847. {}
  1848. };
  1849. static struct platform_driver nmk_gpio_driver = {
  1850. .driver = {
  1851. .owner = THIS_MODULE,
  1852. .name = "gpio",
  1853. .of_match_table = nmk_gpio_match,
  1854. },
  1855. .probe = nmk_gpio_probe,
  1856. };
  1857. static const struct platform_device_id nmk_pinctrl_id[] = {
  1858. { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
  1859. { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
  1860. { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
  1861. { }
  1862. };
  1863. static struct platform_driver nmk_pinctrl_driver = {
  1864. .driver = {
  1865. .owner = THIS_MODULE,
  1866. .name = "pinctrl-nomadik",
  1867. .of_match_table = nmk_pinctrl_match,
  1868. },
  1869. .probe = nmk_pinctrl_probe,
  1870. .id_table = nmk_pinctrl_id,
  1871. #ifdef CONFIG_PM
  1872. .suspend = nmk_pinctrl_suspend,
  1873. .resume = nmk_pinctrl_resume,
  1874. #endif
  1875. };
  1876. static int __init nmk_gpio_init(void)
  1877. {
  1878. int ret;
  1879. ret = platform_driver_register(&nmk_gpio_driver);
  1880. if (ret)
  1881. return ret;
  1882. return platform_driver_register(&nmk_pinctrl_driver);
  1883. }
  1884. core_initcall(nmk_gpio_init);
  1885. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  1886. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  1887. MODULE_LICENSE("GPL");