AD5823AF.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * AD5823AF 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 "AD5823AF_DRV"
  12. #define AF_I2C_SLAVE_ADDR 0x0C
  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. char VCMMSB[1] = { (char)(0x04) };
  31. char VCMLSB[1] = { (char)(0x05) };
  32. g_pstAF_I2Cclient->addr = AF_I2C_SLAVE_ADDR;
  33. g_pstAF_I2Cclient->addr = g_pstAF_I2Cclient->addr >> 1;
  34. /* Read MSB */
  35. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, VCMMSB, 1);
  36. if (i4RetValue < 0) {
  37. LOG_INF("I2C read MSB send failed!!\n");
  38. return -1;
  39. }
  40. i4RetValue = i2c_master_recv(g_pstAF_I2Cclient, &pBuff[1], 1);
  41. if (i4RetValue < 0) {
  42. LOG_INF("I2C read MSB recv failed!!\n");
  43. return -1;
  44. }
  45. /* Read LSB */
  46. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, VCMLSB, 1);
  47. if (i4RetValue < 0) {
  48. LOG_INF("I2C read LSB send failed!!\n");
  49. return -1;
  50. }
  51. i4RetValue = i2c_master_recv(g_pstAF_I2Cclient, &pBuff[0], 1);
  52. if (i4RetValue < 0) {
  53. LOG_INF("I2C read LSB recv failed!!\n");
  54. return -1;
  55. }
  56. *a_pu2Result = ((u16) pBuff[0] + (u16) (pBuff[1] << 8));
  57. return 0;
  58. }
  59. static int s4AF_WriteReg(u16 a_u2Data)
  60. {
  61. int i4RetValue = 0;
  62. /* 0x04[1:0] VCM MSB data */
  63. /* 0x05[7:0] VCM LSB data */
  64. char VCMMSB[2] = { (char)(0x04), (char)((a_u2Data >> 8) & 0x03) };
  65. char VCMLSB[2] = { (char)(0x05), (char)(a_u2Data & 0xFF) };
  66. g_pstAF_I2Cclient->addr = AF_I2C_SLAVE_ADDR;
  67. g_pstAF_I2Cclient->addr = g_pstAF_I2Cclient->addr >> 1;
  68. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, VCMMSB, 2);
  69. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, VCMLSB, 2);
  70. if (i4RetValue < 0) {
  71. LOG_INF("I2C send failed!!\n");
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. static inline int getAFInfo(__user stAF_MotorInfo *pstMotorInfo)
  77. {
  78. stAF_MotorInfo stMotorInfo;
  79. stMotorInfo.u4MacroPosition = g_u4AF_MACRO;
  80. stMotorInfo.u4InfPosition = g_u4AF_INF;
  81. stMotorInfo.u4CurrentPosition = g_u4CurrPosition;
  82. stMotorInfo.bIsSupportSR = 1;
  83. stMotorInfo.bIsMotorMoving = 1;
  84. if (*g_pAF_Opened >= 1)
  85. stMotorInfo.bIsMotorOpen = 1;
  86. else
  87. stMotorInfo.bIsMotorOpen = 0;
  88. if (copy_to_user(pstMotorInfo, &stMotorInfo, sizeof(stAF_MotorInfo)))
  89. LOG_INF("copy to user failed when getting motor information\n");
  90. return 0;
  91. }
  92. static int AD5823_Mode_Init(void)
  93. {
  94. int i4RetValue = 0;
  95. char Mode[2] = { (char)(0x02), (char)(0x01) };
  96. char MoveTime[2] = { (char)(0x03), (char)(0x4B) };
  97. char VCMMSB[2] = { (char)(0x04), (char)(0x05) };
  98. char VCMLSB[2] = { (char)(0x05), (char)(0x32) };
  99. LOG_INF("mode_init : 0x02\n");
  100. g_pstAF_I2Cclient->addr = AF_I2C_SLAVE_ADDR;
  101. g_pstAF_I2Cclient->addr = g_pstAF_I2Cclient->addr >> 1;
  102. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, Mode, 2);
  103. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, MoveTime, 2);
  104. #if 1
  105. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, VCMMSB, 2);
  106. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, VCMLSB, 2);
  107. #endif
  108. return 0;
  109. }
  110. static inline int moveAF(unsigned long a_u4Position)
  111. {
  112. int ret = 0;
  113. if ((a_u4Position > g_u4AF_MACRO) || (a_u4Position < g_u4AF_INF)) {
  114. LOG_INF("out of range\n");
  115. return -EINVAL;
  116. }
  117. if (*g_pAF_Opened == 1) {
  118. unsigned short InitPos;
  119. AD5823_Mode_Init();
  120. ret = s4AF_ReadReg(&InitPos);
  121. if (ret == 0) {
  122. LOG_INF("Init Pos %6d\n", InitPos);
  123. spin_lock(g_pAF_SpinLock);
  124. g_u4CurrPosition = (unsigned long)InitPos;
  125. spin_unlock(g_pAF_SpinLock);
  126. } else {
  127. spin_lock(g_pAF_SpinLock);
  128. g_u4CurrPosition = 0;
  129. spin_unlock(g_pAF_SpinLock);
  130. }
  131. spin_lock(g_pAF_SpinLock);
  132. *g_pAF_Opened = 2;
  133. spin_unlock(g_pAF_SpinLock);
  134. }
  135. if (g_u4CurrPosition == a_u4Position)
  136. return 0;
  137. spin_lock(g_pAF_SpinLock);
  138. g_u4TargetPosition = a_u4Position;
  139. spin_unlock(g_pAF_SpinLock);
  140. /* LOG_INF("move [curr] %d [target] %d\n", g_u4CurrPosition, g_u4TargetPosition); */
  141. if (s4AF_WriteReg((unsigned short)g_u4TargetPosition) == 0) {
  142. spin_lock(g_pAF_SpinLock);
  143. g_u4CurrPosition = (unsigned long)g_u4TargetPosition;
  144. spin_unlock(g_pAF_SpinLock);
  145. } else {
  146. LOG_INF("set I2C failed when moving the motor\n");
  147. }
  148. return 0;
  149. }
  150. static inline int setAFInf(unsigned long a_u4Position)
  151. {
  152. spin_lock(g_pAF_SpinLock);
  153. g_u4AF_INF = a_u4Position;
  154. spin_unlock(g_pAF_SpinLock);
  155. return 0;
  156. }
  157. static inline int setAFMacro(unsigned long a_u4Position)
  158. {
  159. spin_lock(g_pAF_SpinLock);
  160. g_u4AF_MACRO = a_u4Position;
  161. spin_unlock(g_pAF_SpinLock);
  162. return 0;
  163. }
  164. /* ////////////////////////////////////////////////////////////// */
  165. long AD5823AF_Ioctl(struct file *a_pstFile, unsigned int a_u4Command, unsigned long a_u4Param)
  166. {
  167. long i4RetValue = 0;
  168. switch (a_u4Command) {
  169. case AFIOC_G_MOTORINFO:
  170. i4RetValue = getAFInfo((__user stAF_MotorInfo *) (a_u4Param));
  171. break;
  172. case AFIOC_T_MOVETO:
  173. i4RetValue = moveAF(a_u4Param);
  174. break;
  175. case AFIOC_T_SETINFPOS:
  176. i4RetValue = setAFInf(a_u4Param);
  177. break;
  178. case AFIOC_T_SETMACROPOS:
  179. i4RetValue = setAFMacro(a_u4Param);
  180. break;
  181. default:
  182. LOG_INF("No CMD\n");
  183. i4RetValue = -EPERM;
  184. break;
  185. }
  186. return i4RetValue;
  187. }
  188. /* Main jobs: */
  189. /* 1.Deallocate anything that "open" allocated in private_data. */
  190. /* 2.Shut down the device on last close. */
  191. /* 3.Only called once on last time. */
  192. /* Q1 : Try release multiple times. */
  193. int AD5823AF_Release(struct inode *a_pstInode, struct file *a_pstFile)
  194. {
  195. LOG_INF("Start\n");
  196. if (*g_pAF_Opened == 2) {
  197. LOG_INF("Wait\n");
  198. s4AF_WriteReg(200);
  199. msleep(20);
  200. s4AF_WriteReg(100);
  201. msleep(20);
  202. }
  203. if (*g_pAF_Opened) {
  204. LOG_INF("Free\n");
  205. spin_lock(g_pAF_SpinLock);
  206. *g_pAF_Opened = 0;
  207. spin_unlock(g_pAF_SpinLock);
  208. }
  209. LOG_INF("End\n");
  210. return 0;
  211. }
  212. void AD5823AF_SetI2Cclient(struct i2c_client *pstAF_I2Cclient, spinlock_t *pAF_SpinLock, int *pAF_Opened)
  213. {
  214. g_pstAF_I2Cclient = pstAF_I2Cclient;
  215. g_pAF_SpinLock = pAF_SpinLock;
  216. g_pAF_Opened = pAF_Opened;
  217. }