max8998.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * max8698.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. static int max8998_i2c_device_read(struct max8998_dev *max8998, u8 reg, u8 *dest)
  37. {
  38. struct i2c_client *client = max8998->i2c_client;
  39. int ret;
  40. mutex_lock(&max8998->iolock);
  41. ret = i2c_smbus_read_byte_data(client, reg);
  42. mutex_unlock(&max8998->iolock);
  43. if (ret < 0)
  44. return ret;
  45. ret &= 0xff;
  46. *dest = ret;
  47. return 0;
  48. }
  49. static int max8998_i2c_device_write(struct max8998_dev *max8998, u8 reg, u8 value)
  50. {
  51. struct i2c_client *client = max8998->i2c_client;
  52. int ret;
  53. mutex_lock(&max8998->iolock);
  54. ret = i2c_smbus_write_byte_data(client, reg, value);
  55. mutex_unlock(&max8998->iolock);
  56. return ret;
  57. }
  58. static int max8998_i2c_device_update(struct max8998_dev *max8998, u8 reg,
  59. u8 val, u8 mask)
  60. {
  61. struct i2c_client *client = max8998->i2c_client;
  62. int ret;
  63. mutex_lock(&max8998->iolock);
  64. ret = i2c_smbus_read_byte_data(client, reg);
  65. if (ret >= 0) {
  66. u8 old_val = ret & 0xff;
  67. u8 new_val = (val & mask) | (old_val & (~mask));
  68. ret = i2c_smbus_write_byte_data(client, reg, new_val);
  69. if (ret >= 0)
  70. ret = 0;
  71. }
  72. mutex_unlock(&max8998->iolock);
  73. return ret;
  74. }
  75. static int max8998_i2c_probe(struct i2c_client *i2c,
  76. const struct i2c_device_id *id)
  77. {
  78. struct max8998_dev *max8998;
  79. int ret = 0;
  80. max8998 = kzalloc(sizeof(struct max8998_dev), GFP_KERNEL);
  81. if (max8998 == NULL)
  82. return -ENOMEM;
  83. i2c_set_clientdata(i2c, max8998);
  84. max8998->dev = &i2c->dev;
  85. max8998->i2c_client = i2c;
  86. max8998->dev_read = max8998_i2c_device_read;
  87. max8998->dev_write = max8998_i2c_device_write;
  88. max8998->dev_update = max8998_i2c_device_update;
  89. mutex_init(&max8998->iolock);
  90. ret = mfd_add_devices(max8998->dev, -1,
  91. max8998_devs, ARRAY_SIZE(max8998_devs),
  92. NULL, 0);
  93. if (ret < 0)
  94. goto err;
  95. return ret;
  96. err:
  97. mfd_remove_devices(max8998->dev);
  98. kfree(max8998);
  99. return ret;
  100. }
  101. static int max8998_i2c_remove(struct i2c_client *i2c)
  102. {
  103. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  104. mfd_remove_devices(max8998->dev);
  105. kfree(max8998);
  106. return 0;
  107. }
  108. static const struct i2c_device_id max8998_i2c_id[] = {
  109. { "max8998", 0 },
  110. { }
  111. };
  112. MODULE_DEVICE_TABLE(i2c, max8998_i2c_id);
  113. static struct i2c_driver max8998_i2c_driver = {
  114. .driver = {
  115. .name = "max8998",
  116. .owner = THIS_MODULE,
  117. },
  118. .probe = max8998_i2c_probe,
  119. .remove = max8998_i2c_remove,
  120. .id_table = max8998_i2c_id,
  121. };
  122. static int __init max8998_i2c_init(void)
  123. {
  124. return i2c_add_driver(&max8998_i2c_driver);
  125. }
  126. /* init early so consumer devices can complete system boot */
  127. subsys_initcall(max8998_i2c_init);
  128. static void __exit max8998_i2c_exit(void)
  129. {
  130. i2c_del_driver(&max8998_i2c_driver);
  131. }
  132. module_exit(max8998_i2c_exit);
  133. MODULE_DESCRIPTION("MAXIM 8998 multi-function core driver");
  134. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  135. MODULE_LICENSE("GPL");