gpio.c 18 KB

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