summaryrefslogtreecommitdiff
path: root/drivers/mfd/pfuze-core.c
blob: 96b62924adec0b37f184d2689ea06450eee12d48 (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
/*
 * Copyright (C) 2011-2012 Freescale Semiconductor, Inc. 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 as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <linux/slab.h>
#include <linux/module.h>
/*#define VERBOSE_DEBUG*/
#include <linux/platform_device.h>
#include <linux/mutex.h>
#include <linux/interrupt.h>
#include <linux/i2c.h>
#include <linux/mfd/core.h>
#include <linux/mfd/pfuze.h>
#include <linux/delay.h>

#define PFUZE_REG_DEVICE_ID	0
#define PFUZE_REG_REVISION_ID	3
#define PFUZE_REG_FAB_ID	4
#define PFUZE_REG_INT_STATUS0	5
#define PFUZE_REG_INT_MASK0	6
#define PFUZE_REG_INT_SENSE0	7

struct mc_pfuze {
	struct i2c_client *i2c_client;
	struct mutex lock;
	int irq;		/*irq number */
	irq_handler_t irqhandler[PFUZE_NUM_IRQ];
	void *irqdata[PFUZE_NUM_IRQ];
};

static const struct i2c_device_id pfuze_device_id[] = {
	{"pfuze100", 0},
	{},
};

enum pfuze_id {
	PFUZE_ID_PFUZE100,
	PFUZE_ID_INVALID,
};
static const char *pfuze_chipname[] = {
	[PFUZE_ID_PFUZE100] = "pfuze100",
};

void pfuze_lock(struct mc_pfuze *mc_pfuze)
{
	if (!mutex_trylock(&mc_pfuze->lock)) {
		dev_dbg(&mc_pfuze->i2c_client->dev, "wait for %s from %pf\n",
			__func__, __builtin_return_address(0));
		mutex_lock(&mc_pfuze->lock);
	}
	dev_dbg(&mc_pfuze->i2c_client->dev, "%s from %pf\n",
		__func__, __builtin_return_address(0));
}

EXPORT_SYMBOL(pfuze_lock);

void pfuze_unlock(struct mc_pfuze *mc_pfuze)
{
	dev_dbg(&mc_pfuze->i2c_client->dev, "%s from %pf\n", __func__,
		__builtin_return_address(0));
	mutex_unlock(&mc_pfuze->lock);
}

EXPORT_SYMBOL(pfuze_unlock);

int pfuze_reg_read(struct mc_pfuze *mc_pfuze, unsigned int offset,
		   unsigned char *val)
{
	unsigned char data;
	int ret, i;
	BUG_ON(!mutex_is_locked(&mc_pfuze->lock));
	if (offset > PFUZE_NUMREGS)
		return -EINVAL;
	data = (unsigned char)offset;
	for (i = 0; i < PFUZE_I2C_RETRY_TIMES; i++) {
		ret =
		    i2c_smbus_read_i2c_block_data(mc_pfuze->i2c_client, data, 1,
						  val);
		if (ret == 1)
			break;
		msleep(1);
	}
	if (ret != 1) {
		dev_err(&mc_pfuze->i2c_client->dev, "recv failed!:%i,%x\n", ret,
			*val);
		return -1;
	}

	return 0;
}

EXPORT_SYMBOL(pfuze_reg_read);

int pfuze_reg_write(struct mc_pfuze *mc_pfuze, unsigned int offset,
		    unsigned char val)
{
	unsigned char buf[2];
	int ret;

	buf[0] = (unsigned char)offset;
	memcpy(&buf[1], &val, 1);

	ret = i2c_master_send(mc_pfuze->i2c_client, buf, 2);
	if (ret != 2) {
		dev_err(&mc_pfuze->i2c_client->dev, "write failed!:%i\n", ret);
		return ret;
	}
	return 0;
}

EXPORT_SYMBOL(pfuze_reg_write);

int pfuze_reg_rmw(struct mc_pfuze *mc_pfuze, unsigned int offset,
		  unsigned char mask, unsigned char val)
{
	int ret;
	unsigned char valread;
	BUG_ON(val & ~mask);
	ret = pfuze_reg_read(mc_pfuze, offset, &valread);
	if (ret)
		return ret;
	valread = (valread & ~mask) | val;
	return pfuze_reg_write(mc_pfuze, offset, valread);
}

EXPORT_SYMBOL(pfuze_reg_rmw);

int pfuze_irq_mask(struct mc_pfuze *mc_pfuze, int irq)
{
	int ret;
	unsigned int offmask = PFUZE_REG_INT_MASK0 + 3 * (irq / 8);
	unsigned char irqbit = 1 << (irq % 8);
	unsigned char mask;

	if (irq < 0 || irq >= PFUZE_NUM_IRQ)
		return -EINVAL;

	ret = pfuze_reg_read(mc_pfuze, offmask, &mask);
	if (ret)
		return ret;
	if (mask & irqbit)
		/* already masked */
		return 0;
	return pfuze_reg_write(mc_pfuze, offmask, mask | irqbit);
}

EXPORT_SYMBOL(pfuze_irq_mask);

int pfuze_irq_unmask(struct mc_pfuze *mc_pfuze, int irq)
{
	int ret;
	unsigned int offmask = PFUZE_REG_INT_MASK0 + 3 * (irq / 8);
	unsigned char irqbit = 1 << (irq % 8);
	unsigned char mask;

	if (irq < 0 || irq >= PFUZE_NUM_IRQ)
		return -EINVAL;

	ret = pfuze_reg_read(mc_pfuze, offmask, &mask);
	if (ret)
		return ret;
	if (!(mask & irqbit))
		/* already masked */
		return 0;
	return pfuze_reg_write(mc_pfuze, offmask, mask & ~irqbit);
}

EXPORT_SYMBOL(pfuze_irq_unmask);

int pfuze_irq_status(struct mc_pfuze *mc_pfuze, int irq, int *enabled,
		     int *pending)
{
	int ret;
	unsigned int offmask = PFUZE_REG_INT_MASK0 + 3 * (irq / 8);
	unsigned int offstat = PFUZE_REG_INT_STATUS0 + 3 * (irq / 8);
	unsigned char irqbit = 1 << (irq % 8);

	if (irq < 0 || irq >= PFUZE_NUM_IRQ)
		return -EINVAL;
	if (enabled) {
		unsigned char mask;
		ret = pfuze_reg_read(mc_pfuze, offmask, &mask);
		if (ret)
			return ret;
		*enabled = mask & irqbit;
	}
	if (pending) {
		unsigned char stat;
		ret = pfuze_reg_read(mc_pfuze, offstat, &stat);
		if (ret)
			return ret;
		*pending = stat & irqbit;
	}
	return 0;
}

EXPORT_SYMBOL(pfuze_irq_status);

int pfuze_irq_ack(struct mc_pfuze *mc_pfuze, int irq)
{
	unsigned int offstat = PFUZE_REG_INT_STATUS0 + 3 * (irq / 8);
	unsigned char val = 1 << (irq % 8);
	BUG_ON(irq < 0 || irq >= PFUZE_NUM_IRQ);
	return pfuze_reg_write(mc_pfuze, offstat, val);
}

EXPORT_SYMBOL(pfuze_irq_ack);

int pfuze_irq_request_nounmask(struct mc_pfuze *mc_pfuze, int irq,
			       irq_handler_t handler, const char *name,
			       void *dev)
{
	BUG_ON(!mutex_is_locked(&mc_pfuze->lock));
	BUG_ON(!handler);

	if (irq < 0 || irq >= PFUZE_NUM_IRQ)
		return -EINVAL;
	if (mc_pfuze->irqhandler[irq])
		return -EBUSY;
	mc_pfuze->irqhandler[irq] = handler;
	mc_pfuze->irqdata[irq] = dev;

	return 0;
}

EXPORT_SYMBOL(pfuze_irq_request_nounmask);

int pfuze_irq_request(struct mc_pfuze *mc_pfuze, int irq,
		      irq_handler_t handler, const char *name, void *dev)
{
	int ret;

	ret = pfuze_irq_request_nounmask(mc_pfuze, irq, handler, name, dev);
	if (ret)
		return ret;
	ret = pfuze_irq_unmask(mc_pfuze, irq);
	if (ret) {
		mc_pfuze->irqhandler[irq] = NULL;
		mc_pfuze->irqdata[irq] = NULL;
		return ret;
	}
	return 0;
}

EXPORT_SYMBOL(pfuze_irq_request);

int pfuze_irq_free(struct mc_pfuze *mc_pfuze, int irq, void *dev)
{
	int ret;
	BUG_ON(!mutex_is_locked(&mc_pfuze->lock));

	if (irq < 0 || irq >= PFUZE_NUM_IRQ || !mc_pfuze->irqhandler[irq] ||
	    mc_pfuze->irqdata[irq] != dev)
		return -EINVAL;

	ret = pfuze_irq_mask(mc_pfuze, irq);
	if (ret)
		return ret;
	mc_pfuze->irqhandler[irq] = NULL;
	mc_pfuze->irqdata[irq] = NULL;
	return 0;
}

EXPORT_SYMBOL(pfuze_irq_free);

static inline irqreturn_t pfuze_irqhandler(struct mc_pfuze *mc_pfuze, int irq)
{
	return mc_pfuze->irqhandler[irq] (irq, mc_pfuze->irqdata[irq]);
}

static int pfuze_irq_handle(struct mc_pfuze *mc_pfuze, unsigned int offstat,
			    unsigned int offmask, int baseirq)
{
	unsigned char stat, mask;
	int ret = pfuze_reg_read(mc_pfuze, offstat, &stat);
	int num_handled = 0;

	if (ret)
		return ret;
	ret = pfuze_reg_read(mc_pfuze, offmask, &mask);
	if (ret)
		return ret;
	while (stat & ~mask) {
		unsigned char irq = __ffs(stat & ~mask);
		stat &= ~(1 << irq);
		if (likely(mc_pfuze->irqhandler[baseirq + irq])) {
			irqreturn_t handled;
			handled = pfuze_irqhandler(mc_pfuze, baseirq + irq);
			if (handled == IRQ_HANDLED)
				num_handled++;
		} else {
			dev_err(&mc_pfuze->i2c_client->dev,
				"BUG: irq %u but no handler\n", baseirq + irq);
			mask |= 1 << irq;
			ret = pfuze_reg_write(mc_pfuze, offmask, mask);
		}
	}
	return num_handled;
}

static irqreturn_t pfuze_irq_thread(int irq, void *data)
{
	struct mc_pfuze *mc_pfuze = data;
	irqreturn_t ret;
	int i, handled = 0;

	pfuze_lock(mc_pfuze);
	for (i = 0; i < 7; i++) {
		ret = pfuze_irq_handle(mc_pfuze, PFUZE_REG_INT_STATUS0 + 3 * i,
				       PFUZE_REG_INT_MASK0 + 3 * i, 8 * i);
		if (ret > 0)
			handled = 1;
	}
	pfuze_unlock(mc_pfuze);
	return IRQ_RETVAL(handled);
}

static int pfuze_identify(struct mc_pfuze *mc_pfuze, enum pfuze_id *id)
{
	unsigned char value;
	int ret;
	ret = pfuze_reg_read(mc_pfuze, PFUZE_REG_DEVICE_ID, &value);
	if (ret)
		return ret;
	switch (value & 0x0f) {
	case 0x0:
		*id = PFUZE_ID_PFUZE100;
		break;
	default:
		*id = PFUZE_ID_INVALID;
		dev_warn(&mc_pfuze->i2c_client->dev, "Illegal ID: %x\n", value);
		break;
	}
	ret = pfuze_reg_read(mc_pfuze, PFUZE_REG_REVISION_ID, &value);
	if (ret)
		return ret;
	dev_info(&mc_pfuze->i2c_client->dev,
		 "ID: %d,Full lay: %x ,Metal lay: %x\n",
		 *id, (value & 0xf0) >> 4, value & 0x0f);
	ret = pfuze_reg_read(mc_pfuze, PFUZE_REG_FAB_ID, &value);
	if (ret)
		return ret;
	dev_info(&mc_pfuze->i2c_client->dev, "FAB: %x ,FIN: %x\n",
		 (value & 0xc) >> 2, value & 0x3);
	return 0;
}

static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
						const struct i2c_client *client)
{
	while (id->name[0]) {
		if (strcmp(client->name, id->name) == 0)
			return id;
		id++;
	}
	return NULL;

}

