tps65910.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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)
  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. struct tps65910_board *pmic_plat_data;
  118. struct tps65910_platform_data *init_data;
  119. int ret = 0;
  120. pmic_plat_data = dev_get_platdata(&i2c->dev);
  121. if (!pmic_plat_data)
  122. return -EINVAL;
  123. init_data = kzalloc(sizeof(struct tps65910_platform_data), GFP_KERNEL);
  124. if (init_data == NULL)
  125. return -ENOMEM;
  126. init_data->irq = pmic_plat_data->irq;
  127. init_data->irq_base = pmic_plat_data->irq;
  128. tps65910 = kzalloc(sizeof(struct tps65910), GFP_KERNEL);
  129. if (tps65910 == NULL)
  130. return -ENOMEM;
  131. i2c_set_clientdata(i2c, tps65910);
  132. tps65910->dev = &i2c->dev;
  133. tps65910->i2c_client = i2c;
  134. tps65910->id = id->driver_data;
  135. tps65910->read = tps65910_i2c_read;
  136. tps65910->write = tps65910_i2c_write;
  137. mutex_init(&tps65910->io_mutex);
  138. ret = mfd_add_devices(tps65910->dev, -1,
  139. tps65910s, ARRAY_SIZE(tps65910s),
  140. NULL, 0);
  141. if (ret < 0)
  142. goto err;
  143. tps65910_gpio_init(tps65910, pmic_plat_data->gpio_base);
  144. ret = tps65910_irq_init(tps65910, init_data->irq, init_data);
  145. if (ret < 0)
  146. goto err;
  147. return ret;
  148. err:
  149. mfd_remove_devices(tps65910->dev);
  150. kfree(tps65910);
  151. return ret;
  152. }
  153. static int tps65910_i2c_remove(struct i2c_client *i2c)
  154. {
  155. struct tps65910 *tps65910 = i2c_get_clientdata(i2c);
  156. mfd_remove_devices(tps65910->dev);
  157. kfree(tps65910);
  158. return 0;
  159. }
  160. static const struct i2c_device_id tps65910_i2c_id[] = {
  161. { "tps65910", TPS65910 },
  162. { "tps65911", TPS65911 },
  163. { }
  164. };
  165. MODULE_DEVICE_TABLE(i2c, tps65910_i2c_id);
  166. static struct i2c_driver tps65910_i2c_driver = {
  167. .driver = {
  168. .name = "tps65910",
  169. .owner = THIS_MODULE,
  170. },
  171. .probe = tps65910_i2c_probe,
  172. .remove = tps65910_i2c_remove,
  173. .id_table = tps65910_i2c_id,
  174. };
  175. static int __init tps65910_i2c_init(void)
  176. {
  177. return i2c_add_driver(&tps65910_i2c_driver);
  178. }
  179. /* init early so consumer devices can complete system boot */
  180. subsys_initcall(tps65910_i2c_init);
  181. static void __exit tps65910_i2c_exit(void)
  182. {
  183. i2c_del_driver(&tps65910_i2c_driver);
  184. }
  185. module_exit(tps65910_i2c_exit);
  186. MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
  187. MODULE_AUTHOR("Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>");
  188. MODULE_DESCRIPTION("TPS6591x chip family multi-function driver");
  189. MODULE_LICENSE("GPL");