tps6507x.c 3.1 KB

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