max8998.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * max8998.c - mfd core driver for the Maxim 8998
  3. *
  4. * Copyright (C) 2009-2010 Samsung Electronics
  5. * Kyungmin Park <kyungmin.park@samsung.com>
  6. * Marek Szyprowski <m.szyprowski@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/i2c.h>
  27. #include <linux/mutex.h>
  28. #include <linux/mfd/core.h>
  29. #include <linux/mfd/max8998.h>
  30. #include <linux/mfd/max8998-private.h>
  31. static struct mfd_cell max8998_devs[] = {
  32. {
  33. .name = "max8998-pmic",
  34. }
  35. };
  36. int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
  37. {
  38. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  39. int ret;
  40. mutex_lock(&max8998->iolock);
  41. ret = i2c_smbus_read_byte_data(i2c, reg);
  42. mutex_unlock(&max8998->iolock);
  43. if (ret < 0)
  44. return ret;
  45. ret &= 0xff;
  46. *dest = ret;
  47. return 0;
  48. }
  49. EXPORT_SYMBOL(max8998_read_reg);
  50. int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  51. {
  52. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  53. int ret;
  54. mutex_lock(&max8998->iolock);
  55. ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
  56. mutex_unlock(&max8998->iolock);
  57. if (ret < 0)
  58. return ret;
  59. return 0;
  60. }
  61. EXPORT_SYMBOL(max8998_bulk_read);
  62. int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
  63. {
  64. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  65. int ret;
  66. mutex_lock(&max8998->iolock);
  67. ret = i2c_smbus_write_byte_data(i2c, reg, value);
  68. mutex_unlock(&max8998->iolock);
  69. return ret;
  70. }
  71. EXPORT_SYMBOL(max8998_write_reg);
  72. int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
  73. {
  74. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  75. int ret;
  76. mutex_lock(&max8998->iolock);
  77. ret = i2c_smbus_read_byte_data(i2c, reg);
  78. if (ret >= 0) {
  79. u8 old_val = ret & 0xff;
  80. u8 new_val = (val & mask) | (old_val & (~mask));
  81. ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
  82. if (ret >= 0)
  83. ret = 0;
  84. }
  85. mutex_unlock(&max8998->iolock);
  86. return ret;
  87. }
  88. EXPORT_SYMBOL(max8998_update_reg);
  89. static int max8998_i2c_probe(struct i2c_client *i2c,
  90. const struct i2c_device_id *id)
  91. {
  92. struct max8998_platform_data *pdata = i2c->dev.platform_data;
  93. struct max8998_dev *max8998;
  94. int ret = 0;
  95. max8998 = kzalloc(sizeof(struct max8998_dev), GFP_KERNEL);
  96. if (max8998 == NULL)
  97. return -ENOMEM;
  98. i2c_set_clientdata(i2c, max8998);
  99. max8998->dev = &i2c->dev;
  100. max8998->i2c = i2c;
  101. max8998->irq = i2c->irq;
  102. if (pdata) {
  103. max8998->ono = pdata->ono;
  104. max8998->irq_base = pdata->irq_base;
  105. }
  106. mutex_init(&max8998->iolock);
  107. max8998_irq_init(max8998);
  108. ret = mfd_add_devices(max8998->dev, -1,
  109. max8998_devs, ARRAY_SIZE(max8998_devs),
  110. NULL, 0);
  111. if (ret < 0)
  112. goto err;
  113. return ret;
  114. err:
  115. mfd_remove_devices(max8998->dev);
  116. kfree(max8998);
  117. return ret;
  118. }
  119. static int max8998_i2c_remove(struct i2c_client *i2c)
  120. {
  121. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  122. mfd_remove_devices(max8998->dev);
  123. kfree(max8998);
  124. return 0;
  125. }
  126. static const struct i2c_device_id max8998_i2c_id[] = {
  127. { "max8998", 0 },
  128. { "lp3974", 0 },
  129. { }
  130. };
  131. MODULE_DEVICE_TABLE(i2c, max8998_i2c_id);
  132. static struct i2c_driver max8998_i2c_driver = {
  133. .driver = {
  134. .name = "max8998",
  135. .owner = THIS_MODULE,
  136. },
  137. .probe = max8998_i2c_probe,
  138. .remove = max8998_i2c_remove,
  139. .id_table = max8998_i2c_id,
  140. };
  141. static int __init max8998_i2c_init(void)
  142. {
  143. return i2c_add_driver(&max8998_i2c_driver);
  144. }
  145. /* init early so consumer devices can complete system boot */
  146. subsys_initcall(max8998_i2c_init);
  147. static void __exit max8998_i2c_exit(void)
  148. {
  149. i2c_del_driver(&max8998_i2c_driver);
  150. }
  151. module_exit(max8998_i2c_exit);
  152. MODULE_DESCRIPTION("MAXIM 8998 multi-function core driver");
  153. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  154. MODULE_LICENSE("GPL");