pinctrl-nomadik.c 51 KB

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