OSDN Git Service

spi: xcomm: Remove duplicate code to set default bits_per_word and max speed
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / spi / spi-xcomm.c
1 /*
2  * Analog Devices AD-FMCOMMS1-EBZ board I2C-SPI bridge driver
3  *
4  * Copyright 2012 Analog Devices Inc.
5  * Author: Lars-Peter Clausen <lars@metafoo.de>
6  *
7  * Licensed under the GPL-2 or later.
8  */
9
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>
17
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
28
29 #define SPI_XCOMM_CMD_UPDATE_CONFIG     0x03
30 #define SPI_XCOMM_CMD_WRITE             0x04
31
32 #define SPI_XCOMM_CLOCK 48000000
33
34 struct spi_xcomm {
35         struct i2c_client *i2c;
36
37         uint16_t settings;
38         uint16_t chipselect;
39
40         unsigned int current_speed;
41
42         uint8_t buf[63];
43 };
44
45 static int spi_xcomm_sync_config(struct spi_xcomm *spi_xcomm, unsigned int len)
46 {
47         uint16_t settings;
48         uint8_t *buf = spi_xcomm->buf;
49
50         settings = spi_xcomm->settings;
51         settings |= len << SPI_XCOMM_SETTINGS_LEN_OFFSET;
52
53         buf[0] = SPI_XCOMM_CMD_UPDATE_CONFIG;
54         put_unaligned_be16(settings, &buf[1]);
55         put_unaligned_be16(spi_xcomm->chipselect, &buf[3]);
56
57         return i2c_master_send(spi_xcomm->i2c, buf, 5);
58 }
59
60 static void spi_xcomm_chipselect(struct spi_xcomm *spi_xcomm,
61         struct spi_device *spi, int is_active)
62 {
63         unsigned long cs = spi->chip_select;
64         uint16_t chipselect = spi_xcomm->chipselect;
65
66         if (is_active)
67                 chipselect |= BIT(cs);
68         else
69                 chipselect &= ~BIT(cs);
70
71         spi_xcomm->chipselect = chipselect;
72 }
73
74 static int spi_xcomm_setup_transfer(struct spi_xcomm *spi_xcomm,
75         struct spi_device *spi, struct spi_transfer *t, unsigned int *settings)
76 {
77         if (t->len > 62)
78                 return -EINVAL;
79
80         if (t->speed_hz != spi_xcomm->current_speed) {
81                 unsigned int divider;
82
83                 divider = DIV_ROUND_UP(SPI_XCOMM_CLOCK, t->speed_hz);
84                 if (divider >= 64)
85                         *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_64;
86                 else if (divider >= 16)
87                         *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_16;
88                 else
89                         *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_4;
90
91                 spi_xcomm->current_speed = t->speed_hz;
92         }
93
94         if (spi->mode & SPI_CPOL)
95                 *settings |= SPI_XCOMM_SETTINGS_CPOL;
96         else
97                 *settings &= ~SPI_XCOMM_SETTINGS_CPOL;
98
99         if (spi->mode & SPI_CPHA)
100                 *settings &= ~SPI_XCOMM_SETTINGS_CPHA;
101         else
102                 *settings |= SPI_XCOMM_SETTINGS_CPHA;
103
104         if (spi->mode & SPI_3WIRE)
105                 *settings |= SPI_XCOMM_SETTINGS_3WIRE;
106         else
107                 *settings &= ~SPI_XCOMM_SETTINGS_3WIRE;
108
109         return 0;
110 }
111
112 static int spi_xcomm_txrx_bufs(struct spi_xcomm *spi_xcomm,
113         struct spi_device *spi, struct spi_transfer *t)
114 {
115         int ret;
116
117         if (t->tx_buf) {
118                 spi_xcomm->buf[0] = SPI_XCOMM_CMD_WRITE;
119                 memcpy(spi_xcomm->buf + 1, t->tx_buf, t->len);
120
121                 ret = i2c_master_send(spi_xcomm->i2c, spi_xcomm->buf, t->len + 1);
122                 if (ret < 0)
123                         return ret;
124                 else if (ret != t->len + 1)
125                         return -EIO;
126         } else if (t->rx_buf) {
127                 ret = i2c_master_recv(spi_xcomm->i2c, t->rx_buf, t->len);
128                 if (ret < 0)
129                         return ret;
130                 else if (ret != t->len)
131                         return -EIO;
132         }
133
134         return t->len;
135 }
136
137 static int spi_xcomm_transfer_one(struct spi_master *master,
138         struct spi_message *msg)
139 {
140         struct spi_xcomm *spi_xcomm = spi_master_get_devdata(master);
141         unsigned int settings = spi_xcomm->settings;
142         struct spi_device *spi = msg->spi;
143         unsigned cs_change = 0;
144         struct spi_transfer *t;
145         bool is_first = true;
146         int status = 0;
147         bool is_last;
148
149         spi_xcomm_chipselect(spi_xcomm, spi, true);
150
151         list_for_each_entry(t, &msg->transfers, transfer_list) {
152
153                 if (!t->tx_buf && !t->rx_buf && t->len) {
154                         status = -EINVAL;
155                         break;
156                 }
157
158                 status = spi_xcomm_setup_transfer(spi_xcomm, spi, t, &settings);
159                 if (status < 0)
160                         break;
161
162                 is_last = list_is_last(&t->transfer_list, &msg->transfers);
163                 cs_change = t->cs_change;
164
165                 if (cs_change ^ is_last)
166                         settings |= BIT(5);
167                 else
168                         settings &= ~BIT(5);
169
170                 if (t->rx_buf) {
171                         spi_xcomm->settings = settings;
172                         status = spi_xcomm_sync_config(spi_xcomm, t->len);
173                         if (status < 0)
174                                 break;
175                 } else if (settings != spi_xcomm->settings || is_first) {
176                         spi_xcomm->settings = settings;
177                         status = spi_xcomm_sync_config(spi_xcomm, 0);
178                         if (status < 0)
179                                 break;
180                 }
181
182                 if (t->len) {
183                         status = spi_xcomm_txrx_bufs(spi_xcomm, spi, t);
184
185                         if (status < 0)
186                                 break;
187
188                         if (status > 0)
189                                 msg->actual_length += status;
190                 }
191                 status = 0;
192
193                 if (t->delay_usecs)
194                         udelay(t->delay_usecs);
195
196                 is_first = false;
197         }
198
199         if (status != 0 || !cs_change)
200                 spi_xcomm_chipselect(spi_xcomm, spi, false);
201
202         msg->status = status;
203         spi_finalize_current_message(master);
204
205         return status;
206 }
207
208 static int spi_xcomm_probe(struct i2c_client *i2c,
209         const struct i2c_device_id *id)
210 {
211         struct spi_xcomm *spi_xcomm;
212         struct spi_master *master;
213         int ret;
214
215         master = spi_alloc_master(&i2c->dev, sizeof(*spi_xcomm));
216         if (!master)
217                 return -ENOMEM;
218
219         spi_xcomm = spi_master_get_devdata(master);
220         spi_xcomm->i2c = i2c;
221
222         master->num_chipselect = 16;
223         master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_3WIRE;
224         master->bits_per_word_mask = SPI_BPW_MASK(8);
225         master->flags = SPI_MASTER_HALF_DUPLEX;
226         master->transfer_one_message = spi_xcomm_transfer_one;
227         master->dev.of_node = i2c->dev.of_node;
228         i2c_set_clientdata(i2c, master);
229
230         ret = devm_spi_register_master(&i2c->dev, master);
231         if (ret < 0)
232                 spi_master_put(master);
233
234         return ret;
235 }
236
237 static const struct i2c_device_id spi_xcomm_ids[] = {
238         { "spi-xcomm" },
239         { },
240 };
241
242 static struct i2c_driver spi_xcomm_driver = {
243         .driver = {
244                 .name   = "spi-xcomm",
245                 .owner  = THIS_MODULE,
246         },
247         .id_table       = spi_xcomm_ids,
248         .probe          = spi_xcomm_probe,
249 };
250 module_i2c_driver(spi_xcomm_driver);
251
252 MODULE_LICENSE("GPL");
253 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
254 MODULE_DESCRIPTION("Analog Devices AD-FMCOMMS1-EBZ board I2C-SPI bridge driver");