pinctrl-nomadik.c 52 KB

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