xref: /openbmc/linux/drivers/w1/masters/ds2482.c (revision 4da722ca)
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 <linux/w1.h>
24 
25 /**
26  * Allow the active pullup to be disabled, default is enabled.
27  *
28  * Note from the DS2482 datasheet:
29  * The APU bit controls whether an active pullup (controlled slew-rate
30  * transistor) or a passive pullup (Rwpu resistor) will be used to drive
31  * a 1-Wire line from low to high. When APU = 0, active pullup is disabled
32  * (resistor mode). Active Pullup should always be selected unless there is
33  * only a single slave on the 1-Wire line.
34  */
35 static int ds2482_active_pullup = 1;
36 module_param_named(active_pullup, ds2482_active_pullup, int, 0644);
37 MODULE_PARM_DESC(active_pullup, "Active pullup (apply to all buses): " \
38 				"0-disable, 1-enable (default)");
39 
40 /**
41  * The DS2482 registers - there are 3 registers that are addressed by a read
42  * pointer. The read pointer is set by the last command executed.
43  *
44  * To read the data, issue a register read for any address
45  */
46 #define DS2482_CMD_RESET		0xF0	/* No param */
47 #define DS2482_CMD_SET_READ_PTR		0xE1	/* Param: DS2482_PTR_CODE_xxx */
48 #define DS2482_CMD_CHANNEL_SELECT	0xC3	/* Param: Channel byte - DS2482-800 only */
49 #define DS2482_CMD_WRITE_CONFIG		0xD2	/* Param: Config byte */
50 #define DS2482_CMD_1WIRE_RESET		0xB4	/* Param: None */
51 #define DS2482_CMD_1WIRE_SINGLE_BIT	0x87	/* Param: Bit byte (bit7) */
52 #define DS2482_CMD_1WIRE_WRITE_BYTE	0xA5	/* Param: Data byte */
53 #define DS2482_CMD_1WIRE_READ_BYTE	0x96	/* Param: None */
54 /* Note to read the byte, Set the ReadPtr to Data then read (any addr) */
55 #define DS2482_CMD_1WIRE_TRIPLET	0x78	/* Param: Dir byte (bit7) */
56 
57 /* Values for DS2482_CMD_SET_READ_PTR */
58 #define DS2482_PTR_CODE_STATUS		0xF0
59 #define DS2482_PTR_CODE_DATA		0xE1
60 #define DS2482_PTR_CODE_CHANNEL		0xD2	/* DS2482-800 only */
61 #define DS2482_PTR_CODE_CONFIG		0xC3
62 
63 /**
64  * Configure Register bit definitions
65  * The top 4 bits always read 0.
66  * To write, the top nibble must be the 1's compl. of the low nibble.
67  */
68 #define DS2482_REG_CFG_1WS		0x08	/* 1-wire speed */
69 #define DS2482_REG_CFG_SPU		0x04	/* strong pull-up */
70 #define DS2482_REG_CFG_PPM		0x02	/* presence pulse masking */
71 #define DS2482_REG_CFG_APU		0x01	/* active pull-up */
72 
73 
74 /**
75  * Write and verify codes for the CHANNEL_SELECT command (DS2482-800 only).
76  * To set the channel, write the value at the index of the channel.
77  * Read and compare against the corresponding value to verify the change.
78  */
79 static const u8 ds2482_chan_wr[8] =
80 	{ 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 };
81 static const u8 ds2482_chan_rd[8] =
82 	{ 0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87 };
83 
84 
85 /**
86  * Status Register bit definitions (read only)
87  */
88 #define DS2482_REG_STS_DIR		0x80
89 #define DS2482_REG_STS_TSB		0x40
90 #define DS2482_REG_STS_SBR		0x20
91 #define DS2482_REG_STS_RST		0x10
92 #define DS2482_REG_STS_LL		0x08
93 #define DS2482_REG_STS_SD		0x04
94 #define DS2482_REG_STS_PPD		0x02
95 #define DS2482_REG_STS_1WB		0x01
96 
97 /*
98  * Client data (each client gets its own)
99  */
100 
101 struct ds2482_data;
102 
103 struct ds2482_w1_chan {
104 	struct ds2482_data	*pdev;
105 	u8			channel;
106 	struct w1_bus_master	w1_bm;
107 };
108 
109 struct ds2482_data {
110 	struct i2c_client	*client;
111 	struct mutex		access_lock;
112 
113 	/* 1-wire interface(s) */
114 	int			w1_count;	/* 1 or 8 */
115 	struct ds2482_w1_chan	w1_ch[8];
116 
117 	/* per-device values */
118 	u8			channel;
119 	u8			read_prt;	/* see DS2482_PTR_CODE_xxx */
120 	u8			reg_config;
121 };
122 
123 
124 /**
125  * Helper to calculate values for configuration register
126  * @param conf the raw config value
127  * @return the value w/ complements that can be written to register
128  */
129 static inline u8 ds2482_calculate_config(u8 conf)
130 {
131 	if (ds2482_active_pullup)
132 		conf |= DS2482_REG_CFG_APU;
133 
134 	return conf | ((~conf & 0x0f) << 4);
135 }
136 
137 
138 /**
139  * Sets the read pointer.
140  * @param pdev		The ds2482 client pointer
141  * @param read_ptr	see DS2482_PTR_CODE_xxx above
142  * @return -1 on failure, 0 on success
143  */
144 static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr)
145 {
146 	if (pdev->read_prt != read_ptr) {
147 		if (i2c_smbus_write_byte_data(pdev->client,
148 					      DS2482_CMD_SET_READ_PTR,
149 					      read_ptr) < 0)
150 			return -1;
151 
152 		pdev->read_prt = read_ptr;
153 	}
154 	return 0;
155 }
156 
157 /**
158  * Sends a command without a parameter
159  * @param pdev	The ds2482 client pointer
160  * @param cmd	DS2482_CMD_RESET,
161  *		DS2482_CMD_1WIRE_RESET,
162  *		DS2482_CMD_1WIRE_READ_BYTE
163  * @return -1 on failure, 0 on success
164  */
165 static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd)
166 {
167 	if (i2c_smbus_write_byte(pdev->client, cmd) < 0)
168 		return -1;
169 
170 	pdev->read_prt = DS2482_PTR_CODE_STATUS;
171 	return 0;
172 }
173 
174 /**
175  * Sends a command with a parameter
176  * @param pdev	The ds2482 client pointer
177  * @param cmd	DS2482_CMD_WRITE_CONFIG,
178  *		DS2482_CMD_1WIRE_SINGLE_BIT,
179  *		DS2482_CMD_1WIRE_WRITE_BYTE,
180  *		DS2482_CMD_1WIRE_TRIPLET
181  * @param byte	The data to send
182  * @return -1 on failure, 0 on success
183  */
184 static inline int ds2482_send_cmd_data(struct ds2482_data *pdev,
185 				       u8 cmd, u8 byte)
186 {
187 	if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0)
188 		return -1;
189 
190 	/* all cmds leave in STATUS, except CONFIG */
191 	pdev->read_prt = (cmd != DS2482_CMD_WRITE_CONFIG) ?
192 			 DS2482_PTR_CODE_STATUS : DS2482_PTR_CODE_CONFIG;
193 	return 0;
194 }
195 
196 
197 /*
198  * 1-Wire interface code
199  */
200 
201 #define DS2482_WAIT_IDLE_TIMEOUT	100
202 
203 /**
204  * Waits until the 1-wire interface is idle (not busy)
205  *
206  * @param pdev Pointer to the device structure
207  * @return the last value read from status or -1 (failure)
208  */
209 static int ds2482_wait_1wire_idle(struct ds2482_data *pdev)
210 {
211 	int temp = -1;
212 	int retries = 0;
213 
214 	if (!ds2482_select_register(pdev, DS2482_PTR_CODE_STATUS)) {
215 		do {
216 			temp = i2c_smbus_read_byte(pdev->client);
217 		} while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) &&
218 			 (++retries < DS2482_WAIT_IDLE_TIMEOUT));
219 	}
220 
221 	if (retries >= DS2482_WAIT_IDLE_TIMEOUT)
222 		pr_err("%s: timeout on channel %d\n",
223 		       __func__, pdev->channel);
224 
225 	return temp;
226 }
227 
228 /**
229  * Selects a w1 channel.
230  * The 1-wire interface must be idle before calling this function.
231  *
232  * @param pdev		The ds2482 client pointer
233  * @param channel	0-7
234  * @return		-1 (failure) or 0 (success)
235  */
236 static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel)
237 {
238 	if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT,
239 				      ds2482_chan_wr[channel]) < 0)
240 		return -1;
241 
242 	pdev->read_prt = DS2482_PTR_CODE_CHANNEL;
243 	pdev->channel = -1;
244 	if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) {
245 		pdev->channel = channel;
246 		return 0;
247 	}
248 	return -1;
249 }
250 
251 
252 /**
253  * Performs the touch-bit function, which writes a 0 or 1 and reads the level.
254  *
255  * @param data	The ds2482 channel pointer
256  * @param bit	The level to write: 0 or non-zero
257  * @return	The level read: 0 or 1
258  */
259 static u8 ds2482_w1_touch_bit(void *data, u8 bit)
260 {
261 	struct ds2482_w1_chan *pchan = data;
262 	struct ds2482_data    *pdev = pchan->pdev;
263 	int status = -1;
264 
265 	mutex_lock(&pdev->access_lock);
266 
267 	/* Select the channel */
268 	ds2482_wait_1wire_idle(pdev);
269 	if (pdev->w1_count > 1)
270 		ds2482_set_channel(pdev, pchan->channel);
271 
272 	/* Send the touch command, wait until 1WB == 0, return the status */
273 	if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_SINGLE_BIT,
274 				  bit ? 0xFF : 0))
275 		status = ds2482_wait_1wire_idle(pdev);
276 
277 	mutex_unlock(&pdev->access_lock);
278 
279 	return (status & DS2482_REG_STS_SBR) ? 1 : 0;
280 }
281 
282 /**
283  * Performs the triplet function, which reads two bits and writes a bit.
284  * The bit written is determined by the two reads:
285  *   00 => dbit, 01 => 0, 10 => 1
286  *
287  * @param data	The ds2482 channel pointer
288  * @param dbit	The direction to choose if both branches are valid
289  * @return	b0=read1 b1=read2 b3=bit written
290  */
291 static u8 ds2482_w1_triplet(void *data, u8 dbit)
292 {
293 	struct ds2482_w1_chan *pchan = data;
294 	struct ds2482_data    *pdev = pchan->pdev;
295 	int status = (3 << 5);
296 
297 	mutex_lock(&pdev->access_lock);
298 
299 	/* Select the channel */
300 	ds2482_wait_1wire_idle(pdev);
301 	if (pdev->w1_count > 1)
302 		ds2482_set_channel(pdev, pchan->channel);
303 
304 	/* Send the triplet command, wait until 1WB == 0, return the status */
305 	if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_TRIPLET,
306 				  dbit ? 0xFF : 0))
307 		status = ds2482_wait_1wire_idle(pdev);
308 
309 	mutex_unlock(&pdev->access_lock);
310 
311 	/* Decode the status */
312 	return (status >> 5);
313 }
314 
315 /**
316  * Performs the write byte function.
317  *
318  * @param data	The ds2482 channel pointer
319  * @param byte	The value to write
320  */
321 static void ds2482_w1_write_byte(void *data, u8 byte)
322 {
323 	struct ds2482_w1_chan *pchan = data;
324 	struct ds2482_data    *pdev = pchan->pdev;
325 
326 	mutex_lock(&pdev->access_lock);
327 
328 	/* Select the channel */
329 	ds2482_wait_1wire_idle(pdev);
330 	if (pdev->w1_count > 1)
331 		ds2482_set_channel(pdev, pchan->channel);
332 
333 	/* Send the write byte command */
334 	ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_WRITE_BYTE, byte);
335 
336 	mutex_unlock(&pdev->access_lock);
337 }
338 
339 /**
340  * Performs the read byte function.
341  *
342  * @param data	The ds2482 channel pointer
343  * @return	The value read
344  */
345 static u8 ds2482_w1_read_byte(void *data)
346 {
347 	struct ds2482_w1_chan *pchan = data;
348 	struct ds2482_data    *pdev = pchan->pdev;
349 	int result;
350 
351 	mutex_lock(&pdev->access_lock);
352 
353 	/* Select the channel */
354 	ds2482_wait_1wire_idle(pdev);
355 	if (pdev->w1_count > 1)
356 		ds2482_set_channel(pdev, pchan->channel);
357 
358 	/* Send the read byte command */
359 	ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_READ_BYTE);
360 
361 	/* Wait until 1WB == 0 */
362 	ds2482_wait_1wire_idle(pdev);
363 
364 	/* Select the data register */
365 	ds2482_select_register(pdev, DS2482_PTR_CODE_DATA);
366 
367 	/* Read the data byte */
368 	result = i2c_smbus_read_byte(pdev->client);
369 
370 	mutex_unlock(&pdev->access_lock);
371 
372 	return result;
373 }
374 
375 
376 /**
377  * Sends a reset on the 1-wire interface
378  *
379  * @param data	The ds2482 channel pointer
380  * @return	0=Device present, 1=No device present or error
381  */
382 static u8 ds2482_w1_reset_bus(void *data)
383 {
384 	struct ds2482_w1_chan *pchan = data;
385 	struct ds2482_data    *pdev = pchan->pdev;
386 	int err;
387 	u8 retval = 1;
388 
389 	mutex_lock(&pdev->access_lock);
390 
391 	/* Select the channel */
392 	ds2482_wait_1wire_idle(pdev);
393 	if (pdev->w1_count > 1)
394 		ds2482_set_channel(pdev, pchan->channel);
395 
396 	/* Send the reset command */
397 	err = ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_RESET);
398 	if (err >= 0) {
399 		/* Wait until the reset is complete */
400 		err = ds2482_wait_1wire_idle(pdev);
401 		retval = !(err & DS2482_REG_STS_PPD);
402 
403 		/* If the chip did reset since detect, re-config it */
404 		if (err & DS2482_REG_STS_RST)
405 			ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG,
406 					     ds2482_calculate_config(0x00));
407 	}
408 
409 	mutex_unlock(&pdev->access_lock);
410 
411 	return retval;
412 }
413 
414 static u8 ds2482_w1_set_pullup(void *data, int delay)
415 {
416 	struct ds2482_w1_chan *pchan = data;
417 	struct ds2482_data    *pdev = pchan->pdev;
418 	u8 retval = 1;
419 
420 	/* if delay is non-zero activate the pullup,
421 	 * the strong pullup will be automatically deactivated
422 	 * by the master, so do not explicitly deactive it
423 	 */
424 	if (delay) {
425 		/* both waits are crucial, otherwise devices might not be
426 		 * powered long enough, causing e.g. a w1_therm sensor to
427 		 * provide wrong conversion results
428 		 */
429 		ds2482_wait_1wire_idle(pdev);
430 		/* note: it seems like both SPU and APU have to be set! */
431 		retval = ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG,
432 			ds2482_calculate_config(DS2482_REG_CFG_SPU |
433 						DS2482_REG_CFG_APU));
434 		ds2482_wait_1wire_idle(pdev);
435 	}
436 
437 	return retval;
438 }
439 
440 
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 (!i2c_check_functionality(client->adapter,
450 				     I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
451 				     I2C_FUNC_SMBUS_BYTE))
452 		return -ENODEV;
453 
454 	if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) {
455 		err = -ENOMEM;
456 		goto exit;
457 	}
458 
459 	data->client = client;
460 	i2c_set_clientdata(client, data);
461 
462 	/* Reset the device (sets the read_ptr to status) */
463 	if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) {
464 		dev_warn(&client->dev, "DS2482 reset failed.\n");
465 		goto exit_free;
466 	}
467 
468 	/* Sleep at least 525ns to allow the reset to complete */
469 	ndelay(525);
470 
471 	/* Read the status byte - only reset bit and line should be set */
472 	temp1 = i2c_smbus_read_byte(client);
473 	if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) {
474 		dev_warn(&client->dev, "DS2482 reset status "
475 			 "0x%02X - not a DS2482\n", temp1);
476 		goto exit_free;
477 	}
478 
479 	/* Detect the 8-port version */
480 	data->w1_count = 1;
481 	if (ds2482_set_channel(data, 7) == 0)
482 		data->w1_count = 8;
483 
484 	/* Set all config items to 0 (off) */
485 	ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG,
486 		ds2482_calculate_config(0x00));
487 
488 	mutex_init(&data->access_lock);
489 
490 	/* Register 1-wire interface(s) */
491 	for (idx = 0; idx < data->w1_count; idx++) {
492 		data->w1_ch[idx].pdev = data;
493 		data->w1_ch[idx].channel = idx;
494 
495 		/* Populate all the w1 bus master stuff */
496 		data->w1_ch[idx].w1_bm.data       = &data->w1_ch[idx];
497 		data->w1_ch[idx].w1_bm.read_byte  = ds2482_w1_read_byte;
498 		data->w1_ch[idx].w1_bm.write_byte = ds2482_w1_write_byte;
499 		data->w1_ch[idx].w1_bm.touch_bit  = ds2482_w1_touch_bit;
500 		data->w1_ch[idx].w1_bm.triplet    = ds2482_w1_triplet;
501 		data->w1_ch[idx].w1_bm.reset_bus  = ds2482_w1_reset_bus;
502 		data->w1_ch[idx].w1_bm.set_pullup = ds2482_w1_set_pullup;
503 
504 		err = w1_add_master_device(&data->w1_ch[idx].w1_bm);
505 		if (err) {
506 			data->w1_ch[idx].pdev = NULL;
507 			goto exit_w1_remove;
508 		}
509 	}
510 
511 	return 0;
512 
513 exit_w1_remove:
514 	for (idx = 0; idx < data->w1_count; idx++) {
515 		if (data->w1_ch[idx].pdev != NULL)
516 			w1_remove_master_device(&data->w1_ch[idx].w1_bm);
517 	}
518 exit_free:
519 	kfree(data);
520 exit:
521 	return err;
522 }
523 
524 static int ds2482_remove(struct i2c_client *client)
525 {
526 	struct ds2482_data   *data = i2c_get_clientdata(client);
527 	int idx;
528 
529 	/* Unregister the 1-wire bridge(s) */
530 	for (idx = 0; idx < data->w1_count; idx++) {
531 		if (data->w1_ch[idx].pdev != NULL)
532 			w1_remove_master_device(&data->w1_ch[idx].w1_bm);
533 	}
534 
535 	/* Free the memory */
536 	kfree(data);
537 	return 0;
538 }
539 
540 /**
541  * Driver data (common to all clients)
542  */
543 static const struct i2c_device_id ds2482_id[] = {
544 	{ "ds2482", 0 },
545 	{ }
546 };
547 MODULE_DEVICE_TABLE(i2c, ds2482_id);
548 
549 static struct i2c_driver ds2482_driver = {
550 	.driver = {
551 		.name	= "ds2482",
552 	},
553 	.probe		= ds2482_probe,
554 	.remove		= ds2482_remove,
555 	.id_table	= ds2482_id,
556 };
557 module_i2c_driver(ds2482_driver);
558 
559 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
560 MODULE_DESCRIPTION("DS2482 driver");
561 MODULE_LICENSE("GPL");
562