gpio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * arch/arm/plat-orion/gpio.c
  3. *
  4. * Marvell Orion SoC GPIO handling.
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/irq.h>
  13. #include <linux/module.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/bitops.h>
  16. #include <linux/io.h>
  17. #include <linux/gpio.h>
  18. #include <linux/leds.h>
  19. /*
  20. * GPIO unit register offsets.
  21. */
  22. #define GPIO_OUT_OFF 0x0000
  23. #define GPIO_IO_CONF_OFF 0x0004
  24. #define GPIO_BLINK_EN_OFF 0x0008
  25. #define GPIO_IN_POL_OFF 0x000c
  26. #define GPIO_DATA_IN_OFF 0x0010
  27. #define GPIO_EDGE_CAUSE_OFF 0x0014
  28. #define GPIO_EDGE_MASK_OFF 0x0018
  29. #define GPIO_LEVEL_MASK_OFF 0x001c
  30. struct orion_gpio_chip {
  31. struct gpio_chip chip;
  32. spinlock_t lock;
  33. void __iomem *base;
  34. unsigned long valid_input;
  35. unsigned long valid_output;
  36. int mask_offset;
  37. int secondary_irq_base;
  38. };
  39. static void __iomem *GPIO_OUT(struct orion_gpio_chip *ochip)
  40. {
  41. return ochip->base + GPIO_OUT_OFF;
  42. }
  43. static void __iomem *GPIO_IO_CONF(struct orion_gpio_chip *ochip)
  44. {
  45. return ochip->base + GPIO_IO_CONF_OFF;
  46. }
  47. static void __iomem *GPIO_BLINK_EN(struct orion_gpio_chip *ochip)
  48. {
  49. return ochip->base + GPIO_BLINK_EN_OFF;
  50. }
  51. static void __iomem *GPIO_IN_POL(struct orion_gpio_chip *ochip)
  52. {
  53. return ochip->base + GPIO_IN_POL_OFF;
  54. }
  55. static void __iomem *GPIO_DATA_IN(struct orion_gpio_chip *ochip)
  56. {
  57. return ochip->base + GPIO_DATA_IN_OFF;
  58. }
  59. static void __iomem *GPIO_EDGE_CAUSE(struct orion_gpio_chip *ochip)
  60. {
  61. return ochip->base + GPIO_EDGE_CAUSE_OFF;
  62. }
  63. static void __iomem *GPIO_EDGE_MASK(struct orion_gpio_chip *ochip)
  64. {
  65. return ochip->base + ochip->mask_offset + GPIO_EDGE_MASK_OFF;
  66. }
  67. static void __iomem *GPIO_LEVEL_MASK(struct orion_gpio_chip *ochip)
  68. {
  69. return ochip->base + ochip->mask_offset + GPIO_LEVEL_MASK_OFF;
  70. }
  71. static struct orion_gpio_chip orion_gpio_chips[2];
  72. static int orion_gpio_chip_count;
  73. static inline void
  74. __set_direction(struct orion_gpio_chip *ochip, unsigned pin, int input)
  75. {
  76. u32 u;
  77. u = readl(GPIO_IO_CONF(ochip));
  78. if (input)
  79. u |= 1 << pin;
  80. else
  81. u &= ~(1 << pin);
  82. writel(u, GPIO_IO_CONF(ochip));
  83. }
  84. static void __set_level(struct orion_gpio_chip *ochip, unsigned pin, int high)
  85. {
  86. u32 u;
  87. u = readl(GPIO_OUT(ochip));
  88. if (high)
  89. u |= 1 << pin;
  90. else
  91. u &= ~(1 << pin);
  92. writel(u, GPIO_OUT(ochip));
  93. }
  94. static inline void
  95. __set_blinking(struct orion_gpio_chip *ochip, unsigned pin, int blink)
  96. {
  97. u32 u;
  98. u = readl(GPIO_BLINK_EN(ochip));
  99. if (blink)
  100. u |= 1 << pin;
  101. else
  102. u &= ~(1 << pin);
  103. writel(u, GPIO_BLINK_EN(ochip));
  104. }
  105. static inline int
  106. orion_gpio_is_valid(struct orion_gpio_chip *ochip, unsigned pin, int mode)
  107. {
  108. if (pin >= ochip->chip.ngpio)
  109. goto err_out;
  110. if ((mode & GPIO_INPUT_OK) && !test_bit(pin, &ochip->valid_input))
  111. goto err_out;
  112. if ((mode & GPIO_OUTPUT_OK) && !test_bit(pin, &ochip->valid_output))
  113. goto err_out;
  114. return 1;
  115. err_out:
  116. pr_debug("%s: invalid GPIO %d\n", __func__, pin);
  117. return false;
  118. }
  119. /*
  120. * GENERIC_GPIO primitives.
  121. */
  122. static int orion_gpio_request(struct gpio_chip *chip, unsigned pin)
  123. {
  124. struct orion_gpio_chip *ochip =
  125. container_of(chip, struct orion_gpio_chip, chip);
  126. if (orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK) ||
  127. orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
  128. return 0;
  129. return -EINVAL;
  130. }
  131. static int orion_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  132. {
  133. struct orion_gpio_chip *ochip =
  134. container_of(chip, struct orion_gpio_chip, chip);
  135. unsigned long flags;
  136. if (!orion_gpio_is_valid(ochip, pin, GPIO_INPUT_OK))
  137. return -EINVAL;
  138. spin_lock_irqsave(&ochip->lock, flags);
  139. __set_direction(ochip, pin, 1);
  140. spin_unlock_irqrestore(&ochip->lock, flags);
  141. return 0;
  142. }
  143. static int orion_gpio_get(struct gpio_chip *chip, unsigned pin)
  144. {
  145. struct orion_gpio_chip *ochip =
  146. container_of(chip, struct orion_gpio_chip, chip);
  147. int val;
  148. if (readl(GPIO_IO_CONF(ochip)) & (1 << pin)) {
  149. val = readl(GPIO_DATA_IN(ochip)) ^ readl(GPIO_IN_POL(ochip));
  150. } else {
  151. val = readl(GPIO_OUT(ochip));
  152. }
  153. return (val >> pin) & 1;
  154. }
  155. static int
  156. orion_gpio_direction_output(struct gpio_chip *chip, unsigned pin, int value)
  157. {
  158. struct orion_gpio_chip *ochip =
  159. container_of(chip, struct orion_gpio_chip, chip);
  160. unsigned long flags;
  161. if (!orion_gpio_is_valid(ochip, pin, GPIO_OUTPUT_OK))
  162. return -EINVAL;
  163. spin_lock_irqsave(&ochip->lock, flags);
  164. __set_blinking(ochip, pin, 0);
  165. __set_level(ochip, pin, value);
  166. __set_direction(ochip, pin, 0);
  167. spin_unlock_irqrestore(&ochip->lock, flags);
  168. return 0;
  169. }
  170. static void orion_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
  171. {
  172. struct orion_gpio_chip *ochip =
  173. container_of(chip, struct orion_gpio_chip, chip);
  174. unsigned long flags;
  175. spin_lock_irqsave(&ochip->lock, flags);
  176. __set_level(ochip, pin, value);
  177. spin_unlock_irqrestore(&ochip->lock, flags);
  178. }
  179. static int orion_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
  180. {
  181. struct orion_gpio_chip *ochip =
  182. container_of(chip, struct orion_gpio_chip, chip);
  183. return ochip->secondary_irq_base + pin;
  184. }
  185. /*
  186. * Orion-specific GPIO API extensions.
  187. */
  188. static struct orion_gpio_chip *orion_gpio_chip_find(int pin)
  189. {
  190. int i;
  191. for (i = 0; i < orion_gpio_chip_count; i++) {
  192. struct orion_gpio_chip *ochip = orion_gpio_chips + i;
  193. struct gpio_chip *chip = &ochip->chip;
  194. if (pin >= chip->base && pin < chip->base + chip->ngpio)
  195. return ochip;
  196. }
  197. return NULL;
  198. }
  199. void __init orion_gpio_set_unused(unsigned pin)
  200. {
  201. struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
  202. if (ochip == NULL)
  203. return;
  204. pin -= ochip->chip.base;
  205. /* Configure as output, drive low. */
  206. __set_level(ochip, pin, 0);
  207. __set_direction(ochip, pin, 0);
  208. }
  209. void __init orion_gpio_set_valid(unsigned pin, int mode)
  210. {
  211. struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
  212. if (ochip == NULL)
  213. return;
  214. pin -= ochip->chip.base;
  215. if (mode == 1)
  216. mode = GPIO_INPUT_OK | GPIO_OUTPUT_OK;
  217. if (mode & GPIO_INPUT_OK)
  218. __set_bit(pin, &ochip->valid_input);
  219. else
  220. __clear_bit(pin, &ochip->valid_input);
  221. if (mode & GPIO_OUTPUT_OK)
  222. __set_bit(pin, &ochip->valid_output);
  223. else
  224. __clear_bit(pin, &ochip->valid_output);
  225. }
  226. void orion_gpio_set_blink(unsigned pin, int blink)
  227. {
  228. struct orion_gpio_chip *ochip = orion_gpio_chip_find(pin);
  229. unsigned long flags;
  230. if (ochip == NULL)
  231. return;
  232. spin_lock_irqsave(&ochip->lock, flags);
  233. __set_level(ochip, pin & 31, 0);
  234. __set_blinking(ochip, pin & 31, blink);
  235. spin_unlock_irqrestore(&ochip->lock, flags);
  236. }
  237. EXPORT_SYMBOL(orion_gpio_set_blink);
  238. #define ORION_BLINK_HALF_PERIOD 100 /* ms */
  239. int orion_gpio_led_blink_set(unsigned gpio, int state,
  240. unsigned long *delay_on, unsigned long *delay_off)
  241. {
  242. if (delay_on && delay_off && !*delay_on && !*delay_off)
  243. *delay_on = *delay_off = ORION_BLINK_HALF_PERIOD;
  244. switch (state) {
  245. case GPIO_LED_NO_BLINK_LOW:
  246. case GPIO_LED_NO_BLINK_HIGH:
  247. orion_gpio_set_blink(gpio, 0);
  248. gpio_set_value(gpio, state);
  249. break;
  250. case GPIO_LED_BLINK:
  251. orion_gpio_set_blink(gpio, 1);
  252. }
  253. return 0;
  254. }
  255. EXPORT_SYMBOL_GPL(orion_gpio_led_blink_set);
  256. /*****************************************************************************
  257. * Orion GPIO IRQ
  258. *
  259. * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
  260. * value of the line or the opposite value.
  261. *
  262. * Level IRQ handlers: DATA_IN is used directly as cause register.
  263. * Interrupt are masked by LEVEL_MASK registers.
  264. * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
  265. * Interrupt are masked by EDGE_MASK registers.
  266. * Both-edge handlers: Similar to regular Edge handlers, but also swaps
  267. * the polarity to catch the next line transaction.
  268. * This is a race condition that might not perfectly
  269. * work on some use cases.
  270. *
  271. * Every eight GPIO lines are grouped (OR'ed) before going up to main
  272. * cause register.
  273. *
  274. * EDGE cause mask
  275. * data-in /--------| |-----| |----\
  276. * -----| |----- ---- to main cause reg
  277. * X \----------------| |----/
  278. * polarity LEVEL mask
  279. *
  280. ****************************************************************************/
  281. static int gpio_irq_set_type(struct irq_data *d, u32 type)
  282. {
  283. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  284. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  285. struct orion_gpio_chip *ochip = gc->private;
  286. int pin;
  287. u32 u;
  288. pin = d->irq - gc->irq_base;
  289. u = readl(GPIO_IO_CONF(ochip)) & (1 << pin);
  290. if (!u) {
  291. printk(KERN_ERR "orion gpio_irq_set_type failed "
  292. "(irq %d, pin %d).\n", d->irq, pin);
  293. return -EINVAL;
  294. }
  295. type &= IRQ_TYPE_SENSE_MASK;
  296. if (type == IRQ_TYPE_NONE)
  297. return -EINVAL;
  298. /* Check if we need to change chip and handler */
  299. if (!(ct->type & type))
  300. if (irq_setup_alt_chip(d, type))
  301. return -EINVAL;
  302. /*
  303. * Configure interrupt polarity.
  304. */
  305. if (type == IRQ_TYPE_EDGE_RISING || type == IRQ_TYPE_LEVEL_HIGH) {
  306. u = readl(GPIO_IN_POL(ochip));
  307. u &= ~(1 << pin);
  308. writel(u, GPIO_IN_POL(ochip));
  309. } else if (type == IRQ_TYPE_EDGE_FALLING || type == IRQ_TYPE_LEVEL_LOW) {
  310. u = readl(GPIO_IN_POL(ochip));
  311. u |= 1 << pin;
  312. writel(u, GPIO_IN_POL(ochip));
  313. } else if (type == IRQ_TYPE_EDGE_BOTH) {
  314. u32 v;
  315. v = readl(GPIO_IN_POL(ochip)) ^ readl(GPIO_DATA_IN(ochip));
  316. /*
  317. * set initial polarity based on current input level
  318. */
  319. u = readl(GPIO_IN_POL(ochip));
  320. if (v & (1 << pin))
  321. u |= 1 << pin; /* falling */
  322. else
  323. u &= ~(1 << pin); /* rising */
  324. writel(u, GPIO_IN_POL(ochip));
  325. }
  326. return 0;
  327. }
  328. void __init orion_gpio_init(int gpio_base, int ngpio,
  329. u32 base, int mask_offset, int secondary_irq_base)
  330. {
  331. struct orion_gpio_chip *ochip;
  332. struct irq_chip_generic *gc;
  333. struct irq_chip_type *ct;
  334. char gc_label[16];
  335. if (orion_gpio_chip_count == ARRAY_SIZE(orion_gpio_chips))
  336. return;
  337. snprintf(gc_label, sizeof(gc_label), "orion_gpio%d",
  338. orion_gpio_chip_count);
  339. ochip = orion_gpio_chips + orion_gpio_chip_count;
  340. ochip->chip.label = kstrdup(gc_label, GFP_KERNEL);
  341. ochip->chip.request = orion_gpio_request;
  342. ochip->chip.direction_input = orion_gpio_direction_input;
  343. ochip->chip.get = orion_gpio_get;
  344. ochip->chip.direction_output = orion_gpio_direction_output;
  345. ochip->chip.set = orion_gpio_set;
  346. ochip->chip.to_irq = orion_gpio_to_irq;
  347. ochip->chip.base = gpio_base;
  348. ochip->chip.ngpio = ngpio;
  349. ochip->chip.can_sleep = 0;
  350. spin_lock_init(&ochip->lock);
  351. ochip->base = (void __iomem *)base;
  352. ochip->valid_input = 0;
  353. ochip->valid_output = 0;
  354. ochip->mask_offset = mask_offset;
  355. ochip->secondary_irq_base = secondary_irq_base;
  356. gpiochip_add(&ochip->chip);
  357. orion_gpio_chip_count++;
  358. /*
  359. * Mask and clear GPIO interrupts.
  360. */
  361. writel(0, GPIO_EDGE_CAUSE(ochip));
  362. writel(0, GPIO_EDGE_MASK(ochip));
  363. writel(0, GPIO_LEVEL_MASK(ochip));
  364. gc = irq_alloc_generic_chip("orion_gpio_irq", 2, secondary_irq_base,
  365. ochip->base, handle_level_irq);
  366. gc->private = ochip;
  367. ct = gc->chip_types;
  368. ct->regs.mask = ochip->mask_offset + GPIO_LEVEL_MASK_OFF;
  369. ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
  370. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  371. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  372. ct->chip.irq_set_type = gpio_irq_set_type;
  373. ct++;
  374. ct->regs.mask = ochip->mask_offset + GPIO_EDGE_MASK_OFF;
  375. ct->regs.ack = GPIO_EDGE_CAUSE_OFF;
  376. ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  377. ct->chip.irq_ack = irq_gc_ack_clr_bit;
  378. ct->chip.irq_mask = irq_gc_mask_clr_bit;
  379. ct->chip.irq_unmask = irq_gc_mask_set_bit;
  380. ct->chip.irq_set_type = gpio_irq_set_type;
  381. ct->handler = handle_edge_irq;
  382. irq_setup_generic_chip(gc, IRQ_MSK(ngpio), IRQ_GC_INIT_MASK_CACHE,
  383. IRQ_NOREQUEST, IRQ_LEVEL | IRQ_NOPROBE);
  384. }
  385. void orion_gpio_irq_handler(int pinoff)
  386. {
  387. struct orion_gpio_chip *ochip;
  388. u32 cause, type;
  389. int i;
  390. ochip = orion_gpio_chip_find(pinoff);
  391. if (ochip == NULL)
  392. return;
  393. cause = readl(GPIO_DATA_IN(ochip)) & readl(GPIO_LEVEL_MASK(ochip));
  394. cause |= readl(GPIO_EDGE_CAUSE(ochip)) & readl(GPIO_EDGE_MASK(ochip));
  395. for (i = 0; i < ochip->chip.ngpio; i++) {
  396. int irq;
  397. irq = ochip->secondary_irq_base + i;
  398. if (!(cause & (1 << i)))
  399. continue;
  400. type = irqd_get_trigger_type(irq_get_irq_data(irq));
  401. if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  402. /* Swap polarity (race with GPIO line) */
  403. u32 polarity;
  404. polarity = readl(GPIO_IN_POL(ochip));
  405. polarity ^= 1 << i;
  406. writel(polarity, GPIO_IN_POL(ochip));
  407. }
  408. generic_handle_irq(irq);
  409. }
  410. }