pinctrl-nomadik.c 50 KB

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