sn65dsi83_i2c.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #ifndef BUILD_LK
  2. #include <linux/interrupt.h>
  3. #include <linux/i2c.h>
  4. #include <linux/slab.h>
  5. #include <linux/irq.h>
  6. #include <linux/miscdevice.h>
  7. #include <asm/uaccess.h>
  8. #include <linux/delay.h>
  9. #include <linux/input.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/kobject.h>
  12. #include <linux/platform_device.h>
  13. #include <asm/atomic.h>
  14. #include <linux/of.h>
  15. #include <linux/module.h>
  16. #include "sn65dsi83_i2c.h"
  17. /**********************************************************
  18. *
  19. * [I2C Slave Setting]
  20. *
  21. *********************************************************/
  22. #define sn65dsi83_SLAVE_ADDR_WRITE 0x58
  23. static struct i2c_client *new_client;
  24. static const struct i2c_device_id sn65dsi83_i2c_id[] = { {"sn65dsi83", 0}, {} };
  25. static int sn65dsi83_driver_probe(struct i2c_client *client, const struct i2c_device_id *id);
  26. static int sn65dsi83_driver_remove(struct i2c_client *client);
  27. #ifdef CONFIG_OF
  28. static const struct of_device_id sn65dsi83_id[] = {
  29. {.compatible = "sn65dsi83"},
  30. {},
  31. };
  32. MODULE_DEVICE_TABLE(of, sn65dsi83_id);
  33. #endif
  34. static struct i2c_driver sn65dsi83_driver = {
  35. .driver = {
  36. .owner = THIS_MODULE,
  37. .name = "sn65dsi83",
  38. #ifdef CONFIG_OF
  39. .of_match_table = of_match_ptr(sn65dsi83_id),
  40. #endif
  41. },
  42. .probe = sn65dsi83_driver_probe,
  43. .remove = sn65dsi83_driver_remove,
  44. .id_table = sn65dsi83_i2c_id,
  45. };
  46. /**********************************************************
  47. *
  48. * [Global Variable]
  49. *
  50. *********************************************************/
  51. static DEFINE_MUTEX(sn65dsi83_i2c_access);
  52. /**********************************************************
  53. *
  54. * [I2C Function For Read/Write fan5405]
  55. *
  56. *********************************************************/
  57. int sn65dsi83_read_byte(kal_uint8 cmd, kal_uint8 *returnData)
  58. {
  59. char cmd_buf[2] = { 0x00, 0x00 };
  60. char readData = 0;
  61. int ret = 0;
  62. mutex_lock(&sn65dsi83_i2c_access);
  63. /*
  64. new_client->ext_flag =
  65. ((new_client->ext_flag) & I2C_MASK_FLAG) | I2C_WR_FLAG | I2C_DIRECTION_FLAG;
  66. */
  67. cmd_buf[0] = cmd;
  68. ret = i2c_master_send(new_client, &cmd_buf[0], 1);
  69. ret = i2c_master_recv(new_client, &cmd_buf[1], 1);
  70. if (ret < 0) {
  71. /*new_client->ext_flag = 0; */
  72. mutex_unlock(&sn65dsi83_i2c_access);
  73. return 0;
  74. }
  75. readData = cmd_buf[1];
  76. *returnData = readData;
  77. /*new_client->ext_flag = 0; */
  78. mutex_unlock(&sn65dsi83_i2c_access);
  79. return 1;
  80. }
  81. int sn65dsi83_write_byte(kal_uint8 cmd, kal_uint8 writeData)
  82. {
  83. char write_data[2] = { 0 };
  84. int ret = 0;
  85. mutex_lock(&sn65dsi83_i2c_access);
  86. write_data[0] = cmd;
  87. write_data[1] = writeData;
  88. /*new_client->ext_flag = ((new_client->ext_flag) & I2C_MASK_FLAG) | I2C_DIRECTION_FLAG; */
  89. ret = i2c_master_send(new_client, write_data, 2);
  90. if (ret < 0) {
  91. /*new_client->ext_flag = 0; */
  92. mutex_unlock(&sn65dsi83_i2c_access);
  93. return 0;
  94. }
  95. /*new_client->ext_flag = 0; */
  96. mutex_unlock(&sn65dsi83_i2c_access);
  97. return 1;
  98. }
  99. static int sn65dsi83_driver_probe(struct i2c_client *client, const struct i2c_device_id *id)
  100. {
  101. int err = 0;
  102. pr_debug("[sn65dsi83_driver_probe]name=%s addr=0x%x\n", client->name, client->addr);
  103. new_client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  104. if (!new_client) {
  105. err = -ENOMEM;
  106. goto exit;
  107. }
  108. memset(new_client, 0, sizeof(struct i2c_client));
  109. new_client = client;
  110. return 0;
  111. exit:
  112. return err;
  113. }
  114. static int sn65dsi83_driver_remove(struct i2c_client *client)
  115. {
  116. pr_debug("sn65dsi83_driver_remove\n");
  117. new_client = NULL;
  118. i2c_unregister_device(client);
  119. return 0;
  120. }
  121. #define SN65DSI83_BUSNUM 4
  122. /*
  123. static struct i2c_board_info i2c_sn65dsi83 __initdata = {
  124. I2C_BOARD_INFO("sn65dsi83", (sn65dsi83_SLAVE_ADDR_WRITE >> 1))
  125. };
  126. */
  127. static int __init sn65dsi83_init(void)
  128. {
  129. pr_notice("[sn65dsi83_init] init start\n");
  130. /* i2c_register_board_info(SN65DSI83_BUSNUM, &i2c_sn65dsi83, 1); */
  131. if (i2c_add_driver(&sn65dsi83_driver) != 0)
  132. pr_notice("[sn65dsi83_init] failed to register sn65dsi83 i2c driver.\n");
  133. else
  134. pr_notice("[sn65dsi83_init] Success to register sn65dsi83 i2c driver.\n");
  135. return 0;
  136. }
  137. static void __exit sn65dsi83_exit(void)
  138. {
  139. i2c_del_driver(&sn65dsi83_driver);
  140. }
  141. late_initcall(sn65dsi83_init);
  142. module_exit(sn65dsi83_exit);
  143. #endif