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 static int w1_ds2780_do_io(struct device
*dev
, char *buf
, int addr
,
32 struct w1_slave
*sl
= container_of(dev
, struct w1_slave
, dev
);
34 if (addr
> DS2780_DATA_SIZE
|| addr
< 0)
37 count
= min_t(int, count
, DS2780_DATA_SIZE
- addr
);
39 if (w1_reset_select_slave(sl
) == 0) {
41 w1_write_8(sl
->master
, W1_DS2780_WRITE_DATA
);
42 w1_write_8(sl
->master
, addr
);
43 w1_write_block(sl
->master
, buf
, count
);
45 w1_write_8(sl
->master
, W1_DS2780_READ_DATA
);
46 w1_write_8(sl
->master
, addr
);
47 count
= w1_read_block(sl
->master
, buf
, count
);
54 int w1_ds2780_io(struct device
*dev
, char *buf
, int addr
, size_t count
,
57 struct w1_slave
*sl
= container_of(dev
, struct w1_slave
, dev
);
63 mutex_lock(&sl
->master
->mutex
);
65 ret
= w1_ds2780_do_io(dev
, buf
, addr
, count
, io
);
67 mutex_unlock(&sl
->master
->mutex
);
71 EXPORT_SYMBOL(w1_ds2780_io
);
73 int w1_ds2780_io_nolock(struct device
*dev
, char *buf
, int addr
, size_t count
,
81 ret
= w1_ds2780_do_io(dev
, buf
, addr
, count
, io
);
85 EXPORT_SYMBOL(w1_ds2780_io_nolock
);
87 int w1_ds2780_eeprom_cmd(struct device
*dev
, int addr
, int cmd
)
89 struct w1_slave
*sl
= container_of(dev
, struct w1_slave
, dev
);
94 mutex_lock(&sl
->master
->mutex
);
96 if (w1_reset_select_slave(sl
) == 0) {
97 w1_write_8(sl
->master
, cmd
);
98 w1_write_8(sl
->master
, addr
);
101 mutex_unlock(&sl
->master
->mutex
);
104 EXPORT_SYMBOL(w1_ds2780_eeprom_cmd
);
106 static ssize_t
w1_ds2780_read_bin(struct file
*filp
,
107 struct kobject
*kobj
,
108 struct bin_attribute
*bin_attr
,
109 char *buf
, loff_t off
, size_t count
)
111 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
112 return w1_ds2780_io(dev
, buf
, off
, count
, 0);
115 static struct bin_attribute w1_ds2780_bin_attr
= {
120 .size
= DS2780_DATA_SIZE
,
121 .read
= w1_ds2780_read_bin
,
124 static DEFINE_IDA(bat_ida
);
126 static int w1_ds2780_add_slave(struct w1_slave
*sl
)
130 struct platform_device
*pdev
;
132 id
= ida_simple_get(&bat_ida
, 0, 0, GFP_KERNEL
);
138 pdev
= platform_device_alloc("ds2780-battery", id
);
141 goto pdev_alloc_failed
;
143 pdev
->dev
.parent
= &sl
->dev
;
145 ret
= platform_device_add(pdev
);
147 goto pdev_add_failed
;
149 ret
= sysfs_create_bin_file(&sl
->dev
.kobj
, &w1_ds2780_bin_attr
);
151 goto bin_attr_failed
;
153 dev_set_drvdata(&sl
->dev
, pdev
);
159 platform_device_unregister(pdev
);
161 ida_simple_remove(&bat_ida
, id
);
166 static void w1_ds2780_remove_slave(struct w1_slave
*sl
)
168 struct platform_device
*pdev
= dev_get_drvdata(&sl
->dev
);
171 platform_device_unregister(pdev
);
172 ida_simple_remove(&bat_ida
, id
);
173 sysfs_remove_bin_file(&sl
->dev
.kobj
, &w1_ds2780_bin_attr
);
176 static struct w1_family_ops w1_ds2780_fops
= {
177 .add_slave
= w1_ds2780_add_slave
,
178 .remove_slave
= w1_ds2780_remove_slave
,
181 static struct w1_family w1_ds2780_family
= {
182 .fid
= W1_FAMILY_DS2780
,
183 .fops
= &w1_ds2780_fops
,
186 static int __init
w1_ds2780_init(void)
189 return w1_register_family(&w1_ds2780_family
);
192 static void __exit
w1_ds2780_exit(void)
194 w1_unregister_family(&w1_ds2780_family
);
195 ida_destroy(&bat_ida
);
198 module_init(w1_ds2780_init
);
199 module_exit(w1_ds2780_exit
);
201 MODULE_LICENSE("GPL");
202 MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
203 MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC");