max77693.c 11 KB

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