gpio-nomadik.c 31 KB

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