Linux 3.12.39
[linux/fpc-iii.git] / drivers / spi / spi-xcomm.c
blob4258c712ad3c273d5f8ab571e9b3f829d0f256cf
1 /*
2 * Analog Devices AD-FMCOMMS1-EBZ board I2C-SPI bridge driver
4 * Copyright 2012 Analog Devices Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 * Licensed under the GPL-2 or later.
8 */
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/i2c.h>
15 #include <linux/spi/spi.h>
16 #include <asm/unaligned.h>
18 #define SPI_XCOMM_SETTINGS_LEN_OFFSET 10
19 #define SPI_XCOMM_SETTINGS_3WIRE BIT(6)
20 #define SPI_XCOMM_SETTINGS_CS_HIGH BIT(5)
21 #define SPI_XCOMM_SETTINGS_SAMPLE_END BIT(4)
22 #define SPI_XCOMM_SETTINGS_CPHA BIT(3)
23 #define SPI_XCOMM_SETTINGS_CPOL BIT(2)
24 #define SPI_XCOMM_SETTINGS_CLOCK_DIV_MASK 0x3
25 #define SPI_XCOMM_SETTINGS_CLOCK_DIV_64 0x2
26 #define SPI_XCOMM_SETTINGS_CLOCK_DIV_16 0x1
27 #define SPI_XCOMM_SETTINGS_CLOCK_DIV_4 0x0
29 #define SPI_XCOMM_CMD_UPDATE_CONFIG 0x03
30 #define SPI_XCOMM_CMD_WRITE 0x04
32 #define SPI_XCOMM_CLOCK 48000000
34 struct spi_xcomm {
35 struct i2c_client *i2c;
37 uint16_t settings;
38 uint16_t chipselect;
40 unsigned int current_speed;
42 uint8_t buf[63];
45 static int spi_xcomm_sync_config(struct spi_xcomm *spi_xcomm, unsigned int len)
47 uint16_t settings;
48 uint8_t *buf = spi_xcomm->buf;
50 settings = spi_xcomm->settings;
51 settings |= len << SPI_XCOMM_SETTINGS_LEN_OFFSET;
53 buf[0] = SPI_XCOMM_CMD_UPDATE_CONFIG;
54 put_unaligned_be16(settings, &buf[1]);
55 put_unaligned_be16(spi_xcomm->chipselect, &buf[3]);
57 return i2c_master_send(spi_xcomm->i2c, buf, 5);
60 static void spi_xcomm_chipselect(struct spi_xcomm *spi_xcomm,
61 struct spi_device *spi, int is_active)
63 unsigned long cs = spi->chip_select;
64 uint16_t chipselect = spi_xcomm->chipselect;
66 if (is_active)
67 chipselect |= BIT(cs);
68 else
69 chipselect &= ~BIT(cs);
71 spi_xcomm->chipselect = chipselect;
74 static int spi_xcomm_setup_transfer(struct spi_xcomm *spi_xcomm,
75 struct spi_device *spi, struct spi_transfer *t, unsigned int *settings)
77 unsigned int speed;
79 if (t->len > 62)
80 return -EINVAL;
82 speed = t->speed_hz ? t->speed_hz : spi->max_speed_hz;
84 if (speed != spi_xcomm->current_speed) {
85 unsigned int divider = DIV_ROUND_UP(SPI_XCOMM_CLOCK, speed);
86 if (divider >= 64)
87 *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_64;
88 else if (divider >= 16)
89 *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_16;
90 else
91 *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_4;
93 spi_xcomm->current_speed = speed;
96 if (spi->mode & SPI_CPOL)
97 *settings |= SPI_XCOMM_SETTINGS_CPOL;
98 else
99 *settings &= ~SPI_XCOMM_SETTINGS_CPOL;
101 if (spi->mode & SPI_CPHA)
102 *settings &= ~SPI_XCOMM_SETTINGS_CPHA;
103 else
104 *settings |= SPI_XCOMM_SETTINGS_CPHA;
106 if (spi->mode & SPI_3WIRE)
107 *settings |= SPI_XCOMM_SETTINGS_3WIRE;
108 else
109 *settings &= ~SPI_XCOMM_SETTINGS_3WIRE;
111 return 0;
114 static int spi_xcomm_txrx_bufs(struct spi_xcomm *spi_xcomm,
115 struct spi_device *spi, struct spi_transfer *t)
117 int ret;
119 if (t->tx_buf) {
120 spi_xcomm->buf[0] = SPI_XCOMM_CMD_WRITE;
121 memcpy(spi_xcomm->buf + 1, t->tx_buf, t->len);
123 ret = i2c_master_send(spi_xcomm->i2c, spi_xcomm->buf, t->len + 1);
124 if (ret < 0)
125 return ret;
126 else if (ret != t->len + 1)
127 return -EIO;
128 } else if (t->rx_buf) {
129 ret = i2c_master_recv(spi_xcomm->i2c, t->rx_buf, t->len);
130 if (ret < 0)
131 return ret;
132 else if (ret != t->len)
133 return -EIO;
136 return t->len;
139 static int spi_xcomm_transfer_one(struct spi_master *master,
140 struct spi_message *msg)
142 struct spi_xcomm *spi_xcomm = spi_master_get_devdata(master);
143 unsigned int settings = spi_xcomm->settings;
144 struct spi_device *spi = msg->spi;
145 unsigned cs_change = 0;
146 struct spi_transfer *t;
147 bool is_first = true;
148 int status = 0;
149 bool is_last;
151 is_first = true;
153 spi_xcomm_chipselect(spi_xcomm, spi, true);
155 list_for_each_entry(t, &msg->transfers, transfer_list) {
157 if (!t->tx_buf && !t->rx_buf && t->len) {
158 status = -EINVAL;
159 break;
162 status = spi_xcomm_setup_transfer(spi_xcomm, spi, t, &settings);
163 if (status < 0)
164 break;
166 is_last = list_is_last(&t->transfer_list, &msg->transfers);
167 cs_change = t->cs_change;
169 if (cs_change ^ is_last)
170 settings |= BIT(5);
171 else
172 settings &= ~BIT(5);
174 if (t->rx_buf) {
175 spi_xcomm->settings = settings;
176 status = spi_xcomm_sync_config(spi_xcomm, t->len);
177 if (status < 0)
178 break;
179 } else if (settings != spi_xcomm->settings || is_first) {
180 spi_xcomm->settings = settings;
181 status = spi_xcomm_sync_config(spi_xcomm, 0);
182 if (status < 0)
183 break;
186 if (t->len) {
187 status = spi_xcomm_txrx_bufs(spi_xcomm, spi, t);
189 if (status < 0)
190 break;
192 if (status > 0)
193 msg->actual_length += status;
195 status = 0;
197 if (t->delay_usecs)
198 udelay(t->delay_usecs);
200 is_first = false;
203 if (status != 0 || !cs_change)
204 spi_xcomm_chipselect(spi_xcomm, spi, false);
206 msg->status = status;
207 spi_finalize_current_message(master);
209 return status;
212 static int spi_xcomm_probe(struct i2c_client *i2c,
213 const struct i2c_device_id *id)
215 struct spi_xcomm *spi_xcomm;
216 struct spi_master *master;
217 int ret;
219 master = spi_alloc_master(&i2c->dev, sizeof(*spi_xcomm));
220 if (!master)
221 return -ENOMEM;
223 spi_xcomm = spi_master_get_devdata(master);
224 spi_xcomm->i2c = i2c;
226 master->num_chipselect = 16;
227 master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_3WIRE;
228 master->bits_per_word_mask = SPI_BPW_MASK(8);
229 master->flags = SPI_MASTER_HALF_DUPLEX;
230 master->transfer_one_message = spi_xcomm_transfer_one;
231 master->dev.of_node = i2c->dev.of_node;
232 i2c_set_clientdata(i2c, master);
234 ret = spi_register_master(master);
235 if (ret < 0)
236 spi_master_put(master);
238 return ret;
241 static int spi_xcomm_remove(struct i2c_client *i2c)
243 struct spi_master *master = i2c_get_clientdata(i2c);
245 spi_unregister_master(master);
247 return 0;
250 static const struct i2c_device_id spi_xcomm_ids[] = {
251 { "spi-xcomm" },
252 { },
255 static struct i2c_driver spi_xcomm_driver = {
256 .driver = {
257 .name = "spi-xcomm",
258 .owner = THIS_MODULE,
260 .id_table = spi_xcomm_ids,
261 .probe = spi_xcomm_probe,
262 .remove = spi_xcomm_remove,
264 module_i2c_driver(spi_xcomm_driver);
266 MODULE_LICENSE("GPL");
267 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
268 MODULE_DESCRIPTION("Analog Devices AD-FMCOMMS1-EBZ board I2C-SPI bridge driver");