gpio.c 16 KB

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