gpio.c 25 KB

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