max77693.c 11 KB

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