qlcnic: add beacon test support.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / w1 / slaves / w1_ds2780.c
blob274c8f38303f5748478c47309c6e255b5c69ead1
1 /*
2 * 1-Wire implementation for the ds2780 chip
4 * Copyright (C) 2010 Indesign, LLC
6 * Author: Clifton Barnes <cabarnes@indesign-llc.com>
8 * Based on w1-ds2760 driver
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/types.h>
20 #include <linux/platform_device.h>
21 #include <linux/mutex.h>
22 #include <linux/idr.h>
24 #include "../w1.h"
25 #include "../w1_int.h"
26 #include "../w1_family.h"
27 #include "w1_ds2780.h"
29 int w1_ds2780_io(struct device *dev, char *buf, int addr, size_t count,
30 int io)
32 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
34 if (!dev)
35 return -ENODEV;
37 mutex_lock(&sl->master->mutex);
39 if (addr > DS2780_DATA_SIZE || addr < 0) {
40 count = 0;
41 goto out;
43 count = min_t(int, count, DS2780_DATA_SIZE - addr);
45 if (w1_reset_select_slave(sl) == 0) {
46 if (io) {
47 w1_write_8(sl->master, W1_DS2780_WRITE_DATA);
48 w1_write_8(sl->master, addr);
49 w1_write_block(sl->master, buf, count);
50 /* XXX w1_write_block returns void, not n_written */
51 } else {
52 w1_write_8(sl->master, W1_DS2780_READ_DATA);
53 w1_write_8(sl->master, addr);
54 count = w1_read_block(sl->master, buf, count);
58 out:
59 mutex_unlock(&sl->master->mutex);
61 return count;
63 EXPORT_SYMBOL(w1_ds2780_io);
65 int w1_ds2780_eeprom_cmd(struct device *dev, int addr, int cmd)
67 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
69 if (!dev)
70 return -EINVAL;
72 mutex_lock(&sl->master->mutex);
74 if (w1_reset_select_slave(sl) == 0) {
75 w1_write_8(sl->master, cmd);
76 w1_write_8(sl->master, addr);
79 mutex_unlock(&sl->master->mutex);
80 return 0;
82 EXPORT_SYMBOL(w1_ds2780_eeprom_cmd);
84 static ssize_t w1_ds2780_read_bin(struct file *filp,
85 struct kobject *kobj,
86 struct bin_attribute *bin_attr,
87 char *buf, loff_t off, size_t count)
89 struct device *dev = container_of(kobj, struct device, kobj);
90 return w1_ds2780_io(dev, buf, off, count, 0);
93 static struct bin_attribute w1_ds2780_bin_attr = {
94 .attr = {
95 .name = "w1_slave",
96 .mode = S_IRUGO,
98 .size = DS2780_DATA_SIZE,
99 .read = w1_ds2780_read_bin,
102 static DEFINE_IDR(bat_idr);
103 static DEFINE_MUTEX(bat_idr_lock);
105 static int new_bat_id(void)
107 int ret;
109 while (1) {
110 int id;
112 ret = idr_pre_get(&bat_idr, GFP_KERNEL);
113 if (ret == 0)
114 return -ENOMEM;
116 mutex_lock(&bat_idr_lock);
117 ret = idr_get_new(&bat_idr, NULL, &id);
118 mutex_unlock(&bat_idr_lock);
120 if (ret == 0) {
121 ret = id & MAX_ID_MASK;
122 break;
123 } else if (ret == -EAGAIN) {
124 continue;
125 } else {
126 break;
130 return ret;
133 static void release_bat_id(int id)
135 mutex_lock(&bat_idr_lock);
136 idr_remove(&bat_idr, id);
137 mutex_unlock(&bat_idr_lock);
140 static int w1_ds2780_add_slave(struct w1_slave *sl)
142 int ret;
143 int id;
144 struct platform_device *pdev;
146 id = new_bat_id();
147 if (id < 0) {
148 ret = id;
149 goto noid;
152 pdev = platform_device_alloc("ds2780-battery", id);
153 if (!pdev) {
154 ret = -ENOMEM;
155 goto pdev_alloc_failed;
157 pdev->dev.parent = &sl->dev;
159 ret = platform_device_add(pdev);
160 if (ret)
161 goto pdev_add_failed;
163 ret = sysfs_create_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr);
164 if (ret)
165 goto bin_attr_failed;
167 dev_set_drvdata(&sl->dev, pdev);
169 return 0;
171 bin_attr_failed:
172 pdev_add_failed:
173 platform_device_unregister(pdev);
174 pdev_alloc_failed:
175 release_bat_id(id);
176 noid:
177 return ret;
180 static void w1_ds2780_remove_slave(struct w1_slave *sl)
182 struct platform_device *pdev = dev_get_drvdata(&sl->dev);
183 int id = pdev->id;
185 platform_device_unregister(pdev);
186 release_bat_id(id);
187 sysfs_remove_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr);
190 static struct w1_family_ops w1_ds2780_fops = {
191 .add_slave = w1_ds2780_add_slave,
192 .remove_slave = w1_ds2780_remove_slave,
195 static struct w1_family w1_ds2780_family = {
196 .fid = W1_FAMILY_DS2780,
197 .fops = &w1_ds2780_fops,
200 static int __init w1_ds2780_init(void)
202 idr_init(&bat_idr);
203 return w1_register_family(&w1_ds2780_family);
206 static void __exit w1_ds2780_exit(void)
208 w1_unregister_family(&w1_ds2780_family);
209 idr_destroy(&bat_idr);
212 module_init(w1_ds2780_init);
213 module_exit(w1_ds2780_exit);
215 MODULE_LICENSE("GPL");
216 MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
217 MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC");