Revert "unicode: Don't special case ignorable code points"
[linux.git] / drivers / mfd / gateworks-gsc.c
bloba3301502ce6a5b4779f4211e0a3e9e657617fc55
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.
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>
18 #include <linux/of.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/regmap.h>
23 #include <linux/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.
30 #define I2C_RETRIES 3
32 int gsc_write(void *context, unsigned int reg, unsigned int val)
34 struct i2c_client *client = context;
35 int retry, ret;
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)
44 break;
47 return 0;
49 EXPORT_SYMBOL_GPL(gsc_write);
51 int gsc_read(void *context, unsigned int reg, unsigned int *val)
53 struct i2c_client *client = context;
54 int retry, ret;
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)
63 break;
65 *val = ret & 0xff;
67 return 0;
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)
78 int ret;
79 unsigned char regs[4];
81 dev_info(&gsc->i2c->dev, "GSC powerdown for %ld seconds\n",
82 secs);
84 put_unaligned_le32(secs, regs);
85 ret = regmap_bulk_write(gsc->regmap, GSC_TIME_ADD, regs, 4);
86 if (ret)
87 return ret;
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;
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));
102 return ret;
105 static ssize_t gsc_show(struct device *dev, struct device_attribute *attr,
106 char *buf)
108 struct gsc_dev *gsc = dev_get_drvdata(dev);
109 const char *name = attr->attr.name;
110 int rz = 0;
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);
119 return rz;
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;
127 long value;
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);
136 return count;
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[] = {
147 &attr_fwver.attr,
148 &attr_fwcrc.attr,
149 &attr_pwrdown.attr,
150 NULL,
153 static struct attribute_group attr_group = {
154 .attrs = gsc_attrs,
157 static const struct of_device_id gsc_of_match[] = {
158 { .compatible = "gw,gsc", },
161 MODULE_DEVICE_TABLE(of, gsc_of_match);
163 static const 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 = {
169 .reg_bits = 8,
170 .val_bits = 8,
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",
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,
197 static int gsc_probe(struct i2c_client *client)
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;
205 gsc = devm_kzalloc(dev, sizeof(*gsc), GFP_KERNEL);
206 if (!gsc)
207 return -ENOMEM;
209 gsc->dev = &client->dev;
210 gsc->i2c = client;
211 i2c_set_clientdata(client, gsc);
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);
218 if (regmap_read(gsc->regmap, GSC_FW_VER, &reg))
219 return -EIO;
220 gsc->fwver = reg;
222 regmap_read(gsc->regmap, GSC_FW_CRC, &reg);
223 gsc->fwcrc = reg;
224 regmap_read(gsc->regmap, GSC_FW_CRC + 1, &reg);
225 gsc->fwcrc |= reg << 8;
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);
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;
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);
245 if (ret)
246 dev_err(dev, "failed to create sysfs attrs\n");
248 ret = devm_of_platform_populate(dev);
249 if (ret) {
250 sysfs_remove_group(&dev->kobj, &attr_group);
251 return ret;
254 return 0;
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 = {
263 .driver = {
264 .name = "gateworks-gsc",
265 .of_match_table = gsc_of_match,
267 .probe = gsc_probe,
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");