pinctrl-nomadik.c 53 KB

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