ccci_pmic.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*****************************************************************************
  2. *
  3. * Filename:
  4. * ---------
  5. * ccci_pmic.c
  6. *
  7. * Project:
  8. * --------
  9. * ALPS
  10. *
  11. * Description:
  12. * ------------
  13. * MT65XX CCCI PMIC Driver
  14. *
  15. ****************************************************************************/
  16. #include <linux/kernel.h>
  17. #include <linux/device.h>
  18. #include <linux/cdev.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/wakelock.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/delay.h>
  24. #include <linux/wait.h>
  25. #include <linux/fs.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/dma-mapping.h>
  29. #include <ccci.h>
  30. #include <ccci_pmic.h>
  31. enum vsim_sel_enum {
  32. VSIM_1_3V = 0,
  33. VSIM_1_5V,
  34. VSIM_1_8V,
  35. VSIM_2_5V,
  36. VSIM_2_8V,
  37. VSIM_3_0V,
  38. VSIM_3_3V,
  39. VSIM_1_2V
  40. };
  41. #define CONFIG_CCCI_PMIC_DEBUG_MSG
  42. #define CCCI_PMIC_DEVNAME "ccci_pmic"
  43. #define CCCI_PMIC_QSIZE (10)
  44. static DEFINE_SPINLOCK(pmic_spinlock);
  45. static struct shared_mem_pmic_t *ccci_pmic_shared_mem;
  46. static int ccci_pmic_shared_mem_phys_addr;
  47. static dev_t pmic_dev_num;
  48. static struct cdev pmic_cdev;
  49. static struct kfifo pmic_fifo;
  50. static void ccci_pmic_read(struct work_struct *ws);
  51. static DECLARE_WORK(ccci_pmic_read_work, ccci_pmic_read);
  52. int g_VSIM_1 = 2;
  53. int g_VSIM_2 = 2;
  54. static void ccci_pmic_read(struct work_struct *ws)
  55. {
  56. unsigned long exec_time_1 = 0, exec_time_2 = 0;
  57. unsigned long arg;
  58. unsigned long flag;
  59. /* mt6326_check_power(); */
  60. /*
  61. * 0. Start Timer
  62. */
  63. /* exec_time_1 = sampletrigger(0, 0, 0); */
  64. exec_time_1 = 0;
  65. /* #ifdef CONFIG_CCCI_PMIC_DEBUG_MSG */
  66. CCCI_PMIC_MSG("ccci_pmic_callback : exec_time_1 = %ld ms\r\n",
  67. exec_time_1);
  68. /* #endif */
  69. /*
  70. * 1. Parsing CCCI Message Format which received from MD site
  71. */
  72. spin_lock_irqsave(&pmic_spinlock, flag);
  73. if (kfifo_out(&pmic_fifo, (unsigned char *)&arg, sizeof(unsigned long))
  74. != sizeof(unsigned long)) {
  75. spin_unlock_irqrestore(&pmic_spinlock, flag);
  76. CCCI_MSG("<pmic>Unable to get new request from fifo\n");
  77. return;
  78. }
  79. spin_unlock_irqrestore(&pmic_spinlock, flag);
  80. ccci_pmic_shared_mem->ccci_msg.pmic6326_op = arg & 0x000000FF;
  81. ccci_pmic_shared_mem->ccci_msg.pmic6326_type = (arg & 0x0000FF00) >> 8;
  82. ccci_pmic_shared_mem->ccci_msg.pmic6326_param1 =
  83. (arg & 0x00FF0000) >> 16;
  84. ccci_pmic_shared_mem->ccci_msg.pmic6326_param2 =
  85. (arg & 0xFF000000) >> 24;
  86. /*
  87. * 2. Execute the operation (API) that MD site called
  88. */
  89. switch (ccci_pmic_shared_mem->ccci_msg.pmic6326_op) {
  90. case PMIC6326_VSIM_ENABLE:
  91. /* #ifdef CONFIG_CCCI_PMIC_DEBUG_MSG */
  92. CCCI_PMIC_MSG("PMIC6326_VSIM_ENABLE\n");
  93. /* #endif */
  94. /* pmic_vsim_enable(ccci_pmic_shared_mem->ccci_msg.pmic6326_param1); */
  95. break;
  96. case PMIC6326_VSIM_SET_AND_ENABLE:
  97. /* #ifdef CONFIG_CCCI_PMIC_DEBUG_MSG */
  98. CCCI_PMIC_MSG("PMIC6326_VSIM_SET_AND_ENABLE\n");
  99. /* #endif */
  100. /* pmic_vsim_cal(0); */
  101. /* pmic_vsim_sel(ccci_pmic_shared_mem->ccci_msg.pmic6326_param1); */
  102. /* pmic_vsim_enable(KAL_TRUE); */
  103. break;
  104. case PMIC6236_LOCK:
  105. /* #ifdef CONFIG_CCCI_PMIC_DEBUG_MSG */
  106. CCCI_PMIC_MSG("PMIC6236_LOCK\n");
  107. /* #endif */
  108. break;
  109. case PMIC6326_UNLOCK:
  110. /* #ifdef CONFIG_CCCI_PMIC_DEBUG_MSG */
  111. CCCI_PMIC_MSG("CONFIG_CCCI_PMIC_DEBUG_MSG\n");
  112. /* #endif */
  113. break;
  114. case PMIC6326_VSIM2_ENABLE:
  115. /* #ifdef CONFIG_CCCI_PMIC_DEBUG_MSG */
  116. CCCI_PMIC_MSG("PMIC6326_VSIM2_ENABLE\n");
  117. /* #endif */
  118. /* pmic_vsim2_enable(ccci_pmic_shared_mem->ccci_msg.pmic6326_param1); */
  119. break;
  120. case PMIC6326_VSIM2_SET_AND_ENABLE:
  121. /* #ifdef CONFIG_CCCI_PMIC_DEBUG_MSG */
  122. CCCI_PMIC_MSG("PMIC6326_VSIM2_SET_AND_ENABLE\n");
  123. /* #endif */
  124. /* pmic_vsim2_sel(ccci_pmic_shared_mem->ccci_msg.pmic6326_param1); */
  125. /* pmic_vsim2_enable(KAL_TRUE); */
  126. break;
  127. default:
  128. /* #ifdef CONFIG_CCCI_PMIC_DEBUG_MSG */
  129. CCCI_PMIC_MSG("Error op\n");
  130. /* #endif */
  131. }
  132. /* exec_time_2 = sampletrigger(0, 0, 1); */
  133. exec_time_2 = 10000;
  134. /* #ifdef CONFIG_CCCI_PMIC_DEBUG_MSG */
  135. CCCI_PMIC_MSG("exec_time_2 = %ld ms\r\n", exec_time_2);
  136. /* #endif */
  137. exec_time_2 = (exec_time_2 - exec_time_1) / 1000;
  138. /* #ifdef CONFIG_CCCI_PMIC_DEBUG_MSG */
  139. CCCI_PMIC_MSG("exec_time = %ld ms\r\n", exec_time_2);
  140. /* #endif */
  141. /*
  142. * 3. AP site write Message to share memory "ccci_pmic_shared_mem"
  143. */
  144. ccci_pmic_shared_mem->ccci_msg.pmic6326_type = (kal_uint8) PMIC6326_RES;
  145. ccci_pmic_shared_mem->ccci_msg_info.pmic6326_exec_time = exec_time_2;
  146. ccci_pmic_shared_mem->ccci_msg = ccci_pmic_shared_mem->ccci_msg;
  147. ccci_pmic_shared_mem->ccci_msg_info =
  148. ccci_pmic_shared_mem->ccci_msg_info;
  149. }
  150. static void ccci_pmic_callback(struct CCCI_BUFF_T *buff, void *private_data)
  151. {
  152. unsigned long flag;
  153. /* #ifdef CONFIG_CCCI_PMIC_DEBUG_MSG */
  154. CCCI_PMIC_MSG("callback++\n");
  155. /* #endif */
  156. spin_lock_irqsave(&pmic_spinlock, flag);
  157. if (buff->channel == CCCI_PMIC_RX) {
  158. if (kfifo_in
  159. (&pmic_fifo, (unsigned char *)&buff->data[1],
  160. sizeof(unsigned long)) != sizeof(unsigned long)) {
  161. spin_unlock_irqrestore(&pmic_spinlock, flag);
  162. /* CCCI_LOGE("CCCI_PMIC: Unable to put new request into fifo\n"); */
  163. CCCI_PMIC_MSG("callback--0\n");
  164. return;
  165. }
  166. spin_unlock_irqrestore(&pmic_spinlock, flag);
  167. schedule_work(&ccci_pmic_read_work);
  168. }
  169. CCCI_PMIC_MSG("callback--1\n");
  170. }
  171. static int ccci_pmic_start(void)
  172. {
  173. int size;
  174. /* #ifdef CONFIG_CCCI_PMIC_DEBUG_MSG */
  175. CCCI_PMIC_MSG("ccci_pmic_start\n");
  176. /* #endif */
  177. if (0 !=
  178. kfifo_alloc(&pmic_fifo, sizeof(unsigned long) * CCCI_PMIC_QSIZE,
  179. GFP_KERNEL)) {
  180. /* CCCI_LOGE("CCCI_PMIC: Unable to create fifo\n"); */
  181. CCCI_MSG("<pmic> Unable to create fifo\n");
  182. return -EFAULT;
  183. }
  184. CCCI_PMIC_MSG("%x:%x:%d\n", (unsigned int)ccci_pmic_shared_mem,
  185. (unsigned int)ccci_pmic_shared_mem_phys_addr, size);
  186. return 0;
  187. }
  188. static void ccci_pmic_stop(void)
  189. {
  190. /* #ifdef CONFIG_CCCI_PMIC_DEBUG_MSG */
  191. CCCI_PMIC_MSG("pmic_stop\n");
  192. /* #endif */
  193. ccci_pmic_shared_mem = NULL;
  194. ccci_pmic_shared_mem_phys_addr = 0;
  195. kfifo_free(&pmic_fifo);
  196. }
  197. static const struct file_operations pmic_fops = {
  198. .owner = THIS_MODULE,
  199. };
  200. int __init ccci_pmic_init(void)
  201. {
  202. int ret;
  203. if (alloc_chrdev_region(&pmic_dev_num, 0, 1, CCCI_PMIC_DEVNAME)) {
  204. CCCI_MSG("<pmic>Device major number allocation failed\n");
  205. return -EAGAIN;
  206. }
  207. cdev_init(&pmic_cdev, &pmic_fops);
  208. pmic_cdev.owner = THIS_MODULE;
  209. ret = cdev_add(&pmic_cdev, pmic_dev_num, 1);
  210. if (ret) {
  211. CCCI_MSG("<pmic>Char device add failed\n");
  212. return ret;
  213. }
  214. ret = ccci_pmic_start();
  215. if (ret) {
  216. CCCI_MSG("<pmic>Unable to initialize environment\n");
  217. return ret;
  218. }
  219. CCCI_PMIC_MSG("Init complete, device major number = %d\n",
  220. MAJOR(pmic_dev_num));
  221. return 0;
  222. }
  223. void __exit ccci_pmic_exit(void)
  224. {
  225. ccci_pmic_stop();
  226. cdev_del(&pmic_cdev);
  227. unregister_chrdev_region(pmic_dev_num, 1);
  228. }