max77693.c 11 KB

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