18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * The Gateworks System Controller (GSC) is a multi-function 48c2ecf20Sopenharmony_ci * device designed for use in Gateworks Single Board Computers. 58c2ecf20Sopenharmony_ci * The control interface is I2C, with an interrupt. The device supports 68c2ecf20Sopenharmony_ci * system functions such as push-button monitoring, multiple ADC's for 78c2ecf20Sopenharmony_ci * voltage and temperature monitoring, fan controller and watchdog monitor. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Copyright (C) 2020 Gateworks Corporation 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/device.h> 138c2ecf20Sopenharmony_ci#include <linux/i2c.h> 148c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 158c2ecf20Sopenharmony_ci#include <linux/mfd/gsc.h> 168c2ecf20Sopenharmony_ci#include <linux/module.h> 178c2ecf20Sopenharmony_ci#include <linux/mutex.h> 188c2ecf20Sopenharmony_ci#include <linux/of.h> 198c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 208c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 218c2ecf20Sopenharmony_ci#include <linux/regmap.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* 268c2ecf20Sopenharmony_ci * The GSC suffers from an errata where occasionally during 278c2ecf20Sopenharmony_ci * ADC cycles the chip can NAK I2C transactions. To ensure we have reliable 288c2ecf20Sopenharmony_ci * register access we place retries around register access. 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_ci#define I2C_RETRIES 3 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ciint gsc_write(void *context, unsigned int reg, unsigned int val) 338c2ecf20Sopenharmony_ci{ 348c2ecf20Sopenharmony_ci struct i2c_client *client = context; 358c2ecf20Sopenharmony_ci int retry, ret; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci for (retry = 0; retry < I2C_RETRIES; retry++) { 388c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(client, reg, val); 398c2ecf20Sopenharmony_ci /* 408c2ecf20Sopenharmony_ci * -EAGAIN returned when the i2c host controller is busy 418c2ecf20Sopenharmony_ci * -EIO returned when i2c device is busy 428c2ecf20Sopenharmony_ci */ 438c2ecf20Sopenharmony_ci if (ret != -EAGAIN && ret != -EIO) 448c2ecf20Sopenharmony_ci break; 458c2ecf20Sopenharmony_ci } 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci return 0; 488c2ecf20Sopenharmony_ci} 498c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(gsc_write); 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ciint gsc_read(void *context, unsigned int reg, unsigned int *val) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci struct i2c_client *client = context; 548c2ecf20Sopenharmony_ci int retry, ret; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci for (retry = 0; retry < I2C_RETRIES; retry++) { 578c2ecf20Sopenharmony_ci ret = i2c_smbus_read_byte_data(client, reg); 588c2ecf20Sopenharmony_ci /* 598c2ecf20Sopenharmony_ci * -EAGAIN returned when the i2c host controller is busy 608c2ecf20Sopenharmony_ci * -EIO returned when i2c device is busy 618c2ecf20Sopenharmony_ci */ 628c2ecf20Sopenharmony_ci if (ret != -EAGAIN && ret != -EIO) 638c2ecf20Sopenharmony_ci break; 648c2ecf20Sopenharmony_ci } 658c2ecf20Sopenharmony_ci *val = ret & 0xff; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci return 0; 688c2ecf20Sopenharmony_ci} 698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(gsc_read); 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci/* 728c2ecf20Sopenharmony_ci * gsc_powerdown - API to use GSC to power down board for a specific time 738c2ecf20Sopenharmony_ci * 748c2ecf20Sopenharmony_ci * secs - number of seconds to remain powered off 758c2ecf20Sopenharmony_ci */ 768c2ecf20Sopenharmony_cistatic int gsc_powerdown(struct gsc_dev *gsc, unsigned long secs) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci int ret; 798c2ecf20Sopenharmony_ci unsigned char regs[4]; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci dev_info(&gsc->i2c->dev, "GSC powerdown for %ld seconds\n", 828c2ecf20Sopenharmony_ci secs); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci put_unaligned_le32(secs, regs); 858c2ecf20Sopenharmony_ci ret = regmap_bulk_write(gsc->regmap, GSC_TIME_ADD, regs, 4); 868c2ecf20Sopenharmony_ci if (ret) 878c2ecf20Sopenharmony_ci return ret; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci ret = regmap_update_bits(gsc->regmap, GSC_CTRL_1, 908c2ecf20Sopenharmony_ci BIT(GSC_CTRL_1_SLEEP_ADD), 918c2ecf20Sopenharmony_ci BIT(GSC_CTRL_1_SLEEP_ADD)); 928c2ecf20Sopenharmony_ci if (ret) 938c2ecf20Sopenharmony_ci return ret; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci ret = regmap_update_bits(gsc->regmap, GSC_CTRL_1, 968c2ecf20Sopenharmony_ci BIT(GSC_CTRL_1_SLEEP_ACTIVATE) | 978c2ecf20Sopenharmony_ci BIT(GSC_CTRL_1_SLEEP_ENABLE), 988c2ecf20Sopenharmony_ci BIT(GSC_CTRL_1_SLEEP_ACTIVATE) | 998c2ecf20Sopenharmony_ci BIT(GSC_CTRL_1_SLEEP_ENABLE)); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci return ret; 1038c2ecf20Sopenharmony_ci} 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_cistatic ssize_t gsc_show(struct device *dev, struct device_attribute *attr, 1068c2ecf20Sopenharmony_ci char *buf) 1078c2ecf20Sopenharmony_ci{ 1088c2ecf20Sopenharmony_ci struct gsc_dev *gsc = dev_get_drvdata(dev); 1098c2ecf20Sopenharmony_ci const char *name = attr->attr.name; 1108c2ecf20Sopenharmony_ci int rz = 0; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci if (strcasecmp(name, "fw_version") == 0) 1138c2ecf20Sopenharmony_ci rz = sprintf(buf, "%d\n", gsc->fwver); 1148c2ecf20Sopenharmony_ci else if (strcasecmp(name, "fw_crc") == 0) 1158c2ecf20Sopenharmony_ci rz = sprintf(buf, "0x%04x\n", gsc->fwcrc); 1168c2ecf20Sopenharmony_ci else 1178c2ecf20Sopenharmony_ci dev_err(dev, "invalid command: '%s'\n", name); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci return rz; 1208c2ecf20Sopenharmony_ci} 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_cistatic ssize_t gsc_store(struct device *dev, struct device_attribute *attr, 1238c2ecf20Sopenharmony_ci const char *buf, size_t count) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci struct gsc_dev *gsc = dev_get_drvdata(dev); 1268c2ecf20Sopenharmony_ci const char *name = attr->attr.name; 1278c2ecf20Sopenharmony_ci long value; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci if (strcasecmp(name, "powerdown") == 0) { 1308c2ecf20Sopenharmony_ci if (kstrtol(buf, 0, &value) == 0) 1318c2ecf20Sopenharmony_ci gsc_powerdown(gsc, value); 1328c2ecf20Sopenharmony_ci } else { 1338c2ecf20Sopenharmony_ci dev_err(dev, "invalid command: '%s\n", name); 1348c2ecf20Sopenharmony_ci } 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci return count; 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic struct device_attribute attr_fwver = 1408c2ecf20Sopenharmony_ci __ATTR(fw_version, 0440, gsc_show, NULL); 1418c2ecf20Sopenharmony_cistatic struct device_attribute attr_fwcrc = 1428c2ecf20Sopenharmony_ci __ATTR(fw_crc, 0440, gsc_show, NULL); 1438c2ecf20Sopenharmony_cistatic struct device_attribute attr_pwrdown = 1448c2ecf20Sopenharmony_ci __ATTR(powerdown, 0220, NULL, gsc_store); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_cistatic struct attribute *gsc_attrs[] = { 1478c2ecf20Sopenharmony_ci &attr_fwver.attr, 1488c2ecf20Sopenharmony_ci &attr_fwcrc.attr, 1498c2ecf20Sopenharmony_ci &attr_pwrdown.attr, 1508c2ecf20Sopenharmony_ci NULL, 1518c2ecf20Sopenharmony_ci}; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistatic struct attribute_group attr_group = { 1548c2ecf20Sopenharmony_ci .attrs = gsc_attrs, 1558c2ecf20Sopenharmony_ci}; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic const struct of_device_id gsc_of_match[] = { 1588c2ecf20Sopenharmony_ci { .compatible = "gw,gsc", }, 1598c2ecf20Sopenharmony_ci { } 1608c2ecf20Sopenharmony_ci}; 1618c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, gsc_of_match); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_cistatic struct regmap_bus gsc_regmap_bus = { 1648c2ecf20Sopenharmony_ci .reg_read = gsc_read, 1658c2ecf20Sopenharmony_ci .reg_write = gsc_write, 1668c2ecf20Sopenharmony_ci}; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_cistatic const struct regmap_config gsc_regmap_config = { 1698c2ecf20Sopenharmony_ci .reg_bits = 8, 1708c2ecf20Sopenharmony_ci .val_bits = 8, 1718c2ecf20Sopenharmony_ci .cache_type = REGCACHE_NONE, 1728c2ecf20Sopenharmony_ci .max_register = GSC_WP, 1738c2ecf20Sopenharmony_ci}; 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_cistatic const struct regmap_irq gsc_irqs[] = { 1768c2ecf20Sopenharmony_ci REGMAP_IRQ_REG(GSC_IRQ_PB, 0, BIT(GSC_IRQ_PB)), 1778c2ecf20Sopenharmony_ci REGMAP_IRQ_REG(GSC_IRQ_KEY_ERASED, 0, BIT(GSC_IRQ_KEY_ERASED)), 1788c2ecf20Sopenharmony_ci REGMAP_IRQ_REG(GSC_IRQ_EEPROM_WP, 0, BIT(GSC_IRQ_EEPROM_WP)), 1798c2ecf20Sopenharmony_ci REGMAP_IRQ_REG(GSC_IRQ_RESV, 0, BIT(GSC_IRQ_RESV)), 1808c2ecf20Sopenharmony_ci REGMAP_IRQ_REG(GSC_IRQ_GPIO, 0, BIT(GSC_IRQ_GPIO)), 1818c2ecf20Sopenharmony_ci REGMAP_IRQ_REG(GSC_IRQ_TAMPER, 0, BIT(GSC_IRQ_TAMPER)), 1828c2ecf20Sopenharmony_ci REGMAP_IRQ_REG(GSC_IRQ_WDT_TIMEOUT, 0, BIT(GSC_IRQ_WDT_TIMEOUT)), 1838c2ecf20Sopenharmony_ci REGMAP_IRQ_REG(GSC_IRQ_SWITCH_HOLD, 0, BIT(GSC_IRQ_SWITCH_HOLD)), 1848c2ecf20Sopenharmony_ci}; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_cistatic const struct regmap_irq_chip gsc_irq_chip = { 1878c2ecf20Sopenharmony_ci .name = "gateworks-gsc", 1888c2ecf20Sopenharmony_ci .irqs = gsc_irqs, 1898c2ecf20Sopenharmony_ci .num_irqs = ARRAY_SIZE(gsc_irqs), 1908c2ecf20Sopenharmony_ci .num_regs = 1, 1918c2ecf20Sopenharmony_ci .status_base = GSC_IRQ_STATUS, 1928c2ecf20Sopenharmony_ci .mask_base = GSC_IRQ_ENABLE, 1938c2ecf20Sopenharmony_ci .mask_invert = true, 1948c2ecf20Sopenharmony_ci .ack_base = GSC_IRQ_STATUS, 1958c2ecf20Sopenharmony_ci .ack_invert = true, 1968c2ecf20Sopenharmony_ci}; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_cistatic int gsc_probe(struct i2c_client *client) 1998c2ecf20Sopenharmony_ci{ 2008c2ecf20Sopenharmony_ci struct device *dev = &client->dev; 2018c2ecf20Sopenharmony_ci struct gsc_dev *gsc; 2028c2ecf20Sopenharmony_ci struct regmap_irq_chip_data *irq_data; 2038c2ecf20Sopenharmony_ci int ret; 2048c2ecf20Sopenharmony_ci unsigned int reg; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci gsc = devm_kzalloc(dev, sizeof(*gsc), GFP_KERNEL); 2078c2ecf20Sopenharmony_ci if (!gsc) 2088c2ecf20Sopenharmony_ci return -ENOMEM; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci gsc->dev = &client->dev; 2118c2ecf20Sopenharmony_ci gsc->i2c = client; 2128c2ecf20Sopenharmony_ci i2c_set_clientdata(client, gsc); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci gsc->regmap = devm_regmap_init(dev, &gsc_regmap_bus, client, 2158c2ecf20Sopenharmony_ci &gsc_regmap_config); 2168c2ecf20Sopenharmony_ci if (IS_ERR(gsc->regmap)) 2178c2ecf20Sopenharmony_ci return PTR_ERR(gsc->regmap); 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci if (regmap_read(gsc->regmap, GSC_FW_VER, ®)) 2208c2ecf20Sopenharmony_ci return -EIO; 2218c2ecf20Sopenharmony_ci gsc->fwver = reg; 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci regmap_read(gsc->regmap, GSC_FW_CRC, ®); 2248c2ecf20Sopenharmony_ci gsc->fwcrc = reg; 2258c2ecf20Sopenharmony_ci regmap_read(gsc->regmap, GSC_FW_CRC + 1, ®); 2268c2ecf20Sopenharmony_ci gsc->fwcrc |= reg << 8; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci gsc->i2c_hwmon = devm_i2c_new_dummy_device(dev, client->adapter, 2298c2ecf20Sopenharmony_ci GSC_HWMON); 2308c2ecf20Sopenharmony_ci if (IS_ERR(gsc->i2c_hwmon)) { 2318c2ecf20Sopenharmony_ci dev_err(dev, "Failed to allocate I2C device for HWMON\n"); 2328c2ecf20Sopenharmony_ci return PTR_ERR(gsc->i2c_hwmon); 2338c2ecf20Sopenharmony_ci } 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci ret = devm_regmap_add_irq_chip(dev, gsc->regmap, client->irq, 2368c2ecf20Sopenharmony_ci IRQF_ONESHOT | IRQF_SHARED | 2378c2ecf20Sopenharmony_ci IRQF_TRIGGER_LOW, 0, 2388c2ecf20Sopenharmony_ci &gsc_irq_chip, &irq_data); 2398c2ecf20Sopenharmony_ci if (ret) 2408c2ecf20Sopenharmony_ci return ret; 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci dev_info(dev, "Gateworks System Controller v%d: fw 0x%04x\n", 2438c2ecf20Sopenharmony_ci gsc->fwver, gsc->fwcrc); 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci ret = sysfs_create_group(&dev->kobj, &attr_group); 2468c2ecf20Sopenharmony_ci if (ret) 2478c2ecf20Sopenharmony_ci dev_err(dev, "failed to create sysfs attrs\n"); 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci ret = devm_of_platform_populate(dev); 2508c2ecf20Sopenharmony_ci if (ret) { 2518c2ecf20Sopenharmony_ci sysfs_remove_group(&dev->kobj, &attr_group); 2528c2ecf20Sopenharmony_ci return ret; 2538c2ecf20Sopenharmony_ci } 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci return 0; 2568c2ecf20Sopenharmony_ci} 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_cistatic int gsc_remove(struct i2c_client *client) 2598c2ecf20Sopenharmony_ci{ 2608c2ecf20Sopenharmony_ci sysfs_remove_group(&client->dev.kobj, &attr_group); 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci return 0; 2638c2ecf20Sopenharmony_ci} 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_cistatic struct i2c_driver gsc_driver = { 2668c2ecf20Sopenharmony_ci .driver = { 2678c2ecf20Sopenharmony_ci .name = "gateworks-gsc", 2688c2ecf20Sopenharmony_ci .of_match_table = gsc_of_match, 2698c2ecf20Sopenharmony_ci }, 2708c2ecf20Sopenharmony_ci .probe_new = gsc_probe, 2718c2ecf20Sopenharmony_ci .remove = gsc_remove, 2728c2ecf20Sopenharmony_ci}; 2738c2ecf20Sopenharmony_cimodule_i2c_driver(gsc_driver); 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ciMODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>"); 2768c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("I2C Core interface for GSC"); 2778c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 278