pinctrl-nomadik.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276
  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 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 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 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. };
  1750. static int nmk_pinctrl_suspend(struct platform_device *pdev, pm_message_t state)
  1751. {
  1752. struct nmk_pinctrl *npct;
  1753. npct = platform_get_drvdata(pdev);
  1754. if (!npct)
  1755. return -EINVAL;
  1756. return pinctrl_force_sleep(npct->pctl);
  1757. }
  1758. static int nmk_pinctrl_resume(struct platform_device *pdev)
  1759. {
  1760. struct nmk_pinctrl *npct;
  1761. npct = platform_get_drvdata(pdev);
  1762. if (!npct)
  1763. return -EINVAL;
  1764. return pinctrl_force_default(npct->pctl);
  1765. }
  1766. static int nmk_pinctrl_probe(struct platform_device *pdev)
  1767. {
  1768. const struct platform_device_id *platid = platform_get_device_id(pdev);
  1769. struct device_node *np = pdev->dev.of_node;
  1770. struct device_node *prcm_np;
  1771. struct nmk_pinctrl *npct;
  1772. struct resource *res;
  1773. unsigned int version = 0;
  1774. int i;
  1775. npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
  1776. if (!npct)
  1777. return -ENOMEM;
  1778. if (platid)
  1779. version = platid->driver_data;
  1780. else if (np) {
  1781. const struct of_device_id *match;
  1782. match = of_match_device(nmk_pinctrl_match, &pdev->dev);
  1783. if (!match)
  1784. return -ENODEV;
  1785. version = (unsigned int) match->data;
  1786. }
  1787. /* Poke in other ASIC variants here */
  1788. if (version == PINCTRL_NMK_STN8815)
  1789. nmk_pinctrl_stn8815_init(&npct->soc);
  1790. if (version == PINCTRL_NMK_DB8500)
  1791. nmk_pinctrl_db8500_init(&npct->soc);
  1792. if (version == PINCTRL_NMK_DB8540)
  1793. nmk_pinctrl_db8540_init(&npct->soc);
  1794. if (np) {
  1795. prcm_np = of_parse_phandle(np, "prcm", 0);
  1796. if (prcm_np)
  1797. npct->prcm_base = of_iomap(prcm_np, 0);
  1798. }
  1799. /* Allow platform passed information to over-write DT. */
  1800. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1801. if (res)
  1802. npct->prcm_base = devm_ioremap(&pdev->dev, res->start,
  1803. resource_size(res));
  1804. if (!npct->prcm_base) {
  1805. if (version == PINCTRL_NMK_STN8815) {
  1806. dev_info(&pdev->dev,
  1807. "No PRCM base, "
  1808. "assuming no ALT-Cx control is available\n");
  1809. } else {
  1810. dev_err(&pdev->dev, "missing PRCM base address\n");
  1811. return -EINVAL;
  1812. }
  1813. }
  1814. /*
  1815. * We need all the GPIO drivers to probe FIRST, or we will not be able
  1816. * to obtain references to the struct gpio_chip * for them, and we
  1817. * need this to proceed.
  1818. */
  1819. for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
  1820. if (!nmk_gpio_chips[npct->soc->gpio_ranges[i].id]) {
  1821. dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
  1822. return -EPROBE_DEFER;
  1823. }
  1824. npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[npct->soc->gpio_ranges[i].id]->chip;
  1825. }
  1826. nmk_pinctrl_desc.pins = npct->soc->pins;
  1827. nmk_pinctrl_desc.npins = npct->soc->npins;
  1828. npct->dev = &pdev->dev;
  1829. npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
  1830. if (!npct->pctl) {
  1831. dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
  1832. return -EINVAL;
  1833. }
  1834. /* We will handle a range of GPIO pins */
  1835. for (i = 0; i < npct->soc->gpio_num_ranges; i++)
  1836. pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
  1837. platform_set_drvdata(pdev, npct);
  1838. dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
  1839. return 0;
  1840. }
  1841. static const struct of_device_id nmk_gpio_match[] = {
  1842. { .compatible = "st,nomadik-gpio", },
  1843. {}
  1844. };
  1845. static struct platform_driver nmk_gpio_driver = {
  1846. .driver = {
  1847. .owner = THIS_MODULE,
  1848. .name = "gpio",
  1849. .of_match_table = nmk_gpio_match,
  1850. },
  1851. .probe = nmk_gpio_probe,
  1852. };
  1853. static const struct platform_device_id nmk_pinctrl_id[] = {
  1854. { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
  1855. { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
  1856. { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
  1857. { }
  1858. };
  1859. static struct platform_driver nmk_pinctrl_driver = {
  1860. .driver = {
  1861. .owner = THIS_MODULE,
  1862. .name = "pinctrl-nomadik",
  1863. .of_match_table = nmk_pinctrl_match,
  1864. },
  1865. .probe = nmk_pinctrl_probe,
  1866. .id_table = nmk_pinctrl_id,
  1867. #ifdef CONFIG_PM
  1868. .suspend = nmk_pinctrl_suspend,
  1869. .resume = nmk_pinctrl_resume,
  1870. #endif
  1871. };
  1872. static int __init nmk_gpio_init(void)
  1873. {
  1874. int ret;
  1875. ret = platform_driver_register(&nmk_gpio_driver);
  1876. if (ret)
  1877. return ret;
  1878. return platform_driver_register(&nmk_pinctrl_driver);
  1879. }
  1880. core_initcall(nmk_gpio_init);
  1881. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  1882. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  1883. MODULE_LICENSE("GPL");