gpio.c 25 KB

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