max77693.c 6.6 KB

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