Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * MFD core driver for Ricoh RN5T618 PMIC
0004  *
0005  * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
0006  * Copyright (C) 2016 Toradex AG
0007  */
0008 
0009 #include <linux/delay.h>
0010 #include <linux/i2c.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/irq.h>
0013 #include <linux/mfd/core.h>
0014 #include <linux/mfd/rn5t618.h>
0015 #include <linux/module.h>
0016 #include <linux/of_device.h>
0017 #include <linux/platform_device.h>
0018 #include <linux/reboot.h>
0019 #include <linux/regmap.h>
0020 
0021 static const struct mfd_cell rn5t618_cells[] = {
0022     { .name = "rn5t618-regulator" },
0023     { .name = "rn5t618-wdt" },
0024 };
0025 
0026 static const struct mfd_cell rc5t619_cells[] = {
0027     { .name = "rn5t618-adc" },
0028     { .name = "rn5t618-power" },
0029     { .name = "rn5t618-regulator" },
0030     { .name = "rc5t619-rtc" },
0031     { .name = "rn5t618-wdt" },
0032 };
0033 
0034 static bool rn5t618_volatile_reg(struct device *dev, unsigned int reg)
0035 {
0036     switch (reg) {
0037     case RN5T618_WATCHDOGCNT:
0038     case RN5T618_DCIRQ:
0039     case RN5T618_ILIMDATAH ... RN5T618_AIN0DATAL:
0040     case RN5T618_ADCCNT3:
0041     case RN5T618_IR_ADC1 ... RN5T618_IR_ADC3:
0042     case RN5T618_IR_GPR:
0043     case RN5T618_IR_GPF:
0044     case RN5T618_MON_IOIN:
0045     case RN5T618_INTMON:
0046     case RN5T618_RTC_CTRL1 ... RN5T618_RTC_CTRL2:
0047     case RN5T618_RTC_SECONDS ... RN5T618_RTC_YEAR:
0048     case RN5T618_CHGCTL1:
0049     case RN5T618_REGISET1 ... RN5T618_REGISET2:
0050     case RN5T618_CHGSTATE:
0051     case RN5T618_CHGCTRL_IRR ... RN5T618_CHGERR_MONI:
0052     case RN5T618_GCHGDET:
0053     case RN5T618_CONTROL ... RN5T618_CC_AVEREG0:
0054         return true;
0055     default:
0056         return false;
0057     }
0058 }
0059 
0060 static const struct regmap_config rn5t618_regmap_config = {
0061     .reg_bits   = 8,
0062     .val_bits   = 8,
0063     .volatile_reg   = rn5t618_volatile_reg,
0064     .max_register   = RN5T618_MAX_REG,
0065     .cache_type = REGCACHE_RBTREE,
0066 };
0067 
0068 static const struct regmap_irq rc5t619_irqs[] = {
0069     REGMAP_IRQ_REG(RN5T618_IRQ_SYS, 0, BIT(0)),
0070     REGMAP_IRQ_REG(RN5T618_IRQ_DCDC, 0, BIT(1)),
0071     REGMAP_IRQ_REG(RN5T618_IRQ_RTC, 0, BIT(2)),
0072     REGMAP_IRQ_REG(RN5T618_IRQ_ADC, 0, BIT(3)),
0073     REGMAP_IRQ_REG(RN5T618_IRQ_GPIO, 0, BIT(4)),
0074     REGMAP_IRQ_REG(RN5T618_IRQ_CHG, 0, BIT(6)),
0075 };
0076 
0077 static const struct regmap_irq_chip rc5t619_irq_chip = {
0078     .name = "rc5t619",
0079     .irqs = rc5t619_irqs,
0080     .num_irqs = ARRAY_SIZE(rc5t619_irqs),
0081     .num_regs = 1,
0082     .status_base = RN5T618_INTMON,
0083     .mask_base = RN5T618_INTEN,
0084     .mask_invert = true,
0085 };
0086 
0087 static struct i2c_client *rn5t618_pm_power_off;
0088 static struct notifier_block rn5t618_restart_handler;
0089 
0090 static int rn5t618_irq_init(struct rn5t618 *rn5t618)
0091 {
0092     const struct regmap_irq_chip *irq_chip = NULL;
0093     int ret;
0094 
0095     if (!rn5t618->irq)
0096         return 0;
0097 
0098     switch (rn5t618->variant) {
0099     case RC5T619:
0100         irq_chip = &rc5t619_irq_chip;
0101         break;
0102     default:
0103         dev_err(rn5t618->dev, "Currently no IRQ support for variant %d\n",
0104             (int)rn5t618->variant);
0105         return -ENOENT;
0106     }
0107 
0108     ret = devm_regmap_add_irq_chip(rn5t618->dev, rn5t618->regmap,
0109                        rn5t618->irq,
0110                        IRQF_TRIGGER_LOW | IRQF_ONESHOT,
0111                        0, irq_chip, &rn5t618->irq_data);
0112     if (ret)
0113         dev_err(rn5t618->dev, "Failed to register IRQ chip\n");
0114 
0115     return ret;
0116 }
0117 
0118 static void rn5t618_trigger_poweroff_sequence(bool repower)
0119 {
0120     int ret;
0121 
0122     /* disable automatic repower-on */
0123     ret = i2c_smbus_read_byte_data(rn5t618_pm_power_off, RN5T618_REPCNT);
0124     if (ret < 0)
0125         goto err;
0126 
0127     ret &= ~RN5T618_REPCNT_REPWRON;
0128     if (repower)
0129         ret |= RN5T618_REPCNT_REPWRON;
0130 
0131     ret = i2c_smbus_write_byte_data(rn5t618_pm_power_off,
0132                     RN5T618_REPCNT, (u8)ret);
0133     if (ret < 0)
0134         goto err;
0135 
0136     /* start power-off sequence */
0137     ret = i2c_smbus_read_byte_data(rn5t618_pm_power_off, RN5T618_SLPCNT);
0138     if (ret < 0)
0139         goto err;
0140 
0141     ret |= RN5T618_SLPCNT_SWPWROFF;
0142 
0143     ret = i2c_smbus_write_byte_data(rn5t618_pm_power_off,
0144                     RN5T618_SLPCNT, (u8)ret);
0145     if (ret < 0)
0146         goto err;
0147 
0148     return;
0149 
0150 err:
0151     dev_alert(&rn5t618_pm_power_off->dev, "Failed to shutdown (err = %d)\n", ret);
0152 }
0153 
0154 static void rn5t618_power_off(void)
0155 {
0156     rn5t618_trigger_poweroff_sequence(false);
0157 }
0158 
0159 static int rn5t618_restart(struct notifier_block *this,
0160                 unsigned long mode, void *cmd)
0161 {
0162     rn5t618_trigger_poweroff_sequence(true);
0163 
0164     /*
0165      * Re-power factor detection on PMIC side is not instant. 1ms
0166      * proved to be enough time until reset takes effect.
0167      */
0168     mdelay(1);
0169 
0170     return NOTIFY_DONE;
0171 }
0172 
0173 static const struct of_device_id rn5t618_of_match[] = {
0174     { .compatible = "ricoh,rn5t567", .data = (void *)RN5T567 },
0175     { .compatible = "ricoh,rn5t618", .data = (void *)RN5T618 },
0176     { .compatible = "ricoh,rc5t619", .data = (void *)RC5T619 },
0177     { }
0178 };
0179 MODULE_DEVICE_TABLE(of, rn5t618_of_match);
0180 
0181 static int rn5t618_i2c_probe(struct i2c_client *i2c)
0182 {
0183     const struct of_device_id *of_id;
0184     struct rn5t618 *priv;
0185     int ret;
0186 
0187     of_id = of_match_device(rn5t618_of_match, &i2c->dev);
0188     if (!of_id) {
0189         dev_err(&i2c->dev, "Failed to find matching DT ID\n");
0190         return -EINVAL;
0191     }
0192 
0193     priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
0194     if (!priv)
0195         return -ENOMEM;
0196 
0197     i2c_set_clientdata(i2c, priv);
0198     priv->variant = (long)of_id->data;
0199     priv->irq = i2c->irq;
0200     priv->dev = &i2c->dev;
0201 
0202     priv->regmap = devm_regmap_init_i2c(i2c, &rn5t618_regmap_config);
0203     if (IS_ERR(priv->regmap)) {
0204         ret = PTR_ERR(priv->regmap);
0205         dev_err(&i2c->dev, "regmap init failed: %d\n", ret);
0206         return ret;
0207     }
0208 
0209     if (priv->variant == RC5T619)
0210         ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_NONE,
0211                        rc5t619_cells,
0212                        ARRAY_SIZE(rc5t619_cells),
0213                        NULL, 0, NULL);
0214     else
0215         ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_NONE,
0216                        rn5t618_cells,
0217                        ARRAY_SIZE(rn5t618_cells),
0218                        NULL, 0, NULL);
0219     if (ret) {
0220         dev_err(&i2c->dev, "failed to add sub-devices: %d\n", ret);
0221         return ret;
0222     }
0223 
0224     rn5t618_pm_power_off = i2c;
0225     if (of_device_is_system_power_controller(i2c->dev.of_node)) {
0226         if (!pm_power_off)
0227             pm_power_off = rn5t618_power_off;
0228         else
0229             dev_warn(&i2c->dev, "Poweroff callback already assigned\n");
0230     }
0231 
0232     rn5t618_restart_handler.notifier_call = rn5t618_restart;
0233     rn5t618_restart_handler.priority = 192;
0234 
0235     ret = register_restart_handler(&rn5t618_restart_handler);
0236     if (ret) {
0237         dev_err(&i2c->dev, "cannot register restart handler, %d\n", ret);
0238         return ret;
0239     }
0240 
0241     return rn5t618_irq_init(priv);
0242 }
0243 
0244 static int rn5t618_i2c_remove(struct i2c_client *i2c)
0245 {
0246     if (i2c == rn5t618_pm_power_off) {
0247         rn5t618_pm_power_off = NULL;
0248         pm_power_off = NULL;
0249     }
0250 
0251     unregister_restart_handler(&rn5t618_restart_handler);
0252 
0253     return 0;
0254 }
0255 
0256 static int __maybe_unused rn5t618_i2c_suspend(struct device *dev)
0257 {
0258     struct rn5t618 *priv = dev_get_drvdata(dev);
0259 
0260     if (priv->irq)
0261         disable_irq(priv->irq);
0262 
0263     return 0;
0264 }
0265 
0266 static int __maybe_unused rn5t618_i2c_resume(struct device *dev)
0267 {
0268     struct rn5t618 *priv = dev_get_drvdata(dev);
0269 
0270     if (priv->irq)
0271         enable_irq(priv->irq);
0272 
0273     return 0;
0274 }
0275 
0276 static SIMPLE_DEV_PM_OPS(rn5t618_i2c_dev_pm_ops,
0277             rn5t618_i2c_suspend,
0278             rn5t618_i2c_resume);
0279 
0280 static struct i2c_driver rn5t618_i2c_driver = {
0281     .driver = {
0282         .name = "rn5t618",
0283         .of_match_table = of_match_ptr(rn5t618_of_match),
0284         .pm = &rn5t618_i2c_dev_pm_ops,
0285     },
0286     .probe_new = rn5t618_i2c_probe,
0287     .remove = rn5t618_i2c_remove,
0288 };
0289 
0290 module_i2c_driver(rn5t618_i2c_driver);
0291 
0292 MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
0293 MODULE_DESCRIPTION("Ricoh RN5T567/618 MFD driver");
0294 MODULE_LICENSE("GPL v2");