summaryrefslogtreecommitdiff
path: root/cleopatre/linux-2.6.25.10-spc300/drivers
diff options
context:
space:
mode:
authorYacine Belkadi2011-08-11 14:45:24 +0200
committerYacine Belkadi2011-09-15 09:37:37 +0200
commit06517bf9d20c7a660e1185da69e09d9d714278fa (patch)
tree602039a21318df62dcd9b4fa025c21186d0b28f6 /cleopatre/linux-2.6.25.10-spc300/drivers
parent36f83288370f476ca10040ba4384195914e33aef (diff)
cleo/linux/drv/ad986x: refactor the code for reading/writing to the AFE registers, refs #453
Diffstat (limited to 'cleopatre/linux-2.6.25.10-spc300/drivers')
-rw-r--r--cleopatre/linux-2.6.25.10-spc300/drivers/char/ad986x.c74
1 files changed, 53 insertions, 21 deletions
diff --git a/cleopatre/linux-2.6.25.10-spc300/drivers/char/ad986x.c b/cleopatre/linux-2.6.25.10-spc300/drivers/char/ad986x.c
index 3edf458d91..77e606168f 100644
--- a/cleopatre/linux-2.6.25.10-spc300/drivers/char/ad986x.c
+++ b/cleopatre/linux-2.6.25.10-spc300/drivers/char/ad986x.c
@@ -28,6 +28,7 @@
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/spi/spi.h>
+
#include <linux/afe.h>
static int ad986x_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);
@@ -47,6 +48,56 @@ static struct miscdevice ad986x_miscdev = {
static struct spi_device *spi_dev;
/**
+ * Read a register of the AD986X device.
+ *
+ * \param reg the register.
+ * \param[out] val the value of the register.
+ * \return 0, on success.
+ * -EFAULT, on error.
+ */
+static int ad986x_read_reg(uint8_t reg, uint8_t *val)
+{
+ uint8_t command;
+
+ //Create SPI tx command : 1b(r/w) + 2b(nbbytes) + 5b(addr)
+ command = (uint8_t)((1 << 7) | (1 << 5) | (reg & 0x1F));
+
+ //Send it and wait the response
+ return spi_write_then_read(spi_dev, &command, 1, val, 1);
+}
+
+/**
+ * Write to a register of the AD986X device.
+ *
+ * \param reg the register.
+ * \param val the value to write.
+ * \return 0, on success.
+ * -EFAULT, on error.
+ */
+static int ad986x_write_reg(uint8_t reg, uint8_t val)
+{
+ uint8_t command[2];
+ uint8_t header;
+
+ //Create SPI tx transfer : 1b(r/w) + 2b(nbbytes) + 5b(addr) + 8b(data)
+ header = (uint8_t)((0 << 7) | (2 << 5) | (reg & 0x1F));
+
+ if(spi_dev->mode & SPI_LSB_FIRST)
+ {
+ command[0] = val;
+ command[1] = header;
+ }
+ else //MSB first need to swap
+ {
+ command[0] = header;
+ command[1] = val;
+ }
+
+ //Send it
+ return spi_write(spi_dev, &command[0], 2) ? -EFAULT : 0;
+}
+
+/**
* IOCTL commands for AD986X device.
*
* \param inode inode structure.
@@ -59,7 +110,6 @@ static int ad986x_ioctl(struct inode *inode, struct file *file, unsigned int cmd
{
void __user *argp = (void __user *)arg;
struct afe_info info;
- uint8_t command[2];
switch(cmd)
{
@@ -68,11 +118,7 @@ static int ad986x_ioctl(struct inode *inode, struct file *file, unsigned int cmd
if(copy_from_user(&info, argp, sizeof(struct afe_info)))
return -EFAULT;
- //Create SPI tx command : 1b(r/w) + 2b(nbbytes) + 5b(addr)
- command[0] = (uint8_t)((1 << 7) | (1 << 5) | (info.reg & 0x1F));
-
- //Send it and wait the response
- if(spi_write_then_read(spi_dev, &command[0], 1, &info.val, 1))
+ if (ad986x_read_reg(info.reg, &info.val))
return -EFAULT;
//Give the response to user
@@ -84,21 +130,7 @@ static int ad986x_ioctl(struct inode *inode, struct file *file, unsigned int cmd
if(copy_from_user(&info, argp, sizeof(struct afe_info)))
return -EFAULT;
- //Create SPI tx command : 1b(r/w) + 2b(nbbytes) + 5b(addr) + 8b(data)
- if(spi_dev->mode & SPI_LSB_FIRST)
- {
- command[0] = info.val;
- command[1] = (uint8_t)((0 << 7) | (2 << 5) | (info.reg & 0x1F));
- }
- else //MSB first need to swap
- {
- command[0] = (uint8_t)((0 << 7) | (2 << 5) | (info.reg & 0x1F));
- command[1] = info.val;
- }
-
- //Send it
- return spi_write(spi_dev, &command[0], 2) ? -EFAULT : 0;
-
+ return ad986x_write_reg(info.reg, info.val);
default:
return -EFAULT;