]>
Commit | Line | Data |
---|---|---|
275ac746 CB |
1 | /* |
2 | * 1-Wire implementation for the ds2780 chip | |
3 | * | |
4 | * Copyright (C) 2010 Indesign, LLC | |
5 | * | |
6 | * Author: Clifton Barnes <[email protected]> | |
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 | ||
9fe678fa CB |
29 | static int w1_ds2780_do_io(struct device *dev, char *buf, int addr, |
30 | size_t count, int io) | |
275ac746 CB |
31 | { |
32 | struct w1_slave *sl = container_of(dev, struct w1_slave, dev); | |
33 | ||
9fe678fa CB |
34 | if (addr > DS2780_DATA_SIZE || addr < 0) |
35 | return 0; | |
275ac746 | 36 | |
275ac746 CB |
37 | count = min_t(int, count, DS2780_DATA_SIZE - addr); |
38 | ||
39 | if (w1_reset_select_slave(sl) == 0) { | |
40 | if (io) { | |
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); | |
275ac746 CB |
44 | } else { |
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); | |
48 | } | |
49 | } | |
50 | ||
9fe678fa CB |
51 | return count; |
52 | } | |
53 | ||
54 | int w1_ds2780_io(struct device *dev, char *buf, int addr, size_t count, | |
55 | int io) | |
56 | { | |
57 | struct w1_slave *sl = container_of(dev, struct w1_slave, dev); | |
58 | int ret; | |
59 | ||
60 | if (!dev) | |
61 | return -ENODEV; | |
62 | ||
b02f8bed | 63 | mutex_lock(&sl->master->bus_mutex); |
9fe678fa CB |
64 | |
65 | ret = w1_ds2780_do_io(dev, buf, addr, count, io); | |
66 | ||
b02f8bed | 67 | mutex_unlock(&sl->master->bus_mutex); |
275ac746 | 68 | |
9fe678fa | 69 | return ret; |
275ac746 CB |
70 | } |
71 | EXPORT_SYMBOL(w1_ds2780_io); | |
72 | ||
73 | int w1_ds2780_eeprom_cmd(struct device *dev, int addr, int cmd) | |
74 | { | |
75 | struct w1_slave *sl = container_of(dev, struct w1_slave, dev); | |
76 | ||
77 | if (!dev) | |
78 | return -EINVAL; | |
79 | ||
b02f8bed | 80 | mutex_lock(&sl->master->bus_mutex); |
275ac746 CB |
81 | |
82 | if (w1_reset_select_slave(sl) == 0) { | |
83 | w1_write_8(sl->master, cmd); | |
84 | w1_write_8(sl->master, addr); | |
85 | } | |
86 | ||
b02f8bed | 87 | mutex_unlock(&sl->master->bus_mutex); |
275ac746 CB |
88 | return 0; |
89 | } | |
90 | EXPORT_SYMBOL(w1_ds2780_eeprom_cmd); | |
91 | ||
9365261d GKH |
92 | static ssize_t w1_slave_read(struct file *filp, struct kobject *kobj, |
93 | struct bin_attribute *bin_attr, char *buf, | |
94 | loff_t off, size_t count) | |
275ac746 CB |
95 | { |
96 | struct device *dev = container_of(kobj, struct device, kobj); | |
97 | return w1_ds2780_io(dev, buf, off, count, 0); | |
98 | } | |
99 | ||
9365261d GKH |
100 | static BIN_ATTR_RO(w1_slave, DS2780_DATA_SIZE); |
101 | ||
102 | static struct bin_attribute *w1_ds2780_bin_attrs[] = { | |
103 | &bin_attr_w1_slave, | |
104 | NULL, | |
105 | }; | |
106 | ||
107 | static const struct attribute_group w1_ds2780_group = { | |
108 | .bin_attrs = w1_ds2780_bin_attrs, | |
109 | }; | |
110 | ||
111 | static const struct attribute_group *w1_ds2780_groups[] = { | |
112 | &w1_ds2780_group, | |
113 | NULL, | |
275ac746 CB |
114 | }; |
115 | ||
3e542817 | 116 | static DEFINE_IDA(bat_ida); |
275ac746 CB |
117 | |
118 | static int w1_ds2780_add_slave(struct w1_slave *sl) | |
119 | { | |
120 | int ret; | |
121 | int id; | |
122 | struct platform_device *pdev; | |
123 | ||
3e542817 | 124 | id = ida_simple_get(&bat_ida, 0, 0, GFP_KERNEL); |
275ac746 CB |
125 | if (id < 0) { |
126 | ret = id; | |
127 | goto noid; | |
128 | } | |
129 | ||
130 | pdev = platform_device_alloc("ds2780-battery", id); | |
131 | if (!pdev) { | |
132 | ret = -ENOMEM; | |
133 | goto pdev_alloc_failed; | |
134 | } | |
135 | pdev->dev.parent = &sl->dev; | |
136 | ||
137 | ret = platform_device_add(pdev); | |
138 | if (ret) | |
139 | goto pdev_add_failed; | |
140 | ||
275ac746 CB |
141 | dev_set_drvdata(&sl->dev, pdev); |
142 | ||
143 | return 0; | |
144 | ||
275ac746 | 145 | pdev_add_failed: |
c5cfedf2 | 146 | platform_device_put(pdev); |
275ac746 | 147 | pdev_alloc_failed: |
3e542817 | 148 | ida_simple_remove(&bat_ida, id); |
275ac746 CB |
149 | noid: |
150 | return ret; | |
151 | } | |
152 | ||
153 | static void w1_ds2780_remove_slave(struct w1_slave *sl) | |
154 | { | |
155 | struct platform_device *pdev = dev_get_drvdata(&sl->dev); | |
156 | int id = pdev->id; | |
157 | ||
158 | platform_device_unregister(pdev); | |
3e542817 | 159 | ida_simple_remove(&bat_ida, id); |
275ac746 CB |
160 | } |
161 | ||
162 | static struct w1_family_ops w1_ds2780_fops = { | |
163 | .add_slave = w1_ds2780_add_slave, | |
164 | .remove_slave = w1_ds2780_remove_slave, | |
9365261d | 165 | .groups = w1_ds2780_groups, |
275ac746 CB |
166 | }; |
167 | ||
168 | static struct w1_family w1_ds2780_family = { | |
169 | .fid = W1_FAMILY_DS2780, | |
170 | .fops = &w1_ds2780_fops, | |
171 | }; | |
172 | ||
173 | static int __init w1_ds2780_init(void) | |
174 | { | |
3e542817 | 175 | ida_init(&bat_ida); |
275ac746 CB |
176 | return w1_register_family(&w1_ds2780_family); |
177 | } | |
178 | ||
179 | static void __exit w1_ds2780_exit(void) | |
180 | { | |
181 | w1_unregister_family(&w1_ds2780_family); | |
3e542817 | 182 | ida_destroy(&bat_ida); |
275ac746 CB |
183 | } |
184 | ||
185 | module_init(w1_ds2780_init); | |
186 | module_exit(w1_ds2780_exit); | |
187 | ||
188 | MODULE_LICENSE("GPL"); | |
189 | MODULE_AUTHOR("Clifton Barnes <[email protected]>"); | |
190 | MODULE_DESCRIPTION("1-wire Driver for Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC"); | |
8d7bda51 | 191 | MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2780)); |