tps6507x.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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_device.h>
  22. #include <linux/mfd/core.h>
  23. #include <linux/mfd/tps6507x.h>
  24. static struct mfd_cell tps6507x_devs[] = {
  25. {
  26. .name = "tps6507x-pmic",
  27. },
  28. {
  29. .name = "tps6507x-ts",
  30. },
  31. };
  32. static int tps6507x_i2c_read_device(struct tps6507x_dev *tps6507x, char reg,
  33. int bytes, void *dest)
  34. {
  35. struct i2c_client *i2c = tps6507x->i2c_client;
  36. struct i2c_msg xfer[2];
  37. int ret;
  38. /* Write register */
  39. xfer[0].addr = i2c->addr;
  40. xfer[0].flags = 0;
  41. xfer[0].len = 1;
  42. xfer[0].buf = &reg;
  43. /* Read data */
  44. xfer[1].addr = i2c->addr;
  45. xfer[1].flags = I2C_M_RD;
  46. xfer[1].len = bytes;
  47. xfer[1].buf = dest;
  48. ret = i2c_transfer(i2c->adapter, xfer, 2);
  49. if (ret == 2)
  50. ret = 0;
  51. else if (ret >= 0)
  52. ret = -EIO;
  53. return ret;
  54. }
  55. static int tps6507x_i2c_write_device(struct tps6507x_dev *tps6507x, char reg,
  56. int bytes, void *src)
  57. {
  58. struct i2c_client *i2c = tps6507x->i2c_client;
  59. /* we add 1 byte for device register */
  60. u8 msg[TPS6507X_MAX_REGISTER + 1];
  61. int ret;
  62. if (bytes > TPS6507X_MAX_REGISTER)
  63. return -EINVAL;
  64. msg[0] = reg;
  65. memcpy(&msg[1], src, bytes);
  66. ret = i2c_master_send(i2c, msg, bytes + 1);
  67. if (ret < 0)
  68. return ret;
  69. if (ret != bytes + 1)
  70. return -EIO;
  71. return 0;
  72. }
  73. static int tps6507x_i2c_probe(struct i2c_client *i2c,
  74. const struct i2c_device_id *id)
  75. {
  76. struct tps6507x_dev *tps6507x;
  77. tps6507x = devm_kzalloc(&i2c->dev, sizeof(struct tps6507x_dev),
  78. GFP_KERNEL);
  79. if (tps6507x == NULL)
  80. return -ENOMEM;
  81. i2c_set_clientdata(i2c, tps6507x);
  82. tps6507x->dev = &i2c->dev;
  83. tps6507x->i2c_client = i2c;
  84. tps6507x->read_dev = tps6507x_i2c_read_device;
  85. tps6507x->write_dev = tps6507x_i2c_write_device;
  86. return mfd_add_devices(tps6507x->dev, -1, tps6507x_devs,
  87. ARRAY_SIZE(tps6507x_devs), NULL, 0, NULL);
  88. }
  89. static int tps6507x_i2c_remove(struct i2c_client *i2c)
  90. {
  91. struct tps6507x_dev *tps6507x = i2c_get_clientdata(i2c);
  92. mfd_remove_devices(tps6507x->dev);
  93. return 0;
  94. }
  95. static const struct i2c_device_id tps6507x_i2c_id[] = {
  96. { "tps6507x", 0 },
  97. { }
  98. };
  99. MODULE_DEVICE_TABLE(i2c, tps6507x_i2c_id);
  100. #ifdef CONFIG_OF
  101. static struct of_device_id tps6507x_of_match[] = {
  102. {.compatible = "ti,tps6507x", },
  103. {},
  104. };
  105. MODULE_DEVICE_TABLE(of, tps6507x_of_match);
  106. #endif
  107. static struct i2c_driver tps6507x_i2c_driver = {
  108. .driver = {
  109. .name = "tps6507x",
  110. .owner = THIS_MODULE,
  111. .of_match_table = of_match_ptr(tps6507x_of_match),
  112. },
  113. .probe = tps6507x_i2c_probe,
  114. .remove = tps6507x_i2c_remove,
  115. .id_table = tps6507x_i2c_id,
  116. };
  117. static int __init tps6507x_i2c_init(void)
  118. {
  119. return i2c_add_driver(&tps6507x_i2c_driver);
  120. }
  121. /* init early so consumer devices can complete system boot */
  122. subsys_initcall(tps6507x_i2c_init);
  123. static void __exit tps6507x_i2c_exit(void)
  124. {
  125. i2c_del_driver(&tps6507x_i2c_driver);
  126. }
  127. module_exit(tps6507x_i2c_exit);
  128. MODULE_DESCRIPTION("TPS6507x chip family multi-function driver");
  129. MODULE_LICENSE("GPL");