rn5t618.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * MFD core driver for Ricoh RN5T618 PMIC
  3. *
  4. * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
  5. * Copyright (C) 2016 Toradex AG
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/i2c.h>
  16. #include <linux/mfd/core.h>
  17. #include <linux/mfd/rn5t618.h>
  18. #include <linux/module.h>
  19. #include <linux/of_device.h>
  20. #include <linux/reboot.h>
  21. #include <linux/regmap.h>
  22. static const struct mfd_cell rn5t618_cells[] = {
  23. { .name = "rn5t618-regulator" },
  24. { .name = "rn5t618-wdt" },
  25. };
  26. static bool rn5t618_volatile_reg(struct device *dev, unsigned int reg)
  27. {
  28. switch (reg) {
  29. case RN5T618_WATCHDOGCNT:
  30. case RN5T618_DCIRQ:
  31. case RN5T618_ILIMDATAH ... RN5T618_AIN0DATAL:
  32. case RN5T618_ADCCNT3:
  33. case RN5T618_IR_ADC1 ... RN5T618_IR_ADC3:
  34. case RN5T618_IR_GPR:
  35. case RN5T618_IR_GPF:
  36. case RN5T618_MON_IOIN:
  37. case RN5T618_INTMON:
  38. return true;
  39. default:
  40. return false;
  41. }
  42. }
  43. static const struct regmap_config rn5t618_regmap_config = {
  44. .reg_bits = 8,
  45. .val_bits = 8,
  46. .volatile_reg = rn5t618_volatile_reg,
  47. .max_register = RN5T618_MAX_REG,
  48. .cache_type = REGCACHE_RBTREE,
  49. };
  50. static struct rn5t618 *rn5t618_pm_power_off;
  51. static struct notifier_block rn5t618_restart_handler;
  52. static void rn5t618_trigger_poweroff_sequence(bool repower)
  53. {
  54. /* disable automatic repower-on */
  55. regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_REPCNT,
  56. RN5T618_REPCNT_REPWRON,
  57. repower ? RN5T618_REPCNT_REPWRON : 0);
  58. /* start power-off sequence */
  59. regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_SLPCNT,
  60. RN5T618_SLPCNT_SWPWROFF, RN5T618_SLPCNT_SWPWROFF);
  61. }
  62. static void rn5t618_power_off(void)
  63. {
  64. rn5t618_trigger_poweroff_sequence(false);
  65. }
  66. static int rn5t618_restart(struct notifier_block *this,
  67. unsigned long mode, void *cmd)
  68. {
  69. rn5t618_trigger_poweroff_sequence(true);
  70. /*
  71. * Re-power factor detection on PMIC side is not instant. 1ms
  72. * proved to be enough time until reset takes effect.
  73. */
  74. mdelay(1);
  75. return NOTIFY_DONE;
  76. }
  77. static const struct of_device_id rn5t618_of_match[] = {
  78. { .compatible = "ricoh,rn5t567", .data = (void *)RN5T567 },
  79. { .compatible = "ricoh,rn5t618", .data = (void *)RN5T618 },
  80. { .compatible = "ricoh,rc5t619", .data = (void *)RC5T619 },
  81. { }
  82. };
  83. MODULE_DEVICE_TABLE(of, rn5t618_of_match);
  84. static int rn5t618_i2c_probe(struct i2c_client *i2c,
  85. const struct i2c_device_id *id)
  86. {
  87. const struct of_device_id *of_id;
  88. struct rn5t618 *priv;
  89. int ret;
  90. of_id = of_match_device(rn5t618_of_match, &i2c->dev);
  91. if (!of_id) {
  92. dev_err(&i2c->dev, "Failed to find matching DT ID\n");
  93. return -EINVAL;
  94. }
  95. priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
  96. if (!priv)
  97. return -ENOMEM;
  98. i2c_set_clientdata(i2c, priv);
  99. priv->variant = (long)of_id->data;
  100. priv->regmap = devm_regmap_init_i2c(i2c, &rn5t618_regmap_config);
  101. if (IS_ERR(priv->regmap)) {
  102. ret = PTR_ERR(priv->regmap);
  103. dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
  104. return ret;
  105. }
  106. ret = devm_mfd_add_devices(&i2c->dev, -1, rn5t618_cells,
  107. ARRAY_SIZE(rn5t618_cells), NULL, 0, NULL);
  108. if (ret) {
  109. dev_err(&i2c->dev, "failed to add sub-devices: %d\n", ret);
  110. return ret;
  111. }
  112. rn5t618_pm_power_off = priv;
  113. if (of_device_is_system_power_controller(i2c->dev.of_node)) {
  114. if (!pm_power_off)
  115. pm_power_off = rn5t618_power_off;
  116. else
  117. dev_warn(&i2c->dev, "Poweroff callback already assigned\n");
  118. }
  119. rn5t618_restart_handler.notifier_call = rn5t618_restart;
  120. rn5t618_restart_handler.priority = 192;
  121. ret = register_restart_handler(&rn5t618_restart_handler);
  122. if (ret) {
  123. dev_err(&i2c->dev, "cannot register restart handler, %d\n", ret);
  124. return ret;
  125. }
  126. return 0;
  127. }
  128. static int rn5t618_i2c_remove(struct i2c_client *i2c)
  129. {
  130. struct rn5t618 *priv = i2c_get_clientdata(i2c);
  131. if (priv == rn5t618_pm_power_off) {
  132. rn5t618_pm_power_off = NULL;
  133. pm_power_off = NULL;
  134. }
  135. unregister_restart_handler(&rn5t618_restart_handler);
  136. return 0;
  137. }
  138. static const struct i2c_device_id rn5t618_i2c_id[] = {
  139. { }
  140. };
  141. MODULE_DEVICE_TABLE(i2c, rn5t618_i2c_id);
  142. static struct i2c_driver rn5t618_i2c_driver = {
  143. .driver = {
  144. .name = "rn5t618",
  145. .of_match_table = of_match_ptr(rn5t618_of_match),
  146. },
  147. .probe = rn5t618_i2c_probe,
  148. .remove = rn5t618_i2c_remove,
  149. .id_table = rn5t618_i2c_id,
  150. };
  151. module_i2c_driver(rn5t618_i2c_driver);
  152. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  153. MODULE_DESCRIPTION("Ricoh RN5T567/618 MFD driver");
  154. MODULE_LICENSE("GPL v2");