pinctrl-nomadik.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  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. /*
  34. * For the U8500 archs, use the PRCMU register interface, for the older
  35. * Nomadik, provide some stubs. The functions using these will only be
  36. * called on the U8500 series.
  37. */
  38. #ifdef CONFIG_ARCH_U8500
  39. #include <linux/mfd/dbx500-prcmu.h>
  40. #else
  41. static inline u32 prcmu_read(unsigned int reg) {
  42. return 0;
  43. }
  44. static inline void prcmu_write(unsigned int reg, u32 value) {}
  45. static inline void prcmu_write_masked(unsigned int reg, u32 mask, u32 value) {}
  46. #endif
  47. #include <linux/platform_data/pinctrl-nomadik.h>
  48. #include <asm/mach/irq.h>
  49. #include <mach/irqs.h>
  50. #include "pinctrl-nomadik.h"
  51. /*
  52. * The GPIO module in the Nomadik family of Systems-on-Chip is an
  53. * AMBA device, managing 32 pins and alternate functions. The logic block
  54. * is currently used in the Nomadik and ux500.
  55. *
  56. * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
  57. */
  58. struct nmk_gpio_chip {
  59. struct gpio_chip chip;
  60. struct irq_domain *domain;
  61. void __iomem *addr;
  62. struct clk *clk;
  63. unsigned int bank;
  64. unsigned int parent_irq;
  65. int secondary_parent_irq;
  66. u32 (*get_secondary_status)(unsigned int bank);
  67. void (*set_ioforce)(bool enable);
  68. spinlock_t lock;
  69. bool sleepmode;
  70. /* Keep track of configured edges */
  71. u32 edge_rising;
  72. u32 edge_falling;
  73. u32 real_wake;
  74. u32 rwimsc;
  75. u32 fwimsc;
  76. u32 rimsc;
  77. u32 fimsc;
  78. u32 pull_up;
  79. u32 lowemi;
  80. };
  81. struct nmk_pinctrl {
  82. struct device *dev;
  83. struct pinctrl_dev *pctl;
  84. const struct nmk_pinctrl_soc_data *soc;
  85. };
  86. static struct nmk_gpio_chip *
  87. nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
  88. static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
  89. #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
  90. static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
  91. unsigned offset, int gpio_mode)
  92. {
  93. u32 bit = 1 << offset;
  94. u32 afunc, bfunc;
  95. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
  96. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
  97. if (gpio_mode & NMK_GPIO_ALT_A)
  98. afunc |= bit;
  99. if (gpio_mode & NMK_GPIO_ALT_B)
  100. bfunc |= bit;
  101. writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
  102. writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
  103. }
  104. static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
  105. unsigned offset, enum nmk_gpio_slpm mode)
  106. {
  107. u32 bit = 1 << offset;
  108. u32 slpm;
  109. slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
  110. if (mode == NMK_GPIO_SLPM_NOCHANGE)
  111. slpm |= bit;
  112. else
  113. slpm &= ~bit;
  114. writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
  115. }
  116. static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
  117. unsigned offset, enum nmk_gpio_pull pull)
  118. {
  119. u32 bit = 1 << offset;
  120. u32 pdis;
  121. pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
  122. if (pull == NMK_GPIO_PULL_NONE) {
  123. pdis |= bit;
  124. nmk_chip->pull_up &= ~bit;
  125. } else {
  126. pdis &= ~bit;
  127. }
  128. writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
  129. if (pull == NMK_GPIO_PULL_UP) {
  130. nmk_chip->pull_up |= bit;
  131. writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
  132. } else if (pull == NMK_GPIO_PULL_DOWN) {
  133. nmk_chip->pull_up &= ~bit;
  134. writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
  135. }
  136. }
  137. static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
  138. unsigned offset, bool lowemi)
  139. {
  140. u32 bit = BIT(offset);
  141. bool enabled = nmk_chip->lowemi & bit;
  142. if (lowemi == enabled)
  143. return;
  144. if (lowemi)
  145. nmk_chip->lowemi |= bit;
  146. else
  147. nmk_chip->lowemi &= ~bit;
  148. writel_relaxed(nmk_chip->lowemi,
  149. nmk_chip->addr + NMK_GPIO_LOWEMI);
  150. }
  151. static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
  152. unsigned offset)
  153. {
  154. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  155. }
  156. static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
  157. unsigned offset, int val)
  158. {
  159. if (val)
  160. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
  161. else
  162. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
  163. }
  164. static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
  165. unsigned offset, int val)
  166. {
  167. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
  168. __nmk_gpio_set_output(nmk_chip, offset, val);
  169. }
  170. static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
  171. unsigned offset, int gpio_mode,
  172. bool glitch)
  173. {
  174. u32 rwimsc = nmk_chip->rwimsc;
  175. u32 fwimsc = nmk_chip->fwimsc;
  176. if (glitch && nmk_chip->set_ioforce) {
  177. u32 bit = BIT(offset);
  178. /* Prevent spurious wakeups */
  179. writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
  180. writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
  181. nmk_chip->set_ioforce(true);
  182. }
  183. __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
  184. if (glitch && nmk_chip->set_ioforce) {
  185. nmk_chip->set_ioforce(false);
  186. writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
  187. writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
  188. }
  189. }
  190. static void
  191. nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
  192. {
  193. u32 falling = nmk_chip->fimsc & BIT(offset);
  194. u32 rising = nmk_chip->rimsc & BIT(offset);
  195. int gpio = nmk_chip->chip.base + offset;
  196. int irq = NOMADIK_GPIO_TO_IRQ(gpio);
  197. struct irq_data *d = irq_get_irq_data(irq);
  198. if (!rising && !falling)
  199. return;
  200. if (!d || !irqd_irq_disabled(d))
  201. return;
  202. if (rising) {
  203. nmk_chip->rimsc &= ~BIT(offset);
  204. writel_relaxed(nmk_chip->rimsc,
  205. nmk_chip->addr + NMK_GPIO_RIMSC);
  206. }
  207. if (falling) {
  208. nmk_chip->fimsc &= ~BIT(offset);
  209. writel_relaxed(nmk_chip->fimsc,
  210. nmk_chip->addr + NMK_GPIO_FIMSC);
  211. }
  212. dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
  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 (prcmu_read(reg) & BIT(bit)) {
  249. prcmu_write_masked(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 (prcmu_read(reg) & BIT(bit)) {
  276. prcmu_write_masked(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. prcmu_write_masked(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. int nmk_gpio_get_mode(int gpio)
  579. {
  580. struct nmk_gpio_chip *nmk_chip;
  581. u32 afunc, bfunc, bit;
  582. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  583. if (!nmk_chip)
  584. return -EINVAL;
  585. bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
  586. clk_enable(nmk_chip->clk);
  587. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
  588. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
  589. clk_disable(nmk_chip->clk);
  590. return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
  591. }
  592. EXPORT_SYMBOL(nmk_gpio_get_mode);
  593. /* IRQ functions */
  594. static inline int nmk_gpio_get_bitmask(int gpio)
  595. {
  596. return 1 << (gpio % NMK_GPIO_PER_CHIP);
  597. }
  598. static void nmk_gpio_irq_ack(struct irq_data *d)
  599. {
  600. struct nmk_gpio_chip *nmk_chip;
  601. nmk_chip = irq_data_get_irq_chip_data(d);
  602. if (!nmk_chip)
  603. return;
  604. clk_enable(nmk_chip->clk);
  605. writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
  606. clk_disable(nmk_chip->clk);
  607. }
  608. enum nmk_gpio_irq_type {
  609. NORMAL,
  610. WAKE,
  611. };
  612. static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
  613. int gpio, enum nmk_gpio_irq_type which,
  614. bool enable)
  615. {
  616. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  617. u32 *rimscval;
  618. u32 *fimscval;
  619. u32 rimscreg;
  620. u32 fimscreg;
  621. if (which == NORMAL) {
  622. rimscreg = NMK_GPIO_RIMSC;
  623. fimscreg = NMK_GPIO_FIMSC;
  624. rimscval = &nmk_chip->rimsc;
  625. fimscval = &nmk_chip->fimsc;
  626. } else {
  627. rimscreg = NMK_GPIO_RWIMSC;
  628. fimscreg = NMK_GPIO_FWIMSC;
  629. rimscval = &nmk_chip->rwimsc;
  630. fimscval = &nmk_chip->fwimsc;
  631. }
  632. /* we must individually set/clear the two edges */
  633. if (nmk_chip->edge_rising & bitmask) {
  634. if (enable)
  635. *rimscval |= bitmask;
  636. else
  637. *rimscval &= ~bitmask;
  638. writel(*rimscval, nmk_chip->addr + rimscreg);
  639. }
  640. if (nmk_chip->edge_falling & bitmask) {
  641. if (enable)
  642. *fimscval |= bitmask;
  643. else
  644. *fimscval &= ~bitmask;
  645. writel(*fimscval, nmk_chip->addr + fimscreg);
  646. }
  647. }
  648. static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
  649. int gpio, bool on)
  650. {
  651. /*
  652. * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
  653. * disabled, since setting SLPM to 1 increases power consumption, and
  654. * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
  655. */
  656. if (nmk_chip->sleepmode && on) {
  657. __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
  658. NMK_GPIO_SLPM_WAKEUP_ENABLE);
  659. }
  660. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
  661. }
  662. static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
  663. {
  664. struct nmk_gpio_chip *nmk_chip;
  665. unsigned long flags;
  666. u32 bitmask;
  667. nmk_chip = irq_data_get_irq_chip_data(d);
  668. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  669. if (!nmk_chip)
  670. return -EINVAL;
  671. clk_enable(nmk_chip->clk);
  672. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  673. spin_lock(&nmk_chip->lock);
  674. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
  675. if (!(nmk_chip->real_wake & bitmask))
  676. __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
  677. spin_unlock(&nmk_chip->lock);
  678. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  679. clk_disable(nmk_chip->clk);
  680. return 0;
  681. }
  682. static void nmk_gpio_irq_mask(struct irq_data *d)
  683. {
  684. nmk_gpio_irq_maskunmask(d, false);
  685. }
  686. static void nmk_gpio_irq_unmask(struct irq_data *d)
  687. {
  688. nmk_gpio_irq_maskunmask(d, true);
  689. }
  690. static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  691. {
  692. struct nmk_gpio_chip *nmk_chip;
  693. unsigned long flags;
  694. u32 bitmask;
  695. nmk_chip = irq_data_get_irq_chip_data(d);
  696. if (!nmk_chip)
  697. return -EINVAL;
  698. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  699. clk_enable(nmk_chip->clk);
  700. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  701. spin_lock(&nmk_chip->lock);
  702. if (irqd_irq_disabled(d))
  703. __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
  704. if (on)
  705. nmk_chip->real_wake |= bitmask;
  706. else
  707. nmk_chip->real_wake &= ~bitmask;
  708. spin_unlock(&nmk_chip->lock);
  709. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  710. clk_disable(nmk_chip->clk);
  711. return 0;
  712. }
  713. static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  714. {
  715. bool enabled = !irqd_irq_disabled(d);
  716. bool wake = irqd_is_wakeup_set(d);
  717. struct nmk_gpio_chip *nmk_chip;
  718. unsigned long flags;
  719. u32 bitmask;
  720. nmk_chip = irq_data_get_irq_chip_data(d);
  721. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  722. if (!nmk_chip)
  723. return -EINVAL;
  724. if (type & IRQ_TYPE_LEVEL_HIGH)
  725. return -EINVAL;
  726. if (type & IRQ_TYPE_LEVEL_LOW)
  727. return -EINVAL;
  728. clk_enable(nmk_chip->clk);
  729. spin_lock_irqsave(&nmk_chip->lock, flags);
  730. if (enabled)
  731. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
  732. if (enabled || wake)
  733. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
  734. nmk_chip->edge_rising &= ~bitmask;
  735. if (type & IRQ_TYPE_EDGE_RISING)
  736. nmk_chip->edge_rising |= bitmask;
  737. nmk_chip->edge_falling &= ~bitmask;
  738. if (type & IRQ_TYPE_EDGE_FALLING)
  739. nmk_chip->edge_falling |= bitmask;
  740. if (enabled)
  741. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
  742. if (enabled || wake)
  743. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
  744. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  745. clk_disable(nmk_chip->clk);
  746. return 0;
  747. }
  748. static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
  749. {
  750. struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
  751. clk_enable(nmk_chip->clk);
  752. nmk_gpio_irq_unmask(d);
  753. return 0;
  754. }
  755. static void nmk_gpio_irq_shutdown(struct irq_data *d)
  756. {
  757. struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
  758. nmk_gpio_irq_mask(d);
  759. clk_disable(nmk_chip->clk);
  760. }
  761. static struct irq_chip nmk_gpio_irq_chip = {
  762. .name = "Nomadik-GPIO",
  763. .irq_ack = nmk_gpio_irq_ack,
  764. .irq_mask = nmk_gpio_irq_mask,
  765. .irq_unmask = nmk_gpio_irq_unmask,
  766. .irq_set_type = nmk_gpio_irq_set_type,
  767. .irq_set_wake = nmk_gpio_irq_set_wake,
  768. .irq_startup = nmk_gpio_irq_startup,
  769. .irq_shutdown = nmk_gpio_irq_shutdown,
  770. .flags = IRQCHIP_MASK_ON_SUSPEND,
  771. };
  772. static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
  773. u32 status)
  774. {
  775. struct nmk_gpio_chip *nmk_chip;
  776. struct irq_chip *host_chip = irq_get_chip(irq);
  777. chained_irq_enter(host_chip, desc);
  778. nmk_chip = irq_get_handler_data(irq);
  779. while (status) {
  780. int bit = __ffs(status);
  781. generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
  782. status &= ~BIT(bit);
  783. }
  784. chained_irq_exit(host_chip, desc);
  785. }
  786. static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  787. {
  788. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  789. u32 status;
  790. clk_enable(nmk_chip->clk);
  791. status = readl(nmk_chip->addr + NMK_GPIO_IS);
  792. clk_disable(nmk_chip->clk);
  793. __nmk_gpio_irq_handler(irq, desc, status);
  794. }
  795. static void nmk_gpio_secondary_irq_handler(unsigned int irq,
  796. struct irq_desc *desc)
  797. {
  798. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  799. u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
  800. __nmk_gpio_irq_handler(irq, desc, status);
  801. }
  802. static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
  803. {
  804. irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
  805. irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
  806. if (nmk_chip->secondary_parent_irq >= 0) {
  807. irq_set_chained_handler(nmk_chip->secondary_parent_irq,
  808. nmk_gpio_secondary_irq_handler);
  809. irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
  810. }
  811. return 0;
  812. }
  813. /* I/O Functions */
  814. static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
  815. {
  816. /*
  817. * Map back to global GPIO space and request muxing, the direction
  818. * parameter does not matter for this controller.
  819. */
  820. int gpio = chip->base + offset;
  821. return pinctrl_request_gpio(gpio);
  822. }
  823. static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
  824. {
  825. int gpio = chip->base + offset;
  826. pinctrl_free_gpio(gpio);
  827. }
  828. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
  829. {
  830. struct nmk_gpio_chip *nmk_chip =
  831. container_of(chip, struct nmk_gpio_chip, chip);
  832. clk_enable(nmk_chip->clk);
  833. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  834. clk_disable(nmk_chip->clk);
  835. return 0;
  836. }
  837. static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
  838. {
  839. struct nmk_gpio_chip *nmk_chip =
  840. container_of(chip, struct nmk_gpio_chip, chip);
  841. u32 bit = 1 << offset;
  842. int value;
  843. clk_enable(nmk_chip->clk);
  844. value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  845. clk_disable(nmk_chip->clk);
  846. return value;
  847. }
  848. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  849. int val)
  850. {
  851. struct nmk_gpio_chip *nmk_chip =
  852. container_of(chip, struct nmk_gpio_chip, chip);
  853. clk_enable(nmk_chip->clk);
  854. __nmk_gpio_set_output(nmk_chip, offset, val);
  855. clk_disable(nmk_chip->clk);
  856. }
  857. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
  858. int val)
  859. {
  860. struct nmk_gpio_chip *nmk_chip =
  861. container_of(chip, struct nmk_gpio_chip, chip);
  862. clk_enable(nmk_chip->clk);
  863. __nmk_gpio_make_output(nmk_chip, offset, val);
  864. clk_disable(nmk_chip->clk);
  865. return 0;
  866. }
  867. static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  868. {
  869. struct nmk_gpio_chip *nmk_chip =
  870. container_of(chip, struct nmk_gpio_chip, chip);
  871. return irq_create_mapping(nmk_chip->domain, offset);
  872. }
  873. #ifdef CONFIG_DEBUG_FS
  874. #include <linux/seq_file.h>
  875. static void nmk_gpio_dbg_show_one(struct seq_file *s, struct gpio_chip *chip,
  876. unsigned offset, unsigned gpio)
  877. {
  878. const char *label = gpiochip_is_requested(chip, offset);
  879. struct nmk_gpio_chip *nmk_chip =
  880. container_of(chip, struct nmk_gpio_chip, chip);
  881. int mode;
  882. bool is_out;
  883. bool pull;
  884. u32 bit = 1 << offset;
  885. const char *modes[] = {
  886. [NMK_GPIO_ALT_GPIO] = "gpio",
  887. [NMK_GPIO_ALT_A] = "altA",
  888. [NMK_GPIO_ALT_B] = "altB",
  889. [NMK_GPIO_ALT_C] = "altC",
  890. };
  891. clk_enable(nmk_chip->clk);
  892. is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
  893. pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
  894. mode = nmk_gpio_get_mode(gpio);
  895. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
  896. gpio, label ?: "(none)",
  897. is_out ? "out" : "in ",
  898. chip->get
  899. ? (chip->get(chip, offset) ? "hi" : "lo")
  900. : "? ",
  901. (mode < 0) ? "unknown" : modes[mode],
  902. pull ? "pull" : "none");
  903. if (label && !is_out) {
  904. int irq = gpio_to_irq(gpio);
  905. struct irq_desc *desc = irq_to_desc(irq);
  906. /* This races with request_irq(), set_irq_type(),
  907. * and set_irq_wake() ... but those are "rare".
  908. */
  909. if (irq >= 0 && desc->action) {
  910. char *trigger;
  911. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  912. if (nmk_chip->edge_rising & bitmask)
  913. trigger = "edge-rising";
  914. else if (nmk_chip->edge_falling & bitmask)
  915. trigger = "edge-falling";
  916. else
  917. trigger = "edge-undefined";
  918. seq_printf(s, " irq-%d %s%s",
  919. irq, trigger,
  920. irqd_is_wakeup_set(&desc->irq_data)
  921. ? " wakeup" : "");
  922. }
  923. }
  924. clk_disable(nmk_chip->clk);
  925. }
  926. static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  927. {
  928. unsigned i;
  929. unsigned gpio = chip->base;
  930. for (i = 0; i < chip->ngpio; i++, gpio++) {
  931. nmk_gpio_dbg_show_one(s, chip, i, gpio);
  932. seq_printf(s, "\n");
  933. }
  934. }
  935. #else
  936. static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
  937. struct gpio_chip *chip,
  938. unsigned offset, unsigned gpio)
  939. {
  940. }
  941. #define nmk_gpio_dbg_show NULL
  942. #endif
  943. /* This structure is replicated for each GPIO block allocated at probe time */
  944. static struct gpio_chip nmk_gpio_template = {
  945. .request = nmk_gpio_request,
  946. .free = nmk_gpio_free,
  947. .direction_input = nmk_gpio_make_input,
  948. .get = nmk_gpio_get_input,
  949. .direction_output = nmk_gpio_make_output,
  950. .set = nmk_gpio_set_output,
  951. .to_irq = nmk_gpio_to_irq,
  952. .dbg_show = nmk_gpio_dbg_show,
  953. .can_sleep = 0,
  954. };
  955. void nmk_gpio_clocks_enable(void)
  956. {
  957. int i;
  958. for (i = 0; i < NUM_BANKS; i++) {
  959. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  960. if (!chip)
  961. continue;
  962. clk_enable(chip->clk);
  963. }
  964. }
  965. void nmk_gpio_clocks_disable(void)
  966. {
  967. int i;
  968. for (i = 0; i < NUM_BANKS; i++) {
  969. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  970. if (!chip)
  971. continue;
  972. clk_disable(chip->clk);
  973. }
  974. }
  975. /*
  976. * Called from the suspend/resume path to only keep the real wakeup interrupts
  977. * (those that have had set_irq_wake() called on them) as wakeup interrupts,
  978. * and not the rest of the interrupts which we needed to have as wakeups for
  979. * cpuidle.
  980. *
  981. * PM ops are not used since this needs to be done at the end, after all the
  982. * other drivers are done with their suspend callbacks.
  983. */
  984. void nmk_gpio_wakeups_suspend(void)
  985. {
  986. int i;
  987. for (i = 0; i < NUM_BANKS; i++) {
  988. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  989. if (!chip)
  990. break;
  991. clk_enable(chip->clk);
  992. writel(chip->rwimsc & chip->real_wake,
  993. chip->addr + NMK_GPIO_RWIMSC);
  994. writel(chip->fwimsc & chip->real_wake,
  995. chip->addr + NMK_GPIO_FWIMSC);
  996. clk_disable(chip->clk);
  997. }
  998. }
  999. void nmk_gpio_wakeups_resume(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. break;
  1006. clk_enable(chip->clk);
  1007. writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
  1008. writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
  1009. clk_disable(chip->clk);
  1010. }
  1011. }
  1012. /*
  1013. * Read the pull up/pull down status.
  1014. * A bit set in 'pull_up' means that pull up
  1015. * is selected if pull is enabled in PDIS register.
  1016. * Note: only pull up/down set via this driver can
  1017. * be detected due to HW limitations.
  1018. */
  1019. void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
  1020. {
  1021. if (gpio_bank < NUM_BANKS) {
  1022. struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
  1023. if (!chip)
  1024. return;
  1025. *pull_up = chip->pull_up;
  1026. }
  1027. }
  1028. int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
  1029. irq_hw_number_t hwirq)
  1030. {
  1031. struct nmk_gpio_chip *nmk_chip = d->host_data;
  1032. if (!nmk_chip)
  1033. return -EINVAL;
  1034. irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
  1035. set_irq_flags(irq, IRQF_VALID);
  1036. irq_set_chip_data(irq, nmk_chip);
  1037. irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
  1038. return 0;
  1039. }
  1040. const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
  1041. .map = nmk_gpio_irq_map,
  1042. .xlate = irq_domain_xlate_twocell,
  1043. };
  1044. static int __devinit nmk_gpio_probe(struct platform_device *dev)
  1045. {
  1046. struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
  1047. struct device_node *np = dev->dev.of_node;
  1048. struct nmk_gpio_chip *nmk_chip;
  1049. struct gpio_chip *chip;
  1050. struct resource *res;
  1051. struct clk *clk;
  1052. int secondary_irq;
  1053. void __iomem *base;
  1054. int irq_start = 0;
  1055. int irq;
  1056. int ret;
  1057. if (!pdata && !np) {
  1058. dev_err(&dev->dev, "No platform data or device tree found\n");
  1059. return -ENODEV;
  1060. }
  1061. if (np) {
  1062. pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
  1063. if (!pdata)
  1064. return -ENOMEM;
  1065. if (of_get_property(np, "st,supports-sleepmode", NULL))
  1066. pdata->supports_sleepmode = true;
  1067. if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
  1068. dev_err(&dev->dev, "gpio-bank property not found\n");
  1069. ret = -EINVAL;
  1070. goto out;
  1071. }
  1072. pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
  1073. pdata->num_gpio = NMK_GPIO_PER_CHIP;
  1074. }
  1075. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1076. if (!res) {
  1077. ret = -ENOENT;
  1078. goto out;
  1079. }
  1080. irq = platform_get_irq(dev, 0);
  1081. if (irq < 0) {
  1082. ret = irq;
  1083. goto out;
  1084. }
  1085. secondary_irq = platform_get_irq(dev, 1);
  1086. if (secondary_irq >= 0 && !pdata->get_secondary_status) {
  1087. ret = -EINVAL;
  1088. goto out;
  1089. }
  1090. base = devm_request_and_ioremap(&dev->dev, res);
  1091. if (!base) {
  1092. ret = -ENOMEM;
  1093. goto out;
  1094. }
  1095. clk = devm_clk_get(&dev->dev, NULL);
  1096. if (IS_ERR(clk)) {
  1097. ret = PTR_ERR(clk);
  1098. goto out;
  1099. }
  1100. clk_prepare(clk);
  1101. nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
  1102. if (!nmk_chip) {
  1103. ret = -ENOMEM;
  1104. goto out;
  1105. }
  1106. /*
  1107. * The virt address in nmk_chip->addr is in the nomadik register space,
  1108. * so we can simply convert the resource address, without remapping
  1109. */
  1110. nmk_chip->bank = dev->id;
  1111. nmk_chip->clk = clk;
  1112. nmk_chip->addr = base;
  1113. nmk_chip->chip = nmk_gpio_template;
  1114. nmk_chip->parent_irq = irq;
  1115. nmk_chip->secondary_parent_irq = secondary_irq;
  1116. nmk_chip->get_secondary_status = pdata->get_secondary_status;
  1117. nmk_chip->set_ioforce = pdata->set_ioforce;
  1118. nmk_chip->sleepmode = pdata->supports_sleepmode;
  1119. spin_lock_init(&nmk_chip->lock);
  1120. chip = &nmk_chip->chip;
  1121. chip->base = pdata->first_gpio;
  1122. chip->ngpio = pdata->num_gpio;
  1123. chip->label = pdata->name ?: dev_name(&dev->dev);
  1124. chip->dev = &dev->dev;
  1125. chip->owner = THIS_MODULE;
  1126. clk_enable(nmk_chip->clk);
  1127. nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
  1128. clk_disable(nmk_chip->clk);
  1129. #ifdef CONFIG_OF_GPIO
  1130. chip->of_node = np;
  1131. #endif
  1132. ret = gpiochip_add(&nmk_chip->chip);
  1133. if (ret)
  1134. goto out;
  1135. BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
  1136. nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
  1137. platform_set_drvdata(dev, nmk_chip);
  1138. if (!np)
  1139. irq_start = NOMADIK_GPIO_TO_IRQ(pdata->first_gpio);
  1140. nmk_chip->domain = irq_domain_add_simple(np,
  1141. NMK_GPIO_PER_CHIP, irq_start,
  1142. &nmk_gpio_irq_simple_ops, nmk_chip);
  1143. if (!nmk_chip->domain) {
  1144. dev_err(&dev->dev, "failed to create irqdomain\n");
  1145. ret = -ENOSYS;
  1146. goto out;
  1147. }
  1148. nmk_gpio_init_irq(nmk_chip);
  1149. dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
  1150. return 0;
  1151. out:
  1152. dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
  1153. pdata->first_gpio, pdata->first_gpio+31);
  1154. return ret;
  1155. }
  1156. static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
  1157. {
  1158. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1159. return npct->soc->ngroups;
  1160. }
  1161. static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
  1162. unsigned selector)
  1163. {
  1164. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1165. return npct->soc->groups[selector].name;
  1166. }
  1167. static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  1168. const unsigned **pins,
  1169. unsigned *num_pins)
  1170. {
  1171. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1172. *pins = npct->soc->groups[selector].pins;
  1173. *num_pins = npct->soc->groups[selector].npins;
  1174. return 0;
  1175. }
  1176. static struct pinctrl_gpio_range *
  1177. nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
  1178. {
  1179. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1180. int i;
  1181. for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
  1182. struct pinctrl_gpio_range *range;
  1183. range = &npct->soc->gpio_ranges[i];
  1184. if (offset >= range->pin_base &&
  1185. offset <= (range->pin_base + range->npins - 1))
  1186. return range;
  1187. }
  1188. return NULL;
  1189. }
  1190. static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  1191. unsigned offset)
  1192. {
  1193. struct pinctrl_gpio_range *range;
  1194. struct gpio_chip *chip;
  1195. range = nmk_match_gpio_range(pctldev, offset);
  1196. if (!range || !range->gc) {
  1197. seq_printf(s, "invalid pin offset");
  1198. return;
  1199. }
  1200. chip = range->gc;
  1201. nmk_gpio_dbg_show_one(s, chip, offset - chip->base, offset);
  1202. }
  1203. static struct pinctrl_ops nmk_pinctrl_ops = {
  1204. .get_groups_count = nmk_get_groups_cnt,
  1205. .get_group_name = nmk_get_group_name,
  1206. .get_group_pins = nmk_get_group_pins,
  1207. .pin_dbg_show = nmk_pin_dbg_show,
  1208. };
  1209. static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  1210. {
  1211. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1212. return npct->soc->nfunctions;
  1213. }
  1214. static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
  1215. unsigned function)
  1216. {
  1217. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1218. return npct->soc->functions[function].name;
  1219. }
  1220. static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  1221. unsigned function,
  1222. const char * const **groups,
  1223. unsigned * const num_groups)
  1224. {
  1225. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1226. *groups = npct->soc->functions[function].groups;
  1227. *num_groups = npct->soc->functions[function].ngroups;
  1228. return 0;
  1229. }
  1230. static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
  1231. unsigned group)
  1232. {
  1233. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1234. const struct nmk_pingroup *g;
  1235. static unsigned int slpm[NUM_BANKS];
  1236. unsigned long flags;
  1237. bool glitch;
  1238. int ret = -EINVAL;
  1239. int i;
  1240. g = &npct->soc->groups[group];
  1241. if (g->altsetting < 0)
  1242. return -EINVAL;
  1243. dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
  1244. /*
  1245. * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
  1246. * we may pass through an undesired state. In this case we take
  1247. * some extra care.
  1248. *
  1249. * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
  1250. * - Save SLPM registers (since we have a shadow register in the
  1251. * nmk_chip we're using that as backup)
  1252. * - Set SLPM=0 for the IOs you want to switch and others to 1
  1253. * - Configure the GPIO registers for the IOs that are being switched
  1254. * - Set IOFORCE=1
  1255. * - Modify the AFLSA/B registers for the IOs that are being switched
  1256. * - Set IOFORCE=0
  1257. * - Restore SLPM registers
  1258. * - Any spurious wake up event during switch sequence to be ignored
  1259. * and cleared
  1260. *
  1261. * We REALLY need to save ALL slpm registers, because the external
  1262. * IOFORCE will switch *all* ports to their sleepmode setting to as
  1263. * to avoid glitches. (Not just one port!)
  1264. */
  1265. glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
  1266. if (glitch) {
  1267. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  1268. /* Initially don't put any pins to sleep when switching */
  1269. memset(slpm, 0xff, sizeof(slpm));
  1270. /*
  1271. * Then mask the pins that need to be sleeping now when we're
  1272. * switching to the ALT C function.
  1273. */
  1274. for (i = 0; i < g->npins; i++)
  1275. slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
  1276. nmk_gpio_glitch_slpm_init(slpm);
  1277. }
  1278. for (i = 0; i < g->npins; i++) {
  1279. struct pinctrl_gpio_range *range;
  1280. struct nmk_gpio_chip *nmk_chip;
  1281. struct gpio_chip *chip;
  1282. unsigned bit;
  1283. range = nmk_match_gpio_range(pctldev, g->pins[i]);
  1284. if (!range) {
  1285. dev_err(npct->dev,
  1286. "invalid pin offset %d in group %s at index %d\n",
  1287. g->pins[i], g->name, i);
  1288. goto out_glitch;
  1289. }
  1290. if (!range->gc) {
  1291. dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
  1292. g->pins[i], g->name, i);
  1293. goto out_glitch;
  1294. }
  1295. chip = range->gc;
  1296. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1297. dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
  1298. clk_enable(nmk_chip->clk);
  1299. bit = g->pins[i] % NMK_GPIO_PER_CHIP;
  1300. /*
  1301. * If the pin is switching to altfunc, and there was an
  1302. * interrupt installed on it which has been lazy disabled,
  1303. * actually mask the interrupt to prevent spurious interrupts
  1304. * that would occur while the pin is under control of the
  1305. * peripheral. Only SKE does this.
  1306. */
  1307. nmk_gpio_disable_lazy_irq(nmk_chip, bit);
  1308. __nmk_gpio_set_mode_safe(nmk_chip, bit,
  1309. (g->altsetting & NMK_GPIO_ALT_C), glitch);
  1310. clk_disable(nmk_chip->clk);
  1311. /*
  1312. * Call PRCM GPIOCR config function in case ALTC
  1313. * has been selected:
  1314. * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
  1315. * must be set.
  1316. * - If selection is pure ALTC and previous selection was ALTCx,
  1317. * then some bits in PRCM GPIOCR registers must be cleared.
  1318. */
  1319. if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
  1320. nmk_prcm_altcx_set_mode(npct, g->pins[i],
  1321. g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
  1322. }
  1323. /* When all pins are successfully reconfigured we get here */
  1324. ret = 0;
  1325. out_glitch:
  1326. if (glitch) {
  1327. nmk_gpio_glitch_slpm_restore(slpm);
  1328. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  1329. }
  1330. return ret;
  1331. }
  1332. static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
  1333. unsigned function, unsigned group)
  1334. {
  1335. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1336. const struct nmk_pingroup *g;
  1337. g = &npct->soc->groups[group];
  1338. if (g->altsetting < 0)
  1339. return;
  1340. /* Poke out the mux, set the pin to some default state? */
  1341. dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
  1342. }
  1343. int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
  1344. struct pinctrl_gpio_range *range,
  1345. unsigned offset)
  1346. {
  1347. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1348. struct nmk_gpio_chip *nmk_chip;
  1349. struct gpio_chip *chip;
  1350. unsigned bit;
  1351. if (!range) {
  1352. dev_err(npct->dev, "invalid range\n");
  1353. return -EINVAL;
  1354. }
  1355. if (!range->gc) {
  1356. dev_err(npct->dev, "missing GPIO chip in range\n");
  1357. return -EINVAL;
  1358. }
  1359. chip = range->gc;
  1360. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1361. dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
  1362. clk_enable(nmk_chip->clk);
  1363. bit = offset % NMK_GPIO_PER_CHIP;
  1364. /* There is no glitch when converting any pin to GPIO */
  1365. __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
  1366. clk_disable(nmk_chip->clk);
  1367. return 0;
  1368. }
  1369. void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
  1370. struct pinctrl_gpio_range *range,
  1371. unsigned offset)
  1372. {
  1373. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1374. dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
  1375. /* Set the pin to some default state, GPIO is usually default */
  1376. }
  1377. static struct pinmux_ops nmk_pinmux_ops = {
  1378. .get_functions_count = nmk_pmx_get_funcs_cnt,
  1379. .get_function_name = nmk_pmx_get_func_name,
  1380. .get_function_groups = nmk_pmx_get_func_groups,
  1381. .enable = nmk_pmx_enable,
  1382. .disable = nmk_pmx_disable,
  1383. .gpio_request_enable = nmk_gpio_request_enable,
  1384. .gpio_disable_free = nmk_gpio_disable_free,
  1385. };
  1386. int nmk_pin_config_get(struct pinctrl_dev *pctldev,
  1387. unsigned pin,
  1388. unsigned long *config)
  1389. {
  1390. /* Not implemented */
  1391. return -EINVAL;
  1392. }
  1393. int nmk_pin_config_set(struct pinctrl_dev *pctldev,
  1394. unsigned pin,
  1395. unsigned long config)
  1396. {
  1397. static const char *pullnames[] = {
  1398. [NMK_GPIO_PULL_NONE] = "none",
  1399. [NMK_GPIO_PULL_UP] = "up",
  1400. [NMK_GPIO_PULL_DOWN] = "down",
  1401. [3] /* illegal */ = "??"
  1402. };
  1403. static const char *slpmnames[] = {
  1404. [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
  1405. [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
  1406. };
  1407. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1408. struct nmk_gpio_chip *nmk_chip;
  1409. struct pinctrl_gpio_range *range;
  1410. struct gpio_chip *chip;
  1411. unsigned bit;
  1412. /*
  1413. * The pin config contains pin number and altfunction fields, here
  1414. * we just ignore that part. It's being handled by the framework and
  1415. * pinmux callback respectively.
  1416. */
  1417. pin_cfg_t cfg = (pin_cfg_t) config;
  1418. int pull = PIN_PULL(cfg);
  1419. int slpm = PIN_SLPM(cfg);
  1420. int output = PIN_DIR(cfg);
  1421. int val = PIN_VAL(cfg);
  1422. bool lowemi = PIN_LOWEMI(cfg);
  1423. bool gpiomode = PIN_GPIOMODE(cfg);
  1424. bool sleep = PIN_SLEEPMODE(cfg);
  1425. range = nmk_match_gpio_range(pctldev, pin);
  1426. if (!range) {
  1427. dev_err(npct->dev, "invalid pin offset %d\n", pin);
  1428. return -EINVAL;
  1429. }
  1430. if (!range->gc) {
  1431. dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
  1432. pin);
  1433. return -EINVAL;
  1434. }
  1435. chip = range->gc;
  1436. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1437. if (sleep) {
  1438. int slpm_pull = PIN_SLPM_PULL(cfg);
  1439. int slpm_output = PIN_SLPM_DIR(cfg);
  1440. int slpm_val = PIN_SLPM_VAL(cfg);
  1441. /* All pins go into GPIO mode at sleep */
  1442. gpiomode = true;
  1443. /*
  1444. * The SLPM_* values are normal values + 1 to allow zero to
  1445. * mean "same as normal".
  1446. */
  1447. if (slpm_pull)
  1448. pull = slpm_pull - 1;
  1449. if (slpm_output)
  1450. output = slpm_output - 1;
  1451. if (slpm_val)
  1452. val = slpm_val - 1;
  1453. dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
  1454. pin,
  1455. slpm_pull ? pullnames[pull] : "same",
  1456. slpm_output ? (output ? "output" : "input") : "same",
  1457. slpm_val ? (val ? "high" : "low") : "same");
  1458. }
  1459. dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
  1460. pin, cfg, pullnames[pull], slpmnames[slpm],
  1461. output ? "output " : "input",
  1462. output ? (val ? "high" : "low") : "",
  1463. lowemi ? "on" : "off" );
  1464. clk_enable(nmk_chip->clk);
  1465. bit = pin % NMK_GPIO_PER_CHIP;
  1466. if (gpiomode)
  1467. /* No glitch when going to GPIO mode */
  1468. __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
  1469. if (output)
  1470. __nmk_gpio_make_output(nmk_chip, bit, val);
  1471. else {
  1472. __nmk_gpio_make_input(nmk_chip, bit);
  1473. __nmk_gpio_set_pull(nmk_chip, bit, pull);
  1474. }
  1475. /* TODO: isn't this only applicable on output pins? */
  1476. __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
  1477. __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
  1478. clk_disable(nmk_chip->clk);
  1479. return 0;
  1480. }
  1481. static struct pinconf_ops nmk_pinconf_ops = {
  1482. .pin_config_get = nmk_pin_config_get,
  1483. .pin_config_set = nmk_pin_config_set,
  1484. };
  1485. static struct pinctrl_desc nmk_pinctrl_desc = {
  1486. .name = "pinctrl-nomadik",
  1487. .pctlops = &nmk_pinctrl_ops,
  1488. .pmxops = &nmk_pinmux_ops,
  1489. .confops = &nmk_pinconf_ops,
  1490. .owner = THIS_MODULE,
  1491. };
  1492. static const struct of_device_id nmk_pinctrl_match[] = {
  1493. {
  1494. .compatible = "stericsson,nmk_pinctrl",
  1495. .data = (void *)PINCTRL_NMK_DB8500,
  1496. },
  1497. {},
  1498. };
  1499. static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
  1500. {
  1501. const struct platform_device_id *platid = platform_get_device_id(pdev);
  1502. struct device_node *np = pdev->dev.of_node;
  1503. struct nmk_pinctrl *npct;
  1504. unsigned int version = 0;
  1505. int i;
  1506. npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
  1507. if (!npct)
  1508. return -ENOMEM;
  1509. if (platid)
  1510. version = platid->driver_data;
  1511. else if (np)
  1512. version = (unsigned int)
  1513. of_match_device(nmk_pinctrl_match, &pdev->dev)->data;
  1514. /* Poke in other ASIC variants here */
  1515. if (version == PINCTRL_NMK_STN8815)
  1516. nmk_pinctrl_stn8815_init(&npct->soc);
  1517. if (version == PINCTRL_NMK_DB8500)
  1518. nmk_pinctrl_db8500_init(&npct->soc);
  1519. if (version == PINCTRL_NMK_DB8540)
  1520. nmk_pinctrl_db8540_init(&npct->soc);
  1521. /*
  1522. * We need all the GPIO drivers to probe FIRST, or we will not be able
  1523. * to obtain references to the struct gpio_chip * for them, and we
  1524. * need this to proceed.
  1525. */
  1526. for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
  1527. if (!nmk_gpio_chips[i]) {
  1528. dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
  1529. return -EPROBE_DEFER;
  1530. }
  1531. npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[i]->chip;
  1532. }
  1533. nmk_pinctrl_desc.pins = npct->soc->pins;
  1534. nmk_pinctrl_desc.npins = npct->soc->npins;
  1535. npct->dev = &pdev->dev;
  1536. npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
  1537. if (!npct->pctl) {
  1538. dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
  1539. return -EINVAL;
  1540. }
  1541. /* We will handle a range of GPIO pins */
  1542. for (i = 0; i < npct->soc->gpio_num_ranges; i++)
  1543. pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
  1544. platform_set_drvdata(pdev, npct);
  1545. dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
  1546. return 0;
  1547. }
  1548. static const struct of_device_id nmk_gpio_match[] = {
  1549. { .compatible = "st,nomadik-gpio", },
  1550. {}
  1551. };
  1552. static struct platform_driver nmk_gpio_driver = {
  1553. .driver = {
  1554. .owner = THIS_MODULE,
  1555. .name = "gpio",
  1556. .of_match_table = nmk_gpio_match,
  1557. },
  1558. .probe = nmk_gpio_probe,
  1559. };
  1560. static const struct platform_device_id nmk_pinctrl_id[] = {
  1561. { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
  1562. { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
  1563. { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
  1564. };
  1565. static struct platform_driver nmk_pinctrl_driver = {
  1566. .driver = {
  1567. .owner = THIS_MODULE,
  1568. .name = "pinctrl-nomadik",
  1569. .of_match_table = nmk_pinctrl_match,
  1570. },
  1571. .probe = nmk_pinctrl_probe,
  1572. .id_table = nmk_pinctrl_id,
  1573. };
  1574. static int __init nmk_gpio_init(void)
  1575. {
  1576. int ret;
  1577. ret = platform_driver_register(&nmk_gpio_driver);
  1578. if (ret)
  1579. return ret;
  1580. return platform_driver_register(&nmk_pinctrl_driver);
  1581. }
  1582. core_initcall(nmk_gpio_init);
  1583. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  1584. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  1585. MODULE_LICENSE("GPL");