1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 	Mantis PCI bridge driver
4 
5 	Copyright (C) Manu Abraham (abraham.manu@gmail.com)
6 
7 */
8 
9 #include <linux/kernel.h>
10 #include <linux/i2c.h>
11 
12 #include <linux/signal.h>
13 #include <linux/sched.h>
14 #include <linux/interrupt.h>
15 #include <asm/io.h>
16 
17 #include <media/dmxdev.h>
18 #include <media/dvbdev.h>
19 #include <media/dvb_demux.h>
20 #include <media/dvb_frontend.h>
21 #include <media/dvb_net.h>
22 
23 #include "mantis_common.h"
24 #include "mantis_reg.h"
25 #include "mantis_ioc.h"
26 
27 static int read_eeprom_bytes(struct mantis_pci *mantis, u8 reg, u8 *data, u8 length)
28 {
29 	struct i2c_adapter *adapter = &mantis->adapter;
30 	int err;
31 	u8 buf = reg;
32 
33 	struct i2c_msg msg[] = {
34 		{ .addr = 0x50, .flags = 0, .buf = &buf, .len = 1 },
35 		{ .addr = 0x50, .flags = I2C_M_RD, .buf = data, .len = length },
36 	};
37 
38 	err = i2c_transfer(adapter, msg, 2);
39 	if (err < 0) {
40 		dprintk(MANTIS_ERROR, 1, "ERROR: i2c read: < err=%i d0=0x%02x d1=0x%02x >",
41 			err, data[0], data[1]);
42 
43 		return err;
44 	}
45 
46 	return 0;
47 }
48 int mantis_get_mac(struct mantis_pci *mantis)
49 {
50 	int err;
51 	u8 mac_addr[6] = {0};
52 
53 	err = read_eeprom_bytes(mantis, 0x08, mac_addr, 6);
54 	if (err < 0) {
55 		dprintk(MANTIS_ERROR, 1, "ERROR: Mantis EEPROM read error <%d>", err);
56 
57 		return err;
58 	}
59 
60 	dprintk(MANTIS_ERROR, 0, "    MAC Address=[%pM]\n", mac_addr);
61 
62 	return 0;
63 }
64 EXPORT_SYMBOL_GPL(mantis_get_mac);
65 
66 /* Turn the given bit on or off. */
67 void mantis_gpio_set_bits(struct mantis_pci *mantis, u32 bitpos, u8 value)
68 {
69 	u32 cur;
70 
71 	dprintk(MANTIS_DEBUG, 1, "Set Bit <%d> to <%d>", bitpos, value);
72 	cur = mmread(MANTIS_GPIF_ADDR);
73 	if (value)
74 		mantis->gpio_status = cur | (1 << bitpos);
75 	else
76 		mantis->gpio_status = cur & (~(1 << bitpos));
77 
78 	dprintk(MANTIS_DEBUG, 1, "GPIO Value <%02x>", mantis->gpio_status);
79 	mmwrite(mantis->gpio_status, MANTIS_GPIF_ADDR);
80 	mmwrite(0x00, MANTIS_GPIF_DOUT);
81 }
82 EXPORT_SYMBOL_GPL(mantis_gpio_set_bits);
83 
84 int mantis_stream_control(struct mantis_pci *mantis, enum mantis_stream_control stream_ctl)
85 {
86 	u32 reg;
87 
88 	reg = mmread(MANTIS_CONTROL);
89 	switch (stream_ctl) {
90 	case STREAM_TO_HIF:
91 		dprintk(MANTIS_DEBUG, 1, "Set stream to HIF");
92 		reg &= 0xff - MANTIS_BYPASS;
93 		mmwrite(reg, MANTIS_CONTROL);
94 		reg |= MANTIS_BYPASS;
95 		mmwrite(reg, MANTIS_CONTROL);
96 		break;
97 
98 	case STREAM_TO_CAM:
99 		dprintk(MANTIS_DEBUG, 1, "Set stream to CAM");
100 		reg |= MANTIS_BYPASS;
101 		mmwrite(reg, MANTIS_CONTROL);
102 		reg &= 0xff - MANTIS_BYPASS;
103 		mmwrite(reg, MANTIS_CONTROL);
104 		break;
105 	default:
106 		dprintk(MANTIS_ERROR, 1, "Unknown MODE <%02x>", stream_ctl);
107 		return -1;
108 	}
109 
110 	return 0;
111 }
112 EXPORT_SYMBOL_GPL(mantis_stream_control);
113