Merge remote-tracking branch 'regulator/fix/rk808' into regulator-linus
[deliverable/linux.git] / drivers / mfd / tps65217.c
CommitLineData
d48f411c
AC
1/*
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>
817bb7fb
AC
27#include <linux/of.h>
28#include <linux/of_device.h>
d48f411c
AC
29
30#include <linux/mfd/core.h>
31#include <linux/mfd/tps65217.h>
32
30fe2b5b 33static const struct mfd_cell tps65217s[] = {
817bb7fb
AC
34 {
35 .name = "tps65217-pmic",
36 },
b6290ffe
MK
37 {
38 .name = "tps65217-bl",
39 },
817bb7fb
AC
40};
41
d48f411c
AC
42/**
43 * tps65217_reg_read: Read a single tps65217 register.
44 *
45 * @tps: Device to read from.
46 * @reg: Register to read.
47 * @val: Contians the value
48 */
49int tps65217_reg_read(struct tps65217 *tps, unsigned int reg,
50 unsigned int *val)
51{
52 return regmap_read(tps->regmap, reg, val);
53}
54EXPORT_SYMBOL_GPL(tps65217_reg_read);
55
56/**
57 * tps65217_reg_write: Write a single tps65217 register.
58 *
59 * @tps65217: Device to write to.
60 * @reg: Register to write to.
61 * @val: Value to write.
62 * @level: Password protected level
63 */
64int tps65217_reg_write(struct tps65217 *tps, unsigned int reg,
65 unsigned int val, unsigned int level)
66{
67 int ret;
68 unsigned int xor_reg_val;
69
70 switch (level) {
71 case TPS65217_PROTECT_NONE:
72 return regmap_write(tps->regmap, reg, val);
73 case TPS65217_PROTECT_L1:
74 xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
75 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
76 xor_reg_val);
77 if (ret < 0)
78 return ret;
79
80 return regmap_write(tps->regmap, reg, val);
81 case TPS65217_PROTECT_L2:
82 xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
83 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
84 xor_reg_val);
85 if (ret < 0)
86 return ret;
87 ret = regmap_write(tps->regmap, reg, val);
88 if (ret < 0)
89 return ret;
90 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
91 xor_reg_val);
92 if (ret < 0)
93 return ret;
94 return regmap_write(tps->regmap, reg, val);
95 default:
96 return -EINVAL;
97 }
98}
99EXPORT_SYMBOL_GPL(tps65217_reg_write);
100
101/**
102 * tps65217_update_bits: Modify bits w.r.t mask, val and level.
103 *
104 * @tps65217: Device to write to.
105 * @reg: Register to read-write to.
106 * @mask: Mask.
107 * @val: Value to write.
108 * @level: Password protected level
109 */
27757e82 110static int tps65217_update_bits(struct tps65217 *tps, unsigned int reg,
d48f411c
AC
111 unsigned int mask, unsigned int val, unsigned int level)
112{
113 int ret;
114 unsigned int data;
115
116 ret = tps65217_reg_read(tps, reg, &data);
117 if (ret) {
118 dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
119 return ret;
120 }
121
122 data &= ~mask;
123 data |= val & mask;
124
125 ret = tps65217_reg_write(tps, reg, data, level);
126 if (ret)
127 dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
128
129 return ret;
130}
131
132int tps65217_set_bits(struct tps65217 *tps, unsigned int reg,
133 unsigned int mask, unsigned int val, unsigned int level)
134{
135 return tps65217_update_bits(tps, reg, mask, val, level);
136}
137EXPORT_SYMBOL_GPL(tps65217_set_bits);
138
139int tps65217_clear_bits(struct tps65217 *tps, unsigned int reg,
140 unsigned int mask, unsigned int level)
141{
142 return tps65217_update_bits(tps, reg, mask, 0, level);
143}
144EXPORT_SYMBOL_GPL(tps65217_clear_bits);
145
146static struct regmap_config tps65217_regmap_config = {
147 .reg_bits = 8,
148 .val_bits = 8,
0b496b4c
MB
149
150 .max_register = TPS65217_REG_MAX,
d48f411c
AC
151};
152
817bb7fb
AC
153static const struct of_device_id tps65217_of_match[] = {
154 { .compatible = "ti,tps65217", .data = (void *)TPS65217 },
155 { /* sentinel */ },
156};
157
f791be49 158static int tps65217_probe(struct i2c_client *client,
d48f411c
AC
159 const struct i2c_device_id *ids)
160{
161 struct tps65217 *tps;
d48f411c 162 unsigned int version;
5c6fbd56 163 unsigned long chip_id = ids->driver_data;
817bb7fb 164 const struct of_device_id *match;
eb433dad 165 bool status_off = false;
817bb7fb 166 int ret;
d48f411c 167
817bb7fb
AC
168 if (client->dev.of_node) {
169 match = of_match_device(tps65217_of_match, &client->dev);
170 if (!match) {
171 dev_err(&client->dev,
172 "Failed to find matching dt id\n");
173 return -EINVAL;
174 }
5c6fbd56 175 chip_id = (unsigned long)match->data;
eb433dad
CFP
176 status_off = of_property_read_bool(client->dev.of_node,
177 "ti,pmic-shutdown-controller");
817bb7fb
AC
178 }
179
180 if (!chip_id) {
181 dev_err(&client->dev, "id is null.\n");
182 return -ENODEV;
183 }
a7f1b63e 184
d48f411c
AC
185 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
186 if (!tps)
187 return -ENOMEM;
188
817bb7fb
AC
189 i2c_set_clientdata(client, tps);
190 tps->dev = &client->dev;
191 tps->id = chip_id;
192
0ef4619c 193 tps->regmap = devm_regmap_init_i2c(client, &tps65217_regmap_config);
d48f411c
AC
194 if (IS_ERR(tps->regmap)) {
195 ret = PTR_ERR(tps->regmap);
196 dev_err(tps->dev, "Failed to allocate register map: %d\n",
197 ret);
198 return ret;
199 }
200
817bb7fb 201 ret = mfd_add_devices(tps->dev, -1, tps65217s,
55692af5 202 ARRAY_SIZE(tps65217s), NULL, 0, NULL);
817bb7fb
AC
203 if (ret < 0) {
204 dev_err(tps->dev, "mfd_add_devices failed: %d\n", ret);
205 return ret;
206 }
d48f411c
AC
207
208 ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version);
209 if (ret < 0) {
0ef4619c
AL
210 dev_err(tps->dev, "Failed to read revision register: %d\n",
211 ret);
212 return ret;
d48f411c
AC
213 }
214
eb433dad
CFP
215 /* Set the PMIC to shutdown on PWR_EN toggle */
216 if (status_off) {
217 ret = tps65217_set_bits(tps, TPS65217_REG_STATUS,
218 TPS65217_STATUS_OFF, TPS65217_STATUS_OFF,
219 TPS65217_PROTECT_NONE);
220 if (ret)
221 dev_warn(tps->dev, "unable to set the status OFF\n");
222 }
223
d48f411c
AC
224 dev_info(tps->dev, "TPS65217 ID %#x version 1.%d\n",
225 (version & TPS65217_CHIPID_CHIP_MASK) >> 4,
226 version & TPS65217_CHIPID_REV_MASK);
227
d48f411c 228 return 0;
d48f411c
AC
229}
230
4740f73f 231static int tps65217_remove(struct i2c_client *client)
d48f411c
AC
232{
233 struct tps65217 *tps = i2c_get_clientdata(client);
d48f411c 234
817bb7fb 235 mfd_remove_devices(tps->dev);
d48f411c 236
d48f411c
AC
237 return 0;
238}
239
240static const struct i2c_device_id tps65217_id_table[] = {
817bb7fb
AC
241 {"tps65217", TPS65217},
242 { /* sentinel */ }
d48f411c
AC
243};
244MODULE_DEVICE_TABLE(i2c, tps65217_id_table);
245
246static struct i2c_driver tps65217_driver = {
247 .driver = {
248 .name = "tps65217",
a7f1b63e 249 .owner = THIS_MODULE,
a351451a 250 .of_match_table = tps65217_of_match,
d48f411c
AC
251 },
252 .id_table = tps65217_id_table,
253 .probe = tps65217_probe,
84449216 254 .remove = tps65217_remove,
d48f411c
AC
255};
256
257static int __init tps65217_init(void)
258{
259 return i2c_add_driver(&tps65217_driver);
260}
261subsys_initcall(tps65217_init);
262
263static void __exit tps65217_exit(void)
264{
265 i2c_del_driver(&tps65217_driver);
266}
267module_exit(tps65217_exit);
268
269MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
270MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
271MODULE_LICENSE("GPL v2");
This page took 0.221018 seconds and 5 git commands to generate.