gpio-nomadik.c 29 KB

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