1 // SPDX-License-Identifier: GPL-2.0
2 /*
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.
8 *
9 * Copyright (C) 2020 Gateworks Corporation
10 */
11
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>
18 #include <linux/of.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
22
23 #include <linux/unaligned.h>
24
25 /*
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.
29 */
30 #define I2C_RETRIES 3
31
gsc_write(void * context,unsigned int reg,unsigned int val)32 int gsc_write(void *context, unsigned int reg, unsigned int val)
33 {
34 struct i2c_client *client = context;
35 int retry, ret;
36
37 for (retry = 0; retry < I2C_RETRIES; retry++) {
38 ret = i2c_smbus_write_byte_data(client, reg, val);
39 /*
40 * -EAGAIN returned when the i2c host controller is busy
41 * -EIO returned when i2c device is busy
42 */
43 if (ret != -EAGAIN && ret != -EIO)
44 break;
45 }
46
47 return 0;
48 }
49 EXPORT_SYMBOL_GPL(gsc_write);
50
gsc_read(void * context,unsigned int reg,unsigned int * val)51 int gsc_read(void *context, unsigned int reg, unsigned int *val)
52 {
53 struct i2c_client *client = context;
54 int retry, ret;
55
56 for (retry = 0; retry < I2C_RETRIES; retry++) {
57 ret = i2c_smbus_read_byte_data(client, reg);
58 /*
59 * -EAGAIN returned when the i2c host controller is busy
60 * -EIO returned when i2c device is busy
61 */
62 if (ret != -EAGAIN && ret != -EIO)
63 break;
64 }
65 *val = ret & 0xff;
66
67 return 0;
68 }
69 EXPORT_SYMBOL_GPL(gsc_read);
70
71 /*
72 * gsc_powerdown - API to use GSC to power down board for a specific time
73 *
74 * secs - number of seconds to remain powered off
75 */
gsc_powerdown(struct gsc_dev * gsc,unsigned long secs)76 static int gsc_powerdown(struct gsc_dev *gsc, unsigned long secs)
77 {
78 int ret;
79 unsigned char regs[4];
80
81 dev_info(&gsc->i2c->dev, "GSC powerdown for %ld seconds\n",
82 secs);
83
84 put_unaligned_le32(secs, regs);
85 ret = regmap_bulk_write(gsc->regmap, GSC_TIME_ADD, regs, 4);
86 if (ret)
87 return ret;
88
89 ret = regmap_update_bits(gsc->regmap, GSC_CTRL_1,
90 BIT(GSC_CTRL_1_SLEEP_ADD),
91 BIT(GSC_CTRL_1_SLEEP_ADD));
92 if (ret)
93 return ret;
94
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));
100
101
102 return ret;
103 }
104
gsc_show(struct device * dev,struct device_attribute * attr,char * buf)105 static ssize_t gsc_show(struct device *dev, struct device_attribute *attr,
106 char *buf)
107 {
108 struct gsc_dev *gsc = dev_get_drvdata(dev);
109 const char *name = attr->attr.name;
110 int rz = 0;
111
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);
116 else
117 dev_err(dev, "invalid command: '%s'\n", name);
118
119 return rz;
120 }
121
gsc_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)122 static ssize_t gsc_store(struct device *dev, struct device_attribute *attr,
123 const char *buf, size_t count)
124 {
125 struct gsc_dev *gsc = dev_get_drvdata(dev);
126 const char *name = attr->attr.name;
127 long value;
128
129 if (strcasecmp(name, "powerdown") == 0) {
130 if (kstrtol(buf, 0, &value) == 0)
131 gsc_powerdown(gsc, value);
132 } else {
133 dev_err(dev, "invalid command: '%s\n", name);
134 }
135
136 return count;
137 }
138
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);
145
146 static struct attribute *gsc_attrs[] = {
147 &attr_fwver.attr,
148 &attr_fwcrc.attr,
149 &attr_pwrdown.attr,
150 NULL,
151 };
152
153 static struct attribute_group attr_group = {
154 .attrs = gsc_attrs,
155 };
156
157 static const struct of_device_id gsc_of_match[] = {
158 { .compatible = "gw,gsc", },
159 { }
160 };
161 MODULE_DEVICE_TABLE(of, gsc_of_match);
162
163 static const struct regmap_bus gsc_regmap_bus = {
164 .reg_read = gsc_read,
165 .reg_write = gsc_write,
166 };
167
168 static const struct regmap_config gsc_regmap_config = {
169 .reg_bits = 8,
170 .val_bits = 8,
171 .cache_type = REGCACHE_NONE,
172 .max_register = GSC_WP,
173 };
174
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)),
184 };
185
186 static const struct regmap_irq_chip gsc_irq_chip = {
187 .name = "gateworks-gsc",
188 .irqs = gsc_irqs,
189 .num_irqs = ARRAY_SIZE(gsc_irqs),
190 .num_regs = 1,
191 .status_base = GSC_IRQ_STATUS,
192 .unmask_base = GSC_IRQ_ENABLE,
193 .ack_base = GSC_IRQ_STATUS,
194 .ack_invert = true,
195 };
196
gsc_probe(struct i2c_client * client)197 static int gsc_probe(struct i2c_client *client)
198 {
199 struct device *dev = &client->dev;
200 struct gsc_dev *gsc;
201 struct regmap_irq_chip_data *irq_data;
202 int ret;
203 unsigned int reg;
204
205 gsc = devm_kzalloc(dev, sizeof(*gsc), GFP_KERNEL);
206 if (!gsc)
207 return -ENOMEM;
208
209 gsc->dev = &client->dev;
210 gsc->i2c = client;
211 i2c_set_clientdata(client, gsc);
212
213 gsc->regmap = devm_regmap_init(dev, &gsc_regmap_bus, client,
214 &gsc_regmap_config);
215 if (IS_ERR(gsc->regmap))
216 return PTR_ERR(gsc->regmap);
217
218 if (regmap_read(gsc->regmap, GSC_FW_VER, ®))
219 return -EIO;
220 gsc->fwver = reg;
221
222 regmap_read(gsc->regmap, GSC_FW_CRC, ®);
223 gsc->fwcrc = reg;
224 regmap_read(gsc->regmap, GSC_FW_CRC + 1, ®);
225 gsc->fwcrc |= reg << 8;
226
227 gsc->i2c_hwmon = devm_i2c_new_dummy_device(dev, client->adapter,
228 GSC_HWMON);
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);
232 }
233
234 ret = devm_regmap_add_irq_chip(dev, gsc->regmap, client->irq,
235 IRQF_ONESHOT | IRQF_SHARED |
236 IRQF_TRIGGER_LOW, 0,
237 &gsc_irq_chip, &irq_data);
238 if (ret)
239 return ret;
240
241 dev_info(dev, "Gateworks System Controller v%d: fw 0x%04x\n",
242 gsc->fwver, gsc->fwcrc);
243
244 ret = sysfs_create_group(&dev->kobj, &attr_group);
245 if (ret)
246 dev_err(dev, "failed to create sysfs attrs\n");
247
248 ret = devm_of_platform_populate(dev);
249 if (ret) {
250 sysfs_remove_group(&dev->kobj, &attr_group);
251 return ret;
252 }
253
254 return 0;
255 }
256
gsc_remove(struct i2c_client * client)257 static void gsc_remove(struct i2c_client *client)
258 {
259 sysfs_remove_group(&client->dev.kobj, &attr_group);
260 }
261
262 static struct i2c_driver gsc_driver = {
263 .driver = {
264 .name = "gateworks-gsc",
265 .of_match_table = gsc_of_match,
266 },
267 .probe = gsc_probe,
268 .remove = gsc_remove,
269 };
270 module_i2c_driver(gsc_driver);
271
272 MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>");
273 MODULE_DESCRIPTION("I2C Core interface for GSC");
274 MODULE_LICENSE("GPL v2");
275