.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * MFD core driver for Ricoh RN5T618 PMIC |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com> |
---|
5 | 6 | * 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 | 7 | */ |
---|
14 | 8 | |
---|
15 | 9 | #include <linux/delay.h> |
---|
16 | 10 | #include <linux/i2c.h> |
---|
| 11 | +#include <linux/interrupt.h> |
---|
| 12 | +#include <linux/irq.h> |
---|
17 | 13 | #include <linux/mfd/core.h> |
---|
18 | 14 | #include <linux/mfd/rn5t618.h> |
---|
19 | 15 | #include <linux/module.h> |
---|
20 | 16 | #include <linux/of_device.h> |
---|
| 17 | +#include <linux/platform_device.h> |
---|
21 | 18 | #include <linux/reboot.h> |
---|
22 | 19 | #include <linux/regmap.h> |
---|
23 | 20 | |
---|
24 | 21 | static const struct mfd_cell rn5t618_cells[] = { |
---|
25 | 22 | { .name = "rn5t618-regulator" }, |
---|
| 23 | + { .name = "rn5t618-wdt" }, |
---|
| 24 | +}; |
---|
| 25 | + |
---|
| 26 | +static const struct mfd_cell rc5t619_cells[] = { |
---|
| 27 | + { .name = "rn5t618-adc" }, |
---|
| 28 | + { .name = "rn5t618-power" }, |
---|
| 29 | + { .name = "rn5t618-regulator" }, |
---|
| 30 | + { .name = "rc5t619-rtc" }, |
---|
26 | 31 | { .name = "rn5t618-wdt" }, |
---|
27 | 32 | }; |
---|
28 | 33 | |
---|
.. | .. |
---|
38 | 43 | case RN5T618_IR_GPF: |
---|
39 | 44 | case RN5T618_MON_IOIN: |
---|
40 | 45 | case RN5T618_INTMON: |
---|
| 46 | + case RN5T618_RTC_CTRL1 ... RN5T618_RTC_CTRL2: |
---|
| 47 | + case RN5T618_RTC_SECONDS ... RN5T618_RTC_YEAR: |
---|
| 48 | + case RN5T618_CHGSTATE: |
---|
| 49 | + case RN5T618_CHGCTRL_IRR ... RN5T618_CHGERR_MONI: |
---|
| 50 | + case RN5T618_CONTROL ... RN5T618_CC_AVEREG0: |
---|
41 | 51 | return true; |
---|
42 | 52 | default: |
---|
43 | 53 | return false; |
---|
.. | .. |
---|
52 | 62 | .cache_type = REGCACHE_RBTREE, |
---|
53 | 63 | }; |
---|
54 | 64 | |
---|
55 | | -static struct rn5t618 *rn5t618_pm_power_off; |
---|
| 65 | +static const struct regmap_irq rc5t619_irqs[] = { |
---|
| 66 | + REGMAP_IRQ_REG(RN5T618_IRQ_SYS, 0, BIT(0)), |
---|
| 67 | + REGMAP_IRQ_REG(RN5T618_IRQ_DCDC, 0, BIT(1)), |
---|
| 68 | + REGMAP_IRQ_REG(RN5T618_IRQ_RTC, 0, BIT(2)), |
---|
| 69 | + REGMAP_IRQ_REG(RN5T618_IRQ_ADC, 0, BIT(3)), |
---|
| 70 | + REGMAP_IRQ_REG(RN5T618_IRQ_GPIO, 0, BIT(4)), |
---|
| 71 | + REGMAP_IRQ_REG(RN5T618_IRQ_CHG, 0, BIT(6)), |
---|
| 72 | +}; |
---|
| 73 | + |
---|
| 74 | +static const struct regmap_irq_chip rc5t619_irq_chip = { |
---|
| 75 | + .name = "rc5t619", |
---|
| 76 | + .irqs = rc5t619_irqs, |
---|
| 77 | + .num_irqs = ARRAY_SIZE(rc5t619_irqs), |
---|
| 78 | + .num_regs = 1, |
---|
| 79 | + .status_base = RN5T618_INTMON, |
---|
| 80 | + .mask_base = RN5T618_INTEN, |
---|
| 81 | + .mask_invert = true, |
---|
| 82 | +}; |
---|
| 83 | + |
---|
| 84 | +static struct i2c_client *rn5t618_pm_power_off; |
---|
56 | 85 | static struct notifier_block rn5t618_restart_handler; |
---|
| 86 | + |
---|
| 87 | +static int rn5t618_irq_init(struct rn5t618 *rn5t618) |
---|
| 88 | +{ |
---|
| 89 | + const struct regmap_irq_chip *irq_chip = NULL; |
---|
| 90 | + int ret; |
---|
| 91 | + |
---|
| 92 | + if (!rn5t618->irq) |
---|
| 93 | + return 0; |
---|
| 94 | + |
---|
| 95 | + switch (rn5t618->variant) { |
---|
| 96 | + case RC5T619: |
---|
| 97 | + irq_chip = &rc5t619_irq_chip; |
---|
| 98 | + break; |
---|
| 99 | + default: |
---|
| 100 | + dev_err(rn5t618->dev, "Currently no IRQ support for variant %d\n", |
---|
| 101 | + (int)rn5t618->variant); |
---|
| 102 | + return -ENOENT; |
---|
| 103 | + } |
---|
| 104 | + |
---|
| 105 | + ret = devm_regmap_add_irq_chip(rn5t618->dev, rn5t618->regmap, |
---|
| 106 | + rn5t618->irq, |
---|
| 107 | + IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
---|
| 108 | + 0, irq_chip, &rn5t618->irq_data); |
---|
| 109 | + if (ret) |
---|
| 110 | + dev_err(rn5t618->dev, "Failed to register IRQ chip\n"); |
---|
| 111 | + |
---|
| 112 | + return ret; |
---|
| 113 | +} |
---|
57 | 114 | |
---|
58 | 115 | static void rn5t618_trigger_poweroff_sequence(bool repower) |
---|
59 | 116 | { |
---|
| 117 | + int ret; |
---|
| 118 | + |
---|
60 | 119 | /* disable automatic repower-on */ |
---|
61 | | - regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_REPCNT, |
---|
62 | | - RN5T618_REPCNT_REPWRON, |
---|
63 | | - repower ? RN5T618_REPCNT_REPWRON : 0); |
---|
| 120 | + ret = i2c_smbus_read_byte_data(rn5t618_pm_power_off, RN5T618_REPCNT); |
---|
| 121 | + if (ret < 0) |
---|
| 122 | + goto err; |
---|
| 123 | + |
---|
| 124 | + ret &= ~RN5T618_REPCNT_REPWRON; |
---|
| 125 | + if (repower) |
---|
| 126 | + ret |= RN5T618_REPCNT_REPWRON; |
---|
| 127 | + |
---|
| 128 | + ret = i2c_smbus_write_byte_data(rn5t618_pm_power_off, |
---|
| 129 | + RN5T618_REPCNT, (u8)ret); |
---|
| 130 | + if (ret < 0) |
---|
| 131 | + goto err; |
---|
| 132 | + |
---|
64 | 133 | /* start power-off sequence */ |
---|
65 | | - regmap_update_bits(rn5t618_pm_power_off->regmap, RN5T618_SLPCNT, |
---|
66 | | - RN5T618_SLPCNT_SWPWROFF, RN5T618_SLPCNT_SWPWROFF); |
---|
| 134 | + ret = i2c_smbus_read_byte_data(rn5t618_pm_power_off, RN5T618_SLPCNT); |
---|
| 135 | + if (ret < 0) |
---|
| 136 | + goto err; |
---|
| 137 | + |
---|
| 138 | + ret |= RN5T618_SLPCNT_SWPWROFF; |
---|
| 139 | + |
---|
| 140 | + ret = i2c_smbus_write_byte_data(rn5t618_pm_power_off, |
---|
| 141 | + RN5T618_SLPCNT, (u8)ret); |
---|
| 142 | + if (ret < 0) |
---|
| 143 | + goto err; |
---|
| 144 | + |
---|
| 145 | + return; |
---|
| 146 | + |
---|
| 147 | +err: |
---|
| 148 | + dev_alert(&rn5t618_pm_power_off->dev, "Failed to shutdown (err = %d)\n", ret); |
---|
67 | 149 | } |
---|
68 | 150 | |
---|
69 | 151 | static void rn5t618_power_off(void) |
---|
.. | .. |
---|
93 | 175 | }; |
---|
94 | 176 | MODULE_DEVICE_TABLE(of, rn5t618_of_match); |
---|
95 | 177 | |
---|
96 | | -static int rn5t618_i2c_probe(struct i2c_client *i2c, |
---|
97 | | - const struct i2c_device_id *id) |
---|
| 178 | +static int rn5t618_i2c_probe(struct i2c_client *i2c) |
---|
98 | 179 | { |
---|
99 | 180 | const struct of_device_id *of_id; |
---|
100 | 181 | struct rn5t618 *priv; |
---|
.. | .. |
---|
112 | 193 | |
---|
113 | 194 | i2c_set_clientdata(i2c, priv); |
---|
114 | 195 | priv->variant = (long)of_id->data; |
---|
| 196 | + priv->irq = i2c->irq; |
---|
| 197 | + priv->dev = &i2c->dev; |
---|
115 | 198 | |
---|
116 | 199 | priv->regmap = devm_regmap_init_i2c(i2c, &rn5t618_regmap_config); |
---|
117 | 200 | if (IS_ERR(priv->regmap)) { |
---|
.. | .. |
---|
120 | 203 | return ret; |
---|
121 | 204 | } |
---|
122 | 205 | |
---|
123 | | - ret = devm_mfd_add_devices(&i2c->dev, -1, rn5t618_cells, |
---|
124 | | - ARRAY_SIZE(rn5t618_cells), NULL, 0, NULL); |
---|
| 206 | + if (priv->variant == RC5T619) |
---|
| 207 | + ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_NONE, |
---|
| 208 | + rc5t619_cells, |
---|
| 209 | + ARRAY_SIZE(rc5t619_cells), |
---|
| 210 | + NULL, 0, NULL); |
---|
| 211 | + else |
---|
| 212 | + ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_NONE, |
---|
| 213 | + rn5t618_cells, |
---|
| 214 | + ARRAY_SIZE(rn5t618_cells), |
---|
| 215 | + NULL, 0, NULL); |
---|
125 | 216 | if (ret) { |
---|
126 | 217 | dev_err(&i2c->dev, "failed to add sub-devices: %d\n", ret); |
---|
127 | 218 | return ret; |
---|
128 | 219 | } |
---|
129 | 220 | |
---|
130 | | - rn5t618_pm_power_off = priv; |
---|
| 221 | + rn5t618_pm_power_off = i2c; |
---|
131 | 222 | if (of_device_is_system_power_controller(i2c->dev.of_node)) { |
---|
132 | 223 | if (!pm_power_off) |
---|
133 | 224 | pm_power_off = rn5t618_power_off; |
---|
.. | .. |
---|
144 | 235 | return ret; |
---|
145 | 236 | } |
---|
146 | 237 | |
---|
147 | | - return 0; |
---|
| 238 | + return rn5t618_irq_init(priv); |
---|
148 | 239 | } |
---|
149 | 240 | |
---|
150 | 241 | static int rn5t618_i2c_remove(struct i2c_client *i2c) |
---|
151 | 242 | { |
---|
152 | | - struct rn5t618 *priv = i2c_get_clientdata(i2c); |
---|
153 | | - |
---|
154 | | - if (priv == rn5t618_pm_power_off) { |
---|
| 243 | + if (i2c == rn5t618_pm_power_off) { |
---|
155 | 244 | rn5t618_pm_power_off = NULL; |
---|
156 | 245 | pm_power_off = NULL; |
---|
157 | 246 | } |
---|
.. | .. |
---|
161 | 250 | return 0; |
---|
162 | 251 | } |
---|
163 | 252 | |
---|
164 | | -static const struct i2c_device_id rn5t618_i2c_id[] = { |
---|
165 | | - { } |
---|
166 | | -}; |
---|
167 | | -MODULE_DEVICE_TABLE(i2c, rn5t618_i2c_id); |
---|
| 253 | +static int __maybe_unused rn5t618_i2c_suspend(struct device *dev) |
---|
| 254 | +{ |
---|
| 255 | + struct rn5t618 *priv = dev_get_drvdata(dev); |
---|
| 256 | + |
---|
| 257 | + if (priv->irq) |
---|
| 258 | + disable_irq(priv->irq); |
---|
| 259 | + |
---|
| 260 | + return 0; |
---|
| 261 | +} |
---|
| 262 | + |
---|
| 263 | +static int __maybe_unused rn5t618_i2c_resume(struct device *dev) |
---|
| 264 | +{ |
---|
| 265 | + struct rn5t618 *priv = dev_get_drvdata(dev); |
---|
| 266 | + |
---|
| 267 | + if (priv->irq) |
---|
| 268 | + enable_irq(priv->irq); |
---|
| 269 | + |
---|
| 270 | + return 0; |
---|
| 271 | +} |
---|
| 272 | + |
---|
| 273 | +static SIMPLE_DEV_PM_OPS(rn5t618_i2c_dev_pm_ops, |
---|
| 274 | + rn5t618_i2c_suspend, |
---|
| 275 | + rn5t618_i2c_resume); |
---|
168 | 276 | |
---|
169 | 277 | static struct i2c_driver rn5t618_i2c_driver = { |
---|
170 | 278 | .driver = { |
---|
171 | 279 | .name = "rn5t618", |
---|
172 | 280 | .of_match_table = of_match_ptr(rn5t618_of_match), |
---|
| 281 | + .pm = &rn5t618_i2c_dev_pm_ops, |
---|
173 | 282 | }, |
---|
174 | | - .probe = rn5t618_i2c_probe, |
---|
| 283 | + .probe_new = rn5t618_i2c_probe, |
---|
175 | 284 | .remove = rn5t618_i2c_remove, |
---|
176 | | - .id_table = rn5t618_i2c_id, |
---|
177 | 285 | }; |
---|
178 | 286 | |
---|
179 | 287 | module_i2c_driver(rn5t618_i2c_driver); |
---|