gpio-nomadik.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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 <mach/hardware.h>
  29. #include <mach/gpio.h>
  30. /*
  31. * The GPIO module in the Nomadik family of Systems-on-Chip is an
  32. * AMBA device, managing 32 pins and alternate functions. The logic block
  33. * is currently used in the Nomadik and ux500.
  34. *
  35. * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
  36. */
  37. #define NMK_GPIO_PER_CHIP 32
  38. struct nmk_gpio_chip {
  39. struct gpio_chip chip;
  40. void __iomem *addr;
  41. struct clk *clk;
  42. unsigned int bank;
  43. unsigned int parent_irq;
  44. int secondary_parent_irq;
  45. u32 (*get_secondary_status)(unsigned int bank);
  46. void (*set_ioforce)(bool enable);
  47. spinlock_t lock;
  48. bool sleepmode;
  49. /* Keep track of configured edges */
  50. u32 edge_rising;
  51. u32 edge_falling;
  52. u32 real_wake;
  53. u32 rwimsc;
  54. u32 fwimsc;
  55. u32 slpm;
  56. u32 enabled;
  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. slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
  240. writel(temp, chip->addr + NMK_GPIO_SLPC);
  241. }
  242. }
  243. static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
  244. {
  245. int i;
  246. for (i = 0; i < NUM_BANKS; i++) {
  247. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  248. if (!chip)
  249. break;
  250. writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
  251. }
  252. }
  253. static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
  254. {
  255. static unsigned int slpm[NUM_BANKS];
  256. unsigned long flags;
  257. bool glitch = false;
  258. int ret = 0;
  259. int i;
  260. for (i = 0; i < num; i++) {
  261. if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
  262. glitch = true;
  263. break;
  264. }
  265. }
  266. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  267. if (glitch) {
  268. memset(slpm, 0xff, sizeof(slpm));
  269. for (i = 0; i < num; i++) {
  270. int pin = PIN_NUM(cfgs[i]);
  271. int offset = pin % NMK_GPIO_PER_CHIP;
  272. if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
  273. slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
  274. }
  275. nmk_gpio_glitch_slpm_init(slpm);
  276. }
  277. for (i = 0; i < num; i++) {
  278. struct nmk_gpio_chip *nmk_chip;
  279. int pin = PIN_NUM(cfgs[i]);
  280. nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(pin));
  281. if (!nmk_chip) {
  282. ret = -EINVAL;
  283. break;
  284. }
  285. spin_lock(&nmk_chip->lock);
  286. __nmk_config_pin(nmk_chip, pin - nmk_chip->chip.base,
  287. cfgs[i], sleep, glitch ? slpm : NULL);
  288. spin_unlock(&nmk_chip->lock);
  289. }
  290. if (glitch)
  291. nmk_gpio_glitch_slpm_restore(slpm);
  292. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  293. return ret;
  294. }
  295. /**
  296. * nmk_config_pin - configure a pin's mux attributes
  297. * @cfg: pin confguration
  298. *
  299. * Configures a pin's mode (alternate function or GPIO), its pull up status,
  300. * and its sleep mode based on the specified configuration. The @cfg is
  301. * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
  302. * are constructed using, and can be further enhanced with, the macros in
  303. * plat/pincfg.h.
  304. *
  305. * If a pin's mode is set to GPIO, it is configured as an input to avoid
  306. * side-effects. The gpio can be manipulated later using standard GPIO API
  307. * calls.
  308. */
  309. int nmk_config_pin(pin_cfg_t cfg, bool sleep)
  310. {
  311. return __nmk_config_pins(&cfg, 1, sleep);
  312. }
  313. EXPORT_SYMBOL(nmk_config_pin);
  314. /**
  315. * nmk_config_pins - configure several pins at once
  316. * @cfgs: array of pin configurations
  317. * @num: number of elments in the array
  318. *
  319. * Configures several pins using nmk_config_pin(). Refer to that function for
  320. * further information.
  321. */
  322. int nmk_config_pins(pin_cfg_t *cfgs, int num)
  323. {
  324. return __nmk_config_pins(cfgs, num, false);
  325. }
  326. EXPORT_SYMBOL(nmk_config_pins);
  327. int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
  328. {
  329. return __nmk_config_pins(cfgs, num, true);
  330. }
  331. EXPORT_SYMBOL(nmk_config_pins_sleep);
  332. /**
  333. * nmk_gpio_set_slpm() - configure the sleep mode of a pin
  334. * @gpio: pin number
  335. * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
  336. *
  337. * This register is actually in the pinmux layer, not the GPIO block itself.
  338. * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
  339. * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
  340. * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
  341. * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
  342. * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
  343. * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
  344. *
  345. * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
  346. * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
  347. * entered) regardless of the altfunction selected. Also wake-up detection is
  348. * ENABLED.
  349. *
  350. * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
  351. * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
  352. * (for altfunction GPIO) or respective on-chip peripherals (for other
  353. * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
  354. *
  355. * Note that enable_irq_wake() will automatically enable wakeup detection.
  356. */
  357. int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
  358. {
  359. struct nmk_gpio_chip *nmk_chip;
  360. unsigned long flags;
  361. nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  362. if (!nmk_chip)
  363. return -EINVAL;
  364. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  365. spin_lock(&nmk_chip->lock);
  366. __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
  367. spin_unlock(&nmk_chip->lock);
  368. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  369. return 0;
  370. }
  371. /**
  372. * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
  373. * @gpio: pin number
  374. * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
  375. *
  376. * Enables/disables pull up/down on a specified pin. This only takes effect if
  377. * the pin is configured as an input (either explicitly or by the alternate
  378. * function).
  379. *
  380. * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
  381. * configured as an input. Otherwise, due to the way the controller registers
  382. * work, this function will change the value output on the pin.
  383. */
  384. int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
  385. {
  386. struct nmk_gpio_chip *nmk_chip;
  387. unsigned long flags;
  388. nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  389. if (!nmk_chip)
  390. return -EINVAL;
  391. spin_lock_irqsave(&nmk_chip->lock, flags);
  392. __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
  393. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  394. return 0;
  395. }
  396. /* Mode functions */
  397. /**
  398. * nmk_gpio_set_mode() - set the mux mode of a gpio pin
  399. * @gpio: pin number
  400. * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
  401. * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
  402. *
  403. * Sets the mode of the specified pin to one of the alternate functions or
  404. * plain GPIO.
  405. */
  406. int nmk_gpio_set_mode(int gpio, int gpio_mode)
  407. {
  408. struct nmk_gpio_chip *nmk_chip;
  409. unsigned long flags;
  410. nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  411. if (!nmk_chip)
  412. return -EINVAL;
  413. spin_lock_irqsave(&nmk_chip->lock, flags);
  414. __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
  415. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  416. return 0;
  417. }
  418. EXPORT_SYMBOL(nmk_gpio_set_mode);
  419. int nmk_gpio_get_mode(int gpio)
  420. {
  421. struct nmk_gpio_chip *nmk_chip;
  422. u32 afunc, bfunc, bit;
  423. nmk_chip = irq_get_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  424. if (!nmk_chip)
  425. return -EINVAL;
  426. bit = 1 << (gpio - nmk_chip->chip.base);
  427. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
  428. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
  429. return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
  430. }
  431. EXPORT_SYMBOL(nmk_gpio_get_mode);
  432. /* IRQ functions */
  433. static inline int nmk_gpio_get_bitmask(int gpio)
  434. {
  435. return 1 << (gpio % 32);
  436. }
  437. static void nmk_gpio_irq_ack(struct irq_data *d)
  438. {
  439. int gpio;
  440. struct nmk_gpio_chip *nmk_chip;
  441. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  442. nmk_chip = irq_data_get_irq_chip_data(d);
  443. if (!nmk_chip)
  444. return;
  445. writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
  446. }
  447. enum nmk_gpio_irq_type {
  448. NORMAL,
  449. WAKE,
  450. };
  451. static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
  452. int gpio, enum nmk_gpio_irq_type which,
  453. bool enable)
  454. {
  455. u32 rimsc = which == WAKE ? NMK_GPIO_RWIMSC : NMK_GPIO_RIMSC;
  456. u32 fimsc = which == WAKE ? NMK_GPIO_FWIMSC : NMK_GPIO_FIMSC;
  457. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  458. u32 reg;
  459. /* we must individually set/clear the two edges */
  460. if (nmk_chip->edge_rising & bitmask) {
  461. reg = readl(nmk_chip->addr + rimsc);
  462. if (enable)
  463. reg |= bitmask;
  464. else
  465. reg &= ~bitmask;
  466. writel(reg, nmk_chip->addr + rimsc);
  467. }
  468. if (nmk_chip->edge_falling & bitmask) {
  469. reg = readl(nmk_chip->addr + fimsc);
  470. if (enable)
  471. reg |= bitmask;
  472. else
  473. reg &= ~bitmask;
  474. writel(reg, nmk_chip->addr + fimsc);
  475. }
  476. }
  477. static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
  478. int gpio, bool on)
  479. {
  480. if (nmk_chip->sleepmode) {
  481. __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base,
  482. on ? NMK_GPIO_SLPM_WAKEUP_ENABLE
  483. : NMK_GPIO_SLPM_WAKEUP_DISABLE);
  484. }
  485. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
  486. }
  487. static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
  488. {
  489. int gpio;
  490. struct nmk_gpio_chip *nmk_chip;
  491. unsigned long flags;
  492. u32 bitmask;
  493. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  494. nmk_chip = irq_data_get_irq_chip_data(d);
  495. bitmask = nmk_gpio_get_bitmask(gpio);
  496. if (!nmk_chip)
  497. return -EINVAL;
  498. if (enable)
  499. nmk_chip->enabled |= bitmask;
  500. else
  501. nmk_chip->enabled &= ~bitmask;
  502. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  503. spin_lock(&nmk_chip->lock);
  504. __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, enable);
  505. if (!(nmk_chip->real_wake & bitmask))
  506. __nmk_gpio_set_wake(nmk_chip, gpio, enable);
  507. spin_unlock(&nmk_chip->lock);
  508. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  509. return 0;
  510. }
  511. static void nmk_gpio_irq_mask(struct irq_data *d)
  512. {
  513. nmk_gpio_irq_maskunmask(d, false);
  514. }
  515. static void nmk_gpio_irq_unmask(struct irq_data *d)
  516. {
  517. nmk_gpio_irq_maskunmask(d, true);
  518. }
  519. static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  520. {
  521. struct nmk_gpio_chip *nmk_chip;
  522. unsigned long flags;
  523. u32 bitmask;
  524. int gpio;
  525. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  526. nmk_chip = irq_data_get_irq_chip_data(d);
  527. if (!nmk_chip)
  528. return -EINVAL;
  529. bitmask = nmk_gpio_get_bitmask(gpio);
  530. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  531. spin_lock(&nmk_chip->lock);
  532. if (!(nmk_chip->enabled & bitmask))
  533. __nmk_gpio_set_wake(nmk_chip, gpio, on);
  534. if (on)
  535. nmk_chip->real_wake |= bitmask;
  536. else
  537. nmk_chip->real_wake &= ~bitmask;
  538. spin_unlock(&nmk_chip->lock);
  539. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  540. return 0;
  541. }
  542. static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  543. {
  544. bool enabled, wake = irqd_is_wakeup_set(d);
  545. int gpio;
  546. struct nmk_gpio_chip *nmk_chip;
  547. unsigned long flags;
  548. u32 bitmask;
  549. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  550. nmk_chip = irq_data_get_irq_chip_data(d);
  551. bitmask = nmk_gpio_get_bitmask(gpio);
  552. if (!nmk_chip)
  553. return -EINVAL;
  554. if (type & IRQ_TYPE_LEVEL_HIGH)
  555. return -EINVAL;
  556. if (type & IRQ_TYPE_LEVEL_LOW)
  557. return -EINVAL;
  558. enabled = nmk_chip->enabled & bitmask;
  559. spin_lock_irqsave(&nmk_chip->lock, flags);
  560. if (enabled)
  561. __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
  562. if (enabled || wake)
  563. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
  564. nmk_chip->edge_rising &= ~bitmask;
  565. if (type & IRQ_TYPE_EDGE_RISING)
  566. nmk_chip->edge_rising |= bitmask;
  567. nmk_chip->edge_falling &= ~bitmask;
  568. if (type & IRQ_TYPE_EDGE_FALLING)
  569. nmk_chip->edge_falling |= bitmask;
  570. if (enabled)
  571. __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
  572. if (enabled || wake)
  573. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
  574. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  575. return 0;
  576. }
  577. static struct irq_chip nmk_gpio_irq_chip = {
  578. .name = "Nomadik-GPIO",
  579. .irq_ack = nmk_gpio_irq_ack,
  580. .irq_mask = nmk_gpio_irq_mask,
  581. .irq_unmask = nmk_gpio_irq_unmask,
  582. .irq_set_type = nmk_gpio_irq_set_type,
  583. .irq_set_wake = nmk_gpio_irq_set_wake,
  584. };
  585. static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
  586. u32 status)
  587. {
  588. struct nmk_gpio_chip *nmk_chip;
  589. struct irq_chip *host_chip = irq_get_chip(irq);
  590. unsigned int first_irq;
  591. chained_irq_enter(host_chip, desc);
  592. nmk_chip = irq_get_handler_data(irq);
  593. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  594. while (status) {
  595. int bit = __ffs(status);
  596. generic_handle_irq(first_irq + bit);
  597. status &= ~BIT(bit);
  598. }
  599. chained_irq_exit(host_chip, desc);
  600. }
  601. static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  602. {
  603. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  604. u32 status = readl(nmk_chip->addr + NMK_GPIO_IS);
  605. __nmk_gpio_irq_handler(irq, desc, status);
  606. }
  607. static void nmk_gpio_secondary_irq_handler(unsigned int irq,
  608. struct irq_desc *desc)
  609. {
  610. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  611. u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
  612. __nmk_gpio_irq_handler(irq, desc, status);
  613. }
  614. static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
  615. {
  616. unsigned int first_irq;
  617. int i;
  618. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  619. for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
  620. irq_set_chip_and_handler(i, &nmk_gpio_irq_chip,
  621. handle_edge_irq);
  622. set_irq_flags(i, IRQF_VALID);
  623. irq_set_chip_data(i, nmk_chip);
  624. irq_set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
  625. }
  626. irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
  627. irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
  628. if (nmk_chip->secondary_parent_irq >= 0) {
  629. irq_set_chained_handler(nmk_chip->secondary_parent_irq,
  630. nmk_gpio_secondary_irq_handler);
  631. irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
  632. }
  633. return 0;
  634. }
  635. /* I/O Functions */
  636. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
  637. {
  638. struct nmk_gpio_chip *nmk_chip =
  639. container_of(chip, struct nmk_gpio_chip, chip);
  640. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  641. return 0;
  642. }
  643. static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
  644. {
  645. struct nmk_gpio_chip *nmk_chip =
  646. container_of(chip, struct nmk_gpio_chip, chip);
  647. u32 bit = 1 << offset;
  648. return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  649. }
  650. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  651. int val)
  652. {
  653. struct nmk_gpio_chip *nmk_chip =
  654. container_of(chip, struct nmk_gpio_chip, chip);
  655. __nmk_gpio_set_output(nmk_chip, offset, val);
  656. }
  657. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
  658. int val)
  659. {
  660. struct nmk_gpio_chip *nmk_chip =
  661. container_of(chip, struct nmk_gpio_chip, chip);
  662. __nmk_gpio_make_output(nmk_chip, offset, val);
  663. return 0;
  664. }
  665. static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  666. {
  667. struct nmk_gpio_chip *nmk_chip =
  668. container_of(chip, struct nmk_gpio_chip, chip);
  669. return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
  670. }
  671. #ifdef CONFIG_DEBUG_FS
  672. #include <linux/seq_file.h>
  673. static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  674. {
  675. int mode;
  676. unsigned i;
  677. unsigned gpio = chip->base;
  678. int is_out;
  679. struct nmk_gpio_chip *nmk_chip =
  680. container_of(chip, struct nmk_gpio_chip, chip);
  681. const char *modes[] = {
  682. [NMK_GPIO_ALT_GPIO] = "gpio",
  683. [NMK_GPIO_ALT_A] = "altA",
  684. [NMK_GPIO_ALT_B] = "altB",
  685. [NMK_GPIO_ALT_C] = "altC",
  686. };
  687. for (i = 0; i < chip->ngpio; i++, gpio++) {
  688. const char *label = gpiochip_is_requested(chip, i);
  689. bool pull;
  690. u32 bit = 1 << i;
  691. is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
  692. pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
  693. mode = nmk_gpio_get_mode(gpio);
  694. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
  695. gpio, label ?: "(none)",
  696. is_out ? "out" : "in ",
  697. chip->get
  698. ? (chip->get(chip, i) ? "hi" : "lo")
  699. : "? ",
  700. (mode < 0) ? "unknown" : modes[mode],
  701. pull ? "pull" : "none");
  702. if (label && !is_out) {
  703. int irq = gpio_to_irq(gpio);
  704. struct irq_desc *desc = irq_to_desc(irq);
  705. /* This races with request_irq(), set_irq_type(),
  706. * and set_irq_wake() ... but those are "rare".
  707. */
  708. if (irq >= 0 && desc->action) {
  709. char *trigger;
  710. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  711. if (nmk_chip->edge_rising & bitmask)
  712. trigger = "edge-rising";
  713. else if (nmk_chip->edge_falling & bitmask)
  714. trigger = "edge-falling";
  715. else
  716. trigger = "edge-undefined";
  717. seq_printf(s, " irq-%d %s%s",
  718. irq, trigger,
  719. irqd_is_wakeup_set(&desc->irq_data)
  720. ? " wakeup" : "");
  721. }
  722. }
  723. seq_printf(s, "\n");
  724. }
  725. }
  726. #else
  727. #define nmk_gpio_dbg_show NULL
  728. #endif
  729. /* This structure is replicated for each GPIO block allocated at probe time */
  730. static struct gpio_chip nmk_gpio_template = {
  731. .direction_input = nmk_gpio_make_input,
  732. .get = nmk_gpio_get_input,
  733. .direction_output = nmk_gpio_make_output,
  734. .set = nmk_gpio_set_output,
  735. .to_irq = nmk_gpio_to_irq,
  736. .dbg_show = nmk_gpio_dbg_show,
  737. .can_sleep = 0,
  738. };
  739. /*
  740. * Called from the suspend/resume path to only keep the real wakeup interrupts
  741. * (those that have had set_irq_wake() called on them) as wakeup interrupts,
  742. * and not the rest of the interrupts which we needed to have as wakeups for
  743. * cpuidle.
  744. *
  745. * PM ops are not used since this needs to be done at the end, after all the
  746. * other drivers are done with their suspend callbacks.
  747. */
  748. void nmk_gpio_wakeups_suspend(void)
  749. {
  750. int i;
  751. for (i = 0; i < NUM_BANKS; i++) {
  752. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  753. if (!chip)
  754. break;
  755. chip->rwimsc = readl(chip->addr + NMK_GPIO_RWIMSC);
  756. chip->fwimsc = readl(chip->addr + NMK_GPIO_FWIMSC);
  757. writel(chip->rwimsc & chip->real_wake,
  758. chip->addr + NMK_GPIO_RWIMSC);
  759. writel(chip->fwimsc & chip->real_wake,
  760. chip->addr + NMK_GPIO_FWIMSC);
  761. if (chip->sleepmode) {
  762. chip->slpm = readl(chip->addr + NMK_GPIO_SLPC);
  763. /* 0 -> wakeup enable */
  764. writel(~chip->real_wake, chip->addr + NMK_GPIO_SLPC);
  765. }
  766. }
  767. }
  768. void nmk_gpio_wakeups_resume(void)
  769. {
  770. int i;
  771. for (i = 0; i < NUM_BANKS; i++) {
  772. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  773. if (!chip)
  774. break;
  775. writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
  776. writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
  777. if (chip->sleepmode)
  778. writel(chip->slpm, chip->addr + NMK_GPIO_SLPC);
  779. }
  780. }
  781. /*
  782. * Read the pull up/pull down status.
  783. * A bit set in 'pull_up' means that pull up
  784. * is selected if pull is enabled in PDIS register.
  785. * Note: only pull up/down set via this driver can
  786. * be detected due to HW limitations.
  787. */
  788. void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
  789. {
  790. if (gpio_bank < NUM_BANKS) {
  791. struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
  792. if (!chip)
  793. return;
  794. *pull_up = chip->pull_up;
  795. }
  796. }
  797. static int __devinit nmk_gpio_probe(struct platform_device *dev)
  798. {
  799. struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
  800. struct nmk_gpio_chip *nmk_chip;
  801. struct gpio_chip *chip;
  802. struct resource *res;
  803. struct clk *clk;
  804. int secondary_irq;
  805. int irq;
  806. int ret;
  807. if (!pdata)
  808. return -ENODEV;
  809. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  810. if (!res) {
  811. ret = -ENOENT;
  812. goto out;
  813. }
  814. irq = platform_get_irq(dev, 0);
  815. if (irq < 0) {
  816. ret = irq;
  817. goto out;
  818. }
  819. secondary_irq = platform_get_irq(dev, 1);
  820. if (secondary_irq >= 0 && !pdata->get_secondary_status) {
  821. ret = -EINVAL;
  822. goto out;
  823. }
  824. if (request_mem_region(res->start, resource_size(res),
  825. dev_name(&dev->dev)) == NULL) {
  826. ret = -EBUSY;
  827. goto out;
  828. }
  829. clk = clk_get(&dev->dev, NULL);
  830. if (IS_ERR(clk)) {
  831. ret = PTR_ERR(clk);
  832. goto out_release;
  833. }
  834. clk_enable(clk);
  835. nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
  836. if (!nmk_chip) {
  837. ret = -ENOMEM;
  838. goto out_clk;
  839. }
  840. /*
  841. * The virt address in nmk_chip->addr is in the nomadik register space,
  842. * so we can simply convert the resource address, without remapping
  843. */
  844. nmk_chip->bank = dev->id;
  845. nmk_chip->clk = clk;
  846. nmk_chip->addr = io_p2v(res->start);
  847. nmk_chip->chip = nmk_gpio_template;
  848. nmk_chip->parent_irq = irq;
  849. nmk_chip->secondary_parent_irq = secondary_irq;
  850. nmk_chip->get_secondary_status = pdata->get_secondary_status;
  851. nmk_chip->set_ioforce = pdata->set_ioforce;
  852. nmk_chip->sleepmode = pdata->supports_sleepmode;
  853. spin_lock_init(&nmk_chip->lock);
  854. chip = &nmk_chip->chip;
  855. chip->base = pdata->first_gpio;
  856. chip->ngpio = pdata->num_gpio;
  857. chip->label = pdata->name ?: dev_name(&dev->dev);
  858. chip->dev = &dev->dev;
  859. chip->owner = THIS_MODULE;
  860. ret = gpiochip_add(&nmk_chip->chip);
  861. if (ret)
  862. goto out_free;
  863. BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
  864. nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
  865. platform_set_drvdata(dev, nmk_chip);
  866. nmk_gpio_init_irq(nmk_chip);
  867. dev_info(&dev->dev, "Bits %i-%i at address %p\n",
  868. nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
  869. return 0;
  870. out_free:
  871. kfree(nmk_chip);
  872. out_clk:
  873. clk_disable(clk);
  874. clk_put(clk);
  875. out_release:
  876. release_mem_region(res->start, resource_size(res));
  877. out:
  878. dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
  879. pdata->first_gpio, pdata->first_gpio+31);
  880. return ret;
  881. }
  882. static struct platform_driver nmk_gpio_driver = {
  883. .driver = {
  884. .owner = THIS_MODULE,
  885. .name = "gpio",
  886. },
  887. .probe = nmk_gpio_probe,
  888. };
  889. static int __init nmk_gpio_init(void)
  890. {
  891. return platform_driver_register(&nmk_gpio_driver);
  892. }
  893. core_initcall(nmk_gpio_init);
  894. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  895. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  896. MODULE_LICENSE("GPL");