summaryrefslogtreecommitdiff
path: root/drivers/mfd/qcom-i2c-pmic.c
blob: 590e4c1a3f52437270c386ea66a77ae774994bab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/* Copyright (c) 2016-2017 The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#define pr_fmt(fmt) "I2C PMIC: %s: " fmt, __func__

#include <linux/bitops.h>
#include <linux/i2c.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/irqdomain.h>
#include <linux/module.h>
#include <linux/of_platform.h>
#include <linux/pinctrl/consumer.h>
#include <linux/regmap.h>
#include <linux/slab.h>

#define I2C_INTR_STATUS_BASE	0x0550
#define INT_RT_STS_OFFSET	0x10
#define INT_SET_TYPE_OFFSET	0x11
#define INT_POL_HIGH_OFFSET	0x12
#define INT_POL_LOW_OFFSET	0x13
#define INT_LATCHED_CLR_OFFSET	0x14
#define INT_EN_SET_OFFSET	0x15
#define INT_EN_CLR_OFFSET	0x16
#define INT_LATCHED_STS_OFFSET	0x18
#define INT_PENDING_STS_OFFSET	0x19
#define INT_MID_SEL_OFFSET	0x1A
#define INT_MID_SEL_MASK	GENMASK(1, 0)
#define INT_PRIORITY_OFFSET	0x1B
#define INT_PRIORITY_BIT	BIT(0)

enum {
	IRQ_SET_TYPE = 0,
	IRQ_POL_HIGH,
	IRQ_POL_LOW,
	IRQ_LATCHED_CLR, /* not needed but makes life easy */
	IRQ_EN_SET,
	IRQ_MAX_REGS,
};

struct i2c_pmic_periph {
	void		*data;
	u16		addr;
	u8		cached[IRQ_MAX_REGS];
	u8		synced[IRQ_MAX_REGS];
	u8		wake;
	struct mutex	lock;
};

struct i2c_pmic {
	struct device		*dev;
	struct regmap		*regmap;
	struct irq_domain	*domain;
	struct i2c_pmic_periph	*periph;
	struct pinctrl		*pinctrl;
	const char		*pinctrl_name;
	int			num_periphs;
};

static void i2c_pmic_irq_bus_lock(struct irq_data *d)
{
	struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d);

	mutex_lock(&periph->lock);
}

static void i2c_pmic_sync_type_polarity(struct i2c_pmic *chip,
			       struct i2c_pmic_periph *periph)
{
	int rc;

	/* did any irq type change? */
	if (periph->cached[IRQ_SET_TYPE] ^ periph->synced[IRQ_SET_TYPE]) {
		rc = regmap_write(chip->regmap,
				  periph->addr | INT_SET_TYPE_OFFSET,
				  periph->cached[IRQ_SET_TYPE]);
		if (rc < 0) {
			pr_err("Couldn't set periph 0x%04x irqs 0x%02x type rc=%d\n",
				periph->addr, periph->cached[IRQ_SET_TYPE], rc);
			return;
		}

		periph->synced[IRQ_SET_TYPE] = periph->cached[IRQ_SET_TYPE];
	}

	/* did any polarity high change? */
	if (periph->cached[IRQ_POL_HIGH] ^ periph->synced[IRQ_POL_HIGH]) {
		rc = regmap_write(chip->regmap,
				  periph->addr | INT_POL_HIGH_OFFSET,
				  periph->cached[IRQ_POL_HIGH]);
		if (rc < 0) {
			pr_err("Couldn't set periph 0x%04x irqs 0x%02x polarity high rc=%d\n",
				periph->addr, periph->cached[IRQ_POL_HIGH], rc);
			return;
		}

		periph->synced[IRQ_POL_HIGH] = periph->cached[IRQ_POL_HIGH];
	}

