rt5033.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * MFD core driver for the Richtek RT5033.
  3. *
  4. * RT5033 comprises multiple sub-devices switcing charger, fuel gauge,
  5. * flash LED, current source, LDO and BUCK regulators.
  6. *
  7. * Copyright (C) 2014 Samsung Electronics, Co., Ltd.
  8. * Author: Beomho Seo <beomho.seo@samsung.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published bythe Free Software Foundation.
  13. */
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/of_device.h>
  18. #include <linux/mfd/core.h>
  19. #include <linux/mfd/rt5033.h>
  20. #include <linux/mfd/rt5033-private.h>
  21. static const struct regmap_irq rt5033_irqs[] = {
  22. { .mask = RT5033_PMIC_IRQ_BUCKOCP, },
  23. { .mask = RT5033_PMIC_IRQ_BUCKLV, },
  24. { .mask = RT5033_PMIC_IRQ_SAFELDOLV, },
  25. { .mask = RT5033_PMIC_IRQ_LDOLV, },
  26. { .mask = RT5033_PMIC_IRQ_OT, },
  27. { .mask = RT5033_PMIC_IRQ_VDDA_UV, },
  28. };
  29. static const struct regmap_irq_chip rt5033_irq_chip = {
  30. .name = "rt5033",
  31. .status_base = RT5033_REG_PMIC_IRQ_STAT,
  32. .mask_base = RT5033_REG_PMIC_IRQ_CTRL,
  33. .mask_invert = true,
  34. .num_regs = 1,
  35. .irqs = rt5033_irqs,
  36. .num_irqs = ARRAY_SIZE(rt5033_irqs),
  37. };
  38. static const struct mfd_cell rt5033_devs[] = {
  39. { .name = "rt5033-regulator", },
  40. {
  41. .name = "rt5033-charger",
  42. .of_compatible = "richtek,rt5033-charger",
  43. }, {
  44. .name = "rt5033-battery",
  45. .of_compatible = "richtek,rt5033-battery",
  46. },
  47. };
  48. static const struct regmap_config rt5033_regmap_config = {
  49. .reg_bits = 8,
  50. .val_bits = 8,
  51. .max_register = RT5033_REG_END,
  52. };
  53. static int rt5033_i2c_probe(struct i2c_client *i2c,
  54. const struct i2c_device_id *id)
  55. {
  56. struct rt5033_dev *rt5033;
  57. unsigned int dev_id;
  58. int ret;
  59. rt5033 = devm_kzalloc(&i2c->dev, sizeof(*rt5033), GFP_KERNEL);
  60. if (!rt5033)
  61. return -ENOMEM;
  62. i2c_set_clientdata(i2c, rt5033);
  63. rt5033->dev = &i2c->dev;
  64. rt5033->irq = i2c->irq;
  65. rt5033->wakeup = true;
  66. rt5033->regmap = devm_regmap_init_i2c(i2c, &rt5033_regmap_config);
  67. if (IS_ERR(rt5033->regmap)) {
  68. dev_err(&i2c->dev, "Failed to allocate register map.\n");
  69. return PTR_ERR(rt5033->regmap);
  70. }
  71. ret = regmap_read(rt5033->regmap, RT5033_REG_DEVICE_ID, &dev_id);
  72. if (ret) {
  73. dev_err(&i2c->dev, "Device not found\n");
  74. return -ENODEV;
  75. }
  76. dev_info(&i2c->dev, "Device found Device ID: %04x\n", dev_id);
  77. ret = regmap_add_irq_chip(rt5033->regmap, rt5033->irq,
  78. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  79. 0, &rt5033_irq_chip, &rt5033->irq_data);
  80. if (ret) {
  81. dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n",
  82. rt5033->irq, ret);
  83. return ret;
  84. }
  85. ret = mfd_add_devices(rt5033->dev, -1, rt5033_devs,
  86. ARRAY_SIZE(rt5033_devs), NULL, 0,
  87. regmap_irq_get_domain(rt5033->irq_data));
  88. if (ret < 0) {
  89. dev_err(&i2c->dev, "Failed to add RT5033 child devices.\n");
  90. return ret;
  91. }
  92. device_init_wakeup(rt5033->dev, rt5033->wakeup);
  93. return 0;
  94. }
  95. static int rt5033_i2c_remove(struct i2c_client *i2c)
  96. {
  97. mfd_remove_devices(&i2c->dev);
  98. return 0;
  99. }
  100. static const struct i2c_device_id rt5033_i2c_id[] = {
  101. { "rt5033", },
  102. { }
  103. };
  104. MODULE_DEVICE_TABLE(i2c, rt5033_i2c_id);
  105. static const struct of_device_id rt5033_dt_match[] = {
  106. { .compatible = "richtek,rt5033", },
  107. { }
  108. };
  109. MODULE_DEVICE_TABLE(of, rt5033_dt_match);
  110. static struct i2c_driver rt5033_driver = {
  111. .driver = {
  112. .name = "rt5033",
  113. .of_match_table = of_match_ptr(rt5033_dt_match),
  114. },
  115. .probe = rt5033_i2c_probe,
  116. .remove = rt5033_i2c_remove,
  117. .id_table = rt5033_i2c_id,
  118. };
  119. module_i2c_driver(rt5033_driver);
  120. MODULE_ALIAS("i2c:rt5033");
  121. MODULE_DESCRIPTION("Richtek RT5033 multi-function core driver");
  122. MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
  123. MODULE_LICENSE("GPL");