blob: 13834a0d28172fe5334f0385571fafb34cc21248 [file] [log] [blame]
Keerthy44b4dc62014-02-06 11:20:12 +05301/*
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
Keerthy45311562016-09-19 13:09:05 +053036static const struct mfd_cell tps65218_cells[] = {
37 {
38 .name = "tps65218-pwrbutton",
39 .of_compatible = "ti,tps65218-pwrbutton",
40 },
41 {
42 .name = "tps65218-gpio",
43 .of_compatible = "ti,tps65218-gpio",
44 },
45 { .name = "tps65218-regulator", },
46};
47
Keerthy44b4dc62014-02-06 11:20:12 +053048/**
Keerthy44b4dc62014-02-06 11:20:12 +053049 * tps65218_reg_write: Write a single tps65218 register.
50 *
51 * @tps65218: Device to write to.
52 * @reg: Register to write to.
53 * @val: Value to write.
54 * @level: Password protected level
55 */
56int tps65218_reg_write(struct tps65218 *tps, unsigned int reg,
57 unsigned int val, unsigned int level)
58{
59 int ret;
60 unsigned int xor_reg_val;
61
62 switch (level) {
63 case TPS65218_PROTECT_NONE:
64 return regmap_write(tps->regmap, reg, val);
65 case TPS65218_PROTECT_L1:
66 xor_reg_val = reg ^ TPS65218_PASSWORD_REGS_UNLOCK;
67 ret = regmap_write(tps->regmap, TPS65218_REG_PASSWORD,
68 xor_reg_val);
69 if (ret < 0)
70 return ret;
71
72 return regmap_write(tps->regmap, reg, val);
73 default:
74 return -EINVAL;
75 }
76}
77EXPORT_SYMBOL_GPL(tps65218_reg_write);
78
79/**
80 * tps65218_update_bits: Modify bits w.r.t mask, val and level.
81 *
82 * @tps65218: Device to write to.
83 * @reg: Register to read-write to.
84 * @mask: Mask.
85 * @val: Value to write.
86 * @level: Password protected level
87 */
88static int tps65218_update_bits(struct tps65218 *tps, unsigned int reg,
89 unsigned int mask, unsigned int val, unsigned int level)
90{
91 int ret;
92 unsigned int data;
93
Keerthy0aced352016-09-19 13:09:02 +053094 ret = regmap_read(tps->regmap, reg, &data);
Keerthy44b4dc62014-02-06 11:20:12 +053095 if (ret) {
96 dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
97 return ret;
98 }
99
100 data &= ~mask;
101 data |= val & mask;
102
103 mutex_lock(&tps->tps_lock);
104 ret = tps65218_reg_write(tps, reg, data, level);
105 if (ret)
106 dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
107 mutex_unlock(&tps->tps_lock);
108
109 return ret;
110}
111
112int tps65218_set_bits(struct tps65218 *tps, unsigned int reg,
113 unsigned int mask, unsigned int val, unsigned int level)
114{
115 return tps65218_update_bits(tps, reg, mask, val, level);
116}
117EXPORT_SYMBOL_GPL(tps65218_set_bits);
118
119int tps65218_clear_bits(struct tps65218 *tps, unsigned int reg,
120 unsigned int mask, unsigned int level)
121{
122 return tps65218_update_bits(tps, reg, mask, 0, level);
123}
124EXPORT_SYMBOL_GPL(tps65218_clear_bits);
125
Felipe Balbi773328d2014-12-26 13:28:20 -0600126static const struct regmap_range tps65218_yes_ranges[] = {
127 regmap_reg_range(TPS65218_REG_INT1, TPS65218_REG_INT2),
128 regmap_reg_range(TPS65218_REG_STATUS, TPS65218_REG_STATUS),
129};
130
131static const struct regmap_access_table tps65218_volatile_table = {
132 .yes_ranges = tps65218_yes_ranges,
133 .n_yes_ranges = ARRAY_SIZE(tps65218_yes_ranges),
134};
135
Krzysztof Kozlowski18bb3992015-01-05 10:01:31 +0100136static const struct regmap_config tps65218_regmap_config = {
Keerthy44b4dc62014-02-06 11:20:12 +0530137 .reg_bits = 8,
138 .val_bits = 8,
139 .cache_type = REGCACHE_RBTREE,
Felipe Balbi773328d2014-12-26 13:28:20 -0600140 .volatile_table = &tps65218_volatile_table,
Keerthy44b4dc62014-02-06 11:20:12 +0530141};
142
143static const struct regmap_irq tps65218_irqs[] = {
144 /* INT1 IRQs */
145 [TPS65218_PRGC_IRQ] = {
146 .mask = TPS65218_INT1_PRGC,
147 },
148 [TPS65218_CC_AQC_IRQ] = {
149 .mask = TPS65218_INT1_CC_AQC,
150 },
151 [TPS65218_HOT_IRQ] = {
152 .mask = TPS65218_INT1_HOT,
153 },
154 [TPS65218_PB_IRQ] = {
155 .mask = TPS65218_INT1_PB,
156 },
157 [TPS65218_AC_IRQ] = {
158 .mask = TPS65218_INT1_AC,
159 },
160 [TPS65218_VPRG_IRQ] = {
161 .mask = TPS65218_INT1_VPRG,
162 },
163 [TPS65218_INVALID1_IRQ] = {
164 },
165 [TPS65218_INVALID2_IRQ] = {
166 },
167 /* INT2 IRQs*/
168 [TPS65218_LS1_I_IRQ] = {
169 .mask = TPS65218_INT2_LS1_I,
170 .reg_offset = 1,
171 },
172 [TPS65218_LS2_I_IRQ] = {
173 .mask = TPS65218_INT2_LS2_I,
174 .reg_offset = 1,
175 },
176 [TPS65218_LS3_I_IRQ] = {
177 .mask = TPS65218_INT2_LS3_I,
178 .reg_offset = 1,
179 },
180 [TPS65218_LS1_F_IRQ] = {
181 .mask = TPS65218_INT2_LS1_F,
182 .reg_offset = 1,
183 },
184 [TPS65218_LS2_F_IRQ] = {
185 .mask = TPS65218_INT2_LS2_F,
186 .reg_offset = 1,
187 },
188 [TPS65218_LS3_F_IRQ] = {
189 .mask = TPS65218_INT2_LS3_F,
190 .reg_offset = 1,
191 },
192 [TPS65218_INVALID3_IRQ] = {
193 },
194 [TPS65218_INVALID4_IRQ] = {
195 },
196};
197
198static struct regmap_irq_chip tps65218_irq_chip = {
199 .name = "tps65218",
200 .irqs = tps65218_irqs,
201 .num_irqs = ARRAY_SIZE(tps65218_irqs),
202
203 .num_regs = 2,
204 .mask_base = TPS65218_REG_INT_MASK1,
Felipe Balbif29ae362014-12-26 13:28:21 -0600205 .status_base = TPS65218_REG_INT1,
Keerthy44b4dc62014-02-06 11:20:12 +0530206};
207
208static const struct of_device_id of_tps65218_match_table[] = {
209 { .compatible = "ti,tps65218", },
Stephen Boyd83205132014-05-23 17:16:52 -0700210 {}
Keerthy44b4dc62014-02-06 11:20:12 +0530211};
Javier Martinez Canillas4895e492015-07-30 18:18:41 +0200212MODULE_DEVICE_TABLE(of, of_tps65218_match_table);
Keerthy44b4dc62014-02-06 11:20:12 +0530213
214static 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;
Tero Kristof11fa172016-08-10 17:53:54 +0530220 unsigned int chipid;
Keerthy44b4dc62014-02-06 11:20:12 +0530221
222 match = of_match_device(of_tps65218_match_table, &client->dev);
223 if (!match) {
224 dev_err(&client->dev,
225 "Failed to find matching dt id\n");
226 return -EINVAL;
227 }
228
229 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
230 if (!tps)
231 return -ENOMEM;
232
233 i2c_set_clientdata(client, tps);
234 tps->dev = &client->dev;
235 tps->irq = client->irq;
236 tps->regmap = devm_regmap_init_i2c(client, &tps65218_regmap_config);
237 if (IS_ERR(tps->regmap)) {
238 ret = PTR_ERR(tps->regmap);
239 dev_err(tps->dev, "Failed to allocate register map: %d\n",
240 ret);
241 return ret;
242 }
243
244 mutex_init(&tps->tps_lock);
245
246 ret = regmap_add_irq_chip(tps->regmap, tps->irq,
247 IRQF_ONESHOT, 0, &tps65218_irq_chip,
248 &tps->irq_data);
249 if (ret < 0)
250 return ret;
251
Keerthy0aced352016-09-19 13:09:02 +0530252 ret = regmap_read(tps->regmap, TPS65218_REG_CHIPID, &chipid);
Tero Kristof11fa172016-08-10 17:53:54 +0530253 if (ret) {
254 dev_err(tps->dev, "Failed to read chipid: %d\n", ret);
255 return ret;
256 }
257
258 tps->rev = chipid & TPS65218_CHIPID_REV_MASK;
259
Keerthy45311562016-09-19 13:09:05 +0530260 ret = mfd_add_devices(tps->dev, PLATFORM_DEVID_AUTO, tps65218_cells,
261 ARRAY_SIZE(tps65218_cells), NULL, 0,
262 regmap_irq_get_domain(tps->irq_data));
263
Keerthy44b4dc62014-02-06 11:20:12 +0530264 if (ret < 0)
265 goto err_irq;
266
267 return 0;
268
269err_irq:
270 regmap_del_irq_chip(tps->irq, tps->irq_data);
271
272 return ret;
273}
274
275static int tps65218_remove(struct i2c_client *client)
276{
277 struct tps65218 *tps = i2c_get_clientdata(client);
278
279 regmap_del_irq_chip(tps->irq, tps->irq_data);
280
281 return 0;
282}
283
284static const struct i2c_device_id tps65218_id_table[] = {
285 { "tps65218", TPS65218 },
286 { },
287};
288MODULE_DEVICE_TABLE(i2c, tps65218_id_table);
289
290static struct i2c_driver tps65218_driver = {
291 .driver = {
292 .name = "tps65218",
Keerthy44b4dc62014-02-06 11:20:12 +0530293 .of_match_table = of_tps65218_match_table,
294 },
295 .probe = tps65218_probe,
296 .remove = tps65218_remove,
297 .id_table = tps65218_id_table,
298};
299
300module_i2c_driver(tps65218_driver);
301
302MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
303MODULE_DESCRIPTION("TPS65218 chip family multi-function driver");
304MODULE_LICENSE("GPL v2");