gpio-nomadik.c 29 KB

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