pinctrl-nomadik.c 35 KB

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