aboutsummaryrefslogtreecommitdiff
path: root/drivers/mfd/da9052-spi.c
blob: 9769811c643d2d822300db09951c33b456461d9d (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
/*
 * Copyright(c) 2009 Dialog Semiconductor Ltd.
 *
 * 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.
 *
 * da9052-spi.c: SPI SSC (Synchronous Serial Communication) driver for DA9052
 */

#include <linux/module.h>
#include <linux/device.h>
#include <linux/mfd/core.h>
#include <linux/spi/spi.h>
#include <linux/mfd/da9052/da9052.h>
#include <linux/mfd/da9052/reg.h>


struct da9052 *da9052_spi;

#define SPI_CONNECTED 0

static int da9052_spi_is_connected(void)
{

	struct da9052_ssc_msg msg;

	/* printk("Entered da9052_spi_is_connected.............\n"); */

	msg.addr = DA9052_INTERFACE_REG;

	/* Test spi connectivity by performing read of the GPIO_0-1 register
	   and then verify the read value*/
	if (0 != da9052_spi_read(da9052_spi, &msg)) {
		printk(KERN_DEBUG "da9052_spi_is_connected - "\
		       "spi read failed.............\n");
		return -1;
	} else if (0x88 != msg.data) {
		printk(KERN_DEBUG "da9052_spi_is_connected - "		\
		       "spi read failed. Msg data =%x ..............\n",
		       msg.data);
		return -1;
	}

	return 0;

}

static int da9052_spi_probe(struct spi_device *spi)
{
	/* printk("\n\tEntered da9052_spi_probe.....\n"); */

	da9052_spi = kzalloc(sizeof(struct da9052), GFP_KERNEL);

	if (!da9052_spi)
		return -ENOMEM;


	spi->mode = SPI_MODE_0 | SPI_CPOL;
	spi->bits_per_word = 8;
	spi_setup(spi);

	da9052_spi->dev = &spi->dev;

	da9052_spi->spi_dev = spi;

	/*
	 * Allocate memory for RX/TX bufferes used in single register
	 * read/write
	 */
	da9052_spi->spi_rx_buf = kmalloc(2, GFP_KERNEL | GFP_DMA);
	if (!da9052_spi->spi_rx_buf)
		return -ENOMEM;

	da9052_spi->spi_tx_buf = kmalloc(2, GFP_KERNEL | GFP_DMA);
	if (!da9052_spi->spi_tx_buf)
		return -ENOMEM;

	da9052_spi->spi_active_page  = PAGECON_0;
	da9052_spi->rw_pol = 1;


	dev_set_drvdata(&spi->dev, da9052_spi);


	/* Validate SPI connectivity */
	if (SPI_CONNECTED  == da9052_spi_is_connected()) {
		/* SPI is connected */
		da9052_spi->connecting_device = SPI;
		if (0 != da9052_ssc_init(da9052_spi))
			return -ENODEV;
	} else {
		return -ENODEV;
	}

	/* printk("Exiting da9052_spi_probe.....\n"); */

	return 0;
}

static int da9052_spi_remove(struct spi_device *spi)
{
	struct da9052 *da9052 = dev_get_drvdata(&spi->dev);

	printk("Entered da9052_spi_remove()\n");
	if (SPI == da9052->connecting_device)
		da9052_ssc_exit(da9052);
	mfd_remove_devices(&spi->dev);
	kfree(da9052->spi_rx_buf);
	kfree(da9052->spi_tx_buf);
	kfree(da9052);
	return 0;
}

static struct spi_driver da9052_spi_driver = {
	.driver = {
		.name	 = DA9052_SSC_SPI_DEVICE_NAME,
		.bus	= &spi_bus_type,
		.owner	= THIS_MODULE,
	},
	.probe		= da9052_spi_probe,
	.remove		= __devexit_p(da9052_spi_remove),
};


static int da9052_spi_set_page(struct da9052 *da9052, unsigned char page)
{

	struct da9052_ssc_msg sscmsg;
	struct spi_message message;
	struct spi_transfer xfer;
	int ret = 0;

	printk(KERN_DEBUG "Entered da9052_spi_set_page.....\n");
	if ((page != PAGECON_0) && ((page != PAGECON_128)))
		return INVALID_PAGE;

	/* Current configuration is PAGE-0 and write request for PAGE-1 */
	/* set register address */
	sscmsg.addr = DA9052_PAGECON0_REG;
	/* set value */
	sscmsg.data = page;

	/* Check value of R/W_POL bit of INTERFACE register */
	if (!da9052->rw_pol) {
		/* We need to set 0th bit for write operation */
		sscmsg.addr = ((sscmsg.addr << 1) | RW_POL);
	} else {
		/* We need to reset 0th bit for write operation */
		sscmsg.addr = (sscmsg.addr << 1);
	}

	/* SMDK-6410 host SPI driver specific stuff */

	/* Build our spi message */
	printk(KERN_DEBUG "da9052_spi_set_page - "\
	       "Calling spi_message_init.....\n");
	spi_message_init(&message);
	memset(&xfer, 0, sizeof(xfer));

	xfer.len = 2;
	xfer.tx_buf = da9052->spi_tx_buf;
	xfer.rx_buf = da9052->spi_rx_buf;

	da9052->spi_tx_buf[0] = sscmsg.addr;
	da9052->spi_tx_buf[1] = sscmsg.data;

	printk(KERN_DEBUG "da9052_spi_set_page - "\
	       "Calling spi_message_add_tail.....\n");
	spi_message_add_tail(&xfer, &message);

	/* Now, do the i/o */
	printk(KERN_DEBUG "da9052_spi_set_page - Calling spi_sync.....\n");
	ret = spi_sync(da9052->spi_dev, &message);

	if (ret == 0) {
		/* Active Page set successfully */
		da9052->spi_active_page = page;
		return 0;
	} else {
		/* Error in setting Active Page */
		return ret;
	}

	return 0;
}

int da9052_spi_write(struct da9052 *da9052, struct da9052_ssc_msg *msg)
{

	struct spi_message message;
	struct spi_transfer xfer;
	int ret;

	/*
	 * We need a seperate copy of da9052_ssc_msg so that caller's
	 * copy remains intact
	*/
	struct da9052_ssc_msg sscmsg;

	/* Copy callers data in to our local copy */
	sscmsg.addr = msg->addr;
	sscmsg.data = msg->data;

	if ((sscmsg.addr > PAGE_0_END) &&
		(da9052->spi_active_page == PAGECON_0)) {
		/*
		* Current configuration is PAGE-0 and write request
		* for PAGE-1
		*/
		da9052_spi_set_page(da9052, PAGECON_128);
		/* Set register address accordindly */
		sscmsg.addr = (sscmsg.addr - PAGE_1_START);
	} else if ((sscmsg.addr < PAGE_1_START) &&
		(da9052->spi_active_page == PAGECON_128)) {
		/*
		* Current configuration is PAGE-1 and write request
		* for PAGE-0
		*/
		da9052_spi_set_page(da9052, PAGECON_0);
	} else if (sscmsg.addr > PAGE_0_END) {
		/*
		* Current configuration is PAGE-1 and write request
		* for PAGE-1. Just need to adjust register address
		*/
		sscmsg.addr = (sscmsg.addr - PAGE_1_START);
	}

	/* Check value of R/W_POL bit of INTERFACE register */
	if (!da9052->rw_pol) {
		/* We need to set 0th bit for write operation */
		sscmsg.addr = ((sscmsg.addr << 1) | RW_POL);
	} else {
		/* We need to reset 0th bit for write operation */
		sscmsg.addr = (sscmsg.addr << 1);
	}

	/* SMDK-6410 host SPI driver specific stuff */

	/* Build our spi message */
	spi_message_init(&message);
	memset(&xfer, 0, sizeof(xfer));

	xfer.len = 2;
	xfer.tx_buf = da9052->spi_tx_buf;
	xfer.rx_buf = da9052->spi_rx_buf;

	da9052->spi_tx_buf[0] = sscmsg.addr;
	da9052->spi_tx_buf[1] = sscmsg.data;

	spi_message_add_tail(&xfer, &message);

	/* Now, do the i/o */
	ret = spi_sync(da9052->spi_dev, &message);

	return ret;
}

int da9052_spi_write_many(struct da9052 *da9052, struct da9052_ssc_msg *sscmsg,
			  int msg_no)
{
	int cnt, ret = 0;

	for (cnt = 0; cnt < msg_no; cnt++, sscmsg++) {
		ret = da9052_ssc_write(da9052, sscmsg);
		if (ret != 0) {
			printk(KERN_DEBUG "Error in %s\n", __func__);
			return -EIO;
		}
	}

	return 0;
}

int da9052_spi_read(struct da9052 *da9052, struct da9052_ssc_msg *msg)
{

	struct spi_message message;
	struct spi_transfer xfer;
	int ret;

	/*
	* We need a seperate copy of da9052_ssc_msg so that
	* caller's copy remains intact
	*/
	struct da9052_ssc_msg sscmsg;


	/* Copy callers data in to our local copy */
	sscmsg.addr = msg->addr;
	sscmsg.data = msg->data;

	if ((sscmsg.addr > PAGE_0_END) &&
		(da9052->spi_active_page == PAGECON_0)) {
		/*
		* Current configuration is PAGE-0 and
		* read request for PAGE-1
		*/
		printk(KERN_DEBUG "da9052_spi_read - if PAGECON_128.....\n");
		da9052_spi_set_page(da9052, PAGECON_128);
		/* Set register address accordindly */
		sscmsg.addr = (sscmsg.addr - PAGE_1_START);
	} else if ((sscmsg.addr < PAGE_1_START) &&
		(da9052->spi_active_page == PAGECON_128)) {
		/*
		* Current configuration is PAGE-1 and
		* write request for PAGE-0
		*/
		printk(KERN_DEBUG "da9052_spi_read - if PAGECON_0.....\n");
		da9052_spi_set_page(da9052, PAGECON_0);
	} else if (sscmsg.addr > PAGE_0_END) {
		/*
		* Current configuration is PAGE-1 and write
		* request for PAGE-1
		* Just need to adjust register address
		*/
		sscmsg.addr = (sscmsg.addr - PAGE_1_START);
	}

	/* Check value of R/W_POL bit of INTERFACE register */
	if (da9052->rw_pol) {
		/* We need to set 0th bit for read operation */
		sscmsg.addr = ((sscmsg.addr << 1) | RW_POL);
	} else {
		/* We need to reset 0th bit for write operation */
		sscmsg.addr = (sscmsg.addr << 1);
	}

	/* SMDK-6410 host SPI driver specific stuff */

	/* Build our spi message */
	spi_message_init(&message);
	memset(&xfer, 0, sizeof(xfer));

	xfer.len = 2;
	xfer.tx_buf = da9052->spi_tx_buf;
	xfer.rx_buf = da9052->spi_rx_buf;

	da9052->spi_tx_buf[0] = sscmsg.addr;
	da9052->spi_tx_buf[1] = 0xff;

	da9052->spi_rx_buf[0] = 0;
	da9052->spi_rx_buf[1] = 0;

	spi_message_add_tail(&xfer, &message);

	/* Now, do the i/o */
	ret = spi_sync(da9052->spi_dev, &message);

	if (ret == 0) {
		/* Update read value in callers copy */
		msg->data = da9052->spi_rx_buf[1];
		return 0;
	} else {
		return ret;
	}


	return 0;
}

int da9052_spi_read_many(struct da9052 *da9052, struct da9052_ssc_msg *sscmsg,
			 int msg_no)
{
	int cnt, ret = 0;

	for (cnt = 0; cnt < msg_no; cnt++, sscmsg++) {
		ret = da9052_ssc_read(da9052, sscmsg);
		if (ret != 0) {
			printk(KERN_DEBUG "Error in %s\n", __func__);
			return -EIO;
		}
	}

	return 0;
}

static int __init da9052_spi_init(void)
{
	int ret = 0;
	/*printk("Entered da9052_spi_init.....\n");*/
	ret = spi_register_driver(&da9052_spi_driver);
	if (ret != 0) {
		printk(KERN_ERR "Unable to register %s\n",
		       DA9052_SSC_SPI_DEVICE_NAME);
		return ret;
	}
	return 0;
}
module_init(da9052_spi_init);

static void __exit da9052_spi_exit(void)
{
	spi_unregister_driver(&da9052_spi_driver);
}

module_exit(da9052_spi_exit);

MODULE_AUTHOR("Dialog Semiconductor Ltd <dchen@diasemi.com>");
MODULE_DESCRIPTION("SPI driver for Dialog DA9052 PMIC");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:" DA9052_SSC_SPI_DEVICE_NAME);