	/* did any polarity low change? */
	if (periph->cached[IRQ_POL_LOW] ^ periph->synced[IRQ_POL_LOW]) {
		rc = regmap_write(chip->regmap,
				  periph->addr | INT_POL_LOW_OFFSET,
				  periph->cached[IRQ_POL_LOW]);
		if (rc < 0) {
			pr_err("Couldn't set periph 0x%04x irqs 0x%02x polarity low rc=%d\n",
				periph->addr, periph->cached[IRQ_POL_LOW], rc);
			return;
		}

		periph->synced[IRQ_POL_LOW] = periph->cached[IRQ_POL_LOW];
	}
}

static void i2c_pmic_sync_enable(struct i2c_pmic *chip,
				 struct i2c_pmic_periph *periph)
{
	u8 en_set, en_clr;
	int rc;

	/* determine which irqs were enabled and which were disabled */
	en_clr = periph->synced[IRQ_EN_SET] & ~periph->cached[IRQ_EN_SET];
	en_set = ~periph->synced[IRQ_EN_SET] & periph->cached[IRQ_EN_SET];

	/* were any irqs disabled? */
	if (en_clr) {
		rc = regmap_write(chip->regmap,
				  periph->addr | INT_EN_CLR_OFFSET, en_clr);
		if (rc < 0) {
			pr_err("Couldn't disable periph 0x%04x irqs 0x%02x rc=%d\n",
				periph->addr, en_clr, rc);
			return;
		}
	}

	/* were any irqs enabled? */
	if (en_set) {
		rc = regmap_write(chip->regmap,
				  periph->addr | INT_EN_SET_OFFSET, en_set);
		if (rc < 0) {
			pr_err("Couldn't enable periph 0x%04x irqs 0x%02x rc=%d\n",
				periph->addr, en_set, rc);
			return;
		}
	}

	/* irq enabled status was written to hardware */
	periph->synced[IRQ_EN_SET] = periph->cached[IRQ_EN_SET];
}

static void i2c_pmic_irq_bus_sync_unlock(struct irq_data *d)
{
	struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d);
	struct i2c_pmic *chip = periph->data;

	i2c_pmic_sync_type_polarity(chip, periph);
	i2c_pmic_sync_enable(chip, periph);
	mutex_unlock(&periph->lock);
}

static void i2c_pmic_irq_disable(struct irq_data *d)
{
	struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d);

	periph->cached[IRQ_EN_SET] &= ~d->hwirq & 0xFF;
}

static void i2c_pmic_irq_enable(struct irq_data *d)
{
	struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d);

	periph->cached[IRQ_EN_SET] |= d->hwirq & 0xFF;
}

static int i2c_pmic_irq_set_type(struct irq_data *d, unsigned int irq_type)
{
	struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d);

	switch (irq_type) {
	case IRQ_TYPE_EDGE_RISING:
		periph->cached[IRQ_SET_TYPE] |= d->hwirq & 0xFF;
		periph->cached[IRQ_POL_HIGH] |= d->hwirq & 0xFF;
		periph->cached[IRQ_POL_LOW] &= ~d->hwirq & 0xFF;
		break;
	case IRQ_TYPE_EDGE_FALLING:
		periph->cached[IRQ_SET_TYPE] |= d->hwirq & 0xFF;
		periph->cached[IRQ_POL_HIGH] &= ~d->hwirq & 0xFF;
		periph->cached[IRQ_POL_LOW] |= d->hwirq & 0xFF;
		break;
	case IRQ_TYPE_EDGE_BOTH:
		periph->cached[IRQ_SET_TYPE] |= d->hwirq & 0xFF;
		periph->cached[IRQ_POL_HIGH] |= d->hwirq & 0xFF;
		periph->cached[IRQ_POL_LOW] |= d->hwirq & 0xFF;
		break;
	case IRQ_TYPE_LEVEL_HIGH:
		periph->cached[IRQ_SET_TYPE] &= ~d->hwirq & 0xFF;
		periph->cached[IRQ_POL_HIGH] |= d->hwirq & 0xFF;
		periph->cached[IRQ_POL_LOW] &= ~d->hwirq & 0xFF;
		break;
	case IRQ_TYPE_LEVEL_LOW:
		periph->cached[IRQ_SET_TYPE] &= ~d->hwirq & 0xFF;
		periph->cached[IRQ_POL_HIGH] &= ~d->hwirq & 0xFF;
		periph->cached[IRQ_POL_LOW] |= d->hwirq & 0xFF;
		break;
	default:
		pr_err("irq type 0x%04x is not supported\n", irq_type);
		return -EINVAL;
	}

	return 0;
}

