AK7345AF.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * AK7345AF voice coil motor driver
  3. *
  4. *
  5. */
  6. #include <linux/i2c.h>
  7. #include <linux/delay.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/fs.h>
  10. #include "lens_info.h"
  11. #define AF_DRVNAME "AK7345AF_DRV"
  12. #define AF_I2C_SLAVE_ADDR 0x18
  13. #define AF_DEBUG
  14. #ifdef AF_DEBUG
  15. #define LOG_INF(format, args...) pr_debug(AF_DRVNAME " [%s] " format, __func__, ##args)
  16. #else
  17. #define LOG_INF(format, args...)
  18. #endif
  19. static struct i2c_client *g_pstAF_I2Cclient;
  20. static int *g_pAF_Opened;
  21. static spinlock_t *g_pAF_SpinLock;
  22. static unsigned long g_u4AF_INF;
  23. static unsigned long g_u4AF_MACRO = 1023;
  24. static unsigned long g_u4TargetPosition;
  25. static unsigned long g_u4CurrPosition;
  26. static int s4AF_ReadReg(u16 a_u2Addr, u16 *a_pu2Result)
  27. {
  28. int i4RetValue = 0;
  29. char pBuff;
  30. g_pstAF_I2Cclient->addr = AF_I2C_SLAVE_ADDR;
  31. g_pstAF_I2Cclient->addr = g_pstAF_I2Cclient->addr >> 1;
  32. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, &a_u2Addr, 1);
  33. if (i4RetValue < 0) {
  34. LOG_INF("I2C read - send failed!!\n");
  35. return -1;
  36. }
  37. i4RetValue = i2c_master_recv(g_pstAF_I2Cclient, &pBuff, 1);
  38. if (i4RetValue < 0) {
  39. LOG_INF("I2C read - recv failed!!\n");
  40. return -1;
  41. }
  42. *a_pu2Result = pBuff;
  43. return 0;
  44. }
  45. static int s4AF_WriteReg(u16 a_u2Addr, u16 a_u2Data)
  46. {
  47. int i4RetValue = 0;
  48. char puSendCmd[2] = { (char)a_u2Addr, (char)a_u2Data };
  49. g_pstAF_I2Cclient->addr = AF_I2C_SLAVE_ADDR;
  50. g_pstAF_I2Cclient->addr = g_pstAF_I2Cclient->addr >> 1;
  51. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, puSendCmd, 2);
  52. if (i4RetValue < 0) {
  53. LOG_INF("I2C write failed!!\n");
  54. return -1;
  55. }
  56. return 0;
  57. }
  58. static inline int getAFInfo(__user stAF_MotorInfo * pstMotorInfo)
  59. {
  60. stAF_MotorInfo stMotorInfo;
  61. stMotorInfo.u4MacroPosition = g_u4AF_MACRO;
  62. stMotorInfo.u4InfPosition = g_u4AF_INF;
  63. stMotorInfo.u4CurrentPosition = g_u4CurrPosition;
  64. stMotorInfo.bIsSupportSR = 1;
  65. stMotorInfo.bIsMotorMoving = 1;
  66. if (*g_pAF_Opened >= 1)
  67. stMotorInfo.bIsMotorOpen = 1;
  68. else
  69. stMotorInfo.bIsMotorOpen = 0;
  70. if (copy_to_user(pstMotorInfo, &stMotorInfo, sizeof(stAF_MotorInfo)))
  71. LOG_INF("copy to user failed when getting motor information\n");
  72. return 0;
  73. }
  74. static inline int moveAF(unsigned long a_u4Position)
  75. {
  76. int ret = 0;
  77. if ((a_u4Position > g_u4AF_MACRO) || (a_u4Position < g_u4AF_INF)) {
  78. LOG_INF("out of range\n");
  79. return -EINVAL;
  80. }
  81. if (*g_pAF_Opened == 1) {
  82. unsigned short InitPos, InitPosM, InitPosL;
  83. /* 00:active mode 10:Standby mode x1:Sleep mode */
  84. s4AF_WriteReg(0x02, 0x00); /* from Standby mode to Active mode */
  85. msleep(20);
  86. s4AF_ReadReg(0x0, &InitPosM);
  87. ret = s4AF_ReadReg(0x1, &InitPosL);
  88. InitPos = ((InitPosM & 0xFF) << 1) + ((InitPosL >> 7) & 1);
  89. if (ret == 0) {
  90. LOG_INF("Init Pos %6d\n", InitPos);
  91. spin_lock(g_pAF_SpinLock);
  92. g_u4CurrPosition = (unsigned long)InitPos;
  93. spin_unlock(g_pAF_SpinLock);
  94. } else {
  95. spin_lock(g_pAF_SpinLock);
  96. g_u4CurrPosition = 0;
  97. spin_unlock(g_pAF_SpinLock);
  98. }
  99. spin_lock(g_pAF_SpinLock);
  100. *g_pAF_Opened = 2;
  101. spin_unlock(g_pAF_SpinLock);
  102. }
  103. if (g_u4CurrPosition == a_u4Position)
  104. return 0;
  105. spin_lock(g_pAF_SpinLock);
  106. g_u4TargetPosition = a_u4Position;
  107. spin_unlock(g_pAF_SpinLock);
  108. /* LOG_INF("move [curr] %d [target] %d\n", g_u4CurrPosition, g_u4TargetPosition); */
  109. s4AF_WriteReg(0x02, 0x00);
  110. if (s4AF_WriteReg(0x0, (u16) ((g_u4TargetPosition >> 2) & 0xff)) == 0 &&
  111. s4AF_WriteReg(0x1, (u16) (((g_u4TargetPosition >> 1) & 1) << 7)) == 0) {
  112. spin_lock(g_pAF_SpinLock);
  113. g_u4CurrPosition = (unsigned long)g_u4TargetPosition;
  114. spin_unlock(g_pAF_SpinLock);
  115. } else {
  116. LOG_INF("set I2C failed when moving the motor\n");
  117. }
  118. return 0;
  119. }
  120. static inline int setAFInf(unsigned long a_u4Position)
  121. {
  122. spin_lock(g_pAF_SpinLock);
  123. g_u4AF_INF = a_u4Position;
  124. spin_unlock(g_pAF_SpinLock);
  125. return 0;
  126. }
  127. static inline int setAFMacro(unsigned long a_u4Position)
  128. {
  129. spin_lock(g_pAF_SpinLock);
  130. g_u4AF_MACRO = a_u4Position;
  131. spin_unlock(g_pAF_SpinLock);
  132. return 0;
  133. }
  134. /* ////////////////////////////////////////////////////////////// */
  135. long AK7345AF_Ioctl(struct file *a_pstFile, unsigned int a_u4Command, unsigned long a_u4Param)
  136. {
  137. long i4RetValue = 0;
  138. switch (a_u4Command) {
  139. case AFIOC_G_MOTORINFO:
  140. i4RetValue = getAFInfo((__user stAF_MotorInfo *) (a_u4Param));
  141. break;
  142. case AFIOC_T_MOVETO:
  143. i4RetValue = moveAF(a_u4Param);
  144. break;
  145. case AFIOC_T_SETINFPOS:
  146. i4RetValue = setAFInf(a_u4Param);
  147. break;
  148. case AFIOC_T_SETMACROPOS:
  149. i4RetValue = setAFMacro(a_u4Param);
  150. break;
  151. default:
  152. LOG_INF("No CMD\n");
  153. i4RetValue = -EPERM;
  154. break;
  155. }
  156. return i4RetValue;
  157. }
  158. /* Main jobs: */
  159. /* 1.Deallocate anything that "open" allocated in private_data. */
  160. /* 2.Shut down the device on last close. */
  161. /* 3.Only called once on last time. */
  162. /* Q1 : Try release multiple times. */
  163. int AK7345AF_Release(struct inode *a_pstInode, struct file *a_pstFile)
  164. {
  165. LOG_INF("Start\n");
  166. if (*g_pAF_Opened == 2) {
  167. LOG_INF("Wait\n");
  168. msleep(20);
  169. }
  170. if (*g_pAF_Opened) {
  171. LOG_INF("Free\n");
  172. spin_lock(g_pAF_SpinLock);
  173. *g_pAF_Opened = 0;
  174. spin_unlock(g_pAF_SpinLock);
  175. }
  176. LOG_INF("End\n");
  177. return 0;
  178. }
  179. void AK7345AF_SetI2Cclient(struct i2c_client *pstAF_I2Cclient, spinlock_t *pAF_SpinLock, int *pAF_Opened)
  180. {
  181. g_pstAF_I2Cclient = pstAF_I2Cclient;
  182. g_pAF_SpinLock = pAF_SpinLock;
  183. g_pAF_Opened = pAF_Opened;
  184. }