gpio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /*
  2. * ETRAX CRISv32 general port I/O device
  3. *
  4. * Copyright (c) 1999-2006 Axis Communications AB
  5. *
  6. * Authors: Bjorn Wesen (initial version)
  7. * Ola Knutsson (LED handling)
  8. * Johan Adolfsson (read/set directions, write, port G,
  9. * port to ETRAX FS.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/ioport.h>
  16. #include <linux/errno.h>
  17. #include <linux/kernel.h>
  18. #include <linux/fs.h>
  19. #include <linux/string.h>
  20. #include <linux/poll.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/mutex.h>
  25. #include <asm/etraxgpio.h>
  26. #include <hwregs/reg_map.h>
  27. #include <hwregs/reg_rdwr.h>
  28. #include <hwregs/gio_defs.h>
  29. #include <hwregs/intr_vect_defs.h>
  30. #include <asm/io.h>
  31. #include <asm/irq.h>
  32. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  33. #include "../i2c.h"
  34. #define VIRT_I2C_ADDR 0x40
  35. #endif
  36. /* The following gio ports on ETRAX FS is available:
  37. * pa 8 bits, supports interrupts off, hi, low, set, posedge, negedge anyedge
  38. * pb 18 bits
  39. * pc 18 bits
  40. * pd 18 bits
  41. * pe 18 bits
  42. * each port has a rw_px_dout, r_px_din and rw_px_oe register.
  43. */
  44. #define GPIO_MAJOR 120 /* experimental MAJOR number */
  45. #define D(x)
  46. #if 0
  47. static int dp_cnt;
  48. #define DP(x) \
  49. do { \
  50. dp_cnt++; \
  51. if (dp_cnt % 1000 == 0) \
  52. x; \
  53. } while (0)
  54. #else
  55. #define DP(x)
  56. #endif
  57. static DEFINE_MUTEX(gpio_mutex);
  58. static char gpio_name[] = "etrax gpio";
  59. #if 0
  60. static wait_queue_head_t *gpio_wq;
  61. #endif
  62. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  63. static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
  64. unsigned long arg);
  65. #endif
  66. static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  67. static ssize_t gpio_write(struct file *file, const char *buf, size_t count,
  68. loff_t *off);
  69. static int gpio_open(struct inode *inode, struct file *filp);
  70. static int gpio_release(struct inode *inode, struct file *filp);
  71. static unsigned int gpio_poll(struct file *filp,
  72. struct poll_table_struct *wait);
  73. /* private data per open() of this driver */
  74. struct gpio_private {
  75. struct gpio_private *next;
  76. /* The IO_CFG_WRITE_MODE_VALUE only support 8 bits: */
  77. unsigned char clk_mask;
  78. unsigned char data_mask;
  79. unsigned char write_msb;
  80. unsigned char pad1;
  81. /* These fields are generic */
  82. unsigned long highalarm, lowalarm;
  83. wait_queue_head_t alarm_wq;
  84. int minor;
  85. };
  86. /* linked list of alarms to check for */
  87. static struct gpio_private *alarmlist;
  88. static int gpio_some_alarms; /* Set if someone uses alarm */
  89. static unsigned long gpio_pa_high_alarms;
  90. static unsigned long gpio_pa_low_alarms;
  91. static DEFINE_SPINLOCK(alarm_lock);
  92. #define NUM_PORTS (GPIO_MINOR_LAST+1)
  93. #define GIO_REG_RD_ADDR(reg) \
  94. (volatile unsigned long *)(regi_gio + REG_RD_ADDR_gio_##reg)
  95. #define GIO_REG_WR_ADDR(reg) \
  96. (volatile unsigned long *)(regi_gio + REG_RD_ADDR_gio_##reg)
  97. unsigned long led_dummy;
  98. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  99. static unsigned long virtual_dummy;
  100. static unsigned long virtual_rw_pv_oe = CONFIG_ETRAX_DEF_GIO_PV_OE;
  101. static unsigned short cached_virtual_gpio_read;
  102. #endif
  103. static volatile unsigned long *data_out[NUM_PORTS] = {
  104. GIO_REG_WR_ADDR(rw_pa_dout),
  105. GIO_REG_WR_ADDR(rw_pb_dout),
  106. &led_dummy,
  107. GIO_REG_WR_ADDR(rw_pc_dout),
  108. GIO_REG_WR_ADDR(rw_pd_dout),
  109. GIO_REG_WR_ADDR(rw_pe_dout),
  110. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  111. &virtual_dummy,
  112. #endif
  113. };
  114. static volatile unsigned long *data_in[NUM_PORTS] = {
  115. GIO_REG_RD_ADDR(r_pa_din),
  116. GIO_REG_RD_ADDR(r_pb_din),
  117. &led_dummy,
  118. GIO_REG_RD_ADDR(r_pc_din),
  119. GIO_REG_RD_ADDR(r_pd_din),
  120. GIO_REG_RD_ADDR(r_pe_din),
  121. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  122. &virtual_dummy,
  123. #endif
  124. };
  125. static unsigned long changeable_dir[NUM_PORTS] = {
  126. CONFIG_ETRAX_PA_CHANGEABLE_DIR,
  127. CONFIG_ETRAX_PB_CHANGEABLE_DIR,
  128. 0,
  129. CONFIG_ETRAX_PC_CHANGEABLE_DIR,
  130. CONFIG_ETRAX_PD_CHANGEABLE_DIR,
  131. CONFIG_ETRAX_PE_CHANGEABLE_DIR,
  132. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  133. CONFIG_ETRAX_PV_CHANGEABLE_DIR,
  134. #endif
  135. };
  136. static unsigned long changeable_bits[NUM_PORTS] = {
  137. CONFIG_ETRAX_PA_CHANGEABLE_BITS,
  138. CONFIG_ETRAX_PB_CHANGEABLE_BITS,
  139. 0,
  140. CONFIG_ETRAX_PC_CHANGEABLE_BITS,
  141. CONFIG_ETRAX_PD_CHANGEABLE_BITS,
  142. CONFIG_ETRAX_PE_CHANGEABLE_BITS,
  143. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  144. CONFIG_ETRAX_PV_CHANGEABLE_BITS,
  145. #endif
  146. };
  147. static volatile unsigned long *dir_oe[NUM_PORTS] = {
  148. GIO_REG_WR_ADDR(rw_pa_oe),
  149. GIO_REG_WR_ADDR(rw_pb_oe),
  150. &led_dummy,
  151. GIO_REG_WR_ADDR(rw_pc_oe),
  152. GIO_REG_WR_ADDR(rw_pd_oe),
  153. GIO_REG_WR_ADDR(rw_pe_oe),
  154. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  155. &virtual_rw_pv_oe,
  156. #endif
  157. };
  158. static unsigned int gpio_poll(struct file *file, struct poll_table_struct *wait)
  159. {
  160. unsigned int mask = 0;
  161. struct gpio_private *priv = file->private_data;
  162. unsigned long data;
  163. poll_wait(file, &priv->alarm_wq, wait);
  164. if (priv->minor == GPIO_MINOR_A) {
  165. reg_gio_rw_intr_cfg intr_cfg;
  166. unsigned long tmp;
  167. unsigned long flags;
  168. local_irq_save(flags);
  169. data = REG_TYPE_CONV(unsigned long, reg_gio_r_pa_din,
  170. REG_RD(gio, regi_gio, r_pa_din));
  171. /* PA has support for interrupt
  172. * lets activate high for those low and with highalarm set
  173. */
  174. intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
  175. tmp = ~data & priv->highalarm & 0xFF;
  176. if (tmp & (1 << 0))
  177. intr_cfg.pa0 = regk_gio_hi;
  178. if (tmp & (1 << 1))
  179. intr_cfg.pa1 = regk_gio_hi;
  180. if (tmp & (1 << 2))
  181. intr_cfg.pa2 = regk_gio_hi;
  182. if (tmp & (1 << 3))
  183. intr_cfg.pa3 = regk_gio_hi;
  184. if (tmp & (1 << 4))
  185. intr_cfg.pa4 = regk_gio_hi;
  186. if (tmp & (1 << 5))
  187. intr_cfg.pa5 = regk_gio_hi;
  188. if (tmp & (1 << 6))
  189. intr_cfg.pa6 = regk_gio_hi;
  190. if (tmp & (1 << 7))
  191. intr_cfg.pa7 = regk_gio_hi;
  192. /*
  193. * lets activate low for those high and with lowalarm set
  194. */
  195. tmp = data & priv->lowalarm & 0xFF;
  196. if (tmp & (1 << 0))
  197. intr_cfg.pa0 = regk_gio_lo;
  198. if (tmp & (1 << 1))
  199. intr_cfg.pa1 = regk_gio_lo;
  200. if (tmp & (1 << 2))
  201. intr_cfg.pa2 = regk_gio_lo;
  202. if (tmp & (1 << 3))
  203. intr_cfg.pa3 = regk_gio_lo;
  204. if (tmp & (1 << 4))
  205. intr_cfg.pa4 = regk_gio_lo;
  206. if (tmp & (1 << 5))
  207. intr_cfg.pa5 = regk_gio_lo;
  208. if (tmp & (1 << 6))
  209. intr_cfg.pa6 = regk_gio_lo;
  210. if (tmp & (1 << 7))
  211. intr_cfg.pa7 = regk_gio_lo;
  212. REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
  213. local_irq_restore(flags);
  214. } else if (priv->minor <= GPIO_MINOR_E)
  215. data = *data_in[priv->minor];
  216. else
  217. return 0;
  218. if ((data & priv->highalarm) || (~data & priv->lowalarm))
  219. mask = POLLIN|POLLRDNORM;
  220. DP(printk(KERN_DEBUG "gpio_poll ready: mask 0x%08X\n", mask));
  221. return mask;
  222. }
  223. int etrax_gpio_wake_up_check(void)
  224. {
  225. struct gpio_private *priv;
  226. unsigned long data = 0;
  227. unsigned long flags;
  228. int ret = 0;
  229. spin_lock_irqsave(&alarm_lock, flags);
  230. priv = alarmlist;
  231. while (priv) {
  232. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  233. if (priv->minor == GPIO_MINOR_V)
  234. data = (unsigned long)cached_virtual_gpio_read;
  235. else {
  236. data = *data_in[priv->minor];
  237. if (priv->minor == GPIO_MINOR_A)
  238. priv->lowalarm |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  239. }
  240. #else
  241. data = *data_in[priv->minor];
  242. #endif
  243. if ((data & priv->highalarm) ||
  244. (~data & priv->lowalarm)) {
  245. DP(printk(KERN_DEBUG
  246. "etrax_gpio_wake_up_check %i\n", priv->minor));
  247. wake_up_interruptible(&priv->alarm_wq);
  248. ret = 1;
  249. }
  250. priv = priv->next;
  251. }
  252. spin_unlock_irqrestore(&alarm_lock, flags);
  253. return ret;
  254. }
  255. static irqreturn_t
  256. gpio_poll_timer_interrupt(int irq, void *dev_id)
  257. {
  258. if (gpio_some_alarms)
  259. return IRQ_RETVAL(etrax_gpio_wake_up_check());
  260. return IRQ_NONE;
  261. }
  262. static irqreturn_t
  263. gpio_pa_interrupt(int irq, void *dev_id)
  264. {
  265. reg_gio_rw_intr_mask intr_mask;
  266. reg_gio_r_masked_intr masked_intr;
  267. reg_gio_rw_ack_intr ack_intr;
  268. unsigned long tmp;
  269. unsigned long tmp2;
  270. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  271. unsigned char enable_gpiov_ack = 0;
  272. #endif
  273. /* Find what PA interrupts are active */
  274. masked_intr = REG_RD(gio, regi_gio, r_masked_intr);
  275. tmp = REG_TYPE_CONV(unsigned long, reg_gio_r_masked_intr, masked_intr);
  276. /* Find those that we have enabled */
  277. spin_lock(&alarm_lock);
  278. tmp &= (gpio_pa_high_alarms | gpio_pa_low_alarms);
  279. spin_unlock(&alarm_lock);
  280. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  281. /* Something changed on virtual GPIO. Interrupt is acked by
  282. * reading the device.
  283. */
  284. if (tmp & (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN)) {
  285. i2c_read(VIRT_I2C_ADDR, (void *)&cached_virtual_gpio_read,
  286. sizeof(cached_virtual_gpio_read));
  287. enable_gpiov_ack = 1;
  288. }
  289. #endif
  290. /* Ack them */
  291. ack_intr = REG_TYPE_CONV(reg_gio_rw_ack_intr, unsigned long, tmp);
  292. REG_WR(gio, regi_gio, rw_ack_intr, ack_intr);
  293. /* Disable those interrupts.. */
  294. intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
  295. tmp2 = REG_TYPE_CONV(unsigned long, reg_gio_rw_intr_mask, intr_mask);
  296. tmp2 &= ~tmp;
  297. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  298. /* Do not disable interrupt on virtual GPIO. Changes on virtual
  299. * pins are only noticed by an interrupt.
  300. */
  301. if (enable_gpiov_ack)
  302. tmp2 |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  303. #endif
  304. intr_mask = REG_TYPE_CONV(reg_gio_rw_intr_mask, unsigned long, tmp2);
  305. REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
  306. if (gpio_some_alarms)
  307. return IRQ_RETVAL(etrax_gpio_wake_up_check());
  308. return IRQ_NONE;
  309. }
  310. static ssize_t gpio_write(struct file *file, const char *buf, size_t count,
  311. loff_t *off)
  312. {
  313. struct gpio_private *priv = file->private_data;
  314. unsigned char data, clk_mask, data_mask, write_msb;
  315. unsigned long flags;
  316. unsigned long shadow;
  317. volatile unsigned long *port;
  318. ssize_t retval = count;
  319. /* Only bits 0-7 may be used for write operations but allow all
  320. devices except leds... */
  321. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  322. if (priv->minor == GPIO_MINOR_V)
  323. return -EFAULT;
  324. #endif
  325. if (priv->minor == GPIO_MINOR_LEDS)
  326. return -EFAULT;
  327. if (!access_ok(VERIFY_READ, buf, count))
  328. return -EFAULT;
  329. clk_mask = priv->clk_mask;
  330. data_mask = priv->data_mask;
  331. /* It must have been configured using the IO_CFG_WRITE_MODE */
  332. /* Perhaps a better error code? */
  333. if (clk_mask == 0 || data_mask == 0)
  334. return -EPERM;
  335. write_msb = priv->write_msb;
  336. D(printk(KERN_DEBUG "gpio_write: %lu to data 0x%02X clk 0x%02X "
  337. "msb: %i\n", count, data_mask, clk_mask, write_msb));
  338. port = data_out[priv->minor];
  339. while (count--) {
  340. int i;
  341. data = *buf++;
  342. if (priv->write_msb) {
  343. for (i = 7; i >= 0; i--) {
  344. local_irq_save(flags);
  345. shadow = *port;
  346. *port = shadow &= ~clk_mask;
  347. if (data & 1<<i)
  348. *port = shadow |= data_mask;
  349. else
  350. *port = shadow &= ~data_mask;
  351. /* For FPGA: min 5.0ns (DCC) before CCLK high */
  352. *port = shadow |= clk_mask;
  353. local_irq_restore(flags);
  354. }
  355. } else {
  356. for (i = 0; i <= 7; i++) {
  357. local_irq_save(flags);
  358. shadow = *port;
  359. *port = shadow &= ~clk_mask;
  360. if (data & 1<<i)
  361. *port = shadow |= data_mask;
  362. else
  363. *port = shadow &= ~data_mask;
  364. /* For FPGA: min 5.0ns (DCC) before CCLK high */
  365. *port = shadow |= clk_mask;
  366. local_irq_restore(flags);
  367. }
  368. }
  369. }
  370. return retval;
  371. }
  372. static int
  373. gpio_open(struct inode *inode, struct file *filp)
  374. {
  375. struct gpio_private *priv;
  376. int p = iminor(inode);
  377. if (p > GPIO_MINOR_LAST)
  378. return -EINVAL;
  379. priv = kmalloc(sizeof(struct gpio_private), GFP_KERNEL);
  380. if (!priv)
  381. return -ENOMEM;
  382. mutex_lock(&gpio_mutex);
  383. memset(priv, 0, sizeof(*priv));
  384. priv->minor = p;
  385. /* initialize the io/alarm struct */
  386. priv->clk_mask = 0;
  387. priv->data_mask = 0;
  388. priv->highalarm = 0;
  389. priv->lowalarm = 0;
  390. init_waitqueue_head(&priv->alarm_wq);
  391. filp->private_data = (void *)priv;
  392. /* link it into our alarmlist */
  393. spin_lock_irq(&alarm_lock);
  394. priv->next = alarmlist;
  395. alarmlist = priv;
  396. spin_unlock_irq(&alarm_lock);
  397. mutex_unlock(&gpio_mutex);
  398. return 0;
  399. }
  400. static int
  401. gpio_release(struct inode *inode, struct file *filp)
  402. {
  403. struct gpio_private *p;
  404. struct gpio_private *todel;
  405. /* local copies while updating them: */
  406. unsigned long a_high, a_low;
  407. unsigned long some_alarms;
  408. /* unlink from alarmlist and free the private structure */
  409. spin_lock_irq(&alarm_lock);
  410. p = alarmlist;
  411. todel = filp->private_data;
  412. if (p == todel) {
  413. alarmlist = todel->next;
  414. } else {
  415. while (p->next != todel)
  416. p = p->next;
  417. p->next = todel->next;
  418. }
  419. kfree(todel);
  420. /* Check if there are still any alarms set */
  421. p = alarmlist;
  422. some_alarms = 0;
  423. a_high = 0;
  424. a_low = 0;
  425. while (p) {
  426. if (p->minor == GPIO_MINOR_A) {
  427. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  428. p->lowalarm |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  429. #endif
  430. a_high |= p->highalarm;
  431. a_low |= p->lowalarm;
  432. }
  433. if (p->highalarm | p->lowalarm)
  434. some_alarms = 1;
  435. p = p->next;
  436. }
  437. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  438. /* Variables 'some_alarms' and 'a_low' needs to be set here again
  439. * to ensure that interrupt for virtual GPIO is handled.
  440. */
  441. some_alarms = 1;
  442. a_low |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  443. #endif
  444. gpio_some_alarms = some_alarms;
  445. gpio_pa_high_alarms = a_high;
  446. gpio_pa_low_alarms = a_low;
  447. spin_unlock_irq(&alarm_lock);
  448. return 0;
  449. }
  450. /* Main device API. ioctl's to read/set/clear bits, as well as to
  451. * set alarms to wait for using a subsequent select().
  452. */
  453. inline unsigned long setget_input(struct gpio_private *priv, unsigned long arg)
  454. {
  455. /* Set direction 0=unchanged 1=input,
  456. * return mask with 1=input
  457. */
  458. unsigned long flags;
  459. unsigned long dir_shadow;
  460. local_irq_save(flags);
  461. dir_shadow = *dir_oe[priv->minor];
  462. dir_shadow &= ~(arg & changeable_dir[priv->minor]);
  463. *dir_oe[priv->minor] = dir_shadow;
  464. local_irq_restore(flags);
  465. if (priv->minor == GPIO_MINOR_A)
  466. dir_shadow ^= 0xFF; /* Only 8 bits */
  467. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  468. else if (priv->minor == GPIO_MINOR_V)
  469. dir_shadow ^= 0xFFFF; /* Only 16 bits */
  470. #endif
  471. else
  472. dir_shadow ^= 0x3FFFF; /* Only 18 bits */
  473. return dir_shadow;
  474. } /* setget_input */
  475. inline unsigned long setget_output(struct gpio_private *priv, unsigned long arg)
  476. {
  477. unsigned long flags;
  478. unsigned long dir_shadow;
  479. local_irq_save(flags);
  480. dir_shadow = *dir_oe[priv->minor];
  481. dir_shadow |= (arg & changeable_dir[priv->minor]);
  482. *dir_oe[priv->minor] = dir_shadow;
  483. local_irq_restore(flags);
  484. return dir_shadow;
  485. } /* setget_output */
  486. static int gpio_leds_ioctl(unsigned int cmd, unsigned long arg);
  487. static int
  488. gpio_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned long arg)
  489. {
  490. unsigned long flags;
  491. unsigned long val;
  492. unsigned long shadow;
  493. struct gpio_private *priv = file->private_data;
  494. if (_IOC_TYPE(cmd) != ETRAXGPIO_IOCTYPE)
  495. return -EINVAL;
  496. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  497. if (priv->minor == GPIO_MINOR_V)
  498. return virtual_gpio_ioctl(file, cmd, arg);
  499. #endif
  500. switch (_IOC_NR(cmd)) {
  501. case IO_READBITS: /* Use IO_READ_INBITS and IO_READ_OUTBITS instead */
  502. /* Read the port. */
  503. return *data_in[priv->minor];
  504. break;
  505. case IO_SETBITS:
  506. local_irq_save(flags);
  507. /* Set changeable bits with a 1 in arg. */
  508. shadow = *data_out[priv->minor];
  509. shadow |= (arg & changeable_bits[priv->minor]);
  510. *data_out[priv->minor] = shadow;
  511. local_irq_restore(flags);
  512. break;
  513. case IO_CLRBITS:
  514. local_irq_save(flags);
  515. /* Clear changeable bits with a 1 in arg. */
  516. shadow = *data_out[priv->minor];
  517. shadow &= ~(arg & changeable_bits[priv->minor]);
  518. *data_out[priv->minor] = shadow;
  519. local_irq_restore(flags);
  520. break;
  521. case IO_HIGHALARM:
  522. /* Set alarm when bits with 1 in arg go high. */
  523. priv->highalarm |= arg;
  524. spin_lock_irqsave(&alarm_lock, flags);
  525. gpio_some_alarms = 1;
  526. if (priv->minor == GPIO_MINOR_A)
  527. gpio_pa_high_alarms |= arg;
  528. spin_unlock_irqrestore(&alarm_lock, flags);
  529. break;
  530. case IO_LOWALARM:
  531. /* Set alarm when bits with 1 in arg go low. */
  532. priv->lowalarm |= arg;
  533. spin_lock_irqsave(&alarm_lock, flags);
  534. gpio_some_alarms = 1;
  535. if (priv->minor == GPIO_MINOR_A)
  536. gpio_pa_low_alarms |= arg;
  537. spin_unlock_irqrestore(&alarm_lock, flags);
  538. break;
  539. case IO_CLRALARM:
  540. /* Clear alarm for bits with 1 in arg. */
  541. priv->highalarm &= ~arg;
  542. priv->lowalarm &= ~arg;
  543. spin_lock_irqsave(&alarm_lock, flags);
  544. if (priv->minor == GPIO_MINOR_A) {
  545. if (gpio_pa_high_alarms & arg ||
  546. gpio_pa_low_alarms & arg)
  547. /* Must update the gpio_pa_*alarms masks */
  548. ;
  549. }
  550. spin_unlock_irqrestore(&alarm_lock, flags);
  551. break;
  552. case IO_READDIR: /* Use IO_SETGET_INPUT/OUTPUT instead! */
  553. /* Read direction 0=input 1=output */
  554. return *dir_oe[priv->minor];
  555. case IO_SETINPUT: /* Use IO_SETGET_INPUT instead! */
  556. /* Set direction 0=unchanged 1=input,
  557. * return mask with 1=input
  558. */
  559. return setget_input(priv, arg);
  560. break;
  561. case IO_SETOUTPUT: /* Use IO_SETGET_OUTPUT instead! */
  562. /* Set direction 0=unchanged 1=output,
  563. * return mask with 1=output
  564. */
  565. return setget_output(priv, arg);
  566. case IO_CFG_WRITE_MODE:
  567. {
  568. unsigned long dir_shadow;
  569. dir_shadow = *dir_oe[priv->minor];
  570. priv->clk_mask = arg & 0xFF;
  571. priv->data_mask = (arg >> 8) & 0xFF;
  572. priv->write_msb = (arg >> 16) & 0x01;
  573. /* Check if we're allowed to change the bits and
  574. * the direction is correct
  575. */
  576. if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
  577. (priv->data_mask & changeable_bits[priv->minor]) &&
  578. (priv->clk_mask & dir_shadow) &&
  579. (priv->data_mask & dir_shadow))) {
  580. priv->clk_mask = 0;
  581. priv->data_mask = 0;
  582. return -EPERM;
  583. }
  584. break;
  585. }
  586. case IO_READ_INBITS:
  587. /* *arg is result of reading the input pins */
  588. val = *data_in[priv->minor];
  589. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  590. return -EFAULT;
  591. return 0;
  592. break;
  593. case IO_READ_OUTBITS:
  594. /* *arg is result of reading the output shadow */
  595. val = *data_out[priv->minor];
  596. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  597. return -EFAULT;
  598. break;
  599. case IO_SETGET_INPUT:
  600. /* bits set in *arg is set to input,
  601. * *arg updated with current input pins.
  602. */
  603. if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
  604. return -EFAULT;
  605. val = setget_input(priv, val);
  606. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  607. return -EFAULT;
  608. break;
  609. case IO_SETGET_OUTPUT:
  610. /* bits set in *arg is set to output,
  611. * *arg updated with current output pins.
  612. */
  613. if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
  614. return -EFAULT;
  615. val = setget_output(priv, val);
  616. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  617. return -EFAULT;
  618. break;
  619. default:
  620. if (priv->minor == GPIO_MINOR_LEDS)
  621. return gpio_leds_ioctl(cmd, arg);
  622. else
  623. return -EINVAL;
  624. } /* switch */
  625. return 0;
  626. }
  627. static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  628. {
  629. long ret;
  630. mutex_lock(&gpio_mutex);
  631. ret = gpio_ioctl_unlocked(file, cmd, arg);
  632. mutex_unlock(&gpio_mutex);
  633. return ret;
  634. }
  635. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  636. static int
  637. virtual_gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  638. {
  639. unsigned long flags;
  640. unsigned short val;
  641. unsigned short shadow;
  642. struct gpio_private *priv = file->private_data;
  643. switch (_IOC_NR(cmd)) {
  644. case IO_SETBITS:
  645. local_irq_save(flags);
  646. /* Set changeable bits with a 1 in arg. */
  647. i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  648. shadow |= ~*dir_oe[priv->minor];
  649. shadow |= (arg & changeable_bits[priv->minor]);
  650. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  651. local_irq_restore(flags);
  652. break;
  653. case IO_CLRBITS:
  654. local_irq_save(flags);
  655. /* Clear changeable bits with a 1 in arg. */
  656. i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  657. shadow |= ~*dir_oe[priv->minor];
  658. shadow &= ~(arg & changeable_bits[priv->minor]);
  659. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  660. local_irq_restore(flags);
  661. break;
  662. case IO_HIGHALARM:
  663. /* Set alarm when bits with 1 in arg go high. */
  664. priv->highalarm |= arg;
  665. spin_lock(&alarm_lock);
  666. gpio_some_alarms = 1;
  667. spin_unlock(&alarm_lock);
  668. break;
  669. case IO_LOWALARM:
  670. /* Set alarm when bits with 1 in arg go low. */
  671. priv->lowalarm |= arg;
  672. spin_lock(&alarm_lock);
  673. gpio_some_alarms = 1;
  674. spin_unlock(&alarm_lock);
  675. break;
  676. case IO_CLRALARM:
  677. /* Clear alarm for bits with 1 in arg. */
  678. priv->highalarm &= ~arg;
  679. priv->lowalarm &= ~arg;
  680. spin_lock(&alarm_lock);
  681. spin_unlock(&alarm_lock);
  682. break;
  683. case IO_CFG_WRITE_MODE:
  684. {
  685. unsigned long dir_shadow;
  686. dir_shadow = *dir_oe[priv->minor];
  687. priv->clk_mask = arg & 0xFF;
  688. priv->data_mask = (arg >> 8) & 0xFF;
  689. priv->write_msb = (arg >> 16) & 0x01;
  690. /* Check if we're allowed to change the bits and
  691. * the direction is correct
  692. */
  693. if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
  694. (priv->data_mask & changeable_bits[priv->minor]) &&
  695. (priv->clk_mask & dir_shadow) &&
  696. (priv->data_mask & dir_shadow))) {
  697. priv->clk_mask = 0;
  698. priv->data_mask = 0;
  699. return -EPERM;
  700. }
  701. break;
  702. }
  703. case IO_READ_INBITS:
  704. /* *arg is result of reading the input pins */
  705. val = cached_virtual_gpio_read;
  706. val &= ~*dir_oe[priv->minor];
  707. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  708. return -EFAULT;
  709. return 0;
  710. break;
  711. case IO_READ_OUTBITS:
  712. /* *arg is result of reading the output shadow */
  713. i2c_read(VIRT_I2C_ADDR, (void *)&val, sizeof(val));
  714. val &= *dir_oe[priv->minor];
  715. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  716. return -EFAULT;
  717. break;
  718. case IO_SETGET_INPUT:
  719. {
  720. /* bits set in *arg is set to input,
  721. * *arg updated with current input pins.
  722. */
  723. unsigned short input_mask = ~*dir_oe[priv->minor];
  724. if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
  725. return -EFAULT;
  726. val = setget_input(priv, val);
  727. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  728. return -EFAULT;
  729. if ((input_mask & val) != input_mask) {
  730. /* Input pins changed. All ports desired as input
  731. * should be set to logic 1.
  732. */
  733. unsigned short change = input_mask ^ val;
  734. i2c_read(VIRT_I2C_ADDR, (void *)&shadow,
  735. sizeof(shadow));
  736. shadow &= ~change;
  737. shadow |= val;
  738. i2c_write(VIRT_I2C_ADDR, (void *)&shadow,
  739. sizeof(shadow));
  740. }
  741. break;
  742. }
  743. case IO_SETGET_OUTPUT:
  744. /* bits set in *arg is set to output,
  745. * *arg updated with current output pins.
  746. */
  747. if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
  748. return -EFAULT;
  749. val = setget_output(priv, val);
  750. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  751. return -EFAULT;
  752. break;
  753. default:
  754. return -EINVAL;
  755. } /* switch */
  756. return 0;
  757. }
  758. #endif /* CONFIG_ETRAX_VIRTUAL_GPIO */
  759. static int
  760. gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
  761. {
  762. unsigned char green;
  763. unsigned char red;
  764. switch (_IOC_NR(cmd)) {
  765. case IO_LEDACTIVE_SET:
  766. green = ((unsigned char) arg) & 1;
  767. red = (((unsigned char) arg) >> 1) & 1;
  768. CRIS_LED_ACTIVE_SET_G(green);
  769. CRIS_LED_ACTIVE_SET_R(red);
  770. break;
  771. default:
  772. return -EINVAL;
  773. } /* switch */
  774. return 0;
  775. }
  776. static const struct file_operations gpio_fops = {
  777. .owner = THIS_MODULE,
  778. .poll = gpio_poll,
  779. .unlocked_ioctl = gpio_ioctl,
  780. .write = gpio_write,
  781. .open = gpio_open,
  782. .release = gpio_release,
  783. .llseek = noop_llseek,
  784. };
  785. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  786. static void
  787. virtual_gpio_init(void)
  788. {
  789. reg_gio_rw_intr_cfg intr_cfg;
  790. reg_gio_rw_intr_mask intr_mask;
  791. unsigned short shadow;
  792. shadow = ~virtual_rw_pv_oe; /* Input ports should be set to logic 1 */
  793. shadow |= CONFIG_ETRAX_DEF_GIO_PV_OUT;
  794. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  795. /* Set interrupt mask and on what state the interrupt shall trigger.
  796. * For virtual gpio the interrupt shall trigger on logic '0'.
  797. */
  798. intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
  799. intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
  800. switch (CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN) {
  801. case 0:
  802. intr_cfg.pa0 = regk_gio_lo;
  803. intr_mask.pa0 = regk_gio_yes;
  804. break;
  805. case 1:
  806. intr_cfg.pa1 = regk_gio_lo;
  807. intr_mask.pa1 = regk_gio_yes;
  808. break;
  809. case 2:
  810. intr_cfg.pa2 = regk_gio_lo;
  811. intr_mask.pa2 = regk_gio_yes;
  812. break;
  813. case 3:
  814. intr_cfg.pa3 = regk_gio_lo;
  815. intr_mask.pa3 = regk_gio_yes;
  816. break;
  817. case 4:
  818. intr_cfg.pa4 = regk_gio_lo;
  819. intr_mask.pa4 = regk_gio_yes;
  820. break;
  821. case 5:
  822. intr_cfg.pa5 = regk_gio_lo;
  823. intr_mask.pa5 = regk_gio_yes;
  824. break;
  825. case 6:
  826. intr_cfg.pa6 = regk_gio_lo;
  827. intr_mask.pa6 = regk_gio_yes;
  828. break;
  829. case 7:
  830. intr_cfg.pa7 = regk_gio_lo;
  831. intr_mask.pa7 = regk_gio_yes;
  832. break;
  833. }
  834. REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
  835. REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
  836. gpio_pa_low_alarms |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  837. gpio_some_alarms = 1;
  838. }
  839. #endif
  840. /* main driver initialization routine, called from mem.c */
  841. static __init int
  842. gpio_init(void)
  843. {
  844. int res;
  845. /* do the formalities */
  846. res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
  847. if (res < 0) {
  848. printk(KERN_ERR "gpio: couldn't get a major number.\n");
  849. return res;
  850. }
  851. /* Clear all leds */
  852. CRIS_LED_NETWORK_GRP0_SET(0);
  853. CRIS_LED_NETWORK_GRP1_SET(0);
  854. CRIS_LED_ACTIVE_SET(0);
  855. CRIS_LED_DISK_READ(0);
  856. CRIS_LED_DISK_WRITE(0);
  857. printk(KERN_INFO "ETRAX FS GPIO driver v2.5, (c) 2003-2007 "
  858. "Axis Communications AB\n");
  859. /* We call etrax_gpio_wake_up_check() from timer interrupt and
  860. * from cpu_idle() in kernel/process.c
  861. * The check in cpu_idle() reduces latency from ~15 ms to ~6 ms
  862. * in some tests.
  863. */
  864. if (request_irq(TIMER0_INTR_VECT, gpio_poll_timer_interrupt,
  865. IRQF_SHARED | IRQF_DISABLED, "gpio poll", &alarmlist))
  866. printk(KERN_ERR "timer0 irq for gpio\n");
  867. if (request_irq(GIO_INTR_VECT, gpio_pa_interrupt,
  868. IRQF_SHARED | IRQF_DISABLED, "gpio PA", &alarmlist))
  869. printk(KERN_ERR "PA irq for gpio\n");
  870. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  871. virtual_gpio_init();
  872. #endif
  873. return res;
  874. }
  875. /* this makes sure that gpio_init is called during kernel boot */
  876. module_init(gpio_init);