#ifdef CONFIG_PM_SLEEP
static int i2c_pmic_irq_set_wake(struct irq_data *d, unsigned int on)
{
	struct i2c_pmic_periph *periph = irq_data_get_irq_chip_data(d);

	if (on)
		periph->wake |= d->hwirq & 0xFF;
	else
		periph->wake &= ~d->hwirq & 0xFF;

	return 0;
}
#else
#define i2c_pmic_irq_set_wake NULL
#endif

static struct irq_chip i2c_pmic_irq_chip = {
	.name			= "i2c_pmic_irq_chip",
	.irq_bus_lock		= i2c_pmic_irq_bus_lock,
	.irq_bus_sync_unlock	= i2c_pmic_irq_bus_sync_unlock,
	.irq_disable		= i2c_pmic_irq_disable,
	.irq_enable		= i2c_pmic_irq_enable,
	.irq_set_type		= i2c_pmic_irq_set_type,
	.irq_set_wake		= i2c_pmic_irq_set_wake,
};

static struct i2c_pmic_periph *i2c_pmic_find_periph(struct i2c_pmic *chip,
						    irq_hw_number_t hwirq)
{
	int i;

	for (i = 0; i < chip->num_periphs; i++)
		if (chip->periph[i].addr == (hwirq & 0xFF00))
			return &chip->periph[i];

	pr_err_ratelimited("Couldn't find periph struct for hwirq 0x%04lx\n",
			   hwirq);
	return NULL;
}

static int i2c_pmic_domain_map(struct irq_domain *d, unsigned int virq,
			irq_hw_number_t hwirq)
{
	struct i2c_pmic *chip = d->host_data;
	struct i2c_pmic_periph *periph = i2c_pmic_find_periph(chip, hwirq);

	if (!periph)
		return -ENODEV;

	irq_set_chip_data(virq, periph);
	irq_set_chip_and_handler(virq, &i2c_pmic_irq_chip, handle_level_irq);
	irq_set_nested_thread(virq, 1);
	irq_set_noprobe(virq);
	return 0;
}

static int i2c_pmic_domain_xlate(struct irq_domain *d,
				 struct device_node *ctrlr, const u32 *intspec,
				 unsigned int intsize, unsigned long *out_hwirq,
				 unsigned int *out_type)
{
	if (intsize != 3)
		return -EINVAL;

	if (intspec[0] > 0xFF || intspec[1] > 0x7 ||
					intspec[2] > IRQ_TYPE_SENSE_MASK)
		return -EINVAL;

	/*
	 * Interrupt specifiers are triplets
	 * <peripheral-address, irq-number, IRQ_TYPE_*>
	 *
	 * peripheral-address - The base address of the peripheral
	 * irq-number	      - The zero based bit position of the peripheral's
	 *			interrupt registers corresponding to the irq
	 *			where the LSB is 0 and the MSB is 7
	 * IRQ_TYPE_*	      - Please refer to linux/irq.h
	 */
	*out_hwirq = intspec[0] << 8 | BIT(intspec[1]);
	*out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;

	return 0;
}

static const struct irq_domain_ops i2c_pmic_domain_ops = {
	.map	= i2c_pmic_domain_map,
	.xlate	= i2c_pmic_domain_xlate,
};

