blob: 274c8f38303f5748478c47309c6e255b5c69ead1 [file] [log] [blame]
Clifton Barnes275ac742011-05-26 16:26:04 -07001/*
2 * 1-Wire implementation for the ds2780 chip
3 *
4 * Copyright (C) 2010 Indesign, LLC
5 *
6 * Author: Clifton Barnes <cabarnes@indesign-llc.com>
7 *
8 * Based on w1-ds2760 driver
9 *
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.
13 *
14 */
15
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>
23
24#include "../w1.h"
25#include "../w1_int.h"
26#include "../w1_family.h"
27#include "w1_ds2780.h"
28
29int w1_ds2780_io(struct device *dev, char *buf, int addr, size_t count,
30 int io)
31{
32 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
33
34 if (!dev)
35 return -ENODEV;
36
37 mutex_lock(&sl->master->mutex);
38
39 if (addr > DS2780_DATA_SIZE || addr < 0) {
40 count = 0;
41 goto out;
42 }
43 count = min_t(int, count, DS2780_DATA_SIZE - addr);
44
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);
55 }
56 }
57
58out:
59 mutex_unlock(&sl->master->mutex);
60
61 return count;
62}
63EXPORT_SYMBOL(w1_ds2780_io);
64
65int w1_ds2780_eeprom_cmd(struct device *dev, int addr, int cmd)
66{
67 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
68
69 if (!dev)
70 return -EINVAL;
71
72 mutex_lock(&sl->master->mutex);
73
74 if (w1_reset_select_slave(sl) == 0) {
75 w1_write_8(sl->master, cmd);
76 w1_write_8(sl->master, addr);
77 }
78
79 mutex_unlock(&sl->master->mutex);
80 return 0;
81}
82EXPORT_SYMBOL(w1_ds2780_eeprom_cmd);
83
84static 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)
88{
89 struct device *dev = container_of(kobj, struct device, kobj);
90 return w1_ds2780_io(dev, buf, off, count, 0);
91}
92
93static struct bin_attribute w1_ds2780_bin_attr = {
94 .attr = {
95 .name = "w1_slave",
96 .mode = S_IRUGO,
97 },
98 .size = DS2780_DATA_SIZE,
99 .read = w1_ds2780_read_bin,
100};
101
102static DEFINE_IDR(bat_idr);
103static DEFINE_MUTEX(bat_idr_lock);
104
105static int new_bat_id(void)
106{
107 int ret;
108
109 while (1) {
110 int id;
111
112 ret = idr_pre_get(&bat_idr, GFP_KERNEL);
113 if (ret == 0)
114 return -ENOMEM;
115
116 mutex_lock(&bat_idr_lock);
117 ret = idr_get_new(&bat_idr, NULL, &id);
118 mutex_unlock(&bat_idr_lock);
119
120 if (ret == 0) {
121 ret = id & MAX_ID_MASK;
122 break;
123 } else if (ret == -EAGAIN) {
124 continue;
125 } else {
126 break;
127 }
128 }
129
130 return ret;
131}
132
133static void release_bat_id(int id)
134{
135 mutex_lock(&bat_idr_lock);
136 idr_remove(&bat_idr, id);
137 mutex_unlock(&bat_idr_lock);
138}
139
140static int w1_ds2780_add_slave(struct w1_slave *sl)
141{
142 int ret;
143 int id;
144 struct platform_device *pdev;
145
146 id = new_bat_id();
147 if (id < 0) {
148 ret = id;
149 goto noid;
150 }
151
152 pdev = platform_device_alloc("ds2780-battery", id);
153 if (!pdev) {
154 ret = -ENOMEM;
155 goto pdev_alloc_failed;
156 }
157 pdev->dev.parent = &sl->dev;
158
159 ret = platform_device_add(pdev);
160 if (ret)
161 goto pdev_add_failed;
162
163 ret = sysfs_create_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr);
164 if (ret)
165 goto bin_attr_failed;
166
167 dev_set_drvdata(&sl->dev, pdev);
168
169 return 0;
170
171bin_attr_failed:
172pdev_add_failed:
173 platform_device_unregister(pdev);
174pdev_alloc_failed:
175 release_bat_id(id);
176noid:
177 return ret;
178}
179
180static void w1_ds2780_remove_slave(struct w1_slave *sl)
181{
182 struct platform_device *pdev = dev_get_drvdata(&sl->dev);
183 int id = pdev->id;
184
185 platform_device_unregister(pdev);
186 release_bat_id(id);
187 sysfs_remove_bin_file(&sl->dev.kobj, &w1_ds2780_bin_attr);
188}
189
190static struct w1_family_ops w1_ds2780_fops = {
191 .add_slave = w1_ds2780_add_slave,
192 .remove_slave = w1_ds2780_remove_slave,
193};
194
195static struct w1_family w1_ds2780_family = {
196 .fid = W1_FAMILY_DS2780,
197 .fops = &w1_ds2780_fops,
198};
199
200static int __init w1_ds2780_init(void)
201{
202 idr_init(&bat_idr);
203 return w1_register_family(&w1_ds2780_family);
204}
205
206static void __exit w1_ds2780_exit(void)
207{
208 w1_unregister_family(&w1_ds2780_family);
209 idr_destroy(&bat_idr);
210}
211
212module_init(w1_ds2780_init);
213module_exit(w1_ds2780_exit);
214
215MODULE_LICENSE("GPL");
216MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
217MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC");