lcm_i2c.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #if defined(MTK_LCM_DEVICE_TREE_SUPPORT)
  2. #ifndef BUILD_LK
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/fs.h>
  6. #include <linux/slab.h>
  7. #include <linux/init.h>
  8. #include <linux/list.h>
  9. #include <linux/i2c.h>
  10. #include <linux/i2c-dev.h>
  11. #include <linux/irq.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/string.h>
  17. #include <linux/wait.h>
  18. #endif
  19. #ifdef BUILD_LK
  20. #include <platform/upmu_common.h>
  21. #include <platform/upmu_hw.h>
  22. #include <platform/mt_gpio.h>
  23. #include <platform/mt_i2c.h>
  24. #include <platform/mt_pmic.h>
  25. #include <string.h>
  26. #else
  27. #ifdef CONFIG_MTK_LEGACY
  28. #include <mach/mt_pm_ldo.h> /* hwPowerOn */
  29. #include <mt-plat/upmu_common.h>
  30. #include <mach/upmu_sw.h>
  31. #include <mach/upmu_hw.h>
  32. #endif
  33. #endif
  34. #ifdef CONFIG_MTK_LEGACY
  35. #include <mach/mt_gpio.h>
  36. #include <cust_gpio_usage.h>
  37. #include <cust_i2c.h>
  38. #endif
  39. #include "lcm_define.h"
  40. #include "lcm_drv.h"
  41. #include "lcm_i2c.h"
  42. /*****************************************************************************
  43. * Define
  44. *****************************************************************************/
  45. #ifndef CONFIG_FPGA_EARLY_PORTING
  46. #ifdef CONFIG_MTK_LEGACY
  47. #define LCM_I2C_ADDR 0x3E
  48. #define LCM_I2C_BUSNUM I2C_I2C_LCD_BIAS_CHANNEL /* for I2C channel 0 */
  49. #define LCM_I2C_ID_NAME "tps65132"
  50. #else
  51. #define LCM_I2C_ADDR 0x3E
  52. #define LCM_I2C_BUSNUM 1 /* for I2C channel 0 */
  53. #define LCM_I2C_ID_NAME "I2C_LCD_BIAS"
  54. #endif
  55. /*****************************************************************************
  56. * GLobal Variable
  57. *****************************************************************************/
  58. #ifdef CONFIG_MTK_LEGACY
  59. static struct i2c_board_info _lcm_i2c_board_info __initdata = {
  60. I2C_BOARD_INFO(LCM_I2C_ID_NAME, LCM_I2C_ADDR)
  61. };
  62. #else
  63. static const struct of_device_id _lcm_i2c_of_match[] = {
  64. {
  65. .compatible = "mediatek,I2C_LCD_BIAS",
  66. },
  67. };
  68. #endif
  69. static struct i2c_client *_lcm_i2c_client;
  70. /*****************************************************************************
  71. * Function Prototype
  72. *****************************************************************************/
  73. static int _lcm_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
  74. static int _lcm_i2c_remove(struct i2c_client *client);
  75. /*****************************************************************************
  76. * Data Structure
  77. *****************************************************************************/
  78. struct _lcm_i2c_dev {
  79. struct i2c_client *client;
  80. };
  81. static const struct i2c_device_id _lcm_i2c_id[] = {
  82. {LCM_I2C_ID_NAME, 0},
  83. {}
  84. };
  85. /* #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)) */
  86. /* static struct i2c_client_address_data addr_data = { .forces = forces,}; */
  87. /* #endif */
  88. static struct i2c_driver _lcm_i2c_driver = {
  89. .id_table = _lcm_i2c_id,
  90. .probe = _lcm_i2c_probe,
  91. .remove = _lcm_i2c_remove,
  92. /* .detect = _lcm_i2c_detect, */
  93. .driver = {
  94. .owner = THIS_MODULE,
  95. .name = LCM_I2C_ID_NAME,
  96. #ifdef CONFIG_MTK_LEGACY
  97. #else
  98. .of_match_table = _lcm_i2c_of_match,
  99. #endif
  100. },
  101. };
  102. /*****************************************************************************
  103. * Extern Area
  104. *****************************************************************************/
  105. /*****************************************************************************
  106. * Function
  107. *****************************************************************************/
  108. static int _lcm_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  109. {
  110. pr_debug("[LCM][I2C] _lcm_i2c_probe\n");
  111. pr_debug("[LCM][I2C] NT: info==>name=%s addr=0x%x\n", client->name, client->addr);
  112. _lcm_i2c_client = client;
  113. return 0;
  114. }
  115. static int _lcm_i2c_remove(struct i2c_client *client)
  116. {
  117. pr_debug("[LCM][I2C] _lcm_i2c_remove\n");
  118. _lcm_i2c_client = NULL;
  119. i2c_unregister_device(client);
  120. return 0;
  121. }
  122. static int _lcm_i2c_write_bytes(unsigned char addr, unsigned char value)
  123. {
  124. int ret = 0;
  125. struct i2c_client *client = _lcm_i2c_client;
  126. char write_data[2] = { 0 };
  127. write_data[0] = addr;
  128. write_data[1] = value;
  129. ret = i2c_master_send(client, write_data, 2);
  130. if (ret < 0)
  131. pr_err("[LCM][ERROR] _lcm_i2c write data fail !!\n");
  132. return ret;
  133. }
  134. /*
  135. * module load/unload record keeping
  136. */
  137. static int __init _lcm_i2c_init(void)
  138. {
  139. pr_debug("[LCM][I2C] _lcm_i2c_init\n");
  140. #ifdef CONFIG_MTK_LEGACY
  141. i2c_register_board_info(LCM_I2C_BUSNUM, &_lcm_i2c_board_info, 1);
  142. pr_debug("[LCM][I2C] _lcm_i2c_init2\n");
  143. #endif
  144. i2c_add_driver(&_lcm_i2c_driver);
  145. pr_debug("[LCM][I2C] _lcm_i2c_init success\n");
  146. return 0;
  147. }
  148. static void __exit _lcm_i2c_exit(void)
  149. {
  150. pr_debug("[LCM][I2C] _lcm_i2c_exit\n");
  151. i2c_del_driver(&_lcm_i2c_driver);
  152. }
  153. #endif
  154. static LCM_STATUS _lcm_i2c_check_data(char type, const LCM_DATA_T2 *t2)
  155. {
  156. switch (type) {
  157. case LCM_I2C_WRITE:
  158. if (t2->cmd > 0xFF) {
  159. pr_err("[LCM][ERROR] %s/%d: %d\n", __func__, __LINE__, t2->cmd);
  160. return LCM_STATUS_ERROR;
  161. }
  162. if (t2->data > 0xFF) {
  163. pr_err("[LCM][ERROR] %s/%d: %d\n", __func__, __LINE__, t2->data);
  164. return LCM_STATUS_ERROR;
  165. }
  166. break;
  167. default:
  168. pr_err("[LCM][ERROR] %s/%d: %d\n", __func__, __LINE__, type);
  169. return LCM_STATUS_ERROR;
  170. }
  171. return LCM_STATUS_OK;
  172. }
  173. LCM_STATUS lcm_i2c_set_data(char type, const LCM_DATA_T2 *t2)
  174. {
  175. unsigned int ret_code = 0;
  176. /* check parameter is valid */
  177. if (LCM_STATUS_OK == _lcm_i2c_check_data(type, t2)) {
  178. switch (type) {
  179. case LCM_I2C_WRITE:
  180. pr_debug("[LCM][I2C] %s/%d: %d, 0x%x, 0x%x\n", __func__, __LINE__, type,
  181. t2->cmd, t2->data);
  182. ret_code =
  183. _lcm_i2c_write_bytes((unsigned char)t2->cmd, (unsigned char)t2->data);
  184. break;
  185. default:
  186. pr_err("[LCM][ERROR] %s/%d: %d\n", __func__, __LINE__, type);
  187. return LCM_STATUS_ERROR;
  188. }
  189. } else {
  190. pr_err("[LCM][ERROR] %s/%d: %d, 0x%x, 0x%x\n", __func__, __LINE__, type, t2->cmd,
  191. t2->data);
  192. return LCM_STATUS_ERROR;
  193. }
  194. if (ret_code < 0) {
  195. pr_err("[LCM][ERROR] %s/%d: 0x%x, 0x%x, %d\n", __func__, __LINE__,
  196. (unsigned int)t2->cmd, (unsigned int)t2->data, ret_code);
  197. return LCM_STATUS_ERROR;
  198. }
  199. return LCM_STATUS_OK;
  200. }
  201. #ifndef CONFIG_FPGA_EARLY_PORTING
  202. module_init(_lcm_i2c_init);
  203. module_exit(_lcm_i2c_exit);
  204. MODULE_AUTHOR("Joey Pan");
  205. MODULE_DESCRIPTION("MTK LCM I2C Driver");
  206. MODULE_LICENSE("GPL");
  207. #endif
  208. #endif