pinctrl-nomadik.c 54 KB

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