static void i2c_pmic_irq_ack_now(struct i2c_pmic *chip, u16 hwirq)
{
	int rc;

	rc = regmap_write(chip->regmap,
			  (hwirq & 0xFF00) | INT_LATCHED_CLR_OFFSET,
			  hwirq & 0xFF);
	if (rc < 0)
		pr_err_ratelimited("Couldn't ack 0x%04x rc=%d\n", hwirq, rc);
}

static void i2c_pmic_irq_disable_now(struct i2c_pmic *chip, u16 hwirq)
{
	struct i2c_pmic_periph *periph = i2c_pmic_find_periph(chip, hwirq);
	int rc;

	if (!periph)
		return;

	mutex_lock(&periph->lock);
	periph->cached[IRQ_EN_SET] &= ~hwirq & 0xFF;

	rc = regmap_write(chip->regmap,
			  (hwirq & 0xFF00) | INT_EN_CLR_OFFSET,
			  hwirq & 0xFF);
	if (rc < 0) {
		pr_err_ratelimited("Couldn't disable irq 0x%04x rc=%d\n",
				   hwirq, rc);
		goto unlock;
	}

	periph->synced[IRQ_EN_SET] = periph->cached[IRQ_EN_SET];

unlock:
	mutex_unlock(&periph->lock);
}

static void i2c_pmic_periph_status_handler(struct i2c_pmic *chip,
					   u16 periph_address, u8 periph_status)
{
	unsigned int hwirq, virq;
	int i;

	while (periph_status) {
		i = ffs(periph_status) - 1;
		periph_status &= ~BIT(i);
		hwirq = periph_address | BIT(i);
		virq = irq_find_mapping(chip->domain, hwirq);
		if (virq == 0) {
			pr_err_ratelimited("Couldn't find mapping; disabling 0x%04x\n",
					   hwirq);
			i2c_pmic_irq_disable_now(chip, hwirq);
			continue;
		}

		handle_nested_irq(virq);
		i2c_pmic_irq_ack_now(chip, hwirq);
	}
}

static void i2c_pmic_summary_status_handler(struct i2c_pmic *chip,
					    struct i2c_pmic_periph *periph,
					    u8 summary_status)
{
	unsigned int periph_status;
	int rc, i;

	while (summary_status) {
		i = ffs(summary_status) - 1;
		summary_status &= ~BIT(i);

		rc = regmap_read(chip->regmap,
				 periph[i].addr | INT_LATCHED_STS_OFFSET,
				 &periph_status);
		if (rc < 0) {
			pr_err_ratelimited("Couldn't read 0x%04x | INT_LATCHED_STS rc=%d\n",
					   periph[i].addr, rc);
			continue;
		}

		i2c_pmic_periph_status_handler(chip, periph[i].addr,
					       periph_status);
	}
}

static irqreturn_t i2c_pmic_irq_handler(int irq, void *dev_id)
{
	struct i2c_pmic *chip = dev_id;
	struct i2c_pmic_periph *periph;
	unsigned int summary_status;
	int rc, i;

	for (i = 0; i < DIV_ROUND_UP(chip->num_periphs, BITS_PER_BYTE); i++) {
		rc = regmap_read(chip->regmap, I2C_INTR_STATUS_BASE + i,
				&summary_status);
		if (rc < 0) {
			pr_err_ratelimited("Couldn't read I2C_INTR_STATUS%d rc=%d\n",
					   i, rc);
			continue;
		}

		if (summary_status == 0)
			continue;

		periph = &chip->periph[i * 8];
		i2c_pmic_summary_status_handler(chip, periph, summary_status);
	}

	return IRQ_HANDLED;
}

