1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2008 Thomas Chou <thomas@wytron.com.tw>
7 * Based on spi_s3c24xx.c, which is:
8 * Copyright (c) 2006 Ben Dooks
9 * Copyright (c) 2006 Simtec Electronics
10 * Ben Dooks <ben@simtec.co.uk>
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>
22 #define DRV_NAME "spi_altera"
24 enum altera_spi_type
{
25 ALTERA_SPI_TYPE_UNKNOWN
,
26 ALTERA_SPI_TYPE_SUBDEV
,
29 static const struct regmap_config spi_altera_config
= {
36 static int altera_spi_probe(struct platform_device
*pdev
)
38 const struct platform_device_id
*platid
= platform_get_device_id(pdev
);
39 struct altera_spi_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
40 enum altera_spi_type type
= ALTERA_SPI_TYPE_UNKNOWN
;
41 struct altera_spi
*hw
;
42 struct spi_controller
*host
;
46 host
= spi_alloc_host(&pdev
->dev
, sizeof(struct altera_spi
));
50 /* setup the host state. */
54 if (pdata
->num_chipselect
> ALTERA_SPI_MAX_CS
) {
56 "Invalid number of chipselect: %u\n",
57 pdata
->num_chipselect
);
62 host
->num_chipselect
= pdata
->num_chipselect
;
63 host
->mode_bits
= pdata
->mode_bits
;
64 host
->bits_per_word_mask
= pdata
->bits_per_word_mask
;
66 host
->num_chipselect
= 16;
67 host
->mode_bits
= SPI_CS_HIGH
;
68 host
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(1, 16);
71 host
->dev
.of_node
= pdev
->dev
.of_node
;
73 hw
= spi_controller_get_devdata(host
);
77 type
= platid
->driver_data
;
79 /* find and map our resources */
80 if (type
== ALTERA_SPI_TYPE_SUBDEV
) {
81 struct resource
*regoff
;
83 hw
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
85 dev_err(&pdev
->dev
, "get regmap failed\n");
89 regoff
= platform_get_resource(pdev
, IORESOURCE_REG
, 0);
91 hw
->regoff
= regoff
->start
;
95 res
= devm_platform_ioremap_resource(pdev
, 0);
101 hw
->regmap
= devm_regmap_init_mmio(&pdev
->dev
, res
,
103 if (IS_ERR(hw
->regmap
)) {
104 dev_err(&pdev
->dev
, "regmap mmio init failed\n");
105 err
= PTR_ERR(hw
->regmap
);
110 altera_spi_init_host(host
);
112 /* irq is optional */
113 hw
->irq
= platform_get_irq(pdev
, 0);
115 err
= devm_request_irq(&pdev
->dev
, hw
->irq
, altera_spi_irq
, 0,
121 err
= devm_spi_register_controller(&pdev
->dev
, host
);
126 for (i
= 0; i
< pdata
->num_devices
; i
++) {
127 if (!spi_new_device(host
, pdata
->devices
+ i
))
129 "unable to create SPI device: %s\n",
130 pdata
->devices
[i
].modalias
);
134 dev_info(&pdev
->dev
, "regoff %u, irq %d\n", hw
->regoff
, hw
->irq
);
138 spi_controller_put(host
);
143 static const struct of_device_id altera_spi_match
[] = {
144 { .compatible
= "ALTR,spi-1.0", },
145 { .compatible
= "altr,spi-1.0", },
148 MODULE_DEVICE_TABLE(of
, altera_spi_match
);
149 #endif /* CONFIG_OF */
151 static const struct platform_device_id altera_spi_ids
[] = {
152 { DRV_NAME
, ALTERA_SPI_TYPE_UNKNOWN
},
153 { "subdev_spi_altera", ALTERA_SPI_TYPE_SUBDEV
},
156 MODULE_DEVICE_TABLE(platform
, altera_spi_ids
);
158 static struct platform_driver altera_spi_driver
= {
159 .probe
= altera_spi_probe
,
163 .of_match_table
= of_match_ptr(altera_spi_match
),
165 .id_table
= altera_spi_ids
,
167 module_platform_driver(altera_spi_driver
);
169 MODULE_DESCRIPTION("Altera SPI driver");
170 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
171 MODULE_LICENSE("GPL");