max77693.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 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 int max77693_i2c_probe(struct i2c_client *i2c,
  91. const struct i2c_device_id *id)
  92. {
  93. struct max77693_dev *max77693;
  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. i2c_set_clientdata(i2c, max77693);
  101. max77693->dev = &i2c->dev;
  102. max77693->i2c = i2c;
  103. max77693->irq = i2c->irq;
  104. max77693->type = id->driver_data;
  105. max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config);
  106. if (IS_ERR(max77693->regmap)) {
  107. ret = PTR_ERR(max77693->regmap);
  108. dev_err(max77693->dev, "failed to allocate register map: %d\n",
  109. ret);
  110. return ret;
  111. }
  112. ret = max77693_read_reg(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
  113. &reg_data);
  114. if (ret < 0) {
  115. dev_err(max77693->dev, "device not found on this channel\n");
  116. return ret;
  117. } else
  118. dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
  119. max77693->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
  120. i2c_set_clientdata(max77693->muic, max77693);
  121. max77693->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
  122. i2c_set_clientdata(max77693->haptic, max77693);
  123. /*
  124. * Initialize register map for MUIC device because use regmap-muic
  125. * instance of MUIC device when irq of max77693 is initialized
  126. * before call max77693-muic probe() function.
  127. */
  128. max77693->regmap_muic = devm_regmap_init_i2c(max77693->muic,
  129. &max77693_regmap_config);
  130. if (IS_ERR(max77693->regmap_muic)) {
  131. ret = PTR_ERR(max77693->regmap_muic);
  132. dev_err(max77693->dev,
  133. "failed to allocate register map: %d\n", ret);
  134. goto err_regmap_muic;
  135. }
  136. ret = max77693_irq_init(max77693);
  137. if (ret < 0)
  138. goto err_irq;
  139. pm_runtime_set_active(max77693->dev);
  140. ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
  141. ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
  142. if (ret < 0)
  143. goto err_mfd;
  144. return ret;
  145. err_mfd:
  146. max77693_irq_exit(max77693);
  147. err_irq:
  148. err_regmap_muic:
  149. i2c_unregister_device(max77693->muic);
  150. i2c_unregister_device(max77693->haptic);
  151. return ret;
  152. }
  153. static int max77693_i2c_remove(struct i2c_client *i2c)
  154. {
  155. struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
  156. mfd_remove_devices(max77693->dev);
  157. max77693_irq_exit(max77693);
  158. i2c_unregister_device(max77693->muic);
  159. i2c_unregister_device(max77693->haptic);
  160. return 0;
  161. }
  162. static const struct i2c_device_id max77693_i2c_id[] = {
  163. { "max77693", TYPE_MAX77693 },
  164. { }
  165. };
  166. MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
  167. static int max77693_suspend(struct device *dev)
  168. {
  169. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  170. struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
  171. if (device_may_wakeup(dev))
  172. irq_set_irq_wake(max77693->irq, 1);
  173. return 0;
  174. }
  175. static int max77693_resume(struct device *dev)
  176. {
  177. struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
  178. struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
  179. if (device_may_wakeup(dev))
  180. irq_set_irq_wake(max77693->irq, 0);
  181. return max77693_irq_resume(max77693);
  182. }
  183. static const struct dev_pm_ops max77693_pm = {
  184. .suspend = max77693_suspend,
  185. .resume = max77693_resume,
  186. };
  187. #ifdef CONFIG_OF
  188. static struct of_device_id max77693_dt_match[] = {
  189. { .compatible = "maxim,max77693" },
  190. {},
  191. };
  192. #endif
  193. static struct i2c_driver max77693_i2c_driver = {
  194. .driver = {
  195. .name = "max77693",
  196. .owner = THIS_MODULE,
  197. .pm = &max77693_pm,
  198. .of_match_table = of_match_ptr(max77693_dt_match),
  199. },
  200. .probe = max77693_i2c_probe,
  201. .remove = max77693_i2c_remove,
  202. .id_table = max77693_i2c_id,
  203. };
  204. static int __init max77693_i2c_init(void)
  205. {
  206. return i2c_add_driver(&max77693_i2c_driver);
  207. }
  208. /* init early so consumer devices can complete system boot */
  209. subsys_initcall(max77693_i2c_init);
  210. static void __exit max77693_i2c_exit(void)
  211. {
  212. i2c_del_driver(&max77693_i2c_driver);
  213. }
  214. module_exit(max77693_i2c_exit);
  215. MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
  216. MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
  217. MODULE_LICENSE("GPL");