BU6424AF.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * BU6424AF 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 "BU6424AF_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(unsigned short *a_pu2Result)
  27. {
  28. int i4RetValue = 0;
  29. char pBuff[2];
  30. g_pstAF_I2Cclient->addr = AF_I2C_SLAVE_ADDR;
  31. g_pstAF_I2Cclient->addr = g_pstAF_I2Cclient->addr >> 1;
  32. i4RetValue = i2c_master_recv(g_pstAF_I2Cclient, pBuff, 2);
  33. if (i4RetValue < 0) {
  34. LOG_INF("I2C read failed!!\n");
  35. return -1;
  36. }
  37. *a_pu2Result = (((u16)(pBuff[0] & 0x03)) << 8) + pBuff[1];
  38. return 0;
  39. }
  40. static int s4AF_WriteReg(u16 a_u2Data)
  41. {
  42. int i4RetValue = 0;
  43. char puSendCmd[2] = {(char)(((a_u2Data >> 8) & 0x03) | 0xc0), (char)(a_u2Data & 0xff)};
  44. g_pstAF_I2Cclient->addr = AF_I2C_SLAVE_ADDR;
  45. g_pstAF_I2Cclient->addr = g_pstAF_I2Cclient->addr >> 1;
  46. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, puSendCmd, 2);
  47. if (i4RetValue < 0) {
  48. LOG_INF("I2C send failed!!\n");
  49. return -1;
  50. }
  51. return 0;
  52. }
  53. static inline int getAFInfo(__user stAF_MotorInfo * pstMotorInfo)
  54. {
  55. stAF_MotorInfo stMotorInfo;
  56. stMotorInfo.u4MacroPosition = g_u4AF_MACRO;
  57. stMotorInfo.u4InfPosition = g_u4AF_INF;
  58. stMotorInfo.u4CurrentPosition = g_u4CurrPosition;
  59. stMotorInfo.bIsSupportSR = 1;
  60. stMotorInfo.bIsMotorMoving = 1;
  61. if (*g_pAF_Opened >= 1)
  62. stMotorInfo.bIsMotorOpen = 1;
  63. else
  64. stMotorInfo.bIsMotorOpen = 0;
  65. if (copy_to_user(pstMotorInfo, &stMotorInfo, sizeof(stAF_MotorInfo)))
  66. LOG_INF("copy to user failed when getting motor information\n");
  67. return 0;
  68. }
  69. static inline int moveAF(unsigned long a_u4Position)
  70. {
  71. int ret = 0;
  72. if ((a_u4Position > g_u4AF_MACRO) || (a_u4Position < g_u4AF_INF)) {
  73. LOG_INF("out of range\n");
  74. return -EINVAL;
  75. }
  76. if (*g_pAF_Opened == 1) {
  77. unsigned short InitPos;
  78. ret = s4AF_ReadReg(&InitPos);
  79. if (ret == 0) {
  80. LOG_INF("Init Pos %6d\n", InitPos);
  81. spin_lock(g_pAF_SpinLock);
  82. g_u4CurrPosition = (unsigned long)InitPos;
  83. spin_unlock(g_pAF_SpinLock);
  84. } else {
  85. spin_lock(g_pAF_SpinLock);
  86. g_u4CurrPosition = 0;
  87. spin_unlock(g_pAF_SpinLock);
  88. }
  89. spin_lock(g_pAF_SpinLock);
  90. *g_pAF_Opened = 2;
  91. spin_unlock(g_pAF_SpinLock);
  92. }
  93. if (g_u4CurrPosition == a_u4Position)
  94. return 0;
  95. spin_lock(g_pAF_SpinLock);
  96. g_u4TargetPosition = a_u4Position;
  97. spin_unlock(g_pAF_SpinLock);
  98. /* LOG_INF("move [curr] %d [target] %d\n", g_u4CurrPosition, g_u4TargetPosition); */
  99. if (s4AF_WriteReg((unsigned short)g_u4TargetPosition) == 0) {
  100. spin_lock(g_pAF_SpinLock);
  101. g_u4CurrPosition = (unsigned long)g_u4TargetPosition;
  102. spin_unlock(g_pAF_SpinLock);
  103. } else {
  104. LOG_INF("set I2C failed when moving the motor\n");
  105. }
  106. return 0;
  107. }
  108. static inline int setAFInf(unsigned long a_u4Position)
  109. {
  110. spin_lock(g_pAF_SpinLock);
  111. g_u4AF_INF = a_u4Position;
  112. spin_unlock(g_pAF_SpinLock);
  113. return 0;
  114. }
  115. static inline int setAFMacro(unsigned long a_u4Position)
  116. {
  117. spin_lock(g_pAF_SpinLock);
  118. g_u4AF_MACRO = a_u4Position;
  119. spin_unlock(g_pAF_SpinLock);
  120. return 0;
  121. }
  122. /* ////////////////////////////////////////////////////////////// */
  123. long BU6424AF_Ioctl(struct file *a_pstFile, unsigned int a_u4Command, unsigned long a_u4Param)
  124. {
  125. long i4RetValue = 0;
  126. switch (a_u4Command) {
  127. case AFIOC_G_MOTORINFO:
  128. i4RetValue = getAFInfo((__user stAF_MotorInfo *) (a_u4Param));
  129. break;
  130. case AFIOC_T_MOVETO:
  131. i4RetValue = moveAF(a_u4Param);
  132. break;
  133. case AFIOC_T_SETINFPOS:
  134. i4RetValue = setAFInf(a_u4Param);
  135. break;
  136. case AFIOC_T_SETMACROPOS:
  137. i4RetValue = setAFMacro(a_u4Param);
  138. break;
  139. default:
  140. LOG_INF("No CMD\n");
  141. i4RetValue = -EPERM;
  142. break;
  143. }
  144. return i4RetValue;
  145. }
  146. /* Main jobs: */
  147. /* 1.Deallocate anything that "open" allocated in private_data. */
  148. /* 2.Shut down the device on last close. */
  149. /* 3.Only called once on last time. */
  150. /* Q1 : Try release multiple times. */
  151. int BU6424AF_Release(struct inode *a_pstInode, struct file *a_pstFile)
  152. {
  153. LOG_INF("Start\n");
  154. if (*g_pAF_Opened == 2) {
  155. char puSendCmd[2];
  156. puSendCmd[0] = (char)(0x00);
  157. puSendCmd[1] = (char)(0x00);
  158. i2c_master_send(g_pstAF_I2Cclient, puSendCmd, 2);
  159. LOG_INF("Wait\n");
  160. /*s4AF_WriteReg(200);
  161. msleep(20);
  162. s4AF_WriteReg(100);
  163. msleep(20);*/
  164. }
  165. if (*g_pAF_Opened) {
  166. LOG_INF("Free\n");
  167. spin_lock(g_pAF_SpinLock);
  168. *g_pAF_Opened = 0;
  169. spin_unlock(g_pAF_SpinLock);
  170. }
  171. LOG_INF("End\n");
  172. return 0;
  173. }
  174. void BU6424AF_SetI2Cclient(struct i2c_client *pstAF_I2Cclient, spinlock_t *pAF_SpinLock, int *pAF_Opened)
  175. {
  176. g_pstAF_I2Cclient = pstAF_I2Cclient;
  177. g_pAF_SpinLock = pAF_SpinLock;
  178. g_pAF_Opened = pAF_Opened;
  179. }