gpio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. #define NMK_GPIO_PER_CHIP 32
  36. struct nmk_gpio_chip {
  37. struct gpio_chip chip;
  38. void __iomem *addr;
  39. struct clk *clk;
  40. unsigned int parent_irq;
  41. spinlock_t lock;
  42. /* Keep track of configured edges */
  43. u32 edge_rising;
  44. u32 edge_falling;
  45. };
  46. static void __nmk_gpio_set_mode(struct nmk_gpio_chip *nmk_chip,
  47. unsigned offset, int gpio_mode)
  48. {
  49. u32 bit = 1 << offset;
  50. u32 afunc, bfunc;
  51. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
  52. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
  53. if (gpio_mode & NMK_GPIO_ALT_A)
  54. afunc |= bit;
  55. if (gpio_mode & NMK_GPIO_ALT_B)
  56. bfunc |= bit;
  57. writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
  58. writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
  59. }
  60. static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
  61. unsigned offset, enum nmk_gpio_slpm mode)
  62. {
  63. u32 bit = 1 << offset;
  64. u32 slpm;
  65. slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
  66. if (mode == NMK_GPIO_SLPM_NOCHANGE)
  67. slpm |= bit;
  68. else
  69. slpm &= ~bit;
  70. writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
  71. }
  72. static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
  73. unsigned offset, enum nmk_gpio_pull pull)
  74. {
  75. u32 bit = 1 << offset;
  76. u32 pdis;
  77. pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
  78. if (pull == NMK_GPIO_PULL_NONE)
  79. pdis |= bit;
  80. else
  81. pdis &= ~bit;
  82. writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
  83. if (pull == NMK_GPIO_PULL_UP)
  84. writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
  85. else if (pull == NMK_GPIO_PULL_DOWN)
  86. writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
  87. }
  88. static void __nmk_gpio_make_input(struct nmk_gpio_chip *nmk_chip,
  89. unsigned offset)
  90. {
  91. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  92. }
  93. static void __nmk_gpio_set_output(struct nmk_gpio_chip *nmk_chip,
  94. unsigned offset, int val)
  95. {
  96. if (val)
  97. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATS);
  98. else
  99. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DATC);
  100. }
  101. static void __nmk_gpio_make_output(struct nmk_gpio_chip *nmk_chip,
  102. unsigned offset, int val)
  103. {
  104. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
  105. __nmk_gpio_set_output(nmk_chip, offset, val);
  106. }
  107. static void __nmk_config_pin(struct nmk_gpio_chip *nmk_chip, unsigned offset,
  108. pin_cfg_t cfg)
  109. {
  110. static const char *afnames[] = {
  111. [NMK_GPIO_ALT_GPIO] = "GPIO",
  112. [NMK_GPIO_ALT_A] = "A",
  113. [NMK_GPIO_ALT_B] = "B",
  114. [NMK_GPIO_ALT_C] = "C"
  115. };
  116. static const char *pullnames[] = {
  117. [NMK_GPIO_PULL_NONE] = "none",
  118. [NMK_GPIO_PULL_UP] = "up",
  119. [NMK_GPIO_PULL_DOWN] = "down",
  120. [3] /* illegal */ = "??"
  121. };
  122. static const char *slpmnames[] = {
  123. [NMK_GPIO_SLPM_INPUT] = "input/wakeup",
  124. [NMK_GPIO_SLPM_NOCHANGE] = "no-change/no-wakeup",
  125. };
  126. int pin = PIN_NUM(cfg);
  127. int pull = PIN_PULL(cfg);
  128. int af = PIN_ALT(cfg);
  129. int slpm = PIN_SLPM(cfg);
  130. int output = PIN_DIR(cfg);
  131. int val = PIN_VAL(cfg);
  132. dev_dbg(nmk_chip->chip.dev, "pin %d: af %s, pull %s, slpm %s (%s%s)\n",
  133. pin, afnames[af], pullnames[pull], slpmnames[slpm],
  134. output ? "output " : "input",
  135. output ? (val ? "high" : "low") : "");
  136. if (output)
  137. __nmk_gpio_make_output(nmk_chip, offset, val);
  138. else {
  139. __nmk_gpio_make_input(nmk_chip, offset);
  140. __nmk_gpio_set_pull(nmk_chip, offset, pull);
  141. }
  142. __nmk_gpio_set_slpm(nmk_chip, offset, slpm);
  143. __nmk_gpio_set_mode(nmk_chip, offset, af);
  144. }
  145. /**
  146. * nmk_config_pin - configure a pin's mux attributes
  147. * @cfg: pin confguration
  148. *
  149. * Configures a pin's mode (alternate function or GPIO), its pull up status,
  150. * and its sleep mode based on the specified configuration. The @cfg is
  151. * usually one of the SoC specific macros defined in mach/<soc>-pins.h. These
  152. * are constructed using, and can be further enhanced with, the macros in
  153. * plat/pincfg.h.
  154. *
  155. * If a pin's mode is set to GPIO, it is configured as an input to avoid
  156. * side-effects. The gpio can be manipulated later using standard GPIO API
  157. * calls.
  158. */
  159. int nmk_config_pin(pin_cfg_t cfg)
  160. {
  161. struct nmk_gpio_chip *nmk_chip;
  162. int gpio = PIN_NUM(cfg);
  163. unsigned long flags;
  164. nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  165. if (!nmk_chip)
  166. return -EINVAL;
  167. spin_lock_irqsave(&nmk_chip->lock, flags);
  168. __nmk_config_pin(nmk_chip, gpio - nmk_chip->chip.base, cfg);
  169. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  170. return 0;
  171. }
  172. EXPORT_SYMBOL(nmk_config_pin);
  173. /**
  174. * nmk_config_pins - configure several pins at once
  175. * @cfgs: array of pin configurations
  176. * @num: number of elments in the array
  177. *
  178. * Configures several pins using nmk_config_pin(). Refer to that function for
  179. * further information.
  180. */
  181. int nmk_config_pins(pin_cfg_t *cfgs, int num)
  182. {
  183. int ret = 0;
  184. int i;
  185. for (i = 0; i < num; i++) {
  186. int ret = nmk_config_pin(cfgs[i]);
  187. if (ret)
  188. break;
  189. }
  190. return ret;
  191. }
  192. EXPORT_SYMBOL(nmk_config_pins);
  193. /**
  194. * nmk_gpio_set_slpm() - configure the sleep mode of a pin
  195. * @gpio: pin number
  196. * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
  197. *
  198. * Sets the sleep mode of a pin. If @mode is NMK_GPIO_SLPM_INPUT, the pin is
  199. * changed to an input (with pullup/down enabled) in sleep and deep sleep. If
  200. * @mode is NMK_GPIO_SLPM_NOCHANGE, the pin remains in the state it was
  201. * configured even when in sleep and deep sleep.
  202. *
  203. * On DB8500v2 onwards, this setting loses the previous meaning and instead
  204. * indicates if wakeup detection is enabled on the pin. Note that
  205. * enable_irq_wake() will automatically enable wakeup detection.
  206. */
  207. int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
  208. {
  209. struct nmk_gpio_chip *nmk_chip;
  210. unsigned long flags;
  211. nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  212. if (!nmk_chip)
  213. return -EINVAL;
  214. spin_lock_irqsave(&nmk_chip->lock, flags);
  215. __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
  216. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  217. return 0;
  218. }
  219. /**
  220. * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
  221. * @gpio: pin number
  222. * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
  223. *
  224. * Enables/disables pull up/down on a specified pin. This only takes effect if
  225. * the pin is configured as an input (either explicitly or by the alternate
  226. * function).
  227. *
  228. * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
  229. * configured as an input. Otherwise, due to the way the controller registers
  230. * work, this function will change the value output on the pin.
  231. */
  232. int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
  233. {
  234. struct nmk_gpio_chip *nmk_chip;
  235. unsigned long flags;
  236. nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  237. if (!nmk_chip)
  238. return -EINVAL;
  239. spin_lock_irqsave(&nmk_chip->lock, flags);
  240. __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
  241. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  242. return 0;
  243. }
  244. /* Mode functions */
  245. int nmk_gpio_set_mode(int gpio, int gpio_mode)
  246. {
  247. struct nmk_gpio_chip *nmk_chip;
  248. unsigned long flags;
  249. nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  250. if (!nmk_chip)
  251. return -EINVAL;
  252. spin_lock_irqsave(&nmk_chip->lock, flags);
  253. __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
  254. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  255. return 0;
  256. }
  257. EXPORT_SYMBOL(nmk_gpio_set_mode);
  258. int nmk_gpio_get_mode(int gpio)
  259. {
  260. struct nmk_gpio_chip *nmk_chip;
  261. u32 afunc, bfunc, bit;
  262. nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
  263. if (!nmk_chip)
  264. return -EINVAL;
  265. bit = 1 << (gpio - nmk_chip->chip.base);
  266. afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
  267. bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
  268. return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
  269. }
  270. EXPORT_SYMBOL(nmk_gpio_get_mode);
  271. /* IRQ functions */
  272. static inline int nmk_gpio_get_bitmask(int gpio)
  273. {
  274. return 1 << (gpio % 32);
  275. }
  276. static void nmk_gpio_irq_ack(unsigned int irq)
  277. {
  278. int gpio;
  279. struct nmk_gpio_chip *nmk_chip;
  280. gpio = NOMADIK_IRQ_TO_GPIO(irq);
  281. nmk_chip = get_irq_chip_data(irq);
  282. if (!nmk_chip)
  283. return;
  284. writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
  285. }
  286. enum nmk_gpio_irq_type {
  287. NORMAL,
  288. WAKE,
  289. };
  290. static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
  291. int gpio, enum nmk_gpio_irq_type which,
  292. bool enable)
  293. {
  294. u32 rimsc = which == WAKE ? NMK_GPIO_RWIMSC : NMK_GPIO_RIMSC;
  295. u32 fimsc = which == WAKE ? NMK_GPIO_FWIMSC : NMK_GPIO_FIMSC;
  296. u32 bitmask = nmk_gpio_get_bitmask(gpio);
  297. u32 reg;
  298. /* we must individually set/clear the two edges */
  299. if (nmk_chip->edge_rising & bitmask) {
  300. reg = readl(nmk_chip->addr + rimsc);
  301. if (enable)
  302. reg |= bitmask;
  303. else
  304. reg &= ~bitmask;
  305. writel(reg, nmk_chip->addr + rimsc);
  306. }
  307. if (nmk_chip->edge_falling & bitmask) {
  308. reg = readl(nmk_chip->addr + fimsc);
  309. if (enable)
  310. reg |= bitmask;
  311. else
  312. reg &= ~bitmask;
  313. writel(reg, nmk_chip->addr + fimsc);
  314. }
  315. }
  316. static int nmk_gpio_irq_modify(unsigned int irq, enum nmk_gpio_irq_type which,
  317. bool enable)
  318. {
  319. int gpio;
  320. struct nmk_gpio_chip *nmk_chip;
  321. unsigned long flags;
  322. u32 bitmask;
  323. gpio = NOMADIK_IRQ_TO_GPIO(irq);
  324. nmk_chip = get_irq_chip_data(irq);
  325. bitmask = nmk_gpio_get_bitmask(gpio);
  326. if (!nmk_chip)
  327. return -EINVAL;
  328. spin_lock_irqsave(&nmk_chip->lock, flags);
  329. __nmk_gpio_irq_modify(nmk_chip, gpio, which, enable);
  330. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  331. return 0;
  332. }
  333. static void nmk_gpio_irq_mask(unsigned int irq)
  334. {
  335. nmk_gpio_irq_modify(irq, NORMAL, false);
  336. }
  337. static void nmk_gpio_irq_unmask(unsigned int irq)
  338. {
  339. nmk_gpio_irq_modify(irq, NORMAL, true);
  340. }
  341. static int nmk_gpio_irq_set_wake(unsigned int irq, unsigned int on)
  342. {
  343. struct nmk_gpio_chip *nmk_chip;
  344. unsigned long flags;
  345. int gpio;
  346. gpio = NOMADIK_IRQ_TO_GPIO(irq);
  347. nmk_chip = get_irq_chip_data(irq);
  348. if (!nmk_chip)
  349. return -EINVAL;
  350. spin_lock_irqsave(&nmk_chip->lock, flags);
  351. #ifdef CONFIG_ARCH_U8500
  352. if (cpu_is_u8500v2()) {
  353. __nmk_gpio_set_slpm(nmk_chip, gpio,
  354. on ? NMK_GPIO_SLPM_WAKEUP_ENABLE
  355. : NMK_GPIO_SLPM_WAKEUP_DISABLE);
  356. }
  357. #endif
  358. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, on);
  359. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  360. return 0;
  361. }
  362. static int nmk_gpio_irq_set_type(unsigned int irq, unsigned int type)
  363. {
  364. struct irq_desc *desc = irq_to_desc(irq);
  365. bool enabled = !(desc->status & IRQ_DISABLED);
  366. bool wake = desc->wake_depth;
  367. int gpio;
  368. struct nmk_gpio_chip *nmk_chip;
  369. unsigned long flags;
  370. u32 bitmask;
  371. gpio = NOMADIK_IRQ_TO_GPIO(irq);
  372. nmk_chip = get_irq_chip_data(irq);
  373. bitmask = nmk_gpio_get_bitmask(gpio);
  374. if (!nmk_chip)
  375. return -EINVAL;
  376. if (type & IRQ_TYPE_LEVEL_HIGH)
  377. return -EINVAL;
  378. if (type & IRQ_TYPE_LEVEL_LOW)
  379. return -EINVAL;
  380. spin_lock_irqsave(&nmk_chip->lock, flags);
  381. if (enabled)
  382. __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, false);
  383. if (wake)
  384. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, false);
  385. nmk_chip->edge_rising &= ~bitmask;
  386. if (type & IRQ_TYPE_EDGE_RISING)
  387. nmk_chip->edge_rising |= bitmask;
  388. nmk_chip->edge_falling &= ~bitmask;
  389. if (type & IRQ_TYPE_EDGE_FALLING)
  390. nmk_chip->edge_falling |= bitmask;
  391. if (enabled)
  392. __nmk_gpio_irq_modify(nmk_chip, gpio, NORMAL, true);
  393. if (wake)
  394. __nmk_gpio_irq_modify(nmk_chip, gpio, WAKE, true);
  395. spin_unlock_irqrestore(&nmk_chip->lock, flags);
  396. return 0;
  397. }
  398. static struct irq_chip nmk_gpio_irq_chip = {
  399. .name = "Nomadik-GPIO",
  400. .ack = nmk_gpio_irq_ack,
  401. .mask = nmk_gpio_irq_mask,
  402. .unmask = nmk_gpio_irq_unmask,
  403. .set_type = nmk_gpio_irq_set_type,
  404. .set_wake = nmk_gpio_irq_set_wake,
  405. };
  406. static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
  407. {
  408. struct nmk_gpio_chip *nmk_chip;
  409. struct irq_chip *host_chip = get_irq_chip(irq);
  410. unsigned int gpio_irq;
  411. u32 pending;
  412. unsigned int first_irq;
  413. if (host_chip->mask_ack)
  414. host_chip->mask_ack(irq);
  415. else {
  416. host_chip->mask(irq);
  417. if (host_chip->ack)
  418. host_chip->ack(irq);
  419. }
  420. nmk_chip = get_irq_data(irq);
  421. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  422. while ( (pending = readl(nmk_chip->addr + NMK_GPIO_IS)) ) {
  423. gpio_irq = first_irq + __ffs(pending);
  424. generic_handle_irq(gpio_irq);
  425. }
  426. host_chip->unmask(irq);
  427. }
  428. static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
  429. {
  430. unsigned int first_irq;
  431. int i;
  432. first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
  433. for (i = first_irq; i < first_irq + NMK_GPIO_PER_CHIP; i++) {
  434. set_irq_chip(i, &nmk_gpio_irq_chip);
  435. set_irq_handler(i, handle_edge_irq);
  436. set_irq_flags(i, IRQF_VALID);
  437. set_irq_chip_data(i, nmk_chip);
  438. set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
  439. }
  440. set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
  441. set_irq_data(nmk_chip->parent_irq, nmk_chip);
  442. return 0;
  443. }
  444. /* I/O Functions */
  445. static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
  446. {
  447. struct nmk_gpio_chip *nmk_chip =
  448. container_of(chip, struct nmk_gpio_chip, chip);
  449. writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
  450. return 0;
  451. }
  452. static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
  453. {
  454. struct nmk_gpio_chip *nmk_chip =
  455. container_of(chip, struct nmk_gpio_chip, chip);
  456. u32 bit = 1 << offset;
  457. return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
  458. }
  459. static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
  460. int val)
  461. {
  462. struct nmk_gpio_chip *nmk_chip =
  463. container_of(chip, struct nmk_gpio_chip, chip);
  464. __nmk_gpio_set_output(nmk_chip, offset, val);
  465. }
  466. static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
  467. int val)
  468. {
  469. struct nmk_gpio_chip *nmk_chip =
  470. container_of(chip, struct nmk_gpio_chip, chip);
  471. __nmk_gpio_make_output(nmk_chip, offset, val);
  472. return 0;
  473. }
  474. static int nmk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  475. {
  476. struct nmk_gpio_chip *nmk_chip =
  477. container_of(chip, struct nmk_gpio_chip, chip);
  478. return NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base) + offset;
  479. }
  480. /* This structure is replicated for each GPIO block allocated at probe time */
  481. static struct gpio_chip nmk_gpio_template = {
  482. .direction_input = nmk_gpio_make_input,
  483. .get = nmk_gpio_get_input,
  484. .direction_output = nmk_gpio_make_output,
  485. .set = nmk_gpio_set_output,
  486. .to_irq = nmk_gpio_to_irq,
  487. .ngpio = NMK_GPIO_PER_CHIP,
  488. .can_sleep = 0,
  489. };
  490. static int __devinit nmk_gpio_probe(struct platform_device *dev)
  491. {
  492. struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
  493. struct nmk_gpio_chip *nmk_chip;
  494. struct gpio_chip *chip;
  495. struct resource *res;
  496. struct clk *clk;
  497. int irq;
  498. int ret;
  499. if (!pdata)
  500. return -ENODEV;
  501. res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  502. if (!res) {
  503. ret = -ENOENT;
  504. goto out;
  505. }
  506. irq = platform_get_irq(dev, 0);
  507. if (irq < 0) {
  508. ret = irq;
  509. goto out;
  510. }
  511. if (request_mem_region(res->start, resource_size(res),
  512. dev_name(&dev->dev)) == NULL) {
  513. ret = -EBUSY;
  514. goto out;
  515. }
  516. clk = clk_get(&dev->dev, NULL);
  517. if (IS_ERR(clk)) {
  518. ret = PTR_ERR(clk);
  519. goto out_release;
  520. }
  521. clk_enable(clk);
  522. nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
  523. if (!nmk_chip) {
  524. ret = -ENOMEM;
  525. goto out_clk;
  526. }
  527. /*
  528. * The virt address in nmk_chip->addr is in the nomadik register space,
  529. * so we can simply convert the resource address, without remapping
  530. */
  531. nmk_chip->clk = clk;
  532. nmk_chip->addr = io_p2v(res->start);
  533. nmk_chip->chip = nmk_gpio_template;
  534. nmk_chip->parent_irq = irq;
  535. spin_lock_init(&nmk_chip->lock);
  536. chip = &nmk_chip->chip;
  537. chip->base = pdata->first_gpio;
  538. chip->label = pdata->name;
  539. chip->dev = &dev->dev;
  540. chip->owner = THIS_MODULE;
  541. ret = gpiochip_add(&nmk_chip->chip);
  542. if (ret)
  543. goto out_free;
  544. platform_set_drvdata(dev, nmk_chip);
  545. nmk_gpio_init_irq(nmk_chip);
  546. dev_info(&dev->dev, "Bits %i-%i at address %p\n",
  547. nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
  548. return 0;
  549. out_free:
  550. kfree(nmk_chip);
  551. out_clk:
  552. clk_disable(clk);
  553. clk_put(clk);
  554. out_release:
  555. release_mem_region(res->start, resource_size(res));
  556. out:
  557. dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
  558. pdata->first_gpio, pdata->first_gpio+31);
  559. return ret;
  560. }
  561. static struct platform_driver nmk_gpio_driver = {
  562. .driver = {
  563. .owner = THIS_MODULE,
  564. .name = "gpio",
  565. },
  566. .probe = nmk_gpio_probe,
  567. .suspend = NULL, /* to be done */
  568. .resume = NULL,
  569. };
  570. static int __init nmk_gpio_init(void)
  571. {
  572. return platform_driver_register(&nmk_gpio_driver);
  573. }
  574. core_initcall(nmk_gpio_init);
  575. MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
  576. MODULE_DESCRIPTION("Nomadik GPIO Driver");
  577. MODULE_LICENSE("GPL");