gpio-nomadik.c 26 KB

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