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/kernel.h>
19 #include <linux/device.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/init.h>
23 #include <linux/i2c.h>
24 #include <linux/slab.h>
25 #include <linux/regmap.h>
26 #include <linux/err.h>
28 #include <linux/of_device.h>
30 #include <linux/mfd/core.h>
31 #include <linux/mfd/tps65217.h>
33 static const struct mfd_cell tps65217s
[] = {
35 .name
= "tps65217-pmic",
36 .of_compatible
= "ti,tps65217-pmic",
39 .name
= "tps65217-bl",
40 .of_compatible
= "ti,tps65217-bl",
43 .name
= "tps65217-charger",
44 .of_compatible
= "ti,tps65217-charger",
49 * tps65217_reg_read: Read a single tps65217 register.
51 * @tps: Device to read from.
52 * @reg: Register to read.
53 * @val: Contians the value
55 int tps65217_reg_read(struct tps65217
*tps
, unsigned int reg
,
58 return regmap_read(tps
->regmap
, reg
, val
);
60 EXPORT_SYMBOL_GPL(tps65217_reg_read
);
63 * tps65217_reg_write: Write a single tps65217 register.
65 * @tps65217: Device to write to.
66 * @reg: Register to write to.
67 * @val: Value to write.
68 * @level: Password protected level
70 int tps65217_reg_write(struct tps65217
*tps
, unsigned int reg
,
71 unsigned int val
, unsigned int level
)
74 unsigned int xor_reg_val
;
77 case TPS65217_PROTECT_NONE
:
78 return regmap_write(tps
->regmap
, reg
, val
);
79 case TPS65217_PROTECT_L1
:
80 xor_reg_val
= reg
^ TPS65217_PASSWORD_REGS_UNLOCK
;
81 ret
= regmap_write(tps
->regmap
, TPS65217_REG_PASSWORD
,
86 return regmap_write(tps
->regmap
, reg
, val
);
87 case TPS65217_PROTECT_L2
:
88 xor_reg_val
= reg
^ TPS65217_PASSWORD_REGS_UNLOCK
;
89 ret
= regmap_write(tps
->regmap
, TPS65217_REG_PASSWORD
,
93 ret
= regmap_write(tps
->regmap
, reg
, val
);
96 ret
= regmap_write(tps
->regmap
, TPS65217_REG_PASSWORD
,
100 return regmap_write(tps
->regmap
, reg
, val
);
105 EXPORT_SYMBOL_GPL(tps65217_reg_write
);
108 * tps65217_update_bits: Modify bits w.r.t mask, val and level.
110 * @tps65217: Device to write to.
111 * @reg: Register to read-write to.
113 * @val: Value to write.
114 * @level: Password protected level
116 static int tps65217_update_bits(struct tps65217
*tps
, unsigned int reg
,
117 unsigned int mask
, unsigned int val
, unsigned int level
)
122 ret
= tps65217_reg_read(tps
, reg
, &data
);
124 dev_err(tps
->dev
, "Read from reg 0x%x failed\n", reg
);
131 ret
= tps65217_reg_write(tps
, reg
, data
, level
);
133 dev_err(tps
->dev
, "Write for reg 0x%x failed\n", reg
);
138 int tps65217_set_bits(struct tps65217
*tps
, unsigned int reg
,
139 unsigned int mask
, unsigned int val
, unsigned int level
)
141 return tps65217_update_bits(tps
, reg
, mask
, val
, level
);
143 EXPORT_SYMBOL_GPL(tps65217_set_bits
);
145 int tps65217_clear_bits(struct tps65217
*tps
, unsigned int reg
,
146 unsigned int mask
, unsigned int level
)
148 return tps65217_update_bits(tps
, reg
, mask
, 0, level
);
150 EXPORT_SYMBOL_GPL(tps65217_clear_bits
);
152 static const struct regmap_config tps65217_regmap_config
= {
156 .max_register
= TPS65217_REG_MAX
,
159 static const struct of_device_id tps65217_of_match
[] = {
160 { .compatible
= "ti,tps65217", .data
= (void *)TPS65217
},
163 MODULE_DEVICE_TABLE(of
, tps65217_of_match
);
165 static int tps65217_probe(struct i2c_client
*client
,
166 const struct i2c_device_id
*ids
)
168 struct tps65217
*tps
;
169 unsigned int version
;
170 unsigned long chip_id
= ids
->driver_data
;
171 const struct of_device_id
*match
;
172 bool status_off
= false;
175 if (client
->dev
.of_node
) {
176 match
= of_match_device(tps65217_of_match
, &client
->dev
);
178 dev_err(&client
->dev
,
179 "Failed to find matching dt id\n");
182 chip_id
= (unsigned long)match
->data
;
183 status_off
= of_property_read_bool(client
->dev
.of_node
,
184 "ti,pmic-shutdown-controller");
188 dev_err(&client
->dev
, "id is null.\n");
192 tps
= devm_kzalloc(&client
->dev
, sizeof(*tps
), GFP_KERNEL
);
196 i2c_set_clientdata(client
, tps
);
197 tps
->dev
= &client
->dev
;
200 tps
->regmap
= devm_regmap_init_i2c(client
, &tps65217_regmap_config
);
201 if (IS_ERR(tps
->regmap
)) {
202 ret
= PTR_ERR(tps
->regmap
);
203 dev_err(tps
->dev
, "Failed to allocate register map: %d\n",
208 ret
= mfd_add_devices(tps
->dev
, -1, tps65217s
,
209 ARRAY_SIZE(tps65217s
), NULL
, 0, NULL
);
211 dev_err(tps
->dev
, "mfd_add_devices failed: %d\n", ret
);
215 ret
= tps65217_reg_read(tps
, TPS65217_REG_CHIPID
, &version
);
217 dev_err(tps
->dev
, "Failed to read revision register: %d\n",
222 /* Set the PMIC to shutdown on PWR_EN toggle */
224 ret
= tps65217_set_bits(tps
, TPS65217_REG_STATUS
,
225 TPS65217_STATUS_OFF
, TPS65217_STATUS_OFF
,
226 TPS65217_PROTECT_NONE
);
228 dev_warn(tps
->dev
, "unable to set the status OFF\n");
231 dev_info(tps
->dev
, "TPS65217 ID %#x version 1.%d\n",
232 (version
& TPS65217_CHIPID_CHIP_MASK
) >> 4,
233 version
& TPS65217_CHIPID_REV_MASK
);
238 static int tps65217_remove(struct i2c_client
*client
)
240 struct tps65217
*tps
= i2c_get_clientdata(client
);
242 mfd_remove_devices(tps
->dev
);
247 static const struct i2c_device_id tps65217_id_table
[] = {
248 {"tps65217", TPS65217
},
251 MODULE_DEVICE_TABLE(i2c
, tps65217_id_table
);
253 static struct i2c_driver tps65217_driver
= {
256 .of_match_table
= tps65217_of_match
,
258 .id_table
= tps65217_id_table
,
259 .probe
= tps65217_probe
,
260 .remove
= tps65217_remove
,
263 static int __init
tps65217_init(void)
265 return i2c_add_driver(&tps65217_driver
);
267 subsys_initcall(tps65217_init
);
269 static void __exit
tps65217_exit(void)
271 i2c_del_driver(&tps65217_driver
);
273 module_exit(tps65217_exit
);
275 MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
276 MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
277 MODULE_LICENSE("GPL v2");