ucmb: Add support for a reset line to the microcontroller.
[openwrt/svn-archive/archive.git] / utils / ucmb / driver / ucmb.c
1 /*
2 * Microcontroller Message Bus
3 *
4 * Copyright (c) 2009 Michael Buesch <mb@bu3sch.de>
5 *
6 * Licensed under the GNU/GPL. See COPYING for details.
7 */
8
9 #include "ucmb.h"
10
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/miscdevice.h>
14 #include <linux/fs.h>
15 #include <linux/spi/spi.h>
16 #include <linux/spi/spi_gpio.h>
17 #include <linux/spi/spi_bitbang.h>
18 #include <linux/gpio.h>
19 #include <linux/gfp.h>
20 #include <linux/delay.h>
21 #include <linux/crc16.h>
22
23
24 #define PFX "ucmb: "
25
26 #undef DEBUG
27
28
29 MODULE_LICENSE("GPL");
30 MODULE_DESCRIPTION("Microcontroller Message Bus");
31 MODULE_AUTHOR("Michael Buesch");
32
33
34 struct ucmb {
35 struct mutex mutex;
36
37 unsigned int msg_delay_ms;
38 unsigned int gpio_reset;
39 bool reset_activelow;
40
41 /* Misc character device driver */
42 struct miscdevice mdev;
43 struct file_operations mdev_fops;
44
45 /* SPI driver */
46 struct spi_device *sdev;
47
48 /* SPI-GPIO driver */
49 struct spi_gpio_platform_data spi_gpio_pdata;
50 struct platform_device spi_gpio_pdev;
51 };
52
53 struct ucmb_message_hdr {
54 __le16 magic; /* UCMB_MAGIC */
55 __le16 len; /* Payload length (excluding header and footer) */
56 } __attribute__((packed));
57
58 struct ucmb_message_footer {
59 __le16 crc; /* CRC of the header + payload. */
60 } __attribute__((packed));
61
62 struct ucmb_status {
63 __le16 magic; /* UCMB_MAGIC */
64 __le16 code; /* enum ucmb_status_code */
65 } __attribute__((packed));
66
67 #define UCMB_MAGIC 0x1337
68
69 enum ucmb_status_code {
70 UCMB_STAT_OK = 0,
71 UCMB_STAT_EPROTO, /* Protocol format error */
72 UCMB_STAT_ENOMEM, /* Out of memory */
73 UCMB_STAT_E2BIG, /* Message too big */
74 UCMB_STAT_ECRC, /* CRC error */
75 };
76
77
78 static int ucmb_spi_busnum_count = 1337;
79
80
81 static struct ucmb_platform_data ucmb_list[] = {
82 { //FIXME don't define it here.
83 .name = "ucmb",
84 .gpio_cs = SPI_GPIO_NO_CHIPSELECT,
85 .gpio_sck = 0,
86 .gpio_miso = 1,
87 .gpio_mosi = 2,
88 .gpio_reset = 3,
89 .reset_activelow = 0,
90 .mode = SPI_MODE_0,
91 .max_speed_hz = 128000, /* Hz */
92 .msg_delay_ms = 1, /* mS */
93 },
94 };
95
96
97 static int __devinit ucmb_spi_probe(struct spi_device *sdev)
98 {
99 return 0;
100 }
101
102 static int __devexit ucmb_spi_remove(struct spi_device *sdev)
103 {
104 return 0;
105 }
106
107 static struct spi_driver ucmb_spi_driver = {
108 .driver = {
109 .name = "ucmb",
110 .bus = &spi_bus_type,
111 .owner = THIS_MODULE,
112 },
113 .probe = ucmb_spi_probe,
114 .remove = __devexit_p(ucmb_spi_remove),
115 };
116
117 static void ucmb_toggle_reset_line(struct ucmb *ucmb, bool active)
118 {
119 if (ucmb->reset_activelow)
120 active = !active;
121 gpio_set_value(ucmb->gpio_reset, active);
122 }
123
124 static int ucmb_reset_microcontroller(struct ucmb *ucmb)
125 {
126 if (ucmb->gpio_reset == UCMB_NO_RESET)
127 return -ENODEV;
128
129 ucmb_toggle_reset_line(ucmb, 1);
130 msleep(50);
131 ucmb_toggle_reset_line(ucmb, 0);
132 msleep(10);
133
134 return 0;
135 }
136
137 static int ucmb_status_code_to_errno(enum ucmb_status_code code)
138 {
139 switch (code) {
140 case UCMB_STAT_OK:
141 return 0;
142 case UCMB_STAT_EPROTO:
143 return -EPROTO;
144 case UCMB_STAT_ENOMEM:
145 return -ENOMEM;
146 case UCMB_STAT_E2BIG:
147 return -E2BIG;
148 case UCMB_STAT_ECRC:
149 return -EBADMSG;
150 }
151 return -EBUSY;
152 }
153
154 static inline struct ucmb * filp_to_ucmb(struct file *filp)
155 {
156 return container_of(filp->f_op, struct ucmb, mdev_fops);
157 }
158
159 static int ucmb_ioctl(struct inode *inode, struct file *filp,
160 unsigned int cmd, unsigned long arg)
161 {
162 struct ucmb *ucmb = filp_to_ucmb(filp);
163 int ret = 0;
164
165 mutex_lock(&ucmb->mutex);
166 switch (cmd) {
167 case UCMB_IOCTL_RESETUC:
168 ret = ucmb_reset_microcontroller(ucmb);
169 break;
170 default:
171 ret = -EINVAL;
172 }
173 mutex_unlock(&ucmb->mutex);
174
175 return ret;
176 }
177
178 static ssize_t ucmb_read(struct file *filp, char __user *user_buf,
179 size_t size, loff_t *offp)
180 {
181 struct ucmb *ucmb = filp_to_ucmb(filp);
182 u8 *buf;
183 int res, err;
184 struct ucmb_message_hdr hdr;
185 struct ucmb_message_footer footer;
186 struct ucmb_status status = { .magic = cpu_to_le16(UCMB_MAGIC), };
187 u16 crc = 0xFFFF;
188
189 mutex_lock(&ucmb->mutex);
190
191 size = min_t(size_t, size, PAGE_SIZE);
192
193 err = -ENOMEM;
194 buf = (char *)__get_free_page(GFP_KERNEL);
195 if (!buf)
196 goto out;
197
198 err = spi_read(ucmb->sdev, (u8 *)&hdr, sizeof(hdr));
199 if (err)
200 goto out_free;
201 #ifdef DEBUG
202 printk(KERN_DEBUG PFX "Received message header 0x%04X 0x%04X\n",
203 le16_to_cpu(hdr.magic), le16_to_cpu(hdr.len));
204 #endif
205 err = -EPROTO;
206 if (hdr.magic != cpu_to_le16(UCMB_MAGIC))
207 goto out_free;
208 err = -ENOBUFS;
209 if (size < le16_to_cpu(hdr.len))
210 goto out_free;
211 size = le16_to_cpu(hdr.len);
212 err = spi_read(ucmb->sdev, buf, size);
213 if (err)
214 goto out_free;
215 err = spi_read(ucmb->sdev, (u8 *)&footer, sizeof(footer));
216 if (err)
217 goto out_free;
218
219 crc = crc16(crc, (u8 *)&hdr, sizeof(hdr));
220 crc = crc16(crc, buf, size);
221 crc ^= 0xFFFF;
222 if (crc != le16_to_cpu(footer.crc)) {
223 err = -EPROTO;
224 status.code = UCMB_STAT_ECRC;
225 goto out_send_status;
226 }
227
228 if (copy_to_user(user_buf, buf, size)) {
229 err = -EFAULT;
230 status.code = UCMB_STAT_ENOMEM;
231 goto out_send_status;
232 }
233
234 status.code = UCMB_STAT_OK;
235 err = 0;
236
237 out_send_status:
238 res = spi_write(ucmb->sdev, (u8 *)&status, sizeof(status));
239 if (res && !err)
240 err = res;
241 out_free:
242 free_page((unsigned long)buf);
243 out:
244 mutex_unlock(&ucmb->mutex);
245
246 return err ? err : size;
247 }
248
249 static ssize_t ucmb_write(struct file *filp, const char __user *user_buf,
250 size_t size, loff_t *offp)
251 {
252 struct ucmb *ucmb = filp_to_ucmb(filp);
253 u8 *buf;
254 int err;
255 struct ucmb_message_hdr hdr = { .magic = cpu_to_le16(UCMB_MAGIC), };
256 struct ucmb_message_footer footer = { .crc = 0xFFFF, };
257 struct ucmb_status status;
258 struct spi_transfer spi_hdr_xfer;
259 struct spi_transfer spi_footer_xfer;
260 struct spi_transfer spi_data_xfer;
261 struct spi_message spi_msg;
262
263 mutex_lock(&ucmb->mutex);
264
265 err = -ENOMEM;
266 buf = (char *)__get_free_page(GFP_KERNEL);
267 if (!buf)
268 goto out;
269
270 size = min_t(size_t, PAGE_SIZE, size);
271 err = -EFAULT;
272 if (copy_from_user(buf, user_buf, size))
273 goto out_free;
274 hdr.len = cpu_to_le16(size);
275
276 footer.crc = crc16(footer.crc, (u8 *)&hdr, sizeof(hdr));
277 footer.crc = crc16(footer.crc, buf, size);
278 footer.crc ^= 0xFFFF;
279
280 spi_message_init(&spi_msg);
281
282 memset(&spi_hdr_xfer, 0, sizeof(spi_hdr_xfer));
283 spi_hdr_xfer.tx_buf = &hdr;
284 spi_hdr_xfer.len = sizeof(hdr);
285 spi_message_add_tail(&spi_hdr_xfer, &spi_msg);
286
287 memset(&spi_data_xfer, 0, sizeof(spi_data_xfer));
288 spi_data_xfer.tx_buf = buf;
289 spi_data_xfer.len = size;
290 spi_message_add_tail(&spi_data_xfer, &spi_msg);
291
292 memset(&spi_footer_xfer, 0, sizeof(spi_footer_xfer));
293 spi_footer_xfer.tx_buf = &footer;
294 spi_footer_xfer.len = sizeof(footer);
295 spi_message_add_tail(&spi_footer_xfer, &spi_msg);
296
297 /* Send the message, including header. */
298 err = spi_sync(ucmb->sdev, &spi_msg);
299 if (err)
300 goto out_free;
301
302 /* The microcontroller deserves some time to process the message. */
303 if (ucmb->msg_delay_ms)
304 msleep(ucmb->msg_delay_ms);
305
306 /* Get the status code. */
307 err = spi_read(ucmb->sdev, (u8 *)&status, sizeof(status));
308 if (err)
309 goto out_free;
310 #ifdef DEBUG
311 printk(KERN_DEBUG PFX "Sent message. Status report: 0x%04X 0x%04X\n",
312 le16_to_cpu(status.magic), le16_to_cpu(status.code));
313 #endif
314 err = -EPROTO;
315 if (status.magic != cpu_to_le16(UCMB_MAGIC))
316 goto out_free;
317 err = ucmb_status_code_to_errno(le16_to_cpu(status.code));
318 if (err)
319 goto out_free;
320
321 out_free:
322 free_page((unsigned long)buf);
323 out:
324 mutex_unlock(&ucmb->mutex);
325
326 return err ? err : size;
327 }
328
329 static int __devinit ucmb_probe(struct platform_device *pdev)
330 {
331 struct ucmb_platform_data *pdata;
332 struct ucmb *ucmb;
333 int err;
334 const int bus_num = ucmb_spi_busnum_count++;
335 struct spi_bitbang *bb;
336
337 pdata = pdev->dev.platform_data;
338 if (!pdata)
339 return -ENXIO;
340
341 ucmb = kzalloc(sizeof(struct ucmb), GFP_KERNEL);
342 if (!ucmb)
343 return -ENOMEM;
344 mutex_init(&ucmb->mutex);
345 ucmb->msg_delay_ms = pdata->msg_delay_ms;
346 ucmb->gpio_reset = pdata->gpio_reset;
347 ucmb->reset_activelow = pdata->reset_activelow;
348
349 /* Create the SPI GPIO bus master. */
350
351 #ifdef CONFIG_SPI_GPIO_MODULE
352 err = request_module("spi_gpio");
353 if (err)
354 printk(KERN_WARNING PFX "Failed to request spi_gpio module\n");
355 #endif /* CONFIG_SPI_GPIO_MODULE */
356
357 ucmb->spi_gpio_pdata.sck = pdata->gpio_sck;
358 ucmb->spi_gpio_pdata.mosi = pdata->gpio_mosi;
359 ucmb->spi_gpio_pdata.miso = pdata->gpio_miso;
360 ucmb->spi_gpio_pdata.num_chipselect = 1;
361
362 ucmb->spi_gpio_pdev.name = "spi_gpio";
363 ucmb->spi_gpio_pdev.id = bus_num;
364 ucmb->spi_gpio_pdev.dev.platform_data = &ucmb->spi_gpio_pdata;
365
366 err = platform_device_register(&ucmb->spi_gpio_pdev);
367 if (err) {
368 printk(KERN_ERR PFX "Failed to register SPI-GPIO platform device\n");
369 goto err_free_ucmb;
370 }
371 bb = platform_get_drvdata(&ucmb->spi_gpio_pdev);
372 if (!bb || !bb->master) {
373 printk(KERN_ERR PFX "No bitbanged master device found.\n");
374 goto err_unreg_spi_gpio_pdev;
375 }
376
377 /* Create the SPI device. */
378
379 ucmb->sdev = spi_alloc_device(bb->master);
380 if (!ucmb->sdev) {
381 printk(KERN_ERR PFX "Failed to allocate SPI device\n");
382 goto err_unreg_spi_gpio_pdev;
383 }
384 ucmb->sdev->max_speed_hz = pdata->max_speed_hz;
385 ucmb->sdev->chip_select = 0;
386 ucmb->sdev->mode = pdata->mode;
387 strlcpy(ucmb->sdev->modalias, "ucmb", /* We are the SPI driver. */
388 sizeof(ucmb->sdev->modalias));
389 ucmb->sdev->controller_data = (void *)pdata->gpio_cs;
390 err = spi_add_device(ucmb->sdev);
391 if (err) {
392 printk(KERN_ERR PFX "Failed to add SPI device\n");
393 goto err_free_spi_device;
394 }
395
396 /* Initialize the RESET line. */
397
398 if (pdata->gpio_reset != UCMB_NO_RESET) {
399 err = gpio_request(pdata->gpio_reset, pdata->name);
400 if (err) {
401 printk(KERN_ERR PFX
402 "Failed to request RESET GPIO line\n");
403 goto err_unreg_spi_device;
404 }
405 err = gpio_direction_output(pdata->gpio_reset,
406 pdata->reset_activelow);
407 if (err) {
408 printk(KERN_ERR PFX
409 "Failed to set RESET GPIO direction\n");
410 goto err_free_reset_gpio;
411 }
412 ucmb_reset_microcontroller(ucmb);
413 }
414
415 /* Create the Misc char device. */
416
417 ucmb->mdev.minor = MISC_DYNAMIC_MINOR;
418 ucmb->mdev.name = pdata->name;
419 ucmb->mdev.parent = &pdev->dev;
420 ucmb->mdev_fops.read = ucmb_read;
421 ucmb->mdev_fops.write = ucmb_write;
422 ucmb->mdev_fops.ioctl = ucmb_ioctl;
423 ucmb->mdev.fops = &ucmb->mdev_fops;
424
425 err = misc_register(&ucmb->mdev);
426 if (err) {
427 printk(KERN_ERR PFX "Failed to register miscdev %s\n",
428 ucmb->mdev.name);
429 goto err_free_reset_gpio;
430 }
431
432 platform_set_drvdata(pdev, ucmb);
433
434 printk(KERN_INFO PFX "Registered message bus \"%s\"\n", pdata->name);
435
436 return 0;
437
438 err_free_reset_gpio:
439 if (pdata->gpio_reset != UCMB_NO_RESET)
440 gpio_free(pdata->gpio_reset);
441 err_unreg_spi_device:
442 spi_unregister_device(ucmb->sdev);
443 err_free_spi_device:
444 spi_dev_put(ucmb->sdev);
445 err_unreg_spi_gpio_pdev:
446 platform_device_unregister(&ucmb->spi_gpio_pdev);
447 err_free_ucmb:
448 kfree(ucmb);
449
450 return err;
451 }
452
453 static int __devexit ucmb_remove(struct platform_device *pdev)
454 {
455 struct ucmb *ucmb = platform_get_drvdata(pdev);
456 int err;
457
458 err = misc_deregister(&ucmb->mdev);
459 if (err) {
460 printk(KERN_ERR PFX "Failed to unregister miscdev %s\n",
461 ucmb->mdev.name);
462 }
463 if (ucmb->gpio_reset != UCMB_NO_RESET)
464 gpio_free(ucmb->gpio_reset);
465 spi_unregister_device(ucmb->sdev);
466 spi_dev_put(ucmb->sdev);
467 platform_device_unregister(&ucmb->spi_gpio_pdev);
468
469 kfree(ucmb);
470 platform_set_drvdata(pdev, NULL);
471
472 return 0;
473 }
474
475 static struct platform_driver ucmb_driver = {
476 .driver = {
477 .name = "ucmb",
478 .owner = THIS_MODULE,
479 },
480 .probe = ucmb_probe,
481 .remove = __devexit_p(ucmb_remove),
482 };
483
484 static int ucmb_modinit(void)
485 {
486 struct ucmb_platform_data *pdata;
487 struct platform_device *pdev;
488 int err, i;
489
490 printk(KERN_INFO "Microcontroller message bus driver\n");
491
492 err = platform_driver_register(&ucmb_driver);
493 if (err) {
494 printk(KERN_ERR PFX "Failed to register platform driver\n");
495 return err;
496 }
497 err = spi_register_driver(&ucmb_spi_driver);
498 if (err) {
499 printk(KERN_ERR PFX "Failed to register SPI driver\n");
500 platform_driver_unregister(&ucmb_driver);
501 return err;
502 }
503
504 for (i = 0; i < ARRAY_SIZE(ucmb_list); i++) {
505 pdata = &ucmb_list[i];
506
507 pdev = platform_device_alloc("ucmb", i);
508 if (!pdev) {
509 printk(KERN_ERR PFX "Failed to allocate platform device.\n");
510 break;
511 }
512 err = platform_device_add_data(pdev, pdata, sizeof(*pdata));
513 if (err) {
514 printk(KERN_ERR PFX "Failed to add platform data.\n");
515 platform_device_put(pdev);
516 break;
517 }
518 err = platform_device_add(pdev);
519 if (err) {
520 printk(KERN_ERR PFX "Failed to register platform device.\n");
521 platform_device_put(pdev);
522 break;
523 }
524 pdata->pdev = pdev;
525 }
526
527 return 0;
528 }
529 module_init(ucmb_modinit);
530
531 static void ucmb_modexit(void)
532 {
533 struct ucmb_platform_data *pdata;
534 int i;
535
536 for (i = 0; i < ARRAY_SIZE(ucmb_list); i++) {
537 pdata = &ucmb_list[i];
538
539 if (pdata->pdev) {
540 platform_device_unregister(pdata->pdev);
541 platform_device_put(pdata->pdev);
542 }
543 }
544 spi_unregister_driver(&ucmb_spi_driver);
545 platform_driver_unregister(&ucmb_driver);
546 }
547 module_exit(ucmb_modexit);