4 * TPS65217 chip family multi-function driver
6 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
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.
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.
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #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>
28 #include <linux/of_device.h>
29 #include <linux/platform_device.h>
30 #include <linux/regmap.h>
31 #include <linux/slab.h>
33 #include <linux/mfd/core.h>
34 #include <linux/mfd/tps65217.h>
36 static struct resource charger_resources[] = {
37 DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_AC, "AC"),
38 DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_USB, "USB"),
46 static const struct tps65217_irq tps65217_irqs[] = {
48 .mask = TPS65217_INT_PBM,
49 .interrupt = TPS65217_INT_PBI,
52 .mask = TPS65217_INT_ACM,
53 .interrupt = TPS65217_INT_ACI,
55 [TPS65217_IRQ_USB] = {
56 .mask = TPS65217_INT_USBM,
57 .interrupt = TPS65217_INT_USBI,
61 static void tps65217_irq_lock(struct irq_data *data)
63 struct tps65217 *tps = irq_data_get_irq_chip_data(data);
65 mutex_lock(&tps->irq_lock);
68 static void tps65217_irq_sync_unlock(struct irq_data *data)
70 struct tps65217 *tps = irq_data_get_irq_chip_data(data);
73 ret = tps65217_reg_write(tps, TPS65217_REG_INT, tps->irq_mask,
74 TPS65217_PROTECT_NONE);
76 dev_err(tps->dev, "Failed to sync IRQ masks\n");
78 mutex_unlock(&tps->irq_lock);
81 static const inline struct tps65217_irq *
82 irq_to_tps65217_irq(struct tps65217 *tps, struct irq_data *data)
84 return &tps65217_irqs[data->hwirq];
87 static void tps65217_irq_enable(struct irq_data *data)
89 struct tps65217 *tps = irq_data_get_irq_chip_data(data);
90 const struct tps65217_irq *irq_data = irq_to_tps65217_irq(tps, data);
92 tps->irq_mask &= ~irq_data->mask;
95 static void tps65217_irq_disable(struct irq_data *data)
97 struct tps65217 *tps = irq_data_get_irq_chip_data(data);
98 const struct tps65217_irq *irq_data = irq_to_tps65217_irq(tps, data);
100 tps->irq_mask |= irq_data->mask;
103 static struct irq_chip tps65217_irq_chip = {
104 .irq_bus_lock = tps65217_irq_lock,
105 .irq_bus_sync_unlock = tps65217_irq_sync_unlock,
106 .irq_enable = tps65217_irq_enable,
107 .irq_disable = tps65217_irq_disable,
110 static struct mfd_cell tps65217s[] = {
112 .name = "tps65217-pmic",
113 .of_compatible = "ti,tps65217-pmic",
116 .name = "tps65217-bl",
117 .of_compatible = "ti,tps65217-bl",
120 .name = "tps65217-charger",
121 .num_resources = ARRAY_SIZE(charger_resources),
122 .resources = charger_resources,
123 .of_compatible = "ti,tps65217-charger",
127 static irqreturn_t tps65217_irq_thread(int irq, void *data)
129 struct tps65217 *tps = data;
131 bool handled = false;
135 ret = tps65217_reg_read(tps, TPS65217_REG_INT, &status);
137 dev_err(tps->dev, "Failed to read IRQ status: %d\n",
142 for (i = 0; i < ARRAY_SIZE(tps65217_irqs); i++) {
143 if (status & tps65217_irqs[i].interrupt) {
144 handle_nested_irq(irq_find_mapping(tps->irq_domain, i));
155 static int tps65217_irq_map(struct irq_domain *h, unsigned int virq,
158 struct tps65217 *tps = h->host_data;
160 irq_set_chip_data(virq, tps);
161 irq_set_chip_and_handler(virq, &tps65217_irq_chip, handle_edge_irq);
162 irq_set_nested_thread(virq, 1);
163 irq_set_parent(virq, tps->irq);
164 irq_set_noprobe(virq);
169 static const struct irq_domain_ops tps65217_irq_domain_ops = {
170 .map = tps65217_irq_map,
173 static int tps65217_irq_init(struct tps65217 *tps, int irq)
177 mutex_init(&tps->irq_lock);
180 /* Mask all interrupt sources */
181 tps->irq_mask = (TPS65217_INT_RESERVEDM | TPS65217_INT_PBM
182 | TPS65217_INT_ACM | TPS65217_INT_USBM);
183 tps65217_reg_write(tps, TPS65217_REG_INT, tps->irq_mask,
184 TPS65217_PROTECT_NONE);
186 tps->irq_domain = irq_domain_add_linear(tps->dev->of_node,
187 TPS65217_NUM_IRQ, &tps65217_irq_domain_ops, tps);
188 if (!tps->irq_domain) {
189 dev_err(tps->dev, "Could not create IRQ domain\n");
193 ret = devm_request_threaded_irq(tps->dev, irq, NULL,
194 tps65217_irq_thread, IRQF_ONESHOT,
195 "tps65217-irq", tps);
197 dev_err(tps->dev, "Failed to request IRQ %d: %d\n",
206 * tps65217_reg_read: Read a single tps65217 register.
208 * @tps: Device to read from.
209 * @reg: Register to read.
210 * @val: Contians the value
212 int tps65217_reg_read(struct tps65217 *tps, unsigned int reg,
215 return regmap_read(tps->regmap, reg, val);
217 EXPORT_SYMBOL_GPL(tps65217_reg_read);
220 * tps65217_reg_write: Write a single tps65217 register.
222 * @tps65217: Device to write to.
223 * @reg: Register to write to.
224 * @val: Value to write.
225 * @level: Password protected level
227 int tps65217_reg_write(struct tps65217 *tps, unsigned int reg,
228 unsigned int val, unsigned int level)
231 unsigned int xor_reg_val;
234 case TPS65217_PROTECT_NONE:
235 return regmap_write(tps->regmap, reg, val);
236 case TPS65217_PROTECT_L1:
237 xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
238 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
243 return regmap_write(tps->regmap, reg, val);
244 case TPS65217_PROTECT_L2:
245 xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
246 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
250 ret = regmap_write(tps->regmap, reg, val);
253 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
257 return regmap_write(tps->regmap, reg, val);
262 EXPORT_SYMBOL_GPL(tps65217_reg_write);
265 * tps65217_update_bits: Modify bits w.r.t mask, val and level.
267 * @tps65217: Device to write to.
268 * @reg: Register to read-write to.
270 * @val: Value to write.
271 * @level: Password protected level
273 static int tps65217_update_bits(struct tps65217 *tps, unsigned int reg,
274 unsigned int mask, unsigned int val, unsigned int level)
279 ret = tps65217_reg_read(tps, reg, &data);
281 dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
288 ret = tps65217_reg_write(tps, reg, data, level);
290 dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
295 int tps65217_set_bits(struct tps65217 *tps, unsigned int reg,
296 unsigned int mask, unsigned int val, unsigned int level)
298 return tps65217_update_bits(tps, reg, mask, val, level);
300 EXPORT_SYMBOL_GPL(tps65217_set_bits);
302 int tps65217_clear_bits(struct tps65217 *tps, unsigned int reg,
303 unsigned int mask, unsigned int level)
305 return tps65217_update_bits(tps, reg, mask, 0, level);
307 EXPORT_SYMBOL_GPL(tps65217_clear_bits);
309 static bool tps65217_volatile_reg(struct device *dev, unsigned int reg)
312 case TPS65217_REG_INT:
319 static const struct regmap_config tps65217_regmap_config = {
323 .max_register = TPS65217_REG_MAX,
324 .volatile_reg = tps65217_volatile_reg,
327 static const struct of_device_id tps65217_of_match[] = {
328 { .compatible = "ti,tps65217", .data = (void *)TPS65217 },
331 MODULE_DEVICE_TABLE(of, tps65217_of_match);
333 static int tps65217_probe(struct i2c_client *client,
334 const struct i2c_device_id *ids)
336 struct tps65217 *tps;
337 unsigned int version;
338 unsigned long chip_id = ids->driver_data;
339 const struct of_device_id *match;
340 bool status_off = false;
343 if (client->dev.of_node) {
344 match = of_match_device(tps65217_of_match, &client->dev);
346 dev_err(&client->dev,
347 "Failed to find matching dt id\n");
350 chip_id = (unsigned long)match->data;
351 status_off = of_property_read_bool(client->dev.of_node,
352 "ti,pmic-shutdown-controller");
356 dev_err(&client->dev, "id is null.\n");
360 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
364 i2c_set_clientdata(client, tps);
365 tps->dev = &client->dev;
368 tps->regmap = devm_regmap_init_i2c(client, &tps65217_regmap_config);
369 if (IS_ERR(tps->regmap)) {
370 ret = PTR_ERR(tps->regmap);
371 dev_err(tps->dev, "Failed to allocate register map: %d\n",
377 tps65217_irq_init(tps, client->irq);
381 /* Don't tell children about IRQ resources which won't fire */
382 for (i = 0; i < ARRAY_SIZE(tps65217s); i++)
383 tps65217s[i].num_resources = 0;
386 ret = devm_mfd_add_devices(tps->dev, -1, tps65217s,
387 ARRAY_SIZE(tps65217s), NULL, 0,
390 dev_err(tps->dev, "mfd_add_devices failed: %d\n", ret);
394 ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version);
396 dev_err(tps->dev, "Failed to read revision register: %d\n",
401 /* Set the PMIC to shutdown on PWR_EN toggle */
403 ret = tps65217_set_bits(tps, TPS65217_REG_STATUS,
404 TPS65217_STATUS_OFF, TPS65217_STATUS_OFF,
405 TPS65217_PROTECT_NONE);
407 dev_warn(tps->dev, "unable to set the status OFF\n");
410 dev_info(tps->dev, "TPS65217 ID %#x version 1.%d\n",
411 (version & TPS65217_CHIPID_CHIP_MASK) >> 4,
412 version & TPS65217_CHIPID_REV_MASK);
417 static const struct i2c_device_id tps65217_id_table[] = {
418 {"tps65217", TPS65217},
421 MODULE_DEVICE_TABLE(i2c, tps65217_id_table);
423 static struct i2c_driver tps65217_driver = {
426 .of_match_table = tps65217_of_match,
428 .id_table = tps65217_id_table,
429 .probe = tps65217_probe,
432 static int __init tps65217_init(void)
434 return i2c_add_driver(&tps65217_driver);
436 subsys_initcall(tps65217_init);
438 static void __exit tps65217_exit(void)
440 i2c_del_driver(&tps65217_driver);
442 module_exit(tps65217_exit);
444 MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
445 MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
446 MODULE_LICENSE("GPL v2");