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>
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
,
32 struct w1_slave
*sl
= container_of(dev
, struct w1_slave
, dev
);
37 mutex_lock(&sl
->master
->mutex
);
39 if (addr
> DS2780_DATA_SIZE
|| addr
< 0) {
43 count
= min_t(int, count
, DS2780_DATA_SIZE
- addr
);
45 if (w1_reset_select_slave(sl
) == 0) {
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 */
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
);
59 mutex_unlock(&sl
->master
->mutex
);
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
);
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
);
82 EXPORT_SYMBOL(w1_ds2780_eeprom_cmd
);
84 static ssize_t
w1_ds2780_read_bin(struct file
*filp
,
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
= {
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)
112 ret
= idr_pre_get(&bat_idr
, GFP_KERNEL
);
116 mutex_lock(&bat_idr_lock
);
117 ret
= idr_get_new(&bat_idr
, NULL
, &id
);
118 mutex_unlock(&bat_idr_lock
);
121 ret
= id
& MAX_ID_MASK
;
123 } else if (ret
== -EAGAIN
) {
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
)
144 struct platform_device
*pdev
;
152 pdev
= platform_device_alloc("ds2780-battery", id
);
155 goto pdev_alloc_failed
;
157 pdev
->dev
.parent
= &sl
->dev
;
159 ret
= platform_device_add(pdev
);
161 goto pdev_add_failed
;
163 ret
= sysfs_create_bin_file(&sl
->dev
.kobj
, &w1_ds2780_bin_attr
);
165 goto bin_attr_failed
;
167 dev_set_drvdata(&sl
->dev
, pdev
);
173 platform_device_unregister(pdev
);
180 static void w1_ds2780_remove_slave(struct w1_slave
*sl
)
182 struct platform_device
*pdev
= dev_get_drvdata(&sl
->dev
);
185 platform_device_unregister(pdev
);
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)
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");