pinctrl-nomadik.c 48 KB

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