blob: 55eb86c9e214109256312990e7d532090b7d3017 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Evgeniy Polyakov77859252005-05-20 22:33:25 +04002 * w1_therm.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Evgeniy Polyakova8018762011-08-25 15:59:06 -07004 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
Evgeniy Polyakov77859252005-05-20 22:33:25 +04005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the therms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <asm/types.h>
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
Al Virof6a57032006-10-18 01:47:25 -040027#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/device.h>
29#include <linux/types.h>
David Frieseb2c0da2014-01-15 22:29:24 -060030#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/delay.h>
32
Evgeniy Polyakovbd529cf2005-12-06 13:38:28 +030033#include "../w1.h"
Evgeniy Polyakovbd529cf2005-12-06 13:38:28 +030034#include "../w1_int.h"
35#include "../w1_family.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37MODULE_LICENSE("GPL");
Evgeniy Polyakova8018762011-08-25 15:59:06 -070038MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
Alexander Stein8d7bda52013-05-26 20:06:50 +020040MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18S20));
41MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1822));
42MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS18B20));
43MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS1825));
44MODULE_ALIAS("w1-family-" __stringify(W1_THERM_DS28EA00));
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
David Fries6cd15972008-10-15 22:04:43 -070046/* Allow the strong pullup to be disabled, but default to enabled.
47 * If it was disabled a parasite powered device might not get the require
48 * current to do a temperature conversion. If it is enabled parasite powered
49 * devices have a better chance of getting the current required.
Michael Arndt29e55072013-02-17 20:51:20 +010050 * In case the parasite power-detection is not working (seems to be the case
51 * for some DS18S20) the strong pullup can also be forced, regardless of the
52 * power state of the devices.
53 *
54 * Summary of options:
55 * - strong_pullup = 0 Disable strong pullup completely
56 * - strong_pullup = 1 Enable automatic strong pullup detection
57 * - strong_pullup = 2 Force strong pullup
David Fries6cd15972008-10-15 22:04:43 -070058 */
59static int w1_strong_pullup = 1;
60module_param_named(strong_pullup, w1_strong_pullup, int, 0);
61
David Friesf7134ee2015-05-08 19:51:50 -050062struct w1_therm_family_data {
63 uint8_t rom[9];
64 atomic_t refcnt;
65};
66
67/* return the address of the refcnt in the family data */
68#define THERM_REFCNT(family_data) \
69 (&((struct w1_therm_family_data*)family_data)->refcnt)
70
David Frieseb2c0da2014-01-15 22:29:24 -060071static int w1_therm_add_slave(struct w1_slave *sl)
72{
David Friesf7134ee2015-05-08 19:51:50 -050073 sl->family_data = kzalloc(sizeof(struct w1_therm_family_data),
74 GFP_KERNEL);
David Frieseb2c0da2014-01-15 22:29:24 -060075 if (!sl->family_data)
76 return -ENOMEM;
David Friesf7134ee2015-05-08 19:51:50 -050077 atomic_set(THERM_REFCNT(sl->family_data), 1);
David Frieseb2c0da2014-01-15 22:29:24 -060078 return 0;
79}
80
81static void w1_therm_remove_slave(struct w1_slave *sl)
82{
David Friesf7134ee2015-05-08 19:51:50 -050083 int refcnt = atomic_sub_return(1, THERM_REFCNT(sl->family_data));
84 while(refcnt) {
85 msleep(1000);
86 refcnt = atomic_read(THERM_REFCNT(sl->family_data));
87 }
David Frieseb2c0da2014-01-15 22:29:24 -060088 kfree(sl->family_data);
89 sl->family_data = NULL;
90}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Greg Kroah-Hartmanb8a9f442013-08-21 15:44:56 -070092static ssize_t w1_slave_show(struct device *device,
David Fries347ba8a2008-10-15 22:04:51 -070093 struct device_attribute *attr, char *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Greg Kroah-Hartmanb8a9f442013-08-21 15:44:56 -070095static DEVICE_ATTR_RO(w1_slave);
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +040096
Greg Kroah-Hartmanb8a9f442013-08-21 15:44:56 -070097static struct attribute *w1_therm_attrs[] = {
98 &dev_attr_w1_slave.attr,
99 NULL,
100};
101ATTRIBUTE_GROUPS(w1_therm);
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static struct w1_family_ops w1_therm_fops = {
David Frieseb2c0da2014-01-15 22:29:24 -0600104 .add_slave = w1_therm_add_slave,
105 .remove_slave = w1_therm_remove_slave,
Greg Kroah-Hartmanb8a9f442013-08-21 15:44:56 -0700106 .groups = w1_therm_groups,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400109static struct w1_family w1_therm_family_DS18S20 = {
110 .fid = W1_THERM_DS18S20,
111 .fops = &w1_therm_fops,
112};
113
114static struct w1_family w1_therm_family_DS18B20 = {
115 .fid = W1_THERM_DS18B20,
116 .fops = &w1_therm_fops,
117};
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400118
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400119static struct w1_family w1_therm_family_DS1822 = {
120 .fid = W1_THERM_DS1822,
121 .fops = &w1_therm_fops,
122};
123
Christian Glindkampf7b13712011-07-26 16:08:55 -0700124static struct w1_family w1_therm_family_DS28EA00 = {
125 .fid = W1_THERM_DS28EA00,
126 .fops = &w1_therm_fops,
127};
128
Raphael Assenatf3261df2012-08-16 12:56:40 -0400129static struct w1_family w1_therm_family_DS1825 = {
130 .fid = W1_THERM_DS1825,
131 .fops = &w1_therm_fops,
132};
133
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400134struct w1_therm_family_converter
135{
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400136 u8 broken;
137 u16 reserved;
138 struct w1_family *f;
139 int (*convert)(u8 rom[9]);
140};
141
David Fries7129b122008-02-06 01:38:09 -0800142/* The return value is millidegrees Centigrade. */
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400143static inline int w1_DS18B20_convert_temp(u8 rom[9]);
144static inline int w1_DS18S20_convert_temp(u8 rom[9]);
145
146static struct w1_therm_family_converter w1_therm_families[] = {
147 {
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400148 .f = &w1_therm_family_DS18S20,
149 .convert = w1_DS18S20_convert_temp
150 },
151 {
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400152 .f = &w1_therm_family_DS1822,
153 .convert = w1_DS18B20_convert_temp
154 },
155 {
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400156 .f = &w1_therm_family_DS18B20,
157 .convert = w1_DS18B20_convert_temp
158 },
Christian Glindkampf7b13712011-07-26 16:08:55 -0700159 {
160 .f = &w1_therm_family_DS28EA00,
161 .convert = w1_DS18B20_convert_temp
162 },
Raphael Assenatf3261df2012-08-16 12:56:40 -0400163 {
164 .f = &w1_therm_family_DS1825,
165 .convert = w1_DS18B20_convert_temp
166 }
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400167};
168
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400169static inline int w1_DS18B20_convert_temp(u8 rom[9])
170{
Ian Dall9a6a1ec2010-04-23 13:17:53 -0400171 s16 t = le16_to_cpup((__le16 *)rom);
172 return t*1000/16;
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400173}
174
175static inline int w1_DS18S20_convert_temp(u8 rom[9])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 int t, h;
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400178
179 if (!rom[7])
180 return 0;
Evgeniy Polyakovbd529cf2005-12-06 13:38:28 +0300181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (rom[1] == 0)
183 t = ((s32)rom[0] >> 1)*1000;
184 else
185 t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
Evgeniy Polyakovbd529cf2005-12-06 13:38:28 +0300186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 t -= 250;
188 h = 1000*((s32)rom[7] - (s32)rom[6]);
189 h /= (s32)rom[7];
190 t += h;
191
192 return t;
193}
194
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400195static inline int w1_convert_temp(u8 rom[9], u8 fid)
196{
197 int i;
198
Ahmed S. Darwish9d0094d2007-02-12 00:52:05 -0800199 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
Evgeniy Polyakov4e470aa2005-05-20 22:50:33 +0400200 if (w1_therm_families[i].f->fid == fid)
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400201 return w1_therm_families[i].convert(rom);
202
203 return 0;
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Greg Kroah-Hartmanb8a9f442013-08-21 15:44:56 -0700207static ssize_t w1_slave_show(struct device *device,
David Fries347ba8a2008-10-15 22:04:51 -0700208 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
David Fries347ba8a2008-10-15 22:04:51 -0700210 struct w1_slave *sl = dev_to_w1_slave(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct w1_master *dev = sl->master;
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100212 u8 rom[9], crc, verdict, external_power;
David Friesf7134ee2015-05-08 19:51:50 -0500213 int i, ret, max_trying = 10;
David Fries347ba8a2008-10-15 22:04:51 -0700214 ssize_t c = PAGE_SIZE;
David Friesf7134ee2015-05-08 19:51:50 -0500215 u8 *family_data = sl->family_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
David Friesf7134ee2015-05-08 19:51:50 -0500217 ret = mutex_lock_interruptible(&dev->bus_mutex);
218 if (ret != 0)
219 goto post_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
David Friesf7134ee2015-05-08 19:51:50 -0500221 if(!sl->family_data)
222 {
223 ret = -ENODEV;
224 goto pre_unlock;
225 }
226
227 /* prevent the slave from going away in sleep */
228 atomic_inc(THERM_REFCNT(family_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 memset(rom, 0, sizeof(rom));
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 while (max_trying--) {
David Stevenson867ff982012-12-18 01:37:56 +0000232
233 verdict = 0;
234 crc = 0;
235
Evgeniy Polyakovea7d8f62005-08-11 17:27:49 +0400236 if (!w1_reset_select_slave(sl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 unsigned int tm = 750;
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100239 unsigned long sleep_rem;
240
241 w1_write_8(dev, W1_READ_PSUPPLY);
242 external_power = w1_read_8(dev);
243
244 if (w1_reset_select_slave(sl))
245 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David Fries6cd15972008-10-15 22:04:43 -0700247 /* 750ms strong pullup (or delay) after the convert */
Michael Arndt29e55072013-02-17 20:51:20 +0100248 if (w1_strong_pullup == 2 ||
249 (!external_power && w1_strong_pullup))
David Fries6cd15972008-10-15 22:04:43 -0700250 w1_next_pullup(dev, tm);
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 w1_write_8(dev, W1_CONVERT_TEMP);
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100253
254 if (external_power) {
NeilBrownb02f8be2012-05-18 15:59:52 +1000255 mutex_unlock(&dev->bus_mutex);
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100256
257 sleep_rem = msleep_interruptible(tm);
David Friesf7134ee2015-05-08 19:51:50 -0500258 if (sleep_rem != 0) {
259 ret = -EINTR;
260 goto post_unlock;
261 }
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100262
David Friesf7134ee2015-05-08 19:51:50 -0500263 ret = mutex_lock_interruptible(&dev->bus_mutex);
264 if (ret != 0)
265 goto post_unlock;
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100266 } else if (!w1_strong_pullup) {
267 sleep_rem = msleep_interruptible(tm);
268 if (sleep_rem != 0) {
David Friesf7134ee2015-05-08 19:51:50 -0500269 ret = -EINTR;
270 goto pre_unlock;
Maciej Szmigiero377195c2011-11-16 00:43:16 +0100271 }
272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Evgeniy Polyakovea7d8f62005-08-11 17:27:49 +0400274 if (!w1_reset_select_slave(sl)) {
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 w1_write_8(dev, W1_READ_SCRATCHPAD);
277 if ((count = w1_read_block(dev, rom, 9)) != 9) {
David Fries347ba8a2008-10-15 22:04:51 -0700278 dev_warn(device, "w1_read_block() "
279 "returned %u instead of 9.\n",
280 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
283 crc = w1_calc_crc8(rom, 8);
284
David Fries80c002d2008-01-22 03:31:39 -0800285 if (rom[8] == crc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 verdict = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 }
289
David Stevenson867ff982012-12-18 01:37:56 +0000290 if (verdict)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 break;
292 }
293
294 for (i = 0; i < 9; ++i)
David Fries347ba8a2008-10-15 22:04:51 -0700295 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rom[i]);
296 c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 crc, (verdict) ? "YES" : "NO");
298 if (verdict)
David Friesf7134ee2015-05-08 19:51:50 -0500299 memcpy(family_data, rom, sizeof(rom));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 else
David Stevenson867ff982012-12-18 01:37:56 +0000301 dev_warn(device, "Read failed CRC check\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 for (i = 0; i < 9; ++i)
David Frieseb2c0da2014-01-15 22:29:24 -0600304 c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ",
David Friesf7134ee2015-05-08 19:51:50 -0500305 ((u8 *)family_data)[i]);
Evgeniy Polyakovbd529cf2005-12-06 13:38:28 +0300306
David Fries347ba8a2008-10-15 22:04:51 -0700307 c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
308 w1_convert_temp(rom, sl->family->fid));
David Friesf7134ee2015-05-08 19:51:50 -0500309 ret = PAGE_SIZE - c;
310
311pre_unlock:
NeilBrownb02f8be2012-05-18 15:59:52 +1000312 mutex_unlock(&dev->bus_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
David Friesf7134ee2015-05-08 19:51:50 -0500314post_unlock:
315 atomic_dec(THERM_REFCNT(family_data));
316 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319static int __init w1_therm_init(void)
320{
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400321 int err, i;
322
Ahmed S. Darwish9d0094d2007-02-12 00:52:05 -0800323 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400324 err = w1_register_family(w1_therm_families[i].f);
325 if (err)
326 w1_therm_families[i].broken = 1;
327 }
328
329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332static void __exit w1_therm_fini(void)
333{
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400334 int i;
335
Ahmed S. Darwish9d0094d2007-02-12 00:52:05 -0800336 for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
Evgeniy Polyakov718a5382005-04-17 22:58:14 +0400337 if (!w1_therm_families[i].broken)
338 w1_unregister_family(w1_therm_families[i].f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341module_init(w1_therm_init);
342module_exit(w1_therm_fini);