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