pinctrl-nomadik.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  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. clk_enable(nmk_chip->clk);
  532. nmk_gpio_irq_unmask(d);
  533. return 0;
  534. }
  535. static void nmk_gpio_irq_shutdown(struct irq_data *d)
  536. {
  537. struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
  538. nmk_gpio_irq_mask(d);
  539. clk_disable(nmk_chip->clk);
  540. }
  541. static struct irq_chip nmk_gpio_irq_chip = {
  542. .name = "Nomadik-GPIO",
  543. .irq_ack = nmk_gpio_irq_ack,
  544. .irq_mask = nmk_gpio_irq_mask,
  545. .irq_unmask = nmk_gpio_irq_unmask,
  546. .irq_set_type = nmk_gpio_irq_set_type,
  547. .irq_set_wake = nmk_gpio_irq_set_wake,
  548. .irq_startup = nmk_gpio_irq_startup,
  549. .irq_shutdown = nmk_gpio_irq_shutdown,
  550. .flags = IRQCHIP_MASK_ON_SUSPEND,
  551. };
  552. static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
  553. u32 status)
  554. {
  555. struct nmk_gpio_chip *nmk_chip;
  556. struct irq_chip *host_chip = irq_get_chip(irq);
  557. chained_irq_enter(host_chip, desc);
  558. nmk_chip = irq_get_handler_data(irq);
  559. while (status) {
  560. int bit = __ffs(status);
  561. generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
  562. status &= ~BIT(bit);
  563. }
  564. chained_irq_exit(host_chip, desc);
  565. }
  566. static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  567. {
  568. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  569. u32 status;
  570. clk_enable(nmk_chip->clk);
  571. status = readl(nmk_chip->addr + NMK_GPIO_IS);
  572. clk_disable(nmk_chip->clk);
  573. __nmk_gpio_irq_handler(irq, desc, status);
  574. }
  575. static void nmk_gpio_secondary_irq_handler(unsigned int irq,
  576. struct irq_desc *desc)
  577. {
  578. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  579. u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
  580. __nmk_gpio_irq_handler(irq, desc, status);
  581. }
  582. static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
  583. {
  584. irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
  585. irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
  586. if (nmk_chip->secondary_parent_irq >= 0) {
  587. irq_set_chained_handler(nmk_chip->secondary_parent_irq,
  588. nmk_gpio_secondary_irq_handler);
  589. irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
  590. }
  591. return 0;
  592. }
  593. /* I/O Functions */
  594. static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
  595. {
  596. /*
  597. * Map back to global GPIO space and request muxing, the direction
  598. * parameter does not matter for this controller.
  599. */
  600. int gpio = chip->base + offset;
  601. return pinctrl_request_gpio(gpio);
  602. }
  603. static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
  604. {
  605. int gpio = chip->base + offset;
  606. pinctrl_free_gpio(gpio);
  607. }
  608. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
  609. {
  610. struct nmk_gpio_chip *nmk_chip =
  611. container_of(chip, struct nmk_gpio_chip, chip);
  612. clk_enable(nmk_chip->clk);
  613. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  614. clk_disable(nmk_chip->clk);
  615. return 0;
  616. }
  617. static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
  618. {
  619. struct nmk_gpio_chip *nmk_chip =
  620. container_of(chip, struct nmk_gpio_chip, chip);
  621. u32 bit = 1 << offset;
  622. int value;
  623. clk_enable(nmk_chip->clk);
  624. value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  625. clk_disable(nmk_chip->clk);
  626. return value;
  627. }
  628. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  629. int val)
  630. {
  631. struct nmk_gpio_chip *nmk_chip =
  632. container_of(chip, struct nmk_gpio_chip, chip);
  633. clk_enable(nmk_chip->clk);
  634. __nmk_gpio_set_output(nmk_chip, offset, val);
  635. clk_disable(nmk_chip->clk);
  636. }
  637. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
  638. int val)
  639. {
  640. struct nmk_gpio_chip *nmk_chip =
  641. container_of(chip, struct nmk_gpio_chip, chip);
  642. clk_enable(nmk_chip->clk);
  643. __nmk_gpio_make_output(nmk_chip, offset, val);
  644. clk_disable(nmk_chip->clk);
  645. return 0;
  646. }
  647. static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  648. {
  649. struct nmk_gpio_chip *nmk_chip =
  650. container_of(chip, struct nmk_gpio_chip, chip);
  651. return irq_create_mapping(nmk_chip->domain, offset);
  652. }
  653. #ifdef CONFIG_DEBUG_FS
  654. #include <linux/seq_file.h>
  655. static void nmk_gpio_dbg_show_one(struct seq_file *s,
  656. struct pinctrl_dev *pctldev, struct gpio_chip *chip,
  657. unsigned offset, unsigned gpio)
  658. {
  659. const char *label = gpiochip_is_requested(chip, offset);
  660. struct nmk_gpio_chip *nmk_chip =
  661. container_of(chip, struct nmk_gpio_chip, chip);
  662. int mode;
  663. bool is_out;
  664. bool pull;
  665. u32 bit = 1 << offset;
  666. const char *modes[] = {
  667. [NMK_GPIO_ALT_GPIO] = "gpio",
  668. [NMK_GPIO_ALT_A] = "altA",
  669. [NMK_GPIO_ALT_B] = "altB",
  670. [NMK_GPIO_ALT_C] = "altC",
  671. [NMK_GPIO_ALT_C+1] = "altC1",
  672. [NMK_GPIO_ALT_C+2] = "altC2",
  673. [NMK_GPIO_ALT_C+3] = "altC3",
  674. [NMK_GPIO_ALT_C+4] = "altC4",
  675. };
  676. clk_enable(nmk_chip->clk);
  677. is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
  678. pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
  679. mode = nmk_gpio_get_mode(gpio);
  680. if ((mode == NMK_GPIO_ALT_C) && pctldev)
  681. mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio);
  682. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
  683. gpio, label ?: "(none)",
  684. is_out ? "out" : "in ",
  685. chip->get
  686. ? (chip->get(chip, offset) ? "hi" : "lo")
  687. : "? ",
  688. (mode < 0) ? "unknown" : modes[mode],
  689. pull ? "pull" : "none");
  690. if (label && !is_out) {
  691. int irq = gpio_to_irq(gpio);
  692. struct irq_desc *desc = irq_to_desc(irq);
  693. /* This races with request_irq(), set_irq_type(),
  694. * and set_irq_wake() ... but those are "rare".
  695. */
  696. if (irq >= 0 && desc->action) {
  697. char *trigger;
  698. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  699. if (nmk_chip->edge_rising & bitmask)
  700. trigger = "edge-rising";
  701. else if (nmk_chip->edge_falling & bitmask)
  702. trigger = "edge-falling";
  703. else
  704. trigger = "edge-undefined";
  705. seq_printf(s, " irq-%d %s%s",
  706. irq, trigger,
  707. irqd_is_wakeup_set(&desc->irq_data)
  708. ? " wakeup" : "");
  709. }
  710. }
  711. clk_disable(nmk_chip->clk);
  712. }
  713. static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  714. {
  715. unsigned i;
  716. unsigned gpio = chip->base;
  717. for (i = 0; i < chip->ngpio; i++, gpio++) {
  718. nmk_gpio_dbg_show_one(s, NULL, chip, i, gpio);
  719. seq_printf(s, "\n");
  720. }
  721. }
  722. #else
  723. static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
  724. struct pinctrl_dev *pctldev,
  725. struct gpio_chip *chip,
  726. unsigned offset, unsigned gpio)
  727. {
  728. }
  729. #define nmk_gpio_dbg_show NULL
  730. #endif
  731. /* This structure is replicated for each GPIO block allocated at probe time */
  732. static struct gpio_chip nmk_gpio_template = {
  733. .request = nmk_gpio_request,
  734. .free = nmk_gpio_free,
  735. .direction_input = nmk_gpio_make_input,
  736. .get = nmk_gpio_get_input,
  737. .direction_output = nmk_gpio_make_output,
  738. .set = nmk_gpio_set_output,
  739. .to_irq = nmk_gpio_to_irq,
  740. .dbg_show = nmk_gpio_dbg_show,
  741. .can_sleep = 0,
  742. };
  743. void nmk_gpio_clocks_enable(void)
  744. {
  745. int i;
  746. for (i = 0; i < NUM_BANKS; i++) {
  747. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  748. if (!chip)
  749. continue;
  750. clk_enable(chip->clk);
  751. }
  752. }
  753. void nmk_gpio_clocks_disable(void)
  754. {
  755. int i;
  756. for (i = 0; i < NUM_BANKS; i++) {
  757. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  758. if (!chip)
  759. continue;
  760. clk_disable(chip->clk);
  761. }
  762. }
  763. /*
  764. * Called from the suspend/resume path to only keep the real wakeup interrupts
  765. * (those that have had set_irq_wake() called on them) as wakeup interrupts,
  766. * and not the rest of the interrupts which we needed to have as wakeups for
  767. * cpuidle.
  768. *
  769. * PM ops are not used since this needs to be done at the end, after all the
  770. * other drivers are done with their suspend callbacks.
  771. */
  772. void nmk_gpio_wakeups_suspend(void)
  773. {
  774. int i;
  775. for (i = 0; i < NUM_BANKS; i++) {
  776. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  777. if (!chip)
  778. break;
  779. clk_enable(chip->clk);
  780. writel(chip->rwimsc & chip->real_wake,
  781. chip->addr + NMK_GPIO_RWIMSC);
  782. writel(chip->fwimsc & chip->real_wake,
  783. chip->addr + NMK_GPIO_FWIMSC);
  784. clk_disable(chip->clk);
  785. }
  786. }
  787. void nmk_gpio_wakeups_resume(void)
  788. {
  789. int i;
  790. for (i = 0; i < NUM_BANKS; i++) {
  791. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  792. if (!chip)
  793. break;
  794. clk_enable(chip->clk);
  795. writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
  796. writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
  797. clk_disable(chip->clk);
  798. }
  799. }
  800. /*
  801. * Read the pull up/pull down status.
  802. * A bit set in 'pull_up' means that pull up
  803. * is selected if pull is enabled in PDIS register.
  804. * Note: only pull up/down set via this driver can
  805. * be detected due to HW limitations.
  806. */
  807. void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
  808. {
  809. if (gpio_bank < NUM_BANKS) {
  810. struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
  811. if (!chip)
  812. return;
  813. *pull_up = chip->pull_up;
  814. }
  815. }
  816. static int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
  817. irq_hw_number_t hwirq)
  818. {
  819. struct nmk_gpio_chip *nmk_chip = d->host_data;
  820. if (!nmk_chip)
  821. return -EINVAL;
  822. irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
  823. set_irq_flags(irq, IRQF_VALID);
  824. irq_set_chip_data(irq, nmk_chip);
  825. irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
  826. return 0;
  827. }
  828. static const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
  829. .map = nmk_gpio_irq_map,
  830. .xlate = irq_domain_xlate_twocell,
  831. };
  832. static int nmk_gpio_probe(struct platform_device *dev)
  833. {
  834. struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
  835. struct device_node *np = dev->dev.of_node;
  836. struct nmk_gpio_chip *nmk_chip;
  837. struct gpio_chip *chip;
  838. struct resource *res;
  839. struct clk *clk;
  840. int secondary_irq;
  841. void __iomem *base;
  842. int irq_start = 0;
  843. int irq;
  844. int ret;
  845. if (!pdata && !np) {
  846. dev_err(&dev->dev, "No platform data or device tree found\n");
  847. return -ENODEV;
  848. }
  849. if (np) {
  850. pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
  851. if (!pdata)
  852. return -ENOMEM;
  853. if (of_get_property(np, "st,supports-sleepmode", NULL))
  854. pdata->supports_sleepmode = true;
  855. if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
  856. dev_err(&dev->dev, "gpio-bank property not found\n");
  857. return -EINVAL;
  858. }
  859. pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
  860. pdata->num_gpio = NMK_GPIO_PER_CHIP;
  861. }
  862. irq = platform_get_irq(dev, 0);
  863. if (irq < 0)
  864. return irq;
  865. secondary_irq = platform_get_irq(dev, 1);
  866. if (secondary_irq >= 0 && !pdata->get_secondary_status)
  867. return -EINVAL;
  868. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  869. base = devm_ioremap_resource(&dev->dev, res);
  870. if (IS_ERR(base))
  871. return PTR_ERR(base);
  872. clk = devm_clk_get(&dev->dev, NULL);
  873. if (IS_ERR(clk))
  874. return PTR_ERR(clk);
  875. clk_prepare(clk);
  876. nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
  877. if (!nmk_chip)
  878. return -ENOMEM;
  879. /*
  880. * The virt address in nmk_chip->addr is in the nomadik register space,
  881. * so we can simply convert the resource address, without remapping
  882. */
  883. nmk_chip->bank = dev->id;
  884. nmk_chip->clk = clk;
  885. nmk_chip->addr = base;
  886. nmk_chip->chip = nmk_gpio_template;
  887. nmk_chip->parent_irq = irq;
  888. nmk_chip->secondary_parent_irq = secondary_irq;
  889. nmk_chip->get_secondary_status = pdata->get_secondary_status;
  890. nmk_chip->set_ioforce = pdata->set_ioforce;
  891. nmk_chip->sleepmode = pdata->supports_sleepmode;
  892. spin_lock_init(&nmk_chip->lock);
  893. chip = &nmk_chip->chip;
  894. chip->base = pdata->first_gpio;
  895. chip->ngpio = pdata->num_gpio;
  896. chip->label = pdata->name ?: dev_name(&dev->dev);
  897. chip->dev = &dev->dev;
  898. chip->owner = THIS_MODULE;
  899. clk_enable(nmk_chip->clk);
  900. nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
  901. clk_disable(nmk_chip->clk);
  902. #ifdef CONFIG_OF_GPIO
  903. chip->of_node = np;
  904. #endif
  905. ret = gpiochip_add(&nmk_chip->chip);
  906. if (ret)
  907. return ret;
  908. BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
  909. nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
  910. platform_set_drvdata(dev, nmk_chip);
  911. if (!np)
  912. irq_start = pdata->first_irq;
  913. nmk_chip->domain = irq_domain_add_simple(np,
  914. NMK_GPIO_PER_CHIP, irq_start,
  915. &nmk_gpio_irq_simple_ops, nmk_chip);
  916. if (!nmk_chip->domain) {
  917. dev_err(&dev->dev, "failed to create irqdomain\n");
  918. /* Just do this, no matter if it fails */
  919. ret = gpiochip_remove(&nmk_chip->chip);
  920. return -ENOSYS;
  921. }
  922. nmk_gpio_init_irq(nmk_chip);
  923. dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
  924. return 0;
  925. }
  926. static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
  927. {
  928. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  929. return npct->soc->ngroups;
  930. }
  931. static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
  932. unsigned selector)
  933. {
  934. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  935. return npct->soc->groups[selector].name;
  936. }
  937. static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  938. const unsigned **pins,
  939. unsigned *num_pins)
  940. {
  941. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  942. *pins = npct->soc->groups[selector].pins;
  943. *num_pins = npct->soc->groups[selector].npins;
  944. return 0;
  945. }
  946. static struct pinctrl_gpio_range *
  947. nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
  948. {
  949. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  950. int i;
  951. for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
  952. struct pinctrl_gpio_range *range;
  953. range = &npct->soc->gpio_ranges[i];
  954. if (offset >= range->pin_base &&
  955. offset <= (range->pin_base + range->npins - 1))
  956. return range;
  957. }
  958. return NULL;
  959. }
  960. static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  961. unsigned offset)
  962. {
  963. struct pinctrl_gpio_range *range;
  964. struct gpio_chip *chip;
  965. range = nmk_match_gpio_range(pctldev, offset);
  966. if (!range || !range->gc) {
  967. seq_printf(s, "invalid pin offset");
  968. return;
  969. }
  970. chip = range->gc;
  971. nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base, offset);
  972. }
  973. static void nmk_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
  974. struct pinctrl_map *map, unsigned num_maps)
  975. {
  976. int i;
  977. for (i = 0; i < num_maps; i++)
  978. if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  979. kfree(map[i].data.configs.configs);
  980. kfree(map);
  981. }
  982. static int nmk_dt_reserve_map(struct pinctrl_map **map, unsigned *reserved_maps,
  983. unsigned *num_maps, unsigned reserve)
  984. {
  985. unsigned old_num = *reserved_maps;
  986. unsigned new_num = *num_maps + reserve;
  987. struct pinctrl_map *new_map;
  988. if (old_num >= new_num)
  989. return 0;
  990. new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
  991. if (!new_map)
  992. return -ENOMEM;
  993. memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
  994. *map = new_map;
  995. *reserved_maps = new_num;
  996. return 0;
  997. }
  998. static int nmk_dt_add_map_mux(struct pinctrl_map **map, unsigned *reserved_maps,
  999. unsigned *num_maps, const char *group,
  1000. const char *function)
  1001. {
  1002. if (*num_maps == *reserved_maps)
  1003. return -ENOSPC;
  1004. (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
  1005. (*map)[*num_maps].data.mux.group = group;
  1006. (*map)[*num_maps].data.mux.function = function;
  1007. (*num_maps)++;
  1008. return 0;
  1009. }
  1010. static int nmk_dt_add_map_configs(struct pinctrl_map **map,
  1011. unsigned *reserved_maps,
  1012. unsigned *num_maps, const char *group,
  1013. unsigned long *configs, unsigned num_configs)
  1014. {
  1015. unsigned long *dup_configs;
  1016. if (*num_maps == *reserved_maps)
  1017. return -ENOSPC;
  1018. dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
  1019. GFP_KERNEL);
  1020. if (!dup_configs)
  1021. return -ENOMEM;
  1022. (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
  1023. (*map)[*num_maps].data.configs.group_or_pin = group;
  1024. (*map)[*num_maps].data.configs.configs = dup_configs;
  1025. (*map)[*num_maps].data.configs.num_configs = num_configs;
  1026. (*num_maps)++;
  1027. return 0;
  1028. }
  1029. #define NMK_CONFIG_PIN(x, y) { .property = x, .config = y, }
  1030. #define NMK_CONFIG_PIN_ARRAY(x, y) { .property = x, .choice = y, \
  1031. .size = ARRAY_SIZE(y), }
  1032. static const unsigned long nmk_pin_input_modes[] = {
  1033. PIN_INPUT_NOPULL,
  1034. PIN_INPUT_PULLUP,
  1035. PIN_INPUT_PULLDOWN,
  1036. };
  1037. static const unsigned long nmk_pin_output_modes[] = {
  1038. PIN_OUTPUT_LOW,
  1039. PIN_OUTPUT_HIGH,
  1040. PIN_DIR_OUTPUT,
  1041. };
  1042. static const unsigned long nmk_pin_sleep_modes[] = {
  1043. PIN_SLEEPMODE_DISABLED,
  1044. PIN_SLEEPMODE_ENABLED,
  1045. };
  1046. static const unsigned long nmk_pin_sleep_input_modes[] = {
  1047. PIN_SLPM_INPUT_NOPULL,
  1048. PIN_SLPM_INPUT_PULLUP,
  1049. PIN_SLPM_INPUT_PULLDOWN,
  1050. PIN_SLPM_DIR_INPUT,
  1051. };
  1052. static const unsigned long nmk_pin_sleep_output_modes[] = {
  1053. PIN_SLPM_OUTPUT_LOW,
  1054. PIN_SLPM_OUTPUT_HIGH,
  1055. PIN_SLPM_DIR_OUTPUT,
  1056. };
  1057. static const unsigned long nmk_pin_sleep_wakeup_modes[] = {
  1058. PIN_SLPM_WAKEUP_DISABLE,
  1059. PIN_SLPM_WAKEUP_ENABLE,
  1060. };
  1061. static const unsigned long nmk_pin_gpio_modes[] = {
  1062. PIN_GPIOMODE_DISABLED,
  1063. PIN_GPIOMODE_ENABLED,
  1064. };
  1065. static const unsigned long nmk_pin_sleep_pdis_modes[] = {
  1066. PIN_SLPM_PDIS_DISABLED,
  1067. PIN_SLPM_PDIS_ENABLED,
  1068. };
  1069. struct nmk_cfg_param {
  1070. const char *property;
  1071. unsigned long config;
  1072. const unsigned long *choice;
  1073. int size;
  1074. };
  1075. static const struct nmk_cfg_param nmk_cfg_params[] = {
  1076. NMK_CONFIG_PIN_ARRAY("ste,input", nmk_pin_input_modes),
  1077. NMK_CONFIG_PIN_ARRAY("ste,output", nmk_pin_output_modes),
  1078. NMK_CONFIG_PIN_ARRAY("ste,sleep", nmk_pin_sleep_modes),
  1079. NMK_CONFIG_PIN_ARRAY("ste,sleep-input", nmk_pin_sleep_input_modes),
  1080. NMK_CONFIG_PIN_ARRAY("ste,sleep-output", nmk_pin_sleep_output_modes),
  1081. NMK_CONFIG_PIN_ARRAY("ste,sleep-wakeup", nmk_pin_sleep_wakeup_modes),
  1082. NMK_CONFIG_PIN_ARRAY("ste,gpio", nmk_pin_gpio_modes),
  1083. NMK_CONFIG_PIN_ARRAY("ste,sleep-pull-disable", nmk_pin_sleep_pdis_modes),
  1084. };
  1085. static int nmk_dt_pin_config(int index, int val, unsigned long *config)
  1086. {
  1087. int ret = 0;
  1088. if (nmk_cfg_params[index].choice == NULL)
  1089. *config = nmk_cfg_params[index].config;
  1090. else {
  1091. /* test if out of range */
  1092. if (val < nmk_cfg_params[index].size) {
  1093. *config = nmk_cfg_params[index].config |
  1094. nmk_cfg_params[index].choice[val];
  1095. }
  1096. }
  1097. return ret;
  1098. }
  1099. static const char *nmk_find_pin_name(struct pinctrl_dev *pctldev, const char *pin_name)
  1100. {
  1101. int i, pin_number;
  1102. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1103. if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
  1104. for (i = 0; i < npct->soc->npins; i++)
  1105. if (npct->soc->pins[i].number == pin_number)
  1106. return npct->soc->pins[i].name;
  1107. return NULL;
  1108. }
  1109. static bool nmk_pinctrl_dt_get_config(struct device_node *np,
  1110. unsigned long *configs)
  1111. {
  1112. bool has_config = 0;
  1113. unsigned long cfg = 0;
  1114. int i, val, ret;
  1115. for (i = 0; i < ARRAY_SIZE(nmk_cfg_params); i++) {
  1116. ret = of_property_read_u32(np,
  1117. nmk_cfg_params[i].property, &val);
  1118. if (ret != -EINVAL) {
  1119. if (nmk_dt_pin_config(i, val, &cfg) == 0) {
  1120. *configs |= cfg;
  1121. has_config = 1;
  1122. }
  1123. }
  1124. }
  1125. return has_config;
  1126. }
  1127. static int nmk_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  1128. struct device_node *np,
  1129. struct pinctrl_map **map,
  1130. unsigned *reserved_maps,
  1131. unsigned *num_maps)
  1132. {
  1133. int ret;
  1134. const char *function = NULL;
  1135. unsigned long configs = 0;
  1136. bool has_config = 0;
  1137. unsigned reserve = 0;
  1138. struct property *prop;
  1139. const char *group, *gpio_name;
  1140. struct device_node *np_config;
  1141. ret = of_property_read_string(np, "ste,function", &function);
  1142. if (ret >= 0)
  1143. reserve = 1;
  1144. has_config = nmk_pinctrl_dt_get_config(np, &configs);
  1145. np_config = of_parse_phandle(np, "ste,config", 0);
  1146. if (np_config)
  1147. has_config |= nmk_pinctrl_dt_get_config(np_config, &configs);
  1148. ret = of_property_count_strings(np, "ste,pins");
  1149. if (ret < 0)
  1150. goto exit;
  1151. if (has_config)
  1152. reserve++;
  1153. reserve *= ret;
  1154. ret = nmk_dt_reserve_map(map, reserved_maps, num_maps, reserve);
  1155. if (ret < 0)
  1156. goto exit;
  1157. of_property_for_each_string(np, "ste,pins", prop, group) {
  1158. if (function) {
  1159. ret = nmk_dt_add_map_mux(map, reserved_maps, num_maps,
  1160. group, function);
  1161. if (ret < 0)
  1162. goto exit;
  1163. }
  1164. if (has_config) {
  1165. gpio_name = nmk_find_pin_name(pctldev, group);
  1166. ret = nmk_dt_add_map_configs(map, reserved_maps, num_maps,
  1167. gpio_name, &configs, 1);
  1168. if (ret < 0)
  1169. goto exit;
  1170. }
  1171. }
  1172. exit:
  1173. return ret;
  1174. }
  1175. static int nmk_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  1176. struct device_node *np_config,
  1177. struct pinctrl_map **map, unsigned *num_maps)
  1178. {
  1179. unsigned reserved_maps;
  1180. struct device_node *np;
  1181. int ret;
  1182. reserved_maps = 0;
  1183. *map = NULL;
  1184. *num_maps = 0;
  1185. for_each_child_of_node(np_config, np) {
  1186. ret = nmk_pinctrl_dt_subnode_to_map(pctldev, np, map,
  1187. &reserved_maps, num_maps);
  1188. if (ret < 0) {
  1189. nmk_pinctrl_dt_free_map(pctldev, *map, *num_maps);
  1190. return ret;
  1191. }
  1192. }
  1193. return 0;
  1194. }
  1195. static const struct pinctrl_ops nmk_pinctrl_ops = {
  1196. .get_groups_count = nmk_get_groups_cnt,
  1197. .get_group_name = nmk_get_group_name,
  1198. .get_group_pins = nmk_get_group_pins,
  1199. .pin_dbg_show = nmk_pin_dbg_show,
  1200. .dt_node_to_map = nmk_pinctrl_dt_node_to_map,
  1201. .dt_free_map = nmk_pinctrl_dt_free_map,
  1202. };
  1203. static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  1204. {
  1205. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1206. return npct->soc->nfunctions;
  1207. }
  1208. static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
  1209. unsigned function)
  1210. {
  1211. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1212. return npct->soc->functions[function].name;
  1213. }
  1214. static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  1215. unsigned function,
  1216. const char * const **groups,
  1217. unsigned * const num_groups)
  1218. {
  1219. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1220. *groups = npct->soc->functions[function].groups;
  1221. *num_groups = npct->soc->functions[function].ngroups;
  1222. return 0;
  1223. }
  1224. static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
  1225. unsigned group)
  1226. {
  1227. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1228. const struct nmk_pingroup *g;
  1229. static unsigned int slpm[NUM_BANKS];
  1230. unsigned long flags = 0;
  1231. bool glitch;
  1232. int ret = -EINVAL;
  1233. int i;
  1234. g = &npct->soc->groups[group];
  1235. if (g->altsetting < 0)
  1236. return -EINVAL;
  1237. dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
  1238. /*
  1239. * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
  1240. * we may pass through an undesired state. In this case we take
  1241. * some extra care.
  1242. *
  1243. * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
  1244. * - Save SLPM registers (since we have a shadow register in the
  1245. * nmk_chip we're using that as backup)
  1246. * - Set SLPM=0 for the IOs you want to switch and others to 1
  1247. * - Configure the GPIO registers for the IOs that are being switched
  1248. * - Set IOFORCE=1
  1249. * - Modify the AFLSA/B registers for the IOs that are being switched
  1250. * - Set IOFORCE=0
  1251. * - Restore SLPM registers
  1252. * - Any spurious wake up event during switch sequence to be ignored
  1253. * and cleared
  1254. *
  1255. * We REALLY need to save ALL slpm registers, because the external
  1256. * IOFORCE will switch *all* ports to their sleepmode setting to as
  1257. * to avoid glitches. (Not just one port!)
  1258. */
  1259. glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
  1260. if (glitch) {
  1261. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  1262. /* Initially don't put any pins to sleep when switching */
  1263. memset(slpm, 0xff, sizeof(slpm));
  1264. /*
  1265. * Then mask the pins that need to be sleeping now when we're
  1266. * switching to the ALT C function.
  1267. */
  1268. for (i = 0; i < g->npins; i++)
  1269. slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
  1270. nmk_gpio_glitch_slpm_init(slpm);
  1271. }
  1272. for (i = 0; i < g->npins; i++) {
  1273. struct pinctrl_gpio_range *range;
  1274. struct nmk_gpio_chip *nmk_chip;
  1275. struct gpio_chip *chip;
  1276. unsigned bit;
  1277. range = nmk_match_gpio_range(pctldev, g->pins[i]);
  1278. if (!range) {
  1279. dev_err(npct->dev,
  1280. "invalid pin offset %d in group %s at index %d\n",
  1281. g->pins[i], g->name, i);
  1282. goto out_glitch;
  1283. }
  1284. if (!range->gc) {
  1285. dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
  1286. g->pins[i], g->name, i);
  1287. goto out_glitch;
  1288. }
  1289. chip = range->gc;
  1290. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1291. dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
  1292. clk_enable(nmk_chip->clk);
  1293. bit = g->pins[i] % NMK_GPIO_PER_CHIP;
  1294. /*
  1295. * If the pin is switching to altfunc, and there was an
  1296. * interrupt installed on it which has been lazy disabled,
  1297. * actually mask the interrupt to prevent spurious interrupts
  1298. * that would occur while the pin is under control of the
  1299. * peripheral. Only SKE does this.
  1300. */
  1301. nmk_gpio_disable_lazy_irq(nmk_chip, bit);
  1302. __nmk_gpio_set_mode_safe(nmk_chip, bit,
  1303. (g->altsetting & NMK_GPIO_ALT_C), glitch);
  1304. clk_disable(nmk_chip->clk);
  1305. /*
  1306. * Call PRCM GPIOCR config function in case ALTC
  1307. * has been selected:
  1308. * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
  1309. * must be set.
  1310. * - If selection is pure ALTC and previous selection was ALTCx,
  1311. * then some bits in PRCM GPIOCR registers must be cleared.
  1312. */
  1313. if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
  1314. nmk_prcm_altcx_set_mode(npct, g->pins[i],
  1315. g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
  1316. }
  1317. /* When all pins are successfully reconfigured we get here */
  1318. ret = 0;
  1319. out_glitch:
  1320. if (glitch) {
  1321. nmk_gpio_glitch_slpm_restore(slpm);
  1322. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  1323. }
  1324. return ret;
  1325. }
  1326. static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
  1327. unsigned function, unsigned group)
  1328. {
  1329. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1330. const struct nmk_pingroup *g;
  1331. g = &npct->soc->groups[group];
  1332. if (g->altsetting < 0)
  1333. return;
  1334. /* Poke out the mux, set the pin to some default state? */
  1335. dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
  1336. }
  1337. static int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
  1338. struct pinctrl_gpio_range *range,
  1339. unsigned offset)
  1340. {
  1341. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1342. struct nmk_gpio_chip *nmk_chip;
  1343. struct gpio_chip *chip;
  1344. unsigned bit;
  1345. if (!range) {
  1346. dev_err(npct->dev, "invalid range\n");
  1347. return -EINVAL;
  1348. }
  1349. if (!range->gc) {
  1350. dev_err(npct->dev, "missing GPIO chip in range\n");
  1351. return -EINVAL;
  1352. }
  1353. chip = range->gc;
  1354. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1355. dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
  1356. clk_enable(nmk_chip->clk);
  1357. bit = offset % NMK_GPIO_PER_CHIP;
  1358. /* There is no glitch when converting any pin to GPIO */
  1359. __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
  1360. clk_disable(nmk_chip->clk);
  1361. return 0;
  1362. }
  1363. static void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
  1364. struct pinctrl_gpio_range *range,
  1365. unsigned offset)
  1366. {
  1367. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1368. dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
  1369. /* Set the pin to some default state, GPIO is usually default */
  1370. }
  1371. static const struct pinmux_ops nmk_pinmux_ops = {
  1372. .get_functions_count = nmk_pmx_get_funcs_cnt,
  1373. .get_function_name = nmk_pmx_get_func_name,
  1374. .get_function_groups = nmk_pmx_get_func_groups,
  1375. .enable = nmk_pmx_enable,
  1376. .disable = nmk_pmx_disable,
  1377. .gpio_request_enable = nmk_gpio_request_enable,
  1378. .gpio_disable_free = nmk_gpio_disable_free,
  1379. };
  1380. static int nmk_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
  1381. unsigned long *config)
  1382. {
  1383. /* Not implemented */
  1384. return -EINVAL;
  1385. }
  1386. static int nmk_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
  1387. unsigned long *configs, unsigned num_configs)
  1388. {
  1389. static const char *pullnames[] = {
  1390. [NMK_GPIO_PULL_NONE] = "none",
  1391. [NMK_GPIO_PULL_UP] = "up",
  1392. [NMK_GPIO_PULL_DOWN] = "down",
  1393. [3] /* illegal */ = "??"
  1394. };
  1395. static const char *slpmnames[] = {
  1396. [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
  1397. [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
  1398. };
  1399. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1400. struct nmk_gpio_chip *nmk_chip;
  1401. struct pinctrl_gpio_range *range;
  1402. struct gpio_chip *chip;
  1403. unsigned bit;
  1404. pin_cfg_t cfg;
  1405. int pull, slpm, output, val, i;
  1406. bool lowemi, gpiomode, sleep;
  1407. range = nmk_match_gpio_range(pctldev, pin);
  1408. if (!range) {
  1409. dev_err(npct->dev, "invalid pin offset %d\n", pin);
  1410. return -EINVAL;
  1411. }
  1412. if (!range->gc) {
  1413. dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
  1414. pin);
  1415. return -EINVAL;
  1416. }
  1417. chip = range->gc;
  1418. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1419. for (i = 0; i < num_configs; i++) {
  1420. /*
  1421. * The pin config contains pin number and altfunction fields,
  1422. * here we just ignore that part. It's being handled by the
  1423. * framework and pinmux callback respectively.
  1424. */
  1425. cfg = (pin_cfg_t) configs[i];
  1426. pull = PIN_PULL(cfg);
  1427. slpm = PIN_SLPM(cfg);
  1428. output = PIN_DIR(cfg);
  1429. val = PIN_VAL(cfg);
  1430. lowemi = PIN_LOWEMI(cfg);
  1431. gpiomode = PIN_GPIOMODE(cfg);
  1432. sleep = PIN_SLEEPMODE(cfg);
  1433. if (sleep) {
  1434. int slpm_pull = PIN_SLPM_PULL(cfg);
  1435. int slpm_output = PIN_SLPM_DIR(cfg);
  1436. int slpm_val = PIN_SLPM_VAL(cfg);
  1437. /* All pins go into GPIO mode at sleep */
  1438. gpiomode = true;
  1439. /*
  1440. * The SLPM_* values are normal values + 1 to allow zero
  1441. * to mean "same as normal".
  1442. */
  1443. if (slpm_pull)
  1444. pull = slpm_pull - 1;
  1445. if (slpm_output)
  1446. output = slpm_output - 1;
  1447. if (slpm_val)
  1448. val = slpm_val - 1;
  1449. dev_dbg(nmk_chip->chip.dev,
  1450. "pin %d: sleep pull %s, dir %s, val %s\n",
  1451. pin,
  1452. slpm_pull ? pullnames[pull] : "same",
  1453. slpm_output ? (output ? "output" : "input")
  1454. : "same",
  1455. slpm_val ? (val ? "high" : "low") : "same");
  1456. }
  1457. dev_dbg(nmk_chip->chip.dev,
  1458. "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
  1459. pin, cfg, pullnames[pull], slpmnames[slpm],
  1460. output ? "output " : "input",
  1461. output ? (val ? "high" : "low") : "",
  1462. lowemi ? "on" : "off");
  1463. clk_enable(nmk_chip->clk);
  1464. bit = pin % NMK_GPIO_PER_CHIP;
  1465. if (gpiomode)
  1466. /* No glitch when going to GPIO mode */
  1467. __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
  1468. if (output)
  1469. __nmk_gpio_make_output(nmk_chip, bit, val);
  1470. else {
  1471. __nmk_gpio_make_input(nmk_chip, bit);
  1472. __nmk_gpio_set_pull(nmk_chip, bit, pull);
  1473. }
  1474. /* TODO: isn't this only applicable on output pins? */
  1475. __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
  1476. __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
  1477. clk_disable(nmk_chip->clk);
  1478. } /* for each config */
  1479. return 0;
  1480. }
  1481. static const struct pinconf_ops nmk_pinconf_ops = {
  1482. .pin_config_get = nmk_pin_config_get,
  1483. .pin_config_set = nmk_pin_config_set,
  1484. };
  1485. static struct pinctrl_desc nmk_pinctrl_desc = {
  1486. .name = "pinctrl-nomadik",
  1487. .pctlops = &nmk_pinctrl_ops,
  1488. .pmxops = &nmk_pinmux_ops,
  1489. .confops = &nmk_pinconf_ops,
  1490. .owner = THIS_MODULE,
  1491. };
  1492. static const struct of_device_id nmk_pinctrl_match[] = {
  1493. {
  1494. .compatible = "stericsson,stn8815-pinctrl",
  1495. .data = (void *)PINCTRL_NMK_STN8815,
  1496. },
  1497. {
  1498. .compatible = "stericsson,db8500-pinctrl",
  1499. .data = (void *)PINCTRL_NMK_DB8500,
  1500. },
  1501. {
  1502. .compatible = "stericsson,db8540-pinctrl",
  1503. .data = (void *)PINCTRL_NMK_DB8540,
  1504. },
  1505. {},
  1506. };
  1507. static int nmk_pinctrl_suspend(struct platform_device *pdev, pm_message_t state)
  1508. {
  1509. struct nmk_pinctrl *npct;
  1510. npct = platform_get_drvdata(pdev);
  1511. if (!npct)
  1512. return -EINVAL;
  1513. return pinctrl_force_sleep(npct->pctl);
  1514. }
  1515. static int nmk_pinctrl_resume(struct platform_device *pdev)
  1516. {
  1517. struct nmk_pinctrl *npct;
  1518. npct = platform_get_drvdata(pdev);
  1519. if (!npct)
  1520. return -EINVAL;
  1521. return pinctrl_force_default(npct->pctl);
  1522. }
  1523. static int nmk_pinctrl_probe(struct platform_device *pdev)
  1524. {
  1525. const struct platform_device_id *platid = platform_get_device_id(pdev);
  1526. struct device_node *np = pdev->dev.of_node;
  1527. struct device_node *prcm_np;
  1528. struct nmk_pinctrl *npct;
  1529. struct resource *res;
  1530. unsigned int version = 0;
  1531. int i;
  1532. npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
  1533. if (!npct)
  1534. return -ENOMEM;
  1535. if (platid)
  1536. version = platid->driver_data;
  1537. else if (np) {
  1538. const struct of_device_id *match;
  1539. match = of_match_device(nmk_pinctrl_match, &pdev->dev);
  1540. if (!match)
  1541. return -ENODEV;
  1542. version = (unsigned int) match->data;
  1543. }
  1544. /* Poke in other ASIC variants here */
  1545. if (version == PINCTRL_NMK_STN8815)
  1546. nmk_pinctrl_stn8815_init(&npct->soc);
  1547. if (version == PINCTRL_NMK_DB8500)
  1548. nmk_pinctrl_db8500_init(&npct->soc);
  1549. if (version == PINCTRL_NMK_DB8540)
  1550. nmk_pinctrl_db8540_init(&npct->soc);
  1551. if (np) {
  1552. prcm_np = of_parse_phandle(np, "prcm", 0);
  1553. if (prcm_np)
  1554. npct->prcm_base = of_iomap(prcm_np, 0);
  1555. }
  1556. /* Allow platform passed information to over-write DT. */
  1557. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1558. if (res)
  1559. npct->prcm_base = devm_ioremap(&pdev->dev, res->start,
  1560. resource_size(res));
  1561. if (!npct->prcm_base) {
  1562. if (version == PINCTRL_NMK_STN8815) {
  1563. dev_info(&pdev->dev,
  1564. "No PRCM base, "
  1565. "assuming no ALT-Cx control is available\n");
  1566. } else {
  1567. dev_err(&pdev->dev, "missing PRCM base address\n");
  1568. return -EINVAL;
  1569. }
  1570. }
  1571. /*
  1572. * We need all the GPIO drivers to probe FIRST, or we will not be able
  1573. * to obtain references to the struct gpio_chip * for them, and we
  1574. * need this to proceed.
  1575. */
  1576. for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
  1577. if (!nmk_gpio_chips[npct->soc->gpio_ranges[i].id]) {
  1578. dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
  1579. return -EPROBE_DEFER;
  1580. }
  1581. npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[npct->soc->gpio_ranges[i].id]->chip;
  1582. }
  1583. nmk_pinctrl_desc.pins = npct->soc->pins;
  1584. nmk_pinctrl_desc.npins = npct->soc->npins;
  1585. npct->dev = &pdev->dev;
  1586. npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
  1587. if (!npct->pctl) {
  1588. dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
  1589. return -EINVAL;
  1590. }
  1591. /* We will handle a range of GPIO pins */
  1592. for (i = 0; i < npct->soc->gpio_num_ranges; i++)
  1593. pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
  1594. platform_set_drvdata(pdev, npct);
  1595. dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
  1596. return 0;
  1597. }
  1598. static const struct of_device_id nmk_gpio_match[] = {
  1599. { .compatible = "st,nomadik-gpio", },
  1600. {}
  1601. };
  1602. static struct platform_driver nmk_gpio_driver = {
  1603. .driver = {
  1604. .owner = THIS_MODULE,
  1605. .name = "gpio",
  1606. .of_match_table = nmk_gpio_match,
  1607. },
  1608. .probe = nmk_gpio_probe,
  1609. };
  1610. static const struct platform_device_id nmk_pinctrl_id[] = {
  1611. { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
  1612. { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
  1613. { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
  1614. { }
  1615. };
  1616. static struct platform_driver nmk_pinctrl_driver = {
  1617. .driver = {
  1618. .owner = THIS_MODULE,
  1619. .name = "pinctrl-nomadik",
  1620. .of_match_table = nmk_pinctrl_match,
  1621. },
  1622. .probe = nmk_pinctrl_probe,
  1623. .id_table = nmk_pinctrl_id,
  1624. #ifdef CONFIG_PM
  1625. .suspend = nmk_pinctrl_suspend,
  1626. .resume = nmk_pinctrl_resume,
  1627. #endif
  1628. };
  1629. static int __init nmk_gpio_init(void)
  1630. {
  1631. int ret;
  1632. ret = platform_driver_register(&nmk_gpio_driver);
  1633. if (ret)
  1634. return ret;
  1635. return platform_driver_register(&nmk_pinctrl_driver);
  1636. }
  1637. core_initcall(nmk_gpio_init);
  1638. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  1639. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  1640. MODULE_LICENSE("GPL");