rn5t618.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * MFD core driver for Ricoh RN5T618 PMIC
  4. *
  5. * Copyright (C) 2014 Beniamino Galvani <[email protected]>
  6. * Copyright (C) 2016 Toradex AG
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/i2c.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irq.h>
  12. #include <linux/mfd/core.h>
  13. #include <linux/mfd/rn5t618.h>
  14. #include <linux/module.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/reboot.h>
  18. #include <linux/regmap.h>
  19. static const struct mfd_cell rn5t618_cells[] = {
  20. { .name = "rn5t618-regulator" },
  21. { .name = "rn5t618-wdt" },
  22. };
  23. static const struct mfd_cell rc5t619_cells[] = {
  24. { .name = "rn5t618-adc" },
  25. { .name = "rn5t618-power" },
  26. { .name = "rn5t618-regulator" },
  27. { .name = "rc5t619-rtc" },
  28. { .name = "rn5t618-wdt" },
  29. };
  30. static bool rn5t618_volatile_reg(struct device *dev, unsigned int reg)
  31. {
  32. switch (reg) {
  33. case RN5T618_WATCHDOGCNT:
  34. case RN5T618_DCIRQ:
  35. case RN5T618_ILIMDATAH ... RN5T618_AIN0DATAL:
  36. case RN5T618_ADCCNT3:
  37. case RN5T618_IR_ADC1 ... RN5T618_IR_ADC3:
  38. case RN5T618_IR_GPR:
  39. case RN5T618_IR_GPF:
  40. case RN5T618_MON_IOIN:
  41. case RN5T618_INTMON:
  42. case RN5T618_RTC_CTRL1 ... RN5T618_RTC_CTRL2:
  43. case RN5T618_RTC_SECONDS ... RN5T618_RTC_YEAR:
  44. case RN5T618_CHGCTL1:
  45. case RN5T618_REGISET1 ... RN5T618_REGISET2:
  46. case RN5T618_CHGSTATE:
  47. case RN5T618_CHGCTRL_IRR ... RN5T618_CHGERR_MONI:
  48. case RN5T618_GCHGDET:
  49. case RN5T618_CONTROL ... RN5T618_CC_AVEREG0:
  50. return true;
  51. default:
  52. return false;
  53. }
  54. }
  55. static const struct regmap_config rn5t618_regmap_config = {
  56. .reg_bits = 8,
  57. .val_bits = 8,
  58. .volatile_reg = rn5t618_volatile_reg,
  59. .max_register = RN5T618_MAX_REG,
  60. .cache_type = REGCACHE_RBTREE,
  61. };
  62. static const struct regmap_irq rc5t619_irqs[] = {
  63. REGMAP_IRQ_REG(RN5T618_IRQ_SYS, 0, BIT(0)),
  64. REGMAP_IRQ_REG(RN5T618_IRQ_DCDC, 0, BIT(1)),
  65. REGMAP_IRQ_REG(RN5T618_IRQ_RTC, 0, BIT(2)),
  66. REGMAP_IRQ_REG(RN5T618_IRQ_ADC, 0, BIT(3)),
  67. REGMAP_IRQ_REG(RN5T618_IRQ_GPIO, 0, BIT(4)),
  68. REGMAP_IRQ_REG(RN5T618_IRQ_CHG, 0, BIT(6)),
  69. };
  70. static const struct regmap_irq_chip rc5t619_irq_chip = {
  71. .name = "rc5t619",
  72. .irqs = rc5t619_irqs,
  73. .num_irqs = ARRAY_SIZE(rc5t619_irqs),
  74. .num_regs = 1,
  75. .status_base = RN5T618_INTMON,
  76. .mask_base = RN5T618_INTEN,
  77. .mask_invert = true,
  78. };
  79. static struct i2c_client *rn5t618_pm_power_off;
  80. static struct notifier_block rn5t618_restart_handler;
  81. static int rn5t618_irq_init(struct rn5t618 *rn5t618)
  82. {
  83. const struct regmap_irq_chip *irq_chip = NULL;
  84. int ret;
  85. if (!rn5t618->irq)
  86. return 0;
  87. switch (rn5t618->variant) {
  88. case RC5T619:
  89. irq_chip = &rc5t619_irq_chip;
  90. break;
  91. default:
  92. dev_err(rn5t618->dev, "Currently no IRQ support for variant %d\n",
  93. (int)rn5t618->variant);
  94. return -ENOENT;
  95. }
  96. ret = devm_regmap_add_irq_chip(rn5t618->dev, rn5t618->regmap,
  97. rn5t618->irq,
  98. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  99. 0, irq_chip, &rn5t618->irq_data);
  100. if (ret)
  101. dev_err(rn5t618->dev, "Failed to register IRQ chip\n");
  102. return ret;
  103. }
  104. static void rn5t618_trigger_poweroff_sequence(bool repower)
  105. {
  106. int ret;
  107. /* disable automatic repower-on */
  108. ret = i2c_smbus_read_byte_data(rn5t618_pm_power_off, RN5T618_REPCNT);
  109. if (ret < 0)
  110. goto err;
  111. ret &= ~RN5T618_REPCNT_REPWRON;
  112. if (repower)
  113. ret |= RN5T618_REPCNT_REPWRON;
  114. ret = i2c_smbus_write_byte_data(rn5t618_pm_power_off,
  115. RN5T618_REPCNT, (u8)ret);
  116. if (ret < 0)
  117. goto err;
  118. /* start power-off sequence */
  119. ret = i2c_smbus_read_byte_data(rn5t618_pm_power_off, RN5T618_SLPCNT);
  120. if (ret < 0)
  121. goto err;
  122. ret |= RN5T618_SLPCNT_SWPWROFF;
  123. ret = i2c_smbus_write_byte_data(rn5t618_pm_power_off,
  124. RN5T618_SLPCNT, (u8)ret);
  125. if (ret < 0)
  126. goto err;
  127. return;
  128. err:
  129. dev_alert(&rn5t618_pm_power_off->dev, "Failed to shutdown (err = %d)\n", ret);
  130. }
  131. static void rn5t618_power_off(void)
  132. {
  133. rn5t618_trigger_poweroff_sequence(false);
  134. }
  135. static int rn5t618_restart(struct notifier_block *this,
  136. unsigned long mode, void *cmd)
  137. {
  138. rn5t618_trigger_poweroff_sequence(true);
  139. /*
  140. * Re-power factor detection on PMIC side is not instant. 1ms
  141. * proved to be enough time until reset takes effect.
  142. */
  143. mdelay(1);
  144. return NOTIFY_DONE;
  145. }
  146. static const struct of_device_id rn5t618_of_match[] = {
  147. { .compatible = "ricoh,rn5t567", .data = (void *)RN5T567 },
  148. { .compatible = "ricoh,rn5t618", .data = (void *)RN5T618 },
  149. { .compatible = "ricoh,rc5t619", .data = (void *)RC5T619 },
  150. { }
  151. };
  152. MODULE_DEVICE_TABLE(of, rn5t618_of_match);
  153. static int rn5t618_i2c_probe(struct i2c_client *i2c)
  154. {
  155. const struct of_device_id *of_id;
  156. struct rn5t618 *priv;
  157. int ret;
  158. of_id = of_match_device(rn5t618_of_match, &i2c->dev);
  159. if (!of_id) {
  160. dev_err(&i2c->dev, "Failed to find matching DT ID\n");
  161. return -EINVAL;
  162. }
  163. priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
  164. if (!priv)
  165. return -ENOMEM;
  166. i2c_set_clientdata(i2c, priv);
  167. priv->variant = (long)of_id->data;
  168. priv->irq = i2c->irq;
  169. priv->dev = &i2c->dev;
  170. priv->regmap = devm_regmap_init_i2c(i2c, &rn5t618_regmap_config);
  171. if (IS_ERR(priv->regmap)) {
  172. ret = PTR_ERR(priv->regmap);
  173. dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
  174. return ret;
  175. }
  176. if (priv->variant == RC5T619)
  177. ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_NONE,
  178. rc5t619_cells,
  179. ARRAY_SIZE(rc5t619_cells),
  180. NULL, 0, NULL);
  181. else
  182. ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_NONE,
  183. rn5t618_cells,
  184. ARRAY_SIZE(rn5t618_cells),
  185. NULL, 0, NULL);
  186. if (ret) {
  187. dev_err(&i2c->dev, "failed to add sub-devices: %d\n", ret);
  188. return ret;
  189. }
  190. rn5t618_pm_power_off = i2c;
  191. if (of_device_is_system_power_controller(i2c->dev.of_node)) {
  192. if (!pm_power_off)
  193. pm_power_off = rn5t618_power_off;
  194. else
  195. dev_warn(&i2c->dev, "Poweroff callback already assigned\n");
  196. }
  197. rn5t618_restart_handler.notifier_call = rn5t618_restart;
  198. rn5t618_restart_handler.priority = 192;
  199. ret = register_restart_handler(&rn5t618_restart_handler);
  200. if (ret) {
  201. dev_err(&i2c->dev, "cannot register restart handler, %d\n", ret);
  202. return ret;
  203. }
  204. return rn5t618_irq_init(priv);
  205. }
  206. static void rn5t618_i2c_remove(struct i2c_client *i2c)
  207. {
  208. if (i2c == rn5t618_pm_power_off) {
  209. rn5t618_pm_power_off = NULL;
  210. pm_power_off = NULL;
  211. }
  212. unregister_restart_handler(&rn5t618_restart_handler);
  213. }
  214. static int __maybe_unused rn5t618_i2c_suspend(struct device *dev)
  215. {
  216. struct rn5t618 *priv = dev_get_drvdata(dev);
  217. if (priv->irq)
  218. disable_irq(priv->irq);
  219. return 0;
  220. }
  221. static int __maybe_unused rn5t618_i2c_resume(struct device *dev)
  222. {
  223. struct rn5t618 *priv = dev_get_drvdata(dev);
  224. if (priv->irq)
  225. enable_irq(priv->irq);
  226. return 0;
  227. }
  228. static SIMPLE_DEV_PM_OPS(rn5t618_i2c_dev_pm_ops,
  229. rn5t618_i2c_suspend,
  230. rn5t618_i2c_resume);
  231. static struct i2c_driver rn5t618_i2c_driver = {
  232. .driver = {
  233. .name = "rn5t618",
  234. .of_match_table = of_match_ptr(rn5t618_of_match),
  235. .pm = &rn5t618_i2c_dev_pm_ops,
  236. },
  237. .probe_new = rn5t618_i2c_probe,
  238. .remove = rn5t618_i2c_remove,
  239. };
  240. module_i2c_driver(rn5t618_i2c_driver);
  241. MODULE_AUTHOR("Beniamino Galvani <[email protected]>");
  242. MODULE_DESCRIPTION("Ricoh RN5T567/618 MFD driver");
  243. MODULE_LICENSE("GPL v2");