gpio.c 25 KB

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