pinctrl-nomadik.c 41 KB

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