xref: /openbmc/linux/drivers/i2c/busses/i2c-icy.c (revision 0d3b051a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * I2C driver for stand-alone PCF8584 style adapters on Zorro cards
4  *
5  * Original ICY documentation can be found on Aminet:
6  * https://aminet.net/package/docs/hard/icy
7  *
8  * There has been a modern community re-print of this design in 2019:
9  * https://www.a1k.org/forum/index.php?threads/70106/
10  *
11  * The card is basically a Philips PCF8584 connected straight to the
12  * beginning of the AutoConfig'd address space (register S1 on base+2),
13  * with /INT on /INT2 on the Zorro bus.
14  *
15  * Copyright (c) 2019 Max Staudt <max@enpas.org>
16  *
17  * This started as a fork of i2c-elektor.c and has evolved since.
18  * Thanks go to its authors for providing a base to grow on.
19  *
20  *
21  * IRQ support is currently not implemented.
22  *
23  * As it turns out, i2c-algo-pcf is really written with i2c-elektor's
24  * edge-triggered ISA interrupts in mind, while the Amiga's Zorro bus has
25  * level-triggered interrupts. This means that once an interrupt occurs, we
26  * have to tell the PCF8584 to shut up immediately, or it will keep the
27  * interrupt line busy and cause an IRQ storm.
28 
29  * However, because of the PCF8584's host-side protocol, there is no good
30  * way to just quieten it without side effects. Rather, we have to perform
31  * the next read/write operation straight away, which will reset the /INT
32  * pin. This entails re-designing the core of i2c-algo-pcf in the future.
33  * For now, we never request an IRQ from the PCF8584, and poll it instead.
34  */
35 
36 #include <linux/delay.h>
37 #include <linux/init.h>
38 #include <linux/io.h>
39 #include <linux/ioport.h>
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 
43 #include <linux/i2c.h>
44 #include <linux/i2c-algo-pcf.h>
45 
46 #include <asm/amigahw.h>
47 #include <asm/amigaints.h>
48 #include <linux/zorro.h>
49 
50 #include "../algos/i2c-algo-pcf.h"
51 
52 struct icy_i2c {
53 	struct i2c_adapter adapter;
54 
55 	void __iomem *reg_s0;
56 	void __iomem *reg_s1;
57 	struct fwnode_handle *ltc2990_fwnode;
58 	struct i2c_client *ltc2990_client;
59 };
60 
61 /*
62  * Functions called by i2c-algo-pcf
63  */
64 static void icy_pcf_setpcf(void *data, int ctl, int val)
65 {
66 	struct icy_i2c *i2c = (struct icy_i2c *)data;
67 
68 	u8 __iomem *address = ctl ? i2c->reg_s1 : i2c->reg_s0;
69 
70 	z_writeb(val, address);
71 }
72 
73 static int icy_pcf_getpcf(void *data, int ctl)
74 {
75 	struct icy_i2c *i2c = (struct icy_i2c *)data;
76 
77 	u8 __iomem *address = ctl ? i2c->reg_s1 : i2c->reg_s0;
78 
79 	return z_readb(address);
80 }
81 
82 static int icy_pcf_getown(void *data)
83 {
84 	return 0x55;
85 }
86 
87 static int icy_pcf_getclock(void *data)
88 {
89 	return 0x1c;
90 }
91 
92 static void icy_pcf_waitforpin(void *data)
93 {
94 	usleep_range(50, 150);
95 }
96 
97 /*
98  * Main i2c-icy part
99  */
100 static unsigned short const icy_ltc2990_addresses[] = {
101 	0x4c, 0x4d, 0x4e, 0x4f, I2C_CLIENT_END
102 };
103 
104 /*
105  * Additional sensors exposed once this property is applied:
106  *
107  * in1 will be the voltage of the 5V rail, divided by 2.
108  * in2 will be the voltage of the 12V rail, divided by 4.
109  * temp3 will be measured using a PCB loop next the chip.
110  */
111 static const u32 icy_ltc2990_meas_mode[] = {0, 3};
112 
113 static const struct property_entry icy_ltc2990_props[] = {
114 	PROPERTY_ENTRY_U32_ARRAY("lltc,meas-mode", icy_ltc2990_meas_mode),
115 	{ }
116 };
117 
118 static int icy_probe(struct zorro_dev *z,
119 		     const struct zorro_device_id *ent)
120 {
121 	struct icy_i2c *i2c;
122 	struct i2c_algo_pcf_data *algo_data;
123 	struct fwnode_handle *new_fwnode;
124 	struct i2c_board_info ltc2990_info = {
125 		.type		= "ltc2990",
126 	};
127 
128 	i2c = devm_kzalloc(&z->dev, sizeof(*i2c), GFP_KERNEL);
129 	if (!i2c)
130 		return -ENOMEM;
131 
132 	algo_data = devm_kzalloc(&z->dev, sizeof(*algo_data), GFP_KERNEL);
133 	if (!algo_data)
134 		return -ENOMEM;
135 
136 	dev_set_drvdata(&z->dev, i2c);
137 	i2c->adapter.dev.parent = &z->dev;
138 	i2c->adapter.owner = THIS_MODULE;
139 	/* i2c->adapter.algo assigned by i2c_pcf_add_bus() */
140 	i2c->adapter.algo_data = algo_data;
141 	strlcpy(i2c->adapter.name, "ICY I2C Zorro adapter",
142 		sizeof(i2c->adapter.name));
143 
144 	if (!devm_request_mem_region(&z->dev,
145 				     z->resource.start,
146 				     4, i2c->adapter.name))
147 		return -ENXIO;
148 
149 	/* Driver private data */
150 	i2c->reg_s0 = ZTWO_VADDR(z->resource.start);
151 	i2c->reg_s1 = ZTWO_VADDR(z->resource.start + 2);
152 
153 	algo_data->data = i2c;
154 	algo_data->setpcf     = icy_pcf_setpcf;
155 	algo_data->getpcf     = icy_pcf_getpcf;
156 	algo_data->getown     = icy_pcf_getown;
157 	algo_data->getclock   = icy_pcf_getclock;
158 	algo_data->waitforpin = icy_pcf_waitforpin;
159 
160 	if (i2c_pcf_add_bus(&i2c->adapter)) {
161 		dev_err(&z->dev, "i2c_pcf_add_bus() failed\n");
162 		return -ENXIO;
163 	}
164 
165 	dev_info(&z->dev, "ICY I2C controller at %pa, IRQ not implemented\n",
166 		 &z->resource.start);
167 
168 	/*
169 	 * The 2019 a1k.org PCBs have an LTC2990 at 0x4c, so start
170 	 * it automatically once ltc2990 is modprobed.
171 	 *
172 	 * in0 is the voltage of the internal 5V power supply.
173 	 * temp1 is the temperature inside the chip.
174 	 *
175 	 * See property_entry above for in1, in2, temp3.
176 	 */
177 	new_fwnode = fwnode_create_software_node(icy_ltc2990_props, NULL);
178 	if (IS_ERR(new_fwnode)) {
179 		dev_info(&z->dev, "Failed to create fwnode for LTC2990, error: %ld\n",
180 			 PTR_ERR(new_fwnode));
181 	} else {
182 		/*
183 		 * Store the fwnode so we can destroy it on .remove().
184 		 * Only store it on success, as fwnode_remove_software_node()
185 		 * is NULL safe, but not PTR_ERR safe.
186 		 */
187 		i2c->ltc2990_fwnode = new_fwnode;
188 		ltc2990_info.fwnode = new_fwnode;
189 
190 		i2c->ltc2990_client =
191 			i2c_new_scanned_device(&i2c->adapter,
192 					       &ltc2990_info,
193 					       icy_ltc2990_addresses,
194 					       NULL);
195 	}
196 
197 	return 0;
198 }
199 
200 static void icy_remove(struct zorro_dev *z)
201 {
202 	struct icy_i2c *i2c = dev_get_drvdata(&z->dev);
203 
204 	i2c_unregister_device(i2c->ltc2990_client);
205 	fwnode_remove_software_node(i2c->ltc2990_fwnode);
206 
207 	i2c_del_adapter(&i2c->adapter);
208 }
209 
210 static const struct zorro_device_id icy_zorro_tbl[] = {
211 	{ ZORRO_ID(VMC, 15, 0), },
212 	{ 0 }
213 };
214 
215 MODULE_DEVICE_TABLE(zorro, icy_zorro_tbl);
216 
217 static struct zorro_driver icy_driver = {
218 	.name           = "i2c-icy",
219 	.id_table       = icy_zorro_tbl,
220 	.probe          = icy_probe,
221 	.remove         = icy_remove,
222 };
223 
224 module_driver(icy_driver,
225 	      zorro_register_driver,
226 	      zorro_unregister_driver);
227 
228 MODULE_AUTHOR("Max Staudt <max@enpas.org>");
229 MODULE_DESCRIPTION("I2C bus via PCF8584 on ICY Zorro card");
230 MODULE_LICENSE("GPL v2");
231