tps65912-i2c.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * tps65912-i2c.c -- I2C access for TI TPS65912x PMIC
  3. *
  4. * Copyright 2011 Texas Instruments Inc.
  5. *
  6. * Author: Margarita Olaya Cabrera <magi@slimlogic.co.uk>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This driver is based on wm8350 implementation.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/gpio.h>
  20. #include <linux/i2c.h>
  21. #include <linux/mfd/core.h>
  22. #include <linux/mfd/tps65912.h>
  23. static int tps65912_i2c_read(struct tps65912 *tps65912, u8 reg,
  24. int bytes, void *dest)
  25. {
  26. struct i2c_client *i2c = tps65912->control_data;
  27. struct i2c_msg xfer[2];
  28. int ret;
  29. /* Write register */
  30. xfer[0].addr = i2c->addr;
  31. xfer[0].flags = 0;
  32. xfer[0].len = 1;
  33. xfer[0].buf = &reg;
  34. /* Read data */
  35. xfer[1].addr = i2c->addr;
  36. xfer[1].flags = I2C_M_RD;
  37. xfer[1].len = bytes;
  38. xfer[1].buf = dest;
  39. ret = i2c_transfer(i2c->adapter, xfer, 2);
  40. if (ret == 2)
  41. ret = 0;
  42. else if (ret >= 0)
  43. ret = -EIO;
  44. return ret;
  45. }
  46. static int tps65912_i2c_write(struct tps65912 *tps65912, u8 reg,
  47. int bytes, void *src)
  48. {
  49. struct i2c_client *i2c = tps65912->control_data;
  50. /* we add 1 byte for device register */
  51. u8 msg[TPS6591X_MAX_REGISTER + 1];
  52. int ret;
  53. if (bytes > TPS6591X_MAX_REGISTER)
  54. return -EINVAL;
  55. msg[0] = reg;
  56. memcpy(&msg[1], src, bytes);
  57. ret = i2c_master_send(i2c, msg, bytes + 1);
  58. if (ret < 0)
  59. return ret;
  60. if (ret != bytes + 1)
  61. return -EIO;
  62. return 0;
  63. }
  64. static int tps65912_i2c_probe(struct i2c_client *i2c,
  65. const struct i2c_device_id *id)
  66. {
  67. struct tps65912 *tps65912;
  68. tps65912 = devm_kzalloc(&i2c->dev,
  69. sizeof(struct tps65912), GFP_KERNEL);
  70. if (tps65912 == NULL)
  71. return -ENOMEM;
  72. i2c_set_clientdata(i2c, tps65912);
  73. tps65912->dev = &i2c->dev;
  74. tps65912->control_data = i2c;
  75. tps65912->read = tps65912_i2c_read;
  76. tps65912->write = tps65912_i2c_write;
  77. return tps65912_device_init(tps65912);
  78. }
  79. static int tps65912_i2c_remove(struct i2c_client *i2c)
  80. {
  81. struct tps65912 *tps65912 = i2c_get_clientdata(i2c);
  82. tps65912_device_exit(tps65912);
  83. return 0;
  84. }
  85. static const struct i2c_device_id tps65912_i2c_id[] = {
  86. {"tps65912", 0 },
  87. { }
  88. };
  89. MODULE_DEVICE_TABLE(i2c, tps65912_i2c_id);
  90. static struct i2c_driver tps65912_i2c_driver = {
  91. .driver = {
  92. .name = "tps65912",
  93. },
  94. .probe = tps65912_i2c_probe,
  95. .remove = tps65912_i2c_remove,
  96. .id_table = tps65912_i2c_id,
  97. };
  98. static int __init tps65912_i2c_init(void)
  99. {
  100. int ret;
  101. ret = i2c_add_driver(&tps65912_i2c_driver);
  102. if (ret != 0)
  103. pr_err("Failed to register TPS65912 I2C driver: %d\n", ret);
  104. return ret;
  105. }
  106. /* init early so consumer devices can complete system boot */
  107. subsys_initcall(tps65912_i2c_init);
  108. static void __exit tps65912_i2c_exit(void)
  109. {
  110. i2c_del_driver(&tps65912_i2c_driver);
  111. }
  112. module_exit(tps65912_i2c_exit);
  113. MODULE_AUTHOR("Margarita Olaya <magi@slimlogic.co.uk>");
  114. MODULE_DESCRIPTION("TPS6591x chip family multi-function driver");
  115. MODULE_LICENSE("GPL");