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