static const char *pfuze_get_chipname(struct mc_pfuze *mc_pfuze)
{
	const struct i2c_driver *idrv =
	    to_i2c_driver(mc_pfuze->i2c_client->dev.driver);
	const struct i2c_device_id *devid =
	    i2c_match_id(idrv->id_table, mc_pfuze->i2c_client);
	if (!devid)
		return NULL;
	return pfuze_chipname[devid->driver_data];
}

unsigned int pfuze_get_flags(struct mc_pfuze *mc_pfuze)
{
	struct pfuze_platform_data *pdata =
	    dev_get_platdata(&mc_pfuze->i2c_client->dev);
	return pdata->flags;
}

EXPORT_SYMBOL(pfuze_get_flags);

static int pfuze_add_subdevice_pdata(struct mc_pfuze *mc_pfuze,
				     const char *format, void *pdata,
				     size_t pdata_size)
{
	char buf[30];
	const char *name = pfuze_get_chipname(mc_pfuze);
	struct mfd_cell cell = {
		.platform_data = pdata,
		.pdata_size = pdata_size,
	};
	if (snprintf(buf, sizeof(buf), format, name) > sizeof(buf))
		return -E2BIG;
	cell.name = kmemdup(buf, strlen(buf) + 1, GFP_KERNEL);
	if (!cell.name)
		return -ENOMEM;
	return mfd_add_devices(&mc_pfuze->i2c_client->dev, -1, &cell, 1, NULL,
			       0);
}

