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