tps65910.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * tps65910.c -- TI TPS6591x
  3. *
  4. * Copyright 2010 Texas Instruments Inc.
  5. *
  6. * Author: Graeme Gregory <gg@slimlogic.co.uk>
  7. * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/i2c.h>
  20. #include <linux/gpio.h>
  21. #include <linux/mfd/core.h>
  22. #include <linux/mfd/tps65910.h>
  23. static struct mfd_cell tps65910s[] = {
  24. {
  25. .name = "tps65910-pmic",
  26. },
  27. {
  28. .name = "tps65910-rtc",
  29. },
  30. {
  31. .name = "tps65910-power",
  32. },
  33. };
  34. static int tps65910_i2c_read(struct tps65910 *tps65910, u8 reg,
  35. int bytes, void *dest)
  36. {
  37. struct i2c_client *i2c = tps65910->i2c_client;
  38. struct i2c_msg xfer[2];
  39. int ret;
  40. /* Write register */
  41. xfer[0].addr = i2c->addr;
  42. xfer[0].flags = 0;
  43. xfer[0].len = 1;
  44. xfer[0].buf = &reg;
  45. /* Read data */
  46. xfer[1].addr = i2c->addr;
  47. xfer[1].flags = I2C_M_RD;
  48. xfer[1].len = bytes;
  49. xfer[1].buf = dest;
  50. ret = i2c_transfer(i2c->adapter, xfer, 2);
  51. if (ret == 2)
  52. ret = 0;
  53. else if (ret >= 0)
  54. ret = -EIO;
  55. return ret;
  56. }
  57. static int tps65910_i2c_write(struct tps65910 *tps65910, u8 reg,
  58. int bytes, void *src)
  59. {
  60. struct i2c_client *i2c = tps65910->i2c_client;
  61. /* we add 1 byte for device register */
  62. u8 msg[TPS65910_MAX_REGISTER + 1];
  63. int ret;
  64. if (bytes > (TPS65910_MAX_REGISTER + 1))
  65. return -EINVAL;
  66. msg[0] = reg;
  67. memcpy(&msg[1], src, bytes);
  68. ret = i2c_master_send(i2c, msg, bytes + 1);
  69. if (ret < 0)
  70. return ret;
  71. if (ret != bytes + 1)
  72. return -EIO;
  73. return 0;
  74. }
  75. int tps65910_set_bits(struct tps65910 *tps65910, u8 reg, u8 mask)
  76. {
  77. u8 data;
  78. int err;
  79. mutex_lock(&tps65910->io_mutex);
  80. err = tps65910_i2c_read(tps65910, reg, 1, &data);
  81. if (err) {
  82. dev_err(tps65910->dev, "read from reg %x failed\n", reg);
  83. goto out;
  84. }
  85. data |= mask;
  86. err = tps65910_i2c_write(tps65910, reg, 1, &data);
  87. if (err)
  88. dev_err(tps65910->dev, "write to reg %x failed\n", reg);
  89. out:
  90. mutex_unlock(&tps65910->io_mutex);
  91. return err;
  92. }
  93. EXPORT_SYMBOL_GPL(tps65910_set_bits);
  94. int tps65910_clear_bits(struct tps65910 *tps65910, u8 reg, u8 mask)
  95. {
  96. u8 data;
  97. int err;
  98. mutex_lock(&tps65910->io_mutex);
  99. err = tps65910_i2c_read(tps65910, reg, 1, &data);
  100. if (err) {
  101. dev_err(tps65910->dev, "read from reg %x failed\n", reg);
  102. goto out;
  103. }
  104. data &= mask;
  105. err = tps65910_i2c_write(tps65910, reg, 1, &data);
  106. if (err)
  107. dev_err(tps65910->dev, "write to reg %x failed\n", reg);
  108. out:
  109. mutex_unlock(&tps65910->io_mutex);
  110. return err;
  111. }
  112. EXPORT_SYMBOL_GPL(tps65910_clear_bits);
  113. static int tps65910_i2c_probe(struct i2c_client *i2c,
  114. const struct i2c_device_id *id)
  115. {
  116. struct tps65910 *tps65910;
  117. int ret = 0;
  118. tps65910 = kzalloc(sizeof(struct tps65910), GFP_KERNEL);
  119. if (tps65910 == NULL)
  120. return -ENOMEM;
  121. i2c_set_clientdata(i2c, tps65910);
  122. tps65910->dev = &i2c->dev;
  123. tps65910->i2c_client = i2c;
  124. tps65910->read = tps65910_i2c_read;
  125. tps65910->write = tps65910_i2c_write;
  126. mutex_init(&tps65910->io_mutex);
  127. ret = mfd_add_devices(tps65910->dev, -1,
  128. tps65910s, ARRAY_SIZE(tps65910s),
  129. NULL, 0);
  130. if (ret < 0)
  131. goto err;
  132. return ret;
  133. err:
  134. mfd_remove_devices(tps65910->dev);
  135. kfree(tps65910);
  136. return ret;
  137. }
  138. static int tps65910_i2c_remove(struct i2c_client *i2c)
  139. {
  140. struct tps65910 *tps65910 = i2c_get_clientdata(i2c);
  141. mfd_remove_devices(tps65910->dev);
  142. kfree(tps65910);
  143. return 0;
  144. }
  145. static const struct i2c_device_id tps65910_i2c_id[] = {
  146. { "tps65910", 0 },
  147. { }
  148. };
  149. MODULE_DEVICE_TABLE(i2c, tps65910_i2c_id);
  150. static struct i2c_driver tps65910_i2c_driver = {
  151. .driver = {
  152. .name = "tps65910",
  153. .owner = THIS_MODULE,
  154. },
  155. .probe = tps65910_i2c_probe,
  156. .remove = tps65910_i2c_remove,
  157. .id_table = tps65910_i2c_id,
  158. };
  159. static int __init tps65910_i2c_init(void)
  160. {
  161. return i2c_add_driver(&tps65910_i2c_driver);
  162. }
  163. /* init early so consumer devices can complete system boot */
  164. subsys_initcall(tps65910_i2c_init);
  165. static void __exit tps65910_i2c_exit(void)
  166. {
  167. i2c_del_driver(&tps65910_i2c_driver);
  168. }
  169. module_exit(tps65910_i2c_exit);
  170. MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
  171. MODULE_AUTHOR("Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>");
  172. MODULE_DESCRIPTION("TPS6591x chip family multi-function driver");
  173. MODULE_LICENSE("GPL");