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 <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. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
  460. }
  461. static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
  462. {
  463. int gpio;
  464. struct nmk_gpio_chip *nmk_chip;
  465. unsigned long flags;
  466. u32 bitmask;
  467. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  468. nmk_chip = irq_data_get_irq_chip_data(d);
  469. bitmask = nmk_gpio_get_bitmask(gpio);
  470. if (!nmk_chip)
  471. return -EINVAL;
  472. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  473. spin_lock(&nmk_chip->lock);
  474. __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, enable);
  475. if (!(nmk_chip->real_wake & bitmask))
  476. __nmk_gpio_set_wake(nmk_chip, gpio, enable);
  477. spin_unlock(&nmk_chip->lock);
  478. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  479. return 0;
  480. }
  481. static void nmk_gpio_irq_mask(struct irq_data *d)
  482. {
  483. nmk_gpio_irq_maskunmask(d, false);
  484. }
  485. static void nmk_gpio_irq_unmask(struct irq_data *d)
  486. {
  487. nmk_gpio_irq_maskunmask(d, true);
  488. }
  489. static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  490. {
  491. struct irq_desc *desc = irq_to_desc(d->irq);
  492. bool enabled = !(desc->status & IRQ_DISABLED);
  493. struct nmk_gpio_chip *nmk_chip;
  494. unsigned long flags;
  495. u32 bitmask;
  496. int gpio;
  497. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  498. nmk_chip = irq_data_get_irq_chip_data(d);
  499. if (!nmk_chip)
  500. return -EINVAL;
  501. bitmask = nmk_gpio_get_bitmask(gpio);
  502. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  503. spin_lock(&nmk_chip->lock);
  504. if (!enabled)
  505. __nmk_gpio_set_wake(nmk_chip, gpio, on);
  506. if (on)
  507. nmk_chip->real_wake |= bitmask;
  508. else
  509. nmk_chip->real_wake &= ~bitmask;
  510. spin_unlock(&nmk_chip->lock);
  511. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  512. return 0;
  513. }
  514. static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  515. {
  516. struct irq_desc *desc = irq_to_desc(d->irq);
  517. bool enabled = !(desc->status & IRQ_DISABLED);
  518. bool wake = desc->wake_depth;
  519. int gpio;
  520. struct nmk_gpio_chip *nmk_chip;
  521. unsigned long flags;
  522. u32 bitmask;
  523. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  524. nmk_chip = irq_data_get_irq_chip_data(d);
  525. bitmask = nmk_gpio_get_bitmask(gpio);
  526. if (!nmk_chip)
  527. return -EINVAL;
  528. if (type & IRQ_TYPE_LEVEL_HIGH)
  529. return -EINVAL;
  530. if (type & IRQ_TYPE_LEVEL_LOW)
  531. return -EINVAL;
  532. spin_lock_irqsave(&nmk_chip->lock, flags);
  533. if (enabled)
  534. __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
  535. if (enabled || wake)
  536. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
  537. nmk_chip->edge_rising &= ~bitmask;
  538. if (type & IRQ_TYPE_EDGE_RISING)
  539. nmk_chip->edge_rising |= bitmask;
  540. nmk_chip->edge_falling &= ~bitmask;
  541. if (type & IRQ_TYPE_EDGE_FALLING)
  542. nmk_chip->edge_falling |= bitmask;
  543. if (enabled)
  544. __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
  545. if (enabled || wake)
  546. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
  547. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  548. return 0;
  549. }
  550. static struct irq_chip nmk_gpio_irq_chip = {
  551. .name = "Nomadik-GPIO",
  552. .irq_ack = nmk_gpio_irq_ack,
  553. .irq_mask = nmk_gpio_irq_mask,
  554. .irq_unmask = nmk_gpio_irq_unmask,
  555. .irq_set_type = nmk_gpio_irq_set_type,
  556. .irq_set_wake = nmk_gpio_irq_set_wake,
  557. };
  558. static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
  559. u32 status)
  560. {
  561. struct nmk_gpio_chip *nmk_chip;
  562. struct irq_chip *host_chip = get_irq_chip(irq);
  563. unsigned int first_irq;
  564. if (host_chip->irq_mask_ack)
  565. host_chip->irq_mask_ack(&desc->irq_data);
  566. else {
  567. host_chip->irq_mask(&desc->irq_data);
  568. if (host_chip->irq_ack)
  569. host_chip->irq_ack(&desc->irq_data);
  570. }
  571. nmk_chip = get_irq_data(irq);
  572. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  573. while (status) {
  574. int bit = __ffs(status);
  575. generic_handle_irq(first_irq + bit);
  576. status &= ~BIT(bit);
  577. }
  578. host_chip->irq_unmask(&desc->irq_data);
  579. }
  580. static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  581. {
  582. struct nmk_gpio_chip *nmk_chip = get_irq_data(irq);
  583. u32 status = readl(nmk_chip->addr + NMK_GPIO_IS);
  584. __nmk_gpio_irq_handler(irq, desc, status);
  585. }
  586. static void nmk_gpio_secondary_irq_handler(unsigned int irq,
  587. struct irq_desc *desc)
  588. {
  589. struct nmk_gpio_chip *nmk_chip = get_irq_data(irq);
  590. u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
  591. __nmk_gpio_irq_handler(irq, desc, status);
  592. }
  593. static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
  594. {
  595. unsigned int first_irq;
  596. int i;
  597. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  598. for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
  599. set_irq_chip(i, &nmk_gpio_irq_chip);
  600. set_irq_handler(i, handle_edge_irq);
  601. set_irq_flags(i, IRQF_VALID);
  602. set_irq_chip_data(i, nmk_chip);
  603. set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
  604. }
  605. set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
  606. set_irq_data(nmk_chip->parent_irq, nmk_chip);
  607. if (nmk_chip->secondary_parent_irq >= 0) {
  608. set_irq_chained_handler(nmk_chip->secondary_parent_irq,
  609. nmk_gpio_secondary_irq_handler);
  610. set_irq_data(nmk_chip->secondary_parent_irq, nmk_chip);
  611. }
  612. return 0;
  613. }
  614. /* I/O Functions */
  615. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
  616. {
  617. struct nmk_gpio_chip *nmk_chip =
  618. container_of(chip, struct nmk_gpio_chip, chip);
  619. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  620. return 0;
  621. }
  622. static int nmk_gpio_get_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. u32 bit = 1 << offset;
  627. return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  628. }
  629. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  630. int val)
  631. {
  632. struct nmk_gpio_chip *nmk_chip =
  633. container_of(chip, struct nmk_gpio_chip, chip);
  634. __nmk_gpio_set_output(nmk_chip, offset, val);
  635. }
  636. static int nmk_gpio_make_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_make_output(nmk_chip, offset, val);
  642. return 0;
  643. }
  644. static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  645. {
  646. struct nmk_gpio_chip *nmk_chip =
  647. container_of(chip, struct nmk_gpio_chip, chip);
  648. return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
  649. }
  650. #ifdef CONFIG_DEBUG_FS
  651. #include <linux/seq_file.h>
  652. static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  653. {
  654. int mode;
  655. unsigned i;
  656. unsigned gpio = chip->base;
  657. int is_out;
  658. struct nmk_gpio_chip *nmk_chip =
  659. container_of(chip, struct nmk_gpio_chip, chip);
  660. const char *modes[] = {
  661. [NMK_GPIO_ALT_GPIO] = "gpio",
  662. [NMK_GPIO_ALT_A] = "altA",
  663. [NMK_GPIO_ALT_B] = "altB",
  664. [NMK_GPIO_ALT_C] = "altC",
  665. };
  666. for (i = 0; i < chip->ngpio; i++, gpio++) {
  667. const char *label = gpiochip_is_requested(chip, i);
  668. bool pull;
  669. u32 bit = 1 << i;
  670. if (!label)
  671. continue;
  672. is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
  673. pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
  674. mode = nmk_gpio_get_mode(gpio);
  675. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
  676. gpio, label,
  677. is_out ? "out" : "in ",
  678. chip->get
  679. ? (chip->get(chip, i) ? "hi" : "lo")
  680. : "? ",
  681. (mode < 0) ? "unknown" : modes[mode],
  682. pull ? "pull" : "none");
  683. seq_printf(s, "\n");
  684. }
  685. }
  686. #else
  687. #define nmk_gpio_dbg_show NULL
  688. #endif
  689. /* This structure is replicated for each GPIO block allocated at probe time */
  690. static struct gpio_chip nmk_gpio_template = {
  691. .direction_input = nmk_gpio_make_input,
  692. .get = nmk_gpio_get_input,
  693. .direction_output = nmk_gpio_make_output,
  694. .set = nmk_gpio_set_output,
  695. .to_irq = nmk_gpio_to_irq,
  696. .dbg_show = nmk_gpio_dbg_show,
  697. .can_sleep = 0,
  698. };
  699. /*
  700. * Called from the suspend/resume path to only keep the real wakeup interrupts
  701. * (those that have had set_irq_wake() called on them) as wakeup interrupts,
  702. * and not the rest of the interrupts which we needed to have as wakeups for
  703. * cpuidle.
  704. *
  705. * PM ops are not used since this needs to be done at the end, after all the
  706. * other drivers are done with their suspend callbacks.
  707. */
  708. void nmk_gpio_wakeups_suspend(void)
  709. {
  710. int i;
  711. for (i = 0; i < NUM_BANKS; i++) {
  712. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  713. if (!chip)
  714. break;
  715. chip->rwimsc = readl(chip->addr + NMK_GPIO_RWIMSC);
  716. chip->fwimsc = readl(chip->addr + NMK_GPIO_FWIMSC);
  717. writel(chip->rwimsc & chip->real_wake,
  718. chip->addr + NMK_GPIO_RWIMSC);
  719. writel(chip->fwimsc & chip->real_wake,
  720. chip->addr + NMK_GPIO_FWIMSC);
  721. if (cpu_is_u8500v2()) {
  722. chip->slpm = readl(chip->addr + NMK_GPIO_SLPC);
  723. /* 0 -> wakeup enable */
  724. writel(~chip->real_wake, chip->addr + NMK_GPIO_SLPC);
  725. }
  726. }
  727. }
  728. void nmk_gpio_wakeups_resume(void)
  729. {
  730. int i;
  731. for (i = 0; i < NUM_BANKS; i++) {
  732. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  733. if (!chip)
  734. break;
  735. writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
  736. writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
  737. if (cpu_is_u8500v2())
  738. writel(chip->slpm, chip->addr + NMK_GPIO_SLPC);
  739. }
  740. }
  741. static int __devinit nmk_gpio_probe(struct platform_device *dev)
  742. {
  743. struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
  744. struct nmk_gpio_chip *nmk_chip;
  745. struct gpio_chip *chip;
  746. struct resource *res;
  747. struct clk *clk;
  748. int secondary_irq;
  749. int irq;
  750. int ret;
  751. if (!pdata)
  752. return -ENODEV;
  753. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  754. if (!res) {
  755. ret = -ENOENT;
  756. goto out;
  757. }
  758. irq = platform_get_irq(dev, 0);
  759. if (irq < 0) {
  760. ret = irq;
  761. goto out;
  762. }
  763. secondary_irq = platform_get_irq(dev, 1);
  764. if (secondary_irq >= 0 && !pdata->get_secondary_status) {
  765. ret = -EINVAL;
  766. goto out;
  767. }
  768. if (request_mem_region(res->start, resource_size(res),
  769. dev_name(&dev->dev)) == NULL) {
  770. ret = -EBUSY;
  771. goto out;
  772. }
  773. clk = clk_get(&dev->dev, NULL);
  774. if (IS_ERR(clk)) {
  775. ret = PTR_ERR(clk);
  776. goto out_release;
  777. }
  778. clk_enable(clk);
  779. nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
  780. if (!nmk_chip) {
  781. ret = -ENOMEM;
  782. goto out_clk;
  783. }
  784. /*
  785. * The virt address in nmk_chip->addr is in the nomadik register space,
  786. * so we can simply convert the resource address, without remapping
  787. */
  788. nmk_chip->bank = dev->id;
  789. nmk_chip->clk = clk;
  790. nmk_chip->addr = io_p2v(res->start);
  791. nmk_chip->chip = nmk_gpio_template;
  792. nmk_chip->parent_irq = irq;
  793. nmk_chip->secondary_parent_irq = secondary_irq;
  794. nmk_chip->get_secondary_status = pdata->get_secondary_status;
  795. nmk_chip->set_ioforce = pdata->set_ioforce;
  796. spin_lock_init(&nmk_chip->lock);
  797. chip = &nmk_chip->chip;
  798. chip->base = pdata->first_gpio;
  799. chip->ngpio = pdata->num_gpio;
  800. chip->label = pdata->name ?: dev_name(&dev->dev);
  801. chip->dev = &dev->dev;
  802. chip->owner = THIS_MODULE;
  803. ret = gpiochip_add(&nmk_chip->chip);
  804. if (ret)
  805. goto out_free;
  806. BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
  807. nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
  808. platform_set_drvdata(dev, nmk_chip);
  809. nmk_gpio_init_irq(nmk_chip);
  810. dev_info(&dev->dev, "Bits %i-%i at address %p\n",
  811. nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
  812. return 0;
  813. out_free:
  814. kfree(nmk_chip);
  815. out_clk:
  816. clk_disable(clk);
  817. clk_put(clk);
  818. out_release:
  819. release_mem_region(res->start, resource_size(res));
  820. out:
  821. dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
  822. pdata->first_gpio, pdata->first_gpio+31);
  823. return ret;
  824. }
  825. static struct platform_driver nmk_gpio_driver = {
  826. .driver = {
  827. .owner = THIS_MODULE,
  828. .name = "gpio",
  829. },
  830. .probe = nmk_gpio_probe,
  831. };
  832. static int __init nmk_gpio_init(void)
  833. {
  834. return platform_driver_register(&nmk_gpio_driver);
  835. }
  836. core_initcall(nmk_gpio_init);
  837. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  838. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  839. MODULE_LICENSE("GPL");