viperboard.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Nano River Technologies viperboard driver
  3. *
  4. * This is the core driver for the viperboard. There are cell drivers
  5. * available for I2C, ADC and both GPIOs. SPI is not yet supported.
  6. * The drivers do not support all features the board exposes. See user
  7. * manual of the viperboard.
  8. *
  9. * (C) 2012 by Lemonage GmbH
  10. * Author: Lars Poeschel <poeschel@lemonage.de>
  11. * All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/types.h>
  24. #include <linux/mutex.h>
  25. #include <linux/mfd/core.h>
  26. #include <linux/mfd/viperboard.h>
  27. #include <linux/usb.h>
  28. static const struct usb_device_id vprbrd_table[] = {
  29. { USB_DEVICE(0x2058, 0x1005) }, /* Nano River Technologies */
  30. { } /* Terminating entry */
  31. };
  32. MODULE_DEVICE_TABLE(usb, vprbrd_table);
  33. static const struct mfd_cell vprbrd_devs[] = {
  34. {
  35. .name = "viperboard-gpio",
  36. },
  37. {
  38. .name = "viperboard-i2c",
  39. },
  40. {
  41. .name = "viperboard-adc",
  42. },
  43. };
  44. static int vprbrd_probe(struct usb_interface *interface,
  45. const struct usb_device_id *id)
  46. {
  47. struct vprbrd *vb;
  48. u16 version = 0;
  49. int pipe, ret;
  50. /* allocate memory for our device state and initialize it */
  51. vb = kzalloc(sizeof(*vb), GFP_KERNEL);
  52. if (!vb)
  53. return -ENOMEM;
  54. mutex_init(&vb->lock);
  55. vb->usb_dev = usb_get_dev(interface_to_usbdev(interface));
  56. /* save our data pointer in this interface device */
  57. usb_set_intfdata(interface, vb);
  58. dev_set_drvdata(&vb->pdev.dev, vb);
  59. /* get version information, major first, minor then */
  60. pipe = usb_rcvctrlpipe(vb->usb_dev, 0);
  61. ret = usb_control_msg(vb->usb_dev, pipe, VPRBRD_USB_REQUEST_MAJOR,
  62. VPRBRD_USB_TYPE_IN, 0x0000, 0x0000, vb->buf, 1,
  63. VPRBRD_USB_TIMEOUT_MS);
  64. if (ret == 1)
  65. version = vb->buf[0];
  66. ret = usb_control_msg(vb->usb_dev, pipe, VPRBRD_USB_REQUEST_MINOR,
  67. VPRBRD_USB_TYPE_IN, 0x0000, 0x0000, vb->buf, 1,
  68. VPRBRD_USB_TIMEOUT_MS);
  69. if (ret == 1) {
  70. version <<= 8;
  71. version = version | vb->buf[0];
  72. }
  73. dev_info(&interface->dev,
  74. "version %x.%02x found at bus %03d address %03d\n",
  75. version >> 8, version & 0xff,
  76. vb->usb_dev->bus->busnum, vb->usb_dev->devnum);
  77. ret = mfd_add_hotplug_devices(&interface->dev, vprbrd_devs,
  78. ARRAY_SIZE(vprbrd_devs));
  79. if (ret != 0) {
  80. dev_err(&interface->dev, "Failed to add mfd devices to core.");
  81. goto error;
  82. }
  83. return 0;
  84. error:
  85. if (vb) {
  86. usb_put_dev(vb->usb_dev);
  87. kfree(vb);
  88. }
  89. return ret;
  90. }
  91. static void vprbrd_disconnect(struct usb_interface *interface)
  92. {
  93. struct vprbrd *vb = usb_get_intfdata(interface);
  94. mfd_remove_devices(&interface->dev);
  95. usb_set_intfdata(interface, NULL);
  96. usb_put_dev(vb->usb_dev);
  97. kfree(vb);
  98. dev_dbg(&interface->dev, "disconnected\n");
  99. }
  100. static struct usb_driver vprbrd_driver = {
  101. .name = "viperboard",
  102. .probe = vprbrd_probe,
  103. .disconnect = vprbrd_disconnect,
  104. .id_table = vprbrd_table,
  105. };
  106. module_usb_driver(vprbrd_driver);
  107. MODULE_DESCRIPTION("Nano River Technologies viperboard mfd core driver");
  108. MODULE_AUTHOR("Lars Poeschel <poeschel@lemonage.de>");
  109. MODULE_LICENSE("GPL");