summaryrefslogtreecommitdiffstats
path: root/drivers/mfd/mc13783-core.c
blob: e354d2912ef13dc11eb059959faec1ebfd5a3430 (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
/*
 * Copyright 2009 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
 *
 * This code is in parts based on wm8350-core.c and pcf50633-core.c
 *
 * Initial development of this code was funded by
 * Phytec Messtechnik GmbH, http://www.phytec.de
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/mfd/mc13783-private.h>
#include <linux/platform_device.h>
#include <linux/mfd/mc13783.h>
#include <linux/completion.h>
#include <linux/interrupt.h>
#include <linux/mfd/core.h>
#include <linux/spi/spi.h>
#include <linux/uaccess.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/irq.h>

#define MC13783_MAX_REG_NUM	0x3f
#define MC13783_FRAME_MASK	0x00ffffff
#define MC13783_MAX_REG_NUM	0x3f
#define MC13783_REG_NUM_SHIFT	0x19
#define MC13783_WRITE_BIT_SHIFT	31

static inline int spi_rw(struct spi_device *spi, u8 * buf, size_t len)
{
	struct spi_transfer t = {
		.tx_buf = (const void *)buf,
		.rx_buf = buf,
		.len = len,
		.cs_change = 0,
		.delay_usecs = 0,
	};
	struct spi_message m;

	spi_message_init(&m);
	spi_message_add_tail(&t, &m);
	if (spi_sync(spi, &m) != 0 || m.status != 0)
		return -EINVAL;
	return len - m.actual_length;
}

static int mc13783_read(struct mc13783 *mc13783, int reg_num, u32 *reg_val)
{
	unsigned int frame = 0;
	int ret = 0;

	if (reg_num > MC13783_MAX_REG_NUM)
		return -EINVAL;

	frame |= reg_num << MC13783_REG_NUM_SHIFT;

	ret = spi_rw(mc13783->spi_device, (u8 *)&frame, 4);

	*reg_val = frame & MC13783_FRAME_MASK;

	return ret;
}

static int mc13783_write(struct mc13783 *mc13783, int reg_num, u32 reg_val)
{
	unsigned int frame = 0;

	if (reg_num > MC13783_MAX_REG_NUM)
		return -EINVAL;

	frame |= (1 << MC13783_WRITE_BIT_SHIFT);
	frame |= reg_num << MC13783_REG_NUM_SHIFT;
	frame |= reg_val & MC13783_FRAME_MASK;

	return spi_rw(mc13783->spi_device, (u8 *)&frame, 4);
}

int mc13783_reg_read(struct mc13783 *mc13783, int reg_num, u32 *reg_val)
{
	int ret;

	mutex_lock(&mc13783->io_lock);
	ret = mc13783_read(mc13783, reg_num, reg_val);
	mutex_unlock(&mc13783->io_lock);

	return ret;
}
EXPORT_SYMBOL_GPL(mc13783_reg_read);

int mc13783_reg_write(struct mc13783 *mc13783, int reg_num, u32 reg_val)
{
	int ret;

	mutex_lock(&mc13783->io_lock);
	ret = mc13783_write(mc13783, reg_num, reg_val);
	mutex_unlock(&mc13783->io_lock);

	return ret;
}
EXPORT_SYMBOL_GPL(mc13783_reg_write);

/**
 * mc13783_set_bits - Bitmask write
 *
 * @mc13783: Pointer to mc13783 control structure
 * @reg:    Register to access
 * @mask:   Mask of bits to change
 * @val:    Value to set for masked bits
 */
int mc13783_set_bits(struct mc13783 *mc13783, int reg, u32 mask, u32 val)
{
	u32 tmp;
	int ret;

	mutex_lock(&mc13783->io_lock);

	ret = mc13783_read(mc13783, reg, &tmp);
	tmp = (tmp & ~mask) | val;
	if (ret == 0)
		ret = mc13783_write(mc13783, reg, tmp);

	mutex_unlock(&mc13783->io_lock);

	return ret;
}
EXPORT_SYMBOL_GPL(mc13783_set_bits);

int mc13783_register_irq(struct mc13783 *mc13783, int irq,
		void (*handler) (int, void *), void *data)
{
	if (irq < 0 || irq > MC13783_NUM_IRQ || !handler)
		return -EINVAL;

	if (WARN_ON(mc13783->irq_handler[irq].handler))
		return -EBUSY;

	mutex_lock(&mc13783->io_lock);
	mc13783->irq_handler[irq].handler = handler;
	mc13783->irq_handler[irq].data = data;
	mutex_unlock(&mc13783->io_lock);

	return 0;
}
EXPORT_SYMBOL_GPL(mc13783_register_irq);

int mc13783_free_irq(struct mc13783 *mc13783, int irq)
{
	if (irq < 0 || irq > MC13783_NUM_IRQ)
		return -EINVAL;

	mutex_lock(&mc13783->io_lock);
	mc13783->irq_handler[irq].handler = NULL;
	mutex_unlock(&mc13783->io_lock);

	return 0;
}
EXPORT_SYMBOL_GPL(mc13783_free_irq);

static void mc13783_irq_work(struct work_struct *work)
{
	struct mc13783 *mc13783 = container_of(work, struct mc13783, work);
	int i;
	unsigned int adc_sts;

	/* check if the adc has finished any completion */
	mc13783_reg_read(mc13783, MC13783_REG_INTERRUPT_STATUS_0, &adc_sts);
	mc13783_reg_write(mc13783, MC13783_REG_INTERRUPT_STATUS_0,
			adc_sts & MC13783_INT_STAT_ADCDONEI);

	if (adc_sts & MC13783_INT_STAT_ADCDONEI)
		complete_all(&mc13783->adc_done);

	for (i = 0; i < MC13783_NUM_IRQ; i++)
		if (mc13783->irq_handler[i].handler)
			mc13783->irq_handler[i].handler(i,
					mc13783->irq_handler[i].data);
	enable_irq(mc13783->irq);
}

static irqreturn_t mc13783_interrupt(int irq, void *dev_id)
{
	struct mc13783 *mc13783 = dev_id;

	disable_irq_nosync(irq);

	schedule_work(&mc13783->work);
	return IRQ_HANDLED;
}

/* set adc to ts interrupt mode, which generates touchscreen wakeup interrupt */
static inline void mc13783_adc_set_ts_irq_mode(struct mc13783 *mc13783)
{
	unsigned int reg_adc0, reg_adc1;

	reg_adc0 = MC13783_ADC0_ADREFEN | MC13783_ADC0_ADREFMODE
			| MC13783_ADC0_TSMOD0;
	reg_adc1 = MC13783_ADC1_ADEN | MC13783_ADC1_ADTRIGIGN;

	mc13783_reg_write(mc13783, MC13783_REG_ADC_0, reg_adc0);
	mc13783_reg_write(mc13783, MC13783_REG_ADC_1, reg_adc1);
}

int mc13783_adc_do_conversion(struct mc13783 *mc13783, unsigned int mode,
		unsigned int channel, unsigned int *sample)
{
	unsigned int reg_adc0, reg_adc1;
	int i;

	mutex_lock(&mc13783->adc_conv_lock);

	/* set up auto incrementing anyway to make quick read */
	reg_adc0 =  MC13783_ADC0_ADINC1 | MC13783_ADC0_ADINC2;
	/* enable the adc, ignore external triggering and set ASC to trigger
	 * conversion */
	reg_adc1 =  MC13783_ADC1_ADEN | MC13783_ADC1_ADTRIGIGN
		| MC13783_ADC1_ASC;

	/* setup channel number */
	if (channel > 7)
		reg_adc1 |= MC13783_ADC1_ADSEL;

	switch (mode) {
	case MC13783_ADC_MODE_TS:
		/* enables touch screen reference mode and set touchscreen mode
		 * to position mode */
		reg_adc0 |= MC13783_ADC0_ADREFEN | MC13783_ADC0_ADREFMODE
			| MC13783_ADC0_TSMOD0 | MC13783_ADC0_TSMOD1;
		reg_adc1 |= 4 << MC13783_ADC1_CHAN1_SHIFT;
		break;
	case MC13783_ADC_MODE_SINGLE_CHAN:
		reg_adc1 |= (channel & 0x7) << MC13783_ADC1_CHAN0_SHIFT;
		reg_adc1 |= MC13783_ADC1_RAND;
		break;
	case MC13783_ADC_MODE_MULT_CHAN:
		reg_adc1 |= 4 << MC13783_ADC1_CHAN1_SHIFT;
		break;
	default:
		return -EINVAL;
	}

	mc13783_reg_write(mc13783, MC13783_REG_ADC_0, reg_adc0);
	mc13783_reg_write(mc13783, MC13783_REG_ADC_1, reg_adc1);

	wait_for_completion_interruptible(&mc13783->adc_done);

	for (i = 0; i < 4; i++)
		mc13783_reg_read(mc13783, MC13783_REG_ADC_2, &sample[i]);

	if (mc13783->ts_active)
		mc13783_adc_set_ts_irq_mode(mc13783);

	mutex_unlock(&mc13783->adc_conv_lock);

	return 0;
}
EXPORT_SYMBOL_GPL(mc13783_adc_do_conversion);

void mc13783_adc_set_ts_status(struct mc13783 *mc13783, unsigned int status)
{
	mc13783->ts_active = status;
}
EXPORT_SYMBOL_GPL(mc13783_adc_set_ts_status);

static int mc13783_check_revision(struct mc13783 *mc13783)
{
	u32 rev_id, rev1, rev2, finid, icid;

	mc13783_read(mc13783, MC13783_REG_REVISION, &rev_id);

	rev1 = (rev_id & 0x018) >> 3;
	rev2 = (rev_id & 0x007);
	icid = (rev_id & 0x01C0) >> 6;
	finid = (rev_id & 0x01E00) >> 9;

	/* Ver 0.2 is actually 3.2a.  Report as 3.2 */
	if ((rev1 == 0) && (rev2 == 2))
		rev1 = 3;

	if (rev1 == 0 || icid != 2) {
		dev_err(mc13783->dev, "No MC13783 detected.\n");
		return -ENODEV;
	}

	mc13783->revision = ((rev1 * 10) + rev2);
	dev_info(mc13783->dev, "MC13783 Rev %d.%d FinVer %x detected\n", rev1,
	       rev2, finid);

	return 0;
}

/*
 * Register a client device.  This is non-fatal since there is no need to
 * fail the entire device init due to a single platform device failing.
 */
static void mc13783_client_dev_register(struct mc13783 *mc13783,
				       const char *name)
{
	struct mfd_cell cell = {};

	cell.name = name;

	mfd_add_devices(mc13783->dev, -1, &cell, 1, NULL, 0);
}

static int __devinit mc13783_probe(struct spi_device *spi)
{
	struct mc13783 *mc13783;
	struct mc13783_platform_data *pdata = spi->dev.platform_data;
	int ret;

	mc13783 = kzalloc(sizeof(struct mc13783), GFP_KERNEL);
	if (!mc13783)
		return -ENOMEM;

	dev_set_drvdata(&spi->dev, mc13783);
	spi->mode = SPI_MODE_0 | SPI_CS_HIGH;
	spi->bits_per_word = 32;
	spi_setup(spi);

	mc13783->spi_device = spi;
	mc13783->dev = &spi->dev;
	mc13783->irq = spi->irq;

	INIT_WORK(&mc13783->work, mc13783_irq_work);
	mutex_init(&mc13783->io_lock);
	mutex_init(&mc13783->adc_conv_lock);
	init_completion(&mc13783->adc_done);

	if (pdata) {
		mc13783->flags = pdata->flags;
		mc13783->regulators = pdata->regulators;
		mc13783->num_regulators = pdata->num_regulators;
	}

	if (mc13783_check_revision(mc13783)) {
		ret = -ENODEV;
		goto err_out;
	}

	/* clear and mask all interrupts */
	mc13783_reg_write(mc13783, MC13783_REG_INTERRUPT_STATUS_0, 0x00ffffff);
	mc13783_reg_write(mc13783, MC13783_REG_INTERRUPT_MASK_0, 0x00ffffff);
	mc13783_reg_write(mc13783, MC13783_REG_INTERRUPT_STATUS_1, 0x00ffffff);
	mc13783_reg_write(mc13783, MC13783_REG_INTERRUPT_MASK_1, 0x00ffffff);

	/* unmask adcdone interrupts */
	mc13783_set_bits(mc13783, MC13783_REG_INTERRUPT_MASK_0,
			MC13783_INT_MASK_ADCDONEM, 0);

	ret = request_irq(mc13783->irq, mc13783_interrupt,
			IRQF_DISABLED | IRQF_TRIGGER_HIGH, "mc13783",
			mc13783);
	if (ret)
		goto err_out;

	if (mc13783->flags & MC13783_USE_CODEC)
		mc13783_client_dev_register(mc13783, "mc13783-codec");
	if (mc13783->flags & MC13783_USE_ADC)
		mc13783_client_dev_register(mc13783, "mc13783-adc");
	if (mc13783->flags & MC13783_USE_RTC)
		mc13783_client_dev_register(mc13783, "mc13783-rtc");
	if (mc13783->flags & MC13783_USE_REGULATOR)
		mc13783_client_dev_register(mc13783, "mc13783-regulator");
	if (mc13783->flags & MC13783_USE_TOUCHSCREEN)
		mc13783_client_dev_register(mc13783, "mc13783-ts");

	return 0;

err_out:
	kfree(mc13783);
	return ret;
}

static int __devexit mc13783_remove(struct spi_device *spi)
{
	struct mc13783 *mc13783;

	mc13783 = dev_get_drvdata(&spi->dev);

	free_irq(mc13783->irq, mc13783);

	mfd_remove_devices(&spi->dev);

	return 0;
}

static struct spi_driver pmic_driver = {
	.driver = {
		   .name = "mc13783",
		   .bus = &spi_bus_type,
		   .owner = THIS_MODULE,
	},
	.probe = mc13783_probe,
	.remove = __devexit_p(mc13783_remove),
};

static int __init pmic_init(void)
{
	return spi_register_driver(&pmic_driver);
}
subsys_initcall(pmic_init);

static void __exit pmic_exit(void)
{
	spi_unregister_driver(&pmic_driver);
}
module_exit(pmic_exit);

MODULE_DESCRIPTION("Core/Protocol driver for Freescale MC13783 PMIC");
MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
MODULE_LICENSE("GPL");