pinctrl-nomadik.c 52 KB

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