Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Lars Poeschel | f01312d | 2012-11-05 15:48:23 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Nano River Technologies viperboard driver |
| 4 | * |
| 5 | * This is the core driver for the viperboard. There are cell drivers |
| 6 | * available for I2C, ADC and both GPIOs. SPI is not yet supported. |
| 7 | * The drivers do not support all features the board exposes. See user |
| 8 | * manual of the viperboard. |
| 9 | * |
| 10 | * (C) 2012 by Lemonage GmbH |
| 11 | * Author: Lars Poeschel <poeschel@lemonage.de> |
| 12 | * All rights reserved. |
Lars Poeschel | f01312d | 2012-11-05 15:48:23 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/mutex.h> |
| 21 | |
| 22 | #include <linux/mfd/core.h> |
| 23 | #include <linux/mfd/viperboard.h> |
| 24 | |
| 25 | #include <linux/usb.h> |
| 26 | |
| 27 | |
| 28 | static const struct usb_device_id vprbrd_table[] = { |
| 29 | { USB_DEVICE(0x2058, 0x1005) }, /* Nano River Technologies */ |
| 30 | { } /* Terminating entry */ |
| 31 | }; |
| 32 | |
| 33 | MODULE_DEVICE_TABLE(usb, vprbrd_table); |
| 34 | |
Geert Uytterhoeven | 5ac9855 | 2013-11-18 14:33:06 +0100 | [diff] [blame] | 35 | static const struct mfd_cell vprbrd_devs[] = { |
Lars Poeschel | 9d5b72d | 2012-11-05 15:48:24 +0100 | [diff] [blame] | 36 | { |
| 37 | .name = "viperboard-gpio", |
| 38 | }, |
Lars Poeschel | 174a13a | 2012-11-19 16:36:04 +0100 | [diff] [blame] | 39 | { |
| 40 | .name = "viperboard-i2c", |
| 41 | }, |
Lars Poeschel | ffd8a6e | 2012-11-05 15:48:26 +0100 | [diff] [blame] | 42 | { |
| 43 | .name = "viperboard-adc", |
| 44 | }, |
Lars Poeschel | f01312d | 2012-11-05 15:48:23 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static int vprbrd_probe(struct usb_interface *interface, |
| 48 | const struct usb_device_id *id) |
| 49 | { |
| 50 | struct vprbrd *vb; |
| 51 | |
| 52 | u16 version = 0; |
| 53 | int pipe, ret; |
Lars Poeschel | f01312d | 2012-11-05 15:48:23 +0100 | [diff] [blame] | 54 | |
| 55 | /* allocate memory for our device state and initialize it */ |
| 56 | vb = kzalloc(sizeof(*vb), GFP_KERNEL); |
Markus Elfring | 59f4d74 | 2018-03-09 09:56:31 +0100 | [diff] [blame] | 57 | if (!vb) |
Lars Poeschel | f01312d | 2012-11-05 15:48:23 +0100 | [diff] [blame] | 58 | return -ENOMEM; |
Lars Poeschel | f01312d | 2012-11-05 15:48:23 +0100 | [diff] [blame] | 59 | |
| 60 | mutex_init(&vb->lock); |
| 61 | |
| 62 | vb->usb_dev = usb_get_dev(interface_to_usbdev(interface)); |
| 63 | |
| 64 | /* save our data pointer in this interface device */ |
| 65 | usb_set_intfdata(interface, vb); |
| 66 | dev_set_drvdata(&vb->pdev.dev, vb); |
| 67 | |
| 68 | /* get version information, major first, minor then */ |
| 69 | pipe = usb_rcvctrlpipe(vb->usb_dev, 0); |
| 70 | ret = usb_control_msg(vb->usb_dev, pipe, VPRBRD_USB_REQUEST_MAJOR, |
Lars Poeschel | 302b956 | 2012-11-26 11:24:53 +0100 | [diff] [blame] | 71 | VPRBRD_USB_TYPE_IN, 0x0000, 0x0000, vb->buf, 1, |
Lars Poeschel | f01312d | 2012-11-05 15:48:23 +0100 | [diff] [blame] | 72 | VPRBRD_USB_TIMEOUT_MS); |
| 73 | if (ret == 1) |
Lars Poeschel | 302b956 | 2012-11-26 11:24:53 +0100 | [diff] [blame] | 74 | version = vb->buf[0]; |
Lars Poeschel | f01312d | 2012-11-05 15:48:23 +0100 | [diff] [blame] | 75 | |
| 76 | ret = usb_control_msg(vb->usb_dev, pipe, VPRBRD_USB_REQUEST_MINOR, |
Lars Poeschel | 302b956 | 2012-11-26 11:24:53 +0100 | [diff] [blame] | 77 | VPRBRD_USB_TYPE_IN, 0x0000, 0x0000, vb->buf, 1, |
Lars Poeschel | f01312d | 2012-11-05 15:48:23 +0100 | [diff] [blame] | 78 | VPRBRD_USB_TIMEOUT_MS); |
| 79 | if (ret == 1) { |
| 80 | version <<= 8; |
Lars Poeschel | 302b956 | 2012-11-26 11:24:53 +0100 | [diff] [blame] | 81 | version = version | vb->buf[0]; |
Lars Poeschel | f01312d | 2012-11-05 15:48:23 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | dev_info(&interface->dev, |
| 85 | "version %x.%02x found at bus %03d address %03d\n", |
| 86 | version >> 8, version & 0xff, |
| 87 | vb->usb_dev->bus->busnum, vb->usb_dev->devnum); |
| 88 | |
Johan Hovold | 1ab589c | 2014-09-26 12:55:31 +0200 | [diff] [blame] | 89 | ret = mfd_add_hotplug_devices(&interface->dev, vprbrd_devs, |
| 90 | ARRAY_SIZE(vprbrd_devs)); |
Lars Poeschel | f01312d | 2012-11-05 15:48:23 +0100 | [diff] [blame] | 91 | if (ret != 0) { |
| 92 | dev_err(&interface->dev, "Failed to add mfd devices to core."); |
| 93 | goto error; |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | |
| 98 | error: |
| 99 | if (vb) { |
| 100 | usb_put_dev(vb->usb_dev); |
| 101 | kfree(vb); |
| 102 | } |
| 103 | |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | static void vprbrd_disconnect(struct usb_interface *interface) |
| 108 | { |
| 109 | struct vprbrd *vb = usb_get_intfdata(interface); |
| 110 | |
| 111 | mfd_remove_devices(&interface->dev); |
| 112 | usb_set_intfdata(interface, NULL); |
| 113 | usb_put_dev(vb->usb_dev); |
| 114 | kfree(vb); |
| 115 | |
| 116 | dev_dbg(&interface->dev, "disconnected\n"); |
| 117 | } |
| 118 | |
| 119 | static struct usb_driver vprbrd_driver = { |
| 120 | .name = "viperboard", |
| 121 | .probe = vprbrd_probe, |
| 122 | .disconnect = vprbrd_disconnect, |
| 123 | .id_table = vprbrd_table, |
| 124 | }; |
| 125 | |
| 126 | module_usb_driver(vprbrd_driver); |
| 127 | |
| 128 | MODULE_DESCRIPTION("Nano River Technologies viperboard mfd core driver"); |
| 129 | MODULE_AUTHOR("Lars Poeschel <poeschel@lemonage.de>"); |
| 130 | MODULE_LICENSE("GPL"); |