• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * ds2482.c - provides i2c to w1-master bridge(s)
3  * Copyright (C) 2005  Ben Gardner <bgardner@wabtec.com>
4  *
5  * The DS2482 is a sensor chip made by Dallas Semiconductor (Maxim).
6  * It is a I2C to 1-wire bridge.
7  * There are two variations: -100 and -800, which have 1 or 8 1-wire ports.
8  * The complete datasheet can be obtained from MAXIM's website at:
9  *   http://www.maxim-ic.com/quick_view2.cfm/qv_pk/4382
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; version 2 of the License.
14  */
15 
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/delay.h>
21 #include <asm/delay.h>
22 
23 #include "../w1.h"
24 #include "../w1_int.h"
25 
26 /**
27  * Address is selected using 2 pins, resulting in 4 possible addresses.
28  *  0x18, 0x19, 0x1a, 0x1b
29  * However, the chip cannot be detected without doing an i2c write,
30  * so use the force module parameter.
31  */
32 static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
33 
34 /**
35  * Insmod parameters
36  */
37 I2C_CLIENT_INSMOD_1(ds2482);
38 
39 /**
40  * The DS2482 registers - there are 3 registers that are addressed by a read
41  * pointer. The read pointer is set by the last command executed.
42  *
43  * To read the data, issue a register read for any address
44  */
45 #define DS2482_CMD_RESET		0xF0	/* No param */
46 #define DS2482_CMD_SET_READ_PTR		0xE1	/* Param: DS2482_PTR_CODE_xxx */
47 #define DS2482_CMD_CHANNEL_SELECT	0xC3	/* Param: Channel byte - DS2482-800 only */
48 #define DS2482_CMD_WRITE_CONFIG		0xD2	/* Param: Config byte */
49 #define DS2482_CMD_1WIRE_RESET		0xB4	/* Param: None */
50 #define DS2482_CMD_1WIRE_SINGLE_BIT	0x87	/* Param: Bit byte (bit7) */
51 #define DS2482_CMD_1WIRE_WRITE_BYTE	0xA5	/* Param: Data byte */
52 #define DS2482_CMD_1WIRE_READ_BYTE	0x96	/* Param: None */
53 /* Note to read the byte, Set the ReadPtr to Data then read (any addr) */
54 #define DS2482_CMD_1WIRE_TRIPLET	0x78	/* Param: Dir byte (bit7) */
55 
56 /* Values for DS2482_CMD_SET_READ_PTR */
57 #define DS2482_PTR_CODE_STATUS		0xF0
58 #define DS2482_PTR_CODE_DATA		0xE1
59 #define DS2482_PTR_CODE_CHANNEL		0xD2	/* DS2482-800 only */
60 #define DS2482_PTR_CODE_CONFIG		0xC3
61 
62 /**
63  * Configure Register bit definitions
64  * The top 4 bits always read 0.
65  * To write, the top nibble must be the 1's compl. of the low nibble.
66  */
67 #define DS2482_REG_CFG_1WS		0x08
68 #define DS2482_REG_CFG_SPU		0x04
69 #define DS2482_REG_CFG_PPM		0x02
70 #define DS2482_REG_CFG_APU		0x01
71 
72 
73 /**
74  * Write and verify codes for the CHANNEL_SELECT command (DS2482-800 only).
75  * To set the channel, write the value at the index of the channel.
76  * Read and compare against the corresponding value to verify the change.
77  */
78 static const u8 ds2482_chan_wr[8] =
79 	{ 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 };
80 static const u8 ds2482_chan_rd[8] =
81 	{ 0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87 };
82 
83 
84 /**
85  * Status Register bit definitions (read only)
86  */
87 #define DS2482_REG_STS_DIR		0x80
88 #define DS2482_REG_STS_TSB		0x40
89 #define DS2482_REG_STS_SBR		0x20
90 #define DS2482_REG_STS_RST		0x10
91 #define DS2482_REG_STS_LL		0x08
92 #define DS2482_REG_STS_SD		0x04
93 #define DS2482_REG_STS_PPD		0x02
94 #define DS2482_REG_STS_1WB		0x01
95 
96 
97 static int ds2482_probe(struct i2c_client *client,
98 			const struct i2c_device_id *id);
99 static int ds2482_detect(struct i2c_client *client, int kind,
100 			 struct i2c_board_info *info);
101 static int ds2482_remove(struct i2c_client *client);
102 
103 
104 /**
105  * Driver data (common to all clients)
106  */
107 static const struct i2c_device_id ds2482_id[] = {
108 	{ "ds2482", 0 },
109 	{ }
110 };
111 
112 static struct i2c_driver ds2482_driver = {
113 	.driver = {
114 		.owner	= THIS_MODULE,
115 		.name	= "ds2482",
116 	},
117 	.probe		= ds2482_probe,
118 	.remove		= ds2482_remove,
119 	.id_table	= ds2482_id,
120 	.detect		= ds2482_detect,
121 	.address_data	= &addr_data,
122 };
123 
124 /*
125  * Client data (each client gets its own)
126  */
127 
128 struct ds2482_data;
129 
130 struct ds2482_w1_chan {
131 	struct ds2482_data	*pdev;
132 	u8			channel;
133 	struct w1_bus_master	w1_bm;
134 };
135 
136 struct ds2482_data {
137 	struct i2c_client	*client;
138 	struct mutex		access_lock;
139 
140 	/* 1-wire interface(s) */
141 	int			w1_count;	/* 1 or 8 */
142 	struct ds2482_w1_chan	w1_ch[8];
143 
144 	/* per-device values */
145 	u8			channel;
146 	u8			read_prt;	/* see DS2482_PTR_CODE_xxx */
147 	u8			reg_config;
148 };
149 
150 
151 /**
152  * Sets the read pointer.
153  * @param pdev		The ds2482 client pointer
154  * @param read_ptr	see DS2482_PTR_CODE_xxx above
155  * @return -1 on failure, 0 on success
156  */
ds2482_select_register(struct ds2482_data * pdev,u8 read_ptr)157 static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr)
158 {
159 	if (pdev->read_prt != read_ptr) {
160 		if (i2c_smbus_write_byte_data(pdev->client,
161 					      DS2482_CMD_SET_READ_PTR,
162 					      read_ptr) < 0)
163 			return -1;
164 
165 		pdev->read_prt = read_ptr;
166 	}
167 	return 0;
168 }
169 
170 /**
171  * Sends a command without a parameter
172  * @param pdev	The ds2482 client pointer
173  * @param cmd	DS2482_CMD_RESET,
174  *		DS2482_CMD_1WIRE_RESET,
175  *		DS2482_CMD_1WIRE_READ_BYTE
176  * @return -1 on failure, 0 on success
177  */
ds2482_send_cmd(struct ds2482_data * pdev,u8 cmd)178 static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd)
179 {
180 	if (i2c_smbus_write_byte(pdev->client, cmd) < 0)
181 		return -1;
182 
183 	pdev->read_prt = DS2482_PTR_CODE_STATUS;
184 	return 0;
185 }
186 
187 /**
188  * Sends a command with a parameter
189  * @param pdev	The ds2482 client pointer
190  * @param cmd	DS2482_CMD_WRITE_CONFIG,
191  *		DS2482_CMD_1WIRE_SINGLE_BIT,
192  *		DS2482_CMD_1WIRE_WRITE_BYTE,
193  *		DS2482_CMD_1WIRE_TRIPLET
194  * @param byte	The data to send
195  * @return -1 on failure, 0 on success
196  */
ds2482_send_cmd_data(struct ds2482_data * pdev,u8 cmd,u8 byte)197 static inline int ds2482_send_cmd_data(struct ds2482_data *pdev,
198 				       u8 cmd, u8 byte)
199 {
200 	if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0)
201 		return -1;
202 
203 	/* all cmds leave in STATUS, except CONFIG */
204 	pdev->read_prt = (cmd != DS2482_CMD_WRITE_CONFIG) ?
205 			 DS2482_PTR_CODE_STATUS : DS2482_PTR_CODE_CONFIG;
206 	return 0;
207 }
208 
209 
210 /*
211  * 1-Wire interface code
212  */
213 
214 #define DS2482_WAIT_IDLE_TIMEOUT	100
215 
216 /**
217  * Waits until the 1-wire interface is idle (not busy)
218  *
219  * @param pdev Pointer to the device structure
220  * @return the last value read from status or -1 (failure)
221  */
ds2482_wait_1wire_idle(struct ds2482_data * pdev)222 static int ds2482_wait_1wire_idle(struct ds2482_data *pdev)
223 {
224 	int temp = -1;
225 	int retries = 0;
226 
227 	if (!ds2482_select_register(pdev, DS2482_PTR_CODE_STATUS)) {
228 		do {
229 			temp = i2c_smbus_read_byte(pdev->client);
230 		} while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) &&
231 			 (++retries < DS2482_WAIT_IDLE_TIMEOUT));
232 	}
233 
234 	if (retries > DS2482_WAIT_IDLE_TIMEOUT)
235 		printk(KERN_ERR "%s: timeout on channel %d\n",
236 		       __func__, pdev->channel);
237 
238 	return temp;
239 }
240 
241 /**
242  * Selects a w1 channel.
243  * The 1-wire interface must be idle before calling this function.
244  *
245  * @param pdev		The ds2482 client pointer
246  * @param channel	0-7
247  * @return		-1 (failure) or 0 (success)
248  */
ds2482_set_channel(struct ds2482_data * pdev,u8 channel)249 static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel)
250 {
251 	if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT,
252 				      ds2482_chan_wr[channel]) < 0)
253 		return -1;
254 
255 	pdev->read_prt = DS2482_PTR_CODE_CHANNEL;
256 	pdev->channel = -1;
257 	if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) {
258 		pdev->channel = channel;
259 		return 0;
260 	}
261 	return -1;
262 }
263 
264 
265 /**
266  * Performs the touch-bit function, which writes a 0 or 1 and reads the level.
267  *
268  * @param data	The ds2482 channel pointer
269  * @param bit	The level to write: 0 or non-zero
270  * @return	The level read: 0 or 1
271  */
ds2482_w1_touch_bit(void * data,u8 bit)272 static u8 ds2482_w1_touch_bit(void *data, u8 bit)
273 {
274 	struct ds2482_w1_chan *pchan = data;
275 	struct ds2482_data    *pdev = pchan->pdev;
276 	int status = -1;
277 
278 	mutex_lock(&pdev->access_lock);
279 
280 	/* Select the channel */
281 	ds2482_wait_1wire_idle(pdev);
282 	if (pdev->w1_count > 1)
283 		ds2482_set_channel(pdev, pchan->channel);
284 
285 	/* Send the touch command, wait until 1WB == 0, return the status */
286 	if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_SINGLE_BIT,
287 				  bit ? 0xFF : 0))
288 		status = ds2482_wait_1wire_idle(pdev);
289 
290 	mutex_unlock(&pdev->access_lock);
291 
292 	return (status & DS2482_REG_STS_SBR) ? 1 : 0;
293 }
294 
295 /**
296  * Performs the triplet function, which reads two bits and writes a bit.
297  * The bit written is determined by the two reads:
298  *   00 => dbit, 01 => 0, 10 => 1
299  *
300  * @param data	The ds2482 channel pointer
301  * @param dbit	The direction to choose if both branches are valid
302  * @return	b0=read1 b1=read2 b3=bit written
303  */
ds2482_w1_triplet(void * data,u8 dbit)304 static u8 ds2482_w1_triplet(void *data, u8 dbit)
305 {
306 	struct ds2482_w1_chan *pchan = data;
307 	struct ds2482_data    *pdev = pchan->pdev;
308 	int status = (3 << 5);
309 
310 	mutex_lock(&pdev->access_lock);
311 
312 	/* Select the channel */
313 	ds2482_wait_1wire_idle(pdev);
314 	if (pdev->w1_count > 1)
315 		ds2482_set_channel(pdev, pchan->channel);
316 
317 	/* Send the triplet command, wait until 1WB == 0, return the status */
318 	if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_TRIPLET,
319 				  dbit ? 0xFF : 0))
320 		status = ds2482_wait_1wire_idle(pdev);
321 
322 	mutex_unlock(&pdev->access_lock);
323 
324 	/* Decode the status */
325 	return (status >> 5);
326 }
327 
328 /**
329  * Performs the write byte function.
330  *
331  * @param data	The ds2482 channel pointer
332  * @param byte	The value to write
333  */
ds2482_w1_write_byte(void * data,u8 byte)334 static void ds2482_w1_write_byte(void *data, u8 byte)
335 {
336 	struct ds2482_w1_chan *pchan = data;
337 	struct ds2482_data    *pdev = pchan->pdev;
338 
339 	mutex_lock(&pdev->access_lock);
340 
341 	/* Select the channel */
342 	ds2482_wait_1wire_idle(pdev);
343 	if (pdev->w1_count > 1)
344 		ds2482_set_channel(pdev, pchan->channel);
345 
346 	/* Send the write byte command */
347 	ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_WRITE_BYTE, byte);
348 
349 	mutex_unlock(&pdev->access_lock);
350 }
351 
352 /**
353  * Performs the read byte function.
354  *
355  * @param data	The ds2482 channel pointer
356  * @return	The value read
357  */
ds2482_w1_read_byte(void * data)358 static u8 ds2482_w1_read_byte(void *data)
359 {
360 	struct ds2482_w1_chan *pchan = data;
361 	struct ds2482_data    *pdev = pchan->pdev;
362 	int result;
363 
364 	mutex_lock(&pdev->access_lock);
365 
366 	/* Select the channel */
367 	ds2482_wait_1wire_idle(pdev);
368 	if (pdev->w1_count > 1)
369 		ds2482_set_channel(pdev, pchan->channel);
370 
371 	/* Send the read byte command */
372 	ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_READ_BYTE);
373 
374 	/* Wait until 1WB == 0 */
375 	ds2482_wait_1wire_idle(pdev);
376 
377 	/* Select the data register */
378 	ds2482_select_register(pdev, DS2482_PTR_CODE_DATA);
379 
380 	/* Read the data byte */
381 	result = i2c_smbus_read_byte(pdev->client);
382 
383 	mutex_unlock(&pdev->access_lock);
384 
385 	return result;
386 }
387 
388 
389 /**
390  * Sends a reset on the 1-wire interface
391  *
392  * @param data	The ds2482 channel pointer
393  * @return	0=Device present, 1=No device present or error
394  */
ds2482_w1_reset_bus(void * data)395 static u8 ds2482_w1_reset_bus(void *data)
396 {
397 	struct ds2482_w1_chan *pchan = data;
398 	struct ds2482_data    *pdev = pchan->pdev;
399 	int err;
400 	u8 retval = 1;
401 
402 	mutex_lock(&pdev->access_lock);
403 
404 	/* Select the channel */
405 	ds2482_wait_1wire_idle(pdev);
406 	if (pdev->w1_count > 1)
407 		ds2482_set_channel(pdev, pchan->channel);
408 
409 	/* Send the reset command */
410 	err = ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_RESET);
411 	if (err >= 0) {
412 		/* Wait until the reset is complete */
413 		err = ds2482_wait_1wire_idle(pdev);
414 		retval = !(err & DS2482_REG_STS_PPD);
415 
416 		/* If the chip did reset since detect, re-config it */
417 		if (err & DS2482_REG_STS_RST)
418 			ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG,
419 					     0xF0);
420 	}
421 
422 	mutex_unlock(&pdev->access_lock);
423 
424 	return retval;
425 }
426 
427 
ds2482_detect(struct i2c_client * client,int kind,struct i2c_board_info * info)428 static int ds2482_detect(struct i2c_client *client, int kind,
429 			 struct i2c_board_info *info)
430 {
431 	if (!i2c_check_functionality(client->adapter,
432 				     I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
433 				     I2C_FUNC_SMBUS_BYTE))
434 		return -ENODEV;
435 
436 	strlcpy(info->type, "ds2482", I2C_NAME_SIZE);
437 
438 	return 0;
439 }
440 
ds2482_probe(struct i2c_client * client,const struct i2c_device_id * id)441 static int ds2482_probe(struct i2c_client *client,
442 			const struct i2c_device_id *id)
443 {
444 	struct ds2482_data *data;
445 	int err = -ENODEV;
446 	int temp1;
447 	int idx;
448 
449 	if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) {
450 		err = -ENOMEM;
451 		goto exit;
452 	}
453 
454 	data->client = client;
455 	i2c_set_clientdata(client, data);
456 
457 	/* Reset the device (sets the read_ptr to status) */
458 	if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) {
459 		dev_warn(&client->dev, "DS2482 reset failed.\n");
460 		goto exit_free;
461 	}
462 
463 	/* Sleep at least 525ns to allow the reset to complete */
464 	ndelay(525);
465 
466 	/* Read the status byte - only reset bit and line should be set */
467 	temp1 = i2c_smbus_read_byte(client);
468 	if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) {
469 		dev_warn(&client->dev, "DS2482 reset status "
470 			 "0x%02X - not a DS2482\n", temp1);
471 		goto exit_free;
472 	}
473 
474 	/* Detect the 8-port version */
475 	data->w1_count = 1;
476 	if (ds2482_set_channel(data, 7) == 0)
477 		data->w1_count = 8;
478 
479 	/* Set all config items to 0 (off) */
480 	ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG, 0xF0);
481 
482 	mutex_init(&data->access_lock);
483 
484 	/* Register 1-wire interface(s) */
485 	for (idx = 0; idx < data->w1_count; idx++) {
486 		data->w1_ch[idx].pdev = data;
487 		data->w1_ch[idx].channel = idx;
488 
489 		/* Populate all the w1 bus master stuff */
490 		data->w1_ch[idx].w1_bm.data       = &data->w1_ch[idx];
491 		data->w1_ch[idx].w1_bm.read_byte  = ds2482_w1_read_byte;
492 		data->w1_ch[idx].w1_bm.write_byte = ds2482_w1_write_byte;
493 		data->w1_ch[idx].w1_bm.touch_bit  = ds2482_w1_touch_bit;
494 		data->w1_ch[idx].w1_bm.triplet    = ds2482_w1_triplet;
495 		data->w1_ch[idx].w1_bm.reset_bus  = ds2482_w1_reset_bus;
496 
497 		err = w1_add_master_device(&data->w1_ch[idx].w1_bm);
498 		if (err) {
499 			data->w1_ch[idx].pdev = NULL;
500 			goto exit_w1_remove;
501 		}
502 	}
503 
504 	return 0;
505 
506 exit_w1_remove:
507 	for (idx = 0; idx < data->w1_count; idx++) {
508 		if (data->w1_ch[idx].pdev != NULL)
509 			w1_remove_master_device(&data->w1_ch[idx].w1_bm);
510 	}
511 exit_free:
512 	kfree(data);
513 exit:
514 	return err;
515 }
516 
ds2482_remove(struct i2c_client * client)517 static int ds2482_remove(struct i2c_client *client)
518 {
519 	struct ds2482_data   *data = i2c_get_clientdata(client);
520 	int idx;
521 
522 	/* Unregister the 1-wire bridge(s) */
523 	for (idx = 0; idx < data->w1_count; idx++) {
524 		if (data->w1_ch[idx].pdev != NULL)
525 			w1_remove_master_device(&data->w1_ch[idx].w1_bm);
526 	}
527 
528 	/* Free the memory */
529 	kfree(data);
530 	return 0;
531 }
532 
sensors_ds2482_init(void)533 static int __init sensors_ds2482_init(void)
534 {
535 	return i2c_add_driver(&ds2482_driver);
536 }
537 
sensors_ds2482_exit(void)538 static void __exit sensors_ds2482_exit(void)
539 {
540 	i2c_del_driver(&ds2482_driver);
541 }
542 
543 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
544 MODULE_DESCRIPTION("DS2482 driver");
545 MODULE_LICENSE("GPL");
546 
547 module_init(sensors_ds2482_init);
548 module_exit(sensors_ds2482_exit);
549