1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of wl1271
5 * Copyright (C) 2009-2010 Nokia Corporation
7 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
10 #include <linux/irq.h>
11 #include <linux/module.h>
12 #include <linux/vmalloc.h>
13 #include <linux/platform_device.h>
14 #include <linux/mmc/sdio.h>
15 #include <linux/mmc/sdio_func.h>
16 #include <linux/mmc/sdio_ids.h>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/host.h>
19 #include <linux/gpio.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/printk.h>
23 #include <linux/of_irq.h>
26 #include "wl12xx_80211.h"
29 #ifndef SDIO_VENDOR_ID_TI
30 #define SDIO_VENDOR_ID_TI 0x0097
33 #ifndef SDIO_DEVICE_ID_TI_WL1271
34 #define SDIO_DEVICE_ID_TI_WL1271 0x4076
37 static bool dump = false;
39 struct wl12xx_sdio_glue {
41 struct platform_device *core;
44 static const struct sdio_device_id wl1271_devices[] = {
45 { SDIO_DEVICE(SDIO_VENDOR_ID_TI, SDIO_DEVICE_ID_TI_WL1271) },
48 MODULE_DEVICE_TABLE(sdio, wl1271_devices);
50 static void wl1271_sdio_set_block_size(struct device *child,
53 struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
54 struct sdio_func *func = dev_to_sdio_func(glue->dev);
56 sdio_claim_host(func);
57 sdio_set_block_size(func, blksz);
58 sdio_release_host(func);
61 static int __must_check wl12xx_sdio_raw_read(struct device *child, int addr,
62 void *buf, size_t len, bool fixed)
65 struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
66 struct sdio_func *func = dev_to_sdio_func(glue->dev);
68 sdio_claim_host(func);
70 if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG)) {
71 ((u8 *)buf)[0] = sdio_f0_readb(func, addr, &ret);
72 dev_dbg(child->parent, "sdio read 52 addr 0x%x, byte 0x%02x\n",
73 addr, ((u8 *)buf)[0]);
76 ret = sdio_readsb(func, buf, addr, len);
78 ret = sdio_memcpy_fromio(func, buf, addr, len);
80 dev_dbg(child->parent, "sdio read 53 addr 0x%x, %zu bytes\n",
84 sdio_release_host(func);
87 dev_err(child->parent, "sdio read failed (%d)\n", ret);
90 printk(KERN_DEBUG "wlcore_sdio: READ from 0x%04x\n", addr);
91 print_hex_dump(KERN_DEBUG, "wlcore_sdio: READ ",
92 DUMP_PREFIX_OFFSET, 16, 1,
99 static int __must_check wl12xx_sdio_raw_write(struct device *child, int addr,
100 void *buf, size_t len, bool fixed)
103 struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
104 struct sdio_func *func = dev_to_sdio_func(glue->dev);
106 sdio_claim_host(func);
108 if (unlikely(dump)) {
109 printk(KERN_DEBUG "wlcore_sdio: WRITE to 0x%04x\n", addr);
110 print_hex_dump(KERN_DEBUG, "wlcore_sdio: WRITE ",
111 DUMP_PREFIX_OFFSET, 16, 1,
115 if (unlikely(addr == HW_ACCESS_ELP_CTRL_REG)) {
116 sdio_f0_writeb(func, ((u8 *)buf)[0], addr, &ret);
117 dev_dbg(child->parent, "sdio write 52 addr 0x%x, byte 0x%02x\n",
118 addr, ((u8 *)buf)[0]);
120 dev_dbg(child->parent, "sdio write 53 addr 0x%x, %zu bytes\n",
124 ret = sdio_writesb(func, addr, buf, len);
126 ret = sdio_memcpy_toio(func, addr, buf, len);
129 sdio_release_host(func);
132 dev_err(child->parent, "sdio write failed (%d)\n", ret);
137 static int wl12xx_sdio_power_on(struct wl12xx_sdio_glue *glue)
140 struct sdio_func *func = dev_to_sdio_func(glue->dev);
141 struct mmc_card *card = func->card;
143 ret = pm_runtime_get_sync(&card->dev);
145 pm_runtime_put_noidle(&card->dev);
146 dev_err(glue->dev, "%s: failed to get_sync(%d)\n",
152 sdio_claim_host(func);
154 * To guarantee that the SDIO card is power cycled, as required to make
155 * the FW programming to succeed, let's do a brute force HW reset.
157 mmc_hw_reset(card->host);
159 sdio_enable_func(func);
160 sdio_release_host(func);
165 static int wl12xx_sdio_power_off(struct wl12xx_sdio_glue *glue)
167 struct sdio_func *func = dev_to_sdio_func(glue->dev);
168 struct mmc_card *card = func->card;
170 sdio_claim_host(func);
171 sdio_disable_func(func);
172 sdio_release_host(func);
174 /* Let runtime PM know the card is powered off */
175 pm_runtime_put(&card->dev);
179 static int wl12xx_sdio_set_power(struct device *child, bool enable)
181 struct wl12xx_sdio_glue *glue = dev_get_drvdata(child->parent);
184 return wl12xx_sdio_power_on(glue);
186 return wl12xx_sdio_power_off(glue);
189 static struct wl1271_if_operations sdio_ops = {
190 .read = wl12xx_sdio_raw_read,
191 .write = wl12xx_sdio_raw_write,
192 .power = wl12xx_sdio_set_power,
193 .set_block_size = wl1271_sdio_set_block_size,
198 static const struct wilink_family_data wl127x_data = {
200 .nvs_name = "/*(DEBLOBBED)*/",
203 static const struct wilink_family_data wl128x_data = {
205 .nvs_name = "/*(DEBLOBBED)*/",
208 static const struct wilink_family_data wl18xx_data = {
210 .cfg_name = "/*(DEBLOBBED)*/",
211 .nvs_name = "/*(DEBLOBBED)*/",
214 static const struct of_device_id wlcore_sdio_of_match_table[] = {
215 { .compatible = "ti,wl1271", .data = &wl127x_data },
216 { .compatible = "ti,wl1273", .data = &wl127x_data },
217 { .compatible = "ti,wl1281", .data = &wl128x_data },
218 { .compatible = "ti,wl1283", .data = &wl128x_data },
219 { .compatible = "ti,wl1285", .data = &wl128x_data },
220 { .compatible = "ti,wl1801", .data = &wl18xx_data },
221 { .compatible = "ti,wl1805", .data = &wl18xx_data },
222 { .compatible = "ti,wl1807", .data = &wl18xx_data },
223 { .compatible = "ti,wl1831", .data = &wl18xx_data },
224 { .compatible = "ti,wl1835", .data = &wl18xx_data },
225 { .compatible = "ti,wl1837", .data = &wl18xx_data },
229 static int wlcore_probe_of(struct device *dev, int *irq, int *wakeirq,
230 struct wlcore_platdev_data *pdev_data)
232 struct device_node *np = dev->of_node;
233 const struct of_device_id *of_id;
235 of_id = of_match_node(wlcore_sdio_of_match_table, np);
239 pdev_data->family = of_id->data;
241 *irq = irq_of_parse_and_map(np, 0);
243 dev_err(dev, "No irq in platform data\n");
247 *wakeirq = irq_of_parse_and_map(np, 1);
249 /* optional clock frequency params */
250 of_property_read_u32(np, "ref-clock-frequency",
251 &pdev_data->ref_clock_freq);
252 of_property_read_u32(np, "tcxo-clock-frequency",
253 &pdev_data->tcxo_clock_freq);
258 static int wlcore_probe_of(struct device *dev, int *irq, int *wakeirq,
259 struct wlcore_platdev_data *pdev_data)
265 static int wl1271_probe(struct sdio_func *func,
266 const struct sdio_device_id *id)
268 struct wlcore_platdev_data *pdev_data;
269 struct wl12xx_sdio_glue *glue;
270 struct resource res[2];
271 mmc_pm_flag_t mmcflags;
273 int irq, wakeirq, num_irqs;
274 const char *chip_family;
276 /* We are only able to handle the wlan function */
277 if (func->num != 0x02)
280 pdev_data = devm_kzalloc(&func->dev, sizeof(*pdev_data), GFP_KERNEL);
284 pdev_data->if_ops = &sdio_ops;
286 glue = devm_kzalloc(&func->dev, sizeof(*glue), GFP_KERNEL);
290 glue->dev = &func->dev;
292 /* Grab access to FN0 for ELP reg. */
293 func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
295 /* Use block mode for transferring over one block size of data */
296 func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
298 ret = wlcore_probe_of(&func->dev, &irq, &wakeirq, pdev_data);
302 /* if sdio can keep power while host is suspended, enable wow */
303 mmcflags = sdio_get_host_pm_caps(func);
304 dev_dbg(glue->dev, "sdio PM caps = 0x%x\n", mmcflags);
306 if (mmcflags & MMC_PM_KEEP_POWER)
307 pdev_data->pwr_in_suspend = true;
309 sdio_set_drvdata(func, glue);
311 /* Tell PM core that we don't need the card to be powered now */
312 pm_runtime_put_noidle(&func->dev);
315 * Due to a hardware bug, we can't differentiate wl18xx from
316 * wl12xx, because both report the same device ID. The only
317 * way to differentiate is by checking the SDIO revision,
318 * which is 3.00 on the wl18xx chips.
320 if (func->card->cccr.sdio_vsn == SDIO_SDIO_REV_3_00)
321 chip_family = "wl18xx";
323 chip_family = "wl12xx";
325 glue->core = platform_device_alloc(chip_family, PLATFORM_DEVID_AUTO);
327 dev_err(glue->dev, "can't allocate platform_device");
332 glue->core->dev.parent = &func->dev;
334 memset(res, 0x00, sizeof(res));
337 res[0].flags = IORESOURCE_IRQ |
338 irqd_get_trigger_type(irq_get_irq_data(irq));
343 res[1].start = wakeirq;
344 res[1].flags = IORESOURCE_IRQ |
345 irqd_get_trigger_type(irq_get_irq_data(wakeirq));
346 res[1].name = "wakeirq";
351 ret = platform_device_add_resources(glue->core, res, num_irqs);
353 dev_err(glue->dev, "can't add resources\n");
357 ret = platform_device_add_data(glue->core, pdev_data,
360 dev_err(glue->dev, "can't add platform data\n");
364 ret = platform_device_add(glue->core);
366 dev_err(glue->dev, "can't add platform device\n");
372 platform_device_put(glue->core);
378 static void wl1271_remove(struct sdio_func *func)
380 struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
382 /* Undo decrement done above in wl1271_probe */
383 pm_runtime_get_noresume(&func->dev);
385 platform_device_unregister(glue->core);
389 static int wl1271_suspend(struct device *dev)
391 /* Tell MMC/SDIO core it's OK to power down the card
392 * (if it isn't already), but not to remove it completely */
393 struct sdio_func *func = dev_to_sdio_func(dev);
394 struct wl12xx_sdio_glue *glue = sdio_get_drvdata(func);
395 struct wl1271 *wl = platform_get_drvdata(glue->core);
396 mmc_pm_flag_t sdio_flags;
400 dev_err(dev, "no wilink module was probed\n");
404 dev_dbg(dev, "wl1271 suspend. wow_enabled: %d\n",
407 /* check whether sdio should keep power */
408 if (wl->wow_enabled) {
409 sdio_flags = sdio_get_host_pm_caps(func);
411 if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
412 dev_err(dev, "can't keep power while host "
418 /* keep power while host suspended */
419 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
421 dev_err(dev, "error while trying to keep power\n");
429 static int wl1271_resume(struct device *dev)
431 dev_dbg(dev, "wl1271 resume\n");
436 static const struct dev_pm_ops wl1271_sdio_pm_ops = {
437 .suspend = wl1271_suspend,
438 .resume = wl1271_resume,
442 static struct sdio_driver wl1271_sdio_driver = {
443 .name = "wl1271_sdio",
444 .id_table = wl1271_devices,
445 .probe = wl1271_probe,
446 .remove = wl1271_remove,
449 .pm = &wl1271_sdio_pm_ops,
454 static int __init wl1271_init(void)
456 return sdio_register_driver(&wl1271_sdio_driver);
459 static void __exit wl1271_exit(void)
461 sdio_unregister_driver(&wl1271_sdio_driver);
464 module_init(wl1271_init);
465 module_exit(wl1271_exit);
467 module_param(dump, bool, 0600);
468 MODULE_PARM_DESC(dump, "Enable sdio read/write dumps.");
470 MODULE_LICENSE("GPL");
471 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
472 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");