| 1 | |
| 2 | |
| 3 | * Altera SPI driver
|
| 4 | *
|
| 5 | * Copyright (C) 2008 Thomas Chou <[email protected]>
|
| 6 | *
|
| 7 | * Based on spi_s3c24xx.c, which is:
|
| 8 | * Copyright (c) 2006 Ben Dooks
|
| 9 | * Copyright (c) 2006 Simtec Electronics
|
| 10 | * Ben Dooks <[email protected]>
|
| 11 |
|
| 12 |
|
| 13 | #include <linux/interrupt.h>
|
| 14 | #include <linux/errno.h>
|
| 15 | #include <linux/module.h>
|
| 16 | #include <linux/platform_device.h>
|
| 17 | #include <linux/spi/altera.h>
|
| 18 | #include <linux/spi/spi.h>
|
| 19 | #include <linux/io.h>
|
| 20 | #include <linux/of.h>
|
| 21 |
|
| 22 | #define DRV_NAME "spi_altera"
|
| 23 |
|
| 24 | enum altera_spi_type {
|
| 25 | ALTERA_SPI_TYPE_UNKNOWN,
|
| 26 | ALTERA_SPI_TYPE_SUBDEV,
|
| 27 | };
|
| 28 |
|
| 29 | static const struct regmap_config spi_altera_config = {
|
| 30 | .reg_bits = 32,
|
| 31 | .reg_stride = 4,
|
| 32 | .val_bits = 32,
|
| 33 | };
|
| 34 |
|
| 35 | static int altera_spi_probe(struct platform_device *pdev)
|
| 36 | {
|
| 37 | const struct platform_device_id *platid = platform_get_device_id(pdev);
|
| 38 | struct altera_spi_platform_data *pdata = dev_get_platdata(&pdev->dev);
|
| 39 | enum altera_spi_type type = ALTERA_SPI_TYPE_UNKNOWN;
|
| 40 | struct altera_spi *hw;
|
| 41 | struct spi_controller *host;
|
| 42 | int err;
|
| 43 | u16 i;
|
| 44 |
|
| 45 | host = devm_spi_alloc_host(&pdev->dev, sizeof(struct altera_spi));
|
| 46 | if (!host)
|
| 47 | return -ENOMEM;
|
| 48 |
|
| 49 |
|
| 50 | host->bus_num = -1;
|
| 51 |
|
| 52 | if (pdata) {
|
| 53 | if (pdata->num_chipselect > ALTERA_SPI_MAX_CS) {
|
| 54 | dev_err(&pdev->dev,
|
| 55 | "Invalid number of chipselect: %u\n",
|
| 56 | pdata->num_chipselect);
|
| 57 | return -EINVAL;
|
| 58 | }
|
| 59 |
|
| 60 | host->num_chipselect = pdata->num_chipselect;
|
| 61 | host->mode_bits = pdata->mode_bits;
|
| 62 | host->bits_per_word_mask = pdata->bits_per_word_mask;
|
| 63 | } else {
|
| 64 | host->num_chipselect = 16;
|
| 65 | host->mode_bits = SPI_CS_HIGH;
|
| 66 | host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 16);
|
| 67 | }
|
| 68 |
|
| 69 | hw = spi_controller_get_devdata(host);
|
| 70 | hw->dev = &pdev->dev;
|
| 71 |
|
| 72 | if (platid)
|
| 73 | type = platid->driver_data;
|
| 74 |
|
| 75 |
|
| 76 | if (type == ALTERA_SPI_TYPE_SUBDEV) {
|
| 77 | struct resource *regoff;
|
| 78 |
|
| 79 | hw->regmap = dev_get_regmap(pdev->dev.parent, NULL);
|
| 80 | if (!hw->regmap)
|
| 81 | return dev_err_probe(&pdev->dev, -ENODEV, "get regmap failed\n");
|
| 82 |
|
| 83 | regoff = platform_get_resource(pdev, IORESOURCE_REG, 0);
|
| 84 | if (regoff)
|
| 85 | hw->regoff = regoff->start;
|
| 86 | } else {
|
| 87 | void __iomem *res;
|
| 88 |
|
| 89 | res = devm_platform_ioremap_resource(pdev, 0);
|
| 90 | if (IS_ERR(res))
|
| 91 | return PTR_ERR(res);
|
| 92 |
|
| 93 | hw->regmap = devm_regmap_init_mmio(&pdev->dev, res,
|
| 94 | &spi_altera_config);
|
| 95 | if (IS_ERR(hw->regmap))
|
| 96 | return dev_err_probe(&pdev->dev, PTR_ERR(hw->regmap),
|
| 97 | "regmap mmio init failed\n");
|
| 98 | }
|
| 99 |
|
| 100 | altera_spi_init_host(host);
|
| 101 |
|
| 102 |
|
| 103 | hw->irq = platform_get_irq(pdev, 0);
|
| 104 | if (hw->irq >= 0) {
|
| 105 | err = devm_request_irq(&pdev->dev, hw->irq, altera_spi_irq, 0,
|
| 106 | pdev->name, host);
|
| 107 | if (err)
|
| 108 | return err;
|
| 109 | }
|
| 110 |
|
| 111 | err = devm_spi_register_controller(&pdev->dev, host);
|
| 112 | if (err)
|
| 113 | return err;
|
| 114 |
|
| 115 | if (pdata) {
|
| 116 | for (i = 0; i < pdata->num_devices; i++) {
|
| 117 | if (!spi_new_device(host, pdata->devices + i))
|
| 118 | dev_warn(&pdev->dev,
|
| 119 | "unable to create SPI device: %s\n",
|
| 120 | pdata->devices[i].modalias);
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | dev_info(&pdev->dev, "regoff %u, irq %d\n", hw->regoff, hw->irq);
|
| 125 |
|
| 126 | return 0;
|
| 127 | }
|
| 128 |
|
| 129 | #ifdef CONFIG_OF
|
| 130 | static const struct of_device_id altera_spi_match[] = {
|
| 131 | { .compatible = "ALTR,spi-1.0", },
|
| 132 | { .compatible = "altr,spi-1.0", },
|
| 133 | {},
|
| 134 | };
|
| 135 | MODULE_DEVICE_TABLE(of, altera_spi_match);
|
| 136 | #endif
|
| 137 |
|
| 138 | static const struct platform_device_id altera_spi_ids[] = {
|
| 139 | { .name = DRV_NAME, .driver_data = ALTERA_SPI_TYPE_UNKNOWN },
|
| 140 | { .name = "subdev_spi_altera", .driver_data = ALTERA_SPI_TYPE_SUBDEV },
|
| 141 | { }
|
| 142 | };
|
| 143 | MODULE_DEVICE_TABLE(platform, altera_spi_ids);
|
| 144 |
|
| 145 | static struct platform_driver altera_spi_driver = {
|
| 146 | .probe = altera_spi_probe,
|
| 147 | .driver = {
|
| 148 | .name = DRV_NAME,
|
| 149 | .pm = NULL,
|
| 150 | .of_match_table = of_match_ptr(altera_spi_match),
|
| 151 | },
|
| 152 | .id_table = altera_spi_ids,
|
| 153 | };
|
| 154 | module_platform_driver(altera_spi_driver);
|
| 155 |
|
| 156 | MODULE_DESCRIPTION("Altera SPI driver");
|
| 157 | MODULE_AUTHOR("Thomas Chou <[email protected]>");
|
| 158 | MODULE_LICENSE("GPL");
|