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