1/*
2 *	w1_therm.c
3 *
4 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
5 *
6 *
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>
27#include <linux/sched.h>
28#include <linux/device.h>
29#include <linux/types.h>
30#include <linux/delay.h>
31
32#include "../w1.h"
33#include "../w1_int.h"
34#include "../w1_family.h"
35
36MODULE_LICENSE("GPL");
37MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
38MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol, temperature family.");
39
40/* Allow the strong pullup to be disabled, but default to enabled.
41 * If it was disabled a parasite powered device might not get the require
42 * current to do a temperature conversion.  If it is enabled parasite powered
43 * devices have a better chance of getting the current required.
44 */
45static int w1_strong_pullup = 1;
46module_param_named(strong_pullup, w1_strong_pullup, int, 0);
47
48static u8 bad_roms[][9] = {
49				{0xaa, 0x00, 0x4b, 0x46, 0xff, 0xff, 0x0c, 0x10, 0x87},
50				{}
51			};
52
53static ssize_t w1_therm_read(struct device *device,
54	struct device_attribute *attr, char *buf);
55
56static struct device_attribute w1_therm_attr =
57	__ATTR(w1_slave, S_IRUGO, w1_therm_read, NULL);
58
59static int w1_therm_add_slave(struct w1_slave *sl)
60{
61	return device_create_file(&sl->dev, &w1_therm_attr);
62}
63
64static void w1_therm_remove_slave(struct w1_slave *sl)
65{
66	device_remove_file(&sl->dev, &w1_therm_attr);
67}
68
69static struct w1_family_ops w1_therm_fops = {
70	.add_slave	= w1_therm_add_slave,
71	.remove_slave	= w1_therm_remove_slave,
72};
73
74static struct w1_family w1_therm_family_DS18S20 = {
75	.fid = W1_THERM_DS18S20,
76	.fops = &w1_therm_fops,
77};
78
79static struct w1_family w1_therm_family_DS18B20 = {
80	.fid = W1_THERM_DS18B20,
81	.fops = &w1_therm_fops,
82};
83
84static struct w1_family w1_therm_family_DS1822 = {
85	.fid = W1_THERM_DS1822,
86	.fops = &w1_therm_fops,
87};
88
89static struct w1_family w1_therm_family_DS28EA00 = {
90	.fid = W1_THERM_DS28EA00,
91	.fops = &w1_therm_fops,
92};
93
94struct w1_therm_family_converter
95{
96	u8			broken;
97	u16			reserved;
98	struct w1_family	*f;
99	int			(*convert)(u8 rom[9]);
100};
101
102/* The return value is millidegrees Centigrade. */
103static inline int w1_DS18B20_convert_temp(u8 rom[9]);
104static inline int w1_DS18S20_convert_temp(u8 rom[9]);
105
106static struct w1_therm_family_converter w1_therm_families[] = {
107	{
108		.f		= &w1_therm_family_DS18S20,
109		.convert 	= w1_DS18S20_convert_temp
110	},
111	{
112		.f		= &w1_therm_family_DS1822,
113		.convert 	= w1_DS18B20_convert_temp
114	},
115	{
116		.f		= &w1_therm_family_DS18B20,
117		.convert 	= w1_DS18B20_convert_temp
118	},
119	{
120		.f		= &w1_therm_family_DS28EA00,
121		.convert	= w1_DS18B20_convert_temp
122	},
123};
124
125static inline int w1_DS18B20_convert_temp(u8 rom[9])
126{
127	s16 t = le16_to_cpup((__le16 *)rom);
128	return t*1000/16;
129}
130
131static inline int w1_DS18S20_convert_temp(u8 rom[9])
132{
133	int t, h;
134
135	if (!rom[7])
136		return 0;
137
138	if (rom[1] == 0)
139		t = ((s32)rom[0] >> 1)*1000;
140	else
141		t = 1000*(-1*(s32)(0x100-rom[0]) >> 1);
142
143	t -= 250;
144	h = 1000*((s32)rom[7] - (s32)rom[6]);
145	h /= (s32)rom[7];
146	t += h;
147
148	return t;
149}
150
151static inline int w1_convert_temp(u8 rom[9], u8 fid)
152{
153	int i;
154
155	for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
156		if (w1_therm_families[i].f->fid == fid)
157			return w1_therm_families[i].convert(rom);
158
159	return 0;
160}
161
162static int w1_therm_check_rom(u8 rom[9])
163{
164	int i;
165
166	for (i=0; i<sizeof(bad_roms)/9; ++i)
167		if (!memcmp(bad_roms[i], rom, 9))
168			return 1;
169
170	return 0;
171}
172
173static ssize_t w1_therm_read(struct device *device,
174	struct device_attribute *attr, char *buf)
175{
176	struct w1_slave *sl = dev_to_w1_slave(device);
177	struct w1_master *dev = sl->master;
178	u8 rom[9], crc, verdict, external_power;
179	int i, max_trying = 10;
180	ssize_t c = PAGE_SIZE;
181
182	i = mutex_lock_interruptible(&dev->mutex);
183	if (i != 0)
184		return i;
185
186	memset(rom, 0, sizeof(rom));
187
188	verdict = 0;
189	crc = 0;
190
191	while (max_trying--) {
192		if (!w1_reset_select_slave(sl)) {
193			int count = 0;
194			unsigned int tm = 750;
195			unsigned long sleep_rem;
196
197			w1_write_8(dev, W1_READ_PSUPPLY);
198			external_power = w1_read_8(dev);
199
200			if (w1_reset_select_slave(sl))
201				continue;
202
203			/* 750ms strong pullup (or delay) after the convert */
204			if (!external_power && w1_strong_pullup)
205				w1_next_pullup(dev, tm);
206
207			w1_write_8(dev, W1_CONVERT_TEMP);
208
209			if (external_power) {
210				mutex_unlock(&dev->mutex);
211
212				sleep_rem = msleep_interruptible(tm);
213				if (sleep_rem != 0)
214					return -EINTR;
215
216				i = mutex_lock_interruptible(&dev->mutex);
217				if (i != 0)
218					return i;
219			} else if (!w1_strong_pullup) {
220				sleep_rem = msleep_interruptible(tm);
221				if (sleep_rem != 0) {
222					mutex_unlock(&dev->mutex);
223					return -EINTR;
224				}
225			}
226
227			if (!w1_reset_select_slave(sl)) {
228
229				w1_write_8(dev, W1_READ_SCRATCHPAD);
230				if ((count = w1_read_block(dev, rom, 9)) != 9) {
231					dev_warn(device, "w1_read_block() "
232						"returned %u instead of 9.\n",
233						count);
234				}
235
236				crc = w1_calc_crc8(rom, 8);
237
238				if (rom[8] == crc)
239					verdict = 1;
240			}
241		}
242
243		if (!w1_therm_check_rom(rom))
244			break;
245	}
246
247	for (i = 0; i < 9; ++i)
248		c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rom[i]);
249	c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
250			   crc, (verdict) ? "YES" : "NO");
251	if (verdict)
252		memcpy(sl->rom, rom, sizeof(sl->rom));
253	else
254		dev_warn(device, "18S20 doesn't respond to CONVERT_TEMP.\n");
255
256	for (i = 0; i < 9; ++i)
257		c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", sl->rom[i]);
258
259	c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
260		w1_convert_temp(rom, sl->family->fid));
261	mutex_unlock(&dev->mutex);
262
263	return PAGE_SIZE - c;
264}
265
266static int __init w1_therm_init(void)
267{
268	int err, i;
269
270	for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i) {
271		err = w1_register_family(w1_therm_families[i].f);
272		if (err)
273			w1_therm_families[i].broken = 1;
274	}
275
276	return 0;
277}
278
279static void __exit w1_therm_fini(void)
280{
281	int i;
282
283	for (i = 0; i < ARRAY_SIZE(w1_therm_families); ++i)
284		if (!w1_therm_families[i].broken)
285			w1_unregister_family(w1_therm_families[i].f);
286}
287
288module_init(w1_therm_init);
289module_exit(w1_therm_fini);
290