gpio-nomadik.c 29 KB

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