Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Dallas Semiconductor DS1682 Elapsed Time Recorder device driver |
| 4 | * |
| 5 | * Written by: Grant Likely <grant.likely@secretlab.ca> |
| 6 | * |
| 7 | * Copyright (C) 2007 Secret Lab Technologies Ltd. |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * The DS1682 elapsed timer recorder is a simple device that implements |
| 12 | * one elapsed time counter, one event counter, an alarm signal and 10 |
| 13 | * bytes of general purpose EEPROM. |
| 14 | * |
| 15 | * This driver provides access to the DS1682 counters and user data via |
| 16 | * the sysfs. The following attributes are added to the device node: |
| 17 | * elapsed_time (u32): Total elapsed event time in ms resolution |
| 18 | * alarm_time (u32): When elapsed time exceeds the value in alarm_time, |
| 19 | * then the alarm pin is asserted. |
| 20 | * event_count (u16): number of times the event pin has gone low. |
| 21 | * eeprom (u8[10]): general purpose EEPROM |
| 22 | * |
| 23 | * Counter registers and user data are both read/write unless the device |
| 24 | * has been write protected. This driver does not support turning off write |
| 25 | * protection. Once write protection is turned on, it is impossible to |
| 26 | * turn it off again, so I have left the feature out of this driver to avoid |
| 27 | * accidental enabling, but it is trivial to add write protect support. |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | #include <linux/module.h> |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 32 | #include <linux/i2c.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/list.h> |
| 35 | #include <linux/sysfs.h> |
| 36 | #include <linux/ctype.h> |
| 37 | #include <linux/hwmon-sysfs.h> |
| 38 | |
| 39 | /* Device registers */ |
| 40 | #define DS1682_REG_CONFIG 0x00 |
| 41 | #define DS1682_REG_ALARM 0x01 |
| 42 | #define DS1682_REG_ELAPSED 0x05 |
| 43 | #define DS1682_REG_EVT_CNTR 0x09 |
| 44 | #define DS1682_REG_EEPROM 0x0b |
| 45 | #define DS1682_REG_RESET 0x1d |
| 46 | #define DS1682_REG_WRITE_DISABLE 0x1e |
| 47 | #define DS1682_REG_WRITE_MEM_DISABLE 0x1f |
| 48 | |
| 49 | #define DS1682_EEPROM_SIZE 10 |
| 50 | |
| 51 | /* |
| 52 | * Generic counter attributes |
| 53 | */ |
| 54 | static ssize_t ds1682_show(struct device *dev, struct device_attribute *attr, |
| 55 | char *buf) |
| 56 | { |
| 57 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 58 | struct i2c_client *client = to_i2c_client(dev); |
Thomas VanSelus | 066ed42 | 2017-12-22 10:51:23 -0600 | [diff] [blame] | 59 | unsigned long long val, check; |
Aaron Sierra | 2fa065f | 2017-12-22 10:51:09 -0600 | [diff] [blame] | 60 | __le32 val_le = 0; |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 61 | int rc; |
| 62 | |
| 63 | dev_dbg(dev, "ds1682_show() called on %s\n", attr->attr.name); |
| 64 | |
| 65 | /* Read the register */ |
| 66 | rc = i2c_smbus_read_i2c_block_data(client, sattr->index, sattr->nr, |
Aaron Sierra | 2fa065f | 2017-12-22 10:51:09 -0600 | [diff] [blame] | 67 | (u8 *)&val_le); |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 68 | if (rc < 0) |
| 69 | return -EIO; |
| 70 | |
Aaron Sierra | 2fa065f | 2017-12-22 10:51:09 -0600 | [diff] [blame] | 71 | val = le32_to_cpu(val_le); |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 72 | |
Thomas VanSelus | 066ed42 | 2017-12-22 10:51:23 -0600 | [diff] [blame] | 73 | if (sattr->index == DS1682_REG_ELAPSED) { |
| 74 | int retries = 5; |
| 75 | |
| 76 | /* Detect and retry when a tick occurs mid-read */ |
| 77 | do { |
| 78 | rc = i2c_smbus_read_i2c_block_data(client, sattr->index, |
| 79 | sattr->nr, |
| 80 | (u8 *)&val_le); |
| 81 | if (rc < 0 || retries <= 0) |
| 82 | return -EIO; |
| 83 | |
| 84 | check = val; |
| 85 | val = le32_to_cpu(val_le); |
| 86 | retries--; |
| 87 | } while (val != check && val != (check + 1)); |
| 88 | } |
| 89 | |
Aaron Sierra | 2fa065f | 2017-12-22 10:51:09 -0600 | [diff] [blame] | 90 | /* Format the output string and return # of bytes |
| 91 | * Special case: the 32 bit regs are time values with 1/4s |
| 92 | * resolution, scale them up to milliseconds |
| 93 | */ |
| 94 | return sprintf(buf, "%llu\n", (sattr->nr == 4) ? (val * 250) : val); |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static ssize_t ds1682_store(struct device *dev, struct device_attribute *attr, |
| 98 | const char *buf, size_t count) |
| 99 | { |
| 100 | struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr); |
| 101 | struct i2c_client *client = to_i2c_client(dev); |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 102 | u64 val; |
| 103 | __le32 val_le; |
| 104 | int rc; |
| 105 | |
| 106 | dev_dbg(dev, "ds1682_store() called on %s\n", attr->attr.name); |
| 107 | |
| 108 | /* Decode input */ |
Sebastien Bourdelin | 4030074 | 2014-04-01 11:04:28 -0400 | [diff] [blame] | 109 | rc = kstrtoull(buf, 0, &val); |
| 110 | if (rc < 0) { |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 111 | dev_dbg(dev, "input string not a number\n"); |
| 112 | return -EINVAL; |
| 113 | } |
| 114 | |
| 115 | /* Special case: the 32 bit regs are time values with 1/4s |
| 116 | * resolution, scale input down to quarter-seconds */ |
| 117 | if (sattr->nr == 4) |
| 118 | do_div(val, 250); |
| 119 | |
| 120 | /* write out the value */ |
| 121 | val_le = cpu_to_le32(val); |
| 122 | rc = i2c_smbus_write_i2c_block_data(client, sattr->index, sattr->nr, |
| 123 | (u8 *) & val_le); |
| 124 | if (rc < 0) { |
| 125 | dev_err(dev, "register write failed; reg=0x%x, size=%i\n", |
| 126 | sattr->index, sattr->nr); |
| 127 | return -EIO; |
| 128 | } |
| 129 | |
| 130 | return count; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Simple register attributes |
| 135 | */ |
| 136 | static SENSOR_DEVICE_ATTR_2(elapsed_time, S_IRUGO | S_IWUSR, ds1682_show, |
| 137 | ds1682_store, 4, DS1682_REG_ELAPSED); |
| 138 | static SENSOR_DEVICE_ATTR_2(alarm_time, S_IRUGO | S_IWUSR, ds1682_show, |
| 139 | ds1682_store, 4, DS1682_REG_ALARM); |
| 140 | static SENSOR_DEVICE_ATTR_2(event_count, S_IRUGO | S_IWUSR, ds1682_show, |
| 141 | ds1682_store, 2, DS1682_REG_EVT_CNTR); |
| 142 | |
| 143 | static const struct attribute_group ds1682_group = { |
| 144 | .attrs = (struct attribute *[]) { |
| 145 | &sensor_dev_attr_elapsed_time.dev_attr.attr, |
| 146 | &sensor_dev_attr_alarm_time.dev_attr.attr, |
| 147 | &sensor_dev_attr_event_count.dev_attr.attr, |
| 148 | NULL, |
| 149 | }, |
| 150 | }; |
| 151 | |
| 152 | /* |
| 153 | * User data attribute |
| 154 | */ |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 155 | static ssize_t ds1682_eeprom_read(struct file *filp, struct kobject *kobj, |
| 156 | struct bin_attribute *attr, |
Al Viro | 05bd711 | 2007-07-15 21:01:22 +0100 | [diff] [blame] | 157 | char *buf, loff_t off, size_t count) |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 158 | { |
| 159 | struct i2c_client *client = kobj_to_i2c_client(kobj); |
| 160 | int rc; |
| 161 | |
| 162 | dev_dbg(&client->dev, "ds1682_eeprom_read(p=%p, off=%lli, c=%zi)\n", |
| 163 | buf, off, count); |
| 164 | |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 165 | rc = i2c_smbus_read_i2c_block_data(client, DS1682_REG_EEPROM + off, |
| 166 | count, buf); |
| 167 | if (rc < 0) |
| 168 | return -EIO; |
| 169 | |
| 170 | return count; |
| 171 | } |
| 172 | |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 173 | static ssize_t ds1682_eeprom_write(struct file *filp, struct kobject *kobj, |
| 174 | struct bin_attribute *attr, |
Al Viro | 05bd711 | 2007-07-15 21:01:22 +0100 | [diff] [blame] | 175 | char *buf, loff_t off, size_t count) |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 176 | { |
| 177 | struct i2c_client *client = kobj_to_i2c_client(kobj); |
| 178 | |
| 179 | dev_dbg(&client->dev, "ds1682_eeprom_write(p=%p, off=%lli, c=%zi)\n", |
| 180 | buf, off, count); |
| 181 | |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 182 | /* Write out to the device */ |
| 183 | if (i2c_smbus_write_i2c_block_data(client, DS1682_REG_EEPROM + off, |
| 184 | count, buf) < 0) |
| 185 | return -EIO; |
| 186 | |
| 187 | return count; |
| 188 | } |
| 189 | |
Bhumika Goyal | 57daedf | 2017-08-02 14:40:06 +0530 | [diff] [blame] | 190 | static const struct bin_attribute ds1682_eeprom_attr = { |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 191 | .attr = { |
| 192 | .name = "eeprom", |
| 193 | .mode = S_IRUGO | S_IWUSR, |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 194 | }, |
| 195 | .size = DS1682_EEPROM_SIZE, |
| 196 | .read = ds1682_eeprom_read, |
| 197 | .write = ds1682_eeprom_write, |
| 198 | }; |
| 199 | |
| 200 | /* |
| 201 | * Called when a ds1682 device is matched with this driver |
| 202 | */ |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 203 | static int ds1682_probe(struct i2c_client *client, |
| 204 | const struct i2c_device_id *id) |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 205 | { |
| 206 | int rc; |
| 207 | |
| 208 | if (!i2c_check_functionality(client->adapter, |
| 209 | I2C_FUNC_SMBUS_I2C_BLOCK)) { |
| 210 | dev_err(&client->dev, "i2c bus does not support the ds1682\n"); |
| 211 | rc = -ENODEV; |
| 212 | goto exit; |
| 213 | } |
| 214 | |
| 215 | rc = sysfs_create_group(&client->dev.kobj, &ds1682_group); |
| 216 | if (rc) |
| 217 | goto exit; |
| 218 | |
| 219 | rc = sysfs_create_bin_file(&client->dev.kobj, &ds1682_eeprom_attr); |
| 220 | if (rc) |
| 221 | goto exit_bin_attr; |
| 222 | |
| 223 | return 0; |
| 224 | |
| 225 | exit_bin_attr: |
| 226 | sysfs_remove_group(&client->dev.kobj, &ds1682_group); |
| 227 | exit: |
| 228 | return rc; |
| 229 | } |
| 230 | |
| 231 | static int ds1682_remove(struct i2c_client *client) |
| 232 | { |
| 233 | sysfs_remove_bin_file(&client->dev.kobj, &ds1682_eeprom_attr); |
| 234 | sysfs_remove_group(&client->dev.kobj, &ds1682_group); |
| 235 | return 0; |
| 236 | } |
| 237 | |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 238 | static const struct i2c_device_id ds1682_id[] = { |
| 239 | { "ds1682", 0 }, |
| 240 | { } |
| 241 | }; |
| 242 | MODULE_DEVICE_TABLE(i2c, ds1682_id); |
| 243 | |
Javier Martinez Canillas | 5ef1685 | 2017-03-21 10:50:48 -0300 | [diff] [blame] | 244 | static const struct of_device_id ds1682_of_match[] = { |
| 245 | { .compatible = "dallas,ds1682", }, |
| 246 | {} |
| 247 | }; |
| 248 | MODULE_DEVICE_TABLE(of, ds1682_of_match); |
| 249 | |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 250 | static struct i2c_driver ds1682_driver = { |
| 251 | .driver = { |
| 252 | .name = "ds1682", |
Javier Martinez Canillas | 5ef1685 | 2017-03-21 10:50:48 -0300 | [diff] [blame] | 253 | .of_match_table = ds1682_of_match, |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 254 | }, |
| 255 | .probe = ds1682_probe, |
| 256 | .remove = ds1682_remove, |
Jean Delvare | 3760f73 | 2008-04-29 23:11:40 +0200 | [diff] [blame] | 257 | .id_table = ds1682_id, |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 258 | }; |
| 259 | |
Axel Lin | a64fe2e | 2012-01-22 15:36:45 +0800 | [diff] [blame] | 260 | module_i2c_driver(ds1682_driver); |
Grant Likely | 5162b75 | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 261 | |
| 262 | MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>"); |
| 263 | MODULE_DESCRIPTION("DS1682 Elapsed Time Indicator driver"); |
| 264 | MODULE_LICENSE("GPL"); |