main_lens.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * MAIN AF voice coil motor driver
  3. *
  4. *
  5. */
  6. #include <linux/module.h>
  7. #include <linux/i2c.h>
  8. #include <linux/delay.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/cdev.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/fs.h>
  13. #include <asm/atomic.h>
  14. #ifdef CONFIG_COMPAT
  15. #include <linux/compat.h>
  16. #endif
  17. #include "lens_info.h"
  18. #include "lens_list.h"
  19. #define AF_DRVNAME "MAINAF"
  20. #if defined(CONFIG_MTK_LEGACY)
  21. #define I2C_CONFIG_SETTING 1
  22. #elif defined(CONFIG_OF)
  23. #define I2C_CONFIG_SETTING 2 /* device tree */
  24. #else
  25. #define I2C_CONFIG_SETTING 1
  26. #endif
  27. #if I2C_CONFIG_SETTING == 1
  28. #define LENS_I2C_BUSNUM 0
  29. #define I2C_REGISTER_ID 0x28
  30. #endif
  31. #define PLATFORM_DRIVER_NAME "lens_actuator_main_af"
  32. #define AF_DRIVER_CLASS_NAME "actuatordrv_main_af"
  33. #if I2C_CONFIG_SETTING == 1
  34. static struct i2c_board_info kd_lens_dev __initdata = {
  35. I2C_BOARD_INFO(AF_DRVNAME, I2C_REGISTER_ID)
  36. };
  37. #endif
  38. #define AF_DEBUG
  39. #ifdef AF_DEBUG
  40. #define LOG_INF(format, args...) pr_debug(AF_DRVNAME " [%s] " format, __func__, ##args)
  41. #else
  42. #define LOG_INF(format, args...)
  43. #endif
  44. static stAF_DrvList g_stAF_DrvList[MAX_NUM_OF_LENS] = {
  45. #ifdef CONFIG_MTK_LENS_WV511AAF_SUPPORT
  46. {1, AFDRV_WV511AAF, WV511AAF_SetI2Cclient, WV511AAF_Ioctl, WV511AAF_Release},
  47. #endif
  48. #ifdef CONFIG_MTK_LENS_BU6424AF_SUPPORT
  49. {1, AFDRV_BU6424AF, BU6424AF_SetI2Cclient, BU6424AF_Ioctl, BU6424AF_Release},
  50. #endif
  51. #ifdef CONFIG_MTK_LENS_BU6429AF_SUPPORT
  52. {1, AFDRV_BU6429AF, BU6429AF_SetI2Cclient, BU6429AF_Ioctl, BU6429AF_Release},
  53. #endif
  54. #ifdef CONFIG_MTK_LENS_DW9714AF_SUPPORT
  55. {1, AFDRV_DW9714AF, DW9714AF_SetI2Cclient, DW9714AF_Ioctl, DW9714AF_Release},
  56. #endif
  57. #ifdef CONFIG_MTK_LENS_DW9814AF_SUPPORT
  58. {1, AFDRV_DW9814AF, DW9814AF_SetI2Cclient, DW9814AF_Ioctl, DW9814AF_Release},
  59. #endif
  60. #ifdef CONFIG_MTK_LENS_DW9718AF_SUPPORT
  61. {1, AFDRV_DW9718AF, DW9718AF_SetI2Cclient, DW9718AF_Ioctl, DW9718AF_Release},
  62. #endif
  63. #ifdef CONFIG_MTK_LENS_LC898122AF_SUPPORT
  64. {1, AFDRV_LC898122AF, LC898122AF_SetI2Cclient, LC898122AF_Ioctl, LC898122AF_Release},
  65. #endif
  66. #ifdef CONFIG_MTK_LENS_LC898212AF_SUPPORT
  67. {1, AFDRV_LC898212AF, LC898212AF_SetI2Cclient, LC898212AF_Ioctl, LC898212AF_Release},
  68. #endif
  69. #ifdef CONFIG_MTK_LENS_LC898212XDAF_SUPPORT
  70. {1, AFDRV_LC898212XDAF, LC898212XDAF_SetI2Cclient, LC898212XDAF_Ioctl, LC898212XDAF_Release},
  71. #endif
  72. #ifdef CONFIG_MTK_LENS_FM50AF_SUPPORT
  73. {1, AFDRV_FM50AF, FM50AF_SetI2Cclient, FM50AF_Ioctl, FM50AF_Release},
  74. #endif
  75. #ifdef CONFIG_MTK_LENS_AD5820AF_SUPPORT
  76. {1, AFDRV_AD5820AF, AD5820AF_SetI2Cclient, AD5820AF_Ioctl, AD5820AF_Release},
  77. #endif
  78. #ifdef CONFIG_MTK_LENS_AK7371AF_SUPPORT
  79. {1, AFDRV_AK7371AF, AK7371AF_SetI2Cclient, AK7371AF_Ioctl, AK7371AF_Release},
  80. #endif
  81. };
  82. static stAF_DrvList *g_pstAF_CurDrv;
  83. static spinlock_t g_AF_SpinLock;
  84. static int g_s4AF_Opened;
  85. static struct i2c_client *g_pstAF_I2Cclient;
  86. static dev_t g_AF_devno;
  87. static struct cdev *g_pAF_CharDrv;
  88. static struct class *actuator_class;
  89. static long AF_SetMotorName(__user stAF_MotorName * pstMotorName)
  90. {
  91. long i4RetValue = -1;
  92. int i;
  93. stAF_MotorName stMotorName;
  94. if (copy_from_user(&stMotorName , pstMotorName, sizeof(stAF_MotorName)))
  95. LOG_INF("copy to user failed when getting motor information\n");
  96. LOG_INF("Set Motor Name : %s\n", stMotorName.uMotorName);
  97. for (i = 0; i < MAX_NUM_OF_LENS; i++) {
  98. if (g_stAF_DrvList[i].uEnable != 1)
  99. break;
  100. LOG_INF("Search Motor Name : %s\n", g_stAF_DrvList[i].uDrvName);
  101. if (strcmp(stMotorName.uMotorName, g_stAF_DrvList[i].uDrvName) == 0) {
  102. g_pstAF_CurDrv = &g_stAF_DrvList[i];
  103. g_pstAF_CurDrv->pAF_SetI2Cclient(g_pstAF_I2Cclient, &g_AF_SpinLock, &g_s4AF_Opened);
  104. i4RetValue = 1;
  105. break;
  106. }
  107. }
  108. return i4RetValue;
  109. }
  110. /* ////////////////////////////////////////////////////////////// */
  111. static long AF_Ioctl(struct file *a_pstFile, unsigned int a_u4Command, unsigned long a_u4Param)
  112. {
  113. long i4RetValue = 0;
  114. switch (a_u4Command) {
  115. case AFIOC_S_SETDRVNAME:
  116. i4RetValue = AF_SetMotorName((__user stAF_MotorName *)(a_u4Param));
  117. break;
  118. default:
  119. if (g_pstAF_CurDrv)
  120. i4RetValue = g_pstAF_CurDrv->pAF_Ioctl(a_pstFile, a_u4Command, a_u4Param);
  121. break;
  122. }
  123. return i4RetValue;
  124. }
  125. /* Main jobs: */
  126. /* 1.check for device-specified errors, device not ready. */
  127. /* 2.Initialize the device if it is opened for the first time. */
  128. /* 3.Update f_op pointer. */
  129. /* 4.Fill data structures into private_data */
  130. /* CAM_RESET */
  131. static int AF_Open(struct inode *a_pstInode, struct file *a_pstFile)
  132. {
  133. LOG_INF("Start\n");
  134. if (g_s4AF_Opened) {
  135. LOG_INF("The device is opened\n");
  136. return -EBUSY;
  137. }
  138. spin_lock(&g_AF_SpinLock);
  139. g_s4AF_Opened = 1;
  140. spin_unlock(&g_AF_SpinLock);
  141. LOG_INF("End\n");
  142. return 0;
  143. }
  144. /* Main jobs: */
  145. /* 1.Deallocate anything that "open" allocated in private_data. */
  146. /* 2.Shut down the device on last close. */
  147. /* 3.Only called once on last time. */
  148. /* Q1 : Try release multiple times. */
  149. static int AF_Release(struct inode *a_pstInode, struct file *a_pstFile)
  150. {
  151. LOG_INF("Start\n");
  152. if (g_pstAF_CurDrv) {
  153. g_pstAF_CurDrv->pAF_Release(a_pstInode, a_pstFile);
  154. g_pstAF_CurDrv = NULL;
  155. } else {
  156. spin_lock(&g_AF_SpinLock);
  157. g_s4AF_Opened = 0;
  158. spin_unlock(&g_AF_SpinLock);
  159. }
  160. LOG_INF("End\n");
  161. return 0;
  162. }
  163. static const struct file_operations g_stAF_fops = {
  164. .owner = THIS_MODULE,
  165. .open = AF_Open,
  166. .release = AF_Release,
  167. .unlocked_ioctl = AF_Ioctl,
  168. #ifdef CONFIG_COMPAT
  169. .compat_ioctl = AF_Ioctl,
  170. #endif
  171. };
  172. static inline int Register_AF_CharDrv(void)
  173. {
  174. struct device *vcm_device = NULL;
  175. LOG_INF("Start\n");
  176. /* Allocate char driver no. */
  177. if (alloc_chrdev_region(&g_AF_devno, 0, 1, AF_DRVNAME)) {
  178. LOG_INF("Allocate device no failed\n");
  179. return -EAGAIN;
  180. }
  181. /* Allocate driver */
  182. g_pAF_CharDrv = cdev_alloc();
  183. if (NULL == g_pAF_CharDrv) {
  184. unregister_chrdev_region(g_AF_devno, 1);
  185. LOG_INF("Allocate mem for kobject failed\n");
  186. return -ENOMEM;
  187. }
  188. /* Attatch file operation. */
  189. cdev_init(g_pAF_CharDrv, &g_stAF_fops);
  190. g_pAF_CharDrv->owner = THIS_MODULE;
  191. /* Add to system */
  192. if (cdev_add(g_pAF_CharDrv, g_AF_devno, 1)) {
  193. LOG_INF("Attatch file operation failed\n");
  194. unregister_chrdev_region(g_AF_devno, 1);
  195. return -EAGAIN;
  196. }
  197. actuator_class = class_create(THIS_MODULE, AF_DRIVER_CLASS_NAME);
  198. if (IS_ERR(actuator_class)) {
  199. int ret = PTR_ERR(actuator_class);
  200. LOG_INF("Unable to create class, err = %d\n", ret);
  201. return ret;
  202. }
  203. vcm_device = device_create(actuator_class, NULL, g_AF_devno, NULL, AF_DRVNAME);
  204. if (NULL == vcm_device)
  205. return -EIO;
  206. LOG_INF("End\n");
  207. return 0;
  208. }
  209. static inline void Unregister_AF_CharDrv(void)
  210. {
  211. LOG_INF("Start\n");
  212. /* Release char driver */
  213. cdev_del(g_pAF_CharDrv);
  214. unregister_chrdev_region(g_AF_devno, 1);
  215. device_destroy(actuator_class, g_AF_devno);
  216. class_destroy(actuator_class);
  217. LOG_INF("End\n");
  218. }
  219. /* //////////////////////////////////////////////////////////////////// */
  220. static int AF_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
  221. static int AF_i2c_remove(struct i2c_client *client);
  222. static const struct i2c_device_id AF_i2c_id[] = { {AF_DRVNAME, 0}, {} };
  223. /* Compatible name must be the same with that defined in codegen.dws and cust_i2c.dtsi */
  224. /* TOOL : kernel-3.10\tools\dct */
  225. /* PATH : vendor\mediatek\proprietary\custom\#project#\kernel\dct\dct */
  226. #if I2C_CONFIG_SETTING == 2
  227. static const struct of_device_id MAINAF_of_match[] = {
  228. {.compatible = "mediatek,CAMERA_MAIN_AF"},
  229. {},
  230. };
  231. #endif
  232. static struct i2c_driver AF_i2c_driver = {
  233. .probe = AF_i2c_probe,
  234. .remove = AF_i2c_remove,
  235. .driver.name = AF_DRVNAME,
  236. #if I2C_CONFIG_SETTING == 2
  237. .driver.of_match_table = MAINAF_of_match,
  238. #endif
  239. .id_table = AF_i2c_id,
  240. };
  241. static int AF_i2c_remove(struct i2c_client *client)
  242. {
  243. return 0;
  244. }
  245. /* Kirby: add new-style driver {*/
  246. static int AF_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  247. {
  248. int i4RetValue = 0;
  249. LOG_INF("Start\n");
  250. /* Kirby: add new-style driver { */
  251. g_pstAF_I2Cclient = client;
  252. /* Register char driver */
  253. i4RetValue = Register_AF_CharDrv();
  254. if (i4RetValue) {
  255. LOG_INF(" register char device failed!\n");
  256. return i4RetValue;
  257. }
  258. spin_lock_init(&g_AF_SpinLock);
  259. LOG_INF("Attached!!\n");
  260. return 0;
  261. }
  262. static int AF_probe(struct platform_device *pdev)
  263. {
  264. return i2c_add_driver(&AF_i2c_driver);
  265. }
  266. static int AF_remove(struct platform_device *pdev)
  267. {
  268. i2c_del_driver(&AF_i2c_driver);
  269. return 0;
  270. }
  271. static int AF_suspend(struct platform_device *pdev, pm_message_t mesg)
  272. {
  273. return 0;
  274. }
  275. static int AF_resume(struct platform_device *pdev)
  276. {
  277. return 0;
  278. }
  279. /* platform structure */
  280. static struct platform_driver g_stAF_Driver = {
  281. .probe = AF_probe,
  282. .remove = AF_remove,
  283. .suspend = AF_suspend,
  284. .resume = AF_resume,
  285. .driver = {
  286. .name = PLATFORM_DRIVER_NAME,
  287. .owner = THIS_MODULE,
  288. }
  289. };
  290. static struct platform_device g_stAF_device = {
  291. .name = PLATFORM_DRIVER_NAME,
  292. .id = 0,
  293. .dev = {}
  294. };
  295. static int __init MAINAF_i2C_init(void)
  296. {
  297. #if I2C_CONFIG_SETTING == 1
  298. i2c_register_board_info(LENS_I2C_BUSNUM, &kd_lens_dev, 1);
  299. #endif
  300. if (platform_device_register(&g_stAF_device)) {
  301. LOG_INF("failed to register AF driver\n");
  302. return -ENODEV;
  303. }
  304. if (platform_driver_register(&g_stAF_Driver)) {
  305. LOG_INF("Failed to register AF driver\n");
  306. return -ENODEV;
  307. }
  308. return 0;
  309. }
  310. static void __exit MAINAF_i2C_exit(void)
  311. {
  312. platform_driver_unregister(&g_stAF_Driver);
  313. }
  314. module_init(MAINAF_i2C_init);
  315. module_exit(MAINAF_i2C_exit);
  316. MODULE_DESCRIPTION("MAINAF lens module driver");
  317. MODULE_AUTHOR("KY Chen <ky.chen@Mediatek.com>");
  318. MODULE_LICENSE("GPL");