gpio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/io.h>
  18. #include <linux/clk.h>
  19. #include <linux/err.h>
  20. #include <linux/gpio.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/slab.h>
  25. #include <plat/pincfg.h>
  26. #include <mach/hardware.h>
  27. #include <mach/gpio.h>
  28. /*
  29. * The GPIO module in the Nomadik family of Systems-on-Chip is an
  30. * AMBA device, managing 32 pins and alternate functions. The logic block
  31. * is currently only used in the Nomadik.
  32. *
  33. * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
  34. */
  35. struct nmk_gpio_chip {
  36. struct gpio_chip chip;
  37. void __iomem *addr;
  38. struct clk *clk;
  39. unsigned int parent_irq;
  40. spinlock_t lock;
  41. /* Keep track of configured edges */
  42. u32 edge_rising;
  43. u32 edge_falling;
  44. };
  45. static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
  46. unsigned offset, int gpio_mode)
  47. {
  48. u32 bit = 1 << offset;
  49. u32 afunc, bfunc;
  50. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
  51. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
  52. if (gpio_mode & NMK_GPIO_ALT_A)
  53. afunc |= bit;
  54. if (gpio_mode & NMK_GPIO_ALT_B)
  55. bfunc |= bit;
  56. writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
  57. writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
  58. }
  59. static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
  60. unsigned offset, enum nmk_gpio_slpm mode)
  61. {
  62. u32 bit = 1 << offset;
  63. u32 slpm;
  64. slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
  65. if (mode == NMK_GPIO_SLPM_NOCHANGE)
  66. slpm |= bit;
  67. else
  68. slpm &= ~bit;
  69. writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
  70. }
  71. static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
  72. unsigned offset, enum nmk_gpio_pull pull)
  73. {
  74. u32 bit = 1 << offset;
  75. u32 pdis;
  76. pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
  77. if (pull == NMK_GPIO_PULL_NONE)
  78. pdis |= bit;
  79. else
  80. pdis &= ~bit;
  81. writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
  82. if (pull == NMK_GPIO_PULL_UP)
  83. writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
  84. else if (pull == NMK_GPIO_PULL_DOWN)
  85. writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
  86. }
  87. static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
  88. unsigned offset)
  89. {
  90. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  91. }
  92. static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
  93. unsigned offset, int val)
  94. {
  95. if (val)
  96. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
  97. else
  98. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
  99. }
  100. static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
  101. unsigned offset, int val)
  102. {
  103. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
  104. __nmk_gpio_set_output(nmk_chip, offset, val);
  105. }
  106. static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
  107. pin_cfg_t cfg, bool sleep)
  108. {
  109. static const char *afnames[] = {
  110. [NMK_GPIO_ALT_GPIO] = "GPIO",
  111. [NMK_GPIO_ALT_A] = "A",
  112. [NMK_GPIO_ALT_B] = "B",
  113. [NMK_GPIO_ALT_C] = "C"
  114. };
  115. static const char *pullnames[] = {
  116. [NMK_GPIO_PULL_NONE] = "none",
  117. [NMK_GPIO_PULL_UP] = "up",
  118. [NMK_GPIO_PULL_DOWN] = "down",
  119. [3] /* illegal */ = "??"
  120. };
  121. static const char *slpmnames[] = {
  122. [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
  123. [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
  124. };
  125. int pin = PIN_NUM(cfg);
  126. int pull = PIN_PULL(cfg);
  127. int af = PIN_ALT(cfg);
  128. int slpm = PIN_SLPM(cfg);
  129. int output = PIN_DIR(cfg);
  130. int val = PIN_VAL(cfg);
  131. dev_dbg(nmk_chip->chip.dev, "pin %d [%#lx]: af %s, pull %s, slpm %s (%s%s)\n",
  132. pin, cfg, afnames[af], pullnames[pull], slpmnames[slpm],
  133. output ? "output " : "input",
  134. output ? (val ? "high" : "low") : "");
  135. if (sleep) {
  136. int slpm_pull = PIN_SLPM_PULL(cfg);
  137. int slpm_output = PIN_SLPM_DIR(cfg);
  138. int slpm_val = PIN_SLPM_VAL(cfg);
  139. /*
  140. * The SLPM_* values are normal values + 1 to allow zero to
  141. * mean "same as normal".
  142. */
  143. if (slpm_pull)
  144. pull = slpm_pull - 1;
  145. if (slpm_output)
  146. output = slpm_output - 1;
  147. if (slpm_val)
  148. val = slpm_val - 1;
  149. dev_dbg(nmk_chip->chip.dev, "pin %d: sleep pull %s, dir %s, val %s\n",
  150. pin,
  151. slpm_pull ? pullnames[pull] : "same",
  152. slpm_output ? (output ? "output" : "input") : "same",
  153. slpm_val ? (val ? "high" : "low") : "same");
  154. }
  155. if (output)
  156. __nmk_gpio_make_output(nmk_chip, offset, val);
  157. else {
  158. __nmk_gpio_make_input(nmk_chip, offset);
  159. __nmk_gpio_set_pull(nmk_chip, offset, pull);
  160. }
  161. __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
  162. __nmk_gpio_set_mode(nmk_chip, offset, af);
  163. }
  164. /**
  165. * nmk_config_pin - configure a pin's mux attributes
  166. * @cfg: pin confguration
  167. *
  168. * Configures a pin's mode (alternate function or GPIO), its pull up status,
  169. * and its sleep mode based on the specified configuration. The @cfg is
  170. * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
  171. * are constructed using, and can be further enhanced with, the macros in
  172. * plat/pincfg.h.
  173. *
  174. * If a pin's mode is set to GPIO, it is configured as an input to avoid
  175. * side-effects. The gpio can be manipulated later using standard GPIO API
  176. * calls.
  177. */
  178. int nmk_config_pin(pin_cfg_t cfg, bool sleep)
  179. {
  180. struct nmk_gpio_chip *nmk_chip;
  181. int gpio = PIN_NUM(cfg);
  182. unsigned long flags;
  183. nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  184. if (!nmk_chip)
  185. return -EINVAL;
  186. spin_lock_irqsave(&nmk_chip->lock, flags);
  187. __nmk_config_pin(nmk_chip, gpio - nmk_chip->chip.base, cfg, sleep);
  188. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  189. return 0;
  190. }
  191. EXPORT_SYMBOL(nmk_config_pin);
  192. /**
  193. * nmk_config_pins - configure several pins at once
  194. * @cfgs: array of pin configurations
  195. * @num: number of elments in the array
  196. *
  197. * Configures several pins using nmk_config_pin(). Refer to that function for
  198. * further information.
  199. */
  200. int nmk_config_pins(pin_cfg_t *cfgs, int num)
  201. {
  202. int ret = 0;
  203. int i;
  204. for (i = 0; i < num; i++) {
  205. ret = nmk_config_pin(cfgs[i], false);
  206. if (ret)
  207. break;
  208. }
  209. return ret;
  210. }
  211. EXPORT_SYMBOL(nmk_config_pins);
  212. int nmk_config_pins_sleep(pin_cfg_t *cfgs, int num)
  213. {
  214. int ret = 0;
  215. int i;
  216. for (i = 0; i < num; i++) {
  217. ret = nmk_config_pin(cfgs[i], true);
  218. if (ret)
  219. break;
  220. }
  221. return ret;
  222. }
  223. EXPORT_SYMBOL(nmk_config_pins_sleep);
  224. /**
  225. * nmk_gpio_set_slpm() - configure the sleep mode of a pin
  226. * @gpio: pin number
  227. * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
  228. *
  229. * Sets the sleep mode of a pin. If @mode is NMK_GPIO_SLPM_INPUT, the pin is
  230. * changed to an input (with pullup/down enabled) in sleep and deep sleep. If
  231. * @mode is NMK_GPIO_SLPM_NOCHANGE, the pin remains in the state it was
  232. * configured even when in sleep and deep sleep.
  233. *
  234. * On DB8500v2 onwards, this setting loses the previous meaning and instead
  235. * indicates if wakeup detection is enabled on the pin. Note that
  236. * enable_irq_wake() will automatically enable wakeup detection.
  237. */
  238. int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
  239. {
  240. struct nmk_gpio_chip *nmk_chip;
  241. unsigned long flags;
  242. nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  243. if (!nmk_chip)
  244. return -EINVAL;
  245. spin_lock_irqsave(&nmk_chip->lock, flags);
  246. __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
  247. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  248. return 0;
  249. }
  250. /**
  251. * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
  252. * @gpio: pin number
  253. * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
  254. *
  255. * Enables/disables pull up/down on a specified pin. This only takes effect if
  256. * the pin is configured as an input (either explicitly or by the alternate
  257. * function).
  258. *
  259. * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
  260. * configured as an input. Otherwise, due to the way the controller registers
  261. * work, this function will change the value output on the pin.
  262. */
  263. int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
  264. {
  265. struct nmk_gpio_chip *nmk_chip;
  266. unsigned long flags;
  267. nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  268. if (!nmk_chip)
  269. return -EINVAL;
  270. spin_lock_irqsave(&nmk_chip->lock, flags);
  271. __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
  272. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  273. return 0;
  274. }
  275. /* Mode functions */
  276. int nmk_gpio_set_mode(int gpio, int gpio_mode)
  277. {
  278. struct nmk_gpio_chip *nmk_chip;
  279. unsigned long flags;
  280. nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  281. if (!nmk_chip)
  282. return -EINVAL;
  283. spin_lock_irqsave(&nmk_chip->lock, flags);
  284. __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
  285. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  286. return 0;
  287. }
  288. EXPORT_SYMBOL(nmk_gpio_set_mode);
  289. int nmk_gpio_get_mode(int gpio)
  290. {
  291. struct nmk_gpio_chip *nmk_chip;
  292. u32 afunc, bfunc, bit;
  293. nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  294. if (!nmk_chip)
  295. return -EINVAL;
  296. bit = 1 << (gpio - nmk_chip->chip.base);
  297. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
  298. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
  299. return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
  300. }
  301. EXPORT_SYMBOL(nmk_gpio_get_mode);
  302. /* IRQ functions */
  303. static inline int nmk_gpio_get_bitmask(int gpio)
  304. {
  305. return 1 << (gpio % 32);
  306. }
  307. static void nmk_gpio_irq_ack(struct irq_data *d)
  308. {
  309. int gpio;
  310. struct nmk_gpio_chip *nmk_chip;
  311. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  312. nmk_chip = irq_data_get_irq_chip_data(d);
  313. if (!nmk_chip)
  314. return;
  315. writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
  316. }
  317. enum nmk_gpio_irq_type {
  318. NORMAL,
  319. WAKE,
  320. };
  321. static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
  322. int gpio, enum nmk_gpio_irq_type which,
  323. bool enable)
  324. {
  325. u32 rimsc = which == WAKE ? NMK_GPIO_RWIMSC : NMK_GPIO_RIMSC;
  326. u32 fimsc = which == WAKE ? NMK_GPIO_FWIMSC : NMK_GPIO_FIMSC;
  327. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  328. u32 reg;
  329. /* we must individually set/clear the two edges */
  330. if (nmk_chip->edge_rising & bitmask) {
  331. reg = readl(nmk_chip->addr + rimsc);
  332. if (enable)
  333. reg |= bitmask;
  334. else
  335. reg &= ~bitmask;
  336. writel(reg, nmk_chip->addr + rimsc);
  337. }
  338. if (nmk_chip->edge_falling & bitmask) {
  339. reg = readl(nmk_chip->addr + fimsc);
  340. if (enable)
  341. reg |= bitmask;
  342. else
  343. reg &= ~bitmask;
  344. writel(reg, nmk_chip->addr + fimsc);
  345. }
  346. }
  347. static int nmk_gpio_irq_modify(struct irq_data *d, enum nmk_gpio_irq_type which,
  348. bool enable)
  349. {
  350. int gpio;
  351. struct nmk_gpio_chip *nmk_chip;
  352. unsigned long flags;
  353. u32 bitmask;
  354. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  355. nmk_chip = irq_data_get_irq_chip_data(d);
  356. bitmask = nmk_gpio_get_bitmask(gpio);
  357. if (!nmk_chip)
  358. return -EINVAL;
  359. spin_lock_irqsave(&nmk_chip->lock, flags);
  360. __nmk_gpio_irq_modify(nmk_chip, gpio, which, enable);
  361. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  362. return 0;
  363. }
  364. static void nmk_gpio_irq_mask(struct irq_data *d)
  365. {
  366. nmk_gpio_irq_modify(d, NORMAL, false);
  367. }
  368. static void nmk_gpio_irq_unmask(struct irq_data *d)
  369. {
  370. nmk_gpio_irq_modify(d, NORMAL, true);
  371. }
  372. static int nmk_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  373. {
  374. struct nmk_gpio_chip *nmk_chip;
  375. unsigned long flags;
  376. int gpio;
  377. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  378. nmk_chip = irq_data_get_irq_chip_data(d);
  379. if (!nmk_chip)
  380. return -EINVAL;
  381. spin_lock_irqsave(&nmk_chip->lock, flags);
  382. #ifdef CONFIG_ARCH_U8500
  383. if (cpu_is_u8500v2()) {
  384. __nmk_gpio_set_slpm(nmk_chip, gpio,
  385. on ? NMK_GPIO_SLPM_WAKEUP_ENABLE
  386. : NMK_GPIO_SLPM_WAKEUP_DISABLE);
  387. }
  388. #endif
  389. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
  390. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  391. return 0;
  392. }
  393. static int nmk_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  394. {
  395. struct irq_desc *desc = irq_to_desc(d->irq);
  396. bool enabled = !(desc->status & IRQ_DISABLED);
  397. bool wake = desc->wake_depth;
  398. int gpio;
  399. struct nmk_gpio_chip *nmk_chip;
  400. unsigned long flags;
  401. u32 bitmask;
  402. gpio = NOMADIK_IRQ_TO_GPIO(d->irq);
  403. nmk_chip = irq_data_get_irq_chip_data(d);
  404. bitmask = nmk_gpio_get_bitmask(gpio);
  405. if (!nmk_chip)
  406. return -EINVAL;
  407. if (type & IRQ_TYPE_LEVEL_HIGH)
  408. return -EINVAL;
  409. if (type & IRQ_TYPE_LEVEL_LOW)
  410. return -EINVAL;
  411. spin_lock_irqsave(&nmk_chip->lock, flags);
  412. if (enabled)
  413. __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
  414. if (wake)
  415. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
  416. nmk_chip->edge_rising &= ~bitmask;
  417. if (type & IRQ_TYPE_EDGE_RISING)
  418. nmk_chip->edge_rising |= bitmask;
  419. nmk_chip->edge_falling &= ~bitmask;
  420. if (type & IRQ_TYPE_EDGE_FALLING)
  421. nmk_chip->edge_falling |= bitmask;
  422. if (enabled)
  423. __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
  424. if (wake)
  425. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
  426. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  427. return 0;
  428. }
  429. static struct irq_chip nmk_gpio_irq_chip = {
  430. .name = "Nomadik-GPIO",
  431. .irq_ack = nmk_gpio_irq_ack,
  432. .irq_mask = nmk_gpio_irq_mask,
  433. .irq_unmask = nmk_gpio_irq_unmask,
  434. .irq_set_type = nmk_gpio_irq_set_type,
  435. .irq_set_wake = nmk_gpio_irq_set_wake,
  436. };
  437. static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  438. {
  439. struct nmk_gpio_chip *nmk_chip;
  440. struct irq_chip *host_chip = get_irq_chip(irq);
  441. unsigned int gpio_irq;
  442. u32 pending;
  443. unsigned int first_irq;
  444. if (host_chip->irq_mask_ack)
  445. host_chip->irq_mask_ack(&desc->irq_data);
  446. else {
  447. host_chip->irq_mask(&desc->irq_data);
  448. if (host_chip->irq_ack)
  449. host_chip->irq_ack(&desc->irq_data);
  450. }
  451. nmk_chip = get_irq_data(irq);
  452. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  453. while ( (pending = readl(nmk_chip->addr + NMK_GPIO_IS)) ) {
  454. gpio_irq = first_irq + __ffs(pending);
  455. generic_handle_irq(gpio_irq);
  456. }
  457. host_chip->irq_unmask(&desc->irq_data);
  458. }
  459. static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
  460. {
  461. unsigned int first_irq;
  462. int i;
  463. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  464. for (i = first_irq; i < first_irq + nmk_chip->chip.ngpio; i++) {
  465. set_irq_chip(i, &nmk_gpio_irq_chip);
  466. set_irq_handler(i, handle_edge_irq);
  467. set_irq_flags(i, IRQF_VALID);
  468. set_irq_chip_data(i, nmk_chip);
  469. set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
  470. }
  471. set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
  472. set_irq_data(nmk_chip->parent_irq, nmk_chip);
  473. return 0;
  474. }
  475. /* I/O Functions */
  476. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
  477. {
  478. struct nmk_gpio_chip *nmk_chip =
  479. container_of(chip, struct nmk_gpio_chip, chip);
  480. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  481. return 0;
  482. }
  483. static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
  484. {
  485. struct nmk_gpio_chip *nmk_chip =
  486. container_of(chip, struct nmk_gpio_chip, chip);
  487. u32 bit = 1 << offset;
  488. return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  489. }
  490. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  491. int val)
  492. {
  493. struct nmk_gpio_chip *nmk_chip =
  494. container_of(chip, struct nmk_gpio_chip, chip);
  495. __nmk_gpio_set_output(nmk_chip, offset, val);
  496. }
  497. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
  498. int val)
  499. {
  500. struct nmk_gpio_chip *nmk_chip =
  501. container_of(chip, struct nmk_gpio_chip, chip);
  502. __nmk_gpio_make_output(nmk_chip, offset, val);
  503. return 0;
  504. }
  505. static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  506. {
  507. struct nmk_gpio_chip *nmk_chip =
  508. container_of(chip, struct nmk_gpio_chip, chip);
  509. return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
  510. }
  511. #ifdef CONFIG_DEBUG_FS
  512. #include <linux/seq_file.h>
  513. static void nmk_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  514. {
  515. int mode;
  516. unsigned i;
  517. unsigned gpio = chip->base;
  518. int is_out;
  519. struct nmk_gpio_chip *nmk_chip =
  520. container_of(chip, struct nmk_gpio_chip, chip);
  521. const char *modes[] = {
  522. [NMK_GPIO_ALT_GPIO] = "gpio",
  523. [NMK_GPIO_ALT_A] = "altA",
  524. [NMK_GPIO_ALT_B] = "altB",
  525. [NMK_GPIO_ALT_C] = "altC",
  526. };
  527. for (i = 0; i < chip->ngpio; i++, gpio++) {
  528. const char *label = gpiochip_is_requested(chip, i);
  529. bool pull;
  530. u32 bit = 1 << i;
  531. if (!label)
  532. continue;
  533. is_out = readl(nmk_chip->addr + NMK_GPIO_DIR) & bit;
  534. pull = !(readl(nmk_chip->addr + NMK_GPIO_PDIS) & bit);
  535. mode = nmk_gpio_get_mode(gpio);
  536. seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s %s",
  537. gpio, label,
  538. is_out ? "out" : "in ",
  539. chip->get
  540. ? (chip->get(chip, i) ? "hi" : "lo")
  541. : "? ",
  542. (mode < 0) ? "unknown" : modes[mode],
  543. pull ? "pull" : "none");
  544. if (!is_out) {
  545. int irq = gpio_to_irq(gpio);
  546. struct irq_desc *desc = irq_to_desc(irq);
  547. /* This races with request_irq(), set_irq_type(),
  548. * and set_irq_wake() ... but those are "rare".
  549. *
  550. * More significantly, trigger type flags aren't
  551. * currently maintained by genirq.
  552. */
  553. if (irq >= 0 && desc->action) {
  554. char *trigger;
  555. switch (desc->status & IRQ_TYPE_SENSE_MASK) {
  556. case IRQ_TYPE_NONE:
  557. trigger = "(default)";
  558. break;
  559. case IRQ_TYPE_EDGE_FALLING:
  560. trigger = "edge-falling";
  561. break;
  562. case IRQ_TYPE_EDGE_RISING:
  563. trigger = "edge-rising";
  564. break;
  565. case IRQ_TYPE_EDGE_BOTH:
  566. trigger = "edge-both";
  567. break;
  568. case IRQ_TYPE_LEVEL_HIGH:
  569. trigger = "level-high";
  570. break;
  571. case IRQ_TYPE_LEVEL_LOW:
  572. trigger = "level-low";
  573. break;
  574. default:
  575. trigger = "?trigger?";
  576. break;
  577. }
  578. seq_printf(s, " irq-%d %s%s",
  579. irq, trigger,
  580. (desc->status & IRQ_WAKEUP)
  581. ? " wakeup" : "");
  582. }
  583. }
  584. seq_printf(s, "\n");
  585. }
  586. }
  587. #else
  588. #define nmk_gpio_dbg_show NULL
  589. #endif
  590. /* This structure is replicated for each GPIO block allocated at probe time */
  591. static struct gpio_chip nmk_gpio_template = {
  592. .direction_input = nmk_gpio_make_input,
  593. .get = nmk_gpio_get_input,
  594. .direction_output = nmk_gpio_make_output,
  595. .set = nmk_gpio_set_output,
  596. .to_irq = nmk_gpio_to_irq,
  597. .dbg_show = nmk_gpio_dbg_show,
  598. .can_sleep = 0,
  599. };
  600. static int __devinit nmk_gpio_probe(struct platform_device *dev)
  601. {
  602. struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
  603. struct nmk_gpio_chip *nmk_chip;
  604. struct gpio_chip *chip;
  605. struct resource *res;
  606. struct clk *clk;
  607. int irq;
  608. int ret;
  609. if (!pdata)
  610. return -ENODEV;
  611. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  612. if (!res) {
  613. ret = -ENOENT;
  614. goto out;
  615. }
  616. irq = platform_get_irq(dev, 0);
  617. if (irq < 0) {
  618. ret = irq;
  619. goto out;
  620. }
  621. if (request_mem_region(res->start, resource_size(res),
  622. dev_name(&dev->dev)) == NULL) {
  623. ret = -EBUSY;
  624. goto out;
  625. }
  626. clk = clk_get(&dev->dev, NULL);
  627. if (IS_ERR(clk)) {
  628. ret = PTR_ERR(clk);
  629. goto out_release;
  630. }
  631. clk_enable(clk);
  632. nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
  633. if (!nmk_chip) {
  634. ret = -ENOMEM;
  635. goto out_clk;
  636. }
  637. /*
  638. * The virt address in nmk_chip->addr is in the nomadik register space,
  639. * so we can simply convert the resource address, without remapping
  640. */
  641. nmk_chip->clk = clk;
  642. nmk_chip->addr = io_p2v(res->start);
  643. nmk_chip->chip = nmk_gpio_template;
  644. nmk_chip->parent_irq = irq;
  645. spin_lock_init(&nmk_chip->lock);
  646. chip = &nmk_chip->chip;
  647. chip->base = pdata->first_gpio;
  648. chip->ngpio = pdata->num_gpio;
  649. chip->label = pdata->name ?: dev_name(&dev->dev);
  650. chip->dev = &dev->dev;
  651. chip->owner = THIS_MODULE;
  652. ret = gpiochip_add(&nmk_chip->chip);
  653. if (ret)
  654. goto out_free;
  655. platform_set_drvdata(dev, nmk_chip);
  656. nmk_gpio_init_irq(nmk_chip);
  657. dev_info(&dev->dev, "Bits %i-%i at address %p\n",
  658. nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
  659. return 0;
  660. out_free:
  661. kfree(nmk_chip);
  662. out_clk:
  663. clk_disable(clk);
  664. clk_put(clk);
  665. out_release:
  666. release_mem_region(res->start, resource_size(res));
  667. out:
  668. dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
  669. pdata->first_gpio, pdata->first_gpio+31);
  670. return ret;
  671. }
  672. static struct platform_driver nmk_gpio_driver = {
  673. .driver = {
  674. .owner = THIS_MODULE,
  675. .name = "gpio",
  676. },
  677. .probe = nmk_gpio_probe,
  678. .suspend = NULL, /* to be done */
  679. .resume = NULL,
  680. };
  681. static int __init nmk_gpio_init(void)
  682. {
  683. return platform_driver_register(&nmk_gpio_driver);
  684. }
  685. core_initcall(nmk_gpio_init);
  686. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  687. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  688. MODULE_LICENSE("GPL");