1
2
3
4
5
6
7
8
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>
19#include <linux/gfp.h>
20
21#include <linux/w1.h>
22
23#include "w1_ds2760.h"
24
25#define W1_FAMILY_DS2760 0x30
26
27static int w1_ds2760_io(struct device *dev, char *buf, int addr, size_t count,
28 int io)
29{
30 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
31
32 if (!dev)
33 return 0;
34
35 mutex_lock(&sl->master->bus_mutex);
36
37 if (addr > DS2760_DATA_SIZE || addr < 0) {
38 count = 0;
39 goto out;
40 }
41 if (addr + count > DS2760_DATA_SIZE)
42 count = DS2760_DATA_SIZE - addr;
43
44 if (!w1_reset_select_slave(sl)) {
45 if (!io) {
46 w1_write_8(sl->master, W1_DS2760_READ_DATA);
47 w1_write_8(sl->master, addr);
48 count = w1_read_block(sl->master, buf, count);
49 } else {
50 w1_write_8(sl->master, W1_DS2760_WRITE_DATA);
51 w1_write_8(sl->master, addr);
52 w1_write_block(sl->master, buf, count);
53
54 }
55 }
56
57out:
58 mutex_unlock(&sl->master->bus_mutex);
59
60 return count;
61}
62
63int w1_ds2760_read(struct device *dev, char *buf, int addr, size_t count)
64{
65 return w1_ds2760_io(dev, buf, addr, count, 0);
66}
67EXPORT_SYMBOL(w1_ds2760_read);
68
69int w1_ds2760_write(struct device *dev, char *buf, int addr, size_t count)
70{
71 return w1_ds2760_io(dev, buf, addr, count, 1);
72}
73EXPORT_SYMBOL(w1_ds2760_write);
74
75static int w1_ds2760_eeprom_cmd(struct device *dev, int addr, int cmd)
76{
77 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
78
79 if (!dev)
80 return -EINVAL;
81
82 mutex_lock(&sl->master->bus_mutex);
83
84 if (w1_reset_select_slave(sl) == 0) {
85 w1_write_8(sl->master, cmd);
86 w1_write_8(sl->master, addr);
87 }
88
89 mutex_unlock(&sl->master->bus_mutex);
90 return 0;
91}
92
93int w1_ds2760_store_eeprom(struct device *dev, int addr)
94{
95 return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_COPY_DATA);
96}
97EXPORT_SYMBOL(w1_ds2760_store_eeprom);
98
99int w1_ds2760_recall_eeprom(struct device *dev, int addr)
100{
101 return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_RECALL_DATA);
102}
103EXPORT_SYMBOL(w1_ds2760_recall_eeprom);
104
105static ssize_t w1_slave_read(struct file *filp, struct kobject *kobj,
106 struct bin_attribute *bin_attr, char *buf,
107 loff_t off, size_t count)
108{
109 struct device *dev = container_of(kobj, struct device, kobj);
110 return w1_ds2760_read(dev, buf, off, count);
111}
112
113static BIN_ATTR_RO(w1_slave, DS2760_DATA_SIZE);
114
115static struct bin_attribute *w1_ds2760_bin_attrs[] = {
116 &bin_attr_w1_slave,
117 NULL,
118};
119
120static const struct attribute_group w1_ds2760_group = {
121 .bin_attrs = w1_ds2760_bin_attrs,
122};
123
124static const struct attribute_group *w1_ds2760_groups[] = {
125 &w1_ds2760_group,
126 NULL,
127};
128
129static int w1_ds2760_add_slave(struct w1_slave *sl)
130{
131 int ret;
132 struct platform_device *pdev;
133
134 pdev = platform_device_alloc("ds2760-battery", PLATFORM_DEVID_AUTO);
135 if (!pdev)
136 return -ENOMEM;
137 pdev->dev.parent = &sl->dev;
138
139 ret = platform_device_add(pdev);
140 if (ret)
141 goto pdev_add_failed;
142
143 dev_set_drvdata(&sl->dev, pdev);
144
145 return 0;
146
147pdev_add_failed:
148 platform_device_put(pdev);
149
150 return ret;
151}
152
153static void w1_ds2760_remove_slave(struct w1_slave *sl)
154{
155 struct platform_device *pdev = dev_get_drvdata(&sl->dev);
156
157 platform_device_unregister(pdev);
158}
159
160static struct w1_family_ops w1_ds2760_fops = {
161 .add_slave = w1_ds2760_add_slave,
162 .remove_slave = w1_ds2760_remove_slave,
163 .groups = w1_ds2760_groups,
164};
165
166static struct w1_family w1_ds2760_family = {
167 .fid = W1_FAMILY_DS2760,
168 .fops = &w1_ds2760_fops,
169};
170module_w1_family(w1_ds2760_family);
171
172MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>");
173MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");
174MODULE_LICENSE("GPL");
175MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2760));
176