pinctrl-nomadik.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  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/irqdomain.h>
  26. #include <linux/slab.h>
  27. #include <linux/of_device.h>
  28. #include <linux/pinctrl/pinctrl.h>
  29. #include <linux/pinctrl/pinmux.h>
  30. #include <linux/pinctrl/pinconf.h>
  31. /* Since we request GPIOs from ourself */
  32. #include <linux/pinctrl/consumer.h>
  33. /*
  34. * For the U8500 archs, use the PRCMU register interface, for the older
  35. * Nomadik, provide some stubs. The functions using these will only be
  36. * called on the U8500 series.
  37. */
  38. #ifdef CONFIG_ARCH_U8500
  39. #include <linux/mfd/dbx500-prcmu.h>
  40. #else
  41. static inline u32 prcmu_read(unsigned int reg) {
  42. return 0;
  43. }
  44. static inline void prcmu_write(unsigned int reg, u32 value) {}
  45. static inline void prcmu_write_masked(unsigned int reg, u32 mask, u32 value) {}
  46. #endif
  47. #include <asm/mach/irq.h>
  48. #include <plat/pincfg.h>
  49. #include <plat/gpio-nomadik.h>
  50. #include "pinctrl-nomadik.h"
  51. /*
  52. * The GPIO module in the Nomadik family of Systems-on-Chip is an
  53. * AMBA device, managing 32 pins and alternate functions. The logic block
  54. * is currently used in the Nomadik and ux500.
  55. *
  56. * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
  57. */
  58. #define NMK_GPIO_PER_CHIP 32
  59. struct nmk_gpio_chip {
  60. struct gpio_chip chip;
  61. struct irq_domain *domain;
  62. void __iomem *addr;
  63. struct clk *clk;
  64. unsigned int bank;
  65. unsigned int parent_irq;
  66. int secondary_parent_irq;
  67. u32 (*get_secondary_status)(unsigned int bank);
  68. void (*set_ioforce)(bool enable);
  69. spinlock_t lock;
  70. bool sleepmode;
  71. /* Keep track of configured edges */
  72. u32 edge_rising;
  73. u32 edge_falling;
  74. u32 real_wake;
  75. u32 rwimsc;
  76. u32 fwimsc;
  77. u32 rimsc;
  78. u32 fimsc;
  79. u32 pull_up;
  80. u32 lowemi;
  81. };
  82. struct nmk_pinctrl {
  83. struct device *dev;
  84. struct pinctrl_dev *pctl;
  85. const struct nmk_pinctrl_soc_data *soc;
  86. };
  87. static struct nmk_gpio_chip *
  88. nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
  89. static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
  90. #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
  91. static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
  92. unsigned offset, int gpio_mode)
  93. {
  94. u32 bit = 1 << offset;
  95. u32 afunc, bfunc;
  96. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
  97. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
  98. if (gpio_mode & NMK_GPIO_ALT_A)
  99. afunc |= bit;
  100. if (gpio_mode & NMK_GPIO_ALT_B)
  101. bfunc |= bit;
  102. writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
  103. writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
  104. }
  105. static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
  106. unsigned offset, enum nmk_gpio_slpm mode)
  107. {
  108. u32 bit = 1 << offset;
  109. u32 slpm;
  110. slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
  111. if (mode == NMK_GPIO_SLPM_NOCHANGE)
  112. slpm |= bit;
  113. else
  114. slpm &= ~bit;
  115. writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
  116. }
  117. static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
  118. unsigned offset, enum nmk_gpio_pull pull)
  119. {
  120. u32 bit = 1 << offset;
  121. u32 pdis;
  122. pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
  123. if (pull == NMK_GPIO_PULL_NONE) {
  124. pdis |= bit;
  125. nmk_chip->pull_up &= ~bit;
  126. } else {
  127. pdis &= ~bit;
  128. }
  129. writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
  130. if (pull == NMK_GPIO_PULL_UP) {
  131. nmk_chip->pull_up |= bit;
  132. writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
  133. } else if (pull == NMK_GPIO_PULL_DOWN) {
  134. nmk_chip->pull_up &= ~bit;
  135. writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
  136. }
  137. }
  138. static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
  139. unsigned offset, bool lowemi)
  140. {
  141. u32 bit = BIT(offset);
  142. bool enabled = nmk_chip->lowemi & bit;
  143. if (lowemi == enabled)
  144. return;
  145. if (lowemi)
  146. nmk_chip->lowemi |= bit;
  147. else
  148. nmk_chip->lowemi &= ~bit;
  149. writel_relaxed(nmk_chip->lowemi,
  150. nmk_chip->addr + NMK_GPIO_LOWEMI);
  151. }
  152. static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
  153. unsigned offset)
  154. {
  155. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  156. }
  157. static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
  158. unsigned offset, int val)
  159. {
  160. if (val)
  161. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
  162. else
  163. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
  164. }
  165. static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
  166. unsigned offset, int val)
  167. {
  168. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
  169. __nmk_gpio_set_output(nmk_chip, offset, val);
  170. }
  171. static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
  172. unsigned offset, int gpio_mode,
  173. bool glitch)
  174. {
  175. u32 rwimsc = nmk_chip->rwimsc;
  176. u32 fwimsc = nmk_chip->fwimsc;
  177. if (glitch && nmk_chip->set_ioforce) {
  178. u32 bit = BIT(offset);
  179. /* Prevent spurious wakeups */
  180. writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
  181. writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
  182. nmk_chip->set_ioforce(true);
  183. }
  184. __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
  185. if (glitch && nmk_chip->set_ioforce) {
  186. nmk_chip->set_ioforce(false);
  187. writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
  188. writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
  189. }
  190. }
  191. static void
  192. nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
  193. {
  194. u32 falling = nmk_chip->fimsc & BIT(offset);
  195. u32 rising = nmk_chip->rimsc & BIT(offset);
  196. int gpio = nmk_chip->chip.base + offset;
  197. int irq = NOMADIK_GPIO_TO_IRQ(gpio);
  198. struct irq_data *d = irq_get_irq_data(irq);
  199. if (!rising && !falling)
  200. return;
  201. if (!d || !irqd_irq_disabled(d))
  202. return;
  203. if (rising) {
  204. nmk_chip->rimsc &= ~BIT(offset);
  205. writel_relaxed(nmk_chip->rimsc,
  206. nmk_chip->addr + NMK_GPIO_RIMSC);
  207. }
  208. if (falling) {
  209. nmk_chip->fimsc &= ~BIT(offset);
  210. writel_relaxed(nmk_chip->fimsc,
  211. nmk_chip->addr + NMK_GPIO_FIMSC);
  212. }
  213. dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
  214. }
  215. static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
  216. unsigned offset, unsigned alt_num)
  217. {
  218. int i;
  219. u16 reg;
  220. u8 bit;
  221. u8 alt_index;
  222. const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
  223. const u16 *gpiocr_regs;
  224. if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
  225. dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
  226. alt_num);
  227. return;
  228. }
  229. for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
  230. if (npct->soc->altcx_pins[i].pin == offset)
  231. break;
  232. }
  233. if (i == npct->soc->npins_altcx) {
  234. dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
  235. offset);
  236. return;
  237. }
  238. pin_desc = npct->soc->altcx_pins + i;
  239. gpiocr_regs = npct->soc->prcm_gpiocr_registers;
  240. /*
  241. * If alt_num is NULL, just clear current ALTCx selection
  242. * to make sure we come back to a pure ALTC selection
  243. */
  244. if (!alt_num) {
  245. for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
  246. if (pin_desc->altcx[i].used == true) {
  247. reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
  248. bit = pin_desc->altcx[i].control_bit;
  249. if (prcmu_read(reg) & BIT(bit)) {
  250. prcmu_write_masked(reg, BIT(bit), 0);
  251. dev_dbg(npct->dev,
  252. "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
  253. offset, i+1);
  254. }
  255. }
  256. }
  257. return;
  258. }
  259. alt_index = alt_num - 1;
  260. if (pin_desc->altcx[alt_index].used == false) {
  261. dev_warn(npct->dev,
  262. "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
  263. offset, alt_num);
  264. return;
  265. }
  266. /*
  267. * Check if any other ALTCx functions are activated on this pin
  268. * and disable it first.
  269. */
  270. for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
  271. if (i == alt_index)
  272. continue;
  273. if (pin_desc->altcx[i].used == true) {
  274. reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
  275. bit = pin_desc->altcx[i].control_bit;
  276. if (prcmu_read(reg) & BIT(bit)) {
  277. prcmu_write_masked(reg, BIT(bit), 0);
  278. dev_dbg(npct->dev,
  279. "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
  280. offset, i+1);
  281. }
  282. }
  283. }
  284. reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
  285. bit = pin_desc->altcx[alt_index].control_bit;
  286. dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
  287. offset, alt_index+1);
  288. prcmu_write_masked(reg, BIT(bit), BIT(bit));
  289. }
  290. static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
  291. pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
  292. {
  293. static const char *afnames[] = {
  294. [NMK_GPIO_ALT_GPIO] = "GPIO",
  295. [NMK_GPIO_ALT_A] = "A",
  296. [NMK_GPIO_ALT_B] = "B",
  297. [NMK_GPIO_ALT_C] = "C"
  298. };
  299. static const char *pullnames[] = {
  300. [NMK_GPIO_PULL_NONE] = "none",
  301. [NMK_GPIO_PULL_UP] = "up",
  302. [NMK_GPIO_PULL_DOWN] = "down",
  303. [3] /* illegal */ = "??"
  304. };
  305. static const char *slpmnames[] = {
  306. [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
  307. [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
  308. };
  309. int pin = PIN_NUM(cfg);
  310. int pull = PIN_PULL(cfg);
  311. int af = PIN_ALT(cfg);
  312. int slpm = PIN_SLPM(cfg);
  313. int output = PIN_DIR(cfg);
  314. int val = PIN_VAL(cfg);
  315. bool glitch = af == NMK_GPIO_ALT_C;
  316. dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
  317. pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
  318. output ? "output " : "input",
  319. output ? (val ? "high" : "low") : "");
  320. if (sleep) {
  321. int slpm_pull = PIN_SLPM_PULL(cfg);
  322. int slpm_output = PIN_SLPM_DIR(cfg);
  323. int slpm_val = PIN_SLPM_VAL(cfg);
  324. af = NMK_GPIO_ALT_GPIO;
  325. /*
  326. * The SLPM_* values are normal values + 1 to allow zero to
  327. * mean "same as normal".
  328. */
  329. if (slpm_pull)
  330. pull = slpm_pull - 1;
  331. if (slpm_output)
  332. output = slpm_output - 1;
  333. if (slpm_val)
  334. val = slpm_val - 1;
  335. dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
  336. pin,
  337. slpm_pull ? pullnames[pull] : "same",
  338. slpm_output ? (output ? "output" : "input") : "same",
  339. slpm_val ? (val ? "high" : "low") : "same");
  340. }
  341. if (output)
  342. __nmk_gpio_make_output(nmk_chip, offset, val);
  343. else {
  344. __nmk_gpio_make_input(nmk_chip, offset);
  345. __nmk_gpio_set_pull(nmk_chip, offset, pull);
  346. }
  347. __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
  348. /*
  349. * If the pin is switching to altfunc, and there was an interrupt
  350. * installed on it which has been lazy disabled, actually mask the
  351. * interrupt to prevent spurious interrupts that would occur while the
  352. * pin is under control of the peripheral. Only SKE does this.
  353. */
  354. if (af != NMK_GPIO_ALT_GPIO)
  355. nmk_gpio_disable_lazy_irq(nmk_chip, offset);
  356. /*
  357. * If we've backed up the SLPM registers (glitch workaround), modify
  358. * the backups since they will be restored.
  359. */
  360. if (slpmregs) {
  361. if (slpm == NMK_GPIO_SLPM_NOCHANGE)
  362. slpmregs[nmk_chip->bank] |= BIT(offset);
  363. else
  364. slpmregs[nmk_chip->bank] &= ~BIT(offset);
  365. } else
  366. __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
  367. __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
  368. }
  369. /*
  370. * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
  371. * - Save SLPM registers
  372. * - Set SLPM=0 for the IOs you want to switch and others to 1
  373. * - Configure the GPIO registers for the IOs that are being switched
  374. * - Set IOFORCE=1
  375. * - Modify the AFLSA/B registers for the IOs that are being switched
  376. * - Set IOFORCE=0
  377. * - Restore SLPM registers
  378. * - Any spurious wake up event during switch sequence to be ignored and
  379. * cleared
  380. */
  381. static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
  382. {
  383. int i;
  384. for (i = 0; i < NUM_BANKS; i++) {
  385. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  386. unsigned int temp = slpm[i];
  387. if (!chip)
  388. break;
  389. clk_enable(chip->clk);
  390. slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
  391. writel(temp, chip->addr + NMK_GPIO_SLPC);
  392. }
  393. }
  394. static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
  395. {
  396. int i;
  397. for (i = 0; i < NUM_BANKS; i++) {
  398. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  399. if (!chip)
  400. break;
  401. writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
  402. clk_disable(chip->clk);
  403. }
  404. }
  405. static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
  406. {
  407. static unsigned int slpm[NUM_BANKS];
  408. unsigned long flags;
  409. bool glitch = false;
  410. int ret = 0;
  411. int i;
  412. for (i = 0; i < num; i++) {
  413. if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
  414. glitch = true;
  415. break;
  416. }
  417. }
  418. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  419. if (glitch) {
  420. memset(slpm, 0xff, sizeof(slpm));
  421. for (i = 0; i < num; i++) {
  422. int pin = PIN_NUM(cfgs[i]);
  423. int offset = pin % NMK_GPIO_PER_CHIP;
  424. if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
  425. slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
  426. }
  427. nmk_gpio_glitch_slpm_init(slpm);
  428. }
  429. for (i = 0; i < num; i++) {
  430. struct nmk_gpio_chip *nmk_chip;
  431. int pin = PIN_NUM(cfgs[i]);
  432. nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
  433. if (!nmk_chip) {
  434. ret = -EINVAL;
  435. break;
  436. }
  437. clk_enable(nmk_chip->clk);
  438. spin_lock(&nmk_chip->lock);
  439. __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
  440. cfgs[i], sleep, glitch ? slpm : NULL);
  441. spin_unlock(&nmk_chip->lock);
  442. clk_disable(nmk_chip->clk);
  443. }
  444. if (glitch)
  445. nmk_gpio_glitch_slpm_restore(slpm);
  446. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  447. return ret;
  448. }
  449. /**
  450. * nmk_config_pin - configure a pin's mux attributes
  451. * @cfg: pin confguration
  452. * @sleep: Non-zero to apply the sleep mode configuration
  453. * Configures a pin's mode (alternate function or GPIO), its pull up status,
  454. * and its sleep mode based on the specified configuration. The @cfg is
  455. * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
  456. * are constructed using, and can be further enhanced with, the macros in
  457. * plat/pincfg.h.
  458. *
  459. * If a pin's mode is set to GPIO, it is configured as an input to avoid
  460. * side-effects. The gpio can be manipulated later using standard GPIO API
  461. * calls.
  462. */
  463. int nmk_config_pin(pin_cfg_t cfg, bool sleep)
  464. {
  465. return __nmk_config_pins(&cfg, 1, sleep);
  466. }
  467. EXPORT_SYMBOL(nmk_config_pin);
  468. /**
  469. * nmk_config_pins - configure several pins at once
  470. * @cfgs: array of pin configurations
  471. * @num: number of elments in the array
  472. *
  473. * Configures several pins using nmk_config_pin(). Refer to that function for
  474. * further information.
  475. */
  476. int nmk_config_pins(pin_cfg_t *cfgs, int num)
  477. {
  478. return __nmk_config_pins(cfgs, num, false);
  479. }
  480. EXPORT_SYMBOL(nmk_config_pins);
  481. int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
  482. {
  483. return __nmk_config_pins(cfgs, num, true);
  484. }
  485. EXPORT_SYMBOL(nmk_config_pins_sleep);
  486. /**
  487. * nmk_gpio_set_slpm() - configure the sleep mode of a pin
  488. * @gpio: pin number
  489. * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
  490. *
  491. * This register is actually in the pinmux layer, not the GPIO block itself.
  492. * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
  493. * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
  494. * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
  495. * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
  496. * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
  497. * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
  498. *
  499. * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
  500. * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
  501. * entered) regardless of the altfunction selected. Also wake-up detection is
  502. * ENABLED.
  503. *
  504. * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
  505. * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
  506. * (for altfunction GPIO) or respective on-chip peripherals (for other
  507. * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
  508. *
  509. * Note that enable_irq_wake() will automatically enable wakeup detection.
  510. */
  511. int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
  512. {
  513. struct nmk_gpio_chip *nmk_chip;
  514. unsigned long flags;
  515. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  516. if (!nmk_chip)
  517. return -EINVAL;
  518. clk_enable(nmk_chip->clk);
  519. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  520. spin_lock(&nmk_chip->lock);
  521. __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
  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. /**
  528. * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
  529. * @gpio: pin number
  530. * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
  531. *
  532. * Enables/disables pull up/down on a specified pin. This only takes effect if
  533. * the pin is configured as an input (either explicitly or by the alternate
  534. * function).
  535. *
  536. * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
  537. * configured as an input. Otherwise, due to the way the controller registers
  538. * work, this function will change the value output on the pin.
  539. */
  540. int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
  541. {
  542. struct nmk_gpio_chip *nmk_chip;
  543. unsigned long flags;
  544. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  545. if (!nmk_chip)
  546. return -EINVAL;
  547. clk_enable(nmk_chip->clk);
  548. spin_lock_irqsave(&nmk_chip->lock, flags);
  549. __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
  550. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  551. clk_disable(nmk_chip->clk);
  552. return 0;
  553. }
  554. /* Mode functions */
  555. /**
  556. * nmk_gpio_set_mode() - set the mux mode of a gpio pin
  557. * @gpio: pin number
  558. * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
  559. * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
  560. *
  561. * Sets the mode of the specified pin to one of the alternate functions or
  562. * plain GPIO.
  563. */
  564. int nmk_gpio_set_mode(int gpio, int gpio_mode)
  565. {
  566. struct nmk_gpio_chip *nmk_chip;
  567. unsigned long flags;
  568. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  569. if (!nmk_chip)
  570. return -EINVAL;
  571. clk_enable(nmk_chip->clk);
  572. spin_lock_irqsave(&nmk_chip->lock, flags);
  573. __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
  574. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  575. clk_disable(nmk_chip->clk);
  576. return 0;
  577. }
  578. EXPORT_SYMBOL(nmk_gpio_set_mode);
  579. int nmk_gpio_get_mode(int gpio)
  580. {
  581. struct nmk_gpio_chip *nmk_chip;
  582. u32 afunc, bfunc, bit;
  583. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  584. if (!nmk_chip)
  585. return -EINVAL;
  586. bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
  587. clk_enable(nmk_chip->clk);
  588. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
  589. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
  590. clk_disable(nmk_chip->clk);
  591. return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
  592. }
  593. EXPORT_SYMBOL(nmk_gpio_get_mode);
  594. /* IRQ functions */
  595. static inline int nmk_gpio_get_bitmask(int gpio)
  596. {
  597. return 1 << (gpio % NMK_GPIO_PER_CHIP);
  598. }
  599. static void nmk_gpio_irq_ack(struct irq_data *d)
  600. {
  601. struct nmk_gpio_chip *nmk_chip;
  602. nmk_chip = irq_data_get_irq_chip_data(d);
  603. if (!nmk_chip)
  604. return;
  605. clk_enable(nmk_chip->clk);
  606. writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
  607. clk_disable(nmk_chip->clk);
  608. }
  609. enum nmk_gpio_irq_type {
  610. NORMAL,
  611. WAKE,
  612. };
  613. static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
  614. int gpio, enum nmk_gpio_irq_type which,
  615. bool enable)
  616. {
  617. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  618. u32 *rimscval;
  619. u32 *fimscval;
  620. u32 rimscreg;
  621. u32 fimscreg;
  622. if (which == NORMAL) {
  623. rimscreg = NMK_GPIO_RIMSC;
  624. fimscreg = NMK_GPIO_FIMSC;
  625. rimscval = &nmk_chip->rimsc;
  626. fimscval = &nmk_chip->fimsc;
  627. } else {
  628. rimscreg = NMK_GPIO_RWIMSC;
  629. fimscreg = NMK_GPIO_FWIMSC;
  630. rimscval = &nmk_chip->rwimsc;
  631. fimscval = &nmk_chip->fwimsc;
  632. }
  633. /* we must individually set/clear the two edges */
  634. if (nmk_chip->edge_rising & bitmask) {
  635. if (enable)
  636. *rimscval |= bitmask;
  637. else
  638. *rimscval &= ~bitmask;
  639. writel(*rimscval, nmk_chip->addr + rimscreg);
  640. }
  641. if (nmk_chip->edge_falling & bitmask) {
  642. if (enable)
  643. *fimscval |= bitmask;
  644. else
  645. *fimscval &= ~bitmask;
  646. writel(*fimscval, nmk_chip->addr + fimscreg);
  647. }
  648. }
  649. static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
  650. int gpio, bool on)
  651. {
  652. /*
  653. * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
  654. * disabled, since setting SLPM to 1 increases power consumption, and
  655. * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
  656. */
  657. if (nmk_chip->sleepmode && on) {
  658. __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
  659. NMK_GPIO_SLPM_WAKEUP_ENABLE);
  660. }
  661. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
  662. }
  663. static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
  664. {
  665. struct nmk_gpio_chip *nmk_chip;
  666. unsigned long flags;
  667. u32 bitmask;
  668. nmk_chip = irq_data_get_irq_chip_data(d);
  669. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  670. if (!nmk_chip)
  671. return -EINVAL;
  672. clk_enable(nmk_chip->clk);
  673. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  674. spin_lock(&nmk_chip->lock);
  675. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
  676. if (!(nmk_chip->real_wake & bitmask))
  677. __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
  678. spin_unlock(&nmk_chip->lock);
  679. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  680. clk_disable(nmk_chip->clk);
  681. return 0;
  682. }
  683. static void nmk_gpio_irq_mask(struct irq_data *d)
  684. {
  685. nmk_gpio_irq_maskunmask(d, false);
  686. }
  687. static void nmk_gpio_irq_unmask(struct irq_data *d)
  688. {
  689. nmk_gpio_irq_maskunmask(d, true);
  690. }
  691. static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  692. {
  693. struct nmk_gpio_chip *nmk_chip;
  694. unsigned long flags;
  695. u32 bitmask;
  696. nmk_chip = irq_data_get_irq_chip_data(d);
  697. if (!nmk_chip)
  698. return -EINVAL;
  699. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  700. clk_enable(nmk_chip->clk);
  701. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  702. spin_lock(&nmk_chip->lock);
  703. if (irqd_irq_disabled(d))
  704. __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
  705. if (on)
  706. nmk_chip->real_wake |= bitmask;
  707. else
  708. nmk_chip->real_wake &= ~bitmask;
  709. spin_unlock(&nmk_chip->lock);
  710. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  711. clk_disable(nmk_chip->clk);
  712. return 0;
  713. }
  714. static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  715. {
  716. bool enabled = !irqd_irq_disabled(d);
  717. bool wake = irqd_is_wakeup_set(d);
  718. struct nmk_gpio_chip *nmk_chip;
  719. unsigned long flags;
  720. u32 bitmask;
  721. nmk_chip = irq_data_get_irq_chip_data(d);
  722. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  723. if (!nmk_chip)
  724. return -EINVAL;
  725. if (type & IRQ_TYPE_LEVEL_HIGH)
  726. return -EINVAL;
  727. if (type & IRQ_TYPE_LEVEL_LOW)
  728. return -EINVAL;
  729. clk_enable(nmk_chip->clk);
  730. spin_lock_irqsave(&nmk_chip->lock, flags);
  731. if (enabled)
  732. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
  733. if (enabled || wake)
  734. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
  735. nmk_chip->edge_rising &= ~bitmask;
  736. if (type & IRQ_TYPE_EDGE_RISING)
  737. nmk_chip->edge_rising |= bitmask;
  738. nmk_chip->edge_falling &= ~bitmask;
  739. if (type & IRQ_TYPE_EDGE_FALLING)
  740. nmk_chip->edge_falling |= bitmask;
  741. if (enabled)
  742. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
  743. if (enabled || wake)
  744. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
  745. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  746. clk_disable(nmk_chip->clk);
  747. return 0;
  748. }
  749. static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
  750. {
  751. struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
  752. clk_enable(nmk_chip->clk);
  753. nmk_gpio_irq_unmask(d);
  754. return 0;
  755. }
  756. static void nmk_gpio_irq_shutdown(struct irq_data *d)
  757. {
  758. struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
  759. nmk_gpio_irq_mask(d);
  760. clk_disable(nmk_chip->clk);
  761. }
  762. static struct irq_chip nmk_gpio_irq_chip = {
  763. .name = "Nomadik-GPIO",
  764. .irq_ack = nmk_gpio_irq_ack,
  765. .irq_mask = nmk_gpio_irq_mask,
  766. .irq_unmask = nmk_gpio_irq_unmask,
  767. .irq_set_type = nmk_gpio_irq_set_type,
  768. .irq_set_wake = nmk_gpio_irq_set_wake,
  769. .irq_startup = nmk_gpio_irq_startup,
  770. .irq_shutdown = nmk_gpio_irq_shutdown,
  771. .flags = IRQCHIP_MASK_ON_SUSPEND,
  772. };
  773. static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
  774. u32 status)
  775. {
  776. struct nmk_gpio_chip *nmk_chip;
  777. struct irq_chip *host_chip = irq_get_chip(irq);
  778. chained_irq_enter(host_chip, desc);
  779. nmk_chip = irq_get_handler_data(irq);
  780. while (status) {
  781. int bit = __ffs(status);
  782. generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
  783. status &= ~BIT(bit);
  784. }
  785. chained_irq_exit(host_chip, desc);
  786. }
  787. static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  788. {
  789. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  790. u32 status;
  791. clk_enable(nmk_chip->clk);
  792. status = readl(nmk_chip->addr + NMK_GPIO_IS);
  793. clk_disable(nmk_chip->clk);
  794. __nmk_gpio_irq_handler(irq, desc, status);
  795. }
  796. static void nmk_gpio_secondary_irq_handler(unsigned int irq,
  797. struct irq_desc *desc)
  798. {
  799. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  800. u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
  801. __nmk_gpio_irq_handler(irq, desc, status);
  802. }
  803. static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
  804. {
  805. irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
  806. irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
  807. if (nmk_chip->secondary_parent_irq >= 0) {
  808. irq_set_chained_handler(nmk_chip->secondary_parent_irq,
  809. nmk_gpio_secondary_irq_handler);
  810. irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
  811. }
  812. return 0;
  813. }
  814. /* I/O Functions */
  815. static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
  816. {
  817. /*
  818. * Map back to global GPIO space and request muxing, the direction
  819. * parameter does not matter for this controller.
  820. */
  821. int gpio = chip->base + offset;
  822. return pinctrl_request_gpio(gpio);
  823. }
  824. static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
  825. {
  826. int gpio = chip->base + offset;
  827. pinctrl_free_gpio(gpio);
  828. }
  829. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
  830. {
  831. struct nmk_gpio_chip *nmk_chip =
  832. container_of(chip, struct nmk_gpio_chip, chip);
  833. clk_enable(nmk_chip->clk);
  834. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  835. clk_disable(nmk_chip->clk);
  836. return 0;
  837. }
  838. static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
  839. {
  840. struct nmk_gpio_chip *nmk_chip =
  841. container_of(chip, struct nmk_gpio_chip, chip);
  842. u32 bit = 1 << offset;
  843. int value;
  844. clk_enable(nmk_chip->clk);
  845. value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  846. clk_disable(nmk_chip->clk);
  847. return value;
  848. }
  849. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  850. int val)
  851. {
  852. struct nmk_gpio_chip *nmk_chip =
  853. container_of(chip, struct nmk_gpio_chip, chip);
  854. clk_enable(nmk_chip->clk);
  855. __nmk_gpio_set_output(nmk_chip, offset, val);
  856. clk_disable(nmk_chip->clk);
  857. }
  858. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
  859. int val)
  860. {
  861. struct nmk_gpio_chip *nmk_chip =
  862. container_of(chip, struct nmk_gpio_chip, chip);
  863. clk_enable(nmk_chip->clk);
  864. __nmk_gpio_make_output(nmk_chip, offset, val);
  865. clk_disable(nmk_chip->clk);
  866. return 0;
  867. }
  868. static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  869. {
  870. struct nmk_gpio_chip *nmk_chip =
  871. container_of(chip, struct nmk_gpio_chip, chip);
  872. return irq_create_mapping(nmk_chip->domain, offset);
  873. }
  874. #ifdef CONFIG_DEBUG_FS
  875. #include <linux/seq_file.h>
  876. static void nmk_gpio_dbg_show_one(struct seq_file *s, struct gpio_chip *chip,
  877. unsigned offset, unsigned gpio)
  878. {
  879. const char *label = gpiochip_is_requested(chip, offset);
  880. struct nmk_gpio_chip *nmk_chip =
  881. container_of(chip, struct nmk_gpio_chip, chip);
  882. int mode;
  883. bool is_out;
  884. bool pull;
  885. u32 bit = 1 << offset;
  886. const char *modes[] = {
  887. [NMK_GPIO_ALT_GPIO] = "gpio",
  888. [NMK_GPIO_ALT_A] = "altA",
  889. [NMK_GPIO_ALT_B] = "altB",
  890. [NMK_GPIO_ALT_C] = "altC",
  891. };
  892. clk_enable(nmk_chip->clk);
  893. is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
  894. pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
  895. mode = nmk_gpio_get_mode(gpio);
  896. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
  897. gpio, label ?: "(none)",
  898. is_out ? "out" : "in ",
  899. chip->get
  900. ? (chip->get(chip, offset) ? "hi" : "lo")
  901. : "? ",
  902. (mode < 0) ? "unknown" : modes[mode],
  903. pull ? "pull" : "none");
  904. if (label && !is_out) {
  905. int irq = gpio_to_irq(gpio);
  906. struct irq_desc *desc = irq_to_desc(irq);
  907. /* This races with request_irq(), set_irq_type(),
  908. * and set_irq_wake() ... but those are "rare".
  909. */
  910. if (irq >= 0 && desc->action) {
  911. char *trigger;
  912. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  913. if (nmk_chip->edge_rising & bitmask)
  914. trigger = "edge-rising";
  915. else if (nmk_chip->edge_falling & bitmask)
  916. trigger = "edge-falling";
  917. else
  918. trigger = "edge-undefined";
  919. seq_printf(s, " irq-%d %s%s",
  920. irq, trigger,
  921. irqd_is_wakeup_set(&desc->irq_data)
  922. ? " wakeup" : "");
  923. }
  924. }
  925. clk_disable(nmk_chip->clk);
  926. }
  927. static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  928. {
  929. unsigned i;
  930. unsigned gpio = chip->base;
  931. for (i = 0; i < chip->ngpio; i++, gpio++) {
  932. nmk_gpio_dbg_show_one(s, chip, i, gpio);
  933. seq_printf(s, "\n");
  934. }
  935. }
  936. #else
  937. static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
  938. struct gpio_chip *chip,
  939. unsigned offset, unsigned gpio)
  940. {
  941. }
  942. #define nmk_gpio_dbg_show NULL
  943. #endif
  944. /* This structure is replicated for each GPIO block allocated at probe time */
  945. static struct gpio_chip nmk_gpio_template = {
  946. .request = nmk_gpio_request,
  947. .free = nmk_gpio_free,
  948. .direction_input = nmk_gpio_make_input,
  949. .get = nmk_gpio_get_input,
  950. .direction_output = nmk_gpio_make_output,
  951. .set = nmk_gpio_set_output,
  952. .to_irq = nmk_gpio_to_irq,
  953. .dbg_show = nmk_gpio_dbg_show,
  954. .can_sleep = 0,
  955. };
  956. void nmk_gpio_clocks_enable(void)
  957. {
  958. int i;
  959. for (i = 0; i < NUM_BANKS; i++) {
  960. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  961. if (!chip)
  962. continue;
  963. clk_enable(chip->clk);
  964. }
  965. }
  966. void nmk_gpio_clocks_disable(void)
  967. {
  968. int i;
  969. for (i = 0; i < NUM_BANKS; i++) {
  970. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  971. if (!chip)
  972. continue;
  973. clk_disable(chip->clk);
  974. }
  975. }
  976. /*
  977. * Called from the suspend/resume path to only keep the real wakeup interrupts
  978. * (those that have had set_irq_wake() called on them) as wakeup interrupts,
  979. * and not the rest of the interrupts which we needed to have as wakeups for
  980. * cpuidle.
  981. *
  982. * PM ops are not used since this needs to be done at the end, after all the
  983. * other drivers are done with their suspend callbacks.
  984. */
  985. void nmk_gpio_wakeups_suspend(void)
  986. {
  987. int i;
  988. for (i = 0; i < NUM_BANKS; i++) {
  989. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  990. if (!chip)
  991. break;
  992. clk_enable(chip->clk);
  993. writel(chip->rwimsc & chip->real_wake,
  994. chip->addr + NMK_GPIO_RWIMSC);
  995. writel(chip->fwimsc & chip->real_wake,
  996. chip->addr + NMK_GPIO_FWIMSC);
  997. clk_disable(chip->clk);
  998. }
  999. }
  1000. void nmk_gpio_wakeups_resume(void)
  1001. {
  1002. int i;
  1003. for (i = 0; i < NUM_BANKS; i++) {
  1004. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  1005. if (!chip)
  1006. break;
  1007. clk_enable(chip->clk);
  1008. writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
  1009. writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
  1010. clk_disable(chip->clk);
  1011. }
  1012. }
  1013. /*
  1014. * Read the pull up/pull down status.
  1015. * A bit set in 'pull_up' means that pull up
  1016. * is selected if pull is enabled in PDIS register.
  1017. * Note: only pull up/down set via this driver can
  1018. * be detected due to HW limitations.
  1019. */
  1020. void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
  1021. {
  1022. if (gpio_bank < NUM_BANKS) {
  1023. struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
  1024. if (!chip)
  1025. return;
  1026. *pull_up = chip->pull_up;
  1027. }
  1028. }
  1029. int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
  1030. irq_hw_number_t hwirq)
  1031. {
  1032. struct nmk_gpio_chip *nmk_chip = d->host_data;
  1033. if (!nmk_chip)
  1034. return -EINVAL;
  1035. irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
  1036. set_irq_flags(irq, IRQF_VALID);
  1037. irq_set_chip_data(irq, nmk_chip);
  1038. irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
  1039. return 0;
  1040. }
  1041. const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
  1042. .map = nmk_gpio_irq_map,
  1043. .xlate = irq_domain_xlate_twocell,
  1044. };
  1045. static int __devinit nmk_gpio_probe(struct platform_device *dev)
  1046. {
  1047. struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
  1048. struct device_node *np = dev->dev.of_node;
  1049. struct nmk_gpio_chip *nmk_chip;
  1050. struct gpio_chip *chip;
  1051. struct resource *res;
  1052. struct clk *clk;
  1053. int secondary_irq;
  1054. void __iomem *base;
  1055. int irq_start = 0;
  1056. int irq;
  1057. int ret;
  1058. if (!pdata && !np) {
  1059. dev_err(&dev->dev, "No platform data or device tree found\n");
  1060. return -ENODEV;
  1061. }
  1062. if (np) {
  1063. pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
  1064. if (!pdata)
  1065. return -ENOMEM;
  1066. if (of_get_property(np, "st,supports-sleepmode", NULL))
  1067. pdata->supports_sleepmode = true;
  1068. if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
  1069. dev_err(&dev->dev, "gpio-bank property not found\n");
  1070. ret = -EINVAL;
  1071. goto out;
  1072. }
  1073. pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
  1074. pdata->num_gpio = NMK_GPIO_PER_CHIP;
  1075. }
  1076. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1077. if (!res) {
  1078. ret = -ENOENT;
  1079. goto out;
  1080. }
  1081. irq = platform_get_irq(dev, 0);
  1082. if (irq < 0) {
  1083. ret = irq;
  1084. goto out;
  1085. }
  1086. secondary_irq = platform_get_irq(dev, 1);
  1087. if (secondary_irq >= 0 && !pdata->get_secondary_status) {
  1088. ret = -EINVAL;
  1089. goto out;
  1090. }
  1091. base = devm_request_and_ioremap(&dev->dev, res);
  1092. if (!base) {
  1093. ret = -ENOMEM;
  1094. goto out;
  1095. }
  1096. clk = devm_clk_get(&dev->dev, NULL);
  1097. if (IS_ERR(clk)) {
  1098. ret = PTR_ERR(clk);
  1099. goto out;
  1100. }
  1101. clk_prepare(clk);
  1102. nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
  1103. if (!nmk_chip) {
  1104. ret = -ENOMEM;
  1105. goto out;
  1106. }
  1107. /*
  1108. * The virt address in nmk_chip->addr is in the nomadik register space,
  1109. * so we can simply convert the resource address, without remapping
  1110. */
  1111. nmk_chip->bank = dev->id;
  1112. nmk_chip->clk = clk;
  1113. nmk_chip->addr = base;
  1114. nmk_chip->chip = nmk_gpio_template;
  1115. nmk_chip->parent_irq = irq;
  1116. nmk_chip->secondary_parent_irq = secondary_irq;
  1117. nmk_chip->get_secondary_status = pdata->get_secondary_status;
  1118. nmk_chip->set_ioforce = pdata->set_ioforce;
  1119. nmk_chip->sleepmode = pdata->supports_sleepmode;
  1120. spin_lock_init(&nmk_chip->lock);
  1121. chip = &nmk_chip->chip;
  1122. chip->base = pdata->first_gpio;
  1123. chip->ngpio = pdata->num_gpio;
  1124. chip->label = pdata->name ?: dev_name(&dev->dev);
  1125. chip->dev = &dev->dev;
  1126. chip->owner = THIS_MODULE;
  1127. clk_enable(nmk_chip->clk);
  1128. nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
  1129. clk_disable(nmk_chip->clk);
  1130. #ifdef CONFIG_OF_GPIO
  1131. chip->of_node = np;
  1132. #endif
  1133. ret = gpiochip_add(&nmk_chip->chip);
  1134. if (ret)
  1135. goto out;
  1136. BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
  1137. nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
  1138. platform_set_drvdata(dev, nmk_chip);
  1139. if (!np)
  1140. irq_start = NOMADIK_GPIO_TO_IRQ(pdata->first_gpio);
  1141. nmk_chip->domain = irq_domain_add_simple(np,
  1142. NMK_GPIO_PER_CHIP, irq_start,
  1143. &nmk_gpio_irq_simple_ops, nmk_chip);
  1144. if (!nmk_chip->domain) {
  1145. dev_err(&dev->dev, "failed to create irqdomain\n");
  1146. ret = -ENOSYS;
  1147. goto out;
  1148. }
  1149. nmk_gpio_init_irq(nmk_chip);
  1150. dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
  1151. return 0;
  1152. out:
  1153. dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
  1154. pdata->first_gpio, pdata->first_gpio+31);
  1155. return ret;
  1156. }
  1157. static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
  1158. {
  1159. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1160. return npct->soc->ngroups;
  1161. }
  1162. static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
  1163. unsigned selector)
  1164. {
  1165. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1166. return npct->soc->groups[selector].name;
  1167. }
  1168. static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  1169. const unsigned **pins,
  1170. unsigned *num_pins)
  1171. {
  1172. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1173. *pins = npct->soc->groups[selector].pins;
  1174. *num_pins = npct->soc->groups[selector].npins;
  1175. return 0;
  1176. }
  1177. static struct pinctrl_gpio_range *
  1178. nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
  1179. {
  1180. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1181. int i;
  1182. for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
  1183. struct pinctrl_gpio_range *range;
  1184. range = &npct->soc->gpio_ranges[i];
  1185. if (offset >= range->pin_base &&
  1186. offset <= (range->pin_base + range->npins - 1))
  1187. return range;
  1188. }
  1189. return NULL;
  1190. }
  1191. static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  1192. unsigned offset)
  1193. {
  1194. struct pinctrl_gpio_range *range;
  1195. struct gpio_chip *chip;
  1196. range = nmk_match_gpio_range(pctldev, offset);
  1197. if (!range || !range->gc) {
  1198. seq_printf(s, "invalid pin offset");
  1199. return;
  1200. }
  1201. chip = range->gc;
  1202. nmk_gpio_dbg_show_one(s, chip, offset - chip->base, offset);
  1203. }
  1204. static struct pinctrl_ops nmk_pinctrl_ops = {
  1205. .get_groups_count = nmk_get_groups_cnt,
  1206. .get_group_name = nmk_get_group_name,
  1207. .get_group_pins = nmk_get_group_pins,
  1208. .pin_dbg_show = nmk_pin_dbg_show,
  1209. };
  1210. static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  1211. {
  1212. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1213. return npct->soc->nfunctions;
  1214. }
  1215. static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
  1216. unsigned function)
  1217. {
  1218. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1219. return npct->soc->functions[function].name;
  1220. }
  1221. static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  1222. unsigned function,
  1223. const char * const **groups,
  1224. unsigned * const num_groups)
  1225. {
  1226. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1227. *groups = npct->soc->functions[function].groups;
  1228. *num_groups = npct->soc->functions[function].ngroups;
  1229. return 0;
  1230. }
  1231. static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
  1232. unsigned group)
  1233. {
  1234. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1235. const struct nmk_pingroup *g;
  1236. static unsigned int slpm[NUM_BANKS];
  1237. unsigned long flags;
  1238. bool glitch;
  1239. int ret = -EINVAL;
  1240. int i;
  1241. g = &npct->soc->groups[group];
  1242. if (g->altsetting < 0)
  1243. return -EINVAL;
  1244. dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
  1245. /*
  1246. * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
  1247. * we may pass through an undesired state. In this case we take
  1248. * some extra care.
  1249. *
  1250. * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
  1251. * - Save SLPM registers (since we have a shadow register in the
  1252. * nmk_chip we're using that as backup)
  1253. * - Set SLPM=0 for the IOs you want to switch and others to 1
  1254. * - Configure the GPIO registers for the IOs that are being switched
  1255. * - Set IOFORCE=1
  1256. * - Modify the AFLSA/B registers for the IOs that are being switched
  1257. * - Set IOFORCE=0
  1258. * - Restore SLPM registers
  1259. * - Any spurious wake up event during switch sequence to be ignored
  1260. * and cleared
  1261. *
  1262. * We REALLY need to save ALL slpm registers, because the external
  1263. * IOFORCE will switch *all* ports to their sleepmode setting to as
  1264. * to avoid glitches. (Not just one port!)
  1265. */
  1266. glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
  1267. if (glitch) {
  1268. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  1269. /* Initially don't put any pins to sleep when switching */
  1270. memset(slpm, 0xff, sizeof(slpm));
  1271. /*
  1272. * Then mask the pins that need to be sleeping now when we're
  1273. * switching to the ALT C function.
  1274. */
  1275. for (i = 0; i < g->npins; i++)
  1276. slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
  1277. nmk_gpio_glitch_slpm_init(slpm);
  1278. }
  1279. for (i = 0; i < g->npins; i++) {
  1280. struct pinctrl_gpio_range *range;
  1281. struct nmk_gpio_chip *nmk_chip;
  1282. struct gpio_chip *chip;
  1283. unsigned bit;
  1284. range = nmk_match_gpio_range(pctldev, g->pins[i]);
  1285. if (!range) {
  1286. dev_err(npct->dev,
  1287. "invalid pin offset %d in group %s at index %d\n",
  1288. g->pins[i], g->name, i);
  1289. goto out_glitch;
  1290. }
  1291. if (!range->gc) {
  1292. dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
  1293. g->pins[i], g->name, i);
  1294. goto out_glitch;
  1295. }
  1296. chip = range->gc;
  1297. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1298. dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
  1299. clk_enable(nmk_chip->clk);
  1300. bit = g->pins[i] % NMK_GPIO_PER_CHIP;
  1301. /*
  1302. * If the pin is switching to altfunc, and there was an
  1303. * interrupt installed on it which has been lazy disabled,
  1304. * actually mask the interrupt to prevent spurious interrupts
  1305. * that would occur while the pin is under control of the
  1306. * peripheral. Only SKE does this.
  1307. */
  1308. nmk_gpio_disable_lazy_irq(nmk_chip, bit);
  1309. __nmk_gpio_set_mode_safe(nmk_chip, bit,
  1310. (g->altsetting & NMK_GPIO_ALT_C), glitch);
  1311. clk_disable(nmk_chip->clk);
  1312. /*
  1313. * Call PRCM GPIOCR config function in case ALTC
  1314. * has been selected:
  1315. * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
  1316. * must be set.
  1317. * - If selection is pure ALTC and previous selection was ALTCx,
  1318. * then some bits in PRCM GPIOCR registers must be cleared.
  1319. */
  1320. if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
  1321. nmk_prcm_altcx_set_mode(npct, g->pins[i],
  1322. g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
  1323. }
  1324. /* When all pins are successfully reconfigured we get here */
  1325. ret = 0;
  1326. out_glitch:
  1327. if (glitch) {
  1328. nmk_gpio_glitch_slpm_restore(slpm);
  1329. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  1330. }
  1331. return ret;
  1332. }
  1333. static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
  1334. unsigned function, unsigned group)
  1335. {
  1336. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1337. const struct nmk_pingroup *g;
  1338. g = &npct->soc->groups[group];
  1339. if (g->altsetting < 0)
  1340. return;
  1341. /* Poke out the mux, set the pin to some default state? */
  1342. dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
  1343. }
  1344. int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
  1345. struct pinctrl_gpio_range *range,
  1346. unsigned offset)
  1347. {
  1348. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1349. struct nmk_gpio_chip *nmk_chip;
  1350. struct gpio_chip *chip;
  1351. unsigned bit;
  1352. if (!range) {
  1353. dev_err(npct->dev, "invalid range\n");
  1354. return -EINVAL;
  1355. }
  1356. if (!range->gc) {
  1357. dev_err(npct->dev, "missing GPIO chip in range\n");
  1358. return -EINVAL;
  1359. }
  1360. chip = range->gc;
  1361. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1362. dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
  1363. clk_enable(nmk_chip->clk);
  1364. bit = offset % NMK_GPIO_PER_CHIP;
  1365. /* There is no glitch when converting any pin to GPIO */
  1366. __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
  1367. clk_disable(nmk_chip->clk);
  1368. return 0;
  1369. }
  1370. void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
  1371. struct pinctrl_gpio_range *range,
  1372. unsigned offset)
  1373. {
  1374. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1375. dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
  1376. /* Set the pin to some default state, GPIO is usually default */
  1377. }
  1378. static struct pinmux_ops nmk_pinmux_ops = {
  1379. .get_functions_count = nmk_pmx_get_funcs_cnt,
  1380. .get_function_name = nmk_pmx_get_func_name,
  1381. .get_function_groups = nmk_pmx_get_func_groups,
  1382. .enable = nmk_pmx_enable,
  1383. .disable = nmk_pmx_disable,
  1384. .gpio_request_enable = nmk_gpio_request_enable,
  1385. .gpio_disable_free = nmk_gpio_disable_free,
  1386. };
  1387. int nmk_pin_config_get(struct pinctrl_dev *pctldev,
  1388. unsigned pin,
  1389. unsigned long *config)
  1390. {
  1391. /* Not implemented */
  1392. return -EINVAL;
  1393. }
  1394. int nmk_pin_config_set(struct pinctrl_dev *pctldev,
  1395. unsigned pin,
  1396. unsigned long config)
  1397. {
  1398. static const char *pullnames[] = {
  1399. [NMK_GPIO_PULL_NONE] = "none",
  1400. [NMK_GPIO_PULL_UP] = "up",
  1401. [NMK_GPIO_PULL_DOWN] = "down",
  1402. [3] /* illegal */ = "??"
  1403. };
  1404. static const char *slpmnames[] = {
  1405. [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
  1406. [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
  1407. };
  1408. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1409. struct nmk_gpio_chip *nmk_chip;
  1410. struct pinctrl_gpio_range *range;
  1411. struct gpio_chip *chip;
  1412. unsigned bit;
  1413. /*
  1414. * The pin config contains pin number and altfunction fields, here
  1415. * we just ignore that part. It's being handled by the framework and
  1416. * pinmux callback respectively.
  1417. */
  1418. pin_cfg_t cfg = (pin_cfg_t) config;
  1419. int pull = PIN_PULL(cfg);
  1420. int slpm = PIN_SLPM(cfg);
  1421. int output = PIN_DIR(cfg);
  1422. int val = PIN_VAL(cfg);
  1423. bool lowemi = PIN_LOWEMI(cfg);
  1424. bool gpiomode = PIN_GPIOMODE(cfg);
  1425. bool sleep = PIN_SLEEPMODE(cfg);
  1426. range = nmk_match_gpio_range(pctldev, pin);
  1427. if (!range) {
  1428. dev_err(npct->dev, "invalid pin offset %d\n", pin);
  1429. return -EINVAL;
  1430. }
  1431. if (!range->gc) {
  1432. dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
  1433. pin);
  1434. return -EINVAL;
  1435. }
  1436. chip = range->gc;
  1437. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1438. if (sleep) {
  1439. int slpm_pull = PIN_SLPM_PULL(cfg);
  1440. int slpm_output = PIN_SLPM_DIR(cfg);
  1441. int slpm_val = PIN_SLPM_VAL(cfg);
  1442. /* All pins go into GPIO mode at sleep */
  1443. gpiomode = true;
  1444. /*
  1445. * The SLPM_* values are normal values + 1 to allow zero to
  1446. * mean "same as normal".
  1447. */
  1448. if (slpm_pull)
  1449. pull = slpm_pull - 1;
  1450. if (slpm_output)
  1451. output = slpm_output - 1;
  1452. if (slpm_val)
  1453. val = slpm_val - 1;
  1454. dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
  1455. pin,
  1456. slpm_pull ? pullnames[pull] : "same",
  1457. slpm_output ? (output ? "output" : "input") : "same",
  1458. slpm_val ? (val ? "high" : "low") : "same");
  1459. }
  1460. dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
  1461. pin, cfg, pullnames[pull], slpmnames[slpm],
  1462. output ? "output " : "input",
  1463. output ? (val ? "high" : "low") : "",
  1464. lowemi ? "on" : "off" );
  1465. clk_enable(nmk_chip->clk);
  1466. bit = pin % NMK_GPIO_PER_CHIP;
  1467. if (gpiomode)
  1468. /* No glitch when going to GPIO mode */
  1469. __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
  1470. if (output)
  1471. __nmk_gpio_make_output(nmk_chip, bit, val);
  1472. else {
  1473. __nmk_gpio_make_input(nmk_chip, bit);
  1474. __nmk_gpio_set_pull(nmk_chip, bit, pull);
  1475. }
  1476. /* TODO: isn't this only applicable on output pins? */
  1477. __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
  1478. __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
  1479. clk_disable(nmk_chip->clk);
  1480. return 0;
  1481. }
  1482. static struct pinconf_ops nmk_pinconf_ops = {
  1483. .pin_config_get = nmk_pin_config_get,
  1484. .pin_config_set = nmk_pin_config_set,
  1485. };
  1486. static struct pinctrl_desc nmk_pinctrl_desc = {
  1487. .name = "pinctrl-nomadik",
  1488. .pctlops = &nmk_pinctrl_ops,
  1489. .pmxops = &nmk_pinmux_ops,
  1490. .confops = &nmk_pinconf_ops,
  1491. .owner = THIS_MODULE,
  1492. };
  1493. static const struct of_device_id nmk_pinctrl_match[] = {
  1494. {
  1495. .compatible = "stericsson,nmk_pinctrl",
  1496. .data = (void *)PINCTRL_NMK_DB8500,
  1497. },
  1498. {},
  1499. };
  1500. static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
  1501. {
  1502. const struct platform_device_id *platid = platform_get_device_id(pdev);
  1503. struct device_node *np = pdev->dev.of_node;
  1504. struct nmk_pinctrl *npct;
  1505. unsigned int version = 0;
  1506. int i;
  1507. npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
  1508. if (!npct)
  1509. return -ENOMEM;
  1510. if (platid)
  1511. version = platid->driver_data;
  1512. else if (np)
  1513. version = (unsigned int)
  1514. of_match_device(nmk_pinctrl_match, &pdev->dev)->data;
  1515. /* Poke in other ASIC variants here */
  1516. if (version == PINCTRL_NMK_STN8815)
  1517. nmk_pinctrl_stn8815_init(&npct->soc);
  1518. if (version == PINCTRL_NMK_DB8500)
  1519. nmk_pinctrl_db8500_init(&npct->soc);
  1520. if (version == PINCTRL_NMK_DB8540)
  1521. nmk_pinctrl_db8540_init(&npct->soc);
  1522. /*
  1523. * We need all the GPIO drivers to probe FIRST, or we will not be able
  1524. * to obtain references to the struct gpio_chip * for them, and we
  1525. * need this to proceed.
  1526. */
  1527. for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
  1528. if (!nmk_gpio_chips[i]) {
  1529. dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
  1530. return -EPROBE_DEFER;
  1531. }
  1532. npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[i]->chip;
  1533. }
  1534. nmk_pinctrl_desc.pins = npct->soc->pins;
  1535. nmk_pinctrl_desc.npins = npct->soc->npins;
  1536. npct->dev = &pdev->dev;
  1537. npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
  1538. if (!npct->pctl) {
  1539. dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
  1540. return -EINVAL;
  1541. }
  1542. /* We will handle a range of GPIO pins */
  1543. for (i = 0; i < npct->soc->gpio_num_ranges; i++)
  1544. pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
  1545. platform_set_drvdata(pdev, npct);
  1546. dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
  1547. return 0;
  1548. }
  1549. static const struct of_device_id nmk_gpio_match[] = {
  1550. { .compatible = "st,nomadik-gpio", },
  1551. {}
  1552. };
  1553. static struct platform_driver nmk_gpio_driver = {
  1554. .driver = {
  1555. .owner = THIS_MODULE,
  1556. .name = "gpio",
  1557. .of_match_table = nmk_gpio_match,
  1558. },
  1559. .probe = nmk_gpio_probe,
  1560. };
  1561. static const struct platform_device_id nmk_pinctrl_id[] = {
  1562. { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
  1563. { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
  1564. { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
  1565. };
  1566. static struct platform_driver nmk_pinctrl_driver = {
  1567. .driver = {
  1568. .owner = THIS_MODULE,
  1569. .name = "pinctrl-nomadik",
  1570. .of_match_table = nmk_pinctrl_match,
  1571. },
  1572. .probe = nmk_pinctrl_probe,
  1573. .id_table = nmk_pinctrl_id,
  1574. };
  1575. static int __init nmk_gpio_init(void)
  1576. {
  1577. int ret;
  1578. ret = platform_driver_register(&nmk_gpio_driver);
  1579. if (ret)
  1580. return ret;
  1581. return platform_driver_register(&nmk_pinctrl_driver);
  1582. }
  1583. core_initcall(nmk_gpio_init);
  1584. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  1585. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  1586. MODULE_LICENSE("GPL");