static int i2c_pmic_parse_dt(struct i2c_pmic *chip)
{
	struct device_node *node = chip->dev->of_node;
	int rc, i;
	u32 temp;

	if (!node) {
		pr_err("missing device tree\n");
		return -EINVAL;
	}

	chip->num_periphs = of_property_count_u32_elems(node,
							"qcom,periph-map");
	if (chip->num_periphs < 0) {
		pr_err("missing qcom,periph-map property rc=%d\n",
			chip->num_periphs);
		return chip->num_periphs;
	}

	if (chip->num_periphs == 0) {
		pr_err("qcom,periph-map must contain at least one address\n");
		return -EINVAL;
	}

	chip->periph = devm_kcalloc(chip->dev, chip->num_periphs,
				     sizeof(*chip->periph), GFP_KERNEL);
	if (!chip->periph)
		return -ENOMEM;

	for (i = 0; i < chip->num_periphs; i++) {
		rc = of_property_read_u32_index(node, "qcom,periph-map",
						i, &temp);
		if (rc < 0) {
			pr_err("Couldn't read qcom,periph-map[%d] rc=%d\n",
			       i, rc);
			return rc;
		}

		chip->periph[i].addr = (u16)(temp << 8);
		chip->periph[i].data = chip;
		mutex_init(&chip->periph[i].lock);
	}

	of_property_read_string(node, "pinctrl-names", &chip->pinctrl_name);

	return rc;
}

#define MAX_I2C_RETRIES	3
static int i2c_pmic_read(struct regmap *map, unsigned int reg, void *val,
			size_t val_count)
{
	int rc, retries = 0;

	do {
		rc = regmap_bulk_read(map, reg, val, val_count);
	} while (rc == -ENOTCONN && retries++ < MAX_I2C_RETRIES);

	if (retries > 1)
		pr_err("i2c_pmic_read failed for %d retries, rc = %d\n",
			retries - 1, rc);

	return rc;
}

static int i2c_pmic_determine_initial_status(struct i2c_pmic *chip)
{
	int rc, i;

	for (i = 0; i < chip->num_periphs; i++) {
		rc = i2c_pmic_read(chip->regmap,
				chip->periph[i].addr | INT_SET_TYPE_OFFSET,
				chip->periph[i].cached, IRQ_MAX_REGS);
		if (rc < 0) {
			pr_err("Couldn't read irq data rc=%d\n", rc);
			return rc;
		}

		memcpy(chip->periph[i].synced, chip->periph[i].cached,
		       IRQ_MAX_REGS * sizeof(*chip->periph[i].synced));
	}

	return 0;
}

static struct regmap_config i2c_pmic_regmap_config = {
	.reg_bits	= 16,
	.val_bits	= 8,
	.max_register	= 0xFFFF,
};

static int i2c_pmic_probe(struct i2c_client *client,
			  const struct i2c_device_id *id)
{
	struct i2c_pmic *chip;
	int rc = 0;

	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
	if (!chip)
		return -ENOMEM;

	chip->dev = &client->dev;
	chip->regmap = devm_regmap_init_i2c(client, &i2c_pmic_regmap_config);
	if (!chip->regmap)
		return -ENODEV;

	i2c_set_clientdata(client, chip);
	if (!of_property_read_bool(chip->dev->of_node, "interrupt-controller"))
		goto probe_children;

	chip->domain = irq_domain_add_tree(client->dev.of_node,
					   &i2c_pmic_domain_ops, chip);
	if (!chip->domain) {
		rc = -ENOMEM;
		goto cleanup;
	}

	rc = i2c_pmic_parse_dt(chip);
	if (rc < 0) {
		pr_err("Couldn't parse device tree rc=%d\n", rc);
		goto cleanup;
	}

	rc = i2c_pmic_determine_initial_status(chip);
	if (rc < 0) {
		pr_err("Couldn't determine initial status rc=%d\n", rc);
		goto cleanup;
	}

	if (chip->pinctrl_name) {
		chip->pinctrl = devm_pinctrl_get_select(chip->dev,
							chip->pinctrl_name);
		if (IS_ERR(chip->pinctrl)) {
			pr_err("Couldn't select %s pinctrl rc=%ld\n",
				chip->pinctrl_name, PTR_ERR(chip->pinctrl));
			rc = PTR_ERR(chip->pinctrl);
			goto cleanup;
		}
	}

	rc = devm_request_threaded_irq(&client->dev, client->irq, NULL,
				       i2c_pmic_irq_handler,
				       IRQF_ONESHOT | IRQF_SHARED,
				       "i2c_pmic_stat_irq", chip);
	if (rc < 0) {
		pr_err("Couldn't request irq %d rc=%d\n", client->irq, rc);
		goto cleanup;
	}

	enable_irq_wake(client->irq);

probe_children:
	of_platform_populate(chip->dev->of_node, NULL, NULL, chip->dev);
	pr_info("I2C PMIC probe successful\n");
	return rc;

cleanup:
	if (chip->domain)
		irq_domain_remove(chip->domain);
	i2c_set_clientdata(client, NULL);
	return rc;
}

