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 struct mfd_cell tps65217s
[] = {
35 .name
= "tps65217-pmic",
40 * tps65217_reg_read: Read a single tps65217 register.
42 * @tps: Device to read from.
43 * @reg: Register to read.
44 * @val: Contians the value
46 int tps65217_reg_read(struct tps65217
*tps
, unsigned int reg
,
49 return regmap_read(tps
->regmap
, reg
, val
);
51 EXPORT_SYMBOL_GPL(tps65217_reg_read
);
54 * tps65217_reg_write: Write a single tps65217 register.
56 * @tps65217: Device to write to.
57 * @reg: Register to write to.
58 * @val: Value to write.
59 * @level: Password protected level
61 int tps65217_reg_write(struct tps65217
*tps
, unsigned int reg
,
62 unsigned int val
, unsigned int level
)
65 unsigned int xor_reg_val
;
68 case TPS65217_PROTECT_NONE
:
69 return regmap_write(tps
->regmap
, reg
, val
);
70 case TPS65217_PROTECT_L1
:
71 xor_reg_val
= reg
^ TPS65217_PASSWORD_REGS_UNLOCK
;
72 ret
= regmap_write(tps
->regmap
, TPS65217_REG_PASSWORD
,
77 return regmap_write(tps
->regmap
, reg
, val
);
78 case TPS65217_PROTECT_L2
:
79 xor_reg_val
= reg
^ TPS65217_PASSWORD_REGS_UNLOCK
;
80 ret
= regmap_write(tps
->regmap
, TPS65217_REG_PASSWORD
,
84 ret
= regmap_write(tps
->regmap
, reg
, val
);
87 ret
= regmap_write(tps
->regmap
, TPS65217_REG_PASSWORD
,
91 return regmap_write(tps
->regmap
, reg
, val
);
96 EXPORT_SYMBOL_GPL(tps65217_reg_write
);
99 * tps65217_update_bits: Modify bits w.r.t mask, val and level.
101 * @tps65217: Device to write to.
102 * @reg: Register to read-write to.
104 * @val: Value to write.
105 * @level: Password protected level
107 static int tps65217_update_bits(struct tps65217
*tps
, unsigned int reg
,
108 unsigned int mask
, unsigned int val
, unsigned int level
)
113 ret
= tps65217_reg_read(tps
, reg
, &data
);
115 dev_err(tps
->dev
, "Read from reg 0x%x failed\n", reg
);
122 ret
= tps65217_reg_write(tps
, reg
, data
, level
);
124 dev_err(tps
->dev
, "Write for reg 0x%x failed\n", reg
);
129 int tps65217_set_bits(struct tps65217
*tps
, unsigned int reg
,
130 unsigned int mask
, unsigned int val
, unsigned int level
)
132 return tps65217_update_bits(tps
, reg
, mask
, val
, level
);
134 EXPORT_SYMBOL_GPL(tps65217_set_bits
);
136 int tps65217_clear_bits(struct tps65217
*tps
, unsigned int reg
,
137 unsigned int mask
, unsigned int level
)
139 return tps65217_update_bits(tps
, reg
, mask
, 0, level
);
141 EXPORT_SYMBOL_GPL(tps65217_clear_bits
);
143 static struct regmap_config tps65217_regmap_config
= {
148 static const struct of_device_id tps65217_of_match
[] = {
149 { .compatible
= "ti,tps65217", .data
= (void *)TPS65217
},
153 static int __devinit
tps65217_probe(struct i2c_client
*client
,
154 const struct i2c_device_id
*ids
)
156 struct tps65217
*tps
;
157 unsigned int version
;
158 unsigned int chip_id
= ids
->driver_data
;
159 const struct of_device_id
*match
;
162 if (client
->dev
.of_node
) {
163 match
= of_match_device(tps65217_of_match
, &client
->dev
);
165 dev_err(&client
->dev
,
166 "Failed to find matching dt id\n");
169 chip_id
= (unsigned int)match
->data
;
173 dev_err(&client
->dev
, "id is null.\n");
177 tps
= devm_kzalloc(&client
->dev
, sizeof(*tps
), GFP_KERNEL
);
181 i2c_set_clientdata(client
, tps
);
182 tps
->dev
= &client
->dev
;
185 tps
->regmap
= devm_regmap_init_i2c(client
, &tps65217_regmap_config
);
186 if (IS_ERR(tps
->regmap
)) {
187 ret
= PTR_ERR(tps
->regmap
);
188 dev_err(tps
->dev
, "Failed to allocate register map: %d\n",
193 ret
= mfd_add_devices(tps
->dev
, -1, tps65217s
,
194 ARRAY_SIZE(tps65217s
), NULL
, 0, NULL
);
196 dev_err(tps
->dev
, "mfd_add_devices failed: %d\n", ret
);
200 ret
= tps65217_reg_read(tps
, TPS65217_REG_CHIPID
, &version
);
202 dev_err(tps
->dev
, "Failed to read revision register: %d\n",
207 dev_info(tps
->dev
, "TPS65217 ID %#x version 1.%d\n",
208 (version
& TPS65217_CHIPID_CHIP_MASK
) >> 4,
209 version
& TPS65217_CHIPID_REV_MASK
);
214 static int __devexit
tps65217_remove(struct i2c_client
*client
)
216 struct tps65217
*tps
= i2c_get_clientdata(client
);
218 mfd_remove_devices(tps
->dev
);
223 static const struct i2c_device_id tps65217_id_table
[] = {
224 {"tps65217", TPS65217
},
227 MODULE_DEVICE_TABLE(i2c
, tps65217_id_table
);
229 static struct i2c_driver tps65217_driver
= {
232 .owner
= THIS_MODULE
,
233 .of_match_table
= of_match_ptr(tps65217_of_match
),
235 .id_table
= tps65217_id_table
,
236 .probe
= tps65217_probe
,
237 .remove
= __devexit_p(tps65217_remove
),
240 static int __init
tps65217_init(void)
242 return i2c_add_driver(&tps65217_driver
);
244 subsys_initcall(tps65217_init
);
246 static void __exit
tps65217_exit(void)
248 i2c_del_driver(&tps65217_driver
);
250 module_exit(tps65217_exit
);
252 MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
253 MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
254 MODULE_LICENSE("GPL v2");