gpio.c 14 KB

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