blob: df2e7756927f20fd17ab07688cd6c4ba4a39fbc3 [file] [log] [blame]
AnilKumar Chd48f4112012-01-11 16:11:41 +05301/*
2 * tps65217.c
3 *
4 * TPS65217 chip family multi-function driver
5 *
6 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
11 *
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
AnilKumar Chd48f4112012-01-11 16:11:41 +053018#include <linux/device.h>
AnilKumar Chd48f4112012-01-11 16:11:41 +053019#include <linux/err.h>
Marcin Niestroj6556bda2016-09-09 10:42:02 +020020#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/i2c.h>
23#include <linux/irq.h>
24#include <linux/irqdomain.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
AnilKumar Ch817bb7f2012-08-13 20:36:05 +053027#include <linux/of.h>
28#include <linux/of_device.h>
Marcin Niestroj6556bda2016-09-09 10:42:02 +020029#include <linux/platform_device.h>
30#include <linux/regmap.h>
31#include <linux/slab.h>
AnilKumar Chd48f4112012-01-11 16:11:41 +053032
33#include <linux/mfd/core.h>
34#include <linux/mfd/tps65217.h>
35
Marcin Niestroj6556bda2016-09-09 10:42:02 +020036static struct resource charger_resources[] = {
37 DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_AC, "AC"),
38 DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_USB, "USB"),
39};
40
Marcin Niestrojdea9c732016-09-09 10:42:03 +020041static struct resource pb_resources[] = {
42 DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_PB, "PB"),
43};
44
Marcin Niestroj6556bda2016-09-09 10:42:02 +020045struct tps65217_irq {
46 int mask;
47 int interrupt;
48};
49
50static const struct tps65217_irq tps65217_irqs[] = {
51 [TPS65217_IRQ_PB] = {
52 .mask = TPS65217_INT_PBM,
53 .interrupt = TPS65217_INT_PBI,
54 },
55 [TPS65217_IRQ_AC] = {
56 .mask = TPS65217_INT_ACM,
57 .interrupt = TPS65217_INT_ACI,
58 },
59 [TPS65217_IRQ_USB] = {
60 .mask = TPS65217_INT_USBM,
61 .interrupt = TPS65217_INT_USBI,
62 },
63};
64
65static void tps65217_irq_lock(struct irq_data *data)
66{
67 struct tps65217 *tps = irq_data_get_irq_chip_data(data);
68
69 mutex_lock(&tps->irq_lock);
70}
71
72static void tps65217_irq_sync_unlock(struct irq_data *data)
73{
74 struct tps65217 *tps = irq_data_get_irq_chip_data(data);
75 int ret;
76
77 ret = tps65217_reg_write(tps, TPS65217_REG_INT, tps->irq_mask,
78 TPS65217_PROTECT_NONE);
79 if (ret != 0)
80 dev_err(tps->dev, "Failed to sync IRQ masks\n");
81
82 mutex_unlock(&tps->irq_lock);
83}
84
Arnd Bergmannb4feabe2016-09-14 10:13:28 +020085static inline const struct tps65217_irq *
Marcin Niestroj6556bda2016-09-09 10:42:02 +020086irq_to_tps65217_irq(struct tps65217 *tps, struct irq_data *data)
87{
88 return &tps65217_irqs[data->hwirq];
89}
90
91static void tps65217_irq_enable(struct irq_data *data)
92{
93 struct tps65217 *tps = irq_data_get_irq_chip_data(data);
94 const struct tps65217_irq *irq_data = irq_to_tps65217_irq(tps, data);
95
96 tps->irq_mask &= ~irq_data->mask;
97}
98
99static void tps65217_irq_disable(struct irq_data *data)
100{
101 struct tps65217 *tps = irq_data_get_irq_chip_data(data);
102 const struct tps65217_irq *irq_data = irq_to_tps65217_irq(tps, data);
103
104 tps->irq_mask |= irq_data->mask;
105}
106
107static struct irq_chip tps65217_irq_chip = {
108 .irq_bus_lock = tps65217_irq_lock,
109 .irq_bus_sync_unlock = tps65217_irq_sync_unlock,
110 .irq_enable = tps65217_irq_enable,
111 .irq_disable = tps65217_irq_disable,
112};
113
114static struct mfd_cell tps65217s[] = {
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530115 {
116 .name = "tps65217-pmic",
Johannes Pointner11d0d302014-09-25 08:31:42 +0200117 .of_compatible = "ti,tps65217-pmic",
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530118 },
Matthias Kaehlckeb6290ff2012-09-24 22:25:34 +0200119 {
120 .name = "tps65217-bl",
Johannes Pointner11d0d302014-09-25 08:31:42 +0200121 .of_compatible = "ti,tps65217-bl",
Matthias Kaehlckeb6290ff2012-09-24 22:25:34 +0200122 },
Enric Balletbo i Serra55cec672015-09-08 10:09:39 +0200123 {
124 .name = "tps65217-charger",
Marcin Niestroj6556bda2016-09-09 10:42:02 +0200125 .num_resources = ARRAY_SIZE(charger_resources),
126 .resources = charger_resources,
Enric Balletbo i Serra55cec672015-09-08 10:09:39 +0200127 .of_compatible = "ti,tps65217-charger",
128 },
Marcin Niestrojdea9c732016-09-09 10:42:03 +0200129 {
130 .name = "tps65217-pwrbutton",
131 .num_resources = ARRAY_SIZE(pb_resources),
132 .resources = pb_resources,
133 .of_compatible = "ti,tps65217-pwrbutton",
134 },
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530135};
136
Marcin Niestroj6556bda2016-09-09 10:42:02 +0200137static irqreturn_t tps65217_irq_thread(int irq, void *data)
138{
139 struct tps65217 *tps = data;
140 unsigned int status;
141 bool handled = false;
142 int i;
143 int ret;
144
145 ret = tps65217_reg_read(tps, TPS65217_REG_INT, &status);
146 if (ret < 0) {
147 dev_err(tps->dev, "Failed to read IRQ status: %d\n",
148 ret);
149 return IRQ_NONE;
150 }
151
152 for (i = 0; i < ARRAY_SIZE(tps65217_irqs); i++) {
153 if (status & tps65217_irqs[i].interrupt) {
154 handle_nested_irq(irq_find_mapping(tps->irq_domain, i));
155 handled = true;
156 }
157 }
158
159 if (handled)
160 return IRQ_HANDLED;
161
162 return IRQ_NONE;
163}
164
165static int tps65217_irq_map(struct irq_domain *h, unsigned int virq,
166 irq_hw_number_t hw)
167{
168 struct tps65217 *tps = h->host_data;
169
170 irq_set_chip_data(virq, tps);
171 irq_set_chip_and_handler(virq, &tps65217_irq_chip, handle_edge_irq);
172 irq_set_nested_thread(virq, 1);
173 irq_set_parent(virq, tps->irq);
174 irq_set_noprobe(virq);
175
176 return 0;
177}
178
179static const struct irq_domain_ops tps65217_irq_domain_ops = {
180 .map = tps65217_irq_map,
181};
182
183static int tps65217_irq_init(struct tps65217 *tps, int irq)
184{
185 int ret;
186
187 mutex_init(&tps->irq_lock);
188 tps->irq = irq;
189
190 /* Mask all interrupt sources */
191 tps->irq_mask = (TPS65217_INT_RESERVEDM | TPS65217_INT_PBM
192 | TPS65217_INT_ACM | TPS65217_INT_USBM);
193 tps65217_reg_write(tps, TPS65217_REG_INT, tps->irq_mask,
194 TPS65217_PROTECT_NONE);
195
196 tps->irq_domain = irq_domain_add_linear(tps->dev->of_node,
197 TPS65217_NUM_IRQ, &tps65217_irq_domain_ops, tps);
198 if (!tps->irq_domain) {
199 dev_err(tps->dev, "Could not create IRQ domain\n");
200 return -ENOMEM;
201 }
202
203 ret = devm_request_threaded_irq(tps->dev, irq, NULL,
204 tps65217_irq_thread, IRQF_ONESHOT,
205 "tps65217-irq", tps);
206 if (ret) {
207 dev_err(tps->dev, "Failed to request IRQ %d: %d\n",
208 irq, ret);
209 return ret;
210 }
211
212 return 0;
213}
214
AnilKumar Chd48f4112012-01-11 16:11:41 +0530215/**
216 * tps65217_reg_read: Read a single tps65217 register.
217 *
218 * @tps: Device to read from.
219 * @reg: Register to read.
220 * @val: Contians the value
221 */
222int tps65217_reg_read(struct tps65217 *tps, unsigned int reg,
223 unsigned int *val)
224{
225 return regmap_read(tps->regmap, reg, val);
226}
227EXPORT_SYMBOL_GPL(tps65217_reg_read);
228
229/**
230 * tps65217_reg_write: Write a single tps65217 register.
231 *
232 * @tps65217: Device to write to.
233 * @reg: Register to write to.
234 * @val: Value to write.
235 * @level: Password protected level
236 */
237int tps65217_reg_write(struct tps65217 *tps, unsigned int reg,
238 unsigned int val, unsigned int level)
239{
240 int ret;
241 unsigned int xor_reg_val;
242
243 switch (level) {
244 case TPS65217_PROTECT_NONE:
245 return regmap_write(tps->regmap, reg, val);
246 case TPS65217_PROTECT_L1:
247 xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
248 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
249 xor_reg_val);
250 if (ret < 0)
251 return ret;
252
253 return regmap_write(tps->regmap, reg, val);
254 case TPS65217_PROTECT_L2:
255 xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
256 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
257 xor_reg_val);
258 if (ret < 0)
259 return ret;
260 ret = regmap_write(tps->regmap, reg, val);
261 if (ret < 0)
262 return ret;
263 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
264 xor_reg_val);
265 if (ret < 0)
266 return ret;
267 return regmap_write(tps->regmap, reg, val);
268 default:
269 return -EINVAL;
270 }
271}
272EXPORT_SYMBOL_GPL(tps65217_reg_write);
273
274/**
275 * tps65217_update_bits: Modify bits w.r.t mask, val and level.
276 *
277 * @tps65217: Device to write to.
278 * @reg: Register to read-write to.
279 * @mask: Mask.
280 * @val: Value to write.
281 * @level: Password protected level
282 */
Mark Brown27757e82012-05-09 21:18:05 +0100283static int tps65217_update_bits(struct tps65217 *tps, unsigned int reg,
AnilKumar Chd48f4112012-01-11 16:11:41 +0530284 unsigned int mask, unsigned int val, unsigned int level)
285{
286 int ret;
287 unsigned int data;
288
289 ret = tps65217_reg_read(tps, reg, &data);
290 if (ret) {
291 dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
292 return ret;
293 }
294
295 data &= ~mask;
296 data |= val & mask;
297
298 ret = tps65217_reg_write(tps, reg, data, level);
299 if (ret)
300 dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
301
302 return ret;
303}
304
305int tps65217_set_bits(struct tps65217 *tps, unsigned int reg,
306 unsigned int mask, unsigned int val, unsigned int level)
307{
308 return tps65217_update_bits(tps, reg, mask, val, level);
309}
310EXPORT_SYMBOL_GPL(tps65217_set_bits);
311
312int tps65217_clear_bits(struct tps65217 *tps, unsigned int reg,
313 unsigned int mask, unsigned int level)
314{
315 return tps65217_update_bits(tps, reg, mask, 0, level);
316}
317EXPORT_SYMBOL_GPL(tps65217_clear_bits);
318
Marcin Niestroj6556bda2016-09-09 10:42:02 +0200319static bool tps65217_volatile_reg(struct device *dev, unsigned int reg)
320{
321 switch (reg) {
322 case TPS65217_REG_INT:
323 return true;
324 default:
325 return false;
326 }
327}
328
Krzysztof Kozlowskiaf0a8372015-01-05 10:01:30 +0100329static const struct regmap_config tps65217_regmap_config = {
AnilKumar Chd48f4112012-01-11 16:11:41 +0530330 .reg_bits = 8,
331 .val_bits = 8,
Mark Brown0b496b42014-09-05 22:16:18 +0100332
333 .max_register = TPS65217_REG_MAX,
Marcin Niestroj6556bda2016-09-09 10:42:02 +0200334 .volatile_reg = tps65217_volatile_reg,
AnilKumar Chd48f4112012-01-11 16:11:41 +0530335};
336
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530337static const struct of_device_id tps65217_of_match[] = {
338 { .compatible = "ti,tps65217", .data = (void *)TPS65217 },
339 { /* sentinel */ },
340};
Javier Martinez Canillas4895e492015-07-30 18:18:41 +0200341MODULE_DEVICE_TABLE(of, tps65217_of_match);
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530342
Bill Pembertonf791be42012-11-19 13:23:04 -0500343static int tps65217_probe(struct i2c_client *client,
AnilKumar Chd48f4112012-01-11 16:11:41 +0530344 const struct i2c_device_id *ids)
345{
346 struct tps65217 *tps;
AnilKumar Chd48f4112012-01-11 16:11:41 +0530347 unsigned int version;
Lee Jones5c6fbd52014-02-03 08:24:20 +0000348 unsigned long chip_id = ids->driver_data;
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530349 const struct of_device_id *match;
Colin Foe-Parkereb433da2012-11-20 15:18:44 +0530350 bool status_off = false;
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530351 int ret;
AnilKumar Chd48f4112012-01-11 16:11:41 +0530352
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530353 if (client->dev.of_node) {
354 match = of_match_device(tps65217_of_match, &client->dev);
355 if (!match) {
356 dev_err(&client->dev,
357 "Failed to find matching dt id\n");
358 return -EINVAL;
359 }
Lee Jones5c6fbd52014-02-03 08:24:20 +0000360 chip_id = (unsigned long)match->data;
Colin Foe-Parkereb433da2012-11-20 15:18:44 +0530361 status_off = of_property_read_bool(client->dev.of_node,
362 "ti,pmic-shutdown-controller");
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530363 }
364
365 if (!chip_id) {
366 dev_err(&client->dev, "id is null.\n");
367 return -ENODEV;
368 }
AnilKumar Cha7f1b632012-07-10 16:39:42 +0530369
AnilKumar Chd48f4112012-01-11 16:11:41 +0530370 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
371 if (!tps)
372 return -ENOMEM;
373
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530374 i2c_set_clientdata(client, tps);
375 tps->dev = &client->dev;
376 tps->id = chip_id;
377
Axel Lin0ef46192012-04-25 10:06:40 +0800378 tps->regmap = devm_regmap_init_i2c(client, &tps65217_regmap_config);
AnilKumar Chd48f4112012-01-11 16:11:41 +0530379 if (IS_ERR(tps->regmap)) {
380 ret = PTR_ERR(tps->regmap);
381 dev_err(tps->dev, "Failed to allocate register map: %d\n",
382 ret);
383 return ret;
384 }
385
Marcin Niestroj6556bda2016-09-09 10:42:02 +0200386 if (client->irq) {
387 tps65217_irq_init(tps, client->irq);
388 } else {
389 int i;
390
391 /* Don't tell children about IRQ resources which won't fire */
392 for (i = 0; i < ARRAY_SIZE(tps65217s); i++)
393 tps65217s[i].num_resources = 0;
394 }
395
Laxman Dewanganb89b6b62016-04-08 00:13:12 +0530396 ret = devm_mfd_add_devices(tps->dev, -1, tps65217s,
Marcin Niestroj6556bda2016-09-09 10:42:02 +0200397 ARRAY_SIZE(tps65217s), NULL, 0,
398 tps->irq_domain);
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530399 if (ret < 0) {
400 dev_err(tps->dev, "mfd_add_devices failed: %d\n", ret);
401 return ret;
402 }
AnilKumar Chd48f4112012-01-11 16:11:41 +0530403
404 ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version);
405 if (ret < 0) {
Axel Lin0ef46192012-04-25 10:06:40 +0800406 dev_err(tps->dev, "Failed to read revision register: %d\n",
407 ret);
408 return ret;
AnilKumar Chd48f4112012-01-11 16:11:41 +0530409 }
410
Colin Foe-Parkereb433da2012-11-20 15:18:44 +0530411 /* Set the PMIC to shutdown on PWR_EN toggle */
412 if (status_off) {
413 ret = tps65217_set_bits(tps, TPS65217_REG_STATUS,
414 TPS65217_STATUS_OFF, TPS65217_STATUS_OFF,
415 TPS65217_PROTECT_NONE);
416 if (ret)
417 dev_warn(tps->dev, "unable to set the status OFF\n");
418 }
419
AnilKumar Chd48f4112012-01-11 16:11:41 +0530420 dev_info(tps->dev, "TPS65217 ID %#x version 1.%d\n",
421 (version & TPS65217_CHIPID_CHIP_MASK) >> 4,
422 version & TPS65217_CHIPID_REV_MASK);
423
AnilKumar Chd48f4112012-01-11 16:11:41 +0530424 return 0;
AnilKumar Chd48f4112012-01-11 16:11:41 +0530425}
426
Milo Kimac863122016-11-15 22:02:11 +0900427static int tps65217_remove(struct i2c_client *client)
428{
429 struct tps65217 *tps = i2c_get_clientdata(client);
430 unsigned int virq;
431 int i;
432
433 for (i = 0; i < ARRAY_SIZE(tps65217_irqs); i++) {
434 virq = irq_find_mapping(tps->irq_domain, i);
435 if (virq)
436 irq_dispose_mapping(virq);
437 }
438
439 irq_domain_remove(tps->irq_domain);
440 tps->irq_domain = NULL;
441
442 return 0;
443}
444
AnilKumar Chd48f4112012-01-11 16:11:41 +0530445static const struct i2c_device_id tps65217_id_table[] = {
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530446 {"tps65217", TPS65217},
447 { /* sentinel */ }
AnilKumar Chd48f4112012-01-11 16:11:41 +0530448};
449MODULE_DEVICE_TABLE(i2c, tps65217_id_table);
450
451static struct i2c_driver tps65217_driver = {
452 .driver = {
453 .name = "tps65217",
Sachin Kamata3514512013-10-15 09:18:47 +0530454 .of_match_table = tps65217_of_match,
AnilKumar Chd48f4112012-01-11 16:11:41 +0530455 },
456 .id_table = tps65217_id_table,
457 .probe = tps65217_probe,
Milo Kimac863122016-11-15 22:02:11 +0900458 .remove = tps65217_remove,
AnilKumar Chd48f4112012-01-11 16:11:41 +0530459};
460
461static int __init tps65217_init(void)
462{
463 return i2c_add_driver(&tps65217_driver);
464}
465subsys_initcall(tps65217_init);
466
467static void __exit tps65217_exit(void)
468{
469 i2c_del_driver(&tps65217_driver);
470}
471module_exit(tps65217_exit);
472
473MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
474MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
475MODULE_LICENSE("GPL v2");