blob: a95e9421b73580df95402f718a4166898a1c41dd [file] [log] [blame]
AnilKumar Chd48f4112012-01-11 16:11:41 +05301/*
2 * tps65217.c
3 *
4 * TPS65217 chip family multi-function driver
5 *
6 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
7 *
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.
11 *
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.
16 */
17
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>
AnilKumar Ch1922b0f2012-08-13 20:36:05 +053027#include <linux/of.h>
28#include <linux/of_device.h>
AnilKumar Chd48f4112012-01-11 16:11:41 +053029
30#include <linux/mfd/core.h>
31#include <linux/mfd/tps65217.h>
32
AnilKumar Ch1922b0f2012-08-13 20:36:05 +053033static struct mfd_cell tps65217s[] = {
34 {
35 .name = "tps65217-pmic",
36 },
37};
38
AnilKumar Chd48f4112012-01-11 16:11:41 +053039/**
40 * tps65217_reg_read: Read a single tps65217 register.
41 *
42 * @tps: Device to read from.
43 * @reg: Register to read.
44 * @val: Contians the value
45 */
46int tps65217_reg_read(struct tps65217 *tps, unsigned int reg,
47 unsigned int *val)
48{
49 return regmap_read(tps->regmap, reg, val);
50}
51EXPORT_SYMBOL_GPL(tps65217_reg_read);
52
53/**
54 * tps65217_reg_write: Write a single tps65217 register.
55 *
56 * @tps65217: Device to write to.
57 * @reg: Register to write to.
58 * @val: Value to write.
59 * @level: Password protected level
60 */
61int tps65217_reg_write(struct tps65217 *tps, unsigned int reg,
62 unsigned int val, unsigned int level)
63{
64 int ret;
65 unsigned int xor_reg_val;
66
67 switch (level) {
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,
73 xor_reg_val);
74 if (ret < 0)
75 return ret;
76
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,
81 xor_reg_val);
82 if (ret < 0)
83 return ret;
84 ret = regmap_write(tps->regmap, reg, val);
85 if (ret < 0)
86 return ret;
87 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
88 xor_reg_val);
89 if (ret < 0)
90 return ret;
91 return regmap_write(tps->regmap, reg, val);
92 default:
93 return -EINVAL;
94 }
95}
96EXPORT_SYMBOL_GPL(tps65217_reg_write);
97
98/**
99 * tps65217_update_bits: Modify bits w.r.t mask, val and level.
100 *
101 * @tps65217: Device to write to.
102 * @reg: Register to read-write to.
103 * @mask: Mask.
104 * @val: Value to write.
105 * @level: Password protected level
106 */
Mark Brown27757e82012-05-09 21:18:05 +0100107static int tps65217_update_bits(struct tps65217 *tps, unsigned int reg,
AnilKumar Chd48f4112012-01-11 16:11:41 +0530108 unsigned int mask, unsigned int val, unsigned int level)
109{
110 int ret;
111 unsigned int data;
112
113 ret = tps65217_reg_read(tps, reg, &data);
114 if (ret) {
115 dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
116 return ret;
117 }
118
119 data &= ~mask;
120 data |= val & mask;
121
122 ret = tps65217_reg_write(tps, reg, data, level);
123 if (ret)
124 dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
125
126 return ret;
127}
128
129int tps65217_set_bits(struct tps65217 *tps, unsigned int reg,
130 unsigned int mask, unsigned int val, unsigned int level)
131{
132 return tps65217_update_bits(tps, reg, mask, val, level);
133}
134EXPORT_SYMBOL_GPL(tps65217_set_bits);
135
136int tps65217_clear_bits(struct tps65217 *tps, unsigned int reg,
137 unsigned int mask, unsigned int level)
138{
139 return tps65217_update_bits(tps, reg, mask, 0, level);
140}
141EXPORT_SYMBOL_GPL(tps65217_clear_bits);
142
143static struct regmap_config tps65217_regmap_config = {
144 .reg_bits = 8,
145 .val_bits = 8,
146};
147
AnilKumar Ch1922b0f2012-08-13 20:36:05 +0530148static const struct of_device_id tps65217_of_match[] = {
149 { .compatible = "ti,tps65217", .data = (void *)TPS65217 },
150 { /* sentinel */ },
151};
152
AnilKumar Chd48f4112012-01-11 16:11:41 +0530153static int __devinit tps65217_probe(struct i2c_client *client,
154 const struct i2c_device_id *ids)
155{
156 struct tps65217 *tps;
AnilKumar Chd48f4112012-01-11 16:11:41 +0530157 unsigned int version;
AnilKumar Ch1922b0f2012-08-13 20:36:05 +0530158 unsigned int chip_id = ids->driver_data;
159 const struct of_device_id *match;
160 int ret;
AnilKumar Chd48f4112012-01-11 16:11:41 +0530161
AnilKumar Ch1922b0f2012-08-13 20:36:05 +0530162 if (client->dev.of_node) {
163 match = of_match_device(tps65217_of_match, &client->dev);
164 if (!match) {
165 dev_err(&client->dev,
166 "Failed to find matching dt id\n");
167 return -EINVAL;
168 }
169 chip_id = (unsigned int)match->data;
170 }
171
172 if (!chip_id) {
173 dev_err(&client->dev, "id is null.\n");
174 return -ENODEV;
175 }
AnilKumar Cha7f1b632012-07-10 16:39:42 +0530176
AnilKumar Chd48f4112012-01-11 16:11:41 +0530177 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
178 if (!tps)
179 return -ENOMEM;
180
AnilKumar Ch1922b0f2012-08-13 20:36:05 +0530181 i2c_set_clientdata(client, tps);
182 tps->dev = &client->dev;
183 tps->id = chip_id;
184
Axel Lin0ef46192012-04-25 10:06:40 +0800185 tps->regmap = devm_regmap_init_i2c(client, &tps65217_regmap_config);
AnilKumar Chd48f4112012-01-11 16:11:41 +0530186 if (IS_ERR(tps->regmap)) {
187 ret = PTR_ERR(tps->regmap);
188 dev_err(tps->dev, "Failed to allocate register map: %d\n",
189 ret);
190 return ret;
191 }
192
AnilKumar Ch1922b0f2012-08-13 20:36:05 +0530193 ret = mfd_add_devices(tps->dev, -1, tps65217s,
Mark Brown0848c942012-09-11 15:16:36 +0800194 ARRAY_SIZE(tps65217s), NULL, 0, NULL);
AnilKumar Ch1922b0f2012-08-13 20:36:05 +0530195 if (ret < 0) {
196 dev_err(tps->dev, "mfd_add_devices failed: %d\n", ret);
197 return ret;
198 }
AnilKumar Chd48f4112012-01-11 16:11:41 +0530199
200 ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version);
201 if (ret < 0) {
Axel Lin0ef46192012-04-25 10:06:40 +0800202 dev_err(tps->dev, "Failed to read revision register: %d\n",
203 ret);
204 return ret;
AnilKumar Chd48f4112012-01-11 16:11:41 +0530205 }
206
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);
210
AnilKumar Chd48f4112012-01-11 16:11:41 +0530211 return 0;
AnilKumar Chd48f4112012-01-11 16:11:41 +0530212}
213
214static int __devexit tps65217_remove(struct i2c_client *client)
215{
216 struct tps65217 *tps = i2c_get_clientdata(client);
AnilKumar Chd48f4112012-01-11 16:11:41 +0530217
AnilKumar Ch1922b0f2012-08-13 20:36:05 +0530218 mfd_remove_devices(tps->dev);
AnilKumar Chd48f4112012-01-11 16:11:41 +0530219
AnilKumar Chd48f4112012-01-11 16:11:41 +0530220 return 0;
221}
222
223static const struct i2c_device_id tps65217_id_table[] = {
AnilKumar Ch1922b0f2012-08-13 20:36:05 +0530224 {"tps65217", TPS65217},
225 { /* sentinel */ }
AnilKumar Chd48f4112012-01-11 16:11:41 +0530226};
227MODULE_DEVICE_TABLE(i2c, tps65217_id_table);
228
229static struct i2c_driver tps65217_driver = {
230 .driver = {
231 .name = "tps65217",
AnilKumar Cha7f1b632012-07-10 16:39:42 +0530232 .owner = THIS_MODULE,
233 .of_match_table = of_match_ptr(tps65217_of_match),
AnilKumar Chd48f4112012-01-11 16:11:41 +0530234 },
235 .id_table = tps65217_id_table,
236 .probe = tps65217_probe,
237 .remove = __devexit_p(tps65217_remove),
238};
239
240static int __init tps65217_init(void)
241{
242 return i2c_add_driver(&tps65217_driver);
243}
244subsys_initcall(tps65217_init);
245
246static void __exit tps65217_exit(void)
247{
248 i2c_del_driver(&tps65217_driver);
249}
250module_exit(tps65217_exit);
251
252MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
253MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
254MODULE_LICENSE("GPL v2");