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