tps6507x.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. int ret = 0;
  77. tps6507x = kzalloc(sizeof(struct tps6507x_dev), 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. ret = mfd_add_devices(tps6507x->dev, -1,
  86. tps6507x_devs, ARRAY_SIZE(tps6507x_devs),
  87. NULL, 0);
  88. if (ret < 0)
  89. goto err;
  90. return ret;
  91. err:
  92. mfd_remove_devices(tps6507x->dev);
  93. kfree(tps6507x);
  94. return ret;
  95. }
  96. static int tps6507x_i2c_remove(struct i2c_client *i2c)
  97. {
  98. struct tps6507x_dev *tps6507x = i2c_get_clientdata(i2c);
  99. mfd_remove_devices(tps6507x->dev);
  100. kfree(tps6507x);
  101. return 0;
  102. }
  103. static const struct i2c_device_id tps6507x_i2c_id[] = {
  104. { "tps6507x", 0 },
  105. { }
  106. };
  107. MODULE_DEVICE_TABLE(i2c, tps6507x_i2c_id);
  108. static struct i2c_driver tps6507x_i2c_driver = {
  109. .driver = {
  110. .name = "tps6507x",
  111. .owner = THIS_MODULE,
  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");