LC898122AF.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * LC898122AF 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 "Ois.h"
  11. #include "OisDef.h"
  12. #include "lens_info.h"
  13. #define AF_DRVNAME "LC898122AF_DRV"
  14. #define AF_I2C_SLAVE_ADDR 0x48
  15. #define AF_DEBUG
  16. #ifdef AF_DEBUG
  17. #define LOG_INF(format, args...) pr_debug(AF_DRVNAME " [%s] " format, __func__, ##args)
  18. #else
  19. #define LOG_INF(format, args...)
  20. #endif
  21. static struct i2c_client *g_pstAF_I2Cclient;
  22. static int *g_pAF_Opened;
  23. static spinlock_t *g_pAF_SpinLock;
  24. static unsigned long g_u4AF_INF;
  25. static unsigned long g_u4AF_MACRO = 1023;
  26. static unsigned long g_u4TargetPosition;
  27. static unsigned long g_u4CurrPosition;
  28. void RegWriteA_LC898122AF(unsigned short RegAddr, unsigned char RegData)
  29. {
  30. int i4RetValue = 0;
  31. char puSendCmd[3] = { (char)((RegAddr >> 8) & 0xFF), (char)(RegAddr & 0xFF), RegData };
  32. /* LOG_INF("I2C w (%x %x)\n", RegAddr, RegData); */
  33. g_pstAF_I2Cclient->addr = (AF_I2C_SLAVE_ADDR >> 1);
  34. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, puSendCmd, 3);
  35. if (i4RetValue < 0) {
  36. LOG_INF("I2C send failed!!\n");
  37. return;
  38. }
  39. }
  40. void RegReadA_LC898122AF(unsigned short RegAddr, unsigned char *RegData)
  41. {
  42. int i4RetValue = 0;
  43. char pBuff[2] = { (char)(RegAddr >> 8), (char)(RegAddr & 0xFF) };
  44. g_pstAF_I2Cclient->addr = (AF_I2C_SLAVE_ADDR >> 1);
  45. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, pBuff, 2);
  46. if (i4RetValue < 0) {
  47. LOG_INF("[CAMERA SENSOR] read I2C send failed!!\n");
  48. return;
  49. }
  50. i4RetValue = i2c_master_recv(g_pstAF_I2Cclient, (u8 *) RegData, 1);
  51. /* LOG_INF("I2C r (%x %x)\n", RegAddr, *RegData); */
  52. if (i4RetValue != 1) {
  53. LOG_INF("[CAMERA SENSOR] I2C read failed!!\n");
  54. return;
  55. }
  56. }
  57. void RamWriteA_LC898122AF(unsigned short RamAddr, unsigned short RamData)
  58. {
  59. int i4RetValue = 0;
  60. char puSendCmd[4] = { (char)((RamAddr >> 8) & 0xFF),
  61. (char)(RamAddr & 0xFF),
  62. (char)((RamData >> 8) & 0xFF),
  63. (char)(RamData & 0xFF)
  64. };
  65. /* LOG_INF("I2C w2 (%x %x)\n", RamAddr, RamData); */
  66. g_pstAF_I2Cclient->addr = (AF_I2C_SLAVE_ADDR >> 1);
  67. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, puSendCmd, 4);
  68. if (i4RetValue < 0) {
  69. LOG_INF("I2C send failed!!\n");
  70. return;
  71. }
  72. }
  73. void RamReadA_LC898122AF(unsigned short RamAddr, void *ReadData)
  74. {
  75. int i4RetValue = 0;
  76. char pBuff[2] = { (char)(RamAddr >> 8), (char)(RamAddr & 0xFF) };
  77. unsigned short vRcvBuff = 0;
  78. unsigned long *pRcvBuff;
  79. pRcvBuff = (unsigned long *)ReadData;
  80. /* g_pstAF_I2Cclient->addr = (AF_I2C_SLAVE_ADDR >> 1); */
  81. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, pBuff, 2);
  82. if (i4RetValue < 0) {
  83. LOG_INF("[CAMERA SENSOR] read I2C send failed!!\n");
  84. return;
  85. }
  86. i4RetValue = i2c_master_recv(g_pstAF_I2Cclient, (u8 *) &vRcvBuff, 2);
  87. if (i4RetValue != 2) {
  88. LOG_INF("[CAMERA SENSOR] I2C read failed!!\n");
  89. return;
  90. }
  91. *pRcvBuff = ((vRcvBuff & 0xFF) << 8) + ((vRcvBuff >> 8) & 0xFF);
  92. /* LOG_INF("I2C r2 (%x %x)\n", RamAddr, (unsigned int)*pRcvBuff); */
  93. }
  94. void RamWrite32A_LC898122AF(unsigned short RamAddr, unsigned long RamData)
  95. {
  96. int i4RetValue = 0;
  97. char puSendCmd[6] = { (char)((RamAddr >> 8) & 0xFF),
  98. (char)(RamAddr & 0xFF),
  99. (char)((RamData >> 24) & 0xFF),
  100. (char)((RamData >> 16) & 0xFF),
  101. (char)((RamData >> 8) & 0xFF),
  102. (char)(RamData & 0xFF)
  103. };
  104. /* LOG_INF("I2C w4 (%x %x)\n", RamAddr, (unsigned int)RamData); */
  105. g_pstAF_I2Cclient->addr = (AF_I2C_SLAVE_ADDR >> 1);
  106. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, puSendCmd, 6);
  107. if (i4RetValue < 0) {
  108. LOG_INF("I2C send failed!!\n");
  109. return;
  110. }
  111. }
  112. void RamRead32A_LC898122AF(unsigned short RamAddr, void *ReadData)
  113. {
  114. int i4RetValue = 0;
  115. char pBuff[2] = { (char)(RamAddr >> 8), (char)(RamAddr & 0xFF) };
  116. unsigned long *pRcvBuff, vRcvBuff = 0;
  117. pRcvBuff = (unsigned long *)ReadData;
  118. g_pstAF_I2Cclient->addr = (AF_I2C_SLAVE_ADDR >> 1);
  119. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, pBuff, 2);
  120. if (i4RetValue < 0) {
  121. LOG_INF("[CAMERA SENSOR] read I2C send failed!!\n");
  122. return;
  123. }
  124. i4RetValue = i2c_master_recv(g_pstAF_I2Cclient, (u8 *) &vRcvBuff, 4);
  125. if (i4RetValue != 4) {
  126. LOG_INF("[CAMERA SENSOR] I2C read failed!!\n");
  127. return;
  128. }
  129. *pRcvBuff = ((vRcvBuff & 0xFF) << 24)
  130. + (((vRcvBuff >> 8) & 0xFF) << 16)
  131. + (((vRcvBuff >> 16) & 0xFF) << 8)
  132. + (((vRcvBuff >> 24) & 0xFF));
  133. /* LOG_INF("I2C r4 (%x %x)\n", RamAddr, (unsigned int)*pRcvBuff); */
  134. }
  135. void WitTim_LC898122AF(unsigned short UsWitTim)
  136. {
  137. msleep(UsWitTim);
  138. }
  139. void LC898prtvalue(unsigned short prtvalue)
  140. {
  141. LOG_INF("printvalue ======%x\n", prtvalue);
  142. }
  143. static unsigned char s4LC898OTP_ReadReg(unsigned short RegAddr)
  144. {
  145. int i4RetValue = 0;
  146. unsigned char pBuff = (unsigned char)RegAddr;
  147. unsigned char RegData = 0xFF;
  148. g_pstAF_I2Cclient->addr = (0xA0 >> 1);
  149. i4RetValue = i2c_master_send(g_pstAF_I2Cclient, &pBuff, 1);
  150. if (i4RetValue < 0) {
  151. LOG_INF("[CAMERA SENSOR] read I2C send failed!!\n");
  152. return 0xff;
  153. }
  154. i4RetValue = i2c_master_recv(g_pstAF_I2Cclient, &RegData, 1);
  155. LOG_INF("OTPI2C r (%x %x)\n", RegAddr, RegData);
  156. if (i4RetValue != 1) {
  157. LOG_INF("[CAMERA SENSOR] I2C read failed!!\n");
  158. return 0xff;
  159. }
  160. return RegData;
  161. }
  162. static inline int getAFInfo(__user stAF_MotorInfo * pstMotorInfo)
  163. {
  164. stAF_MotorInfo stMotorInfo;
  165. stMotorInfo.u4MacroPosition = g_u4AF_MACRO;
  166. stMotorInfo.u4InfPosition = g_u4AF_INF;
  167. stMotorInfo.u4CurrentPosition = g_u4CurrPosition;
  168. stMotorInfo.bIsSupportSR = 1;
  169. stMotorInfo.bIsMotorMoving = 1;
  170. if (*g_pAF_Opened >= 1)
  171. stMotorInfo.bIsMotorOpen = 1;
  172. else
  173. stMotorInfo.bIsMotorOpen = 0;
  174. if (copy_to_user(pstMotorInfo, &stMotorInfo, sizeof(stAF_MotorInfo)))
  175. LOG_INF("copy to user failed when getting motor information\n");
  176. return 0;
  177. }
  178. static void LC898122AF_init_drv(void)
  179. {
  180. unsigned short addrotp;
  181. unsigned long dataotp = 0;
  182. IniSetAf();
  183. IniSet();
  184. RamAccFixMod(ON); /* 16bit Fix mode */
  185. addrotp = 0x30;
  186. dataotp = (s4LC898OTP_ReadReg(addrotp) << 8) + s4LC898OTP_ReadReg(addrotp + 1);
  187. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  188. /* RamWriteA_LC898122AF(0x1479,dataotp); //Hall offset X */
  189. addrotp = 0x32;
  190. dataotp = (s4LC898OTP_ReadReg(addrotp) << 8) + s4LC898OTP_ReadReg(addrotp + 1);
  191. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  192. /* RamWriteA_LC898122AF(0x14F9,dataotp); //Hall offset Y */
  193. addrotp = 0x34;
  194. dataotp = (s4LC898OTP_ReadReg(addrotp) << 8) + s4LC898OTP_ReadReg(addrotp + 1);
  195. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  196. /* RamWriteA_LC898122AF(0x147A,dataotp); //Hall bias X */
  197. addrotp = 0x36;
  198. dataotp = (s4LC898OTP_ReadReg(addrotp) << 8) + s4LC898OTP_ReadReg(addrotp + 1);
  199. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  200. /* RamWriteA_LC898122AF(0x14FA,dataotp); //Hall bias Y */
  201. addrotp = 0x38;
  202. dataotp = (s4LC898OTP_ReadReg(addrotp) << 8) + s4LC898OTP_ReadReg(addrotp + 1);
  203. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  204. /* RamWriteA_LC898122AF(0x1450,dataotp); //Hall AD offset X */
  205. addrotp = 0x3A;
  206. dataotp = (s4LC898OTP_ReadReg(addrotp) << 8) + s4LC898OTP_ReadReg(addrotp + 1);
  207. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  208. /* RamWriteA_LC898122AF(0x14D0,dataotp); //Hall AD offset Y */
  209. addrotp = 0x3C;
  210. dataotp = (s4LC898OTP_ReadReg(addrotp) << 8) + s4LC898OTP_ReadReg(addrotp + 1);
  211. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  212. /* RamWriteA_LC898122AF(0x10D3,dataotp); //Loop gain X */
  213. addrotp = 0x3E;
  214. dataotp = (s4LC898OTP_ReadReg(addrotp) << 8) + s4LC898OTP_ReadReg(addrotp + 1);
  215. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  216. /* RamWriteA_LC898122AF(0x11D3,dataotp); //Loop gain Y */
  217. RamAccFixMod(OFF); /* 32bit Float mode */
  218. addrotp = 0x44;
  219. dataotp = s4LC898OTP_ReadReg(addrotp);
  220. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  221. /* RegWriteA_LC898122AF(0x02a0,dataotp); //Gyro offset X M */
  222. addrotp = 0x45;
  223. dataotp = s4LC898OTP_ReadReg(addrotp);
  224. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  225. /* RegWriteA_LC898122AF(0x02a1,dataotp); //Gyro offset X L */
  226. addrotp = 0x46;
  227. dataotp = s4LC898OTP_ReadReg(addrotp);
  228. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  229. /* RegWriteA_LC898122AF(0x02a2,dataotp); //Gyro offset Y M */
  230. addrotp = 0x47;
  231. dataotp = s4LC898OTP_ReadReg(addrotp);
  232. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  233. /* RegWriteA_LC898122AF(0x02a3,dataotp); //Gyro offset Y L */
  234. addrotp = 0x48;
  235. dataotp = s4LC898OTP_ReadReg(addrotp);
  236. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  237. /* RegWriteA_LC898122AF(0x0257,dataotp);//OSC */
  238. addrotp = 0x49;
  239. dataotp = (s4LC898OTP_ReadReg(addrotp) << 24)
  240. + (s4LC898OTP_ReadReg(addrotp + 1) << 16)
  241. + (s4LC898OTP_ReadReg(addrotp + 2) << 8)
  242. + s4LC898OTP_ReadReg(addrotp + 3);
  243. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  244. /* RamWrite32A_LC898122AF(0x1020,dataotp); //Gyro gain X */
  245. addrotp = 0x4D;
  246. dataotp = (s4LC898OTP_ReadReg(addrotp) << 24)
  247. + (s4LC898OTP_ReadReg(addrotp + 1) << 16)
  248. + (s4LC898OTP_ReadReg(addrotp + 2) << 8)
  249. + s4LC898OTP_ReadReg(addrotp + 3);
  250. LOG_INF("[OTP]0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  251. /* RamWrite32A_LC898122AF(0x1120,dataotp); //Gyro gain Y */
  252. RamWriteA_LC898122AF(TCODEH, 100); /* focus position */
  253. RtnCen(0);
  254. msleep(100);
  255. SetPanTiltMode(ON);
  256. msleep(20);
  257. OisEna();
  258. SetH1cMod(MOVMODE); /* movie mode */
  259. /* SetH1cMod(0); //still mode */
  260. addrotp = 0x20;
  261. dataotp = (s4LC898OTP_ReadReg(addrotp) << 8) + s4LC898OTP_ReadReg(addrotp + 1);
  262. LOG_INF("[OTP]AF start current 0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  263. LOG_INF("[OTP]AF start current 0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  264. LOG_INF("[OTP]AF start current 0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  265. addrotp = 0x22;
  266. dataotp = (s4LC898OTP_ReadReg(addrotp) << 8) + s4LC898OTP_ReadReg(addrotp + 1);
  267. LOG_INF("[OTP]AF Infinit 0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  268. LOG_INF("[OTP]AF Infinit 0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  269. LOG_INF("[OTP]AF Infinit 0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  270. addrotp = 0x24;
  271. dataotp = (s4LC898OTP_ReadReg(addrotp) << 8) + s4LC898OTP_ReadReg(addrotp + 1);
  272. LOG_INF("[OTP]AF Macro 0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  273. LOG_INF("[OTP]AF Macro 0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  274. LOG_INF("[OTP]AF Macro 0x%x 0x%x\n", addrotp, (unsigned int)dataotp);
  275. LOG_INF("LC898122AF_Open - End\n");
  276. }
  277. static inline int moveAF(unsigned long a_u4Position)
  278. {
  279. int ret = 0;
  280. if ((a_u4Position > g_u4AF_MACRO) || (a_u4Position < g_u4AF_INF)) {
  281. LOG_INF("out of range\n");
  282. return -EINVAL;
  283. }
  284. if (*g_pAF_Opened == 1) {
  285. LC898122AF_init_drv();
  286. spin_lock(g_pAF_SpinLock);
  287. g_u4CurrPosition = 0;
  288. *g_pAF_Opened = 2;
  289. spin_unlock(g_pAF_SpinLock);
  290. }
  291. if (g_u4CurrPosition == a_u4Position)
  292. return 0;
  293. spin_lock(g_pAF_SpinLock);
  294. g_u4TargetPosition = a_u4Position;
  295. spin_unlock(g_pAF_SpinLock);
  296. /* LOG_INF("move [curr] %d [target] %d\n", g_u4CurrPosition, g_u4TargetPosition); */
  297. RamWriteA_LC898122AF(TCODEH, g_u4TargetPosition);
  298. spin_lock(g_pAF_SpinLock);
  299. g_u4CurrPosition = (unsigned long)g_u4TargetPosition;
  300. spin_unlock(g_pAF_SpinLock);
  301. return 0;
  302. }
  303. static inline int setAFInf(unsigned long a_u4Position)
  304. {
  305. spin_lock(g_pAF_SpinLock);
  306. g_u4AF_INF = a_u4Position;
  307. spin_unlock(g_pAF_SpinLock);
  308. return 0;
  309. }
  310. static inline int setAFMacro(unsigned long a_u4Position)
  311. {
  312. spin_lock(g_pAF_SpinLock);
  313. g_u4AF_MACRO = a_u4Position;
  314. spin_unlock(g_pAF_SpinLock);
  315. return 0;
  316. }
  317. /* ////////////////////////////////////////////////////////////// */
  318. long LC898122AF_Ioctl(struct file *a_pstFile, unsigned int a_u4Command, unsigned long a_u4Param)
  319. {
  320. long i4RetValue = 0;
  321. switch (a_u4Command) {
  322. case AFIOC_G_MOTORINFO:
  323. i4RetValue = getAFInfo((__user stAF_MotorInfo *) (a_u4Param));
  324. break;
  325. case AFIOC_T_MOVETO:
  326. i4RetValue = moveAF(a_u4Param);
  327. break;
  328. case AFIOC_T_SETINFPOS:
  329. i4RetValue = setAFInf(a_u4Param);
  330. break;
  331. case AFIOC_T_SETMACROPOS:
  332. i4RetValue = setAFMacro(a_u4Param);
  333. break;
  334. default:
  335. LOG_INF("No CMD\n");
  336. i4RetValue = -EPERM;
  337. break;
  338. }
  339. return i4RetValue;
  340. }
  341. /* Main jobs: */
  342. /* 1.Deallocate anything that "open" allocated in private_data. */
  343. /* 2.Shut down the device on last close. */
  344. /* 3.Only called once on last time. */
  345. /* Q1 : Try release multiple times. */
  346. int LC898122AF_Release(struct inode *a_pstInode, struct file *a_pstFile)
  347. {
  348. LOG_INF("Start\n");
  349. if (*g_pAF_Opened == 2) {
  350. LOG_INF("Wait\n");
  351. RamWriteA_LC898122AF(TCODEH, 100); /* focus position */
  352. msleep(20);
  353. RamWriteA_LC898122AF(TCODEH, 50); /* focus position */
  354. msleep(20);
  355. RtnCen(0);
  356. SrvCon(X_DIR, OFF);
  357. SrvCon(Y_DIR, OFF);
  358. }
  359. if (*g_pAF_Opened) {
  360. LOG_INF("Free\n");
  361. spin_lock(g_pAF_SpinLock);
  362. *g_pAF_Opened = 0;
  363. spin_unlock(g_pAF_SpinLock);
  364. }
  365. LOG_INF("End\n");
  366. return 0;
  367. }
  368. void LC898122AF_SetI2Cclient(struct i2c_client *pstAF_I2Cclient, spinlock_t *pAF_SpinLock, int *pAF_Opened)
  369. {
  370. g_pstAF_I2Cclient = pstAF_I2Cclient;
  371. g_pAF_SpinLock = pAF_SpinLock;
  372. g_pAF_Opened = pAF_Opened;
  373. }