[go: nahoru, domu]

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