Keerthy | 44b4dc6 | 2014-02-06 11:20:12 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Driver for TPS65218 Integrated power management chipsets |
| 3 | * |
| 4 | * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 11 | * kind, whether expressed or implied; without even the implied warranty |
| 12 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License version 2 for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/regmap.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/of.h> |
| 26 | #include <linux/of_device.h> |
| 27 | #include <linux/irq.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/mutex.h> |
| 30 | |
| 31 | #include <linux/mfd/core.h> |
| 32 | #include <linux/mfd/tps65218.h> |
| 33 | |
| 34 | #define TPS65218_PASSWORD_REGS_UNLOCK 0x7D |
| 35 | |
| 36 | /** |
| 37 | * tps65218_reg_read: Read a single tps65218 register. |
| 38 | * |
| 39 | * @tps: Device to read from. |
| 40 | * @reg: Register to read. |
| 41 | * @val: Contians the value |
| 42 | */ |
| 43 | int tps65218_reg_read(struct tps65218 *tps, unsigned int reg, |
| 44 | unsigned int *val) |
| 45 | { |
| 46 | return regmap_read(tps->regmap, reg, val); |
| 47 | } |
| 48 | EXPORT_SYMBOL_GPL(tps65218_reg_read); |
| 49 | |
| 50 | /** |
| 51 | * tps65218_reg_write: Write a single tps65218 register. |
| 52 | * |
| 53 | * @tps65218: Device to write to. |
| 54 | * @reg: Register to write to. |
| 55 | * @val: Value to write. |
| 56 | * @level: Password protected level |
| 57 | */ |
| 58 | int tps65218_reg_write(struct tps65218 *tps, unsigned int reg, |
| 59 | unsigned int val, unsigned int level) |
| 60 | { |
| 61 | int ret; |
| 62 | unsigned int xor_reg_val; |
| 63 | |
| 64 | switch (level) { |
| 65 | case TPS65218_PROTECT_NONE: |
| 66 | return regmap_write(tps->regmap, reg, val); |
| 67 | case TPS65218_PROTECT_L1: |
| 68 | xor_reg_val = reg ^ TPS65218_PASSWORD_REGS_UNLOCK; |
| 69 | ret = regmap_write(tps->regmap, TPS65218_REG_PASSWORD, |
| 70 | xor_reg_val); |
| 71 | if (ret < 0) |
| 72 | return ret; |
| 73 | |
| 74 | return regmap_write(tps->regmap, reg, val); |
| 75 | default: |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | } |
| 79 | EXPORT_SYMBOL_GPL(tps65218_reg_write); |
| 80 | |
| 81 | /** |
| 82 | * tps65218_update_bits: Modify bits w.r.t mask, val and level. |
| 83 | * |
| 84 | * @tps65218: Device to write to. |
| 85 | * @reg: Register to read-write to. |
| 86 | * @mask: Mask. |
| 87 | * @val: Value to write. |
| 88 | * @level: Password protected level |
| 89 | */ |
| 90 | static int tps65218_update_bits(struct tps65218 *tps, unsigned int reg, |
| 91 | unsigned int mask, unsigned int val, unsigned int level) |
| 92 | { |
| 93 | int ret; |
| 94 | unsigned int data; |
| 95 | |
| 96 | ret = tps65218_reg_read(tps, reg, &data); |
| 97 | if (ret) { |
| 98 | dev_err(tps->dev, "Read from reg 0x%x failed\n", reg); |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | data &= ~mask; |
| 103 | data |= val & mask; |
| 104 | |
| 105 | mutex_lock(&tps->tps_lock); |
| 106 | ret = tps65218_reg_write(tps, reg, data, level); |
| 107 | if (ret) |
| 108 | dev_err(tps->dev, "Write for reg 0x%x failed\n", reg); |
| 109 | mutex_unlock(&tps->tps_lock); |
| 110 | |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | int tps65218_set_bits(struct tps65218 *tps, unsigned int reg, |
| 115 | unsigned int mask, unsigned int val, unsigned int level) |
| 116 | { |
| 117 | return tps65218_update_bits(tps, reg, mask, val, level); |
| 118 | } |
| 119 | EXPORT_SYMBOL_GPL(tps65218_set_bits); |
| 120 | |
| 121 | int tps65218_clear_bits(struct tps65218 *tps, unsigned int reg, |
| 122 | unsigned int mask, unsigned int level) |
| 123 | { |
| 124 | return tps65218_update_bits(tps, reg, mask, 0, level); |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(tps65218_clear_bits); |
| 127 | |
Felipe Balbi | 773328d | 2014-12-26 13:28:20 -0600 | [diff] [blame^] | 128 | static const struct regmap_range tps65218_yes_ranges[] = { |
| 129 | regmap_reg_range(TPS65218_REG_INT1, TPS65218_REG_INT2), |
| 130 | regmap_reg_range(TPS65218_REG_STATUS, TPS65218_REG_STATUS), |
| 131 | }; |
| 132 | |
| 133 | static const struct regmap_access_table tps65218_volatile_table = { |
| 134 | .yes_ranges = tps65218_yes_ranges, |
| 135 | .n_yes_ranges = ARRAY_SIZE(tps65218_yes_ranges), |
| 136 | }; |
| 137 | |
Keerthy | 44b4dc6 | 2014-02-06 11:20:12 +0530 | [diff] [blame] | 138 | static struct regmap_config tps65218_regmap_config = { |
| 139 | .reg_bits = 8, |
| 140 | .val_bits = 8, |
| 141 | .cache_type = REGCACHE_RBTREE, |
Felipe Balbi | 773328d | 2014-12-26 13:28:20 -0600 | [diff] [blame^] | 142 | .volatile_table = &tps65218_volatile_table, |
Keerthy | 44b4dc6 | 2014-02-06 11:20:12 +0530 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | static const struct regmap_irq tps65218_irqs[] = { |
| 146 | /* INT1 IRQs */ |
| 147 | [TPS65218_PRGC_IRQ] = { |
| 148 | .mask = TPS65218_INT1_PRGC, |
| 149 | }, |
| 150 | [TPS65218_CC_AQC_IRQ] = { |
| 151 | .mask = TPS65218_INT1_CC_AQC, |
| 152 | }, |
| 153 | [TPS65218_HOT_IRQ] = { |
| 154 | .mask = TPS65218_INT1_HOT, |
| 155 | }, |
| 156 | [TPS65218_PB_IRQ] = { |
| 157 | .mask = TPS65218_INT1_PB, |
| 158 | }, |
| 159 | [TPS65218_AC_IRQ] = { |
| 160 | .mask = TPS65218_INT1_AC, |
| 161 | }, |
| 162 | [TPS65218_VPRG_IRQ] = { |
| 163 | .mask = TPS65218_INT1_VPRG, |
| 164 | }, |
| 165 | [TPS65218_INVALID1_IRQ] = { |
| 166 | }, |
| 167 | [TPS65218_INVALID2_IRQ] = { |
| 168 | }, |
| 169 | /* INT2 IRQs*/ |
| 170 | [TPS65218_LS1_I_IRQ] = { |
| 171 | .mask = TPS65218_INT2_LS1_I, |
| 172 | .reg_offset = 1, |
| 173 | }, |
| 174 | [TPS65218_LS2_I_IRQ] = { |
| 175 | .mask = TPS65218_INT2_LS2_I, |
| 176 | .reg_offset = 1, |
| 177 | }, |
| 178 | [TPS65218_LS3_I_IRQ] = { |
| 179 | .mask = TPS65218_INT2_LS3_I, |
| 180 | .reg_offset = 1, |
| 181 | }, |
| 182 | [TPS65218_LS1_F_IRQ] = { |
| 183 | .mask = TPS65218_INT2_LS1_F, |
| 184 | .reg_offset = 1, |
| 185 | }, |
| 186 | [TPS65218_LS2_F_IRQ] = { |
| 187 | .mask = TPS65218_INT2_LS2_F, |
| 188 | .reg_offset = 1, |
| 189 | }, |
| 190 | [TPS65218_LS3_F_IRQ] = { |
| 191 | .mask = TPS65218_INT2_LS3_F, |
| 192 | .reg_offset = 1, |
| 193 | }, |
| 194 | [TPS65218_INVALID3_IRQ] = { |
| 195 | }, |
| 196 | [TPS65218_INVALID4_IRQ] = { |
| 197 | }, |
| 198 | }; |
| 199 | |
| 200 | static struct regmap_irq_chip tps65218_irq_chip = { |
| 201 | .name = "tps65218", |
| 202 | .irqs = tps65218_irqs, |
| 203 | .num_irqs = ARRAY_SIZE(tps65218_irqs), |
| 204 | |
| 205 | .num_regs = 2, |
| 206 | .mask_base = TPS65218_REG_INT_MASK1, |
| 207 | }; |
| 208 | |
| 209 | static const struct of_device_id of_tps65218_match_table[] = { |
| 210 | { .compatible = "ti,tps65218", }, |
Stephen Boyd | 8320513 | 2014-05-23 17:16:52 -0700 | [diff] [blame] | 211 | {} |
Keerthy | 44b4dc6 | 2014-02-06 11:20:12 +0530 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | static int tps65218_probe(struct i2c_client *client, |
| 215 | const struct i2c_device_id *ids) |
| 216 | { |
| 217 | struct tps65218 *tps; |
| 218 | const struct of_device_id *match; |
| 219 | int ret; |
| 220 | |
| 221 | match = of_match_device(of_tps65218_match_table, &client->dev); |
| 222 | if (!match) { |
| 223 | dev_err(&client->dev, |
| 224 | "Failed to find matching dt id\n"); |
| 225 | return -EINVAL; |
| 226 | } |
| 227 | |
| 228 | tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL); |
| 229 | if (!tps) |
| 230 | return -ENOMEM; |
| 231 | |
| 232 | i2c_set_clientdata(client, tps); |
| 233 | tps->dev = &client->dev; |
| 234 | tps->irq = client->irq; |
| 235 | tps->regmap = devm_regmap_init_i2c(client, &tps65218_regmap_config); |
| 236 | if (IS_ERR(tps->regmap)) { |
| 237 | ret = PTR_ERR(tps->regmap); |
| 238 | dev_err(tps->dev, "Failed to allocate register map: %d\n", |
| 239 | ret); |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | mutex_init(&tps->tps_lock); |
| 244 | |
| 245 | ret = regmap_add_irq_chip(tps->regmap, tps->irq, |
| 246 | IRQF_ONESHOT, 0, &tps65218_irq_chip, |
| 247 | &tps->irq_data); |
| 248 | if (ret < 0) |
| 249 | return ret; |
| 250 | |
| 251 | ret = of_platform_populate(client->dev.of_node, NULL, NULL, |
| 252 | &client->dev); |
| 253 | if (ret < 0) |
| 254 | goto err_irq; |
| 255 | |
| 256 | return 0; |
| 257 | |
| 258 | err_irq: |
| 259 | regmap_del_irq_chip(tps->irq, tps->irq_data); |
| 260 | |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static int tps65218_remove(struct i2c_client *client) |
| 265 | { |
| 266 | struct tps65218 *tps = i2c_get_clientdata(client); |
| 267 | |
| 268 | regmap_del_irq_chip(tps->irq, tps->irq_data); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static const struct i2c_device_id tps65218_id_table[] = { |
| 274 | { "tps65218", TPS65218 }, |
| 275 | { }, |
| 276 | }; |
| 277 | MODULE_DEVICE_TABLE(i2c, tps65218_id_table); |
| 278 | |
| 279 | static struct i2c_driver tps65218_driver = { |
| 280 | .driver = { |
| 281 | .name = "tps65218", |
| 282 | .owner = THIS_MODULE, |
| 283 | .of_match_table = of_tps65218_match_table, |
| 284 | }, |
| 285 | .probe = tps65218_probe, |
| 286 | .remove = tps65218_remove, |
| 287 | .id_table = tps65218_id_table, |
| 288 | }; |
| 289 | |
| 290 | module_i2c_driver(tps65218_driver); |
| 291 | |
| 292 | MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>"); |
| 293 | MODULE_DESCRIPTION("TPS65218 chip family multi-function driver"); |
| 294 | MODULE_LICENSE("GPL v2"); |