2 * Microcontroller Message Bus
4 * Copyright (c) 2009 Michael Buesch <mb@bu3sch.de>
6 * Licensed under the GNU/GPL. See COPYING for details.
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/miscdevice.h>
15 #include <linux/spi/spi.h>
16 #include <linux/spi/spi_gpio.h>
17 #include <linux/spi/spi_bitbang.h>
18 #include <linux/gfp.h>
24 MODULE_LICENSE("GPL");
25 MODULE_DESCRIPTION("Microcontroller Message Bus");
26 MODULE_AUTHOR("Michael Buesch");
30 /* Misc character device driver */
31 struct miscdevice mdev;
32 struct file_operations mdev_fops;
35 struct spi_device *sdev;
38 struct spi_gpio_platform_data spi_gpio_pdata;
39 struct platform_device spi_gpio_pdev;
42 static int ucmb_spi_busnum_count = 1337;
45 static struct ucmb_platform_data ucmb_list[] = {
46 { //FIXME don't define it here.
55 static int __devinit ucmb_spi_probe(struct spi_device *sdev)
60 static int __devexit ucmb_spi_remove(struct spi_device *sdev)
65 static struct spi_driver ucmb_spi_driver = {
71 .probe = ucmb_spi_probe,
72 .remove = __devexit_p(ucmb_spi_remove),
75 static inline struct ucmb * filp_to_ucmb(struct file *filp)
77 return container_of(filp->f_op, struct ucmb, mdev_fops);
81 static ssize_t ucmb_read(struct file *filp, char __user *user_buf,
82 size_t size, loff_t *offp)
84 struct ucmb *ucmb = filp_to_ucmb(filp);
93 buf = (char *)__get_free_page(GFP_KERNEL);
97 err = spi_read(ucmb->sdev, buf, size);
102 if (copy_to_user(user_buf, buf, size))
108 __free_page((unsigned long)buf);
110 return err ? err : size;
114 static ssize_t ucmb_write(struct file *filp, const char __user *user_buf,
115 size_t size, loff_t *offp)
117 struct ucmb *ucmb = filp_to_ucmb(filp);
122 buf = (char *)__get_free_page(GFP_KERNEL);
126 size = min(PAGE_SIZE, size);
128 if (copy_from_user(buf, user_buf, size))
131 err = spi_write(ucmb->sdev, buf, size);
134 __free_page((unsigned long)buf);
136 return err ? err : size;
139 static int __devinit ucmb_probe(struct platform_device *pdev)
141 struct ucmb_platform_data *pdata;
144 const int bus_num = ucmb_spi_busnum_count++;
145 struct spi_bitbang *bb;
147 pdata = pdev->dev.platform_data;
151 ucmb = kzalloc(sizeof(struct ucmb), GFP_KERNEL);
155 /* Create the SPI GPIO bus master. */
157 #ifdef CONFIG_SPI_GPIO_MODULE
158 err = request_module("spi_gpio");
160 printk(KERN_WARNING PFX "Failed to request spi_gpio module\n");
161 #endif /* CONFIG_SPI_GPIO_MODULE */
163 ucmb->spi_gpio_pdata.sck = pdata->gpio_sck;
164 ucmb->spi_gpio_pdata.mosi = pdata->gpio_mosi;
165 ucmb->spi_gpio_pdata.miso = pdata->gpio_miso;
166 ucmb->spi_gpio_pdata.num_chipselect = 1;
168 ucmb->spi_gpio_pdev.name = "spi_gpio";
169 ucmb->spi_gpio_pdev.id = bus_num;
170 ucmb->spi_gpio_pdev.dev.platform_data = &ucmb->spi_gpio_pdata;
172 err = platform_device_register(&ucmb->spi_gpio_pdev);
174 printk(KERN_ERR PFX "Failed to register SPI-GPIO platform device\n");
177 bb = platform_get_drvdata(&ucmb->spi_gpio_pdev);
178 if (!bb || !bb->master) {
179 printk(KERN_ERR PFX "No bitbanged master device found.\n");
180 goto err_unreg_spi_gpio_pdev;
183 /* Create the SPI device. */
185 ucmb->sdev = spi_alloc_device(bb->master);
187 printk(KERN_ERR PFX "Failed to allocate SPI device\n");
188 goto err_unreg_spi_gpio_pdev;
190 ucmb->sdev->max_speed_hz = 5000000;
191 ucmb->sdev->chip_select = 0;
192 ucmb->sdev->mode = SPI_MODE_0;
193 strlcpy(ucmb->sdev->modalias, "ucmb", /* We are the SPI driver. */
194 sizeof(ucmb->sdev->modalias));
195 ucmb->sdev->controller_data = (void *)SPI_GPIO_NO_CHIPSELECT;
196 err = spi_add_device(ucmb->sdev);
198 printk(KERN_ERR PFX "Failed to add SPI device\n");
199 goto err_free_spi_device;
202 /* Create the Misc char device. */
204 ucmb->mdev.minor = MISC_DYNAMIC_MINOR;
205 ucmb->mdev.name = pdata->name;
206 ucmb->mdev.parent = &pdev->dev;
207 ucmb->mdev_fops.read = ucmb_read;
208 ucmb->mdev_fops.write = ucmb_write;
209 ucmb->mdev.fops = &ucmb->mdev_fops;
211 err = misc_register(&ucmb->mdev);
213 printk(KERN_ERR PFX "Failed to register miscdev %s\n",
215 goto err_unreg_spi_device;
218 platform_set_drvdata(pdev, ucmb);
220 printk(KERN_INFO PFX "Registered message bus \"%s\"\n", pdata->name);
224 err_unreg_spi_device:
225 spi_unregister_device(ucmb->sdev);
227 spi_dev_put(ucmb->sdev);
228 err_unreg_spi_gpio_pdev:
229 platform_device_unregister(&ucmb->spi_gpio_pdev);
236 static int __devexit ucmb_remove(struct platform_device *pdev)
238 struct ucmb *ucmb = platform_get_drvdata(pdev);
241 err = misc_deregister(&ucmb->mdev);
243 printk(KERN_ERR PFX "Failed to unregister miscdev %s\n",
246 spi_unregister_device(ucmb->sdev);
247 spi_dev_put(ucmb->sdev);
248 platform_device_unregister(&ucmb->spi_gpio_pdev);
251 platform_set_drvdata(pdev, NULL);
256 static struct platform_driver ucmb_driver = {
259 .owner = THIS_MODULE,
262 .remove = __devexit_p(ucmb_probe),
265 static int ucmb_modinit(void)
267 struct ucmb_platform_data *pdata;
268 struct platform_device *pdev;
271 printk(KERN_INFO "Microcontroller message bus driver\n");
273 err = platform_driver_register(&ucmb_driver);
275 printk(KERN_ERR PFX "Failed to register platform driver\n");
278 err = spi_register_driver(&ucmb_spi_driver);
280 printk(KERN_ERR PFX "Failed to register SPI driver\n");
281 platform_driver_unregister(&ucmb_driver);
285 for (i = 0; i < ARRAY_SIZE(ucmb_list); i++) {
286 pdata = &ucmb_list[i];
288 pdev = platform_device_alloc("ucmb", i);
290 printk(KERN_ERR PFX "Failed to allocate platform device.\n");
293 err = platform_device_add_data(pdev, pdata, sizeof(*pdata));
295 printk(KERN_ERR PFX "Failed to add platform data.\n");
296 platform_device_put(pdev);
299 err = platform_device_add(pdev);
301 printk(KERN_ERR PFX "Failed to register platform device.\n");
302 platform_device_put(pdev);
310 module_init(ucmb_modinit);
312 static void ucmb_modexit(void)
314 struct ucmb_platform_data *pdata;
317 for (i = 0; i < ARRAY_SIZE(ucmb_list); i++) {
318 pdata = &ucmb_list[i];
321 platform_device_unregister(pdata->pdev);
322 platform_device_put(pdata->pdev);
325 spi_unregister_driver(&ucmb_spi_driver);
326 platform_driver_unregister(&ucmb_driver);
328 module_exit(ucmb_modexit);