Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Core driver for TI TPS65090 PMIC family |
| 3 | * |
| 4 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
| 5 | |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/irq.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/i2c.h> |
| 26 | #include <linux/mfd/core.h> |
| 27 | #include <linux/mfd/tps65090.h> |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 28 | #include <linux/of.h> |
| 29 | #include <linux/of_device.h> |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 30 | #include <linux/err.h> |
| 31 | |
| 32 | #define NUM_INT_REG 2 |
| 33 | #define TOTAL_NUM_REG 0x18 |
| 34 | |
| 35 | /* interrupt status registers */ |
| 36 | #define TPS65090_INT_STS 0x0 |
| 37 | #define TPS65090_INT_STS2 0x1 |
| 38 | |
| 39 | /* interrupt mask registers */ |
| 40 | #define TPS65090_INT_MSK 0x2 |
| 41 | #define TPS65090_INT_MSK2 0x3 |
| 42 | |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 43 | #define TPS65090_INT1_MASK_VAC_STATUS_CHANGE 1 |
| 44 | #define TPS65090_INT1_MASK_VSYS_STATUS_CHANGE 2 |
| 45 | #define TPS65090_INT1_MASK_BAT_STATUS_CHANGE 3 |
| 46 | #define TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE 4 |
| 47 | #define TPS65090_INT1_MASK_CHARGING_COMPLETE 5 |
| 48 | #define TPS65090_INT1_MASK_OVERLOAD_DCDC1 6 |
| 49 | #define TPS65090_INT1_MASK_OVERLOAD_DCDC2 7 |
| 50 | #define TPS65090_INT2_MASK_OVERLOAD_DCDC3 0 |
| 51 | #define TPS65090_INT2_MASK_OVERLOAD_FET1 1 |
| 52 | #define TPS65090_INT2_MASK_OVERLOAD_FET2 2 |
| 53 | #define TPS65090_INT2_MASK_OVERLOAD_FET3 3 |
| 54 | #define TPS65090_INT2_MASK_OVERLOAD_FET4 4 |
| 55 | #define TPS65090_INT2_MASK_OVERLOAD_FET5 5 |
| 56 | #define TPS65090_INT2_MASK_OVERLOAD_FET6 6 |
| 57 | #define TPS65090_INT2_MASK_OVERLOAD_FET7 7 |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 58 | |
Rhyland Klein | 36c772e | 2013-03-12 18:08:07 -0400 | [diff] [blame] | 59 | static struct resource charger_resources[] = { |
| 60 | { |
| 61 | .start = TPS65090_IRQ_VAC_STATUS_CHANGE, |
| 62 | .end = TPS65090_IRQ_VAC_STATUS_CHANGE, |
| 63 | .flags = IORESOURCE_IRQ, |
| 64 | } |
| 65 | }; |
| 66 | |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 67 | static struct mfd_cell tps65090s[] = { |
| 68 | { |
Venu Byravarasu | b7e5378 | 2012-07-18 18:33:42 +0530 | [diff] [blame] | 69 | .name = "tps65090-pmic", |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 70 | }, |
| 71 | { |
Laxman Dewangan | e2e8ffc | 2012-11-20 08:44:45 +0530 | [diff] [blame] | 72 | .name = "tps65090-charger", |
Rhyland Klein | 36c772e | 2013-03-12 18:08:07 -0400 | [diff] [blame] | 73 | .num_resources = ARRAY_SIZE(charger_resources), |
| 74 | .resources = &charger_resources[0], |
Rhyland Klein | b50cf35 | 2013-04-10 13:14:55 -0400 | [diff] [blame] | 75 | .of_compatible = "ti,tps65090-charger", |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 76 | }, |
| 77 | }; |
| 78 | |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 79 | static const struct regmap_irq tps65090_irqs[] = { |
| 80 | /* INT1 IRQs*/ |
| 81 | [TPS65090_IRQ_VAC_STATUS_CHANGE] = { |
| 82 | .mask = TPS65090_INT1_MASK_VAC_STATUS_CHANGE, |
| 83 | }, |
| 84 | [TPS65090_IRQ_VSYS_STATUS_CHANGE] = { |
| 85 | .mask = TPS65090_INT1_MASK_VSYS_STATUS_CHANGE, |
| 86 | }, |
| 87 | [TPS65090_IRQ_BAT_STATUS_CHANGE] = { |
| 88 | .mask = TPS65090_INT1_MASK_BAT_STATUS_CHANGE, |
| 89 | }, |
| 90 | [TPS65090_IRQ_CHARGING_STATUS_CHANGE] = { |
| 91 | .mask = TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE, |
| 92 | }, |
| 93 | [TPS65090_IRQ_CHARGING_COMPLETE] = { |
| 94 | .mask = TPS65090_INT1_MASK_CHARGING_COMPLETE, |
| 95 | }, |
| 96 | [TPS65090_IRQ_OVERLOAD_DCDC1] = { |
| 97 | .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC1, |
| 98 | }, |
| 99 | [TPS65090_IRQ_OVERLOAD_DCDC2] = { |
| 100 | .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC2, |
| 101 | }, |
| 102 | /* INT2 IRQs*/ |
| 103 | [TPS65090_IRQ_OVERLOAD_DCDC3] = { |
| 104 | .reg_offset = 1, |
| 105 | .mask = TPS65090_INT2_MASK_OVERLOAD_DCDC3, |
| 106 | }, |
| 107 | [TPS65090_IRQ_OVERLOAD_FET1] = { |
| 108 | .reg_offset = 1, |
| 109 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET1, |
| 110 | }, |
| 111 | [TPS65090_IRQ_OVERLOAD_FET2] = { |
| 112 | .reg_offset = 1, |
| 113 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET2, |
| 114 | }, |
| 115 | [TPS65090_IRQ_OVERLOAD_FET3] = { |
| 116 | .reg_offset = 1, |
| 117 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET3, |
| 118 | }, |
| 119 | [TPS65090_IRQ_OVERLOAD_FET4] = { |
| 120 | .reg_offset = 1, |
| 121 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET4, |
| 122 | }, |
| 123 | [TPS65090_IRQ_OVERLOAD_FET5] = { |
| 124 | .reg_offset = 1, |
| 125 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET5, |
| 126 | }, |
| 127 | [TPS65090_IRQ_OVERLOAD_FET6] = { |
| 128 | .reg_offset = 1, |
| 129 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET6, |
| 130 | }, |
| 131 | [TPS65090_IRQ_OVERLOAD_FET7] = { |
| 132 | .reg_offset = 1, |
| 133 | .mask = TPS65090_INT2_MASK_OVERLOAD_FET7, |
| 134 | }, |
| 135 | }; |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 136 | |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 137 | static struct regmap_irq_chip tps65090_irq_chip = { |
| 138 | .name = "tps65090", |
| 139 | .irqs = tps65090_irqs, |
| 140 | .num_irqs = ARRAY_SIZE(tps65090_irqs), |
| 141 | .num_regs = NUM_INT_REG, |
| 142 | .status_base = TPS65090_INT_STS, |
| 143 | .mask_base = TPS65090_INT_MSK, |
| 144 | .mask_invert = true, |
| 145 | }; |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 146 | |
| 147 | static bool is_volatile_reg(struct device *dev, unsigned int reg) |
| 148 | { |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 149 | if ((reg == TPS65090_INT_STS) || (reg == TPS65090_INT_STS2)) |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 150 | return true; |
| 151 | else |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | static const struct regmap_config tps65090_regmap_config = { |
| 156 | .reg_bits = 8, |
| 157 | .val_bits = 8, |
| 158 | .max_register = TOTAL_NUM_REG, |
| 159 | .num_reg_defaults_raw = TOTAL_NUM_REG, |
| 160 | .cache_type = REGCACHE_RBTREE, |
| 161 | .volatile_reg = is_volatile_reg, |
| 162 | }; |
| 163 | |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 164 | #ifdef CONFIG_OF |
| 165 | static const struct of_device_id tps65090_of_match[] = { |
| 166 | { .compatible = "ti,tps65090",}, |
| 167 | {}, |
| 168 | }; |
| 169 | MODULE_DEVICE_TABLE(of, tps65090_of_match); |
| 170 | #endif |
| 171 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 172 | static int tps65090_i2c_probe(struct i2c_client *client, |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 173 | const struct i2c_device_id *id) |
| 174 | { |
Jingoo Han | 334a41c | 2013-07-30 17:10:05 +0900 | [diff] [blame] | 175 | struct tps65090_platform_data *pdata = dev_get_platdata(&client->dev); |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 176 | int irq_base = 0; |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 177 | struct tps65090 *tps65090; |
| 178 | int ret; |
| 179 | |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 180 | if (!pdata && !client->dev.of_node) { |
| 181 | dev_err(&client->dev, |
| 182 | "tps65090 requires platform data or of_node\n"); |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 183 | return -EINVAL; |
| 184 | } |
| 185 | |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 186 | if (pdata) |
| 187 | irq_base = pdata->irq_base; |
| 188 | |
Laxman Dewangan | e8e6f04 | 2012-11-20 08:44:46 +0530 | [diff] [blame] | 189 | tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL); |
| 190 | if (!tps65090) { |
| 191 | dev_err(&client->dev, "mem alloc for tps65090 failed\n"); |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 192 | return -ENOMEM; |
Laxman Dewangan | e8e6f04 | 2012-11-20 08:44:46 +0530 | [diff] [blame] | 193 | } |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 194 | |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 195 | tps65090->dev = &client->dev; |
| 196 | i2c_set_clientdata(client, tps65090); |
| 197 | |
Laxman Dewangan | 3863db3 | 2012-11-20 08:44:47 +0530 | [diff] [blame] | 198 | tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config); |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 199 | if (IS_ERR(tps65090->rmap)) { |
Axel Lin | b683a0a | 2012-04-25 09:30:36 +0800 | [diff] [blame] | 200 | ret = PTR_ERR(tps65090->rmap); |
| 201 | dev_err(&client->dev, "regmap_init failed with err: %d\n", ret); |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | if (client->irq) { |
| 206 | ret = regmap_add_irq_chip(tps65090->rmap, client->irq, |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 207 | IRQF_ONESHOT | IRQF_TRIGGER_LOW, irq_base, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 208 | &tps65090_irq_chip, &tps65090->irq_data); |
| 209 | if (ret) { |
| 210 | dev_err(&client->dev, |
| 211 | "IRQ init failed with err: %d\n", ret); |
| 212 | return ret; |
| 213 | } |
Axel Lin | b683a0a | 2012-04-25 09:30:36 +0800 | [diff] [blame] | 214 | } |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 215 | |
| 216 | ret = mfd_add_devices(tps65090->dev, -1, tps65090s, |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 217 | ARRAY_SIZE(tps65090s), NULL, |
Laxman Dewangan | 4f979ed | 2013-01-29 14:35:17 +0530 | [diff] [blame] | 218 | 0, regmap_irq_get_domain(tps65090->irq_data)); |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 219 | if (ret) { |
| 220 | dev_err(&client->dev, "add mfd devices failed with err: %d\n", |
| 221 | ret); |
Axel Lin | 1d88f7a | 2012-04-25 10:04:58 +0800 | [diff] [blame] | 222 | goto err_irq_exit; |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 227 | err_irq_exit: |
| 228 | if (client->irq) |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 229 | regmap_del_irq_chip(client->irq, tps65090->irq_data); |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 230 | return ret; |
| 231 | } |
| 232 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 233 | static int tps65090_i2c_remove(struct i2c_client *client) |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 234 | { |
| 235 | struct tps65090 *tps65090 = i2c_get_clientdata(client); |
| 236 | |
| 237 | mfd_remove_devices(tps65090->dev); |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 238 | if (client->irq) |
Laxman Dewangan | 759f259 | 2012-11-20 08:44:49 +0530 | [diff] [blame] | 239 | regmap_del_irq_chip(client->irq, tps65090->irq_data); |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 244 | static const struct i2c_device_id tps65090_id_table[] = { |
| 245 | { "tps65090", 0 }, |
| 246 | { }, |
| 247 | }; |
| 248 | MODULE_DEVICE_TABLE(i2c, tps65090_id_table); |
| 249 | |
| 250 | static struct i2c_driver tps65090_driver = { |
| 251 | .driver = { |
| 252 | .name = "tps65090", |
| 253 | .owner = THIS_MODULE, |
Laxman Dewangan | 4071931 | 2013-01-29 14:35:15 +0530 | [diff] [blame] | 254 | .of_match_table = of_match_ptr(tps65090_of_match), |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 255 | }, |
| 256 | .probe = tps65090_i2c_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 257 | .remove = tps65090_i2c_remove, |
Venu Byravarasu | 3c33be06 | 2012-03-16 11:10:19 +0530 | [diff] [blame] | 258 | .id_table = tps65090_id_table, |
| 259 | }; |
| 260 | |
| 261 | static int __init tps65090_init(void) |
| 262 | { |
| 263 | return i2c_add_driver(&tps65090_driver); |
| 264 | } |
| 265 | subsys_initcall(tps65090_init); |
| 266 | |
| 267 | static void __exit tps65090_exit(void) |
| 268 | { |
| 269 | i2c_del_driver(&tps65090_driver); |
| 270 | } |
| 271 | module_exit(tps65090_exit); |
| 272 | |
| 273 | MODULE_DESCRIPTION("TPS65090 core driver"); |
| 274 | MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>"); |
| 275 | MODULE_LICENSE("GPL v2"); |