w1: ds2760: add support for EEPROM read and write
[deliverable/linux.git] / drivers / w1 / slaves / w1_ds2760.c
CommitLineData
d7ce6d1d
AV
1/*
2 * 1-Wire implementation for the ds2760 chip
3 *
4 * Copyright © 2004-2005, Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
5 *
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.
9 *
10 */
11
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
20#include "../w1.h"
21#include "../w1_int.h"
22#include "../w1_family.h"
23#include "w1_ds2760.h"
24
25static int w1_ds2760_io(struct device *dev, char *buf, int addr, size_t count,
26 int io)
27{
28 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
29
30 if (!dev)
31 return 0;
32
33 mutex_lock(&sl->master->mutex);
34
35 if (addr > DS2760_DATA_SIZE || addr < 0) {
36 count = 0;
37 goto out;
38 }
39 if (addr + count > DS2760_DATA_SIZE)
40 count = DS2760_DATA_SIZE - addr;
41
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 */
52 }
53 }
54
55out:
56 mutex_unlock(&sl->master->mutex);
57
58 return count;
59}
60
61int w1_ds2760_read(struct device *dev, char *buf, int addr, size_t count)
62{
63 return w1_ds2760_io(dev, buf, addr, count, 0);
64}
65
66int w1_ds2760_write(struct device *dev, char *buf, int addr, size_t count)
67{
68 return w1_ds2760_io(dev, buf, addr, count, 1);
69}
70
0b47b570
DM
71static int w1_ds2760_eeprom_cmd(struct device *dev, int addr, int cmd)
72{
73 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
74
75 if (!dev)
76 return -EINVAL;
77
78 mutex_lock(&sl->master->mutex);
79
80 if (w1_reset_select_slave(sl) == 0) {
81 w1_write_8(sl->master, cmd);
82 w1_write_8(sl->master, addr);
83 }
84
85 mutex_unlock(&sl->master->mutex);
86 return 0;
87}
88
89int w1_ds2760_store_eeprom(struct device *dev, int addr)
90{
91 return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_COPY_DATA);
92}
93
94int w1_ds2760_recall_eeprom(struct device *dev, int addr)
95{
96 return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_RECALL_DATA);
97}
98
0909fca5
AM
99static ssize_t w1_ds2760_read_bin(struct kobject *kobj,
100 struct bin_attribute *bin_attr,
101 char *buf, loff_t off, size_t count)
d7ce6d1d
AV
102{
103 struct device *dev = container_of(kobj, struct device, kobj);
104 return w1_ds2760_read(dev, buf, off, count);
105}
106
107static struct bin_attribute w1_ds2760_bin_attr = {
108 .attr = {
109 .name = "w1_slave",
110 .mode = S_IRUGO,
d7ce6d1d
AV
111 },
112 .size = DS2760_DATA_SIZE,
113 .read = w1_ds2760_read_bin,
114};
115
116static DEFINE_IDR(bat_idr);
117static DEFINE_MUTEX(bat_idr_lock);
118
119static int new_bat_id(void)
120{
121 int ret;
122
123 while (1) {
124 int id;
125
126 ret = idr_pre_get(&bat_idr, GFP_KERNEL);
127 if (ret == 0)
128 return -ENOMEM;
129
130 mutex_lock(&bat_idr_lock);
131 ret = idr_get_new(&bat_idr, NULL, &id);
132 mutex_unlock(&bat_idr_lock);
133
134 if (ret == 0) {
135 ret = id & MAX_ID_MASK;
136 break;
137 } else if (ret == -EAGAIN) {
138 continue;
139 } else {
140 break;
141 }
142 }
143
144 return ret;
145}
146
147static void release_bat_id(int id)
148{
149 mutex_lock(&bat_idr_lock);
150 idr_remove(&bat_idr, id);
151 mutex_unlock(&bat_idr_lock);
d7ce6d1d
AV
152}
153
154static int w1_ds2760_add_slave(struct w1_slave *sl)
155{
156 int ret;
157 int id;
158 struct platform_device *pdev;
159
160 id = new_bat_id();
161 if (id < 0) {
162 ret = id;
163 goto noid;
164 }
165
166 pdev = platform_device_alloc("ds2760-battery", id);
167 if (!pdev) {
168 ret = -ENOMEM;
169 goto pdev_alloc_failed;
170 }
171 pdev->dev.parent = &sl->dev;
172
173 ret = platform_device_add(pdev);
174 if (ret)
175 goto pdev_add_failed;
176
177 ret = sysfs_create_bin_file(&sl->dev.kobj, &w1_ds2760_bin_attr);
178 if (ret)
179 goto bin_attr_failed;
180
181 dev_set_drvdata(&sl->dev, pdev);
182
183 goto success;
184
185bin_attr_failed:
186pdev_add_failed:
187 platform_device_unregister(pdev);
188pdev_alloc_failed:
189 release_bat_id(id);
190noid:
191success:
192 return ret;
193}
194
195static void w1_ds2760_remove_slave(struct w1_slave *sl)
196{
197 struct platform_device *pdev = dev_get_drvdata(&sl->dev);
198 int id = pdev->id;
199
200 platform_device_unregister(pdev);
201 release_bat_id(id);
202 sysfs_remove_bin_file(&sl->dev.kobj, &w1_ds2760_bin_attr);
d7ce6d1d
AV
203}
204
205static struct w1_family_ops w1_ds2760_fops = {
206 .add_slave = w1_ds2760_add_slave,
207 .remove_slave = w1_ds2760_remove_slave,
208};
209
210static struct w1_family w1_ds2760_family = {
211 .fid = W1_FAMILY_DS2760,
212 .fops = &w1_ds2760_fops,
213};
214
215static int __init w1_ds2760_init(void)
216{
217 printk(KERN_INFO "1-Wire driver for the DS2760 battery monitor "
218 " chip - (c) 2004-2005, Szabolcs Gyurko\n");
219 idr_init(&bat_idr);
220 return w1_register_family(&w1_ds2760_family);
221}
222
223static void __exit w1_ds2760_exit(void)
224{
225 w1_unregister_family(&w1_ds2760_family);
226 idr_destroy(&bat_idr);
227}
228
229EXPORT_SYMBOL(w1_ds2760_read);
230EXPORT_SYMBOL(w1_ds2760_write);
0b47b570
DM
231EXPORT_SYMBOL(w1_ds2760_store_eeprom);
232EXPORT_SYMBOL(w1_ds2760_recall_eeprom);
d7ce6d1d
AV
233
234module_init(w1_ds2760_init);
235module_exit(w1_ds2760_exit);
236
237MODULE_LICENSE("GPL");
238MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>");
239MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");
This page took 0.234138 seconds and 5 git commands to generate.