gpio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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/smp_lock.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 <asm/arch/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 char gpio_name[] = "etrax gpio";
  60. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  61. static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
  62. unsigned long arg);
  63. #endif
  64. static int gpio_ioctl(struct inode *inode, struct file *file,
  65. 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. lock_kernel();
  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. unlock_kernel();
  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 int gpio_ioctl(struct inode *inode, 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. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  566. static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
  567. unsigned long arg)
  568. {
  569. unsigned long flags;
  570. unsigned short val;
  571. unsigned short shadow;
  572. struct gpio_private *priv = file->private_data;
  573. switch (_IOC_NR(cmd)) {
  574. case IO_SETBITS:
  575. spin_lock_irqsave(&gpio_lock, flags);
  576. /* Set changeable bits with a 1 in arg. */
  577. i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  578. shadow |= ~readl(dir_oe[priv->minor]) |
  579. (arg & changeable_bits[priv->minor]);
  580. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  581. spin_lock_irqrestore(&gpio_lock, flags);
  582. break;
  583. case IO_CLRBITS:
  584. spin_lock_irqsave(&gpio_lock, flags);
  585. /* Clear changeable bits with a 1 in arg. */
  586. i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  587. shadow |= ~readl(dir_oe[priv->minor]) &
  588. ~(arg & changeable_bits[priv->minor]);
  589. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  590. spin_lock_irqrestore(&gpio_lock, flags);
  591. break;
  592. case IO_HIGHALARM:
  593. /* Set alarm when bits with 1 in arg go high. */
  594. priv->highalarm |= arg;
  595. break;
  596. case IO_LOWALARM:
  597. /* Set alarm when bits with 1 in arg go low. */
  598. priv->lowalarm |= arg;
  599. break;
  600. case IO_CLRALARM:
  601. /* Clear alarm for bits with 1 in arg. */
  602. priv->highalarm &= ~arg;
  603. priv->lowalarm &= ~arg;
  604. break;
  605. case IO_CFG_WRITE_MODE:
  606. {
  607. unsigned long dir_shadow;
  608. dir_shadow = readl(dir_oe[priv->minor]);
  609. priv->clk_mask = arg & 0xFF;
  610. priv->data_mask = (arg >> 8) & 0xFF;
  611. priv->write_msb = (arg >> 16) & 0x01;
  612. /* Check if we're allowed to change the bits and
  613. * the direction is correct
  614. */
  615. if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
  616. (priv->data_mask & changeable_bits[priv->minor]) &&
  617. (priv->clk_mask & dir_shadow) &&
  618. (priv->data_mask & dir_shadow))) {
  619. priv->clk_mask = 0;
  620. priv->data_mask = 0;
  621. return -EPERM;
  622. }
  623. break;
  624. }
  625. case IO_READ_INBITS:
  626. /* *arg is result of reading the input pins */
  627. val = cached_virtual_gpio_read & ~readl(dir_oe[priv->minor]);
  628. if (copy_to_user((void __user *)arg, &val, sizeof(val)))
  629. return -EFAULT;
  630. return 0;
  631. case IO_READ_OUTBITS:
  632. /* *arg is result of reading the output shadow */
  633. i2c_read(VIRT_I2C_ADDR, (void *)&val, sizeof(val));
  634. val &= readl(dir_oe[priv->minor]);
  635. if (copy_to_user((void __user *)arg, &val, sizeof(val)))
  636. return -EFAULT;
  637. break;
  638. case IO_SETGET_INPUT:
  639. {
  640. /* bits set in *arg is set to input,
  641. * *arg updated with current input pins.
  642. */
  643. unsigned short input_mask = ~readl(dir_oe[priv->minor]);
  644. if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
  645. return -EFAULT;
  646. val = setget_input(priv, val);
  647. if (copy_to_user((void __user *)arg, &val, sizeof(val)))
  648. return -EFAULT;
  649. if ((input_mask & val) != input_mask) {
  650. /* Input pins changed. All ports desired as input
  651. * should be set to logic 1.
  652. */
  653. unsigned short change = input_mask ^ val;
  654. i2c_read(VIRT_I2C_ADDR, (void *)&shadow,
  655. sizeof(shadow));
  656. shadow &= ~change;
  657. shadow |= val;
  658. i2c_write(VIRT_I2C_ADDR, (void *)&shadow,
  659. sizeof(shadow));
  660. }
  661. break;
  662. }
  663. case IO_SETGET_OUTPUT:
  664. /* bits set in *arg is set to output,
  665. * *arg updated with current output pins.
  666. */
  667. if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
  668. return -EFAULT;
  669. val = setget_output(priv, val);
  670. if (copy_to_user((void __user *)arg, &val, sizeof(val)))
  671. return -EFAULT;
  672. break;
  673. default:
  674. return -EINVAL;
  675. } /* switch */
  676. return 0;
  677. }
  678. #endif /* CONFIG_ETRAX_VIRTUAL_GPIO */
  679. static int gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
  680. {
  681. unsigned char green;
  682. unsigned char red;
  683. switch (_IOC_NR(cmd)) {
  684. case IO_LEDACTIVE_SET:
  685. green = ((unsigned char) arg) & 1;
  686. red = (((unsigned char) arg) >> 1) & 1;
  687. CRIS_LED_ACTIVE_SET_G(green);
  688. CRIS_LED_ACTIVE_SET_R(red);
  689. break;
  690. default:
  691. return -EINVAL;
  692. } /* switch */
  693. return 0;
  694. }
  695. static int gpio_pwm_set_mode(unsigned long arg, int pwm_port)
  696. {
  697. int pinmux_pwm = pinmux_pwm0 + pwm_port;
  698. int mode;
  699. reg_gio_rw_pwm0_ctrl rw_pwm_ctrl = {
  700. .ccd_val = 0,
  701. .ccd_override = regk_gio_no,
  702. .mode = regk_gio_no
  703. };
  704. int allocstatus;
  705. if (get_user(mode, &((struct io_pwm_set_mode *) arg)->mode))
  706. return -EFAULT;
  707. rw_pwm_ctrl.mode = mode;
  708. if (mode != PWM_OFF)
  709. allocstatus = crisv32_pinmux_alloc_fixed(pinmux_pwm);
  710. else
  711. allocstatus = crisv32_pinmux_dealloc_fixed(pinmux_pwm);
  712. if (allocstatus)
  713. return allocstatus;
  714. REG_WRITE(reg_gio_rw_pwm0_ctrl, REG_ADDR(gio, regi_gio, rw_pwm0_ctrl) +
  715. 12 * pwm_port, rw_pwm_ctrl);
  716. return 0;
  717. }
  718. static int gpio_pwm_set_period(unsigned long arg, int pwm_port)
  719. {
  720. struct io_pwm_set_period periods;
  721. reg_gio_rw_pwm0_var rw_pwm_widths;
  722. if (copy_from_user(&periods, (void __user *)arg, sizeof(periods)))
  723. return -EFAULT;
  724. if (periods.lo > 8191 || periods.hi > 8191)
  725. return -EINVAL;
  726. rw_pwm_widths.lo = periods.lo;
  727. rw_pwm_widths.hi = periods.hi;
  728. REG_WRITE(reg_gio_rw_pwm0_var, REG_ADDR(gio, regi_gio, rw_pwm0_var) +
  729. 12 * pwm_port, rw_pwm_widths);
  730. return 0;
  731. }
  732. static int gpio_pwm_set_duty(unsigned long arg, int pwm_port)
  733. {
  734. unsigned int duty;
  735. reg_gio_rw_pwm0_data rw_pwm_duty;
  736. if (get_user(duty, &((struct io_pwm_set_duty *) arg)->duty))
  737. return -EFAULT;
  738. if (duty > 255)
  739. return -EINVAL;
  740. rw_pwm_duty.data = duty;
  741. REG_WRITE(reg_gio_rw_pwm0_data, REG_ADDR(gio, regi_gio, rw_pwm0_data) +
  742. 12 * pwm_port, rw_pwm_duty);
  743. return 0;
  744. }
  745. static int gpio_pwm_ioctl(struct gpio_private *priv, unsigned int cmd,
  746. unsigned long arg)
  747. {
  748. int pwm_port = priv->minor - GPIO_MINOR_PWM0;
  749. switch (_IOC_NR(cmd)) {
  750. case IO_PWM_SET_MODE:
  751. return gpio_pwm_set_mode(arg, pwm_port);
  752. case IO_PWM_SET_PERIOD:
  753. return gpio_pwm_set_period(arg, pwm_port);
  754. case IO_PWM_SET_DUTY:
  755. return gpio_pwm_set_duty(arg, pwm_port);
  756. default:
  757. return -EINVAL;
  758. }
  759. return 0;
  760. }
  761. static const struct file_operations gpio_fops = {
  762. .owner = THIS_MODULE,
  763. .poll = gpio_poll,
  764. .ioctl = gpio_ioctl,
  765. .write = gpio_write,
  766. .open = gpio_open,
  767. .release = gpio_release,
  768. };
  769. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  770. static void __init virtual_gpio_init(void)
  771. {
  772. reg_gio_rw_intr_cfg intr_cfg;
  773. reg_gio_rw_intr_mask intr_mask;
  774. unsigned short shadow;
  775. shadow = ~virtual_rw_pv_oe; /* Input ports should be set to logic 1 */
  776. shadow |= CONFIG_ETRAX_DEF_GIO_PV_OUT;
  777. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  778. /* Set interrupt mask and on what state the interrupt shall trigger.
  779. * For virtual gpio the interrupt shall trigger on logic '0'.
  780. */
  781. intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
  782. intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
  783. switch (CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN) {
  784. case 0:
  785. intr_cfg.pa0 = regk_gio_lo;
  786. intr_mask.pa0 = regk_gio_yes;
  787. break;
  788. case 1:
  789. intr_cfg.pa1 = regk_gio_lo;
  790. intr_mask.pa1 = regk_gio_yes;
  791. break;
  792. case 2:
  793. intr_cfg.pa2 = regk_gio_lo;
  794. intr_mask.pa2 = regk_gio_yes;
  795. break;
  796. case 3:
  797. intr_cfg.pa3 = regk_gio_lo;
  798. intr_mask.pa3 = regk_gio_yes;
  799. break;
  800. case 4:
  801. intr_cfg.pa4 = regk_gio_lo;
  802. intr_mask.pa4 = regk_gio_yes;
  803. break;
  804. case 5:
  805. intr_cfg.pa5 = regk_gio_lo;
  806. intr_mask.pa5 = regk_gio_yes;
  807. break;
  808. case 6:
  809. intr_cfg.pa6 = regk_gio_lo;
  810. intr_mask.pa6 = regk_gio_yes;
  811. break;
  812. case 7:
  813. intr_cfg.pa7 = regk_gio_lo;
  814. intr_mask.pa7 = regk_gio_yes;
  815. break;
  816. }
  817. REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
  818. REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
  819. }
  820. #endif
  821. /* main driver initialization routine, called from mem.c */
  822. static int __init gpio_init(void)
  823. {
  824. int res;
  825. printk(KERN_INFO "ETRAX FS GPIO driver v2.7, (c) 2003-2008 "
  826. "Axis Communications AB\n");
  827. /* do the formalities */
  828. res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
  829. if (res < 0) {
  830. printk(KERN_ERR "gpio: couldn't get a major number.\n");
  831. return res;
  832. }
  833. /* Clear all leds */
  834. CRIS_LED_NETWORK_GRP0_SET(0);
  835. CRIS_LED_NETWORK_GRP1_SET(0);
  836. CRIS_LED_ACTIVE_SET(0);
  837. CRIS_LED_DISK_READ(0);
  838. CRIS_LED_DISK_WRITE(0);
  839. int res2 = request_irq(GIO_INTR_VECT, gpio_interrupt,
  840. IRQF_SHARED | IRQF_DISABLED, "gpio", &alarmlist);
  841. if (res2) {
  842. printk(KERN_ERR "err: irq for gpio\n");
  843. return res2;
  844. }
  845. /* No IRQs by default. */
  846. REG_WR_INT(gio, regi_gio, rw_intr_pins, 0);
  847. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  848. virtual_gpio_init();
  849. #endif
  850. return res;
  851. }
  852. /* this makes sure that gpio_init is called during kernel boot */
  853. module_init(gpio_init);