Merge branch 'for_linus' of git://cavan.codon.org.uk/platform-drivers-x86
[linux-drm-fsl-dcu.git] / drivers / net / wireless / cw1200 / cw1200_spi.c
1 /*
2  * Mac80211 SPI driver for ST-Ericsson CW1200 device
3  *
4  * Copyright (c) 2011, Sagrad Inc.
5  * Author:  Solomon Peachy <speachy@sagrad.com>
6  *
7  * Based on cw1200_sdio.c
8  * Copyright (c) 2010, ST-Ericsson
9  * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  */
15
16 #include <linux/module.h>
17 #include <linux/gpio.h>
18 #include <linux/delay.h>
19 #include <linux/spinlock.h>
20 #include <linux/interrupt.h>
21 #include <net/mac80211.h>
22
23 #include <linux/spi/spi.h>
24 #include <linux/device.h>
25
26 #include "cw1200.h"
27 #include "hwbus.h"
28 #include <linux/platform_data/net-cw1200.h>
29 #include "hwio.h"
30
31 MODULE_AUTHOR("Solomon Peachy <speachy@sagrad.com>");
32 MODULE_DESCRIPTION("mac80211 ST-Ericsson CW1200 SPI driver");
33 MODULE_LICENSE("GPL");
34 MODULE_ALIAS("spi:cw1200_wlan_spi");
35
36 /* #define SPI_DEBUG */
37
38 struct hwbus_priv {
39         struct spi_device       *func;
40         struct cw1200_common    *core;
41         const struct cw1200_platform_data_spi *pdata;
42         spinlock_t              lock; /* Serialize all bus operations */
43         int claimed;
44 };
45
46 #define SDIO_TO_SPI_ADDR(addr) ((addr & 0x1f)>>2)
47 #define SET_WRITE 0x7FFF /* usage: and operation */
48 #define SET_READ 0x8000  /* usage: or operation */
49
50 /* Notes on byte ordering:
51    LE:  B0 B1 B2 B3
52    BE:  B3 B2 B1 B0
53
54    Hardware expects 32-bit data to be written as 16-bit BE words:
55
56    B1 B0 B3 B2
57 */
58
59 static int cw1200_spi_memcpy_fromio(struct hwbus_priv *self,
60                                      unsigned int addr,
61                                      void *dst, int count)
62 {
63         int ret, i;
64         u16 regaddr;
65         struct spi_message      m;
66
67         struct spi_transfer     t_addr = {
68                 .tx_buf         = &regaddr,
69                 .len            = sizeof(regaddr),
70         };
71         struct spi_transfer     t_msg = {
72                 .rx_buf         = dst,
73                 .len            = count,
74         };
75
76         regaddr = (SDIO_TO_SPI_ADDR(addr))<<12;
77         regaddr |= SET_READ;
78         regaddr |= (count>>1);
79
80 #ifdef SPI_DEBUG
81         pr_info("READ : %04d from 0x%02x (%04x)\n", count, addr, regaddr);
82 #endif
83
84         /* Header is LE16 */
85         regaddr = cpu_to_le16(regaddr);
86
87         /* We have to byteswap if the SPI bus is limited to 8b operation
88            or we are running on a Big Endian system
89         */
90 #if defined(__LITTLE_ENDIAN)
91         if (self->func->bits_per_word == 8)
92 #endif
93                 regaddr = swab16(regaddr);
94
95         spi_message_init(&m);
96         spi_message_add_tail(&t_addr, &m);
97         spi_message_add_tail(&t_msg, &m);
98         ret = spi_sync(self->func, &m);
99
100 #ifdef SPI_DEBUG
101         pr_info("READ : ");
102         for (i = 0; i < t_addr.len; i++)
103                 printk("%02x ", ((u8 *)t_addr.tx_buf)[i]);
104         printk(" : ");
105         for (i = 0; i < t_msg.len; i++)
106                 printk("%02x ", ((u8 *)t_msg.rx_buf)[i]);
107         printk("\n");
108 #endif
109
110         /* We have to byteswap if the SPI bus is limited to 8b operation
111            or we are running on a Big Endian system
112         */
113 #if defined(__LITTLE_ENDIAN)
114         if (self->func->bits_per_word == 8)
115 #endif
116         {
117                 uint16_t *buf = (uint16_t *)dst;
118                 for (i = 0; i < ((count + 1) >> 1); i++)
119                         buf[i] = swab16(buf[i]);
120         }
121
122         return ret;
123 }
124
125 static int cw1200_spi_memcpy_toio(struct hwbus_priv *self,
126                                    unsigned int addr,
127                                    const void *src, int count)
128 {
129         int rval, i;
130         u16 regaddr;
131         struct spi_transfer     t_addr = {
132                 .tx_buf         = &regaddr,
133                 .len            = sizeof(regaddr),
134         };
135         struct spi_transfer     t_msg = {
136                 .tx_buf         = src,
137                 .len            = count,
138         };
139         struct spi_message      m;
140
141         regaddr = (SDIO_TO_SPI_ADDR(addr))<<12;
142         regaddr &= SET_WRITE;
143         regaddr |= (count>>1);
144
145 #ifdef SPI_DEBUG
146         pr_info("WRITE: %04d  to  0x%02x (%04x)\n", count, addr, regaddr);
147 #endif
148
149         /* Header is LE16 */
150         regaddr = cpu_to_le16(regaddr);
151
152         /* We have to byteswap if the SPI bus is limited to 8b operation
153            or we are running on a Big Endian system
154         */
155 #if defined(__LITTLE_ENDIAN)
156         if (self->func->bits_per_word == 8)
157 #endif
158         {
159                 uint16_t *buf = (uint16_t *)src;
160                 regaddr = swab16(regaddr);
161                 for (i = 0; i < ((count + 1) >> 1); i++)
162                         buf[i] = swab16(buf[i]);
163         }
164
165 #ifdef SPI_DEBUG
166         pr_info("WRITE: ");
167         for (i = 0; i < t_addr.len; i++)
168                 printk("%02x ", ((u8 *)t_addr.tx_buf)[i]);
169         printk(" : ");
170         for (i = 0; i < t_msg.len; i++)
171                 printk("%02x ", ((u8 *)t_msg.tx_buf)[i]);
172         printk("\n");
173 #endif
174
175         spi_message_init(&m);
176         spi_message_add_tail(&t_addr, &m);
177         spi_message_add_tail(&t_msg, &m);
178         rval = spi_sync(self->func, &m);
179
180 #ifdef SPI_DEBUG
181         pr_info("WROTE: %d\n", m.actual_length);
182 #endif
183
184 #if defined(__LITTLE_ENDIAN)
185         /* We have to byteswap if the SPI bus is limited to 8b operation */
186         if (self->func->bits_per_word == 8)
187 #endif
188         {
189                 uint16_t *buf = (uint16_t *)src;
190                 for (i = 0; i < ((count + 1) >> 1); i++)
191                         buf[i] = swab16(buf[i]);
192         }
193         return rval;
194 }
195
196 static void cw1200_spi_lock(struct hwbus_priv *self)
197 {
198         unsigned long flags;
199
200         might_sleep();
201
202         spin_lock_irqsave(&self->lock, flags);
203         while (1) {
204                 set_current_state(TASK_UNINTERRUPTIBLE);
205                 if (!self->claimed)
206                         break;
207                 spin_unlock_irqrestore(&self->lock, flags);
208                 schedule();
209                 spin_lock_irqsave(&self->lock, flags);
210         }
211         set_current_state(TASK_RUNNING);
212         self->claimed = 1;
213         spin_unlock_irqrestore(&self->lock, flags);
214
215         return;
216 }
217
218 static void cw1200_spi_unlock(struct hwbus_priv *self)
219 {
220         unsigned long flags;
221
222         spin_lock_irqsave(&self->lock, flags);
223         self->claimed = 0;
224         spin_unlock_irqrestore(&self->lock, flags);
225         return;
226 }
227
228 static irqreturn_t cw1200_spi_irq_handler(int irq, void *dev_id)
229 {
230         struct hwbus_priv *self = dev_id;
231
232         if (self->core) {
233                 cw1200_irq_handler(self->core);
234                 return IRQ_HANDLED;
235         } else {
236                 return IRQ_NONE;
237         }
238 }
239
240 static int cw1200_spi_irq_subscribe(struct hwbus_priv *self)
241 {
242         int ret;
243
244         pr_debug("SW IRQ subscribe\n");
245
246         ret = request_any_context_irq(self->func->irq, cw1200_spi_irq_handler,
247                                       IRQF_TRIGGER_HIGH,
248                                       "cw1200_wlan_irq", self);
249         if (WARN_ON(ret < 0))
250                 goto exit;
251
252         ret = enable_irq_wake(self->func->irq);
253         if (WARN_ON(ret))
254                 goto free_irq;
255
256         return 0;
257
258 free_irq:
259         free_irq(self->func->irq, self);
260 exit:
261         return ret;
262 }
263
264 static int cw1200_spi_irq_unsubscribe(struct hwbus_priv *self)
265 {
266         int ret = 0;
267
268         pr_debug("SW IRQ unsubscribe\n");
269         disable_irq_wake(self->func->irq);
270         free_irq(self->func->irq, self);
271
272         return ret;
273 }
274
275 static int cw1200_spi_off(const struct cw1200_platform_data_spi *pdata)
276 {
277         if (pdata->reset) {
278                 gpio_set_value(pdata->reset, 0);
279                 msleep(30); /* Min is 2 * CLK32K cycles */
280                 gpio_free(pdata->reset);
281         }
282
283         if (pdata->power_ctrl)
284                 pdata->power_ctrl(pdata, false);
285         if (pdata->clk_ctrl)
286                 pdata->clk_ctrl(pdata, false);
287
288         return 0;
289 }
290
291 static int cw1200_spi_on(const struct cw1200_platform_data_spi *pdata)
292 {
293         /* Ensure I/Os are pulled low */
294         if (pdata->reset) {
295                 gpio_request(pdata->reset, "cw1200_wlan_reset");
296                 gpio_direction_output(pdata->reset, 0);
297         }
298         if (pdata->powerup) {
299                 gpio_request(pdata->powerup, "cw1200_wlan_powerup");
300                 gpio_direction_output(pdata->powerup, 0);
301         }
302         if (pdata->reset || pdata->powerup)
303                 msleep(10); /* Settle time? */
304
305         /* Enable 3v3 and 1v8 to hardware */
306         if (pdata->power_ctrl) {
307                 if (pdata->power_ctrl(pdata, true)) {
308                         pr_err("power_ctrl() failed!\n");
309                         return -1;
310                 }
311         }
312
313         /* Enable CLK32K */
314         if (pdata->clk_ctrl) {
315                 if (pdata->clk_ctrl(pdata, true)) {
316                         pr_err("clk_ctrl() failed!\n");
317                         return -1;
318                 }
319                 msleep(10); /* Delay until clock is stable for 2 cycles */
320         }
321
322         /* Enable POWERUP signal */
323         if (pdata->powerup) {
324                 gpio_set_value(pdata->powerup, 1);
325                 msleep(250); /* or more..? */
326         }
327         /* Enable RSTn signal */
328         if (pdata->reset) {
329                 gpio_set_value(pdata->reset, 1);
330                 msleep(50); /* Or more..? */
331         }
332         return 0;
333 }
334
335 static size_t cw1200_spi_align_size(struct hwbus_priv *self, size_t size)
336 {
337         return size & 1 ? size + 1 : size;
338 }
339
340 static int cw1200_spi_pm(struct hwbus_priv *self, bool suspend)
341 {
342         return irq_set_irq_wake(self->func->irq, suspend);
343 }
344
345 static struct hwbus_ops cw1200_spi_hwbus_ops = {
346         .hwbus_memcpy_fromio    = cw1200_spi_memcpy_fromio,
347         .hwbus_memcpy_toio      = cw1200_spi_memcpy_toio,
348         .lock                   = cw1200_spi_lock,
349         .unlock                 = cw1200_spi_unlock,
350         .align_size             = cw1200_spi_align_size,
351         .power_mgmt             = cw1200_spi_pm,
352 };
353
354 /* Probe Function to be called by SPI stack when device is discovered */
355 static int cw1200_spi_probe(struct spi_device *func)
356 {
357         const struct cw1200_platform_data_spi *plat_data =
358                 func->dev.platform_data;
359         struct hwbus_priv *self;
360         int status;
361
362         /* Sanity check speed */
363         if (func->max_speed_hz > 52000000)
364                 func->max_speed_hz = 52000000;
365         if (func->max_speed_hz < 1000000)
366                 func->max_speed_hz = 1000000;
367
368         /* Fix up transfer size */
369         if (plat_data->spi_bits_per_word)
370                 func->bits_per_word = plat_data->spi_bits_per_word;
371         if (!func->bits_per_word)
372                 func->bits_per_word = 16;
373
374         /* And finally.. */
375         func->mode = SPI_MODE_0;
376
377         pr_info("cw1200_wlan_spi: Probe called (CS %d M %d BPW %d CLK %d)\n",
378                 func->chip_select, func->mode, func->bits_per_word,
379                 func->max_speed_hz);
380
381         if (cw1200_spi_on(plat_data)) {
382                 pr_err("spi_on() failed!\n");
383                 return -1;
384         }
385
386         if (spi_setup(func)) {
387                 pr_err("spi_setup() failed!\n");
388                 return -1;
389         }
390
391         self = kzalloc(sizeof(*self), GFP_KERNEL);
392         if (!self) {
393                 pr_err("Can't allocate SPI hwbus_priv.");
394                 return -ENOMEM;
395         }
396
397         self->pdata = plat_data;
398         self->func = func;
399         spin_lock_init(&self->lock);
400
401         spi_set_drvdata(func, self);
402
403         status = cw1200_spi_irq_subscribe(self);
404
405         status = cw1200_core_probe(&cw1200_spi_hwbus_ops,
406                                    self, &func->dev, &self->core,
407                                    self->pdata->ref_clk,
408                                    self->pdata->macaddr,
409                                    self->pdata->sdd_file,
410                                    self->pdata->have_5ghz);
411
412         if (status) {
413                 cw1200_spi_irq_unsubscribe(self);
414                 cw1200_spi_off(plat_data);
415                 kfree(self);
416         }
417
418         return status;
419 }
420
421 /* Disconnect Function to be called by SPI stack when device is disconnected */
422 static int cw1200_spi_disconnect(struct spi_device *func)
423 {
424         struct hwbus_priv *self = spi_get_drvdata(func);
425
426         if (self) {
427                 cw1200_spi_irq_unsubscribe(self);
428                 if (self->core) {
429                         cw1200_core_release(self->core);
430                         self->core = NULL;
431                 }
432                 kfree(self);
433         }
434         cw1200_spi_off(func->dev.platform_data);
435
436         return 0;
437 }
438
439 #ifdef CONFIG_PM
440 static int cw1200_spi_suspend(struct device *dev, pm_message_t state)
441 {
442         struct hwbus_priv *self = spi_get_drvdata(to_spi_device(dev));
443
444         if (!cw1200_can_suspend(self->core))
445                 return -EAGAIN;
446
447         /* XXX notify host that we have to keep CW1200 powered on? */
448         return 0;
449 }
450
451 static int cw1200_spi_resume(struct device *dev)
452 {
453         return 0;
454 }
455 #endif
456
457 static struct spi_driver spi_driver = {
458         .probe          = cw1200_spi_probe,
459         .remove         = cw1200_spi_disconnect,
460         .driver = {
461                 .name           = "cw1200_wlan_spi",
462                 .bus            = &spi_bus_type,
463                 .owner          = THIS_MODULE,
464 #ifdef CONFIG_PM
465                 .suspend        = cw1200_spi_suspend,
466                 .resume         = cw1200_spi_resume,
467 #endif
468         },
469 };
470
471 module_spi_driver(spi_driver);