pinctrl-nomadik.c 49 KB

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