gpio.c 25 KB

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