pinctrl-nomadik.c 49 KB

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