static ssize_t pfuze_show(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
	unsigned char i, value;
	int offset = PFUZE_NUMREGS;
	struct i2c_client *client = to_i2c_client(dev);
	struct mc_pfuze *mc_pfuze = i2c_get_clientdata(client);

	pfuze_lock(mc_pfuze);
	for (i = 0; i < offset; i++) {
		pfuze_reg_read(mc_pfuze, i, &value);
		pr_info("reg%03d: %02x\t\t", i, value);
	}
	pfuze_unlock(mc_pfuze);
	pr_info("\n");

	return 0;
}

static ssize_t pfuze_store(struct device *dev,
			   struct device_attribute *attr, const char *buf,
			   size_t count)
{
	unsigned char reg, value;
	int ret;
	char *p;
	struct i2c_client *client = to_i2c_client(dev);
	struct mc_pfuze *mc_pfuze = i2c_get_clientdata(client);

	reg = simple_strtoul(buf, NULL, 10);

	p = NULL;
	p = memchr(buf, ' ', count);

	if (p == NULL) {
		pfuze_lock(mc_pfuze);
		pfuze_reg_read(mc_pfuze, reg, &value);
		pfuze_unlock(mc_pfuze);
		pr_debug("reg%03d: %02x\n", reg, value);
		return count;
	}

	p += 1;

	value = simple_strtoul(p, NULL, 16);
	pfuze_lock(mc_pfuze);
	ret = pfuze_reg_write(mc_pfuze, reg, value);
	if (ret == 0)
		pr_debug("write reg%03d: %02x\n", reg, value);
	else
		pr_debug("register update failed\n");

	pfuze_unlock(mc_pfuze);
	return count;
}

