it6151_i2c.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. //#include "it6151_i2c.h"
  59. //#include "cust_charging.h"
  60. //#include <mach/charging.h>
  61. /**********************************************************
  62. *
  63. * [I2C Slave Setting]
  64. *
  65. *********************************************************/
  66. static DEFINE_MUTEX(it6151_i2c_access);
  67. #define it6151_BUSNUM 2
  68. #define DEVICE_NAME "it6151"
  69. #define it6151_DEVICE_ID 0x5c
  70. struct i2c_client *it6151_0;
  71. struct i2c_client *it6151_1;
  72. static const struct i2c_device_id it6151_id[] = {
  73. { "it6151_0", 0 },
  74. { "it6151_1", 0 }
  75. };
  76. static struct i2c_board_info it6151_i2c[] = {
  77. {
  78. .type = "it6151_0",
  79. .addr = 0x5C,
  80. },
  81. {
  82. .type = "it6151_1",
  83. .addr = 0x6C,
  84. }
  85. };
  86. // i2c_smbus_read_byte_data(sii902xA,RegOffset);
  87. UINT8 it6151_reg_i2c_read (struct i2c_client *client,UINT8 RegOffset)
  88. {
  89. UINT8 Readnum;
  90. Readnum = i2c_smbus_read_byte_data(client,RegOffset);
  91. printk("[6151]client:%s , read RegOffset=0x%x,Readnum=0x%x \n", client->name, RegOffset, Readnum);
  92. return Readnum;
  93. }
  94. UINT8 it6151_reg_i2c_read_byte(U8 dev_addr,U8 *cmdBuffer, U8 *dataBuffer)
  95. {
  96. UINT8 RetVal = 0xFF;
  97. if(dev_addr == it6151_0->addr)
  98. RetVal = it6151_reg_i2c_read(it6151_0, *cmdBuffer);
  99. else if(dev_addr == it6151_1->addr)
  100. RetVal = it6151_reg_i2c_read(it6151_1, *cmdBuffer);
  101. else
  102. printk("[it6151_reg_i2c_read_byte]error: no this dev_addr \n");
  103. return RetVal;
  104. }
  105. void it6151_reg_i2c_write (struct i2c_client *client,UINT8 RegOffset, UINT8 Data)
  106. {
  107. i2c_smbus_write_byte_data(client, RegOffset, Data);
  108. printk("[6151]client:%s , write RegOffset=0x%x,Data=0x%x \n", client->name, RegOffset, Data);
  109. }
  110. void it6151_reg_i2c_write_byte(U8 dev_addr,U8 cmd, U8 data)
  111. {
  112. U8 write_data[8];
  113. write_data[0] = cmd;
  114. write_data[1] = data;
  115. if(dev_addr == 0x5C)
  116. {
  117. it6151_0->addr = 0x5C;
  118. it6151_0->ext_flag = 0;
  119. i2c_master_send(it6151_0, write_data, 2);
  120. }
  121. else if(dev_addr == 0x6C)
  122. {
  123. it6151_1->addr = 0x6C;
  124. it6151_1->ext_flag = 0;
  125. i2c_master_send(it6151_1, write_data, 2);
  126. }
  127. else
  128. printk("[it6151_reg_i2c_read_byte]error: no this dev_addr \n");
  129. }
  130. static int match_id(const struct i2c_device_id *id, const struct i2c_client *client)
  131. {
  132. if (strcmp(client->name, id->name) == 0)
  133. return true;
  134. return false;
  135. }
  136. static int it6151_i2c_probe(struct i2c_client *client,
  137. const struct i2c_device_id *id)
  138. {
  139. int ret = 0;
  140. int err=0;
  141. if(match_id(&it6151_id[0], client))
  142. {
  143. if (!(it6151_0 = kmalloc(sizeof(struct i2c_client), GFP_KERNEL))) {
  144. err = -ENOMEM;
  145. goto exit;
  146. }
  147. memset(it6151_0, 0, sizeof(struct i2c_client));
  148. it6151_0 = client;
  149. dev_info(&client->adapter->dev, "attached it6151_0: %s "
  150. "into i2c adapter successfully \n", id->name);
  151. }
  152. else if(match_id(&it6151_id[1], client))
  153. {
  154. if (!(it6151_1 = kmalloc(sizeof(struct i2c_client), GFP_KERNEL))) {
  155. err = -ENOMEM;
  156. goto exit;
  157. }
  158. memset(it6151_1, 0, sizeof(struct i2c_client));
  159. it6151_1 = client;
  160. dev_info(&client->adapter->dev, "attached it6151_1: %s "
  161. "into i2c adapter successfully \n", id->name);
  162. }
  163. else
  164. {
  165. dev_err(&client->adapter->dev, "invalid i2c adapter: can not found dev_id matched\n");
  166. return -EIO;
  167. }
  168. printk("[it6151_i2c_probe] Suss \n");
  169. return ret;
  170. exit:
  171. return err;
  172. }
  173. static struct i2c_driver it6151_i2c_driver = {
  174. .driver = {
  175. .name = DEVICE_NAME,
  176. },
  177. .probe = it6151_i2c_probe,
  178. .id_table = it6151_id,
  179. };
  180. static int __init it6151_i2c_init(void)
  181. {
  182. // int ret=0; //fixed for build warning
  183. //battery_xlog_printk(BAT_LOG_CRTI,"[it6151_i2c_init] init start\n");
  184. printk("[it6151_i2c_init] init start\n");
  185. i2c_register_board_info(it6151_BUSNUM, it6151_i2c, 2);
  186. if(i2c_add_driver(&it6151_i2c_driver)!=0)
  187. {
  188. //battery_xlog_printk(BAT_LOG_CRTI,"[it6151_i2c_init] failed to register it6151 i2c driver.\n");
  189. printk("[it6151_i2c_init] failed to register it6151 i2c driver.\n");
  190. }
  191. else
  192. {
  193. //battery_xlog_printk(BAT_LOG_CRTI,"[it6151_i2c_init] Success to register it6151 i2c driver.\n");
  194. printk("[it6151_i2c_init] Success to register it6151 i2c driver.\n");
  195. }
  196. return 0;
  197. }
  198. static void __exit it6151_i2c_exit(void)
  199. {
  200. i2c_del_driver(&it6151_i2c_driver);
  201. }
  202. module_init(it6151_i2c_init);
  203. module_exit(it6151_i2c_exit);
  204. MODULE_DESCRIPTION("I2C it6151 Driver");
  205. MODULE_AUTHOR("jitao.shi<jitao.shi@mediatek.com>");
  206. #endif