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 46*cdb55539SMax 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 fwnode_handle *ltc2990_fwnode; 58724041aeSMax Staudt struct i2c_client *ltc2990_client; 594768e90eSMax Staudt }; 604768e90eSMax Staudt 614768e90eSMax Staudt /* 624768e90eSMax Staudt * Functions called by i2c-algo-pcf 634768e90eSMax Staudt */ 644768e90eSMax Staudt static void icy_pcf_setpcf(void *data, int ctl, int val) 654768e90eSMax Staudt { 664768e90eSMax Staudt struct icy_i2c *i2c = (struct icy_i2c *)data; 674768e90eSMax Staudt 684768e90eSMax Staudt u8 __iomem *address = ctl ? i2c->reg_s1 : i2c->reg_s0; 694768e90eSMax Staudt 704768e90eSMax Staudt z_writeb(val, address); 714768e90eSMax Staudt } 724768e90eSMax Staudt 734768e90eSMax Staudt static int icy_pcf_getpcf(void *data, int ctl) 744768e90eSMax Staudt { 754768e90eSMax Staudt struct icy_i2c *i2c = (struct icy_i2c *)data; 764768e90eSMax Staudt 774768e90eSMax Staudt u8 __iomem *address = ctl ? i2c->reg_s1 : i2c->reg_s0; 784768e90eSMax Staudt 794768e90eSMax Staudt return z_readb(address); 804768e90eSMax Staudt } 814768e90eSMax Staudt 824768e90eSMax Staudt static int icy_pcf_getown(void *data) 834768e90eSMax Staudt { 844768e90eSMax Staudt return 0x55; 854768e90eSMax Staudt } 864768e90eSMax Staudt 874768e90eSMax Staudt static int icy_pcf_getclock(void *data) 884768e90eSMax Staudt { 894768e90eSMax Staudt return 0x1c; 904768e90eSMax Staudt } 914768e90eSMax Staudt 924768e90eSMax Staudt static void icy_pcf_waitforpin(void *data) 934768e90eSMax Staudt { 944768e90eSMax Staudt usleep_range(50, 150); 954768e90eSMax Staudt } 964768e90eSMax Staudt 974768e90eSMax Staudt /* 984768e90eSMax Staudt * Main i2c-icy part 994768e90eSMax Staudt */ 100724041aeSMax Staudt static unsigned short const icy_ltc2990_addresses[] = { 101724041aeSMax Staudt 0x4c, 0x4d, 0x4e, 0x4f, I2C_CLIENT_END 102724041aeSMax Staudt }; 103724041aeSMax Staudt 104724041aeSMax Staudt /* 105724041aeSMax Staudt * Additional sensors exposed once this property is applied: 106724041aeSMax Staudt * 107724041aeSMax Staudt * in1 will be the voltage of the 5V rail, divided by 2. 108724041aeSMax Staudt * in2 will be the voltage of the 12V rail, divided by 4. 109724041aeSMax Staudt * temp3 will be measured using a PCB loop next the chip. 110724041aeSMax Staudt */ 111724041aeSMax Staudt static const u32 icy_ltc2990_meas_mode[] = {0, 3}; 112724041aeSMax Staudt 113724041aeSMax Staudt static const struct property_entry icy_ltc2990_props[] = { 114724041aeSMax Staudt PROPERTY_ENTRY_U32_ARRAY("lltc,meas-mode", icy_ltc2990_meas_mode), 115724041aeSMax Staudt { } 116724041aeSMax Staudt }; 117724041aeSMax Staudt 1184768e90eSMax Staudt static int icy_probe(struct zorro_dev *z, 1194768e90eSMax Staudt const struct zorro_device_id *ent) 1204768e90eSMax Staudt { 1214768e90eSMax Staudt struct icy_i2c *i2c; 1224768e90eSMax Staudt struct i2c_algo_pcf_data *algo_data; 123724041aeSMax Staudt struct fwnode_handle *new_fwnode; 124724041aeSMax Staudt struct i2c_board_info ltc2990_info = { 125724041aeSMax Staudt .type = "ltc2990", 126724041aeSMax Staudt }; 1274768e90eSMax Staudt 1284768e90eSMax Staudt i2c = devm_kzalloc(&z->dev, sizeof(*i2c), GFP_KERNEL); 1294768e90eSMax Staudt if (!i2c) 1304768e90eSMax Staudt return -ENOMEM; 1314768e90eSMax Staudt 1324768e90eSMax Staudt algo_data = devm_kzalloc(&z->dev, sizeof(*algo_data), GFP_KERNEL); 1334768e90eSMax Staudt if (!algo_data) 1344768e90eSMax Staudt return -ENOMEM; 1354768e90eSMax Staudt 1364768e90eSMax Staudt dev_set_drvdata(&z->dev, i2c); 1374768e90eSMax Staudt i2c->adapter.dev.parent = &z->dev; 1384768e90eSMax Staudt i2c->adapter.owner = THIS_MODULE; 1394768e90eSMax Staudt /* i2c->adapter.algo assigned by i2c_pcf_add_bus() */ 1404768e90eSMax Staudt i2c->adapter.algo_data = algo_data; 1414768e90eSMax Staudt strlcpy(i2c->adapter.name, "ICY I2C Zorro adapter", 1424768e90eSMax Staudt sizeof(i2c->adapter.name)); 1434768e90eSMax Staudt 1444768e90eSMax Staudt if (!devm_request_mem_region(&z->dev, 1454768e90eSMax Staudt z->resource.start, 1464768e90eSMax Staudt 4, i2c->adapter.name)) 1474768e90eSMax Staudt return -ENXIO; 1484768e90eSMax Staudt 1494768e90eSMax Staudt /* Driver private data */ 1504768e90eSMax Staudt i2c->reg_s0 = ZTWO_VADDR(z->resource.start); 1514768e90eSMax Staudt i2c->reg_s1 = ZTWO_VADDR(z->resource.start + 2); 1524768e90eSMax Staudt 1534768e90eSMax Staudt algo_data->data = i2c; 1544768e90eSMax Staudt algo_data->setpcf = icy_pcf_setpcf; 1554768e90eSMax Staudt algo_data->getpcf = icy_pcf_getpcf; 1564768e90eSMax Staudt algo_data->getown = icy_pcf_getown; 1574768e90eSMax Staudt algo_data->getclock = icy_pcf_getclock; 1584768e90eSMax Staudt algo_data->waitforpin = icy_pcf_waitforpin; 1594768e90eSMax Staudt 1604768e90eSMax Staudt if (i2c_pcf_add_bus(&i2c->adapter)) { 1614768e90eSMax Staudt dev_err(&z->dev, "i2c_pcf_add_bus() failed\n"); 1624768e90eSMax Staudt return -ENXIO; 1634768e90eSMax Staudt } 1644768e90eSMax Staudt 1654768e90eSMax Staudt dev_info(&z->dev, "ICY I2C controller at %pa, IRQ not implemented\n", 1664768e90eSMax Staudt &z->resource.start); 1674768e90eSMax Staudt 168724041aeSMax Staudt /* 169724041aeSMax Staudt * The 2019 a1k.org PCBs have an LTC2990 at 0x4c, so start 170724041aeSMax Staudt * it automatically once ltc2990 is modprobed. 171724041aeSMax Staudt * 172724041aeSMax Staudt * in0 is the voltage of the internal 5V power supply. 173724041aeSMax Staudt * temp1 is the temperature inside the chip. 174724041aeSMax Staudt * 175724041aeSMax Staudt * See property_entry above for in1, in2, temp3. 176724041aeSMax Staudt */ 177724041aeSMax Staudt new_fwnode = fwnode_create_software_node(icy_ltc2990_props, NULL); 178724041aeSMax Staudt if (IS_ERR(new_fwnode)) { 179724041aeSMax Staudt dev_info(&z->dev, "Failed to create fwnode for LTC2990, error: %ld\n", 180724041aeSMax Staudt PTR_ERR(new_fwnode)); 181724041aeSMax Staudt } else { 182724041aeSMax Staudt /* 183724041aeSMax Staudt * Store the fwnode so we can destroy it on .remove(). 184724041aeSMax Staudt * Only store it on success, as fwnode_remove_software_node() 185724041aeSMax Staudt * is NULL safe, but not PTR_ERR safe. 186724041aeSMax Staudt */ 187724041aeSMax Staudt i2c->ltc2990_fwnode = new_fwnode; 188724041aeSMax Staudt ltc2990_info.fwnode = new_fwnode; 189724041aeSMax Staudt 190724041aeSMax Staudt i2c->ltc2990_client = 191ce668524SWolfram Sang i2c_new_scanned_device(&i2c->adapter, 192724041aeSMax Staudt <c2990_info, 193724041aeSMax Staudt icy_ltc2990_addresses, 194724041aeSMax Staudt NULL); 195724041aeSMax Staudt } 196724041aeSMax Staudt 1974768e90eSMax Staudt return 0; 1984768e90eSMax Staudt } 1994768e90eSMax Staudt 2004768e90eSMax Staudt static void icy_remove(struct zorro_dev *z) 2014768e90eSMax Staudt { 2024768e90eSMax Staudt struct icy_i2c *i2c = dev_get_drvdata(&z->dev); 2034768e90eSMax Staudt 204724041aeSMax Staudt i2c_unregister_device(i2c->ltc2990_client); 205724041aeSMax Staudt fwnode_remove_software_node(i2c->ltc2990_fwnode); 206724041aeSMax Staudt 2074768e90eSMax Staudt i2c_del_adapter(&i2c->adapter); 2084768e90eSMax Staudt } 2094768e90eSMax Staudt 2104768e90eSMax Staudt static const struct zorro_device_id icy_zorro_tbl[] = { 2114768e90eSMax Staudt { ZORRO_ID(VMC, 15, 0), }, 2124768e90eSMax Staudt { 0 } 2134768e90eSMax Staudt }; 2144768e90eSMax Staudt 2154768e90eSMax Staudt MODULE_DEVICE_TABLE(zorro, icy_zorro_tbl); 2164768e90eSMax Staudt 2174768e90eSMax Staudt static struct zorro_driver icy_driver = { 2184768e90eSMax Staudt .name = "i2c-icy", 2194768e90eSMax Staudt .id_table = icy_zorro_tbl, 2204768e90eSMax Staudt .probe = icy_probe, 2214768e90eSMax Staudt .remove = icy_remove, 2224768e90eSMax Staudt }; 2234768e90eSMax Staudt 2244768e90eSMax Staudt module_driver(icy_driver, 2254768e90eSMax Staudt zorro_register_driver, 2264768e90eSMax Staudt zorro_unregister_driver); 2274768e90eSMax Staudt 2284768e90eSMax Staudt MODULE_AUTHOR("Max Staudt <max@enpas.org>"); 2294768e90eSMax Staudt MODULE_DESCRIPTION("I2C bus via PCF8584 on ICY Zorro card"); 2304768e90eSMax Staudt MODULE_LICENSE("GPL v2"); 231