s5m-core.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * s5m87xx.c
  3. *
  4. * Copyright (c) 2011 Samsung Electronics Co., Ltd
  5. * http://www.samsung.com
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/i2c.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/mutex.h>
  22. #include <linux/mfd/core.h>
  23. #include <linux/mfd/s5m87xx/s5m-core.h>
  24. #include <linux/mfd/s5m87xx/s5m-pmic.h>
  25. #include <linux/mfd/s5m87xx/s5m-rtc.h>
  26. #include <linux/regmap.h>
  27. static struct mfd_cell s5m8751_devs[] = {
  28. {
  29. .name = "s5m8751-pmic",
  30. }, {
  31. .name = "s5m-charger",
  32. }, {
  33. .name = "s5m8751-codec",
  34. },
  35. };
  36. static struct mfd_cell s5m8763_devs[] = {
  37. {
  38. .name = "s5m8763-pmic",
  39. }, {
  40. .name = "s5m-rtc",
  41. }, {
  42. .name = "s5m-charger",
  43. },
  44. };
  45. static struct mfd_cell s5m8767_devs[] = {
  46. {
  47. .name = "s5m8767-pmic",
  48. }, {
  49. .name = "s5m-rtc",
  50. },
  51. };
  52. int s5m_reg_read(struct s5m87xx_dev *s5m87xx, u8 reg, void *dest)
  53. {
  54. return regmap_read(s5m87xx->regmap, reg, dest);
  55. }
  56. EXPORT_SYMBOL_GPL(s5m_reg_read);
  57. int s5m_bulk_read(struct s5m87xx_dev *s5m87xx, u8 reg, int count, u8 *buf)
  58. {
  59. return regmap_bulk_read(s5m87xx->regmap, reg, buf, count);
  60. }
  61. EXPORT_SYMBOL_GPL(s5m_bulk_read);
  62. int s5m_reg_write(struct s5m87xx_dev *s5m87xx, u8 reg, u8 value)
  63. {
  64. return regmap_write(s5m87xx->regmap, reg, value);
  65. }
  66. EXPORT_SYMBOL_GPL(s5m_reg_write);
  67. int s5m_bulk_write(struct s5m87xx_dev *s5m87xx, u8 reg, int count, u8 *buf)
  68. {
  69. return regmap_raw_write(s5m87xx->regmap, reg, buf, count);
  70. }
  71. EXPORT_SYMBOL_GPL(s5m_bulk_write);
  72. int s5m_reg_update(struct s5m87xx_dev *s5m87xx, u8 reg, u8 val, u8 mask)
  73. {
  74. return regmap_update_bits(s5m87xx->regmap, reg, mask, val);
  75. }
  76. EXPORT_SYMBOL_GPL(s5m_reg_update);
  77. static struct regmap_config s5m_regmap_config = {
  78. .reg_bits = 8,
  79. .val_bits = 8,
  80. };
  81. static int s5m87xx_i2c_probe(struct i2c_client *i2c,
  82. const struct i2c_device_id *id)
  83. {
  84. struct s5m_platform_data *pdata = i2c->dev.platform_data;
  85. struct s5m87xx_dev *s5m87xx;
  86. int ret;
  87. s5m87xx = devm_kzalloc(&i2c->dev, sizeof(struct s5m87xx_dev),
  88. GFP_KERNEL);
  89. if (s5m87xx == NULL)
  90. return -ENOMEM;
  91. i2c_set_clientdata(i2c, s5m87xx);
  92. s5m87xx->dev = &i2c->dev;
  93. s5m87xx->i2c = i2c;
  94. s5m87xx->irq = i2c->irq;
  95. s5m87xx->type = id->driver_data;
  96. if (pdata) {
  97. s5m87xx->device_type = pdata->device_type;
  98. s5m87xx->ono = pdata->ono;
  99. s5m87xx->irq_base = pdata->irq_base;
  100. s5m87xx->wakeup = pdata->wakeup;
  101. }
  102. s5m87xx->regmap = regmap_init_i2c(i2c, &s5m_regmap_config);
  103. if (IS_ERR(s5m87xx->regmap)) {
  104. ret = PTR_ERR(s5m87xx->regmap);
  105. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  106. ret);
  107. goto err;
  108. }
  109. s5m87xx->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
  110. i2c_set_clientdata(s5m87xx->rtc, s5m87xx);
  111. if (pdata && pdata->cfg_pmic_irq)
  112. pdata->cfg_pmic_irq();
  113. s5m_irq_init(s5m87xx);
  114. pm_runtime_set_active(s5m87xx->dev);
  115. switch (s5m87xx->device_type) {
  116. case S5M8751X:
  117. ret = mfd_add_devices(s5m87xx->dev, -1, s5m8751_devs,
  118. ARRAY_SIZE(s5m8751_devs), NULL, 0);
  119. break;
  120. case S5M8763X:
  121. ret = mfd_add_devices(s5m87xx->dev, -1, s5m8763_devs,
  122. ARRAY_SIZE(s5m8763_devs), NULL, 0);
  123. break;
  124. case S5M8767X:
  125. ret = mfd_add_devices(s5m87xx->dev, -1, s5m8767_devs,
  126. ARRAY_SIZE(s5m8767_devs), NULL, 0);
  127. break;
  128. default:
  129. /* If this happens the probe function is problem */
  130. BUG();
  131. }
  132. if (ret < 0)
  133. goto err;
  134. return ret;
  135. err:
  136. mfd_remove_devices(s5m87xx->dev);
  137. s5m_irq_exit(s5m87xx);
  138. i2c_unregister_device(s5m87xx->rtc);
  139. regmap_exit(s5m87xx->regmap);
  140. return ret;
  141. }
  142. static int s5m87xx_i2c_remove(struct i2c_client *i2c)
  143. {
  144. struct s5m87xx_dev *s5m87xx = i2c_get_clientdata(i2c);
  145. mfd_remove_devices(s5m87xx->dev);
  146. s5m_irq_exit(s5m87xx);
  147. i2c_unregister_device(s5m87xx->rtc);
  148. regmap_exit(s5m87xx->regmap);
  149. return 0;
  150. }
  151. static const struct i2c_device_id s5m87xx_i2c_id[] = {
  152. { "s5m87xx", 0 },
  153. { }
  154. };
  155. MODULE_DEVICE_TABLE(i2c, s5m87xx_i2c_id);
  156. static struct i2c_driver s5m87xx_i2c_driver = {
  157. .driver = {
  158. .name = "s5m87xx",
  159. .owner = THIS_MODULE,
  160. },
  161. .probe = s5m87xx_i2c_probe,
  162. .remove = s5m87xx_i2c_remove,
  163. .id_table = s5m87xx_i2c_id,
  164. };
  165. static int __init s5m87xx_i2c_init(void)
  166. {
  167. return i2c_add_driver(&s5m87xx_i2c_driver);
  168. }
  169. subsys_initcall(s5m87xx_i2c_init);
  170. static void __exit s5m87xx_i2c_exit(void)
  171. {
  172. i2c_del_driver(&s5m87xx_i2c_driver);
  173. }
  174. module_exit(s5m87xx_i2c_exit);
  175. MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
  176. MODULE_DESCRIPTION("Core support for the S5M MFD");
  177. MODULE_LICENSE("GPL");