summaryrefslogtreecommitdiffstats
path: root/drivers/mfd/ab8500-spi.c
blob: 01b6d584442cdcf686c8c4f643193b9c51360e4a (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
/*
 * Copyright (C) ST-Ericsson SA 2010
 *
 * License Terms: GNU General Public License v2
 * Author: Srinidhi Kasagar <srinidhi.kasagar@stericsson.com>
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/spi/spi.h>
#include <linux/mfd/ab8500.h>

/*
 * This funtion writes to any AB8500 registers using
 * SPI protocol &  before it writes it packs the data
 * in the below 24 bit frame format
 *
 *	 *|------------------------------------|
 *	 *| 23|22...18|17.......10|9|8|7......0|
 *	 *| r/w  bank       adr          data  |
 *	 * ------------------------------------
 *
 * This function shouldn't be called from interrupt
 * context
 */
static int ab8500_spi_write(struct ab8500 *ab8500, u16 addr, u8 data)
{
	struct spi_device *spi = container_of(ab8500->dev, struct spi_device,
					      dev);
	unsigned long spi_data = addr << 10 | data;
	struct spi_transfer xfer;
	struct spi_message msg;

	ab8500->tx_buf[0] = spi_data;
	ab8500->rx_buf[0] = 0;

	xfer.tx_buf	= ab8500->tx_buf;
	xfer.rx_buf	= NULL;
	xfer.len	= sizeof(unsigned long);

	spi_message_init(&msg);
	spi_message_add_tail(&xfer, &msg);

	return spi_sync(spi, &msg);
}

static int ab8500_spi_read(struct ab8500 *ab8500, u16 addr)
{
	struct spi_device *spi = container_of(ab8500->dev, struct spi_device,
					      dev);
	unsigned long spi_data = 1 << 23 | addr << 10;
	struct spi_transfer xfer;
	struct spi_message msg;
	int ret;

	ab8500->tx_buf[0] = spi_data;
	ab8500->rx_buf[0] = 0;

	xfer.tx_buf	= ab8500->tx_buf;
	xfer.rx_buf	= ab8500->rx_buf;
	xfer.len	= sizeof(unsigned long);

	spi_message_init(&msg);
	spi_message_add_tail(&xfer, &msg);

	ret = spi_sync(spi, &msg);
	if (!ret)
		/*
		 * Only the 8 lowermost bytes are
		 * defined with value, the rest may
		 * vary depending on chip/board noise.
		 */
		ret = ab8500->rx_buf[0] & 0xFFU;

	return ret;
}

static int __devinit ab8500_spi_probe(struct spi_device *spi)
{
	struct ab8500 *ab8500;
	int ret;

	spi->bits_per_word = 24;
	ret = spi_setup(spi);
	if (ret < 0)
		return ret;

	ab8500 = kzalloc(sizeof *ab8500, GFP_KERNEL);
	if (!ab8500)
		return -ENOMEM;

	ab8500->dev = &spi->dev;
	ab8500->irq = spi->irq;

	ab8500->read = ab8500_spi_read;
	ab8500->write = ab8500_spi_write;

	spi_set_drvdata(spi, ab8500);

	ret = ab8500_init(ab8500);
	if (ret)
		kfree(ab8500);

	return ret;
}

static int __devexit ab8500_spi_remove(struct spi_device *spi)
{
	struct ab8500 *ab8500 = spi_get_drvdata(spi);

	ab8500_exit(ab8500);
	kfree(ab8500);

	return 0;
}

static struct spi_driver ab8500_spi_driver = {
	.driver = {
		.name = "ab8500",
		.owner = THIS_MODULE,
	},
	.probe	= ab8500_spi_probe,
	.remove	= __devexit_p(ab8500_spi_remove)
};

static int __init ab8500_spi_init(void)
{
	return spi_register_driver(&ab8500_spi_driver);
}
subsys_initcall(ab8500_spi_init);

static void __exit ab8500_spi_exit(void)
{
	spi_unregister_driver(&ab8500_spi_driver);
}
module_exit(ab8500_spi_exit);

MODULE_AUTHOR("Srinidhi KASAGAR <srinidhi.kasagar@stericsson.com");
MODULE_DESCRIPTION("AB8500 SPI");
MODULE_LICENSE("GPL v2");