max8998.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #define RTC_I2C_ADDR (0x0c >> 1)
  32. static struct mfd_cell max8998_devs[] = {
  33. {
  34. .name = "max8998-pmic",
  35. }, {
  36. .name = "max8998-rtc",
  37. },
  38. };
  39. int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
  40. {
  41. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  42. int ret;
  43. mutex_lock(&max8998->iolock);
  44. ret = i2c_smbus_read_byte_data(i2c, reg);
  45. mutex_unlock(&max8998->iolock);
  46. if (ret < 0)
  47. return ret;
  48. ret &= 0xff;
  49. *dest = ret;
  50. return 0;
  51. }
  52. EXPORT_SYMBOL(max8998_read_reg);
  53. int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  54. {
  55. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  56. int ret;
  57. mutex_lock(&max8998->iolock);
  58. ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
  59. mutex_unlock(&max8998->iolock);
  60. if (ret < 0)
  61. return ret;
  62. return 0;
  63. }
  64. EXPORT_SYMBOL(max8998_bulk_read);
  65. int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
  66. {
  67. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  68. int ret;
  69. mutex_lock(&max8998->iolock);
  70. ret = i2c_smbus_write_byte_data(i2c, reg, value);
  71. mutex_unlock(&max8998->iolock);
  72. return ret;
  73. }
  74. EXPORT_SYMBOL(max8998_write_reg);
  75. int max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
  76. {
  77. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  78. int ret;
  79. mutex_lock(&max8998->iolock);
  80. ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
  81. mutex_unlock(&max8998->iolock);
  82. if (ret < 0)
  83. return ret;
  84. return 0;
  85. }
  86. EXPORT_SYMBOL(max8998_bulk_write);
  87. int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
  88. {
  89. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  90. int ret;
  91. mutex_lock(&max8998->iolock);
  92. ret = i2c_smbus_read_byte_data(i2c, reg);
  93. if (ret >= 0) {
  94. u8 old_val = ret & 0xff;
  95. u8 new_val = (val & mask) | (old_val & (~mask));
  96. ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
  97. }
  98. mutex_unlock(&max8998->iolock);
  99. return ret;
  100. }
  101. EXPORT_SYMBOL(max8998_update_reg);
  102. static int max8998_i2c_probe(struct i2c_client *i2c,
  103. const struct i2c_device_id *id)
  104. {
  105. struct max8998_platform_data *pdata = i2c->dev.platform_data;
  106. struct max8998_dev *max8998;
  107. int ret = 0;
  108. max8998 = kzalloc(sizeof(struct max8998_dev), GFP_KERNEL);
  109. if (max8998 == NULL)
  110. return -ENOMEM;
  111. i2c_set_clientdata(i2c, max8998);
  112. max8998->dev = &i2c->dev;
  113. max8998->i2c = i2c;
  114. max8998->irq = i2c->irq;
  115. max8998->type = id->driver_data;
  116. if (pdata) {
  117. max8998->ono = pdata->ono;
  118. max8998->irq_base = pdata->irq_base;
  119. }
  120. mutex_init(&max8998->iolock);
  121. max8998->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
  122. i2c_set_clientdata(max8998->rtc, max8998);
  123. max8998_irq_init(max8998);
  124. ret = mfd_add_devices(max8998->dev, -1,
  125. max8998_devs, ARRAY_SIZE(max8998_devs),
  126. NULL, 0);
  127. if (ret < 0)
  128. goto err;
  129. return ret;
  130. err:
  131. mfd_remove_devices(max8998->dev);
  132. max8998_irq_exit(max8998);
  133. i2c_unregister_device(max8998->rtc);
  134. kfree(max8998);
  135. return ret;
  136. }
  137. static int max8998_i2c_remove(struct i2c_client *i2c)
  138. {
  139. struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
  140. mfd_remove_devices(max8998->dev);
  141. max8998_irq_exit(max8998);
  142. i2c_unregister_device(max8998->rtc);
  143. kfree(max8998);
  144. return 0;
  145. }
  146. static const struct i2c_device_id max8998_i2c_id[] = {
  147. { "max8998", TYPE_MAX8998 },
  148. { "lp3974", TYPE_LP3974},
  149. { }
  150. };
  151. MODULE_DEVICE_TABLE(i2c, max8998_i2c_id);
  152. static struct i2c_driver max8998_i2c_driver = {
  153. .driver = {
  154. .name = "max8998",
  155. .owner = THIS_MODULE,
  156. },
  157. .probe = max8998_i2c_probe,
  158. .remove = max8998_i2c_remove,
  159. .id_table = max8998_i2c_id,
  160. };
  161. static int __init max8998_i2c_init(void)
  162. {
  163. return i2c_add_driver(&max8998_i2c_driver);
  164. }
  165. /* init early so consumer devices can complete system boot */
  166. subsys_initcall(max8998_i2c_init);
  167. static void __exit max8998_i2c_exit(void)
  168. {
  169. i2c_del_driver(&max8998_i2c_driver);
  170. }
  171. module_exit(max8998_i2c_exit);
  172. MODULE_DESCRIPTION("MAXIM 8998 multi-function core driver");
  173. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  174. MODULE_LICENSE("GPL");