blob: d409352fe51fe97f20b263ba818b5d7948582cab [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/**
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
57#define DS2482_REG_CFG_SPU 0x04
58#define DS2482_REG_CFG_PPM 0x02
59#define DS2482_REG_CFG_APU 0x01
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 * Sets the read pointer.
146 * @param pdev The ds2482 client pointer
147 * @param read_ptr see DS2482_PTR_CODE_xxx above
148 * @return -1 on failure, 0 on success
149 */
150static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr)
151{
152 if (pdev->read_prt != read_ptr) {
153 if (i2c_smbus_write_byte_data(pdev->client,
154 DS2482_CMD_SET_READ_PTR,
155 read_ptr) < 0)
156 return -1;
157
158 pdev->read_prt = read_ptr;
159 }
160 return 0;
161}
162
163/**
164 * Sends a command without a parameter
165 * @param pdev The ds2482 client pointer
166 * @param cmd DS2482_CMD_RESET,
167 * DS2482_CMD_1WIRE_RESET,
168 * DS2482_CMD_1WIRE_READ_BYTE
169 * @return -1 on failure, 0 on success
170 */
171static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd)
172{
173 if (i2c_smbus_write_byte(pdev->client, cmd) < 0)
174 return -1;
175
176 pdev->read_prt = DS2482_PTR_CODE_STATUS;
177 return 0;
178}
179
180/**
181 * Sends a command with a parameter
182 * @param pdev The ds2482 client pointer
183 * @param cmd DS2482_CMD_WRITE_CONFIG,
184 * DS2482_CMD_1WIRE_SINGLE_BIT,
185 * DS2482_CMD_1WIRE_WRITE_BYTE,
186 * DS2482_CMD_1WIRE_TRIPLET
187 * @param byte The data to send
188 * @return -1 on failure, 0 on success
189 */
190static inline int ds2482_send_cmd_data(struct ds2482_data *pdev,
191 u8 cmd, u8 byte)
192{
193 if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0)
194 return -1;
195
196 /* all cmds leave in STATUS, except CONFIG */
197 pdev->read_prt = (cmd != DS2482_CMD_WRITE_CONFIG) ?
198 DS2482_PTR_CODE_STATUS : DS2482_PTR_CODE_CONFIG;
199 return 0;
200}
201
202
203/*
204 * 1-Wire interface code
205 */
206
207#define DS2482_WAIT_IDLE_TIMEOUT 100
208
209/**
210 * Waits until the 1-wire interface is idle (not busy)
211 *
212 * @param pdev Pointer to the device structure
213 * @return the last value read from status or -1 (failure)
214 */
215static int ds2482_wait_1wire_idle(struct ds2482_data *pdev)
216{
217 int temp = -1;
218 int retries = 0;
219
220 if (!ds2482_select_register(pdev, DS2482_PTR_CODE_STATUS)) {
221 do {
222 temp = i2c_smbus_read_byte(pdev->client);
223 } while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) &&
224 (++retries < DS2482_WAIT_IDLE_TIMEOUT));
225 }
226
227 if (retries >= DS2482_WAIT_IDLE_TIMEOUT)
228 printk(KERN_ERR "%s: timeout on channel %d\n",
229 __func__, pdev->channel);
230
231 return temp;
232}
233
234/**
235 * Selects a w1 channel.
236 * The 1-wire interface must be idle before calling this function.
237 *
238 * @param pdev The ds2482 client pointer
239 * @param channel 0-7
240 * @return -1 (failure) or 0 (success)
241 */
242static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel)
243{
244 if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT,
245 ds2482_chan_wr[channel]) < 0)
246 return -1;
247
248 pdev->read_prt = DS2482_PTR_CODE_CHANNEL;
249 pdev->channel = -1;
250 if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) {
251 pdev->channel = channel;
252 return 0;
253 }
254 return -1;
255}
256
257
258/**
259 * Performs the touch-bit function, which writes a 0 or 1 and reads the level.
260 *
261 * @param data The ds2482 channel pointer
262 * @param bit The level to write: 0 or non-zero
263 * @return The level read: 0 or 1
264 */
265static u8 ds2482_w1_touch_bit(void *data, u8 bit)
266{
267 struct ds2482_w1_chan *pchan = data;
268 struct ds2482_data *pdev = pchan->pdev;
269 int status = -1;
270
271 mutex_lock(&pdev->access_lock);
272
273 /* Select the channel */
274 ds2482_wait_1wire_idle(pdev);
275 if (pdev->w1_count > 1)
276 ds2482_set_channel(pdev, pchan->channel);
277
278 /* Send the touch command, wait until 1WB == 0, return the status */
279 if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_SINGLE_BIT,
280 bit ? 0xFF : 0))
281 status = ds2482_wait_1wire_idle(pdev);
282
283 mutex_unlock(&pdev->access_lock);
284
285 return (status & DS2482_REG_STS_SBR) ? 1 : 0;
286}
287
288/**
289 * Performs the triplet function, which reads two bits and writes a bit.
290 * The bit written is determined by the two reads:
291 * 00 => dbit, 01 => 0, 10 => 1
292 *
293 * @param data The ds2482 channel pointer
294 * @param dbit The direction to choose if both branches are valid
295 * @return b0=read1 b1=read2 b3=bit written
296 */
297static u8 ds2482_w1_triplet(void *data, u8 dbit)
298{
299 struct ds2482_w1_chan *pchan = data;
300 struct ds2482_data *pdev = pchan->pdev;
301 int status = (3 << 5);
302
303 mutex_lock(&pdev->access_lock);
304
305 /* Select the channel */
306 ds2482_wait_1wire_idle(pdev);
307 if (pdev->w1_count > 1)
308 ds2482_set_channel(pdev, pchan->channel);
309
310 /* Send the triplet command, wait until 1WB == 0, return the status */
311 if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_TRIPLET,
312 dbit ? 0xFF : 0))
313 status = ds2482_wait_1wire_idle(pdev);
314
315 mutex_unlock(&pdev->access_lock);
316
317 /* Decode the status */
318 return (status >> 5);
319}
320
321/**
322 * Performs the write byte function.
323 *
324 * @param data The ds2482 channel pointer
325 * @param byte The value to write
326 */
327static void ds2482_w1_write_byte(void *data, u8 byte)
328{
329 struct ds2482_w1_chan *pchan = data;
330 struct ds2482_data *pdev = pchan->pdev;
331
332 mutex_lock(&pdev->access_lock);
333
334 /* Select the channel */
335 ds2482_wait_1wire_idle(pdev);
336 if (pdev->w1_count > 1)
337 ds2482_set_channel(pdev, pchan->channel);
338
339 /* Send the write byte command */
340 ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_WRITE_BYTE, byte);
341
342 mutex_unlock(&pdev->access_lock);
343}
344
345/**
346 * Performs the read byte function.
347 *
348 * @param data The ds2482 channel pointer
349 * @return The value read
350 */
351static u8 ds2482_w1_read_byte(void *data)
352{
353 struct ds2482_w1_chan *pchan = data;
354 struct ds2482_data *pdev = pchan->pdev;
355 int result;
356
357 mutex_lock(&pdev->access_lock);
358
359 /* Select the channel */
360 ds2482_wait_1wire_idle(pdev);
361 if (pdev->w1_count > 1)
362 ds2482_set_channel(pdev, pchan->channel);
363
364 /* Send the read byte command */
365 ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_READ_BYTE);
366
367 /* Wait until 1WB == 0 */
368 ds2482_wait_1wire_idle(pdev);
369
370 /* Select the data register */
371 ds2482_select_register(pdev, DS2482_PTR_CODE_DATA);
372
373 /* Read the data byte */
374 result = i2c_smbus_read_byte(pdev->client);
375
376 mutex_unlock(&pdev->access_lock);
377
378 return result;
379}
380
381
382/**
383 * Sends a reset on the 1-wire interface
384 *
385 * @param data The ds2482 channel pointer
386 * @return 0=Device present, 1=No device present or error
387 */
388static u8 ds2482_w1_reset_bus(void *data)
389{
390 struct ds2482_w1_chan *pchan = data;
391 struct ds2482_data *pdev = pchan->pdev;
392 int err;
393 u8 retval = 1;
394
395 mutex_lock(&pdev->access_lock);
396
397 /* Select the channel */
398 ds2482_wait_1wire_idle(pdev);
399 if (pdev->w1_count > 1)
400 ds2482_set_channel(pdev, pchan->channel);
401
402 /* Send the reset command */
403 err = ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_RESET);
404 if (err >= 0) {
405 /* Wait until the reset is complete */
406 err = ds2482_wait_1wire_idle(pdev);
407 retval = !(err & DS2482_REG_STS_PPD);
408
409 /* If the chip did reset since detect, re-config it */
410 if (err & DS2482_REG_STS_RST)
411 ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG,
412 0xF0);
413 }
414
415 mutex_unlock(&pdev->access_lock);
416
417 return retval;
418}
419
420static int ds2482_suspend(struct device *dev)
421{
422 struct i2c_client *client = to_i2c_client(dev);
423 struct ds2482_data *data = i2c_get_clientdata(client);
424
425 if (data->slpz_gpio >= 0)
426 gpio_set_value(data->slpz_gpio, 0);
427 return 0;
428}
429
430static int ds2482_resume(struct device *dev)
431{
432 struct i2c_client *client = to_i2c_client(dev);
433 struct ds2482_data *data = i2c_get_clientdata(client);
434
435 if (data->slpz_gpio >= 0)
436 gpio_set_value(data->slpz_gpio, 1);
437 return 0;
438}
439
440static int ds2482_probe(struct i2c_client *client,
441 const struct i2c_device_id *id)
442{
443 struct ds2482_data *data;
444 struct ds2482_platform_data *pdata;
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, 0xF0);
486
487 mutex_init(&data->access_lock);
488
489 /* Register 1-wire interface(s) */
490 for (idx = 0; idx < data->w1_count; idx++) {
491 data->w1_ch[idx].pdev = data;
492 data->w1_ch[idx].channel = idx;
493
494 /* Populate all the w1 bus master stuff */
495 data->w1_ch[idx].w1_bm.data = &data->w1_ch[idx];
496 data->w1_ch[idx].w1_bm.read_byte = ds2482_w1_read_byte;
497 data->w1_ch[idx].w1_bm.write_byte = ds2482_w1_write_byte;
498 data->w1_ch[idx].w1_bm.touch_bit = ds2482_w1_touch_bit;
499 data->w1_ch[idx].w1_bm.triplet = ds2482_w1_triplet;
500 data->w1_ch[idx].w1_bm.reset_bus = ds2482_w1_reset_bus;
501
502 err = w1_add_master_device(&data->w1_ch[idx].w1_bm);
503 if (err) {
504 data->w1_ch[idx].pdev = NULL;
505 goto exit_w1_remove;
506 }
507 }
508
509 pdata = client->dev.platform_data;
510 data->slpz_gpio = pdata ? pdata->slpz_gpio : -1;
511
512 if (data->slpz_gpio >= 0) {
513 err = gpio_request_one(data->slpz_gpio, GPIOF_OUT_INIT_HIGH,
514 "ds2482.slpz");
515 if (err < 0)
516 goto exit_w1_remove;
517 }
518
519 return 0;
520
521exit_w1_remove:
522 for (idx = 0; idx < data->w1_count; idx++) {
523 if (data->w1_ch[idx].pdev != NULL)
524 w1_remove_master_device(&data->w1_ch[idx].w1_bm);
525 }
526exit_free:
527 kfree(data);
528exit:
529 return err;
530}
531
532static int ds2482_remove(struct i2c_client *client)
533{
534 struct ds2482_data *data = i2c_get_clientdata(client);
535 int idx;
536
537 /* Unregister the 1-wire bridge(s) */
538 for (idx = 0; idx < data->w1_count; idx++) {
539 if (data->w1_ch[idx].pdev != NULL)
540 w1_remove_master_device(&data->w1_ch[idx].w1_bm);
541 }
542
543 if (data->slpz_gpio >= 0) {
544 gpio_set_value(data->slpz_gpio, 0);
545 gpio_free(data->slpz_gpio);
546 }
547
548 /* Free the memory */
549 kfree(data);
550 return 0;
551}
552
553static int __init sensors_ds2482_init(void)
554{
555 return i2c_add_driver(&ds2482_driver);
556}
557
558static void __exit sensors_ds2482_exit(void)
559{
560 i2c_del_driver(&ds2482_driver);
561}
562
563MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
564MODULE_DESCRIPTION("DS2482 driver");
565MODULE_LICENSE("GPL");
566
567module_init(sensors_ds2482_init);
568module_exit(sensors_ds2482_exit);