xref: /linux/drivers/spi/spi-altera-platform.c (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Altera SPI driver
4  *
5  * Copyright (C) 2008 Thomas Chou <thomas@wytron.com.tw>
6  *
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>
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 = -ENODEV;
43 	u16 i;
44 
45 	host = spi_alloc_host(&pdev->dev, sizeof(struct altera_spi));
46 	if (!host)
47 		return err;
48 
49 	/* setup the host state. */
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 			err = -EINVAL;
58 			goto exit;
59 		}
60 
61 		host->num_chipselect = pdata->num_chipselect;
62 		host->mode_bits = pdata->mode_bits;
63 		host->bits_per_word_mask = pdata->bits_per_word_mask;
64 	} else {
65 		host->num_chipselect = 16;
66 		host->mode_bits = SPI_CS_HIGH;
67 		host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 16);
68 	}
69 
70 	host->dev.of_node = pdev->dev.of_node;
71 
72 	hw = spi_controller_get_devdata(host);
73 	hw->dev = &pdev->dev;
74 
75 	if (platid)
76 		type = platid->driver_data;
77 
78 	/* find and map our resources */
79 	if (type == ALTERA_SPI_TYPE_SUBDEV) {
80 		struct resource *regoff;
81 
82 		hw->regmap = dev_get_regmap(pdev->dev.parent, NULL);
83 		if (!hw->regmap) {
84 			dev_err(&pdev->dev, "get regmap failed\n");
85 			goto exit;
86 		}
87 
88 		regoff = platform_get_resource(pdev, IORESOURCE_REG, 0);
89 		if (regoff)
90 			hw->regoff = regoff->start;
91 	} else {
92 		void __iomem *res;
93 
94 		res = devm_platform_ioremap_resource(pdev, 0);
95 		if (IS_ERR(res)) {
96 			err = PTR_ERR(res);
97 			goto exit;
98 		}
99 
100 		hw->regmap = devm_regmap_init_mmio(&pdev->dev, res,
101 						   &spi_altera_config);
102 		if (IS_ERR(hw->regmap)) {
103 			dev_err(&pdev->dev, "regmap mmio init failed\n");
104 			err = PTR_ERR(hw->regmap);
105 			goto exit;
106 		}
107 	}
108 
109 	altera_spi_init_host(host);
110 
111 	/* irq is optional */
112 	hw->irq = platform_get_irq(pdev, 0);
113 	if (hw->irq >= 0) {
114 		err = devm_request_irq(&pdev->dev, hw->irq, altera_spi_irq, 0,
115 				       pdev->name, host);
116 		if (err)
117 			goto exit;
118 	}
119 
120 	err = devm_spi_register_controller(&pdev->dev, host);
121 	if (err)
122 		goto exit;
123 
124 	if (pdata) {
125 		for (i = 0; i < pdata->num_devices; i++) {
126 			if (!spi_new_device(host, pdata->devices + i))
127 				dev_warn(&pdev->dev,
128 					 "unable to create SPI device: %s\n",
129 					 pdata->devices[i].modalias);
130 		}
131 	}
132 
133 	dev_info(&pdev->dev, "regoff %u, irq %d\n", hw->regoff, hw->irq);
134 
135 	return 0;
136 exit:
137 	spi_controller_put(host);
138 	return err;
139 }
140 
141 #ifdef CONFIG_OF
142 static const struct of_device_id altera_spi_match[] = {
143 	{ .compatible = "ALTR,spi-1.0", },
144 	{ .compatible = "altr,spi-1.0", },
145 	{},
146 };
147 MODULE_DEVICE_TABLE(of, altera_spi_match);
148 #endif /* CONFIG_OF */
149 
150 static const struct platform_device_id altera_spi_ids[] = {
151 	{ DRV_NAME,		ALTERA_SPI_TYPE_UNKNOWN },
152 	{ "subdev_spi_altera",	ALTERA_SPI_TYPE_SUBDEV },
153 	{ }
154 };
155 MODULE_DEVICE_TABLE(platform, altera_spi_ids);
156 
157 static struct platform_driver altera_spi_driver = {
158 	.probe = altera_spi_probe,
159 	.driver = {
160 		.name = DRV_NAME,
161 		.pm = NULL,
162 		.of_match_table = of_match_ptr(altera_spi_match),
163 	},
164 	.id_table	= altera_spi_ids,
165 };
166 module_platform_driver(altera_spi_driver);
167 
168 MODULE_DESCRIPTION("Altera SPI driver");
169 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
170 MODULE_LICENSE("GPL");
171