pinctrl-nomadik.c 50 KB

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