FM50AF.c 4.8 KB

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