pinctrl-nomadik.c 48 KB

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