Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  Nano River Technologies viperboard i2c master driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  (C) 2012 by Lemonage GmbH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *  Author: Lars Poeschel <poeschel@lemonage.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/mfd/viperboard.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) struct vprbrd_i2c {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct i2c_adapter i2c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	u8 bus_freq_param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) /* i2c bus frequency module parameter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static u8 i2c_bus_param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static unsigned int i2c_bus_freq = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) module_param(i2c_bus_freq, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) MODULE_PARM_DESC(i2c_bus_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	"i2c bus frequency in khz (default is 100) valid values: 10, 100, 200, 400, 1000, 3000, 6000");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static int vprbrd_i2c_status(struct i2c_adapter *i2c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct vprbrd_i2c_status *status, bool prev_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	u16 bytes_xfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct vprbrd *vb = (struct vprbrd *)i2c->algo_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	/* check for protocol error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	bytes_xfer = sizeof(struct vprbrd_i2c_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	ret = usb_control_msg(vb->usb_dev, usb_rcvctrlpipe(vb->usb_dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		VPRBRD_USB_REQUEST_I2C, VPRBRD_USB_TYPE_IN, 0x0000, 0x0000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		status, bytes_xfer, VPRBRD_USB_TIMEOUT_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (ret != bytes_xfer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		prev_error = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (prev_error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		dev_err(&i2c->dev, "failure in usb communication\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	dev_dbg(&i2c->dev, "  status = %d\n", status->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (status->status != 0x00) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		dev_err(&i2c->dev, "failure: i2c protocol error\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return -EPROTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static int vprbrd_i2c_receive(struct usb_device *usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct vprbrd_i2c_read_msg *rmsg, int bytes_xfer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	int ret, bytes_actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	/* send the read request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	ret = usb_bulk_msg(usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		usb_sndbulkpipe(usb_dev, VPRBRD_EP_OUT), rmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		sizeof(struct vprbrd_i2c_read_hdr), &bytes_actual,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		VPRBRD_USB_TIMEOUT_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if ((ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		|| (bytes_actual != sizeof(struct vprbrd_i2c_read_hdr))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		dev_err(&usb_dev->dev, "failure transmitting usb\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		error = -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	/* read the actual data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	ret = usb_bulk_msg(usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		usb_rcvbulkpipe(usb_dev, VPRBRD_EP_IN), rmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		bytes_xfer, &bytes_actual, VPRBRD_USB_TIMEOUT_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if ((ret < 0) || (bytes_xfer != bytes_actual)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		dev_err(&usb_dev->dev, "failure receiving usb\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		error = -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static int vprbrd_i2c_addr(struct usb_device *usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct vprbrd_i2c_addr_msg *amsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	int ret, bytes_actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	ret = usb_bulk_msg(usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		usb_sndbulkpipe(usb_dev, VPRBRD_EP_OUT), amsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		sizeof(struct vprbrd_i2c_addr_msg), &bytes_actual,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		VPRBRD_USB_TIMEOUT_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if ((ret < 0) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			(sizeof(struct vprbrd_i2c_addr_msg) != bytes_actual)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		dev_err(&usb_dev->dev, "failure transmitting usb\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		return -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static int vprbrd_i2c_read(struct vprbrd *vb, struct i2c_msg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	u16 remain_len, len1, len2, start = 0x0000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct vprbrd_i2c_read_msg *rmsg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		(struct vprbrd_i2c_read_msg *)vb->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	remain_len = msg->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	rmsg->header.cmd = VPRBRD_I2C_CMD_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	while (remain_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		rmsg->header.addr = cpu_to_le16(start + 0x4000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		if (remain_len <= 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			len1 = remain_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			len2 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			rmsg->header.len0 = remain_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			rmsg->header.len1 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			rmsg->header.len2 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			rmsg->header.len3 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			rmsg->header.len4 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			rmsg->header.len5 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			remain_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		} else if (remain_len <= 510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			len1 = remain_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			len2 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			rmsg->header.len0 = remain_len - 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			rmsg->header.len1 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			rmsg->header.len2 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			rmsg->header.len3 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			rmsg->header.len4 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			rmsg->header.len5 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			remain_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		} else if (remain_len <= 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			len1 = remain_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			len2 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			rmsg->header.len0 = remain_len - 510;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			rmsg->header.len1 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			rmsg->header.len2 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			rmsg->header.len3 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			rmsg->header.len4 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			rmsg->header.len5 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			remain_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		} else if (remain_len <= 767) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			len1 = 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			len2 = remain_len - 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			rmsg->header.len0 = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			rmsg->header.len1 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			rmsg->header.len2 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			rmsg->header.len3 = remain_len - 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			rmsg->header.len4 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			rmsg->header.len5 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			remain_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		} else if (remain_len <= 1022) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			len1 = 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			len2 = remain_len - 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			rmsg->header.len0 = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			rmsg->header.len1 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			rmsg->header.len2 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			rmsg->header.len3 = remain_len - 767;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			rmsg->header.len4 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			rmsg->header.len5 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			remain_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		} else if (remain_len <= 1024) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			len1 = 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			len2 = remain_len - 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			rmsg->header.len0 = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			rmsg->header.len1 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			rmsg->header.len2 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			rmsg->header.len3 = remain_len - 1022;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			rmsg->header.len4 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			rmsg->header.len5 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			remain_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			len1 = 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 			len2 = 512;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			rmsg->header.len0 = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			rmsg->header.len1 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			rmsg->header.len2 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			rmsg->header.len3 = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			rmsg->header.len4 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			rmsg->header.len5 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			remain_len -= 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			start += 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		rmsg->header.tf1 = cpu_to_le16(len1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		rmsg->header.tf2 = cpu_to_le16(len2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		/* first read transfer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		ret = vprbrd_i2c_receive(vb->usb_dev, rmsg, len1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		/* copy the received data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		memcpy(msg->buf + start, rmsg, len1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		/* second read transfer if neccessary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		if (len2 > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			ret = vprbrd_i2c_receive(vb->usb_dev, rmsg, len2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			/* copy the received data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			memcpy(msg->buf + start + 512, rmsg, len2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static int vprbrd_i2c_write(struct vprbrd *vb, struct i2c_msg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	int ret, bytes_actual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	u16 remain_len, bytes_xfer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		start = 0x0000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	struct vprbrd_i2c_write_msg *wmsg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		(struct vprbrd_i2c_write_msg *)vb->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	remain_len = msg->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	wmsg->header.cmd = VPRBRD_I2C_CMD_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	wmsg->header.last = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	wmsg->header.chan = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	wmsg->header.spi = 0x0000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	while (remain_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		wmsg->header.addr = cpu_to_le16(start + 0x4000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		if (remain_len > 503) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			wmsg->header.len1 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			wmsg->header.len2 = 0xf8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			remain_len -= 503;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			bytes_xfer = 503 + sizeof(struct vprbrd_i2c_write_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			start += 503;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		} else if (remain_len > 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			wmsg->header.len1 = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			wmsg->header.len2 = (remain_len - 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			bytes_xfer = remain_len +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				sizeof(struct vprbrd_i2c_write_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			remain_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			wmsg->header.len1 = remain_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			wmsg->header.len2 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			bytes_xfer = remain_len +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				sizeof(struct vprbrd_i2c_write_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			remain_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		memcpy(wmsg->data, msg->buf + start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			bytes_xfer - sizeof(struct vprbrd_i2c_write_hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		ret = usb_bulk_msg(vb->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			usb_sndbulkpipe(vb->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			VPRBRD_EP_OUT), wmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			bytes_xfer, &bytes_actual, VPRBRD_USB_TIMEOUT_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		if ((ret < 0) || (bytes_xfer != bytes_actual))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			return -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static int vprbrd_i2c_xfer(struct i2c_adapter *i2c, struct i2c_msg *msgs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	struct i2c_msg *pmsg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	int i, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct vprbrd *vb = (struct vprbrd *)i2c->algo_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct vprbrd_i2c_addr_msg *amsg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		(struct vprbrd_i2c_addr_msg *)vb->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	struct vprbrd_i2c_status *smsg = (struct vprbrd_i2c_status *)vb->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	dev_dbg(&i2c->dev, "master xfer %d messages:\n", num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	for (i = 0 ; i < num ; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		pmsg = &msgs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		dev_dbg(&i2c->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			"  %d: %s (flags %d) %d bytes to 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			i, pmsg->flags & I2C_M_RD ? "read" : "write",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			pmsg->flags, pmsg->len, pmsg->addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		mutex_lock(&vb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		/* directly send the message */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		if (pmsg->flags & I2C_M_RD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			/* read data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			amsg->cmd = VPRBRD_I2C_CMD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			amsg->unknown2 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			amsg->unknown3 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			amsg->addr = pmsg->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			amsg->unknown1 = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			amsg->len = cpu_to_le16(pmsg->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			/* send the addr and len, we're interested to board */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 			ret = vprbrd_i2c_addr(vb->usb_dev, amsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 				error = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			ret = vprbrd_i2c_read(vb, pmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 				error = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			ret = vprbrd_i2c_status(i2c, smsg, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 				error = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			/* in case of protocol error, return the error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			/* write data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 			ret = vprbrd_i2c_write(vb, pmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			amsg->cmd = VPRBRD_I2C_CMD_ADDR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			amsg->unknown2 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			amsg->unknown3 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			amsg->addr = pmsg->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			amsg->unknown1 = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			amsg->len = cpu_to_le16(pmsg->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			/* send the addr, the data goes to to board */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			ret = vprbrd_i2c_addr(vb->usb_dev, amsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 				error = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			ret = vprbrd_i2c_status(i2c, smsg, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 				error = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 			if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		mutex_unlock(&vb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	return num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	mutex_unlock(&vb->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static u32 vprbrd_i2c_func(struct i2c_adapter *i2c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /* This is the actual algorithm we define */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static const struct i2c_algorithm vprbrd_algorithm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	.master_xfer	= vprbrd_i2c_xfer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	.functionality	= vprbrd_i2c_func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static const struct i2c_adapter_quirks vprbrd_quirks = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	.max_read_len = 2048,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	.max_write_len = 2048,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static int vprbrd_i2c_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	struct vprbrd *vb = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	struct vprbrd_i2c *vb_i2c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	int pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	vb_i2c = devm_kzalloc(&pdev->dev, sizeof(*vb_i2c), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if (vb_i2c == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	/* setup i2c adapter description */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	vb_i2c->i2c.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	vb_i2c->i2c.class = I2C_CLASS_HWMON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	vb_i2c->i2c.algo = &vprbrd_algorithm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	vb_i2c->i2c.quirks = &vprbrd_quirks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	vb_i2c->i2c.algo_data = vb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	/* save the param in usb capabable memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	vb_i2c->bus_freq_param = i2c_bus_param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	snprintf(vb_i2c->i2c.name, sizeof(vb_i2c->i2c.name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		 "viperboard at bus %03d device %03d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		 vb->usb_dev->bus->busnum, vb->usb_dev->devnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	/* setting the bus frequency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	if ((i2c_bus_param <= VPRBRD_I2C_FREQ_10KHZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		&& (i2c_bus_param >= VPRBRD_I2C_FREQ_6MHZ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		pipe = usb_sndctrlpipe(vb->usb_dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		ret = usb_control_msg(vb->usb_dev, pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			VPRBRD_USB_REQUEST_I2C_FREQ, VPRBRD_USB_TYPE_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			0x0000, 0x0000, &vb_i2c->bus_freq_param, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			VPRBRD_USB_TIMEOUT_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		if (ret != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			dev_err(&pdev->dev, "failure setting i2c_bus_freq to %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 				i2c_bus_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			"invalid i2c_bus_freq setting:%d\n", i2c_bus_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	vb_i2c->i2c.dev.parent = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	/* attach to i2c layer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	i2c_add_adapter(&vb_i2c->i2c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	platform_set_drvdata(pdev, vb_i2c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static int vprbrd_i2c_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	struct vprbrd_i2c *vb_i2c = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	i2c_del_adapter(&vb_i2c->i2c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static struct platform_driver vprbrd_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	.driver.name	= "viperboard-i2c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	.driver.owner	= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	.probe		= vprbrd_i2c_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	.remove		= vprbrd_i2c_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static int __init vprbrd_i2c_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	switch (i2c_bus_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	case 6000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		i2c_bus_param = VPRBRD_I2C_FREQ_6MHZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	case 3000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		i2c_bus_param = VPRBRD_I2C_FREQ_3MHZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	case 1000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		i2c_bus_param = VPRBRD_I2C_FREQ_1MHZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	case 400:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		i2c_bus_param = VPRBRD_I2C_FREQ_400KHZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	case 200:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		i2c_bus_param = VPRBRD_I2C_FREQ_200KHZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	case 100:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		i2c_bus_param = VPRBRD_I2C_FREQ_100KHZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	case 10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		i2c_bus_param = VPRBRD_I2C_FREQ_10KHZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		pr_warn("invalid i2c_bus_freq (%d)\n", i2c_bus_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		i2c_bus_param = VPRBRD_I2C_FREQ_100KHZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	return platform_driver_register(&vprbrd_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) subsys_initcall(vprbrd_i2c_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) static void __exit vprbrd_i2c_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	platform_driver_unregister(&vprbrd_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) module_exit(vprbrd_i2c_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) MODULE_AUTHOR("Lars Poeschel <poeschel@lemonage.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) MODULE_DESCRIPTION("I2C master driver for Nano River Techs Viperboard");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) MODULE_ALIAS("platform:viperboard-i2c");