pinctrl-nomadik.c 55 KB

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