tps6507x.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * tps6507x.c -- TPS6507x chip family multi-function driver
  3. *
  4. * Copyright (c) 2010 RidgeRun (todd.fischer@ridgerun.com)
  5. *
  6. * Author: Todd Fischer
  7. * todd.fischer@ridgerun.com
  8. *
  9. * Credits:
  10. *
  11. * Using code from wm831x-*.c, wm8400-core, Wolfson Microelectronics PLC.
  12. *
  13. * For licencing details see kernel-base/COPYING
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/i2c.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/mfd/core.h>
  24. #include <linux/mfd/tps6507x.h>
  25. static const struct mfd_cell tps6507x_devs[] = {
  26. {
  27. .name = "tps6507x-pmic",
  28. },
  29. {
  30. .name = "tps6507x-ts",
  31. },
  32. };
  33. static int tps6507x_i2c_read_device(struct tps6507x_dev *tps6507x, char reg,
  34. int bytes, void *dest)
  35. {
  36. struct i2c_client *i2c = tps6507x->i2c_client;
  37. struct i2c_msg xfer[2];
  38. int ret;
  39. /* Write register */
  40. xfer[0].addr = i2c->addr;
  41. xfer[0].flags = 0;
  42. xfer[0].len = 1;
  43. xfer[0].buf = &reg;
  44. /* Read data */
  45. xfer[1].addr = i2c->addr;
  46. xfer[1].flags = I2C_M_RD;
  47. xfer[1].len = bytes;
  48. xfer[1].buf = dest;
  49. ret = i2c_transfer(i2c->adapter, xfer, 2);
  50. if (ret == 2)
  51. ret = 0;
  52. else if (ret >= 0)
  53. ret = -EIO;
  54. return ret;
  55. }
  56. static int tps6507x_i2c_write_device(struct tps6507x_dev *tps6507x, char reg,
  57. int bytes, void *src)
  58. {
  59. struct i2c_client *i2c = tps6507x->i2c_client;
  60. /* we add 1 byte for device register */
  61. u8 msg[TPS6507X_MAX_REGISTER + 1];
  62. int ret;
  63. if (bytes > TPS6507X_MAX_REGISTER)
  64. return -EINVAL;
  65. msg[0] = reg;
  66. memcpy(&msg[1], src, bytes);
  67. ret = i2c_master_send(i2c, msg, bytes + 1);
  68. if (ret < 0)
  69. return ret;
  70. if (ret != bytes + 1)
  71. return -EIO;
  72. return 0;
  73. }
  74. static int tps6507x_i2c_probe(struct i2c_client *i2c,
  75. const struct i2c_device_id *id)
  76. {
  77. struct tps6507x_dev *tps6507x;
  78. tps6507x = devm_kzalloc(&i2c->dev, sizeof(struct tps6507x_dev),
  79. GFP_KERNEL);
  80. if (tps6507x == NULL)
  81. return -ENOMEM;
  82. i2c_set_clientdata(i2c, tps6507x);
  83. tps6507x->dev = &i2c->dev;
  84. tps6507x->i2c_client = i2c;
  85. tps6507x->read_dev = tps6507x_i2c_read_device;
  86. tps6507x->write_dev = tps6507x_i2c_write_device;
  87. return devm_mfd_add_devices(tps6507x->dev, -1, tps6507x_devs,
  88. ARRAY_SIZE(tps6507x_devs), NULL, 0, NULL);
  89. }
  90. static const struct i2c_device_id tps6507x_i2c_id[] = {
  91. { "tps6507x", 0 },
  92. { }
  93. };
  94. MODULE_DEVICE_TABLE(i2c, tps6507x_i2c_id);
  95. #ifdef CONFIG_OF
  96. static const struct of_device_id tps6507x_of_match[] = {
  97. {.compatible = "ti,tps6507x", },
  98. {},
  99. };
  100. MODULE_DEVICE_TABLE(of, tps6507x_of_match);
  101. #endif
  102. static struct i2c_driver tps6507x_i2c_driver = {
  103. .driver = {
  104. .name = "tps6507x",
  105. .of_match_table = of_match_ptr(tps6507x_of_match),
  106. },
  107. .probe = tps6507x_i2c_probe,
  108. .id_table = tps6507x_i2c_id,
  109. };
  110. static int __init tps6507x_i2c_init(void)
  111. {
  112. return i2c_add_driver(&tps6507x_i2c_driver);
  113. }
  114. /* init early so consumer devices can complete system boot */
  115. subsys_initcall(tps6507x_i2c_init);
  116. static void __exit tps6507x_i2c_exit(void)
  117. {
  118. i2c_del_driver(&tps6507x_i2c_driver);
  119. }
  120. module_exit(tps6507x_i2c_exit);
  121. MODULE_DESCRIPTION("TPS6507x chip family multi-function driver");
  122. MODULE_LICENSE("GPL");