pinctrl-nomadik.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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/pinctrl/pinctrl.h>
  28. #include <asm/mach/irq.h>
  29. #include <plat/pincfg.h>
  30. #include <plat/gpio-nomadik.h>
  31. #include "pinctrl-nomadik.h"
  32. /*
  33. * The GPIO module in the Nomadik family of Systems-on-Chip is an
  34. * AMBA device, managing 32 pins and alternate functions. The logic block
  35. * is currently used in the Nomadik and ux500.
  36. *
  37. * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
  38. */
  39. #define NMK_GPIO_PER_CHIP 32
  40. struct nmk_gpio_chip {
  41. struct gpio_chip chip;
  42. struct irq_domain *domain;
  43. void __iomem *addr;
  44. struct clk *clk;
  45. unsigned int bank;
  46. unsigned int parent_irq;
  47. int secondary_parent_irq;
  48. u32 (*get_secondary_status)(unsigned int bank);
  49. void (*set_ioforce)(bool enable);
  50. spinlock_t lock;
  51. bool sleepmode;
  52. /* Keep track of configured edges */
  53. u32 edge_rising;
  54. u32 edge_falling;
  55. u32 real_wake;
  56. u32 rwimsc;
  57. u32 fwimsc;
  58. u32 rimsc;
  59. u32 fimsc;
  60. u32 pull_up;
  61. u32 lowemi;
  62. };
  63. struct nmk_pinctrl {
  64. struct device *dev;
  65. struct pinctrl_dev *pctl;
  66. const struct nmk_pinctrl_soc_data *soc;
  67. };
  68. static struct nmk_gpio_chip *
  69. nmk_gpio_chips[DIV_ROUND_UP(ARCH_NR_GPIOS, NMK_GPIO_PER_CHIP)];
  70. static DEFINE_SPINLOCK(nmk_gpio_slpm_lock);
  71. #define NUM_BANKS ARRAY_SIZE(nmk_gpio_chips)
  72. static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
  73. unsigned offset, int gpio_mode)
  74. {
  75. u32 bit = 1 << offset;
  76. u32 afunc, bfunc;
  77. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
  78. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
  79. if (gpio_mode & NMK_GPIO_ALT_A)
  80. afunc |= bit;
  81. if (gpio_mode & NMK_GPIO_ALT_B)
  82. bfunc |= bit;
  83. writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
  84. writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
  85. }
  86. static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
  87. unsigned offset, enum nmk_gpio_slpm mode)
  88. {
  89. u32 bit = 1 << offset;
  90. u32 slpm;
  91. slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
  92. if (mode == NMK_GPIO_SLPM_NOCHANGE)
  93. slpm |= bit;
  94. else
  95. slpm &= ~bit;
  96. writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
  97. }
  98. static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
  99. unsigned offset, enum nmk_gpio_pull pull)
  100. {
  101. u32 bit = 1 << offset;
  102. u32 pdis;
  103. pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
  104. if (pull == NMK_GPIO_PULL_NONE) {
  105. pdis |= bit;
  106. nmk_chip->pull_up &= ~bit;
  107. } else {
  108. pdis &= ~bit;
  109. }
  110. writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
  111. if (pull == NMK_GPIO_PULL_UP) {
  112. nmk_chip->pull_up |= bit;
  113. writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
  114. } else if (pull == NMK_GPIO_PULL_DOWN) {
  115. nmk_chip->pull_up &= ~bit;
  116. writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
  117. }
  118. }
  119. static void __nmk_gpio_set_lowemi(struct nmk_gpio_chip *nmk_chip,
  120. unsigned offset, bool lowemi)
  121. {
  122. u32 bit = BIT(offset);
  123. bool enabled = nmk_chip->lowemi & bit;
  124. if (lowemi == enabled)
  125. return;
  126. if (lowemi)
  127. nmk_chip->lowemi |= bit;
  128. else
  129. nmk_chip->lowemi &= ~bit;
  130. writel_relaxed(nmk_chip->lowemi,
  131. nmk_chip->addr + NMK_GPIO_LOWEMI);
  132. }
  133. static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
  134. unsigned offset)
  135. {
  136. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  137. }
  138. static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
  139. unsigned offset, int val)
  140. {
  141. if (val)
  142. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
  143. else
  144. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
  145. }
  146. static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
  147. unsigned offset, int val)
  148. {
  149. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
  150. __nmk_gpio_set_output(nmk_chip, offset, val);
  151. }
  152. static void __nmk_gpio_set_mode_safe(struct nmk_gpio_chip *nmk_chip,
  153. unsigned offset, int gpio_mode,
  154. bool glitch)
  155. {
  156. u32 rwimsc = nmk_chip->rwimsc;
  157. u32 fwimsc = nmk_chip->fwimsc;
  158. if (glitch && nmk_chip->set_ioforce) {
  159. u32 bit = BIT(offset);
  160. /* Prevent spurious wakeups */
  161. writel(rwimsc & ~bit, nmk_chip->addr + NMK_GPIO_RWIMSC);
  162. writel(fwimsc & ~bit, nmk_chip->addr + NMK_GPIO_FWIMSC);
  163. nmk_chip->set_ioforce(true);
  164. }
  165. __nmk_gpio_set_mode(nmk_chip, offset, gpio_mode);
  166. if (glitch && nmk_chip->set_ioforce) {
  167. nmk_chip->set_ioforce(false);
  168. writel(rwimsc, nmk_chip->addr + NMK_GPIO_RWIMSC);
  169. writel(fwimsc, nmk_chip->addr + NMK_GPIO_FWIMSC);
  170. }
  171. }
  172. static void
  173. nmk_gpio_disable_lazy_irq(struct nmk_gpio_chip *nmk_chip, unsigned offset)
  174. {
  175. u32 falling = nmk_chip->fimsc & BIT(offset);
  176. u32 rising = nmk_chip->rimsc & BIT(offset);
  177. int gpio = nmk_chip->chip.base + offset;
  178. int irq = NOMADIK_GPIO_TO_IRQ(gpio);
  179. struct irq_data *d = irq_get_irq_data(irq);
  180. if (!rising && !falling)
  181. return;
  182. if (!d || !irqd_irq_disabled(d))
  183. return;
  184. if (rising) {
  185. nmk_chip->rimsc &= ~BIT(offset);
  186. writel_relaxed(nmk_chip->rimsc,
  187. nmk_chip->addr + NMK_GPIO_RIMSC);
  188. }
  189. if (falling) {
  190. nmk_chip->fimsc &= ~BIT(offset);
  191. writel_relaxed(nmk_chip->fimsc,
  192. nmk_chip->addr + NMK_GPIO_FIMSC);
  193. }
  194. dev_dbg(nmk_chip->chip.dev, "%d: clearing interrupt mask\n", gpio);
  195. }
  196. static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
  197. pin_cfg_t cfg, bool sleep, unsigned int *slpmregs)
  198. {
  199. static const char *afnames[] = {
  200. [NMK_GPIO_ALT_GPIO] = "GPIO",
  201. [NMK_GPIO_ALT_A] = "A",
  202. [NMK_GPIO_ALT_B] = "B",
  203. [NMK_GPIO_ALT_C] = "C"
  204. };
  205. static const char *pullnames[] = {
  206. [NMK_GPIO_PULL_NONE] = "none",
  207. [NMK_GPIO_PULL_UP] = "up",
  208. [NMK_GPIO_PULL_DOWN] = "down",
  209. [3] /* illegal */ = "??"
  210. };
  211. static const char *slpmnames[] = {
  212. [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
  213. [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
  214. };
  215. int pin = PIN_NUM(cfg);
  216. int pull = PIN_PULL(cfg);
  217. int af = PIN_ALT(cfg);
  218. int slpm = PIN_SLPM(cfg);
  219. int output = PIN_DIR(cfg);
  220. int val = PIN_VAL(cfg);
  221. bool glitch = af == NMK_GPIO_ALT_C;
  222. dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
  223. pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
  224. output ? "output " : "input",
  225. output ? (val ? "high" : "low") : "");
  226. if (sleep) {
  227. int slpm_pull = PIN_SLPM_PULL(cfg);
  228. int slpm_output = PIN_SLPM_DIR(cfg);
  229. int slpm_val = PIN_SLPM_VAL(cfg);
  230. af = NMK_GPIO_ALT_GPIO;
  231. /*
  232. * The SLPM_* values are normal values + 1 to allow zero to
  233. * mean "same as normal".
  234. */
  235. if (slpm_pull)
  236. pull = slpm_pull - 1;
  237. if (slpm_output)
  238. output = slpm_output - 1;
  239. if (slpm_val)
  240. val = slpm_val - 1;
  241. dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
  242. pin,
  243. slpm_pull ? pullnames[pull] : "same",
  244. slpm_output ? (output ? "output" : "input") : "same",
  245. slpm_val ? (val ? "high" : "low") : "same");
  246. }
  247. if (output)
  248. __nmk_gpio_make_output(nmk_chip, offset, val);
  249. else {
  250. __nmk_gpio_make_input(nmk_chip, offset);
  251. __nmk_gpio_set_pull(nmk_chip, offset, pull);
  252. }
  253. __nmk_gpio_set_lowemi(nmk_chip, offset, PIN_LOWEMI(cfg));
  254. /*
  255. * If the pin is switching to altfunc, and there was an interrupt
  256. * installed on it which has been lazy disabled, actually mask the
  257. * interrupt to prevent spurious interrupts that would occur while the
  258. * pin is under control of the peripheral. Only SKE does this.
  259. */
  260. if (af != NMK_GPIO_ALT_GPIO)
  261. nmk_gpio_disable_lazy_irq(nmk_chip, offset);
  262. /*
  263. * If we've backed up the SLPM registers (glitch workaround), modify
  264. * the backups since they will be restored.
  265. */
  266. if (slpmregs) {
  267. if (slpm == NMK_GPIO_SLPM_NOCHANGE)
  268. slpmregs[nmk_chip->bank] |= BIT(offset);
  269. else
  270. slpmregs[nmk_chip->bank] &= ~BIT(offset);
  271. } else
  272. __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
  273. __nmk_gpio_set_mode_safe(nmk_chip, offset, af, glitch);
  274. }
  275. /*
  276. * Safe sequence used to switch IOs between GPIO and Alternate-C mode:
  277. * - Save SLPM registers
  278. * - Set SLPM=0 for the IOs you want to switch and others to 1
  279. * - Configure the GPIO registers for the IOs that are being switched
  280. * - Set IOFORCE=1
  281. * - Modify the AFLSA/B registers for the IOs that are being switched
  282. * - Set IOFORCE=0
  283. * - Restore SLPM registers
  284. * - Any spurious wake up event during switch sequence to be ignored and
  285. * cleared
  286. */
  287. static void nmk_gpio_glitch_slpm_init(unsigned int *slpm)
  288. {
  289. int i;
  290. for (i = 0; i < NUM_BANKS; i++) {
  291. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  292. unsigned int temp = slpm[i];
  293. if (!chip)
  294. break;
  295. clk_enable(chip->clk);
  296. slpm[i] = readl(chip->addr + NMK_GPIO_SLPC);
  297. writel(temp, chip->addr + NMK_GPIO_SLPC);
  298. }
  299. }
  300. static void nmk_gpio_glitch_slpm_restore(unsigned int *slpm)
  301. {
  302. int i;
  303. for (i = 0; i < NUM_BANKS; i++) {
  304. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  305. if (!chip)
  306. break;
  307. writel(slpm[i], chip->addr + NMK_GPIO_SLPC);
  308. clk_disable(chip->clk);
  309. }
  310. }
  311. static int __nmk_config_pins(pin_cfg_t *cfgs, int num, bool sleep)
  312. {
  313. static unsigned int slpm[NUM_BANKS];
  314. unsigned long flags;
  315. bool glitch = false;
  316. int ret = 0;
  317. int i;
  318. for (i = 0; i < num; i++) {
  319. if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C) {
  320. glitch = true;
  321. break;
  322. }
  323. }
  324. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  325. if (glitch) {
  326. memset(slpm, 0xff, sizeof(slpm));
  327. for (i = 0; i < num; i++) {
  328. int pin = PIN_NUM(cfgs[i]);
  329. int offset = pin % NMK_GPIO_PER_CHIP;
  330. if (PIN_ALT(cfgs[i]) == NMK_GPIO_ALT_C)
  331. slpm[pin / NMK_GPIO_PER_CHIP] &= ~BIT(offset);
  332. }
  333. nmk_gpio_glitch_slpm_init(slpm);
  334. }
  335. for (i = 0; i < num; i++) {
  336. struct nmk_gpio_chip *nmk_chip;
  337. int pin = PIN_NUM(cfgs[i]);
  338. nmk_chip = nmk_gpio_chips[pin / NMK_GPIO_PER_CHIP];
  339. if (!nmk_chip) {
  340. ret = -EINVAL;
  341. break;
  342. }
  343. clk_enable(nmk_chip->clk);
  344. spin_lock(&nmk_chip->lock);
  345. __nmk_config_pin(nmk_chip, pin % NMK_GPIO_PER_CHIP,
  346. cfgs[i], sleep, glitch ? slpm : NULL);
  347. spin_unlock(&nmk_chip->lock);
  348. clk_disable(nmk_chip->clk);
  349. }
  350. if (glitch)
  351. nmk_gpio_glitch_slpm_restore(slpm);
  352. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  353. return ret;
  354. }
  355. /**
  356. * nmk_config_pin - configure a pin's mux attributes
  357. * @cfg: pin confguration
  358. *
  359. * Configures a pin's mode (alternate function or GPIO), its pull up status,
  360. * and its sleep mode based on the specified configuration. The @cfg is
  361. * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
  362. * are constructed using, and can be further enhanced with, the macros in
  363. * plat/pincfg.h.
  364. *
  365. * If a pin's mode is set to GPIO, it is configured as an input to avoid
  366. * side-effects. The gpio can be manipulated later using standard GPIO API
  367. * calls.
  368. */
  369. int nmk_config_pin(pin_cfg_t cfg, bool sleep)
  370. {
  371. return __nmk_config_pins(&cfg, 1, sleep);
  372. }
  373. EXPORT_SYMBOL(nmk_config_pin);
  374. /**
  375. * nmk_config_pins - configure several pins at once
  376. * @cfgs: array of pin configurations
  377. * @num: number of elments in the array
  378. *
  379. * Configures several pins using nmk_config_pin(). Refer to that function for
  380. * further information.
  381. */
  382. int nmk_config_pins(pin_cfg_t *cfgs, int num)
  383. {
  384. return __nmk_config_pins(cfgs, num, false);
  385. }
  386. EXPORT_SYMBOL(nmk_config_pins);
  387. int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
  388. {
  389. return __nmk_config_pins(cfgs, num, true);
  390. }
  391. EXPORT_SYMBOL(nmk_config_pins_sleep);
  392. /**
  393. * nmk_gpio_set_slpm() - configure the sleep mode of a pin
  394. * @gpio: pin number
  395. * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
  396. *
  397. * This register is actually in the pinmux layer, not the GPIO block itself.
  398. * The GPIO1B_SLPM register defines the GPIO mode when SLEEP/DEEP-SLEEP
  399. * mode is entered (i.e. when signal IOFORCE is HIGH by the platform code).
  400. * Each GPIO can be configured to be forced into GPIO mode when IOFORCE is
  401. * HIGH, overriding the normal setting defined by GPIO_AFSELx registers.
  402. * When IOFORCE returns LOW (by software, after SLEEP/DEEP-SLEEP exit),
  403. * the GPIOs return to the normal setting defined by GPIO_AFSELx registers.
  404. *
  405. * If @mode is NMK_GPIO_SLPM_INPUT, the corresponding GPIO is switched to GPIO
  406. * mode when signal IOFORCE is HIGH (i.e. when SLEEP/DEEP-SLEEP mode is
  407. * entered) regardless of the altfunction selected. Also wake-up detection is
  408. * ENABLED.
  409. *
  410. * If @mode is NMK_GPIO_SLPM_NOCHANGE, the corresponding GPIO remains
  411. * controlled by NMK_GPIO_DATC, NMK_GPIO_DATS, NMK_GPIO_DIR, NMK_GPIO_PDIS
  412. * (for altfunction GPIO) or respective on-chip peripherals (for other
  413. * altfuncs) when IOFORCE is HIGH. Also wake-up detection DISABLED.
  414. *
  415. * Note that enable_irq_wake() will automatically enable wakeup detection.
  416. */
  417. int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
  418. {
  419. struct nmk_gpio_chip *nmk_chip;
  420. unsigned long flags;
  421. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  422. if (!nmk_chip)
  423. return -EINVAL;
  424. clk_enable(nmk_chip->clk);
  425. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  426. spin_lock(&nmk_chip->lock);
  427. __nmk_gpio_set_slpm(nmk_chip, gpio % NMK_GPIO_PER_CHIP, mode);
  428. spin_unlock(&nmk_chip->lock);
  429. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  430. clk_disable(nmk_chip->clk);
  431. return 0;
  432. }
  433. /**
  434. * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
  435. * @gpio: pin number
  436. * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
  437. *
  438. * Enables/disables pull up/down on a specified pin. This only takes effect if
  439. * the pin is configured as an input (either explicitly or by the alternate
  440. * function).
  441. *
  442. * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
  443. * configured as an input. Otherwise, due to the way the controller registers
  444. * work, this function will change the value output on the pin.
  445. */
  446. int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
  447. {
  448. struct nmk_gpio_chip *nmk_chip;
  449. unsigned long flags;
  450. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  451. if (!nmk_chip)
  452. return -EINVAL;
  453. clk_enable(nmk_chip->clk);
  454. spin_lock_irqsave(&nmk_chip->lock, flags);
  455. __nmk_gpio_set_pull(nmk_chip, gpio % NMK_GPIO_PER_CHIP, pull);
  456. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  457. clk_disable(nmk_chip->clk);
  458. return 0;
  459. }
  460. /* Mode functions */
  461. /**
  462. * nmk_gpio_set_mode() - set the mux mode of a gpio pin
  463. * @gpio: pin number
  464. * @gpio_mode: one of NMK_GPIO_ALT_GPIO, NMK_GPIO_ALT_A,
  465. * NMK_GPIO_ALT_B, and NMK_GPIO_ALT_C
  466. *
  467. * Sets the mode of the specified pin to one of the alternate functions or
  468. * plain GPIO.
  469. */
  470. int nmk_gpio_set_mode(int gpio, int gpio_mode)
  471. {
  472. struct nmk_gpio_chip *nmk_chip;
  473. unsigned long flags;
  474. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  475. if (!nmk_chip)
  476. return -EINVAL;
  477. clk_enable(nmk_chip->clk);
  478. spin_lock_irqsave(&nmk_chip->lock, flags);
  479. __nmk_gpio_set_mode(nmk_chip, gpio % NMK_GPIO_PER_CHIP, gpio_mode);
  480. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  481. clk_disable(nmk_chip->clk);
  482. return 0;
  483. }
  484. EXPORT_SYMBOL(nmk_gpio_set_mode);
  485. int nmk_gpio_get_mode(int gpio)
  486. {
  487. struct nmk_gpio_chip *nmk_chip;
  488. u32 afunc, bfunc, bit;
  489. nmk_chip = nmk_gpio_chips[gpio / NMK_GPIO_PER_CHIP];
  490. if (!nmk_chip)
  491. return -EINVAL;
  492. bit = 1 << (gpio % NMK_GPIO_PER_CHIP);
  493. clk_enable(nmk_chip->clk);
  494. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
  495. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
  496. clk_disable(nmk_chip->clk);
  497. return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
  498. }
  499. EXPORT_SYMBOL(nmk_gpio_get_mode);
  500. /* IRQ functions */
  501. static inline int nmk_gpio_get_bitmask(int gpio)
  502. {
  503. return 1 << (gpio % NMK_GPIO_PER_CHIP);
  504. }
  505. static void nmk_gpio_irq_ack(struct irq_data *d)
  506. {
  507. struct nmk_gpio_chip *nmk_chip;
  508. nmk_chip = irq_data_get_irq_chip_data(d);
  509. if (!nmk_chip)
  510. return;
  511. clk_enable(nmk_chip->clk);
  512. writel(nmk_gpio_get_bitmask(d->hwirq), nmk_chip->addr + NMK_GPIO_IC);
  513. clk_disable(nmk_chip->clk);
  514. }
  515. enum nmk_gpio_irq_type {
  516. NORMAL,
  517. WAKE,
  518. };
  519. static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
  520. int gpio, enum nmk_gpio_irq_type which,
  521. bool enable)
  522. {
  523. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  524. u32 *rimscval;
  525. u32 *fimscval;
  526. u32 rimscreg;
  527. u32 fimscreg;
  528. if (which == NORMAL) {
  529. rimscreg = NMK_GPIO_RIMSC;
  530. fimscreg = NMK_GPIO_FIMSC;
  531. rimscval = &nmk_chip->rimsc;
  532. fimscval = &nmk_chip->fimsc;
  533. } else {
  534. rimscreg = NMK_GPIO_RWIMSC;
  535. fimscreg = NMK_GPIO_FWIMSC;
  536. rimscval = &nmk_chip->rwimsc;
  537. fimscval = &nmk_chip->fwimsc;
  538. }
  539. /* we must individually set/clear the two edges */
  540. if (nmk_chip->edge_rising & bitmask) {
  541. if (enable)
  542. *rimscval |= bitmask;
  543. else
  544. *rimscval &= ~bitmask;
  545. writel(*rimscval, nmk_chip->addr + rimscreg);
  546. }
  547. if (nmk_chip->edge_falling & bitmask) {
  548. if (enable)
  549. *fimscval |= bitmask;
  550. else
  551. *fimscval &= ~bitmask;
  552. writel(*fimscval, nmk_chip->addr + fimscreg);
  553. }
  554. }
  555. static void __nmk_gpio_set_wake(struct nmk_gpio_chip *nmk_chip,
  556. int gpio, bool on)
  557. {
  558. /*
  559. * Ensure WAKEUP_ENABLE is on. No need to disable it if wakeup is
  560. * disabled, since setting SLPM to 1 increases power consumption, and
  561. * wakeup is anyhow controlled by the RIMSC and FIMSC registers.
  562. */
  563. if (nmk_chip->sleepmode && on) {
  564. __nmk_gpio_set_slpm(nmk_chip, gpio % nmk_chip->chip.base,
  565. NMK_GPIO_SLPM_WAKEUP_ENABLE);
  566. }
  567. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
  568. }
  569. static int nmk_gpio_irq_maskunmask(struct irq_data *d, bool enable)
  570. {
  571. struct nmk_gpio_chip *nmk_chip;
  572. unsigned long flags;
  573. u32 bitmask;
  574. nmk_chip = irq_data_get_irq_chip_data(d);
  575. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  576. if (!nmk_chip)
  577. return -EINVAL;
  578. clk_enable(nmk_chip->clk);
  579. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  580. spin_lock(&nmk_chip->lock);
  581. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, enable);
  582. if (!(nmk_chip->real_wake & bitmask))
  583. __nmk_gpio_set_wake(nmk_chip, d->hwirq, enable);
  584. spin_unlock(&nmk_chip->lock);
  585. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  586. clk_disable(nmk_chip->clk);
  587. return 0;
  588. }
  589. static void nmk_gpio_irq_mask(struct irq_data *d)
  590. {
  591. nmk_gpio_irq_maskunmask(d, false);
  592. }
  593. static void nmk_gpio_irq_unmask(struct irq_data *d)
  594. {
  595. nmk_gpio_irq_maskunmask(d, true);
  596. }
  597. static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  598. {
  599. struct nmk_gpio_chip *nmk_chip;
  600. unsigned long flags;
  601. u32 bitmask;
  602. nmk_chip = irq_data_get_irq_chip_data(d);
  603. if (!nmk_chip)
  604. return -EINVAL;
  605. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  606. clk_enable(nmk_chip->clk);
  607. spin_lock_irqsave(&nmk_gpio_slpm_lock, flags);
  608. spin_lock(&nmk_chip->lock);
  609. if (irqd_irq_disabled(d))
  610. __nmk_gpio_set_wake(nmk_chip, d->hwirq, on);
  611. if (on)
  612. nmk_chip->real_wake |= bitmask;
  613. else
  614. nmk_chip->real_wake &= ~bitmask;
  615. spin_unlock(&nmk_chip->lock);
  616. spin_unlock_irqrestore(&nmk_gpio_slpm_lock, flags);
  617. clk_disable(nmk_chip->clk);
  618. return 0;
  619. }
  620. static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  621. {
  622. bool enabled = !irqd_irq_disabled(d);
  623. bool wake = irqd_is_wakeup_set(d);
  624. struct nmk_gpio_chip *nmk_chip;
  625. unsigned long flags;
  626. u32 bitmask;
  627. nmk_chip = irq_data_get_irq_chip_data(d);
  628. bitmask = nmk_gpio_get_bitmask(d->hwirq);
  629. if (!nmk_chip)
  630. return -EINVAL;
  631. if (type & IRQ_TYPE_LEVEL_HIGH)
  632. return -EINVAL;
  633. if (type & IRQ_TYPE_LEVEL_LOW)
  634. return -EINVAL;
  635. clk_enable(nmk_chip->clk);
  636. spin_lock_irqsave(&nmk_chip->lock, flags);
  637. if (enabled)
  638. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, false);
  639. if (enabled || wake)
  640. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, false);
  641. nmk_chip->edge_rising &= ~bitmask;
  642. if (type & IRQ_TYPE_EDGE_RISING)
  643. nmk_chip->edge_rising |= bitmask;
  644. nmk_chip->edge_falling &= ~bitmask;
  645. if (type & IRQ_TYPE_EDGE_FALLING)
  646. nmk_chip->edge_falling |= bitmask;
  647. if (enabled)
  648. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, NORMAL, true);
  649. if (enabled || wake)
  650. __nmk_gpio_irq_modify(nmk_chip, d->hwirq, WAKE, true);
  651. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  652. clk_disable(nmk_chip->clk);
  653. return 0;
  654. }
  655. static unsigned int nmk_gpio_irq_startup(struct irq_data *d)
  656. {
  657. struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
  658. clk_enable(nmk_chip->clk);
  659. nmk_gpio_irq_unmask(d);
  660. return 0;
  661. }
  662. static void nmk_gpio_irq_shutdown(struct irq_data *d)
  663. {
  664. struct nmk_gpio_chip *nmk_chip = irq_data_get_irq_chip_data(d);
  665. nmk_gpio_irq_mask(d);
  666. clk_disable(nmk_chip->clk);
  667. }
  668. static struct irq_chip nmk_gpio_irq_chip = {
  669. .name = "Nomadik-GPIO",
  670. .irq_ack = nmk_gpio_irq_ack,
  671. .irq_mask = nmk_gpio_irq_mask,
  672. .irq_unmask = nmk_gpio_irq_unmask,
  673. .irq_set_type = nmk_gpio_irq_set_type,
  674. .irq_set_wake = nmk_gpio_irq_set_wake,
  675. .irq_startup = nmk_gpio_irq_startup,
  676. .irq_shutdown = nmk_gpio_irq_shutdown,
  677. };
  678. static void __nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc,
  679. u32 status)
  680. {
  681. struct nmk_gpio_chip *nmk_chip;
  682. struct irq_chip *host_chip = irq_get_chip(irq);
  683. unsigned int first_irq;
  684. chained_irq_enter(host_chip, desc);
  685. nmk_chip = irq_get_handler_data(irq);
  686. first_irq = nmk_chip->domain->revmap_data.legacy.first_irq;
  687. while (status) {
  688. int bit = __ffs(status);
  689. generic_handle_irq(first_irq + bit);
  690. status &= ~BIT(bit);
  691. }
  692. chained_irq_exit(host_chip, desc);
  693. }
  694. static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  695. {
  696. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  697. u32 status;
  698. clk_enable(nmk_chip->clk);
  699. status = readl(nmk_chip->addr + NMK_GPIO_IS);
  700. clk_disable(nmk_chip->clk);
  701. __nmk_gpio_irq_handler(irq, desc, status);
  702. }
  703. static void nmk_gpio_secondary_irq_handler(unsigned int irq,
  704. struct irq_desc *desc)
  705. {
  706. struct nmk_gpio_chip *nmk_chip = irq_get_handler_data(irq);
  707. u32 status = nmk_chip->get_secondary_status(nmk_chip->bank);
  708. __nmk_gpio_irq_handler(irq, desc, status);
  709. }
  710. static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
  711. {
  712. irq_set_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
  713. irq_set_handler_data(nmk_chip->parent_irq, nmk_chip);
  714. if (nmk_chip->secondary_parent_irq >= 0) {
  715. irq_set_chained_handler(nmk_chip->secondary_parent_irq,
  716. nmk_gpio_secondary_irq_handler);
  717. irq_set_handler_data(nmk_chip->secondary_parent_irq, nmk_chip);
  718. }
  719. return 0;
  720. }
  721. /* I/O Functions */
  722. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
  723. {
  724. struct nmk_gpio_chip *nmk_chip =
  725. container_of(chip, struct nmk_gpio_chip, chip);
  726. clk_enable(nmk_chip->clk);
  727. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  728. clk_disable(nmk_chip->clk);
  729. return 0;
  730. }
  731. static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
  732. {
  733. struct nmk_gpio_chip *nmk_chip =
  734. container_of(chip, struct nmk_gpio_chip, chip);
  735. u32 bit = 1 << offset;
  736. int value;
  737. clk_enable(nmk_chip->clk);
  738. value = (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  739. clk_disable(nmk_chip->clk);
  740. return value;
  741. }
  742. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  743. int val)
  744. {
  745. struct nmk_gpio_chip *nmk_chip =
  746. container_of(chip, struct nmk_gpio_chip, chip);
  747. clk_enable(nmk_chip->clk);
  748. __nmk_gpio_set_output(nmk_chip, offset, val);
  749. clk_disable(nmk_chip->clk);
  750. }
  751. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
  752. int val)
  753. {
  754. struct nmk_gpio_chip *nmk_chip =
  755. container_of(chip, struct nmk_gpio_chip, chip);
  756. clk_enable(nmk_chip->clk);
  757. __nmk_gpio_make_output(nmk_chip, offset, val);
  758. clk_disable(nmk_chip->clk);
  759. return 0;
  760. }
  761. static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  762. {
  763. struct nmk_gpio_chip *nmk_chip =
  764. container_of(chip, struct nmk_gpio_chip, chip);
  765. return irq_find_mapping(nmk_chip->domain, offset);
  766. }
  767. #ifdef CONFIG_DEBUG_FS
  768. #include <linux/seq_file.h>
  769. static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  770. {
  771. int mode;
  772. unsigned i;
  773. unsigned gpio = chip->base;
  774. int is_out;
  775. struct nmk_gpio_chip *nmk_chip =
  776. container_of(chip, struct nmk_gpio_chip, chip);
  777. const char *modes[] = {
  778. [NMK_GPIO_ALT_GPIO] = "gpio",
  779. [NMK_GPIO_ALT_A] = "altA",
  780. [NMK_GPIO_ALT_B] = "altB",
  781. [NMK_GPIO_ALT_C] = "altC",
  782. };
  783. clk_enable(nmk_chip->clk);
  784. for (i = 0; i < chip->ngpio; i++, gpio++) {
  785. const char *label = gpiochip_is_requested(chip, i);
  786. bool pull;
  787. u32 bit = 1 << i;
  788. is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
  789. pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
  790. mode = nmk_gpio_get_mode(gpio);
  791. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
  792. gpio, label ?: "(none)",
  793. is_out ? "out" : "in ",
  794. chip->get
  795. ? (chip->get(chip, i) ? "hi" : "lo")
  796. : "? ",
  797. (mode < 0) ? "unknown" : modes[mode],
  798. pull ? "pull" : "none");
  799. if (label && !is_out) {
  800. int irq = gpio_to_irq(gpio);
  801. struct irq_desc *desc = irq_to_desc(irq);
  802. /* This races with request_irq(), set_irq_type(),
  803. * and set_irq_wake() ... but those are "rare".
  804. */
  805. if (irq >= 0 && desc->action) {
  806. char *trigger;
  807. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  808. if (nmk_chip->edge_rising & bitmask)
  809. trigger = "edge-rising";
  810. else if (nmk_chip->edge_falling & bitmask)
  811. trigger = "edge-falling";
  812. else
  813. trigger = "edge-undefined";
  814. seq_printf(s, " irq-%d %s%s",
  815. irq, trigger,
  816. irqd_is_wakeup_set(&desc->irq_data)
  817. ? " wakeup" : "");
  818. }
  819. }
  820. seq_printf(s, "\n");
  821. }
  822. clk_disable(nmk_chip->clk);
  823. }
  824. #else
  825. #define nmk_gpio_dbg_show NULL
  826. #endif
  827. /* This structure is replicated for each GPIO block allocated at probe time */
  828. static struct gpio_chip nmk_gpio_template = {
  829. .direction_input = nmk_gpio_make_input,
  830. .get = nmk_gpio_get_input,
  831. .direction_output = nmk_gpio_make_output,
  832. .set = nmk_gpio_set_output,
  833. .to_irq = nmk_gpio_to_irq,
  834. .dbg_show = nmk_gpio_dbg_show,
  835. .can_sleep = 0,
  836. };
  837. void nmk_gpio_clocks_enable(void)
  838. {
  839. int i;
  840. for (i = 0; i < NUM_BANKS; i++) {
  841. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  842. if (!chip)
  843. continue;
  844. clk_enable(chip->clk);
  845. }
  846. }
  847. void nmk_gpio_clocks_disable(void)
  848. {
  849. int i;
  850. for (i = 0; i < NUM_BANKS; i++) {
  851. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  852. if (!chip)
  853. continue;
  854. clk_disable(chip->clk);
  855. }
  856. }
  857. /*
  858. * Called from the suspend/resume path to only keep the real wakeup interrupts
  859. * (those that have had set_irq_wake() called on them) as wakeup interrupts,
  860. * and not the rest of the interrupts which we needed to have as wakeups for
  861. * cpuidle.
  862. *
  863. * PM ops are not used since this needs to be done at the end, after all the
  864. * other drivers are done with their suspend callbacks.
  865. */
  866. void nmk_gpio_wakeups_suspend(void)
  867. {
  868. int i;
  869. for (i = 0; i < NUM_BANKS; i++) {
  870. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  871. if (!chip)
  872. break;
  873. clk_enable(chip->clk);
  874. writel(chip->rwimsc & chip->real_wake,
  875. chip->addr + NMK_GPIO_RWIMSC);
  876. writel(chip->fwimsc & chip->real_wake,
  877. chip->addr + NMK_GPIO_FWIMSC);
  878. clk_disable(chip->clk);
  879. }
  880. }
  881. void nmk_gpio_wakeups_resume(void)
  882. {
  883. int i;
  884. for (i = 0; i < NUM_BANKS; i++) {
  885. struct nmk_gpio_chip *chip = nmk_gpio_chips[i];
  886. if (!chip)
  887. break;
  888. clk_enable(chip->clk);
  889. writel(chip->rwimsc, chip->addr + NMK_GPIO_RWIMSC);
  890. writel(chip->fwimsc, chip->addr + NMK_GPIO_FWIMSC);
  891. clk_disable(chip->clk);
  892. }
  893. }
  894. /*
  895. * Read the pull up/pull down status.
  896. * A bit set in 'pull_up' means that pull up
  897. * is selected if pull is enabled in PDIS register.
  898. * Note: only pull up/down set via this driver can
  899. * be detected due to HW limitations.
  900. */
  901. void nmk_gpio_read_pull(int gpio_bank, u32 *pull_up)
  902. {
  903. if (gpio_bank < NUM_BANKS) {
  904. struct nmk_gpio_chip *chip = nmk_gpio_chips[gpio_bank];
  905. if (!chip)
  906. return;
  907. *pull_up = chip->pull_up;
  908. }
  909. }
  910. int nmk_gpio_irq_map(struct irq_domain *d, unsigned int irq,
  911. irq_hw_number_t hwirq)
  912. {
  913. struct nmk_gpio_chip *nmk_chip = d->host_data;
  914. if (!nmk_chip)
  915. return -EINVAL;
  916. irq_set_chip_and_handler(irq, &nmk_gpio_irq_chip, handle_edge_irq);
  917. set_irq_flags(irq, IRQF_VALID);
  918. irq_set_chip_data(irq, nmk_chip);
  919. irq_set_irq_type(irq, IRQ_TYPE_EDGE_FALLING);
  920. return 0;
  921. }
  922. const struct irq_domain_ops nmk_gpio_irq_simple_ops = {
  923. .map = nmk_gpio_irq_map,
  924. .xlate = irq_domain_xlate_twocell,
  925. };
  926. static int __devinit nmk_gpio_probe(struct platform_device *dev)
  927. {
  928. struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
  929. struct device_node *np = dev->dev.of_node;
  930. struct nmk_gpio_chip *nmk_chip;
  931. struct gpio_chip *chip;
  932. struct resource *res;
  933. struct clk *clk;
  934. int secondary_irq;
  935. void __iomem *base;
  936. int irq;
  937. int ret;
  938. if (!pdata && !np) {
  939. dev_err(&dev->dev, "No platform data or device tree found\n");
  940. return -ENODEV;
  941. }
  942. if (np) {
  943. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  944. if (!pdata)
  945. return -ENOMEM;
  946. if (of_get_property(np, "supports-sleepmode", NULL))
  947. pdata->supports_sleepmode = true;
  948. if (of_property_read_u32(np, "gpio-bank", &dev->id)) {
  949. dev_err(&dev->dev, "gpio-bank property not found\n");
  950. ret = -EINVAL;
  951. goto out;
  952. }
  953. pdata->first_gpio = dev->id * NMK_GPIO_PER_CHIP;
  954. pdata->num_gpio = NMK_GPIO_PER_CHIP;
  955. }
  956. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  957. if (!res) {
  958. ret = -ENOENT;
  959. goto out;
  960. }
  961. irq = platform_get_irq(dev, 0);
  962. if (irq < 0) {
  963. ret = irq;
  964. goto out;
  965. }
  966. secondary_irq = platform_get_irq(dev, 1);
  967. if (secondary_irq >= 0 && !pdata->get_secondary_status) {
  968. ret = -EINVAL;
  969. goto out;
  970. }
  971. if (request_mem_region(res->start, resource_size(res),
  972. dev_name(&dev->dev)) == NULL) {
  973. ret = -EBUSY;
  974. goto out;
  975. }
  976. base = ioremap(res->start, resource_size(res));
  977. if (!base) {
  978. ret = -ENOMEM;
  979. goto out_release;
  980. }
  981. clk = clk_get(&dev->dev, NULL);
  982. if (IS_ERR(clk)) {
  983. ret = PTR_ERR(clk);
  984. goto out_unmap;
  985. }
  986. nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
  987. if (!nmk_chip) {
  988. ret = -ENOMEM;
  989. goto out_clk;
  990. }
  991. /*
  992. * The virt address in nmk_chip->addr is in the nomadik register space,
  993. * so we can simply convert the resource address, without remapping
  994. */
  995. nmk_chip->bank = dev->id;
  996. nmk_chip->clk = clk;
  997. nmk_chip->addr = base;
  998. nmk_chip->chip = nmk_gpio_template;
  999. nmk_chip->parent_irq = irq;
  1000. nmk_chip->secondary_parent_irq = secondary_irq;
  1001. nmk_chip->get_secondary_status = pdata->get_secondary_status;
  1002. nmk_chip->set_ioforce = pdata->set_ioforce;
  1003. nmk_chip->sleepmode = pdata->supports_sleepmode;
  1004. spin_lock_init(&nmk_chip->lock);
  1005. chip = &nmk_chip->chip;
  1006. chip->base = pdata->first_gpio;
  1007. chip->ngpio = pdata->num_gpio;
  1008. chip->label = pdata->name ?: dev_name(&dev->dev);
  1009. chip->dev = &dev->dev;
  1010. chip->owner = THIS_MODULE;
  1011. clk_enable(nmk_chip->clk);
  1012. nmk_chip->lowemi = readl_relaxed(nmk_chip->addr + NMK_GPIO_LOWEMI);
  1013. clk_disable(nmk_chip->clk);
  1014. #ifdef CONFIG_OF_GPIO
  1015. chip->of_node = np;
  1016. #endif
  1017. ret = gpiochip_add(&nmk_chip->chip);
  1018. if (ret)
  1019. goto out_free;
  1020. BUG_ON(nmk_chip->bank >= ARRAY_SIZE(nmk_gpio_chips));
  1021. nmk_gpio_chips[nmk_chip->bank] = nmk_chip;
  1022. platform_set_drvdata(dev, nmk_chip);
  1023. nmk_chip->domain = irq_domain_add_legacy(np, NMK_GPIO_PER_CHIP,
  1024. NOMADIK_GPIO_TO_IRQ(pdata->first_gpio),
  1025. 0, &nmk_gpio_irq_simple_ops, nmk_chip);
  1026. if (!nmk_chip->domain) {
  1027. pr_err("%s: Failed to create irqdomain\n", np->full_name);
  1028. ret = -ENOSYS;
  1029. goto out_free;
  1030. }
  1031. nmk_gpio_init_irq(nmk_chip);
  1032. dev_info(&dev->dev, "at address %p\n", nmk_chip->addr);
  1033. return 0;
  1034. out_free:
  1035. kfree(nmk_chip);
  1036. out_clk:
  1037. clk_disable(clk);
  1038. clk_put(clk);
  1039. out_unmap:
  1040. iounmap(base);
  1041. out_release:
  1042. release_mem_region(res->start, resource_size(res));
  1043. out:
  1044. dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
  1045. pdata->first_gpio, pdata->first_gpio+31);
  1046. if (np)
  1047. kfree(pdata);
  1048. return ret;
  1049. }
  1050. static int nmk_get_groups_cnt(struct pinctrl_dev *pctldev)
  1051. {
  1052. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1053. return npct->soc->ngroups;
  1054. }
  1055. static const char *nmk_get_group_name(struct pinctrl_dev *pctldev,
  1056. unsigned selector)
  1057. {
  1058. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1059. return npct->soc->groups[selector].name;
  1060. }
  1061. static int nmk_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  1062. const unsigned **pins,
  1063. unsigned *num_pins)
  1064. {
  1065. struct nmk_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  1066. *pins = npct->soc->groups[selector].pins;
  1067. *num_pins = npct->soc->groups[selector].npins;
  1068. return 0;
  1069. }
  1070. static void nmk_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  1071. unsigned offset)
  1072. {
  1073. seq_printf(s, " Nomadik GPIO");
  1074. }
  1075. static struct pinctrl_ops nmk_pinctrl_ops = {
  1076. .get_groups_count = nmk_get_groups_cnt,
  1077. .get_group_name = nmk_get_group_name,
  1078. .get_group_pins = nmk_get_group_pins,
  1079. .pin_dbg_show = nmk_pin_dbg_show,
  1080. };
  1081. static struct pinctrl_desc nmk_pinctrl_desc = {
  1082. .name = "pinctrl-nomadik",
  1083. .pctlops = &nmk_pinctrl_ops,
  1084. .owner = THIS_MODULE,
  1085. };
  1086. static int __devinit nmk_pinctrl_probe(struct platform_device *pdev)
  1087. {
  1088. const struct platform_device_id *platid = platform_get_device_id(pdev);
  1089. struct nmk_pinctrl *npct;
  1090. int i;
  1091. npct = devm_kzalloc(&pdev->dev, sizeof(*npct), GFP_KERNEL);
  1092. if (!npct)
  1093. return -ENOMEM;
  1094. /* Poke in other ASIC variants here */
  1095. if (platid->driver_data == PINCTRL_NMK_DB8500)
  1096. nmk_pinctrl_db8500_init(&npct->soc);
  1097. /*
  1098. * We need all the GPIO drivers to probe FIRST, or we will not be able
  1099. * to obtain references to the struct gpio_chip * for them, and we
  1100. * need this to proceed.
  1101. */
  1102. for (i = 0; i < npct->soc->gpio_num_ranges; i++) {
  1103. if (!nmk_gpio_chips[i]) {
  1104. dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
  1105. devm_kfree(&pdev->dev, npct);
  1106. return -EPROBE_DEFER;
  1107. }
  1108. npct->soc->gpio_ranges[i].gc = &nmk_gpio_chips[i]->chip;
  1109. }
  1110. nmk_pinctrl_desc.pins = npct->soc->pins;
  1111. nmk_pinctrl_desc.npins = npct->soc->npins;
  1112. npct->dev = &pdev->dev;
  1113. npct->pctl = pinctrl_register(&nmk_pinctrl_desc, &pdev->dev, npct);
  1114. if (!npct->pctl) {
  1115. dev_err(&pdev->dev, "could not register Nomadik pinctrl driver\n");
  1116. return -EINVAL;
  1117. }
  1118. /* We will handle a range of GPIO pins */
  1119. for (i = 0; i < npct->soc->gpio_num_ranges; i++)
  1120. pinctrl_add_gpio_range(npct->pctl, &npct->soc->gpio_ranges[i]);
  1121. platform_set_drvdata(pdev, npct);
  1122. dev_info(&pdev->dev, "initialized Nomadik pin control driver\n");
  1123. return 0;
  1124. }
  1125. static const struct of_device_id nmk_gpio_match[] = {
  1126. { .compatible = "st,nomadik-gpio", },
  1127. {}
  1128. };
  1129. static struct platform_driver nmk_gpio_driver = {
  1130. .driver = {
  1131. .owner = THIS_MODULE,
  1132. .name = "gpio",
  1133. .of_match_table = nmk_gpio_match,
  1134. },
  1135. .probe = nmk_gpio_probe,
  1136. };
  1137. static const struct platform_device_id nmk_pinctrl_id[] = {
  1138. { "pinctrl-stn8815", PINCTRL_NMK_STN8815 },
  1139. { "pinctrl-db8500", PINCTRL_NMK_DB8500 },
  1140. };
  1141. static struct platform_driver nmk_pinctrl_driver = {
  1142. .driver = {
  1143. .owner = THIS_MODULE,
  1144. .name = "pinctrl-nomadik",
  1145. },
  1146. .probe = nmk_pinctrl_probe,
  1147. .id_table = nmk_pinctrl_id,
  1148. };
  1149. static int __init nmk_gpio_init(void)
  1150. {
  1151. int ret;
  1152. ret = platform_driver_register(&nmk_gpio_driver);
  1153. if (ret)
  1154. return ret;
  1155. return platform_driver_register(&nmk_pinctrl_driver);
  1156. }
  1157. core_initcall(nmk_gpio_init);
  1158. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  1159. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  1160. MODULE_LICENSE("GPL");