static struct device_attribute pfuze_dev_attr = {
	.attr = {
		 .name = "pfuze_ctl",
		 .mode = S_IRUSR | S_IWUSR,
		 },
	.show = pfuze_show,
	.store = pfuze_store,
};

static int pfuze_probe(struct i2c_client *client,
		       const struct i2c_device_id *id)
{
	struct mc_pfuze *mc_pfuze;
	struct pfuze_platform_data *pdata = client->dev.platform_data;
	int ret, i;
	enum pfuze_id pfuze_id;
	mc_pfuze = kzalloc(sizeof(*mc_pfuze), GFP_KERNEL);
	if (!mc_pfuze)
		return -ENOMEM;
	i2c_set_clientdata(client, mc_pfuze);
	mc_pfuze->i2c_client = client;

	mutex_init(&mc_pfuze->lock);
	pfuze_lock(mc_pfuze);
	ret = pfuze_identify(mc_pfuze, &pfuze_id);
	if (ret || pfuze_id == PFUZE_ID_INVALID)
		goto err_revision;
	/* mask all of irqs */
	for (i = 0; i < 7; i++) {
		ret =
		    pfuze_reg_write(mc_pfuze, PFUZE_REG_INT_MASK0 + 3 * i,
				    0xff);
		if (ret)
			goto err_mask;
	}
	ret = request_threaded_irq(client->irq, NULL, pfuze_irq_thread,
				   IRQF_ONESHOT | IRQF_TRIGGER_LOW, "pfuze",
				   mc_pfuze);
	if (ret) {
	      err_mask:
	      err_revision:
		pfuze_unlock(mc_pfuze);
		dev_set_drvdata(&client->dev, NULL);
		kfree(mc_pfuze);
		return ret;
	}
	pfuze_unlock(mc_pfuze);
	if (pdata->flags & PFUZE_USE_REGULATOR) {
		struct pfuze_regulator_platform_data regulator_pdata = {
			.num_regulators = pdata->num_regulators,
			.regulators = pdata->regulators,
			.pfuze_init = pdata->pfuze_init,
		};
		pfuze_add_subdevice_pdata(mc_pfuze, "%s-regulator",
					  &regulator_pdata,
					  sizeof(regulator_pdata));
	}
	ret = device_create_file(&client->dev, &pfuze_dev_attr);
	if (ret)
		dev_err(&client->dev, "create device file failed!\n");
	return 0;
}

static int __devexit pfuze_remove(struct i2c_client *client)
{
	struct mc_pfuze *mc_pfuze = dev_get_drvdata(&client->dev);
	free_irq(mc_pfuze->i2c_client->irq, mc_pfuze);
	mfd_remove_devices(&client->dev);
	kfree(mc_pfuze);
	return 0;
}

static struct i2c_driver pfuze_driver = {
	.id_table = pfuze_device_id,
	.driver = {
		   .name = "mc_pfuze",
		   .owner = THIS_MODULE,
		   },
	.probe = pfuze_probe,
	.remove = __devexit_p(pfuze_remove),
};

static int __init pfuze_init(void)
{
	return i2c_add_driver(&pfuze_driver);
}

subsys_initcall(pfuze_init);

static void __exit pfuze_exit(void)
{
	i2c_del_driver(&pfuze_driver);
}

module_exit(pfuze_exit);

MODULE_DESCRIPTION("Core driver for Freescale PFUZE PMIC");
MODULE_AUTHOR("Freescale Semiconductor, Inc");
MODULE_LICENSE("GPL v2");