tokenring: convert smctr to net_device_ops
[linux-2.6/mini2440.git] / drivers / w1 / masters / ds2482.c
blobdf52cb355f7dd10b093bff43796c283c62c9bb26
1 /**
2 * ds2482.c - provides i2c to w1-master bridge(s)
3 * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
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
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.
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>
23 #include "../w1.h"
24 #include "../w1_int.h"
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.
32 static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
34 /**
35 * Insmod parameters
37 I2C_CLIENT_INSMOD_1(ds2482);
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.
43 * To read the data, issue a register read for any address
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) */
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
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.
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
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.
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 };
84 /**
85 * Status Register bit definitions (read only)
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
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);
105 * Driver data (common to all clients)
107 static const struct i2c_device_id ds2482_id[] = {
108 { "ds2482", 0 },
112 static struct i2c_driver ds2482_driver = {
113 .driver = {
114 .owner = THIS_MODULE,
115 .name = "ds2482",
117 .probe = ds2482_probe,
118 .remove = ds2482_remove,
119 .id_table = ds2482_id,
120 .detect = ds2482_detect,
121 .address_data = &addr_data,
125 * Client data (each client gets its own)
128 struct ds2482_data;
130 struct ds2482_w1_chan {
131 struct ds2482_data *pdev;
132 u8 channel;
133 struct w1_bus_master w1_bm;
136 struct ds2482_data {
137 struct i2c_client *client;
138 struct mutex access_lock;
140 /* 1-wire interface(s) */
141 int w1_count; /* 1 or 8 */
142 struct ds2482_w1_chan w1_ch[8];
144 /* per-device values */
145 u8 channel;
146 u8 read_prt; /* see DS2482_PTR_CODE_xxx */
147 u8 reg_config;
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
157 static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr)
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;
165 pdev->read_prt = read_ptr;
167 return 0;
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
178 static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd)
180 if (i2c_smbus_write_byte(pdev->client, cmd) < 0)
181 return -1;
183 pdev->read_prt = DS2482_PTR_CODE_STATUS;
184 return 0;
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
197 static inline int ds2482_send_cmd_data(struct ds2482_data *pdev,
198 u8 cmd, u8 byte)
200 if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0)
201 return -1;
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;
211 * 1-Wire interface code
214 #define DS2482_WAIT_IDLE_TIMEOUT 100
217 * Waits until the 1-wire interface is idle (not busy)
219 * @param pdev Pointer to the device structure
220 * @return the last value read from status or -1 (failure)
222 static int ds2482_wait_1wire_idle(struct ds2482_data *pdev)
224 int temp = -1;
225 int retries = 0;
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));
234 if (retries > DS2482_WAIT_IDLE_TIMEOUT)
235 printk(KERN_ERR "%s: timeout on channel %d\n",
236 __func__, pdev->channel);
238 return temp;
242 * Selects a w1 channel.
243 * The 1-wire interface must be idle before calling this function.
245 * @param pdev The ds2482 client pointer
246 * @param channel 0-7
247 * @return -1 (failure) or 0 (success)
249 static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel)
251 if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT,
252 ds2482_chan_wr[channel]) < 0)
253 return -1;
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;
261 return -1;
266 * Performs the touch-bit function, which writes a 0 or 1 and reads the level.
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
272 static u8 ds2482_w1_touch_bit(void *data, u8 bit)
274 struct ds2482_w1_chan *pchan = data;
275 struct ds2482_data *pdev = pchan->pdev;
276 int status = -1;
278 mutex_lock(&pdev->access_lock);
280 /* Select the channel */
281 ds2482_wait_1wire_idle(pdev);
282 if (pdev->w1_count > 1)
283 ds2482_set_channel(pdev, pchan->channel);
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);
290 mutex_unlock(&pdev->access_lock);
292 return (status & DS2482_REG_STS_SBR) ? 1 : 0;
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
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
304 static u8 ds2482_w1_triplet(void *data, u8 dbit)
306 struct ds2482_w1_chan *pchan = data;
307 struct ds2482_data *pdev = pchan->pdev;
308 int status = (3 << 5);
310 mutex_lock(&pdev->access_lock);
312 /* Select the channel */
313 ds2482_wait_1wire_idle(pdev);
314 if (pdev->w1_count > 1)
315 ds2482_set_channel(pdev, pchan->channel);
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);
322 mutex_unlock(&pdev->access_lock);
324 /* Decode the status */
325 return (status >> 5);
329 * Performs the write byte function.
331 * @param data The ds2482 channel pointer
332 * @param byte The value to write
334 static void ds2482_w1_write_byte(void *data, u8 byte)
336 struct ds2482_w1_chan *pchan = data;
337 struct ds2482_data *pdev = pchan->pdev;
339 mutex_lock(&pdev->access_lock);
341 /* Select the channel */
342 ds2482_wait_1wire_idle(pdev);
343 if (pdev->w1_count > 1)
344 ds2482_set_channel(pdev, pchan->channel);
346 /* Send the write byte command */
347 ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_WRITE_BYTE, byte);
349 mutex_unlock(&pdev->access_lock);
353 * Performs the read byte function.
355 * @param data The ds2482 channel pointer
356 * @return The value read
358 static u8 ds2482_w1_read_byte(void *data)
360 struct ds2482_w1_chan *pchan = data;
361 struct ds2482_data *pdev = pchan->pdev;
362 int result;
364 mutex_lock(&pdev->access_lock);
366 /* Select the channel */
367 ds2482_wait_1wire_idle(pdev);
368 if (pdev->w1_count > 1)
369 ds2482_set_channel(pdev, pchan->channel);
371 /* Send the read byte command */
372 ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_READ_BYTE);
374 /* Wait until 1WB == 0 */
375 ds2482_wait_1wire_idle(pdev);
377 /* Select the data register */
378 ds2482_select_register(pdev, DS2482_PTR_CODE_DATA);
380 /* Read the data byte */
381 result = i2c_smbus_read_byte(pdev->client);
383 mutex_unlock(&pdev->access_lock);
385 return result;
390 * Sends a reset on the 1-wire interface
392 * @param data The ds2482 channel pointer
393 * @return 0=Device present, 1=No device present or error
395 static u8 ds2482_w1_reset_bus(void *data)
397 struct ds2482_w1_chan *pchan = data;
398 struct ds2482_data *pdev = pchan->pdev;
399 int err;
400 u8 retval = 1;
402 mutex_lock(&pdev->access_lock);
404 /* Select the channel */
405 ds2482_wait_1wire_idle(pdev);
406 if (pdev->w1_count > 1)
407 ds2482_set_channel(pdev, pchan->channel);
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);
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);
422 mutex_unlock(&pdev->access_lock);
424 return retval;
428 static int ds2482_detect(struct i2c_client *client, int kind,
429 struct i2c_board_info *info)
431 if (!i2c_check_functionality(client->adapter,
432 I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
433 I2C_FUNC_SMBUS_BYTE))
434 return -ENODEV;
436 strlcpy(info->type, "ds2482", I2C_NAME_SIZE);
438 return 0;
441 static int ds2482_probe(struct i2c_client *client,
442 const struct i2c_device_id *id)
444 struct ds2482_data *data;
445 int err = -ENODEV;
446 int temp1;
447 int idx;
449 if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) {
450 err = -ENOMEM;
451 goto exit;
454 data->client = client;
455 i2c_set_clientdata(client, data);
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;
463 /* Sleep at least 525ns to allow the reset to complete */
464 ndelay(525);
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;
474 /* Detect the 8-port version */
475 data->w1_count = 1;
476 if (ds2482_set_channel(data, 7) == 0)
477 data->w1_count = 8;
479 /* Set all config items to 0 (off) */
480 ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG, 0xF0);
482 mutex_init(&data->access_lock);
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;
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;
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;
504 return 0;
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);
511 exit_free:
512 kfree(data);
513 exit:
514 return err;
517 static int ds2482_remove(struct i2c_client *client)
519 struct ds2482_data *data = i2c_get_clientdata(client);
520 int idx;
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);
528 /* Free the memory */
529 kfree(data);
530 return 0;
533 static int __init sensors_ds2482_init(void)
535 return i2c_add_driver(&ds2482_driver);
538 static void __exit sensors_ds2482_exit(void)
540 i2c_del_driver(&ds2482_driver);
543 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
544 MODULE_DESCRIPTION("DS2482 driver");
545 MODULE_LICENSE("GPL");
547 module_init(sensors_ds2482_init);
548 module_exit(sensors_ds2482_exit);