pinctrl-nomadik.c 50 KB

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