max77693.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // max77693.c - mfd core driver for the MAX 77693
  4. //
  5. // Copyright (C) 2012 Samsung Electronics
  6. // SangYoung Son <[email protected]>
  7. //
  8. // This program is not provided / owned by Maxim Integrated Products.
  9. //
  10. // This driver is based on max8997.c
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/i2c.h>
  14. #include <linux/err.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/of.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/mutex.h>
  19. #include <linux/mfd/core.h>
  20. #include <linux/mfd/max77693.h>
  21. #include <linux/mfd/max77693-common.h>
  22. #include <linux/mfd/max77693-private.h>
  23. #include <linux/regulator/machine.h>
  24. #include <linux/regmap.h>
  25. #define I2C_ADDR_PMIC (0xCC >> 1) /* Charger, Flash LED */
  26. #define I2C_ADDR_MUIC (0x4A >> 1)
  27. #define I2C_ADDR_HAPTIC (0x90 >> 1)
  28. static const struct mfd_cell max77693_devs[] = {
  29. { .name = "max77693-pmic", },
  30. {
  31. .name = "max77693-charger",
  32. .of_compatible = "maxim,max77693-charger",
  33. },
  34. {
  35. .name = "max77693-muic",
  36. .of_compatible = "maxim,max77693-muic",
  37. },
  38. {
  39. .name = "max77693-haptic",
  40. .of_compatible = "maxim,max77693-haptic",
  41. },
  42. {
  43. .name = "max77693-led",
  44. .of_compatible = "maxim,max77693-led",
  45. },
  46. };
  47. static const struct regmap_config max77693_regmap_config = {
  48. .reg_bits = 8,
  49. .val_bits = 8,
  50. .max_register = MAX77693_PMIC_REG_END,
  51. };
  52. static const struct regmap_irq max77693_led_irqs[] = {
  53. { .mask = LED_IRQ_FLED2_OPEN, },
  54. { .mask = LED_IRQ_FLED2_SHORT, },
  55. { .mask = LED_IRQ_FLED1_OPEN, },
  56. { .mask = LED_IRQ_FLED1_SHORT, },
  57. { .mask = LED_IRQ_MAX_FLASH, },
  58. };
  59. static const struct regmap_irq_chip max77693_led_irq_chip = {
  60. .name = "max77693-led",
  61. .status_base = MAX77693_LED_REG_FLASH_INT,
  62. .mask_base = MAX77693_LED_REG_FLASH_INT_MASK,
  63. .mask_invert = false,
  64. .num_regs = 1,
  65. .irqs = max77693_led_irqs,
  66. .num_irqs = ARRAY_SIZE(max77693_led_irqs),
  67. };
  68. static const struct regmap_irq max77693_topsys_irqs[] = {
  69. { .mask = TOPSYS_IRQ_T120C_INT, },
  70. { .mask = TOPSYS_IRQ_T140C_INT, },
  71. { .mask = TOPSYS_IRQ_LOWSYS_INT, },
  72. };
  73. static const struct regmap_irq_chip max77693_topsys_irq_chip = {
  74. .name = "max77693-topsys",
  75. .status_base = MAX77693_PMIC_REG_TOPSYS_INT,
  76. .mask_base = MAX77693_PMIC_REG_TOPSYS_INT_MASK,
  77. .mask_invert = false,
  78. .num_regs = 1,
  79. .irqs = max77693_topsys_irqs,
  80. .num_irqs = ARRAY_SIZE(max77693_topsys_irqs),
  81. };
  82. static const struct regmap_irq max77693_charger_irqs[] = {
  83. { .mask = CHG_IRQ_BYP_I, },
  84. { .mask = CHG_IRQ_THM_I, },
  85. { .mask = CHG_IRQ_BAT_I, },
  86. { .mask = CHG_IRQ_CHG_I, },
  87. { .mask = CHG_IRQ_CHGIN_I, },
  88. };
  89. static const struct regmap_irq_chip max77693_charger_irq_chip = {
  90. .name = "max77693-charger",
  91. .status_base = MAX77693_CHG_REG_CHG_INT,
  92. .mask_base = MAX77693_CHG_REG_CHG_INT_MASK,
  93. .mask_invert = false,
  94. .num_regs = 1,
  95. .irqs = max77693_charger_irqs,
  96. .num_irqs = ARRAY_SIZE(max77693_charger_irqs),
  97. };
  98. static const struct regmap_config max77693_regmap_muic_config = {
  99. .reg_bits = 8,
  100. .val_bits = 8,
  101. .max_register = MAX77693_MUIC_REG_END,
  102. };
  103. static const struct regmap_irq max77693_muic_irqs[] = {
  104. { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC, },
  105. { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_LOW, },
  106. { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_ERR, },
  107. { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC1K, },
  108. { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGTYP, },
  109. { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGDETREUN, },
  110. { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DCDTMR, },
  111. { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DXOVP, },
  112. { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VBVOLT, },
  113. { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VIDRM, },
  114. { .reg_offset = 2, .mask = MUIC_IRQ_INT3_EOC, },
  115. { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CGMBC, },
  116. { .reg_offset = 2, .mask = MUIC_IRQ_INT3_OVP, },
  117. { .reg_offset = 2, .mask = MUIC_IRQ_INT3_MBCCHG_ERR, },
  118. { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CHG_ENABLED, },
  119. { .reg_offset = 2, .mask = MUIC_IRQ_INT3_BAT_DET, },
  120. };
  121. static const struct regmap_irq_chip max77693_muic_irq_chip = {
  122. .name = "max77693-muic",
  123. .status_base = MAX77693_MUIC_REG_INT1,
  124. .mask_base = MAX77693_MUIC_REG_INTMASK1,
  125. .mask_invert = true,
  126. .num_regs = 3,
  127. .irqs = max77693_muic_irqs,
  128. .num_irqs = ARRAY_SIZE(max77693_muic_irqs),
  129. };
  130. static const struct regmap_config max77693_regmap_haptic_config = {
  131. .reg_bits = 8,
  132. .val_bits = 8,
  133. .max_register = MAX77693_HAPTIC_REG_END,
  134. };
  135. static int max77693_i2c_probe(struct i2c_client *i2c,
  136. const struct i2c_device_id *id)
  137. {
  138. struct max77693_dev *max77693;
  139. unsigned int reg_data;
  140. int ret = 0;
  141. max77693 = devm_kzalloc(&i2c->dev,
  142. sizeof(struct max77693_dev), GFP_KERNEL);
  143. if (max77693 == NULL)
  144. return -ENOMEM;
  145. i2c_set_clientdata(i2c, max77693);
  146. max77693->dev = &i2c->dev;
  147. max77693->i2c = i2c;
  148. max77693->irq = i2c->irq;
  149. max77693->type = id->driver_data;
  150. max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config);
  151. if (IS_ERR(max77693->regmap)) {
  152. ret = PTR_ERR(max77693->regmap);
  153. dev_err(max77693->dev, "failed to allocate register map: %d\n",
  154. ret);
  155. return ret;
  156. }
  157. ret = regmap_read(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
  158. &reg_data);
  159. if (ret < 0) {
  160. dev_err(max77693->dev, "device not found on this channel\n");
  161. return ret;
  162. } else
  163. dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
  164. max77693->i2c_muic = i2c_new_dummy_device(i2c->adapter, I2C_ADDR_MUIC);
  165. if (IS_ERR(max77693->i2c_muic)) {
  166. dev_err(max77693->dev, "Failed to allocate I2C device for MUIC\n");
  167. return PTR_ERR(max77693->i2c_muic);
  168. }
  169. i2c_set_clientdata(max77693->i2c_muic, max77693);
  170. max77693->i2c_haptic = i2c_new_dummy_device(i2c->adapter, I2C_ADDR_HAPTIC);
  171. if (IS_ERR(max77693->i2c_haptic)) {
  172. dev_err(max77693->dev, "Failed to allocate I2C device for Haptic\n");
  173. ret = PTR_ERR(max77693->i2c_haptic);
  174. goto err_i2c_haptic;
  175. }
  176. i2c_set_clientdata(max77693->i2c_haptic, max77693);
  177. max77693->regmap_haptic = devm_regmap_init_i2c(max77693->i2c_haptic,
  178. &max77693_regmap_haptic_config);
  179. if (IS_ERR(max77693->regmap_haptic)) {
  180. ret = PTR_ERR(max77693->regmap_haptic);
  181. dev_err(max77693->dev,
  182. "failed to initialize haptic register map: %d\n", ret);
  183. goto err_regmap;
  184. }
  185. /*
  186. * Initialize register map for MUIC device because use regmap-muic
  187. * instance of MUIC device when irq of max77693 is initialized
  188. * before call max77693-muic probe() function.
  189. */
  190. max77693->regmap_muic = devm_regmap_init_i2c(max77693->i2c_muic,
  191. &max77693_regmap_muic_config);
  192. if (IS_ERR(max77693->regmap_muic)) {
  193. ret = PTR_ERR(max77693->regmap_muic);
  194. dev_err(max77693->dev,
  195. "failed to allocate register map: %d\n", ret);
  196. goto err_regmap;
  197. }
  198. ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
  199. IRQF_ONESHOT | IRQF_SHARED, 0,
  200. &max77693_led_irq_chip,
  201. &max77693->irq_data_led);
  202. if (ret) {
  203. dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
  204. goto err_regmap;
  205. }
  206. ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
  207. IRQF_ONESHOT | IRQF_SHARED, 0,
  208. &max77693_topsys_irq_chip,
  209. &max77693->irq_data_topsys);
  210. if (ret) {
  211. dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
  212. goto err_irq_topsys;
  213. }
  214. ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
  215. IRQF_ONESHOT | IRQF_SHARED, 0,
  216. &max77693_charger_irq_chip,
  217. &max77693->irq_data_chg);
  218. if (ret) {
  219. dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
  220. goto err_irq_charger;
  221. }
  222. ret = regmap_add_irq_chip(max77693->regmap_muic, max77693->irq,
  223. IRQF_ONESHOT | IRQF_SHARED, 0,
  224. &max77693_muic_irq_chip,
  225. &max77693->irq_data_muic);
  226. if (ret) {
  227. dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
  228. goto err_irq_muic;
  229. }
  230. /* Unmask interrupts from all blocks in interrupt source register */
  231. ret = regmap_update_bits(max77693->regmap,
  232. MAX77693_PMIC_REG_INTSRC_MASK,
  233. SRC_IRQ_ALL, (unsigned int)~SRC_IRQ_ALL);
  234. if (ret < 0) {
  235. dev_err(max77693->dev,
  236. "Could not unmask interrupts in INTSRC: %d\n",
  237. ret);
  238. goto err_intsrc;
  239. }
  240. pm_runtime_set_active(max77693->dev);
  241. ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
  242. ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
  243. if (ret < 0)
  244. goto err_mfd;
  245. return ret;
  246. err_mfd:
  247. mfd_remove_devices(max77693->dev);
  248. err_intsrc:
  249. regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
  250. err_irq_muic:
  251. regmap_del_irq_chip(max77693->irq, max77693->irq_data_chg);
  252. err_irq_charger:
  253. regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
  254. err_irq_topsys:
  255. regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
  256. err_regmap:
  257. i2c_unregister_device(max77693->i2c_haptic);
  258. err_i2c_haptic:
  259. i2c_unregister_device(max77693->i2c_muic);
  260. return ret;
  261. }
  262. static void max77693_i2c_remove(struct i2c_client *i2c)
  263. {
  264. struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
  265. mfd_remove_devices(max77693->dev);
  266. regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
  267. regmap_del_irq_chip(max77693->irq, max77693->irq_data_chg);
  268. regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
  269. regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
  270. i2c_unregister_device(max77693->i2c_muic);
  271. i2c_unregister_device(max77693->i2c_haptic);
  272. }
  273. static const struct i2c_device_id max77693_i2c_id[] = {
  274. { "max77693", TYPE_MAX77693 },
  275. { }
  276. };
  277. MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
  278. static int max77693_suspend(struct device *dev)
  279. {
  280. struct i2c_client *i2c = to_i2c_client(dev);
  281. struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
  282. if (device_may_wakeup(dev)) {
  283. enable_irq_wake(max77693->irq);
  284. disable_irq(max77693->irq);
  285. }
  286. return 0;
  287. }
  288. static int max77693_resume(struct device *dev)
  289. {
  290. struct i2c_client *i2c = to_i2c_client(dev);
  291. struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
  292. if (device_may_wakeup(dev)) {
  293. disable_irq_wake(max77693->irq);
  294. enable_irq(max77693->irq);
  295. }
  296. return 0;
  297. }
  298. static const struct dev_pm_ops max77693_pm = {
  299. .suspend = max77693_suspend,
  300. .resume = max77693_resume,
  301. };
  302. #ifdef CONFIG_OF
  303. static const struct of_device_id max77693_dt_match[] = {
  304. { .compatible = "maxim,max77693" },
  305. {},
  306. };
  307. MODULE_DEVICE_TABLE(of, max77693_dt_match);
  308. #endif
  309. static struct i2c_driver max77693_i2c_driver = {
  310. .driver = {
  311. .name = "max77693",
  312. .pm = &max77693_pm,
  313. .of_match_table = of_match_ptr(max77693_dt_match),
  314. },
  315. .probe = max77693_i2c_probe,
  316. .remove = max77693_i2c_remove,
  317. .id_table = max77693_i2c_id,
  318. };
  319. module_i2c_driver(max77693_i2c_driver);
  320. MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
  321. MODULE_AUTHOR("SangYoung, Son <[email protected]>");
  322. MODULE_LICENSE("GPL");