pinctrl-nomadik.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  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 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/irq.h>
  25. #include <linux/irqdomain.h>
  26. #include <linux/slab.h>
  27. #include <linux/of_device.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 <linux/platform_data/pinctrl-nomadik.h>
  34. #include <asm/mach/irq.h>
  35. #include "pinctrl-nomadik.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. struct nmk_gpio_chip {
  44. struct gpio_chip chip;
  45. struct irq_domain *domain;
  46. void __iomem *addr;
  47. struct clk *clk;
  48. unsigned int bank;
  49. unsigned int parent_irq;
  50. int secondary_parent_irq;
  51. u32 (*get_secondary_status)(unsigned int bank);
  52. void (*set_ioforce)(bool enable);
  53. spinlock_t lock;
  54. bool sleepmode;
  55. /* Keep track of configured edges */
  56. u32 edge_rising;
  57. u32 edge_falling;
  58. u32 real_wake;
  59. u32 rwimsc;
  60. u32 fwimsc;
  61. u32 rimsc;
  62. u32 fimsc;
  63. u32 pull_up;
  64. u32 lowemi;
  65. };
  66. /**
  67. * struct nmk_pinctrl - state container for the Nomadik pin controller
  68. * @dev: containing device pointer
  69. * @pctl: corresponding pin controller device
  70. * @soc: SoC data for this specific chip
  71. * @prcm_base: PRCM register range virtual base
  72. */
  73. struct nmk_pinctrl {
  74. struct device *dev;
  75. struct pinctrl_dev *pctl;
  76. const struct nmk_pinctrl_soc_data *soc;
  77. void __iomem *prcm_base;
  78. };
  79. static struct nmk_gpio_chip *
  80. nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
  81. static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
  82. #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
  83. static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
  84. unsigned offset, int gpio_mode)
  85. {
  86. u32 bit = 1 << offset;
  87. u32 afunc, bfunc;
  88. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
  89. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
  90. if (gpio_mode & NMK_GPIO_ALT_A)
  91. afunc |= bit;
  92. if (gpio_mode & NMK_GPIO_ALT_B)
  93. bfunc |= bit;
  94. writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
  95. writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
  96. }
  97. static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
  98. unsigned offset, enum nmk_gpio_slpm mode)
  99. {
  100. u32 bit = 1 << offset;
  101. u32 slpm;
  102. slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
  103. if (mode == NMK_GPIO_SLPM_NOCHANGE)
  104. slpm |= bit;
  105. else
  106. slpm &= ~bit;
  107. writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
  108. }
  109. static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
  110. unsigned offset, enum nmk_gpio_pull pull)
  111. {
  112. u32 bit = 1 << offset;
  113. u32 pdis;
  114. pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
  115. if (pull == NMK_GPIO_PULL_NONE) {
  116. pdis |= bit;
  117. nmk_chip->pull_up &= ~bit;
  118. } else {
  119. pdis &= ~bit;
  120. }
  121. writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
  122. if (pull == NMK_GPIO_PULL_UP) {
  123. nmk_chip->pull_up |= bit;
  124. writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
  125. } else if (pull == NMK_GPIO_PULL_DOWN) {
  126. nmk_chip->pull_up &= ~bit;
  127. writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
  128. }
  129. }
  130. static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
  131. unsigned offset, bool lowemi)
  132. {
  133. u32 bit = BIT(offset);
  134. bool enabled = nmk_chip->lowemi & bit;
  135. if (lowemi == enabled)
  136. return;
  137. if (lowemi)
  138. nmk_chip->lowemi |= bit;
  139. else
  140. nmk_chip->lowemi &= ~bit;
  141. writel_relaxed(nmk_chip->lowemi,
  142. nmk_chip->addr + NMK_GPIO_LOWEMI);
  143. }
  144. static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
  145. unsigned offset)
  146. {
  147. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  148. }
  149. static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
  150. unsigned offset, int val)
  151. {
  152. if (val)
  153. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
  154. else
  155. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
  156. }
  157. static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
  158. unsigned offset, int val)
  159. {
  160. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
  161. __nmk_gpio_set_output(nmk_chip, offset, val);
  162. }
  163. static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
  164. unsigned offset, int gpio_mode,
  165. bool glitch)
  166. {
  167. u32 rwimsc = nmk_chip->rwimsc;
  168. u32 fwimsc = nmk_chip->fwimsc;
  169. if (glitch && nmk_chip->set_ioforce) {
  170. u32 bit = BIT(offset);
  171. /* Prevent spurious wakeups */
  172. writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
  173. writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
  174. nmk_chip->set_ioforce(true);
  175. }
  176. __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
  177. if (glitch && nmk_chip->set_ioforce) {
  178. nmk_chip->set_ioforce(false);
  179. writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
  180. writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
  181. }
  182. }
  183. static void
  184. nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
  185. {
  186. u32 falling = nmk_chip->fimsc & BIT(offset);
  187. u32 rising = nmk_chip->rimsc & BIT(offset);
  188. int gpio = nmk_chip->chip.base + offset;
  189. int irq = NOMADIK_GPIO_TO_IRQ(gpio);
  190. struct irq_data *d = irq_get_irq_data(irq);
  191. if (!rising && !falling)
  192. return;
  193. if (!d || !irqd_irq_disabled(d))
  194. return;
  195. if (rising) {
  196. nmk_chip->rimsc &= ~BIT(offset);
  197. writel_relaxed(nmk_chip->rimsc,
  198. nmk_chip->addr + NMK_GPIO_RIMSC);
  199. }
  200. if (falling) {
  201. nmk_chip->fimsc &= ~BIT(offset);
  202. writel_relaxed(nmk_chip->fimsc,
  203. nmk_chip->addr + NMK_GPIO_FIMSC);
  204. }
  205. dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
  206. }
  207. static void nmk_write_masked(void __iomem *reg, u32 mask, u32 value)
  208. {
  209. u32 val;
  210. val = readl(reg);
  211. val = ((val & ~mask) | (value & mask));
  212. writel(val, reg);
  213. }
  214. static void nmk_prcm_altcx_set_mode(struct nmk_pinctrl *npct,
  215. unsigned offset, unsigned alt_num)
  216. {
  217. int i;
  218. u16 reg;
  219. u8 bit;
  220. u8 alt_index;
  221. const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
  222. const u16 *gpiocr_regs;
  223. if (alt_num > PRCM_IDX_GPIOCR_ALTC_MAX) {
  224. dev_err(npct->dev, "PRCM GPIOCR: alternate-C%i is invalid\n",
  225. alt_num);
  226. return;
  227. }
  228. for (i = 0 ; i < npct->soc->npins_altcx ; i++) {
  229. if (npct->soc->altcx_pins[i].pin == offset)
  230. break;
  231. }
  232. if (i == npct->soc->npins_altcx) {
  233. dev_dbg(npct->dev, "PRCM GPIOCR: pin %i is not found\n",
  234. offset);
  235. return;
  236. }
  237. pin_desc = npct->soc->altcx_pins + i;
  238. gpiocr_regs = npct->soc->prcm_gpiocr_registers;
  239. /*
  240. * If alt_num is NULL, just clear current ALTCx selection
  241. * to make sure we come back to a pure ALTC selection
  242. */
  243. if (!alt_num) {
  244. for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
  245. if (pin_desc->altcx[i].used == true) {
  246. reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
  247. bit = pin_desc->altcx[i].control_bit;
  248. if (readl(npct->prcm_base + reg) & BIT(bit)) {
  249. nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
  250. dev_dbg(npct->dev,
  251. "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
  252. offset, i+1);
  253. }
  254. }
  255. }
  256. return;
  257. }
  258. alt_index = alt_num - 1;
  259. if (pin_desc->altcx[alt_index].used == false) {
  260. dev_warn(npct->dev,
  261. "PRCM GPIOCR: pin %i: alternate-C%i does not exist\n",
  262. offset, alt_num);
  263. return;
  264. }
  265. /*
  266. * Check if any other ALTCx functions are activated on this pin
  267. * and disable it first.
  268. */
  269. for (i = 0 ; i < PRCM_IDX_GPIOCR_ALTC_MAX ; i++) {
  270. if (i == alt_index)
  271. continue;
  272. if (pin_desc->altcx[i].used == true) {
  273. reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
  274. bit = pin_desc->altcx[i].control_bit;
  275. if (readl(npct->prcm_base + reg) & BIT(bit)) {
  276. nmk_write_masked(npct->prcm_base + reg, BIT(bit), 0);
  277. dev_dbg(npct->dev,
  278. "PRCM GPIOCR: pin %i: alternate-C%i has been disabled\n",
  279. offset, i+1);
  280. }
  281. }
  282. }
  283. reg = gpiocr_regs[pin_desc->altcx[alt_index].reg_index];
  284. bit = pin_desc->altcx[alt_index].control_bit;
  285. dev_dbg(npct->dev, "PRCM GPIOCR: pin %i: alternate-C%i has been selected\n",
  286. offset, alt_index+1);
  287. nmk_write_masked(npct->prcm_base + reg, BIT(bit), BIT(bit));
  288. }
  289. static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
  290. pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
  291. {
  292. static const char *afnames[] = {
  293. [NMK_GPIO_ALT_GPIO] = "GPIO",
  294. [NMK_GPIO_ALT_A] = "A",
  295. [NMK_GPIO_ALT_B] = "B",
  296. [NMK_GPIO_ALT_C] = "C"
  297. };
  298. static const char *pullnames[] = {
  299. [NMK_GPIO_PULL_NONE] = "none",
  300. [NMK_GPIO_PULL_UP] = "up",
  301. [NMK_GPIO_PULL_DOWN] = "down",
  302. [3] /* illegal */ = "??"
  303. };
  304. static const char *slpmnames[] = {
  305. [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
  306. [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
  307. };
  308. int pin = PIN_NUM(cfg);
  309. int pull = PIN_PULL(cfg);
  310. int af = PIN_ALT(cfg);
  311. int slpm = PIN_SLPM(cfg);
  312. int output = PIN_DIR(cfg);
  313. int val = PIN_VAL(cfg);
  314. bool glitch = af == NMK_GPIO_ALT_C;
  315. dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
  316. pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
  317. output ? "output " : "input",
  318. output ? (val ? "high" : "low") : "");
  319. if (sleep) {
  320. int slpm_pull = PIN_SLPM_PULL(cfg);
  321. int slpm_output = PIN_SLPM_DIR(cfg);
  322. int slpm_val = PIN_SLPM_VAL(cfg);
  323. af = NMK_GPIO_ALT_GPIO;
  324. /*
  325. * The SLPM_* values are normal values + 1 to allow zero to
  326. * mean "same as normal".
  327. */
  328. if (slpm_pull)
  329. pull = slpm_pull - 1;
  330. if (slpm_output)
  331. output = slpm_output - 1;
  332. if (slpm_val)
  333. val = slpm_val - 1;
  334. dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
  335. pin,
  336. slpm_pull ? pullnames[pull] : "same",
  337. slpm_output ? (output ? "output" : "input") : "same",
  338. slpm_val ? (val ? "high" : "low") : "same");
  339. }
  340. if (output)
  341. __nmk_gpio_make_output(nmk_chip, offset, val);
  342. else {
  343. __nmk_gpio_make_input(nmk_chip, offset);
  344. __nmk_gpio_set_pull(nmk_chip, offset, pull);
  345. }
  346. __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
  347. /*
  348. * If the pin is switching to altfunc, and there was an interrupt
  349. * installed on it which has been lazy disabled, actually mask the
  350. * interrupt to prevent spurious interrupts that would occur while the
  351. * pin is under control of the peripheral. Only SKE does this.
  352. */
  353. if (af != NMK_GPIO_ALT_GPIO)
  354. nmk_gpio_disable_lazy_irq(nmk_chip, offset);
  355. /*
  356. * If we've backed up the SLPM registers (glitch workaround), modify
  357. * the backups since they will be restored.
  358. */
  359. if (slpmregs) {
  360. if (slpm == NMK_GPIO_SLPM_NOCHANGE)
  361. slpmregs[nmk_chip->bank] |= BIT(offset);
  362. else
  363. slpmregs[nmk_chip->bank] &= ~BIT(offset);
  364. } else
  365. __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
  366. __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
  367. }
  368. /*
  369. * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
  370. * - Save SLPM registers
  371. * - Set SLPM=0 for the IOs you want to switch and others to 1
  372. * - Configure the GPIO registers for the IOs that are being switched
  373. * - Set IOFORCE=1
  374. * - Modify the AFLSA/B registers for the IOs that are being switched
  375. * - Set IOFORCE=0
  376. * - Restore SLPM registers
  377. * - Any spurious wake up event during switch sequence to be ignored and
  378. * cleared
  379. */
  380. static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
  381. {
  382. int i;
  383. for (i = 0; i < NUM_BANKS; i++) {
  384. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  385. unsigned int temp = slpm[i];
  386. if (!chip)
  387. break;
  388. clk_enable(chip->clk);
  389. slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
  390. writel(temp, chip->addr + NMK_GPIO_SLPC);
  391. }
  392. }
  393. static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
  394. {
  395. int i;
  396. for (i = 0; i < NUM_BANKS; i++) {
  397. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  398. if (!chip)
  399. break;
  400. writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
  401. clk_disable(chip->clk);
  402. }
  403. }
  404. static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
  405. {
  406. static unsigned int slpm[NUM_BANKS];
  407. unsigned long flags;
  408. bool glitch = false;
  409. int ret = 0;
  410. int i;
  411. for (i = 0; i < num; i++) {
  412. if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
  413. glitch = true;
  414. break;
  415. }
  416. }
  417. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  418. if (glitch) {
  419. memset(slpm, 0xff, sizeof(slpm));
  420. for (i = 0; i < num; i++) {
  421. int pin = PIN_NUM(cfgs[i]);
  422. int offset = pin % NMK_GPIO_PER_CHIP;
  423. if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
  424. slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
  425. }
  426. nmk_gpio_glitch_slpm_init(slpm);
  427. }
  428. for (i = 0; i < num; i++) {
  429. struct nmk_gpio_chip *nmk_chip;
  430. int pin = PIN_NUM(cfgs[i]);
  431. nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
  432. if (!nmk_chip) {
  433. ret = -EINVAL;
  434. break;
  435. }
  436. clk_enable(nmk_chip->clk);
  437. spin_lock(&nmk_chip->lock);
  438. __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
  439. cfgs[i], sleep, glitch ? slpm : NULL);
  440. spin_unlock(&nmk_chip->lock);
  441. clk_disable(nmk_chip->clk);
  442. }
  443. if (glitch)
  444. nmk_gpio_glitch_slpm_restore(slpm);
  445. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  446. return ret;
  447. }
  448. /**
  449. * nmk_config_pin - configure a pin's mux attributes
  450. * @cfg: pin confguration
  451. * @sleep: Non-zero to apply the sleep mode configuration
  452. * Configures a pin's mode (alternate function or GPIO), its pull up status,
  453. * and its sleep mode based on the specified configuration. The @cfg is
  454. * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
  455. * are constructed using, and can be further enhanced with, the macros in
  456. * <linux/platform_data/pinctrl-nomadik.h>
  457. *
  458. * If a pin's mode is set to GPIO, it is configured as an input to avoid
  459. * side-effects. The gpio can be manipulated later using standard GPIO API
  460. * calls.
  461. */
  462. int nmk_config_pin(pin_cfg_t cfg, bool sleep)
  463. {
  464. return __nmk_config_pins(&cfg, 1, sleep);
  465. }
  466. EXPORT_SYMBOL(nmk_config_pin);
  467. /**
  468. * nmk_config_pins - configure several pins at once
  469. * @cfgs: array of pin configurations
  470. * @num: number of elments in the array
  471. *
  472. * Configures several pins using nmk_config_pin(). Refer to that function for
  473. * further information.
  474. */
  475. int nmk_config_pins(pin_cfg_t *cfgs, int num)
  476. {
  477. return __nmk_config_pins(cfgs, num, false);
  478. }
  479. EXPORT_SYMBOL(nmk_config_pins);
  480. int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
  481. {
  482. return __nmk_config_pins(cfgs, num, true);
  483. }
  484. EXPORT_SYMBOL(nmk_config_pins_sleep);
  485. /**
  486. * nmk_gpio_set_slpm() - configure the sleep mode of a pin
  487. * @gpio: pin number
  488. * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
  489. *
  490. * This register is actually in the pinmux layer, not the GPIO block itself.
  491. * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
  492. * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
  493. * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
  494. * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
  495. * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
  496. * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
  497. *
  498. * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
  499. * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
  500. * entered) regardless of the altfunction selected. Also wake-up detection is
  501. * ENABLED.
  502. *
  503. * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
  504. * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
  505. * (for altfunction GPIO) or respective on-chip peripherals (for other
  506. * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
  507. *
  508. * Note that enable_irq_wake() will automatically enable wakeup detection.
  509. */
  510. int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
  511. {
  512. struct nmk_gpio_chip *nmk_chip;
  513. unsigned long flags;
  514. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  515. if (!nmk_chip)
  516. return -EINVAL;
  517. clk_enable(nmk_chip->clk);
  518. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  519. spin_lock(&nmk_chip->lock);
  520. __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
  521. spin_unlock(&nmk_chip->lock);
  522. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  523. clk_disable(nmk_chip->clk);
  524. return 0;
  525. }
  526. /**
  527. * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
  528. * @gpio: pin number
  529. * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
  530. *
  531. * Enables/disables pull up/down on a specified pin. This only takes effect if
  532. * the pin is configured as an input (either explicitly or by the alternate
  533. * function).
  534. *
  535. * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
  536. * configured as an input. Otherwise, due to the way the controller registers
  537. * work, this function will change the value output on the pin.
  538. */
  539. int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
  540. {
  541. struct nmk_gpio_chip *nmk_chip;
  542. unsigned long flags;
  543. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  544. if (!nmk_chip)
  545. return -EINVAL;
  546. clk_enable(nmk_chip->clk);
  547. spin_lock_irqsave(&nmk_chip->lock, flags);
  548. __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
  549. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  550. clk_disable(nmk_chip->clk);
  551. return 0;
  552. }
  553. /* Mode functions */
  554. /**
  555. * nmk_gpio_set_mode() - set the mux mode of a gpio pin
  556. * @gpio: pin number
  557. * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
  558. * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
  559. *
  560. * Sets the mode of the specified pin to one of the alternate functions or
  561. * plain GPIO.
  562. */
  563. int nmk_gpio_set_mode(int gpio, int gpio_mode)
  564. {
  565. struct nmk_gpio_chip *nmk_chip;
  566. unsigned long flags;
  567. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  568. if (!nmk_chip)
  569. return -EINVAL;
  570. clk_enable(nmk_chip->clk);
  571. spin_lock_irqsave(&nmk_chip->lock, flags);
  572. __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
  573. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  574. clk_disable(nmk_chip->clk);
  575. return 0;
  576. }
  577. EXPORT_SYMBOL(nmk_gpio_set_mode);
  578. static int nmk_prcm_gpiocr_get_mode(struct pinctrl_dev *pctldev, int gpio)
  579. {
  580. int i;
  581. u16 reg;
  582. u8 bit;
  583. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  584. const struct prcm_gpiocr_altcx_pin_desc *pin_desc;
  585. const u16 *gpiocr_regs;
  586. for (i = 0; i < npct->soc->npins_altcx; i++) {
  587. if (npct->soc->altcx_pins[i].pin == gpio)
  588. break;
  589. }
  590. if (i == npct->soc->npins_altcx)
  591. return NMK_GPIO_ALT_C;
  592. pin_desc = npct->soc->altcx_pins + i;
  593. gpiocr_regs = npct->soc->prcm_gpiocr_registers;
  594. for (i = 0; i < PRCM_IDX_GPIOCR_ALTC_MAX; i++) {
  595. if (pin_desc->altcx[i].used == true) {
  596. reg = gpiocr_regs[pin_desc->altcx[i].reg_index];
  597. bit = pin_desc->altcx[i].control_bit;
  598. if (readl(npct->prcm_base + reg) & BIT(bit))
  599. return NMK_GPIO_ALT_C+i+1;
  600. }
  601. }
  602. return NMK_GPIO_ALT_C;
  603. }
  604. int nmk_gpio_get_mode(int gpio)
  605. {
  606. struct nmk_gpio_chip *nmk_chip;
  607. u32 afunc, bfunc, bit;
  608. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  609. if (!nmk_chip)
  610. return -EINVAL;
  611. bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
  612. clk_enable(nmk_chip->clk);
  613. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
  614. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
  615. clk_disable(nmk_chip->clk);
  616. return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
  617. }
  618. EXPORT_SYMBOL(nmk_gpio_get_mode);
  619. /* IRQ functions */
  620. static inline int nmk_gpio_get_bitmask(int gpio)
  621. {
  622. return 1 << (gpio % NMK_GPIO_PER_CHIP);
  623. }
  624. static void nmk_gpio_irq_ack(struct irq_data *d)
  625. {
  626. struct nmk_gpio_chip *nmk_chip;
  627. nmk_chip = irq_data_get_irq_chip_data(d);
  628. if (!nmk_chip)
  629. return;
  630. clk_enable(nmk_chip->clk);
  631. writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
  632. clk_disable(nmk_chip->clk);
  633. }
  634. enum nmk_gpio_irq_type {
  635. NORMAL,
  636. WAKE,
  637. };
  638. static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
  639. int gpio, enum nmk_gpio_irq_type which,
  640. bool enable)
  641. {
  642. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  643. u32 *rimscval;
  644. u32 *fimscval;
  645. u32 rimscreg;
  646. u32 fimscreg;
  647. if (which == NORMAL) {
  648. rimscreg = NMK_GPIO_RIMSC;
  649. fimscreg = NMK_GPIO_FIMSC;
  650. rimscval = &nmk_chip->rimsc;
  651. fimscval = &nmk_chip->fimsc;
  652. } else {
  653. rimscreg = NMK_GPIO_RWIMSC;
  654. fimscreg = NMK_GPIO_FWIMSC;
  655. rimscval = &nmk_chip->rwimsc;
  656. fimscval = &nmk_chip->fwimsc;
  657. }
  658. /* we must individually set/clear the two edges */
  659. if (nmk_chip->edge_rising & bitmask) {
  660. if (enable)
  661. *rimscval |= bitmask;
  662. else
  663. *rimscval &= ~bitmask;
  664. writel(*rimscval, nmk_chip->addr + rimscreg);
  665. }
  666. if (nmk_chip->edge_falling & bitmask) {
  667. if (enable)
  668. *fimscval |= bitmask;
  669. else
  670. *fimscval &= ~bitmask;
  671. writel(*fimscval, nmk_chip->addr + fimscreg);
  672. }
  673. }
  674. static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
  675. int gpio, bool on)
  676. {
  677. /*
  678. * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
  679. * disabled, since setting SLPM to 1 increases power consumption, and
  680. * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
  681. */
  682. if (nmk_chip->sleepmode && on) {
  683. __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP,
  684. NMK_GPIO_SLPM_WAKEUP_ENABLE);
  685. }
  686. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
  687. }
  688. static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
  689. {
  690. struct nmk_gpio_chip *nmk_chip;
  691. unsigned long flags;
  692. u32 bitmask;
  693. nmk_chip = irq_data_get_irq_chip_data(d);
  694. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  695. if (!nmk_chip)
  696. return -EINVAL;
  697. clk_enable(nmk_chip->clk);
  698. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  699. spin_lock(&nmk_chip->lock);
  700. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
  701. if (!(nmk_chip->real_wake & bitmask))
  702. __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
  703. spin_unlock(&nmk_chip->lock);
  704. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  705. clk_disable(nmk_chip->clk);
  706. return 0;
  707. }
  708. static void nmk_gpio_irq_mask(struct irq_data *d)
  709. {
  710. nmk_gpio_irq_maskunmask(d, false);
  711. }
  712. static void nmk_gpio_irq_unmask(struct irq_data *d)
  713. {
  714. nmk_gpio_irq_maskunmask(d, true);
  715. }
  716. static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  717. {
  718. struct nmk_gpio_chip *nmk_chip;
  719. unsigned long flags;
  720. u32 bitmask;
  721. nmk_chip = irq_data_get_irq_chip_data(d);
  722. if (!nmk_chip)
  723. return -EINVAL;
  724. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  725. clk_enable(nmk_chip->clk);
  726. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  727. spin_lock(&nmk_chip->lock);
  728. if (irqd_irq_disabled(d))
  729. __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
  730. if (on)
  731. nmk_chip->real_wake |= bitmask;
  732. else
  733. nmk_chip->real_wake &= ~bitmask;
  734. spin_unlock(&nmk_chip->lock);
  735. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  736. clk_disable(nmk_chip->clk);
  737. return 0;
  738. }
  739. static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  740. {
  741. bool enabled = !irqd_irq_disabled(d);
  742. bool wake = irqd_is_wakeup_set(d);
  743. struct nmk_gpio_chip *nmk_chip;
  744. unsigned long flags;
  745. u32 bitmask;
  746. nmk_chip = irq_data_get_irq_chip_data(d);
  747. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  748. if (!nmk_chip)
  749. return -EINVAL;
  750. if (type & IRQ_TYPE_LEVEL_HIGH)
  751. return -EINVAL;
  752. if (type & IRQ_TYPE_LEVEL_LOW)
  753. return -EINVAL;
  754. clk_enable(nmk_chip->clk);
  755. spin_lock_irqsave(&nmk_chip->lock, flags);
  756. if (enabled)
  757. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
  758. if (enabled || wake)
  759. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
  760. nmk_chip->edge_rising &= ~bitmask;
  761. if (type & IRQ_TYPE_EDGE_RISING)
  762. nmk_chip->edge_rising |= bitmask;
  763. nmk_chip->edge_falling &= ~bitmask;
  764. if (type & IRQ_TYPE_EDGE_FALLING)
  765. nmk_chip->edge_falling |= bitmask;
  766. if (enabled)
  767. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
  768. if (enabled || wake)
  769. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
  770. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  771. clk_disable(nmk_chip->clk);
  772. return 0;
  773. }
  774. static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
  775. {
  776. struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
  777. clk_enable(nmk_chip->clk);
  778. nmk_gpio_irq_unmask(d);
  779. return 0;
  780. }
  781. static void nmk_gpio_irq_shutdown(struct irq_data *d)
  782. {
  783. struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
  784. nmk_gpio_irq_mask(d);
  785. clk_disable(nmk_chip->clk);
  786. }
  787. static struct irq_chip nmk_gpio_irq_chip = {
  788. .name = "Nomadik-GPIO",
  789. .irq_ack = nmk_gpio_irq_ack,
  790. .irq_mask = nmk_gpio_irq_mask,
  791. .irq_unmask = nmk_gpio_irq_unmask,
  792. .irq_set_type = nmk_gpio_irq_set_type,
  793. .irq_set_wake = nmk_gpio_irq_set_wake,
  794. .irq_startup = nmk_gpio_irq_startup,
  795. .irq_shutdown = nmk_gpio_irq_shutdown,
  796. .flags = IRQCHIP_MASK_ON_SUSPEND,
  797. };
  798. static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
  799. u32 status)
  800. {
  801. struct nmk_gpio_chip *nmk_chip;
  802. struct irq_chip *host_chip = irq_get_chip(irq);
  803. chained_irq_enter(host_chip, desc);
  804. nmk_chip = irq_get_handler_data(irq);
  805. while (status) {
  806. int bit = __ffs(status);
  807. generic_handle_irq(irq_find_mapping(nmk_chip->domain, bit));
  808. status &= ~BIT(bit);
  809. }
  810. chained_irq_exit(host_chip, desc);
  811. }
  812. static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  813. {
  814. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  815. u32 status;
  816. clk_enable(nmk_chip->clk);
  817. status = readl(nmk_chip->addr + NMK_GPIO_IS);
  818. clk_disable(nmk_chip->clk);
  819. __nmk_gpio_irq_handler(irq, desc, status);
  820. }
  821. static void nmk_gpio_secondary_irq_handler(unsigned int irq,
  822. struct irq_desc *desc)
  823. {
  824. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  825. u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
  826. __nmk_gpio_irq_handler(irq, desc, status);
  827. }
  828. static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
  829. {
  830. irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
  831. irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
  832. if (nmk_chip->secondary_parent_irq >= 0) {
  833. irq_set_chained_handler(nmk_chip->secondary_parent_irq,
  834. nmk_gpio_secondary_irq_handler);
  835. irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
  836. }
  837. return 0;
  838. }
  839. /* I/O Functions */
  840. static int nmk_gpio_request(struct gpio_chip *chip, unsigned offset)
  841. {
  842. /*
  843. * Map back to global GPIO space and request muxing, the direction
  844. * parameter does not matter for this controller.
  845. */
  846. int gpio = chip->base + offset;
  847. return pinctrl_request_gpio(gpio);
  848. }
  849. static void nmk_gpio_free(struct gpio_chip *chip, unsigned offset)
  850. {
  851. int gpio = chip->base + offset;
  852. pinctrl_free_gpio(gpio);
  853. }
  854. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
  855. {
  856. struct nmk_gpio_chip *nmk_chip =
  857. container_of(chip, struct nmk_gpio_chip, chip);
  858. clk_enable(nmk_chip->clk);
  859. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  860. clk_disable(nmk_chip->clk);
  861. return 0;
  862. }
  863. static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
  864. {
  865. struct nmk_gpio_chip *nmk_chip =
  866. container_of(chip, struct nmk_gpio_chip, chip);
  867. u32 bit = 1 << offset;
  868. int value;
  869. clk_enable(nmk_chip->clk);
  870. value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  871. clk_disable(nmk_chip->clk);
  872. return value;
  873. }
  874. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  875. int val)
  876. {
  877. struct nmk_gpio_chip *nmk_chip =
  878. container_of(chip, struct nmk_gpio_chip, chip);
  879. clk_enable(nmk_chip->clk);
  880. __nmk_gpio_set_output(nmk_chip, offset, val);
  881. clk_disable(nmk_chip->clk);
  882. }
  883. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
  884. int val)
  885. {
  886. struct nmk_gpio_chip *nmk_chip =
  887. container_of(chip, struct nmk_gpio_chip, chip);
  888. clk_enable(nmk_chip->clk);
  889. __nmk_gpio_make_output(nmk_chip, offset, val);
  890. clk_disable(nmk_chip->clk);
  891. return 0;
  892. }
  893. static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  894. {
  895. struct nmk_gpio_chip *nmk_chip =
  896. container_of(chip, struct nmk_gpio_chip, chip);
  897. return irq_create_mapping(nmk_chip->domain, offset);
  898. }
  899. #ifdef CONFIG_DEBUG_FS
  900. #include <linux/seq_file.h>
  901. static void nmk_gpio_dbg_show_one(struct seq_file *s,
  902. struct pinctrl_dev *pctldev, struct gpio_chip *chip,
  903. unsigned offset, unsigned gpio)
  904. {
  905. const char *label = gpiochip_is_requested(chip, offset);
  906. struct nmk_gpio_chip *nmk_chip =
  907. container_of(chip, struct nmk_gpio_chip, chip);
  908. int mode;
  909. bool is_out;
  910. bool pull;
  911. u32 bit = 1 << offset;
  912. const char *modes[] = {
  913. [NMK_GPIO_ALT_GPIO] = "gpio",
  914. [NMK_GPIO_ALT_A] = "altA",
  915. [NMK_GPIO_ALT_B] = "altB",
  916. [NMK_GPIO_ALT_C] = "altC",
  917. [NMK_GPIO_ALT_C+1] = "altC1",
  918. [NMK_GPIO_ALT_C+2] = "altC2",
  919. [NMK_GPIO_ALT_C+3] = "altC3",
  920. [NMK_GPIO_ALT_C+4] = "altC4",
  921. };
  922. clk_enable(nmk_chip->clk);
  923. is_out = !!(readl(nmk_chip->addr + NMK_GPIO_DIR) & bit);
  924. pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
  925. mode = nmk_gpio_get_mode(gpio);
  926. if ((mode == NMK_GPIO_ALT_C) && pctldev)
  927. mode = nmk_prcm_gpiocr_get_mode(pctldev, gpio);
  928. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
  929. gpio, label ?: "(none)",
  930. is_out ? "out" : "in ",
  931. chip->get
  932. ? (chip->get(chip, offset) ? "hi" : "lo")
  933. : "? ",
  934. (mode < 0) ? "unknown" : modes[mode],
  935. pull ? "pull" : "none");
  936. if (label && !is_out) {
  937. int irq = gpio_to_irq(gpio);
  938. struct irq_desc *desc = irq_to_desc(irq);
  939. /* This races with request_irq(), set_irq_type(),
  940. * and set_irq_wake() ... but those are "rare".
  941. */
  942. if (irq >= 0 && desc->action) {
  943. char *trigger;
  944. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  945. if (nmk_chip->edge_rising & bitmask)
  946. trigger = "edge-rising";
  947. else if (nmk_chip->edge_falling & bitmask)
  948. trigger = "edge-falling";
  949. else
  950. trigger = "edge-undefined";
  951. seq_printf(s, " irq-%d %s%s",
  952. irq, trigger,
  953. irqd_is_wakeup_set(&desc->irq_data)
  954. ? " wakeup" : "");
  955. }
  956. }
  957. clk_disable(nmk_chip->clk);
  958. }
  959. static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  960. {
  961. unsigned i;
  962. unsigned gpio = chip->base;
  963. for (i = 0; i < chip->ngpio; i++, gpio++) {
  964. nmk_gpio_dbg_show_one(s, NULL, chip, i, gpio);
  965. seq_printf(s, "\n");
  966. }
  967. }
  968. #else
  969. static inline void nmk_gpio_dbg_show_one(struct seq_file *s,
  970. struct pinctrl_dev *pctldev,
  971. struct gpio_chip *chip,
  972. unsigned offset, unsigned gpio)
  973. {
  974. }
  975. #define nmk_gpio_dbg_show NULL
  976. #endif
  977. /* This structure is replicated for each GPIO block allocated at probe time */
  978. static struct gpio_chip nmk_gpio_template = {
  979. .request = nmk_gpio_request,
  980. .free = nmk_gpio_free,
  981. .direction_input = nmk_gpio_make_input,
  982. .get = nmk_gpio_get_input,
  983. .direction_output = nmk_gpio_make_output,
  984. .set = nmk_gpio_set_output,
  985. .to_irq = nmk_gpio_to_irq,
  986. .dbg_show = nmk_gpio_dbg_show,
  987. .can_sleep = 0,
  988. };
  989. void nmk_gpio_clocks_enable(void)
  990. {
  991. int i;
  992. for (i = 0; i < NUM_BANKS; i++) {
  993. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  994. if (!chip)
  995. continue;
  996. clk_enable(chip->clk);
  997. }
  998. }
  999. void nmk_gpio_clocks_disable(void)
  1000. {
  1001. int i;
  1002. for (i = 0; i < NUM_BANKS; i++) {
  1003. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  1004. if (!chip)
  1005. continue;
  1006. clk_disable(chip->clk);
  1007. }
  1008. }
  1009. /*
  1010. * Called from the suspend/resume path to only keep the real wakeup interrupts
  1011. * (those that have had set_irq_wake() called on them) as wakeup interrupts,
  1012. * and not the rest of the interrupts which we needed to have as wakeups for
  1013. * cpuidle.
  1014. *
  1015. * PM ops are not used since this needs to be done at the end, after all the
  1016. * other drivers are done with their suspend callbacks.
  1017. */
  1018. void nmk_gpio_wakeups_suspend(void)
  1019. {
  1020. int i;
  1021. for (i = 0; i < NUM_BANKS; i++) {
  1022. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  1023. if (!chip)
  1024. break;
  1025. clk_enable(chip->clk);
  1026. writel(chip->rwimsc & chip->real_wake,
  1027. chip->addr + NMK_GPIO_RWIMSC);
  1028. writel(chip->fwimsc & chip->real_wake,
  1029. chip->addr + NMK_GPIO_FWIMSC);
  1030. clk_disable(chip->clk);
  1031. }
  1032. }
  1033. void nmk_gpio_wakeups_resume(void)
  1034. {
  1035. int i;
  1036. for (i = 0; i < NUM_BANKS; i++) {
  1037. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  1038. if (!chip)
  1039. break;
  1040. clk_enable(chip->clk);
  1041. writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
  1042. writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
  1043. clk_disable(chip->clk);
  1044. }
  1045. }
  1046. /*
  1047. * Read the pull up/pull down status.
  1048. * A bit set in 'pull_up' means that pull up
  1049. * is selected if pull is enabled in PDIS register.
  1050. * Note: only pull up/down set via this driver can
  1051. * be detected due to HW limitations.
  1052. */
  1053. void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
  1054. {
  1055. if (gpio_bank < NUM_BANKS) {
  1056. struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
  1057. if (!chip)
  1058. return;
  1059. *pull_up = chip->pull_up;
  1060. }
  1061. }
  1062. static int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
  1063. irq_hw_number_t hwirq)
  1064. {
  1065. struct nmk_gpio_chip *nmk_chip = d->host_data;
  1066. if (!nmk_chip)
  1067. return -EINVAL;
  1068. irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
  1069. set_irq_flags(irq, IRQF_VALID);
  1070. irq_set_chip_data(irq, nmk_chip);
  1071. irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
  1072. return 0;
  1073. }
  1074. const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
  1075. .map = nmk_gpio_irq_map,
  1076. .xlate = irq_domain_xlate_twocell,
  1077. };
  1078. static int __devinit nmk_gpio_probe(struct platform_device *dev)
  1079. {
  1080. struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
  1081. struct device_node *np = dev->dev.of_node;
  1082. struct nmk_gpio_chip *nmk_chip;
  1083. struct gpio_chip *chip;
  1084. struct resource *res;
  1085. struct clk *clk;
  1086. int secondary_irq;
  1087. void __iomem *base;
  1088. int irq_start = 0;
  1089. int irq;
  1090. int ret;
  1091. if (!pdata && !np) {
  1092. dev_err(&dev->dev, "No platform data or device tree found\n");
  1093. return -ENODEV;
  1094. }
  1095. if (np) {
  1096. pdata = devm_kzalloc(&dev->dev, sizeof(*pdata), GFP_KERNEL);
  1097. if (!pdata)
  1098. return -ENOMEM;
  1099. if (of_get_property(np, "st,supports-sleepmode", NULL))
  1100. pdata->supports_sleepmode = true;
  1101. if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
  1102. dev_err(&dev->dev, "gpio-bank property not found\n");
  1103. ret = -EINVAL;
  1104. goto out;
  1105. }
  1106. pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
  1107. pdata->num_gpio = NMK_GPIO_PER_CHIP;
  1108. }
  1109. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1110. if (!res) {
  1111. ret = -ENOENT;
  1112. goto out;
  1113. }
  1114. irq = platform_get_irq(dev, 0);
  1115. if (irq < 0) {
  1116. ret = irq;
  1117. goto out;
  1118. }
  1119. secondary_irq = platform_get_irq(dev, 1);
  1120. if (secondary_irq >= 0 && !pdata->get_secondary_status) {
  1121. ret = -EINVAL;
  1122. goto out;
  1123. }
  1124. base = devm_request_and_ioremap(&dev->dev, res);
  1125. if (!base) {
  1126. ret = -ENOMEM;
  1127. goto out;
  1128. }
  1129. clk = devm_clk_get(&dev->dev, NULL);
  1130. if (IS_ERR(clk)) {
  1131. ret = PTR_ERR(clk);
  1132. goto out;
  1133. }
  1134. clk_prepare(clk);
  1135. nmk_chip = devm_kzalloc(&dev->dev, sizeof(*nmk_chip), GFP_KERNEL);
  1136. if (!nmk_chip) {
  1137. ret = -ENOMEM;
  1138. goto out;
  1139. }
  1140. /*
  1141. * The virt address in nmk_chip->addr is in the nomadik register space,
  1142. * so we can simply convert the resource address, without remapping
  1143. */
  1144. nmk_chip->bank = dev->id;
  1145. nmk_chip->clk = clk;
  1146. nmk_chip->addr = base;
  1147. nmk_chip->chip = nmk_gpio_template;
  1148. nmk_chip->parent_irq = irq;
  1149. nmk_chip->secondary_parent_irq = secondary_irq;
  1150. nmk_chip->get_secondary_status = pdata->get_secondary_status;
  1151. nmk_chip->set_ioforce = pdata->set_ioforce;
  1152. nmk_chip->sleepmode = pdata->supports_sleepmode;
  1153. spin_lock_init(&nmk_chip->lock);
  1154. chip = &nmk_chip->chip;
  1155. chip->base = pdata->first_gpio;
  1156. chip->ngpio = pdata->num_gpio;
  1157. chip->label = pdata->name ?: dev_name(&dev->dev);
  1158. chip->dev = &dev->dev;
  1159. chip->owner = THIS_MODULE;
  1160. clk_enable(nmk_chip->clk);
  1161. nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
  1162. clk_disable(nmk_chip->clk);
  1163. #ifdef CONFIG_OF_GPIO
  1164. chip->of_node = np;
  1165. #endif
  1166. ret = gpiochip_add(&nmk_chip->chip);
  1167. if (ret)
  1168. goto out;
  1169. BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
  1170. nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
  1171. platform_set_drvdata(dev, nmk_chip);
  1172. if (!np)
  1173. irq_start = NOMADIK_GPIO_TO_IRQ(pdata->first_gpio);
  1174. nmk_chip->domain = irq_domain_add_simple(np,
  1175. NMK_GPIO_PER_CHIP, irq_start,
  1176. &nmk_gpio_irq_simple_ops, nmk_chip);
  1177. if (!nmk_chip->domain) {
  1178. dev_err(&dev->dev, "failed to create irqdomain\n");
  1179. ret = -ENOSYS;
  1180. goto out;
  1181. }
  1182. nmk_gpio_init_irq(nmk_chip);
  1183. dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
  1184. return 0;
  1185. out:
  1186. dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
  1187. pdata->first_gpio, pdata->first_gpio+31);
  1188. return ret;
  1189. }
  1190. static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
  1191. {
  1192. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1193. return npct->soc->ngroups;
  1194. }
  1195. static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
  1196. unsigned selector)
  1197. {
  1198. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1199. return npct->soc->groups[selector].name;
  1200. }
  1201. static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  1202. const unsigned **pins,
  1203. unsigned *num_pins)
  1204. {
  1205. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1206. *pins = npct->soc->groups[selector].pins;
  1207. *num_pins = npct->soc->groups[selector].npins;
  1208. return 0;
  1209. }
  1210. static struct pinctrl_gpio_range *
  1211. nmk_match_gpio_range(struct pinctrl_dev *pctldev, unsigned offset)
  1212. {
  1213. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1214. int i;
  1215. for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
  1216. struct pinctrl_gpio_range *range;
  1217. range = &npct->soc->gpio_ranges[i];
  1218. if (offset >= range->pin_base &&
  1219. offset <= (range->pin_base + range->npins - 1))
  1220. return range;
  1221. }
  1222. return NULL;
  1223. }
  1224. static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  1225. unsigned offset)
  1226. {
  1227. struct pinctrl_gpio_range *range;
  1228. struct gpio_chip *chip;
  1229. range = nmk_match_gpio_range(pctldev, offset);
  1230. if (!range || !range->gc) {
  1231. seq_printf(s, "invalid pin offset");
  1232. return;
  1233. }
  1234. chip = range->gc;
  1235. nmk_gpio_dbg_show_one(s, pctldev, chip, offset - chip->base, offset);
  1236. }
  1237. static struct pinctrl_ops nmk_pinctrl_ops = {
  1238. .get_groups_count = nmk_get_groups_cnt,
  1239. .get_group_name = nmk_get_group_name,
  1240. .get_group_pins = nmk_get_group_pins,
  1241. .pin_dbg_show = nmk_pin_dbg_show,
  1242. };
  1243. static int nmk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  1244. {
  1245. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1246. return npct->soc->nfunctions;
  1247. }
  1248. static const char *nmk_pmx_get_func_name(struct pinctrl_dev *pctldev,
  1249. unsigned function)
  1250. {
  1251. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1252. return npct->soc->functions[function].name;
  1253. }
  1254. static int nmk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  1255. unsigned function,
  1256. const char * const **groups,
  1257. unsigned * const num_groups)
  1258. {
  1259. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1260. *groups = npct->soc->functions[function].groups;
  1261. *num_groups = npct->soc->functions[function].ngroups;
  1262. return 0;
  1263. }
  1264. static int nmk_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
  1265. unsigned group)
  1266. {
  1267. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1268. const struct nmk_pingroup *g;
  1269. static unsigned int slpm[NUM_BANKS];
  1270. unsigned long flags;
  1271. bool glitch;
  1272. int ret = -EINVAL;
  1273. int i;
  1274. g = &npct->soc->groups[group];
  1275. if (g->altsetting < 0)
  1276. return -EINVAL;
  1277. dev_dbg(npct->dev, "enable group %s, %u pins\n", g->name, g->npins);
  1278. /*
  1279. * If we're setting altfunc C by setting both AFSLA and AFSLB to 1,
  1280. * we may pass through an undesired state. In this case we take
  1281. * some extra care.
  1282. *
  1283. * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
  1284. * - Save SLPM registers (since we have a shadow register in the
  1285. * nmk_chip we're using that as backup)
  1286. * - Set SLPM=0 for the IOs you want to switch and others to 1
  1287. * - Configure the GPIO registers for the IOs that are being switched
  1288. * - Set IOFORCE=1
  1289. * - Modify the AFLSA/B registers for the IOs that are being switched
  1290. * - Set IOFORCE=0
  1291. * - Restore SLPM registers
  1292. * - Any spurious wake up event during switch sequence to be ignored
  1293. * and cleared
  1294. *
  1295. * We REALLY need to save ALL slpm registers, because the external
  1296. * IOFORCE will switch *all* ports to their sleepmode setting to as
  1297. * to avoid glitches. (Not just one port!)
  1298. */
  1299. glitch = ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C);
  1300. if (glitch) {
  1301. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  1302. /* Initially don't put any pins to sleep when switching */
  1303. memset(slpm, 0xff, sizeof(slpm));
  1304. /*
  1305. * Then mask the pins that need to be sleeping now when we're
  1306. * switching to the ALT C function.
  1307. */
  1308. for (i = 0; i < g->npins; i++)
  1309. slpm[g->pins[i] / NMK_GPIO_PER_CHIP] &= ~BIT(g->pins[i]);
  1310. nmk_gpio_glitch_slpm_init(slpm);
  1311. }
  1312. for (i = 0; i < g->npins; i++) {
  1313. struct pinctrl_gpio_range *range;
  1314. struct nmk_gpio_chip *nmk_chip;
  1315. struct gpio_chip *chip;
  1316. unsigned bit;
  1317. range = nmk_match_gpio_range(pctldev, g->pins[i]);
  1318. if (!range) {
  1319. dev_err(npct->dev,
  1320. "invalid pin offset %d in group %s at index %d\n",
  1321. g->pins[i], g->name, i);
  1322. goto out_glitch;
  1323. }
  1324. if (!range->gc) {
  1325. dev_err(npct->dev, "GPIO chip missing in range for pin offset %d in group %s at index %d\n",
  1326. g->pins[i], g->name, i);
  1327. goto out_glitch;
  1328. }
  1329. chip = range->gc;
  1330. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1331. dev_dbg(npct->dev, "setting pin %d to altsetting %d\n", g->pins[i], g->altsetting);
  1332. clk_enable(nmk_chip->clk);
  1333. bit = g->pins[i] % NMK_GPIO_PER_CHIP;
  1334. /*
  1335. * If the pin is switching to altfunc, and there was an
  1336. * interrupt installed on it which has been lazy disabled,
  1337. * actually mask the interrupt to prevent spurious interrupts
  1338. * that would occur while the pin is under control of the
  1339. * peripheral. Only SKE does this.
  1340. */
  1341. nmk_gpio_disable_lazy_irq(nmk_chip, bit);
  1342. __nmk_gpio_set_mode_safe(nmk_chip, bit,
  1343. (g->altsetting & NMK_GPIO_ALT_C), glitch);
  1344. clk_disable(nmk_chip->clk);
  1345. /*
  1346. * Call PRCM GPIOCR config function in case ALTC
  1347. * has been selected:
  1348. * - If selection is a ALTCx, some bits in PRCM GPIOCR registers
  1349. * must be set.
  1350. * - If selection is pure ALTC and previous selection was ALTCx,
  1351. * then some bits in PRCM GPIOCR registers must be cleared.
  1352. */
  1353. if ((g->altsetting & NMK_GPIO_ALT_C) == NMK_GPIO_ALT_C)
  1354. nmk_prcm_altcx_set_mode(npct, g->pins[i],
  1355. g->altsetting >> NMK_GPIO_ALT_CX_SHIFT);
  1356. }
  1357. /* When all pins are successfully reconfigured we get here */
  1358. ret = 0;
  1359. out_glitch:
  1360. if (glitch) {
  1361. nmk_gpio_glitch_slpm_restore(slpm);
  1362. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  1363. }
  1364. return ret;
  1365. }
  1366. static void nmk_pmx_disable(struct pinctrl_dev *pctldev,
  1367. unsigned function, unsigned group)
  1368. {
  1369. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1370. const struct nmk_pingroup *g;
  1371. g = &npct->soc->groups[group];
  1372. if (g->altsetting < 0)
  1373. return;
  1374. /* Poke out the mux, set the pin to some default state? */
  1375. dev_dbg(npct->dev, "disable group %s, %u pins\n", g->name, g->npins);
  1376. }
  1377. static int nmk_gpio_request_enable(struct pinctrl_dev *pctldev,
  1378. struct pinctrl_gpio_range *range,
  1379. unsigned offset)
  1380. {
  1381. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1382. struct nmk_gpio_chip *nmk_chip;
  1383. struct gpio_chip *chip;
  1384. unsigned bit;
  1385. if (!range) {
  1386. dev_err(npct->dev, "invalid range\n");
  1387. return -EINVAL;
  1388. }
  1389. if (!range->gc) {
  1390. dev_err(npct->dev, "missing GPIO chip in range\n");
  1391. return -EINVAL;
  1392. }
  1393. chip = range->gc;
  1394. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1395. dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
  1396. clk_enable(nmk_chip->clk);
  1397. bit = offset % NMK_GPIO_PER_CHIP;
  1398. /* There is no glitch when converting any pin to GPIO */
  1399. __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
  1400. clk_disable(nmk_chip->clk);
  1401. return 0;
  1402. }
  1403. static void nmk_gpio_disable_free(struct pinctrl_dev *pctldev,
  1404. struct pinctrl_gpio_range *range,
  1405. unsigned offset)
  1406. {
  1407. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1408. dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
  1409. /* Set the pin to some default state, GPIO is usually default */
  1410. }
  1411. static struct pinmux_ops nmk_pinmux_ops = {
  1412. .get_functions_count = nmk_pmx_get_funcs_cnt,
  1413. .get_function_name = nmk_pmx_get_func_name,
  1414. .get_function_groups = nmk_pmx_get_func_groups,
  1415. .enable = nmk_pmx_enable,
  1416. .disable = nmk_pmx_disable,
  1417. .gpio_request_enable = nmk_gpio_request_enable,
  1418. .gpio_disable_free = nmk_gpio_disable_free,
  1419. };
  1420. static int nmk_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
  1421. unsigned long *config)
  1422. {
  1423. /* Not implemented */
  1424. return -EINVAL;
  1425. }
  1426. static int nmk_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
  1427. unsigned long config)
  1428. {
  1429. static const char *pullnames[] = {
  1430. [NMK_GPIO_PULL_NONE] = "none",
  1431. [NMK_GPIO_PULL_UP] = "up",
  1432. [NMK_GPIO_PULL_DOWN] = "down",
  1433. [3] /* illegal */ = "??"
  1434. };
  1435. static const char *slpmnames[] = {
  1436. [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
  1437. [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
  1438. };
  1439. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1440. struct nmk_gpio_chip *nmk_chip;
  1441. struct pinctrl_gpio_range *range;
  1442. struct gpio_chip *chip;
  1443. unsigned bit;
  1444. /*
  1445. * The pin config contains pin number and altfunction fields, here
  1446. * we just ignore that part. It's being handled by the framework and
  1447. * pinmux callback respectively.
  1448. */
  1449. pin_cfg_t cfg = (pin_cfg_t) config;
  1450. int pull = PIN_PULL(cfg);
  1451. int slpm = PIN_SLPM(cfg);
  1452. int output = PIN_DIR(cfg);
  1453. int val = PIN_VAL(cfg);
  1454. bool lowemi = PIN_LOWEMI(cfg);
  1455. bool gpiomode = PIN_GPIOMODE(cfg);
  1456. bool sleep = PIN_SLEEPMODE(cfg);
  1457. range = nmk_match_gpio_range(pctldev, pin);
  1458. if (!range) {
  1459. dev_err(npct->dev, "invalid pin offset %d\n", pin);
  1460. return -EINVAL;
  1461. }
  1462. if (!range->gc) {
  1463. dev_err(npct->dev, "GPIO chip missing in range for pin %d\n",
  1464. pin);
  1465. return -EINVAL;
  1466. }
  1467. chip = range->gc;
  1468. nmk_chip = container_of(chip, struct nmk_gpio_chip, chip);
  1469. if (sleep) {
  1470. int slpm_pull = PIN_SLPM_PULL(cfg);
  1471. int slpm_output = PIN_SLPM_DIR(cfg);
  1472. int slpm_val = PIN_SLPM_VAL(cfg);
  1473. /* All pins go into GPIO mode at sleep */
  1474. gpiomode = true;
  1475. /*
  1476. * The SLPM_* values are normal values + 1 to allow zero to
  1477. * mean "same as normal".
  1478. */
  1479. if (slpm_pull)
  1480. pull = slpm_pull - 1;
  1481. if (slpm_output)
  1482. output = slpm_output - 1;
  1483. if (slpm_val)
  1484. val = slpm_val - 1;
  1485. dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
  1486. pin,
  1487. slpm_pull ? pullnames[pull] : "same",
  1488. slpm_output ? (output ? "output" : "input") : "same",
  1489. slpm_val ? (val ? "high" : "low") : "same");
  1490. }
  1491. dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: pull %s, slpm %s (%s%s), lowemi %s\n",
  1492. pin, cfg, pullnames[pull], slpmnames[slpm],
  1493. output ? "output " : "input",
  1494. output ? (val ? "high" : "low") : "",
  1495. lowemi ? "on" : "off" );
  1496. clk_enable(nmk_chip->clk);
  1497. bit = pin % NMK_GPIO_PER_CHIP;
  1498. if (gpiomode)
  1499. /* No glitch when going to GPIO mode */
  1500. __nmk_gpio_set_mode(nmk_chip, bit, NMK_GPIO_ALT_GPIO);
  1501. if (output)
  1502. __nmk_gpio_make_output(nmk_chip, bit, val);
  1503. else {
  1504. __nmk_gpio_make_input(nmk_chip, bit);
  1505. __nmk_gpio_set_pull(nmk_chip, bit, pull);
  1506. }
  1507. /* TODO: isn't this only applicable on output pins? */
  1508. __nmk_gpio_set_lowemi(nmk_chip, bit, lowemi);
  1509. __nmk_gpio_set_slpm(nmk_chip, bit, slpm);
  1510. clk_disable(nmk_chip->clk);
  1511. return 0;
  1512. }
  1513. static struct pinconf_ops nmk_pinconf_ops = {
  1514. .pin_config_get = nmk_pin_config_get,
  1515. .pin_config_set = nmk_pin_config_set,
  1516. };
  1517. static struct pinctrl_desc nmk_pinctrl_desc = {
  1518. .name = "pinctrl-nomadik",
  1519. .pctlops = &nmk_pinctrl_ops,
  1520. .pmxops = &nmk_pinmux_ops,
  1521. .confops = &nmk_pinconf_ops,
  1522. .owner = THIS_MODULE,
  1523. };
  1524. static const struct of_device_id nmk_pinctrl_match[] = {
  1525. {
  1526. .compatible = "stericsson,nmk_pinctrl",
  1527. .data = (void *)PINCTRL_NMK_DB8500,
  1528. },
  1529. {},
  1530. };
  1531. static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
  1532. {
  1533. const struct platform_device_id *platid = platform_get_device_id(pdev);
  1534. struct device_node *np = pdev->dev.of_node;
  1535. struct nmk_pinctrl *npct;
  1536. struct resource *res;
  1537. unsigned int version = 0;
  1538. int i;
  1539. npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
  1540. if (!npct)
  1541. return -ENOMEM;
  1542. if (platid)
  1543. version = platid->driver_data;
  1544. else if (np) {
  1545. const struct of_device_id *match;
  1546. match = of_match_device(nmk_pinctrl_match, &pdev->dev);
  1547. if (!match)
  1548. return -ENODEV;
  1549. version = (unsigned int) match->data;
  1550. }
  1551. /* Poke in other ASIC variants here */
  1552. if (version == PINCTRL_NMK_STN8815)
  1553. nmk_pinctrl_stn8815_init(&npct->soc);
  1554. if (version == PINCTRL_NMK_DB8500)
  1555. nmk_pinctrl_db8500_init(&npct->soc);
  1556. if (version == PINCTRL_NMK_DB8540)
  1557. nmk_pinctrl_db8540_init(&npct->soc);
  1558. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1559. if (res) {
  1560. npct->prcm_base = devm_ioremap(&pdev->dev, res->start,
  1561. resource_size(res));
  1562. if (!npct->prcm_base) {
  1563. dev_err(&pdev->dev,
  1564. "failed to ioremap PRCM registers\n");
  1565. return -ENOMEM;
  1566. }
  1567. } else {
  1568. dev_info(&pdev->dev,
  1569. "No PRCM base, assume no ALT-Cx control is available\n");
  1570. }
  1571. /*
  1572. * We need all the GPIO drivers to probe FIRST, or we will not be able
  1573. * to obtain references to the struct gpio_chip * for them, and we
  1574. * need this to proceed.
  1575. */
  1576. for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
  1577. if (!nmk_gpio_chips[npct->soc->gpio_ranges[i].id]) {
  1578. dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
  1579. return -EPROBE_DEFER;
  1580. }
  1581. npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[npct->soc->gpio_ranges[i].id]->chip;
  1582. }
  1583. nmk_pinctrl_desc.pins = npct->soc->pins;
  1584. nmk_pinctrl_desc.npins = npct->soc->npins;
  1585. npct->dev = &pdev->dev;
  1586. npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
  1587. if (!npct->pctl) {
  1588. dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
  1589. return -EINVAL;
  1590. }
  1591. /* We will handle a range of GPIO pins */
  1592. for (i = 0; i < npct->soc->gpio_num_ranges; i++)
  1593. pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
  1594. platform_set_drvdata(pdev, npct);
  1595. dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
  1596. return 0;
  1597. }
  1598. static const struct of_device_id nmk_gpio_match[] = {
  1599. { .compatible = "st,nomadik-gpio", },
  1600. {}
  1601. };
  1602. static struct platform_driver nmk_gpio_driver = {
  1603. .driver = {
  1604. .owner = THIS_MODULE,
  1605. .name = "gpio",
  1606. .of_match_table = nmk_gpio_match,
  1607. },
  1608. .probe = nmk_gpio_probe,
  1609. };
  1610. static const struct platform_device_id nmk_pinctrl_id[] = {
  1611. { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
  1612. { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
  1613. { "pinctrl-db8540", PINCTRL_NMK_DB8540 },
  1614. { }
  1615. };
  1616. static struct platform_driver nmk_pinctrl_driver = {
  1617. .driver = {
  1618. .owner = THIS_MODULE,
  1619. .name = "pinctrl-nomadik",
  1620. .of_match_table = nmk_pinctrl_match,
  1621. },
  1622. .probe = nmk_pinctrl_probe,
  1623. .id_table = nmk_pinctrl_id,
  1624. };
  1625. static int __init nmk_gpio_init(void)
  1626. {
  1627. int ret;
  1628. ret = platform_driver_register(&nmk_gpio_driver);
  1629. if (ret)
  1630. return ret;
  1631. return platform_driver_register(&nmk_pinctrl_driver);
  1632. }
  1633. core_initcall(nmk_gpio_init);
  1634. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  1635. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  1636. MODULE_LICENSE("GPL");