xref: /linux/drivers/i2c/busses/i2c-icy.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
14768e90eSMax Staudt // SPDX-License-Identifier: GPL-2.0
24768e90eSMax Staudt /*
34768e90eSMax Staudt  * I2C driver for stand-alone PCF8584 style adapters on Zorro cards
44768e90eSMax Staudt  *
54768e90eSMax Staudt  * Original ICY documentation can be found on Aminet:
64768e90eSMax Staudt  * https://aminet.net/package/docs/hard/icy
74768e90eSMax Staudt  *
84768e90eSMax Staudt  * There has been a modern community re-print of this design in 2019:
94768e90eSMax Staudt  * https://www.a1k.org/forum/index.php?threads/70106/
104768e90eSMax Staudt  *
114768e90eSMax Staudt  * The card is basically a Philips PCF8584 connected straight to the
124768e90eSMax Staudt  * beginning of the AutoConfig'd address space (register S1 on base+2),
134768e90eSMax Staudt  * with /INT on /INT2 on the Zorro bus.
144768e90eSMax Staudt  *
154768e90eSMax Staudt  * Copyright (c) 2019 Max Staudt <max@enpas.org>
164768e90eSMax Staudt  *
174768e90eSMax Staudt  * This started as a fork of i2c-elektor.c and has evolved since.
184768e90eSMax Staudt  * Thanks go to its authors for providing a base to grow on.
194768e90eSMax Staudt  *
204768e90eSMax Staudt  *
214768e90eSMax Staudt  * IRQ support is currently not implemented.
224768e90eSMax Staudt  *
234768e90eSMax Staudt  * As it turns out, i2c-algo-pcf is really written with i2c-elektor's
244768e90eSMax Staudt  * edge-triggered ISA interrupts in mind, while the Amiga's Zorro bus has
254768e90eSMax Staudt  * level-triggered interrupts. This means that once an interrupt occurs, we
264768e90eSMax Staudt  * have to tell the PCF8584 to shut up immediately, or it will keep the
274768e90eSMax Staudt  * interrupt line busy and cause an IRQ storm.
284768e90eSMax Staudt 
294768e90eSMax Staudt  * However, because of the PCF8584's host-side protocol, there is no good
304768e90eSMax Staudt  * way to just quieten it without side effects. Rather, we have to perform
314768e90eSMax Staudt  * the next read/write operation straight away, which will reset the /INT
324768e90eSMax Staudt  * pin. This entails re-designing the core of i2c-algo-pcf in the future.
334768e90eSMax Staudt  * For now, we never request an IRQ from the PCF8584, and poll it instead.
344768e90eSMax Staudt  */
354768e90eSMax Staudt 
364768e90eSMax Staudt #include <linux/delay.h>
374768e90eSMax Staudt #include <linux/init.h>
384768e90eSMax Staudt #include <linux/io.h>
394768e90eSMax Staudt #include <linux/ioport.h>
404768e90eSMax Staudt #include <linux/kernel.h>
414768e90eSMax Staudt #include <linux/module.h>
424768e90eSMax Staudt 
434768e90eSMax Staudt #include <linux/i2c.h>
444768e90eSMax Staudt #include <linux/i2c-algo-pcf.h>
454768e90eSMax Staudt 
46cdb55539SMax Staudt #include <asm/amigahw.h>
474768e90eSMax Staudt #include <asm/amigaints.h>
484768e90eSMax Staudt #include <linux/zorro.h>
494768e90eSMax Staudt 
504768e90eSMax Staudt #include "../algos/i2c-algo-pcf.h"
514768e90eSMax Staudt 
524768e90eSMax Staudt struct icy_i2c {
534768e90eSMax Staudt 	struct i2c_adapter adapter;
544768e90eSMax Staudt 
554768e90eSMax Staudt 	void __iomem *reg_s0;
564768e90eSMax Staudt 	void __iomem *reg_s1;
57724041aeSMax Staudt 	struct i2c_client *ltc2990_client;
584768e90eSMax Staudt };
594768e90eSMax Staudt 
604768e90eSMax Staudt /*
614768e90eSMax Staudt  * Functions called by i2c-algo-pcf
624768e90eSMax Staudt  */
icy_pcf_setpcf(void * data,int ctl,int val)634768e90eSMax Staudt static void icy_pcf_setpcf(void *data, int ctl, int val)
644768e90eSMax Staudt {
654768e90eSMax Staudt 	struct icy_i2c *i2c = (struct icy_i2c *)data;
664768e90eSMax Staudt 
674768e90eSMax Staudt 	u8 __iomem *address = ctl ? i2c->reg_s1 : i2c->reg_s0;
684768e90eSMax Staudt 
694768e90eSMax Staudt 	z_writeb(val, address);
704768e90eSMax Staudt }
714768e90eSMax Staudt 
icy_pcf_getpcf(void * data,int ctl)724768e90eSMax Staudt static int icy_pcf_getpcf(void *data, int ctl)
734768e90eSMax Staudt {
744768e90eSMax Staudt 	struct icy_i2c *i2c = (struct icy_i2c *)data;
754768e90eSMax Staudt 
764768e90eSMax Staudt 	u8 __iomem *address = ctl ? i2c->reg_s1 : i2c->reg_s0;
774768e90eSMax Staudt 
784768e90eSMax Staudt 	return z_readb(address);
794768e90eSMax Staudt }
804768e90eSMax Staudt 
icy_pcf_getown(void * data)814768e90eSMax Staudt static int icy_pcf_getown(void *data)
824768e90eSMax Staudt {
834768e90eSMax Staudt 	return 0x55;
844768e90eSMax Staudt }
854768e90eSMax Staudt 
icy_pcf_getclock(void * data)864768e90eSMax Staudt static int icy_pcf_getclock(void *data)
874768e90eSMax Staudt {
884768e90eSMax Staudt 	return 0x1c;
894768e90eSMax Staudt }
904768e90eSMax Staudt 
icy_pcf_waitforpin(void * data)914768e90eSMax Staudt static void icy_pcf_waitforpin(void *data)
924768e90eSMax Staudt {
934768e90eSMax Staudt 	usleep_range(50, 150);
944768e90eSMax Staudt }
954768e90eSMax Staudt 
964768e90eSMax Staudt /*
974768e90eSMax Staudt  * Main i2c-icy part
984768e90eSMax Staudt  */
99724041aeSMax Staudt static unsigned short const icy_ltc2990_addresses[] = {
100724041aeSMax Staudt 	0x4c, 0x4d, 0x4e, 0x4f, I2C_CLIENT_END
101724041aeSMax Staudt };
102724041aeSMax Staudt 
103724041aeSMax Staudt /*
104724041aeSMax Staudt  * Additional sensors exposed once this property is applied:
105724041aeSMax Staudt  *
106724041aeSMax Staudt  * in1 will be the voltage of the 5V rail, divided by 2.
107724041aeSMax Staudt  * in2 will be the voltage of the 12V rail, divided by 4.
108724041aeSMax Staudt  * temp3 will be measured using a PCB loop next the chip.
109724041aeSMax Staudt  */
110724041aeSMax Staudt static const u32 icy_ltc2990_meas_mode[] = {0, 3};
111724041aeSMax Staudt 
112724041aeSMax Staudt static const struct property_entry icy_ltc2990_props[] = {
113724041aeSMax Staudt 	PROPERTY_ENTRY_U32_ARRAY("lltc,meas-mode", icy_ltc2990_meas_mode),
114724041aeSMax Staudt 	{ }
115724041aeSMax Staudt };
116724041aeSMax Staudt 
117dd7a3710SHeikki Krogerus static const struct software_node icy_ltc2990_node = {
118dd7a3710SHeikki Krogerus 	.properties = icy_ltc2990_props,
119dd7a3710SHeikki Krogerus };
120dd7a3710SHeikki Krogerus 
icy_probe(struct zorro_dev * z,const struct zorro_device_id * ent)1214768e90eSMax Staudt static int icy_probe(struct zorro_dev *z,
1224768e90eSMax Staudt 		     const struct zorro_device_id *ent)
1234768e90eSMax Staudt {
1244768e90eSMax Staudt 	struct icy_i2c *i2c;
1254768e90eSMax Staudt 	struct i2c_algo_pcf_data *algo_data;
126724041aeSMax Staudt 	struct i2c_board_info ltc2990_info = {
127724041aeSMax Staudt 		.type		= "ltc2990",
128dd7a3710SHeikki Krogerus 		.swnode		= &icy_ltc2990_node,
129724041aeSMax Staudt 	};
1304768e90eSMax Staudt 
1314768e90eSMax Staudt 	i2c = devm_kzalloc(&z->dev, sizeof(*i2c), GFP_KERNEL);
1324768e90eSMax Staudt 	if (!i2c)
1334768e90eSMax Staudt 		return -ENOMEM;
1344768e90eSMax Staudt 
1354768e90eSMax Staudt 	algo_data = devm_kzalloc(&z->dev, sizeof(*algo_data), GFP_KERNEL);
1364768e90eSMax Staudt 	if (!algo_data)
1374768e90eSMax Staudt 		return -ENOMEM;
1384768e90eSMax Staudt 
1394768e90eSMax Staudt 	dev_set_drvdata(&z->dev, i2c);
1404768e90eSMax Staudt 	i2c->adapter.dev.parent = &z->dev;
1414768e90eSMax Staudt 	i2c->adapter.owner = THIS_MODULE;
1424768e90eSMax Staudt 	/* i2c->adapter.algo assigned by i2c_pcf_add_bus() */
1434768e90eSMax Staudt 	i2c->adapter.algo_data = algo_data;
144*ea1558ceSWolfram Sang 	strscpy(i2c->adapter.name, "ICY I2C Zorro adapter",
1454768e90eSMax Staudt 		sizeof(i2c->adapter.name));
1464768e90eSMax Staudt 
1474768e90eSMax Staudt 	if (!devm_request_mem_region(&z->dev,
1484768e90eSMax Staudt 				     z->resource.start,
1494768e90eSMax Staudt 				     4, i2c->adapter.name))
1504768e90eSMax Staudt 		return -ENXIO;
1514768e90eSMax Staudt 
1524768e90eSMax Staudt 	/* Driver private data */
1534768e90eSMax Staudt 	i2c->reg_s0 = ZTWO_VADDR(z->resource.start);
1544768e90eSMax Staudt 	i2c->reg_s1 = ZTWO_VADDR(z->resource.start + 2);
1554768e90eSMax Staudt 
1564768e90eSMax Staudt 	algo_data->data = i2c;
1574768e90eSMax Staudt 	algo_data->setpcf     = icy_pcf_setpcf;
1584768e90eSMax Staudt 	algo_data->getpcf     = icy_pcf_getpcf;
1594768e90eSMax Staudt 	algo_data->getown     = icy_pcf_getown;
1604768e90eSMax Staudt 	algo_data->getclock   = icy_pcf_getclock;
1614768e90eSMax Staudt 	algo_data->waitforpin = icy_pcf_waitforpin;
1624768e90eSMax Staudt 
1634768e90eSMax Staudt 	if (i2c_pcf_add_bus(&i2c->adapter)) {
1644768e90eSMax Staudt 		dev_err(&z->dev, "i2c_pcf_add_bus() failed\n");
1654768e90eSMax Staudt 		return -ENXIO;
1664768e90eSMax Staudt 	}
1674768e90eSMax Staudt 
1684768e90eSMax Staudt 	dev_info(&z->dev, "ICY I2C controller at %pa, IRQ not implemented\n",
1694768e90eSMax Staudt 		 &z->resource.start);
1704768e90eSMax Staudt 
171724041aeSMax Staudt 	/*
172724041aeSMax Staudt 	 * The 2019 a1k.org PCBs have an LTC2990 at 0x4c, so start
173724041aeSMax Staudt 	 * it automatically once ltc2990 is modprobed.
174724041aeSMax Staudt 	 *
175724041aeSMax Staudt 	 * in0 is the voltage of the internal 5V power supply.
176724041aeSMax Staudt 	 * temp1 is the temperature inside the chip.
177724041aeSMax Staudt 	 *
178724041aeSMax Staudt 	 * See property_entry above for in1, in2, temp3.
179724041aeSMax Staudt 	 */
180dd7a3710SHeikki Krogerus 	i2c->ltc2990_client = i2c_new_scanned_device(&i2c->adapter,
181724041aeSMax Staudt 						     &ltc2990_info,
182724041aeSMax Staudt 						     icy_ltc2990_addresses,
183724041aeSMax Staudt 						     NULL);
1844768e90eSMax Staudt 	return 0;
1854768e90eSMax Staudt }
1864768e90eSMax Staudt 
icy_remove(struct zorro_dev * z)1874768e90eSMax Staudt static void icy_remove(struct zorro_dev *z)
1884768e90eSMax Staudt {
1894768e90eSMax Staudt 	struct icy_i2c *i2c = dev_get_drvdata(&z->dev);
1904768e90eSMax Staudt 
191724041aeSMax Staudt 	i2c_unregister_device(i2c->ltc2990_client);
1924768e90eSMax Staudt 	i2c_del_adapter(&i2c->adapter);
1934768e90eSMax Staudt }
1944768e90eSMax Staudt 
1954768e90eSMax Staudt static const struct zorro_device_id icy_zorro_tbl[] = {
1964768e90eSMax Staudt 	{ ZORRO_ID(VMC, 15, 0), },
1974768e90eSMax Staudt 	{ 0 }
1984768e90eSMax Staudt };
1994768e90eSMax Staudt 
2004768e90eSMax Staudt MODULE_DEVICE_TABLE(zorro, icy_zorro_tbl);
2014768e90eSMax Staudt 
2024768e90eSMax Staudt static struct zorro_driver icy_driver = {
2034768e90eSMax Staudt 	.name           = "i2c-icy",
2044768e90eSMax Staudt 	.id_table       = icy_zorro_tbl,
2054768e90eSMax Staudt 	.probe          = icy_probe,
2064768e90eSMax Staudt 	.remove         = icy_remove,
2074768e90eSMax Staudt };
2084768e90eSMax Staudt 
2094768e90eSMax Staudt module_driver(icy_driver,
2104768e90eSMax Staudt 	      zorro_register_driver,
2114768e90eSMax Staudt 	      zorro_unregister_driver);
2124768e90eSMax Staudt 
2134768e90eSMax Staudt MODULE_AUTHOR("Max Staudt <max@enpas.org>");
2144768e90eSMax Staudt MODULE_DESCRIPTION("I2C bus via PCF8584 on ICY Zorro card");
2154768e90eSMax Staudt MODULE_LICENSE("GPL v2");
216