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