pinctrl-nomadik.c 54 KB

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