static int i2c_pmic_remove(struct i2c_client *client)
{
	struct i2c_pmic *chip = i2c_get_clientdata(client);

	of_platform_depopulate(chip->dev);
	if (chip->domain)
		irq_domain_remove(chip->domain);
	i2c_set_clientdata(client, NULL);
	return 0;
}

#ifdef CONFIG_PM_SLEEP
static int i2c_pmic_suspend(struct device *dev)
{
	struct i2c_pmic *chip = dev_get_drvdata(dev);
	struct i2c_pmic_periph *periph;
	int rc = 0, i;

	for (i = 0; i < chip->num_periphs; i++) {
		periph = &chip->periph[i];

		rc = regmap_write(chip->regmap,
				  periph->addr | INT_EN_CLR_OFFSET, 0xFF);
		if (rc < 0) {
			pr_err_ratelimited("Couldn't clear 0x%04x irqs rc=%d\n",
				periph->addr, rc);
			continue;
		}

		rc = regmap_write(chip->regmap,
				  periph->addr | INT_EN_SET_OFFSET,
				  periph->wake);
		if (rc < 0)
			pr_err_ratelimited("Couldn't enable 0x%04x wake irqs 0x%02x rc=%d\n",
			       periph->addr, periph->wake, rc);
	}

	return rc;
}

static int i2c_pmic_resume(struct device *dev)
{
	struct i2c_pmic *chip = dev_get_drvdata(dev);
	struct i2c_pmic_periph *periph;
	int rc = 0, i;

	for (i = 0; i < chip->num_periphs; i++) {
		periph = &chip->periph[i];

		rc = regmap_write(chip->regmap,
				  periph->addr | INT_EN_CLR_OFFSET, 0xFF);
		if (rc < 0) {
			pr_err("Couldn't clear 0x%04x irqs rc=%d\n",
				periph->addr, rc);
			continue;
		}

		rc = regmap_write(chip->regmap,
				  periph->addr | INT_EN_SET_OFFSET,
				  periph->synced[IRQ_EN_SET]);
		if (rc < 0)
			pr_err("Couldn't restore 0x%04x synced irqs 0x%02x rc=%d\n",
			       periph->addr, periph->synced[IRQ_EN_SET], rc);
	}

	return rc;
}
#endif
static SIMPLE_DEV_PM_OPS(i2c_pmic_pm_ops, i2c_pmic_suspend, i2c_pmic_resume);

static const struct of_device_id i2c_pmic_match_table[] = {
	{ .compatible = "qcom,i2c-pmic", },
	{ },
};

static const struct i2c_device_id i2c_pmic_id[] = {
	{ "i2c-pmic", 0 },
	{ },
};
MODULE_DEVICE_TABLE(i2c, i2c_pmic_id);

static struct i2c_driver i2c_pmic_driver = {
	.driver		= {
		.name		= "i2c_pmic",
		.owner		= THIS_MODULE,
		.pm		= &i2c_pmic_pm_ops,
		.of_match_table	= i2c_pmic_match_table,
	},
	.probe		= i2c_pmic_probe,
	.remove		= i2c_pmic_remove,
	.id_table	= i2c_pmic_id,
};

module_i2c_driver(i2c_pmic_driver);

MODULE_LICENSE("GPL v2");
MODULE_ALIAS("i2c:i2c_pmic");