ACPI: stop complaints about interrupt link End Tags and blank IRQ descriptors
[linux-2.6/mini2440.git] / drivers / w1 / slaves / w1_ds2760.c
blobed6b0576208c27ca237670999ba328a267b350f9
1 /*
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>
20 #include "../w1.h"
21 #include "../w1_int.h"
22 #include "../w1_family.h"
23 #include "w1_ds2760.h"
25 static int w1_ds2760_io(struct device *dev, char *buf, int addr, size_t count,
26 int io)
28 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
30 if (!dev)
31 return 0;
33 mutex_lock(&sl->master->mutex);
35 if (addr > DS2760_DATA_SIZE || addr < 0) {
36 count = 0;
37 goto out;
39 if (addr + count > DS2760_DATA_SIZE)
40 count = DS2760_DATA_SIZE - addr;
42 if (!w1_reset_select_slave(sl)) {
43 if (!io) {
44 w1_write_8(sl->master, W1_DS2760_READ_DATA);
45 w1_write_8(sl->master, addr);
46 count = w1_read_block(sl->master, buf, count);
47 } else {
48 w1_write_8(sl->master, W1_DS2760_WRITE_DATA);
49 w1_write_8(sl->master, addr);
50 w1_write_block(sl->master, buf, count);
51 /* XXX w1_write_block returns void, not n_written */
55 out:
56 mutex_unlock(&sl->master->mutex);
58 return count;
61 int w1_ds2760_read(struct device *dev, char *buf, int addr, size_t count)
63 return w1_ds2760_io(dev, buf, addr, count, 0);
66 int w1_ds2760_write(struct device *dev, char *buf, int addr, size_t count)
68 return w1_ds2760_io(dev, buf, addr, count, 1);
71 static ssize_t w1_ds2760_read_bin(struct kobject *kobj,
72 struct bin_attribute *bin_attr,
73 char *buf, loff_t off, size_t count)
75 struct device *dev = container_of(kobj, struct device, kobj);
76 return w1_ds2760_read(dev, buf, off, count);
79 static struct bin_attribute w1_ds2760_bin_attr = {
80 .attr = {
81 .name = "w1_slave",
82 .mode = S_IRUGO,
83 .owner = THIS_MODULE,
85 .size = DS2760_DATA_SIZE,
86 .read = w1_ds2760_read_bin,
89 static DEFINE_IDR(bat_idr);
90 static DEFINE_MUTEX(bat_idr_lock);
92 static int new_bat_id(void)
94 int ret;
96 while (1) {
97 int id;
99 ret = idr_pre_get(&bat_idr, GFP_KERNEL);
100 if (ret == 0)
101 return -ENOMEM;
103 mutex_lock(&bat_idr_lock);
104 ret = idr_get_new(&bat_idr, NULL, &id);
105 mutex_unlock(&bat_idr_lock);
107 if (ret == 0) {
108 ret = id & MAX_ID_MASK;
109 break;
110 } else if (ret == -EAGAIN) {
111 continue;
112 } else {
113 break;
117 return ret;
120 static void release_bat_id(int id)
122 mutex_lock(&bat_idr_lock);
123 idr_remove(&bat_idr, id);
124 mutex_unlock(&bat_idr_lock);
127 static int w1_ds2760_add_slave(struct w1_slave *sl)
129 int ret;
130 int id;
131 struct platform_device *pdev;
133 id = new_bat_id();
134 if (id < 0) {
135 ret = id;
136 goto noid;
139 pdev = platform_device_alloc("ds2760-battery", id);
140 if (!pdev) {
141 ret = -ENOMEM;
142 goto pdev_alloc_failed;
144 pdev->dev.parent = &sl->dev;
146 ret = platform_device_add(pdev);
147 if (ret)
148 goto pdev_add_failed;
150 ret = sysfs_create_bin_file(&sl->dev.kobj, &w1_ds2760_bin_attr);
151 if (ret)
152 goto bin_attr_failed;
154 dev_set_drvdata(&sl->dev, pdev);
156 goto success;
158 bin_attr_failed:
159 pdev_add_failed:
160 platform_device_unregister(pdev);
161 pdev_alloc_failed:
162 release_bat_id(id);
163 noid:
164 success:
165 return ret;
168 static void w1_ds2760_remove_slave(struct w1_slave *sl)
170 struct platform_device *pdev = dev_get_drvdata(&sl->dev);
171 int id = pdev->id;
173 platform_device_unregister(pdev);
174 release_bat_id(id);
175 sysfs_remove_bin_file(&sl->dev.kobj, &w1_ds2760_bin_attr);
178 static struct w1_family_ops w1_ds2760_fops = {
179 .add_slave = w1_ds2760_add_slave,
180 .remove_slave = w1_ds2760_remove_slave,
183 static struct w1_family w1_ds2760_family = {
184 .fid = W1_FAMILY_DS2760,
185 .fops = &w1_ds2760_fops,
188 static int __init w1_ds2760_init(void)
190 printk(KERN_INFO "1-Wire driver for the DS2760 battery monitor "
191 " chip - (c) 2004-2005, Szabolcs Gyurko\n");
192 idr_init(&bat_idr);
193 return w1_register_family(&w1_ds2760_family);
196 static void __exit w1_ds2760_exit(void)
198 w1_unregister_family(&w1_ds2760_family);
199 idr_destroy(&bat_idr);
202 EXPORT_SYMBOL(w1_ds2760_read);
203 EXPORT_SYMBOL(w1_ds2760_write);
205 module_init(w1_ds2760_init);
206 module_exit(w1_ds2760_exit);
208 MODULE_LICENSE("GPL");
209 MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>");
210 MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");