max77843.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * MFD core driver for the Maxim MAX77843
  3. *
  4. * Copyright (C) 2015 Samsung Electronics
  5. * Author: Jaewon Kim <jaewon02.kim@samsung.com>
  6. * Author: Beomho Seo <beomho.seo@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/i2c.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/mfd/max77693-common.h>
  19. #include <linux/mfd/max77843-private.h>
  20. #include <linux/of_device.h>
  21. #include <linux/platform_device.h>
  22. static const struct mfd_cell max77843_devs[] = {
  23. {
  24. .name = "max77843-muic",
  25. .of_compatible = "maxim,max77843-muic",
  26. }, {
  27. .name = "max77843-regulator",
  28. .of_compatible = "maxim,max77843-regulator",
  29. }, {
  30. .name = "max77843-charger",
  31. .of_compatible = "maxim,max77843-charger"
  32. }, {
  33. .name = "max77843-fuelgauge",
  34. .of_compatible = "maxim,max77843-fuelgauge",
  35. }, {
  36. .name = "max77843-haptic",
  37. .of_compatible = "maxim,max77843-haptic",
  38. },
  39. };
  40. static const struct regmap_config max77843_charger_regmap_config = {
  41. .reg_bits = 8,
  42. .val_bits = 8,
  43. .max_register = MAX77843_CHG_REG_END,
  44. };
  45. static const struct regmap_config max77843_regmap_config = {
  46. .reg_bits = 8,
  47. .val_bits = 8,
  48. .max_register = MAX77843_SYS_REG_END,
  49. };
  50. static const struct regmap_irq max77843_irqs[] = {
  51. /* TOPSYS interrupts */
  52. { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_SYSUVLO_INT, },
  53. { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_SYSOVLO_INT, },
  54. { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_TSHDN_INT, },
  55. { .reg_offset = 0, .mask = MAX77843_SYS_IRQ_TM_INT, },
  56. };
  57. static const struct regmap_irq_chip max77843_irq_chip = {
  58. .name = "max77843",
  59. .status_base = MAX77843_SYS_REG_SYSINTSRC,
  60. .mask_base = MAX77843_SYS_REG_SYSINTMASK,
  61. .mask_invert = false,
  62. .num_regs = 1,
  63. .irqs = max77843_irqs,
  64. .num_irqs = ARRAY_SIZE(max77843_irqs),
  65. };
  66. /* Charger and Charger regulator use same regmap. */
  67. static int max77843_chg_init(struct max77693_dev *max77843)
  68. {
  69. int ret;
  70. max77843->i2c_chg = i2c_new_dummy(max77843->i2c->adapter, I2C_ADDR_CHG);
  71. if (!max77843->i2c_chg) {
  72. dev_err(&max77843->i2c->dev,
  73. "Cannot allocate I2C device for Charger\n");
  74. return -ENODEV;
  75. }
  76. i2c_set_clientdata(max77843->i2c_chg, max77843);
  77. max77843->regmap_chg = devm_regmap_init_i2c(max77843->i2c_chg,
  78. &max77843_charger_regmap_config);
  79. if (IS_ERR(max77843->regmap_chg)) {
  80. ret = PTR_ERR(max77843->regmap_chg);
  81. goto err_chg_i2c;
  82. }
  83. return 0;
  84. err_chg_i2c:
  85. i2c_unregister_device(max77843->i2c_chg);
  86. return ret;
  87. }
  88. static int max77843_probe(struct i2c_client *i2c,
  89. const struct i2c_device_id *id)
  90. {
  91. struct max77693_dev *max77843;
  92. unsigned int reg_data;
  93. int ret;
  94. max77843 = devm_kzalloc(&i2c->dev, sizeof(*max77843), GFP_KERNEL);
  95. if (!max77843)
  96. return -ENOMEM;
  97. i2c_set_clientdata(i2c, max77843);
  98. max77843->dev = &i2c->dev;
  99. max77843->i2c = i2c;
  100. max77843->irq = i2c->irq;
  101. max77843->type = id->driver_data;
  102. max77843->regmap = devm_regmap_init_i2c(i2c,
  103. &max77843_regmap_config);
  104. if (IS_ERR(max77843->regmap)) {
  105. dev_err(&i2c->dev, "Failed to allocate topsys register map\n");
  106. return PTR_ERR(max77843->regmap);
  107. }
  108. ret = regmap_add_irq_chip(max77843->regmap, max77843->irq,
  109. IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
  110. 0, &max77843_irq_chip, &max77843->irq_data_topsys);
  111. if (ret) {
  112. dev_err(&i2c->dev, "Failed to add TOPSYS IRQ chip\n");
  113. return ret;
  114. }
  115. ret = regmap_read(max77843->regmap,
  116. MAX77843_SYS_REG_PMICID, &reg_data);
  117. if (ret < 0) {
  118. dev_err(&i2c->dev, "Failed to read PMIC ID\n");
  119. goto err_pmic_id;
  120. }
  121. dev_info(&i2c->dev, "device ID: 0x%x\n", reg_data);
  122. ret = max77843_chg_init(max77843);
  123. if (ret) {
  124. dev_err(&i2c->dev, "Failed to init Charger\n");
  125. goto err_pmic_id;
  126. }
  127. ret = regmap_update_bits(max77843->regmap,
  128. MAX77843_SYS_REG_INTSRCMASK,
  129. MAX77843_INTSRC_MASK_MASK,
  130. (unsigned int)~MAX77843_INTSRC_MASK_MASK);
  131. if (ret < 0) {
  132. dev_err(&i2c->dev, "Failed to unmask interrupt source\n");
  133. goto err_pmic_id;
  134. }
  135. ret = mfd_add_devices(max77843->dev, -1, max77843_devs,
  136. ARRAY_SIZE(max77843_devs), NULL, 0, NULL);
  137. if (ret < 0) {
  138. dev_err(&i2c->dev, "Failed to add mfd device\n");
  139. goto err_pmic_id;
  140. }
  141. device_init_wakeup(max77843->dev, true);
  142. return 0;
  143. err_pmic_id:
  144. regmap_del_irq_chip(max77843->irq, max77843->irq_data_topsys);
  145. return ret;
  146. }
  147. static const struct of_device_id max77843_dt_match[] = {
  148. { .compatible = "maxim,max77843", },
  149. { },
  150. };
  151. static const struct i2c_device_id max77843_id[] = {
  152. { "max77843", TYPE_MAX77843, },
  153. { },
  154. };
  155. static int __maybe_unused max77843_suspend(struct device *dev)
  156. {
  157. struct i2c_client *i2c = to_i2c_client(dev);
  158. struct max77693_dev *max77843 = i2c_get_clientdata(i2c);
  159. disable_irq(max77843->irq);
  160. if (device_may_wakeup(dev))
  161. enable_irq_wake(max77843->irq);
  162. return 0;
  163. }
  164. static int __maybe_unused max77843_resume(struct device *dev)
  165. {
  166. struct i2c_client *i2c = to_i2c_client(dev);
  167. struct max77693_dev *max77843 = i2c_get_clientdata(i2c);
  168. if (device_may_wakeup(dev))
  169. disable_irq_wake(max77843->irq);
  170. enable_irq(max77843->irq);
  171. return 0;
  172. }
  173. static SIMPLE_DEV_PM_OPS(max77843_pm, max77843_suspend, max77843_resume);
  174. static struct i2c_driver max77843_i2c_driver = {
  175. .driver = {
  176. .name = "max77843",
  177. .pm = &max77843_pm,
  178. .of_match_table = max77843_dt_match,
  179. .suppress_bind_attrs = true,
  180. },
  181. .probe = max77843_probe,
  182. .id_table = max77843_id,
  183. };
  184. static int __init max77843_i2c_init(void)
  185. {
  186. return i2c_add_driver(&max77843_i2c_driver);
  187. }
  188. subsys_initcall(max77843_i2c_init);