gpio-nomadik.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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 <plat/gpio-nomadik.h>
  29. #include <mach/hardware.h>
  30. #include <asm/gpio.h>
  31. /*
  32. * The GPIO module in the Nomadik family of Systems-on-Chip is an
  33. * AMBA device, managing 32 pins and alternate functions. The logic block
  34. * is currently used in the Nomadik and ux500.
  35. *
  36. * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
  37. */
  38. #define NMK_GPIO_PER_CHIP 32
  39. struct nmk_gpio_chip {
  40. struct gpio_chip chip;
  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 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. /*
  494. * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
  495. * disabled, since setting SLPM to 1 increases power consumption, and
  496. * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
  497. */
  498. if (nmk_chip->sleepmode && on) {
  499. __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base,
  500. NMK_GPIO_SLPM_WAKEUP_ENABLE);
  501. }
  502. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
  503. }
  504. static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
  505. {
  506. int gpio;
  507. struct nmk_gpio_chip *nmk_chip;
  508. unsigned long flags;
  509. u32 bitmask;
  510. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  511. nmk_chip = irq_data_get_irq_chip_data(d);
  512. bitmask = nmk_gpio_get_bitmask(gpio);
  513. if (!nmk_chip)
  514. return -EINVAL;
  515. clk_enable(nmk_chip->clk);
  516. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  517. spin_lock(&nmk_chip->lock);
  518. __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, enable);
  519. if (!(nmk_chip->real_wake & bitmask))
  520. __nmk_gpio_set_wake(nmk_chip, gpio, enable);
  521. spin_unlock(&nmk_chip->lock);
  522. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  523. clk_disable(nmk_chip->clk);
  524. return 0;
  525. }
  526. static void nmk_gpio_irq_mask(struct irq_data *d)
  527. {
  528. nmk_gpio_irq_maskunmask(d, false);
  529. }
  530. static void nmk_gpio_irq_unmask(struct irq_data *d)
  531. {
  532. nmk_gpio_irq_maskunmask(d, true);
  533. }
  534. static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  535. {
  536. struct nmk_gpio_chip *nmk_chip;
  537. unsigned long flags;
  538. u32 bitmask;
  539. int gpio;
  540. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  541. nmk_chip = irq_data_get_irq_chip_data(d);
  542. if (!nmk_chip)
  543. return -EINVAL;
  544. bitmask = nmk_gpio_get_bitmask(gpio);
  545. clk_enable(nmk_chip->clk);
  546. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  547. spin_lock(&nmk_chip->lock);
  548. if (irqd_irq_disabled(d))
  549. __nmk_gpio_set_wake(nmk_chip, gpio, on);
  550. if (on)
  551. nmk_chip->real_wake |= bitmask;
  552. else
  553. nmk_chip->real_wake &= ~bitmask;
  554. spin_unlock(&nmk_chip->lock);
  555. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  556. clk_disable(nmk_chip->clk);
  557. return 0;
  558. }
  559. static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  560. {
  561. bool enabled = !irqd_irq_disabled(d);
  562. bool wake = irqd_is_wakeup_set(d);
  563. int gpio;
  564. struct nmk_gpio_chip *nmk_chip;
  565. unsigned long flags;
  566. u32 bitmask;
  567. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  568. nmk_chip = irq_data_get_irq_chip_data(d);
  569. bitmask = nmk_gpio_get_bitmask(gpio);
  570. if (!nmk_chip)
  571. return -EINVAL;
  572. if (type & IRQ_TYPE_LEVEL_HIGH)
  573. return -EINVAL;
  574. if (type & IRQ_TYPE_LEVEL_LOW)
  575. return -EINVAL;
  576. clk_enable(nmk_chip->clk);
  577. spin_lock_irqsave(&nmk_chip->lock, flags);
  578. if (enabled)
  579. __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
  580. if (enabled || wake)
  581. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
  582. nmk_chip->edge_rising &= ~bitmask;
  583. if (type & IRQ_TYPE_EDGE_RISING)
  584. nmk_chip->edge_rising |= bitmask;
  585. nmk_chip->edge_falling &= ~bitmask;
  586. if (type & IRQ_TYPE_EDGE_FALLING)
  587. nmk_chip->edge_falling |= bitmask;
  588. if (enabled)
  589. __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
  590. if (enabled || wake)
  591. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
  592. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  593. clk_disable(nmk_chip->clk);
  594. return 0;
  595. }
  596. static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
  597. {
  598. struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
  599. clk_enable(nmk_chip->clk);
  600. nmk_gpio_irq_unmask(d);
  601. return 0;
  602. }
  603. static void nmk_gpio_irq_shutdown(struct irq_data *d)
  604. {
  605. struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
  606. nmk_gpio_irq_mask(d);
  607. clk_disable(nmk_chip->clk);
  608. }
  609. static struct irq_chip nmk_gpio_irq_chip = {
  610. .name = "Nomadik-GPIO",
  611. .irq_ack = nmk_gpio_irq_ack,
  612. .irq_mask = nmk_gpio_irq_mask,
  613. .irq_unmask = nmk_gpio_irq_unmask,
  614. .irq_set_type = nmk_gpio_irq_set_type,
  615. .irq_set_wake = nmk_gpio_irq_set_wake,
  616. .irq_startup = nmk_gpio_irq_startup,
  617. .irq_shutdown = nmk_gpio_irq_shutdown,
  618. };
  619. static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
  620. u32 status)
  621. {
  622. struct nmk_gpio_chip *nmk_chip;
  623. struct irq_chip *host_chip = irq_get_chip(irq);
  624. unsigned int first_irq;
  625. chained_irq_enter(host_chip, desc);
  626. nmk_chip = irq_get_handler_data(irq);
  627. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  628. while (status) {
  629. int bit = __ffs(status);
  630. generic_handle_irq(first_irq + bit);
  631. status &= ~BIT(bit);
  632. }
  633. chained_irq_exit(host_chip, desc);
  634. }
  635. static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  636. {
  637. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  638. u32 status;
  639. clk_enable(nmk_chip->clk);
  640. status = readl(nmk_chip->addr + NMK_GPIO_IS);
  641. clk_disable(nmk_chip->clk);
  642. __nmk_gpio_irq_handler(irq, desc, status);
  643. }
  644. static void nmk_gpio_secondary_irq_handler(unsigned int irq,
  645. struct irq_desc *desc)
  646. {
  647. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  648. u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
  649. __nmk_gpio_irq_handler(irq, desc, status);
  650. }
  651. static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
  652. {
  653. unsigned int first_irq;
  654. int i;
  655. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  656. for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
  657. irq_set_chip_and_handler(i, &nmk_gpio_irq_chip,
  658. handle_edge_irq);
  659. set_irq_flags(i, IRQF_VALID);
  660. irq_set_chip_data(i, nmk_chip);
  661. irq_set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
  662. }
  663. irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
  664. irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
  665. if (nmk_chip->secondary_parent_irq >= 0) {
  666. irq_set_chained_handler(nmk_chip->secondary_parent_irq,
  667. nmk_gpio_secondary_irq_handler);
  668. irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
  669. }
  670. return 0;
  671. }
  672. /* I/O Functions */
  673. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
  674. {
  675. struct nmk_gpio_chip *nmk_chip =
  676. container_of(chip, struct nmk_gpio_chip, chip);
  677. clk_enable(nmk_chip->clk);
  678. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  679. clk_disable(nmk_chip->clk);
  680. return 0;
  681. }
  682. static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
  683. {
  684. struct nmk_gpio_chip *nmk_chip =
  685. container_of(chip, struct nmk_gpio_chip, chip);
  686. u32 bit = 1 << offset;
  687. int value;
  688. clk_enable(nmk_chip->clk);
  689. value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  690. clk_disable(nmk_chip->clk);
  691. return value;
  692. }
  693. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  694. int val)
  695. {
  696. struct nmk_gpio_chip *nmk_chip =
  697. container_of(chip, struct nmk_gpio_chip, chip);
  698. clk_enable(nmk_chip->clk);
  699. __nmk_gpio_set_output(nmk_chip, offset, val);
  700. clk_disable(nmk_chip->clk);
  701. }
  702. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
  703. int val)
  704. {
  705. struct nmk_gpio_chip *nmk_chip =
  706. container_of(chip, struct nmk_gpio_chip, chip);
  707. clk_enable(nmk_chip->clk);
  708. __nmk_gpio_make_output(nmk_chip, offset, val);
  709. clk_disable(nmk_chip->clk);
  710. return 0;
  711. }
  712. static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  713. {
  714. struct nmk_gpio_chip *nmk_chip =
  715. container_of(chip, struct nmk_gpio_chip, chip);
  716. return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
  717. }
  718. #ifdef CONFIG_DEBUG_FS
  719. #include <linux/seq_file.h>
  720. static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  721. {
  722. int mode;
  723. unsigned i;
  724. unsigned gpio = chip->base;
  725. int is_out;
  726. struct nmk_gpio_chip *nmk_chip =
  727. container_of(chip, struct nmk_gpio_chip, chip);
  728. const char *modes[] = {
  729. [NMK_GPIO_ALT_GPIO] = "gpio",
  730. [NMK_GPIO_ALT_A] = "altA",
  731. [NMK_GPIO_ALT_B] = "altB",
  732. [NMK_GPIO_ALT_C] = "altC",
  733. };
  734. clk_enable(nmk_chip->clk);
  735. for (i = 0; i < chip->ngpio; i++, gpio++) {
  736. const char *label = gpiochip_is_requested(chip, i);
  737. bool pull;
  738. u32 bit = 1 << i;
  739. is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
  740. pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
  741. mode = nmk_gpio_get_mode(gpio);
  742. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
  743. gpio, label ?: "(none)",
  744. is_out ? "out" : "in ",
  745. chip->get
  746. ? (chip->get(chip, i) ? "hi" : "lo")
  747. : "? ",
  748. (mode < 0) ? "unknown" : modes[mode],
  749. pull ? "pull" : "none");
  750. if (label && !is_out) {
  751. int irq = gpio_to_irq(gpio);
  752. struct irq_desc *desc = irq_to_desc(irq);
  753. /* This races with request_irq(), set_irq_type(),
  754. * and set_irq_wake() ... but those are "rare".
  755. */
  756. if (irq >= 0 && desc->action) {
  757. char *trigger;
  758. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  759. if (nmk_chip->edge_rising & bitmask)
  760. trigger = "edge-rising";
  761. else if (nmk_chip->edge_falling & bitmask)
  762. trigger = "edge-falling";
  763. else
  764. trigger = "edge-undefined";
  765. seq_printf(s, " irq-%d %s%s",
  766. irq, trigger,
  767. irqd_is_wakeup_set(&desc->irq_data)
  768. ? " wakeup" : "");
  769. }
  770. }
  771. seq_printf(s, "\n");
  772. }
  773. clk_disable(nmk_chip->clk);
  774. }
  775. #else
  776. #define nmk_gpio_dbg_show NULL
  777. #endif
  778. /* This structure is replicated for each GPIO block allocated at probe time */
  779. static struct gpio_chip nmk_gpio_template = {
  780. .direction_input = nmk_gpio_make_input,
  781. .get = nmk_gpio_get_input,
  782. .direction_output = nmk_gpio_make_output,
  783. .set = nmk_gpio_set_output,
  784. .to_irq = nmk_gpio_to_irq,
  785. .dbg_show = nmk_gpio_dbg_show,
  786. .can_sleep = 0,
  787. };
  788. void nmk_gpio_clocks_enable(void)
  789. {
  790. int i;
  791. for (i = 0; i < NUM_BANKS; i++) {
  792. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  793. if (!chip)
  794. continue;
  795. clk_enable(chip->clk);
  796. }
  797. }
  798. void nmk_gpio_clocks_disable(void)
  799. {
  800. int i;
  801. for (i = 0; i < NUM_BANKS; i++) {
  802. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  803. if (!chip)
  804. continue;
  805. clk_disable(chip->clk);
  806. }
  807. }
  808. /*
  809. * Called from the suspend/resume path to only keep the real wakeup interrupts
  810. * (those that have had set_irq_wake() called on them) as wakeup interrupts,
  811. * and not the rest of the interrupts which we needed to have as wakeups for
  812. * cpuidle.
  813. *
  814. * PM ops are not used since this needs to be done at the end, after all the
  815. * other drivers are done with their suspend callbacks.
  816. */
  817. void nmk_gpio_wakeups_suspend(void)
  818. {
  819. int i;
  820. for (i = 0; i < NUM_BANKS; i++) {
  821. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  822. if (!chip)
  823. break;
  824. clk_enable(chip->clk);
  825. chip->rwimsc = readl(chip->addr + NMK_GPIO_RWIMSC);
  826. chip->fwimsc = readl(chip->addr + NMK_GPIO_FWIMSC);
  827. writel(chip->rwimsc & chip->real_wake,
  828. chip->addr + NMK_GPIO_RWIMSC);
  829. writel(chip->fwimsc & chip->real_wake,
  830. chip->addr + NMK_GPIO_FWIMSC);
  831. clk_disable(chip->clk);
  832. }
  833. }
  834. void nmk_gpio_wakeups_resume(void)
  835. {
  836. int i;
  837. for (i = 0; i < NUM_BANKS; i++) {
  838. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  839. if (!chip)
  840. break;
  841. clk_enable(chip->clk);
  842. writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
  843. writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
  844. clk_disable(chip->clk);
  845. }
  846. }
  847. /*
  848. * Read the pull up/pull down status.
  849. * A bit set in 'pull_up' means that pull up
  850. * is selected if pull is enabled in PDIS register.
  851. * Note: only pull up/down set via this driver can
  852. * be detected due to HW limitations.
  853. */
  854. void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
  855. {
  856. if (gpio_bank < NUM_BANKS) {
  857. struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
  858. if (!chip)
  859. return;
  860. *pull_up = chip->pull_up;
  861. }
  862. }
  863. static int __devinit nmk_gpio_probe(struct platform_device *dev)
  864. {
  865. struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
  866. struct nmk_gpio_chip *nmk_chip;
  867. struct gpio_chip *chip;
  868. struct resource *res;
  869. struct clk *clk;
  870. int secondary_irq;
  871. int irq;
  872. int ret;
  873. if (!pdata)
  874. return -ENODEV;
  875. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  876. if (!res) {
  877. ret = -ENOENT;
  878. goto out;
  879. }
  880. irq = platform_get_irq(dev, 0);
  881. if (irq < 0) {
  882. ret = irq;
  883. goto out;
  884. }
  885. secondary_irq = platform_get_irq(dev, 1);
  886. if (secondary_irq >= 0 && !pdata->get_secondary_status) {
  887. ret = -EINVAL;
  888. goto out;
  889. }
  890. if (request_mem_region(res->start, resource_size(res),
  891. dev_name(&dev->dev)) == NULL) {
  892. ret = -EBUSY;
  893. goto out;
  894. }
  895. clk = clk_get(&dev->dev, NULL);
  896. if (IS_ERR(clk)) {
  897. ret = PTR_ERR(clk);
  898. goto out_release;
  899. }
  900. nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
  901. if (!nmk_chip) {
  902. ret = -ENOMEM;
  903. goto out_clk;
  904. }
  905. /*
  906. * The virt address in nmk_chip->addr is in the nomadik register space,
  907. * so we can simply convert the resource address, without remapping
  908. */
  909. nmk_chip->bank = dev->id;
  910. nmk_chip->clk = clk;
  911. nmk_chip->addr = io_p2v(res->start);
  912. nmk_chip->chip = nmk_gpio_template;
  913. nmk_chip->parent_irq = irq;
  914. nmk_chip->secondary_parent_irq = secondary_irq;
  915. nmk_chip->get_secondary_status = pdata->get_secondary_status;
  916. nmk_chip->set_ioforce = pdata->set_ioforce;
  917. nmk_chip->sleepmode = pdata->supports_sleepmode;
  918. spin_lock_init(&nmk_chip->lock);
  919. chip = &nmk_chip->chip;
  920. chip->base = pdata->first_gpio;
  921. chip->ngpio = pdata->num_gpio;
  922. chip->label = pdata->name ?: dev_name(&dev->dev);
  923. chip->dev = &dev->dev;
  924. chip->owner = THIS_MODULE;
  925. ret = gpiochip_add(&nmk_chip->chip);
  926. if (ret)
  927. goto out_free;
  928. BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
  929. nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
  930. platform_set_drvdata(dev, nmk_chip);
  931. nmk_gpio_init_irq(nmk_chip);
  932. dev_info(&dev->dev, "at address %p\n",
  933. nmk_chip->addr);
  934. return 0;
  935. out_free:
  936. kfree(nmk_chip);
  937. out_clk:
  938. clk_disable(clk);
  939. clk_put(clk);
  940. out_release:
  941. release_mem_region(res->start, resource_size(res));
  942. out:
  943. dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
  944. pdata->first_gpio, pdata->first_gpio+31);
  945. return ret;
  946. }
  947. static struct platform_driver nmk_gpio_driver = {
  948. .driver = {
  949. .owner = THIS_MODULE,
  950. .name = "gpio",
  951. },
  952. .probe = nmk_gpio_probe,
  953. };
  954. static int __init nmk_gpio_init(void)
  955. {
  956. return platform_driver_register(&nmk_gpio_driver);
  957. }
  958. core_initcall(nmk_gpio_init);
  959. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  960. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  961. MODULE_LICENSE("GPL");