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