bat_i2c.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * battery i2c interface
  3. * Copyright (C) 2010 Amlogic, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the named License,
  8. * or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
  18. *
  19. * Author: shoufu zhao<shoufu.zhao@amlogic.com>
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/i2c.h>
  27. #include <linux/i2c-dev.h>
  28. #include <linux/miscdevice.h>
  29. #include <linux/mutex.h>
  30. #include <linux/mm.h>
  31. #include <linux/device.h>
  32. #include <linux/fs.h>
  33. #include <linux/delay.h>
  34. #include <linux/sysctl.h>
  35. #include <asm/uaccess.h>
  36. #include <mach/pinmux.h>
  37. #include <mach/gpio.h>
  38. #include "bat_i2c.h"
  39. #define BAT_I2C_DEBUG_LOG 0
  40. #if BAT_I2C_DEBUG_LOG == 1
  41. #define logd(x...) printk(x)
  42. #else
  43. #define logd(x...) NULL
  44. #endif
  45. static struct i2c_client *bat_i2c_client = NULL;
  46. int bat_i2c_read_byte(u8 reg_port, u8 *data_buf)
  47. {
  48. s32 ret = 0;
  49. ret = i2c_smbus_read_byte_data(bat_i2c_client, reg_port);
  50. *data_buf = ret & 0xFF;
  51. udelay(100);
  52. return ret;
  53. }
  54. int bat_i2c_write_byte(u8 reg_port, u8 *data_buf)
  55. {
  56. s32 ret = 0;
  57. ret = i2c_smbus_write_byte_data(bat_i2c_client, reg_port, *data_buf);
  58. udelay(100);
  59. return ret;
  60. }
  61. int bat_i2c_read_word(u8 reg_port, u16 *data_buf)
  62. {
  63. s32 ret = 0;
  64. ret = i2c_smbus_read_word_data(bat_i2c_client, reg_port);
  65. *data_buf = ret & 0xFFFF;
  66. udelay(100);
  67. return ret;
  68. }
  69. int bat_i2c_write_word(u8 reg_port, u16 *data_buf)
  70. {
  71. s32 ret = 0;
  72. ret = i2c_smbus_write_word_data(bat_i2c_client, reg_port, *data_buf);
  73. udelay(100);
  74. return ret;
  75. }
  76. int bat_i2c_block_read(u8 reg_port, u8 *data_buf)
  77. {
  78. return i2c_smbus_read_block_data(bat_i2c_client, reg_port, data_buf);
  79. }
  80. static int bat_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
  81. {
  82. int ret = 0;
  83. logd("battery i2c probe\n");
  84. bat_i2c_client = client;
  85. return ret;
  86. }
  87. static int bat_i2c_remove(struct i2c_client *client)
  88. {
  89. return 0;
  90. }
  91. static const struct i2c_device_id bat_i2c_id[] = {
  92. { "bat_i2c", 0 },
  93. { }
  94. };
  95. static struct i2c_driver bat_i2c_driver = {
  96. .probe = bat_i2c_probe,
  97. .remove = bat_i2c_remove,
  98. .id_table = bat_i2c_id,
  99. .driver = {
  100. .name = "bat_i2c",
  101. },
  102. };
  103. static int __init bat_i2c_init(void)
  104. {
  105. int ret;
  106. logd("battery i2c init\n");
  107. if (bat_i2c_client)
  108. {
  109. ret = 0;
  110. }
  111. else
  112. {
  113. ret = i2c_add_driver(&bat_i2c_driver);
  114. if (ret < 0) {
  115. printk("add battery i2c driver error\n");
  116. }
  117. }
  118. return ret;
  119. }
  120. module_init(bat_i2c_init);