WV511AAF.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * MD218A voice coil motor driver
  3. *
  4. *
  5. */
  6. #include <linux/i2c.h>
  7. #include <linux/delay.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/cdev.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/fs.h>
  12. #include <asm/atomic.h>
  13. #ifdef CONFIG_COMPAT
  14. #include <linux/compat.h>
  15. #endif
  16. #include "lens_info.h"
  17. #define AF_DRVNAME "WV511AAF"
  18. #define I2C_SLAVE_ADDRESS 0x18
  19. #define AF_DEBUG
  20. #ifdef AF_DEBUG
  21. #define LOG_INF(format, args...) pr_debug(AF_DRVNAME " [%s] " format, __func__, ##args)
  22. #else
  23. #define LOG_INF(format, args...)
  24. #endif
  25. static spinlock_t *g_AF_SpinLock;
  26. static struct i2c_client *g_pstAF_I2Cclient;
  27. static int *g_s4AF_Opened;
  28. static int g_i4Dir;
  29. static int g_sr;
  30. static int g_i4MotorStatus;
  31. static unsigned long g_u4AF_INF;
  32. static unsigned long g_u4AF_MACRO = 1023;
  33. static unsigned long g_u4TargetPosition;
  34. static unsigned long g_u4CurrPosition;
  35. static int s4AF_ReadReg(unsigned short *a_pu2Result)
  36. {
  37. int i4RetValue = 0;
  38. char pBuff[2];
  39. i4RetValue = i2c_master_recv(g_pstAF_I2Cclient, pBuff, 2);
  40. if (i4RetValue < 0) {
  41. LOG_INF("I2C read failed!!\n");
  42. return -1;
  43. }
  44. *a_pu2Result = (((u16) pBuff[0]) << 4) + (pBuff[1] >> 4);
  45. return 0;
  46. }
  47. static int s4AF_WriteReg(u16 a_u2Data)
  48. {
  49. int i4RetValue = 0;
  50. char puSendCmd[2] = { (char)(a_u2Data >> 4), (char)((a_u2Data & 0xF) << 4) };
  51. /* LOG_INF("g_sr %d, write %d\n", g_sr, a_u2Data); */
  52. /* g_pstAF_I2Cclient->ext_flag |= I2C_A_FILTER_MSG; */
  53. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, puSendCmd, 2);
  54. if (i4RetValue < 0) {
  55. LOG_INF("I2C send failed!!\n");
  56. return -1;
  57. }
  58. return 0;
  59. }
  60. static inline int getAFInfo(__user stAF_MotorInfo * pstMotorInfo)
  61. {
  62. stAF_MotorInfo stMotorInfo;
  63. stMotorInfo.u4MacroPosition = g_u4AF_MACRO;
  64. stMotorInfo.u4InfPosition = g_u4AF_INF;
  65. stMotorInfo.u4CurrentPosition = g_u4CurrPosition;
  66. stMotorInfo.bIsSupportSR = 1;
  67. if (g_i4MotorStatus == 1)
  68. stMotorInfo.bIsMotorMoving = 1;
  69. else
  70. stMotorInfo.bIsMotorMoving = 0;
  71. if (*g_s4AF_Opened >= 1)
  72. stMotorInfo.bIsMotorOpen = 1;
  73. else
  74. stMotorInfo.bIsMotorOpen = 0;
  75. if (copy_to_user(pstMotorInfo, &stMotorInfo, sizeof(stAF_MotorInfo)))
  76. LOG_INF("copy to user failed when getting motor information\n");
  77. return 0;
  78. }
  79. #ifdef LensdrvCM3
  80. static inline int getAFMETA(__user stWV511AAF_MotorMETAInfo * pstMotorMETAInfo)
  81. {
  82. stWV511AAF_MotorMETAInfo stMotorMETAInfo;
  83. stMotorMETAInfo.Aperture = 2.8; /* fn */
  84. stMotorMETAInfo.Facing = 1;
  85. stMotorMETAInfo.FilterDensity = 1; /* X */
  86. stMotorMETAInfo.FocalDistance = 1.0; /* diopters */
  87. stMotorMETAInfo.FocalLength = 34.0; /* mm */
  88. stMotorMETAInfo.FocusRange = 1.0; /* diopters */
  89. stMotorMETAInfo.InfoAvalibleApertures = 2.8;
  90. stMotorMETAInfo.InfoAvalibleFilterDensity = 1;
  91. stMotorMETAInfo.InfoAvalibleFocalLength = 34.0;
  92. stMotorMETAInfo.InfoAvalibleHypeDistance = 1.0;
  93. stMotorMETAInfo.InfoAvalibleMinFocusDistance = 1.0;
  94. stMotorMETAInfo.InfoAvalibleOptStabilization = 0;
  95. stMotorMETAInfo.OpticalAxisAng[0] = 0.0;
  96. stMotorMETAInfo.OpticalAxisAng[1] = 0.0;
  97. stMotorMETAInfo.Position[0] = 0.0;
  98. stMotorMETAInfo.Position[1] = 0.0;
  99. stMotorMETAInfo.Position[2] = 0.0;
  100. stMotorMETAInfo.State = 0;
  101. stMotorMETAInfo.u4OIS_Mode = 0;
  102. if (copy_to_user(pstMotorMETAInfo, &stMotorMETAInfo, sizeof(stWV511AAF_MotorMETAInfo)))
  103. LOG_INF("copy to user failed when getting motor information\n");
  104. return 0;
  105. }
  106. #endif
  107. static inline int moveAF(unsigned long a_u4Position)
  108. {
  109. int ret = 0;
  110. if ((a_u4Position > g_u4AF_MACRO) || (a_u4Position < g_u4AF_INF)) {
  111. LOG_INF("out of range\n");
  112. return -EINVAL;
  113. }
  114. if (*g_s4AF_Opened == 1) {
  115. unsigned short InitPos;
  116. ret = s4AF_ReadReg(&InitPos);
  117. if (ret == 0) {
  118. LOG_INF("Init Pos %6d\n", InitPos);
  119. spin_lock(g_AF_SpinLock);
  120. g_u4CurrPosition = (unsigned long)InitPos;
  121. spin_unlock(g_AF_SpinLock);
  122. } else {
  123. spin_lock(g_AF_SpinLock);
  124. g_u4CurrPosition = 0;
  125. spin_unlock(g_AF_SpinLock);
  126. }
  127. spin_lock(g_AF_SpinLock);
  128. *g_s4AF_Opened = 2;
  129. spin_unlock(g_AF_SpinLock);
  130. }
  131. if (g_u4CurrPosition < a_u4Position) {
  132. spin_lock(g_AF_SpinLock);
  133. g_i4Dir = 1;
  134. spin_unlock(g_AF_SpinLock);
  135. } else if (g_u4CurrPosition > a_u4Position) {
  136. spin_lock(g_AF_SpinLock);
  137. g_i4Dir = -1;
  138. spin_unlock(g_AF_SpinLock);
  139. } else {
  140. return 0;
  141. }
  142. spin_lock(g_AF_SpinLock);
  143. g_u4TargetPosition = a_u4Position;
  144. spin_unlock(g_AF_SpinLock);
  145. /* LOG_INF("move [curr] %d [target] %d\n", g_u4CurrPosition, g_u4TargetPosition); */
  146. spin_lock(g_AF_SpinLock);
  147. g_sr = 3;
  148. g_i4MotorStatus = 0;
  149. spin_unlock(g_AF_SpinLock);
  150. if (s4AF_WriteReg((unsigned short)g_u4TargetPosition) == 0) {
  151. spin_lock(g_AF_SpinLock);
  152. g_u4CurrPosition = (unsigned long)g_u4TargetPosition;
  153. spin_unlock(g_AF_SpinLock);
  154. } else {
  155. LOG_INF("set I2C failed when moving the motor\n");
  156. spin_lock(g_AF_SpinLock);
  157. g_i4MotorStatus = -1;
  158. spin_unlock(g_AF_SpinLock);
  159. }
  160. return 0;
  161. }
  162. static inline int setAFInf(unsigned long a_u4Position)
  163. {
  164. spin_lock(g_AF_SpinLock);
  165. g_u4AF_INF = a_u4Position;
  166. spin_unlock(g_AF_SpinLock);
  167. return 0;
  168. }
  169. static inline int setAFMacro(unsigned long a_u4Position)
  170. {
  171. spin_lock(g_AF_SpinLock);
  172. g_u4AF_MACRO = a_u4Position;
  173. spin_unlock(g_AF_SpinLock);
  174. return 0;
  175. }
  176. /* ////////////////////////////////////////////////////////////// */
  177. long WV511AAF_Ioctl(struct file *a_pstFile, unsigned int a_u4Command, unsigned long a_u4Param)
  178. {
  179. long i4RetValue = 0;
  180. switch (a_u4Command) {
  181. case AFIOC_G_MOTORINFO:
  182. i4RetValue = getAFInfo((__user stAF_MotorInfo *) (a_u4Param));
  183. break;
  184. #ifdef LensdrvCM3
  185. case WV511AAFIOC_G_MOTORMETAINFO:
  186. i4RetValue = getAFMETA((__user stAF_MotorInfo *) (a_u4Param));
  187. break;
  188. #endif
  189. case AFIOC_T_MOVETO:
  190. i4RetValue = moveAF(a_u4Param);
  191. break;
  192. case AFIOC_T_SETINFPOS:
  193. i4RetValue = setAFInf(a_u4Param);
  194. break;
  195. case AFIOC_T_SETMACROPOS:
  196. i4RetValue = setAFMacro(a_u4Param);
  197. break;
  198. default:
  199. LOG_INF("No CMD\n");
  200. i4RetValue = -EPERM;
  201. break;
  202. }
  203. return i4RetValue;
  204. }
  205. /* Main jobs: */
  206. /* 1.Deallocate anything that "open" allocated in private_data. */
  207. /* 2.Shut down the device on last close. */
  208. /* 3.Only called once on last time. */
  209. /* Q1 : Try release multiple times. */
  210. int WV511AAF_Release(struct inode *a_pstInode, struct file *a_pstFile)
  211. {
  212. LOG_INF("Start\n");
  213. if (*g_s4AF_Opened == 2) {
  214. g_sr = 5;
  215. s4AF_WriteReg(200);
  216. msleep(20);
  217. s4AF_WriteReg(100);
  218. msleep(20);
  219. }
  220. if (*g_s4AF_Opened) {
  221. LOG_INF("Free\n");
  222. spin_lock(g_AF_SpinLock);
  223. *g_s4AF_Opened = 0;
  224. spin_unlock(g_AF_SpinLock);
  225. }
  226. LOG_INF("End\n");
  227. return 0;
  228. }
  229. void WV511AAF_SetI2Cclient(struct i2c_client *pstAF_I2Cclient, spinlock_t *pAF_SpinLock,
  230. int *pAF_Opened)
  231. {
  232. printk("zhanghf %s \n",__func__);
  233. g_pstAF_I2Cclient = pstAF_I2Cclient;
  234. g_AF_SpinLock = pAF_SpinLock;
  235. g_s4AF_Opened = pAF_Opened;
  236. }