it6121_i2c.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 it6121_MIPIRX_SLAVE_ADDR_WRITE (0x6C << 0)
  64. #define DEVICE_NAME "it6121"
  65. static struct i2c_client *it6121_mipirx = NULL;
  66. static const struct i2c_device_id it6121_i2c_id[] =
  67. {
  68. {"it6121_mipirx", 0},
  69. };
  70. static int it6121_i2c_driver_probe(struct i2c_client *client, const struct i2c_device_id *id);
  71. static struct i2c_driver it6121_i2c_driver = {
  72. .driver = {
  73. .name = DEVICE_NAME,
  74. },
  75. .probe = it6121_i2c_driver_probe,
  76. .id_table = it6121_i2c_id,
  77. };
  78. /**********************************************************
  79. *
  80. * [Global Variable]
  81. *
  82. *********************************************************/
  83. static DEFINE_MUTEX(it6121_i2c_access);
  84. /**********************************************************
  85. *
  86. * [I2C Function For Read/Write fan5405]
  87. *
  88. *********************************************************/
  89. int it6121_i2c_read_byte(kal_uint8 dev_addr, kal_uint8 addr, kal_uint8 *returnData)
  90. {
  91. char cmd_buf[1]={0x00};
  92. char readData = 0;
  93. int ret=0;
  94. #ifdef it6121_DEBUG
  95. /* dump write_data for check */
  96. printk("[KE/it6121_read_byte] dev_addr = 0x%x, read_data[0x%x] = 0x%x \n", dev_addr, addr, *returnData);
  97. #endif
  98. mutex_lock(&it6121_i2c_access);
  99. if(dev_addr == it6121_MIPIRX_SLAVE_ADDR_WRITE)
  100. {
  101. it6121_mipirx->ext_flag=((it6121_mipirx->ext_flag) & I2C_MASK_FLAG ) | I2C_WR_FLAG | I2C_DIRECTION_FLAG;
  102. cmd_buf[0] = addr;
  103. ret = i2c_master_send(it6121_mipirx, &cmd_buf[0], (1<<8 | 1));
  104. if (ret < 0)
  105. {
  106. it6121_mipirx->ext_flag=0;
  107. mutex_unlock(&it6121_i2c_access);
  108. return 0;
  109. }
  110. readData = cmd_buf[0];
  111. *returnData = readData;
  112. it6121_mipirx->ext_flag=0;
  113. }
  114. else
  115. {
  116. printk("[it6121_i2c_read_byte]error: no this dev_addr! \n");
  117. }
  118. mutex_unlock(&it6121_i2c_access);
  119. return 1;
  120. }
  121. int it6121_i2c_write_byte(kal_uint8 dev_addr, kal_uint8 addr, kal_uint8 writeData)
  122. {
  123. char write_data[2] = {0};
  124. int ret=0;
  125. #ifdef it6121_DEBUG
  126. /* dump write_data for check */
  127. printk("[KE/it6121_i2c_write] dev_addr = 0x%x, write_data[0x%x] = 0x%x \n", dev_addr, addr, writeData);
  128. #endif
  129. mutex_lock(&it6121_i2c_access);
  130. write_data[0] = addr;
  131. write_data[1] = writeData;
  132. if(dev_addr == it6121_MIPIRX_SLAVE_ADDR_WRITE)
  133. {
  134. it6121_mipirx->addr = it6121_MIPIRX_SLAVE_ADDR_WRITE;
  135. it6121_mipirx->ext_flag=((it6121_mipirx->ext_flag) & I2C_MASK_FLAG) | I2C_DIRECTION_FLAG;
  136. ret = i2c_master_send(it6121_mipirx, write_data, 2);
  137. if(ret < 0)
  138. {
  139. it6121_mipirx->ext_flag=0;
  140. mutex_unlock(&it6121_i2c_access);
  141. return 0;
  142. }
  143. it6121_mipirx->ext_flag=0;
  144. }
  145. else
  146. {
  147. printk("[it6121_i2c_write_byte]error: no this dev_addr! \n");
  148. }
  149. mutex_unlock(&it6121_i2c_access);
  150. return 1;
  151. }
  152. static int match_id(const struct i2c_client *client, const struct i2c_device_id *id)
  153. {
  154. if (strcmp(client->name, id->name) == 0)
  155. return true;
  156. else
  157. return false;
  158. }
  159. static int it6121_i2c_driver_probe(struct i2c_client *client, const struct i2c_device_id *id)
  160. {
  161. int err=0;
  162. printk("[it6121_i2c_driver_probe] start!\n");
  163. if(match_id(client, &it6121_i2c_id[0]))
  164. {
  165. if (!(it6121_mipirx = kmalloc(sizeof(struct i2c_client), GFP_KERNEL)))
  166. {
  167. err = -ENOMEM;
  168. goto exit;
  169. }
  170. memset(it6121_mipirx, 0, sizeof(struct i2c_client));
  171. it6121_mipirx = client;
  172. }
  173. else
  174. {
  175. printk("[it6121_i2c_driver_probe] error!\n");
  176. err = -EIO;
  177. goto exit;
  178. }
  179. printk("[it6121_i2c_driver_probe] %s i2c sucess!\n", client->name);
  180. return 0;
  181. exit:
  182. return err;
  183. }
  184. #define IT6121_BUSNUM 0
  185. static struct i2c_board_info __initdata it6121_I2C[] =
  186. {
  187. {I2C_BOARD_INFO("it6121_mipirx", it6121_MIPIRX_SLAVE_ADDR_WRITE)},
  188. };
  189. static int __init it6121_init(void)
  190. {
  191. printk("[it6121_init] init start\n");
  192. i2c_register_board_info(IT6121_BUSNUM, it6121_I2C, 1);
  193. if(i2c_add_driver(&it6121_i2c_driver)!=0)
  194. {
  195. printk("[it6121_init] Failed to register it6121 i2c driver.\n");
  196. }
  197. else
  198. {
  199. printk("[it6121_init] Success to register it6121 i2c driver.\n");
  200. }
  201. return 0;
  202. }
  203. static void __exit it6121_exit(void)
  204. {
  205. i2c_del_driver(&it6121_i2c_driver);
  206. }
  207. module_init(it6121_init);
  208. module_exit(it6121_exit);
  209. MODULE_DESCRIPTION("I2C IT6121 Driver");
  210. MODULE_AUTHOR("James Lo<james.lo@mediatek.com>");
  211. #endif