pinctrl-nomadik.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Pinmux & pinconf driver for the IP block found in the Nomadik SoC. This
  4. * depends on gpio-nomadik and some handling is intertwined; see nmk_gpio_chips
  5. * which is used by this driver to access the GPIO banks array.
  6. *
  7. * Copyright (C) 2008,2009 STMicroelectronics
  8. * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it>
  9. * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com>
  10. * Copyright (C) 2011-2013 Linus Walleij <linus.walleij@linaro.org>
  11. */
  12. #include <linux/bitops.h>
  13. #include <linux/cleanup.h>
  14. #include <linux/clk.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/gpio/driver.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/property.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/string_choices.h>
  31. #include <linux/types.h>
  32. /* Since we request GPIOs from ourself */
  33. #include <linux/pinctrl/consumer.h>
  34. #include <linux/pinctrl/machine.h>
  35. #include <linux/pinctrl/pinconf.h>
  36. #include <linux/pinctrl/pinctrl.h>
  37. #include <linux/pinctrl/pinmux.h>
  38. #include "../core.h"
  39. #include "../pinctrl-utils.h"
  40. #include <linux/gpio/gpio-nomadik.h>
  41. /*
  42. * pin configurations are represented by 32-bit integers:
  43. *
  44. * bit 0.. 8 - Pin Number (512 Pins Maximum)
  45. * bit 9..10 - Alternate Function Selection
  46. * bit 11..12 - Pull up/down state
  47. * bit 13 - Sleep mode behaviour
  48. * bit 14 - Direction
  49. * bit 15 - Value (if output)
  50. * bit 16..18 - SLPM pull up/down state
  51. * bit 19..20 - SLPM direction
  52. * bit 21..22 - SLPM Value (if output)
  53. * bit 23..25 - PDIS value (if input)
  54. * bit 26 - Gpio mode
  55. * bit 27 - Sleep mode
  56. *
  57. * to facilitate the definition, the following macros are provided
  58. *
  59. * PIN_CFG_DEFAULT - default config (0):
  60. * pull up/down = disabled
  61. * sleep mode = input/wakeup
  62. * direction = input
  63. * value = low
  64. * SLPM direction = same as normal
  65. * SLPM pull = same as normal
  66. * SLPM value = same as normal
  67. *
  68. * PIN_CFG - default config with alternate function
  69. */
  70. #define PIN_NUM_MASK 0x1ff
  71. #define PIN_NUM(x) ((x) & PIN_NUM_MASK)
  72. #define PIN_ALT_SHIFT 9
  73. #define PIN_ALT_MASK (0x3 << PIN_ALT_SHIFT)
  74. #define PIN_ALT(x) (((x) & PIN_ALT_MASK) >> PIN_ALT_SHIFT)
  75. #define PIN_GPIO (NMK_GPIO_ALT_GPIO << PIN_ALT_SHIFT)
  76. #define PIN_ALT_A (NMK_GPIO_ALT_A << PIN_ALT_SHIFT)
  77. #define PIN_ALT_B (NMK_GPIO_ALT_B << PIN_ALT_SHIFT)
  78. #define PIN_ALT_C (NMK_GPIO_ALT_C << PIN_ALT_SHIFT)
  79. #define PIN_PULL_SHIFT 11
  80. #define PIN_PULL_MASK (0x3 << PIN_PULL_SHIFT)
  81. #define PIN_PULL(x) (((x) & PIN_PULL_MASK) >> PIN_PULL_SHIFT)
  82. #define PIN_PULL_NONE (NMK_GPIO_PULL_NONE << PIN_PULL_SHIFT)
  83. #define PIN_PULL_UP (NMK_GPIO_PULL_UP << PIN_PULL_SHIFT)
  84. #define PIN_PULL_DOWN (NMK_GPIO_PULL_DOWN << PIN_PULL_SHIFT)
  85. #define PIN_SLPM_SHIFT 13
  86. #define PIN_SLPM_MASK (0x1 << PIN_SLPM_SHIFT)
  87. #define PIN_SLPM(x) (((x) & PIN_SLPM_MASK) >> PIN_SLPM_SHIFT)
  88. #define PIN_SLPM_MAKE_INPUT (NMK_GPIO_SLPM_INPUT << PIN_SLPM_SHIFT)
  89. #define PIN_SLPM_NOCHANGE (NMK_GPIO_SLPM_NOCHANGE << PIN_SLPM_SHIFT)
  90. /* These two replace the above in DB8500v2+ */
  91. #define PIN_SLPM_WAKEUP_ENABLE (NMK_GPIO_SLPM_WAKEUP_ENABLE << PIN_SLPM_SHIFT)
  92. #define PIN_SLPM_WAKEUP_DISABLE (NMK_GPIO_SLPM_WAKEUP_DISABLE << PIN_SLPM_SHIFT)
  93. #define PIN_SLPM_USE_MUX_SETTINGS_IN_SLEEP PIN_SLPM_WAKEUP_DISABLE
  94. #define PIN_SLPM_GPIO PIN_SLPM_WAKEUP_ENABLE /* In SLPM, pin is a gpio */
  95. #define PIN_SLPM_ALTFUNC PIN_SLPM_WAKEUP_DISABLE /* In SLPM, pin is altfunc */
  96. #define PIN_DIR_SHIFT 14
  97. #define PIN_DIR_MASK (0x1 << PIN_DIR_SHIFT)
  98. #define PIN_DIR(x) (((x) & PIN_DIR_MASK) >> PIN_DIR_SHIFT)
  99. #define PIN_DIR_INPUT (0 << PIN_DIR_SHIFT)
  100. #define PIN_DIR_OUTPUT (1 << PIN_DIR_SHIFT)
  101. #define PIN_VAL_SHIFT 15
  102. #define PIN_VAL_MASK (0x1 << PIN_VAL_SHIFT)
  103. #define PIN_VAL(x) (((x) & PIN_VAL_MASK) >> PIN_VAL_SHIFT)
  104. #define PIN_VAL_LOW (0 << PIN_VAL_SHIFT)
  105. #define PIN_VAL_HIGH (1 << PIN_VAL_SHIFT)
  106. #define PIN_SLPM_PULL_SHIFT 16
  107. #define PIN_SLPM_PULL_MASK (0x7 << PIN_SLPM_PULL_SHIFT)
  108. #define PIN_SLPM_PULL(x) \
  109. (((x) & PIN_SLPM_PULL_MASK) >> PIN_SLPM_PULL_SHIFT)
  110. #define PIN_SLPM_PULL_NONE \
  111. ((1 + NMK_GPIO_PULL_NONE) << PIN_SLPM_PULL_SHIFT)
  112. #define PIN_SLPM_PULL_UP \
  113. ((1 + NMK_GPIO_PULL_UP) << PIN_SLPM_PULL_SHIFT)
  114. #define PIN_SLPM_PULL_DOWN \
  115. ((1 + NMK_GPIO_PULL_DOWN) << PIN_SLPM_PULL_SHIFT)
  116. #define PIN_SLPM_DIR_SHIFT 19
  117. #define PIN_SLPM_DIR_MASK (0x3 << PIN_SLPM_DIR_SHIFT)
  118. #define PIN_SLPM_DIR(x) \
  119. (((x) & PIN_SLPM_DIR_MASK) >> PIN_SLPM_DIR_SHIFT)
  120. #define PIN_SLPM_DIR_INPUT ((1 + 0) << PIN_SLPM_DIR_SHIFT)
  121. #define PIN_SLPM_DIR_OUTPUT ((1 + 1) << PIN_SLPM_DIR_SHIFT)
  122. #define PIN_SLPM_VAL_SHIFT 21
  123. #define PIN_SLPM_VAL_MASK (0x3 << PIN_SLPM_VAL_SHIFT)
  124. #define PIN_SLPM_VAL(x) \
  125. (((x) & PIN_SLPM_VAL_MASK) >> PIN_SLPM_VAL_SHIFT)
  126. #define PIN_SLPM_VAL_LOW ((1 + 0) << PIN_SLPM_VAL_SHIFT)
  127. #define PIN_SLPM_VAL_HIGH ((1 + 1) << PIN_SLPM_VAL_SHIFT)
  128. #define PIN_SLPM_PDIS_SHIFT 23
  129. #define PIN_SLPM_PDIS_MASK (0x3 << PIN_SLPM_PDIS_SHIFT)
  130. #define PIN_SLPM_PDIS(x) \
  131. (((x) & PIN_SLPM_PDIS_MASK) >> PIN_SLPM_PDIS_SHIFT)
  132. #define PIN_SLPM_PDIS_NO_CHANGE (0 << PIN_SLPM_PDIS_SHIFT)
  133. #define PIN_SLPM_PDIS_DISABLED (1 << PIN_SLPM_PDIS_SHIFT)
  134. #define PIN_SLPM_PDIS_ENABLED (2 << PIN_SLPM_PDIS_SHIFT)
  135. #define PIN_LOWEMI_SHIFT 25
  136. #define PIN_LOWEMI_MASK (0x1 << PIN_LOWEMI_SHIFT)
  137. #define PIN_LOWEMI(x) (((x) & PIN_LOWEMI_MASK) >> PIN_LOWEMI_SHIFT)
  138. #define PIN_LOWEMI_DISABLED (0 << PIN_LOWEMI_SHIFT)
  139. #define PIN_LOWEMI_ENABLED (1 << PIN_LOWEMI_SHIFT)
  140. #define PIN_GPIOMODE_SHIFT 26
  141. #define PIN_GPIOMODE_MASK (0x1 << PIN_GPIOMODE_SHIFT)
  142. #define PIN_GPIOMODE(x) (((x) & PIN_GPIOMODE_MASK) >> PIN_GPIOMODE_SHIFT)
  143. #define PIN_GPIOMODE_DISABLED (0 << PIN_GPIOMODE_SHIFT)
  144. #define PIN_GPIOMODE_ENABLED (1 << PIN_GPIOMODE_SHIFT)
  145. #define PIN_SLEEPMODE_SHIFT 27
  146. #define PIN_SLEEPMODE_MASK (0x1 << PIN_SLEEPMODE_SHIFT)
  147. #define PIN_SLEEPMODE(x) (((x) & PIN_SLEEPMODE_MASK) >> PIN_SLEEPMODE_SHIFT)
  148. #define PIN_SLEEPMODE_DISABLED (0 << PIN_SLEEPMODE_SHIFT)
  149. #define PIN_SLEEPMODE_ENABLED (1 << PIN_SLEEPMODE_SHIFT)
  150. /* Shortcuts. Use these instead of separate DIR, PULL, and VAL. */
  151. #define PIN_INPUT_PULLDOWN (PIN_DIR_INPUT | PIN_PULL_DOWN)
  152. #define PIN_INPUT_PULLUP (PIN_DIR_INPUT | PIN_PULL_UP)
  153. #define PIN_INPUT_NOPULL (PIN_DIR_INPUT | PIN_PULL_NONE)
  154. #define PIN_OUTPUT_LOW (PIN_DIR_OUTPUT | PIN_VAL_LOW)
  155. #define PIN_OUTPUT_HIGH (PIN_DIR_OUTPUT | PIN_VAL_HIGH)
  156. #define PIN_SLPM_INPUT_PULLDOWN (PIN_SLPM_DIR_INPUT | PIN_SLPM_PULL_DOWN)
  157. #define PIN_SLPM_INPUT_PULLUP (PIN_SLPM_DIR_INPUT | PIN_SLPM_PULL_UP)
  158. #define PIN_SLPM_INPUT_NOPULL (PIN_SLPM_DIR_INPUT | PIN_SLPM_PULL_NONE)
  159. #define PIN_SLPM_OUTPUT_LOW (PIN_SLPM_DIR_OUTPUT | PIN_SLPM_VAL_LOW)
  160. #define PIN_SLPM_OUTPUT_HIGH (PIN_SLPM_DIR_OUTPUT | PIN_SLPM_VAL_HIGH)
  161. #define PIN_CFG_DEFAULT (0)
  162. #define PIN_CFG(num, alt) \
  163. (PIN_CFG_DEFAULT |\
  164. (PIN_NUM(num) | PIN_##alt))
  165. #define PIN_CFG_INPUT(num, alt, pull) \
  166. (PIN_CFG_DEFAULT |\
  167. (PIN_NUM(num) | PIN_##alt | PIN_INPUT_##pull))
  168. #define PIN_CFG_OUTPUT(num, alt, val) \
  169. (PIN_CFG_DEFAULT |\
  170. (PIN_NUM(num) | PIN_##alt | PIN_OUTPUT_##val))
  171. /**
  172. * struct nmk_pinctrl - state container for the Nomadik pin controller
  173. * @dev: containing device pointer
  174. * @pctl: corresponding pin controller device
  175. * @soc: SoC data for this specific chip
  176. * @prcm_base: PRCM register range virtual base
  177. */
  178. struct nmk_pinctrl {
  179. struct device *dev;
  180. struct pinctrl_dev *pctl;
  181. const struct nmk_pinctrl_soc_data *soc;
  182. void __iomem *prcm_base;
  183. };
  184. /* See nmk_gpio_populate_chip() that fills this array. */
  185. struct nmk_gpio_chip *nmk_gpio_chips[NMK_MAX_BANKS];
  186. DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
  187. static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
  188. unsigned int offset, int gpio_mode)
  189. {
  190. u32 afunc, bfunc;
  191. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~BIT(offset);
  192. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~BIT(offset);
  193. if (gpio_mode & NMK_GPIO_ALT_A)
  194. afunc |= BIT(offset);
  195. if (gpio_mode & NMK_GPIO_ALT_B)
  196. bfunc |= BIT(offset);
  197. writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
  198. writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
  199. }
  200. static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
  201. unsigned int offset, enum nmk_gpio_pull pull)
  202. {
  203. u32 pdis;
  204. pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
  205. if (pull == NMK_GPIO_PULL_NONE) {
  206. pdis |= BIT(offset);
  207. nmk_chip->pull_up &= ~BIT(offset);
  208. } else {
  209. pdis &= ~BIT(offset);
  210. }
  211. writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
  212. if (pull == NMK_GPIO_PULL_UP) {
  213. nmk_chip->pull_up |= BIT(offset);
  214. writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DATS);
  215. } else if (pull == NMK_GPIO_PULL_DOWN) {
  216. nmk_chip->pull_up &= ~BIT(offset);
  217. writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DATC);
  218. }
  219. }
  220. static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
  221. unsigned int offset, bool lowemi)
  222. {
  223. bool enabled = nmk_chip->lowemi & BIT(offset);
  224. if (lowemi == enabled)
  225. return;
  226. if (lowemi)
  227. nmk_chip->lowemi |= BIT(offset);
  228. else
  229. nmk_chip->lowemi &= ~BIT(offset);
  230. writel_relaxed(nmk_chip->lowemi,
  231. nmk_chip->addr + NMK_GPIO_LOWEMI);
  232. }
  233. static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
  234. unsigned int offset)
  235. {
  236. writel(BIT(offset), nmk_chip->addr + NMK_GPIO_DIRC);
  237. }
  238. static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
  239. unsigned int offset, int gpio_mode,
  240. bool glitch)
  241. {
  242. u32 rwimsc = nmk_chip->rwimsc;
  243. u32 fwimsc = nmk_chip->fwimsc;
  244. if (glitch && nmk_chip->set_ioforce) {
  245. u32 bit = BIT(offset);
  246. /* Prevent spurious wakeups */
  247. writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
  248. writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
  249. nmk_chip->set_ioforce(true);
  250. }
  251. __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
  252. if (glitch && nmk_chip->set_ioforce) {
  253. nmk_chip->set_ioforce(false);
  254. writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
  255. writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
  256. }
  257. }
  258. static void
  259. nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned int offset)
  260. {
  261. u32 falling = nmk_chip->fimsc & BIT(offset);
  262. u32 rising = nmk_chip->rimsc & BIT(offset);
  263. int gpio = nmk_chip->chip.base + offset;
  264. int irq = irq_find_mapping(nmk_chip->chip.irq.domain, offset);
  265. struct irq_data *d = irq_get_irq_data(irq);
  266. if (!rising && !falling)
  267. return;
  268. if (!d || !irqd_irq_disabled(d))
  269. return;
  270. if (rising) {
  271. nmk_chip->rimsc &= ~BIT(offset);
  272. writel_relaxed(nmk_chip->rimsc,
  273. nmk_chip->addr + NMK_GPIO_RIMSC);
  274. }
  275. if (falling) {
  276. nmk_chip->fimsc &= ~BIT(offset);
  277. writel_relaxed(nmk_chip->fimsc,
  278. nmk_chip->addr + NMK_GPIO_FIMSC);
  279. }
  280. dev_dbg(nmk_chip->chip.parent, "%d: clearing interrupt mask\n", gpio);
  281. }
  282. static void nmk_write_masked(void __iomem *reg, u32 mask, u32 value)
  283. {
  284. u32 val;
  285. val = readl(reg);
  286. val = ((val & ~mask) | (value & mask));
  287. writel(val, reg);
  288. }
  289. static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
  290. unsigned int offset, unsigned int alt_num)
  291. {
  292. int i;
  293. u16 reg;
  294. u8 bit;
  295. u8 alt_index;
  296. const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
  297. const u16 *gpiocr_regs;
  298. if (!npct->prcm_base)
  299. return;
  300. if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
  301. dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
  302. alt_num);
  303. return;
  304. }
  305. for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
  306. if (npct->soc->altcx_pins[i].pin == offset)
  307. break;
  308. }
  309. if (i == npct->soc->npins_altcx) {
  310. dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
  311. offset);
  312. return;
  313. }
  314. pin_desc = npct->soc->altcx_pins + i;
  315. gpiocr_regs = npct->soc->prcm_gpiocr_registers;
  316. /*
  317. * If alt_num is NULL, just clear current ALTCx selection
  318. * to make sure we come back to a pure ALTC selection
  319. */
  320. if (!alt_num) {
  321. for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
  322. if (pin_desc->altcx[i].used) {
  323. reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
  324. bit = pin_desc->altcx[i].control_bit;
  325. if (readl(npct->prcm_base + reg) & BIT(bit)) {
  326. nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
  327. dev_dbg(npct->dev,
  328. "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
  329. offset, i + 1);
  330. }
  331. }
  332. }
  333. return;
  334. }
  335. alt_index = alt_num - 1;
  336. if (!pin_desc->altcx[alt_index].used) {
  337. dev_warn(npct->dev,
  338. "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
  339. offset, alt_num);
  340. return;
  341. }
  342. /*
  343. * Check if any other ALTCx functions are activated on this pin
  344. * and disable it first.
  345. */
  346. for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
  347. if (i == alt_index)
  348. continue;
  349. if (pin_desc->altcx[i].used) {
  350. reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
  351. bit = pin_desc->altcx[i].control_bit;
  352. if (readl(npct->prcm_base + reg) & BIT(bit)) {
  353. nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
  354. dev_dbg(npct->dev,
  355. "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
  356. offset, i + 1);
  357. }
  358. }
  359. }
  360. reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
  361. bit = pin_desc->altcx[alt_index].control_bit;
  362. dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
  363. offset, alt_index + 1);
  364. nmk_write_masked(npct->prcm_base + reg, BIT(bit), BIT(bit));
  365. }
  366. /*
  367. * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
  368. * - Save SLPM registers
  369. * - Set SLPM=0 for the IOs you want to switch and others to 1
  370. * - Configure the GPIO registers for the IOs that are being switched
  371. * - Set IOFORCE=1
  372. * - Modify the AFLSA/B registers for the IOs that are being switched
  373. * - Set IOFORCE=0
  374. * - Restore SLPM registers
  375. * - Any spurious wake up event during switch sequence to be ignored and
  376. * cleared
  377. */
  378. static int nmk_gpio_glitch_slpm_init(unsigned int *slpm)
  379. {
  380. int i, j, ret;
  381. for (i = 0; i < NMK_MAX_BANKS; i++) {
  382. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  383. unsigned int temp = slpm[i];
  384. if (!chip)
  385. break;
  386. ret = clk_enable(chip->clk);
  387. if (ret) {
  388. for (j = 0; j < i; j++) {
  389. chip = nmk_gpio_chips[j];
  390. clk_disable(chip->clk);
  391. }
  392. return ret;
  393. }
  394. slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
  395. writel(temp, chip->addr + NMK_GPIO_SLPC);
  396. }
  397. return 0;
  398. }
  399. static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
  400. {
  401. int i;
  402. for (i = 0; i < NMK_MAX_BANKS; i++) {
  403. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  404. if (!chip)
  405. break;
  406. writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
  407. clk_disable(chip->clk);
  408. }
  409. }
  410. /* Only called by gpio-nomadik but requires knowledge of struct nmk_pinctrl. */
  411. int __maybe_unused nmk_prcm_gpiocr_get_mode(struct pinctrl_dev *pctldev, int gpio)
  412. {
  413. int i;
  414. u16 reg;
  415. u8 bit;
  416. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  417. const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
  418. const u16 *gpiocr_regs;
  419. if (!npct->prcm_base)
  420. return NMK_GPIO_ALT_C;
  421. for (i = 0; i < npct->soc->npins_altcx; i++) {
  422. if (npct->soc->altcx_pins[i].pin == gpio)
  423. break;
  424. }
  425. if (i == npct->soc->npins_altcx)
  426. return NMK_GPIO_ALT_C;
  427. pin_desc = npct->soc->altcx_pins + i;
  428. gpiocr_regs = npct->soc->prcm_gpiocr_registers;
  429. for (i = 0; i < PRCM_IDX_GPIOCR_ALTC_MAX; i++) {
  430. if (pin_desc->altcx[i].used) {
  431. reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
  432. bit = pin_desc->altcx[i].control_bit;
  433. if (readl(npct->prcm_base + reg) & BIT(bit))
  434. return NMK_GPIO_ALT_C + i + 1;
  435. }
  436. }
  437. return NMK_GPIO_ALT_C;
  438. }
  439. static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
  440. {
  441. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  442. return npct->soc->ngroups;
  443. }
  444. static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
  445. unsigned int selector)
  446. {
  447. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  448. return npct->soc->groups[selector].grp.name;
  449. }
  450. static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned int selector,
  451. const unsigned int **pins,
  452. unsigned int *num_pins)
  453. {
  454. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  455. *pins = npct->soc->groups[selector].grp.pins;
  456. *num_pins = npct->soc->groups[selector].grp.npins;
  457. return 0;
  458. }
  459. /* This makes the mapping from pin number to a GPIO chip. We also return the pin
  460. * offset in the GPIO chip for convenience (and to avoid a second loop).
  461. */
  462. static struct nmk_gpio_chip *find_nmk_gpio_from_pin(unsigned int pin,
  463. unsigned int *offset)
  464. {
  465. int i, j = 0;
  466. struct nmk_gpio_chip *nmk_gpio;
  467. /* We assume that pins are allocated in bank order. */
  468. for (i = 0; i < NMK_MAX_BANKS; i++) {
  469. nmk_gpio = nmk_gpio_chips[i];
  470. if (!nmk_gpio)
  471. continue;
  472. if (pin >= j && pin < j + nmk_gpio->chip.ngpio) {
  473. if (offset)
  474. *offset = pin - j;
  475. return nmk_gpio;
  476. }
  477. j += nmk_gpio->chip.ngpio;
  478. }
  479. return NULL;
  480. }
  481. static struct gpio_chip *find_gc_from_pin(unsigned int pin)
  482. {
  483. struct nmk_gpio_chip *nmk_gpio = find_nmk_gpio_from_pin(pin, NULL);
  484. if (nmk_gpio)
  485. return &nmk_gpio->chip;
  486. return NULL;
  487. }
  488. static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  489. unsigned int offset)
  490. {
  491. struct gpio_chip *chip = find_gc_from_pin(offset);
  492. if (!chip) {
  493. seq_printf(s, "invalid pin offset");
  494. return;
  495. }
  496. nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base);
  497. }
  498. static int nmk_dt_add_map_mux(struct pinctrl_map **map, unsigned int *reserved_maps,
  499. unsigned int *num_maps, const char *group,
  500. const char *function)
  501. {
  502. if (*num_maps == *reserved_maps)
  503. return -ENOSPC;
  504. (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
  505. (*map)[*num_maps].data.mux.group = group;
  506. (*map)[*num_maps].data.mux.function = function;
  507. (*num_maps)++;
  508. return 0;
  509. }
  510. static int nmk_dt_add_map_configs(struct pinctrl_map **map,
  511. unsigned int *reserved_maps,
  512. unsigned int *num_maps, const char *group,
  513. unsigned long *configs, unsigned int num_configs)
  514. {
  515. unsigned long *dup_configs;
  516. if (*num_maps == *reserved_maps)
  517. return -ENOSPC;
  518. dup_configs = kmemdup_array(configs, num_configs, sizeof(*dup_configs), GFP_KERNEL);
  519. if (!dup_configs)
  520. return -ENOMEM;
  521. (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
  522. (*map)[*num_maps].data.configs.group_or_pin = group;
  523. (*map)[*num_maps].data.configs.configs = dup_configs;
  524. (*map)[*num_maps].data.configs.num_configs = num_configs;
  525. (*num_maps)++;
  526. return 0;
  527. }
  528. #define NMK_CONFIG_PIN(x, y) { .property = x, .config = y, }
  529. #define NMK_CONFIG_PIN_ARRAY(x, y) { .property = x, .choice = y, \
  530. .size = ARRAY_SIZE(y), }
  531. static const unsigned long nmk_pin_input_modes[] = {
  532. PIN_INPUT_NOPULL,
  533. PIN_INPUT_PULLUP,
  534. PIN_INPUT_PULLDOWN,
  535. };
  536. static const unsigned long nmk_pin_output_modes[] = {
  537. PIN_OUTPUT_LOW,
  538. PIN_OUTPUT_HIGH,
  539. PIN_DIR_OUTPUT,
  540. };
  541. static const unsigned long nmk_pin_sleep_modes[] = {
  542. PIN_SLEEPMODE_DISABLED,
  543. PIN_SLEEPMODE_ENABLED,
  544. };
  545. static const unsigned long nmk_pin_sleep_input_modes[] = {
  546. PIN_SLPM_INPUT_NOPULL,
  547. PIN_SLPM_INPUT_PULLUP,
  548. PIN_SLPM_INPUT_PULLDOWN,
  549. PIN_SLPM_DIR_INPUT,
  550. };
  551. static const unsigned long nmk_pin_sleep_output_modes[] = {
  552. PIN_SLPM_OUTPUT_LOW,
  553. PIN_SLPM_OUTPUT_HIGH,
  554. PIN_SLPM_DIR_OUTPUT,
  555. };
  556. static const unsigned long nmk_pin_sleep_wakeup_modes[] = {
  557. PIN_SLPM_WAKEUP_DISABLE,
  558. PIN_SLPM_WAKEUP_ENABLE,
  559. };
  560. static const unsigned long nmk_pin_gpio_modes[] = {
  561. PIN_GPIOMODE_DISABLED,
  562. PIN_GPIOMODE_ENABLED,
  563. };
  564. static const unsigned long nmk_pin_sleep_pdis_modes[] = {
  565. PIN_SLPM_PDIS_DISABLED,
  566. PIN_SLPM_PDIS_ENABLED,
  567. };
  568. struct nmk_cfg_param {
  569. const char *property;
  570. unsigned long config;
  571. const unsigned long *choice;
  572. int size;
  573. };
  574. static const struct nmk_cfg_param nmk_cfg_params[] = {
  575. NMK_CONFIG_PIN_ARRAY("ste,input", nmk_pin_input_modes),
  576. NMK_CONFIG_PIN_ARRAY("ste,output", nmk_pin_output_modes),
  577. NMK_CONFIG_PIN_ARRAY("ste,sleep", nmk_pin_sleep_modes),
  578. NMK_CONFIG_PIN_ARRAY("ste,sleep-input", nmk_pin_sleep_input_modes),
  579. NMK_CONFIG_PIN_ARRAY("ste,sleep-output", nmk_pin_sleep_output_modes),
  580. NMK_CONFIG_PIN_ARRAY("ste,sleep-wakeup", nmk_pin_sleep_wakeup_modes),
  581. NMK_CONFIG_PIN_ARRAY("ste,gpio", nmk_pin_gpio_modes),
  582. NMK_CONFIG_PIN_ARRAY("ste,sleep-pull-disable", nmk_pin_sleep_pdis_modes),
  583. };
  584. static int nmk_dt_pin_config(int index, int val, unsigned long *config)
  585. {
  586. if (!nmk_cfg_params[index].choice) {
  587. *config = nmk_cfg_params[index].config;
  588. } else {
  589. /* test if out of range */
  590. if (val < nmk_cfg_params[index].size) {
  591. *config = nmk_cfg_params[index].config |
  592. nmk_cfg_params[index].choice[val];
  593. }
  594. }
  595. return 0;
  596. }
  597. static const char *nmk_find_pin_name(struct pinctrl_dev *pctldev, const char *pin_name)
  598. {
  599. int i, pin_number;
  600. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  601. if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
  602. for (i = 0; i < npct->soc->npins; i++)
  603. if (npct->soc->pins[i].number == pin_number)
  604. return npct->soc->pins[i].name;
  605. return NULL;
  606. }
  607. static bool nmk_pinctrl_dt_get_config(struct device_node *np,
  608. unsigned long *configs)
  609. {
  610. bool has_config = 0;
  611. unsigned long cfg = 0;
  612. int i, val, ret;
  613. for (i = 0; i < ARRAY_SIZE(nmk_cfg_params); i++) {
  614. ret = of_property_read_u32(np, nmk_cfg_params[i].property, &val);
  615. if (ret != -EINVAL) {
  616. if (nmk_dt_pin_config(i, val, &cfg) == 0) {
  617. *configs |= cfg;
  618. has_config = 1;
  619. }
  620. }
  621. }
  622. return has_config;
  623. }
  624. static int nmk_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  625. struct device_node *np,
  626. struct pinctrl_map **map,
  627. unsigned int *reserved_maps,
  628. unsigned int *num_maps)
  629. {
  630. int ret;
  631. const char *function = NULL;
  632. unsigned long configs = 0;
  633. bool has_config = 0;
  634. struct property *prop;
  635. struct device_node *np_config;
  636. ret = of_property_read_string(np, "function", &function);
  637. if (ret >= 0) {
  638. const char *group;
  639. ret = of_property_count_strings(np, "groups");
  640. if (ret < 0)
  641. goto exit;
  642. ret = pinctrl_utils_reserve_map(pctldev, map,
  643. reserved_maps,
  644. num_maps, ret);
  645. if (ret < 0)
  646. goto exit;
  647. of_property_for_each_string(np, "groups", prop, group) {
  648. ret = nmk_dt_add_map_mux(map, reserved_maps, num_maps,
  649. group, function);
  650. if (ret < 0)
  651. goto exit;
  652. }
  653. }
  654. has_config = nmk_pinctrl_dt_get_config(np, &configs);
  655. np_config = of_parse_phandle(np, "ste,config", 0);
  656. if (np_config) {
  657. has_config |= nmk_pinctrl_dt_get_config(np_config, &configs);
  658. of_node_put(np_config);
  659. }
  660. if (has_config) {
  661. const char *gpio_name;
  662. const char *pin;
  663. ret = of_property_count_strings(np, "pins");
  664. if (ret < 0)
  665. goto exit;
  666. ret = pinctrl_utils_reserve_map(pctldev, map,
  667. reserved_maps,
  668. num_maps, ret);
  669. if (ret < 0)
  670. goto exit;
  671. of_property_for_each_string(np, "pins", prop, pin) {
  672. gpio_name = nmk_find_pin_name(pctldev, pin);
  673. ret = nmk_dt_add_map_configs(map, reserved_maps,
  674. num_maps,
  675. gpio_name, &configs, 1);
  676. if (ret < 0)
  677. goto exit;
  678. }
  679. }
  680. exit:
  681. return ret;
  682. }
  683. static int nmk_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  684. struct device_node *np_config,
  685. struct pinctrl_map **map,
  686. unsigned int *num_maps)
  687. {
  688. unsigned int reserved_maps;
  689. int ret;
  690. reserved_maps = 0;
  691. *map = NULL;
  692. *num_maps = 0;
  693. for_each_child_of_node_scoped(np_config, np) {
  694. ret = nmk_pinctrl_dt_subnode_to_map(pctldev, np, map,
  695. &reserved_maps, num_maps);
  696. if (ret < 0) {
  697. pinctrl_utils_free_map(pctldev, *map, *num_maps);
  698. return ret;
  699. }
  700. }
  701. return 0;
  702. }
  703. static const struct pinctrl_ops nmk_pinctrl_ops = {
  704. .get_groups_count = nmk_get_groups_cnt,
  705. .get_group_name = nmk_get_group_name,
  706. .get_group_pins = nmk_get_group_pins,
  707. .pin_dbg_show = nmk_pin_dbg_show,
  708. .dt_node_to_map = nmk_pinctrl_dt_node_to_map,
  709. .dt_free_map = pinctrl_utils_free_map,
  710. };
  711. static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  712. {
  713. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  714. return npct->soc->nfunctions;
  715. }
  716. static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
  717. unsigned int function)
  718. {
  719. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  720. return npct->soc->functions[function].name;
  721. }
  722. static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  723. unsigned int function,
  724. const char * const **groups,
  725. unsigned * const num_groups)
  726. {
  727. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  728. *groups = npct->soc->functions[function].groups;
  729. *num_groups = npct->soc->functions[function].ngroups;
  730. return 0;
  731. }
  732. static int nmk_pmx_set(struct pinctrl_dev *pctldev, unsigned int function,
  733. unsigned int group)
  734. {
  735. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  736. const struct nmk_pingroup *g;
  737. static unsigned int slpm[NMK_MAX_BANKS];
  738. unsigned long flags = 0;
  739. bool glitch;
  740. int ret = -EINVAL;
  741. int i;
  742. g = &npct->soc->groups[group];
  743. if (g->altsetting < 0)
  744. return -EINVAL;
  745. dev_dbg(npct->dev, "enable group %s, %zu pins\n", g->grp.name, g->grp.npins);
  746. /*
  747. * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
  748. * we may pass through an undesired state. In this case we take
  749. * some extra care.
  750. *
  751. * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
  752. * - Save SLPM registers (since we have a shadow register in the
  753. * nmk_chip we're using that as backup)
  754. * - Set SLPM=0 for the IOs you want to switch and others to 1
  755. * - Configure the GPIO registers for the IOs that are being switched
  756. * - Set IOFORCE=1
  757. * - Modify the AFLSA/B registers for the IOs that are being switched
  758. * - Set IOFORCE=0
  759. * - Restore SLPM registers
  760. * - Any spurious wake up event during switch sequence to be ignored
  761. * and cleared
  762. *
  763. * We REALLY need to save ALL slpm registers, because the external
  764. * IOFORCE will switch *all* ports to their sleepmode setting to as
  765. * to avoid glitches. (Not just one port!)
  766. */
  767. glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
  768. if (glitch) {
  769. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  770. /* Initially don't put any pins to sleep when switching */
  771. memset(slpm, 0xff, sizeof(slpm));
  772. /*
  773. * Then mask the pins that need to be sleeping now when we're
  774. * switching to the ALT C function.
  775. */
  776. for (i = 0; i < g->grp.npins; i++) {
  777. struct nmk_gpio_chip *nmk_chip;
  778. unsigned int bit;
  779. nmk_chip = find_nmk_gpio_from_pin(g->grp.pins[i], &bit);
  780. if (!nmk_chip) {
  781. dev_err(npct->dev,
  782. "invalid pin offset %d in group %s at index %d\n",
  783. g->grp.pins[i], g->grp.name, i);
  784. goto out_pre_slpm_init;
  785. }
  786. slpm[nmk_chip->bank] &= ~BIT(bit);
  787. }
  788. ret = nmk_gpio_glitch_slpm_init(slpm);
  789. if (ret)
  790. goto out_pre_slpm_init;
  791. }
  792. for (i = 0; i < g->grp.npins; i++) {
  793. struct nmk_gpio_chip *nmk_chip;
  794. unsigned int bit;
  795. nmk_chip = find_nmk_gpio_from_pin(g->grp.pins[i], &bit);
  796. if (!nmk_chip) {
  797. dev_err(npct->dev,
  798. "invalid pin offset %d in group %s at index %d\n",
  799. g->grp.pins[i], g->grp.name, i);
  800. goto out_glitch;
  801. }
  802. dev_dbg(npct->dev, "setting pin %d to altsetting %d\n",
  803. g->grp.pins[i], g->altsetting);
  804. ret = clk_enable(nmk_chip->clk);
  805. if (ret)
  806. goto out_glitch;
  807. /*
  808. * If the pin is switching to altfunc, and there was an
  809. * interrupt installed on it which has been lazy disabled,
  810. * actually mask the interrupt to prevent spurious interrupts
  811. * that would occur while the pin is under control of the
  812. * peripheral. Only SKE does this.
  813. */
  814. nmk_gpio_disable_lazy_irq(nmk_chip, bit);
  815. __nmk_gpio_set_mode_safe(nmk_chip, bit,
  816. (g->altsetting & NMK_GPIO_ALT_C), glitch);
  817. clk_disable(nmk_chip->clk);
  818. /*
  819. * Call PRCM GPIOCR config function in case ALTC
  820. * has been selected:
  821. * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
  822. * must be set.
  823. * - If selection is pure ALTC and previous selection was ALTCx,
  824. * then some bits in PRCM GPIOCR registers must be cleared.
  825. */
  826. if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
  827. nmk_prcm_altcx_set_mode(npct, g->grp.pins[i],
  828. g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
  829. }
  830. /* When all pins are successfully reconfigured we get here */
  831. ret = 0;
  832. out_glitch:
  833. if (glitch)
  834. nmk_gpio_glitch_slpm_restore(slpm);
  835. out_pre_slpm_init:
  836. if (glitch)
  837. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  838. return ret;
  839. }
  840. static int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
  841. struct pinctrl_gpio_range *range,
  842. unsigned int pin)
  843. {
  844. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  845. struct nmk_gpio_chip *nmk_chip;
  846. struct gpio_chip *chip;
  847. unsigned int bit;
  848. int ret;
  849. if (!range) {
  850. dev_err(npct->dev, "invalid range\n");
  851. return -EINVAL;
  852. }
  853. if (!range->gc) {
  854. dev_err(npct->dev, "missing GPIO chip in range\n");
  855. return -EINVAL;
  856. }
  857. chip = range->gc;
  858. nmk_chip = gpiochip_get_data(chip);
  859. dev_dbg(npct->dev, "enable pin %u as GPIO\n", pin);
  860. find_nmk_gpio_from_pin(pin, &bit);
  861. ret = clk_enable(nmk_chip->clk);
  862. if (ret)
  863. return ret;
  864. /* There is no glitch when converting any pin to GPIO */
  865. __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
  866. clk_disable(nmk_chip->clk);
  867. return 0;
  868. }
  869. static void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
  870. struct pinctrl_gpio_range *range,
  871. unsigned int pin)
  872. {
  873. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  874. dev_dbg(npct->dev, "disable pin %u as GPIO\n", pin);
  875. /* Set the pin to some default state, GPIO is usually default */
  876. }
  877. static const struct pinmux_ops nmk_pinmux_ops = {
  878. .get_functions_count = nmk_pmx_get_funcs_cnt,
  879. .get_function_name = nmk_pmx_get_func_name,
  880. .get_function_groups = nmk_pmx_get_func_groups,
  881. .set_mux = nmk_pmx_set,
  882. .gpio_request_enable = nmk_gpio_request_enable,
  883. .gpio_disable_free = nmk_gpio_disable_free,
  884. .strict = true,
  885. };
  886. static int nmk_pin_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
  887. unsigned long *config)
  888. {
  889. /* Not implemented */
  890. return -EINVAL;
  891. }
  892. static int nmk_pin_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
  893. unsigned long *configs, unsigned int num_configs)
  894. {
  895. static const char * const pullnames[] = {
  896. [NMK_GPIO_PULL_NONE] = "none",
  897. [NMK_GPIO_PULL_UP] = "up",
  898. [NMK_GPIO_PULL_DOWN] = "down",
  899. [3] /* illegal */ = "??"
  900. };
  901. static const char * const slpmnames[] = {
  902. [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
  903. [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
  904. };
  905. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  906. struct nmk_gpio_chip *nmk_chip;
  907. unsigned int bit;
  908. unsigned long cfg;
  909. int pull, slpm, output, val, i;
  910. bool lowemi, gpiomode, sleep;
  911. int ret;
  912. nmk_chip = find_nmk_gpio_from_pin(pin, &bit);
  913. if (!nmk_chip) {
  914. dev_err(npct->dev,
  915. "invalid pin offset %d\n", pin);
  916. return -EINVAL;
  917. }
  918. for (i = 0; i < num_configs; i++) {
  919. /*
  920. * The pin config contains pin number and altfunction fields,
  921. * here we just ignore that part. It's being handled by the
  922. * framework and pinmux callback respectively.
  923. */
  924. cfg = configs[i];
  925. pull = PIN_PULL(cfg);
  926. slpm = PIN_SLPM(cfg);
  927. output = PIN_DIR(cfg);
  928. val = PIN_VAL(cfg);
  929. lowemi = PIN_LOWEMI(cfg);
  930. gpiomode = PIN_GPIOMODE(cfg);
  931. sleep = PIN_SLEEPMODE(cfg);
  932. if (sleep) {
  933. int slpm_pull = PIN_SLPM_PULL(cfg);
  934. int slpm_output = PIN_SLPM_DIR(cfg);
  935. int slpm_val = PIN_SLPM_VAL(cfg);
  936. /* All pins go into GPIO mode at sleep */
  937. gpiomode = true;
  938. /*
  939. * The SLPM_* values are normal values + 1 to allow zero
  940. * to mean "same as normal".
  941. */
  942. if (slpm_pull)
  943. pull = slpm_pull - 1;
  944. if (slpm_output)
  945. output = slpm_output - 1;
  946. if (slpm_val)
  947. val = slpm_val - 1;
  948. dev_dbg(nmk_chip->chip.parent,
  949. "pin %d: sleep pull %s, dir %s, val %s\n",
  950. pin,
  951. slpm_pull ? pullnames[pull] : "same",
  952. slpm_output ? (output ? "output" : "input")
  953. : "same",
  954. slpm_val ? str_high_low(val) : "same");
  955. }
  956. dev_dbg(nmk_chip->chip.parent,
  957. "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
  958. pin, cfg, pullnames[pull], slpmnames[slpm],
  959. output ? "output " : "input",
  960. output ? str_high_low(val) : "",
  961. str_on_off(lowemi));
  962. ret = clk_enable(nmk_chip->clk);
  963. if (ret)
  964. return ret;
  965. if (gpiomode)
  966. /* No glitch when going to GPIO mode */
  967. __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
  968. if (output) {
  969. __nmk_gpio_make_output(nmk_chip, bit, val);
  970. } else {
  971. __nmk_gpio_make_input(nmk_chip, bit);
  972. __nmk_gpio_set_pull(nmk_chip, bit, pull);
  973. }
  974. /* TODO: isn't this only applicable on output pins? */
  975. __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
  976. __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
  977. clk_disable(nmk_chip->clk);
  978. } /* for each config */
  979. return 0;
  980. }
  981. static const struct pinconf_ops nmk_pinconf_ops = {
  982. .pin_config_get = nmk_pin_config_get,
  983. .pin_config_set = nmk_pin_config_set,
  984. };
  985. static struct pinctrl_desc nmk_pinctrl_desc = {
  986. .name = "pinctrl-nomadik",
  987. .pctlops = &nmk_pinctrl_ops,
  988. .pmxops = &nmk_pinmux_ops,
  989. .confops = &nmk_pinconf_ops,
  990. .owner = THIS_MODULE,
  991. };
  992. static const struct of_device_id nmk_pinctrl_match[] = {
  993. {
  994. .compatible = "stericsson,stn8815-pinctrl",
  995. .data = (void *)PINCTRL_NMK_STN8815,
  996. },
  997. {
  998. .compatible = "stericsson,db8500-pinctrl",
  999. .data = (void *)PINCTRL_NMK_DB8500,
  1000. },
  1001. {},
  1002. };
  1003. #ifdef CONFIG_PM_SLEEP
  1004. static int nmk_pinctrl_suspend(struct device *dev)
  1005. {
  1006. struct nmk_pinctrl *npct;
  1007. npct = dev_get_drvdata(dev);
  1008. if (!npct)
  1009. return -EINVAL;
  1010. return pinctrl_force_sleep(npct->pctl);
  1011. }
  1012. static int nmk_pinctrl_resume(struct device *dev)
  1013. {
  1014. struct nmk_pinctrl *npct;
  1015. npct = dev_get_drvdata(dev);
  1016. if (!npct)
  1017. return -EINVAL;
  1018. return pinctrl_force_default(npct->pctl);
  1019. }
  1020. #endif
  1021. static int nmk_pinctrl_probe(struct platform_device *pdev)
  1022. {
  1023. struct fwnode_handle *fwnode = dev_fwnode(&pdev->dev);
  1024. struct fwnode_handle *prcm_fwnode;
  1025. struct nmk_pinctrl *npct;
  1026. uintptr_t version = 0;
  1027. int i;
  1028. npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
  1029. if (!npct)
  1030. return -ENOMEM;
  1031. version = (uintptr_t)device_get_match_data(&pdev->dev);
  1032. /* Poke in other ASIC variants here */
  1033. if (version == PINCTRL_NMK_STN8815)
  1034. nmk_pinctrl_stn8815_init(&npct->soc);
  1035. if (version == PINCTRL_NMK_DB8500)
  1036. nmk_pinctrl_db8500_init(&npct->soc);
  1037. /*
  1038. * Since we depend on the GPIO chips to provide clock and register base
  1039. * for the pin control operations, make sure that we have these
  1040. * populated before we continue. Follow the phandles to instantiate
  1041. * them. The GPIO portion of the actual hardware may be probed before
  1042. * or after this point: it shouldn't matter as the APIs are orthogonal.
  1043. */
  1044. for (i = 0; i < NMK_MAX_BANKS; i++) {
  1045. struct fwnode_handle *gpio_fwnode;
  1046. struct nmk_gpio_chip *nmk_chip;
  1047. gpio_fwnode = fwnode_find_reference(fwnode, "nomadik-gpio-chips", i);
  1048. if (IS_ERR(gpio_fwnode))
  1049. continue;
  1050. dev_info(&pdev->dev, "populate NMK GPIO %d \"%pfwP\"\n", i, gpio_fwnode);
  1051. nmk_chip = nmk_gpio_populate_chip(gpio_fwnode, pdev);
  1052. if (IS_ERR(nmk_chip))
  1053. dev_err(&pdev->dev,
  1054. "could not populate nmk chip struct - continue anyway\n");
  1055. else
  1056. /* We are NOT compatible with mobileye,eyeq5-gpio. */
  1057. BUG_ON(nmk_chip->is_mobileye_soc);
  1058. fwnode_handle_put(gpio_fwnode);
  1059. }
  1060. prcm_fwnode = fwnode_find_reference(fwnode, "prcm", 0);
  1061. if (!IS_ERR(prcm_fwnode)) {
  1062. npct->prcm_base = fwnode_iomap(prcm_fwnode, 0);
  1063. fwnode_handle_put(prcm_fwnode);
  1064. }
  1065. if (!npct->prcm_base) {
  1066. if (version == PINCTRL_NMK_STN8815) {
  1067. dev_info(&pdev->dev,
  1068. "No PRCM base, assuming no ALT-Cx control is available\n");
  1069. } else {
  1070. dev_err(&pdev->dev, "missing PRCM base address\n");
  1071. return -EINVAL;
  1072. }
  1073. }
  1074. nmk_pinctrl_desc.pins = npct->soc->pins;
  1075. nmk_pinctrl_desc.npins = npct->soc->npins;
  1076. npct->dev = &pdev->dev;
  1077. npct->pctl = devm_pinctrl_register(&pdev->dev, &nmk_pinctrl_desc, npct);
  1078. if (IS_ERR(npct->pctl)) {
  1079. dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
  1080. return PTR_ERR(npct->pctl);
  1081. }
  1082. platform_set_drvdata(pdev, npct);
  1083. dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
  1084. return 0;
  1085. }
  1086. static SIMPLE_DEV_PM_OPS(nmk_pinctrl_pm_ops,
  1087. nmk_pinctrl_suspend,
  1088. nmk_pinctrl_resume);
  1089. static struct platform_driver nmk_pinctrl_driver = {
  1090. .driver = {
  1091. .name = "pinctrl-nomadik",
  1092. .of_match_table = nmk_pinctrl_match,
  1093. .pm = &nmk_pinctrl_pm_ops,
  1094. },
  1095. .probe = nmk_pinctrl_probe,
  1096. };
  1097. static int __init nmk_pinctrl_init(void)
  1098. {
  1099. return platform_driver_register(&nmk_pinctrl_driver);
  1100. }
  1101. core_initcall(nmk_pinctrl_init);