max77693.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/of.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/mutex.h>
  33. #include <linux/mfd/core.h>
  34. #include <linux/mfd/max77693.h>
  35. #include <linux/mfd/max77693-private.h>
  36. #include <linux/regulator/machine.h>
  37. #include <linux/regmap.h>
  38. #define I2C_ADDR_PMIC (0xCC >> 1) /* Charger, Flash LED */
  39. #define I2C_ADDR_MUIC (0x4A >> 1)
  40. #define I2C_ADDR_HAPTIC (0x90 >> 1)
  41. static const struct mfd_cell max77693_devs[] = {
  42. { .name = "max77693-pmic", },
  43. { .name = "max77693-charger", },
  44. { .name = "max77693-flash", },
  45. { .name = "max77693-muic", },
  46. { .name = "max77693-haptic", },
  47. };
  48. int max77693_read_reg(struct regmap *map, u8 reg, u8 *dest)
  49. {
  50. unsigned int val;
  51. int ret;
  52. ret = regmap_read(map, reg, &val);
  53. *dest = val;
  54. return ret;
  55. }
  56. EXPORT_SYMBOL_GPL(max77693_read_reg);
  57. int max77693_bulk_read(struct regmap *map, u8 reg, int count, u8 *buf)
  58. {
  59. int ret;
  60. ret = regmap_bulk_read(map, reg, buf, count);
  61. return ret;
  62. }
  63. EXPORT_SYMBOL_GPL(max77693_bulk_read);
  64. int max77693_write_reg(struct regmap *map, u8 reg, u8 value)
  65. {
  66. int ret;
  67. ret = regmap_write(map, reg, value);
  68. return ret;
  69. }
  70. EXPORT_SYMBOL_GPL(max77693_write_reg);
  71. int max77693_bulk_write(struct regmap *map, u8 reg, int count, u8 *buf)
  72. {
  73. int ret;
  74. ret = regmap_bulk_write(map, reg, buf, count);
  75. return ret;
  76. }
  77. EXPORT_SYMBOL_GPL(max77693_bulk_write);
  78. int max77693_update_reg(struct regmap *map, u8 reg, u8 val, u8 mask)
  79. {
  80. int ret;
  81. ret = regmap_update_bits(map, reg, mask, val);
  82. return ret;
  83. }
  84. EXPORT_SYMBOL_GPL(max77693_update_reg);
  85. static const struct regmap_config max77693_regmap_config = {
  86. .reg_bits = 8,
  87. .val_bits = 8,
  88. .max_register = MAX77693_PMIC_REG_END,
  89. };
  90. static const struct regmap_config max77693_regmap_muic_config = {
  91. .reg_bits = 8,
  92. .val_bits = 8,
  93. .max_register = MAX77693_MUIC_REG_END,
  94. };
  95. static int max77693_i2c_probe(struct i2c_client *i2c,
  96. const struct i2c_device_id *id)
  97. {
  98. struct max77693_dev *max77693;
  99. u8 reg_data;
  100. int ret = 0;
  101. max77693 = devm_kzalloc(&i2c->dev,
  102. sizeof(struct max77693_dev), GFP_KERNEL);
  103. if (max77693 == NULL)
  104. return -ENOMEM;
  105. i2c_set_clientdata(i2c, max77693);
  106. max77693->dev = &i2c->dev;
  107. max77693->i2c = i2c;
  108. max77693->irq = i2c->irq;
  109. max77693->type = id->driver_data;
  110. max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config);
  111. if (IS_ERR(max77693->regmap)) {
  112. ret = PTR_ERR(max77693->regmap);
  113. dev_err(max77693->dev, "failed to allocate register map: %d\n",
  114. ret);
  115. return ret;
  116. }
  117. ret = max77693_read_reg(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
  118. &reg_data);
  119. if (ret < 0) {
  120. dev_err(max77693->dev, "device not found on this channel\n");
  121. return ret;
  122. } else
  123. dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
  124. max77693->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
  125. if (!max77693->muic) {
  126. dev_err(max77693->dev, "Failed to allocate I2C device for MUIC\n");
  127. return -ENODEV;
  128. }
  129. i2c_set_clientdata(max77693->muic, max77693);
  130. max77693->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
  131. if (!max77693->haptic) {
  132. dev_err(max77693->dev, "Failed to allocate I2C device for Haptic\n");
  133. ret = -ENODEV;
  134. goto err_i2c_haptic;
  135. }
  136. i2c_set_clientdata(max77693->haptic, max77693);
  137. /*
  138. * Initialize register map for MUIC device because use regmap-muic
  139. * instance of MUIC device when irq of max77693 is initialized
  140. * before call max77693-muic probe() function.
  141. */
  142. max77693->regmap_muic = devm_regmap_init_i2c(max77693->muic,
  143. &max77693_regmap_muic_config);
  144. if (IS_ERR(max77693->regmap_muic)) {
  145. ret = PTR_ERR(max77693->regmap_muic);
  146. dev_err(max77693->dev,
  147. "failed to allocate register map: %d\n", ret);
  148. goto err_regmap_muic;
  149. }
  150. ret = max77693_irq_init(max77693);
  151. if (ret < 0)
  152. goto err_irq;
  153. pm_runtime_set_active(max77693->dev);
  154. ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
  155. ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
  156. if (ret < 0)
  157. goto err_mfd;
  158. return ret;
  159. err_mfd:
  160. max77693_irq_exit(max77693);
  161. err_irq:
  162. err_regmap_muic:
  163. i2c_unregister_device(max77693->haptic);
  164. err_i2c_haptic:
  165. i2c_unregister_device(max77693->muic);
  166. return ret;
  167. }
  168. static int max77693_i2c_remove(struct i2c_client *i2c)
  169. {
  170. struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
  171. mfd_remove_devices(max77693->dev);
  172. max77693_irq_exit(max77693);
  173. i2c_unregister_device(max77693->muic);
  174. i2c_unregister_device(max77693->haptic);
  175. return 0;
  176. }
  177. static const struct i2c_device_id max77693_i2c_id[] = {
  178. { "max77693", TYPE_MAX77693 },
  179. { }
  180. };
  181. MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
  182. static int max77693_suspend(struct device *dev)
  183. {
  184. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  185. struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
  186. if (device_may_wakeup(dev))
  187. irq_set_irq_wake(max77693->irq, 1);
  188. return 0;
  189. }
  190. static int max77693_resume(struct device *dev)
  191. {
  192. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  193. struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
  194. if (device_may_wakeup(dev))
  195. irq_set_irq_wake(max77693->irq, 0);
  196. return max77693_irq_resume(max77693);
  197. }
  198. static const struct dev_pm_ops max77693_pm = {
  199. .suspend = max77693_suspend,
  200. .resume = max77693_resume,
  201. };
  202. #ifdef CONFIG_OF
  203. static const struct of_device_id max77693_dt_match[] = {
  204. { .compatible = "maxim,max77693" },
  205. {},
  206. };
  207. #endif
  208. static struct i2c_driver max77693_i2c_driver = {
  209. .driver = {
  210. .name = "max77693",
  211. .owner = THIS_MODULE,
  212. .pm = &max77693_pm,
  213. .of_match_table = of_match_ptr(max77693_dt_match),
  214. },
  215. .probe = max77693_i2c_probe,
  216. .remove = max77693_i2c_remove,
  217. .id_table = max77693_i2c_id,
  218. };
  219. static int __init max77693_i2c_init(void)
  220. {
  221. return i2c_add_driver(&max77693_i2c_driver);
  222. }
  223. /* init early so consumer devices can complete system boot */
  224. subsys_initcall(max77693_i2c_init);
  225. static void __exit max77693_i2c_exit(void)
  226. {
  227. i2c_del_driver(&max77693_i2c_driver);
  228. }
  229. module_exit(max77693_i2c_exit);
  230. MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
  231. MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
  232. MODULE_LICENSE("GPL");