Thomas Gleixner | 9952f69 | 2019-05-28 10:10:04 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Core driver for TI TPS65090 PMIC family |
| 4 | * |
| 5 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
Paul Gortmaker | 26fce5e | 2019-01-13 13:36:41 -0500 | [diff] [blame] | 6 | * |
| 7 | * Author: Venu Byravarasu <vbyravarasu@nvidia.com> |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/irq.h> |
| 12 | #include <linux/kernel.h> |
Paul Gortmaker | 26fce5e | 2019-01-13 13:36:41 -0500 | [diff] [blame] | 13 | #include <linux/init.h> |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 14 | #include <linux/mutex.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/mfd/core.h> |
| 18 | #include <linux/mfd/tps65090.h> |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 19 | #include <linux/of.h> |
| 20 | #include <linux/of_device.h> |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 21 | #include <linux/err.h> |
| 22 | |
| 23 | #define NUM_INT_REG 2 |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 24 | |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 25 | #define TPS65090_INT1_MASK_VAC_STATUS_CHANGE 1 |
| 26 | #define TPS65090_INT1_MASK_VSYS_STATUS_CHANGE 2 |
| 27 | #define TPS65090_INT1_MASK_BAT_STATUS_CHANGE 3 |
| 28 | #define TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE 4 |
| 29 | #define TPS65090_INT1_MASK_CHARGING_COMPLETE 5 |
| 30 | #define TPS65090_INT1_MASK_OVERLOAD_DCDC1 6 |
| 31 | #define TPS65090_INT1_MASK_OVERLOAD_DCDC2 7 |
| 32 | #define TPS65090_INT2_MASK_OVERLOAD_DCDC3 0 |
| 33 | #define TPS65090_INT2_MASK_OVERLOAD_FET1 1 |
| 34 | #define TPS65090_INT2_MASK_OVERLOAD_FET2 2 |
| 35 | #define TPS65090_INT2_MASK_OVERLOAD_FET3 3 |
| 36 | #define TPS65090_INT2_MASK_OVERLOAD_FET4 4 |
| 37 | #define TPS65090_INT2_MASK_OVERLOAD_FET5 5 |
| 38 | #define TPS65090_INT2_MASK_OVERLOAD_FET6 6 |
| 39 | #define TPS65090_INT2_MASK_OVERLOAD_FET7 7 |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 40 | |
Rikard Falkeborn | c4a164f | 2020-09-22 21:26:59 +0200 | [diff] [blame] | 41 | static const struct resource charger_resources[] = { |
Rhyland Klein | 36c772e | 2013-03-12 18:08:07 -0400 | [diff] [blame] | 42 | { |
| 43 | .start = TPS65090_IRQ_VAC_STATUS_CHANGE, |
| 44 | .end = TPS65090_IRQ_VAC_STATUS_CHANGE, |
| 45 | .flags = IORESOURCE_IRQ, |
| 46 | } |
| 47 | }; |
| 48 | |
Doug Anderson | 7d81177 | 2014-04-16 16:12:25 -0700 | [diff] [blame] | 49 | enum tps65090_cells { |
| 50 | PMIC = 0, |
| 51 | CHARGER = 1, |
| 52 | }; |
| 53 | |
| 54 | static struct mfd_cell tps65090s[] = { |
| 55 | [PMIC] = { |
Venu Byravarasu | b7e5378 | 2012-07-18 18:33:42 +0530 | [diff] [blame] | 56 | .name = "tps65090-pmic", |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 57 | }, |
Doug Anderson | 7d81177 | 2014-04-16 16:12:25 -0700 | [diff] [blame] | 58 | [CHARGER] = { |
Laxman Dewangan | e2e8ffc | 2012-11-20 08:44:45 +0530 | [diff] [blame] | 59 | .name = "tps65090-charger", |
Rhyland Klein | 36c772e | 2013-03-12 18:08:07 -0400 | [diff] [blame] | 60 | .num_resources = ARRAY_SIZE(charger_resources), |
| 61 | .resources = &charger_resources[0], |
Rhyland Klein | b50cf35 | 2013-04-10 13:14:55 -0400 | [diff] [blame] | 62 | .of_compatible = "ti,tps65090-charger", |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 63 | }, |
| 64 | }; |
| 65 | |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 66 | static const struct regmap_irq tps65090_irqs[] = { |
| 67 | /* INT1 IRQs*/ |
| 68 | [TPS65090_IRQ_VAC_STATUS_CHANGE] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 69 | .mask = TPS65090_INT1_MASK_VAC_STATUS_CHANGE, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 70 | }, |
| 71 | [TPS65090_IRQ_VSYS_STATUS_CHANGE] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 72 | .mask = TPS65090_INT1_MASK_VSYS_STATUS_CHANGE, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 73 | }, |
| 74 | [TPS65090_IRQ_BAT_STATUS_CHANGE] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 75 | .mask = TPS65090_INT1_MASK_BAT_STATUS_CHANGE, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 76 | }, |
| 77 | [TPS65090_IRQ_CHARGING_STATUS_CHANGE] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 78 | .mask = TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 79 | }, |
| 80 | [TPS65090_IRQ_CHARGING_COMPLETE] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 81 | .mask = TPS65090_INT1_MASK_CHARGING_COMPLETE, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 82 | }, |
| 83 | [TPS65090_IRQ_OVERLOAD_DCDC1] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 84 | .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC1, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 85 | }, |
| 86 | [TPS65090_IRQ_OVERLOAD_DCDC2] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 87 | .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC2, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 88 | }, |
| 89 | /* INT2 IRQs*/ |
| 90 | [TPS65090_IRQ_OVERLOAD_DCDC3] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 91 | .reg_offset = 1, |
| 92 | .mask = TPS65090_INT2_MASK_OVERLOAD_DCDC3, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 93 | }, |
| 94 | [TPS65090_IRQ_OVERLOAD_FET1] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 95 | .reg_offset = 1, |
| 96 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET1, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 97 | }, |
| 98 | [TPS65090_IRQ_OVERLOAD_FET2] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 99 | .reg_offset = 1, |
| 100 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET2, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 101 | }, |
| 102 | [TPS65090_IRQ_OVERLOAD_FET3] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 103 | .reg_offset = 1, |
| 104 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET3, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 105 | }, |
| 106 | [TPS65090_IRQ_OVERLOAD_FET4] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 107 | .reg_offset = 1, |
| 108 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET4, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 109 | }, |
| 110 | [TPS65090_IRQ_OVERLOAD_FET5] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 111 | .reg_offset = 1, |
| 112 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET5, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 113 | }, |
| 114 | [TPS65090_IRQ_OVERLOAD_FET6] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 115 | .reg_offset = 1, |
| 116 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET6, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 117 | }, |
| 118 | [TPS65090_IRQ_OVERLOAD_FET7] = { |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 119 | .reg_offset = 1, |
| 120 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET7, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 121 | }, |
| 122 | }; |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 123 | |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 124 | static struct regmap_irq_chip tps65090_irq_chip = { |
| 125 | .name = "tps65090", |
| 126 | .irqs = tps65090_irqs, |
| 127 | .num_irqs = ARRAY_SIZE(tps65090_irqs), |
| 128 | .num_regs = NUM_INT_REG, |
Doug Anderson | c42ba72 | 2014-04-16 16:12:27 -0700 | [diff] [blame] | 129 | .status_base = TPS65090_REG_INTR_STS, |
| 130 | .mask_base = TPS65090_REG_INTR_MASK, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 131 | .mask_invert = true, |
| 132 | }; |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 133 | |
| 134 | static bool is_volatile_reg(struct device *dev, unsigned int reg) |
| 135 | { |
Doug Anderson | c42ba72 | 2014-04-16 16:12:27 -0700 | [diff] [blame] | 136 | /* Nearly all registers have status bits mixed in, except a few */ |
| 137 | switch (reg) { |
| 138 | case TPS65090_REG_INTR_MASK: |
| 139 | case TPS65090_REG_INTR_MASK2: |
| 140 | case TPS65090_REG_CG_CTRL0: |
| 141 | case TPS65090_REG_CG_CTRL1: |
| 142 | case TPS65090_REG_CG_CTRL2: |
| 143 | case TPS65090_REG_CG_CTRL3: |
| 144 | case TPS65090_REG_CG_CTRL4: |
| 145 | case TPS65090_REG_CG_CTRL5: |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 146 | return false; |
Doug Anderson | c42ba72 | 2014-04-16 16:12:27 -0700 | [diff] [blame] | 147 | } |
| 148 | return true; |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static const struct regmap_config tps65090_regmap_config = { |
| 152 | .reg_bits = 8, |
| 153 | .val_bits = 8, |
Maciej S. Szmigiero | 5c14889 | 2016-01-31 23:00:06 +0100 | [diff] [blame] | 154 | .max_register = TPS65090_MAX_REG, |
| 155 | .num_reg_defaults_raw = TPS65090_NUM_REGS, |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 156 | .cache_type = REGCACHE_RBTREE, |
| 157 | .volatile_reg = is_volatile_reg, |
| 158 | }; |
| 159 | |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 160 | #ifdef CONFIG_OF |
| 161 | static const struct of_device_id tps65090_of_match[] = { |
| 162 | { .compatible = "ti,tps65090",}, |
| 163 | {}, |
| 164 | }; |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 165 | #endif |
| 166 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 167 | static int tps65090_i2c_probe(struct i2c_client *client, |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 168 | const struct i2c_device_id *id) |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 169 | { |
Jingoo Han | 334a41ce | 2013-07-30 17:10:05 +0900 | [diff] [blame] | 170 | struct tps65090_platform_data *pdata = dev_get_platdata(&client->dev); |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 171 | int irq_base = 0; |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 172 | struct tps65090 *tps65090; |
| 173 | int ret; |
| 174 | |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 175 | if (!pdata && !client->dev.of_node) { |
| 176 | dev_err(&client->dev, |
| 177 | "tps65090 requires platform data or of_node\n"); |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 178 | return -EINVAL; |
| 179 | } |
| 180 | |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 181 | if (pdata) |
| 182 | irq_base = pdata->irq_base; |
| 183 | |
Laxman Dewangan | e8e6f04 | 2012-11-20 08:44:46 +0530 | [diff] [blame] | 184 | tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL); |
Markus Elfring | 8a232f0 | 2018-03-08 15:25:58 +0100 | [diff] [blame] | 185 | if (!tps65090) |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 186 | return -ENOMEM; |
| 187 | |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 188 | tps65090->dev = &client->dev; |
| 189 | i2c_set_clientdata(client, tps65090); |
| 190 | |
Laxman Dewangan | 3863db3 | 2012-11-20 08:44:47 +0530 | [diff] [blame] | 191 | tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config); |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 192 | if (IS_ERR(tps65090->rmap)) { |
Axel Lin | b683a0a | 2012-04-25 09:30:36 +0800 | [diff] [blame] | 193 | ret = PTR_ERR(tps65090->rmap); |
| 194 | dev_err(&client->dev, "regmap_init failed with err: %d\n", ret); |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | if (client->irq) { |
| 199 | ret = regmap_add_irq_chip(tps65090->rmap, client->irq, |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 200 | IRQF_ONESHOT | IRQF_TRIGGER_LOW, irq_base, |
| 201 | &tps65090_irq_chip, &tps65090->irq_data); |
| 202 | if (ret) { |
| 203 | dev_err(&client->dev, |
| 204 | "IRQ init failed with err: %d\n", ret); |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 205 | return ret; |
| 206 | } |
Doug Anderson | 7d81177 | 2014-04-16 16:12:25 -0700 | [diff] [blame] | 207 | } else { |
| 208 | /* Don't tell children they have an IRQ that'll never fire */ |
| 209 | tps65090s[CHARGER].num_resources = 0; |
Axel Lin | b683a0a | 2012-04-25 09:30:36 +0800 | [diff] [blame] | 210 | } |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 211 | |
| 212 | ret = mfd_add_devices(tps65090->dev, -1, tps65090s, |
Lee Jones | 12849b6 | 2014-11-10 12:28:36 +0000 | [diff] [blame] | 213 | ARRAY_SIZE(tps65090s), NULL, |
| 214 | 0, regmap_irq_get_domain(tps65090->irq_data)); |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 215 | if (ret) { |
| 216 | dev_err(&client->dev, "add mfd devices failed with err: %d\n", |
| 217 | ret); |
Axel Lin | 1d88f7a | 2012-04-25 10:04:58 +0800 | [diff] [blame] | 218 | goto err_irq_exit; |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 223 | err_irq_exit: |
| 224 | if (client->irq) |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 225 | regmap_del_irq_chip(client->irq, tps65090->irq_data); |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 226 | return ret; |
| 227 | } |
| 228 | |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 229 | |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 230 | static const struct i2c_device_id tps65090_id_table[] = { |
| 231 | { "tps65090", 0 }, |
| 232 | { }, |
| 233 | }; |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 234 | |
| 235 | static struct i2c_driver tps65090_driver = { |
| 236 | .driver = { |
| 237 | .name = "tps65090", |
Paul Gortmaker | 26fce5e | 2019-01-13 13:36:41 -0500 | [diff] [blame] | 238 | .suppress_bind_attrs = true, |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 239 | .of_match_table = of_match_ptr(tps65090_of_match), |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 240 | }, |
| 241 | .probe = tps65090_i2c_probe, |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 242 | .id_table = tps65090_id_table, |
| 243 | }; |
| 244 | |
| 245 | static int __init tps65090_init(void) |
| 246 | { |
| 247 | return i2c_add_driver(&tps65090_driver); |
| 248 | } |
| 249 | subsys_initcall(tps65090_init); |