pinctrl-nomadik.c 54 KB

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