b53: clean up code to match kernel style better
[openwrt.git] / target / linux / generic / files / drivers / net / phy / b53 / b53_spi.c
1 /*
2  * B53 register access through SPI
3  *
4  * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #include <asm/unaligned.h>
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/spi/spi.h>
24 #include <linux/platform_data/b53.h>
25
26 #include "b53_priv.h"
27
28 #define B53_SPI_DATA            0xf0
29
30 #define B53_SPI_STATUS          0xfe
31 #define B53_SPI_CMD_SPIF        BIT(7)
32 #define B53_SPI_CMD_RACK        BIT(5)
33
34 #define B53_SPI_CMD_READ        0x00
35 #define B53_SPI_CMD_WRITE       0x01
36 #define B53_SPI_CMD_NORMAL      0x60
37 #define B53_SPI_CMD_FAST        0x10
38
39 #define B53_SPI_PAGE_SELECT     0xff
40
41 static inline int b53_spi_read_reg(struct spi_device *spi, u8 reg, u8 *val,
42                                      unsigned len)
43 {
44         u8 txbuf[2];
45
46         txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_READ;
47         txbuf[1] = reg;
48
49         return spi_write_then_read(spi, txbuf, 2, val, len);
50 }
51
52 static inline int b53_spi_clear_status(struct spi_device *spi)
53 {
54         unsigned int i;
55         u8 rxbuf;
56         int ret;
57
58         for (i = 0; i < 10; i++) {
59                 ret = b53_spi_read_reg(spi, B53_SPI_STATUS, &rxbuf, 1);
60                 if (ret)
61                         return ret;
62
63                 if (!(rxbuf & B53_SPI_CMD_SPIF))
64                         break;
65
66                 mdelay(1);
67         }
68
69         if (i == 10)
70                 return -EIO;
71
72         return 0;
73 }
74
75 static inline int b53_spi_set_page(struct spi_device *spi, u8 page)
76 {
77         u8 txbuf[3];
78
79         txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
80         txbuf[1] = B53_SPI_PAGE_SELECT;
81         txbuf[2] = page;
82
83         return spi_write(spi, txbuf, sizeof(txbuf));
84 }
85
86 static inline int b53_prepare_reg_access(struct spi_device *spi, u8 page)
87 {
88         int ret = b53_spi_clear_status(spi);
89
90         if (ret)
91                 return ret;
92
93         return b53_spi_set_page(spi, page);
94 }
95
96 static int b53_spi_prepare_reg_read(struct spi_device *spi, u8 reg)
97 {
98         u8 rxbuf;
99         int retry_count;
100         int ret;
101
102         ret = b53_spi_read_reg(spi, reg, &rxbuf, 1);
103         if (ret)
104                 return ret;
105
106         for (retry_count = 0; retry_count < 10; retry_count++) {
107                 ret = b53_spi_read_reg(spi, B53_SPI_STATUS, &rxbuf, 1);
108                 if (ret)
109                         return ret;
110
111                 if (rxbuf & B53_SPI_CMD_RACK)
112                         break;
113
114                 mdelay(1);
115         }
116
117         if (retry_count == 10)
118                 return -EIO;
119
120         return 0;
121 }
122
123 static int b53_spi_read(struct b53_device *dev, u8 page, u8 reg, u8 *data,
124                         unsigned len)
125 {
126         struct spi_device *spi = dev->priv;
127         int ret;
128
129         ret = b53_prepare_reg_access(spi, page);
130         if (ret)
131                 return ret;
132
133         ret = b53_spi_prepare_reg_read(spi, reg);
134         if (ret)
135                 return ret;
136
137         return b53_spi_read_reg(spi, B53_SPI_DATA, data, len);
138 }
139
140 static int b53_spi_read8(struct b53_device *dev, u8 page, u8 reg, u8 *val)
141 {
142         return b53_spi_read(dev, page, reg, val, 1);
143 }
144
145 static int b53_spi_read16(struct b53_device *dev, u8 page, u8 reg, u16 *val)
146 {
147         int ret = b53_spi_read(dev, page, reg, (u8 *)val, 2);
148
149         if (!ret)
150                 *val = le16_to_cpu(*val);
151
152         return ret;
153 }
154
155 static int b53_spi_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val)
156 {
157         int ret = b53_spi_read(dev, page, reg, (u8 *)val, 4);
158
159         if (!ret)
160                 *val = le32_to_cpu(*val);
161
162         return ret;
163 }
164
165 static int b53_spi_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val)
166 {
167         int ret;
168
169         *val = 0;
170         ret = b53_spi_read(dev, page, reg, (u8 *)val, 6);
171         if (!ret)
172                 *val = le64_to_cpu(*val);
173
174         return ret;
175 }
176
177 static int b53_spi_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val)
178 {
179         int ret = b53_spi_read(dev, page, reg, (u8 *)val, 8);
180
181         if (!ret)
182                 *val = le64_to_cpu(*val);
183
184         return ret;
185 }
186
187 static int b53_spi_write8(struct b53_device *dev, u8 page, u8 reg, u8 value)
188 {
189         struct spi_device *spi = dev->priv;
190         int ret;
191         u8 txbuf[3];
192
193         ret = b53_prepare_reg_access(spi, page);
194         if (ret)
195                 return ret;
196
197         txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
198         txbuf[1] = reg;
199         txbuf[2] = value;
200
201         return spi_write(spi, txbuf, sizeof(txbuf));
202 }
203
204 static int b53_spi_write16(struct b53_device *dev, u8 page, u8 reg, u16 value)
205 {
206         struct spi_device *spi = dev->priv;
207         int ret;
208         u8 txbuf[4];
209
210         ret = b53_prepare_reg_access(spi, page);
211         if (ret)
212                 return ret;
213
214         txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
215         txbuf[1] = reg;
216         put_unaligned_le16(value, &txbuf[2]);
217
218         return spi_write(spi, txbuf, sizeof(txbuf));
219 }
220
221 static int b53_spi_write32(struct b53_device *dev, u8 page, u8 reg, u32 value)
222 {
223         struct spi_device *spi = dev->priv;
224         int ret;
225         u8 txbuf[6];
226
227         ret = b53_prepare_reg_access(spi, page);
228         if (ret)
229                 return ret;
230
231         txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
232         txbuf[1] = reg;
233         put_unaligned_le32(value, &txbuf[2]);
234
235         return spi_write(spi, txbuf, sizeof(txbuf));
236 }
237
238 static int b53_spi_write48(struct b53_device *dev, u8 page, u8 reg, u64 value)
239 {
240         struct spi_device *spi = dev->priv;
241         int ret;
242         u8 txbuf[10];
243
244         ret = b53_prepare_reg_access(spi, page);
245         if (ret)
246                 return ret;
247
248         txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
249         txbuf[1] = reg;
250         put_unaligned_le64(value, &txbuf[2]);
251
252         return spi_write(spi, txbuf, sizeof(txbuf) - 2);
253 }
254
255 static int b53_spi_write64(struct b53_device *dev, u8 page, u8 reg, u64 value)
256 {
257         struct spi_device *spi = dev->priv;
258         int ret;
259         u8 txbuf[10];
260
261         ret = b53_prepare_reg_access(spi, page);
262         if (ret)
263                 return ret;
264
265         txbuf[0] = B53_SPI_CMD_NORMAL | B53_SPI_CMD_WRITE;
266         txbuf[1] = reg;
267         put_unaligned_le64(value, &txbuf[2]);
268
269         return spi_write(spi, txbuf, sizeof(txbuf));
270 }
271
272 static struct b53_io_ops b53_spi_ops = {
273         .read8 = b53_spi_read8,
274         .read16 = b53_spi_read16,
275         .read32 = b53_spi_read32,
276         .read48 = b53_spi_read48,
277         .read64 = b53_spi_read64,
278         .write8 = b53_spi_write8,
279         .write16 = b53_spi_write16,
280         .write32 = b53_spi_write32,
281         .write48 = b53_spi_write48,
282         .write64 = b53_spi_write64,
283 };
284
285 static int b53_spi_probe(struct spi_device *spi)
286 {
287         struct b53_device *dev;
288         int ret;
289
290         dev = b53_switch_alloc(&spi->dev, &b53_spi_ops, spi);
291         if (!dev)
292                 return -ENOMEM;
293
294         if (spi->dev.platform_data)
295                 dev->pdata = spi->dev.platform_data;
296
297         ret = b53_switch_register(dev);
298         if (ret)
299                 return ret;
300
301         spi_set_drvdata(spi, dev);
302
303         return 0;
304 }
305
306 static int b53_spi_remove(struct spi_device *spi)
307 {
308         struct b53_device *dev = spi_get_drvdata(spi);
309
310         if (dev)
311                 b53_switch_remove(dev);
312
313         return 0;
314 }
315
316 static struct spi_driver b53_spi_driver = {
317         .driver = {
318                 .name   = "b53-switch",
319                 .bus    = &spi_bus_type,
320                 .owner  = THIS_MODULE,
321         },
322         .probe  = b53_spi_probe,
323         .remove = b53_spi_remove,
324 };
325
326 module_spi_driver(b53_spi_driver);
327
328 MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
329 MODULE_DESCRIPTION("B53 SPI access driver");
330 MODULE_LICENSE("Dual BSD/GPL");