Thomas Gleixner | 3c910ec | 2019-06-01 10:09:00 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 2 | /* |
| 3 | * MFD core driver for Ricoh RN5T618 PMIC |
| 4 | * |
| 5 | * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com> |
Stefan Agner | a99ab50 | 2016-06-28 22:53:19 -0700 | [diff] [blame] | 6 | * Copyright (C) 2016 Toradex AG |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Stefan Agner | a370f60 | 2016-06-28 22:53:22 -0700 | [diff] [blame] | 9 | #include <linux/delay.h> |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 10 | #include <linux/i2c.h> |
| 11 | #include <linux/mfd/core.h> |
| 12 | #include <linux/mfd/rn5t618.h> |
| 13 | #include <linux/module.h> |
Stefan Agner | a99ab50 | 2016-06-28 22:53:19 -0700 | [diff] [blame] | 14 | #include <linux/of_device.h> |
Stefan Agner | a370f60 | 2016-06-28 22:53:22 -0700 | [diff] [blame] | 15 | #include <linux/reboot.h> |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 16 | #include <linux/regmap.h> |
| 17 | |
| 18 | static const struct mfd_cell rn5t618_cells[] = { |
| 19 | { .name = "rn5t618-regulator" }, |
| 20 | { .name = "rn5t618-wdt" }, |
| 21 | }; |
| 22 | |
| 23 | static bool rn5t618_volatile_reg(struct device *dev, unsigned int reg) |
| 24 | { |
| 25 | switch (reg) { |
| 26 | case RN5T618_WATCHDOGCNT: |
| 27 | case RN5T618_DCIRQ: |
| 28 | case RN5T618_ILIMDATAH ... RN5T618_AIN0DATAL: |
| 29 | case RN5T618_IR_ADC1 ... RN5T618_IR_ADC3: |
| 30 | case RN5T618_IR_GPR: |
| 31 | case RN5T618_IR_GPF: |
| 32 | case RN5T618_MON_IOIN: |
| 33 | case RN5T618_INTMON: |
| 34 | return true; |
| 35 | default: |
| 36 | return false; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static const struct regmap_config rn5t618_regmap_config = { |
| 41 | .reg_bits = 8, |
| 42 | .val_bits = 8, |
| 43 | .volatile_reg = rn5t618_volatile_reg, |
| 44 | .max_register = RN5T618_MAX_REG, |
| 45 | .cache_type = REGCACHE_RBTREE, |
| 46 | }; |
| 47 | |
| 48 | static struct rn5t618 *rn5t618_pm_power_off; |
Stefan Agner | a370f60 | 2016-06-28 22:53:22 -0700 | [diff] [blame] | 49 | static struct notifier_block rn5t618_restart_handler; |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 50 | |
Stefan Agner | a370f60 | 2016-06-28 22:53:22 -0700 | [diff] [blame] | 51 | static void rn5t618_trigger_poweroff_sequence(bool repower) |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 52 | { |
| 53 | /* disable automatic repower-on */ |
| 54 | regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_REPCNT, |
Stefan Agner | a370f60 | 2016-06-28 22:53:22 -0700 | [diff] [blame] | 55 | RN5T618_REPCNT_REPWRON, |
| 56 | repower ? RN5T618_REPCNT_REPWRON : 0); |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 57 | /* start power-off sequence */ |
| 58 | regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_SLPCNT, |
| 59 | RN5T618_SLPCNT_SWPWROFF, RN5T618_SLPCNT_SWPWROFF); |
| 60 | } |
| 61 | |
Stefan Agner | a370f60 | 2016-06-28 22:53:22 -0700 | [diff] [blame] | 62 | static void rn5t618_power_off(void) |
| 63 | { |
| 64 | rn5t618_trigger_poweroff_sequence(false); |
| 65 | } |
| 66 | |
| 67 | static int rn5t618_restart(struct notifier_block *this, |
| 68 | unsigned long mode, void *cmd) |
| 69 | { |
| 70 | rn5t618_trigger_poweroff_sequence(true); |
| 71 | |
| 72 | /* |
| 73 | * Re-power factor detection on PMIC side is not instant. 1ms |
| 74 | * proved to be enough time until reset takes effect. |
| 75 | */ |
| 76 | mdelay(1); |
| 77 | |
| 78 | return NOTIFY_DONE; |
| 79 | } |
| 80 | |
Stefan Agner | a99ab50 | 2016-06-28 22:53:19 -0700 | [diff] [blame] | 81 | static const struct of_device_id rn5t618_of_match[] = { |
| 82 | { .compatible = "ricoh,rn5t567", .data = (void *)RN5T567 }, |
| 83 | { .compatible = "ricoh,rn5t618", .data = (void *)RN5T618 }, |
Pierre-Hugues Husson | c5e589a | 2016-11-05 17:19:24 +0100 | [diff] [blame] | 84 | { .compatible = "ricoh,rc5t619", .data = (void *)RC5T619 }, |
Stefan Agner | a99ab50 | 2016-06-28 22:53:19 -0700 | [diff] [blame] | 85 | { } |
| 86 | }; |
| 87 | MODULE_DEVICE_TABLE(of, rn5t618_of_match); |
| 88 | |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 89 | static int rn5t618_i2c_probe(struct i2c_client *i2c, |
| 90 | const struct i2c_device_id *id) |
| 91 | { |
Stefan Agner | a99ab50 | 2016-06-28 22:53:19 -0700 | [diff] [blame] | 92 | const struct of_device_id *of_id; |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 93 | struct rn5t618 *priv; |
| 94 | int ret; |
| 95 | |
Stefan Agner | a99ab50 | 2016-06-28 22:53:19 -0700 | [diff] [blame] | 96 | of_id = of_match_device(rn5t618_of_match, &i2c->dev); |
| 97 | if (!of_id) { |
| 98 | dev_err(&i2c->dev, "Failed to find matching DT ID\n"); |
| 99 | return -EINVAL; |
| 100 | } |
| 101 | |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 102 | priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL); |
| 103 | if (!priv) |
| 104 | return -ENOMEM; |
| 105 | |
| 106 | i2c_set_clientdata(i2c, priv); |
Stefan Agner | a99ab50 | 2016-06-28 22:53:19 -0700 | [diff] [blame] | 107 | priv->variant = (long)of_id->data; |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 108 | |
| 109 | priv->regmap = devm_regmap_init_i2c(i2c, &rn5t618_regmap_config); |
| 110 | if (IS_ERR(priv->regmap)) { |
| 111 | ret = PTR_ERR(priv->regmap); |
| 112 | dev_err(&i2c->dev, "regmap init failed: %d\n", ret); |
| 113 | return ret; |
| 114 | } |
| 115 | |
Laxman Dewangan | f41206c | 2016-04-08 00:13:07 +0530 | [diff] [blame] | 116 | ret = devm_mfd_add_devices(&i2c->dev, -1, rn5t618_cells, |
| 117 | ARRAY_SIZE(rn5t618_cells), NULL, 0, NULL); |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 118 | if (ret) { |
| 119 | dev_err(&i2c->dev, "failed to add sub-devices: %d\n", ret); |
| 120 | return ret; |
| 121 | } |
| 122 | |
Stefan Agner | a370f60 | 2016-06-28 22:53:22 -0700 | [diff] [blame] | 123 | rn5t618_pm_power_off = priv; |
Stefan Agner | fd65ae4 | 2016-06-28 22:53:21 -0700 | [diff] [blame] | 124 | if (of_device_is_system_power_controller(i2c->dev.of_node)) { |
Stefan Agner | a370f60 | 2016-06-28 22:53:22 -0700 | [diff] [blame] | 125 | if (!pm_power_off) |
Stefan Agner | fd65ae4 | 2016-06-28 22:53:21 -0700 | [diff] [blame] | 126 | pm_power_off = rn5t618_power_off; |
Stefan Agner | a370f60 | 2016-06-28 22:53:22 -0700 | [diff] [blame] | 127 | else |
Stefan Agner | fd65ae4 | 2016-06-28 22:53:21 -0700 | [diff] [blame] | 128 | dev_warn(&i2c->dev, "Poweroff callback already assigned\n"); |
Stefan Agner | a370f60 | 2016-06-28 22:53:22 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | rn5t618_restart_handler.notifier_call = rn5t618_restart; |
| 132 | rn5t618_restart_handler.priority = 192; |
| 133 | |
| 134 | ret = register_restart_handler(&rn5t618_restart_handler); |
| 135 | if (ret) { |
| 136 | dev_err(&i2c->dev, "cannot register restart handler, %d\n", ret); |
| 137 | return ret; |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int rn5t618_i2c_remove(struct i2c_client *i2c) |
| 144 | { |
| 145 | struct rn5t618 *priv = i2c_get_clientdata(i2c); |
| 146 | |
| 147 | if (priv == rn5t618_pm_power_off) { |
| 148 | rn5t618_pm_power_off = NULL; |
| 149 | pm_power_off = NULL; |
| 150 | } |
| 151 | |
Stefan Agner | ecca790 | 2017-05-15 23:24:11 -0700 | [diff] [blame] | 152 | unregister_restart_handler(&rn5t618_restart_handler); |
| 153 | |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 157 | static const struct i2c_device_id rn5t618_i2c_id[] = { |
| 158 | { } |
| 159 | }; |
| 160 | MODULE_DEVICE_TABLE(i2c, rn5t618_i2c_id); |
| 161 | |
| 162 | static struct i2c_driver rn5t618_i2c_driver = { |
| 163 | .driver = { |
| 164 | .name = "rn5t618", |
| 165 | .of_match_table = of_match_ptr(rn5t618_of_match), |
| 166 | }, |
| 167 | .probe = rn5t618_i2c_probe, |
| 168 | .remove = rn5t618_i2c_remove, |
| 169 | .id_table = rn5t618_i2c_id, |
| 170 | }; |
| 171 | |
| 172 | module_i2c_driver(rn5t618_i2c_driver); |
| 173 | |
| 174 | MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>"); |
Stefan Agner | a99ab50 | 2016-06-28 22:53:19 -0700 | [diff] [blame] | 175 | MODULE_DESCRIPTION("Ricoh RN5T567/618 MFD driver"); |
Beniamino Galvani | 9bb9e29 | 2014-08-30 14:50:23 +0200 | [diff] [blame] | 176 | MODULE_LICENSE("GPL v2"); |