gpio.c 24 KB

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