pinctrl-nomadik.c 57 KB

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