1 // SPDX-License-Identifier: GPL-2.0
3 * The Gateworks System Controller (GSC) is a multi-function
4 * device designed for use in Gateworks Single Board Computers.
5 * The control interface is I2C, with an interrupt. The device supports
6 * system functions such as push-button monitoring, multiple ADC's for
7 * voltage and temperature monitoring, fan controller and watchdog monitor.
9 * Copyright (C) 2020 Gateworks Corporation
12 #include <linux/device.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/mfd/gsc.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
23 #include <asm/unaligned.h>
26 * The GSC suffers from an errata where occasionally during
27 * ADC cycles the chip can NAK I2C transactions. To ensure we have reliable
28 * register access we place retries around register access.
32 int gsc_write(void *context, unsigned int reg, unsigned int val)
34 struct i2c_client *client = context;
37 for (retry = 0; retry < I2C_RETRIES; retry++) {
38 ret = i2c_smbus_write_byte_data(client, reg, val);
40 * -EAGAIN returned when the i2c host controller is busy
41 * -EIO returned when i2c device is busy
43 if (ret != -EAGAIN && ret != -EIO)
49 EXPORT_SYMBOL_GPL(gsc_write);
51 int gsc_read(void *context, unsigned int reg, unsigned int *val)
53 struct i2c_client *client = context;
56 for (retry = 0; retry < I2C_RETRIES; retry++) {
57 ret = i2c_smbus_read_byte_data(client, reg);
59 * -EAGAIN returned when the i2c host controller is busy
60 * -EIO returned when i2c device is busy
62 if (ret != -EAGAIN && ret != -EIO)
69 EXPORT_SYMBOL_GPL(gsc_read);
72 * gsc_powerdown - API to use GSC to power down board for a specific time
74 * secs - number of seconds to remain powered off
76 static int gsc_powerdown(struct gsc_dev *gsc, unsigned long secs)
79 unsigned char regs[4];
81 dev_info(&gsc->i2c->dev, "GSC powerdown for %ld seconds\n",
84 put_unaligned_le32(secs, regs);
85 ret = regmap_bulk_write(gsc->regmap, GSC_TIME_ADD, regs, 4);
89 ret = regmap_update_bits(gsc->regmap, GSC_CTRL_1,
90 BIT(GSC_CTRL_1_SLEEP_ADD),
91 BIT(GSC_CTRL_1_SLEEP_ADD));
95 ret = regmap_update_bits(gsc->regmap, GSC_CTRL_1,
96 BIT(GSC_CTRL_1_SLEEP_ACTIVATE) |
97 BIT(GSC_CTRL_1_SLEEP_ENABLE),
98 BIT(GSC_CTRL_1_SLEEP_ACTIVATE) |
99 BIT(GSC_CTRL_1_SLEEP_ENABLE));
105 static ssize_t gsc_show(struct device *dev, struct device_attribute *attr,
108 struct gsc_dev *gsc = dev_get_drvdata(dev);
109 const char *name = attr->attr.name;
112 if (strcasecmp(name, "fw_version") == 0)
113 rz = sprintf(buf, "%d\n", gsc->fwver);
114 else if (strcasecmp(name, "fw_crc") == 0)
115 rz = sprintf(buf, "0x%04x\n", gsc->fwcrc);
117 dev_err(dev, "invalid command: '%s'\n", name);
122 static ssize_t gsc_store(struct device *dev, struct device_attribute *attr,
123 const char *buf, size_t count)
125 struct gsc_dev *gsc = dev_get_drvdata(dev);
126 const char *name = attr->attr.name;
129 if (strcasecmp(name, "powerdown") == 0) {
130 if (kstrtol(buf, 0, &value) == 0)
131 gsc_powerdown(gsc, value);
133 dev_err(dev, "invalid command: '%s\n", name);
139 static struct device_attribute attr_fwver =
140 __ATTR(fw_version, 0440, gsc_show, NULL);
141 static struct device_attribute attr_fwcrc =
142 __ATTR(fw_crc, 0440, gsc_show, NULL);
143 static struct device_attribute attr_pwrdown =
144 __ATTR(powerdown, 0220, NULL, gsc_store);
146 static struct attribute *gsc_attrs[] = {
153 static struct attribute_group attr_group = {
157 static const struct of_device_id gsc_of_match[] = {
158 { .compatible = "gw,gsc", },
161 MODULE_DEVICE_TABLE(of, gsc_of_match);
163 static struct regmap_bus gsc_regmap_bus = {
164 .reg_read = gsc_read,
165 .reg_write = gsc_write,
168 static const struct regmap_config gsc_regmap_config = {
171 .cache_type = REGCACHE_NONE,
172 .max_register = GSC_WP,
175 static const struct regmap_irq gsc_irqs[] = {
176 REGMAP_IRQ_REG(GSC_IRQ_PB, 0, BIT(GSC_IRQ_PB)),
177 REGMAP_IRQ_REG(GSC_IRQ_KEY_ERASED, 0, BIT(GSC_IRQ_KEY_ERASED)),
178 REGMAP_IRQ_REG(GSC_IRQ_EEPROM_WP, 0, BIT(GSC_IRQ_EEPROM_WP)),
179 REGMAP_IRQ_REG(GSC_IRQ_RESV, 0, BIT(GSC_IRQ_RESV)),
180 REGMAP_IRQ_REG(GSC_IRQ_GPIO, 0, BIT(GSC_IRQ_GPIO)),
181 REGMAP_IRQ_REG(GSC_IRQ_TAMPER, 0, BIT(GSC_IRQ_TAMPER)),
182 REGMAP_IRQ_REG(GSC_IRQ_WDT_TIMEOUT, 0, BIT(GSC_IRQ_WDT_TIMEOUT)),
183 REGMAP_IRQ_REG(GSC_IRQ_SWITCH_HOLD, 0, BIT(GSC_IRQ_SWITCH_HOLD)),
186 static const struct regmap_irq_chip gsc_irq_chip = {
187 .name = "gateworks-gsc",
189 .num_irqs = ARRAY_SIZE(gsc_irqs),
191 .status_base = GSC_IRQ_STATUS,
192 .unmask_base = GSC_IRQ_ENABLE,
193 .ack_base = GSC_IRQ_STATUS,
197 static int gsc_probe(struct i2c_client *client)
199 struct device *dev = &client->dev;
201 struct regmap_irq_chip_data *irq_data;
205 gsc = devm_kzalloc(dev, sizeof(*gsc), GFP_KERNEL);
209 gsc->dev = &client->dev;
211 i2c_set_clientdata(client, gsc);
213 gsc->regmap = devm_regmap_init(dev, &gsc_regmap_bus, client,
215 if (IS_ERR(gsc->regmap))
216 return PTR_ERR(gsc->regmap);
218 if (regmap_read(gsc->regmap, GSC_FW_VER, ®))
222 regmap_read(gsc->regmap, GSC_FW_CRC, ®);
224 regmap_read(gsc->regmap, GSC_FW_CRC + 1, ®);
225 gsc->fwcrc |= reg << 8;
227 gsc->i2c_hwmon = devm_i2c_new_dummy_device(dev, client->adapter,
229 if (IS_ERR(gsc->i2c_hwmon)) {
230 dev_err(dev, "Failed to allocate I2C device for HWMON\n");
231 return PTR_ERR(gsc->i2c_hwmon);
234 ret = devm_regmap_add_irq_chip(dev, gsc->regmap, client->irq,
235 IRQF_ONESHOT | IRQF_SHARED |
237 &gsc_irq_chip, &irq_data);
241 dev_info(dev, "Gateworks System Controller v%d: fw 0x%04x\n",
242 gsc->fwver, gsc->fwcrc);
244 ret = sysfs_create_group(&dev->kobj, &attr_group);
246 dev_err(dev, "failed to create sysfs attrs\n");
248 ret = devm_of_platform_populate(dev);
250 sysfs_remove_group(&dev->kobj, &attr_group);
257 static void gsc_remove(struct i2c_client *client)
259 sysfs_remove_group(&client->dev.kobj, &attr_group);
262 static struct i2c_driver gsc_driver = {
264 .name = "gateworks-gsc",
265 .of_match_table = gsc_of_match,
268 .remove = gsc_remove,
270 module_i2c_driver(gsc_driver);
272 MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>");
273 MODULE_DESCRIPTION("I2C Core interface for GSC");
274 MODULE_LICENSE("GPL v2");