| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- /*
- * AD5823AF voice coil motor driver
- *
- *
- */
- #include <linux/i2c.h>
- #include <linux/delay.h>
- #include <linux/uaccess.h>
- #include <linux/fs.h>
- #include "lens_info.h"
- #define AF_DRVNAME "AD5823AF_DRV"
- #define AF_I2C_SLAVE_ADDR 0x0C
- #define AF_DEBUG
- #ifdef AF_DEBUG
- #define LOG_INF(format, args...) pr_debug(AF_DRVNAME " [%s] " format, __func__, ##args)
- #else
- #define LOG_INF(format, args...)
- #endif
- static struct i2c_client *g_pstAF_I2Cclient;
- static int *g_pAF_Opened;
- static spinlock_t *g_pAF_SpinLock;
- static unsigned long g_u4AF_INF;
- static unsigned long g_u4AF_MACRO = 1023;
- static unsigned long g_u4TargetPosition;
- static unsigned long g_u4CurrPosition;
- static int s4AF_ReadReg(unsigned short *a_pu2Result)
- {
- int i4RetValue = 0;
- char pBuff[2];
- char VCMMSB[1] = { (char)(0x04) };
- char VCMLSB[1] = { (char)(0x05) };
- g_pstAF_I2Cclient->addr = AF_I2C_SLAVE_ADDR;
- g_pstAF_I2Cclient->addr = g_pstAF_I2Cclient->addr >> 1;
- /* Read MSB */
- i4RetValue = i2c_master_send(g_pstAF_I2Cclient, VCMMSB, 1);
- if (i4RetValue < 0) {
- LOG_INF("I2C read MSB send failed!!\n");
- return -1;
- }
- i4RetValue = i2c_master_recv(g_pstAF_I2Cclient, &pBuff[1], 1);
- if (i4RetValue < 0) {
- LOG_INF("I2C read MSB recv failed!!\n");
- return -1;
- }
- /* Read LSB */
- i4RetValue = i2c_master_send(g_pstAF_I2Cclient, VCMLSB, 1);
- if (i4RetValue < 0) {
- LOG_INF("I2C read LSB send failed!!\n");
- return -1;
- }
- i4RetValue = i2c_master_recv(g_pstAF_I2Cclient, &pBuff[0], 1);
- if (i4RetValue < 0) {
- LOG_INF("I2C read LSB recv failed!!\n");
- return -1;
- }
- *a_pu2Result = ((u16) pBuff[0] + (u16) (pBuff[1] << 8));
- return 0;
- }
- static int s4AF_WriteReg(u16 a_u2Data)
- {
- int i4RetValue = 0;
- /* 0x04[1:0] VCM MSB data */
- /* 0x05[7:0] VCM LSB data */
- char VCMMSB[2] = { (char)(0x04), (char)((a_u2Data >> 8) & 0x03) };
- char VCMLSB[2] = { (char)(0x05), (char)(a_u2Data & 0xFF) };
- g_pstAF_I2Cclient->addr = AF_I2C_SLAVE_ADDR;
- g_pstAF_I2Cclient->addr = g_pstAF_I2Cclient->addr >> 1;
- i4RetValue = i2c_master_send(g_pstAF_I2Cclient, VCMMSB, 2);
- i4RetValue = i2c_master_send(g_pstAF_I2Cclient, VCMLSB, 2);
- if (i4RetValue < 0) {
- LOG_INF("I2C send failed!!\n");
- return -1;
- }
- return 0;
- }
- static inline int getAFInfo(__user stAF_MotorInfo *pstMotorInfo)
- {
- stAF_MotorInfo stMotorInfo;
- stMotorInfo.u4MacroPosition = g_u4AF_MACRO;
- stMotorInfo.u4InfPosition = g_u4AF_INF;
- stMotorInfo.u4CurrentPosition = g_u4CurrPosition;
- stMotorInfo.bIsSupportSR = 1;
- stMotorInfo.bIsMotorMoving = 1;
- if (*g_pAF_Opened >= 1)
- stMotorInfo.bIsMotorOpen = 1;
- else
- stMotorInfo.bIsMotorOpen = 0;
- if (copy_to_user(pstMotorInfo, &stMotorInfo, sizeof(stAF_MotorInfo)))
- LOG_INF("copy to user failed when getting motor information\n");
- return 0;
- }
- static int AD5823_Mode_Init(void)
- {
- int i4RetValue = 0;
- char Mode[2] = { (char)(0x02), (char)(0x01) };
- char MoveTime[2] = { (char)(0x03), (char)(0x4B) };
- char VCMMSB[2] = { (char)(0x04), (char)(0x05) };
- char VCMLSB[2] = { (char)(0x05), (char)(0x32) };
- LOG_INF("mode_init : 0x02\n");
- g_pstAF_I2Cclient->addr = AF_I2C_SLAVE_ADDR;
- g_pstAF_I2Cclient->addr = g_pstAF_I2Cclient->addr >> 1;
- i4RetValue = i2c_master_send(g_pstAF_I2Cclient, Mode, 2);
- i4RetValue = i2c_master_send(g_pstAF_I2Cclient, MoveTime, 2);
- #if 1
- i4RetValue = i2c_master_send(g_pstAF_I2Cclient, VCMMSB, 2);
- i4RetValue = i2c_master_send(g_pstAF_I2Cclient, VCMLSB, 2);
- #endif
- return 0;
- }
- static inline int moveAF(unsigned long a_u4Position)
- {
- int ret = 0;
- if ((a_u4Position > g_u4AF_MACRO) || (a_u4Position < g_u4AF_INF)) {
- LOG_INF("out of range\n");
- return -EINVAL;
- }
- if (*g_pAF_Opened == 1) {
- unsigned short InitPos;
- AD5823_Mode_Init();
- ret = s4AF_ReadReg(&InitPos);
- if (ret == 0) {
- LOG_INF("Init Pos %6d\n", InitPos);
- spin_lock(g_pAF_SpinLock);
- g_u4CurrPosition = (unsigned long)InitPos;
- spin_unlock(g_pAF_SpinLock);
- } else {
- spin_lock(g_pAF_SpinLock);
- g_u4CurrPosition = 0;
- spin_unlock(g_pAF_SpinLock);
- }
- spin_lock(g_pAF_SpinLock);
- *g_pAF_Opened = 2;
- spin_unlock(g_pAF_SpinLock);
- }
- if (g_u4CurrPosition == a_u4Position)
- return 0;
- spin_lock(g_pAF_SpinLock);
- g_u4TargetPosition = a_u4Position;
- spin_unlock(g_pAF_SpinLock);
- /* LOG_INF("move [curr] %d [target] %d\n", g_u4CurrPosition, g_u4TargetPosition); */
- if (s4AF_WriteReg((unsigned short)g_u4TargetPosition) == 0) {
- spin_lock(g_pAF_SpinLock);
- g_u4CurrPosition = (unsigned long)g_u4TargetPosition;
- spin_unlock(g_pAF_SpinLock);
- } else {
- LOG_INF("set I2C failed when moving the motor\n");
- }
- return 0;
- }
- static inline int setAFInf(unsigned long a_u4Position)
- {
- spin_lock(g_pAF_SpinLock);
- g_u4AF_INF = a_u4Position;
- spin_unlock(g_pAF_SpinLock);
- return 0;
- }
- static inline int setAFMacro(unsigned long a_u4Position)
- {
- spin_lock(g_pAF_SpinLock);
- g_u4AF_MACRO = a_u4Position;
- spin_unlock(g_pAF_SpinLock);
- return 0;
- }
- /* ////////////////////////////////////////////////////////////// */
- long AD5823AF_Ioctl(struct file *a_pstFile, unsigned int a_u4Command, unsigned long a_u4Param)
- {
- long i4RetValue = 0;
- switch (a_u4Command) {
- case AFIOC_G_MOTORINFO:
- i4RetValue = getAFInfo((__user stAF_MotorInfo *) (a_u4Param));
- break;
- case AFIOC_T_MOVETO:
- i4RetValue = moveAF(a_u4Param);
- break;
- case AFIOC_T_SETINFPOS:
- i4RetValue = setAFInf(a_u4Param);
- break;
- case AFIOC_T_SETMACROPOS:
- i4RetValue = setAFMacro(a_u4Param);
- break;
- default:
- LOG_INF("No CMD\n");
- i4RetValue = -EPERM;
- break;
- }
- return i4RetValue;
- }
- /* Main jobs: */
- /* 1.Deallocate anything that "open" allocated in private_data. */
- /* 2.Shut down the device on last close. */
- /* 3.Only called once on last time. */
- /* Q1 : Try release multiple times. */
- int AD5823AF_Release(struct inode *a_pstInode, struct file *a_pstFile)
- {
- LOG_INF("Start\n");
- if (*g_pAF_Opened == 2) {
- LOG_INF("Wait\n");
- s4AF_WriteReg(200);
- msleep(20);
- s4AF_WriteReg(100);
- msleep(20);
- }
- if (*g_pAF_Opened) {
- LOG_INF("Free\n");
- spin_lock(g_pAF_SpinLock);
- *g_pAF_Opened = 0;
- spin_unlock(g_pAF_SpinLock);
- }
- LOG_INF("End\n");
- return 0;
- }
- void AD5823AF_SetI2Cclient(struct i2c_client *pstAF_I2Cclient, spinlock_t *pAF_SpinLock, int *pAF_Opened)
- {
- g_pstAF_I2Cclient = pstAF_I2Cclient;
- g_pAF_SpinLock = pAF_SpinLock;
- g_pAF_Opened = pAF_Opened;
- }
|