pinctrl-nomadik.c 54 KB

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