mt8193_iic.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #define pr_fmt(fmt) "mt8193-iic: " fmt
  2. #define DEBUG 1
  3. #include <generated/autoconf.h>
  4. #include <linux/mm.h>
  5. #include <linux/init.h>
  6. #include <linux/fb.h>
  7. #include <linux/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/kthread.h>
  11. #include <linux/rtpm_prio.h>
  12. #include <linux/vmalloc.h>
  13. #include <asm/uaccess.h>
  14. #include <asm/atomic.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/io.h>
  17. #include <mach/irqs.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/fs.h>
  21. #include <linux/file.h>
  22. #include <linux/mm.h>
  23. #include <linux/cdev.h>
  24. #include <asm/tlbflush.h>
  25. #include <asm/page.h>
  26. #include <asm/io.h>
  27. #include <asm/uaccess.h>
  28. #include <linux/slab.h>
  29. #include <generated/autoconf.h>
  30. #include <linux/module.h>
  31. #include <linux/mm.h>
  32. #include <linux/init.h>
  33. #include <linux/fb.h>
  34. #include <linux/delay.h>
  35. #include <linux/device.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/kthread.h>
  38. #include <linux/rtpm_prio.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/atomic.h>
  41. #include <asm/cacheflush.h>
  42. #include <asm/io.h>
  43. #include <mach/irqs.h>
  44. #include <linux/vmalloc.h>
  45. #include <asm/uaccess.h>
  46. #include "mt8193_iic.h"
  47. #include "mt8193.h"
  48. /*----------------------------------------------------------------------------*/
  49. /* mt8193 device information */
  50. /*----------------------------------------------------------------------------*/
  51. #define MAX_TRANSACTION_LENGTH 8
  52. #define MT8193_DEVICE_NAME "mtk-multibridge"
  53. #define MT8193_I2C_SLAVE_ADDR 0x3A
  54. #define MT8193_I2C_DEVICE_ADDR_LEN 2
  55. /*----------------------------------------------------------------------------*/
  56. static int mt8193_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
  57. static int mt8193_i2c_remove(struct i2c_client *client);
  58. static struct i2c_client *mt8193_i2c_client;
  59. static const struct i2c_device_id mt8193_i2c_id[] = {{MT8193_DEVICE_NAME, 0}, {} };
  60. static struct i2c_board_info i2c_mt8193 __initdata = {I2C_BOARD_INFO(MT8193_DEVICE_NAME, (MT8193_I2C_SLAVE_ADDR>>1))};
  61. /*----------------------------------------------------------------------------*/
  62. struct i2c_driver mt8193_i2c_driver = {
  63. .probe = mt8193_i2c_probe,
  64. .remove = mt8193_i2c_remove,
  65. .driver = { .name = MT8193_DEVICE_NAME, },
  66. .id_table = mt8193_i2c_id,
  67. };
  68. struct mt8193_i2c_data {
  69. struct i2c_client *client;
  70. uint16_t addr;
  71. int use_reset; /*use RESET flag*/
  72. int use_irq; /*use EINT flag*/
  73. int retry;
  74. };
  75. static struct mt8193_i2c_data *obj_i2c_data;
  76. /*----------------------------------------------------------------------------*/
  77. int mt8193_i2c_read(u16 addr, u32 *data)
  78. {
  79. u8 rxBuf[8] = {0};
  80. int ret = 0;
  81. struct i2c_client *client = mt8193_i2c_client;
  82. u8 lens;
  83. if (((addr >> 8) & 0xFF) >= 0x80) {
  84. /* 8 bit : fast mode */
  85. rxBuf[0] = (addr >> 8) & 0xFF;
  86. lens = 1;
  87. } else {
  88. /* 16 bit : noraml mode */
  89. rxBuf[0] = (addr >> 8) & 0xFF;
  90. rxBuf[1] = addr & 0xFF;
  91. lens = 2;
  92. }
  93. client->addr = (client->addr & I2C_MASK_FLAG);
  94. client->timing = 400;
  95. client->ext_flag = I2C_WR_FLAG;
  96. ret = i2c_master_send(client, (const char *)&rxBuf, (4 << 8) | lens);
  97. if (ret < 0) {
  98. pr_err("%s: read error\n", __func__);
  99. return -EFAULT;
  100. }
  101. *data = (rxBuf[3] << 24) | (rxBuf[2] << 16) | (rxBuf[1] << 8) | (rxBuf[0]); /*LSB fisrt*/
  102. return 0;
  103. }
  104. /*----------------------------------------------------------------------------*/
  105. EXPORT_SYMBOL_GPL(mt8193_i2c_read);
  106. /*----------------------------------------------------------------------------*/
  107. int mt8193_i2c_write(u16 addr, u32 data)
  108. {
  109. struct i2c_client *client = mt8193_i2c_client;
  110. u8 buffer[8];
  111. int ret = 0;
  112. struct i2c_msg msg = {
  113. .addr = client->addr & I2C_MASK_FLAG,
  114. .flags = 0,
  115. .len = (((addr >> 8) & 0xFF) >= 0x80)?5:6,
  116. .buf = buffer,
  117. .timing = 400,
  118. };
  119. if (((addr >> 8) & 0xFF) >= 0x80) {
  120. /* 8 bit : fast mode */
  121. buffer[0] = (addr >> 8) & 0xFF;
  122. buffer[1] = (data >> 24) & 0xFF;
  123. buffer[2] = (data >> 16) & 0xFF;
  124. buffer[3] = (data >> 8) & 0xFF;
  125. buffer[4] = data & 0xFF;
  126. } else {
  127. /* 16 bit : noraml mode */
  128. buffer[0] = (addr >> 8) & 0xFF;
  129. buffer[1] = addr & 0xFF;
  130. buffer[2] = (data >> 24) & 0xFF;
  131. buffer[3] = (data >> 16) & 0xFF;
  132. buffer[4] = (data >> 8) & 0xFF;
  133. buffer[5] = data & 0xFF;
  134. }
  135. ret = i2c_transfer(client->adapter, &msg, 1);
  136. if (ret < 0) {
  137. pr_err("%s: send command error\n", __func__);
  138. return -EFAULT;
  139. }
  140. return 0;
  141. }
  142. /*----------------------------------------------------------------------------*/
  143. EXPORT_SYMBOL_GPL(mt8193_i2c_write);
  144. /*----------------------------------------------------------------------------*/
  145. /* IIC Probe */
  146. /*----------------------------------------------------------------------------*/
  147. static int mt8193_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  148. {
  149. int ret = -1;
  150. struct mt8193_i2c_data *obj;
  151. pr_debug("%s\n", __func__);
  152. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  153. if (obj == NULL) {
  154. ret = -ENOMEM;
  155. pr_err("%s: Allocate ts memory fail\n", __func__);
  156. return ret;
  157. }
  158. obj_i2c_data = obj;
  159. client->timing = 400;
  160. obj->client = client;
  161. mt8193_i2c_client = obj->client;
  162. i2c_set_clientdata(client, obj);
  163. return 0;
  164. }
  165. /*----------------------------------------------------------------------------*/
  166. static int mt8193_i2c_remove(struct i2c_client *client)
  167. {
  168. pr_debug("%s\n", __func__);
  169. mt8193_i2c_client = NULL;
  170. i2c_unregister_device(client);
  171. kfree(i2c_get_clientdata(client));
  172. return 0;
  173. }
  174. /*----------------------------------------------------------------------------*/
  175. /* device driver probe */
  176. /*----------------------------------------------------------------------------*/
  177. static int mt8193_probe(struct platform_device *pdev)
  178. {
  179. pr_debug("%s\n", __func__);
  180. if (i2c_add_driver(&mt8193_i2c_driver)) {
  181. pr_err("%s: unable to add mt8193 i2c driver\n", __func__);
  182. return -1;
  183. }
  184. return 0;
  185. }
  186. /*----------------------------------------------------------------------------*/
  187. static int mt8193_remove(struct platform_device *pdev)
  188. {
  189. pr_debug("%s\n", __func__);
  190. i2c_del_driver(&mt8193_i2c_driver);
  191. return 0;
  192. }
  193. /*----------------------------------------------------------------------------*/
  194. #ifdef CONFIG_OF
  195. static const struct of_device_id multibridge_of_ids[] = {
  196. {.compatible = "mediatek,multibridge", },
  197. {}
  198. };
  199. #endif
  200. static struct platform_driver mt8193_mb_driver = {
  201. .probe = mt8193_probe,
  202. .remove = mt8193_remove,
  203. .driver = {
  204. .name = "multibridge",
  205. #ifdef CONFIG_OF
  206. .of_match_table = multibridge_of_ids,
  207. #endif
  208. }
  209. };
  210. /*----------------------------------------------------------------------------*/
  211. static int __init mt8193_mb_init(void)
  212. {
  213. int ret = 0;
  214. pr_debug("%s\n", __func__);
  215. /*i2c_register_board_info(1, &i2c_mt8193, 1);*/
  216. ret = platform_driver_register(&mt8193_mb_driver);
  217. if (ret) {
  218. pr_err("failed to register mt8193_mb_driver, ret=%d\n", ret);
  219. return ret;
  220. }
  221. return 0;
  222. }
  223. /*----------------------------------------------------------------------------*/
  224. static void __exit mt8193_mb_exit(void)
  225. {
  226. platform_driver_unregister(&mt8193_mb_driver);
  227. }
  228. /*----------------------------------------------------------------------------*/
  229. static int __init mt8193_i2c_board_init(void)
  230. {
  231. int ret = 0;
  232. pr_debug("%s\n", __func__);
  233. ret = i2c_register_board_info(MT8193_I2C_ID, &i2c_mt8193, 1);
  234. if (ret)
  235. pr_err("failed to register mt8193 i2c_board_info, ret=%d\n", ret);
  236. return ret;
  237. }
  238. /*----------------------------------------------------------------------------*/
  239. core_initcall(mt8193_i2c_board_init);
  240. module_init(mt8193_mb_init);
  241. module_exit(mt8193_mb_exit);
  242. MODULE_AUTHOR("SS, Wu <ss.wu@mediatek.com>");
  243. MODULE_DESCRIPTION("MT8193 Driver");
  244. MODULE_LICENSE("GPL");