gpio.c 25 KB

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