gpio-vr41xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Driver for NEC VR4100 series General-purpose I/O Unit.
  3. *
  4. * Copyright (C) 2002 MontaVista Software Inc.
  5. * Author: Yoichi Yuasa <source@mvista.com>
  6. * Copyright (C) 2003-2009 Yoichi Yuasa <yuasa@linux-mips.org>
  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 as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/fs.h>
  24. #include <linux/gpio.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/irq.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/types.h>
  34. #include <asm/vr41xx/giu.h>
  35. #include <asm/vr41xx/irq.h>
  36. #include <asm/vr41xx/vr41xx.h>
  37. MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
  38. MODULE_DESCRIPTION("NEC VR4100 series General-purpose I/O Unit driver");
  39. MODULE_LICENSE("GPL");
  40. #define GIUIOSELL 0x00
  41. #define GIUIOSELH 0x02
  42. #define GIUPIODL 0x04
  43. #define GIUPIODH 0x06
  44. #define GIUINTSTATL 0x08
  45. #define GIUINTSTATH 0x0a
  46. #define GIUINTENL 0x0c
  47. #define GIUINTENH 0x0e
  48. #define GIUINTTYPL 0x10
  49. #define GIUINTTYPH 0x12
  50. #define GIUINTALSELL 0x14
  51. #define GIUINTALSELH 0x16
  52. #define GIUINTHTSELL 0x18
  53. #define GIUINTHTSELH 0x1a
  54. #define GIUPODATL 0x1c
  55. #define GIUPODATEN 0x1c
  56. #define GIUPODATH 0x1e
  57. #define PIOEN0 0x0100
  58. #define PIOEN1 0x0200
  59. #define GIUPODAT 0x1e
  60. #define GIUFEDGEINHL 0x20
  61. #define GIUFEDGEINHH 0x22
  62. #define GIUREDGEINHL 0x24
  63. #define GIUREDGEINHH 0x26
  64. #define GIUUSEUPDN 0x1e0
  65. #define GIUTERMUPDN 0x1e2
  66. #define GPIO_HAS_PULLUPDOWN_IO 0x0001
  67. #define GPIO_HAS_OUTPUT_ENABLE 0x0002
  68. #define GPIO_HAS_INTERRUPT_EDGE_SELECT 0x0100
  69. enum {
  70. GPIO_INPUT,
  71. GPIO_OUTPUT,
  72. };
  73. static DEFINE_SPINLOCK(giu_lock);
  74. static unsigned long giu_flags;
  75. static void __iomem *giu_base;
  76. static struct gpio_chip vr41xx_gpio_chip;
  77. #define giu_read(offset) readw(giu_base + (offset))
  78. #define giu_write(offset, value) writew((value), giu_base + (offset))
  79. #define GPIO_PIN_OF_IRQ(irq) ((irq) - GIU_IRQ_BASE)
  80. #define GIUINT_HIGH_OFFSET 16
  81. #define GIUINT_HIGH_MAX 32
  82. static inline u16 giu_set(u16 offset, u16 set)
  83. {
  84. u16 data;
  85. data = giu_read(offset);
  86. data |= set;
  87. giu_write(offset, data);
  88. return data;
  89. }
  90. static inline u16 giu_clear(u16 offset, u16 clear)
  91. {
  92. u16 data;
  93. data = giu_read(offset);
  94. data &= ~clear;
  95. giu_write(offset, data);
  96. return data;
  97. }
  98. static void ack_giuint_low(struct irq_data *d)
  99. {
  100. giu_write(GIUINTSTATL, 1 << GPIO_PIN_OF_IRQ(d->irq));
  101. }
  102. static void mask_giuint_low(struct irq_data *d)
  103. {
  104. giu_clear(GIUINTENL, 1 << GPIO_PIN_OF_IRQ(d->irq));
  105. }
  106. static void mask_ack_giuint_low(struct irq_data *d)
  107. {
  108. unsigned int pin;
  109. pin = GPIO_PIN_OF_IRQ(d->irq);
  110. giu_clear(GIUINTENL, 1 << pin);
  111. giu_write(GIUINTSTATL, 1 << pin);
  112. }
  113. static void unmask_giuint_low(struct irq_data *d)
  114. {
  115. giu_set(GIUINTENL, 1 << GPIO_PIN_OF_IRQ(d->irq));
  116. }
  117. static unsigned int startup_giuint(struct irq_data *data)
  118. {
  119. int ret;
  120. ret = gpiochip_lock_as_irq(&vr41xx_gpio_chip, irqd_to_hwirq(data));
  121. if (ret) {
  122. dev_err(vr41xx_gpio_chip.parent,
  123. "unable to lock HW IRQ %lu for IRQ\n",
  124. data->hwirq);
  125. return ret;
  126. }
  127. /* Satisfy the .enable semantics by unmasking the line */
  128. unmask_giuint_low(data);
  129. return 0;
  130. }
  131. static void shutdown_giuint(struct irq_data *data)
  132. {
  133. mask_giuint_low(data);
  134. gpiochip_unlock_as_irq(&vr41xx_gpio_chip, data->hwirq);
  135. }
  136. static struct irq_chip giuint_low_irq_chip = {
  137. .name = "GIUINTL",
  138. .irq_ack = ack_giuint_low,
  139. .irq_mask = mask_giuint_low,
  140. .irq_mask_ack = mask_ack_giuint_low,
  141. .irq_unmask = unmask_giuint_low,
  142. .irq_startup = startup_giuint,
  143. .irq_shutdown = shutdown_giuint,
  144. };
  145. static void ack_giuint_high(struct irq_data *d)
  146. {
  147. giu_write(GIUINTSTATH,
  148. 1 << (GPIO_PIN_OF_IRQ(d->irq) - GIUINT_HIGH_OFFSET));
  149. }
  150. static void mask_giuint_high(struct irq_data *d)
  151. {
  152. giu_clear(GIUINTENH, 1 << (GPIO_PIN_OF_IRQ(d->irq) - GIUINT_HIGH_OFFSET));
  153. }
  154. static void mask_ack_giuint_high(struct irq_data *d)
  155. {
  156. unsigned int pin;
  157. pin = GPIO_PIN_OF_IRQ(d->irq) - GIUINT_HIGH_OFFSET;
  158. giu_clear(GIUINTENH, 1 << pin);
  159. giu_write(GIUINTSTATH, 1 << pin);
  160. }
  161. static void unmask_giuint_high(struct irq_data *d)
  162. {
  163. giu_set(GIUINTENH, 1 << (GPIO_PIN_OF_IRQ(d->irq) - GIUINT_HIGH_OFFSET));
  164. }
  165. static struct irq_chip giuint_high_irq_chip = {
  166. .name = "GIUINTH",
  167. .irq_ack = ack_giuint_high,
  168. .irq_mask = mask_giuint_high,
  169. .irq_mask_ack = mask_ack_giuint_high,
  170. .irq_unmask = unmask_giuint_high,
  171. };
  172. static int giu_get_irq(unsigned int irq)
  173. {
  174. u16 pendl, pendh, maskl, maskh;
  175. int i;
  176. pendl = giu_read(GIUINTSTATL);
  177. pendh = giu_read(GIUINTSTATH);
  178. maskl = giu_read(GIUINTENL);
  179. maskh = giu_read(GIUINTENH);
  180. maskl &= pendl;
  181. maskh &= pendh;
  182. if (maskl) {
  183. for (i = 0; i < 16; i++) {
  184. if (maskl & (1 << i))
  185. return GIU_IRQ(i);
  186. }
  187. } else if (maskh) {
  188. for (i = 0; i < 16; i++) {
  189. if (maskh & (1 << i))
  190. return GIU_IRQ(i + GIUINT_HIGH_OFFSET);
  191. }
  192. }
  193. printk(KERN_ERR "spurious GIU interrupt: %04x(%04x),%04x(%04x)\n",
  194. maskl, pendl, maskh, pendh);
  195. atomic_inc(&irq_err_count);
  196. return -EINVAL;
  197. }
  198. void vr41xx_set_irq_trigger(unsigned int pin, irq_trigger_t trigger,
  199. irq_signal_t signal)
  200. {
  201. u16 mask;
  202. if (pin < GIUINT_HIGH_OFFSET) {
  203. mask = 1 << pin;
  204. if (trigger != IRQ_TRIGGER_LEVEL) {
  205. giu_set(GIUINTTYPL, mask);
  206. if (signal == IRQ_SIGNAL_HOLD)
  207. giu_set(GIUINTHTSELL, mask);
  208. else
  209. giu_clear(GIUINTHTSELL, mask);
  210. if (giu_flags & GPIO_HAS_INTERRUPT_EDGE_SELECT) {
  211. switch (trigger) {
  212. case IRQ_TRIGGER_EDGE_FALLING:
  213. giu_set(GIUFEDGEINHL, mask);
  214. giu_clear(GIUREDGEINHL, mask);
  215. break;
  216. case IRQ_TRIGGER_EDGE_RISING:
  217. giu_clear(GIUFEDGEINHL, mask);
  218. giu_set(GIUREDGEINHL, mask);
  219. break;
  220. default:
  221. giu_set(GIUFEDGEINHL, mask);
  222. giu_set(GIUREDGEINHL, mask);
  223. break;
  224. }
  225. }
  226. irq_set_chip_and_handler(GIU_IRQ(pin),
  227. &giuint_low_irq_chip,
  228. handle_edge_irq);
  229. } else {
  230. giu_clear(GIUINTTYPL, mask);
  231. giu_clear(GIUINTHTSELL, mask);
  232. irq_set_chip_and_handler(GIU_IRQ(pin),
  233. &giuint_low_irq_chip,
  234. handle_level_irq);
  235. }
  236. giu_write(GIUINTSTATL, mask);
  237. } else if (pin < GIUINT_HIGH_MAX) {
  238. mask = 1 << (pin - GIUINT_HIGH_OFFSET);
  239. if (trigger != IRQ_TRIGGER_LEVEL) {
  240. giu_set(GIUINTTYPH, mask);
  241. if (signal == IRQ_SIGNAL_HOLD)
  242. giu_set(GIUINTHTSELH, mask);
  243. else
  244. giu_clear(GIUINTHTSELH, mask);
  245. if (giu_flags & GPIO_HAS_INTERRUPT_EDGE_SELECT) {
  246. switch (trigger) {
  247. case IRQ_TRIGGER_EDGE_FALLING:
  248. giu_set(GIUFEDGEINHH, mask);
  249. giu_clear(GIUREDGEINHH, mask);
  250. break;
  251. case IRQ_TRIGGER_EDGE_RISING:
  252. giu_clear(GIUFEDGEINHH, mask);
  253. giu_set(GIUREDGEINHH, mask);
  254. break;
  255. default:
  256. giu_set(GIUFEDGEINHH, mask);
  257. giu_set(GIUREDGEINHH, mask);
  258. break;
  259. }
  260. }
  261. irq_set_chip_and_handler(GIU_IRQ(pin),
  262. &giuint_high_irq_chip,
  263. handle_edge_irq);
  264. } else {
  265. giu_clear(GIUINTTYPH, mask);
  266. giu_clear(GIUINTHTSELH, mask);
  267. irq_set_chip_and_handler(GIU_IRQ(pin),
  268. &giuint_high_irq_chip,
  269. handle_level_irq);
  270. }
  271. giu_write(GIUINTSTATH, mask);
  272. }
  273. }
  274. EXPORT_SYMBOL_GPL(vr41xx_set_irq_trigger);
  275. void vr41xx_set_irq_level(unsigned int pin, irq_level_t level)
  276. {
  277. u16 mask;
  278. if (pin < GIUINT_HIGH_OFFSET) {
  279. mask = 1 << pin;
  280. if (level == IRQ_LEVEL_HIGH)
  281. giu_set(GIUINTALSELL, mask);
  282. else
  283. giu_clear(GIUINTALSELL, mask);
  284. giu_write(GIUINTSTATL, mask);
  285. } else if (pin < GIUINT_HIGH_MAX) {
  286. mask = 1 << (pin - GIUINT_HIGH_OFFSET);
  287. if (level == IRQ_LEVEL_HIGH)
  288. giu_set(GIUINTALSELH, mask);
  289. else
  290. giu_clear(GIUINTALSELH, mask);
  291. giu_write(GIUINTSTATH, mask);
  292. }
  293. }
  294. EXPORT_SYMBOL_GPL(vr41xx_set_irq_level);
  295. static int giu_set_direction(struct gpio_chip *chip, unsigned pin, int dir)
  296. {
  297. u16 offset, mask, reg;
  298. unsigned long flags;
  299. if (pin >= chip->ngpio)
  300. return -EINVAL;
  301. if (pin < 16) {
  302. offset = GIUIOSELL;
  303. mask = 1 << pin;
  304. } else if (pin < 32) {
  305. offset = GIUIOSELH;
  306. mask = 1 << (pin - 16);
  307. } else {
  308. if (giu_flags & GPIO_HAS_OUTPUT_ENABLE) {
  309. offset = GIUPODATEN;
  310. mask = 1 << (pin - 32);
  311. } else {
  312. switch (pin) {
  313. case 48:
  314. offset = GIUPODATH;
  315. mask = PIOEN0;
  316. break;
  317. case 49:
  318. offset = GIUPODATH;
  319. mask = PIOEN1;
  320. break;
  321. default:
  322. return -EINVAL;
  323. }
  324. }
  325. }
  326. spin_lock_irqsave(&giu_lock, flags);
  327. reg = giu_read(offset);
  328. if (dir == GPIO_OUTPUT)
  329. reg |= mask;
  330. else
  331. reg &= ~mask;
  332. giu_write(offset, reg);
  333. spin_unlock_irqrestore(&giu_lock, flags);
  334. return 0;
  335. }
  336. int vr41xx_gpio_pullupdown(unsigned int pin, gpio_pull_t pull)
  337. {
  338. u16 reg, mask;
  339. unsigned long flags;
  340. if ((giu_flags & GPIO_HAS_PULLUPDOWN_IO) != GPIO_HAS_PULLUPDOWN_IO)
  341. return -EPERM;
  342. if (pin >= 15)
  343. return -EINVAL;
  344. mask = 1 << pin;
  345. spin_lock_irqsave(&giu_lock, flags);
  346. if (pull == GPIO_PULL_UP || pull == GPIO_PULL_DOWN) {
  347. reg = giu_read(GIUTERMUPDN);
  348. if (pull == GPIO_PULL_UP)
  349. reg |= mask;
  350. else
  351. reg &= ~mask;
  352. giu_write(GIUTERMUPDN, reg);
  353. reg = giu_read(GIUUSEUPDN);
  354. reg |= mask;
  355. giu_write(GIUUSEUPDN, reg);
  356. } else {
  357. reg = giu_read(GIUUSEUPDN);
  358. reg &= ~mask;
  359. giu_write(GIUUSEUPDN, reg);
  360. }
  361. spin_unlock_irqrestore(&giu_lock, flags);
  362. return 0;
  363. }
  364. EXPORT_SYMBOL_GPL(vr41xx_gpio_pullupdown);
  365. static int vr41xx_gpio_get(struct gpio_chip *chip, unsigned pin)
  366. {
  367. u16 reg, mask;
  368. if (pin >= chip->ngpio)
  369. return -EINVAL;
  370. if (pin < 16) {
  371. reg = giu_read(GIUPIODL);
  372. mask = 1 << pin;
  373. } else if (pin < 32) {
  374. reg = giu_read(GIUPIODH);
  375. mask = 1 << (pin - 16);
  376. } else if (pin < 48) {
  377. reg = giu_read(GIUPODATL);
  378. mask = 1 << (pin - 32);
  379. } else {
  380. reg = giu_read(GIUPODATH);
  381. mask = 1 << (pin - 48);
  382. }
  383. if (reg & mask)
  384. return 1;
  385. return 0;
  386. }
  387. static void vr41xx_gpio_set(struct gpio_chip *chip, unsigned pin,
  388. int value)
  389. {
  390. u16 offset, mask, reg;
  391. unsigned long flags;
  392. if (pin >= chip->ngpio)
  393. return;
  394. if (pin < 16) {
  395. offset = GIUPIODL;
  396. mask = 1 << pin;
  397. } else if (pin < 32) {
  398. offset = GIUPIODH;
  399. mask = 1 << (pin - 16);
  400. } else if (pin < 48) {
  401. offset = GIUPODATL;
  402. mask = 1 << (pin - 32);
  403. } else {
  404. offset = GIUPODATH;
  405. mask = 1 << (pin - 48);
  406. }
  407. spin_lock_irqsave(&giu_lock, flags);
  408. reg = giu_read(offset);
  409. if (value)
  410. reg |= mask;
  411. else
  412. reg &= ~mask;
  413. giu_write(offset, reg);
  414. spin_unlock_irqrestore(&giu_lock, flags);
  415. }
  416. static int vr41xx_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  417. {
  418. return giu_set_direction(chip, offset, GPIO_INPUT);
  419. }
  420. static int vr41xx_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  421. int value)
  422. {
  423. vr41xx_gpio_set(chip, offset, value);
  424. return giu_set_direction(chip, offset, GPIO_OUTPUT);
  425. }
  426. static int vr41xx_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  427. {
  428. if (offset >= chip->ngpio)
  429. return -EINVAL;
  430. return GIU_IRQ_BASE + offset;
  431. }
  432. static struct gpio_chip vr41xx_gpio_chip = {
  433. .label = "vr41xx",
  434. .owner = THIS_MODULE,
  435. .direction_input = vr41xx_gpio_direction_input,
  436. .get = vr41xx_gpio_get,
  437. .direction_output = vr41xx_gpio_direction_output,
  438. .set = vr41xx_gpio_set,
  439. .to_irq = vr41xx_gpio_to_irq,
  440. };
  441. static int giu_probe(struct platform_device *pdev)
  442. {
  443. struct resource *res;
  444. unsigned int trigger, i, pin;
  445. struct irq_chip *chip;
  446. int irq, ret;
  447. switch (pdev->id) {
  448. case GPIO_50PINS_PULLUPDOWN:
  449. giu_flags = GPIO_HAS_PULLUPDOWN_IO;
  450. vr41xx_gpio_chip.ngpio = 50;
  451. break;
  452. case GPIO_36PINS:
  453. vr41xx_gpio_chip.ngpio = 36;
  454. break;
  455. case GPIO_48PINS_EDGE_SELECT:
  456. giu_flags = GPIO_HAS_INTERRUPT_EDGE_SELECT;
  457. vr41xx_gpio_chip.ngpio = 48;
  458. break;
  459. default:
  460. dev_err(&pdev->dev, "GIU: unknown ID %d\n", pdev->id);
  461. return -ENODEV;
  462. }
  463. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  464. if (!res)
  465. return -EBUSY;
  466. giu_base = ioremap(res->start, resource_size(res));
  467. if (!giu_base)
  468. return -ENOMEM;
  469. vr41xx_gpio_chip.parent = &pdev->dev;
  470. ret = gpiochip_add_data(&vr41xx_gpio_chip, NULL);
  471. if (!ret) {
  472. iounmap(giu_base);
  473. return -ENODEV;
  474. }
  475. giu_write(GIUINTENL, 0);
  476. giu_write(GIUINTENH, 0);
  477. trigger = giu_read(GIUINTTYPH) << 16;
  478. trigger |= giu_read(GIUINTTYPL);
  479. for (i = GIU_IRQ_BASE; i <= GIU_IRQ_LAST; i++) {
  480. pin = GPIO_PIN_OF_IRQ(i);
  481. if (pin < GIUINT_HIGH_OFFSET)
  482. chip = &giuint_low_irq_chip;
  483. else
  484. chip = &giuint_high_irq_chip;
  485. if (trigger & (1 << pin))
  486. irq_set_chip_and_handler(i, chip, handle_edge_irq);
  487. else
  488. irq_set_chip_and_handler(i, chip, handle_level_irq);
  489. }
  490. irq = platform_get_irq(pdev, 0);
  491. if (irq < 0 || irq >= nr_irqs)
  492. return -EBUSY;
  493. return cascade_irq(irq, giu_get_irq);
  494. }
  495. static int giu_remove(struct platform_device *pdev)
  496. {
  497. if (giu_base) {
  498. iounmap(giu_base);
  499. giu_base = NULL;
  500. }
  501. return 0;
  502. }
  503. static struct platform_driver giu_device_driver = {
  504. .probe = giu_probe,
  505. .remove = giu_remove,
  506. .driver = {
  507. .name = "GIU",
  508. },
  509. };
  510. module_platform_driver(giu_device_driver);