blob: 41b5d598814d73e6d392c98648777e0c6eef4add [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
AnilKumar Chd48f4112012-01-11 16:11:41 +053018#include <linux/device.h>
AnilKumar Chd48f4112012-01-11 16:11:41 +053019#include <linux/err.h>
Marcin Niestroj6556bda2016-09-09 10:42:02 +020020#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/i2c.h>
23#include <linux/irq.h>
24#include <linux/irqdomain.h>
25#include <linux/kernel.h>
26#include <linux/module.h>
AnilKumar Ch817bb7f2012-08-13 20:36:05 +053027#include <linux/of.h>
28#include <linux/of_device.h>
Marcin Niestroj6556bda2016-09-09 10:42:02 +020029#include <linux/platform_device.h>
30#include <linux/regmap.h>
31#include <linux/slab.h>
AnilKumar Chd48f4112012-01-11 16:11:41 +053032
33#include <linux/mfd/core.h>
34#include <linux/mfd/tps65217.h>
35
Marcin Niestroj6556bda2016-09-09 10:42:02 +020036static struct resource charger_resources[] = {
37 DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_AC, "AC"),
38 DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_USB, "USB"),
39};
40
41struct tps65217_irq {
42 int mask;
43 int interrupt;
44};
45
46static const struct tps65217_irq tps65217_irqs[] = {
47 [TPS65217_IRQ_PB] = {
48 .mask = TPS65217_INT_PBM,
49 .interrupt = TPS65217_INT_PBI,
50 },
51 [TPS65217_IRQ_AC] = {
52 .mask = TPS65217_INT_ACM,
53 .interrupt = TPS65217_INT_ACI,
54 },
55 [TPS65217_IRQ_USB] = {
56 .mask = TPS65217_INT_USBM,
57 .interrupt = TPS65217_INT_USBI,
58 },
59};
60
61static void tps65217_irq_lock(struct irq_data *data)
62{
63 struct tps65217 *tps = irq_data_get_irq_chip_data(data);
64
65 mutex_lock(&tps->irq_lock);
66}
67
68static void tps65217_irq_sync_unlock(struct irq_data *data)
69{
70 struct tps65217 *tps = irq_data_get_irq_chip_data(data);
71 int ret;
72
73 ret = tps65217_reg_write(tps, TPS65217_REG_INT, tps->irq_mask,
74 TPS65217_PROTECT_NONE);
75 if (ret != 0)
76 dev_err(tps->dev, "Failed to sync IRQ masks\n");
77
78 mutex_unlock(&tps->irq_lock);
79}
80
81static const inline struct tps65217_irq *
82irq_to_tps65217_irq(struct tps65217 *tps, struct irq_data *data)
83{
84 return &tps65217_irqs[data->hwirq];
85}
86
87static void tps65217_irq_enable(struct irq_data *data)
88{
89 struct tps65217 *tps = irq_data_get_irq_chip_data(data);
90 const struct tps65217_irq *irq_data = irq_to_tps65217_irq(tps, data);
91
92 tps->irq_mask &= ~irq_data->mask;
93}
94
95static void tps65217_irq_disable(struct irq_data *data)
96{
97 struct tps65217 *tps = irq_data_get_irq_chip_data(data);
98 const struct tps65217_irq *irq_data = irq_to_tps65217_irq(tps, data);
99
100 tps->irq_mask |= irq_data->mask;
101}
102
103static struct irq_chip tps65217_irq_chip = {
104 .irq_bus_lock = tps65217_irq_lock,
105 .irq_bus_sync_unlock = tps65217_irq_sync_unlock,
106 .irq_enable = tps65217_irq_enable,
107 .irq_disable = tps65217_irq_disable,
108};
109
110static struct mfd_cell tps65217s[] = {
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530111 {
112 .name = "tps65217-pmic",
Johannes Pointner11d0d302014-09-25 08:31:42 +0200113 .of_compatible = "ti,tps65217-pmic",
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530114 },
Matthias Kaehlckeb6290ff2012-09-24 22:25:34 +0200115 {
116 .name = "tps65217-bl",
Johannes Pointner11d0d302014-09-25 08:31:42 +0200117 .of_compatible = "ti,tps65217-bl",
Matthias Kaehlckeb6290ff2012-09-24 22:25:34 +0200118 },
Enric Balletbo i Serra55cec672015-09-08 10:09:39 +0200119 {
120 .name = "tps65217-charger",
Marcin Niestroj6556bda2016-09-09 10:42:02 +0200121 .num_resources = ARRAY_SIZE(charger_resources),
122 .resources = charger_resources,
Enric Balletbo i Serra55cec672015-09-08 10:09:39 +0200123 .of_compatible = "ti,tps65217-charger",
124 },
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530125};
126
Marcin Niestroj6556bda2016-09-09 10:42:02 +0200127static irqreturn_t tps65217_irq_thread(int irq, void *data)
128{
129 struct tps65217 *tps = data;
130 unsigned int status;
131 bool handled = false;
132 int i;
133 int ret;
134
135 ret = tps65217_reg_read(tps, TPS65217_REG_INT, &status);
136 if (ret < 0) {
137 dev_err(tps->dev, "Failed to read IRQ status: %d\n",
138 ret);
139 return IRQ_NONE;
140 }
141
142 for (i = 0; i < ARRAY_SIZE(tps65217_irqs); i++) {
143 if (status & tps65217_irqs[i].interrupt) {
144 handle_nested_irq(irq_find_mapping(tps->irq_domain, i));
145 handled = true;
146 }
147 }
148
149 if (handled)
150 return IRQ_HANDLED;
151
152 return IRQ_NONE;
153}
154
155static int tps65217_irq_map(struct irq_domain *h, unsigned int virq,
156 irq_hw_number_t hw)
157{
158 struct tps65217 *tps = h->host_data;
159
160 irq_set_chip_data(virq, tps);
161 irq_set_chip_and_handler(virq, &tps65217_irq_chip, handle_edge_irq);
162 irq_set_nested_thread(virq, 1);
163 irq_set_parent(virq, tps->irq);
164 irq_set_noprobe(virq);
165
166 return 0;
167}
168
169static const struct irq_domain_ops tps65217_irq_domain_ops = {
170 .map = tps65217_irq_map,
171};
172
173static int tps65217_irq_init(struct tps65217 *tps, int irq)
174{
175 int ret;
176
177 mutex_init(&tps->irq_lock);
178 tps->irq = irq;
179
180 /* Mask all interrupt sources */
181 tps->irq_mask = (TPS65217_INT_RESERVEDM | TPS65217_INT_PBM
182 | TPS65217_INT_ACM | TPS65217_INT_USBM);
183 tps65217_reg_write(tps, TPS65217_REG_INT, tps->irq_mask,
184 TPS65217_PROTECT_NONE);
185
186 tps->irq_domain = irq_domain_add_linear(tps->dev->of_node,
187 TPS65217_NUM_IRQ, &tps65217_irq_domain_ops, tps);
188 if (!tps->irq_domain) {
189 dev_err(tps->dev, "Could not create IRQ domain\n");
190 return -ENOMEM;
191 }
192
193 ret = devm_request_threaded_irq(tps->dev, irq, NULL,
194 tps65217_irq_thread, IRQF_ONESHOT,
195 "tps65217-irq", tps);
196 if (ret) {
197 dev_err(tps->dev, "Failed to request IRQ %d: %d\n",
198 irq, ret);
199 return ret;
200 }
201
202 return 0;
203}
204
AnilKumar Chd48f4112012-01-11 16:11:41 +0530205/**
206 * tps65217_reg_read: Read a single tps65217 register.
207 *
208 * @tps: Device to read from.
209 * @reg: Register to read.
210 * @val: Contians the value
211 */
212int tps65217_reg_read(struct tps65217 *tps, unsigned int reg,
213 unsigned int *val)
214{
215 return regmap_read(tps->regmap, reg, val);
216}
217EXPORT_SYMBOL_GPL(tps65217_reg_read);
218
219/**
220 * tps65217_reg_write: Write a single tps65217 register.
221 *
222 * @tps65217: Device to write to.
223 * @reg: Register to write to.
224 * @val: Value to write.
225 * @level: Password protected level
226 */
227int tps65217_reg_write(struct tps65217 *tps, unsigned int reg,
228 unsigned int val, unsigned int level)
229{
230 int ret;
231 unsigned int xor_reg_val;
232
233 switch (level) {
234 case TPS65217_PROTECT_NONE:
235 return regmap_write(tps->regmap, reg, val);
236 case TPS65217_PROTECT_L1:
237 xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
238 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
239 xor_reg_val);
240 if (ret < 0)
241 return ret;
242
243 return regmap_write(tps->regmap, reg, val);
244 case TPS65217_PROTECT_L2:
245 xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
246 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
247 xor_reg_val);
248 if (ret < 0)
249 return ret;
250 ret = regmap_write(tps->regmap, reg, val);
251 if (ret < 0)
252 return ret;
253 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
254 xor_reg_val);
255 if (ret < 0)
256 return ret;
257 return regmap_write(tps->regmap, reg, val);
258 default:
259 return -EINVAL;
260 }
261}
262EXPORT_SYMBOL_GPL(tps65217_reg_write);
263
264/**
265 * tps65217_update_bits: Modify bits w.r.t mask, val and level.
266 *
267 * @tps65217: Device to write to.
268 * @reg: Register to read-write to.
269 * @mask: Mask.
270 * @val: Value to write.
271 * @level: Password protected level
272 */
Mark Brown27757e82012-05-09 21:18:05 +0100273static int tps65217_update_bits(struct tps65217 *tps, unsigned int reg,
AnilKumar Chd48f4112012-01-11 16:11:41 +0530274 unsigned int mask, unsigned int val, unsigned int level)
275{
276 int ret;
277 unsigned int data;
278
279 ret = tps65217_reg_read(tps, reg, &data);
280 if (ret) {
281 dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
282 return ret;
283 }
284
285 data &= ~mask;
286 data |= val & mask;
287
288 ret = tps65217_reg_write(tps, reg, data, level);
289 if (ret)
290 dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
291
292 return ret;
293}
294
295int tps65217_set_bits(struct tps65217 *tps, unsigned int reg,
296 unsigned int mask, unsigned int val, unsigned int level)
297{
298 return tps65217_update_bits(tps, reg, mask, val, level);
299}
300EXPORT_SYMBOL_GPL(tps65217_set_bits);
301
302int tps65217_clear_bits(struct tps65217 *tps, unsigned int reg,
303 unsigned int mask, unsigned int level)
304{
305 return tps65217_update_bits(tps, reg, mask, 0, level);
306}
307EXPORT_SYMBOL_GPL(tps65217_clear_bits);
308
Marcin Niestroj6556bda2016-09-09 10:42:02 +0200309static bool tps65217_volatile_reg(struct device *dev, unsigned int reg)
310{
311 switch (reg) {
312 case TPS65217_REG_INT:
313 return true;
314 default:
315 return false;
316 }
317}
318
Krzysztof Kozlowskiaf0a8372015-01-05 10:01:30 +0100319static const struct regmap_config tps65217_regmap_config = {
AnilKumar Chd48f4112012-01-11 16:11:41 +0530320 .reg_bits = 8,
321 .val_bits = 8,
Mark Brown0b496b42014-09-05 22:16:18 +0100322
323 .max_register = TPS65217_REG_MAX,
Marcin Niestroj6556bda2016-09-09 10:42:02 +0200324 .volatile_reg = tps65217_volatile_reg,
AnilKumar Chd48f4112012-01-11 16:11:41 +0530325};
326
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530327static const struct of_device_id tps65217_of_match[] = {
328 { .compatible = "ti,tps65217", .data = (void *)TPS65217 },
329 { /* sentinel */ },
330};
Javier Martinez Canillas4895e492015-07-30 18:18:41 +0200331MODULE_DEVICE_TABLE(of, tps65217_of_match);
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530332
Bill Pembertonf791be42012-11-19 13:23:04 -0500333static int tps65217_probe(struct i2c_client *client,
AnilKumar Chd48f4112012-01-11 16:11:41 +0530334 const struct i2c_device_id *ids)
335{
336 struct tps65217 *tps;
AnilKumar Chd48f4112012-01-11 16:11:41 +0530337 unsigned int version;
Lee Jones5c6fbd52014-02-03 08:24:20 +0000338 unsigned long chip_id = ids->driver_data;
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530339 const struct of_device_id *match;
Colin Foe-Parkereb433da2012-11-20 15:18:44 +0530340 bool status_off = false;
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530341 int ret;
AnilKumar Chd48f4112012-01-11 16:11:41 +0530342
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530343 if (client->dev.of_node) {
344 match = of_match_device(tps65217_of_match, &client->dev);
345 if (!match) {
346 dev_err(&client->dev,
347 "Failed to find matching dt id\n");
348 return -EINVAL;
349 }
Lee Jones5c6fbd52014-02-03 08:24:20 +0000350 chip_id = (unsigned long)match->data;
Colin Foe-Parkereb433da2012-11-20 15:18:44 +0530351 status_off = of_property_read_bool(client->dev.of_node,
352 "ti,pmic-shutdown-controller");
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530353 }
354
355 if (!chip_id) {
356 dev_err(&client->dev, "id is null.\n");
357 return -ENODEV;
358 }
AnilKumar Cha7f1b632012-07-10 16:39:42 +0530359
AnilKumar Chd48f4112012-01-11 16:11:41 +0530360 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
361 if (!tps)
362 return -ENOMEM;
363
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530364 i2c_set_clientdata(client, tps);
365 tps->dev = &client->dev;
366 tps->id = chip_id;
367
Axel Lin0ef46192012-04-25 10:06:40 +0800368 tps->regmap = devm_regmap_init_i2c(client, &tps65217_regmap_config);
AnilKumar Chd48f4112012-01-11 16:11:41 +0530369 if (IS_ERR(tps->regmap)) {
370 ret = PTR_ERR(tps->regmap);
371 dev_err(tps->dev, "Failed to allocate register map: %d\n",
372 ret);
373 return ret;
374 }
375
Marcin Niestroj6556bda2016-09-09 10:42:02 +0200376 if (client->irq) {
377 tps65217_irq_init(tps, client->irq);
378 } else {
379 int i;
380
381 /* Don't tell children about IRQ resources which won't fire */
382 for (i = 0; i < ARRAY_SIZE(tps65217s); i++)
383 tps65217s[i].num_resources = 0;
384 }
385
Laxman Dewanganb89b6b62016-04-08 00:13:12 +0530386 ret = devm_mfd_add_devices(tps->dev, -1, tps65217s,
Marcin Niestroj6556bda2016-09-09 10:42:02 +0200387 ARRAY_SIZE(tps65217s), NULL, 0,
388 tps->irq_domain);
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530389 if (ret < 0) {
390 dev_err(tps->dev, "mfd_add_devices failed: %d\n", ret);
391 return ret;
392 }
AnilKumar Chd48f4112012-01-11 16:11:41 +0530393
394 ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version);
395 if (ret < 0) {
Axel Lin0ef46192012-04-25 10:06:40 +0800396 dev_err(tps->dev, "Failed to read revision register: %d\n",
397 ret);
398 return ret;
AnilKumar Chd48f4112012-01-11 16:11:41 +0530399 }
400
Colin Foe-Parkereb433da2012-11-20 15:18:44 +0530401 /* Set the PMIC to shutdown on PWR_EN toggle */
402 if (status_off) {
403 ret = tps65217_set_bits(tps, TPS65217_REG_STATUS,
404 TPS65217_STATUS_OFF, TPS65217_STATUS_OFF,
405 TPS65217_PROTECT_NONE);
406 if (ret)
407 dev_warn(tps->dev, "unable to set the status OFF\n");
408 }
409
AnilKumar Chd48f4112012-01-11 16:11:41 +0530410 dev_info(tps->dev, "TPS65217 ID %#x version 1.%d\n",
411 (version & TPS65217_CHIPID_CHIP_MASK) >> 4,
412 version & TPS65217_CHIPID_REV_MASK);
413
AnilKumar Chd48f4112012-01-11 16:11:41 +0530414 return 0;
AnilKumar Chd48f4112012-01-11 16:11:41 +0530415}
416
AnilKumar Chd48f4112012-01-11 16:11:41 +0530417static const struct i2c_device_id tps65217_id_table[] = {
AnilKumar Ch817bb7f2012-08-13 20:36:05 +0530418 {"tps65217", TPS65217},
419 { /* sentinel */ }
AnilKumar Chd48f4112012-01-11 16:11:41 +0530420};
421MODULE_DEVICE_TABLE(i2c, tps65217_id_table);
422
423static struct i2c_driver tps65217_driver = {
424 .driver = {
425 .name = "tps65217",
Sachin Kamata3514512013-10-15 09:18:47 +0530426 .of_match_table = tps65217_of_match,
AnilKumar Chd48f4112012-01-11 16:11:41 +0530427 },
428 .id_table = tps65217_id_table,
429 .probe = tps65217_probe,
AnilKumar Chd48f4112012-01-11 16:11:41 +0530430};
431
432static int __init tps65217_init(void)
433{
434 return i2c_add_driver(&tps65217_driver);
435}
436subsys_initcall(tps65217_init);
437
438static void __exit tps65217_exit(void)
439{
440 i2c_del_driver(&tps65217_driver);
441}
442module_exit(tps65217_exit);
443
444MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
445MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
446MODULE_LICENSE("GPL v2");