gpio-nomadik.c 29 KB

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