Commit | Line | Data |
---|---|---|
2aec85b2 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
d48f411c AC |
2 | /* |
3 | * tps65217.c | |
4 | * | |
5 | * TPS65217 chip family multi-function driver | |
6 | * | |
4f4ed454 | 7 | * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/ |
d48f411c AC |
8 | */ |
9 | ||
d48f411c | 10 | #include <linux/device.h> |
6556bdac | 11 | #include <linux/err.h> |
d48f411c | 12 | #include <linux/init.h> |
6556bdac | 13 | #include <linux/interrupt.h> |
d48f411c | 14 | #include <linux/i2c.h> |
6556bdac MN |
15 | #include <linux/irq.h> |
16 | #include <linux/irqdomain.h> | |
17 | #include <linux/kernel.h> | |
18 | #include <linux/module.h> | |
817bb7fb | 19 | #include <linux/of.h> |
6556bdac MN |
20 | #include <linux/platform_device.h> |
21 | #include <linux/regmap.h> | |
22 | #include <linux/slab.h> | |
d48f411c AC |
23 | |
24 | #include <linux/mfd/core.h> | |
25 | #include <linux/mfd/tps65217.h> | |
26 | ||
0aefed0e | 27 | static const struct resource charger_resources[] = { |
6556bdac MN |
28 | DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_AC, "AC"), |
29 | DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_USB, "USB"), | |
30 | }; | |
31 | ||
0aefed0e | 32 | static const struct resource pb_resources[] = { |
dea9c730 MN |
33 | DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_PB, "PB"), |
34 | }; | |
35 | ||
6556bdac MN |
36 | static void tps65217_irq_lock(struct irq_data *data) |
37 | { | |
38 | struct tps65217 *tps = irq_data_get_irq_chip_data(data); | |
39 | ||
40 | mutex_lock(&tps->irq_lock); | |
41 | } | |
42 | ||
43 | static void tps65217_irq_sync_unlock(struct irq_data *data) | |
44 | { | |
45 | struct tps65217 *tps = irq_data_get_irq_chip_data(data); | |
46 | int ret; | |
47 | ||
fa917052 MK |
48 | ret = tps65217_set_bits(tps, TPS65217_REG_INT, TPS65217_INT_MASK, |
49 | tps->irq_mask, TPS65217_PROTECT_NONE); | |
6556bdac MN |
50 | if (ret != 0) |
51 | dev_err(tps->dev, "Failed to sync IRQ masks\n"); | |
52 | ||
53 | mutex_unlock(&tps->irq_lock); | |
54 | } | |
55 | ||
6556bdac MN |
56 | static void tps65217_irq_enable(struct irq_data *data) |
57 | { | |
58 | struct tps65217 *tps = irq_data_get_irq_chip_data(data); | |
fa917052 | 59 | u8 mask = BIT(data->hwirq) << TPS65217_INT_SHIFT; |
6556bdac | 60 | |
fa917052 | 61 | tps->irq_mask &= ~mask; |
6556bdac MN |
62 | } |
63 | ||
64 | static void tps65217_irq_disable(struct irq_data *data) | |
65 | { | |
66 | struct tps65217 *tps = irq_data_get_irq_chip_data(data); | |
fa917052 | 67 | u8 mask = BIT(data->hwirq) << TPS65217_INT_SHIFT; |
6556bdac | 68 | |
fa917052 | 69 | tps->irq_mask |= mask; |
6556bdac MN |
70 | } |
71 | ||
72 | static struct irq_chip tps65217_irq_chip = { | |
f6602064 | 73 | .name = "tps65217", |
6556bdac MN |
74 | .irq_bus_lock = tps65217_irq_lock, |
75 | .irq_bus_sync_unlock = tps65217_irq_sync_unlock, | |
76 | .irq_enable = tps65217_irq_enable, | |
77 | .irq_disable = tps65217_irq_disable, | |
78 | }; | |
79 | ||
80 | static struct mfd_cell tps65217s[] = { | |
817bb7fb AC |
81 | { |
82 | .name = "tps65217-pmic", | |
11d0d300 | 83 | .of_compatible = "ti,tps65217-pmic", |
817bb7fb | 84 | }, |
b6290ffe MK |
85 | { |
86 | .name = "tps65217-bl", | |
11d0d300 | 87 | .of_compatible = "ti,tps65217-bl", |
b6290ffe | 88 | }, |
55cec67a EBS |
89 | { |
90 | .name = "tps65217-charger", | |
6556bdac MN |
91 | .num_resources = ARRAY_SIZE(charger_resources), |
92 | .resources = charger_resources, | |
55cec67a EBS |
93 | .of_compatible = "ti,tps65217-charger", |
94 | }, | |
dea9c730 MN |
95 | { |
96 | .name = "tps65217-pwrbutton", | |
97 | .num_resources = ARRAY_SIZE(pb_resources), | |
98 | .resources = pb_resources, | |
99 | .of_compatible = "ti,tps65217-pwrbutton", | |
100 | }, | |
817bb7fb AC |
101 | }; |
102 | ||
6556bdac MN |
103 | static irqreturn_t tps65217_irq_thread(int irq, void *data) |
104 | { | |
105 | struct tps65217 *tps = data; | |
106 | unsigned int status; | |
107 | bool handled = false; | |
108 | int i; | |
109 | int ret; | |
110 | ||
111 | ret = tps65217_reg_read(tps, TPS65217_REG_INT, &status); | |
112 | if (ret < 0) { | |
113 | dev_err(tps->dev, "Failed to read IRQ status: %d\n", | |
114 | ret); | |
115 | return IRQ_NONE; | |
116 | } | |
117 | ||
fa917052 MK |
118 | for (i = 0; i < TPS65217_NUM_IRQ; i++) { |
119 | if (status & BIT(i)) { | |
6556bdac MN |
120 | handle_nested_irq(irq_find_mapping(tps->irq_domain, i)); |
121 | handled = true; | |
122 | } | |
123 | } | |
124 | ||
125 | if (handled) | |
126 | return IRQ_HANDLED; | |
127 | ||
128 | return IRQ_NONE; | |
129 | } | |
130 | ||
131 | static int tps65217_irq_map(struct irq_domain *h, unsigned int virq, | |
132 | irq_hw_number_t hw) | |
133 | { | |
134 | struct tps65217 *tps = h->host_data; | |
135 | ||
136 | irq_set_chip_data(virq, tps); | |
137 | irq_set_chip_and_handler(virq, &tps65217_irq_chip, handle_edge_irq); | |
138 | irq_set_nested_thread(virq, 1); | |
139 | irq_set_parent(virq, tps->irq); | |
140 | irq_set_noprobe(virq); | |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
145 | static const struct irq_domain_ops tps65217_irq_domain_ops = { | |
146 | .map = tps65217_irq_map, | |
147 | }; | |
148 | ||
149 | static int tps65217_irq_init(struct tps65217 *tps, int irq) | |
150 | { | |
151 | int ret; | |
152 | ||
153 | mutex_init(&tps->irq_lock); | |
154 | tps->irq = irq; | |
155 | ||
156 | /* Mask all interrupt sources */ | |
6d2c2b9f MK |
157 | tps->irq_mask = TPS65217_INT_MASK; |
158 | tps65217_set_bits(tps, TPS65217_REG_INT, TPS65217_INT_MASK, | |
159 | TPS65217_INT_MASK, TPS65217_PROTECT_NONE); | |
6556bdac MN |
160 | |
161 | tps->irq_domain = irq_domain_add_linear(tps->dev->of_node, | |
162 | TPS65217_NUM_IRQ, &tps65217_irq_domain_ops, tps); | |
163 | if (!tps->irq_domain) { | |
164 | dev_err(tps->dev, "Could not create IRQ domain\n"); | |
165 | return -ENOMEM; | |
166 | } | |
167 | ||
168 | ret = devm_request_threaded_irq(tps->dev, irq, NULL, | |
169 | tps65217_irq_thread, IRQF_ONESHOT, | |
170 | "tps65217-irq", tps); | |
171 | if (ret) { | |
172 | dev_err(tps->dev, "Failed to request IRQ %d: %d\n", | |
173 | irq, ret); | |
174 | return ret; | |
175 | } | |
176 | ||
93559191 MK |
177 | enable_irq_wake(irq); |
178 | ||
6556bdac MN |
179 | return 0; |
180 | } | |
181 | ||
d48f411c AC |
182 | /** |
183 | * tps65217_reg_read: Read a single tps65217 register. | |
184 | * | |
185 | * @tps: Device to read from. | |
186 | * @reg: Register to read. | |
187 | * @val: Contians the value | |
188 | */ | |
189 | int tps65217_reg_read(struct tps65217 *tps, unsigned int reg, | |
190 | unsigned int *val) | |
191 | { | |
192 | return regmap_read(tps->regmap, reg, val); | |
193 | } | |
194 | EXPORT_SYMBOL_GPL(tps65217_reg_read); | |
195 | ||
196 | /** | |
197 | * tps65217_reg_write: Write a single tps65217 register. | |
198 | * | |
4976bfb8 | 199 | * @tps: Device to write to. |
d48f411c AC |
200 | * @reg: Register to write to. |
201 | * @val: Value to write. | |
202 | * @level: Password protected level | |
203 | */ | |
204 | int tps65217_reg_write(struct tps65217 *tps, unsigned int reg, | |
205 | unsigned int val, unsigned int level) | |
206 | { | |
207 | int ret; | |
208 | unsigned int xor_reg_val; | |
209 | ||
210 | switch (level) { | |
211 | case TPS65217_PROTECT_NONE: | |
212 | return regmap_write(tps->regmap, reg, val); | |
213 | case TPS65217_PROTECT_L1: | |
214 | xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK; | |
215 | ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD, | |
216 | xor_reg_val); | |
217 | if (ret < 0) | |
218 | return ret; | |
219 | ||
220 | return regmap_write(tps->regmap, reg, val); | |
221 | case TPS65217_PROTECT_L2: | |
222 | xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK; | |
223 | ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD, | |
224 | xor_reg_val); | |
225 | if (ret < 0) | |
226 | return ret; | |
227 | ret = regmap_write(tps->regmap, reg, val); | |
228 | if (ret < 0) | |
229 | return ret; | |
230 | ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD, | |
231 | xor_reg_val); | |
232 | if (ret < 0) | |
233 | return ret; | |
234 | return regmap_write(tps->regmap, reg, val); | |
235 | default: | |
236 | return -EINVAL; | |
237 | } | |
238 | } | |
239 | EXPORT_SYMBOL_GPL(tps65217_reg_write); | |
240 | ||
241 | /** | |
242 | * tps65217_update_bits: Modify bits w.r.t mask, val and level. | |
243 | * | |
4976bfb8 | 244 | * @tps: Device to write to. |
d48f411c AC |
245 | * @reg: Register to read-write to. |
246 | * @mask: Mask. | |
247 | * @val: Value to write. | |
248 | * @level: Password protected level | |
249 | */ | |
27757e82 | 250 | static int tps65217_update_bits(struct tps65217 *tps, unsigned int reg, |
d48f411c AC |
251 | unsigned int mask, unsigned int val, unsigned int level) |
252 | { | |
253 | int ret; | |
254 | unsigned int data; | |
255 | ||
256 | ret = tps65217_reg_read(tps, reg, &data); | |
257 | if (ret) { | |
258 | dev_err(tps->dev, "Read from reg 0x%x failed\n", reg); | |
259 | return ret; | |
260 | } | |
261 | ||
262 | data &= ~mask; | |
263 | data |= val & mask; | |
264 | ||
265 | ret = tps65217_reg_write(tps, reg, data, level); | |
266 | if (ret) | |
267 | dev_err(tps->dev, "Write for reg 0x%x failed\n", reg); | |
268 | ||
269 | return ret; | |
270 | } | |
271 | ||
272 | int tps65217_set_bits(struct tps65217 *tps, unsigned int reg, | |
273 | unsigned int mask, unsigned int val, unsigned int level) | |
274 | { | |
275 | return tps65217_update_bits(tps, reg, mask, val, level); | |
276 | } | |
277 | EXPORT_SYMBOL_GPL(tps65217_set_bits); | |
278 | ||
279 | int tps65217_clear_bits(struct tps65217 *tps, unsigned int reg, | |
280 | unsigned int mask, unsigned int level) | |
281 | { | |
282 | return tps65217_update_bits(tps, reg, mask, 0, level); | |
283 | } | |
284 | EXPORT_SYMBOL_GPL(tps65217_clear_bits); | |
285 | ||
6556bdac MN |
286 | static bool tps65217_volatile_reg(struct device *dev, unsigned int reg) |
287 | { | |
288 | switch (reg) { | |
289 | case TPS65217_REG_INT: | |
290 | return true; | |
291 | default: | |
292 | return false; | |
293 | } | |
294 | } | |
295 | ||
af0a837d | 296 | static const struct regmap_config tps65217_regmap_config = { |
d48f411c AC |
297 | .reg_bits = 8, |
298 | .val_bits = 8, | |
0b496b4c MB |
299 | |
300 | .max_register = TPS65217_REG_MAX, | |
6556bdac | 301 | .volatile_reg = tps65217_volatile_reg, |
d48f411c AC |
302 | }; |
303 | ||
817bb7fb | 304 | static const struct of_device_id tps65217_of_match[] = { |
511cb174 | 305 | { .compatible = "ti,tps65217"}, |
817bb7fb AC |
306 | { /* sentinel */ }, |
307 | }; | |
4895e493 | 308 | MODULE_DEVICE_TABLE(of, tps65217_of_match); |
817bb7fb | 309 | |
511cb174 | 310 | static int tps65217_probe(struct i2c_client *client) |
d48f411c AC |
311 | { |
312 | struct tps65217 *tps; | |
d48f411c | 313 | unsigned int version; |
eb433dad | 314 | bool status_off = false; |
817bb7fb | 315 | int ret; |
d48f411c | 316 | |
511cb174 K |
317 | status_off = of_property_read_bool(client->dev.of_node, |
318 | "ti,pmic-shutdown-controller"); | |
a7f1b63e | 319 | |
d48f411c AC |
320 | tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL); |
321 | if (!tps) | |
322 | return -ENOMEM; | |
323 | ||
817bb7fb AC |
324 | i2c_set_clientdata(client, tps); |
325 | tps->dev = &client->dev; | |
817bb7fb | 326 | |
0ef4619c | 327 | tps->regmap = devm_regmap_init_i2c(client, &tps65217_regmap_config); |
d48f411c AC |
328 | if (IS_ERR(tps->regmap)) { |
329 | ret = PTR_ERR(tps->regmap); | |
330 | dev_err(tps->dev, "Failed to allocate register map: %d\n", | |
331 | ret); | |
332 | return ret; | |
333 | } | |
334 | ||
6556bdac MN |
335 | if (client->irq) { |
336 | tps65217_irq_init(tps, client->irq); | |
337 | } else { | |
338 | int i; | |
339 | ||
340 | /* Don't tell children about IRQ resources which won't fire */ | |
341 | for (i = 0; i < ARRAY_SIZE(tps65217s); i++) | |
342 | tps65217s[i].num_resources = 0; | |
343 | } | |
344 | ||
b89b6b6b | 345 | ret = devm_mfd_add_devices(tps->dev, -1, tps65217s, |
6556bdac MN |
346 | ARRAY_SIZE(tps65217s), NULL, 0, |
347 | tps->irq_domain); | |
817bb7fb AC |
348 | if (ret < 0) { |
349 | dev_err(tps->dev, "mfd_add_devices failed: %d\n", ret); | |
350 | return ret; | |
351 | } | |
d48f411c AC |
352 | |
353 | ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version); | |
354 | if (ret < 0) { | |
0ef4619c AL |
355 | dev_err(tps->dev, "Failed to read revision register: %d\n", |
356 | ret); | |
357 | return ret; | |
d48f411c AC |
358 | } |
359 | ||
eb433dad CFP |
360 | /* Set the PMIC to shutdown on PWR_EN toggle */ |
361 | if (status_off) { | |
362 | ret = tps65217_set_bits(tps, TPS65217_REG_STATUS, | |
363 | TPS65217_STATUS_OFF, TPS65217_STATUS_OFF, | |
364 | TPS65217_PROTECT_NONE); | |
365 | if (ret) | |
366 | dev_warn(tps->dev, "unable to set the status OFF\n"); | |
367 | } | |
368 | ||
d48f411c AC |
369 | dev_info(tps->dev, "TPS65217 ID %#x version 1.%d\n", |
370 | (version & TPS65217_CHIPID_CHIP_MASK) >> 4, | |
371 | version & TPS65217_CHIPID_REV_MASK); | |
372 | ||
d48f411c | 373 | return 0; |
d48f411c AC |
374 | } |
375 | ||
ed5c2f5f | 376 | static void tps65217_remove(struct i2c_client *client) |
40a50f8b MK |
377 | { |
378 | struct tps65217 *tps = i2c_get_clientdata(client); | |
379 | unsigned int virq; | |
380 | int i; | |
381 | ||
fa917052 | 382 | for (i = 0; i < TPS65217_NUM_IRQ; i++) { |
40a50f8b MK |
383 | virq = irq_find_mapping(tps->irq_domain, i); |
384 | if (virq) | |
385 | irq_dispose_mapping(virq); | |
386 | } | |
387 | ||
388 | irq_domain_remove(tps->irq_domain); | |
389 | tps->irq_domain = NULL; | |
40a50f8b MK |
390 | } |
391 | ||
d48f411c | 392 | static const struct i2c_device_id tps65217_id_table[] = { |
817bb7fb AC |
393 | {"tps65217", TPS65217}, |
394 | { /* sentinel */ } | |
d48f411c AC |
395 | }; |
396 | MODULE_DEVICE_TABLE(i2c, tps65217_id_table); | |
397 | ||
398 | static struct i2c_driver tps65217_driver = { | |
399 | .driver = { | |
400 | .name = "tps65217", | |
a351451a | 401 | .of_match_table = tps65217_of_match, |
d48f411c AC |
402 | }, |
403 | .id_table = tps65217_id_table, | |
9816d859 | 404 | .probe = tps65217_probe, |
40a50f8b | 405 | .remove = tps65217_remove, |
d48f411c AC |
406 | }; |
407 | ||
408 | static int __init tps65217_init(void) | |
409 | { | |
410 | return i2c_add_driver(&tps65217_driver); | |
411 | } | |
412 | subsys_initcall(tps65217_init); | |
413 | ||
414 | static void __exit tps65217_exit(void) | |
415 | { | |
416 | i2c_del_driver(&tps65217_driver); | |
417 | } | |
418 | module_exit(tps65217_exit); | |
419 | ||
420 | MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>"); | |
421 | MODULE_DESCRIPTION("TPS65217 chip family multi-function driver"); | |
422 | MODULE_LICENSE("GPL v2"); |