it6151_i2c.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2015. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. *
  31. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #ifndef BUILD_LK
  36. #include <linux/interrupt.h>
  37. #include <linux/i2c.h>
  38. #include <linux/slab.h>
  39. #include <linux/irq.h>
  40. #include <linux/miscdevice.h>
  41. #include <asm/uaccess.h>
  42. #include <linux/delay.h>
  43. #include <linux/input.h>
  44. #include <linux/workqueue.h>
  45. #include <linux/kobject.h>
  46. #include <linux/earlysuspend.h>
  47. #include <linux/platform_device.h>
  48. #include <asm/atomic.h>
  49. #include <cust_acc.h>
  50. #include <linux/hwmsensor.h>
  51. #include <linux/hwmsen_dev.h>
  52. #include <linux/sensors_io.h>
  53. #include <linux/hwmsen_helper.h>
  54. #include <linux/xlog.h>
  55. #include <mach/mt_typedefs.h>
  56. #include <mach/mt_gpio.h>
  57. #include <mach/mt_pm_ldo.h>
  58. /**********************************************************
  59. *
  60. * [I2C Slave Setting]
  61. *
  62. *********************************************************/
  63. #define IT6151_EDP_SLAVE_ADDR_WRITE (0x5C << 0)
  64. #define IT6151_MIPIRX_SLAVE_ADDR_WRITE (0x6C << 0)
  65. #define DEVICE_NAME "IT6151"
  66. static struct i2c_client *it6151_mipirx = NULL;
  67. static struct i2c_client *it6151_edp = NULL;
  68. static const struct i2c_device_id it6151_i2c_id[] =
  69. {
  70. {"it6151_edp", 1},
  71. {"it6151_mipirx", 1},
  72. };
  73. static int it6151_i2c_driver_probe(struct i2c_client *client, const struct i2c_device_id *id);
  74. static struct i2c_driver it6151_i2c_driver = {
  75. .driver = {
  76. .name = DEVICE_NAME,
  77. },
  78. .probe = it6151_i2c_driver_probe,
  79. .id_table = it6151_i2c_id,
  80. };
  81. /**********************************************************
  82. *
  83. * [Global Variable]
  84. *
  85. *********************************************************/
  86. static DEFINE_MUTEX(it6151_i2c_access);
  87. /**********************************************************
  88. *
  89. * [I2C Function For Read/Write fan5405]
  90. *
  91. *********************************************************/
  92. int it6151_i2c_read_byte(kal_uint8 dev_addr, kal_uint8 addr, kal_uint8 *returnData)
  93. {
  94. char cmd_buf[1]={0x00};
  95. char readData = 0;
  96. int ret=0;
  97. #ifdef IT6151_DEBUG
  98. /* dump write_data for check */
  99. printk("[KE/it6151_read_byte] dev_addr = 0x%x, read_data[0x%x] = 0x%x \n", dev_addr, addr, *returnData);
  100. #endif
  101. mutex_lock(&it6151_i2c_access);
  102. if(dev_addr == IT6151_MIPIRX_SLAVE_ADDR_WRITE)
  103. {
  104. it6151_mipirx->ext_flag=((it6151_mipirx->ext_flag) & I2C_MASK_FLAG ) | I2C_WR_FLAG | I2C_DIRECTION_FLAG;
  105. cmd_buf[0] = addr;
  106. ret = i2c_master_send(it6151_mipirx, &cmd_buf[0], (1<<8 | 1));
  107. if (ret < 0)
  108. {
  109. it6151_mipirx->ext_flag=0;
  110. mutex_unlock(&it6151_i2c_access);
  111. return 0;
  112. }
  113. readData = cmd_buf[0];
  114. *returnData = readData;
  115. it6151_mipirx->ext_flag=0;
  116. }
  117. else if(dev_addr == IT6151_EDP_SLAVE_ADDR_WRITE)
  118. {
  119. it6151_edp->ext_flag=((it6151_edp->ext_flag) & I2C_MASK_FLAG ) | I2C_WR_FLAG | I2C_DIRECTION_FLAG;
  120. cmd_buf[0] = addr;
  121. ret = i2c_master_send(it6151_edp, &cmd_buf[0], (1<<8 | 1));
  122. if (ret < 0)
  123. {
  124. it6151_edp->ext_flag=0;
  125. mutex_unlock(&it6151_i2c_access);
  126. return 0;
  127. }
  128. readData = cmd_buf[0];
  129. *returnData = readData;
  130. it6151_edp->ext_flag=0;
  131. }
  132. else
  133. {
  134. printk("[it6151_i2c_read_byte]error: no this dev_addr! \n");
  135. }
  136. mutex_unlock(&it6151_i2c_access);
  137. return 1;
  138. }
  139. int it6151_i2c_write_byte(kal_uint8 dev_addr, kal_uint8 addr, kal_uint8 writeData)
  140. {
  141. char write_data[2] = {0};
  142. int ret=0;
  143. #ifdef IT6151_DEBUG
  144. /* dump write_data for check */
  145. printk("[KE/it6151_i2c_write] dev_addr = 0x%x, write_data[0x%x] = 0x%x \n", dev_addr, addr, writeData);
  146. #endif
  147. mutex_lock(&it6151_i2c_access);
  148. write_data[0] = addr;
  149. write_data[1] = writeData;
  150. if(dev_addr == IT6151_MIPIRX_SLAVE_ADDR_WRITE)
  151. {
  152. it6151_mipirx->addr = IT6151_MIPIRX_SLAVE_ADDR_WRITE;
  153. it6151_mipirx->ext_flag=((it6151_mipirx->ext_flag) & I2C_MASK_FLAG) | I2C_DIRECTION_FLAG;
  154. ret = i2c_master_send(it6151_mipirx, write_data, 2);
  155. if(ret < 0)
  156. {
  157. it6151_mipirx->ext_flag=0;
  158. mutex_unlock(&it6151_i2c_access);
  159. return 0;
  160. }
  161. it6151_mipirx->ext_flag=0;
  162. }
  163. else if(dev_addr == IT6151_EDP_SLAVE_ADDR_WRITE)
  164. {
  165. it6151_edp->addr = IT6151_EDP_SLAVE_ADDR_WRITE;
  166. it6151_edp->ext_flag=((it6151_edp->ext_flag) & I2C_MASK_FLAG) | I2C_DIRECTION_FLAG;
  167. ret = i2c_master_send(it6151_edp, write_data, 2);
  168. if (ret < 0)
  169. {
  170. it6151_edp->ext_flag=0;
  171. mutex_unlock(&it6151_i2c_access);
  172. return 0;
  173. }
  174. it6151_edp->ext_flag=0;
  175. }
  176. else
  177. {
  178. printk("[it6151_i2c_write_byte]error: no this dev_addr! \n");
  179. }
  180. mutex_unlock(&it6151_i2c_access);
  181. return 1;
  182. }
  183. static int match_id(const struct i2c_client *client, const struct i2c_device_id *id)
  184. {
  185. if (strcmp(client->name, id->name) == 0)
  186. return true;
  187. else
  188. return false;
  189. }
  190. static int it6151_i2c_driver_probe(struct i2c_client *client, const struct i2c_device_id *id)
  191. {
  192. int err=0;
  193. printk("[it6151_i2c_driver_probe] start!\n");
  194. if(match_id(client, &it6151_i2c_id[0]))
  195. {
  196. if (!(it6151_mipirx = kmalloc(sizeof(struct i2c_client), GFP_KERNEL)))
  197. {
  198. err = -ENOMEM;
  199. goto exit;
  200. }
  201. memset(it6151_mipirx, 0, sizeof(struct i2c_client));
  202. it6151_mipirx = client;
  203. }
  204. else if(match_id(client, &it6151_i2c_id[1]))
  205. {
  206. if (!(it6151_edp = kmalloc(sizeof(struct i2c_client), GFP_KERNEL)))
  207. {
  208. err = -ENOMEM;
  209. goto exit;
  210. }
  211. memset(it6151_edp, 0, sizeof(struct i2c_client));
  212. it6151_edp = client;
  213. }
  214. else
  215. {
  216. printk("[it6151_i2c_driver_probe] error!\n");
  217. err = -EIO;
  218. goto exit;
  219. }
  220. printk("[it6151_i2c_driver_probe] %s i2c sucess!\n", client->name);
  221. return 0;
  222. exit:
  223. return err;
  224. }
  225. #define IT6151_BUSNUM 0
  226. static struct i2c_board_info __initdata it6151_I2C[] =
  227. {
  228. {I2C_BOARD_INFO("it6151_edp", IT6151_EDP_SLAVE_ADDR_WRITE)},
  229. {I2C_BOARD_INFO("it6151_mipirx", IT6151_MIPIRX_SLAVE_ADDR_WRITE)},
  230. };
  231. static int __init it6151_init(void)
  232. {
  233. printk("[it6151_init] init start\n");
  234. i2c_register_board_info(IT6151_BUSNUM, it6151_I2C, 2);
  235. if(i2c_add_driver(&it6151_i2c_driver)!=0)
  236. {
  237. printk("[it6151_init] failed to register it6151 i2c driver.\n");
  238. }
  239. else
  240. {
  241. printk("[it6151_init] Success to register it6151 i2c driver.\n");
  242. }
  243. return 0;
  244. }
  245. static void __exit it6151_exit(void)
  246. {
  247. i2c_del_driver(&it6151_i2c_driver);
  248. }
  249. module_init(it6151_init);
  250. module_exit(it6151_exit);
  251. MODULE_DESCRIPTION("I2C it6151 Driver");
  252. MODULE_AUTHOR("James Lo<james.lo@mediatek.com>");
  253. #endif