max77693.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * max77693.c - mfd core driver for the MAX 77693
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. * SangYoung Son <hello.son@smasung.com>
  6. *
  7. * This program is not provided / owned by Maxim Integrated Products.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * This driver is based on max8997.c
  24. */
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/i2c.h>
  28. #include <linux/err.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/mutex.h>
  32. #include <linux/mfd/core.h>
  33. #include <linux/mfd/max77693.h>
  34. #include <linux/mfd/max77693-private.h>
  35. #include <linux/regulator/machine.h>
  36. #include <linux/regmap.h>
  37. #define I2C_ADDR_PMIC (0xCC >> 1) /* Charger, Flash LED */
  38. #define I2C_ADDR_MUIC (0x4A >> 1)
  39. #define I2C_ADDR_HAPTIC (0x90 >> 1)
  40. static struct mfd_cell max77693_devs[] = {
  41. { .name = "max77693-pmic", },
  42. { .name = "max77693-charger", },
  43. { .name = "max77693-flash", },
  44. { .name = "max77693-muic", },
  45. { .name = "max77693-haptic", },
  46. };
  47. int max77693_read_reg(struct regmap *map, u8 reg, u8 *dest)
  48. {
  49. unsigned int val;
  50. int ret;
  51. ret = regmap_read(map, reg, &val);
  52. *dest = val;
  53. return ret;
  54. }
  55. EXPORT_SYMBOL_GPL(max77693_read_reg);
  56. int max77693_bulk_read(struct regmap *map, u8 reg, int count, u8 *buf)
  57. {
  58. int ret;
  59. ret = regmap_bulk_read(map, reg, buf, count);
  60. return ret;
  61. }
  62. EXPORT_SYMBOL_GPL(max77693_bulk_read);
  63. int max77693_write_reg(struct regmap *map, u8 reg, u8 value)
  64. {
  65. int ret;
  66. ret = regmap_write(map, reg, value);
  67. return ret;
  68. }
  69. EXPORT_SYMBOL_GPL(max77693_write_reg);
  70. int max77693_bulk_write(struct regmap *map, u8 reg, int count, u8 *buf)
  71. {
  72. int ret;
  73. ret = regmap_bulk_write(map, reg, buf, count);
  74. return ret;
  75. }
  76. EXPORT_SYMBOL_GPL(max77693_bulk_write);
  77. int max77693_update_reg(struct regmap *map, u8 reg, u8 val, u8 mask)
  78. {
  79. int ret;
  80. ret = regmap_update_bits(map, reg, mask, val);
  81. return ret;
  82. }
  83. EXPORT_SYMBOL_GPL(max77693_update_reg);
  84. static const struct regmap_config max77693_regmap_config = {
  85. .reg_bits = 8,
  86. .val_bits = 8,
  87. .max_register = MAX77693_PMIC_REG_END,
  88. };
  89. static int max77693_i2c_probe(struct i2c_client *i2c,
  90. const struct i2c_device_id *id)
  91. {
  92. struct max77693_dev *max77693;
  93. struct max77693_platform_data *pdata = i2c->dev.platform_data;
  94. u8 reg_data;
  95. int ret = 0;
  96. max77693 = devm_kzalloc(&i2c->dev,
  97. sizeof(struct max77693_dev), GFP_KERNEL);
  98. if (max77693 == NULL)
  99. return -ENOMEM;
  100. max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config);
  101. if (IS_ERR(max77693->regmap)) {
  102. ret = PTR_ERR(max77693->regmap);
  103. dev_err(max77693->dev,"failed to allocate register map: %d\n",
  104. ret);
  105. goto err_regmap;
  106. }
  107. i2c_set_clientdata(i2c, max77693);
  108. max77693->dev = &i2c->dev;
  109. max77693->i2c = i2c;
  110. max77693->irq = i2c->irq;
  111. max77693->type = id->driver_data;
  112. if (!pdata)
  113. goto err_regmap;
  114. max77693->wakeup = pdata->wakeup;
  115. if (max77693_read_reg(max77693->regmap,
  116. MAX77693_PMIC_REG_PMIC_ID2, &reg_data) < 0) {
  117. dev_err(max77693->dev, "device not found on this channel\n");
  118. ret = -ENODEV;
  119. goto err_regmap;
  120. } else
  121. dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
  122. max77693->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
  123. i2c_set_clientdata(max77693->muic, max77693);
  124. max77693->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
  125. i2c_set_clientdata(max77693->haptic, max77693);
  126. /*
  127. * Initialize register map for MUIC device because use regmap-muic
  128. * instance of MUIC device when irq of max77693 is initialized
  129. * before call max77693-muic probe() function.
  130. */
  131. max77693->regmap_muic = devm_regmap_init_i2c(max77693->muic,
  132. &max77693_regmap_config);
  133. if (IS_ERR(max77693->regmap_muic)) {
  134. ret = PTR_ERR(max77693->regmap_muic);
  135. dev_err(max77693->dev,
  136. "failed to allocate register map: %d\n", ret);
  137. goto err_regmap;
  138. }
  139. ret = max77693_irq_init(max77693);
  140. if (ret < 0)
  141. goto err_irq;
  142. pm_runtime_set_active(max77693->dev);
  143. ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
  144. ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
  145. if (ret < 0)
  146. goto err_mfd;
  147. device_init_wakeup(max77693->dev, pdata->wakeup);
  148. return ret;
  149. err_mfd:
  150. max77693_irq_exit(max77693);
  151. err_irq:
  152. i2c_unregister_device(max77693->muic);
  153. i2c_unregister_device(max77693->haptic);
  154. err_regmap:
  155. return ret;
  156. }
  157. static int max77693_i2c_remove(struct i2c_client *i2c)
  158. {
  159. struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
  160. mfd_remove_devices(max77693->dev);
  161. max77693_irq_exit(max77693);
  162. i2c_unregister_device(max77693->muic);
  163. i2c_unregister_device(max77693->haptic);
  164. return 0;
  165. }
  166. static const struct i2c_device_id max77693_i2c_id[] = {
  167. { "max77693", TYPE_MAX77693 },
  168. { }
  169. };
  170. MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
  171. static int max77693_suspend(struct device *dev)
  172. {
  173. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  174. struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
  175. if (device_may_wakeup(dev))
  176. irq_set_irq_wake(max77693->irq, 1);
  177. return 0;
  178. }
  179. static int max77693_resume(struct device *dev)
  180. {
  181. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  182. struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
  183. if (device_may_wakeup(dev))
  184. irq_set_irq_wake(max77693->irq, 0);
  185. return max77693_irq_resume(max77693);
  186. }
  187. static const struct dev_pm_ops max77693_pm = {
  188. .suspend = max77693_suspend,
  189. .resume = max77693_resume,
  190. };
  191. static struct i2c_driver max77693_i2c_driver = {
  192. .driver = {
  193. .name = "max77693",
  194. .owner = THIS_MODULE,
  195. .pm = &max77693_pm,
  196. },
  197. .probe = max77693_i2c_probe,
  198. .remove = max77693_i2c_remove,
  199. .id_table = max77693_i2c_id,
  200. };
  201. static int __init max77693_i2c_init(void)
  202. {
  203. return i2c_add_driver(&max77693_i2c_driver);
  204. }
  205. /* init early so consumer devices can complete system boot */
  206. subsys_initcall(max77693_i2c_init);
  207. static void __exit max77693_i2c_exit(void)
  208. {
  209. i2c_del_driver(&max77693_i2c_driver);
  210. }
  211. module_exit(max77693_i2c_exit);
  212. MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
  213. MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
  214. MODULE_LICENSE("GPL");