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