LC898212AF.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * LC898212AF 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 "LC898212AF_DRV"
  12. #define AF_I2C_SLAVE_ADDR 0xE4
  13. #define EEPROM_I2C_SLAVE_ADDR 0xA0
  14. #define AF_DEBUG
  15. #ifdef AF_DEBUG
  16. #define LOG_INF(format, args...) pr_debug(AF_DRVNAME " [%s] " format, __func__, ##args)
  17. #else
  18. #define LOG_INF(format, args...)
  19. #endif
  20. static struct i2c_client *g_pstAF_I2Cclient;
  21. static int *g_pAF_Opened;
  22. static spinlock_t *g_pAF_SpinLock;
  23. static unsigned long g_u4AF_INF;
  24. static unsigned long g_u4AF_MACRO = 1023;
  25. static unsigned long g_u4TargetPosition;
  26. static unsigned long g_u4CurrPosition;
  27. static int s4EEPROM_ReadReg(u16 addr, u16 *data)
  28. {
  29. u8 u8data = 0;
  30. u8 pu_send_cmd[2] = { (u8) (addr >> 8), (u8) (addr & 0xFF) };
  31. g_pstAF_I2Cclient->addr = (EEPROM_I2C_SLAVE_ADDR) >> 1;
  32. if (i2c_master_send(g_pstAF_I2Cclient, pu_send_cmd, 2) < 0) {
  33. LOG_INF("[s4EEPROM_ReadReg] read I2C send failed!!\n");
  34. return -1;
  35. }
  36. if (i2c_master_recv(g_pstAF_I2Cclient, &u8data, 1) < 0) {
  37. LOG_INF("EEPROM_ReadReg failed!!\n");
  38. return -1;
  39. }
  40. *data = u8data;
  41. LOG_INF("s4EEPROM_ReadReg2 0x%x, 0x%x\n", addr, *data);
  42. return 0;
  43. }
  44. static int s4AF_ReadReg(u8 length, u8 addr, u16 *data)
  45. {
  46. u8 pBuff[2];
  47. u8 u8data = 0;
  48. g_pstAF_I2Cclient->addr = (AF_I2C_SLAVE_ADDR) >> 1;
  49. if (i2c_master_send(g_pstAF_I2Cclient, &addr, 1) < 0) {
  50. LOG_INF("[CAMERA SENSOR] read I2C send failed!!\n");
  51. return -1;
  52. }
  53. if (length == 0) {
  54. if (i2c_master_recv(g_pstAF_I2Cclient, &u8data, 1) < 0) {
  55. LOG_INF("s4AF_ReadReg failed!!\n");
  56. return -1;
  57. }
  58. *data = u8data;
  59. } else if (length == 1) {
  60. if (i2c_master_recv(g_pstAF_I2Cclient, pBuff, 2) < 0) {
  61. LOG_INF("s4AF_ReadReg 2 failed!!\n");
  62. return -1;
  63. }
  64. *data = (((u16) pBuff[0]) << 8) + ((u16) pBuff[1]);
  65. }
  66. LOG_INF("s4AF_ReadReg 0x%x, 0x%x, 0x%x\n", length, addr, *data);
  67. return 0;
  68. }
  69. static int s4AF_WriteReg(u8 length, u8 addr, u16 data)
  70. {
  71. u8 puSendCmd[2] = { addr, (u8) (data & 0xFF) };
  72. u8 puSendCmd2[3] = { addr, (u8) ((data >> 8) & 0xFF), (u8) (data & 0xFF) };
  73. LOG_INF("s4AF_WriteReg 0x%x, 0x%x, 0x%x\n", length, addr, data);
  74. g_pstAF_I2Cclient->addr = (AF_I2C_SLAVE_ADDR) >> 1;
  75. if (length == 0) {
  76. if (i2c_master_send(g_pstAF_I2Cclient, puSendCmd, 2) < 0) {
  77. LOG_INF("s4AF_WriteReg failed!!\n");
  78. return -1;
  79. }
  80. } else if (length == 1) {
  81. if (i2c_master_send(g_pstAF_I2Cclient, puSendCmd2, 3) < 0) {
  82. LOG_INF("s4AF_WriteReg 2 failed!!\n");
  83. return -1;
  84. }
  85. }
  86. return 0;
  87. }
  88. static inline int getAFInfo(__user stAF_MotorInfo *pstMotorInfo)
  89. {
  90. stAF_MotorInfo stMotorInfo;
  91. stMotorInfo.u4MacroPosition = g_u4AF_MACRO;
  92. stMotorInfo.u4InfPosition = g_u4AF_INF;
  93. stMotorInfo.u4CurrentPosition = g_u4CurrPosition;
  94. stMotorInfo.bIsSupportSR = 1;
  95. stMotorInfo.bIsMotorMoving = 1;
  96. if (*g_pAF_Opened >= 1)
  97. stMotorInfo.bIsMotorOpen = 1;
  98. else
  99. stMotorInfo.bIsMotorOpen = 0;
  100. if (copy_to_user(pstMotorInfo, &stMotorInfo, sizeof(stAF_MotorInfo)))
  101. LOG_INF("copy to user failed when getting motor information\n");
  102. return 0;
  103. }
  104. static void LC898212AF_init_drv(void)
  105. {
  106. u16 Reg_0x85;
  107. u16 Reg_0x3C;
  108. u16 eepdata1 = 0, eepdata2 = 0;
  109. u16 posh = 0, posl = 0, max_pos = 0, min_pos = 0, No_eeprom;
  110. s4EEPROM_ReadReg(0x0000, &posl);
  111. s4EEPROM_ReadReg(0x0001, &posh);
  112. min_pos = (posh << 8) + posl;
  113. s4EEPROM_ReadReg(0x0002, &posl);
  114. s4EEPROM_ReadReg(0x0003, &posh);
  115. max_pos = (posh << 8) + posl;
  116. s4EEPROM_ReadReg(0x0004, &eepdata1);
  117. No_eeprom = s4EEPROM_ReadReg(0x0005, &eepdata2);
  118. LOG_INF("min %d, max %x, offset 0x%x, gain 0x%x\n", min_pos, max_pos, eepdata1, eepdata2);
  119. s4AF_WriteReg(0, 0x80, 0x34);
  120. s4AF_WriteReg(0, 0x81, 0x20);
  121. s4AF_WriteReg(0, 0x84, 0xe0);
  122. s4AF_WriteReg(0, 0x87, 0x05);
  123. s4AF_WriteReg(0, 0xA4, 0x24);
  124. s4AF_WriteReg(1, 0x3a, 0x0000);
  125. s4AF_WriteReg(1, 0x04, 0x0000);
  126. s4AF_WriteReg(1, 0x02, 0x0000);
  127. s4AF_WriteReg(1, 0x18, 0x0000);
  128. s4AF_WriteReg(0, 0x88, 0x70);
  129. if (No_eeprom == 0) {
  130. s4AF_WriteReg(0, 0x28, eepdata1);
  131. s4AF_WriteReg(0, 0x29, eepdata2);
  132. } else {
  133. s4AF_WriteReg(0, 0x28, 0x80);
  134. s4AF_WriteReg(0, 0x29, 0x80);
  135. }
  136. s4AF_WriteReg(1, 0x4c, 0x4000);
  137. s4AF_WriteReg(0, 0x83, 0x2c);
  138. s4AF_WriteReg(0, 0x85, 0xc0);
  139. msleep(20);
  140. s4AF_ReadReg(0, 0x85, &Reg_0x85);
  141. while (Reg_0x85 != 0x00) {
  142. msleep(20);
  143. s4AF_ReadReg(0, 0x85, &Reg_0x85);
  144. }
  145. s4AF_WriteReg(0, 0x84, 0xe3);
  146. s4AF_WriteReg(0, 0x97, 0x00);
  147. s4AF_WriteReg(0, 0x98, 0x42);
  148. s4AF_WriteReg(0, 0x99, 0x00);
  149. s4AF_WriteReg(0, 0x9a, 0x00);
  150. s4AF_WriteReg(0, 0x86, 0x40);
  151. s4AF_WriteReg(1, 0x40, 0x8010);
  152. s4AF_WriteReg(1, 0x42, 0x7570);
  153. s4AF_WriteReg(1, 0x44, 0x8b50);
  154. s4AF_WriteReg(1, 0x46, 0x6a10);
  155. s4AF_WriteReg(1, 0x48, 0x5a90);
  156. s4AF_WriteReg(1, 0x4a, 0x2030);
  157. s4AF_WriteReg(1, 0x4c, 0x32f0);
  158. s4AF_WriteReg(1, 0x4e, 0x7ff0);
  159. s4AF_WriteReg(1, 0x50, 0x04f0);
  160. s4AF_WriteReg(1, 0x52, 0x7610);
  161. s4AF_WriteReg(1, 0x54, 0x1450);
  162. s4AF_WriteReg(1, 0x56, 0x0000);
  163. s4AF_WriteReg(1, 0x58, 0x7ff0);
  164. s4AF_WriteReg(1, 0x5a, 0x0680);
  165. s4AF_WriteReg(1, 0x5c, 0x72f0);
  166. s4AF_WriteReg(1, 0x5e, 0x7f70);
  167. s4AF_WriteReg(1, 0x60, 0x7ed0);
  168. s4AF_WriteReg(1, 0x62, 0x7ff0);
  169. s4AF_WriteReg(1, 0x64, 0x0000);
  170. s4AF_WriteReg(1, 0x66, 0x0000);
  171. s4AF_WriteReg(1, 0x68, 0x5130);
  172. s4AF_WriteReg(1, 0x6a, 0x72f0);
  173. s4AF_WriteReg(1, 0x6c, 0x8010);
  174. s4AF_WriteReg(1, 0x6e, 0x0000);
  175. s4AF_WriteReg(1, 0x70, 0x0000);
  176. s4AF_WriteReg(1, 0x72, 0x18e0);
  177. s4AF_WriteReg(1, 0x74, 0x4e30);
  178. s4AF_WriteReg(1, 0x30, 0x0000);
  179. s4AF_WriteReg(1, 0x76, 0x0c50);
  180. s4AF_WriteReg(1, 0x78, 0x4000);
  181. s4AF_ReadReg(1, 0x3C, &Reg_0x3C);
  182. s4AF_WriteReg(1, 0x04, Reg_0x3C);
  183. s4AF_WriteReg(1, 0x18, Reg_0x3C);
  184. s4AF_WriteReg(1, 0x5A, 0x0800);
  185. s4AF_WriteReg(0, 0x83, 0xAC);
  186. s4AF_WriteReg(0, 0xA0, 0x02);
  187. s4AF_WriteReg(1, 0x7A, 0x7000);
  188. s4AF_WriteReg(1, 0x7E, 0x7E00);
  189. s4AF_WriteReg(0, 0x93, 0x40);
  190. s4AF_WriteReg(0, 0x86, 0x60);
  191. s4AF_WriteReg(0, 0x87, 0x85);
  192. msleep(30);
  193. }
  194. static int SetVCMPos(u16 _wData)
  195. {
  196. u16 TargetPos;
  197. /* u32 tmpcal=0; */
  198. u16 ExistentPos = 0;
  199. int i2cret = 0;
  200. _wData = _wData << 2;
  201. /* 0~1024 => 0x8010~0x7ff0 */
  202. if (_wData < 0x800)
  203. TargetPos = 0x800 - _wData;
  204. else
  205. TargetPos = (0x1800 - _wData) & 0xFFF;
  206. TargetPos = TargetPos << 4;
  207. s4AF_ReadReg(1, 0x3C, &ExistentPos);
  208. LOG_INF("SetVCMPos 0x%x 0x%x\n", TargetPos, ExistentPos);
  209. if (TargetPos > ExistentPos) {
  210. s4AF_WriteReg(1, 0xA1, TargetPos & 0xfff0);
  211. s4AF_WriteReg(1, 0x16, 0x0180);
  212. s4AF_WriteReg(0, 0x8F, 0x01);
  213. i2cret = s4AF_WriteReg(0, 0x8A, 0x8D);
  214. } else if (TargetPos < ExistentPos) {
  215. s4AF_WriteReg(1, 0xA1, TargetPos & 0xfff0);
  216. s4AF_WriteReg(1, 0x16, 0xFE80);
  217. s4AF_WriteReg(0, 0x8F, 0x01);
  218. i2cret = s4AF_WriteReg(0, 0x8A, 0x8D);
  219. }
  220. return i2cret;
  221. }
  222. static inline int moveAF(unsigned long a_u4Position)
  223. {
  224. int ret = 0;
  225. if ((a_u4Position > g_u4AF_MACRO) || (a_u4Position < g_u4AF_INF)) {
  226. LOG_INF("out of range\n");
  227. return -EINVAL;
  228. }
  229. if (*g_pAF_Opened == 1) {
  230. unsigned short InitPos;
  231. LC898212AF_init_drv();
  232. ret = s4AF_ReadReg(1, 0x3C, &InitPos);
  233. if (ret == 0) {
  234. LOG_INF("Init Pos %6d\n", InitPos);
  235. spin_lock(g_pAF_SpinLock);
  236. g_u4CurrPosition = (unsigned long)InitPos;
  237. spin_unlock(g_pAF_SpinLock);
  238. } else {
  239. spin_lock(g_pAF_SpinLock);
  240. g_u4CurrPosition = 0;
  241. spin_unlock(g_pAF_SpinLock);
  242. }
  243. spin_lock(g_pAF_SpinLock);
  244. *g_pAF_Opened = 2;
  245. spin_unlock(g_pAF_SpinLock);
  246. }
  247. if (g_u4CurrPosition == a_u4Position)
  248. return 0;
  249. spin_lock(g_pAF_SpinLock);
  250. g_u4TargetPosition = a_u4Position;
  251. spin_unlock(g_pAF_SpinLock);
  252. /* LOG_INF("move [curr] %d [target] %d\n", g_u4CurrPosition, g_u4TargetPosition); */
  253. if (SetVCMPos((u16) g_u4TargetPosition) == 0) {
  254. spin_lock(g_pAF_SpinLock);
  255. g_u4CurrPosition = (unsigned long)g_u4TargetPosition;
  256. spin_unlock(g_pAF_SpinLock);
  257. } else {
  258. LOG_INF("set I2C failed when moving the motor\n");
  259. }
  260. return 0;
  261. }
  262. static inline int setAFInf(unsigned long a_u4Position)
  263. {
  264. spin_lock(g_pAF_SpinLock);
  265. g_u4AF_INF = a_u4Position;
  266. spin_unlock(g_pAF_SpinLock);
  267. return 0;
  268. }
  269. static inline int setAFMacro(unsigned long a_u4Position)
  270. {
  271. spin_lock(g_pAF_SpinLock);
  272. g_u4AF_MACRO = a_u4Position;
  273. spin_unlock(g_pAF_SpinLock);
  274. return 0;
  275. }
  276. /* ////////////////////////////////////////////////////////////// */
  277. long LC898212AF_Ioctl(struct file *a_pstFile, unsigned int a_u4Command, unsigned long a_u4Param)
  278. {
  279. long i4RetValue = 0;
  280. switch (a_u4Command) {
  281. case AFIOC_G_MOTORINFO:
  282. i4RetValue = getAFInfo((__user stAF_MotorInfo *) (a_u4Param));
  283. break;
  284. case AFIOC_T_MOVETO:
  285. i4RetValue = moveAF(a_u4Param);
  286. break;
  287. case AFIOC_T_SETINFPOS:
  288. i4RetValue = setAFInf(a_u4Param);
  289. break;
  290. case AFIOC_T_SETMACROPOS:
  291. i4RetValue = setAFMacro(a_u4Param);
  292. break;
  293. default:
  294. LOG_INF("No CMD\n");
  295. i4RetValue = -EPERM;
  296. break;
  297. }
  298. return i4RetValue;
  299. }
  300. /* Main jobs: */
  301. /* 1.Deallocate anything that "open" allocated in private_data. */
  302. /* 2.Shut down the device on last close. */
  303. /* 3.Only called once on last time. */
  304. /* Q1 : Try release multiple times. */
  305. int LC898212AF_Release(struct inode *a_pstInode, struct file *a_pstFile)
  306. {
  307. LOG_INF("Start\n");
  308. if (*g_pAF_Opened == 2)
  309. LOG_INF("Wait\n");
  310. if (*g_pAF_Opened) {
  311. LOG_INF("Free\n");
  312. spin_lock(g_pAF_SpinLock);
  313. *g_pAF_Opened = 0;
  314. spin_unlock(g_pAF_SpinLock);
  315. }
  316. LOG_INF("End\n");
  317. return 0;
  318. }
  319. void LC898212AF_SetI2Cclient(struct i2c_client *pstAF_I2Cclient, spinlock_t *pAF_SpinLock, int *pAF_Opened)
  320. {
  321. g_pstAF_I2Cclient = pstAF_I2Cclient;
  322. g_pAF_SpinLock = pAF_SpinLock;
  323. g_pAF_Opened = pAF_Opened;
  324. }