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