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