2 * 1-Wire implementation for the ds2760 chip
4 * Copyright © 2004-2005, Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
6 * Use consistent with the GNU GPL is permitted,
7 * provided that this copyright notice is
8 * preserved in its entirety in all copies and derived works.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/types.h>
16 #include <linux/platform_device.h>
17 #include <linux/mutex.h>
18 #include <linux/idr.h>
19 #include <linux/gfp.h>
23 #include "w1_ds2760.h"
25 #define W1_FAMILY_DS2760 0x30
27 static int w1_ds2760_io(struct device *dev, char *buf, int addr, size_t count,
30 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
35 mutex_lock(&sl->master->bus_mutex);
37 if (addr > DS2760_DATA_SIZE || addr < 0) {
41 if (addr + count > DS2760_DATA_SIZE)
42 count = DS2760_DATA_SIZE - addr;
44 if (!w1_reset_select_slave(sl)) {
46 w1_write_8(sl->master, W1_DS2760_READ_DATA);
47 w1_write_8(sl->master, addr);
48 count = w1_read_block(sl->master, buf, count);
50 w1_write_8(sl->master, W1_DS2760_WRITE_DATA);
51 w1_write_8(sl->master, addr);
52 w1_write_block(sl->master, buf, count);
53 /* XXX w1_write_block returns void, not n_written */
58 mutex_unlock(&sl->master->bus_mutex);
63 int w1_ds2760_read(struct device *dev, char *buf, int addr, size_t count)
65 return w1_ds2760_io(dev, buf, addr, count, 0);
67 EXPORT_SYMBOL(w1_ds2760_read);
69 int w1_ds2760_write(struct device *dev, char *buf, int addr, size_t count)
71 return w1_ds2760_io(dev, buf, addr, count, 1);
73 EXPORT_SYMBOL(w1_ds2760_write);
75 static int w1_ds2760_eeprom_cmd(struct device *dev, int addr, int cmd)
77 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
82 mutex_lock(&sl->master->bus_mutex);
84 if (w1_reset_select_slave(sl) == 0) {
85 w1_write_8(sl->master, cmd);
86 w1_write_8(sl->master, addr);
89 mutex_unlock(&sl->master->bus_mutex);
93 int w1_ds2760_store_eeprom(struct device *dev, int addr)
95 return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_COPY_DATA);
97 EXPORT_SYMBOL(w1_ds2760_store_eeprom);
99 int w1_ds2760_recall_eeprom(struct device *dev, int addr)
101 return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_RECALL_DATA);
103 EXPORT_SYMBOL(w1_ds2760_recall_eeprom);
105 static ssize_t w1_slave_read(struct file *filp, struct kobject *kobj,
106 struct bin_attribute *bin_attr, char *buf,
107 loff_t off, size_t count)
109 struct device *dev = container_of(kobj, struct device, kobj);
110 return w1_ds2760_read(dev, buf, off, count);
113 static BIN_ATTR_RO(w1_slave, DS2760_DATA_SIZE);
115 static struct bin_attribute *w1_ds2760_bin_attrs[] = {
120 static const struct attribute_group w1_ds2760_group = {
121 .bin_attrs = w1_ds2760_bin_attrs,
124 static const struct attribute_group *w1_ds2760_groups[] = {
129 static int w1_ds2760_add_slave(struct w1_slave *sl)
132 struct platform_device *pdev;
134 pdev = platform_device_alloc("ds2760-battery", PLATFORM_DEVID_AUTO);
137 pdev->dev.parent = &sl->dev;
139 ret = platform_device_add(pdev);
141 goto pdev_add_failed;
143 dev_set_drvdata(&sl->dev, pdev);
148 platform_device_put(pdev);
153 static void w1_ds2760_remove_slave(struct w1_slave *sl)
155 struct platform_device *pdev = dev_get_drvdata(&sl->dev);
157 platform_device_unregister(pdev);
160 static struct w1_family_ops w1_ds2760_fops = {
161 .add_slave = w1_ds2760_add_slave,
162 .remove_slave = w1_ds2760_remove_slave,
163 .groups = w1_ds2760_groups,
166 static struct w1_family w1_ds2760_family = {
167 .fid = W1_FAMILY_DS2760,
168 .fops = &w1_ds2760_fops,
170 module_w1_family(w1_ds2760_family);
172 MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>");
173 MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");
174 MODULE_LICENSE("GPL");
175 MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2760));